import {z} from 'zod' export type WorkflowStep = {name: string, type: 'mcp_query' | 'llm' | 'api_call' | 'transform', connector?: string | undefined, query?: string | undefined, prompt?: string | undefined, dependencies?: Array | undefined}; export type Workflow = {metadata: {name: string, description?: string | undefined}, spec: {steps: Array}}; export type UserContext = {id?: number | undefined, email?: string | undefined, role?: string | undefined, region?: string | undefined, permissions?: Array | undefined, tenant_id?: string | undefined, org_id?: string | undefined}; export type WorkflowExecuteRequest = {workflow: Workflow, input?: {[key: string]: unknown} | undefined, user?: UserContext | undefined}; export type CreateApiApiV1WorkflowsExecuteArgs = {body: WorkflowExecuteRequest}; export type CreateApiApiV1WorkflowsExecuteOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const userContext = z.object({id: z.number().int().optional(), email: z.string().optional(), role: z.string().optional(), region: z.string().optional(), permissions: z.array(z.string()).optional(), tenant_id: z.string().optional(), org_id: z.string().optional()}); export const workflowExecution = z.object({id: z.string().optional(), workflow_name: z.string().optional(), status: z.enum(["pending", "running", "completed", "failed"]).optional(), input: z.record(z.string(), z.unknown()).optional(), start_time: z.string().optional(), end_time: z.string().optional(), user_context: userContext.optional(), error: z.string().optional(), steps: z.array(z.object({name: z.string().optional(), status: z.enum(["pending", "running", "completed", "failed", "skipped"]).optional(), input: z.record(z.string(), z.unknown()).optional(), output: z.record(z.string(), z.unknown()).optional(), start_time: z.string().optional(), end_time: z.string().optional(), error: z.string().optional(), process_time: z.string().optional()})).optional(), output: z.record(z.string(), z.unknown()).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 }; /** * Execute a workflow * * POST /api/v1/workflows/execute */ export const createApiApiV1WorkflowsExecute = async (args: CreateApiApiV1WorkflowsExecuteArgs, options?: CreateApiApiV1WorkflowsExecuteOptions) => { const {baseUrl = "https://orchestrator.getaxonflow.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/workflows/execute${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(workflowExecution, await res.json()) };