import {z} from 'zod' export type TraceKind = 'decision' | 'retrieval' | 'response'; export type GetApiApiV1ContextIdAuditArgs = {context_id: string, principal?: string | undefined, key?: string | undefined, kind?: TraceKind | undefined, since?: string | undefined, until?: string | undefined, limit?: number | undefined, cursor?: string | undefined}; export type GetApiApiV1ContextIdAuditOptions = {baseUrl: string, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const pageMeta = z.object({hasMore: z.boolean(), nextCursor: z.string().nullable().optional(), totalSize: z.number().int().gte(0).nullable().optional()}); export const traceKind = z.enum(["decision", "retrieval", "response"]); export const auditRowJson = z.object({cost: z.number(), createdAt: z.string(), kind: traceKind, latencyMs: z.number().int(), model: z.string().nullable().optional(), principal: z.string().nullable().optional(), rowsTouched: z.number().int(), traceId: z.string()}); export const auditResponseJson = z.object({page: pageMeta, rows: z.array(auditRowJson)}); 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 }; /** * Audit view over the trace graph. Returns recent traces with principal/key/time/kind filters. * * GET /api/v1/{context_id}/audit */ export const getApiApiV1ContextIdAudit = async (args: GetApiApiV1ContextIdAuditArgs, options: GetApiApiV1ContextIdAuditOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.principal != null) query.append("principal", String(args.principal)) if (args.key != null) query.append("key", String(args.key)) if (args.kind != null) query.append("kind", String(args.kind)) if (args.since != null) query.append("since", String(args.since)) if (args.until != null) query.append("until", String(args.until)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/${encodeURIComponent(String(args.context_id))}/audit${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(auditResponseJson, await res.json()) };