import {z} from 'zod' export type PotScoreLevel = 'CONSTITUTION' | 'VERIFIED' | 'SUPPORTED' | 'INFERRED' | 'HYPOTHESIS' | 'SPECULATION'; export type ScipotApiSchemasQuerySearchFiltersRequest = {domain?: (string | null) | undefined, levels?: (Array | null) | undefined, tags?: (Array | null) | undefined}; export type QueryRequest = {query: string, top_k?: number | undefined, strategy?: ('auto' | 'cag' | 'rag' | 'graph_rag') | undefined, enable_fallback?: boolean | undefined, filters?: (ScipotApiSchemasQuerySearchFiltersRequest | null) | undefined, include_edges?: boolean | undefined, include_stances?: boolean | undefined, as_of?: (string | null) | undefined}; export type CreateApiPotsPotIdQueryArgs = {pot_id: string, body: QueryRequest}; export type CreateApiPotsPotIdQueryOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const restatedByRef = z.object({fact_id: z.string(), document_id: z.string().nullable().optional(), source_type: z.string().nullable().optional(), document_date: z.string().nullable().optional(), locator: z.string().nullable().optional(), partial: z.boolean().optional()}); export const potScoreLevel = z.enum(["CONSTITUTION", "VERIFIED", "SUPPORTED", "INFERRED", "HYPOTHESIS", "SPECULATION"]); export const queryEdgeItem = z.object({from_fact_id: z.string(), to_fact_id: z.string(), type: z.string(), weight: z.number().gte(0).lte(1), rationale: z.string().optional(), peer_fact_id: z.string().nullable().optional(), peer_content_snippet: z.string().nullable().optional(), peer_pot_score: z.number().gte(0).lte(1).nullable().optional(), resolution_type: z.string().nullable().optional(), resolution_notes: z.string().nullable().optional()}); export const factProvenance = z.object({type: z.string(), sources: z.array(z.record(z.string(), z.unknown())).optional(), profile_id: z.string().nullable().optional(), profile_version: z.number().int().nullable().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 queryResultItem = z.object({restated_by: z.array(restatedByRef).optional(), restates: z.string().nullable().optional(), restates_all: z.array(z.string()).optional(), source_authority: z.string().nullable().optional(), source_date: z.string().nullable().optional(), source_date_basis: z.enum(["original_date", "created_at", "fact_created_at"]).nullable().optional(), currency: z.enum(["current", "pending", "future", "not_current"]).optional(), valid_until: z.string().nullable().optional(), superseded_at: z.string().nullable().optional(), currency_reason: z.record(z.string(), z.unknown()).nullable().optional(), fact_id: z.string(), content: z.string(), title: z.string().nullable().optional(), score: z.number().gte(0).lte(1), pot_score: z.number().gte(0).lte(1), original_pot_score: z.number().nullable().optional(), level: potScoreLevel, source: z.enum(["cag", "rag", "graph_rag"]), match_details: z.record(z.string(), z.unknown()), domain: z.string().nullable().optional(), tags: z.array(z.string()).optional(), document_id: z.string().nullable().optional(), edges: z.array(queryEdgeItem).optional(), superseded: z.boolean().optional(), superseded_by: z.array(z.string()).optional(), valid_from: z.string().nullable().optional(), provenance: factProvenance.nullable().optional(), location: documentLocation.nullable().optional(), gap_evidence: z.boolean().nullable().optional()}); export const stanceType = z.enum(["confirm_a", "confirm_b", "dismiss"]); export const seniorityLevel = z.enum(["junior", "mid", "senior", "lead", "principal", "executive"]); export const contributorProfile = z.object({name: z.string().min(1).max(200), role: z.string().min(1).max(200), seniority_level: seniorityLevel, department: z.string().max(200).nullable().optional(), expertise_areas: z.array(z.string()).optional(), bio: z.string().max(1000).nullable().optional()}); export const expertiseEvaluation = z.object({relevance_score: z.number().gte(0).lte(1), method: z.string(), matched_areas: z.array(z.string()).optional(), reasoning: z.string().nullable().optional()}); export const contradictionStance = z.object({id: z.string().optional(), edge_id: z.string(), pot_id: z.string(), stance: stanceType, contributor: contributorProfile, contributor_id: z.string().nullable().optional(), expertise_evaluation: expertiseEvaluation, notes: z.string().max(2000).nullable().optional(), score_delta: z.number().optional(), score_before: z.number().optional(), score_after: z.number().optional(), affected_fact_id: z.string().nullable().optional(), opposing_score_delta: z.number().optional(), opposing_fact_id: z.string().nullable().optional(), created_at: z.string().optional()}); export const stancesSummary = z.object({confirm_a_count: z.number().int().optional(), confirm_b_count: z.number().int().optional(), dismiss_count: z.number().int().optional(), total: z.number().int().optional(), latest_stances: z.array(contradictionStance).optional()}); export const contradictionPair = z.object({fact_a_id: z.string(), fact_a_content_snippet: z.string(), fact_a_pot_score: z.number().gte(0).lte(1), fact_b_id: z.string(), fact_b_content_snippet: z.string(), fact_b_pot_score: z.number().gte(0).lte(1), weight: z.number().gte(0).lte(1), rationale: z.string().optional(), edge_id: z.string().nullable().optional(), companion_of: z.string().nullable().optional(), stances_summary: stancesSummary.nullable().optional(), resolution_type: z.string().nullable().optional(), resolution_notes: z.string().nullable().optional(), winner_fact_id: z.string().nullable().optional()}); export const pendingSupersession = z.object({edge_id: z.string(), from_fact_id: z.string(), to_fact_id: z.string(), weight: z.number().gte(0).lte(1), rationale: z.string().optional(), validation_status: z.string().optional(), from_content_snippet: z.string().optional(), to_content_snippet: z.string().optional(), from_pot_score: z.number().nullable().optional(), to_pot_score: z.number().nullable().optional(), axiom_conflict: z.array(z.string()).nullable().optional()}); export const blockedSupersession = z.object({edge_id: z.string(), from_fact_id: z.string(), to_fact_id: z.string(), weight: z.number().gte(0).lte(1), rationale: z.string().optional(), validation_status: z.string().optional(), from_content_snippet: z.string().optional(), to_content_snippet: z.string().optional(), from_pot_score: z.number().nullable().optional(), to_pot_score: z.number().nullable().optional(), axiom_conflict: z.array(z.string()).nullable().optional(), reason: z.string()}); export const queryEdgesContext = z.object({total_edges: z.number().int().optional(), edges: z.array(queryEdgeItem).optional(), contradictions: z.array(contradictionPair).optional(), contradiction_count: z.number().int().optional(), resolved_contradictions: z.array(contradictionPair).optional(), resolved_contradiction_count: z.number().int().optional(), edges_by_type: z.record(z.string(), z.number().int()).optional(), pending_supersedes: z.array(pendingSupersession).optional(), blocked_supersedes: z.array(blockedSupersession).optional()}); export const queryDataResponse = z.object({results: z.array(queryResultItem).optional(), total_results: z.number().int().gte(0), edges_context: queryEdgesContext.nullable().optional()}); export const classificationResponse = z.object({category: z.string(), confidence: z.number().gte(0).lte(1), suggested_strategy: z.string(), reasoning: z.array(z.string()).optional()}); export const queryMetadataResponse = z.object({strategy_used: z.enum(["cag", "rag", "cag_with_rag_fallback", "graph_rag"]), classification: z.union([classificationResponse, z.record(z.string(), z.unknown())]), cag_attempted: z.boolean(), cag_suggestion: z.string().nullable().optional(), cag_results_count: z.number().int().gte(0), fallback_triggered: z.boolean(), fallback_reason: z.string().nullable().optional(), total_duration_ms: z.number().int().gte(0), cag_duration_ms: z.number().int().gte(0), rag_duration_ms: z.number().int().gte(0), graphrag_duration_ms: z.number().int().gte(0).optional(), traversal_metadata: z.record(z.string(), z.unknown()).nullable().optional(), as_of: z.string().nullable().optional()}); export const gapSignal = z.object({version: z.string(), confidence: z.number().gte(0).lte(1), is_knowledge_gap: z.boolean(), threshold: z.number().gte(0).lte(1), total_facts: z.number().int().gte(0), high_certainty_facts: z.number().int().gte(0), average_score: z.number().gte(0).lte(1), reasons: z.array(z.string()).optional(), judge: z.string().nullable().optional()}); export const queryResponse = z.object({data: queryDataResponse, metadata: queryMetadataResponse, is_knowledge_gap: z.boolean(), gap_signal: gapSignal}); 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 }; /** * Unified query with automatic routing * * POST /pots/{pot_id}/query */ export const createApiPotsPotIdQuery = async (args: CreateApiPotsPotIdQueryArgs, options: CreateApiPotsPotIdQueryOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/pots/${encodeURIComponent(String(args.pot_id))}/query${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(queryResponse, await res.json()) };