fix: harden launch server runtime and public config

This commit is contained in:
stringadmin
2026-06-04 18:58:45 +08:00
parent 1a5992845a
commit df5ea8c65e
14 changed files with 926 additions and 32 deletions
+9 -9
View File
@@ -1495,7 +1495,7 @@ function registerAiRoutes(router) {
res.flushHeaders();
const abortController = new AbortController();
const streamTimer = setTimeout(() => abortController.abort(), 60000);
const streamTimer = setTimeout(() => abortController.abort(), 120000);
req.on("close", () => { clearTimeout(streamTimer); abortController.abort(); });
try {
@@ -1554,7 +1554,7 @@ function registerAiRoutes(router) {
}
} else {
const nonStreamAbort = new AbortController();
const nonStreamTimer = setTimeout(() => nonStreamAbort.abort(), 60000);
const nonStreamTimer = setTimeout(() => nonStreamAbort.abort(), 120000);
const upstream = await fetch(url, { method: "POST", headers: reqHeaders, body: reqBody, signal: nonStreamAbort.signal });
clearTimeout(nonStreamTimer);
const text = await upstream.text().catch(() => "");
@@ -1578,7 +1578,7 @@ function registerAiRoutes(router) {
const fallbackHeaders = { "Content-Type": "application/json", Authorization: "Bearer " + slotResult.apiKey };
const fallbackBody = JSON.stringify({ model: fallbackConfig.model, messages, stream: false, temperature: temperature || 0.7, max_tokens: 4096 });
const fbAbort = new AbortController();
const fbTimer = setTimeout(() => fbAbort.abort(), 60000);
const fbTimer = setTimeout(() => fbAbort.abort(), 90000);
const fbUpstream = await fetch(fallbackUrl, { method: "POST", headers: fallbackHeaders, body: fallbackBody, signal: fbAbort.signal });
clearTimeout(fbTimer);
const fbText = await fbUpstream.text().catch(() => "");
@@ -1609,7 +1609,7 @@ function registerAiRoutes(router) {
} catch (err) {
releaseLease(slotResult);
console.error("[ai/chat] error:", err.message);
res.status(500).json({ error: err.message });
res.status(err.name === "AbortError" ? 504 : 500).json({ error: err.name === "AbortError" ? "AI 上游响应超时,请重试" : err.message });
}
});
@@ -1690,7 +1690,7 @@ function registerAiRoutes(router) {
res.json({ taskId: String(rows[0].id), conversationId: rows[0].conversation_id });
} catch (err) {
res.status(500).json({ error: err.message });
res.status(err.name === "AbortError" ? 504 : 500).json({ error: err.name === "AbortError" ? "AI 上游响应超时,请重试" : err.message });
}
});
@@ -1714,7 +1714,7 @@ function registerAiRoutes(router) {
res.json(formatAiTaskRow(rows[0]));
} catch (err) {
res.status(500).json({ error: err.message });
res.status(err.name === "AbortError" ? 504 : 500).json({ error: err.name === "AbortError" ? "AI 上游响应超时,请重试" : err.message });
}
});
@@ -1761,7 +1761,7 @@ function registerAiRoutes(router) {
taskEvents.off(`task:${taskId}`, onUpdate);
});
} catch (err) {
if (!res.headersSent) res.status(500).json({ error: err.message });
if (!res.headersSent) res.status(err.name === "AbortError" ? 504 : 500).json({ error: err.name === "AbortError" ? "AI 上游响应超时,请重试" : err.message });
}
});
@@ -1826,7 +1826,7 @@ function registerAiRoutes(router) {
res.end(buffer);
} catch (err) {
console.error("[ai/tasks/download] failed:", err.message);
if (!res.headersSent) res.status(500).json({ error: err.message });
if (!res.headersSent) res.status(err.name === "AbortError" ? 504 : 500).json({ error: err.name === "AbortError" ? "AI 上游响应超时,请重试" : err.message });
}
});
@@ -1854,7 +1854,7 @@ function registerAiRoutes(router) {
res.end(buffer);
} catch (err) {
console.error("[ai/proxy-download] failed:", err.message);
if (!res.headersSent) res.status(500).json({ error: err.message });
if (!res.headersSent) res.status(err.name === "AbortError" ? 504 : 500).json({ error: err.name === "AbortError" ? "AI 上游响应超时,请重试" : err.message });
}
});
}