import {z} from 'zod' export type GetApiApiV1JobsJobIdArgs = {job_id: string}; export type GetApiApiV1JobsJobIdOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const ingestionJobResult = z.object({source_ids: z.array(z.number().int()), failed_source_ids: z.array(z.number().int()), memory_count: z.number().int(), entity_count: z.number().int(), edge_count: z.number().int(), tokens_used: z.number().int(), source_errors: z.record(z.string(), z.string()).optional(), error_message: z.string().optional()}).nullable(); export const jobResponse = z.object({job_id: z.string(), status: z.enum(["pending", "processing", "completed", "partial", "failed", "cancelled"]), source_ids: z.array(z.number().int()), result: ingestionJobResult, error_message: z.string().nullable(), created_at: z.string(), started_at: z.string().nullable(), completed_at: z.string().nullable()}); 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 }; /** * Get Job * * GET /api/v1/jobs/{job_id} */ export const getApiApiV1JobsJobId = async (args: GetApiApiV1JobsJobIdArgs, options: GetApiApiV1JobsJobIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/jobs/${encodeURIComponent(String(args.job_id))}${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(jobResponse, await res.json()) };