import {z} from 'zod' export type ExtractFromDocumentRequest = {document_id: string, idempotency_key?: (string | null) | undefined, language?: (string | null) | undefined, context_attachment_ids?: (Array | null) | undefined, profile_id?: (string | null) | undefined, source_authority?: (string | null) | undefined, original_date?: (string | null) | undefined, endorsed_by?: (string | null) | undefined}; export type CreateApiPotsPotIdExtractFromDocumentArgs = {pot_id: string, body: ExtractFromDocumentRequest}; export type CreateApiPotsPotIdExtractFromDocumentOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const ingestionAccepted = z.object({job_id: z.string(), pot_id: z.string(), status: z.literal("pending").optional(), message: z.string().optional(), replayed: z.boolean().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 }; /** * Extract facts from a stored document * * POST /pots/{pot_id}/extract-from-document */ export const createApiPotsPotIdExtractFromDocument = async (args: CreateApiPotsPotIdExtractFromDocumentArgs, options: CreateApiPotsPotIdExtractFromDocumentOptions) => { 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))}/extract-from-document${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(ingestionAccepted, await res.json()) };