import {z} from 'zod' export type CreateApiApiV1WorkflowsWorkflowIdRunArgs = {workflow_id: string | string, 'unstructured-api-key'?: (string | null) | undefined, body: BodyInit}; export type CreateApiApiV1WorkflowsWorkflowIdRunOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const jobStatus = z.enum(["SCHEDULED", "IN_PROGRESS", "COMPLETED", "STOPPED", "FAILED", "REJECTED"]); export const nodeFileMetadata = z.object({node_id: z.string(), file_id: z.string(), node_type: z.string(), node_subtype: z.string()}); export const workflowJobType = z.enum(["ephemeral", "persistent", "scheduled", "template"]); export const jobProcessingStatus = z.enum(["SCHEDULED", "IN_PROGRESS", "SUCCESS", "COMPLETED_WITH_ERRORS", "STOPPED", "FAILED", "REJECTED"]); export const jobCompletionIssue = z.enum(["NO_DOCUMENTS_DISCOVERED", "NO_DOCUMENTS_SUCCEEDED"]); export const jobInformation = z.object({id: z.string(), workflow_id: z.string(), workflow_name: z.string(), status: jobStatus, stop_requested: z.boolean().optional(), created_at: z.string(), runtime: z.string().nullable().optional(), input_file_ids: z.array(z.string()).nullable().optional(), output_node_files: z.array(nodeFileMetadata).nullable().optional(), job_type: workflowJobType.optional(), created_by_id: z.string().nullable().optional(), reason: z.string().nullable().optional(), processing_status: jobProcessingStatus.nullable().optional(), failed_file_count: z.number().int().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 }; /** * Run Workflow * * POST /api/v1/workflows/{workflow_id}/run */ export const createApiApiV1WorkflowsWorkflowIdRun = async (args: CreateApiApiV1WorkflowsWorkflowIdRunArgs, options: CreateApiApiV1WorkflowsWorkflowIdRunOptions) => { 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/workflows/${encodeURIComponent(String(args.workflow_id))}/run${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: args.body}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(jobInformation, await res.json()) };