后台挂起功能修改

This commit is contained in:
董宏凯 committed 2026-06-12 21:38:20 +08:00
1 parent cca954d01c
commit f0c85c4795
8 files changed
+126 -13

No files matched your search

@@ -1,15 +1,18 @@
/**
* 把模型流式文本里的 `<think>...</think>` 块拆出来,作为「思考过程」单独展示。
*
* 兼容三种形态:
* 兼容四种形态:
* 1) **完整** `<think>foo</think>bar` —— 提取 `foo` 作 reasoning,正文为 `bar`
* 2) **多段** `<think>a</think>x<think>b</think>y` —— reasoning 合并为 `a\n\nb`,正文为 `xy`
* 3) **流式未闭合** `bar<think>partial...` —— reasoning 为 `partial...`(尾段),正文为 `bar`
* 4) **模板注入式(只吐闭合)** `foo</think>bar` —— 开标签 `<think>` 被 chat template
* 注入到 prompt 里、模型只生成闭合标签(DeepSeek-R1 / QwQ 等推理模型的典型行为)。
* 此时首个 `</think>` 之前没有配对的 `<think>`,把它之前的内容整体算 reasoning,正文为 `bar`。
*
* 实现思路与主聊天 `src/core/messages/utils.ts::splitInlineReasoning` 对齐,但
* 这里**额外处理了未闭合的 `<think>`** —— 流式渲染时如果模型还没吐出 `</think>`,
* 主聊天那份实现会把 `<think>` 字面量留在正文里,我们用 lastIndexOf 兜底,把尾段
* 也算作 reasoning。
* 这里**额外处理了未闭合的 `<think>`(形态 3)和只吐闭合的 `</think>`(形态 4)** ——
* 主聊天靠后端 `additional_kwargs.reasoning_content` 单列推理,而圆桌多智能体流式只取
* `content` 文本,推理标签会内联在正文里,故这里需要把这两种内联形态都兜住。
*
* Inline `<think>foo` 在文本中间出现(无尾闭合)被认为是流式未闭合的开头:
* 假设之后会闭合,把它之后所有内容算 reasoning。这与 DeepSeek-R1 / QwQ 等
@@ -18,6 +21,9 @@
const THINK_TAG_RE = /<think>\s*([\s\S]*?)\s*<\/think>/g;
const OPEN_TAG = "<think>";
const CLOSE_TAG = "</think>";
// 兜底:正文里绝不应残留任何 think 标签字面量,清掉任何孤立的 `<think>` / `</think>`。
const ANY_THINK_TAG_RE = /<\/?think>/gi;
export interface SplitReasoningResult {
/** 正文(去掉所有 `<think>...</think>` 块以及尾段未闭合 `<think>` 之后内容)。 */
@@ -30,14 +36,27 @@ export function splitInlineReasoning(text: string): SplitReasoningResult {
if (!text) return { content: "", reasoning: null };
const reasoningParts: string[] = [];
// 先处理所有完整 `<think>...</think>` 闭合块。
const closed = text.replace(THINK_TAG_RE, (_, reasoning: string) => {
// 形态 4(模板注入式,只吐闭合):首个 `</think>` 出现在任何 `<think>` 之前(或根本
// 没有 `<think>`),说明开标签被 chat template 注入、模型只生成了闭合标签。把第一个
// `</think>` 之前的内容整体算作 reasoning,正文从闭合标签之后继续解析。
let working = text;
const firstClose = working.indexOf(CLOSE_TAG);
const firstOpen = working.indexOf(OPEN_TAG);
if (firstClose >= 0 && (firstOpen < 0 || firstClose < firstOpen)) {
const lead = working.slice(0, firstClose).trim();
if (lead) reasoningParts.push(lead);
working = working.slice(firstClose + CLOSE_TAG.length);
}
// 形态 1/2:处理所有完整 `<think>...</think>` 闭合块。
const closed = working.replace(THINK_TAG_RE, (_, reasoning: string) => {
const normalized = reasoning.trim();
if (normalized) reasoningParts.push(normalized);
return "";
});
// 处理尾段未闭合 `<think>` —— 流式情况。
// 形态 3:尾段未闭合 `<think>` —— 流式情况。
// 如果 closed 里还残留 `<think>` 字面量,说明这一段还没收到 `</think>`,
// 把它之后的所有内容视为 reasoning-in-progress,正文截到 `<think>` 之前。
let finalContent = closed;
@@ -48,8 +67,16 @@ export function splitInlineReasoning(text: string): SplitReasoningResult {
finalContent = closed.slice(0, openIdx);
}
// 安全兜底:无论上面是否完全配对,正文里都不该再出现 think 标签字面量。
finalContent = finalContent.replace(ANY_THINK_TAG_RE, "");
return {
content: finalContent.trim(),
reasoning: reasoningParts.length > 0 ? reasoningParts.join("\n\n").trim() : null,
};
}
/** 只要正文、丢弃推理过程的便捷封装(用于「交付内容」类只需主体输出的场景)。 */
export function stripReasoning(text: string): string {
return splitInlineReasoning(text).content;
}