From 6a4bfa31a925af67c026cd68700fe7779fd7c4cb Mon Sep 17 00:00:00 2001 From: amankumarsingh77 Date: Sat, 2 May 2026 16:00:58 +0530 Subject: feat: add defineBrowserTool helper, truncation, schemas/common, empty registry Co-Authored-By: Claude Sonnet 4.6 --- src/util/truncate.ts | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/util/truncate.ts (limited to 'src/util/truncate.ts') 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 => { + 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 => { + 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 }; +}; -- cgit v1.2.3