@seekskyworld/open-browser 0.1.0 → 0.3.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/Docs/open-browser-api.d.ts +32 -10
- package/Docs//350/267/250/345/271/263/345/217/260/350/257/225/347/224/250/344/270/216/345/217/221/350/241/214.md +51 -3
- package/README.md +14 -5
- package/README.zh-CN.md +16 -5
- package/access-registry.mjs +13 -3
- package/action-execution.mjs +60 -24
- package/api-contract.mjs +19 -7
- package/browser-extension/manifest.json +1 -4
- package/browser-extension/newtab.css +6 -0
- package/browser-extension/newtab.js +54 -2
- package/browser-extension/phase-labels.js +1 -1
- package/browser-extension/popup.css +1 -1
- package/browser-extension/popup.html +16 -4
- package/browser-extension/popup.js +122 -21
- package/browser-extension/tab-action.js +4 -2
- package/browser-extension/worker.js +102 -9
- package/build-profile.mjs +23 -0
- package/capability-matrix.mjs +4 -4
- package/chromium-host.mjs +81 -25
- package/chromium-shell.mjs +78 -49
- package/cli.mjs +63 -5
- package/contract-validation.mjs +1 -1
- package/desktop/Info.plist +2 -2
- package/desktop/OBControlBar.m +7 -3
- package/desktop/WorkspacePanel.m +4 -2
- package/desktop/brand-keychain.py +20 -4
- package/desktop/build-macos.mjs +31 -12
- package/desktop/build-managed-input.mjs +37 -0
- package/desktop/build-profile.mjs +6 -0
- package/desktop/build.mjs +8 -0
- package/desktop/macos-entitlements.plist +12 -0
- package/desktop/notarize-macos.mjs +189 -0
- package/desktop/package-macos.mjs +54 -0
- package/desktop/patches/chromium-managed-input.patch +144 -0
- package/desktop/sign.mjs +63 -4
- package/desktop/start-profile.mjs +5 -0
- package/desktop/start.mjs +25 -3
- package/desktop/verify-macos-release.mjs +69 -0
- package/desktop/verify-version.mjs +22 -2
- package/diagnostics.mjs +9 -0
- package/engine-service.mjs +244 -29
- package/managed-input.mjs +155 -0
- package/mcp-http.mjs +84 -0
- package/mcp-server.mjs +57 -0
- package/mcp.mjs +3 -40
- package/package.json +24 -3
- package/page-activity.mjs +97 -29
- package/page-observation.mjs +44 -3
- package/paths.mjs +5 -3
- package/receipt-store.mjs +24 -7
- package/runtime.mjs +10 -1
- package/sdk.mjs +53 -26
- package/service.mjs +45 -10
- package/skill-links.mjs +144 -0
- package/skills/open-browser/SKILL.md +8 -76
- package/skills/open-browser/references/macos.md +3 -0
- package/skills/open-browser/references/operations.md +59 -0
- package/skills/open-browser/references/recovery.md +36 -0
- package/skills/open-browser/references/windows.md +3 -0
- package/skills/open-browser/scripts/bootstrap.mjs +112 -45
- package/skills/open-browser/scripts/guide.mjs +17 -0
- package/studio/studio.js +2 -2
- package/tab-adoption.mjs +83 -0
- package/tab-recovery.mjs +100 -0
- package/uploads.mjs +4 -4
- package/visual-page.mjs +17 -7
- package/workspace-lifecycle.mjs +92 -8
- package/workspace-state.mjs +65 -12
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { ServiceError } from './workspace-state.mjs';
|
|
2
|
+
|
|
3
|
+
// The preferred guard is implemented inside Chromium. A stock Chromium build
|
|
4
|
+
// uses a page-level capture fallback, which blocks page input while allowing
|
|
5
|
+
// the service to open a short dispatch window for one Agent action. The
|
|
6
|
+
// fallback avoids keeping a CDP session attached after native support has been
|
|
7
|
+
// ruled out because the extension also uses chrome.debugger.
|
|
8
|
+
const fallbackSource = `(() => {
|
|
9
|
+
const key = '__openBrowserInputGuard';
|
|
10
|
+
const current = globalThis[key];
|
|
11
|
+
if (current) { current.enabled = true; return; }
|
|
12
|
+
const state = { enabled: true };
|
|
13
|
+
const block = event => {
|
|
14
|
+
if (!state.enabled || !event.isTrusted) return;
|
|
15
|
+
event.preventDefault();
|
|
16
|
+
event.stopImmediatePropagation();
|
|
17
|
+
};
|
|
18
|
+
for (const type of ['click','dblclick','mousedown','mouseup','mousemove','mouseenter','mouseleave',
|
|
19
|
+
'pointerdown','pointerup','pointermove','pointercancel','wheel','touchstart','touchmove','touchend',
|
|
20
|
+
'contextmenu','keydown','keypress','keyup','beforeinput','input','change'])
|
|
21
|
+
document.addEventListener(type, block, true);
|
|
22
|
+
globalThis[key] = state;
|
|
23
|
+
})();`;
|
|
24
|
+
|
|
25
|
+
function fallbackExpression(enabled) {
|
|
26
|
+
return `globalThis.__openBrowserInputGuard && (globalThis.__openBrowserInputGuard.enabled = ${enabled ? 'true' : 'false'})`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function setFallbackState(page, enabled) {
|
|
30
|
+
if (page.isClosed()) return;
|
|
31
|
+
let lastError;
|
|
32
|
+
// A handoff can coincide with a renderer navigation. Do not silently report
|
|
33
|
+
// human control before the page-level listener has actually been disabled.
|
|
34
|
+
// Retry the current execution context briefly, then let the lifecycle roll
|
|
35
|
+
// back to a safe state if the page remains unavailable.
|
|
36
|
+
for (let attempt = 0; attempt < 3; attempt++) {
|
|
37
|
+
try {
|
|
38
|
+
// Init scripts cannot be removed from a Playwright page. Append a small
|
|
39
|
+
// state update so a later navigation preserves the handoff decision.
|
|
40
|
+
await page.addInitScript({ content: fallbackExpression(enabled) });
|
|
41
|
+
await page.evaluate(fallbackExpression(enabled));
|
|
42
|
+
const state = await page.evaluate(() => globalThis.__openBrowserInputGuard?.enabled);
|
|
43
|
+
if (state === enabled) return;
|
|
44
|
+
lastError = Error('Input guard state was not applied');
|
|
45
|
+
} catch (error) {
|
|
46
|
+
lastError = error;
|
|
47
|
+
}
|
|
48
|
+
if (!page.isClosed()) await new Promise(resolve => setTimeout(resolve, 25 * (attempt + 1)));
|
|
49
|
+
}
|
|
50
|
+
if (!page.isClosed()) throw lastError || Error('Input guard state was not applied');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function managedInput(context, onBlocked, { allowFallback = false, forceFallback = false } = {}) {
|
|
54
|
+
const sessions = new WeakMap();
|
|
55
|
+
const disconnectedSessions = new WeakSet();
|
|
56
|
+
|
|
57
|
+
async function setGuard(tab, enabled, onLost) {
|
|
58
|
+
const { page } = tab;
|
|
59
|
+
let entry = sessions.get(page);
|
|
60
|
+
if (page.isClosed()) return;
|
|
61
|
+
if (!enabled) {
|
|
62
|
+
if (!entry) return;
|
|
63
|
+
if (entry.mode === 'fallback') {
|
|
64
|
+
await setFallbackState(page, false);
|
|
65
|
+
sessions.delete(page);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
try { await entry.session.send('Input.setOpenBrowserInputGuard', { enabled: false }); }
|
|
69
|
+
catch (error) {
|
|
70
|
+
if (!page.isClosed() && !disconnectedSessions.has(entry.session)) throw error;
|
|
71
|
+
}
|
|
72
|
+
sessions.delete(page);
|
|
73
|
+
try { await entry.session.detach(); }
|
|
74
|
+
catch (error) {
|
|
75
|
+
if (!page.isClosed() && !disconnectedSessions.has(entry.session)) {
|
|
76
|
+
sessions.set(page, entry);
|
|
77
|
+
throw error;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (entry) return;
|
|
83
|
+
if (forceFallback && allowFallback) {
|
|
84
|
+
try {
|
|
85
|
+
await page.addInitScript({ content: fallbackSource });
|
|
86
|
+
await page.evaluate(fallbackSource);
|
|
87
|
+
entry = { mode: 'fallback', depth: 0, transition: Promise.resolve() };
|
|
88
|
+
sessions.set(page, entry);
|
|
89
|
+
} catch (cause) {
|
|
90
|
+
if (!page.isClosed()) throw Object.assign(new ServiceError('input_guard_unavailable'), { cause });
|
|
91
|
+
}
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
const session = await context.newCDPSession(page);
|
|
95
|
+
entry = { session, mode: 'native', depth: 0, transition: Promise.resolve() };
|
|
96
|
+
try {
|
|
97
|
+
session.once('close', () => {
|
|
98
|
+
disconnectedSessions.add(session);
|
|
99
|
+
if (sessions.get(page) !== entry) return;
|
|
100
|
+
sessions.delete(page);
|
|
101
|
+
if (!page.isClosed()) onLost?.();
|
|
102
|
+
});
|
|
103
|
+
session.on('Input.openBrowserInputBlocked', () => {
|
|
104
|
+
if (sessions.get(page) === entry) onBlocked(tab);
|
|
105
|
+
});
|
|
106
|
+
let timer;
|
|
107
|
+
try {
|
|
108
|
+
await Promise.race([
|
|
109
|
+
session.send('Input.setOpenBrowserInputGuard', { enabled: true }),
|
|
110
|
+
new Promise((_, reject) => {
|
|
111
|
+
timer = setTimeout(() => reject(Error('Input guard acknowledgement timed out')), 5000);
|
|
112
|
+
}),
|
|
113
|
+
]);
|
|
114
|
+
} finally { clearTimeout(timer); }
|
|
115
|
+
if (disconnectedSessions.has(session)) throw Error('input guard session closed');
|
|
116
|
+
sessions.set(page, entry);
|
|
117
|
+
} catch (cause) {
|
|
118
|
+
const unsupported = /wasn.t found|not found|unknown command/i.test(String(cause?.message));
|
|
119
|
+
await session.detach().catch(() => {});
|
|
120
|
+
if (!allowFallback || !unsupported) {
|
|
121
|
+
if (!page.isClosed()) throw Object.assign(new ServiceError('input_guard_unavailable'), { cause });
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
try {
|
|
125
|
+
await page.addInitScript({ content: fallbackSource });
|
|
126
|
+
await page.evaluate(fallbackSource);
|
|
127
|
+
entry = { mode: 'fallback', depth: 0, transition: Promise.resolve() };
|
|
128
|
+
sessions.set(page, entry);
|
|
129
|
+
} catch (fallbackCause) {
|
|
130
|
+
if (!page.isClosed()) throw Object.assign(new ServiceError('input_guard_unavailable'), { cause: fallbackCause });
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
setGuard.withAgentInput = async (tab, work) => {
|
|
136
|
+
const entry = sessions.get(tab.page);
|
|
137
|
+
if (!entry || entry.mode !== 'fallback') return work();
|
|
138
|
+
entry.depth++;
|
|
139
|
+
if (entry.depth === 1) {
|
|
140
|
+
entry.transition = entry.transition.then(() => setFallbackState(tab.page, false));
|
|
141
|
+
await entry.transition;
|
|
142
|
+
}
|
|
143
|
+
try { return await work(); }
|
|
144
|
+
finally {
|
|
145
|
+
entry.depth--;
|
|
146
|
+
if (entry.depth === 0) {
|
|
147
|
+
entry.transition = entry.transition.then(() => setFallbackState(tab.page, true));
|
|
148
|
+
await entry.transition.catch(() => {});
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
setGuard.mode = tab => sessions.get(tab.page)?.mode || null;
|
|
154
|
+
return setGuard;
|
|
155
|
+
}
|
package/mcp-http.mjs
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { createServer } from 'node:http';
|
|
3
|
+
import { randomUUID } from 'node:crypto';
|
|
4
|
+
import { readFile } from 'node:fs/promises';
|
|
5
|
+
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
|
|
6
|
+
import { isInitializeRequest } from '@modelcontextprotocol/sdk/types.js';
|
|
7
|
+
import { createMcpServer } from './mcp-server.mjs';
|
|
8
|
+
import { tokenPath } from './paths.mjs';
|
|
9
|
+
|
|
10
|
+
const host = process.env.OPEN_BROWSER_MCP_HTTP_HOST || '127.0.0.1';
|
|
11
|
+
const port = Number(process.env.OPEN_BROWSER_MCP_HTTP_PORT || 9239);
|
|
12
|
+
if (!['127.0.0.1', '::1'].includes(host)) throw new Error('MCP HTTP 只允许绑定回环地址。');
|
|
13
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535) throw new Error('OPEN_BROWSER_MCP_HTTP_PORT 无效。');
|
|
14
|
+
|
|
15
|
+
async function readToken() {
|
|
16
|
+
const configured = process.env.OPEN_BROWSER_MCP_HTTP_TOKEN?.trim();
|
|
17
|
+
if (configured) return configured;
|
|
18
|
+
try { return (await readFile(tokenPath, 'utf8')).trim(); } catch { return ''; }
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async function readBody(req) {
|
|
22
|
+
const chunks = [];
|
|
23
|
+
let bytes = 0;
|
|
24
|
+
for await (const chunk of req) {
|
|
25
|
+
bytes += chunk.length;
|
|
26
|
+
if (bytes > 1024 * 1024) throw new Error('request_too_large');
|
|
27
|
+
chunks.push(chunk);
|
|
28
|
+
}
|
|
29
|
+
if (!bytes) return undefined;
|
|
30
|
+
return JSON.parse(Buffer.concat(chunks, bytes).toString('utf8'));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const sessions = new Map();
|
|
34
|
+
const server = createServer(async (req, res) => {
|
|
35
|
+
if (req.url !== '/mcp') { res.writeHead(404).end(); return; }
|
|
36
|
+
const token = await readToken();
|
|
37
|
+
const authorization = req.headers.authorization || '';
|
|
38
|
+
if (!token || authorization !== `Bearer ${token}`) {
|
|
39
|
+
res.writeHead(401, { 'content-type': 'application/json' }).end(JSON.stringify({ error: 'unauthorized' }));
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const sessionId = req.headers['mcp-session-id'];
|
|
43
|
+
if (req.method === 'DELETE') {
|
|
44
|
+
const session = sessions.get(sessionId);
|
|
45
|
+
if (!session) { res.writeHead(404).end(); return; }
|
|
46
|
+
sessions.delete(sessionId);
|
|
47
|
+
await session.mcp.close().catch(() => {});
|
|
48
|
+
res.writeHead(204).end();
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (!['POST', 'GET'].includes(req.method)) { res.writeHead(405, { allow: 'GET, POST, DELETE' }).end(); return; }
|
|
52
|
+
let body;
|
|
53
|
+
try { body = req.method === 'POST' ? await readBody(req) : undefined; } catch {
|
|
54
|
+
res.writeHead(400, { 'content-type': 'application/json' }).end(JSON.stringify({ error: 'invalid_json' }));
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
let session = sessionId ? sessions.get(sessionId) : undefined;
|
|
58
|
+
if (!session) {
|
|
59
|
+
if (req.method !== 'POST' || !isInitializeRequest(body)) {
|
|
60
|
+
res.writeHead(400, { 'content-type': 'application/json' }).end(JSON.stringify({ error: 'initialize_required' }));
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const mcp = createMcpServer();
|
|
64
|
+
const transport = new StreamableHTTPServerTransport({
|
|
65
|
+
sessionIdGenerator: () => randomUUID(),
|
|
66
|
+
onsessioninitialized: id => { sessions.set(id, session); },
|
|
67
|
+
});
|
|
68
|
+
session = { mcp, transport };
|
|
69
|
+
transport.onclose = () => { if (transport.sessionId) sessions.delete(transport.sessionId); };
|
|
70
|
+
await mcp.connect(transport);
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
await session.transport.handleRequest(req, res, body);
|
|
74
|
+
} catch (error) {
|
|
75
|
+
if (!res.headersSent) res.writeHead(500, { 'content-type': 'application/json' }).end(JSON.stringify({ error: 'mcp_server_error' }));
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
server.listen(port, host, () => console.error(`open-browser MCP HTTP listening on http://${host}:${port}/mcp`));
|
|
80
|
+
for (const signal of ['SIGINT', 'SIGTERM']) process.once(signal, () => {
|
|
81
|
+
server.close();
|
|
82
|
+
void Promise.allSettled([...sessions.values()].map(session => session.mcp.close()))
|
|
83
|
+
.finally(() => { server.closeAllConnections(); });
|
|
84
|
+
});
|
package/mcp-server.mjs
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { Agent } from 'node:http';
|
|
2
|
+
import { readFileSync } from 'node:fs';
|
|
3
|
+
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
4
|
+
import { CallToolRequestSchema, ErrorCode, ListToolsRequestSchema, McpError } from '@modelcontextprotocol/sdk/types.js';
|
|
5
|
+
import { checkAgentRequest } from './contract-validation.mjs';
|
|
6
|
+
import { OpenBrowserClient, OpenBrowserError } from './sdk.mjs';
|
|
7
|
+
import { tools } from './api-contract.mjs';
|
|
8
|
+
|
|
9
|
+
const {version} = JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8'));
|
|
10
|
+
|
|
11
|
+
export function createMcpServer() {
|
|
12
|
+
const server = new Server({ name: 'open-browser', version }, { capabilities: { tools: {} } });
|
|
13
|
+
let api;
|
|
14
|
+
server.onclose = () => {
|
|
15
|
+
api?.close();
|
|
16
|
+
api = undefined;
|
|
17
|
+
};
|
|
18
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools }));
|
|
19
|
+
server.setRequestHandler(CallToolRequestSchema, async ({ params }, { signal }) => {
|
|
20
|
+
let args;
|
|
21
|
+
try {
|
|
22
|
+
const arguments_ = params.arguments ?? {};
|
|
23
|
+
if (['createWorkspace', 'open'].includes(params.name) &&
|
|
24
|
+
(typeof arguments_.name !== 'string' || !arguments_.name.trim()))
|
|
25
|
+
throw new McpError(ErrorCode.InvalidParams, '创建 Agent 任务时必须提供非空任务名称 name。');
|
|
26
|
+
args = checkAgentRequest(params.name, arguments_);
|
|
27
|
+
} catch (error) {
|
|
28
|
+
if (error instanceof McpError) throw error;
|
|
29
|
+
throw new McpError(ErrorCode.InvalidParams, 'Invalid open-browser tool name or arguments');
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
if (!api) {
|
|
33
|
+
const client = server.getClientVersion();
|
|
34
|
+
const clientName = client?.title?.trim() || client?.name?.trim() || 'MCP';
|
|
35
|
+
// Bound idle sockets only. A query or cancellation must be able to
|
|
36
|
+
// reach the service while long-running calls hold their connections.
|
|
37
|
+
api = new OpenBrowserClient({
|
|
38
|
+
agent: new Agent({ keepAlive:true, maxFreeSockets:4 }),
|
|
39
|
+
agentName: process.env.OPEN_BROWSER_AGENT_NAME?.trim() || Array.from(clientName).slice(0, 80).join(''),
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
const result = params.name === 'capabilities'
|
|
43
|
+
? await api.capabilities({signal}) : await api.call(params.name, args, {signal});
|
|
44
|
+
if (params.name === 'captureVisual') {
|
|
45
|
+
const { data, mimeType, ...geometry } = result;
|
|
46
|
+
return { content: [{ type: 'image', mimeType, data }, { type: 'text', text: JSON.stringify(geometry) }] };
|
|
47
|
+
}
|
|
48
|
+
return { content: [{ type: 'text', text: JSON.stringify(result) }] };
|
|
49
|
+
} catch (error) {
|
|
50
|
+
const detail = error instanceof OpenBrowserError
|
|
51
|
+
? { code: error.code, message: error.message, ...(error.nextAction ? { nextAction: error.nextAction } : {}) }
|
|
52
|
+
: { code: 'CLIENT_ERROR', message: '本地服务调用失败。' };
|
|
53
|
+
return { isError: true, content: [{ type: 'text', text: JSON.stringify(detail) }] };
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
return server;
|
|
57
|
+
}
|
package/mcp.mjs
CHANGED
|
@@ -1,43 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
3
2
|
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
import { OpenBrowserClient, OpenBrowserError } from './sdk.mjs';
|
|
7
|
-
|
|
8
|
-
import { tools } from './api-contract.mjs';
|
|
9
|
-
const server = new Server({ name: 'open-browser', version: '0.1.0' }, { capabilities: { tools: {} } });
|
|
10
|
-
|
|
11
|
-
server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools }));
|
|
12
|
-
server.setRequestHandler(CallToolRequestSchema, async ({ params }) => {
|
|
13
|
-
let args;
|
|
14
|
-
try {
|
|
15
|
-
const arguments_ = params.arguments ?? {};
|
|
16
|
-
if (['createWorkspace', 'open'].includes(params.name) &&
|
|
17
|
-
(typeof arguments_.name !== 'string' || !arguments_.name.trim()))
|
|
18
|
-
throw new McpError(ErrorCode.InvalidParams, '创建 Agent 任务时必须提供非空任务名称 name。');
|
|
19
|
-
args = checkAgentRequest(params.name, arguments_);
|
|
20
|
-
} catch (error) {
|
|
21
|
-
if (error instanceof McpError) throw error;
|
|
22
|
-
throw new McpError(ErrorCode.InvalidParams, 'Invalid open-browser tool name or arguments');
|
|
23
|
-
}
|
|
24
|
-
try {
|
|
25
|
-
const client = server.getClientVersion();
|
|
26
|
-
const clientName = client?.title?.trim() || client?.name?.trim() || 'MCP';
|
|
27
|
-
const api = new OpenBrowserClient({ agentName: process.env.OPEN_BROWSER_AGENT_NAME?.trim() ||
|
|
28
|
-
Array.from(clientName).slice(0, 80).join('') });
|
|
29
|
-
const result = params.name === 'capabilities' ? await api.capabilities() : await api.call(params.name, args);
|
|
30
|
-
if (params.name === 'captureVisual') {
|
|
31
|
-
const {data,mimeType,...geometry}=result;
|
|
32
|
-
return {content:[{type:'image',mimeType,data},{type:'text',text:JSON.stringify(geometry)}]};
|
|
33
|
-
}
|
|
34
|
-
return { content: [{ type: 'text', text: JSON.stringify(result) }] };
|
|
35
|
-
} catch (error) {
|
|
36
|
-
const detail = error instanceof OpenBrowserError
|
|
37
|
-
? { code: error.code, message: error.message, ...(error.nextAction ? { nextAction: error.nextAction } : {}) }
|
|
38
|
-
: { code: 'CLIENT_ERROR', message: '本地服务调用失败。' };
|
|
39
|
-
return { isError: true, content: [{ type: 'text', text: JSON.stringify(detail) }] };
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
|
|
3
|
+
import { createMcpServer } from './mcp-server.mjs';
|
|
4
|
+
const server = createMcpServer();
|
|
43
5
|
await server.connect(new StdioServerTransport());
|
|
6
|
+
process.stdin.once('end', () => { void server.close(); });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@seekskyworld/open-browser",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -18,7 +18,8 @@
|
|
|
18
18
|
"bin": {
|
|
19
19
|
"open-browser": "cli.mjs",
|
|
20
20
|
"open-browser-mcp": "mcp.mjs",
|
|
21
|
-
"open-browser-
|
|
21
|
+
"open-browser-mcp-http": "mcp-http.mjs",
|
|
22
|
+
"open-browser-skill": "skills/open-browser/scripts/bootstrap.mjs"
|
|
22
23
|
},
|
|
23
24
|
"exports": {
|
|
24
25
|
".": "./sdk.mjs",
|
|
@@ -34,10 +35,22 @@
|
|
|
34
35
|
},
|
|
35
36
|
"scripts": {
|
|
36
37
|
"start": "node desktop/start.mjs",
|
|
38
|
+
"start:dev": "node desktop/start-profile.mjs dev",
|
|
39
|
+
"start:stable": "node desktop/start-profile.mjs stable",
|
|
37
40
|
"build": "node desktop/build.mjs",
|
|
41
|
+
"build:dev": "node desktop/build-profile.mjs dev",
|
|
42
|
+
"build:stable": "node desktop/build-profile.mjs stable",
|
|
43
|
+
"build:macos:x64": "node desktop/build-macos.mjs --arch x64",
|
|
44
|
+
"build:macos:arm64": "node desktop/build-macos.mjs --arch arm64",
|
|
45
|
+
"build:release-macos": "npm run build:stable && node desktop/notarize-macos.mjs --zip dist/open-browser-macos-arm64.zip --dmg dist/open-browser-macos-arm64.dmg",
|
|
46
|
+
"build:release-macos:x64": "npm run build:macos:x64 && node desktop/notarize-macos.mjs dist/open-browser-x64.app --zip dist/open-browser-macos-x64.zip --dmg dist/open-browser-macos-x64.dmg",
|
|
47
|
+
"build:release-macos:arm64": "npm run build:macos:arm64 && node desktop/notarize-macos.mjs dist/open-browser.app --zip dist/open-browser-macos-arm64.zip --dmg dist/open-browser-macos-arm64.dmg",
|
|
48
|
+
"release:verify-macos": "node desktop/verify-macos-release.mjs",
|
|
38
49
|
"cli": "node cli.mjs",
|
|
39
50
|
"mcp": "node mcp.mjs",
|
|
51
|
+
"mcp:http": "node mcp-http.mjs",
|
|
40
52
|
"package:check": "npm pack --dry-run",
|
|
53
|
+
"prepack": "node desktop/verify-version.mjs",
|
|
41
54
|
"browser:install": "node runtime.mjs",
|
|
42
55
|
"control": "node control.mjs",
|
|
43
56
|
"test": "npm run test:unit",
|
|
@@ -46,7 +59,15 @@
|
|
|
46
59
|
"test:all": "npm run test:unit && npm run test:contract",
|
|
47
60
|
"test:headed": "OPEN_BROWSER_QA_HEADED=1 node desktop/verify-upgrade.mjs",
|
|
48
61
|
"build:verify": "node desktop/verify-version.mjs && npm run build",
|
|
49
|
-
"clean": "node -e \"import('node:fs/promises').then(({rm}) => Promise.all(['dist','.open-browser'].map(p => rm(p,{recursive:true,force:true}))) )\""
|
|
62
|
+
"clean": "node -e \"import('node:fs/promises').then(({rm}) => Promise.all(['dist','.open-browser'].map(p => rm(p,{recursive:true,force:true}))) )\"",
|
|
63
|
+
"package:macos": "npm run build:stable && node desktop/package-macos.mjs --arch arm64 --format both",
|
|
64
|
+
"package:macos:dmg": "npm run build:stable && node desktop/package-macos.mjs --arch arm64 --format dmg",
|
|
65
|
+
"package:macos:arm64": "npm run build:macos:arm64 && node desktop/package-macos.mjs --arch arm64 --format both",
|
|
66
|
+
"package:macos:x64": "npm run build:macos:x64 && node desktop/package-macos.mjs --arch x64 --format both",
|
|
67
|
+
"package:windows:x64": "node desktop/build-portable.mjs win32-x64",
|
|
68
|
+
"package:linux:x64": "node desktop/build-portable.mjs linux-x64",
|
|
69
|
+
"package:portable:all": "npm run package:windows:x64 && npm run package:linux:x64",
|
|
70
|
+
"package:all": "npm run package:macos && npm run package:portable:all"
|
|
50
71
|
},
|
|
51
72
|
"dependencies": {
|
|
52
73
|
"@modelcontextprotocol/sdk": "1.32.2",
|
package/page-activity.mjs
CHANGED
|
@@ -1,81 +1,142 @@
|
|
|
1
1
|
// Runs in a Chromium isolated world. The page receives only a decorative,
|
|
2
2
|
// input-transparent edge. Control requests stay in the browser toolbar.
|
|
3
|
-
function paintEdge(
|
|
3
|
+
function paintEdge(state, origin) {
|
|
4
4
|
const key = '__openBrowserActivityEdge';
|
|
5
|
-
if (!
|
|
5
|
+
if (!state || location.origin !== origin) {
|
|
6
6
|
globalThis[key]?.remove();
|
|
7
7
|
delete globalThis[key];
|
|
8
8
|
return;
|
|
9
9
|
}
|
|
10
|
-
if (
|
|
10
|
+
if (!document.documentElement) return;
|
|
11
|
+
if (globalThis[key]?.isConnected && globalThis[key].dataset.openBrowserActivity === state) return;
|
|
12
|
+
globalThis[key]?.remove();
|
|
11
13
|
const edge = document.createElement('div');
|
|
12
14
|
edge.setAttribute('aria-hidden', 'true');
|
|
13
|
-
edge.setAttribute('data-open-browser-activity',
|
|
15
|
+
edge.setAttribute('data-open-browser-activity', state);
|
|
14
16
|
edge.inert = true;
|
|
15
17
|
for (const [property, value] of Object.entries({
|
|
16
18
|
all: 'initial', position: 'fixed', inset: '0', 'z-index': '2147483647',
|
|
17
19
|
'pointer-events': 'none', 'border-radius': '4px',
|
|
18
|
-
'box-shadow': 'inset 0 0 0 2px #7762e8, inset 0 0 12px 1px rgba(62,159,215,.55)'
|
|
20
|
+
'box-shadow': state === 'running' ? 'inset 0 0 0 2px #7762e8, inset 0 0 12px 1px rgba(62,159,215,.55)'
|
|
21
|
+
: 'inset 0 0 0 2px rgba(119,98,232,.55)',
|
|
19
22
|
})) edge.style.setProperty(property, value, 'important');
|
|
20
|
-
if (!matchMedia('(prefers-reduced-motion: reduce)').matches)
|
|
23
|
+
if (state === 'running' && !matchMedia('(prefers-reduced-motion: reduce)').matches)
|
|
21
24
|
edge.animate([{ opacity: .6 }, { opacity: 1 }], { duration: 850, iterations: Infinity, direction: 'alternate' });
|
|
22
25
|
document.documentElement.append(edge);
|
|
23
26
|
globalThis[key] = edge;
|
|
24
27
|
}
|
|
25
28
|
|
|
26
|
-
function paintTarget(x, y, origin) {
|
|
29
|
+
function paintTarget(x, y, origin, actionKind = '操作') {
|
|
27
30
|
const key = '__openBrowserActionRing';
|
|
28
31
|
globalThis[key]?.remove();
|
|
29
32
|
delete globalThis[key];
|
|
30
33
|
if (origin !== location.origin || !Number.isFinite(x) || !Number.isFinite(y) ||
|
|
31
|
-
x < 0 || y < 0 || x > innerWidth || y > innerHeight || !document.documentElement)
|
|
34
|
+
x < 0 || y < 0 || x > innerWidth || y > innerHeight || !document.documentElement) {
|
|
35
|
+
globalThis.__openBrowserAgentCursor?.remove();
|
|
36
|
+
delete globalThis.__openBrowserAgentCursor;
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
32
39
|
const ring = document.createElement('div');
|
|
33
40
|
ring.inert = true;
|
|
34
41
|
ring.setAttribute('aria-hidden', 'true');
|
|
35
42
|
ring.setAttribute('data-open-browser-target', '');
|
|
43
|
+
const actionLabels = { click: '点击', doubleClick: '双击', hover: '悬停', press: '按键',
|
|
44
|
+
scroll: '滚动', drag: '拖拽', fill: '填写', fillCredential: '填写凭据', select: '选择',
|
|
45
|
+
upload: '上传', setChecked: '切换' };
|
|
36
46
|
for (const [property, value] of Object.entries({
|
|
37
47
|
all: 'initial', position: 'fixed', left: (x - 11) + 'px', top: (y - 11) + 'px',
|
|
38
48
|
width: '18px', height: '18px', border: '2px solid #7659e8', 'border-radius': '50%',
|
|
39
49
|
'box-shadow': '0 0 0 2px white', 'z-index': '2147483647', 'pointer-events': 'none',
|
|
40
50
|
})) ring.style.setProperty(property, value, 'important');
|
|
51
|
+
let cursor = globalThis.__openBrowserAgentCursor;
|
|
52
|
+
if (!cursor?.isConnected || cursor.children.length !== 2) {
|
|
53
|
+
cursor?.remove();
|
|
54
|
+
cursor = document.createElement('div');
|
|
55
|
+
cursor.inert = true;
|
|
56
|
+
cursor.setAttribute('aria-hidden', 'true');
|
|
57
|
+
cursor.setAttribute('data-open-browser-agent-cursor', '');
|
|
58
|
+
const arrow = document.createElement('span'), label = document.createElement('span');
|
|
59
|
+
const style = (node, properties) => {
|
|
60
|
+
for (const [property, value] of Object.entries(properties)) node.style.setProperty(property, value, 'important');
|
|
61
|
+
};
|
|
62
|
+
style(cursor, { all: 'initial', position: 'fixed', width: '18px', height: '24px',
|
|
63
|
+
'z-index': '2147483647', 'pointer-events': 'none',
|
|
64
|
+
transition: matchMedia('(prefers-reduced-motion: reduce)').matches ? 'none' : 'left 140ms ease, top 140ms ease' });
|
|
65
|
+
style(arrow, { all: 'initial', display: 'block', width: '18px', height: '24px', background: '#7659e8',
|
|
66
|
+
'clip-path': 'polygon(0 0, 0 100%, 30% 72%, 48% 100%, 60% 94%, 43% 66%, 86% 66%)', 'pointer-events': 'none' });
|
|
67
|
+
style(label, { all: 'initial', display: 'block', position: 'absolute', padding: '3px 7px',
|
|
68
|
+
color: 'white', background: 'rgba(45,35,90,.92)', 'border-radius': '5px',
|
|
69
|
+
font: '12px/1.2 -apple-system,BlinkMacSystemFont,sans-serif', 'white-space': 'nowrap', 'pointer-events': 'none' });
|
|
70
|
+
cursor.append(arrow, label);
|
|
71
|
+
document.documentElement.append(cursor);
|
|
72
|
+
globalThis.__openBrowserAgentCursor = cursor;
|
|
73
|
+
}
|
|
74
|
+
cursor.style.setProperty('left', x + 'px', 'important');
|
|
75
|
+
cursor.style.setProperty('top', y + 'px', 'important');
|
|
76
|
+
const label = cursor.lastElementChild;
|
|
77
|
+
label.textContent = 'Agent · ' + (actionLabels[actionKind] || '操作');
|
|
78
|
+
label.style.setProperty('left', x + 120 > innerWidth ? 'auto' : '12px', 'important');
|
|
79
|
+
label.style.setProperty('right', x + 120 > innerWidth ? '18px' : 'auto', 'important');
|
|
80
|
+
label.style.setProperty('top', y + 40 > innerHeight ? '-22px' : '10px', 'important');
|
|
41
81
|
document.documentElement.append(ring);
|
|
42
82
|
globalThis[key] = ring;
|
|
43
83
|
if (!matchMedia('(prefers-reduced-motion: reduce)').matches)
|
|
44
84
|
ring.animate([{ transform: 'scale(.75)', opacity: 1 }, { transform: 'scale(1.75)', opacity: 0 }], 460);
|
|
45
|
-
setTimeout(() => {
|
|
85
|
+
setTimeout(() => {
|
|
86
|
+
ring.remove();
|
|
87
|
+
if (globalThis[key] === ring) delete globalThis[key];
|
|
88
|
+
}, 470);
|
|
46
89
|
}
|
|
47
90
|
|
|
48
|
-
async function paint(tab, fn, args, current) {
|
|
91
|
+
async function paint(tab, fn, args, current, retry = true) {
|
|
49
92
|
if (!current() || tab.page.isClosed() || !/^https?:/.test(tab.page.url())) return;
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
93
|
+
let executionContextId = tab.context?.navigation === tab.navigation ? tab.context.id : undefined;
|
|
94
|
+
if (!executionContextId) {
|
|
95
|
+
const navigation = tab.navigation;
|
|
96
|
+
const { frameTree } = await tab.session.send('Page.getFrameTree');
|
|
97
|
+
const created = await tab.session.send('Page.createIsolatedWorld', {
|
|
98
|
+
frameId: frameTree.frame.id, worldName: 'open-browser-activity',
|
|
99
|
+
});
|
|
100
|
+
executionContextId = created.executionContextId;
|
|
101
|
+
if (navigation !== tab.navigation || !current()) return;
|
|
102
|
+
tab.context = { navigation, id: executionContextId };
|
|
103
|
+
}
|
|
54
104
|
if (!current()) return;
|
|
55
|
-
|
|
56
|
-
|
|
105
|
+
try {
|
|
106
|
+
await tab.session.send('Runtime.evaluate', { contextId: executionContextId,
|
|
107
|
+
expression: '(' + fn.toString() + ')(...' + JSON.stringify(args) + ')' });
|
|
108
|
+
} catch (error) {
|
|
109
|
+
if (tab.context?.id === executionContextId && /context|execution|target/i.test(error.message)) {
|
|
110
|
+
tab.context = undefined;
|
|
111
|
+
if (retry) return paint(tab, fn, args, current, false);
|
|
112
|
+
}
|
|
113
|
+
throw error;
|
|
114
|
+
}
|
|
57
115
|
}
|
|
58
116
|
|
|
59
117
|
export function pageActivity() {
|
|
60
118
|
const tabs = new Map();
|
|
61
|
-
function render(tab, origin, force = false) {
|
|
62
|
-
if (!force && tab.origin === origin) return;
|
|
119
|
+
function render(tab, origin, state, force = false) {
|
|
120
|
+
if (!force && tab.origin === origin && tab.edgeState === state) return;
|
|
63
121
|
const wasActive = !!tab.origin;
|
|
64
122
|
tab.origin = origin;
|
|
123
|
+
tab.edgeState = state;
|
|
65
124
|
if (!wasActive && !origin) return;
|
|
66
125
|
const revision = ++tab.revision;
|
|
67
126
|
tab.pending = tab.pending.then(() =>
|
|
68
|
-
paint(tab, paintEdge, [
|
|
127
|
+
paint(tab, paintEdge, [state, origin], () => tab.revision === revision)
|
|
128
|
+
.then(() => { if (state !== 'running') return paint(tab, paintTarget, [null, null, null], () => tab.revision === revision); })
|
|
129
|
+
.catch(() => {}));
|
|
69
130
|
}
|
|
70
131
|
return {
|
|
71
132
|
track(page, session, workspaceId, pageId) {
|
|
72
133
|
if (tabs.has(pageId)) return;
|
|
73
|
-
const tab = { page, session, workspaceId, origin: null, revision: 0, pending: Promise.resolve() };
|
|
134
|
+
const tab = { page, session, workspaceId, origin: null, revision: 0, targetRevision: 0, pending: Promise.resolve() };
|
|
74
135
|
tabs.set(pageId, tab);
|
|
75
136
|
tab.navigation = 0;
|
|
76
|
-
tab.navigated = frame => { if (frame === page.mainFrame()) tab.navigation++; };
|
|
137
|
+
tab.navigated = frame => { if (frame === page.mainFrame()) { tab.navigation++; tab.context = undefined; } };
|
|
77
138
|
page.on('framenavigated',tab.navigated);
|
|
78
|
-
tab.loaded = () => { if (tab.origin) render(tab, tab.origin, true); };
|
|
139
|
+
tab.loaded = () => { if (tab.origin) render(tab, tab.origin, tab.edgeState, true); };
|
|
79
140
|
page.on('domcontentloaded',tab.loaded);
|
|
80
141
|
tab.closed = () => tabs.delete(pageId);
|
|
81
142
|
page.once('close',tab.closed);
|
|
@@ -87,6 +148,7 @@ export function pageActivity() {
|
|
|
87
148
|
tab.mode = 'released';
|
|
88
149
|
tab.generation = null;
|
|
89
150
|
tab.revision++;
|
|
151
|
+
tab.context = undefined;
|
|
90
152
|
tab.page.off('framenavigated',tab.navigated);
|
|
91
153
|
tab.page.off('domcontentloaded',tab.loaded);
|
|
92
154
|
tab.page.off('close',tab.closed);
|
|
@@ -97,24 +159,30 @@ export function pageActivity() {
|
|
|
97
159
|
} finally { await tab.session.detach().catch(() => {}); }
|
|
98
160
|
},
|
|
99
161
|
update({ workspaceId, pages, mode, activity, controlGeneration }) {
|
|
100
|
-
const running = new Set((activity.running || [activity.current]).filter(job => mode === 'agent' && job?.state === 'running'
|
|
162
|
+
const running = new Set((activity.running || [activity.current]).filter(job => mode === 'agent' && job?.state === 'running' &&
|
|
163
|
+
job.kind !== 'captureVisual' && (job.kind !== 'act' || (job.stepKind && job.stepKind !== 'spatial'))).map(job => job.pageId));
|
|
101
164
|
for (const [id, tab] of tabs) {
|
|
102
165
|
if (tab.workspaceId !== workspaceId) continue;
|
|
103
166
|
const previous = tab.generation;
|
|
104
167
|
tab.generation = controlGeneration;
|
|
105
168
|
if (previous && previous !== controlGeneration)
|
|
106
|
-
|
|
169
|
+
tab.pending = tab.pending.then(() => paint(tab, paintTarget, [null, null, null],
|
|
170
|
+
() => tab.generation === controlGeneration)).catch(() => {});
|
|
107
171
|
tab.mode = mode;
|
|
108
|
-
const
|
|
109
|
-
|
|
172
|
+
const page = pages.find(page => page.pageId === id);
|
|
173
|
+
const origin = mode === 'agent' && /^https?:/.test(page?.origin) ? page.origin : null;
|
|
174
|
+
const state = origin ? (running.has(id) ? 'running' : 'idle') : null;
|
|
175
|
+
render(tab, origin, state);
|
|
110
176
|
}
|
|
111
177
|
},
|
|
112
|
-
target({ workspaceId, pageId, x, y, origin, controlGeneration }) {
|
|
178
|
+
target({ workspaceId, pageId, x, y, origin, controlGeneration, actionKind }) {
|
|
113
179
|
const tab = tabs.get(pageId);
|
|
114
180
|
if (!tab || tab.workspaceId !== workspaceId) return;
|
|
115
181
|
const navigation = tab.navigation;
|
|
116
|
-
|
|
117
|
-
|
|
182
|
+
const ticket = ++tab.targetRevision;
|
|
183
|
+
tab.pending = tab.pending.then(() => paint(tab, paintTarget, [x, y, origin, actionKind], () =>
|
|
184
|
+
tab.generation === controlGeneration && tab.navigation === navigation && tab.mode === 'agent' &&
|
|
185
|
+
tab.origin === origin && tab.edgeState === 'running' && tab.targetRevision === ticket)).catch(() => {});
|
|
118
186
|
},
|
|
119
187
|
has(pageId) { return tabs.has(pageId); },
|
|
120
188
|
};
|