@imge/cli 0.0.1 → 0.1.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/LICENSE +21 -0
- package/README.md +80 -1
- package/assets/demo.gif +0 -0
- package/dist/cli.js +697 -0
- package/package.json +42 -15
- package/ui/dist/assets/geist-mono-BoRIzn25.woff2 +0 -0
- package/ui/dist/assets/geist-sans-BNK-hTUD.woff2 +0 -0
- package/ui/dist/assets/index-Ieep9RH_.css +1 -0
- package/ui/dist/assets/index-qo8YHnNN.js +10 -0
- package/ui/dist/index.html +13 -0
- package/ui/src/FONT-LICENSE.txt +92 -0
- package/index.js +0 -2
package/dist/cli.js
ADDED
|
@@ -0,0 +1,697 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/proxy.ts
|
|
4
|
+
import { createRequire } from "node:module";
|
|
5
|
+
import { dirname, join as join4 } from "node:path";
|
|
6
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
7
|
+
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
8
|
+
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
9
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
10
|
+
import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js";
|
|
11
|
+
|
|
12
|
+
// src/browser.ts
|
|
13
|
+
import { createServer } from "node:net";
|
|
14
|
+
import { mkdtemp, rm } from "node:fs/promises";
|
|
15
|
+
import { tmpdir } from "node:os";
|
|
16
|
+
import { join as join2 } from "node:path";
|
|
17
|
+
import { chromium } from "playwright";
|
|
18
|
+
import sharp from "sharp";
|
|
19
|
+
|
|
20
|
+
// src/cdp-extension.ts
|
|
21
|
+
function connectPassiveCdpListeners(_browser) {}
|
|
22
|
+
|
|
23
|
+
// src/security.ts
|
|
24
|
+
var SENSITIVE_NAME = /pass(?:word|code|wd)?|card[ _-]?(?:number|num|no|expiry|exp|cvc|cvv)|credit[ _-]?card|cc[ _-]?(?:number|num|exp|csc|cvc)|cvv|cvc|security[ _-]?code|expir(?:y|ation)|exp[ _-]?(?:date|month|year)|mm[ _/-]?yy/i;
|
|
25
|
+
function isSensitiveField(field) {
|
|
26
|
+
if (field.type?.toLowerCase() === "password")
|
|
27
|
+
return true;
|
|
28
|
+
if (/^cc(?:-|$)/i.test(field.autocomplete || ""))
|
|
29
|
+
return true;
|
|
30
|
+
return [field.name, field.id, field.placeholder, field.ariaLabel].some((value) => SENSITIVE_NAME.test(value || ""));
|
|
31
|
+
}
|
|
32
|
+
function redactText(value, secrets) {
|
|
33
|
+
let text = value;
|
|
34
|
+
for (const secret of [...new Set(secrets)].sort((a, b) => b.length - a.length)) {
|
|
35
|
+
if (secret)
|
|
36
|
+
text = text.split(secret).join("••••");
|
|
37
|
+
}
|
|
38
|
+
return text.split(`
|
|
39
|
+
`).map((line) => {
|
|
40
|
+
if (/^\s*-\s+(?:textbox|input|spinbutton)\b/i.test(line) && SENSITIVE_NAME.test(line)) {
|
|
41
|
+
return line.replace(/(:\s*)([^\[]+?)(\s*(?:\[|$))/, (_match, before, _value, after) => `${before}••••${after}`);
|
|
42
|
+
}
|
|
43
|
+
return line;
|
|
44
|
+
}).join(`
|
|
45
|
+
`);
|
|
46
|
+
}
|
|
47
|
+
function sanitizeArguments(toolName, args, sensitiveTargets) {
|
|
48
|
+
const safe = structuredClone(args);
|
|
49
|
+
const secrets = [];
|
|
50
|
+
if (toolName === "browser_type" && typeof safe.text === "string" && sensitiveTargets.has(String(safe.ref || safe.target))) {
|
|
51
|
+
secrets.push(safe.text);
|
|
52
|
+
safe.text = "••••";
|
|
53
|
+
}
|
|
54
|
+
if (toolName === "browser_fill_form" && Array.isArray(safe.fields)) {
|
|
55
|
+
for (const field of safe.fields) {
|
|
56
|
+
if (!field || typeof field !== "object")
|
|
57
|
+
continue;
|
|
58
|
+
const descriptor = { name: String(field.name || ""), type: String(field.type || ""), id: String(field.id || ""), placeholder: String(field.placeholder || "") };
|
|
59
|
+
if (isSensitiveField(descriptor) || sensitiveTargets.has(String(field.target || field.ref || ""))) {
|
|
60
|
+
if (typeof field.value === "string") {
|
|
61
|
+
secrets.push(field.value);
|
|
62
|
+
field.value = "••••";
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return { arguments: safe, secrets };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// src/config.ts
|
|
71
|
+
import { readFileSync } from "node:fs";
|
|
72
|
+
import { homedir } from "node:os";
|
|
73
|
+
import { join } from "node:path";
|
|
74
|
+
var manifest = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
75
|
+
var PRODUCT_NAME = "imge";
|
|
76
|
+
var VERSION = manifest.version;
|
|
77
|
+
var PLAYER_PORT = 4747;
|
|
78
|
+
var ENV_PREFIX = PRODUCT_NAME.replace(/-/g, "_").toUpperCase();
|
|
79
|
+
var DATA_HOME = process.env[`${ENV_PREFIX}_HOME`] || join(homedir(), `.${PRODUCT_NAME}`, "mcp");
|
|
80
|
+
var SESSIONS_DIR = join(DATA_HOME, "sessions");
|
|
81
|
+
|
|
82
|
+
// src/browser.ts
|
|
83
|
+
async function freePort() {
|
|
84
|
+
const server = createServer();
|
|
85
|
+
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
|
|
86
|
+
const address = server.address();
|
|
87
|
+
if (!address || typeof address === "string")
|
|
88
|
+
throw new Error("No free CDP port");
|
|
89
|
+
const port = address.port;
|
|
90
|
+
await new Promise((resolve) => server.close(() => resolve()));
|
|
91
|
+
return port;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
class BrowserBridge {
|
|
95
|
+
owner;
|
|
96
|
+
observer;
|
|
97
|
+
profileDir;
|
|
98
|
+
endpoint;
|
|
99
|
+
constructor(owner, observer, profileDir, endpoint) {
|
|
100
|
+
this.owner = owner;
|
|
101
|
+
this.observer = observer;
|
|
102
|
+
this.profileDir = profileDir;
|
|
103
|
+
this.endpoint = endpoint;
|
|
104
|
+
}
|
|
105
|
+
static async start() {
|
|
106
|
+
const profileDir = await mkdtemp(join2(tmpdir(), `${PRODUCT_NAME}-browser-`));
|
|
107
|
+
const port = await freePort();
|
|
108
|
+
const endpoint = `http://127.0.0.1:${port}`;
|
|
109
|
+
const owner = await chromium.launchPersistentContext(profileDir, {
|
|
110
|
+
channel: process.env[`${ENV_PREFIX}_BROWSER_CHANNEL`] || "chrome",
|
|
111
|
+
headless: true,
|
|
112
|
+
args: [`--remote-debugging-port=${port}`]
|
|
113
|
+
});
|
|
114
|
+
try {
|
|
115
|
+
for (let attempt = 0;attempt < 50; attempt++) {
|
|
116
|
+
try {
|
|
117
|
+
if ((await fetch(`${endpoint}/json/version`, { signal: AbortSignal.timeout(300) })).ok)
|
|
118
|
+
break;
|
|
119
|
+
} catch {
|
|
120
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
const observer = await chromium.connectOverCDP(endpoint);
|
|
124
|
+
connectPassiveCdpListeners(observer);
|
|
125
|
+
return new BrowserBridge(owner, observer, profileDir, endpoint);
|
|
126
|
+
} catch (error) {
|
|
127
|
+
await owner.close();
|
|
128
|
+
await rm(profileDir, { recursive: true, force: true });
|
|
129
|
+
throw error;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
page() {
|
|
133
|
+
const pages = this.observer.contexts().flatMap((context) => context.pages()).filter((page) => !page.isClosed());
|
|
134
|
+
return [...pages].reverse().find((page) => page.url() !== "about:blank") || pages.at(-1) || null;
|
|
135
|
+
}
|
|
136
|
+
locator(page, raw, target, occurrence) {
|
|
137
|
+
if (target?.role)
|
|
138
|
+
return page.getByRole(target.role, { name: target.name || undefined, exact: true }).nth(Math.max(0, occurrence));
|
|
139
|
+
if (/^[\w-]*e\d+$/.test(raw))
|
|
140
|
+
return null;
|
|
141
|
+
const roleMatch = /^getByRole\(['"]([^'"]+)['"],\s*\{\s*name:\s*['"]([^'"]+)['"]/.exec(raw);
|
|
142
|
+
if (roleMatch)
|
|
143
|
+
return page.getByRole(roleMatch[1], { name: roleMatch[2], exact: true });
|
|
144
|
+
try {
|
|
145
|
+
return page.locator(raw).first();
|
|
146
|
+
} catch {
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
async inspectTarget(raw, target, occurrence = 0) {
|
|
151
|
+
const page = this.page();
|
|
152
|
+
if (!page)
|
|
153
|
+
return { clickPoint: null, sensitive: false, field: null, target };
|
|
154
|
+
const locator = this.locator(page, raw, target, occurrence);
|
|
155
|
+
if (!locator)
|
|
156
|
+
return { clickPoint: null, sensitive: false, field: null, target };
|
|
157
|
+
try {
|
|
158
|
+
const [box, info] = await Promise.all([
|
|
159
|
+
locator.boundingBox({ timeout: 1000 }),
|
|
160
|
+
locator.evaluate((element) => ({
|
|
161
|
+
type: element.getAttribute("type"),
|
|
162
|
+
autocomplete: element.getAttribute("autocomplete"),
|
|
163
|
+
name: element.getAttribute("name"),
|
|
164
|
+
id: element.id,
|
|
165
|
+
placeholder: element.getAttribute("placeholder"),
|
|
166
|
+
ariaLabel: element.getAttribute("aria-label"),
|
|
167
|
+
tag: element.tagName.toLowerCase(),
|
|
168
|
+
text: (element.textContent || "").trim().slice(0, 100)
|
|
169
|
+
}), { timeout: 1000 })
|
|
170
|
+
]);
|
|
171
|
+
const point = box ? { x: box.x + box.width / 2, y: box.y + box.height / 2, width: 0, height: 0 } : null;
|
|
172
|
+
const inferredRole = info.tag === "button" ? "button" : info.tag === "a" ? "link" : ["input", "textarea"].includes(info.tag) ? "textbox" : info.tag;
|
|
173
|
+
const resolvedTarget = target?.role ? target : { ref: target?.ref || null, role: inferredRole, name: info.ariaLabel || info.text || info.placeholder || info.name || "", matchCount: null };
|
|
174
|
+
return { clickPoint: point, sensitive: isSensitiveField(info), field: info, target: resolvedTarget };
|
|
175
|
+
} catch {
|
|
176
|
+
return { clickPoint: null, sensitive: false, field: null, target };
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
async capture() {
|
|
180
|
+
const page = this.page();
|
|
181
|
+
if (!page)
|
|
182
|
+
return { png: null, secrets: [], width: 0, height: 0 };
|
|
183
|
+
try {
|
|
184
|
+
const png = await page.screenshot({ type: "png", animations: "disabled", timeout: 5000 });
|
|
185
|
+
const metadata = await sharp(png).metadata();
|
|
186
|
+
const width = metadata.width || 0, height = metadata.height || 0;
|
|
187
|
+
const secrets = [], overlays = [];
|
|
188
|
+
for (const frame of page.frames()) {
|
|
189
|
+
for (const input of await frame.locator("input, textarea, select").all()) {
|
|
190
|
+
try {
|
|
191
|
+
const info = await input.evaluate((element) => ({
|
|
192
|
+
type: element.getAttribute("type"),
|
|
193
|
+
autocomplete: element.getAttribute("autocomplete"),
|
|
194
|
+
name: element.getAttribute("name"),
|
|
195
|
+
id: element.id,
|
|
196
|
+
placeholder: element.getAttribute("placeholder"),
|
|
197
|
+
ariaLabel: element.getAttribute("aria-label"),
|
|
198
|
+
value: element.value || ""
|
|
199
|
+
}));
|
|
200
|
+
if (!isSensitiveField(info))
|
|
201
|
+
continue;
|
|
202
|
+
if (info.value)
|
|
203
|
+
secrets.push(info.value);
|
|
204
|
+
const box = await input.boundingBox();
|
|
205
|
+
if (!box || !width || !height)
|
|
206
|
+
continue;
|
|
207
|
+
const left = Math.max(0, Math.floor(box.x));
|
|
208
|
+
const top = Math.max(0, Math.floor(box.y));
|
|
209
|
+
const right = Math.min(width, Math.ceil(box.x + box.width));
|
|
210
|
+
const bottom = Math.min(height, Math.ceil(box.y + box.height));
|
|
211
|
+
if (right > left && bottom > top)
|
|
212
|
+
overlays.push({
|
|
213
|
+
input: { create: { width: right - left, height: bottom - top, channels: 4, background: "#111111" } },
|
|
214
|
+
left,
|
|
215
|
+
top
|
|
216
|
+
});
|
|
217
|
+
} catch {}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return { png: overlays.length ? await sharp(png).composite(overlays).png().toBuffer() : png, secrets, width, height };
|
|
221
|
+
} catch {
|
|
222
|
+
return { png: null, secrets: [], width: 0, height: 0 };
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
async close() {
|
|
226
|
+
await this.observer.close().catch(() => {});
|
|
227
|
+
await this.owner.close().catch(() => {});
|
|
228
|
+
await rm(this.profileDir, { recursive: true, force: true }).catch(() => {});
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// src/storage.ts
|
|
233
|
+
import { appendFile, mkdir, readFile, readdir, writeFile } from "node:fs/promises";
|
|
234
|
+
import { join as join3 } from "node:path";
|
|
235
|
+
import { randomUUID } from "node:crypto";
|
|
236
|
+
function newSessionId() {
|
|
237
|
+
return `${new Date().toISOString().replace(/[:.]/g, "-")}-${randomUUID().slice(0, 8)}`;
|
|
238
|
+
}
|
|
239
|
+
function sessionPath(id) {
|
|
240
|
+
if (!/^[\w-]+$/.test(id))
|
|
241
|
+
throw new Error("Invalid session ID");
|
|
242
|
+
return join3(SESSIONS_DIR, id);
|
|
243
|
+
}
|
|
244
|
+
async function saveStep(id, step, png) {
|
|
245
|
+
const directory = sessionPath(id);
|
|
246
|
+
await mkdir(directory, { recursive: true, mode: 448 });
|
|
247
|
+
if (png && step.screenshot)
|
|
248
|
+
await writeFile(join3(directory, step.screenshot), png, { mode: 384 });
|
|
249
|
+
await appendFile(join3(directory, "steps.jsonl"), `${JSON.stringify(step)}
|
|
250
|
+
`, { mode: 384 });
|
|
251
|
+
}
|
|
252
|
+
async function readSteps(id) {
|
|
253
|
+
const contents = await readFile(join3(sessionPath(id), "steps.jsonl"), "utf8");
|
|
254
|
+
return contents.split(`
|
|
255
|
+
`).filter(Boolean).map((line) => JSON.parse(line));
|
|
256
|
+
}
|
|
257
|
+
async function listSessions() {
|
|
258
|
+
const entries = await readdir(SESSIONS_DIR, { withFileTypes: true }).catch(() => []);
|
|
259
|
+
const items = await Promise.all(entries.filter((entry) => entry.isDirectory()).map(async (entry) => {
|
|
260
|
+
try {
|
|
261
|
+
const steps = await readSteps(entry.name);
|
|
262
|
+
if (!steps.length)
|
|
263
|
+
return null;
|
|
264
|
+
const first = steps[0], last = steps.at(-1);
|
|
265
|
+
return {
|
|
266
|
+
id: entry.name,
|
|
267
|
+
task: first.task || first.firstUrl || "",
|
|
268
|
+
agent: first.agent || "Agent",
|
|
269
|
+
startedAt: first.startedAt,
|
|
270
|
+
durationMs: new Date(last.endedAt).getTime() - new Date(first.startedAt).getTime(),
|
|
271
|
+
stepCount: steps.length,
|
|
272
|
+
failed: steps.some((step) => Boolean(step.error))
|
|
273
|
+
};
|
|
274
|
+
} catch {
|
|
275
|
+
return null;
|
|
276
|
+
}
|
|
277
|
+
}));
|
|
278
|
+
return items.filter((item) => item !== null).sort((a, b) => b.startedAt.localeCompare(a.startedAt));
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// src/snapshot.ts
|
|
282
|
+
var NODE = /^\s*-\s+(?:<changed>\s+)?([\w-]+)(?:\s+"((?:\\.|[^"])*)")?[^\n]*?\[ref=([^\]]+)\]/gm;
|
|
283
|
+
function extractAgentView(resultText) {
|
|
284
|
+
const marker = /^- Page Snapshot:|^### (?:Page )?Snapshot\s*$/m.exec(resultText);
|
|
285
|
+
if (marker) {
|
|
286
|
+
const tail = resultText.slice(marker.index + marker[0].length).trimStart();
|
|
287
|
+
return tail.split(/^### [^\n]+$/m)[0].replace(/^```(?:yaml)?\s*\n?/, "").replace(/\n?```\s*$/, "").trimEnd();
|
|
288
|
+
}
|
|
289
|
+
const firstNode = resultText.search(/^\s*-\s+[\w-]+(?:\s+"[^"]*")?.*\[ref=/m);
|
|
290
|
+
return firstNode >= 0 ? resultText.slice(firstNode).trimEnd() : "";
|
|
291
|
+
}
|
|
292
|
+
function snapshotNodes(snapshot) {
|
|
293
|
+
return [...snapshot.matchAll(NODE)].map((match) => ({ role: match[1], name: (match[2] || "").replace(/\\"/g, '"'), ref: match[3], line: match[0].trim() }));
|
|
294
|
+
}
|
|
295
|
+
function mergeSnapshotState(previous, next) {
|
|
296
|
+
if (!next.includes("<changed>") && !next.includes("[unchanged]"))
|
|
297
|
+
return next;
|
|
298
|
+
const nodes = new Map(snapshotNodes(previous).map((node) => [node.ref, node.line]));
|
|
299
|
+
for (const node of snapshotNodes(next))
|
|
300
|
+
nodes.set(node.ref, node.line);
|
|
301
|
+
return [...nodes.values()].join(`
|
|
302
|
+
`);
|
|
303
|
+
}
|
|
304
|
+
function targetFromSnapshot(args, priorSnapshot) {
|
|
305
|
+
const supplied = args.ref || args.target;
|
|
306
|
+
const raw = typeof supplied === "string" ? supplied.replace(/^ref=/, "") : null;
|
|
307
|
+
if (!raw)
|
|
308
|
+
return { target: null, warnings: [], occurrence: 0 };
|
|
309
|
+
const nodes = snapshotNodes(priorSnapshot);
|
|
310
|
+
const found = nodes.find((node) => node.ref === raw);
|
|
311
|
+
if (!found)
|
|
312
|
+
return { target: { ref: raw, role: null, name: null, matchCount: null }, warnings: [], occurrence: 0 };
|
|
313
|
+
const matches = nodes.filter((node) => node.role === found.role && node.name === found.name);
|
|
314
|
+
const warnings = [];
|
|
315
|
+
if (matches.length > 1)
|
|
316
|
+
warnings.push("same-name");
|
|
317
|
+
if (!found.name)
|
|
318
|
+
warnings.push("unnamed");
|
|
319
|
+
return { target: { ref: raw, role: found.role, name: found.name, matchCount: matches.length }, warnings, occurrence: matches.findIndex((node) => node.ref === raw) };
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// src/proxy.ts
|
|
323
|
+
var require2 = createRequire(import.meta.url);
|
|
324
|
+
var playwrightCli = join4(dirname(require2.resolve("@playwright/mcp/package.json")), "cli.js");
|
|
325
|
+
var OBSERVATION_TOOLS = new Set(["browser_snapshot", "browser_find", "browser_take_screenshot", "browser_console_messages", "browser_network_requests", "browser_network_request", "browser_close", "browser_install"]);
|
|
326
|
+
var isActionTool = (name) => name.startsWith("browser_") && !OBSERVATION_TOOLS.has(name);
|
|
327
|
+
function responseText(result) {
|
|
328
|
+
if (!result || typeof result !== "object" || !("content" in result) || !Array.isArray(result.content))
|
|
329
|
+
return "";
|
|
330
|
+
return result.content.filter((item) => item && typeof item === "object" && ("type" in item) && item.type === "text").map((item) => item.text || "").join(`
|
|
331
|
+
`);
|
|
332
|
+
}
|
|
333
|
+
function shortFailure(text) {
|
|
334
|
+
const message = /"message"\s*:\s*"([^"\n]+)/.exec(text)?.[1];
|
|
335
|
+
if (message)
|
|
336
|
+
return message.slice(0, 180);
|
|
337
|
+
return (text.split(`
|
|
338
|
+
`).map((line) => line.trim()).find((line) => line && !line.startsWith("#") && /error|failed|timeout|not found/i.test(line)) || "Action failed").slice(0, 180);
|
|
339
|
+
}
|
|
340
|
+
async function serveProxy(extraArgs = []) {
|
|
341
|
+
const bridge = await BrowserBridge.start();
|
|
342
|
+
const upstream = new Client({ name: PRODUCT_NAME, version: VERSION });
|
|
343
|
+
const upstreamTransport = new StdioClientTransport({ command: process.execPath, args: [playwrightCli, `--cdp-endpoint=${bridge.endpoint}`, ...extraArgs], stderr: "inherit" });
|
|
344
|
+
try {
|
|
345
|
+
await upstream.connect(upstreamTransport);
|
|
346
|
+
} catch (error) {
|
|
347
|
+
await bridge.close();
|
|
348
|
+
throw error;
|
|
349
|
+
}
|
|
350
|
+
const downstream = new Server({ name: PRODUCT_NAME, version: VERSION }, { capabilities: { tools: { listChanged: true } } });
|
|
351
|
+
const sessionId = newSessionId();
|
|
352
|
+
let index = 0, lastSnapshot = "", firstUrl = null;
|
|
353
|
+
let pending = Promise.resolve();
|
|
354
|
+
downstream.setRequestHandler(ListToolsRequestSchema, async () => upstream.listTools());
|
|
355
|
+
downstream.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
356
|
+
const operation = pending.then(async () => {
|
|
357
|
+
const { name, arguments: rawArguments } = request.params;
|
|
358
|
+
const args = rawArguments || {};
|
|
359
|
+
if (!isActionTool(name)) {
|
|
360
|
+
const result2 = await upstream.callTool(request.params);
|
|
361
|
+
const view = extractAgentView(responseText(result2));
|
|
362
|
+
if (view)
|
|
363
|
+
lastSnapshot = mergeSnapshotState(lastSnapshot, view);
|
|
364
|
+
return result2;
|
|
365
|
+
}
|
|
366
|
+
const started = Date.now(), startedAt = new Date(started).toISOString();
|
|
367
|
+
const rawTarget = typeof (args.ref || args.target) === "string" ? String(args.ref || args.target) : "";
|
|
368
|
+
const parsed = targetFromSnapshot(args, lastSnapshot);
|
|
369
|
+
let target = parsed.target;
|
|
370
|
+
const warnings = [...parsed.warnings];
|
|
371
|
+
let clickPoint = null;
|
|
372
|
+
const sensitiveTargets = new Set;
|
|
373
|
+
if (rawTarget) {
|
|
374
|
+
const inspected = await bridge.inspectTarget(rawTarget, target, parsed.occurrence);
|
|
375
|
+
target = inspected.target;
|
|
376
|
+
if (inspected.sensitive || isSensitiveField({ name: target?.name }))
|
|
377
|
+
sensitiveTargets.add(rawTarget);
|
|
378
|
+
if (name === "browser_click")
|
|
379
|
+
clickPoint = inspected.clickPoint;
|
|
380
|
+
if (target?.name === "" && !warnings.includes("unnamed"))
|
|
381
|
+
warnings.push("unnamed");
|
|
382
|
+
}
|
|
383
|
+
if (name === "browser_fill_form" && Array.isArray(args.fields)) {
|
|
384
|
+
for (const field of args.fields) {
|
|
385
|
+
const ref = String(field.target || field.ref || "");
|
|
386
|
+
if (!ref)
|
|
387
|
+
continue;
|
|
388
|
+
const found = targetFromSnapshot({ ref }, lastSnapshot);
|
|
389
|
+
const inspected = await bridge.inspectTarget(ref, found.target, found.occurrence);
|
|
390
|
+
if (inspected.sensitive || isSensitiveField({ name: String(field.name || "") }))
|
|
391
|
+
sensitiveTargets.add(ref);
|
|
392
|
+
if (!target)
|
|
393
|
+
target = inspected.target;
|
|
394
|
+
for (const warning of found.warnings)
|
|
395
|
+
if (!warnings.includes(warning))
|
|
396
|
+
warnings.push(warning);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
const sanitized = sanitizeArguments(name, args, sensitiveTargets);
|
|
400
|
+
let result = null;
|
|
401
|
+
let thrown = null;
|
|
402
|
+
try {
|
|
403
|
+
result = await upstream.callTool(request.params);
|
|
404
|
+
} catch (error2) {
|
|
405
|
+
thrown = error2;
|
|
406
|
+
}
|
|
407
|
+
const captured = await bridge.capture();
|
|
408
|
+
if (clickPoint) {
|
|
409
|
+
clickPoint.width = captured.width;
|
|
410
|
+
clickPoint.height = captured.height;
|
|
411
|
+
}
|
|
412
|
+
const ended = Date.now();
|
|
413
|
+
const rawText = result ? responseText(result) : String(thrown);
|
|
414
|
+
const secrets = [...sanitized.secrets, ...captured.secrets];
|
|
415
|
+
const storedText = redactText(rawText, secrets);
|
|
416
|
+
const rawView = result ? extractAgentView(rawText) : "";
|
|
417
|
+
if (rawView)
|
|
418
|
+
lastSnapshot = mergeSnapshotState(lastSnapshot, rawView);
|
|
419
|
+
const error = result?.isError || thrown ? storedText : null;
|
|
420
|
+
if (name === "browser_navigate" && typeof args.url === "string" && !firstUrl)
|
|
421
|
+
firstUrl = args.url;
|
|
422
|
+
const step = {
|
|
423
|
+
index: ++index,
|
|
424
|
+
toolName: name,
|
|
425
|
+
arguments: sanitized.arguments,
|
|
426
|
+
resultText: storedText,
|
|
427
|
+
agentView: redactText(rawView, secrets),
|
|
428
|
+
error,
|
|
429
|
+
failureReason: error ? shortFailure(error) : null,
|
|
430
|
+
startedAt,
|
|
431
|
+
endedAt: new Date(ended).toISOString(),
|
|
432
|
+
durationMs: ended - started,
|
|
433
|
+
screenshot: captured.png ? `${String(index).padStart(4, "0")}.png` : null,
|
|
434
|
+
target,
|
|
435
|
+
clickPoint,
|
|
436
|
+
warnings,
|
|
437
|
+
agent: downstream.getClientVersion()?.name || null,
|
|
438
|
+
task: process.env[`${ENV_PREFIX}_TASK`] || null,
|
|
439
|
+
firstUrl,
|
|
440
|
+
trackingHits: []
|
|
441
|
+
};
|
|
442
|
+
try {
|
|
443
|
+
await saveStep(sessionId, step, captured.png);
|
|
444
|
+
} catch (error2) {
|
|
445
|
+
console.error(`${PRODUCT_NAME}: could not save step: ${error2 instanceof Error ? error2.message : String(error2)}`);
|
|
446
|
+
}
|
|
447
|
+
if (thrown)
|
|
448
|
+
throw thrown;
|
|
449
|
+
return result;
|
|
450
|
+
});
|
|
451
|
+
pending = operation.catch(() => {
|
|
452
|
+
return;
|
|
453
|
+
});
|
|
454
|
+
return operation;
|
|
455
|
+
});
|
|
456
|
+
const close = async () => {
|
|
457
|
+
await upstream.close().catch(() => {});
|
|
458
|
+
await bridge.close();
|
|
459
|
+
};
|
|
460
|
+
process.once("SIGINT", () => {
|
|
461
|
+
close().then(() => process.exit(0));
|
|
462
|
+
});
|
|
463
|
+
process.once("SIGTERM", () => {
|
|
464
|
+
close().then(() => process.exit(0));
|
|
465
|
+
});
|
|
466
|
+
await downstream.connect(new StdioServerTransport);
|
|
467
|
+
process.stdin.once("end", () => {
|
|
468
|
+
close();
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
// src/player.ts
|
|
473
|
+
import { readFile as readFile2, stat } from "node:fs/promises";
|
|
474
|
+
import { join as join5, resolve, extname } from "node:path";
|
|
475
|
+
import { spawn } from "node:child_process";
|
|
476
|
+
import { serve } from "@hono/node-server";
|
|
477
|
+
import { Hono } from "hono";
|
|
478
|
+
var app = new Hono;
|
|
479
|
+
var uiRoot = resolve(new URL("../ui/dist/", import.meta.url).pathname);
|
|
480
|
+
var mime = { ".html": "text/html; charset=utf-8", ".js": "text/javascript; charset=utf-8", ".css": "text/css; charset=utf-8", ".woff2": "font/woff2", ".svg": "image/svg+xml", ".png": "image/png", ".ico": "image/x-icon" };
|
|
481
|
+
app.get("/api/meta", (c) => c.json({ name: PRODUCT_NAME }));
|
|
482
|
+
app.get("/api/sessions", async (c) => c.json(await listSessions()));
|
|
483
|
+
app.get("/api/sessions/:id/steps", async (c) => {
|
|
484
|
+
try {
|
|
485
|
+
return c.json(await readSteps(c.req.param("id")));
|
|
486
|
+
} catch {
|
|
487
|
+
return c.json({ error: "Session not found" }, 404);
|
|
488
|
+
}
|
|
489
|
+
});
|
|
490
|
+
app.get("/api/sessions/:id/images/:file", async (c) => {
|
|
491
|
+
const file = c.req.param("file");
|
|
492
|
+
if (!/^\d{4,}\.png$/.test(file))
|
|
493
|
+
return c.notFound();
|
|
494
|
+
try {
|
|
495
|
+
return c.body(await readFile2(join5(sessionPath(c.req.param("id")), file)), 200, { "content-type": "image/png", "cache-control": "no-store" });
|
|
496
|
+
} catch {
|
|
497
|
+
return c.notFound();
|
|
498
|
+
}
|
|
499
|
+
});
|
|
500
|
+
app.get("*", async (c) => {
|
|
501
|
+
const requested = c.req.path === "/" ? "/index.html" : c.req.path;
|
|
502
|
+
const file = resolve(uiRoot, `.${requested}`);
|
|
503
|
+
if (!file.startsWith(`${uiRoot}/`) && file !== uiRoot)
|
|
504
|
+
return c.notFound();
|
|
505
|
+
try {
|
|
506
|
+
if (!(await stat(file)).isFile())
|
|
507
|
+
return c.notFound();
|
|
508
|
+
return c.body(await readFile2(file), 200, { "content-type": mime[extname(file)] || "application/octet-stream" });
|
|
509
|
+
} catch {
|
|
510
|
+
return c.notFound();
|
|
511
|
+
}
|
|
512
|
+
});
|
|
513
|
+
function openBrowser(url) {
|
|
514
|
+
if (process.env[`${ENV_PREFIX}_NO_OPEN`] === "1")
|
|
515
|
+
return;
|
|
516
|
+
const platform = process.platform;
|
|
517
|
+
const command = platform === "darwin" ? "open" : platform === "win32" ? "cmd" : "xdg-open";
|
|
518
|
+
const args = platform === "win32" ? ["/c", "start", "", url] : [url];
|
|
519
|
+
const child = spawn(command, args, { detached: true, stdio: "ignore" });
|
|
520
|
+
child.unref();
|
|
521
|
+
}
|
|
522
|
+
async function startPlayer() {
|
|
523
|
+
const url = `http://localhost:${PLAYER_PORT}`;
|
|
524
|
+
try {
|
|
525
|
+
const response = await fetch(`http://127.0.0.1:${PLAYER_PORT}/api/meta`, { signal: AbortSignal.timeout(300) });
|
|
526
|
+
if (response.ok && (await response.json()).name === PRODUCT_NAME) {
|
|
527
|
+
openBrowser(url);
|
|
528
|
+
console.log(url);
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
} catch {}
|
|
532
|
+
await new Promise((resolve2, reject) => {
|
|
533
|
+
const server = serve({ fetch: app.fetch, port: PLAYER_PORT, hostname: "127.0.0.1" }, () => resolve2());
|
|
534
|
+
server.once("error", reject);
|
|
535
|
+
});
|
|
536
|
+
openBrowser(url);
|
|
537
|
+
console.log(url);
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
// src/setup.ts
|
|
541
|
+
import { copyFile, mkdir as mkdir2, readFile as readFile3, readdir as readdir2, rm as rm2, writeFile as writeFile2 } from "node:fs/promises";
|
|
542
|
+
import { homedir as homedir2 } from "node:os";
|
|
543
|
+
import { dirname as dirname2, join as join6 } from "node:path";
|
|
544
|
+
import { createInterface } from "node:readline/promises";
|
|
545
|
+
import { stdin, stdout } from "node:process";
|
|
546
|
+
var launcher = { command: "npx", args: ["--yes", "@imge/cli@latest", "mcp"] };
|
|
547
|
+
var base = process.env[`${ENV_PREFIX}_CONFIG_HOME`] || homedir2();
|
|
548
|
+
var configs = [
|
|
549
|
+
{ id: "claude-code", label: "Claude Code", file: join6(base, ".claude.json"), kind: "json", key: "mcpServers" },
|
|
550
|
+
{ id: "claude-desktop", label: "Claude Desktop", file: process.platform === "darwin" ? join6(base, "Library/Application Support/Claude/claude_desktop_config.json") : process.platform === "win32" ? join6(base, "AppData/Roaming/Claude/claude_desktop_config.json") : join6(base, ".config/Claude/claude_desktop_config.json"), kind: "json", key: "mcpServers" },
|
|
551
|
+
{ id: "cursor", label: "Cursor", file: join6(base, ".cursor/mcp.json"), kind: "json", key: "mcpServers" },
|
|
552
|
+
{ id: "vs-code", label: "VS Code", file: join6(base, ".copilot/mcp-config.json"), kind: "json", key: "mcpServers" },
|
|
553
|
+
{ id: "codex", label: "Codex", file: join6(base, ".codex/config.toml"), kind: "toml" }
|
|
554
|
+
];
|
|
555
|
+
async function confirm(question) {
|
|
556
|
+
const rl = createInterface({ input: stdin, output: stdout });
|
|
557
|
+
try {
|
|
558
|
+
return /^y(?:es)?$/i.test((await rl.question(`${question} [y/N] `)).trim());
|
|
559
|
+
} finally {
|
|
560
|
+
rl.close();
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
function selected(args) {
|
|
564
|
+
const index = args.indexOf("--client");
|
|
565
|
+
const client = index < 0 ? "all" : args[index + 1];
|
|
566
|
+
if (!client || client !== "all" && !configs.some((config) => config.id === client))
|
|
567
|
+
throw new Error(`Unknown client: ${client}. Choose all, ${configs.map((config) => config.id).join(", ")}.`);
|
|
568
|
+
return client === "all" ? configs : configs.filter((config) => config.id === client);
|
|
569
|
+
}
|
|
570
|
+
function entry(config) {
|
|
571
|
+
return config.id === "vs-code" ? { type: "stdio", ...launcher, tools: ["*"] } : launcher;
|
|
572
|
+
}
|
|
573
|
+
function snippet(config) {
|
|
574
|
+
if (config.kind === "toml")
|
|
575
|
+
return `[mcp_servers.${PRODUCT_NAME}]
|
|
576
|
+
command = "npx"
|
|
577
|
+
args = ["--yes", "@imge/cli@latest", "mcp"]`;
|
|
578
|
+
return JSON.stringify({ [config.key]: { [PRODUCT_NAME]: entry(config) } }, null, 2);
|
|
579
|
+
}
|
|
580
|
+
function stamp() {
|
|
581
|
+
return new Date().toISOString().replace(/[:.]/g, "-");
|
|
582
|
+
}
|
|
583
|
+
async function save(config, original, updated) {
|
|
584
|
+
await mkdir2(dirname2(config.file), { recursive: true });
|
|
585
|
+
if (original !== null)
|
|
586
|
+
await copyFile(config.file, `${config.file}.bak-${stamp()}`);
|
|
587
|
+
await writeFile2(config.file, updated);
|
|
588
|
+
}
|
|
589
|
+
async function setup(args = []) {
|
|
590
|
+
const targets = selected(args);
|
|
591
|
+
for (const config of targets)
|
|
592
|
+
console.log(`
|
|
593
|
+
${config.label} · ${config.file}
|
|
594
|
+
${snippet(config)}
|
|
595
|
+
`);
|
|
596
|
+
if (!args.includes("--yes") && !await confirm(`Add imge to ${targets.map((config) => config.label).join(", ")}?`))
|
|
597
|
+
return;
|
|
598
|
+
for (const config of targets) {
|
|
599
|
+
const original = await readFile3(config.file, "utf8").catch(() => null);
|
|
600
|
+
if (config.kind === "toml") {
|
|
601
|
+
if (original && /^\[mcp_servers\.imge\]$/m.test(original)) {
|
|
602
|
+
console.log(`${config.label}: already configured`);
|
|
603
|
+
continue;
|
|
604
|
+
}
|
|
605
|
+
const separator = original ? original.endsWith(`
|
|
606
|
+
`) ? `
|
|
607
|
+
` : `
|
|
608
|
+
|
|
609
|
+
` : "";
|
|
610
|
+
await save(config, original, `${original || ""}${separator}${snippet(config)}
|
|
611
|
+
`);
|
|
612
|
+
} else {
|
|
613
|
+
let data;
|
|
614
|
+
try {
|
|
615
|
+
data = original ? JSON.parse(original) : {};
|
|
616
|
+
} catch {
|
|
617
|
+
console.error(`${config.label}: invalid JSON, skipped`);
|
|
618
|
+
continue;
|
|
619
|
+
}
|
|
620
|
+
const servers = data[config.key] && typeof data[config.key] === "object" && !Array.isArray(data[config.key]) ? data[config.key] : {};
|
|
621
|
+
if (PRODUCT_NAME in servers) {
|
|
622
|
+
console.log(`${config.label}: already configured`);
|
|
623
|
+
continue;
|
|
624
|
+
}
|
|
625
|
+
data[config.key] = { ...servers, [PRODUCT_NAME]: entry(config) };
|
|
626
|
+
await save(config, original, `${JSON.stringify(data, null, 2)}
|
|
627
|
+
`);
|
|
628
|
+
}
|
|
629
|
+
console.log(`${config.label}: added${original !== null ? " (backup saved)" : ""}`);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
async function uninstall(args = []) {
|
|
633
|
+
const targets = selected(args);
|
|
634
|
+
if (!args.includes("--yes") && !await confirm(`Remove imge from ${targets.map((config) => config.label).join(", ")}?`))
|
|
635
|
+
return;
|
|
636
|
+
for (const config of targets) {
|
|
637
|
+
const original = await readFile3(config.file, "utf8").catch(() => null);
|
|
638
|
+
if (!original) {
|
|
639
|
+
console.log(`${config.label}: not configured`);
|
|
640
|
+
continue;
|
|
641
|
+
}
|
|
642
|
+
if (config.kind === "toml") {
|
|
643
|
+
const updated = original.replace(/^\[mcp_servers\.imge\]\s*\n(?:^(?!\[)[^\n]*\n)*/gm, "");
|
|
644
|
+
if (updated === original) {
|
|
645
|
+
console.log(`${config.label}: not configured`);
|
|
646
|
+
continue;
|
|
647
|
+
}
|
|
648
|
+
await save(config, original, updated);
|
|
649
|
+
} else {
|
|
650
|
+
let data;
|
|
651
|
+
try {
|
|
652
|
+
data = JSON.parse(original);
|
|
653
|
+
} catch {
|
|
654
|
+
console.error(`${config.label}: invalid JSON, skipped`);
|
|
655
|
+
continue;
|
|
656
|
+
}
|
|
657
|
+
const servers = data[config.key];
|
|
658
|
+
if (!servers || !(PRODUCT_NAME in servers)) {
|
|
659
|
+
console.log(`${config.label}: not configured`);
|
|
660
|
+
continue;
|
|
661
|
+
}
|
|
662
|
+
delete servers[PRODUCT_NAME];
|
|
663
|
+
await save(config, original, `${JSON.stringify(data, null, 2)}
|
|
664
|
+
`);
|
|
665
|
+
}
|
|
666
|
+
console.log(`${config.label}: removed (backup saved)`);
|
|
667
|
+
}
|
|
668
|
+
console.log("Saved sessions remain on disk. Run `npx @imge/cli clear` to delete them.");
|
|
669
|
+
}
|
|
670
|
+
async function clearSessions() {
|
|
671
|
+
const items = (await readdir2(SESSIONS_DIR, { withFileTypes: true }).catch(() => [])).filter((item) => item.isDirectory() && /^[\w-]+$/.test(item.name)).map((item) => item.name);
|
|
672
|
+
console.log(`${items.length} local session${items.length === 1 ? "" : "s"}.`);
|
|
673
|
+
if (!items.length || !await confirm("Delete all local sessions?"))
|
|
674
|
+
return;
|
|
675
|
+
for (const item of items)
|
|
676
|
+
await rm2(join6(SESSIONS_DIR, item), { recursive: true });
|
|
677
|
+
console.log("Local sessions deleted.");
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
// src/cli.ts
|
|
681
|
+
var [command, ...extra] = process.argv.slice(2);
|
|
682
|
+
if (command === "mcp")
|
|
683
|
+
await serveProxy(extra);
|
|
684
|
+
else if (command === "setup")
|
|
685
|
+
await setup(extra);
|
|
686
|
+
else if (command === "uninstall")
|
|
687
|
+
await uninstall(extra);
|
|
688
|
+
else if (command === "clear")
|
|
689
|
+
await clearSessions();
|
|
690
|
+
else if (!command)
|
|
691
|
+
await startPlayer();
|
|
692
|
+
else if (command === "--help" || command === "-h")
|
|
693
|
+
console.log(`Usage: ${PRODUCT_NAME} [setup|uninstall|mcp|clear]`);
|
|
694
|
+
else {
|
|
695
|
+
console.error(`Usage: ${PRODUCT_NAME} [setup|uninstall|mcp|clear]`);
|
|
696
|
+
process.exitCode = 1;
|
|
697
|
+
}
|