MCPcopy Create free account
hub / github.com/cloudflare/computer / truncate

Function truncate

packages/computer/src/tools/exec.ts:109–124  ·  view source on GitHub ↗
(value: string, maxBytes: number)

Source from the content-addressed store, hash-verified

107const encoder = new TextEncoder();
108
109function truncate(value: string, maxBytes: number): string {
110 if (!value) return value;
111 const totalBytes = encoder.encode(value).byteLength;
112 if (totalBytes <= maxBytes) return value;
113
114 let usedBytes = 0;
115 let endOffset = 0;
116 for (const char of value) {
117 const charBytes = encoder.encode(char).byteLength;
118 if (usedBytes + charBytes > maxBytes) break;
119 usedBytes += charBytes;
120 endOffset += char.length;
121 }
122
123 return `${value.slice(0, endOffset)}\n\n[truncated, ${totalBytes - usedBytes} more bytes]`;
124}

Callers 1

createExecToolFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected