import {z} from 'zod' export type GetApiKnowledgeGapsGapIdArgs = {gap_id: string}; export type GetApiKnowledgeGapsGapIdOptions = {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 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 a knowledge gap * * GET /knowledge-gaps/{gap_id} */ export const getApiKnowledgeGapsGapId = async (args: GetApiKnowledgeGapsGapIdArgs, options: GetApiKnowledgeGapsGapIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/knowledge-gaps/${encodeURIComponent(String(args.gap_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(knowledgeGap, await res.json()) };