import { i as redactSecrets, u as redactToolPayloadText } from "./redact-C-AiL-kv.js";
import { n as truncateUtf16Safe } from "./utf16-slice-DgabhXOg.js";
import { t as sanitizeSurrogates } from "./sanitize-unicode-CHuIUv6Q.js";
//#region src/llm/providers/tool-result-text.ts
const PROVIDER_TOOL_RESULT_MAX_CHARS = 8e3;
const IMAGE_TOOL_RESULT_TYPES = new Set([
	"image",
	"image_url",
	"input_image"
]);
const AUDIO_TOOL_RESULT_TYPES = new Set([
	"audio",
	"input_audio",
	"output_audio"
]);
const MEDIA_ONLY_TOOL_RESULT_TYPES = new Set([...IMAGE_TOOL_RESULT_TYPES, ...AUDIO_TOOL_RESULT_TYPES]);
const INLINE_DATA_URI_PATTERN = /(^|[^A-Za-z0-9_])data:([a-z][a-z0-9.+-]*\/[a-z0-9.+-]+(?:;[a-z0-9.+-]+=[^,;"'\s]+|;base64)*,[^\s"'<>)]+)/gi;
const MIME_KEY_CANDIDATES = [
	"mimeType",
	"mime_type",
	"mediaType",
	"media_type",
	"contentType",
	"content_type"
];
const TEXTUAL_MIME_PATTERN = /^(?:text\/|application\/(?:json|ld\+json|x-ndjson|xml|javascript|x-www-form-urlencoded)|[^/]+\/[^+]+\+(?:json|xml)$)/i;
const OPAQUE_OR_BINARY_FIELD_RE = /^(?:blob|buffer|bytes|encrypted_content|encrypted_stdout)$/i;
function isRecord(value) {
	return value !== null && typeof value === "object" && !Array.isArray(value);
}
function readMimeType(value) {
	if (!isRecord(value)) return;
	for (const key of MIME_KEY_CANDIDATES) {
		const mimeType = value[key];
		if (typeof mimeType === "string" && mimeType.trim().length > 0) return mimeType;
	}
}
function isBinaryMimeType(mimeType) {
	const normalized = mimeType.split(";", 1)[0]?.trim().toLowerCase();
	return normalized ? !TEXTUAL_MIME_PATTERN.test(normalized) : false;
}
function describeOmittedValue(value, label) {
	const length = typeof value === "string" ? value.length : JSON.stringify(value)?.length;
	return length ? `[${label} omitted: ${length} chars]` : `[${label} omitted]`;
}
function redactInlineDataUris(value) {
	return value.replace(INLINE_DATA_URI_PATTERN, (_match, prefix, uri) => `${prefix}[inline data URI: ${uri.length} chars]`);
}
function redactStructuredTextValue(value) {
	const redacted = redactToolPayloadText(value);
	const trimmed = redacted.trim();
	if (!trimmed.startsWith("{") && !trimmed.startsWith("[")) return redacted;
	try {
		const redactedWrapper = redactSecrets({ structuredTextValue: JSON.parse(redacted) });
		return JSON.stringify(redactedWrapper.structuredTextValue);
	} catch {
		return redacted;
	}
}
function stringifyStructuredBlock(block) {
	const seen = /* @__PURE__ */ new WeakSet();
	try {
		const redactedBlock = redactSecrets({ structuredToolResult: block }).structuredToolResult;
		const serialized = JSON.stringify(redactedBlock, function structuredToolResultReplacer(key, value) {
			if (OPAQUE_OR_BINARY_FIELD_RE.test(key)) return `[omitted ${key}]`;
			if (key === "data") {
				const mimeType = readMimeType(this);
				if (mimeType && isBinaryMimeType(mimeType)) return describeOmittedValue(value, "binary data");
			}
			if (typeof value === "bigint") return value.toString();
			if (typeof value === "string") return redactInlineDataUris(redactStructuredTextValue(value));
			if (typeof value === "function" || typeof value === "symbol" || value === void 0) return;
			if (!value || typeof value !== "object") return value;
			if (seen.has(value)) return "[Circular]";
			seen.add(value);
			return value;
		});
		if (!serialized || serialized === "{}") return;
		return serialized;
	} catch {
		return;
	}
}
function truncateProviderToolText(text) {
	if (text.length <= PROVIDER_TOOL_RESULT_MAX_CHARS) return text;
	return `${truncateUtf16Safe(text, PROVIDER_TOOL_RESULT_MAX_CHARS)}\n…(truncated)…`;
}
function describeToolResultMediaPlaceholder(blocks) {
	let hasImage = false;
	let hasAudio = false;
	for (const block of blocks) {
		if (!block || typeof block !== "object") continue;
		const record = block;
		const type = typeof record.type === "string" ? record.type : void 0;
		const mimeType = readMimeType(record);
		if (type && IMAGE_TOOL_RESULT_TYPES.has(type) || mimeType?.toLowerCase().startsWith("image/")) hasImage = true;
		if (type && AUDIO_TOOL_RESULT_TYPES.has(type) || mimeType?.toLowerCase().startsWith("audio/")) hasAudio = true;
	}
	if (hasImage && hasAudio) return "(see attached media)";
	if (hasAudio) return "(see attached audio)";
	if (hasImage) return "(see attached image)";
}
function extractToolResultBlockText(block) {
	if (!block || typeof block !== "object") return;
	const record = block;
	if (typeof record.type === "string" && MEDIA_ONLY_TOOL_RESULT_TYPES.has(record.type)) return;
	if (record.type === "text") {
		const text = typeof record.text === "string" ? record.text : "";
		return text ? sanitizeSurrogates(text) : void 0;
	}
	const structured = stringifyStructuredBlock(record);
	return structured ? sanitizeSurrogates(truncateProviderToolText(structured)) : void 0;
}
function extractToolResultText(blocks) {
	const explicitTexts = [];
	const structuredTexts = [];
	for (const block of blocks) {
		const text = extractToolResultBlockText(block);
		if (!text) continue;
		if (block.type === "text") explicitTexts.push(text);
		else structuredTexts.push(text);
	}
	if (explicitTexts.length > 0) return sanitizeSurrogates(explicitTexts.join("\n"));
	return sanitizeSurrogates(truncateProviderToolText(structuredTexts.join("\n")));
}
//#endregion
export { extractToolResultBlockText as n, extractToolResultText as r, describeToolResultMediaPlaceholder as t };
