fix(agents): strip <thought> and <antthinking> tags from output
This commit is contained in:
committed by
Peter Steinberger
parent
494743a4e5
commit
a580639abf
@@ -32,10 +32,10 @@ import {
|
|||||||
promoteThinkingTagsToBlocks,
|
promoteThinkingTagsToBlocks,
|
||||||
} from "./pi-embedded-utils.js";
|
} from "./pi-embedded-utils.js";
|
||||||
|
|
||||||
const THINKING_TAG_RE = /<\s*\/?\s*think(?:ing)?\s*>/gi;
|
const THINKING_TAG_RE = /<\s*\/?\s*(?:think(?:ing)?|thought|antthinking)\s*>/gi;
|
||||||
const THINKING_OPEN_RE = /<\s*think(?:ing)?\s*>/i;
|
const THINKING_OPEN_RE = /<\s*(?:think(?:ing)?|thought|antthinking)\s*>/i;
|
||||||
const THINKING_CLOSE_RE = /<\s*\/\s*think(?:ing)?\s*>/i;
|
const THINKING_CLOSE_RE = /<\s*\/\s*(?:think(?:ing)?|thought|antthinking)\s*>/i;
|
||||||
const THINKING_TAG_SCAN_RE = /<\s*(\/?)\s*think(?:ing)?\s*>/gi;
|
const THINKING_TAG_SCAN_RE = /<\s*(\/?)\s*(?:think(?:ing)?|thought|antthinking)\s*>/gi;
|
||||||
const TOOL_RESULT_MAX_CHARS = 8000;
|
const TOOL_RESULT_MAX_CHARS = 8000;
|
||||||
const log = createSubsystemLogger("agent/embedded");
|
const log = createSubsystemLogger("agent/embedded");
|
||||||
const RAW_STREAM_ENABLED = process.env.CLAWDBOT_RAW_STREAM === "1";
|
const RAW_STREAM_ENABLED = process.env.CLAWDBOT_RAW_STREAM === "1";
|
||||||
|
|||||||
@@ -52,12 +52,12 @@ export function splitThinkingTaggedText(
|
|||||||
// with a think tag (common for local/OpenAI-compat providers that emulate
|
// with a think tag (common for local/OpenAI-compat providers that emulate
|
||||||
// reasoning blocks via tags).
|
// reasoning blocks via tags).
|
||||||
if (!trimmedStart.startsWith("<")) return null;
|
if (!trimmedStart.startsWith("<")) return null;
|
||||||
const openRe = /<\s*think(?:ing)?\s*>/i;
|
const openRe = /<\s*(?:think(?:ing)?|thought|antthinking)\s*>/i;
|
||||||
const closeRe = /<\s*\/\s*think(?:ing)?\s*>/i;
|
const closeRe = /<\s*\/\s*(?:think(?:ing)?|thought|antthinking)\s*>/i;
|
||||||
if (!openRe.test(trimmedStart)) return null;
|
if (!openRe.test(trimmedStart)) return null;
|
||||||
if (!closeRe.test(text)) return null;
|
if (!closeRe.test(text)) return null;
|
||||||
|
|
||||||
const scanRe = /<\s*(\/?)\s*think(?:ing)?\s*>/gi;
|
const scanRe = /<\s*(\/?)\s*(?:think(?:ing)?|thought|antthinking)\s*>/gi;
|
||||||
let inThinking = false;
|
let inThinking = false;
|
||||||
let cursor = 0;
|
let cursor = 0;
|
||||||
let thinkingStart = 0;
|
let thinkingStart = 0;
|
||||||
@@ -136,7 +136,7 @@ export function promoteThinkingTagsToBlocks(message: AssistantMessage): void {
|
|||||||
|
|
||||||
export function extractThinkingFromTaggedText(text: string): string {
|
export function extractThinkingFromTaggedText(text: string): string {
|
||||||
if (!text) return "";
|
if (!text) return "";
|
||||||
const scanRe = /<\s*(\/?)\s*think(?:ing)?\s*>/gi;
|
const scanRe = /<\s*(\/?)\s*(?:think(?:ing)?|thought|antthinking)\s*>/gi;
|
||||||
let result = "";
|
let result = "";
|
||||||
let lastIndex = 0;
|
let lastIndex = 0;
|
||||||
let inThinking = false;
|
let inThinking = false;
|
||||||
@@ -157,8 +157,8 @@ export function extractThinkingFromTaggedStream(text: string): string {
|
|||||||
const closed = extractThinkingFromTaggedText(text);
|
const closed = extractThinkingFromTaggedText(text);
|
||||||
if (closed) return closed;
|
if (closed) return closed;
|
||||||
|
|
||||||
const openRe = /<\s*think(?:ing)?\s*>/gi;
|
const openRe = /<\s*(?:think(?:ing)?|thought|antthinking)\s*>/gi;
|
||||||
const closeRe = /<\s*\/\s*think(?:ing)?\s*>/gi;
|
const closeRe = /<\s*\/\s*(?:think(?:ing)?|thought|antthinking)\s*>/gi;
|
||||||
const openMatches = [...text.matchAll(openRe)];
|
const openMatches = [...text.matchAll(openRe)];
|
||||||
if (openMatches.length === 0) return "";
|
if (openMatches.length === 0) return "";
|
||||||
const closeMatches = [...text.matchAll(closeRe)];
|
const closeMatches = [...text.matchAll(closeRe)];
|
||||||
|
|||||||
Reference in New Issue
Block a user