Update server.js via upload script - 2026-10-15 11:16:58

This commit is contained in:
mike 2026-10-15 11:17:01 +08:00
parent a49198f9c8
commit bcda9c2ef6
1 changed files with 103 additions and 40 deletions

123
server.js
View File

@ -149,58 +149,85 @@ async function callDeepSeek(apiKey, messages, opts) {
}, messages, opts);
}
// 用流式(stream)方式调用:DeepSeek 繁忙时请求会先排队,期间只发 keep-alive 空行、不出内容,
// 非流式调用根本分不清"在排队"还是"在生成",只能干等到超时。流式就能看到第一个字什么时候出来:
// firstTokenMs 内一个字都没出 → 判定为繁忙(e.slow),上层直接切换到备用服务商。
async function callLLM(provider, messages, opts) {
const { temperature = 0.7, timeoutMs = 120000 } = opts || {};
const { temperature = 0.7, timeoutMs = 120000, firstTokenMs = 60000 } = opts || {};
let maxTokens = (opts && opts.maxTokens) || 600;
if (provider.maxTokensCap) maxTokens = Math.min(maxTokens, provider.maxTokensCap);
const model = provider.model;
const label = provider.name;
// 超时保护:没有这个的话,DeepSeek 一旦卡住或输出失控(一直生成到 max_tokens 上限),请求会无限期挂着,
// 前端就一直转圈。超时后直接报错,用户可以重试。
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeoutMs);
let abortReason = '';
const timer = setTimeout(() => { abortReason = 'total'; controller.abort(); }, timeoutMs);
let firstTimer = setTimeout(() => { abortReason = 'first'; controller.abort(); }, Math.min(firstTokenMs, timeoutMs));
const clearTimers = () => { clearTimeout(timer); if (firstTimer) clearTimeout(firstTimer); firstTimer = null; };
const startedAt = Date.now();
let resp;
const abortError = () => {
if (abortReason === 'first') {
const e = new Error(`${label} ${Math.round(firstTokenMs / 1000)} 秒内没有开始输出(服务器繁忙在排队)`);
e.slow = true;
return e;
}
return new Error(`${label} 超过 ${Math.round(timeoutMs / 1000)} 秒没有返回结果,请重试`);
};
let content = '', finish = null, usage = null;
try {
resp = await fetch(`${provider.base}/chat/completions`, {
const resp = await fetch(`${provider.base}/chat/completions`, {
signal: controller.signal,
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${provider.apiKey}`,
},
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${provider.apiKey}` },
body: JSON.stringify({
model,
messages,
temperature,
max_tokens: maxTokens,
model, messages, temperature, max_tokens: maxTokens,
stream: true, stream_options: { include_usage: true },
...(provider.extraBody || {}),
}),
});
} catch (e) {
clearTimeout(timer);
if (e.name === 'AbortError') throw new Error(`${label} 超过 ${Math.round(timeoutMs / 1000)} 秒没有返回结果,请重试`);
throw e;
}
if (!resp.ok) {
clearTimeout(timer);
const errText = await resp.text().catch(() => '');
const err = new Error(`${label} API error ${resp.status}: ${errText.slice(0, 200)}`);
err.status = resp.status;
throw err;
}
let data;
try { data = await resp.json(); }
catch (e) {
if (e.name === 'AbortError') throw new Error(`${label} 超过 ${Math.round(timeoutMs / 1000)} 秒没有返回结果,请重试`);
const decoder = new TextDecoder();
let buf = '';
for await (const chunk of resp.body) {
buf += decoder.decode(chunk, { stream: true });
let nl;
while ((nl = buf.indexOf('\n')) >= 0) {
const line = buf.slice(0, nl).trim();
buf = buf.slice(nl + 1);
if (!line.startsWith('data:')) continue; // 空行、": keep-alive" 注释
const payload = line.slice(5).trim();
if (payload === '[DONE]') continue;
let j;
try { j = JSON.parse(payload); } catch (e) { continue; }
if (j.error) {
const err = new Error(`${label} API error: ${JSON.stringify(j.error).slice(0, 200)}`);
err.status = 503;
throw err;
}
if (j.usage) usage = j.usage;
const ch = j.choices && j.choices[0];
if (!ch) continue;
const piece = ch.delta && (ch.delta.content || ch.delta.reasoning_content);
if (piece && firstTimer) { clearTimeout(firstTimer); firstTimer = null; }
if (ch.delta && ch.delta.content) content += ch.delta.content;
if (ch.finish_reason) finish = ch.finish_reason;
}
}
} catch (e) {
clearTimers();
if (e.name === 'AbortError' || controller.signal.aborted) throw abortError();
throw e;
} finally { clearTimeout(timer); }
const choice = data.choices && data.choices[0];
}
clearTimers();
// 记录耗时和 token 用量,以后再遇到"很慢"可以直接在日志里看到原因
console.log(`[${label}] ${model} ${Math.round((Date.now() - startedAt) / 1000)}s, finish=${choice && choice.finish_reason}, usage=${JSON.stringify(data.usage || {})}`);
const content = choice && choice.message && choice.message.content;
console.log(`[${label}] ${model} ${Math.round((Date.now() - startedAt) / 1000)}s, finish=${finish}, usage=${JSON.stringify(usage || {})}`);
if (!content) {
const reason = choice && choice.finish_reason ? `(finish_reason: ${choice.finish_reason})` : '';
const reason = finish ? `(finish_reason: ${finish})` : '';
throw new Error(`${label} API returned an empty response${reason}`);
}
return content;
@ -222,6 +249,7 @@ async function callWithRetries(provider, messages, opts, backoffs, emptyRetries)
try {
return await callLLM(provider, messages, opts);
} catch (e) {
if (e.slow) { e.busyExhausted = true; e.busyTries = busyTries; throw e; } // 排队太久:不再死等,交给上层切换
if (isBusyError(e)) {
if (busyTries >= backoffs.length) { e.busyExhausted = true; e.busyTries = busyTries; throw e; }
const wait = backoffs[busyTries++];
@ -254,7 +282,7 @@ async function callDeepSeekWithRetry(apiKey, messages, opts, retries = 1) {
} catch (e) {
if (!(e.busyExhausted || (hasBackup && /empty response/i.test(e.message)))) throw e;
if (!hasBackup) {
const fe = new Error(`DeepSeek 服务器繁忙(${e.status || '网络错误'}),已自动重试 ${e.busyTries} 次仍失败。这是 DeepSeek 那边高峰期过载,请过几分钟再试;也可以在"设置"里配置备用的阿里云。`);
const fe = new Error(e.slow ? `${e.message}。请过几分钟再试;也可以在"设置"里配置备用的阿里云。` : `DeepSeek 服务器繁忙(${e.status || '网络错误'}),已自动重试 ${e.busyTries} 次仍失败。这是 DeepSeek 那边高峰期过载,请过几分钟再试;也可以在"设置"里配置备用的阿里云。`);
fe.status = e.status;
throw fe;
}
@ -752,10 +780,44 @@ moduleDimensions/moduleWeight这组字段里就行,cabinetXxx那组(cabinetM
只返回一个JSON数组,不要有任何解释文字、不要用markdown代码块包裹、不要省略任何一个识别到的型号。`;
// 后台任务:AI 导入可能要一两分钟(DeepSeek 排队、生成长输出),浏览器一直挂着一个请求很容易被
// 中间的代理/CDN 掐断(前端只看到 "Failed to fetch",后台其实还在跑)。改成:上传后立刻返回 jobId,
// 前端每隔几秒来查一次结果。任务结果保留 30 分钟。
const aiJobs = new Map();
function createAiJob() {
const id = crypto.randomBytes(9).toString('hex');
const job = { status: 'running', startedAt: Date.now(), code: 0, data: null };
aiJobs.set(id, job);
for (const [k, j] of aiJobs) if (Date.now() - j.startedAt > 30 * 60 * 1000) aiJobs.delete(k);
// 伪装成 express 的 res,原来的处理逻辑一行不用改
const fakeRes = {
status(code) { job.code = code; return fakeRes; },
json(obj) {
if (!job.code) job.code = 200;
job.status = job.code < 400 ? 'done' : 'error';
job.data = obj;
if (job.status === 'error') console.warn(`[AI任务 ${id}] 失败 ${job.code}: ${(obj && obj.error) || ''}`);
else console.log(`[AI任务 ${id}] 完成,用时 ${Math.round((Date.now() - job.startedAt) / 1000)}s`);
return fakeRes;
},
};
return { id, fakeRes };
}
app.get('/api/ai/jobs/:id', requireAuth, (req, res) => {
const job = aiJobs.get(String(req.params.id));
if (!job) return res.status(404).json({ error: '任务不存在或已过期,请重新导入' });
res.json({ status: job.status, code: job.code, data: job.data, elapsed: Math.round((Date.now() - job.startedAt) / 1000) });
});
app.post('/api/ai/parse-catalog', requireAuth, aiLimiter, (req, res) => {
uploadMemory.array('files', 30)(req, res, async (err) => {
if (err) return res.status(400).json({ error: err.message });
if (!req.files || !req.files.length) return res.status(400).json({ error: 'no file received' });
if (req.query.async === '1') {
const job = createAiJob();
res.status(202).json({ jobId: job.id });
res = job.fakeRes;
}
const cfg = loadAiConfig();
if (!cfg.apiKey && !cfg.backup.apiKey) return res.status(400).json({ error: 'AI API key not configured(设置里填 DeepSeek 或阿里云的 Key)' });
@ -844,6 +906,7 @@ app.post('/api/ai/parse-catalog', requireAuth, aiLimiter, (req, res) => {
const hint = /empty response/i.test(e.message)
? '(重试后仍然失败,可能是一次上传的文件太多导致AI输出内容超长;建议减少单次上传的数量,比如分成2-3批,每批3-5张)'
: '';
console.warn('[AI导入] 失败:', e.message);
res.status(502).json({ error: `${e.message}${hint}` });
} finally {
try { fs.rmSync(tmpDir, { recursive: true, force: true }); } catch (e) {}