import {z} from 'zod' export type GetApiWorkflowExecutionsExecutionIdOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const workflowMinimal = z.object({id: z.string(), name: z.string(), status: z.string()}); export const workflowStepReference = z.object({id: z.string(), identifier: z.string(), stepable_type: z.string().nullable().optional(), position: z.object({x: z.number().optional(), y: z.number().optional()}).and(z.record(z.string(), z.unknown())).nullable().optional()}).and(z.record(z.string(), z.unknown())); export const workflowEvent = z.object({id: z.string(), event_type: z.string(), direction: z.string().nullable().optional(), edge_label: z.string().nullable().optional(), created_at: z.string(), payload: z.record(z.string(), z.unknown()).optional(), step: workflowStepReference.optional()}); export const workflowExecutionDetail = z.object({id: z.string(), status: z.enum(["running", "waiting", "ended", "failed", "handoff"]), started_at: z.string(), ended_at: z.string().nullable().optional(), last_event_at: z.string(), tracking_id: z.string().nullable().optional(), whatsapp_conversation_id: z.string().nullable().optional(), workflow: workflowMinimal.optional(), current_step: workflowStepReference.optional(), error_details: z.record(z.string(), z.unknown()).nullable().optional(), execution_context: z.object({vars: z.record(z.string(), z.unknown()).nullable().optional(), system: z.object({trigger_type: z.string().optional(), tracking_id: z.string().optional()}).and(z.record(z.string(), z.unknown())).nullable().optional(), context: z.object({channel: z.string().optional(), phone_number: z.string().optional()}).and(z.record(z.string(), z.unknown())).nullable().optional(), metadata: z.object({request: z.object({ip: z.string().optional(), user_agent: z.string().optional(), timestamp: z.string().optional()}).and(z.record(z.string(), z.unknown())).optional()}).and(z.record(z.string(), z.unknown())).nullable().optional()}).and(z.record(z.string(), z.unknown())).optional(), events: z.array(workflowEvent).optional()}); export const workflowExecutionResponse = z.object({data: workflowExecutionDetail}); 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 }; /** * Retrieve workflow execution * * GET /workflow_executions/{execution_id} */ export const getApiWorkflowExecutionsExecutionId = async (options?: GetApiWorkflowExecutionsExecutionIdOptions) => { const {baseUrl = "https://api.kapso.ai/platform/v1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/workflow_executions/${encodeURIComponent(String(args.execution_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(workflowExecutionResponse, await res.json()) };