projectionlab-mcp 1.2.1 → 1.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/HOW-IT-WORKS.md +7 -6
- package/MANUAL-SETUP.md +8 -4
- package/README.md +1 -1
- package/REFERENCE.md +27 -11
- package/SETUP-FOR-AGENTS.md +4 -4
- package/dist/bootstrap.js +15 -3
- package/dist/bootstrap.js.map +1 -1
- package/dist/bridge.d.ts +34 -5
- package/dist/config.d.ts +0 -1
- package/dist/config.js +0 -2
- package/dist/config.js.map +1 -1
- package/dist/currentFinances.d.ts +60 -0
- package/dist/currentFinances.js +117 -2
- package/dist/currentFinances.js.map +1 -1
- package/dist/dataModel.d.ts +37 -8
- package/dist/dataModel.js +1138 -15
- package/dist/dataModel.js.map +1 -1
- package/dist/diff.d.ts +18 -10
- package/dist/diff.js +142 -20
- package/dist/diff.js.map +1 -1
- package/dist/editAccess.d.ts +15 -0
- package/dist/editAccess.js +29 -0
- package/dist/editAccess.js.map +1 -0
- package/dist/errors.d.ts +1 -1
- package/dist/errors.js.map +1 -1
- package/dist/extensionBridge.d.ts +99 -10
- package/dist/extensionBridge.js +445 -48
- package/dist/extensionBridge.js.map +1 -1
- package/dist/index.js +2 -3
- package/dist/index.js.map +1 -1
- package/dist/instructions.d.ts +1 -1
- package/dist/instructions.js +1 -1
- package/dist/layout.d.ts +68 -0
- package/dist/layout.js +122 -0
- package/dist/layout.js.map +1 -0
- package/dist/pathPatch.d.ts +41 -3
- package/dist/pathPatch.js +162 -14
- package/dist/pathPatch.js.map +1 -1
- package/dist/planSettings.d.ts +21 -2
- package/dist/planSettings.js +30 -6
- package/dist/planSettings.js.map +1 -1
- package/dist/planVersion.d.ts +13 -6
- package/dist/planVersion.js +33 -15
- package/dist/planVersion.js.map +1 -1
- package/dist/plans.d.ts +24 -3
- package/dist/plans.js +55 -2
- package/dist/plans.js.map +1 -1
- package/dist/realEstate.d.ts +20 -0
- package/dist/realEstate.js +57 -0
- package/dist/realEstate.js.map +1 -1
- package/dist/redact.d.ts +20 -0
- package/dist/redact.js +62 -0
- package/dist/redact.js.map +1 -1
- package/dist/resources/dataModel.d.ts +6 -2
- package/dist/resources/dataModel.js +86 -16
- package/dist/resources/dataModel.js.map +1 -1
- package/dist/resources/knowledge.d.ts +7 -1
- package/dist/resources/knowledge.js +262 -14
- package/dist/resources/knowledge.js.map +1 -1
- package/dist/resources/layoutResource.d.ts +19 -0
- package/dist/resources/layoutResource.js +43 -0
- package/dist/resources/layoutResource.js.map +1 -0
- package/dist/resources/planData.js +1 -1
- package/dist/resources/planData.js.map +1 -1
- package/dist/snapshot.d.ts +2 -1
- package/dist/snapshot.js +3 -1
- package/dist/snapshot.js.map +1 -1
- package/dist/summary.d.ts +39 -2
- package/dist/summary.js +148 -31
- package/dist/summary.js.map +1 -1
- package/dist/tools/bootstrap.js +2 -1
- package/dist/tools/bootstrap.js.map +1 -1
- package/dist/tools/clonePlan.js +10 -11
- package/dist/tools/clonePlan.js.map +1 -1
- package/dist/tools/connection.js +8 -8
- package/dist/tools/connection.js.map +1 -1
- package/dist/tools/deletePlan.js +9 -10
- package/dist/tools/deletePlan.js.map +1 -1
- package/dist/tools/diff.js +54 -15
- package/dist/tools/diff.js.map +1 -1
- package/dist/tools/export.js +5 -5
- package/dist/tools/export.js.map +1 -1
- package/dist/tools/getCurrentFinances.js +80 -24
- package/dist/tools/getCurrentFinances.js.map +1 -1
- package/dist/tools/getPlan.js +7 -1
- package/dist/tools/getPlan.js.map +1 -1
- package/dist/tools/getPlanSection.js +10 -3
- package/dist/tools/getPlanSection.js.map +1 -1
- package/dist/tools/getPlanSettings.js +16 -4
- package/dist/tools/getPlanSettings.js.map +1 -1
- package/dist/tools/restore.d.ts +4 -3
- package/dist/tools/restore.js +82 -44
- package/dist/tools/restore.js.map +1 -1
- package/dist/tools/schema.js +100 -22
- package/dist/tools/schema.js.map +1 -1
- package/dist/tools/snapshots.js +47 -21
- package/dist/tools/snapshots.js.map +1 -1
- package/dist/tools/summary.js +34 -1
- package/dist/tools/summary.js.map +1 -1
- package/dist/tools/updateAccount.js +69 -26
- package/dist/tools/updateAccount.js.map +1 -1
- package/dist/tools/updatePlanEvent.js +49 -29
- package/dist/tools/updatePlanEvent.js.map +1 -1
- package/dist/tools/updatePlanSection.js +10 -10
- package/dist/tools/updatePlanSection.js.map +1 -1
- package/dist/tools/updatePlanSettings.js +33 -15
- package/dist/tools/updatePlanSettings.js.map +1 -1
- package/dist/tools/writeGuard.d.ts +11 -0
- package/dist/tools/writeGuard.js +17 -0
- package/dist/tools/writeGuard.js.map +1 -0
- package/dist/types/projectionlab.d.ts +4 -0
- package/dist/types/wire.d.ts +81 -8
- package/dist/types/wire.js +30 -2
- package/dist/types/wire.js.map +1 -1
- package/package.json +1 -1
- package/skills/projectionlab/SKILL.md +222 -20
package/dist/extensionBridge.js
CHANGED
|
@@ -4,21 +4,36 @@ import { dirname, join } from "node:path";
|
|
|
4
4
|
import { WebSocketServer, WebSocket } from "ws";
|
|
5
5
|
import { buildBootstrapSnippet } from "./bootstrap.js";
|
|
6
6
|
import { config, USER_ENV_FILE } from "./config.js";
|
|
7
|
+
import { editsDisabledError } from "./editAccess.js";
|
|
7
8
|
import { ProjectionLabError } from "./errors.js";
|
|
9
|
+
import { contractFromVersion, detectLayout, exportSections, layoutForContract, layoutLabel, writeNotSupportedError, writeSupported, } from "./layout.js";
|
|
8
10
|
import { log } from "./logger.js";
|
|
11
|
+
import { planChanges, planConflictError } from "./planVersion.js";
|
|
9
12
|
import { VERSION } from "./version.js";
|
|
10
|
-
import { CLOSE_CODES, HEARTBEAT_INTERVAL_MS, MIN_EXTENSION_VERSION, PROTOCOL_VERSION, } from "./types/wire.js";
|
|
13
|
+
import { CLOSE_CODES, HEARTBEAT_INTERVAL_MS, MIN_EXTENSION_VERSION, PROTOCOL_VERSION, READ_METHODS, } from "./types/wire.js";
|
|
14
|
+
/** ProjectionLab's own app origins: the main app and v5 Early Access, where
|
|
15
|
+
* ProjectionLab moves accounts that join Early Access. */
|
|
16
|
+
const PROJECTIONLAB_ORIGINS = ["https://app.projectionlab.com", "https://ea.projectionlab.com"];
|
|
11
17
|
/** Origins allowed to open the WebSocket. Extensions plus the PL app itself
|
|
12
18
|
* (Tier 0 bootstrap snippet runs in page context, so the browser sends the
|
|
13
|
-
* app origin)
|
|
14
|
-
*
|
|
19
|
+
* app origin): either ProjectionLab origin, or the one PL_APP_URL names.
|
|
20
|
+
* Sockets with no Origin header (native clients, e.g. the fake test
|
|
21
|
+
* executor) are allowed; the hello token still gates them. */
|
|
15
22
|
function originAllowed(origin) {
|
|
16
23
|
if (!origin)
|
|
17
24
|
return true;
|
|
18
25
|
return (origin.startsWith("chrome-extension://") ||
|
|
19
26
|
origin.startsWith("moz-extension://") ||
|
|
27
|
+
PROJECTIONLAB_ORIGINS.includes(origin) ||
|
|
20
28
|
origin === new URL(config.appUrl).origin);
|
|
21
29
|
}
|
|
30
|
+
/** One connection-check error line: code, message, then suggestion. The
|
|
31
|
+
* message may already end in a period (the extension's messages do), so the
|
|
32
|
+
* separator adds one only when it is missing. */
|
|
33
|
+
function statusLine(err) {
|
|
34
|
+
const message = /[.!?]$/.test(err.message) ? err.message : `${err.message}.`;
|
|
35
|
+
return `${err.code}: ${message} ${err.suggestion}`;
|
|
36
|
+
}
|
|
22
37
|
const HELLO_TIMEOUT_MS = 5_000;
|
|
23
38
|
/** Bytes a connection may send before its hello is read. A hello frame is a
|
|
24
39
|
* few hundred bytes; the cap stops an unauthenticated peer from making the
|
|
@@ -26,11 +41,30 @@ const HELLO_TIMEOUT_MS = 5_000;
|
|
|
26
41
|
const MAX_PRE_HELLO_BYTES = 64 * 1024;
|
|
27
42
|
/** How often to retry the WebSocket port while another instance holds it. */
|
|
28
43
|
const PORT_RETRY_MS = 3_000;
|
|
44
|
+
/** After the holder yields, how long and how often to retry the bind. The
|
|
45
|
+
* extension reconnects about 2 seconds after an ordinary close, so the port
|
|
46
|
+
* must be bound well before then. */
|
|
47
|
+
const TAKEOVER_BIND_MS = 2_000;
|
|
48
|
+
const TAKEOVER_RETRY_MS = 100;
|
|
49
|
+
/** After taking the port over, how long an operation waits for the executor
|
|
50
|
+
* to reconnect (the extension retries at about 2, 6, then 14 seconds). */
|
|
51
|
+
const EXECUTOR_WAIT_MS = 15_000;
|
|
52
|
+
/** How long to wait for the holder to yield. The holder first finishes its
|
|
53
|
+
* queued operations, each of which can take up to REQUEST_TIMEOUT_MS. */
|
|
54
|
+
const YIELD_TIMEOUT_MS = 75_000;
|
|
55
|
+
/** How long the holder has to answer the WebSocket upgrade on a yield socket.
|
|
56
|
+
* A process that does not is not projectionlab-mcp. */
|
|
57
|
+
const YIELD_HANDSHAKE_MS = 5_000;
|
|
58
|
+
/** How many times one operation asks for the port. Another waiting instance
|
|
59
|
+
* can win the bind after a yield; the next round asks that instance. */
|
|
60
|
+
const TAKEOVER_ROUNDS = 3;
|
|
29
61
|
const EXTENSION_HELP = "Install the MCP Bridge for ProjectionLab extension from the Chrome Web Store " +
|
|
30
62
|
"(https://chromewebstore.google.com/detail/mcp-bridge-for-projection/njgjmcihabchffonldcgfjiocjalpoaf) " +
|
|
31
63
|
"or Firefox Add-ons (https://addons.mozilla.org/en-US/firefox/addon/mcp-bridge-for-projectionlab/); " +
|
|
32
64
|
"the store keeps it up to date. Then enter the token from " +
|
|
33
65
|
"~/.projectionlab-mcp/ws-token on its options page, and open https://app.projectionlab.com. " +
|
|
66
|
+
"An account on ProjectionLab v5 Early Access runs at https://ea.projectionlab.com instead: " +
|
|
67
|
+
"press Allow ea.projectionlab.com on the extension's options page, then open that site. " +
|
|
34
68
|
"Or call projectionlab_get_bootstrap and paste the snippet into the ProjectionLab tab's console. " +
|
|
35
69
|
"Install the extension from one source only: an unpacked copy alongside a store copy leaves the " +
|
|
36
70
|
"browser with two extensions competing for the server's single connection, and one of them stands by.";
|
|
@@ -46,6 +80,14 @@ function parseFrame(raw) {
|
|
|
46
80
|
return null;
|
|
47
81
|
}
|
|
48
82
|
}
|
|
83
|
+
function contractChangedError(message) {
|
|
84
|
+
return new ProjectionLabError("CONTRACT_CHANGED", message, "The ProjectionLab page changed between the version check and the write. Nothing was written. Run projectionlab_check_connection, then retry.");
|
|
85
|
+
}
|
|
86
|
+
/** The refusal for a write prepared from one export layout while the page
|
|
87
|
+
* now offers the other, as when two tabs run different ProjectionLab builds. */
|
|
88
|
+
function layoutChangedError(prepared, live) {
|
|
89
|
+
return contractChangedError(`This write was prepared from ${layoutLabel(prepared)} data, but the ProjectionLab page now offers ${layoutLabel(live)}`);
|
|
90
|
+
}
|
|
49
91
|
/** Best-effort chmod; a file the process does not own keeps its mode. */
|
|
50
92
|
function restrictMode(path, mode) {
|
|
51
93
|
try {
|
|
@@ -60,6 +102,11 @@ export class ExtensionBridge {
|
|
|
60
102
|
conn = null;
|
|
61
103
|
connExecutor = "";
|
|
62
104
|
connInstanceId = "";
|
|
105
|
+
/** Whether the connected executor honors RequestFrame.expectApiVersion. */
|
|
106
|
+
connGuardsApiVersion = false;
|
|
107
|
+
/** The contract from the last successful contract read (knownApiContract). */
|
|
108
|
+
lastContract;
|
|
109
|
+
connEditAccess = "unsupported";
|
|
63
110
|
pending = new Map();
|
|
64
111
|
nextId = 1;
|
|
65
112
|
heartbeat = null;
|
|
@@ -67,6 +114,16 @@ export class ExtensionBridge {
|
|
|
67
114
|
token;
|
|
68
115
|
portBusy = false;
|
|
69
116
|
retryTimer = null;
|
|
117
|
+
/** While non-zero, bind retries run every TAKEOVER_RETRY_MS until this time. */
|
|
118
|
+
fastBindUntil = 0;
|
|
119
|
+
/** Why the last yield request failed; "" when none has. */
|
|
120
|
+
yieldFailure = "";
|
|
121
|
+
/** The socket of a yield request in flight. */
|
|
122
|
+
yieldPeer = null;
|
|
123
|
+
/** When this instance last released the port to another instance. */
|
|
124
|
+
yieldedAt = 0;
|
|
125
|
+
/** Callbacks run whenever the connection or port state changes. */
|
|
126
|
+
stateWaiters = new Set();
|
|
70
127
|
closed = false;
|
|
71
128
|
// Serialize all operations and cache the last full export until a write
|
|
72
129
|
// invalidates it.
|
|
@@ -99,11 +156,26 @@ export class ExtensionBridge {
|
|
|
99
156
|
log.info("extension", `Generated WebSocket token at ${file}`);
|
|
100
157
|
return token;
|
|
101
158
|
}
|
|
159
|
+
notifyState() {
|
|
160
|
+
for (const waiter of [...this.stateWaiters])
|
|
161
|
+
waiter();
|
|
162
|
+
}
|
|
163
|
+
scheduleListen(delayMs) {
|
|
164
|
+
if (this.closed || this.retryTimer)
|
|
165
|
+
return;
|
|
166
|
+
this.retryTimer = setTimeout(() => {
|
|
167
|
+
this.retryTimer = null;
|
|
168
|
+
this.listen();
|
|
169
|
+
}, delayMs);
|
|
170
|
+
this.retryTimer.unref();
|
|
171
|
+
}
|
|
102
172
|
/** Binds the WebSocket port. While another instance (another agent on
|
|
103
|
-
* this machine) holds it, retries every PORT_RETRY_MS
|
|
104
|
-
* that instance exits;
|
|
173
|
+
* this machine) holds it, retries every PORT_RETRY_MS, so it takes over
|
|
174
|
+
* once that instance exits; an operation arriving meanwhile asks the
|
|
175
|
+
* holder to yield (requestPort). Executors reconnect on their own
|
|
176
|
+
* schedule. */
|
|
105
177
|
listen() {
|
|
106
|
-
if (this.closed)
|
|
178
|
+
if (this.closed || this.wss)
|
|
107
179
|
return;
|
|
108
180
|
const wss = new WebSocketServer({
|
|
109
181
|
host: "127.0.0.1",
|
|
@@ -115,23 +187,30 @@ export class ExtensionBridge {
|
|
|
115
187
|
log.info("extension", `Port ${config.wsPort} is free; took over the listener`);
|
|
116
188
|
}
|
|
117
189
|
this.portBusy = false;
|
|
190
|
+
this.fastBindUntil = 0;
|
|
191
|
+
this.yieldFailure = "";
|
|
192
|
+
// A yield request still out is moot once this instance holds the port.
|
|
193
|
+
this.yieldPeer?.terminate();
|
|
118
194
|
log.info("extension", `Listening on ws://127.0.0.1:${config.wsPort}`);
|
|
195
|
+
this.notifyState();
|
|
119
196
|
});
|
|
120
197
|
wss.on("error", (err) => {
|
|
121
198
|
if (err.code === "EADDRINUSE") {
|
|
122
199
|
if (!this.portBusy) {
|
|
123
200
|
log.warn("extension", `Port ${config.wsPort} is held by another process (usually another agent's ` +
|
|
124
|
-
`projectionlab-mcp). Retrying every ${PORT_RETRY_MS / 1000}s
|
|
201
|
+
`projectionlab-mcp). Retrying every ${PORT_RETRY_MS / 1000}s; an operation arriving ` +
|
|
202
|
+
"here asks the holder to hand the port over.");
|
|
125
203
|
}
|
|
126
204
|
this.portBusy = true;
|
|
127
205
|
if (this.wss === wss)
|
|
128
206
|
this.wss = null;
|
|
129
207
|
wss.close();
|
|
130
|
-
this.
|
|
131
|
-
|
|
132
|
-
this.
|
|
133
|
-
|
|
134
|
-
|
|
208
|
+
const fast = this.fastBindUntil > Date.now();
|
|
209
|
+
if (!fast && this.fastBindUntil !== 0) {
|
|
210
|
+
this.fastBindUntil = 0;
|
|
211
|
+
this.notifyState();
|
|
212
|
+
}
|
|
213
|
+
this.scheduleListen(fast ? TAKEOVER_RETRY_MS : PORT_RETRY_MS);
|
|
135
214
|
return;
|
|
136
215
|
}
|
|
137
216
|
log.error("extension", `WebSocket server error: ${err.message}`);
|
|
@@ -164,12 +243,14 @@ export class ExtensionBridge {
|
|
|
164
243
|
}
|
|
165
244
|
};
|
|
166
245
|
socket.on("data", countPreHelloBytes);
|
|
167
|
-
this.awaitHello(ws,
|
|
246
|
+
this.awaitHello(ws, wss, origin, () => socket.off("data", countPreHelloBytes));
|
|
168
247
|
});
|
|
169
248
|
}
|
|
170
|
-
/** New sockets must send a valid hello
|
|
171
|
-
* runs once the first message
|
|
172
|
-
|
|
249
|
+
/** New sockets must send a valid hello (or, from another server instance,
|
|
250
|
+
* a yield) before anything else. `helloRead` runs once the first message
|
|
251
|
+
* arrives, whatever it holds. */
|
|
252
|
+
awaitHello(ws, wss, rawOrigin, helloRead) {
|
|
253
|
+
const origin = rawOrigin ?? "(no origin)";
|
|
173
254
|
const timer = setTimeout(() => {
|
|
174
255
|
ws.close(CLOSE_CODES.AUTH_FAILED, "hello timeout");
|
|
175
256
|
}, HELLO_TIMEOUT_MS);
|
|
@@ -181,6 +262,17 @@ export class ExtensionBridge {
|
|
|
181
262
|
ws.close(CLOSE_CODES.AUTH_FAILED, "malformed hello");
|
|
182
263
|
return;
|
|
183
264
|
}
|
|
265
|
+
if (hello.type === "yield") {
|
|
266
|
+
// Only another server process sends a yield; a browser always sends
|
|
267
|
+
// an Origin header, so a socket carrying one is refused outright.
|
|
268
|
+
if (rawOrigin !== undefined || hello.token !== this.token) {
|
|
269
|
+
log.warn("extension", `Refused a yield request from ${origin}`);
|
|
270
|
+
ws.close(CLOSE_CODES.AUTH_FAILED, "yield refused");
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
this.yieldPort(ws);
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
184
276
|
if (hello.type !== "hello" || hello.token !== this.token) {
|
|
185
277
|
log.warn("extension", `Auth failed for connection from ${origin}`);
|
|
186
278
|
ws.close(CLOSE_CODES.AUTH_FAILED, "bad token");
|
|
@@ -191,6 +283,13 @@ export class ExtensionBridge {
|
|
|
191
283
|
ws.close(CLOSE_CODES.VERSION_MISMATCH, "protocol version mismatch");
|
|
192
284
|
return;
|
|
193
285
|
}
|
|
286
|
+
// A socket accepted before this instance released the port must not
|
|
287
|
+
// become its executor: the browser belongs to the new holder. The
|
|
288
|
+
// ordinary close sends the executor there.
|
|
289
|
+
if (this.wss !== wss) {
|
|
290
|
+
ws.close(CLOSE_CODES.HANDED_OVER, "port handed over");
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
194
293
|
// Only authenticated hellos reach the connection decision, so an
|
|
195
294
|
// unauthenticated client can neither displace an executor nor learn
|
|
196
295
|
// that one is connected.
|
|
@@ -199,7 +298,13 @@ export class ExtensionBridge {
|
|
|
199
298
|
hello.instanceId.length <= 64
|
|
200
299
|
? hello.instanceId
|
|
201
300
|
: "";
|
|
202
|
-
|
|
301
|
+
const guardsApiVersion = Array.isArray(hello.features) && hello.features.includes("expectApiVersion");
|
|
302
|
+
const editAccess = typeof hello.allowEdits === "boolean"
|
|
303
|
+
? hello.allowEdits
|
|
304
|
+
? "enabled"
|
|
305
|
+
: "disabled"
|
|
306
|
+
: "unsupported";
|
|
307
|
+
this.admit(ws, hello.executor, instanceId, hello.takeover === true, guardsApiVersion, editAccess);
|
|
203
308
|
});
|
|
204
309
|
}
|
|
205
310
|
/**
|
|
@@ -210,16 +315,16 @@ export class ExtensionBridge {
|
|
|
210
315
|
* socket is not open is dead and gets dropped first; the heartbeat is what
|
|
211
316
|
* clears a crashed incumbent that still looks open.
|
|
212
317
|
*/
|
|
213
|
-
admit(ws, executor, instanceId, takeover) {
|
|
318
|
+
admit(ws, executor, instanceId, takeover, guardsApiVersion, editAccess) {
|
|
214
319
|
const incumbent = this.conn;
|
|
215
320
|
if (!incumbent || incumbent.readyState !== WebSocket.OPEN) {
|
|
216
321
|
if (incumbent)
|
|
217
322
|
this.dropConnection("dropping a dead executor connection");
|
|
218
|
-
this.adopt(ws, executor, instanceId);
|
|
323
|
+
this.adopt(ws, executor, instanceId, guardsApiVersion, editAccess);
|
|
219
324
|
return;
|
|
220
325
|
}
|
|
221
326
|
if (takeover || (instanceId !== "" && instanceId === this.connInstanceId)) {
|
|
222
|
-
this.adopt(ws, executor, instanceId);
|
|
327
|
+
this.adopt(ws, executor, instanceId, guardsApiVersion, editAccess);
|
|
223
328
|
return;
|
|
224
329
|
}
|
|
225
330
|
log.info("extension", `Refusing ${executor} with BUSY: ${this.connExecutor} holds the connection`);
|
|
@@ -227,7 +332,7 @@ export class ExtensionBridge {
|
|
|
227
332
|
}
|
|
228
333
|
/** Promote an authenticated socket to the active connection. Replacing a
|
|
229
334
|
* live incumbent (takeover or same instance id) closes it with 4003. */
|
|
230
|
-
adopt(ws, executor, instanceId) {
|
|
335
|
+
adopt(ws, executor, instanceId, guardsApiVersion, editAccess) {
|
|
231
336
|
if (this.conn && this.conn !== ws) {
|
|
232
337
|
log.info("extension", `Replacing ${this.connExecutor} with ${executor}`);
|
|
233
338
|
this.conn.close(CLOSE_CODES.REPLACED, "replaced by newer connection");
|
|
@@ -243,7 +348,9 @@ export class ExtensionBridge {
|
|
|
243
348
|
this.conn = ws;
|
|
244
349
|
this.connExecutor = executor;
|
|
245
350
|
this.connInstanceId = instanceId;
|
|
246
|
-
|
|
351
|
+
this.connGuardsApiVersion = guardsApiVersion;
|
|
352
|
+
this.connEditAccess = editAccess;
|
|
353
|
+
log.info("extension", `Executor connected: ${executor} (plan edits ${editAccess})`);
|
|
247
354
|
ws.send(JSON.stringify({
|
|
248
355
|
type: "helloAck",
|
|
249
356
|
protocolVersion: PROTOCOL_VERSION,
|
|
@@ -261,6 +368,145 @@ export class ExtensionBridge {
|
|
|
261
368
|
log.warn("extension", `Socket error from ${executor}: ${err.message}`);
|
|
262
369
|
});
|
|
263
370
|
this.startHeartbeat();
|
|
371
|
+
this.notifyState();
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Hand the port to the server instance that sent a yield frame on `peer`.
|
|
375
|
+
* Runs on the operation chain, so an operation already sent to the
|
|
376
|
+
* browser finishes before the executor is dropped. The executor is closed
|
|
377
|
+
* with HANDED_OVER, an ordinary code, so it reconnects within seconds and
|
|
378
|
+
* reaches the new holder. This instance then waits for the port like any
|
|
379
|
+
* other, and asks for it back when its next operation arrives.
|
|
380
|
+
*/
|
|
381
|
+
yieldPort(peer) {
|
|
382
|
+
log.info("extension", "Another projectionlab-mcp instance asked for the port");
|
|
383
|
+
void this.serialize(async () => {
|
|
384
|
+
const wss = this.wss;
|
|
385
|
+
if (this.closed || !wss || peer.readyState !== WebSocket.OPEN) {
|
|
386
|
+
// This instance no longer holds the port (it yielded to someone
|
|
387
|
+
// else meanwhile, or is shutting down), so the port is released as
|
|
388
|
+
// far as this peer is concerned.
|
|
389
|
+
peer.close(CLOSE_CODES.YIELDED, "port released");
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
this.wss = null;
|
|
393
|
+
wss.close();
|
|
394
|
+
this.dropConnection("handed the connection to another projectionlab-mcp instance", CLOSE_CODES.HANDED_OVER);
|
|
395
|
+
this.portBusy = true;
|
|
396
|
+
this.yieldedAt = Date.now();
|
|
397
|
+
peer.close(CLOSE_CODES.YIELDED, "port released");
|
|
398
|
+
log.info("extension", `Released port ${config.wsPort} to another projectionlab-mcp instance`);
|
|
399
|
+
this.scheduleListen(PORT_RETRY_MS);
|
|
400
|
+
this.notifyState();
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* Ask the instance holding the port to yield it. Resolves "yielded" when
|
|
405
|
+
* it closes the socket with YIELDED, and "free" when nothing accepts the
|
|
406
|
+
* connection or the holder drops it (it just exited). Otherwise resolves
|
|
407
|
+
* the failure: "refused" when the holder answers 4001 (a server that
|
|
408
|
+
* predates yield frames, or one with a different token), "slow" when a
|
|
409
|
+
* holder that accepted the socket does not release the port within
|
|
410
|
+
* YIELD_TIMEOUT_MS, and "foreign" when the holder does not complete a
|
|
411
|
+
* WebSocket upgrade (a process that is not projectionlab-mcp).
|
|
412
|
+
*/
|
|
413
|
+
requestPort() {
|
|
414
|
+
return new Promise((resolve) => {
|
|
415
|
+
const peer = new WebSocket(`ws://127.0.0.1:${config.wsPort}`);
|
|
416
|
+
this.yieldPeer = peer;
|
|
417
|
+
let failure = "";
|
|
418
|
+
const giveUp = (why) => {
|
|
419
|
+
failure = why;
|
|
420
|
+
peer.terminate();
|
|
421
|
+
};
|
|
422
|
+
const handshakeTimer = setTimeout(() => giveUp("foreign"), YIELD_HANDSHAKE_MS);
|
|
423
|
+
const timer = setTimeout(() => giveUp("slow"), YIELD_TIMEOUT_MS);
|
|
424
|
+
handshakeTimer.unref();
|
|
425
|
+
timer.unref();
|
|
426
|
+
peer.on("open", () => {
|
|
427
|
+
clearTimeout(handshakeTimer);
|
|
428
|
+
peer.send(JSON.stringify({ type: "yield", token: this.token }));
|
|
429
|
+
});
|
|
430
|
+
peer.on("unexpected-response", () => giveUp("foreign"));
|
|
431
|
+
peer.on("error", (err) => {
|
|
432
|
+
log.debug("extension", `Yield request socket error: ${err.message}`);
|
|
433
|
+
});
|
|
434
|
+
peer.on("close", (code) => {
|
|
435
|
+
clearTimeout(handshakeTimer);
|
|
436
|
+
clearTimeout(timer);
|
|
437
|
+
if (this.yieldPeer === peer)
|
|
438
|
+
this.yieldPeer = null;
|
|
439
|
+
if (code === CLOSE_CODES.YIELDED)
|
|
440
|
+
resolve("yielded");
|
|
441
|
+
else if (code === CLOSE_CODES.AUTH_FAILED)
|
|
442
|
+
resolve("refused");
|
|
443
|
+
else
|
|
444
|
+
resolve(failure || "free");
|
|
445
|
+
});
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
/** Resolves once `done()` holds, rechecked on every state change, or
|
|
449
|
+
* after `timeoutMs`. */
|
|
450
|
+
waitForState(done, timeoutMs) {
|
|
451
|
+
return new Promise((resolve) => {
|
|
452
|
+
const finish = () => {
|
|
453
|
+
clearTimeout(timer);
|
|
454
|
+
this.stateWaiters.delete(check);
|
|
455
|
+
resolve();
|
|
456
|
+
};
|
|
457
|
+
const check = () => {
|
|
458
|
+
if (done())
|
|
459
|
+
finish();
|
|
460
|
+
};
|
|
461
|
+
const timer = setTimeout(finish, timeoutMs);
|
|
462
|
+
this.stateWaiters.add(check);
|
|
463
|
+
check();
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* The connected executor's socket. While another instance holds the port,
|
|
468
|
+
* asks it to yield, binds the port, and waits for the executor to
|
|
469
|
+
* reconnect here. When another waiting instance wins the bind instead, the
|
|
470
|
+
* next round asks that instance. Runs on the operation chain, so
|
|
471
|
+
* concurrent operations send one yield between them.
|
|
472
|
+
*/
|
|
473
|
+
async executorSocket() {
|
|
474
|
+
for (let round = 0; round < TAKEOVER_ROUNDS && !this.conn && this.portBusy && !this.closed; round++) {
|
|
475
|
+
// Right after yielding, the new holder may not have bound the port
|
|
476
|
+
// yet; asking now would find it free and race that instance for it.
|
|
477
|
+
// Waiting out its bind window means the request reaches the new
|
|
478
|
+
// holder and becomes an ordinary yield.
|
|
479
|
+
const sinceYield = Date.now() - this.yieldedAt;
|
|
480
|
+
if (sinceYield < TAKEOVER_BIND_MS) {
|
|
481
|
+
await new Promise((r) => setTimeout(r, TAKEOVER_BIND_MS - sinceYield));
|
|
482
|
+
}
|
|
483
|
+
// The bind retry may have taken the port during that wait; a yield
|
|
484
|
+
// sent then would reach this instance's own listener.
|
|
485
|
+
if (this.portBusy && !this.closed) {
|
|
486
|
+
const outcome = await this.requestPort();
|
|
487
|
+
if (outcome !== "yielded" && outcome !== "free") {
|
|
488
|
+
this.yieldFailure = outcome;
|
|
489
|
+
break;
|
|
490
|
+
}
|
|
491
|
+
if (this.portBusy && !this.closed) {
|
|
492
|
+
log.info("extension", `Taking over port ${config.wsPort}`);
|
|
493
|
+
this.yieldFailure = "";
|
|
494
|
+
this.fastBindUntil = Date.now() + TAKEOVER_BIND_MS;
|
|
495
|
+
if (this.retryTimer) {
|
|
496
|
+
clearTimeout(this.retryTimer);
|
|
497
|
+
this.retryTimer = null;
|
|
498
|
+
}
|
|
499
|
+
// An attempt in flight retries on the fast schedule from its error
|
|
500
|
+
// handler; otherwise start one now.
|
|
501
|
+
this.listen();
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
await this.waitForState(() => this.conn !== null || this.closed || (this.portBusy && this.fastBindUntil === 0), EXECUTOR_WAIT_MS);
|
|
505
|
+
}
|
|
506
|
+
if (!this.conn) {
|
|
507
|
+
throw new ProjectionLabError("EXTENSION_NOT_CONNECTED", "No browser-side executor is connected", this.notConnectedReason());
|
|
508
|
+
}
|
|
509
|
+
return this.conn;
|
|
264
510
|
}
|
|
265
511
|
onFrame(ws, raw) {
|
|
266
512
|
if (this.conn !== ws)
|
|
@@ -278,6 +524,13 @@ export class ExtensionBridge {
|
|
|
278
524
|
this.settle(frame);
|
|
279
525
|
return;
|
|
280
526
|
}
|
|
527
|
+
if (frame.type === "settings" && typeof frame.allowEdits === "boolean") {
|
|
528
|
+
const access = frame.allowEdits ? "enabled" : "disabled";
|
|
529
|
+
if (access !== this.connEditAccess) {
|
|
530
|
+
log.info("extension", `Plan edits ${access} by ${this.connExecutor}`);
|
|
531
|
+
}
|
|
532
|
+
this.connEditAccess = access;
|
|
533
|
+
}
|
|
281
534
|
}
|
|
282
535
|
settle(frame) {
|
|
283
536
|
const pending = this.pending.get(frame.id);
|
|
@@ -297,13 +550,22 @@ export class ExtensionBridge {
|
|
|
297
550
|
wireError(code, message) {
|
|
298
551
|
switch (code) {
|
|
299
552
|
case "NO_TAB":
|
|
300
|
-
return new ProjectionLabError("PAGE_NOT_LOADED", `No ProjectionLab tab: ${message}`, "Open https://app.projectionlab.com in the browser running the bridge extension"
|
|
553
|
+
return new ProjectionLabError("PAGE_NOT_LOADED", `No ProjectionLab tab: ${message}`, "Open https://app.projectionlab.com in the browser running the bridge extension. " +
|
|
554
|
+
"On ProjectionLab v5 Early Access, open https://ea.projectionlab.com and press " +
|
|
555
|
+
"Allow ea.projectionlab.com on the extension's options page.");
|
|
301
556
|
case "API_NOT_AVAILABLE":
|
|
302
557
|
return new ProjectionLabError("API_NOT_AVAILABLE", message, "Make sure you are logged in to ProjectionLab and plugins are enabled in Account Settings");
|
|
303
558
|
case "API_KEY_INVALID":
|
|
304
559
|
return new ProjectionLabError("API_KEY_INVALID", message, "Check that PL_API_KEY matches your key in Account Settings > Plugins");
|
|
305
560
|
case "TIMEOUT":
|
|
306
561
|
return new ProjectionLabError("TIMEOUT", message, "The ProjectionLab page did not respond in time - check the tab and try again");
|
|
562
|
+
case "CONTRACT_CHANGED":
|
|
563
|
+
return contractChangedError(message);
|
|
564
|
+
case "EDITS_DISABLED":
|
|
565
|
+
// The browser refused the write; the switch was turned off before
|
|
566
|
+
// the settings frame saying so arrived.
|
|
567
|
+
this.connEditAccess = "disabled";
|
|
568
|
+
return editsDisabledError("disabled");
|
|
307
569
|
default:
|
|
308
570
|
return new ProjectionLabError("UNKNOWN", message, "The Plugin API rejected the operation. Verify the payload shape against a fresh export and try again.");
|
|
309
571
|
}
|
|
@@ -333,11 +595,16 @@ export class ExtensionBridge {
|
|
|
333
595
|
}
|
|
334
596
|
this.pending.clear();
|
|
335
597
|
}
|
|
336
|
-
|
|
598
|
+
/** Forget the executor. With `closeCode`, the socket gets a close frame
|
|
599
|
+
* carrying that code; otherwise it is terminated. */
|
|
600
|
+
dropConnection(reason, closeCode) {
|
|
337
601
|
// Close the socket if it's still live - wss.close() waits for all
|
|
338
602
|
// clients, so leaving it open would hang shutdown.
|
|
339
603
|
try {
|
|
340
|
-
|
|
604
|
+
if (closeCode === undefined)
|
|
605
|
+
this.conn?.terminate();
|
|
606
|
+
else
|
|
607
|
+
this.conn?.close(closeCode, reason);
|
|
341
608
|
}
|
|
342
609
|
catch {
|
|
343
610
|
// Already gone.
|
|
@@ -345,6 +612,8 @@ export class ExtensionBridge {
|
|
|
345
612
|
this.conn = null;
|
|
346
613
|
this.connExecutor = "";
|
|
347
614
|
this.connInstanceId = "";
|
|
615
|
+
this.connGuardsApiVersion = false;
|
|
616
|
+
this.connEditAccess = "unsupported";
|
|
348
617
|
this.awaitingPong = false;
|
|
349
618
|
this.exportCache = null;
|
|
350
619
|
if (this.heartbeat) {
|
|
@@ -352,6 +621,7 @@ export class ExtensionBridge {
|
|
|
352
621
|
this.heartbeat = null;
|
|
353
622
|
}
|
|
354
623
|
this.abandonRequests(reason);
|
|
624
|
+
this.notifyState();
|
|
355
625
|
}
|
|
356
626
|
serialize(fn) {
|
|
357
627
|
const next = this.opChain.then(fn, fn);
|
|
@@ -360,11 +630,26 @@ export class ExtensionBridge {
|
|
|
360
630
|
}
|
|
361
631
|
/** Why no executor can be reached, with the fix, for error messages. */
|
|
362
632
|
notConnectedReason() {
|
|
633
|
+
if (this.portBusy && this.yieldFailure === "refused") {
|
|
634
|
+
return (`Port ${config.wsPort} is held by another process that did not hand it over. Usually that ` +
|
|
635
|
+
"is an older projectionlab-mcp started by another agent or session on this machine (older " +
|
|
636
|
+
"versions cannot hand the port over), or one started with a different PL_WS_TOKEN. Close " +
|
|
637
|
+
"that session or quit its MCP host (or update its projectionlab-mcp); this server takes " +
|
|
638
|
+
"over within seconds of the port coming free.");
|
|
639
|
+
}
|
|
640
|
+
if (this.portBusy && this.yieldFailure === "slow") {
|
|
641
|
+
return (`Port ${config.wsPort} is held by another projectionlab-mcp instance that is still busy ` +
|
|
642
|
+
"with its own operations and did not release the port in time. Try again shortly.");
|
|
643
|
+
}
|
|
644
|
+
if (this.portBusy && this.yieldFailure === "foreign") {
|
|
645
|
+
return (`Port ${config.wsPort} is held by a program that is not projectionlab-mcp. Stop that ` +
|
|
646
|
+
"program, or set PL_WS_PORT to a free port for this server and enter the same port on " +
|
|
647
|
+
"the extension's options page.");
|
|
648
|
+
}
|
|
363
649
|
if (this.portBusy) {
|
|
364
|
-
return (`Port ${config.wsPort} is held by another projectionlab-mcp instance
|
|
365
|
-
"
|
|
366
|
-
"
|
|
367
|
-
"once, set PL_WS_PORT to a free port here and point the extension at it.");
|
|
650
|
+
return (`Port ${config.wsPort} is held by another projectionlab-mcp instance, and handing it over ` +
|
|
651
|
+
"to this one did not complete. Try again; if it keeps failing, close the other agent's " +
|
|
652
|
+
"session or quit its MCP host.");
|
|
368
653
|
}
|
|
369
654
|
return `No browser-side executor is connected to ws://127.0.0.1:${config.wsPort}. ${EXTENSION_HELP}`;
|
|
370
655
|
}
|
|
@@ -375,13 +660,32 @@ export class ExtensionBridge {
|
|
|
375
660
|
}
|
|
376
661
|
return config.apiKey;
|
|
377
662
|
}
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
663
|
+
/**
|
|
664
|
+
* Send one request to the executor. Call only from the operation chain.
|
|
665
|
+
*
|
|
666
|
+
* A write passes the guard from assertWriteSupported, and is sent only on
|
|
667
|
+
* the socket the guard's version was read from: a write that would reach
|
|
668
|
+
* another executor is refused, since that executor could be on another
|
|
669
|
+
* ProjectionLab version, and a plan write's conflict check was read from
|
|
670
|
+
* the first one. An executor that honors expectApiVersion also checks the
|
|
671
|
+
* page's version in the same page turn as the call.
|
|
672
|
+
*/
|
|
673
|
+
async request(method, params, timeoutMs = REQUEST_TIMEOUT_MS, guard) {
|
|
674
|
+
const conn = await this.executorSocket();
|
|
675
|
+
// Every write passes here, inside the operation chain, so no tool can
|
|
676
|
+
// send one to an executor whose edits are not enabled.
|
|
677
|
+
if (!READ_METHODS.includes(method) && this.connEditAccess !== "enabled") {
|
|
678
|
+
throw editsDisabledError(this.connEditAccess);
|
|
382
679
|
}
|
|
383
680
|
const id = String(this.nextId++);
|
|
384
681
|
const frame = { type: "request", id, method, params };
|
|
682
|
+
if (guard) {
|
|
683
|
+
if (conn !== guard.socket) {
|
|
684
|
+
throw contractChangedError("The browser executor changed after the Plugin API version was checked");
|
|
685
|
+
}
|
|
686
|
+
if (this.connGuardsApiVersion)
|
|
687
|
+
frame.expectApiVersion = guard.apiVersion;
|
|
688
|
+
}
|
|
385
689
|
return new Promise((resolve, reject) => {
|
|
386
690
|
const timer = setTimeout(() => {
|
|
387
691
|
this.pending.delete(id);
|
|
@@ -405,6 +709,7 @@ export class ExtensionBridge {
|
|
|
405
709
|
apiAvailable: false,
|
|
406
710
|
apiKeyValid: false,
|
|
407
711
|
errors: [],
|
|
712
|
+
writesEnabled: false,
|
|
408
713
|
};
|
|
409
714
|
return this.serialize(async () => {
|
|
410
715
|
if (!config.apiKey) {
|
|
@@ -413,15 +718,19 @@ export class ExtensionBridge {
|
|
|
413
718
|
}
|
|
414
719
|
catch (err) {
|
|
415
720
|
const e = err;
|
|
416
|
-
result.errors.push(
|
|
721
|
+
result.errors.push(statusLine(e));
|
|
417
722
|
}
|
|
418
723
|
}
|
|
419
|
-
|
|
724
|
+
try {
|
|
725
|
+
await this.executorSocket();
|
|
726
|
+
}
|
|
727
|
+
catch {
|
|
420
728
|
result.errors.push(`EXTENSION_NOT_CONNECTED: ${this.notConnectedReason()}`);
|
|
421
729
|
return result;
|
|
422
730
|
}
|
|
423
731
|
result.browser = true;
|
|
424
732
|
result.executor = this.connExecutor || undefined;
|
|
733
|
+
result.writesEnabled = this.connEditAccess === "enabled";
|
|
425
734
|
try {
|
|
426
735
|
await this.request("waitForApi", [config.apiReadyTimeoutMs], config.apiReadyTimeoutMs + 5_000);
|
|
427
736
|
result.pageLoaded = true;
|
|
@@ -432,10 +741,16 @@ export class ExtensionBridge {
|
|
|
432
741
|
result.pageLoaded = true;
|
|
433
742
|
}
|
|
434
743
|
result.errors.push(err instanceof ProjectionLabError
|
|
435
|
-
?
|
|
744
|
+
? statusLine(err)
|
|
436
745
|
: String(err));
|
|
437
746
|
return result;
|
|
438
747
|
}
|
|
748
|
+
try {
|
|
749
|
+
result.apiContract = await this.fetchContract();
|
|
750
|
+
}
|
|
751
|
+
catch (err) {
|
|
752
|
+
result.errors.push(err instanceof ProjectionLabError ? statusLine(err) : String(err));
|
|
753
|
+
}
|
|
439
754
|
if (!config.apiKey)
|
|
440
755
|
return result;
|
|
441
756
|
try {
|
|
@@ -448,6 +763,64 @@ export class ExtensionBridge {
|
|
|
448
763
|
return result;
|
|
449
764
|
});
|
|
450
765
|
}
|
|
766
|
+
async apiContract() {
|
|
767
|
+
return this.serialize(() => this.fetchContract());
|
|
768
|
+
}
|
|
769
|
+
knownApiContract() {
|
|
770
|
+
return this.lastContract;
|
|
771
|
+
}
|
|
772
|
+
/**
|
|
773
|
+
* The Plugin API contract the connected page offers, read fresh on every
|
|
774
|
+
* call so a page reloaded onto another ProjectionLab version is never
|
|
775
|
+
* written to under the old contract. Call only from the operation chain.
|
|
776
|
+
*
|
|
777
|
+
* An executor that predates the apiVersion meta-operation rejects it; the
|
|
778
|
+
* API's member list then decides. A page whose API has a `version` member
|
|
779
|
+
* the executor cannot report is refused rather than assumed to be contract 1.
|
|
780
|
+
*/
|
|
781
|
+
async fetchContract() {
|
|
782
|
+
return (await this.readContract()).contract;
|
|
783
|
+
}
|
|
784
|
+
/** fetchContract, with the raw version and the socket it was read on. */
|
|
785
|
+
async readContract() {
|
|
786
|
+
const socket = await this.executorSocket();
|
|
787
|
+
let version;
|
|
788
|
+
try {
|
|
789
|
+
version = (await this.request("apiVersion", [])) ?? null;
|
|
790
|
+
}
|
|
791
|
+
catch (err) {
|
|
792
|
+
if (!(err instanceof ProjectionLabError) || err.code !== "UNKNOWN")
|
|
793
|
+
throw err;
|
|
794
|
+
const members = (await this.request("describeApi", []));
|
|
795
|
+
if (members && typeof members === "object" && "version" in members) {
|
|
796
|
+
throw new ProjectionLabError("EXECUTOR_OUTDATED", "The ProjectionLab page reports a Plugin API version that this browser executor cannot read", "Update the MCP Bridge for ProjectionLab extension, or paste a fresh snippet from projectionlab_get_bootstrap. Nothing was written.");
|
|
797
|
+
}
|
|
798
|
+
this.lastContract = 1;
|
|
799
|
+
return { contract: 1, apiVersion: null, socket };
|
|
800
|
+
}
|
|
801
|
+
this.lastContract = contractFromVersion(version);
|
|
802
|
+
return { contract: this.lastContract, apiVersion: version, socket };
|
|
803
|
+
}
|
|
804
|
+
/**
|
|
805
|
+
* Refuses a write the page's contract does not support yet, and returns
|
|
806
|
+
* the guard to pass to the write's request. Call only from the operation
|
|
807
|
+
* chain, in the same entry as the write it guards.
|
|
808
|
+
*/
|
|
809
|
+
async assertWriteSupported(method, layout) {
|
|
810
|
+
const { contract, apiVersion, socket } = await this.readContract();
|
|
811
|
+
if (!writeSupported(contract, method))
|
|
812
|
+
throw writeNotSupportedError(method);
|
|
813
|
+
const live = layoutForContract(contract);
|
|
814
|
+
if (live !== layout)
|
|
815
|
+
throw layoutChangedError(layout, live);
|
|
816
|
+
return { apiVersion, socket };
|
|
817
|
+
}
|
|
818
|
+
async editAccess() {
|
|
819
|
+
return this.serialize(async () => {
|
|
820
|
+
await this.executorSocket();
|
|
821
|
+
return this.connEditAccess;
|
|
822
|
+
});
|
|
823
|
+
}
|
|
451
824
|
async describePluginApi() {
|
|
452
825
|
return this.serialize(async () => {
|
|
453
826
|
return (await this.request("describeApi", []));
|
|
@@ -481,34 +854,56 @@ export class ExtensionBridge {
|
|
|
481
854
|
return data;
|
|
482
855
|
});
|
|
483
856
|
}
|
|
484
|
-
async updateAccount(accountId, data, force) {
|
|
857
|
+
async updateAccount(accountId, data, force, layout) {
|
|
485
858
|
await this.serialize(async () => {
|
|
486
859
|
log.info("extension", `Calling updateAccount for ${accountId}${force ? " (force)" : ""}`);
|
|
487
860
|
const opts = { key: this.requireApiKey() };
|
|
488
861
|
if (force)
|
|
489
862
|
opts.force = "true";
|
|
490
|
-
await this.
|
|
863
|
+
const guard = await this.assertWriteSupported("updateAccount", layout);
|
|
864
|
+
await this.request("updateAccount", [accountId, data, opts], REQUEST_TIMEOUT_MS, guard);
|
|
491
865
|
});
|
|
492
866
|
this.invalidateCache();
|
|
493
867
|
}
|
|
494
|
-
async restoreCall(method, payload) {
|
|
868
|
+
async restoreCall(method, payload, layout) {
|
|
495
869
|
await this.serialize(async () => {
|
|
870
|
+
const key = this.requireApiKey();
|
|
871
|
+
const guard = await this.assertWriteSupported(method, layout);
|
|
496
872
|
log.info("extension", `Calling ${method}`);
|
|
497
|
-
await this.request(method, [payload, { key
|
|
873
|
+
await this.request(method, [payload, { key }], REQUEST_TIMEOUT_MS, guard);
|
|
498
874
|
});
|
|
499
875
|
this.invalidateCache();
|
|
500
876
|
}
|
|
501
|
-
async restoreCurrentFinances(state) {
|
|
502
|
-
await this.restoreCall("restoreCurrentFinances", state);
|
|
877
|
+
async restoreCurrentFinances(state, layout) {
|
|
878
|
+
await this.restoreCall("restoreCurrentFinances", state, layout);
|
|
879
|
+
}
|
|
880
|
+
async restorePlans(plans, layout) {
|
|
881
|
+
await this.restoreCall("restorePlans", plans, layout);
|
|
503
882
|
}
|
|
504
|
-
|
|
505
|
-
|
|
883
|
+
/** Re-reads the plans and writes `plans` as one entry on the operation
|
|
884
|
+
* chain, so neither a hand-over to another instance nor another operation
|
|
885
|
+
* of this one can run between the conflict check and the write. */
|
|
886
|
+
async restorePlansIfUnchanged(read, plans, layout) {
|
|
887
|
+
await this.serialize(async () => {
|
|
888
|
+
const key = this.requireApiKey();
|
|
889
|
+
const guard = await this.assertWriteSupported("restorePlans", layout);
|
|
890
|
+
const data = await this.request("exportData", [{ key }]);
|
|
891
|
+
const live = detectLayout(data);
|
|
892
|
+
if (live !== layout)
|
|
893
|
+
throw layoutChangedError(layout, live);
|
|
894
|
+
const changes = planChanges(read, exportSections(data).plans);
|
|
895
|
+
if (changes.length > 0)
|
|
896
|
+
throw planConflictError(changes);
|
|
897
|
+
log.info("extension", "Calling restorePlans");
|
|
898
|
+
await this.request("restorePlans", [plans, { key }], REQUEST_TIMEOUT_MS, guard);
|
|
899
|
+
});
|
|
900
|
+
this.invalidateCache();
|
|
506
901
|
}
|
|
507
|
-
async restoreProgress(progress) {
|
|
508
|
-
await this.restoreCall("restoreProgress", progress);
|
|
902
|
+
async restoreProgress(progress, layout) {
|
|
903
|
+
await this.restoreCall("restoreProgress", progress, layout);
|
|
509
904
|
}
|
|
510
|
-
async restoreSettings(settings) {
|
|
511
|
-
await this.restoreCall("restoreSettings", settings);
|
|
905
|
+
async restoreSettings(settings, layout) {
|
|
906
|
+
await this.restoreCall("restoreSettings", settings, layout);
|
|
512
907
|
}
|
|
513
908
|
async close() {
|
|
514
909
|
this.closed = true;
|
|
@@ -516,6 +911,8 @@ export class ExtensionBridge {
|
|
|
516
911
|
clearTimeout(this.retryTimer);
|
|
517
912
|
this.retryTimer = null;
|
|
518
913
|
}
|
|
914
|
+
this.notifyState();
|
|
915
|
+
this.yieldPeer?.terminate();
|
|
519
916
|
this.dropConnection("server shutting down");
|
|
520
917
|
if (this.wss) {
|
|
521
918
|
log.info("extension", "Closing WebSocket server");
|