import { o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CIXf7egm.js";
import "./src-BntaCZM-.js";
import { t as expectDefined } from "./expect-CyE8FADM.js";
import { i as stripReasoningTagsFromMarkdown, r as scanReasoningTags } from "./reasoning-tags-BLuaWb5N.js";
import { n as isInsideCode, t as findCodeRegions } from "./code-regions-BSt0fAqe.js";
import { s as stripPlainTextToolCallBlocks } from "./src-DlLP3mBE.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
function applyTrim(value, mode) {
	if (mode === "none") return value;
	if (mode === "start") return value.trimStart();
	return value.trim();
}
/** 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);
	}
	return applyTrim(stripReasoningTagsFromMarkdown(cleaned, {
		mode,
		scope
	}), trimMode);
}
//#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;
}
function parseXmlTagAt(text, start) {
	if (text[start] !== "<") return null;
	let cursor = start + 1;
	while (cursor < text.length && /\s/.test(text.charAt(cursor))) cursor += 1;
	let isClose = false;
	if (text[cursor] === "/") {
		isClose = true;
		cursor += 1;
		while (cursor < text.length && /\s/.test(text.charAt(cursor))) cursor += 1;
	}
	const nameStart = cursor;
	if (!/[A-Za-z_:]/.test(text[cursor] ?? "")) return null;
	cursor += 1;
	while (cursor < text.length && /[A-Za-z0-9_.:-]/.test(text.charAt(cursor))) cursor += 1;
	const tagName = normalizeLowercaseStringOrEmpty(text.slice(nameStart, cursor));
	if (!isToolCallBoundary(text[cursor])) return null;
	const contentStart = cursor;
	const closeIndex = findTagCloseIndex(text, cursor);
	if (closeIndex === -1) return {
		contentStart,
		end: text.length,
		isClose,
		isSelfClosing: false,
		tagName,
		isTruncated: true
	};
	return {
		contentStart,
		end: closeIndex + 1,
		isClose,
		isSelfClosing: !isClose && /\/\s*$/.test(text.slice(cursor, closeIndex)),
		tagName,
		isTruncated: false
	};
}
function isToolCallBoundary(char) {
	return !char || /\s/.test(char) || char === "/" || char === ">";
}
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;
	let cursor = start;
	while (cursor < text.length && /\s/.test(text.charAt(cursor))) cursor += 1;
	const nestedTag = parseToolCallTagAt(text, cursor);
	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 isStandaloneOpeningTagLine(text, tagStart, tag) {
	let idx = tagStart - 1;
	while (idx >= 0 && (text[idx] === " " || text[idx] === "	")) idx -= 1;
	if (!(idx < 0 || text[idx] === "\n" || text[idx] === "\r")) return false;
	let after = tag.end;
	while (after < text.length && (text[after] === " " || text[after] === "	")) after += 1;
	return after >= text.length || text[after] === "\n" || text[after] === "\r";
}
function isOpeningTagFollowedByLineBreak(text, tag) {
	let after = tag.end;
	while (after < text.length && (text[after] === " " || text[after] === "	")) after += 1;
	return after >= text.length || text[after] === "\n" || text[after] === "\r";
}
function hasSameLineContentAfterOpeningTag(text, tag) {
	let after = tag.end;
	while (after < text.length && (text[after] === " " || text[after] === "	")) after += 1;
	return after < text.length && text[after] !== "\n" && text[after] !== "\r";
}
function isVisibleLineStart(text) {
	let idx = text.length - 1;
	while (idx >= 0 && (text[idx] === " " || text[idx] === "	")) idx -= 1;
	return idx < 0 || text[idx] === "\n" || text[idx] === "\r";
}
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) {
	let idx = start;
	while (idx < text.length && /\s/.test(text.charAt(idx))) idx += 1;
	if (text[idx] !== "<") return null;
	const tag = parseToolCallTagAt(text, idx);
	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;
	let cursor = tag.end;
	while (cursor < text.length && /\s/.test(text.charAt(cursor))) cursor += 1;
	const nextTag = parseXmlTagAt(text, cursor);
	return nextTag?.tagName === "parameter" && !nextTag.isClose;
}
function consumeImmediateLineBreak(text, start) {
	if (text[start] === "\r" && text[start + 1] === "\n") return start + 2;
	return text[start] === "\n" || text[start] === "\r" ? start + 1 : null;
}
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) {
	let cursor = end;
	while (cursor < text.length && (text[cursor] === " " || text[cursor] === "	")) cursor += 1;
	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) ? consumeImmediateLineBreak(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 shouldStripAdjacentResult = isAdjacentToStrippedToolCallBlock(text, idx, lastStrippedToolCallBlockEnd) && (isOpeningTagFollowedByLineBreak(text, tag) || functionResponseCloseStart !== -1 || hasSameLineContentAfterOpeningTag(text, tag));
			const shouldStripStandaloneResult = tag.tagName === "function_response" && (isStandaloneOpeningTagLine(text, idx, tag) || shouldStripAdjacentResult || functionResponseCloseStart !== -1 && isVisibleLineStart(result) && isOpeningTagFollowedByLineBreak(text, tag));
			if (!tag.isClose && (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;
	while (cursor < text.length) {
		const openMatch = /\[\s*TOOL_(CALL|RESULT)\s*\]/gi.exec(text.slice(cursor));
		if (!openMatch?.[0]) {
			result += text.slice(cursor);
			break;
		}
		const blockKind = openMatch[1]?.toUpperCase();
		const openStart = cursor + (openMatch.index ?? 0);
		const payloadStart = openStart + openMatch[0].length;
		if (isInsideCode(openStart, codeRegions)) {
			result += text.slice(cursor, payloadStart);
			cursor = payloadStart;
			continue;
		}
		const closeMatch = (blockKind === "RESULT" ? /\[\s*\/\s*TOOL_RESULT\s*\]/gi : /\[\s*\/\s*TOOL_CALL\s*\]/gi).exec(text.slice(payloadStart));
		const closeStart = closeMatch?.[0] && !isInsideCode(payloadStart + (closeMatch.index ?? 0), codeRegions) ? payloadStart + (closeMatch.index ?? 0) : -1;
		const payloadEnd = closeStart >= 0 ? closeStart : text.length;
		const payload = text.slice(payloadStart, payloadEnd);
		if (!(blockKind === "RESULT" ? isLegacyBracketToolResultPayload(payload) : isLegacyBracketToolCallPayload(payload))) {
			result += text.slice(cursor, payloadStart);
			cursor = payloadStart;
			continue;
		}
		result += text.slice(cursor, openStart);
		cursor = closeStart >= 0 ? closeStart + (closeMatch?.[0].length ?? 0) : text.length;
	}
	return result;
}
/**
* Strip downgraded tool call text representations that leak into user-visible
* text content when replaying history across providers.
*/
function stripDowngradedToolCallText(text) {
	if (!text) return text;
	if (!/\[Tool (?:Call|Result)/i.test(text) && !/\[Historical context/i.test(text)) return text;
	const consumeJsonish = (input, start, options) => {
		const { allowLeadingNewlines = false } = options ?? {};
		let index = start;
		while (index < input.length) {
			const ch = input[index];
			if (ch === " " || ch === "	") {
				index += 1;
				continue;
			}
			if (allowLeadingNewlines && (ch === "\n" || ch === "\r")) {
				index += 1;
				continue;
			}
			break;
		}
		if (index >= input.length) return null;
		const startChar = input[index];
		if (startChar === "{" || startChar === "[") {
			let depth = 0;
			let inString = false;
			let escape = false;
			for (let idx = index; idx < input.length; idx += 1) {
				const ch = input[idx];
				if (inString) {
					if (escape) escape = false;
					else if (ch === "\\") escape = true;
					else if (ch === "\"") inString = false;
					continue;
				}
				if (ch === "\"") {
					inString = true;
					continue;
				}
				if (ch === "{" || ch === "[") depth += 1;
				else if (ch === "}" || ch === "]") {
					depth -= 1;
					if (depth === 0) return idx + 1;
				}
			}
			return null;
		}
		if (startChar === "\"") {
			let escape = false;
			for (let idx = index + 1; idx < input.length; idx += 1) {
				const ch = input[idx];
				if (escape) {
					escape = false;
					continue;
				}
				if (ch === "\\") {
					escape = true;
					continue;
				}
				if (ch === "\"") return idx + 1;
			}
			return null;
		}
		let end = index;
		while (end < input.length && input[end] !== "\n" && input[end] !== "\r") end += 1;
		return end;
	};
	const stripToolCalls = (input) => {
		const toolCallRe = /\[Tool Call:[^\]]*\]/gi;
		const codeRegions = findCodeRegions(input);
		let result = "";
		let cursor = 0;
		for (const match of input.matchAll(toolCallRe)) {
			const start = match.index ?? 0;
			if (start < cursor) continue;
			if (isInsideCode(start, codeRegions)) continue;
			result += input.slice(cursor, start);
			let index = start + match[0].length;
			while (index < input.length && (input[index] === " " || input[index] === "	")) index += 1;
			if (input[index] === "\r") {
				index += 1;
				if (input[index] === "\n") index += 1;
			} else if (input[index] === "\n") index += 1;
			while (index < input.length && (input[index] === " " || input[index] === "	")) index += 1;
			if (normalizeLowercaseStringOrEmpty(input.slice(index, index + 9)) === "arguments") {
				index += 9;
				if (input[index] === ":") index += 1;
				if (input[index] === " ") index += 1;
				const end = consumeJsonish(input, index, { allowLeadingNewlines: true });
				if (end !== null) index = end;
			}
			if ((input[index] === "\n" || input[index] === "\r") && (result.endsWith("\n") || result.endsWith("\r") || result.length === 0)) {
				if (input[index] === "\r") index += 1;
				if (input[index] === "\n") index += 1;
			}
			cursor = index;
		}
		result += input.slice(cursor);
		return result;
	};
	let cleaned = stripToolCalls(text);
	let codeRegions = findCodeRegions(cleaned);
	cleaned = cleaned.replace(/\[Tool Result for ID[^\]]*\]\n?[\s\S]*?(?=\n*\[Tool |\n*$)/gi, (match, offset) => isInsideCode(offset, codeRegions) ? match : "");
	codeRegions = findCodeRegions(cleaned);
	cleaned = cleaned.replace(/\[Historical context:[^\]]*\]\n?/gi, (match, offset) => isInsideCode(offset, codeRegions) ? 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;
	const codeRegions = findCodeRegions(text);
	let result = "";
	let lineStart = 0;
	while (lineStart < text.length) {
		const newlineIndex = text.indexOf("\n", lineStart);
		const lineEnd = newlineIndex === -1 ? text.length : newlineIndex + 1;
		const rawLine = text.slice(lineStart, lineEnd);
		const trimmed = (rawLine.endsWith("\n") ? rawLine.slice(0, -1).replace(/\r$/, "") : rawLine).trim();
		if (!(!isInsideCode(lineStart, codeRegions) && (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)))) result += rawLine;
		lineStart = lineEnd;
	}
	return result;
}
const ASSISTANT_VISIBLE_TEXT_PIPELINE_OPTIONS = {
	delivery: {
		finalTrim: "both",
		stripFunctionResponseAfterPluralToolCalls: true,
		reasoningMode: "strict",
		reasoningTrim: "both",
		stageOrder: "reasoning-last"
	},
	"final-answer-delivery": {
		finalTrim: "both",
		stripFunctionResponseAfterPluralToolCalls: true,
		reasoningMode: "strict",
		reasoningScope: "leading",
		reasoningTrim: "both",
		stageOrder: "reasoning-last"
	},
	history: {
		finalTrim: "none",
		reasoningMode: "strict",
		reasoningTrim: "none",
		stageOrder: "reasoning-last"
	},
	"internal-scaffolding": {
		finalTrim: "start",
		preserveDowngradedToolText: true,
		preserveMinimaxToolXml: true,
		reasoningMode: "preserve",
		reasoningTrim: "start",
		stageOrder: "reasoning-first"
	},
	"tool-progress": {
		finalTrim: "both",
		stripFunctionCallsXmlPayloads: true,
		stripInternalTraceLines: false,
		reasoningMode: "strict",
		reasoningTrim: "both",
		stageOrder: "reasoning-last"
	}
};
function applyAssistantVisibleTextStagePipeline(text, options) {
	if (!text) return text;
	const stripReasoning = (value) => stripReasoningTagsFromText(value, {
		mode: options.reasoningMode,
		scope: options.reasoningScope,
		trim: options.reasoningTrim
	});
	const applyFinalTrim = (value) => {
		if (options.finalTrim === "none") return value;
		if (options.finalTrim === "start") return value.trimStart();
		return value.trim();
	};
	const stripNonReasoningStages = (value) => {
		let cleaned = value;
		if (!options.preserveMinimaxToolXml) cleaned = stripMinimaxToolCallXml(cleaned);
		cleaned = stripModelSpecialTokens(cleaned);
		cleaned = stripRelevantMemoriesTags(cleaned);
		cleaned = stripToolCallXmlTags(cleaned, {
			stripFunctionCallsXmlPayloads: options.stripFunctionCallsXmlPayloads,
			stripFunctionResponseAfterPluralToolCalls: options.stripFunctionResponseAfterPluralToolCalls
		});
		if (options.stripInternalTraceLines !== false) cleaned = stripAssistantInternalTraceLines(cleaned);
		cleaned = stripLegacyBracketToolCallBlocks(cleaned);
		cleaned = stripPlainTextToolCallBlocks(cleaned, { resolveProtectedRanges: findCodeRegions });
		if (!options.preserveDowngradedToolText) cleaned = stripDowngradedToolCallText(cleaned);
		return cleaned;
	};
	if (options.stageOrder === "reasoning-first") return applyFinalTrim(stripNonReasoningStages(stripReasoning(text)));
	return applyFinalTrim(stripReasoning(stripNonReasoningStages(text)));
}
function sanitizeAssistantVisibleTextWithProfile(text, profile = "delivery") {
	return applyAssistantVisibleTextStagePipeline(text, ASSISTANT_VISIBLE_TEXT_PIPELINE_OPTIONS[profile]);
}
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 { stripAssistantInternalScaffolding as a, stripLegacyBracketToolCallBlocks as c, hasOrphanReasoningCloseBoundary as d, stripReasoningTagsFromText as f, sanitizeAssistantVisibleTextWithProfile as i, stripMinimaxToolCallXml as l, stripFinalTags as m, sanitizeAssistantVisibleText as n, stripAssistantInternalTraceLines as o, findFinalTagMatches as p, sanitizeAssistantVisibleTextWithOptions as r, stripDowngradedToolCallText as s, sanitizeAssistantFinalAnswerText as t, stripToolCallXmlTags as u };
