import {z} from 'zod' export type Header = {key: string, value: string}; export type LinkedCrmRecord = {id: string, code: string}; export type GladiaTranscriptionSettings = {enableDiarization?: boolean | undefined}; export type TranscriptionSettings = {gladia?: GladiaTranscriptionSettings | undefined}; export type TranscriptV1Item = {speaker?: string | undefined, sentence?: string | undefined, startTimestamp: number, endTimestamp?: number | undefined}; export type TranscriptV1Input = {pieceInFlux: string, final: Array}; export type Word = {text: string, startTimestamp: number, endTimestamp: number}; export type TranscriptV2Item = {speaker: string, words: Array}; export type TranscriptV2Input = {pieceInFlux: string, final: Array}; export type TranscriptInput = {v1?: TranscriptV1Input | undefined, v2?: TranscriptV2Input | undefined}; export type ExternalMetadataParticipant = {email: string, name: string, accessType: 'Internal' | 'External', organizer: boolean}; export type ExternalMetadata = {participants?: Array | undefined, extra?: {[key: string]: unknown} | undefined}; export type ImportConversationRequest = {mediaURL: string, userID: string, applicationName?: string | undefined, applicationExternalID?: string | undefined, conversationTitle?: string | undefined, conversationStartedAt?: string | undefined, headers?: Array
| undefined, opportunityID?: string | undefined, linkedCrmRecords?: Array | undefined, skipScorecardCalculation?: boolean | undefined, skipCRMFieldsCalculation?: boolean | undefined, skipOpportunitiesExport?: boolean | undefined, transcriptionSettings?: TranscriptionSettings | undefined, transcript?: TranscriptInput | undefined, externalMetadata?: ExternalMetadata | undefined}; export type CreateApiConversationsImportArgs = {body: ImportConversationRequest}; export type CreateApiConversationsImportOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const importConversationResponse = z.object({uuid: z.string()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Import Conversation * * POST /conversations/import */ export const createApiConversationsImport = async (args: CreateApiConversationsImportArgs, options?: CreateApiConversationsImportOptions) => { const {baseUrl = "https://api.attention.tech/v2", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("Authorization", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/conversations/import${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(importConversationResponse, await res.json()) };