import {z} from 'zod' export type GetApiV1FetchAgentArgs = {agent_id: number}; export type GetApiV1FetchAgentOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const agentType = z.enum(["compliance", "datatable_match", "fraud", "signature", "document_rename", "splitting", "cross_split"]); export const agentStatus = z.enum(["active", "inactive"]); export const agentFields = z.enum(["label", "code", "unit_price", "total_price", "quantity", "invoice_date", "invoice_amount", "invoice_number"]); export const agentApiFieldMapping = z.object({field_id: z.number().int(), agent_field: agentFields}); export const agentApiDatatableRef = z.object({document_id: z.number().int().nullable().optional()}); export const agentApiOut = z.object({id: z.number().int(), type: agentType, name: z.string().nullable().optional(), status: agentStatus, is_locked: z.boolean().nullable().optional(), is_show_hint: z.boolean().nullable().optional(), template_ids: z.array(z.number().int()).optional(), settings: z.record(z.string(), z.unknown()).nullable().optional(), linked_field_ids: z.array(z.number().int()).optional(), field_mapping: z.array(agentApiFieldMapping).optional(), datatable_ref: agentApiDatatableRef.nullable().optional(), source_agent_id: z.number().int().nullable().optional(), source_agent_name: z.string().nullable().optional(), activation_error: z.string().nullable().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 }; /** * Fetch Agent * * GET /v1/fetch_agent/ */ export const getApiV1FetchAgent = async (args: GetApiV1FetchAgentArgs, options: GetApiV1FetchAgentOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.agent_id != null) query.append("agent_id", String(args.agent_id)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/fetch_agent/${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(agentApiOut, await res.json()) };