import {z} from 'zod' export type ContributeRequest = {content: string, contributor_id: string, run_post_ingestion?: boolean | undefined, influence_factor?: (number | null) | undefined, endorsed?: boolean | undefined, tentative?: boolean | undefined, valid_from?: (string | null) | undefined, basis_quote?: (string | null) | undefined, idempotency_key?: (string | null) | undefined}; export type CreateApiPotsPotIdFactsContributeArgs = {pot_id: string, 'Idempotency-Key'?: (string | null) | undefined, body: ContributeRequest}; export type CreateApiPotsPotIdFactsContributeOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const contributedFactState = z.object({fact_id: z.string(), content: z.string(), pot_score: z.number(), level: z.string(), indexed: z.boolean().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 contributeResponse = z.object({pot_id: z.string(), facts_created: z.number().int(), fact_ids: z.array(z.string()).optional(), facts: z.array(contributedFactState).optional(), avg_pot_score: z.number(), expertise_evaluation: expertiseEvaluation, edges_created: z.number().int().optional(), scores_updated: z.number().int().optional(), replayed: z.boolean().optional(), partial: z.boolean().optional(), facts_missing: z.number().int().gte(0).optional(), post_ingestion: z.enum(["skipped", "scheduled", "completed", "failed"]).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 }; /** * Contribute expert knowledge * * POST /pots/{pot_id}/facts/contribute */ export const createApiPotsPotIdFactsContribute = async (args: CreateApiPotsPotIdFactsContributeArgs, options: CreateApiPotsPotIdFactsContributeOptions) => { 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") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) const url = `${baseUrl.replace(/\/+$/, '')}/pots/${encodeURIComponent(String(args.pot_id))}/facts/contribute${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(contributeResponse, await res.json()) };