import { getEnterprisesDirCandidates, loadCorpConfigs, loadGlobalConfig, resolveGlobalConfigFile } from "../core/config/loader.js"; import { resolveCorpConfig } from "../core/config/manager.js"; import { createSealClient } from "../core/http/factory.js"; import { sealTools, 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(); return resolveCorpConfig( corps, { stateful: globalConfig.stateful, defaultCorpId: globalConfig.defaultCorpId, corpId }, getEnterprisesDirCandidates() ); } export function globalConfigSummary(config: GlobalConfig = loadGlobalConfig()) { return { mode: config.stateful ? "stateful" : "stateless", stateful: config.stateful, ...(config.stateful && config.defaultCorpId ? { defaultCorpId: config.defaultCorpId } : {}), 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) ); const handler = tool.handler as SealTool["handler"]; return handler(client, compactParams as never, { corp }); }