import {z} from 'zod' export type ToolCallFunction = {name: string, arguments: {[key: string]: unknown}}; export type ToolCallItem = {id: string, type?: 'function' | undefined, function: ToolCallFunction, isPrecededByText?: (boolean | null) | undefined}; export type VapiCustomer = {number?: (string | null) | undefined, name?: (string | null) | undefined, email?: (string | null) | undefined}; export type VapiCall = {id: string, orgId?: (string | null) | undefined, status?: (string | null) | undefined, assistantId?: (string | null) | undefined, customer?: (VapiCustomer | null) | undefined}; export type VapiModel = {model: string, provider: string, systemPrompt?: (string | null) | undefined, temperature?: (number | null) | undefined}; export type VapiAssistant = {name?: (string | null) | undefined, model?: (VapiModel | null) | undefined, voice?: ({[key: string]: unknown} | null) | undefined, firstMessage?: (string | null) | undefined}; export type ToolCallMessage = {timestamp?: (number | null) | undefined, meta?: ({[key: string]: unknown} | null) | undefined, type: 'tool-calls', toolCalls?: (Array | null) | undefined, toolCallList?: (Array | null) | undefined, toolWithToolCallList?: (Array<{[key: string]: unknown}> | null) | undefined, toolCallId?: (string | null) | undefined, toolName?: (string | null) | undefined, arguments?: ({[key: string]: unknown} | null) | undefined, call?: (VapiCall | null) | undefined, assistant?: (VapiAssistant | null) | undefined} | {[key: string]: unknown}; export type AssistantRequestMessage = {timestamp?: (number | null) | undefined, meta?: ({[key: string]: unknown} | null) | undefined, type: 'assistant-request', call: VapiCall, assistant?: (VapiAssistant | null) | undefined}; export type TranscriptMessage = {timestamp?: (number | null) | undefined, meta?: ({[key: string]: unknown} | null) | undefined, type: 'transcript', role: 'assistant' | 'user', transcript: string, transcriptType?: ('partial' | 'final' | null) | undefined, call: VapiCall}; export type StatusUpdateMessage = {timestamp?: (number | null) | undefined, meta?: ({[key: string]: unknown} | null) | undefined, type: 'status-update', status: string, call: VapiCall}; export type EndOfCallReportMessage = {timestamp?: (number | null) | undefined, meta?: ({[key: string]: unknown} | null) | undefined, type: 'end-of-call-report', call: VapiCall, transcript?: (string | null) | undefined, summary?: (string | null) | undefined, recordingUrl?: (string | null) | undefined, endedReason?: (string | null) | undefined, cost?: (number | null) | undefined, durationSeconds?: (number | null) | undefined}; export type UnknownVapiMessage = {timestamp?: (number | null) | undefined, meta?: ({[key: string]: unknown} | null) | undefined, type: string, raw?: {[key: string]: unknown} | undefined} | {[key: string]: unknown}; export type VapiWebhookRequest = {message: ToolCallMessage | AssistantRequestMessage | TranscriptMessage | StatusUpdateMessage | EndOfCallReportMessage | UnknownVapiMessage}; export type CreateApiApiVapiToolsToolsTestArgs = {body: VapiWebhookRequest}; export type CreateApiApiVapiToolsToolsTestOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const createApiApiVapiToolsToolsTestResponse = z.unknown(); 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 }; /** * Handle Vapi Tools Test * * POST /api/vapi/tools/tools-test */ export const createApiApiVapiToolsToolsTest = async (args: CreateApiApiVapiToolsToolsTestArgs, options: CreateApiApiVapiToolsToolsTestOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/vapi/tools/tools-test${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(createApiApiVapiToolsToolsTestResponse, await res.json()) };