chore: wip ui reasoning tag strip
This commit is contained in:
parent
e2f3823c58
commit
57a2ccf2ae
@ -21,6 +21,7 @@ describe("stripThinkingTags", () => {
|
|||||||
it("returns original text when no tags exist", () => {
|
it("returns original text when no tags exist", () => {
|
||||||
expect(stripThinkingTags("Hello")).toBe("Hello");
|
expect(stripThinkingTags("Hello")).toBe("Hello");
|
||||||
});
|
});
|
||||||
|
<<<<<<< fix/tui-final-tag-strip
|
||||||
|
|
||||||
it("strips <final>…</final> segments", () => {
|
it("strips <final>…</final> segments", () => {
|
||||||
const input = "<final>\n\nHello there\n\n</final>";
|
const input = "<final>\n\nHello there\n\n</final>";
|
||||||
@ -39,4 +40,27 @@ describe("stripThinkingTags", () => {
|
|||||||
expect(stripThinkingTags("Hello</final>")).toBe("Hello");
|
expect(stripThinkingTags("Hello</final>")).toBe("Hello");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
||||||| temp/landpr-
|
||||||
|
});
|
||||||
|
=======
|
||||||
|
>>>>>>> local
|
||||||
|
|
||||||
|
it("strips <final>…</final> segments while keeping content", () => {
|
||||||
|
const input = "<final>\n\nHello there\n\n</final>";
|
||||||
|
expect(stripThinkingTags(input)).toBe("Hello there\n\n");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("strips mixed <think> and <final> tags", () => {
|
||||||
|
const input = "<think>reasoning</think>\n\n<final>Hello</final>";
|
||||||
|
expect(stripThinkingTags(input)).toBe("Hello");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("strips unpaired <final> tags", () => {
|
||||||
|
expect(stripThinkingTags("<final>Hello")).toBe("Hello");
|
||||||
|
expect(stripThinkingTags("Hello</final>")).toBe("Hello");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps incomplete <final tag text", () => {
|
||||||
|
expect(stripThinkingTags("<final\nHello")).toBe("<final\nHello");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@ -67,38 +67,62 @@ export function parseList(input: string): string[] {
|
|||||||
.filter((v) => v.length > 0);
|
.filter((v) => v.length > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
<<<<<<< fix/tui-final-tag-strip
|
||||||
const THINKING_TAG_RE = /<\s*\/?\s*(?:think(?:ing)?|final)\s*>/gi;
|
const THINKING_TAG_RE = /<\s*\/?\s*(?:think(?:ing)?|final)\s*>/gi;
|
||||||
const THINKING_OPEN_RE = /<\s*(?:think(?:ing)?|final)\s*>/i;
|
const THINKING_OPEN_RE = /<\s*(?:think(?:ing)?|final)\s*>/i;
|
||||||
const THINKING_CLOSE_RE = /<\s*\/\s*(?:think(?:ing)?|final)\s*>/i;
|
const THINKING_CLOSE_RE = /<\s*\/\s*(?:think(?:ing)?|final)\s*>/i;
|
||||||
|
||||||| temp/landpr-
|
||||||
|
const THINKING_TAG_RE = /<\s*\/?\s*think(?:ing)?\s*>/gi;
|
||||||
|
const THINKING_OPEN_RE = /<\s*think(?:ing)?\s*>/i;
|
||||||
|
const THINKING_CLOSE_RE = /<\s*\/\s*think(?:ing)?\s*>/i;
|
||||||
|
=======
|
||||||
|
const FINAL_TAG_RE = /<\s*\/?\s*final\s*>/gi;
|
||||||
|
const THINKING_TAG_RE = /<\s*\/?\s*think(?:ing)?\s*>/gi;
|
||||||
|
const THINKING_OPEN_RE = /<\s*think(?:ing)?\s*>/i;
|
||||||
|
const THINKING_CLOSE_RE = /<\s*\/\s*think(?:ing)?\s*>/i;
|
||||||
|
>>>>>>> local
|
||||||
|
|
||||||
export function stripThinkingTags(value: string): string {
|
export function stripThinkingTags(value: string): string {
|
||||||
if (!value) return value;
|
if (!value) return value;
|
||||||
const hasOpen = THINKING_OPEN_RE.test(value);
|
let cleaned = value;
|
||||||
const hasClose = THINKING_CLOSE_RE.test(value);
|
let strippedFinal = false;
|
||||||
if (!hasOpen && !hasClose) return value;
|
if (FINAL_TAG_RE.test(cleaned)) {
|
||||||
// If we don't have a balanced pair, avoid dropping trailing content.
|
FINAL_TAG_RE.lastIndex = 0;
|
||||||
if (hasOpen !== hasClose) {
|
cleaned = cleaned.replace(FINAL_TAG_RE, "");
|
||||||
if (!hasOpen) return value.replace(THINKING_CLOSE_RE, "").trimStart();
|
strippedFinal = true;
|
||||||
return value.replace(THINKING_OPEN_RE, "").trimStart();
|
} else {
|
||||||
|
FINAL_TAG_RE.lastIndex = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!THINKING_TAG_RE.test(value)) return value;
|
const hasOpen = THINKING_OPEN_RE.test(cleaned);
|
||||||
|
const hasClose = THINKING_CLOSE_RE.test(cleaned);
|
||||||
|
if (!hasOpen && !hasClose) return strippedFinal ? cleaned.trimStart() : cleaned;
|
||||||
|
// If we don't have a balanced pair, avoid dropping trailing content.
|
||||||
|
if (hasOpen !== hasClose) {
|
||||||
|
if (!hasOpen) return cleaned.replace(THINKING_CLOSE_RE, "").trimStart();
|
||||||
|
return cleaned.replace(THINKING_OPEN_RE, "").trimStart();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!THINKING_TAG_RE.test(cleaned)) {
|
||||||
|
THINKING_TAG_RE.lastIndex = 0;
|
||||||
|
return strippedFinal ? cleaned.trimStart() : cleaned;
|
||||||
|
}
|
||||||
THINKING_TAG_RE.lastIndex = 0;
|
THINKING_TAG_RE.lastIndex = 0;
|
||||||
|
|
||||||
let result = "";
|
let result = "";
|
||||||
let lastIndex = 0;
|
let lastIndex = 0;
|
||||||
let inThinking = false;
|
let inThinking = false;
|
||||||
for (const match of value.matchAll(THINKING_TAG_RE)) {
|
for (const match of cleaned.matchAll(THINKING_TAG_RE)) {
|
||||||
const idx = match.index ?? 0;
|
const idx = match.index ?? 0;
|
||||||
if (!inThinking) {
|
if (!inThinking) {
|
||||||
result += value.slice(lastIndex, idx);
|
result += cleaned.slice(lastIndex, idx);
|
||||||
}
|
}
|
||||||
const tag = match[0].toLowerCase();
|
const tag = match[0].toLowerCase();
|
||||||
inThinking = !tag.includes("/");
|
inThinking = !tag.includes("/");
|
||||||
lastIndex = idx + match[0].length;
|
lastIndex = idx + match[0].length;
|
||||||
}
|
}
|
||||||
if (!inThinking) {
|
if (!inThinking) {
|
||||||
result += value.slice(lastIndex);
|
result += cleaned.slice(lastIndex);
|
||||||
}
|
}
|
||||||
return result.trimStart();
|
return result.trimStart();
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user