perf(增量上下文): 数组未变时发空增量,不再整段重传
两个场景会重建出与上一轮完全相同的数组: - engine 的重试循环(429 / 5xx / 流中断)复用同一份 messages - 会话中途切模型,cid 不含模型名,消息数不变 原来这两种都走 Reset 全量(1.5MB),而 relay 里明明有一模一样的缓存。 现在发空 messages 数组,relay 用缓存原样转发,上传约 200 字节。 缓存不在时 relay 回 409,走既有的重试分支(带 Reset 全量),自愈。 六条路径回归:首轮全量 / 正常追加 1 条 / 切模型 0 条 / 纯重试 0 条 / 编辑截断全量 / 空增量遇 409 回落全量;__rowId 全程零泄漏。
This commit is contained in:
+11
-5
@@ -750,13 +750,19 @@ async function callModel(cfg, modelId, messages, tools, options) {
|
||||
if (storeOn) {
|
||||
const st = ctxState.get(cid);
|
||||
headers['X-Context-Id'] = cid;
|
||||
// 锚点:上一轮发出去的数组里最后一条库行的 messages.id。
|
||||
const anchor = st && st.len > 0 ? messages[st.len - 1] : null;
|
||||
const anchored = !!(st && messages.length > st.len && anchor && anchor.__rowId === st.lastRowId);
|
||||
if (anchored) {
|
||||
// body.messages 此时是已剥键的全量数组,与 messages 同序同长,索引可直接沿用。
|
||||
body.messages = strippedFull.slice(st.len); // 只发新增
|
||||
const anchorOk = !!(st && anchor && anchor.__rowId === st.lastRowId);
|
||||
if (anchorOk && messages.length === st.len) {
|
||||
// 数组与上次一模一样(错误重试 / 切模型但没新增消息):发空增量,
|
||||
// relay 用缓存原样转发即可。缓存不在时它会回 409,下面重试一次全量。
|
||||
body.messages = [];
|
||||
} else if (anchorOk && messages.length > st.len) {
|
||||
// 前缀没动:只发新增。strippedFull 与 messages 同序同长,索引可直接沿用。
|
||||
body.messages = strippedFull.slice(st.len);
|
||||
} else {
|
||||
headers['X-Context-Reset'] = '1'; // 全量:首轮 / 进程重启 / 压缩后新 id / 历史被改
|
||||
// 全量:首轮 / 进程重启 / 压缩后新 id / 历史被改
|
||||
headers['X-Context-Reset'] = '1';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user