import {z} from 'zod' export type GetApiIngestionStatusJobIdArgs = {job_id: string}; export type GetApiIngestionStatusJobIdOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const jobProgress = z.object({current_step: z.string(), progress_percent: z.number().int().gte(0).lte(100), steps_completed: z.array(z.string()).optional(), steps_remaining: z.array(z.string()).optional()}); export const scope = z.object({org_id: z.string().min(1), user_id: z.string().nullable().optional(), session_id: z.string().nullable().optional()}); export const jobStatusResponse = z.object({job_id: z.string(), job_type: z.string(), status: z.string(), progress: jobProgress, scope: scope, created_at: z.string(), updated_at: z.string(), completed_at: z.string().nullable().optional(), google_drive_file_id: z.string().nullable().optional(), unstructured_job_id: z.string().nullable().optional(), s3_url: z.string().nullable().optional(), helix_response: z.object({}).nullable().optional(), error_message: z.string().nullable().optional(), partial: z.boolean().optional(), partial_errors: z.array(z.string()).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 }; /** * Get Job Status * * GET /ingestion/status/{job_id} */ export const getApiIngestionStatusJobId = async (args: GetApiIngestionStatusJobIdArgs, options: GetApiIngestionStatusJobIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/ingestion/status/${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(jobStatusResponse, await res.json()) };