feat(llm): 支持增量上下文,为模型加 store: true 即走 relay
- 新增 instance.js:data_root/instance-id,首次启动生成 12 位 hex,跨 core 唯一 - engine 传 context_id = <core_id>:<user>:<session_id>,三层定位不撞车 - callModel 对 store: true 的模型只发新增消息 + X-Context-Id/Hash, 409 时改发全量重试一次;指针存内存,重启自动回落全量 - config.js 把 store 字段透传到模型对象(以前会被丢掉) - 未配 store 的模型行为完全不变
This commit is contained in:
+45
-2
@@ -2,6 +2,14 @@
|
||||
// 累积 content 与 tool_calls 分片;支持 abort。
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const crypto = require('node:crypto');
|
||||
|
||||
// 增量上下文:context_id -> { hash, len }(本进程认为 relay 已存到哪一段)。
|
||||
// 只存指针不存正文;进程重启即清空 → 下一轮发全量,relay 整段替换,行为正确。
|
||||
const ctxState = new Map();
|
||||
function ctxHash(messages) {
|
||||
return crypto.createHash('sha256').update(JSON.stringify(messages)).digest('hex');
|
||||
}
|
||||
|
||||
const SYSTEM_PROMPT_TEMPLATE = fs.readFileSync(path.join(__dirname, 'templates', 'profile-default.txt'), 'utf8');
|
||||
|
||||
@@ -714,18 +722,53 @@ async function callModel(cfg, modelId, messages, tools, options) {
|
||||
opts.onFetchParams(logBody);
|
||||
}
|
||||
|
||||
const res = await fetch(modelCfg.url + '/chat/completions', {
|
||||
// ---- 增量上下文(context relay)----
|
||||
// 仅对显式 store: true 的模型生效,且必须带 opts.context_id。
|
||||
// 不做前缀比对:压缩/改写/relay 重启一律靠 409 回退全量,客户端只负责“接着上次发”。
|
||||
const cid = opts.context_id;
|
||||
const storeOn = !!(modelCfg.store && cid);
|
||||
const fullBody = JSON.stringify(body);
|
||||
if (storeOn) {
|
||||
const st = ctxState.get(cid);
|
||||
headers['X-Context-Id'] = cid;
|
||||
if (st && messages.length > st.len) {
|
||||
body.messages = messages.slice(st.len); // 只发新增
|
||||
headers['X-Context-Hash'] = st.hash;
|
||||
} else {
|
||||
headers['X-Context-Hash'] = ''; // 首轮 / 压缩后:全量替换
|
||||
}
|
||||
}
|
||||
|
||||
let res = await fetch(modelCfg.url + '/chat/completions', {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify(body),
|
||||
body: storeOn ? JSON.stringify(body) : fullBody,
|
||||
signal: controller.signal,
|
||||
});
|
||||
|
||||
// relay 说 hash 对不上(缓存没了 / 历史被改写):改发全量重试一次。
|
||||
// 只重试一次:hash="" 对 relay 是“整段替换”,重发后不可能再 409。
|
||||
if (storeOn && res.status === 409) {
|
||||
body.messages = messages;
|
||||
headers['X-Context-Hash'] = '';
|
||||
res = await fetch(modelCfg.url + '/chat/completions', {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify(body),
|
||||
signal: controller.signal,
|
||||
});
|
||||
}
|
||||
|
||||
if (!res.ok) {
|
||||
const text = await res.text().catch(() => '');
|
||||
const err = new Error('上游返回 ' + res.status + ': ' + text.slice(0, 500));
|
||||
err.upstream_status = res.status;
|
||||
throw err;
|
||||
}
|
||||
// 成功才更新“relay 已存到哪”:失败下一轮自然重发全量。
|
||||
if (storeOn) {
|
||||
ctxState.set(cid, { hash: ctxHash(messages), len: messages.length });
|
||||
}
|
||||
|
||||
let content = '';
|
||||
let reasoning = '';
|
||||
|
||||
Reference in New Issue
Block a user