UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

85 lines • 3.08 kB
const PER_RESULT_TRUNCATE_THRESHOLD_BYTES = 4096; const TRUNCATION_HEAD_CHARS = 800; const TRUNCATION_TAIL_CHARS = 400; const TRUNCATION_MARKER_RE = /<truncated:\d+ bytes original[^>]*>/; function utf8Bytes(text) { return new TextEncoder().encode(text).length; } function approxTokens(text) { return Math.ceil(text.length / 4); } function totalTokens(results) { let total = 0; for (const result of results) { for (const block of result.content) { if (block.type === "text") { total += approxTokens(block.text); continue; } if (block.type === "image") { total += approxTokens(block.data); } } } return total; } function buildMarker(originalBytes) { return `<truncated:${originalBytes} bytes original; middle elided to save context - re-run this tool with a narrower range (read offset/limit or a filtered command) to retrieve the elided content>`; } function truncateTextBlock(block, headChars, tailChars) { const text = block.text; const bytes = utf8Bytes(text); if (bytes <= PER_RESULT_TRUNCATE_THRESHOLD_BYTES) return null; if (TRUNCATION_MARKER_RE.test(text)) return null; const head = text.slice(0, headChars); const tail = text.slice(text.length - tailChars); const marker = buildMarker(bytes); return { ...block, text: `${head}\n${marker}\n${tail}` }; } function applyHeadTailTruncation(result, headChars, tailChars) { let modified = false; const newContent = result.content.map((block) => { if (block.type !== "text") return block; const truncated = truncateTextBlock(block, headChars, tailChars); if (truncated) { modified = true; return truncated; } return block; }); if (!modified) return result; return { ...result, content: newContent }; } function reduceMarkedTextToMarker(result) { let modified = false; const newContent = result.content.map((block) => { if (block.type !== "text") return block; const markerMatch = block.text.match(TRUNCATION_MARKER_RE); if (markerMatch && block.text !== markerMatch[0]) { modified = true; return { ...block, text: markerMatch[0] }; } return block; }); if (!modified) return result; return { ...result, content: newContent }; } export function truncateOversizedToolResults(results) { return results.map((result) => applyHeadTailTruncation(result, TRUNCATION_HEAD_CHARS, TRUNCATION_TAIL_CHARS)); } export function prePruneToolOutputsToBudget(results, targetTokens) { if (totalTokens(results) <= targetTokens) { return results.map((result) => ({ ...result })); } const firstPass = truncateOversizedToolResults(results); if (totalTokens(firstPass) <= targetTokens) return firstPass; return firstPass.map((result) => reduceMarkedTextToMarker(result)); } //# sourceMappingURL=tool-truncation.js.map