import {z} from 'zod' export type CreateApiRecordChatMessageChatIdArgs = {chat_id: string, body: {role: 'user', message: string, idempotency_key: string} | {role: 'transfer_target', message: string, idempotency_key: string, metadata?: ({transfer_target_name?: string | undefined}) | undefined}}; export type CreateApiRecordChatMessageChatIdOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiRecordChatMessageChatIdResponse = z.object({message_id: z.string(), created_timestamp: z.number().int()}); 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 }; /** * Record a user or human transfer-target message in an ongoing escalated chat. SMS chats accept only transfer_target and send SMS before recording. API chats only record; delivery is the customer's responsibility. Idempotency keys are scoped to the organization. Successful results are retained for 24 hours. Concurrent requests with the same key wait for the lock. Reusing a key after success returns 200 with the original message_id and created_timestamp, without sending again. No duplicate-key error is raised. Failures are not cached and release the lock so the same key can be retried. Each execution makes one SMS send attempt. If delivery is uncertain or processing fails after sending, a retried request can send the SMS again and cause duplicates. * * POST /record-chat-message/{chat_id} */ export const createApiRecordChatMessageChatId = async (args: CreateApiRecordChatMessageChatIdArgs, options?: CreateApiRecordChatMessageChatIdOptions) => { const {baseUrl = "https://api.retellai.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/record-chat-message/${encodeURIComponent(String(args.chat_id))}${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(createApiRecordChatMessageChatIdResponse, await res.json()) };