import {z} from 'zod' export type GetApiApprovalsArgs = {status?: ('pending' | 'approved' | 'rejected' | 'expired' | 'cancelled' | null) | undefined, source_type?: (string | null) | undefined, approval_type?: ('required' | 'audit' | null) | undefined, pause_type?: (string | null) | undefined, agent_id?: (string | null) | undefined, team_id?: (string | null) | undefined, workflow_id?: (string | null) | undefined, user_id?: (string | null) | undefined, schedule_id?: (string | null) | undefined, run_id?: (string | null) | undefined, limit?: number | undefined, page?: number | undefined}; export type GetApiApprovalsOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const runStatus = z.enum(["PENDING", "RUNNING", "COMPLETED", "PAUSED", "CANCELLED", "ERROR", "REGENERATED"]); export const approvalResponse = z.object({id: z.string(), run_id: z.string(), session_id: z.string(), status: z.string(), source_type: z.string(), approval_type: z.string().nullable().optional(), pause_type: z.string().nullable().optional(), tool_name: z.string().nullable().optional(), tool_args: z.record(z.string(), z.unknown()).nullable().optional(), expires_at: z.number().int().nullable().optional(), agent_id: z.string().nullable().optional(), team_id: z.string().nullable().optional(), workflow_id: z.string().nullable().optional(), user_id: z.string().nullable().optional(), schedule_id: z.string().nullable().optional(), schedule_run_id: z.string().nullable().optional(), source_name: z.string().nullable().optional(), requirements: z.array(z.record(z.string(), z.unknown())).nullable().optional(), context: z.record(z.string(), z.unknown()).nullable().optional(), resolution_data: z.record(z.string(), z.unknown()).nullable().optional(), resolved_by: z.string().nullable().optional(), resolved_at: z.number().int().nullable().optional(), created_at: z.number().int().nullable().optional(), updated_at: z.number().int().nullable().optional(), run_status: runStatus.nullable().optional()}); export const paginationInfo = z.object({page: z.number().int().gte(0).optional(), limit: z.number().int().gte(1).optional(), total_pages: z.number().int().gte(0).optional(), total_count: z.number().int().gte(0).optional(), search_time_ms: z.number().gte(0).optional()}); export const paginatedResponseApprovalResponse = z.object({data: z.array(approvalResponse), meta: paginationInfo}); 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 }; /** * List Approvals * * GET /approvals */ export const getApiApprovals = async (args: GetApiApprovalsArgs, options: GetApiApprovalsOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.source_type != null) query.append("source_type", String(args.source_type)) if (args.approval_type != null) query.append("approval_type", String(args.approval_type)) if (args.pause_type != null) query.append("pause_type", String(args.pause_type)) if (args.agent_id != null) query.append("agent_id", String(args.agent_id)) if (args.team_id != null) query.append("team_id", String(args.team_id)) if (args.workflow_id != null) query.append("workflow_id", String(args.workflow_id)) if (args.user_id != null) query.append("user_id", String(args.user_id)) if (args.schedule_id != null) query.append("schedule_id", String(args.schedule_id)) if (args.run_id != null) query.append("run_id", String(args.run_id)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/approvals${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(paginatedResponseApprovalResponse, await res.json()) };