oira666_pi-browser-agent 0.0.8 → 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
|
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,9 +10,13 @@ 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
|
-
| { type: 'image';
|
|
19
|
+
| { type: 'image'; data: string; mimeType: string };
|
|
16
20
|
|
|
17
21
|
export interface ToolResult {
|
|
18
22
|
content: ToolResultContentBlock[];
|
|
@@ -53,12 +57,8 @@ export function imageResult(text: string, image: { mime: string; dataBase64: str
|
|
|
53
57
|
{ type: 'text', text },
|
|
54
58
|
{
|
|
55
59
|
type: 'image',
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
media_type: image.mime,
|
|
59
|
-
data: image.dataBase64,
|
|
60
|
-
},
|
|
61
|
-
},
|
|
60
|
+
data: image.dataBase64,
|
|
61
|
+
mimeType: image.mime,
|
|
62
62
|
},
|
|
63
63
|
],
|
|
64
64
|
details,
|
|
@@ -147,8 +147,45 @@ export async function formatTextResult(title: string, value: string, ext = 'txt'
|
|
|
147
147
|
};
|
|
148
148
|
}
|
|
149
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
|
+
|
|
150
184
|
export async function spillBase64Payload(base64: string, ext: string): Promise<string> {
|
|
151
|
-
|
|
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}`);
|
|
152
189
|
await writeFile(path, Buffer.from(base64, 'base64'));
|
|
153
190
|
return path;
|
|
154
191
|
}
|
|
@@ -87,8 +87,8 @@ test('browser_get_screenshot returns screenshots as image content and never expo
|
|
|
87
87
|
assert.equal(result.details.imageAttached, true);
|
|
88
88
|
assert.equal(result.details.spilledToFile, true);
|
|
89
89
|
assert.equal((result.content?.[1] as any)?.type, 'image');
|
|
90
|
-
assert.equal((result.content?.[1] as any)?.
|
|
91
|
-
assert.equal((result.content?.[1] as any)?.
|
|
90
|
+
assert.equal((result.content?.[1] as any)?.mimeType, 'image/jpeg');
|
|
91
|
+
assert.equal((result.content?.[1] as any)?.data, largeBase64);
|
|
92
92
|
assert.equal((result.details.result as any).data_base64, undefined);
|
|
93
93
|
assert.equal((result.details.response as any).data.data_base64, undefined);
|
|
94
94
|
|