oira666_pi-browser-agent 0.0.9 → 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oira666_pi-browser-agent",
3
- "version": "0.0.9",
3
+ "version": "0.0.10",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Pi browser agent broker skeleton with lazy browser tool registration",
@@ -1,6 +1,6 @@
1
1
  import { Buffer } from 'node:buffer';
2
2
  import { randomBytes } from 'node:crypto';
3
- import { writeFile } from 'node:fs/promises';
3
+ import { mkdir, readdir, rm, stat, writeFile } from 'node:fs/promises';
4
4
  import { tmpdir } from 'node:os';
5
5
  import { join } from 'node:path';
6
6
 
@@ -10,6 +10,10 @@ import type { BrowserAgentBroker } from '../../broker/server.ts';
10
10
  import type { ResponseFrame } from '../../broker/protocol.ts';
11
11
  import { truncateAndSpill } from '../../util/truncate.ts';
12
12
 
13
+ const SCREENSHOT_TEMP_DIR = join(tmpdir(), 'pi-browser-agent', 'screenshots');
14
+ const SCREENSHOT_TEMP_MAX_AGE_MS = 24 * 60 * 60 * 1000;
15
+ let screenshotCleanupStarted = false;
16
+
13
17
  export type ToolResultContentBlock =
14
18
  | { type: 'text'; text: string }
15
19
  | { type: 'image'; data: string; mimeType: string };
@@ -143,8 +147,45 @@ export async function formatTextResult(title: string, value: string, ext = 'txt'
143
147
  };
144
148
  }
145
149
 
150
+ async function cleanupOldScreenshotTempFiles(): Promise<void> {
151
+ const now = Date.now();
152
+ let entries: string[];
153
+ try {
154
+ entries = await readdir(SCREENSHOT_TEMP_DIR);
155
+ } catch {
156
+ return;
157
+ }
158
+
159
+ await Promise.all(entries.map(async (entry) => {
160
+ const path = join(SCREENSHOT_TEMP_DIR, entry);
161
+ try {
162
+ const info = await stat(path);
163
+ if (!info.isFile()) {
164
+ return;
165
+ }
166
+ const createdAt = Number.isFinite(info.birthtimeMs) && info.birthtimeMs > 0 ? info.birthtimeMs : info.mtimeMs;
167
+ if (now - createdAt > SCREENSHOT_TEMP_MAX_AGE_MS) {
168
+ await rm(path, { force: true });
169
+ }
170
+ } catch {
171
+ // Best-effort cleanup only; never fail the screenshot tool because temp cleanup failed.
172
+ }
173
+ }));
174
+ }
175
+
176
+ function startScreenshotTempCleanup(): void {
177
+ if (screenshotCleanupStarted) {
178
+ return;
179
+ }
180
+ screenshotCleanupStarted = true;
181
+ void cleanupOldScreenshotTempFiles();
182
+ }
183
+
146
184
  export async function spillBase64Payload(base64: string, ext: string): Promise<string> {
147
- const path = join(tmpdir(), `pi-browser-agent-${randomBytes(6).toString('hex')}.${ext}`);
185
+ startScreenshotTempCleanup();
186
+ await mkdir(SCREENSHOT_TEMP_DIR, { recursive: true });
187
+ const safeExt = /^[a-z0-9]+$/i.test(ext) ? ext : 'bin';
188
+ const path = join(SCREENSHOT_TEMP_DIR, `screenshot-${randomBytes(6).toString('hex')}.${safeExt}`);
148
189
  await writeFile(path, Buffer.from(base64, 'base64'));
149
190
  return path;
150
191
  }