gpt-engineer-app[bot] commited on
Commit ·
f7fb007
1
Parent(s): 3273f06
Changes
Browse files- src/server/ai-config.ts +77 -0
src/server/ai-config.ts
CHANGED
|
@@ -37,3 +37,80 @@ export function resolveModel(requested: string): string {
|
|
| 37 |
const { defaultModel } = getAIConfig();
|
| 38 |
return defaultModel ?? requested;
|
| 39 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
const { defaultModel } = getAIConfig();
|
| 38 |
return defaultModel ?? requested;
|
| 39 |
}
|
| 40 |
+
|
| 41 |
+
function isTransientAiStatus(status: number) {
|
| 42 |
+
return [408, 425, 429, 500, 502, 503, 504].includes(status);
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
function extractFetchDetail(error: unknown) {
|
| 46 |
+
const e = error as {
|
| 47 |
+
message?: string;
|
| 48 |
+
cause?: { message?: string; code?: string };
|
| 49 |
+
};
|
| 50 |
+
return e?.cause?.message || e?.cause?.code || e?.message || "Unknown network error";
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
export async function fetchAIWithFallback(body: string, requestedModel: string, label: string) {
|
| 54 |
+
const config = getAIConfig();
|
| 55 |
+
const model = resolveModel(requestedModel);
|
| 56 |
+
const endpoints = [config.baseUrl];
|
| 57 |
+
|
| 58 |
+
if (config.baseUrl !== DEFAULT_BASE_URL && process.env.LOVABLE_API_KEY) {
|
| 59 |
+
endpoints.push(DEFAULT_BASE_URL);
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
let lastError: unknown;
|
| 63 |
+
let lastStatusResponse: Response | null = null;
|
| 64 |
+
|
| 65 |
+
for (const baseUrl of endpoints) {
|
| 66 |
+
const usingFallback = baseUrl !== config.baseUrl;
|
| 67 |
+
const headers = {
|
| 68 |
+
"Content-Type": "application/json",
|
| 69 |
+
...(baseUrl === DEFAULT_BASE_URL
|
| 70 |
+
? { Authorization: `Bearer ${process.env.LOVABLE_API_KEY ?? ""}` }
|
| 71 |
+
: aiAuthHeaders()),
|
| 72 |
+
};
|
| 73 |
+
|
| 74 |
+
for (let attempt = 0; attempt < 3; attempt++) {
|
| 75 |
+
try {
|
| 76 |
+
const response = await fetch(`${baseUrl}/chat/completions`, {
|
| 77 |
+
method: "POST",
|
| 78 |
+
headers,
|
| 79 |
+
body: JSON.stringify({
|
| 80 |
+
...JSON.parse(body),
|
| 81 |
+
model,
|
| 82 |
+
}),
|
| 83 |
+
});
|
| 84 |
+
|
| 85 |
+
if (!response.ok && isTransientAiStatus(response.status) && attempt < 2) {
|
| 86 |
+
console.error(`[${label}] AI request returned ${response.status} on attempt ${attempt + 1}${usingFallback ? " (fallback gateway)" : ""}`);
|
| 87 |
+
await new Promise((resolve) => setTimeout(resolve, 400 * (attempt + 1)));
|
| 88 |
+
continue;
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
if (!response.ok) {
|
| 92 |
+
lastStatusResponse = response;
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
return response;
|
| 96 |
+
} catch (error) {
|
| 97 |
+
lastError = error;
|
| 98 |
+
console.error(
|
| 99 |
+
`[${label}] AI fetch attempt ${attempt + 1} failed${usingFallback ? " (fallback gateway)" : ""}:`,
|
| 100 |
+
extractFetchDetail(error),
|
| 101 |
+
);
|
| 102 |
+
|
| 103 |
+
if (attempt < 2) {
|
| 104 |
+
await new Promise((resolve) => setTimeout(resolve, 400 * (attempt + 1)));
|
| 105 |
+
}
|
| 106 |
+
}
|
| 107 |
+
}
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
if (lastStatusResponse) {
|
| 111 |
+
return lastStatusResponse;
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
const detail = extractFetchDetail(lastError);
|
| 115 |
+
throw new Error(`Could not reach the analysis service: ${detail}. Try again in a moment.`);
|
| 116 |
+
}
|