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>, corp: CorpConfig, params: Record, options: OutputOptions = {} ) { printJson(await runToolResult(name, client, corp, params), options); } export async function runToolResult( name: string, client: Awaited>, corp: CorpConfig, params: Record ) { 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 }); }