import {z} from 'zod' export type GetApiV1ConversationsConversationIdTraceArgs = {conversation_id: string, turn_id?: (string | null) | undefined, cursor?: (string | null) | undefined, limit?: number | undefined, category?: ('product' | 'engineering' | 'all') | undefined}; export type GetApiV1ConversationsConversationIdTraceOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const traceTruncation = z.object({path: z.string(), reason: z.string().nullable().optional(), scope: z.string().nullable().optional(), original_bytes: z.number().int().nullable().optional(), stored_bytes: z.number().int().nullable().optional(), original_chars: z.number().int().nullable().optional(), original_type: z.string().nullable().optional(), original_leaves: z.number().int().nullable().optional(), stored_leaves: z.number().int().nullable().optional(), omitted_entries: z.number().int().nullable().optional()}); export const traceStep = z.object({kind: z.string(), actor: z.enum(["Customer", "Agent", "System"]), title: z.string(), summary: z.string().nullable().optional(), status: z.enum(["Succeeded", "Failed", "Skipped", "In Progress"]), started_at: z.string(), ended_at: z.string().nullable().optional(), duration_ms: z.number().int().nullable().optional(), turn_id: z.string().nullable().optional(), details: z.record(z.string(), z.unknown()).optional(), observation_ids: z.array(z.string()).optional(), truncation: z.array(traceTruncation).optional()}); export const sessionTraceResponse = z.object({steps: z.array(traceStep), 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 }; /** * Get the customer-readable trace for a v2 conversation * * GET /v1/conversations/{conversation_id}/trace */ export const getApiV1ConversationsConversationIdTrace = async (args: GetApiV1ConversationsConversationIdTraceArgs, options: GetApiV1ConversationsConversationIdTraceOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.turn_id != null) query.append("turn_id", String(args.turn_id)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.category != null) query.append("category", String(args.category)) if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/conversations/${encodeURIComponent(String(args.conversation_id))}/trace${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(sessionTraceResponse, await res.json()) };