import {z} from 'zod' export type AuditToolCallRequest = {tool_name: string, caller_name?: string | undefined, tool_type?: string | undefined, input?: Record | undefined, output?: Record | undefined, workflow_id?: string | undefined, step_id?: string | undefined, user_id?: string | undefined, duration_ms?: number | undefined, policies_applied?: Array | undefined, success?: boolean | undefined, error_message?: string | undefined}; export type CreateApiApiV1AuditToolCallArgs = {'X-Tenant-ID': string, 'X-Axonflow-Proxy-Auth': string, 'Idempotency-Key'?: string | undefined, body: AuditToolCallRequest}; export type CreateApiApiV1AuditToolCallOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const auditToolCallResponse = z.object({audit_id: z.string().optional(), status: z.literal("recorded").optional(), timestamp: z.string().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 }; /** * Record a tool call audit entry * * POST /api/v1/audit/tool-call */ export const createApiApiV1AuditToolCall = async (args: CreateApiApiV1AuditToolCallArgs, options?: CreateApiApiV1AuditToolCallOptions) => { const {baseUrl = "https://orchestrator.getaxonflow.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Tenant-ID'] != null) headers.set("X-Tenant-ID", String(args['X-Tenant-ID'])) if (args['X-Axonflow-Proxy-Auth'] != null) headers.set("X-Axonflow-Proxy-Auth", String(args['X-Axonflow-Proxy-Auth'])) if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/audit/tool-call${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(auditToolCallResponse, await res.json()) };