gpt-engineer-app[bot] commited on
Commit ·
bceb8bc
1
Parent(s): 24d4b9a
Changes
Browse files- src/server/ai-config.ts +56 -1
src/server/ai-config.ts
CHANGED
|
@@ -50,7 +50,12 @@ function extractFetchDetail(error: unknown) {
|
|
| 50 |
return e?.cause?.message || e?.cause?.code || e?.message || "Unknown network error";
|
| 51 |
}
|
| 52 |
|
| 53 |
-
export async function fetchAIWithFallback(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
const config = getAIConfig();
|
| 55 |
const model = resolveModel(requestedModel);
|
| 56 |
const endpoints = [config.baseUrl];
|
|
@@ -61,6 +66,50 @@ export async function fetchAIWithFallback(body: string, requestedModel: string,
|
|
| 61 |
|
| 62 |
let lastError: unknown;
|
| 63 |
let lastStatusResponse: Response | null = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
for (const baseUrl of endpoints) {
|
| 66 |
const usingFallback = baseUrl !== config.baseUrl;
|
|
@@ -72,6 +121,9 @@ export async function fetchAIWithFallback(body: string, requestedModel: string,
|
|
| 72 |
};
|
| 73 |
|
| 74 |
for (let attempt = 0; attempt < 3; attempt++) {
|
|
|
|
|
|
|
|
|
|
| 75 |
try {
|
| 76 |
const response = await fetch(`${baseUrl}/chat/completions`, {
|
| 77 |
method: "POST",
|
|
@@ -92,6 +144,7 @@ export async function fetchAIWithFallback(body: string, requestedModel: string,
|
|
| 92 |
lastStatusResponse = response;
|
| 93 |
}
|
| 94 |
|
|
|
|
| 95 |
return response;
|
| 96 |
} catch (error) {
|
| 97 |
lastError = error;
|
|
@@ -108,9 +161,11 @@ export async function fetchAIWithFallback(body: string, requestedModel: string,
|
|
| 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 |
}
|
|
|
|
| 50 |
return e?.cause?.message || e?.cause?.code || e?.message || "Unknown network error";
|
| 51 |
}
|
| 52 |
|
| 53 |
+
export async function fetchAIWithFallback(
|
| 54 |
+
body: string,
|
| 55 |
+
requestedModel: string,
|
| 56 |
+
label: string,
|
| 57 |
+
userId?: string | null,
|
| 58 |
+
) {
|
| 59 |
const config = getAIConfig();
|
| 60 |
const model = resolveModel(requestedModel);
|
| 61 |
const endpoints = [config.baseUrl];
|
|
|
|
| 66 |
|
| 67 |
let lastError: unknown;
|
| 68 |
let lastStatusResponse: Response | null = null;
|
| 69 |
+
let totalAttempts = 0;
|
| 70 |
+
let usedFallbackFinal = false;
|
| 71 |
+
let endpointFinal = `${config.baseUrl}/chat/completions`;
|
| 72 |
+
const startedAt = Date.now();
|
| 73 |
+
|
| 74 |
+
const finalize = async (
|
| 75 |
+
response: Response | null,
|
| 76 |
+
error: unknown,
|
| 77 |
+
) => {
|
| 78 |
+
const duration = Date.now() - startedAt;
|
| 79 |
+
const status = response?.status ?? null;
|
| 80 |
+
const ok = !!response?.ok;
|
| 81 |
+
let errorText: string | null = null;
|
| 82 |
+
if (!ok) {
|
| 83 |
+
if (error) errorText = extractFetchDetail(error);
|
| 84 |
+
else if (response) {
|
| 85 |
+
try {
|
| 86 |
+
const cloned = response.clone();
|
| 87 |
+
errorText = (await cloned.text()).slice(0, 500);
|
| 88 |
+
} catch {
|
| 89 |
+
errorText = `HTTP ${response.status}`;
|
| 90 |
+
}
|
| 91 |
+
}
|
| 92 |
+
}
|
| 93 |
+
try {
|
| 94 |
+
const { supabaseAdmin } = await import("@/integrations/supabase/client.server");
|
| 95 |
+
await supabaseAdmin.from("ai_call_logs").insert({
|
| 96 |
+
user_id: userId ?? null,
|
| 97 |
+
label,
|
| 98 |
+
requested_model: requestedModel,
|
| 99 |
+
resolved_model: model,
|
| 100 |
+
endpoint: endpointFinal,
|
| 101 |
+
base_url: usedFallbackFinal ? DEFAULT_BASE_URL : config.baseUrl,
|
| 102 |
+
used_fallback: usedFallbackFinal,
|
| 103 |
+
status_code: status,
|
| 104 |
+
ok,
|
| 105 |
+
attempts: totalAttempts,
|
| 106 |
+
duration_ms: duration,
|
| 107 |
+
error: errorText,
|
| 108 |
+
});
|
| 109 |
+
} catch (logErr) {
|
| 110 |
+
console.error(`[${label}] Failed to record ai_call_logs entry:`, logErr);
|
| 111 |
+
}
|
| 112 |
+
};
|
| 113 |
|
| 114 |
for (const baseUrl of endpoints) {
|
| 115 |
const usingFallback = baseUrl !== config.baseUrl;
|
|
|
|
| 121 |
};
|
| 122 |
|
| 123 |
for (let attempt = 0; attempt < 3; attempt++) {
|
| 124 |
+
totalAttempts++;
|
| 125 |
+
usedFallbackFinal = usingFallback;
|
| 126 |
+
endpointFinal = `${baseUrl}/chat/completions`;
|
| 127 |
try {
|
| 128 |
const response = await fetch(`${baseUrl}/chat/completions`, {
|
| 129 |
method: "POST",
|
|
|
|
| 144 |
lastStatusResponse = response;
|
| 145 |
}
|
| 146 |
|
| 147 |
+
await finalize(response, null);
|
| 148 |
return response;
|
| 149 |
} catch (error) {
|
| 150 |
lastError = error;
|
|
|
|
| 161 |
}
|
| 162 |
|
| 163 |
if (lastStatusResponse) {
|
| 164 |
+
await finalize(lastStatusResponse, null);
|
| 165 |
return lastStatusResponse;
|
| 166 |
}
|
| 167 |
|
| 168 |
+
await finalize(null, lastError);
|
| 169 |
const detail = extractFetchDetail(lastError);
|
| 170 |
throw new Error(`Could not reach the analysis service: ${detail}. Try again in a moment.`);
|
| 171 |
}
|