oira666_pi-browser-agent 0.0.6 → 0.0.8

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.6",
3
+ "version": "0.0.8",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Pi browser agent broker skeleton with lazy browser tool registration",
@@ -8,11 +8,7 @@ import {
8
8
  requestBridge,
9
9
  textResult,
10
10
  } from './read_only/common.ts';
11
-
12
- interface ToolResult {
13
- content: Array<{ type: 'text'; text: string }>;
14
- details: Record<string, unknown>;
15
- }
11
+ import type { ToolResult } from './read_only/common.ts';
16
12
 
17
13
  function withDefaultActiveTabTarget(params: Record<string, unknown>) {
18
14
  if (typeof params.tab_id === 'number' || typeof params.url === 'string' || params.use_active_tab === true) {
@@ -10,8 +10,12 @@ 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
+ export type ToolResultContentBlock =
14
+ | { type: 'text'; text: string }
15
+ | { type: 'image'; source: { base64: { media_type: string; data: string } } };
16
+
13
17
  export interface ToolResult {
14
- content: Array<{ type: 'text'; text: string }>;
18
+ content: ToolResultContentBlock[];
15
19
  details: Record<string, unknown>;
16
20
  }
17
21
 
@@ -43,6 +47,24 @@ export function textResult(text: string, details: Record<string, unknown>): Tool
43
47
  };
44
48
  }
45
49
 
50
+ export function imageResult(text: string, image: { mime: string; dataBase64: string }, details: Record<string, unknown>): ToolResult {
51
+ return {
52
+ content: [
53
+ { type: 'text', text },
54
+ {
55
+ type: 'image',
56
+ source: {
57
+ base64: {
58
+ media_type: image.mime,
59
+ data: image.dataBase64,
60
+ },
61
+ },
62
+ },
63
+ ],
64
+ details,
65
+ };
66
+ }
67
+
46
68
  export function coerceError(error: unknown, fallbackCode = 'E_INTERNAL'): BrowserReadOnlyToolError {
47
69
  if (error instanceof BrowserReadOnlyToolError) {
48
70
  return error;
@@ -1,3 +1,5 @@
1
+ import { readFile } from 'node:fs/promises';
2
+
1
3
  import { Type } from '@sinclair/typebox';
2
4
 
3
5
  import type { BrowserAgentBroker } from '../../broker/server.ts';
@@ -8,6 +10,7 @@ import {
8
10
  defaultTimeoutMs,
9
11
  formatJsonResult,
10
12
  formatTextResult,
13
+ imageResult,
11
14
  inferFileExtensionFromMime,
12
15
  normalizeScreenshotPayload,
13
16
  requestBridge,
@@ -15,7 +18,7 @@ import {
15
18
  textResult,
16
19
  } from './common.ts';
17
20
 
18
- const SCREENSHOT_INLINE_LIMIT_BYTES = 256 * 1024;
21
+ const SCREENSHOT_IMAGE_CONTENT_LIMIT_BYTES = 5 * 1024 * 1024;
19
22
 
20
23
  function levelsType() {
21
24
  return Type.Array(Type.Union([
@@ -81,6 +84,21 @@ function createJsonReadOnlyTool<TParams>(
81
84
  };
82
85
  }
83
86
 
87
+ function redactScreenshotResponse(response: any): any {
88
+ if (!response || typeof response !== 'object' || !response.data || typeof response.data !== 'object') {
89
+ return response;
90
+ }
91
+
92
+ return {
93
+ ...response,
94
+ data: {
95
+ ...response.data,
96
+ data_base64: undefined,
97
+ dataBase64: undefined,
98
+ },
99
+ };
100
+ }
101
+
84
102
  function createScreenshotTool(broker: BrowserAgentBroker): ReadOnlyToolDefinition<any> {
85
103
  return {
86
104
  name: 'browser_get_screenshot',
@@ -107,30 +125,44 @@ function createScreenshotTool(broker: BrowserAgentBroker): ReadOnlyToolDefinitio
107
125
  const normalized = normalizeScreenshotPayload(data);
108
126
  let path = normalized.path;
109
127
  let dataBase64 = normalized.data_base64;
110
- const inlineBytes = dataBase64 ? Buffer.byteLength(dataBase64, 'base64') : 0;
128
+ let imageBytes = dataBase64 ? Buffer.byteLength(dataBase64, 'base64') : 0;
111
129
 
112
- if (!path && dataBase64 && inlineBytes > SCREENSHOT_INLINE_LIMIT_BYTES) {
130
+ if (!dataBase64 && path) {
131
+ const buffer = await readFile(path);
132
+ imageBytes = buffer.length;
133
+ if (imageBytes <= SCREENSHOT_IMAGE_CONTENT_LIMIT_BYTES) {
134
+ dataBase64 = buffer.toString('base64');
135
+ }
136
+ }
137
+
138
+ if (!path && dataBase64) {
113
139
  path = await spillBase64Payload(dataBase64, inferFileExtensionFromMime(normalized.mime));
114
- dataBase64 = undefined;
115
140
  }
116
141
 
117
- const location = path ? `saved to ${path}` : `returned inline (${inlineBytes} bytes)`;
118
- return textResult(
119
- `Captured screenshot for ${normalized.title || normalized.url || 'tab'}; ${location}.`,
120
- {
121
- ok: true,
122
- requestType: 'browser_get_screenshot',
123
- response,
124
- result: {
125
- ...normalized,
126
- data_base64: dataBase64,
127
- path,
128
- },
129
- inlined: !!dataBase64,
130
- spilledToFile: !!path,
131
- inlineBytes,
142
+ const canAttachImage = !!dataBase64 && imageBytes <= SCREENSHOT_IMAGE_CONTENT_LIMIT_BYTES;
143
+ const location = path ? `saved to ${path}` : `captured ${imageBytes} bytes`;
144
+ const text = canAttachImage
145
+ ? `Captured screenshot for ${normalized.title || normalized.url || 'tab'}; attached as an image (${imageBytes} bytes). ${location}.`
146
+ : `Captured screenshot for ${normalized.title || normalized.url || 'tab'}; image is too large to attach (${imageBytes} bytes), ${location}.`;
147
+ const details = {
148
+ ok: true,
149
+ requestType: 'browser_get_screenshot',
150
+ response: redactScreenshotResponse(response),
151
+ result: {
152
+ ...normalized,
153
+ data_base64: undefined,
154
+ path,
132
155
  },
133
- );
156
+ imageAttached: canAttachImage,
157
+ spilledToFile: !!path,
158
+ imageBytes,
159
+ };
160
+
161
+ if (canAttachImage) {
162
+ return imageResult(text, { mime: normalized.mime, dataBase64 }, details);
163
+ }
164
+
165
+ return textResult(text, details);
134
166
  } catch (error) {
135
167
  const info = coerceError(error);
136
168
  return textResult(`browser_get_screenshot failed: ${info.message}`, {
@@ -62,7 +62,7 @@ test('browser_get_html truncates oversized HTML and spills the full output to a
62
62
  await rm(spillPath, { force: true });
63
63
  });
64
64
 
65
- test('browser_get_screenshot spills large inline payloads to a temp file', async () => {
65
+ test('browser_get_screenshot returns screenshots as image content and never exposes base64 in details', async () => {
66
66
  const largeBase64 = Buffer.alloc(300 * 1024, 7).toString('base64');
67
67
  const tools = createReadOnlyBrowserToolMap(createBrokerHarness(async (type) => {
68
68
  assert.equal(type, 'browser_get_screenshot');
@@ -84,9 +84,13 @@ test('browser_get_screenshot spills large inline payloads to a temp file', async
84
84
 
85
85
  const result = await tools.get('browser_get_screenshot')!.execute('call-1', {});
86
86
  assert.equal(result.details.ok, true);
87
- assert.equal(result.details.inlined, false);
87
+ assert.equal(result.details.imageAttached, true);
88
88
  assert.equal(result.details.spilledToFile, true);
89
+ assert.equal((result.content?.[1] as any)?.type, 'image');
90
+ assert.equal((result.content?.[1] as any)?.source?.base64?.media_type, 'image/jpeg');
91
+ assert.equal((result.content?.[1] as any)?.source?.base64?.data, largeBase64);
89
92
  assert.equal((result.details.result as any).data_base64, undefined);
93
+ assert.equal((result.details.response as any).data.data_base64, undefined);
90
94
 
91
95
  const spillPath = String((result.details.result as any).path);
92
96
  const spilled = await readFile(spillPath);