import {z} from 'zod' export type GetApiApiV1ChatAiConversationsConversationIdTasksTaskIdArgs = {conversation_id: string, task_id: string}; export type GetApiApiV1ChatAiConversationsConversationIdTasksTaskIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const subAgentTraceEntryRead = z.object({agent_label: z.string(), tool_name: z.string(), tool_call_id: z.string(), status: z.enum(["running", "completed", "failed"]), elapsed_ms: z.number().int(), tool_params: z.record(z.string(), z.unknown()).nullable().optional(), follow_up_action_id: z.string().nullable().optional(), kc_review_id: z.string().nullable().optional()}); export const chatTaskDetail = z.object({id: z.string(), specialist: z.string(), status: z.enum(["queued", "running", "succeeded", "failed", "stalled", "budget_exhausted"]), status_message: z.string().nullable(), current_step: z.string().nullable(), created_at: z.string(), updated_at: z.string(), started_at: z.string().nullable(), heartbeat_at: z.string().nullable(), tool_count: z.number().int(), question: z.string().nullable(), trace: z.array(subAgentTraceEntryRead), answer: z.string().nullable(), error: z.string().nullable(), sources: z.array(z.record(z.string(), z.unknown())).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 Conversation Task * * GET /api/v1/chat-ai/conversations/{conversation_id}/tasks/{task_id} */ export const getApiApiV1ChatAiConversationsConversationIdTasksTaskId = async (args: GetApiApiV1ChatAiConversationsConversationIdTasksTaskIdArgs, options: GetApiApiV1ChatAiConversationsConversationIdTasksTaskIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/chat-ai/conversations/${encodeURIComponent(String(args.conversation_id))}/tasks/${encodeURIComponent(String(args.task_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(chatTaskDetail, await res.json()) };