import {z} from 'zod' export type CreateApiApiV1PlansPlanIdStepsStepIdApproveArgs = {planId: string, stepId: string, body: {approved_by?: string | undefined, comment?: string | undefined}}; export type CreateApiApiV1PlansPlanIdStepsStepIdApproveOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const policyMatch = z.object({policy_id: z.string().optional(), policy_name: z.string().optional(), action: z.enum(["allow", "block", "require_approval", "redact"]).optional(), reason: z.string().optional()}); export const retryContext = z.object({gate_count: z.number().int().gte(1), completion_count: z.number().int().gte(0), prior_completion_status: z.enum(["none", "completed", "gated_not_completed"]), prior_output_available: z.boolean(), prior_output: z.record(z.string(), z.unknown()).nullable(), prior_completion_at: z.string().nullable(), first_attempt_at: z.string(), last_attempt_at: z.string(), last_decision: z.enum(["allow", "block", "require_approval"]), idempotency_key: z.string()}); export const approvalResponse = z.object({workflow_id: z.string().optional(), plan_id: z.string().optional(), step_id: z.string().optional(), status: z.enum(["pending", "approved", "rejected", "expired"]).optional(), decision: z.enum(["allow", "block", "require_approval"]).optional(), reason: z.string().optional(), approval_status: z.enum(["pending", "approved", "rejected", "expired"]).optional(), approval_id: z.string().optional(), approved_by: z.string().optional(), approved_at: z.string().optional(), rejected_by: z.string().optional(), rejected_at: z.string().optional(), policies_matched: z.array(policyMatch).optional(), retry_context: retryContext.optional(), message: 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 }; /** * Approve a MAP plan step (HITL parity with WCP) * * POST /api/v1/plans/{planId}/steps/{stepId}/approve */ export const createApiApiV1PlansPlanIdStepsStepIdApprove = async (args: CreateApiApiV1PlansPlanIdStepsStepIdApproveArgs, options?: CreateApiApiV1PlansPlanIdStepsStepIdApproveOptions) => { 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/plans/${encodeURIComponent(String(args.planId))}/steps/${encodeURIComponent(String(args.stepId))}/approve${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(approvalResponse, await res.json()) };