openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
968 lines (967 loc) • 37.5 kB
JavaScript
import "./src-vebZIeLe.js";
import { t as expectDefined } from "./expect-CyE8FADM.js";
import { o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CIXf7egm.js";
import { t as escapeRegExp } from "./regexp-BZyMFTlj.js";
import { i as stripReasoningTagsFromMarkdown, r as scanReasoningTags } from "./reasoning-tags-DQnFHfqj.js";
import { n as isInsideCode, r as stripLinesOutsideCode, t as findCodeRegions } from "./code-regions-BXMpZZLd.js";
import { c as consumeLineBreak, l as skipHorizontalWhitespace, s as stripPlainTextToolCallBlocks, u as skipWhitespace } from "./src-CAtjgaz5.js";
//#region src/shared/text/model-special-tokens.ts
const MODEL_SPECIAL_TOKEN_RE = /<[||][^||]*[||]>/g;
/**
* Strips leaked model control tokens like `<|assistant|>` or full-width pipe variants.
* Code examples are preserved; remove this when providers stop emitting these tokens.
*
* @see https://github.com/openclaw/openclaw/issues/40020
*/
function stripModelSpecialTokens(text) {
if (!text) return text;
MODEL_SPECIAL_TOKEN_RE.lastIndex = 0;
if (!MODEL_SPECIAL_TOKEN_RE.test(text)) return text;
MODEL_SPECIAL_TOKEN_RE.lastIndex = 0;
const codeRegions = findCodeRegions(text);
let out = "";
let cursor = 0;
for (const match of text.matchAll(MODEL_SPECIAL_TOKEN_RE)) {
const matched = match[0];
const start = match.index ?? 0;
const end = start + matched.length;
out += text.slice(cursor, start);
if (codeRegions.some((region) => start < region.end && end > region.start)) out += matched;
else if (/(?:\p{L}|\p{M}|\p{N})$/u.test(out.slice(-2)) && /^[\p{L}\p{N}]/u.test(text.slice(end, end + 2))) out += " ";
cursor = end;
}
out += text.slice(cursor);
return out;
}
//#endregion
//#region src/shared/text/final-tags.ts
const FINAL_TAG_CANDIDATE_RE = /<[^<>]*>/g;
function isWhitespace(char) {
return /\s/.test(char);
}
function parseAttributeList(text) {
let index = 0;
while (index < text.length) {
while (index < text.length && isWhitespace(text[index] ?? "")) index += 1;
if (index >= text.length) return true;
const nameStart = index;
while (index < text.length) {
const char = text[index] ?? "";
if (isWhitespace(char) || char === "=") break;
if (char === "/" || char === "\"" || char === "'" || char === "<" || char === ">") return false;
index += 1;
}
if (index === nameStart) return false;
while (index < text.length && isWhitespace(text[index] ?? "")) index += 1;
if (text[index] !== "=") continue;
index += 1;
while (index < text.length && isWhitespace(text[index] ?? "")) index += 1;
if (index >= text.length) return false;
const quote = text[index];
if (quote === "\"" || quote === "'") {
index += 1;
const end = text.indexOf(quote, index);
if (end === -1) return false;
index = end + 1;
continue;
}
const valueStart = index;
while (index < text.length && !isWhitespace(text[index] ?? "")) {
const char = text[index] ?? "";
if (char === "\"" || char === "'" || char === "<" || char === ">") return false;
index += 1;
}
if (index === valueStart) return false;
}
return true;
}
/** Parses a candidate `<final>` tag while rejecting lookalike names and malformed attributes. */
function parseFinalTag(text) {
if (!text.startsWith("<") || !text.endsWith(">")) return null;
let body = text.slice(1, -1).trimStart();
let isClose = false;
if (body.startsWith("/")) {
isClose = true;
body = body.slice(1).trimStart();
}
if (!body.toLowerCase().startsWith("final")) return null;
const boundary = body[5] ?? "";
if (boundary && !isWhitespace(boundary) && boundary !== "/") return null;
let rest = body.slice(5);
if (isClose) return rest.trim().length === 0 ? {
isClose: true,
isSelfClosing: false
} : null;
const trimmedRest = rest.trimEnd();
const isSelfClosing = trimmedRest.endsWith("/");
rest = isSelfClosing ? trimmedRest.slice(0, -1) : rest;
if (!parseAttributeList(rest)) return null;
return {
isClose: false,
isSelfClosing
};
}
/** Finds valid `<final>` control tags so callers can strip only actual model markers. */
function findFinalTagMatches(text) {
const matches = [];
for (const match of text.matchAll(FINAL_TAG_CANDIDATE_RE)) {
const tagText = match[0];
const parsed = parseFinalTag(tagText);
if (!parsed) continue;
matches.push({
index: match.index ?? 0,
text: tagText,
...parsed
});
}
return matches;
}
/** Removes valid `<final>` tags while preserving their enclosed visible answer text. */
function stripFinalTags(text) {
let output = "";
let lastIndex = 0;
for (const match of findFinalTagMatches(text)) {
output += text.slice(lastIndex, match.index);
lastIndex = match.index + match.text.length;
}
output += text.slice(lastIndex);
return output;
}
//#endregion
//#region src/shared/text/reasoning-tags.ts
/** Detects whether a stray reasoning close tag separates two visible text regions. */
function hasOrphanReasoningCloseBoundary(params) {
return params.before.trim().length > 0 && params.after.trim().length > 0;
}
/** Strips model reasoning/final tags from visible text while preserving literal code examples. */
function stripReasoningTagsFromText(text, options) {
if (!text) return text;
const mode = options?.mode ?? "strict";
const trimMode = options?.trim ?? "both";
const scope = options?.scope ?? "all";
let cleaned = text;
const matches = findFinalTagMatches(cleaned);
const hasThinkingTag = scanReasoningTags(cleaned).tags.length > 0;
if (matches.length === 0 && !hasThinkingTag) return text;
if (matches.length > 0) {
const preCodeRegions = findCodeRegions(cleaned);
let visible = "";
let lastIndex = 0;
for (const match of matches) if (!isInsideCode(match.index, preCodeRegions)) {
visible += cleaned.slice(lastIndex, match.index);
lastIndex = match.index + match.text.length;
}
cleaned = visible + cleaned.slice(lastIndex);
}
const stripped = stripReasoningTagsFromMarkdown(cleaned, {
mode,
scope,
recoverUnclosed: options?.recoverUnclosed
});
if (trimMode === "none") return stripped;
return trimMode === "start" ? stripped.trimStart() : stripped.trim();
}
//#endregion
//#region src/shared/text/text-projection.ts
function createActivatedProjector(filter) {
const activation = new RegExp(filter.activationTokens.map(escapeRegExp).join("|"), "i");
const overlap = Math.max(0, ...filter.activationTokens.map((token) => token.length - 1));
let tail = "";
let active = false;
let previous = "";
let identity = true;
return (input) => {
if (input.delta === "") return identity ? input : {
text: previous,
delta: ""
};
if (!active) {
const appended = tail + (input.delta ?? input.text);
active = activation.test(appended);
tail = !active && overlap ? appended.slice(-overlap) : "";
}
const text = active ? filter.transform(input.text) : input.text;
const nextIdentity = text === input.text;
const delta = input.delta === null ? null : identity && nextIdentity ? input.delta : text.startsWith(previous) ? text.slice(previous.length) : null;
previous = text;
identity = nextIdentity;
return nextIdentity && delta === input.delta ? input : {
text,
delta
};
};
}
function applyTextFilters(input, filters) {
let text = input;
for (const filter of filters) text = filter.transform(text);
return text;
}
function createTextProjection(filters) {
const stages = filters.map((filter) => ({ filter }));
let source = "";
let text = "";
const project = (value) => {
let input = value;
for (const stage of stages) {
if (!stage.projector || input.delta === null) stage.projector = "create" in stage.filter ? stage.filter.create() : createActivatedProjector(stage.filter);
input = stage.projector(input);
}
return input;
};
return {
get source() {
return source;
},
get text() {
return text;
},
append(delta) {
source += delta;
const next = project({
text: source,
delta
});
if (next.delta === null && next.text.startsWith(text)) next.delta = next.text.slice(text.length);
text = next.text;
return next;
},
replace(value) {
source = value;
const next = project({
text: source,
delta: null
});
next.delta = null;
text = next.text;
return next;
}
};
}
function trimTextFilter(mode) {
return {
transform: (text) => mode === "both" ? text.trim() : mode === "start" ? text.trimStart() : text,
create: () => {
let text = "";
let leading = true;
let removedLeading = false;
let pending = "";
return (input) => {
if (mode === "none") return input;
const appended = input.delta ?? input.text;
let delta = leading ? appended.trimStart() : appended;
removedLeading ||= delta.length !== appended.length;
leading &&= !delta;
if (mode === "both") {
const content = delta.trimEnd();
if (content) {
const trailing = delta.slice(content.length);
delta = pending + content;
pending = trailing;
} else {
pending += delta;
delta = "";
}
}
text = !removedLeading && !pending ? input.text : text + delta;
return {
text,
delta: input.delta === null ? null : delta
};
};
}
};
}
function stripLeadingEmptyLines(text) {
return text.trim() ? text.replace(/^(?:[ \t]*\r?\n)+/, "") : "";
}
const leadingEmptyLinesTextFilter = {
transform: stripLeadingEmptyLines,
create: () => {
let started = false;
let identity = false;
let text = "";
return (input) => {
let delta = input.delta ?? input.text;
if (!started) {
started = /\S/.test(delta);
text = started ? stripLeadingEmptyLines(input.text) : "";
identity = text === input.text;
delta = text;
} else text = identity ? input.text : text + delta;
return {
text,
delta: input.delta === null ? null : delta
};
};
}
};
function collapsePlainDuplicateParagraphs(text) {
return createDuplicateParagraphProjector(false)({
text,
delta: null
}).text;
}
function collapseDuplicateParagraphs(text) {
const collapsed = collapsePlainDuplicateParagraphs(text);
if (collapsed === text) return text;
const regions = findCodeRegions(text);
if (regions.length === 0) return collapsed;
let marker = "\0";
while (text.includes(marker)) marker += "\0";
const inlineTokens = /* @__PURE__ */ new Map();
const protectedRegions = regions.map((region, index) => {
const source = text.slice(region.start, region.end);
let token = `${marker}${index}${marker}`;
if (!region.block) {
token = inlineTokens.get(source) ?? token;
inlineTokens.set(source, token);
}
return {
region,
token
};
});
let masked = "";
let cursor = 0;
for (const { region, token } of protectedRegions) {
masked += text.slice(cursor, region.start) + token;
cursor = region.end;
}
let projected = collapsePlainDuplicateParagraphs(masked + text.slice(cursor));
for (const { region, token } of protectedRegions) projected = projected.replace(token, () => text.slice(region.start, region.end));
return projected;
}
function createDuplicateParagraphProjector(protectCode = true) {
let active = false;
let trailingNewline = false;
let text = "";
let completed = "";
let lastNormalized = null;
let pendingEmpty = 0;
let paragraph = "";
let pending = "";
let normalizedLength = 0;
let equal = false;
let newlines = 0;
let hasDuplicate = false;
let wasCollapsed = false;
let wasDuplicate = false;
return (input) => {
let appended = input.delta ?? input.text;
if (!active) {
active = trailingNewline && appended.startsWith("\n") || appended.includes("\n\n");
trailingNewline = appended ? appended.endsWith("\n") : trailingNewline;
if (!active) {
text = input.text;
return input;
}
appended = input.text;
}
let completedParagraph = false;
let added = "";
for (const part of appended.matchAll(/\n+|[^\n]+/g)) {
if (part[0].startsWith("\n")) {
if (newlines < 2 && newlines + part[0].length >= 2) {
completedParagraph = true;
if (!paragraph) {
if (lastNormalized !== null) pendingEmpty++;
} else if (equal && normalizedLength === lastNormalized?.length) hasDuplicate = true;
else {
completed += (completed ? "\n\n" : "") + paragraph;
lastNormalized = paragraph.replace(/\s+/g, " ");
}
paragraph = "";
pending = "";
normalizedLength = 0;
equal = Boolean(lastNormalized);
} else if (newlines + part[0].length < 2 && paragraph) pending += part[0];
newlines += part[0].length;
continue;
}
newlines = 0;
const raw = paragraph ? part[0] : part[0].trimStart();
const content = raw.trimEnd();
if (!content) {
pending += raw;
continue;
}
const piece = pending + content;
pending = raw.slice(content.length);
if (!paragraph) {
const empty = "\n\n".repeat(pendingEmpty);
if (pendingEmpty) {
completed += empty;
pendingEmpty = 0;
lastNormalized = "";
equal = false;
}
added += completed ? empty + "\n\n" : "";
}
paragraph += piece;
added += piece;
if (equal) {
const normalized = piece.replace(/\s+/g, " ");
equal = lastNormalized?.startsWith(normalized, normalizedLength) === true;
normalizedLength += normalized.length;
}
}
const duplicate = Boolean(paragraph) && equal && normalizedLength === lastNormalized?.length;
const collapsed = hasDuplicate || duplicate;
let delta = input.delta;
if (collapsed) {
delta = input.delta === null || !wasCollapsed || duplicate !== wasDuplicate || completedParagraph || protectCode && /\s$/.test(text) ? null : added;
text = delta === null ? protectCode ? collapseDuplicateParagraphs(input.text) : completed + (paragraph && !duplicate ? (completed ? "\n\n" : "") + paragraph : "") : text + delta;
} else {
text = input.text;
if (wasCollapsed) delta = null;
}
wasCollapsed = collapsed;
wasDuplicate = duplicate;
return {
text,
delta
};
};
}
const duplicateParagraphTextFilter = {
transform: collapseDuplicateParagraphs,
create: createDuplicateParagraphProjector
};
//#endregion
//#region src/shared/text/assistant-visible-text.ts
const MEMORY_TAG_RE = /<\s*(\/?)\s*relevant[-_]memories\b[^<>]*>/gi;
const MEMORY_TAG_QUICK_RE = /<\s*\/?\s*relevant[-_]memories\b/i;
const LEGACY_BRACKET_TOOL_BLOCK_QUICK_RE = /\[\s*\/?\s*TOOL_(?:CALL|RESULT)\s*\]/i;
const INTERNAL_TRACE_LINE_QUICK_RE = /(?:📊|🛠️|📖|📝|🔍|🔎|⚙️|tool[-_ ]?call|tool[-_ ]?result|function[-_ ]?call)/i;
const INTERNAL_TRACE_LINE_RE = /^(?:>\s*)?(?:⚠️\s*)?(?:📊|🛠️|📖|📝|🔍|🔎|⚙️)\s*(?:Session Status|Exec|Read|Edit|Write|Patch|Search|Open|Click|Find|Screenshot|Update Plan|Tool Call|Tool Result|Function Call|Shell|Command)\s*:/i;
const INTERNAL_COMPACT_FAILURE_TRACE_LINE_RE = /^(?:>\s*)?⚠️\s*🛠️\s+(?:(?:Exec|Bash)\s+failed(?:(?:\s+\(exit\s+-?\d+\))|(?:\s*:[^\r\n]*))?|\S[^\r\n]*\s+\(agent\)`{0,2}\s+failed(?:\s*:[^\r\n]*)?)\s*$/i;
const INTERNAL_COMPACT_COMMAND_TRACE_LINE_RE = /^(?:>\s*)?🛠️\s*(?:(?:(?:elevated|pty)\b\s*(?:·|,)\s*)+)?(?:`{1,2}\s*\S|(?:run|check|fetch|pull|push|view|show|list|switch|create|merge|rebase|stage|restore|reset|stash|search|find|print|copy|move|remove|install|start|cd|git|pnpm|npm|yarn|bun|node|python|python3|bash|sh)\b)/i;
const INTERNAL_CHANNEL_TRACE_LINE_RE = /^(?:>\s*)?(?:tool[-_ ]?call|tool[-_ ]?result|function[-_ ]?call)\s*[:=]/i;
/**
* Strip XML-style tool call tags that models sometimes emit as plain text.
* This stateful pass hides content from an opening tag through the matching
* closing tag, or to end-of-string if the stream was truncated mid-tag.
*/
const TOOL_CALL_QUICK_RE = /<\s*\/?\s*(?:antml:)?(?:tool_call|tool_result|function_calls?|function_response|function|tool_calls|invoke|parameter)\b/i;
const TOOL_CALL_TAG_NAMES = /* @__PURE__ */ new Set([
"tool_call",
"tool_result",
"function_call",
"function_calls",
"function_response",
"function",
"tool_calls",
"antml:invoke",
"antml:parameter"
]);
const TOOL_CALL_JSON_PAYLOAD_START_RE = /^(?:\s+[A-Za-z_:][-A-Za-z0-9_:.]*\s*=\s*(?:"[^"]*"|'[^']*'|[^\s"'=<>`]+))*\s*(?:\r?\n\s*)?[[{]/;
const TOOL_CALL_XML_PAYLOAD_START_RE = /^\s*(?:\r?\n\s*)?<(?:antml:)?(?:function_call|tool_call|function|invoke|parameters?|arguments?)\b/i;
const NESTED_JSON_TOOL_CALL_PAYLOAD_START_RE = /^\s*(?:\r?\n\s*)?<(?:function_call|tool_call)\b/i;
function endsInsideQuotedString(text, start, end) {
let quoteChar = null;
let isEscaped = false;
for (let idx = start; idx < end; idx += 1) {
const char = text[idx];
if (quoteChar === null) {
if (char === "\"" || char === "'") quoteChar = char;
continue;
}
if (isEscaped) {
isEscaped = false;
continue;
}
if (char === "\\") {
isEscaped = true;
continue;
}
if (char === quoteChar) quoteChar = null;
}
return quoteChar !== null;
}
const XML_TAG_HEAD_RE = /<\s*(?:(\/)\s*)?([A-Za-z_:][A-Za-z0-9_.:-]*)(?=$|[\s/>])/y;
function parseXmlTagAt(text, start) {
XML_TAG_HEAD_RE.lastIndex = start;
const match = XML_TAG_HEAD_RE.exec(text);
if (!match) return null;
const contentStart = XML_TAG_HEAD_RE.lastIndex;
const isClose = match[1] === "/";
const closeIndex = findTagCloseIndex(text, contentStart);
const isTruncated = closeIndex === -1;
return {
contentStart,
end: isTruncated ? text.length : closeIndex + 1,
isClose,
isSelfClosing: !isTruncated && !isClose && /\/\s*$/.test(text.slice(contentStart, closeIndex)),
tagName: normalizeLowercaseStringOrEmpty(match[2]),
isTruncated
};
}
function findTagCloseIndex(text, start) {
let quoteChar = null;
let isEscaped = false;
for (let idx = start; idx < text.length; idx += 1) {
const char = text[idx];
if (quoteChar !== null) {
if (isEscaped) {
isEscaped = false;
continue;
}
if (char === "\\") {
isEscaped = true;
continue;
}
if (char === quoteChar) quoteChar = null;
continue;
}
if (char === "\"" || char === "'") {
quoteChar = char;
continue;
}
if (char === "<") return -1;
if (char === ">") return idx;
}
return -1;
}
function detectToolCallPayloadKind(text, start) {
const rest = text.slice(start);
if (TOOL_CALL_JSON_PAYLOAD_START_RE.test(rest)) return "json";
if (TOOL_CALL_XML_PAYLOAD_START_RE.test(rest)) return "xml";
return null;
}
function startsWithNestedJsonToolCallPayload(text, start) {
if (!NESTED_JSON_TOOL_CALL_PAYLOAD_START_RE.test(text.slice(start))) return false;
const nestedTag = parseToolCallTagAt(text, skipWhitespace(text, start));
if (!nestedTag || nestedTag.isClose || nestedTag.isSelfClosing || nestedTag.isTruncated || nestedTag.tagName !== "function_call" && nestedTag.tagName !== "tool_call") return false;
return TOOL_CALL_JSON_PAYLOAD_START_RE.test(text.slice(nestedTag.end));
}
function isLikelyStandaloneFunctionToolCall(text, tagStart, tag) {
if (tag.tagName !== "function" || tag.isClose || tag.isSelfClosing || tag.isTruncated) return false;
if (!/\bname\s*=/.test(text.slice(tag.contentStart, tag.end))) return false;
let idx = tagStart - 1;
while (idx >= 0 && (text[idx] === " " || text[idx] === " ")) idx -= 1;
return idx < 0 || text[idx] === "\n" || text[idx] === "\r" || /[.!?:]/.test(text.charAt(idx));
}
function isAdjacentToStrippedToolCallBlock(text, tagStart, lastStrippedBlockEnd) {
if (lastStrippedBlockEnd === null || lastStrippedBlockEnd > tagStart) return false;
for (let idx = lastStrippedBlockEnd; idx < tagStart; idx += 1) if (text[idx] !== " " && text[idx] !== " " && text[idx] !== "\n" && text[idx] !== "\r") return false;
return true;
}
function findMatchingToolCallCloseIndex(text, start, tagName) {
for (let idx = start; idx < text.length; idx += 1) {
if (text[idx] !== "<") continue;
const tag = parseToolCallTagAt(text, idx);
if (!tag) continue;
if (tag.isClose && tag.tagName === tagName && !tag.isTruncated) return idx;
idx = Math.max(idx, tag.end - 1);
}
return -1;
}
function findAdjacentOpeningToolCallTag(text, start, tagName) {
const tag = parseToolCallTagAt(text, skipWhitespace(text, start));
if (!tag || tag.isClose || tag.tagName !== tagName) return null;
return tag;
}
function parseToolCallTagAt(text, start) {
const tag = parseXmlTagAt(text, start);
return tag && TOOL_CALL_TAG_NAMES.has(tag.tagName) ? tag : null;
}
function hasMatchingXmlCloseTag(text, start, tagName) {
let depth = 1;
for (let idx = start; idx < text.length; idx += 1) {
if (text[idx] !== "<") continue;
const tag = parseXmlTagAt(text, idx);
if (!tag || tag.tagName !== tagName || tag.isTruncated) continue;
if (tag.isClose) {
depth -= 1;
if (depth === 0) return true;
} else if (!tag.isSelfClosing) depth += 1;
idx = Math.max(idx, tag.end - 1);
}
return false;
}
function isDanglingFunctionParameterParent(text, tag) {
if (tag.tagName !== "function" || !/\bname\s*=/.test(text.slice(tag.contentStart, tag.end))) return false;
const nextTag = parseXmlTagAt(text, skipWhitespace(text, tag.end));
return nextTag?.tagName === "parameter" && !nextTag.isClose;
}
function trimImmediateLineBreakBefore(text, start, end) {
if (end > start && text[end - 1] === "\n") return end - (end - 2 >= start && text[end - 2] === "\r" ? 2 : 1);
return end > start && text[end - 1] === "\r" ? end - 1 : end;
}
function isLineStartAt(text, start) {
let cursor = start - 1;
while (cursor >= 0 && (text[cursor] === " " || text[cursor] === " ")) cursor -= 1;
return cursor < 0 || text[cursor] === "\n" || text[cursor] === "\r";
}
function isLineEndAfter(text, end) {
const cursor = skipHorizontalWhitespace(text, end);
return cursor >= text.length || text[cursor] === "\n" || text[cursor] === "\r";
}
function unwrapStandaloneParameterTags(text) {
if (!/<\s*\/?\s*parameter\b/i.test(text)) return text;
const codeRegions = findCodeRegions(text);
const openTags = [];
let result = "";
let lastIndex = 0;
for (let idx = 0; idx < text.length; idx += 1) {
if (text[idx] !== "<" || isInsideCode(idx, codeRegions)) continue;
const tag = parseXmlTagAt(text, idx);
if (!tag || tag.isTruncated) continue;
if (tag.isClose) {
const openIndex = openTags.findLastIndex((entry) => entry.name === tag.tagName);
if (openIndex !== -1) {
const opening = expectDefined(openTags[openIndex], "open tags entry at open index");
if (opening.unwrap) {
const contentEnd = opening.trimBoundaryLineBreaks && isLineStartAt(text, idx) && isLineEndAfter(text, tag.end) ? trimImmediateLineBreakBefore(text, lastIndex, idx) : idx;
result += text.slice(lastIndex, contentEnd);
lastIndex = tag.end;
}
openTags.splice(openIndex);
}
} else if (tag.isSelfClosing) {
if (tag.tagName === "parameter" && openTags.length === 0) {
result += text.slice(lastIndex, idx);
lastIndex = tag.end;
}
} else if (hasMatchingXmlCloseTag(text, tag.end, tag.tagName) || isDanglingFunctionParameterParent(text, tag)) {
const unwrap = tag.tagName === "parameter" && openTags.length === 0;
let trimBoundaryLineBreaks = false;
if (unwrap) {
result += text.slice(lastIndex, idx);
lastIndex = tag.end;
const contentStart = isLineStartAt(text, idx) ? consumeLineBreak(text, lastIndex) : null;
if (contentStart !== null) {
lastIndex = contentStart;
trimBoundaryLineBreaks = true;
}
}
openTags.push({
name: tag.tagName,
unwrap,
trimBoundaryLineBreaks
});
}
idx = Math.max(idx, tag.end - 1);
}
return result + text.slice(lastIndex);
}
function stripToolCallXmlTags(input, options = {}) {
const text = input;
if (!text || !TOOL_CALL_QUICK_RE.test(text)) return text;
const codeRegions = findCodeRegions(text);
let result = "";
let lastIndex = 0;
let inToolCallBlock = false;
let toolCallBlockContentStart = 0;
let toolCallBlockNeedsQuoteBalance = false;
let toolCallBlockStart = 0;
let toolCallBlockTagName = null;
let lastStrippedToolCallBlockEnd = null;
const visibleTagBalance = /* @__PURE__ */ new Map();
for (let idx = 0; idx < text.length; idx += 1) {
if (text[idx] !== "<") continue;
if (!inToolCallBlock && isInsideCode(idx, codeRegions)) continue;
const tag = parseToolCallTagAt(text, idx);
if (!tag) continue;
if (!inToolCallBlock) {
result += text.slice(lastIndex, idx);
if (tag.isClose) {
if (tag.isTruncated) {
const preserveEnd = tag.contentStart;
result += text.slice(idx, preserveEnd);
lastIndex = preserveEnd;
idx = Math.max(idx, preserveEnd - 1);
continue;
}
const balance = visibleTagBalance.get(tag.tagName) ?? 0;
if (balance > 0) {
result += text.slice(idx, tag.end);
visibleTagBalance.set(tag.tagName, balance - 1);
}
lastIndex = tag.end;
idx = Math.max(idx, tag.end - 1);
continue;
}
if (tag.isSelfClosing) {
lastStrippedToolCallBlockEnd = tag.end;
lastIndex = tag.end;
idx = Math.max(idx, tag.end - 1);
continue;
}
const payloadStart = tag.isTruncated ? tag.contentStart : tag.end;
const isPluralToolCallWrapper = tag.tagName === "function_calls" || tag.tagName === "tool_calls";
const matchingCloseStart = isPluralToolCallWrapper ? findMatchingToolCallCloseIndex(text, tag.end, tag.tagName) : -1;
const matchingCloseTag = matchingCloseStart === -1 ? null : parseToolCallTagAt(text, matchingCloseStart);
const shouldStripPluralWrapperBeforeResponse = options.stripFunctionResponseAfterPluralToolCalls === true && isPluralToolCallWrapper && matchingCloseTag !== null && findAdjacentOpeningToolCallTag(text, matchingCloseTag.end, "function_response") !== null;
const payloadKind = tag.tagName === "tool_call" || tag.tagName === "function" || tag.tagName === "antml:invoke" || (options.stripFunctionCallsXmlPayloads === true || shouldStripPluralWrapperBeforeResponse) && isPluralToolCallWrapper ? detectToolCallPayloadKind(text, payloadStart) : TOOL_CALL_JSON_PAYLOAD_START_RE.test(text.slice(payloadStart)) ? "json" : null;
const shouldStripStandaloneFunction = tag.tagName !== "function" || isLikelyStandaloneFunctionToolCall(text, idx, tag);
const functionResponseCloseStart = tag.tagName === "function_response" ? findMatchingToolCallCloseIndex(text, tag.end, tag.tagName) : -1;
const shouldStripStandaloneResult = tag.tagName === "function_response" && (isLineStartAt(text, idx) && isLineEndAfter(text, tag.end) || isAdjacentToStrippedToolCallBlock(text, idx, lastStrippedToolCallBlockEnd) || functionResponseCloseStart !== -1 && isLineStartAt(result, result.length) && isLineEndAfter(text, tag.end));
if (payloadKind && shouldStripStandaloneFunction || shouldStripStandaloneResult) {
inToolCallBlock = true;
toolCallBlockContentStart = tag.end;
toolCallBlockNeedsQuoteBalance = payloadKind === "json" || payloadKind === "xml" && startsWithNestedJsonToolCallPayload(text, payloadStart);
toolCallBlockStart = idx;
toolCallBlockTagName = tag.tagName;
if (tag.isTruncated) {
lastIndex = text.length;
break;
}
} else {
const preserveEnd = tag.isTruncated ? tag.contentStart : tag.end;
result += text.slice(idx, preserveEnd);
if (!tag.isTruncated) visibleTagBalance.set(tag.tagName, (visibleTagBalance.get(tag.tagName) ?? 0) + 1);
lastIndex = preserveEnd;
idx = Math.max(idx, preserveEnd - 1);
continue;
}
} else if (tag.isClose && (tag.tagName === toolCallBlockTagName || toolCallBlockTagName === "tool_result" && tag.tagName === "tool_call") && (!toolCallBlockNeedsQuoteBalance || !endsInsideQuotedString(text, toolCallBlockContentStart, idx))) {
const closedBlockTagName = toolCallBlockTagName;
inToolCallBlock = false;
toolCallBlockNeedsQuoteBalance = false;
toolCallBlockTagName = null;
if (closedBlockTagName) lastStrippedToolCallBlockEnd = tag.end;
}
lastIndex = tag.end;
idx = Math.max(idx, tag.end - 1);
}
if (!inToolCallBlock) result += text.slice(lastIndex);
else if (toolCallBlockTagName === "function") result += text.slice(toolCallBlockStart);
return unwrapStandaloneParameterTags(result);
}
/**
* Strip malformed Minimax tool invocations that leak into text content.
* Minimax sometimes embeds tool calls as XML in text blocks instead of
* proper structured tool calls.
*/
function stripMinimaxToolCallXml(text) {
if (!text || !/minimax:tool_call/i.test(text)) return text;
const codeRegions = findCodeRegions(text);
const minimaxToolXmlRe = /<invoke\b[^>]*>[\s\S]*?<\/invoke>|<\/?minimax:tool_call>/gi;
let result = "";
let cursor = 0;
for (const match of text.matchAll(minimaxToolXmlRe)) {
const start = match.index ?? 0;
if (isInsideCode(start, codeRegions)) continue;
result += text.slice(cursor, start);
cursor = start + match[0].length;
}
result += text.slice(cursor);
return result;
}
function isLegacyBracketToolCallPayload(value) {
return /\btool\s*=>\s*["'][A-Za-z_][A-Za-z0-9_.:-]{0,119}["']/i.test(value) && /\bargs\s*=>/i.test(value);
}
function isLegacyBracketToolResultPayload(value) {
return /^\s*[{[]/.test(value) || /\b(?:tool|result|output|content)\s*=>/i.test(value) || /\b(?:tool|result|output|content)\s*:/i.test(value);
}
function stripLegacyBracketToolCallBlocks(text) {
if (!text || !LEGACY_BRACKET_TOOL_BLOCK_QUICK_RE.test(text)) return text;
const codeRegions = findCodeRegions(text);
let result = "";
let cursor = 0;
for (const openMatch of text.matchAll(/\[\s*TOOL_(CALL|RESULT)\s*\]/gi)) {
const openStart = openMatch.index;
if (openStart < cursor || isInsideCode(openStart, codeRegions)) continue;
const payloadStart = openStart + openMatch[0].length;
const isResult = openMatch[1]?.toUpperCase() === "RESULT";
const closeRe = isResult ? /\[\s*\/\s*TOOL_RESULT\s*\]/gi : /\[\s*\/\s*TOOL_CALL\s*\]/gi;
closeRe.lastIndex = payloadStart;
const closeMatch = closeRe.exec(text);
const closeStart = closeMatch && !isInsideCode(closeMatch.index, codeRegions) ? closeMatch.index : -1;
const payload = text.slice(payloadStart, closeStart >= 0 ? closeStart : text.length);
if (!(isResult ? isLegacyBracketToolResultPayload(payload) : isLegacyBracketToolCallPayload(payload))) continue;
result += text.slice(cursor, openStart);
cursor = closeStart >= 0 ? closeStart + (closeMatch?.[0].length ?? 0) : text.length;
if (cursor === text.length) break;
}
return result + text.slice(cursor);
}
function consumeJsonish(input, start) {
let index = start;
while (index < input.length && /[ \t\r\n]/.test(input[index] ?? "")) index += 1;
const opening = input[index];
if (opening === void 0) return null;
if (opening !== "{" && opening !== "[" && opening !== "\"") {
while (index < input.length && input[index] !== "\n" && input[index] !== "\r") index += 1;
return index;
}
let depth = opening === "\"" ? 0 : 1;
let inString = opening === "\"";
let escaped = false;
for (index += 1; index < input.length; index += 1) {
const char = input[index];
if (inString) {
if (escaped) escaped = false;
else if (char === "\\") escaped = true;
else if (char === "\"") inString = false;
} else if (char === "\"") inString = true;
else if (char === "{" || char === "[") depth += 1;
else if (char === "}" || char === "]") depth -= 1;
if (!inString && depth === 0) return index + 1;
}
return null;
}
function stripDowngradedToolCalls(input) {
let codeRegions;
let result = "";
let cursor = 0;
for (const match of input.matchAll(/\[Tool Call:[^\]]*\]/gi)) {
const start = match.index;
if (start < cursor || isInsideCode(start, codeRegions ??= findCodeRegions(input))) continue;
result += input.slice(cursor, start);
let index = skipHorizontalWhitespace(input, start + match[0].length);
index = skipHorizontalWhitespace(input, consumeLineBreak(input, index) ?? index);
if (normalizeLowercaseStringOrEmpty(input.slice(index, index + 9)) === "arguments") {
index += 9;
if (input[index] === ":") index += 1;
if (input[index] === " ") index += 1;
index = consumeJsonish(input, index) ?? index;
}
if (!result || result.endsWith("\n") || result.endsWith("\r")) index = consumeLineBreak(input, index) ?? index;
cursor = index;
}
return result + input.slice(cursor);
}
/**
* Strip downgraded tool call text representations that leak into user-visible
* text content when replaying history across providers.
*/
function stripDowngradedToolCallText(text) {
if (!text || !/\[Tool (?:Call|Result)/i.test(text) && !/\[Historical context/i.test(text)) return text;
let cleaned = stripDowngradedToolCalls(text);
for (const pattern of [/\[Tool Result for ID[^\]]*\]\n?[\s\S]*?(?=\n*\[Tool |\n*$)/gi, /\[Historical context:[^\]]*\]\n?/gi]) {
const input = cleaned;
let codeRegions;
cleaned = input.replace(pattern, (match, offset) => isInsideCode(offset, codeRegions ??= findCodeRegions(input)) ? match : "");
}
return cleaned.trim();
}
function stripRelevantMemoriesTags(text) {
if (!text || !MEMORY_TAG_QUICK_RE.test(text)) return text;
MEMORY_TAG_RE.lastIndex = 0;
const codeRegions = findCodeRegions(text);
let result = "";
let lastIndex = 0;
let inMemoryBlock = false;
for (const match of text.matchAll(MEMORY_TAG_RE)) {
const idx = match.index ?? 0;
if (isInsideCode(idx, codeRegions)) continue;
const isClose = match[1] === "/";
if (!inMemoryBlock) {
result += text.slice(lastIndex, idx);
if (!isClose) inMemoryBlock = true;
} else if (isClose) inMemoryBlock = false;
lastIndex = idx + match[0].length;
}
if (!inMemoryBlock) result += text.slice(lastIndex);
return result;
}
function stripAssistantInternalTraceLines(text) {
if (!text || !INTERNAL_TRACE_LINE_QUICK_RE.test(text)) return text;
return stripLinesOutsideCode(text, (line) => {
const trimmed = line.trim();
return INTERNAL_TRACE_LINE_RE.test(trimmed) || INTERNAL_COMPACT_FAILURE_TRACE_LINE_RE.test(trimmed) || INTERNAL_COMPACT_COMMAND_TRACE_LINE_RE.test(trimmed) || INTERNAL_CHANNEL_TRACE_LINE_RE.test(trimmed);
});
}
const profileFilters = /* @__PURE__ */ new Map();
function assistantVisibleTextFilters(profile, streaming = false) {
const key = `${profile}:${streaming}`;
const cached = profileFilters.get(key);
if (cached) return cached;
const preserve = profile === "internal-scaffolding";
const trim = preserve ? "start" : profile === "history" ? "none" : "both";
const reasoning = {
activationTokens: ["<"],
transform: (text) => stripReasoningTagsFromText(text, {
mode: preserve ? "preserve" : "strict",
scope: profile === "final-answer-delivery" ? "leading" : "all",
trim,
recoverUnclosed: !streaming
})
};
const filters = [
...!preserve ? [{
transform: stripMinimaxToolCallXml,
activationTokens: ["<"]
}] : [],
{
transform: stripModelSpecialTokens,
activationTokens: ["<"]
},
{
transform: stripRelevantMemoriesTags,
activationTokens: ["<"]
},
{
activationTokens: ["<"],
transform: (text) => stripToolCallXmlTags(text, {
stripFunctionCallsXmlPayloads: profile === "tool-progress",
stripFunctionResponseAfterPluralToolCalls: profile === "delivery" || profile === "final-answer-delivery"
})
},
...profile === "tool-progress" ? [] : [assistantTraceTextFilter],
{
transform: stripLegacyBracketToolCallBlocks,
activationTokens: ["["]
},
plainToolCallTextFilter,
...!preserve ? [{
transform: stripDowngradedToolCallText,
activationTokens: ["["]
}] : []
];
if (preserve) filters.unshift(reasoning);
else filters.push(reasoning);
filters.push(trimTextFilter(trim));
profileFilters.set(key, filters);
return filters;
}
const assistantTraceTextFilter = {
transform: stripAssistantInternalTraceLines,
activationTokens: [
"tool",
"function",
"📊",
"🛠",
"📖",
"📝",
"🔍",
"🔎",
"⚙"
]
};
const plainToolCallTextFilter = {
transform: (text) => stripPlainTextToolCallBlocks(text, { resolveProtectedRanges: findCodeRegions }),
activationTokens: [
"[",
"<",
"to="
]
};
function sanitizeAssistantVisibleTextWithProfile(text, profile = "delivery", streaming = false) {
return applyTextFilters(text, assistantVisibleTextFilters(profile, streaming));
}
function stripAssistantInternalScaffolding(text) {
return sanitizeAssistantVisibleTextWithProfile(text, "internal-scaffolding");
}
/**
* Canonical user-visible assistant text sanitizer for delivery and history
* extraction paths. Keeps prose, removes internal scaffolding.
*/
function sanitizeAssistantVisibleText(text) {
return sanitizeAssistantVisibleTextWithProfile(text, "delivery");
}
/** Sanitizes text already marked as final-answer prose by the agent runtime. */
function sanitizeAssistantFinalAnswerText(text) {
return sanitizeAssistantVisibleTextWithProfile(text, "final-answer-delivery");
}
/**
* Backwards-compatible trim wrapper.
* Prefer sanitizeAssistantVisibleTextWithProfile for new call sites.
*/
function sanitizeAssistantVisibleTextWithOptions(text, options) {
return sanitizeAssistantVisibleTextWithProfile(text, options?.trim === "none" ? "history" : "delivery");
}
//#endregion
export { trimTextFilter as _, sanitizeAssistantVisibleText as a, findFinalTagMatches as b, stripAssistantInternalScaffolding as c, stripMinimaxToolCallXml as d, stripToolCallXmlTags as f, leadingEmptyLinesTextFilter as g, duplicateParagraphTextFilter as h, sanitizeAssistantFinalAnswerText as i, stripDowngradedToolCallText as l, createTextProjection as m, assistantVisibleTextFilters as n, sanitizeAssistantVisibleTextWithOptions as o, applyTextFilters as p, plainToolCallTextFilter as r, sanitizeAssistantVisibleTextWithProfile as s, assistantTraceTextFilter as t, stripLegacyBracketToolCallBlocks as u, hasOrphanReasoningCloseBoundary as v, stripFinalTags as x, stripReasoningTagsFromText as y };