import {z} from 'zod' export type GetApiV1HitlHandoffsHandoffIdArgs = {handoff_id: string}; export type GetApiV1HitlHandoffsHandoffIdOptions = {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 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 }; /** * Get a handoff * * GET /v1/hitl/handoffs/{handoff_id} */ export const getApiV1HitlHandoffsHandoffId = async (args: GetApiV1HitlHandoffsHandoffIdArgs, options: GetApiV1HitlHandoffsHandoffIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/hitl/handoffs/${encodeURIComponent(String(args.handoff_id))}${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(handoffRead, await res.json()) };