fix: strip antml thinking tags

This commit is contained in:
Ayaan Zaidi
2026-04-02 13:47:46 +05:30
parent 251ba9b4d2
commit 176ff18d18
3 changed files with 17 additions and 2 deletions

View File

@@ -461,6 +461,11 @@ File contents here`,
text: "<antthinking>Some reasoning</antthinking>The actual answer.",
expected: "The actual answer.",
},
{
name: "antml namespaced thinking tag",
text: "<antml:thinking>This shows Robin Waslander DMing maintainers o...</antml:thinking>Actual reply.",
expected: "Actual reply.",
},
{
name: "final wrapper",
text: "<final>\nAnswer\n</final>",

View File

@@ -50,6 +50,11 @@ describe("stripReasoningTagsFromText", () => {
input: "X <antthinking>internal</antthinking> Y",
expected: "X Y",
},
{
name: "strips antml namespaced thinking tags",
input: "Before <antml:thinking>secret</antml:thinking> after",
expected: "Before after",
},
{
name: "strips multiple reasoning blocks",
input: "<think>first</think>A<think>second</think>B",
@@ -194,6 +199,10 @@ describe("stripReasoningTagsFromText", () => {
input: "A <THINK>hidden</THINK> <Thinking>also hidden</Thinking> B",
expected: "A B",
},
{
input: "A <ANTML:THINKING hidden='1'>secret</ANTML:THINKING> B",
expected: "A B",
},
] as const)("handles unicode/attributes/case-insensitive names: %j", (testCase) => {
expectStrippedCase(testCase);
});

View File

@@ -2,9 +2,10 @@ import { findCodeRegions, isInsideCode } from "./code-regions.js";
export type ReasoningTagMode = "strict" | "preserve";
export type ReasoningTagTrim = "none" | "start" | "both";
const QUICK_TAG_RE = /<\s*\/?\s*(?:think(?:ing)?|thought|antthinking|final)\b/i;
const QUICK_TAG_RE = /<\s*\/?\s*(?:(?:antml:)?(?:think(?:ing)?|thought)|antthinking|final)\b/i;
const FINAL_TAG_RE = /<\s*\/?\s*final\b[^<>]*>/gi;
const THINKING_TAG_RE = /<\s*(\/?)\s*(?:think(?:ing)?|thought|antthinking)\b[^<>]*>/gi;
const THINKING_TAG_RE =
/<\s*(\/?)\s*(?:(?:antml:)?(?:think(?:ing)?|thought)|antthinking)\b[^<>]*>/gi;
function applyTrim(value: string, mode: ReasoningTagTrim): string {
if (mode === "none") {