From efef930283ee12ae10d004b5b428ceb61af7540c Mon Sep 17 00:00:00 2001 From: Centauria <20443216+Centauria@users.noreply.github.com> Date: Wed, 18 Nov 2026 15:28:54 +0800 Subject: [PATCH] =?UTF-8?q?feat(llm):=20=E5=A3=B0=E6=98=8E=E7=A2=8E?= =?UTF-8?q?=E7=A2=8E=E5=BF=B5=E4=B8=8E=E9=99=AA=E8=81=8A=E7=9A=84=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E6=80=A7=E8=B4=A8=EF=BC=8C=E9=81=BF=E5=85=8D=E4=BC=9A?= =?UTF-8?q?=E8=AF=9D=E5=88=97=E8=A1=A8=E8=A2=AB=E5=88=B7=E5=B1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 根因:两处 `llm.stream` 只带 `sessionId: 'dsh-pet'`,不带调用性质。代理只能按内容猜——billion-context 对 dsh 插件流量按 system 指纹分车道,而碎碎念的 system 含实时时间(`timeContextOf()` 现算), 指纹每条都变,于是每条碎碎念派生一条 `dsh-pet|sub:` 会话;实测 1148 条会话里 956 条是这种 1 请求记录。 - 实现:`llm-call.ts` 新增 `PET_WHISPER_PURPOSE` / `PET_CHAT_PURPOSE`,两处调用把 dsh-llm 的 `GenerateOptions.purpose` 一起传下去。代理据此走 side 车道并归并到调用身份(billion-context #6880 / #6883)。 - 行为变化:只多一个透传字段。dsh-llm 运行时不做 purpose 校验(纯透传),不认识该字段的代理直接忽略, 不使用 billion-context 的部署无变化;`sessionId` 保持不变(缺它时 opencode-go 回 400)。 - 测试:新增 `src/host/llm-call.test.ts`,钉住声明值必须能进请求头(可打印 ASCII、≤64)且不等于代理保留值 `conversation`(等值会被丢弃,问题静默复发)。 - 涉及文件:`src/host/llm-call.ts`、`src/host/whisper.ts`、`src/host/chat.ts`、`src/host/llm-call.test.ts` --- dsh-pet/src/host/chat.ts | 4 +++- dsh-pet/src/host/llm-call.test.ts | 36 +++++++++++++++++++++++++++++++ dsh-pet/src/host/llm-call.ts | 13 +++++++++++ dsh-pet/src/host/whisper.ts | 4 +++- 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 dsh-pet/src/host/llm-call.test.ts diff --git a/dsh-pet/src/host/chat.ts b/dsh-pet/src/host/chat.ts index a290d57e0..394d2be2b 100644 --- a/dsh-pet/src/host/chat.ts +++ b/dsh-pet/src/host/chat.ts @@ -13,7 +13,7 @@ import { BlockAssembler, createAssistantMessage, createUserMessage, ReasoningEffortId } from '@deepseek-ai/dsh-llm'; import { actionInstruction, extractChatAction } from './chat-action.ts'; import type { PetImageRef } from './image-attachment.ts'; -import { PET_CALL_IDENTITY, noTextReason } from './llm-call.ts'; +import { PET_CALL_IDENTITY, PET_CHAT_PURPOSE, noTextReason } from './llm-call.ts'; import { supportsReasoningOff } from './llm-reasoning.ts'; import { defaultLog } from './log-file.ts'; import { extractChatImage, memeCatalog, type MemeEntry } from './memes.ts'; @@ -205,6 +205,8 @@ async function generateWith(ctx: { llm?: unknown }, once: GenerateOnce): Promise model: sel.model, // 调用身份(见 llm-call.ts):缺它时 opencode-go 回 400 sessionId: PET_CALL_IDENTITY, + // 调用性质(见 llm-call.ts):声明为辅助调用,代理据此不再按 system 指纹另开会话 + purpose: PET_CHAT_PURPOSE, messages: [ ...historyMessages, createUserMessage({ diff --git a/dsh-pet/src/host/llm-call.test.ts b/dsh-pet/src/host/llm-call.test.ts new file mode 100644 index 000000000..cd2cfebc6 --- /dev/null +++ b/dsh-pet/src/host/llm-call.test.ts @@ -0,0 +1,36 @@ +/** + * 调用性质常量测试 —— 钉住「声明值必须能安全进请求头」这条约束:代理把 `purpose` 原样放进 + * `x-bili-plugin-purpose`,只收可打印 ASCII、长度 ≤64;而 `conversation` 是它的保留值, + * 等值声明会被丢弃,分车道问题随即静默复发。 + * + * 只 import 纯常量模块,不拉起 dsh-llm 依赖链。 + * + * 跑法:node --experimental-strip-types --test src/host/llm-call.test.ts + */ +import { test, describe } from 'node:test'; +import assert from 'node:assert/strict'; + +import { PET_CHAT_PURPOSE, PET_WHISPER_PURPOSE } from './llm-call.ts'; + +/** 代理侧请求头校验的等价规则:可打印 ASCII(无空格/控制符)、非空、长度 ≤64。 */ +const HEADER_SAFE = /^[\x21-\x7e]{1,64}$/; + +describe('调用性质 —— 代理请求头约束', () => { + for (const [label, value] of [ + ['碎碎念', PET_WHISPER_PURPOSE], + ['陪聊', PET_CHAT_PURPOSE], + ] as const) { + test(`${label}的声明值可直接进请求头`, () => { + assert.match(value, HEADER_SAFE); + }); + } + + test('两者互不相同 —— 否则代理的 lane 标签分不出两条调用路径', () => { + assert.notEqual(PET_WHISPER_PURPOSE, PET_CHAT_PURPOSE); + }); + + test('都不落在代理保留值 conversation 上 —— 等值声明会被丢弃,问题静默复发', () => { + assert.notEqual(PET_WHISPER_PURPOSE, 'conversation'); + assert.notEqual(PET_CHAT_PURPOSE, 'conversation'); + }); +}); diff --git a/dsh-pet/src/host/llm-call.ts b/dsh-pet/src/host/llm-call.ts index 274de0bb1..b5ba6bc5f 100644 --- a/dsh-pet/src/host/llm-call.ts +++ b/dsh-pet/src/host/llm-call.ts @@ -13,6 +13,19 @@ */ export const PET_CALL_IDENTITY = 'dsh-pet'; +/** + * 碎碎念的调用性质(dsh-llm `GenerateOptions.purpose`):辅助调用,不是对话。 + * + * 不声明时下游只能按内容猜:billion-context 对 dsh 插件流量按 `system` 指纹分车道, + * 而碎碎念的 system 带实时时间,于是每条都派生一条 `<身份>|sub:` 会话记录, + * 几分钟一条,把正常会话挤出列表。声明后走 side 车道并归并到调用身份(#6880/#6883)。 + * 只为可打印 ASCII、长度 ≤64:该值会被代理原样放进请求头。 + */ +export const PET_WHISPER_PURPOSE = 'pet-whisper'; + +/** 陪聊的调用性质:同样是辅助调用,不占会话车道(理由同 `PET_WHISPER_PURPOSE`)。 */ +export const PET_CHAT_PURPOSE = 'pet-chat'; + /** * 没有正文时,按流终止原因给出要展示的原因。 * @param finish - 流终止块(`BlockAssembler.finish`;dsh-llm 把适配器抛错折进这里,不在流里抛出)。 diff --git a/dsh-pet/src/host/whisper.ts b/dsh-pet/src/host/whisper.ts index ce36c9933..a2021a592 100644 --- a/dsh-pet/src/host/whisper.ts +++ b/dsh-pet/src/host/whisper.ts @@ -11,7 +11,7 @@ */ import { BlockAssembler, createUserMessage, ReasoningEffortId } from '@deepseek-ai/dsh-llm'; -import { PET_CALL_IDENTITY, noTextReason } from './llm-call.ts'; +import { PET_CALL_IDENTITY, PET_WHISPER_PURPOSE, noTextReason } from './llm-call.ts'; import { supportsReasoningOff } from './llm-reasoning.ts'; import { defaultLog } from './log-file.ts'; import { modelCandidates, modelLabel, type ModelRef } from './model-selection.ts'; @@ -120,6 +120,8 @@ async function generateWith( model: sel.model, // 调用身份(见 llm-call.ts):缺它时 opencode-go 回 400 sessionId: PET_CALL_IDENTITY, + // 调用性质(见 llm-call.ts):声明为辅助调用,代理据此不再按 system 指纹另开会话 + purpose: PET_WHISPER_PURPOSE, messages: [ createUserMessage({ content: [