import {z} from 'zod' export type GetApiKnowledgeGapsArgs = {state?: ('open' | 'routed' | 'ingested' | 'resolved' | 'expired' | 'dismissed' | 'duplicate' | 'reopened' | null) | undefined, updated_since?: (string | null) | undefined, limit?: number | undefined, cursor?: (string | null) | undefined}; export type GetApiKnowledgeGapsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const gapTransition = z.object({from_state: z.enum(["open", "routed", "ingested", "resolved", "expired", "dismissed", "duplicate", "reopened"]), to_state: z.enum(["open", "routed", "ingested", "resolved", "expired", "dismissed", "duplicate", "reopened"]), actor: z.string(), at: z.string().optional(), note: z.string().max(1000).nullable().optional(), evidence_ref: z.string().max(200).nullable().optional()}); export const gapAnswer = z.object({answer_id: z.string().optional(), actor: z.string().min(1).max(200), contributor_id: z.string().nullable().optional(), completeness: z.enum(["partial", "complete"]), fact_ids: z.array(z.string()), note: z.string().max(1000).nullable().optional(), at: z.string().optional()}); export const knowledgeGap = z.object({gap_id: z.string().optional(), workspace_id: z.string(), pot_id: z.string(), query_text: z.string().min(1).max(4000), context: z.string().max(4000).nullable().optional(), requester_id: z.string().min(1).max(200), domain: z.string().min(1).max(200), trigger: z.enum(["low_confidence", "no_coverage", "manual"]).optional(), trigger_score: z.number().gte(0).lte(1).nullable().optional(), state: z.enum(["open", "routed", "ingested", "resolved", "expired", "dismissed", "duplicate", "reopened"]).optional(), assigned_to: z.string().max(200).nullable().optional(), resolved_cluster_id: z.string().nullable().optional(), capsule_id: z.string().nullable().optional(), idempotency_key: z.string().max(200).nullable().optional(), transitions: z.array(gapTransition).optional(), answers: z.array(gapAnswer).optional(), timeout_at: z.string().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), resolved_at: z.string().nullable().optional()}); export const knowledgeGapListResponse = z.object({data: z.array(knowledgeGap), 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 }; /** * List knowledge gaps (workspace queue) * * GET /knowledge-gaps */ export const getApiKnowledgeGaps = async (args: GetApiKnowledgeGapsArgs, options: GetApiKnowledgeGapsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.state != null) query.append("state", String(args.state)) if (args.updated_since != null) query.append("updated_since", String(args.updated_since)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) const url = `${baseUrl.replace(/\/+$/, '')}/knowledge-gaps${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(knowledgeGapListResponse, await res.json()) };