fix(agents): strip [Historical context: ...] and tool call text from streaming path (#13453)

- Add [Historical context: ...] marker pattern to stripDowngradedToolCallText
- Apply stripDowngradedToolCallText in emitBlockChunk streaming path
- Previously only stripBlockTags ran during streaming, leaking [Tool Call: ...] markers to users
- Add 7 test cases for the new pattern stripping
This commit is contained in:
Cklee
2026-02-11 05:04:52 +09:00
committed by GitHub
parent 67d25c6533
commit 22458f57f2
3 changed files with 48 additions and 4 deletions

View File

@@ -15,7 +15,7 @@ import {
normalizeTextForComparison,
} from "./pi-embedded-helpers.js";
import { createEmbeddedPiSessionEventHandler } from "./pi-embedded-subscribe.handlers.js";
import { formatReasoningMessage } from "./pi-embedded-utils.js";
import { formatReasoningMessage, stripDowngradedToolCallText } from "./pi-embedded-utils.js";
import { hasNonzeroUsage, normalizeUsage, type UsageLike } from "./usage.js";
const THINKING_TAG_SCAN_RE = /<\s*(\/?)\s*(?:think(?:ing)?|thought|antthinking)\s*>/gi;
@@ -449,7 +449,8 @@ export function subscribeEmbeddedPiSession(params: SubscribeEmbeddedPiSessionPar
return;
}
// Strip <think> and <final> blocks across chunk boundaries to avoid leaking reasoning.
const chunk = stripBlockTags(text, state.blockState).trimEnd();
// Also strip downgraded tool call text ([Tool Call: ...], [Historical context: ...], etc.).
const chunk = stripDowngradedToolCallText(stripBlockTags(text, state.blockState)).trimEnd();
if (!chunk) {
return;
}