import {z} from 'zod' export type GetApiPotsPotIdFactsArgs = {pot_id: string, min_pot_score?: (number | null) | undefined, max_pot_score?: (number | null) | undefined, document_id?: (string | null) | undefined, domain?: (string | null) | undefined, status?: (string | null) | undefined, tags?: Array | undefined, sort_by?: ('created_at' | 'updated_at' | 'pot_score') | undefined, sort_order?: ('asc' | 'desc') | undefined, limit?: number | undefined, cursor?: (string | null) | undefined, include_total?: boolean | undefined}; export type GetApiPotsPotIdFactsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const potScoreLevel = z.enum(["CONSTITUTION", "VERIFIED", "SUPPORTED", "INFERRED", "HYPOTHESIS", "SPECULATION"]); export const provenance = z.object({type: z.string(), sources: z.array(z.record(z.string(), z.unknown())).optional(), derivation: z.record(z.string(), z.unknown()).nullable().optional(), profile_id: z.string().nullable().optional(), profile_version: z.number().int().gte(1).nullable().optional()}); export const classification = z.object({domain: z.string().optional(), subdomain: z.string().nullable().optional(), tags: z.array(z.string()).optional(), entity_mentions: z.array(z.string()).optional()}); export const documentLocation = z.object({page_number: z.number().int().gte(1).nullable().optional(), section_path: z.string().nullable().optional(), source_text: z.string().max(500).nullable().optional(), sheet_name: z.string().nullable().optional(), row_number: z.number().int().gte(1).nullable().optional(), cell_ref: z.string().nullable().optional()}); export const factPii = z.object({applied: z.boolean(), mode: z.enum(["off", "anonymize", "block"]), entities: z.record(z.string(), z.number().int()).optional(), coverage: z.enum(["full", "partial", "none"]).optional(), language: z.string().nullable().optional(), unmasked_content: z.string().nullable().optional()}); export const factWarning = z.object({pattern: z.string(), message: z.string(), detail: z.string().nullable().optional(), probability: z.number().nullable().optional()}); export const edgeTypeCounts = z.object({in: z.number().int().gte(0).optional(), out: z.number().int().gte(0).optional()}); export const edgeSummary = z.object({total_in: z.number().int().gte(0).optional(), total_out: z.number().int().gte(0).optional(), by_type: z.record(z.string(), edgeTypeCounts).optional()}); export const factExtended = z.object({id: z.string(), pot_id: z.string(), workspace_id: z.string().nullable().optional(), content: z.string().min(1), title: z.string().max(80).nullable().optional(), content_hash: z.string().nullable().optional(), pot_score: z.number().gte(0).lte(1), original_pot_score: z.number().gte(0).lte(1).nullable().optional(), level: potScoreLevel, embedding_model: z.string().nullable().optional(), provenance: provenance, classification: classification.optional(), status: z.string().optional(), validation_status: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), valid_from: z.string().nullable().optional(), valid_from_source: z.enum(["document_effective", "document_date", "contributor", "imported", "curator"]).nullable().optional(), document_id: z.string().nullable().optional(), language: z.string().nullable().optional(), location: documentLocation.nullable().optional(), scope_inferred: z.enum(["own", "third_party", "mixed_context", "unknown"]).nullable().optional(), scope_confidence: z.number().gte(0).lte(1).nullable().optional(), scope_reason: z.string().max(500).nullable().optional(), context_attachment_ids_used: z.array(z.string()).nullable().optional(), pii: factPii.nullable().optional(), version: z.number().int().optional(), flags: z.array(z.string()).optional(), notes: z.string().nullable().optional(), warnings: z.array(factWarning).optional(), edge_summary: edgeSummary.optional()}).and(z.record(z.string(), z.unknown())); export const factListResponse = z.object({pot_id: z.string(), facts: z.array(factExtended), total: z.number().int().nullable().optional(), limit: z.number().int(), next_cursor: z.string().nullable().optional(), has_more: z.boolean()}); 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 facts in a POT * * GET /pots/{pot_id}/facts */ export const getApiPotsPotIdFacts = async (args: GetApiPotsPotIdFactsArgs, options: GetApiPotsPotIdFactsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.min_pot_score != null) query.append("min_pot_score", String(args.min_pot_score)) if (args.max_pot_score != null) query.append("max_pot_score", String(args.max_pot_score)) if (args.document_id != null) query.append("document_id", String(args.document_id)) if (args.domain != null) query.append("domain", String(args.domain)) if (args.status != null) query.append("status", String(args.status)) for (const item of args.tags ?? []) query.append("tags", String(item)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.include_total != null) query.append("include_total", String(args.include_total)) const url = `${baseUrl.replace(/\/+$/, '')}/pots/${encodeURIComponent(String(args.pot_id))}/facts${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(factListResponse, await res.json()) };