import {z} from 'zod' export type TestSessionFeatureInput = {identity_resolution?: boolean | undefined, memory?: boolean | undefined, summarization?: boolean | undefined, extraction?: boolean | undefined, evaluations?: boolean | undefined, knowledge_gap_analysis?: boolean | undefined}; export type WebCallRequest = {agent_id?: (string | null) | undefined, assistant_revision_id?: (string | null) | undefined, team_id?: (string | null) | undefined, context_variables?: ({[key: string]: unknown} | null) | undefined, test_features?: (TestSessionFeatureInput | null) | undefined}; export type CreateApiV1VoiceWebCallArgs = {body: WebCallRequest}; export type CreateApiV1VoiceWebCallOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const testSessionFeatures = z.object({version: z.literal(1).optional(), identity_resolution: z.boolean(), memory: z.boolean(), summarization: z.boolean(), extraction: z.boolean(), evaluations: z.boolean(), knowledge_gap_analysis: z.boolean()}); export const webCallResponse = z.object({livekit_url: z.string(), access_token: z.string(), room_name: z.string(), session_id: z.string(), end_user_id: z.string(), test_features: testSessionFeatures}); 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 }; /** * Web Call * * POST /v1/voice/web-call */ export const createApiV1VoiceWebCall = async (args: CreateApiV1VoiceWebCallArgs, options: CreateApiV1VoiceWebCallOptions) => { const {baseUrl, 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("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/voice/web-call${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(webCallResponse, await res.json()) };