Repository navigation
Expand file tree
/
Copy pathfake-extension.mjs
More file actions
102 lines (94 loc) · 3.05 KB
/
Copy pathfake-extension.mjs
File metadata and controls
102 lines (94 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env node
// Stands in for the WebBrain extension so the MCP server can be driven without
// Chrome. Speaks the same frames as src/chrome/src/offscreen/cloud-bridge.js:
// the `hello` handshake and the answer to the pairing challenge, then canned
// replies to cloud_run / cloud_status / cloud_respond / cloud_abort. Anything
// else comes back as an error, which is how you check the server's failure path.
//
// cd mcp-server && node scripts/fake-extension.mjs [ws-url]
//
// Defaults to ws://127.0.0.1:17374/extension. Start the server first
// (npm start), then this, then point your MCP client at the server. It pairs
// with the same key the server uses (WEBBRAIN_BRIDGE_KEY, WEBBRAIN_BRIDGE_KEY_FILE
// or ~/.webbrain/bridge-key).
import WebSocket from "ws";
import { loadPairingKey, pairingProof, PAIRING_SCHEME } from "../dist/pairing.js";
const url = process.argv[2] || "ws://127.0.0.1:17374/extension";
const { key: pairingKey } = loadPairingKey();
const socket = new WebSocket(url);
let lastRunId = null;
function log(...args) {
console.log(new Date().toISOString().slice(11, 19), ...args);
}
socket.on("open", () => {
log(`connected to ${url}`);
socket.send(
JSON.stringify({
type: "hello",
client: "webbrain-extension",
protocolVersion: 2,
capabilities: ["saved_workflows_v1", "run_modes_v1", "scheduled_jobs_v1"],
status: { enabled: true },
}),
);
log("hello sent");
});
socket.on("message", (raw) => {
let msg;
try {
msg = JSON.parse(raw.toString());
} catch {
log("non-JSON frame:", raw.toString());
return;
}
if (msg.type === "pairing_challenge" || msg.type === "auth_challenge") {
socket.send(JSON.stringify({ type: "pairing_proof", scheme: PAIRING_SCHEME, proof: pairingProof(pairingKey, msg.nonce) }));
log("pairing proof sent");
return;
}
if (!msg.action) {
log("<-", JSON.stringify(msg));
return;
}
log(`<-${msg.action} id=${msg.id}`, JSON.stringify(msg.payload ?? {}));
const reply = respond(msg);
socket.send(JSON.stringify({ id: msg.id, ...reply }));
log(
`-> ${reply.ok ? "ok" : "err"} id=${msg.id}`,
JSON.stringify(reply.result ?? reply.error),
);
});
function respond(msg) {
switch (msg.action) {
case "cloud_run":
lastRunId = `run_${Date.now()}`;
return {
ok: true,
result: {
runId: lastRunId,
status: "running",
mode: msg.payload?.mode === "act" ? "act" : "ask",
},
};
case "cloud_status":
return {
ok: true,
result: {
runId: lastRunId,
status: "completed",
result: { summary: "fake run completed", finalUrl: "https://example.com/" },
},
};
case "cloud_respond":
case "cloud_abort":
return { ok: true, result: {} };
default:
return { ok: false, error: `fake extension cannot handle '${msg.action}'` };
}
}
socket.on("close", (code, reason) => {
log(`closed code=${code} reason=${reason.toString() || "-"}`);
});
socket.on("error", (error) => {
log("socket error:", error.message);
});