import {z} from 'zod' export type GetApiV1HitlHandoffsArgs = {conversation_id?: (string | null) | undefined, status?: (string | null) | undefined, cursor?: (string | null) | undefined, limit?: number | undefined}; export type GetApiV1HitlHandoffsOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const transcriptPtr = z.object({conversation_id: z.string(), up_to_seq: z.number().int().nullable().optional()}); export const handoffPacket = z.object({static_text: z.string(), reason: z.string().nullable().optional(), context_vars: z.record(z.string(), z.unknown()).optional(), summary: z.string().nullable().optional(), transcript_ptr: transcriptPtr.nullable().optional(), transfer_to: z.string().nullable().optional(), transfer_label: z.string().nullable().optional()}); export const handoffRead = z.object({id: z.string(), organization_id: z.string(), conversation_id: z.string(), conv_turn_id: z.string().nullable(), reason: z.string().nullable(), static_text: z.string(), status: z.enum(["requested", "assigned", "accepted", "resolved", "cancelled"]), assignee: z.string().nullable(), source: z.enum(["workflow_terminal", "router", "policy", "call_transfer", "warm_call_transfer"]), packet: handoffPacket, created_at: z.string(), resolved_at: z.string().nullable()}); export const handoffPage = z.object({items: z.array(handoffRead), next_cursor: z.string().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 }; /** * List handoffs * * GET /v1/hitl/handoffs */ export const getApiV1HitlHandoffs = async (args: GetApiV1HitlHandoffsArgs, options: GetApiV1HitlHandoffsOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.conversation_id != null) query.append("conversation_id", String(args.conversation_id)) if (args.status != null) query.append("status", String(args.status)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/hitl/handoffs${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(handoffPage, await res.json()) };