//#region src/agent/tools/truncate.ts
const DEFAULT_MAX_LINES = 2e3;
const DEFAULT_MAX_BYTES = 50 * 1024;
const GREP_MAX_LINE_LENGTH = 500;
function formatSize(bytes) {
	if (bytes < 1024) return `${bytes}B`;
	if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)}KB`;
	return `${(bytes / (1024 * 1024)).toFixed(1)}MB`;
}
/**
* Truncate from head - keep beginning of content
*/
function truncateHead(content, options = {}) {
	const maxLines = options.maxLines ?? 2e3;
	const maxBytes = options.maxBytes ?? 51200;
	const totalBytes = Buffer.byteLength(content, "utf-8");
	const lines = content.split("\n");
	const totalLines = lines.length;
	if (totalLines <= maxLines && totalBytes <= maxBytes) return result(content, false, null, totalLines, totalBytes);
	if (Buffer.byteLength(lines[0], "utf-8") > maxBytes) return result("", true, "bytes", totalLines, totalBytes, 0, 0, false, true);
	const output = [];
	let bytes = 0;
	let truncatedBy = "lines";
	for (let i = 0; i < lines.length && i < maxLines; i++) {
		const lineLen = Buffer.byteLength(lines[i], "utf-8") + (i > 0 ? 1 : 0);
		if (bytes + lineLen > maxBytes) {
			truncatedBy = "bytes";
			break;
		}
		output.push(lines[i]);
		bytes += lineLen;
	}
	return result(output.join("\n"), true, truncatedBy, totalLines, totalBytes, output.length, bytes);
}
/**
* Truncate from tail - keep end of content (like tail command)
*/
function truncateTail(content, options = {}) {
	const maxLines = options.maxLines ?? 2e3;
	const maxBytes = options.maxBytes ?? 51200;
	const totalBytes = Buffer.byteLength(content, "utf-8");
	const lines = content.split("\n");
	const totalLines = lines.length;
	if (totalLines <= maxLines && totalBytes <= maxBytes) return result(content, false, null, totalLines, totalBytes);
	const output = [];
	let bytes = 0;
	let lastLinePartial = false;
	for (let i = Math.max(0, totalLines - maxLines); i < totalLines; i++) {
		const line = lines[i];
		const lineLen = Buffer.byteLength(line, "utf-8") + (output.length > 0 ? 1 : 0);
		if (bytes + lineLen > maxBytes) {
			if (output.length === 0 && lineLen > maxBytes) {
				const truncated = line.slice(0, Math.floor(maxBytes * .9));
				output.push(truncated);
				lastLinePartial = true;
			}
			break;
		}
		output.push(line);
		bytes += lineLen;
	}
	return result(output.join("\n"), true, "lines", totalLines, totalBytes, output.length, bytes, lastLinePartial);
}
/**
* Truncate long lines to max length
*/
function truncateLine(content, maxLen = 500) {
	return {
		text: content.length > maxLen ? content.slice(0, maxLen) + "..." : content,
		wasTruncated: content.length > maxLen
	};
}
function result(content, truncated, truncatedBy, totalLines, totalBytes, outputLines = totalLines, outputBytes = totalBytes, lastLinePartial = false, firstLineExceedsLimit = false) {
	return {
		content,
		truncated,
		truncatedBy,
		totalLines,
		totalBytes,
		outputLines,
		outputBytes,
		maxLines: DEFAULT_MAX_LINES,
		maxBytes: DEFAULT_MAX_BYTES,
		lastLinePartial,
		firstLineExceedsLimit
	};
}
//#endregion
export { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, GREP_MAX_LINE_LENGTH, formatSize, truncateHead, truncateLine, truncateTail };

//# sourceMappingURL=truncate.js.map