fix: bound AI analysis provider latency
This commit is contained in:
@@ -3,6 +3,9 @@ export type AiRuntimeConfig = {
|
||||
baseUrl: string;
|
||||
apiKey: string;
|
||||
chatModel: string;
|
||||
requestTimeoutMs: number;
|
||||
maxTokens: number;
|
||||
maxRetries: number;
|
||||
};
|
||||
|
||||
export type AiRuntimeConfigResult =
|
||||
@@ -10,11 +13,28 @@ export type AiRuntimeConfigResult =
|
||||
| { success: false; reason: string };
|
||||
|
||||
const DEFAULT_CHAT_MODEL = "gpt-4.1-mini";
|
||||
const DEFAULT_REQUEST_TIMEOUT_MS = 20_000;
|
||||
const DEFAULT_MAX_TOKENS = 1_000;
|
||||
const DEFAULT_MAX_RETRIES = 0;
|
||||
|
||||
function readOptionalEnv(name: string): string {
|
||||
return process.env[name]?.trim() ?? "";
|
||||
}
|
||||
|
||||
function readBoundedIntegerEnv(name: string, fallback: number, min: number, max: number): number {
|
||||
const rawValue = readOptionalEnv(name);
|
||||
if (!rawValue) {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
const value = Number(rawValue);
|
||||
if (!Number.isInteger(value) || value < min || value > max) {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
export function getAiRuntimeConfig(): AiRuntimeConfigResult {
|
||||
const apiKey = readOptionalEnv("AI_API_KEY");
|
||||
if (!apiKey) {
|
||||
@@ -27,6 +47,10 @@ export function getAiRuntimeConfig(): AiRuntimeConfigResult {
|
||||
baseUrl: readOptionalEnv("AI_BASE_URL"),
|
||||
apiKey,
|
||||
chatModel: readOptionalEnv("AI_CHAT_MODEL") || DEFAULT_CHAT_MODEL,
|
||||
requestTimeoutMs: readBoundedIntegerEnv("AI_REQUEST_TIMEOUT_MS", DEFAULT_REQUEST_TIMEOUT_MS, 1_000, 120_000),
|
||||
maxTokens: readBoundedIntegerEnv("AI_MAX_TOKENS", DEFAULT_MAX_TOKENS, 256, 2_000),
|
||||
maxRetries: readBoundedIntegerEnv("AI_MAX_RETRIES", DEFAULT_MAX_RETRIES, 0, 5),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user