pagerig 0.1.1 → 0.1.3
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 +25 -8
- package/lib/bridge.mjs +382 -45
- package/mcp-server.mjs +4 -2
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -269,18 +269,35 @@ args = ["-y", "pagerig"]
|
|
|
269
269
|
bridge 只绑定回环地址。扩展 popup 支持填写 `host:port` 或
|
|
270
270
|
`ws://host:port/ext`,保存后会立即重连。
|
|
271
271
|
|
|
272
|
-
##
|
|
272
|
+
## 多进程与共享 bridge
|
|
273
273
|
|
|
274
|
-
|
|
274
|
+
OpenCode 2 会为每个已加载目录启动一个 MCP 进程,所以同一个客户端可能同时有多个
|
|
275
|
+
PageRig MCP 进程。PageRig 用一个共享 bridge 处理这种情况:
|
|
275
276
|
|
|
276
|
-
-
|
|
277
|
-
-
|
|
278
|
-
-
|
|
277
|
+
- 第一个进程成为 **leader**,绑定 `BB_PORT`,扩展连到它;
|
|
278
|
+
- 后续进程自动成为 **follower**,通过本地 `/proxy` 把工具调用转发给 leader;
|
|
279
|
+
- `browser_status` 返回 `role: "leader"` 或 `"follower"`;
|
|
280
|
+
- leader 退出时,follower 会自动尝试接管端口,扩展随后重连;
|
|
281
|
+
- 只有在端口被占用且无法连接已有 bridge 时,才返回 `PORT_IN_USE`。
|
|
279
282
|
|
|
280
|
-
|
|
283
|
+
如果确实遇到无法加入的端口冲突,可以关闭占用端口的进程,或给新实例设置不同的
|
|
284
|
+
`BB_PORT` 并同步修改扩展 popup 地址。
|
|
285
|
+
|
|
286
|
+
## 资源上限
|
|
281
287
|
|
|
282
|
-
|
|
283
|
-
|
|
288
|
+
为了避免长时间运行时占满内存,PageRig 对捕获数据做了硬限制:
|
|
289
|
+
|
|
290
|
+
| 项目 | 上限 |
|
|
291
|
+
| --- | --- |
|
|
292
|
+
| 网络条目 | 200 条 |
|
|
293
|
+
| 网络响应体总量 | 4 MB |
|
|
294
|
+
| 单个响应体 | 256 KB |
|
|
295
|
+
| console 条目 | 300 条 |
|
|
296
|
+
| 页面启动阶段待发送 hook 缓冲 | 50 条 / 4 MB |
|
|
297
|
+
| 整页截图 | 25 MP(超出会拒绝,建议视口截图) |
|
|
298
|
+
|
|
299
|
+
超过上限的数据会被截断或丢弃,避免扩展和页面进程因内存压力崩溃。需要完整响应体时,
|
|
300
|
+
建议缩小捕获范围(按 URL 或 method 过滤)后重新请求。
|
|
284
301
|
|
|
285
302
|
## 安全与权限
|
|
286
303
|
|
package/lib/bridge.mjs
CHANGED
|
@@ -2,8 +2,11 @@
|
|
|
2
2
|
* PageRig bridge.
|
|
3
3
|
*
|
|
4
4
|
* The Chrome extension can only make outbound connections, so it connects to a
|
|
5
|
-
* loopback WebSocket served here.
|
|
6
|
-
*
|
|
5
|
+
* loopback WebSocket served here. MCP clients such as OpenCode may start one
|
|
6
|
+
* MCP process per directory; the first process becomes the leader and owns the
|
|
7
|
+
* port, while later processes connect to the leader's local /proxy endpoint and
|
|
8
|
+
* forward commands. This keeps a single extension connection and one command
|
|
9
|
+
* queue while still allowing every directory to expose tools.
|
|
7
10
|
*/
|
|
8
11
|
import { createServer } from 'node:http';
|
|
9
12
|
import { randomUUID } from 'node:crypto';
|
|
@@ -14,12 +17,20 @@ const DEFAULT_PORT = 17321;
|
|
|
14
17
|
const DEFAULT_COMMAND_TIMEOUT_MS = 120000;
|
|
15
18
|
const DEFAULT_WAIT_EXTENSION_MS = 35000;
|
|
16
19
|
const MAX_WAIT_HEARTBEAT_MS = 60000;
|
|
20
|
+
const PROXY_HANDSHAKE_MS = 2000;
|
|
21
|
+
const PROXY_STATUS_TIMEOUT_MS = 5000;
|
|
22
|
+
const TAKEOVER_ATTEMPTS = 60;
|
|
23
|
+
const TAKEOVER_INTERVAL_MS = 1000;
|
|
17
24
|
|
|
18
25
|
function positiveNumber(value, fallback) {
|
|
19
26
|
const number = Number(value);
|
|
20
27
|
return Number.isFinite(number) && number > 0 ? number : fallback;
|
|
21
28
|
}
|
|
22
29
|
|
|
30
|
+
function sleep(ms) {
|
|
31
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
32
|
+
}
|
|
33
|
+
|
|
23
34
|
function disconnectedError(message = 'extension_not_connected') {
|
|
24
35
|
const error = new Error(message);
|
|
25
36
|
error.code = 'EXTENSION_DISCONNECTED';
|
|
@@ -30,7 +41,15 @@ function disconnectedError(message = 'extension_not_connected') {
|
|
|
30
41
|
function portInUseError(port, cause) {
|
|
31
42
|
const error = new Error(`port_in_use: ${port}`);
|
|
32
43
|
error.code = 'PORT_IN_USE';
|
|
33
|
-
error.hint =
|
|
44
|
+
error.hint = `端口 ${port} 被占用,但无法连接到已有的 PageRig bridge。关闭占用进程后重试。`;
|
|
45
|
+
error.cause = cause;
|
|
46
|
+
return error;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function proxyError(port, cause) {
|
|
50
|
+
const error = new Error(`proxy_unavailable: ${port}`);
|
|
51
|
+
error.code = 'PROXY_UNAVAILABLE';
|
|
52
|
+
error.hint = `无法连接到已有的 PageRig bridge(${port})。`;
|
|
34
53
|
error.cause = cause;
|
|
35
54
|
return error;
|
|
36
55
|
}
|
|
@@ -60,29 +79,17 @@ function closeWebSocket(ws) {
|
|
|
60
79
|
});
|
|
61
80
|
}
|
|
62
81
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
const host = options.host || process.env.BB_HOST || DEFAULT_HOST;
|
|
75
|
-
const port = positiveNumber(options.port ?? process.env.BB_PORT, DEFAULT_PORT);
|
|
76
|
-
const commandTimeoutMs = positiveNumber(
|
|
77
|
-
options.commandTimeoutMs ?? process.env.BB_TIMEOUT,
|
|
78
|
-
DEFAULT_COMMAND_TIMEOUT_MS,
|
|
79
|
-
);
|
|
80
|
-
const waitForExtensionMs = positiveNumber(
|
|
81
|
-
options.waitForExtensionMs ?? process.env.BB_WAIT_EXT,
|
|
82
|
-
DEFAULT_WAIT_EXTENSION_MS,
|
|
83
|
-
);
|
|
84
|
-
const log = typeof options.log === 'function' ? options.log : (message) => console.error(message);
|
|
85
|
-
const startedAt = Date.now();
|
|
82
|
+
function parseMessage(raw) {
|
|
83
|
+
try {
|
|
84
|
+
return JSON.parse(raw.toString());
|
|
85
|
+
} catch {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** The leader owns the port, the extension socket, and the command queue. */
|
|
91
|
+
async function startLeader(config) {
|
|
92
|
+
const { host, port, commandTimeoutMs, waitForExtensionMs, log, startedAt } = config;
|
|
86
93
|
|
|
87
94
|
/** @type {import('ws').WebSocket | null} */
|
|
88
95
|
let extSocket = null;
|
|
@@ -92,7 +99,6 @@ export async function startBridge(options = {}) {
|
|
|
92
99
|
const pending = new Map();
|
|
93
100
|
|
|
94
101
|
const server = createServer((req, res) => {
|
|
95
|
-
// The bridge only speaks WebSocket; plain HTTP requests are not an API.
|
|
96
102
|
const body = JSON.stringify({ ok: false, error: 'not_found' });
|
|
97
103
|
res.writeHead(404, {
|
|
98
104
|
'content-type': 'application/json; charset=utf-8',
|
|
@@ -110,34 +116,80 @@ export async function startBridge(options = {}) {
|
|
|
110
116
|
return;
|
|
111
117
|
}
|
|
112
118
|
const url = new URL(req.url || '/', `http://${host}:${port}`);
|
|
113
|
-
if (url.pathname
|
|
114
|
-
|
|
119
|
+
if (url.pathname === '/ext') {
|
|
120
|
+
const origin = req.headers.origin || '';
|
|
121
|
+
if (origin && !origin.startsWith('chrome-extension://')) {
|
|
122
|
+
socket.destroy();
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
wss.handleUpgrade(req, socket, head, (ws) => {
|
|
126
|
+
ws.isProxy = false;
|
|
127
|
+
wss.emit('connection', ws, req);
|
|
128
|
+
});
|
|
115
129
|
return;
|
|
116
130
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
131
|
+
if (url.pathname === '/proxy') {
|
|
132
|
+
// Browsers always send Origin on WebSocket handshakes. Requiring its
|
|
133
|
+
// absence keeps a page from driving the browser through /proxy.
|
|
134
|
+
if (req.headers.origin) {
|
|
135
|
+
socket.destroy();
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
const remote = req.socket.remoteAddress || '';
|
|
139
|
+
if (!['127.0.0.1', '::1', '::ffff:127.0.0.1'].includes(remote)) {
|
|
140
|
+
socket.destroy();
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
wss.handleUpgrade(req, socket, head, (ws) => {
|
|
144
|
+
ws.isProxy = true;
|
|
145
|
+
wss.emit('connection', ws, req);
|
|
146
|
+
});
|
|
120
147
|
return;
|
|
121
148
|
}
|
|
122
|
-
|
|
149
|
+
socket.destroy();
|
|
123
150
|
});
|
|
124
151
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
152
|
+
function sendProxy(ws, payload) {
|
|
153
|
+
try {
|
|
154
|
+
ws.send(JSON.stringify(payload));
|
|
155
|
+
} catch {}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function handleProxy(ws) {
|
|
159
|
+
sendProxy(ws, { type: 'hello', bridge: 'pagerig', version: 1 });
|
|
160
|
+
ws.on('message', async (raw) => {
|
|
161
|
+
const message = parseMessage(raw);
|
|
162
|
+
if (!message?.id) return;
|
|
163
|
+
if (message.type === 'status') {
|
|
164
|
+
sendProxy(ws, { id: message.id, ok: true, data: status() });
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
if (message.type !== 'call') return;
|
|
168
|
+
try {
|
|
169
|
+
const data = await call(message.action, message.params || {}, message.timeoutMs);
|
|
170
|
+
sendProxy(ws, { id: message.id, ok: true, data });
|
|
171
|
+
} catch (error) {
|
|
172
|
+
sendProxy(ws, {
|
|
173
|
+
id: message.id,
|
|
174
|
+
ok: false,
|
|
175
|
+
error: String(error?.message || error),
|
|
176
|
+
errorCode: error?.code || 'COMMAND_FAILED',
|
|
177
|
+
errorHint: error?.hint,
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
ws.on('error', () => {});
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function handleExtension(ws) {
|
|
129
185
|
extSocket = ws;
|
|
130
186
|
everConnected = true;
|
|
131
187
|
ws.lastSeen = Date.now();
|
|
132
188
|
log('[bridge] extension connected');
|
|
133
189
|
|
|
134
190
|
ws.on('message', (raw) => {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
message = JSON.parse(raw.toString());
|
|
138
|
-
} catch {
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
191
|
+
const message = parseMessage(raw);
|
|
192
|
+
if (!message) return;
|
|
141
193
|
ws.lastSeen = Date.now();
|
|
142
194
|
if (message.type === 'hello') {
|
|
143
195
|
extensionVersion = message.version || null;
|
|
@@ -174,6 +226,11 @@ export async function startBridge(options = {}) {
|
|
|
174
226
|
});
|
|
175
227
|
|
|
176
228
|
ws.on('error', () => {});
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
wss.on('connection', (ws) => {
|
|
232
|
+
if (ws.isProxy) handleProxy(ws);
|
|
233
|
+
else handleExtension(ws);
|
|
177
234
|
});
|
|
178
235
|
|
|
179
236
|
// MV3 service workers are terminated when idle, and WebSocket messages do
|
|
@@ -279,8 +336,10 @@ export async function startBridge(options = {}) {
|
|
|
279
336
|
extensionVersion,
|
|
280
337
|
host,
|
|
281
338
|
port,
|
|
282
|
-
|
|
339
|
+
role: 'leader',
|
|
340
|
+
proxyClients: [...wss.clients].filter((ws) => ws.isProxy).length,
|
|
283
341
|
pending: pending.size,
|
|
342
|
+
uptimeMs: Date.now() - startedAt,
|
|
284
343
|
};
|
|
285
344
|
}
|
|
286
345
|
|
|
@@ -326,5 +385,283 @@ export async function startBridge(options = {}) {
|
|
|
326
385
|
server.listen(port, host);
|
|
327
386
|
});
|
|
328
387
|
|
|
329
|
-
return { call, status, close
|
|
388
|
+
return { role: 'leader', call, status, close };
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/** A follower forwards calls to the leader over a loopback WebSocket. */
|
|
392
|
+
async function startFollower(config, onDisconnect) {
|
|
393
|
+
const { host, port, commandTimeoutMs, waitForExtensionMs, log } = config;
|
|
394
|
+
const connectHost = host === '0.0.0.0' ? '127.0.0.1' : host;
|
|
395
|
+
const socket = new WebSocket(`ws://${connectHost}:${port}/proxy`);
|
|
396
|
+
const pending = new Map();
|
|
397
|
+
let closed = false;
|
|
398
|
+
let helloDone = false;
|
|
399
|
+
let helloResolve;
|
|
400
|
+
let helloReject;
|
|
401
|
+
const hello = new Promise((resolve, reject) => {
|
|
402
|
+
helloResolve = resolve;
|
|
403
|
+
helloReject = reject;
|
|
404
|
+
});
|
|
405
|
+
const helloTimer = setTimeout(() => {
|
|
406
|
+
helloReject(new Error('proxy_handshake_timeout'));
|
|
407
|
+
try {
|
|
408
|
+
socket.terminate();
|
|
409
|
+
} catch {}
|
|
410
|
+
}, PROXY_HANDSHAKE_MS);
|
|
411
|
+
helloTimer.unref?.();
|
|
412
|
+
|
|
413
|
+
socket.on('message', (raw) => {
|
|
414
|
+
const message = parseMessage(raw);
|
|
415
|
+
if (!message) return;
|
|
416
|
+
if (!helloDone) {
|
|
417
|
+
if (message.type === 'hello' && message.bridge === 'pagerig') {
|
|
418
|
+
helloDone = true;
|
|
419
|
+
clearTimeout(helloTimer);
|
|
420
|
+
helloResolve();
|
|
421
|
+
}
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
if (!message.id) return;
|
|
425
|
+
const entry = pending.get(message.id);
|
|
426
|
+
if (!entry) return;
|
|
427
|
+
pending.delete(message.id);
|
|
428
|
+
if (message.ok) {
|
|
429
|
+
entry.resolve(message.data);
|
|
430
|
+
} else {
|
|
431
|
+
const error = new Error(message.error || 'command_failed');
|
|
432
|
+
error.code = message.errorCode || 'COMMAND_FAILED';
|
|
433
|
+
error.hint = message.errorHint;
|
|
434
|
+
entry.reject(error);
|
|
435
|
+
}
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
socket.on('error', (error) => {
|
|
439
|
+
if (!helloDone) {
|
|
440
|
+
clearTimeout(helloTimer);
|
|
441
|
+
helloReject(error);
|
|
442
|
+
}
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
socket.on('close', () => {
|
|
446
|
+
clearTimeout(helloTimer);
|
|
447
|
+
if (!helloDone) {
|
|
448
|
+
helloReject(new Error('proxy_handshake_closed'));
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
for (const [id, entry] of pending) {
|
|
452
|
+
pending.delete(id);
|
|
453
|
+
const error = new Error('proxy_disconnected');
|
|
454
|
+
error.code = 'PROXY_DISCONNECTED';
|
|
455
|
+
error.hint = '与 leader bridge 的连接已断开,正在尝试恢复。';
|
|
456
|
+
entry.reject(error);
|
|
457
|
+
}
|
|
458
|
+
if (!closed) onDisconnect?.();
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
try {
|
|
462
|
+
await hello;
|
|
463
|
+
log(`[bridge] connected to leader at ws://${connectHost}:${port}/proxy`);
|
|
464
|
+
} catch (error) {
|
|
465
|
+
try {
|
|
466
|
+
socket.terminate();
|
|
467
|
+
} catch {}
|
|
468
|
+
throw proxyError(port, error);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
function request(payload, timeoutMs) {
|
|
472
|
+
return new Promise((resolve, reject) => {
|
|
473
|
+
if (closed) {
|
|
474
|
+
const error = disconnectedError('bridge_closed');
|
|
475
|
+
error.hint = 'PageRig MCP server 正在关闭。';
|
|
476
|
+
reject(error);
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
if (socket.readyState !== WebSocket.OPEN) {
|
|
480
|
+
const error = new Error('proxy_disconnected');
|
|
481
|
+
error.code = 'PROXY_DISCONNECTED';
|
|
482
|
+
reject(error);
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
const id = randomUUID();
|
|
486
|
+
const timer = setTimeout(() => {
|
|
487
|
+
pending.delete(id);
|
|
488
|
+
const error = new Error(`proxy_timeout after ${timeoutMs}ms`);
|
|
489
|
+
error.code = 'TIMEOUT';
|
|
490
|
+
reject(error);
|
|
491
|
+
}, timeoutMs);
|
|
492
|
+
pending.set(id, {
|
|
493
|
+
resolve: (value) => {
|
|
494
|
+
clearTimeout(timer);
|
|
495
|
+
resolve(value);
|
|
496
|
+
},
|
|
497
|
+
reject: (error) => {
|
|
498
|
+
clearTimeout(timer);
|
|
499
|
+
reject(error);
|
|
500
|
+
},
|
|
501
|
+
});
|
|
502
|
+
try {
|
|
503
|
+
socket.send(JSON.stringify({ ...payload, id }));
|
|
504
|
+
} catch (error) {
|
|
505
|
+
pending.delete(id);
|
|
506
|
+
clearTimeout(timer);
|
|
507
|
+
reject(error);
|
|
508
|
+
}
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
async function call(action, params = {}, timeoutMs = commandTimeoutMs) {
|
|
513
|
+
const budget = positiveNumber(timeoutMs, commandTimeoutMs) + waitForExtensionMs + 5000;
|
|
514
|
+
return request({ type: 'call', action, params, timeoutMs }, budget);
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
async function status() {
|
|
518
|
+
const data = await request({ type: 'status' }, PROXY_STATUS_TIMEOUT_MS);
|
|
519
|
+
return { ...data, role: 'follower' };
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
async function close() {
|
|
523
|
+
if (closed) return;
|
|
524
|
+
closed = true;
|
|
525
|
+
clearTimeout(helloTimer);
|
|
526
|
+
for (const [id, entry] of pending) {
|
|
527
|
+
pending.delete(id);
|
|
528
|
+
entry.reject(disconnectedError('bridge_closed'));
|
|
529
|
+
}
|
|
530
|
+
try {
|
|
531
|
+
socket.close();
|
|
532
|
+
} catch {}
|
|
533
|
+
if (socket.readyState === WebSocket.CLOSED) return;
|
|
534
|
+
await new Promise((resolve) => {
|
|
535
|
+
const timer = setTimeout(resolve, 500);
|
|
536
|
+
socket.once('close', () => {
|
|
537
|
+
clearTimeout(timer);
|
|
538
|
+
resolve();
|
|
539
|
+
});
|
|
540
|
+
});
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
return { role: 'follower', call, status, close };
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
/**
|
|
547
|
+
* Start a bridge, becoming the leader when the port is free and a follower
|
|
548
|
+
* otherwise. Followers retry and can take over if the leader disappears.
|
|
549
|
+
*
|
|
550
|
+
* @param {object} [options]
|
|
551
|
+
* @param {string} [options.host]
|
|
552
|
+
* @param {number} [options.port]
|
|
553
|
+
* @param {number} [options.commandTimeoutMs]
|
|
554
|
+
* @param {number} [options.waitForExtensionMs]
|
|
555
|
+
* @param {(message: string) => void} [options.log]
|
|
556
|
+
*/
|
|
557
|
+
export async function startBridge(options = {}) {
|
|
558
|
+
const host = options.host || process.env.BB_HOST || DEFAULT_HOST;
|
|
559
|
+
const port = positiveNumber(options.port ?? process.env.BB_PORT, DEFAULT_PORT);
|
|
560
|
+
const commandTimeoutMs = positiveNumber(
|
|
561
|
+
options.commandTimeoutMs ?? process.env.BB_TIMEOUT,
|
|
562
|
+
DEFAULT_COMMAND_TIMEOUT_MS,
|
|
563
|
+
);
|
|
564
|
+
const waitForExtensionMs = positiveNumber(
|
|
565
|
+
options.waitForExtensionMs ?? process.env.BB_WAIT_EXT,
|
|
566
|
+
DEFAULT_WAIT_EXTENSION_MS,
|
|
567
|
+
);
|
|
568
|
+
const log = typeof options.log === 'function' ? options.log : (message) => console.error(message);
|
|
569
|
+
const config = { host, port, commandTimeoutMs, waitForExtensionMs, log, startedAt: Date.now() };
|
|
570
|
+
|
|
571
|
+
let active = null;
|
|
572
|
+
let closed = false;
|
|
573
|
+
let recovering = null;
|
|
574
|
+
|
|
575
|
+
function fallbackStatus(role, error) {
|
|
576
|
+
return {
|
|
577
|
+
connected: false,
|
|
578
|
+
everConnected: false,
|
|
579
|
+
extensionVersion: null,
|
|
580
|
+
host,
|
|
581
|
+
port,
|
|
582
|
+
role,
|
|
583
|
+
pending: 0,
|
|
584
|
+
uptimeMs: Date.now() - config.startedAt,
|
|
585
|
+
...(error ? { error: String(error.message || error) } : {}),
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
async function recover() {
|
|
590
|
+
if (closed) return;
|
|
591
|
+
if (recovering) return recovering;
|
|
592
|
+
recovering = (async () => {
|
|
593
|
+
for (let attempt = 0; attempt < TAKEOVER_ATTEMPTS && !closed; attempt++) {
|
|
594
|
+
try {
|
|
595
|
+
active = await startLeader(config);
|
|
596
|
+
log('[bridge] failover: became leader');
|
|
597
|
+
return;
|
|
598
|
+
} catch (error) {
|
|
599
|
+
if (error.code !== 'PORT_IN_USE') {
|
|
600
|
+
log(`[bridge] failover leader failed: ${error.message || error}`);
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
try {
|
|
604
|
+
active = await startFollower(config, recover);
|
|
605
|
+
log('[bridge] failover: reconnected as follower');
|
|
606
|
+
return;
|
|
607
|
+
} catch {}
|
|
608
|
+
await sleep(TAKEOVER_INTERVAL_MS);
|
|
609
|
+
}
|
|
610
|
+
if (!closed) log('[bridge] failover: no bridge available');
|
|
611
|
+
active = null;
|
|
612
|
+
})().finally(() => {
|
|
613
|
+
recovering = null;
|
|
614
|
+
});
|
|
615
|
+
return recovering;
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
try {
|
|
619
|
+
active = await startLeader(config);
|
|
620
|
+
} catch (error) {
|
|
621
|
+
if (error.code !== 'PORT_IN_USE') throw error;
|
|
622
|
+
try {
|
|
623
|
+
active = await startFollower(config, recover);
|
|
624
|
+
} catch (followerError) {
|
|
625
|
+
throw portInUseError(port, followerError);
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
return {
|
|
630
|
+
get role() {
|
|
631
|
+
return active?.role || 'recovering';
|
|
632
|
+
},
|
|
633
|
+
|
|
634
|
+
async call(action, params = {}, timeoutMs) {
|
|
635
|
+
if (closed) {
|
|
636
|
+
const error = disconnectedError('bridge_closed');
|
|
637
|
+
error.hint = 'PageRig MCP server 正在关闭。';
|
|
638
|
+
throw error;
|
|
639
|
+
}
|
|
640
|
+
if (recovering) await recovering;
|
|
641
|
+
if (!active) {
|
|
642
|
+
const error = new Error('bridge_unavailable');
|
|
643
|
+
error.code = 'BRIDGE_UNAVAILABLE';
|
|
644
|
+
error.hint = 'PageRig bridge 不可用,请重启 MCP server。';
|
|
645
|
+
throw error;
|
|
646
|
+
}
|
|
647
|
+
return active.call(action, params, timeoutMs);
|
|
648
|
+
},
|
|
649
|
+
|
|
650
|
+
async status() {
|
|
651
|
+
if (closed) return fallbackStatus('closed');
|
|
652
|
+
if (recovering) await recovering.catch(() => {});
|
|
653
|
+
if (!active) return fallbackStatus('recovering');
|
|
654
|
+
try {
|
|
655
|
+
return await active.status();
|
|
656
|
+
} catch (error) {
|
|
657
|
+
return fallbackStatus(active.role, error);
|
|
658
|
+
}
|
|
659
|
+
},
|
|
660
|
+
|
|
661
|
+
async close() {
|
|
662
|
+
if (closed) return;
|
|
663
|
+
closed = true;
|
|
664
|
+
await active?.close();
|
|
665
|
+
},
|
|
666
|
+
};
|
|
330
667
|
}
|
package/mcp-server.mjs
CHANGED
|
@@ -10,9 +10,10 @@ import { writeFile } from 'node:fs/promises';
|
|
|
10
10
|
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
11
11
|
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
12
12
|
import { z } from 'zod';
|
|
13
|
+
import pkg from './package.json' with { type: 'json' };
|
|
13
14
|
import { startBridge } from './lib/bridge.mjs';
|
|
14
15
|
|
|
15
|
-
const server = new McpServer({ name: 'pagerig', version:
|
|
16
|
+
const server = new McpServer({ name: 'pagerig', version: pkg.version });
|
|
16
17
|
const port = Number(process.env.BB_PORT || 17321);
|
|
17
18
|
|
|
18
19
|
let bridge = null;
|
|
@@ -142,13 +143,14 @@ tool(
|
|
|
142
143
|
},
|
|
143
144
|
async () => {
|
|
144
145
|
const local = bridge
|
|
145
|
-
? bridge.status()
|
|
146
|
+
? await bridge.status()
|
|
146
147
|
: {
|
|
147
148
|
connected: false,
|
|
148
149
|
everConnected: false,
|
|
149
150
|
extensionVersion: null,
|
|
150
151
|
host: null,
|
|
151
152
|
port,
|
|
153
|
+
role: 'none',
|
|
152
154
|
pending: 0,
|
|
153
155
|
uptimeMs: 0,
|
|
154
156
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pagerig",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Local bridge that lets an agent read and drive the user's already-logged-in Chrome.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"node": ">=22"
|
|
9
9
|
},
|
|
10
10
|
"bin": {
|
|
11
|
-
"pagerig": "
|
|
11
|
+
"pagerig": "mcp-server.mjs"
|
|
12
12
|
},
|
|
13
13
|
"files": [
|
|
14
14
|
"mcp-server.mjs",
|