import { CONFIG_LIMITS, normalizeLimited } from '../../../../src/config/numericLimits.js'; export interface WorkflowRuntimeConfig { maxActiveRuns: number; modelConcurrency: number; toolConcurrency: number; processConcurrency: number; pipelineConcurrency: number; pipelineMaxConcurrency: number; maxCalls: number; } const ENV = { maxActiveRuns: 'MINIMAL_AGENT_WORKFLOW_MAX_ACTIVE_RUNS', modelConcurrency: 'MINIMAL_AGENT_WORKFLOW_MODEL_CONCURRENCY', toolConcurrency: 'MINIMAL_AGENT_WORKFLOW_TOOL_CONCURRENCY', processConcurrency: 'MINIMAL_AGENT_WORKFLOW_PROCESS_CONCURRENCY', pipelineConcurrency: 'MINIMAL_AGENT_WORKFLOW_PIPELINE_CONCURRENCY', pipelineMaxConcurrency: 'MINIMAL_AGENT_WORKFLOW_PIPELINE_MAX_CONCURRENCY', maxCalls: 'MINIMAL_AGENT_WORKFLOW_MAX_CALLS', } as const; const DEFAULTS: WorkflowRuntimeConfig = { maxActiveRuns: 4, modelConcurrency: 8, toolConcurrency: 16, processConcurrency: 8, pipelineConcurrency: 4, pipelineMaxConcurrency: 32, maxCalls: 1000, }; function envInt( key: keyof WorkflowRuntimeConfig, env: NodeJS.ProcessEnv, ): number { const name = ENV[key]; const raw = env[name]; if (raw === undefined || raw.trim() === '') return DEFAULTS[key]; const value = Number(raw); const limit = CONFIG_LIMITS[`workflow${key[0]!.toUpperCase()}${key.slice(1)}` as keyof typeof CONFIG_LIMITS]; if (!Number.isInteger(value) || value < limit.min || value > limit.max) { throw new Error( `${name} 必须是 ${limit.min}..${limit.max} 的整数,当前值为 ${JSON.stringify(raw)}`, ); } return normalizeLimited(value, limit, DEFAULTS[key]); } export function readWorkflowRuntimeConfig( env: NodeJS.ProcessEnv = process.env, ): WorkflowRuntimeConfig { const config: WorkflowRuntimeConfig = { maxActiveRuns: envInt('maxActiveRuns', env), modelConcurrency: envInt('modelConcurrency', env), toolConcurrency: envInt('toolConcurrency', env), processConcurrency: envInt('processConcurrency', env), pipelineConcurrency: envInt('pipelineConcurrency', env), pipelineMaxConcurrency: envInt('pipelineMaxConcurrency', env), maxCalls: envInt('maxCalls', env), }; if (config.pipelineConcurrency > config.pipelineMaxConcurrency) { throw new Error( `${ENV.pipelineConcurrency} 不能大于 ${ENV.pipelineMaxConcurrency}`, ); } return Object.freeze(config); }