import {z} from 'zod' export type GetApiV1ConversationsSessionIdExtractionJobsArgs = {session_id: string}; export type GetApiV1ConversationsSessionIdExtractionJobsOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const extractionJobResponse = z.object({id: z.string(), trigger_source: z.enum(["close", "rerun"]), status: z.enum(["queued", "processing", "completed", "failed", "skipped", "cancelled"]), schema_id: z.string().nullable(), schema_version: z.number().int(), attempts: z.number().int(), skip_reason: z.string().nullable(), error_message: z.string().nullable(), created_at: z.string(), started_at: z.string().nullable(), completed_at: z.string().nullable(), failed_at: z.string().nullable()}); export const extractionJobListResponse = z.object({items: z.array(extractionJobResponse)}); 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 extraction jobs for a session * * GET /v1/conversations/{session_id}/extraction/jobs */ export const getApiV1ConversationsSessionIdExtractionJobs = async (args: GetApiV1ConversationsSessionIdExtractionJobsArgs, options: GetApiV1ConversationsSessionIdExtractionJobsOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/conversations/${encodeURIComponent(String(args.session_id))}/extraction/jobs${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(extractionJobListResponse, await res.json()) };