/** * Shared audit handler for `GET /audit`. * * Delegates to `RuntimeHandle.auditQuery` and translates `AuditQueryError` * codes into the HTTP-layer result shape. Called by the Fastify route (S4). * * The discriminator field is `success` (matching MCP and the rest of the * runtime-api surface). The route layer translates a successful result * into the wire shape `{ success: true, data: {...} }` and a failure into * `{ success: false, error: { code, message } }` per the senpi-stack * convention. */ import type { AuditQueryArgs, AuditQueryResult } from "../../runtime/audit-query.types.js"; import type { RuntimeHandle } from "../../runtime/run.js"; import type { Logger } from "../../utils/logger.js"; /** * Dependencies injected into {@link auditHandler}. */ export interface AuditDeps { /** Live runtime handles maintained by the plugin. */ runtimeHandles: Map; /** Logger instance — unexpected errors logged at `error` level. */ logger: Logger; } /** * Result returned by {@link auditHandler}. * * Discriminated on `success`: * - `true` → audit query succeeded; `address`, `strategyId`, and `result` are present. * - `false` → failure; `code` is one of `NOT_FOUND | UPSTREAM | ABORTED`. * * `NOT_FOUND` — no running runtime for the given address, or `listStrategies` * returned 0 matches (wallet not owned by the API-key user). * `UPSTREAM` — MCP returned an error (network, auth, schema mismatch, runtime * not started). * `ABORTED` — caller cancelled via `AbortSignal` before MCP responded. */ export type AuditHandlerResult = { success: true; /** Lowercased wallet address. */ address: string; /** * Strategy UUID resolved from the wallet by `listStrategies`. * May be `null` on the first successful call in the unlikely window * between `auditQuery` resolving and `getStrategyId()` being read — * in practice the cache is populated synchronously before the * Promise resolves. */ strategyId: string | null; /** Pass-through `audit_query` response payload. */ result: AuditQueryResult; } | { success: false; code: "NOT_FOUND" | "UPSTREAM" | "ABORTED"; message: string; }; /** * Execute an audit query for one wallet via its runtime handle. * * Steps: * 1. Resolve the runtime handle by `address` (case-insensitive lookup). * Returns `NOT_FOUND` on miss. * 2. Call `handle.auditQuery(args, { signal })`. * 3. Read `handle.getStrategyId()` after a successful call — the id is cached * on the runtime instance from step 2's `listStrategies` resolution. * 4. On `AuditQueryError`: map `code` straight through to the result. * 5. On any other throw: log + return `UPSTREAM`. * * @param address Wallet address to audit (normalised to lowercase internally). * @param args Audit query filters forwarded to `RuntimeHandle.auditQuery`. * @param deps Injected dependencies. * @param signal Optional `AbortSignal` tied to the Fastify request lifetime. * Threads through to the underlying MCP call so the runtime * can cancel in-flight work when the HTTP client disconnects. * @returns A resolved `AuditHandlerResult` — never rejects. */ export declare function auditHandler(address: string, args: AuditQueryArgs, deps: AuditDeps, signal?: AbortSignal): Promise; //# sourceMappingURL=audit.d.ts.map