fix: strip thinking tag artifacts leaked into reasoning_content
Qoder's backend sometimes routes a literal <thinking> opener into reasoning_content while the matching closer lands in the content stream, leaving tag fragments in the thinking block. Strip them during streaming and add live repro scripts.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import type { Api, AssistantMessage, AssistantMessageEvent, AssistantMessageEventStream } from "@earendil-works/pi-ai";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { ThinkingTagParser } from "../thinking-parser.js";
|
||||
import { stripThinkingTags, ThinkingTagParser } from "../thinking-parser.js";
|
||||
|
||||
function createMockStream() {
|
||||
const events: AssistantMessageEvent[] = [];
|
||||
@@ -240,4 +240,68 @@ describe("ThinkingTagParser", () => {
|
||||
expect(output.content).toHaveLength(1);
|
||||
expect(output.content[0]).toMatchObject({ type: "thinking", thinking: "unfinished" });
|
||||
});
|
||||
|
||||
// ── Orphan closing tags (opener arrived via reasoning_content) ─────────
|
||||
// Regression: Qoder's backend splits one `<thinking>...</thinking>` pair
|
||||
// across two SSE fields — opener+reasoning into `reasoning_content`, closer
|
||||
// +answer into `content`. The closer has no opener in the content stream, so
|
||||
// it must be dropped instead of leaking into visible text.
|
||||
|
||||
it("drops an orphan closing tag at the start of content", () => {
|
||||
const parser = new ThinkingTagParser(output, stream);
|
||||
parser.processChunk("</thinking>\n\n让我查一下这个选项");
|
||||
parser.finalize();
|
||||
|
||||
const textBlocks = output.content.filter((c) => c.type === "text");
|
||||
expect(textBlocks).toHaveLength(1);
|
||||
const text = (textBlocks[0] as { type: string; text: string }).text;
|
||||
expect(text).toBe("让我查一下这个选项");
|
||||
expect(text).not.toContain("</thinking>");
|
||||
expect(output.content.some((c) => c.type === "thinking")).toBe(false);
|
||||
});
|
||||
|
||||
it("drops an orphan closer split across stream chunks", () => {
|
||||
const parser = new ThinkingTagParser(output, stream);
|
||||
parser.processChunk("</think");
|
||||
parser.processChunk("ing>\n\nanswer");
|
||||
parser.finalize();
|
||||
|
||||
const textBlocks = output.content.filter((c) => c.type === "text");
|
||||
expect(textBlocks).toHaveLength(1);
|
||||
const text = (textBlocks[0] as { type: string; text: string }).text;
|
||||
expect(text).toBe("answer");
|
||||
expect(text).not.toContain("</thinking>");
|
||||
expect(text).not.toContain("</think");
|
||||
});
|
||||
|
||||
it("drops an orphan closer for the <reasoning> variant too", () => {
|
||||
const parser = new ThinkingTagParser(output, stream);
|
||||
parser.processChunk("</reasoning>\n\nresult");
|
||||
parser.finalize();
|
||||
|
||||
const text = (output.content.find((c) => c.type === "text") as { type: string; text: string })?.text ?? "";
|
||||
expect(text).toBe("result");
|
||||
expect(text).not.toContain("</reasoning>");
|
||||
});
|
||||
|
||||
it("emits text before an orphan closer, then drops the closer", () => {
|
||||
const parser = new ThinkingTagParser(output, stream);
|
||||
parser.processChunk("intro</thinking>\n\noutro");
|
||||
parser.finalize();
|
||||
|
||||
const text = (output.content.find((c) => c.type === "text") as { type: string; text: string })?.text ?? "";
|
||||
expect(text).not.toContain("</thinking>");
|
||||
expect(text).toContain("intro");
|
||||
expect(text).toContain("outro");
|
||||
});
|
||||
|
||||
// ── stripThinkingTags helper ───────────────────────────────────────────
|
||||
|
||||
it("stripThinkingTags removes opening and closing tag variants", () => {
|
||||
expect(stripThinkingTags("<thinking>hello</thinking>")).toBe("hello");
|
||||
expect(stripThinkingTags("<reasoning>deep</reasoning>")).toBe("deep");
|
||||
expect(stripThinkingTags("plain text")).toBe("plain text");
|
||||
expect(stripThinkingTags("<thinking>")).toBe("");
|
||||
expect(stripThinkingTags("</thinking>")).toBe("");
|
||||
});
|
||||
});
|
||||
|
||||
+21
-13
@@ -23,7 +23,7 @@ import {
|
||||
import { getCachedModelConfig } from "./models.js";
|
||||
import { getCachedCredentials } from "./oauth.js";
|
||||
import { qoderEncodeBody } from "./qoder-encoding.js";
|
||||
import { ThinkingTagParser } from "./thinking-parser.js";
|
||||
import { stripThinkingTags, ThinkingTagParser } from "./thinking-parser.js";
|
||||
import { transformMessagesForQoder, transformTools } from "./transform.js";
|
||||
|
||||
interface ToolCallState {
|
||||
@@ -326,19 +326,27 @@ export function streamQoder(
|
||||
if (delta) {
|
||||
// 1. Process reasoning/thinking content (API reasoning)
|
||||
if (delta.reasoning_content) {
|
||||
if (thinkingBlockIndex === -1) {
|
||||
thinkingBlockIndex = output.content.length;
|
||||
output.content.push({ type: "thinking", thinking: "" });
|
||||
stream.push({ type: "thinking_start", contentIndex: thinkingBlockIndex, partial: output });
|
||||
// Qoder's backend sometimes routes a literal `<thinking>`
|
||||
// opener into reasoning_content (with the matching
|
||||
// `</thinking>` closer landing in the content stream). Strip
|
||||
// tag artifacts so the thinking block stays clean, matching
|
||||
// the SDK's ContentBlock model.
|
||||
const reasoningChunk = stripThinkingTags(delta.reasoning_content);
|
||||
if (reasoningChunk) {
|
||||
if (thinkingBlockIndex === -1) {
|
||||
thinkingBlockIndex = output.content.length;
|
||||
output.content.push({ type: "thinking", thinking: "" });
|
||||
stream.push({ type: "thinking_start", contentIndex: thinkingBlockIndex, partial: output });
|
||||
}
|
||||
const block = output.content[thinkingBlockIndex] as ThinkingContent;
|
||||
block.thinking += reasoningChunk;
|
||||
stream.push({
|
||||
type: "thinking_delta",
|
||||
contentIndex: thinkingBlockIndex,
|
||||
delta: reasoningChunk,
|
||||
partial: output,
|
||||
});
|
||||
}
|
||||
const block = output.content[thinkingBlockIndex] as ThinkingContent;
|
||||
block.thinking += delta.reasoning_content;
|
||||
stream.push({
|
||||
type: "thinking_delta",
|
||||
contentIndex: thinkingBlockIndex,
|
||||
delta: delta.reasoning_content,
|
||||
partial: output,
|
||||
});
|
||||
}
|
||||
|
||||
// 2. Process text content
|
||||
|
||||
+60
-15
@@ -5,7 +5,7 @@ import type {
|
||||
ThinkingContent,
|
||||
} from "@earendil-works/pi-ai";
|
||||
|
||||
const THINKING_TAG_VARIANTS: Array<{ open: string; close: string }> = [
|
||||
export const THINKING_TAG_VARIANTS: Array<{ open: string; close: string }> = [
|
||||
{ open: "<thinking>", close: "</thinking>" },
|
||||
{ open: "<think>", close: "</think>" },
|
||||
{ open: "<reasoning>", close: "</reasoning>" },
|
||||
@@ -28,6 +28,25 @@ function getMaxTrailingPossibleTagPrefixLength(text: string, tags: string[]): nu
|
||||
return maxLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove every thinking/reasoning tag variant (open and close) from `text`.
|
||||
*
|
||||
* Qoder's backend sometimes routes a literal `<thinking>` opener into the
|
||||
* `reasoning_content` channel (and the matching `</thinking>` closer into the
|
||||
* `content` channel). Stripping these artifacts keeps the thinking block clean,
|
||||
* matching the SDK's `ContentBlock` model. Best-effort per chunk: a tag split
|
||||
* across stream deltas is not caught here (the ThinkingTagParser handles the
|
||||
* content-channel side with cross-delta buffering).
|
||||
*/
|
||||
export function stripThinkingTags(text: string): string {
|
||||
let out = text;
|
||||
for (const { open, close } of THINKING_TAG_VARIANTS) {
|
||||
if (open.length > 0 && out.includes(open)) out = out.split(open).join("");
|
||||
if (close.length > 0 && out.includes(close)) out = out.split(close).join("");
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export class ThinkingTagParser {
|
||||
private textBuffer = "";
|
||||
private inThinking = false;
|
||||
@@ -90,27 +109,53 @@ export class ThinkingTagParser {
|
||||
}
|
||||
|
||||
private processBeforeThinking(): void {
|
||||
let bestPos = -1;
|
||||
let bestVariant: (typeof THINKING_TAG_VARIANTS)[number] | null = null;
|
||||
// Find the first opener and first closer in the buffer.
|
||||
let bestOpenPos = -1;
|
||||
let bestOpenVariant: (typeof THINKING_TAG_VARIANTS)[number] | null = null;
|
||||
let bestClosePos = -1;
|
||||
let bestCloseVariant: (typeof THINKING_TAG_VARIANTS)[number] | null = null;
|
||||
for (const variant of THINKING_TAG_VARIANTS) {
|
||||
const pos = this.textBuffer.indexOf(variant.open);
|
||||
if (pos !== -1 && (bestPos === -1 || pos < bestPos)) {
|
||||
bestPos = pos;
|
||||
bestVariant = variant;
|
||||
const openPos = this.textBuffer.indexOf(variant.open);
|
||||
if (openPos !== -1 && (bestOpenPos === -1 || openPos < bestOpenPos)) {
|
||||
bestOpenPos = openPos;
|
||||
bestOpenVariant = variant;
|
||||
}
|
||||
const closePos = this.textBuffer.indexOf(variant.close);
|
||||
if (closePos !== -1 && (bestClosePos === -1 || closePos < bestClosePos)) {
|
||||
bestClosePos = closePos;
|
||||
bestCloseVariant = variant;
|
||||
}
|
||||
}
|
||||
if (bestPos !== -1 && bestVariant) {
|
||||
if (bestPos > 0) this.emitText(this.textBuffer.slice(0, bestPos));
|
||||
this.textBuffer = this.textBuffer.slice(bestPos + bestVariant.open.length);
|
||||
this.activeEndTag = bestVariant.close;
|
||||
|
||||
// Opener comes first (or is the only tag): a real thinking block carried
|
||||
// in the content stream. Enter thinking mode; processInsideThinking will
|
||||
// handle its closer.
|
||||
if (bestOpenVariant !== null && (bestCloseVariant === null || bestOpenPos < bestClosePos)) {
|
||||
if (bestOpenPos > 0) this.emitText(this.textBuffer.slice(0, bestOpenPos));
|
||||
this.textBuffer = this.textBuffer.slice(bestOpenPos + bestOpenVariant.open.length);
|
||||
this.activeEndTag = bestOpenVariant.close;
|
||||
this.inThinking = true;
|
||||
return;
|
||||
}
|
||||
|
||||
const trailingPrefixLength = getMaxTrailingPossibleTagPrefixLength(
|
||||
this.textBuffer,
|
||||
THINKING_TAG_VARIANTS.map((variant) => variant.open),
|
||||
);
|
||||
// Closer with no preceding opener: an orphan close tag. Its matching
|
||||
// opener was delivered via the separate `reasoning_content` channel (see
|
||||
// stream.ts), so there is no thinking block to close here. Drop it — and
|
||||
// the separator whitespace the model emits right after `</thinking>` — so
|
||||
// it does not leak into visible text.
|
||||
if (bestCloseVariant !== null) {
|
||||
if (bestClosePos > 0) this.emitText(this.textBuffer.slice(0, bestClosePos));
|
||||
this.textBuffer = this.textBuffer.slice(bestClosePos + bestCloseVariant.close.length);
|
||||
if (this.textBuffer.startsWith("\n\n")) this.textBuffer = this.textBuffer.slice(2);
|
||||
else if (this.textBuffer.startsWith("\n")) this.textBuffer = this.textBuffer.slice(1);
|
||||
return;
|
||||
}
|
||||
|
||||
// No complete tag yet. Hold back any trailing prefix that could be the
|
||||
// start of an opener OR a closer, so a tag split across stream deltas is
|
||||
// not partially emitted as text.
|
||||
const allTags = THINKING_TAG_VARIANTS.flatMap((variant) => [variant.open, variant.close]);
|
||||
const trailingPrefixLength = getMaxTrailingPossibleTagPrefixLength(this.textBuffer, allTags);
|
||||
const safeLen = this.textBuffer.length - trailingPrefixLength;
|
||||
if (safeLen > 0) {
|
||||
this.emitText(this.textBuffer.slice(0, safeLen));
|
||||
|
||||
Reference in New Issue
Block a user