diff options
| author | amankumarsingh77 <amankumarsingh7702@gmal.com> | 2026-05-02 16:00:58 +0530 |
|---|---|---|
| committer | amankumarsingh77 <amankumarsingh7702@gmal.com> | 2026-05-02 16:00:58 +0530 |
| commit | 6a4bfa31a925af67c026cd68700fe7779fd7c4cb (patch) | |
| tree | 640a95ed6b17787bd4022a21f73bd4c0a83d8f40 /src/util/truncate.ts | |
| parent | d3d86f9de1cd3465cd1ec1e9a34c36b2f4878694 (diff) | |
feat: add defineBrowserTool helper, truncation, schemas/common, empty registry
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'src/util/truncate.ts')
| -rw-r--r-- | src/util/truncate.ts | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/util/truncate.ts b/src/util/truncate.ts new file mode 100644 index 0000000..2bf0600 --- /dev/null +++ b/src/util/truncate.ts @@ -0,0 +1,35 @@ +import { mkdtemp, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { + DEFAULT_MAX_BYTES, + DEFAULT_MAX_LINES, + formatSize, + truncateHead, + withFileMutationQueue, +} from "@mariozechner/pi-coding-agent"; + +const tempDirs: string[] = []; + +export const cleanupTempDirs = async (): Promise<void> => { + const dirs = tempDirs.splice(0); + await Promise.all(dirs.map((d) => rm(d, { recursive: true, force: true }).catch(() => {}))); +}; + +export type TruncatedOutput = { + readonly text: string; + readonly fullOutputPath?: string; + readonly wasTruncated: boolean; +}; + +export const applyTruncation = async (output: string, prefix: string): Promise<TruncatedOutput> => { + const t = truncateHead(output, { maxLines: DEFAULT_MAX_LINES, maxBytes: DEFAULT_MAX_BYTES }); + if (!t.truncated) return { text: t.content, wasTruncated: false }; + const dir = await mkdtemp(join(tmpdir(), `pi-bh-${prefix}-`)); + tempDirs.push(dir); + const file = join(dir, "output.txt"); + await withFileMutationQueue(file, async () => { await writeFile(file, output, "utf8"); }); + const omitted = t.totalBytes - t.outputBytes; + const text = `${t.content}\n\n[Output truncated: ${t.outputLines} of ${t.totalLines} lines (${formatSize(t.outputBytes)} of ${formatSize(t.totalBytes)}). ${formatSize(omitted)} omitted. Full output: ${file}]`; + return { text, fullOutputPath: file, wasTruncated: true }; +}; |
