ai-chatbot/services/pascal-editor/apps/editor/lib/pascal-agent-config.ts

42 lines
1.3 KiB
TypeScript

export interface PascalAgentConfig {
apiKey: string
baseUrl: string
configured: boolean
maxOutputTokens: number
maxSteps: number
model: string
stepTimeoutMs: number
totalTimeoutMs: number
}
const DEFAULT_BASE_URL = 'https://api.openai.com/v1'
const DEFAULT_MODEL = 'gpt-4.1-mini'
function readBoundedInteger(
value: string | undefined,
fallback: number,
minimum: number,
maximum: number,
): number {
const parsed = Number.parseInt(value ?? '', 10)
if (!Number.isFinite(parsed)) return fallback
return Math.min(maximum, Math.max(minimum, parsed))
}
export function getPascalAgentConfig(env: NodeJS.ProcessEnv = process.env): PascalAgentConfig {
const apiKey = env.PASCAL_AGENT_API_KEY?.trim() ?? ''
const baseUrl = (env.PASCAL_AGENT_BASE_URL?.trim() || DEFAULT_BASE_URL).replace(/\/$/, '')
const model = env.PASCAL_AGENT_MODEL?.trim() || DEFAULT_MODEL
return {
apiKey,
baseUrl,
configured: apiKey.length > 0 && model.length > 0,
maxOutputTokens: readBoundedInteger(env.PASCAL_AGENT_MAX_OUTPUT_TOKENS, 8192, 1024, 32_768),
maxSteps: readBoundedInteger(env.PASCAL_AGENT_MAX_STEPS, 18, 4, 40),
model,
stepTimeoutMs: readBoundedInteger(env.PASCAL_AGENT_STEP_TIMEOUT_MS, 90_000, 10_000, 180_000),
totalTimeoutMs: readBoundedInteger(env.PASCAL_AGENT_TOTAL_TIMEOUT_MS, 300_000, 30_000, 600_000),
}
}