import {z} from 'zod' export type GetApiV1DefaultBanksBankIdOperationsArgs = {bank_id: string, status?: (string | null) | undefined, type?: (string | null) | undefined, limit?: number | undefined, offset?: number | undefined, exclude_parents?: boolean | undefined, authorization?: (string | null) | undefined}; export type GetApiV1DefaultBanksBankIdOperationsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const refreshMentalModelOperationDetails = z.object({operation_type: z.literal("refresh_mental_model").optional(), outcome: z.enum(["content_written", "content_unchanged", "content_preserved_no_new_facts", "refresh_failed_empty_candidate", "refresh_failed_delta_not_applied", "refresh_failed_structured_output", "refresh_failed_candidate_rejected", "refresh_failed_error"]), failure_reason: z.enum(["empty_candidate", "structured_doc_unreadable", "delta_ops_failed", "delta_ops_all_skipped", "delta_not_applied", "structured_output_failed", "candidate_rejected", "retrieval_failed", "no_answer", "unexpected_error"]).nullable().optional()}); export const operationProgress = z.object({stage: z.string(), at: z.string(), processed: z.number().int().nullable().optional(), total: z.number().int().nullable().optional(), detail: z.record(z.string(), z.number().int()).nullable().optional()}); export const operationResponse = z.object({id: z.string(), task_type: z.string(), operation_id: z.string().nullable().optional(), operation_type: z.string().nullable().optional(), items_count: z.number().int(), document_id: z.string().nullable().optional(), filename: z.string().nullable().optional(), mental_model_id: z.string().nullable().optional(), details: refreshMentalModelOperationDetails.nullable().optional(), created_at: z.string(), updated_at: z.string().nullable().optional(), status: z.string(), error_message: z.string().nullable(), retry_count: z.number().int().nullable().optional(), next_retry_at: z.string().nullable().optional(), progress: operationProgress.nullable().optional()}); export const operationsListResponse = z.object({bank_id: z.string(), total: z.number().int(), limit: z.number().int(), offset: z.number().int(), operations: z.array(operationResponse)}); 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 async operations * * GET /v1/default/banks/{bank_id}/operations */ export const getApiV1DefaultBanksBankIdOperations = async (args: GetApiV1DefaultBanksBankIdOperationsArgs, options: GetApiV1DefaultBanksBankIdOperationsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = 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.type != null) query.append("type", String(args.type)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.exclude_parents != null) query.append("exclude_parents", String(args.exclude_parents)) if (args.authorization != null) headers.set("authorization", String(args.authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/default/banks/${encodeURIComponent(String(args.bank_id))}/operations${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(operationsListResponse, await res.json()) };