import {z} from 'zod' export type GetApiV1HitlApprovalsArgs = {conversation_id?: (string | null) | undefined, status?: (string | null) | undefined, approver_role?: (string | null) | undefined, cursor?: (string | null) | undefined, limit?: number | undefined}; export type GetApiV1HitlApprovalsOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const subjectKind = z.enum(["workflow_node", "tool_call"]); export const approvalRequestSummary = z.object({id: z.string(), conversation_id: z.string(), node_id: z.string().nullable(), subject_kind: subjectKind, subject_ref: z.string(), approver_role: z.string(), status: z.string(), suspend_message: z.string().nullable().optional(), created_at: z.string(), timeout_at: z.string().nullable().optional()}); export const cursorPageApprovalRequestSummary = z.object({items: z.array(approvalRequestSummary), next_cursor: z.string().nullable().optional(), has_more: z.boolean()}); 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 approval requests * * GET /v1/hitl/approvals */ export const getApiV1HitlApprovals = async (args: GetApiV1HitlApprovalsArgs, options: GetApiV1HitlApprovalsOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.conversation_id != null) query.append("conversation_id", String(args.conversation_id)) if (args.status != null) query.append("status", String(args.status)) if (args.approver_role != null) query.append("approver_role", String(args.approver_role)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/hitl/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(cursorPageApprovalRequestSummary, await res.json()) };