import {z} from 'zod' export type GetApiApiV1JobsJobIdDetailsArgs = {job_id: string, 'unstructured-api-key'?: (string | null) | undefined}; export type GetApiApiV1JobsJobIdDetailsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const jobProcessingStatus = z.enum(["SCHEDULED", "IN_PROGRESS", "SUCCESS", "COMPLETED_WITH_ERRORS", "STOPPED", "FAILED", "REJECTED"]); export const preflightFailureCategory = z.enum(["AUTH_INVALID_CREDENTIAL", "AUTH_PERMISSION_DENIED", "AUTH_NETWORK_UNREACHABLE", "AUTH_PROVIDER_UNAVAILABLE", "PROVIDER_RATE_LIMITED", "CONFIG_INVALID", "UNKNOWN"]); export const jobNodePhase = z.enum(["PENDING_CREATE", "IDLE_ZERO", "SCHEDULING", "UNSCHEDULABLE", "STARTING", "RUNNING", "UNSTARTABLE", "CRASH_LOOPING", "UNKNOWN"]); export const jobNodeDetails = z.object({node_id: z.string().nullable().optional(), node_name: z.string().nullable().optional(), node_type: z.string().nullable().optional(), node_subtype: z.string().nullable().optional(), ready: z.number().int(), in_progress: z.number().int(), success: z.number().int(), failure: z.number().int(), failure_category: preflightFailureCategory.nullable().optional(), failure_reason: z.string().nullable().optional(), node_phase: jobNodePhase.nullable().optional(), node_phase_observed_at: z.string().nullable().optional(), last_change_at: z.string().nullable().optional()}); export const jobAggregations = z.object({total_docs: z.number().int().optional(), total_docs_succeeded: z.number().int().optional(), total_docs_failed: z.number().int().optional()}); export const jobProgressState = z.enum(["not_applicable", "starting", "waiting_to_start", "progressing", "no_recent_progress", "unknown"]); export const jobStatus = z.enum(["SCHEDULED", "IN_PROGRESS", "COMPLETED", "STOPPED", "FAILED", "REJECTED"]); export const activeJob = z.object({job_id: z.string(), workflow_id: z.string().nullable().optional(), workflow_name: z.string().nullable().optional(), status: jobStatus, created_at: z.string()}); export const jobProgressInfo = z.object({state: jobProgressState, as_of: z.string(), message: z.string(), last_node_change_at: z.string().nullable().optional(), seconds_since_node_change: z.number().int().nullable().optional(), seconds_without_report: z.number().int().nullable().optional(), nodes_observed: z.number().int().optional(), slowest_node_id: z.string().nullable().optional(), slowest_node_name: z.string().nullable().optional(), outstanding_docs: z.number().int().nullable().optional(), other_active_jobs: z.array(activeJob).optional(), other_active_jobs_total: z.number().int().optional(), stop_requested_at: z.string().nullable().optional()}); export const jobCompletionIssue = z.enum(["NO_DOCUMENTS_DISCOVERED", "NO_DOCUMENTS_SUCCEEDED"]); export const jobDetails = z.object({id: z.string(), processing_status: jobProcessingStatus, node_stats: z.array(jobNodeDetails), summary: jobAggregations.nullable().optional(), message: z.string().nullable().optional(), progress: jobProgressInfo.nullable().optional(), queued_behind_job_ids_snapshot: z.array(z.string()).nullable().optional(), completion_issue: jobCompletionIssue.nullable().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 processing details * * GET /api/v1/jobs/{job_id}/details */ export const getApiApiV1JobsJobIdDetails = async (args: GetApiApiV1JobsJobIdDetailsArgs, options: GetApiApiV1JobsJobIdDetailsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['unstructured-api-key'] != null) headers.set("unstructured-api-key", String(args['unstructured-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/jobs/${encodeURIComponent(String(args.job_id))}/details${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(jobDetails, await res.json()) };