mcp-browser-dev-tools 0.0.6 → 0.2.0
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/README.md +94 -312
- package/package.json +18 -2
- package/src/action-retry.mjs +50 -0
- package/src/browser-adapter.mjs +15 -4
- package/src/browser-requests.mjs +15 -0
- package/src/cdp-client.mjs +104 -10
- package/src/config.mjs +6 -1
- package/src/firefox-bidi-client.mjs +26 -3
- package/src/json-schema.mjs +99 -0
- package/src/mcp-server.mjs +88 -1465
- package/src/page-context.mjs +25 -1034
- package/src/page-script.mjs +1372 -0
- package/src/run-steps.mjs +510 -0
- package/src/run-tabs.mjs +291 -0
- package/src/screenshot-output.mjs +141 -0
- package/src/tool-results.mjs +34 -0
- package/src/tool-schemas.mjs +431 -0
- package/src/tools/action-tools.mjs +318 -0
- package/src/tools/browser-tools.mjs +221 -0
- package/src/tools/inspection-tools.mjs +287 -0
- package/src/tools/state-tools.mjs +327 -0
- package/src/top-level-await.mjs +260 -0
- package/src/wait-for.mjs +453 -47
package/src/run-tabs.mjs
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import { MAX_RUN_STEPS, stepSchema } from "./run-steps.mjs";
|
|
2
|
+
import { asImageToolResult } from "./tool-results.mjs";
|
|
3
|
+
import { waitUntilProperty } from "./tool-schemas.mjs";
|
|
4
|
+
|
|
5
|
+
export const MAX_TABS = 8;
|
|
6
|
+
export const DEFAULT_TAB_TIMEOUT_MS = 60_000;
|
|
7
|
+
export const MAX_TAB_TIMEOUT_MS = 300_000;
|
|
8
|
+
|
|
9
|
+
export function runTabsInputSchema(stepTools, browserFamily) {
|
|
10
|
+
return {
|
|
11
|
+
type: "object",
|
|
12
|
+
properties: {
|
|
13
|
+
tabs: {
|
|
14
|
+
type: "array",
|
|
15
|
+
minItems: 1,
|
|
16
|
+
maxItems: MAX_TABS,
|
|
17
|
+
description: `Tabs to run at the same time (at most ${MAX_TABS}). Each gives either url, to open a new tab there, or sessionId, to use an attached tab, plus the steps to run in it.`,
|
|
18
|
+
items: {
|
|
19
|
+
type: "object",
|
|
20
|
+
properties: {
|
|
21
|
+
url: {
|
|
22
|
+
type: "string",
|
|
23
|
+
description:
|
|
24
|
+
"Open a new tab, attach to it, and load this URL before the steps.",
|
|
25
|
+
},
|
|
26
|
+
waitUntil: waitUntilProperty(),
|
|
27
|
+
sessionId: {
|
|
28
|
+
type: "string",
|
|
29
|
+
description:
|
|
30
|
+
"Run the steps in this attached session instead of opening a tab.",
|
|
31
|
+
},
|
|
32
|
+
steps: {
|
|
33
|
+
type: "array",
|
|
34
|
+
maxItems: MAX_RUN_STEPS,
|
|
35
|
+
description: `Steps to run in this tab, as in run_steps (at most ${MAX_RUN_STEPS}, default none).`,
|
|
36
|
+
items: stepSchema(stepTools),
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
additionalProperties: false,
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
...(browserFamily === "auto"
|
|
43
|
+
? {
|
|
44
|
+
browserFamily: {
|
|
45
|
+
type: "string",
|
|
46
|
+
enum: ["chromium", "firefox"],
|
|
47
|
+
description:
|
|
48
|
+
"Browser to open the url tabs in. Required when a tab gives url.",
|
|
49
|
+
},
|
|
50
|
+
}
|
|
51
|
+
: {}),
|
|
52
|
+
concurrency: {
|
|
53
|
+
type: "integer",
|
|
54
|
+
minimum: 1,
|
|
55
|
+
maximum: MAX_TABS,
|
|
56
|
+
description: `How many tabs run at once (default: all of them, at most ${MAX_TABS}). Lower it for sites that limit how fast they are read.`,
|
|
57
|
+
},
|
|
58
|
+
timeoutMs: {
|
|
59
|
+
type: "integer",
|
|
60
|
+
minimum: 1,
|
|
61
|
+
maximum: MAX_TAB_TIMEOUT_MS,
|
|
62
|
+
description: `How long each tab may take, from opening to its last step, before it fails (default ${DEFAULT_TAB_TIMEOUT_MS}).`,
|
|
63
|
+
},
|
|
64
|
+
keepTabs: {
|
|
65
|
+
type: "boolean",
|
|
66
|
+
description:
|
|
67
|
+
"Leave the tabs opened for url open and attached, and report their sessionId (default false: close them).",
|
|
68
|
+
},
|
|
69
|
+
continueOnError: {
|
|
70
|
+
type: "boolean",
|
|
71
|
+
description:
|
|
72
|
+
"Within each tab, run the remaining steps after a step fails (default false). A failing tab never stops the others.",
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
required: ["tabs"],
|
|
76
|
+
additionalProperties: false,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function timeoutAfter(timeoutMs) {
|
|
81
|
+
let timer;
|
|
82
|
+
const promise = new Promise((_, reject) => {
|
|
83
|
+
timer = setTimeout(() => {
|
|
84
|
+
reject(new Error(`Tab did not finish within ${timeoutMs}ms`));
|
|
85
|
+
}, timeoutMs);
|
|
86
|
+
});
|
|
87
|
+
promise.catch(() => {});
|
|
88
|
+
return { promise, clear: () => clearTimeout(timer) };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export class TabRunner {
|
|
92
|
+
constructor({ stepRunner, browserAdapter }) {
|
|
93
|
+
this.stepRunner = stepRunner;
|
|
94
|
+
this.browserAdapter = browserAdapter;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
prepareTabs(args, configuredFamily) {
|
|
98
|
+
const sessionIds = new Set();
|
|
99
|
+
const tabs = args.tabs.map((tab, index) => {
|
|
100
|
+
const path = `arguments.tabs[${index}]`;
|
|
101
|
+
if ((tab.url === undefined) === (tab.sessionId === undefined)) {
|
|
102
|
+
throw new Error(`${path} needs either url or sessionId`);
|
|
103
|
+
}
|
|
104
|
+
if (tab.waitUntil !== undefined && tab.url === undefined) {
|
|
105
|
+
throw new Error(`${path}.waitUntil requires url`);
|
|
106
|
+
}
|
|
107
|
+
if (tab.sessionId !== undefined) {
|
|
108
|
+
if (sessionIds.has(tab.sessionId)) {
|
|
109
|
+
throw new Error(
|
|
110
|
+
`${path}.sessionId ${tab.sessionId} is already used by another tab`,
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
sessionIds.add(tab.sessionId);
|
|
114
|
+
}
|
|
115
|
+
if (
|
|
116
|
+
tab.url !== undefined &&
|
|
117
|
+
configuredFamily === "auto" &&
|
|
118
|
+
!args.browserFamily
|
|
119
|
+
) {
|
|
120
|
+
throw new Error(
|
|
121
|
+
"arguments.browserFamily is required when a tab gives url",
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const context = { sessionId: tab.sessionId ?? "", count: 0 };
|
|
126
|
+
return {
|
|
127
|
+
index,
|
|
128
|
+
url: tab.url,
|
|
129
|
+
waitUntil: tab.waitUntil ?? "complete",
|
|
130
|
+
sessionId: tab.sessionId,
|
|
131
|
+
steps: this.stepRunner.prepareSteps(
|
|
132
|
+
tab.steps ?? [],
|
|
133
|
+
context,
|
|
134
|
+
`${path}.steps`,
|
|
135
|
+
0,
|
|
136
|
+
),
|
|
137
|
+
};
|
|
138
|
+
});
|
|
139
|
+
return tabs;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async runTab(tab, options, images) {
|
|
143
|
+
const startedAt = Date.now();
|
|
144
|
+
const entry = { index: tab.index, url: tab.url ?? null };
|
|
145
|
+
const controller = new AbortController();
|
|
146
|
+
let opened = null;
|
|
147
|
+
let sessionId = tab.sessionId;
|
|
148
|
+
|
|
149
|
+
const work = (async () => {
|
|
150
|
+
if (tab.url !== undefined) {
|
|
151
|
+
({ targetId: opened } = await this.browserAdapter.createTab(
|
|
152
|
+
"about:blank",
|
|
153
|
+
{ browserFamily: options.browserFamily },
|
|
154
|
+
));
|
|
155
|
+
if (controller.signal.aborted) {
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
({ sessionId } = await this.browserAdapter.attachToTarget(opened));
|
|
159
|
+
if (controller.signal.aborted) {
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
await this.browserAdapter.navigate(sessionId, tab.url, {
|
|
163
|
+
waitUntil: tab.waitUntil,
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
return this.stepRunner.executeSteps(tab.steps, {
|
|
167
|
+
sessionId,
|
|
168
|
+
continueOnError: options.continueOnError,
|
|
169
|
+
images,
|
|
170
|
+
count: 0,
|
|
171
|
+
signal: controller.signal,
|
|
172
|
+
});
|
|
173
|
+
})();
|
|
174
|
+
|
|
175
|
+
const timeout = timeoutAfter(options.timeoutMs);
|
|
176
|
+
let timedOut = false;
|
|
177
|
+
try {
|
|
178
|
+
const results = await Promise.race([work, timeout.promise]);
|
|
179
|
+
const ok =
|
|
180
|
+
results.length === tab.steps.length &&
|
|
181
|
+
results.every((result) => result.ok);
|
|
182
|
+
Object.assign(entry, {
|
|
183
|
+
ok,
|
|
184
|
+
ranSteps: results.length,
|
|
185
|
+
skippedSteps: tab.steps.length - results.length,
|
|
186
|
+
steps: results,
|
|
187
|
+
});
|
|
188
|
+
} catch (error) {
|
|
189
|
+
timedOut = error.message.startsWith("Tab did not finish");
|
|
190
|
+
Object.assign(entry, { ok: false, error: error.message });
|
|
191
|
+
} finally {
|
|
192
|
+
timeout.clear();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (timedOut) {
|
|
196
|
+
// Start no more steps, close a tab opened here so its work fails fast,
|
|
197
|
+
// and wait for the step in progress to end before this worker moves
|
|
198
|
+
// on, so no more than concurrency tabs are ever busy. Report the steps
|
|
199
|
+
// that ran, so every screenshot in the reply belongs to a step.
|
|
200
|
+
controller.abort();
|
|
201
|
+
if (opened && !options.keepTabs) {
|
|
202
|
+
await this.closeTab(opened, entry);
|
|
203
|
+
}
|
|
204
|
+
const results = await work.catch(() => null);
|
|
205
|
+
if (Array.isArray(results)) {
|
|
206
|
+
Object.assign(entry, {
|
|
207
|
+
ranSteps: results.length,
|
|
208
|
+
skippedSteps: tab.steps.length - results.length,
|
|
209
|
+
steps: results,
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
} else if (!entry.ok && sessionId) {
|
|
213
|
+
Object.assign(
|
|
214
|
+
entry,
|
|
215
|
+
await this.stepRunner.describePageAfterFailure(sessionId),
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (sessionId && (tab.sessionId !== undefined || options.keepTabs)) {
|
|
220
|
+
entry.sessionId = sessionId;
|
|
221
|
+
}
|
|
222
|
+
if (opened) {
|
|
223
|
+
entry.targetId = opened;
|
|
224
|
+
if (!options.keepTabs && entry.closed === undefined) {
|
|
225
|
+
await this.closeTab(opened, entry);
|
|
226
|
+
}
|
|
227
|
+
entry.closed ??= false;
|
|
228
|
+
}
|
|
229
|
+
entry.durationMs = Date.now() - startedAt;
|
|
230
|
+
return entry;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
async closeTab(targetId, entry) {
|
|
234
|
+
try {
|
|
235
|
+
await this.browserAdapter.closeTarget(targetId);
|
|
236
|
+
entry.closed = true;
|
|
237
|
+
} catch (error) {
|
|
238
|
+
entry.closed = false;
|
|
239
|
+
entry.closeError = error.message;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
async runTabs(args, configuredFamily) {
|
|
244
|
+
// Validate every tab's steps before opening anything.
|
|
245
|
+
const tabs = this.prepareTabs(args, configuredFamily);
|
|
246
|
+
const options = {
|
|
247
|
+
browserFamily: args.browserFamily,
|
|
248
|
+
continueOnError: args.continueOnError === true,
|
|
249
|
+
keepTabs: args.keepTabs === true,
|
|
250
|
+
timeoutMs: args.timeoutMs ?? DEFAULT_TAB_TIMEOUT_MS,
|
|
251
|
+
};
|
|
252
|
+
const concurrency = Math.min(args.concurrency ?? tabs.length, tabs.length);
|
|
253
|
+
const images = [];
|
|
254
|
+
const results = new Array(tabs.length);
|
|
255
|
+
let next = 0;
|
|
256
|
+
const startedAt = Date.now();
|
|
257
|
+
await Promise.all(
|
|
258
|
+
Array.from({ length: concurrency }, async () => {
|
|
259
|
+
while (next < tabs.length) {
|
|
260
|
+
const tab = tabs[next];
|
|
261
|
+
next += 1;
|
|
262
|
+
results[tab.index] = await this.runTab(tab, options, images);
|
|
263
|
+
}
|
|
264
|
+
}),
|
|
265
|
+
);
|
|
266
|
+
|
|
267
|
+
return {
|
|
268
|
+
value: {
|
|
269
|
+
ok: results.every((result) => result.ok),
|
|
270
|
+
durationMs: Date.now() - startedAt,
|
|
271
|
+
tabs: results,
|
|
272
|
+
},
|
|
273
|
+
images,
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
export function runTabsTool(runner, stepTools, configuredFamily) {
|
|
279
|
+
return [
|
|
280
|
+
"run_tabs",
|
|
281
|
+
{
|
|
282
|
+
definition: {
|
|
283
|
+
name: "run_tabs",
|
|
284
|
+
description: `Run step lists in several tabs at the same time, in one call, and return every tab's results together. Use it to read or check several pages at once, for example to open search results side by side: {"tabs":[{"url":"https://example.com/a","steps":[{"tool":"read_text","arguments":{"maxChars":4000}}]},{"url":"https://example.com/b","steps":[{"tool":"read_text","arguments":{"maxChars":4000}}]}]}. A tab with url opens a new tab, loads the URL (waitUntil, default complete), and runs its steps; a tab with sessionId runs its steps in that attached tab. Steps are the same as in run_steps (including if and repeat) and are all validated before any tab opens. All tabs run at once by default (at most ${MAX_TABS}; concurrency lowers that); each has timeoutMs (default ${DEFAULT_TAB_TIMEOUT_MS}) and fails on its own without stopping the others, with page controls reported as in run_steps. Tabs opened for a url are closed afterwards unless keepTabs is true.`,
|
|
285
|
+
inputSchema: runTabsInputSchema(stepTools, configuredFamily),
|
|
286
|
+
},
|
|
287
|
+
handler: async (args) => runner.runTabs(args, configuredFamily),
|
|
288
|
+
formatResult: asImageToolResult,
|
|
289
|
+
},
|
|
290
|
+
];
|
|
291
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { mkdir, mkdtemp, rename, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
|
|
6
|
+
export function screenshotFormatsFor(browserFamily) {
|
|
7
|
+
return browserFamily === "firefox"
|
|
8
|
+
? ["png", "jpeg"]
|
|
9
|
+
: ["png", "jpeg", "webp"];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const SCREENSHOT_FILE_FORMATS = {
|
|
13
|
+
".png": "png",
|
|
14
|
+
".jpg": "jpeg",
|
|
15
|
+
".jpeg": "jpeg",
|
|
16
|
+
".webp": "webp",
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export function screenshotFileExtensions(browserFamily) {
|
|
20
|
+
const allowed = screenshotFormatsFor(browserFamily);
|
|
21
|
+
return Object.entries(SCREENSHOT_FILE_FORMATS)
|
|
22
|
+
.filter(([, format]) => allowed.includes(format))
|
|
23
|
+
.map(([extension]) => extension);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Only image extensions are accepted, so a screenshot path can never replace
|
|
27
|
+
// a config file or script.
|
|
28
|
+
export function screenshotTarget(args, browserFamily) {
|
|
29
|
+
const output = args.output ?? (args.path === undefined ? "data" : "file");
|
|
30
|
+
if (output !== "file") {
|
|
31
|
+
if (args.path !== undefined || args.overwrite !== undefined) {
|
|
32
|
+
throw new Error(
|
|
33
|
+
`take_screenshot path and overwrite require output file, not ${output}`,
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
return { output, format: args.format ?? "png", filePath: null };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (args.path === undefined) {
|
|
40
|
+
return { output, format: args.format ?? "png", filePath: null };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (!path.isAbsolute(args.path)) {
|
|
44
|
+
throw new Error("take_screenshot path must be absolute");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const format = SCREENSHOT_FILE_FORMATS[path.extname(args.path).toLowerCase()];
|
|
48
|
+
if (!format || !screenshotFormatsFor(browserFamily).includes(format)) {
|
|
49
|
+
throw new Error(
|
|
50
|
+
`take_screenshot path must end with ${screenshotFileExtensions(browserFamily).join(", ")}`,
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (args.format !== undefined && args.format !== format) {
|
|
55
|
+
throw new Error(
|
|
56
|
+
`take_screenshot format ${args.format} does not match the ${format} path extension`,
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return { output, format, filePath: args.path };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Screenshots can show signed-in pages, so files are private to the user.
|
|
64
|
+
export const SCREENSHOT_FILE_MODE = 0o600;
|
|
65
|
+
|
|
66
|
+
export async function writeTempScreenshot(image, format) {
|
|
67
|
+
// mkdtemp makes a fresh 0700 directory, which other users on a shared /tmp
|
|
68
|
+
// cannot pre-create or read.
|
|
69
|
+
const dir = await mkdtemp(path.join(tmpdir(), "mcp-browser-dev-tools-"));
|
|
70
|
+
const filePath = path.join(
|
|
71
|
+
dir,
|
|
72
|
+
`screenshot.${format === "jpeg" ? "jpg" : format}`,
|
|
73
|
+
);
|
|
74
|
+
await writeFile(filePath, image, { flag: "wx", mode: SCREENSHOT_FILE_MODE });
|
|
75
|
+
return filePath;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export async function writeScreenshotFile(filePath, image, overwrite) {
|
|
79
|
+
await mkdir(path.dirname(filePath), { recursive: true });
|
|
80
|
+
if (!overwrite) {
|
|
81
|
+
// O_EXCL also refuses an existing symlink instead of following it.
|
|
82
|
+
try {
|
|
83
|
+
await writeFile(filePath, image, {
|
|
84
|
+
flag: "wx",
|
|
85
|
+
mode: SCREENSHOT_FILE_MODE,
|
|
86
|
+
});
|
|
87
|
+
} catch (error) {
|
|
88
|
+
if (error.code === "EEXIST") {
|
|
89
|
+
throw new Error(
|
|
90
|
+
`${filePath} already exists; pass overwrite: true to replace it`,
|
|
91
|
+
{ cause: error },
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
return filePath;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Renaming a new file into place replaces a symlink at filePath rather
|
|
100
|
+
// than writing through it to a file without an image extension.
|
|
101
|
+
const tempPath = `${filePath}.${randomUUID()}.tmp`;
|
|
102
|
+
try {
|
|
103
|
+
await writeFile(tempPath, image, {
|
|
104
|
+
flag: "wx",
|
|
105
|
+
mode: SCREENSHOT_FILE_MODE,
|
|
106
|
+
});
|
|
107
|
+
await rename(tempPath, filePath);
|
|
108
|
+
} catch (error) {
|
|
109
|
+
await rm(tempPath, { force: true });
|
|
110
|
+
throw error;
|
|
111
|
+
}
|
|
112
|
+
return filePath;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export async function takeScreenshot(server, args) {
|
|
116
|
+
const { output, format, filePath } = screenshotTarget(
|
|
117
|
+
args,
|
|
118
|
+
server.config.browserFamily,
|
|
119
|
+
);
|
|
120
|
+
const screenshot = await server.browserAdapter.takeScreenshot(
|
|
121
|
+
args.sessionId,
|
|
122
|
+
format,
|
|
123
|
+
{
|
|
124
|
+
selector: args.selector,
|
|
125
|
+
},
|
|
126
|
+
);
|
|
127
|
+
if (output !== "file" || screenshot?.found === false) {
|
|
128
|
+
return screenshot;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (typeof screenshot?.data !== "string") {
|
|
132
|
+
throw new Error("The browser returned no screenshot data");
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const { data, ...metadata } = screenshot;
|
|
136
|
+
const image = Buffer.from(data, "base64");
|
|
137
|
+
const savedPath = filePath
|
|
138
|
+
? await writeScreenshotFile(filePath, image, args.overwrite)
|
|
139
|
+
: await writeTempScreenshot(image, format);
|
|
140
|
+
return { ...metadata, path: savedPath };
|
|
141
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export function asToolResult(value) {
|
|
2
|
+
return {
|
|
3
|
+
content: [
|
|
4
|
+
{
|
|
5
|
+
type: "text",
|
|
6
|
+
text: JSON.stringify(value, null, 2),
|
|
7
|
+
},
|
|
8
|
+
],
|
|
9
|
+
structuredContent: value,
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function asImageToolResult({ value, images }) {
|
|
14
|
+
return {
|
|
15
|
+
content: [
|
|
16
|
+
{
|
|
17
|
+
type: "text",
|
|
18
|
+
text: JSON.stringify(value, null, 2),
|
|
19
|
+
},
|
|
20
|
+
...images,
|
|
21
|
+
],
|
|
22
|
+
structuredContent: value,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function moveScreenshotImage(screenshot, images) {
|
|
27
|
+
if (typeof screenshot?.data !== "string") {
|
|
28
|
+
return screenshot;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const { data, ...metadata } = screenshot;
|
|
32
|
+
images.push({ type: "image", data, mimeType: screenshot.mimeType });
|
|
33
|
+
return { ...metadata, image: images.length };
|
|
34
|
+
}
|