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 { sealCapabilities, parseCapabilityParams, type SealCapability } from "../domains/seal/capabilities.js"; import { createLangfuseService, findLangfuseCapability } from "../domains/langfuse/capabilities.js"; import type { LangfuseService } from "../domains/langfuse/service.js"; import { validationError } from "../core/errors.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 runCapability( name: string, client: Awaited>, corp: CorpConfig, params: Record, options: OutputOptions = {} ) { printJson(await runCapabilityResult(name, client, corp, params), options); } export async function runCapabilityResult( name: string, client: Awaited>, corp: CorpConfig, params: Record ) { const tool = sealCapabilities.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. parseCapabilityParams(tool, compactParams); const handler = tool.handler as SealCapability["handler"]; return handler(client, compactParams as never, { corp }); } /** * Dispatches one read-only Langfuse capability and prints its JSON payload. * * Same contract as `runCapability` minus the tenant: no Seal client, no * corp — Langfuse access relies solely on the compile-time injected key pair. * Params are gated by the capability's Zod schema (missing/invalid → exit 13) * before the handler sees them. */ export async function runLangfuseCapability( name: string, params: Record, options: OutputOptions = {} ) { const tool = findLangfuseCapability(name); if (!tool) throw new Error(`Unknown Langfuse tool: ${name}`); const compactParams = Object.fromEntries( Object.entries(params).filter(([, value]) => value !== undefined) ); const parsed = tool.parameters.safeParse(compactParams); if (!parsed.success) { const detail = parsed.error.issues .map((issue) => `${issue.path.length ? issue.path.join(".") : "(root)"}: ${issue.message}`) .join("; "); throw validationError(`Invalid parameters for ${tool.name}: ${detail}`); } const handler = tool.handler as (params: never, service: LangfuseService) => Promise; printJson(await handler(compactParams as never, createLangfuseService()), options); }