fix(agent): strip thinking tags from text content
This commit is contained in:
@@ -47,6 +47,44 @@ function stripDowngradedToolCallText(text: string): string {
|
||||
return cleaned.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* Strip thinking tags and their content from text.
|
||||
* This is a safety net for cases where the model outputs <think> tags
|
||||
* that slip through other filtering mechanisms.
|
||||
*/
|
||||
function stripThinkingTagsFromText(text: string): string {
|
||||
if (!text) return text;
|
||||
// Quick check to avoid regex overhead when no tags present.
|
||||
if (!/(?:think(?:ing)?|thought|antthinking)/i.test(text)) return text;
|
||||
|
||||
const tagRe = /<\s*(\/?)\s*(?:think(?:ing)?|thought|antthinking)\s*>/gi;
|
||||
let result = "";
|
||||
let lastIndex = 0;
|
||||
let inThinking = false;
|
||||
|
||||
for (const match of text.matchAll(tagRe)) {
|
||||
const idx = match.index ?? 0;
|
||||
const isClose = match[1] === "/";
|
||||
|
||||
if (!inThinking && !isClose) {
|
||||
// Opening tag - save text before it.
|
||||
result += text.slice(lastIndex, idx);
|
||||
inThinking = true;
|
||||
} else if (inThinking && isClose) {
|
||||
// Closing tag - skip content inside.
|
||||
inThinking = false;
|
||||
}
|
||||
lastIndex = idx + match[0].length;
|
||||
}
|
||||
|
||||
// Append remaining text if we're not inside thinking.
|
||||
if (!inThinking) {
|
||||
result += text.slice(lastIndex);
|
||||
}
|
||||
|
||||
return result.trim();
|
||||
}
|
||||
|
||||
export function extractAssistantText(msg: AssistantMessage): string {
|
||||
const isTextBlock = (block: unknown): block is { type: "text"; text: string } => {
|
||||
if (!block || typeof block !== "object") return false;
|
||||
@@ -58,7 +96,9 @@ export function extractAssistantText(msg: AssistantMessage): string {
|
||||
? msg.content
|
||||
.filter(isTextBlock)
|
||||
.map((c) =>
|
||||
stripDowngradedToolCallText(stripMinimaxToolCallXml(c.text)).trim(),
|
||||
stripThinkingTagsFromText(
|
||||
stripDowngradedToolCallText(stripMinimaxToolCallXml(c.text)),
|
||||
).trim(),
|
||||
)
|
||||
.filter(Boolean)
|
||||
: [];
|
||||
|
||||
Reference in New Issue
Block a user