All 99 registry tools declare a zod schema, and not one of them was ever parsed: `runToolResult` (every CLI command) and `executeSealTool` (`POST /api/tool/:name` and every `/v1/*` route) handed `params` straight to the handler. A missing required field reached the handler as `undefined` and died as an opaque upstream error or a `Cannot read properties of undefined`, and an out-of-range `limit` sailed through to the tenant. Both paths now gate on `parseToolParams`, failing fast with the field path: exit 13 on the CLI, HTTP 400 behind the adapter (structured exit codes map onto statuses instead of collapsing every failure into 500). The HTTP tool endpoint validates before `selectCorp`, which records usage stats to disk — a request that can never succeed should cause no writes. Two properties keep this a pure addition: the original params are returned rather than zod's parsed output (handlers read undeclared keys — the configure tool accepts `appKey` alongside `apiKey`, which the schema now says out loud), and query strings are coerced to the declared type first, because `/v1/*` passes `url.searchParams` where the schema wants `z.number()`.
70 lines
2.4 KiB
TypeScript
70 lines
2.4 KiB
TypeScript
import { getEnterprisesDirCandidates, loadCorpConfigs, loadGlobalConfig, resolveGlobalConfigFile } from "../core/config/loader.js";
|
|
import { resolveCorpConfig } from "../core/config/manager.js";
|
|
import { recordCorpUsage } from "../core/stats/usage-tracker.js";
|
|
import { createSealClient } from "../core/http/factory.js";
|
|
import { sealTools, parseToolParams, type SealTool } from "../domains/seal/tools.js";
|
|
import type { CorpConfig, GlobalConfig } from "../core/config/types.js";
|
|
import { printJson } from "./printer.js";
|
|
import type { OutputOptions } from "./types.js";
|
|
|
|
export function selectCorp(corpId?: string): CorpConfig {
|
|
const corps = loadCorpConfigs();
|
|
const globalConfig = loadGlobalConfig();
|
|
const corp = resolveCorpConfig(
|
|
corps,
|
|
{
|
|
stateful: globalConfig.stateful,
|
|
defaultCorpId: globalConfig.defaultCorpId,
|
|
corpId
|
|
},
|
|
getEnterprisesDirCandidates()
|
|
);
|
|
recordCorpUsage(corp.id);
|
|
return corp;
|
|
}
|
|
|
|
export function globalConfigSummary(config: GlobalConfig = loadGlobalConfig()) {
|
|
return {
|
|
mode: config.stateful ? "stateful" : "stateless",
|
|
stateful: config.stateful,
|
|
...(config.stateful && config.defaultCorpId
|
|
? { defaultCorpId: config.defaultCorpId }
|
|
: {}),
|
|
...(config.testEnterprisesDir
|
|
? { testEnterprisesDir: config.testEnterprisesDir }
|
|
: {}),
|
|
lint: config.lint,
|
|
configFile: resolveGlobalConfigFile()
|
|
};
|
|
}
|
|
|
|
export async function runTool(
|
|
name: string,
|
|
client: Awaited<ReturnType<typeof createSealClient>>,
|
|
corp: CorpConfig,
|
|
params: Record<string, unknown>,
|
|
options: OutputOptions = {}
|
|
) {
|
|
printJson(await runToolResult(name, client, corp, params), options);
|
|
}
|
|
|
|
export async function runToolResult(
|
|
name: string,
|
|
client: Awaited<ReturnType<typeof createSealClient>>,
|
|
corp: CorpConfig,
|
|
params: Record<string, unknown>
|
|
) {
|
|
const tool = sealTools.find((item) => item.name === name);
|
|
if (!tool) throw new Error(`Unknown Seal tool: ${name}`);
|
|
|
|
const compactParams = Object.fromEntries(
|
|
Object.entries(params).filter(([, value]) => value !== undefined)
|
|
);
|
|
// Schema gate: a missing required field or an out-of-range value fails here
|
|
// with exit 13 instead of reaching the handler as `undefined` and surfacing
|
|
// as an opaque upstream error. Valid params pass through untouched.
|
|
parseToolParams(tool, compactParams);
|
|
const handler = tool.handler as SealTool["handler"];
|
|
return handler(client, compactParams as never, { corp });
|
|
}
|