import {z} from 'zod' export type GetApiApiV0AgentTracesSessionsSessionIdArgs = {session_id: string}; export type GetApiApiV0AgentTracesSessionsSessionIdOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const traceUserResponse = z.object({id: z.string(), name: z.string().nullable().optional(), email: z.string().nullable().optional(), department: z.string().nullable().optional(), is_viewer: z.boolean().optional()}); export const agentAttachmentGetResponse = z.object({id: z.string(), filename: z.string(), content_type: z.string(), size_bytes: z.number().int(), url: z.string().nullable().optional()}); export const kgSourceRef = z.object({document_public_id: z.string(), document_name: z.string().nullable().optional(), page_number: z.number().int().nullable().optional()}); export const kgNodeRef = z.object({node_id: z.string(), name: z.string(), type_id: z.string().nullable().optional(), type_name: z.string().nullable().optional(), ontology: z.string().nullable().optional(), tier: z.string().nullable().optional(), attributes: z.record(z.string(), z.unknown()).optional(), attribute_sources: z.record(z.string(), z.array(kgSourceRef)).optional()}); export const kgResultRefs = z.object({knowledge_base_id: z.string(), nodes: z.array(kgNodeRef).optional(), groundings: z.array(kgSourceRef).optional()}); export const agentMessageFeedbackResponse = z.object({id: z.string(), rating: z.string(), reason: z.string().nullable().optional(), comment: z.string().nullable().optional(), created_at: z.string(), updated_at: z.string()}); export const agentMessageGetResponse = z.object({id: z.string(), run_id: z.string().nullable().optional(), role: z.string(), content: z.string().nullable().optional(), tool_calls: z.array(z.record(z.string(), z.unknown())).nullable().optional(), tool_call_id: z.string().nullable().optional(), attachments: z.array(agentAttachmentGetResponse).nullable().optional(), model_names: z.array(z.string()).nullable().optional(), kg_refs: kgResultRefs.nullable().optional(), feedback: agentMessageFeedbackResponse.nullable().optional(), sequence_number: z.number().int(), created_at: z.string()}); export const agentModelCallGetResponse = z.object({id: z.string(), run_id: z.string(), call_number: z.number().int(), provider: z.string(), model_name: z.string(), status: z.string(), response_text: z.string().nullable(), reasoning_summary: z.string().nullable(), reasoning_content: z.string().nullable(), finish_reason: z.string().nullable(), request_id: z.string().nullable(), response_id: z.string().nullable(), input_tokens: z.number().int().nullable(), cached_input_tokens: z.number().int().nullable(), output_tokens: z.number().int().nullable(), reasoning_tokens: z.number().int().nullable(), total_tokens: z.number().int().nullable(), estimated_cost_usd: z.number().nullable(), citations: z.array(z.record(z.string(), z.unknown())).nullable(), safety: z.record(z.string(), z.unknown()).nullable(), attempts: z.array(z.record(z.string(), z.unknown())).nullable(), provider_metadata: z.record(z.string(), z.unknown()).nullable(), provider_payload: z.record(z.string(), z.unknown()).nullable(), error_message: z.string().nullable(), started_at: z.string(), completed_at: z.string().nullable(), duration_ms: z.number().int().nullable()}); export const agentTraceSummaryResponse = z.object({id: z.string(), session_id: z.string(), session_name: z.string().nullable(), persona_id: z.string().nullable(), persona_name: z.string().nullable(), parent_run_id: z.string().nullable(), status: z.string(), model_name: z.string().nullable(), input_message: z.string(), final_response: z.string().nullable(), total_iterations: z.number().int(), message_count: z.number().int(), tool_call_count: z.number().int(), error_message: z.string().nullable(), created_at: z.string(), completed_at: z.string().nullable(), duration_ms: z.number().int().nullable()}); export const agentConversationTraceResponse = z.object({user: traceUserResponse.nullable().optional(), messages: z.array(agentMessageGetResponse), model_calls: z.array(agentModelCallGetResponse), runs: z.array(agentTraceSummaryResponse), delegated_runs: z.array(agentTraceSummaryResponse)}); 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 trace * * GET /api/v0/agent/traces/sessions/{session_id} */ export const getApiApiV0AgentTracesSessionsSessionId = async (args: GetApiApiV0AgentTracesSessionsSessionIdArgs, options: GetApiApiV0AgentTracesSessionsSessionIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v0/agent/traces/sessions/${encodeURIComponent(String(args.session_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(agentConversationTraceResponse, await res.json()) };