fix(backend): defer provider endpoint/header resolution to call time

The provider previously evaluated env.AI_BASE_URL at module load, which
threw when AI is disabled (no URL configured). Compute the endpoint and
Authorization header lazily inside the fetch calls so importing the module
never fails; the runtime and triage layers already gate on AI_ENABLED first.
This commit is contained in:
lakshit verma 2026-08-19 18:52:47 +05:30
parent 757125e727
commit 1fa20411aa
No known key found for this signature in database

View file

@ -4,11 +4,16 @@ const TIMEOUT_MS = 60_000;
const MAX_CONTENT_LENGTH = 1024 * 1024;
const BAD_STATUS_BODY_LIMIT = 500;
const base = env.AI_BASE_URL!.replace(/\/+$/, '') + '/chat/completions';
const headers = {
'Content-Type': 'application/json',
Authorization: `Bearer ${env.AI_API_KEY!}`,
};
function endpoint(): string {
return env.AI_BASE_URL!.replace(/\/+$/, '') + '/chat/completions';
}
function headers(): Record<string, string> {
return {
'Content-Type': 'application/json',
Authorization: `Bearer ${env.AI_API_KEY!}`,
};
}
export interface ProviderMessage {
role: 'system' | 'user' | 'assistant' | 'tool';
@ -112,9 +117,9 @@ export async function chatComplete(
): Promise<ProviderResult> {
let res: Response;
try {
res = await fetch(base, {
res = await fetch(endpoint(), {
method: 'POST',
headers,
headers: headers(),
body: JSON.stringify(buildBody(messages, tools, opts.maxTokens, false)),
redirect: 'error',
signal: combinedSignal(opts.signal),
@ -156,9 +161,9 @@ export async function chatStream(
): Promise<ProviderResult> {
let res: Response;
try {
res = await fetch(base, {
res = await fetch(endpoint(), {
method: 'POST',
headers,
headers: headers(),
body: JSON.stringify(buildBody(messages, tools, opts.maxTokens, true)),
redirect: 'error',
signal: combinedSignal(opts.signal),