import {z} from 'zod' export type AgentType = 'compliance' | 'datatable_match' | 'fraud' | 'signature' | 'document_rename' | 'splitting' | 'cross_split'; export type AgentStatus = 'active' | 'inactive'; export type AgentFields = 'label' | 'code' | 'unit_price' | 'total_price' | 'quantity' | 'invoice_date' | 'invoice_amount' | 'invoice_number'; export type AgentApiFieldMapping = {field_id: number, agent_field: AgentFields}; export type AgentApiCreate = {type: AgentType, template_ids: Array, name?: (string | null) | undefined, status?: AgentStatus | undefined, settings?: ({[key: string]: unknown} | null) | undefined, linked_field_ids?: Array | undefined, field_mapping?: Array | undefined}; export type CreateApiV1CreateAgentArgs = {body: AgentApiCreate}; export type CreateApiV1CreateAgentOptions = {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 }; /** * Create Agent * * POST /v1/create_agent/ */ export const createApiV1CreateAgent = async (args: CreateApiV1CreateAgentArgs, options: CreateApiV1CreateAgentOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/create_agent/${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(agentApiOut, await res.json()) };