import {z} from 'zod' export type MakeCallPayload = {to: string, prompt_dynamic_variables?: ({[key: string]: unknown} | null) | undefined}; export type CreateApiApiV1CallWidgetIdArgs = {widget_id: string, body: MakeCallPayload}; export type CreateApiApiV1CallWidgetIdOptions = {baseUrl: string, YourAPIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const telephonyProviderEnum = z.enum(["twilio", "plivo", "telnyx", "sip"]); export const telephonyProviderCallDetails = z.object({provider_name: telephonyProviderEnum, call_id: z.string(), response_body: z.record(z.string(), z.unknown())}); export const call = z.object({conversation_id: z.string(), contact_id: z.string(), widget_id: z.string(), assistant_id: z.string(), direction: z.string(), from_number: z.string(), to_number: z.string(), telephony_provider: telephonyProviderCallDetails, prompt_dynamic_variables: z.record(z.string(), z.unknown()).nullable().optional()}); export const iPostResponseBaseCall = z.object({message: z.string().nullable().optional(), meta: z.union([z.record(z.string(), z.unknown()), z.unknown()]).nullable().optional(), data: call.nullable().optional()}); 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 }; /** * Make a call * * POST /api/v1/call/{widget_id} * * Also accepts auth: HTTPBearer */ export const createApiApiV1CallWidgetId = async (args: CreateApiApiV1CallWidgetIdArgs, options: CreateApiApiV1CallWidgetIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, YourAPIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (YourAPIKey !== undefined) query.set("api_key", YourAPIKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/call/${encodeURIComponent(String(args.widget_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(iPostResponseBaseCall, await res.json()) };