@markasso/mcp 0.10.0 → 0.10.2
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/dist/markasso-mcp.js +465 -89
- package/package.json +1 -1
package/dist/markasso-mcp.js
CHANGED
|
@@ -5747,7 +5747,7 @@ function escapeXml(str) {
|
|
|
5747
5747
|
}
|
|
5748
5748
|
//#endregion
|
|
5749
5749
|
//#region package.json
|
|
5750
|
-
var version = "0.10.
|
|
5750
|
+
var version = "0.10.2";
|
|
5751
5751
|
//#endregion
|
|
5752
5752
|
//#region ../src/core/layout.ts
|
|
5753
5753
|
/** Space kept on both sides of a label between ranks. */
|
|
@@ -9105,6 +9105,79 @@ function isValidSessionCommand(value) {
|
|
|
9105
9105
|
if (!Object.hasOwn(CHECKS, value.type)) return false;
|
|
9106
9106
|
return CHECKS[value.type](value);
|
|
9107
9107
|
}
|
|
9108
|
+
/**
|
|
9109
|
+
* How late the timeout check may run before it counts as a wake from sleep
|
|
9110
|
+
* (timers do not run while a machine or a background tab sleeps, and run
|
|
9111
|
+
* late in a throttled one): the old ping then proves nothing, and a fresh
|
|
9112
|
+
* one gets a full timeout. Only a check that runs on time can give up.
|
|
9113
|
+
*/
|
|
9114
|
+
var SLEEP_SLACK_MS = 5e3;
|
|
9115
|
+
/** Pings one socket on a period and reports when it stops answering. */
|
|
9116
|
+
var Heartbeat = class {
|
|
9117
|
+
options;
|
|
9118
|
+
intervalMs;
|
|
9119
|
+
timeoutMs;
|
|
9120
|
+
now;
|
|
9121
|
+
nextPing = null;
|
|
9122
|
+
deadline = null;
|
|
9123
|
+
pingSentAt = 0;
|
|
9124
|
+
stopped = true;
|
|
9125
|
+
constructor(options) {
|
|
9126
|
+
this.options = options;
|
|
9127
|
+
this.intervalMs = options.intervalMs ?? 2e4;
|
|
9128
|
+
this.timeoutMs = options.timeoutMs ?? 1e4;
|
|
9129
|
+
this.now = options.now ?? Date.now;
|
|
9130
|
+
}
|
|
9131
|
+
/** Starts, or restarts, the period. */
|
|
9132
|
+
start() {
|
|
9133
|
+
this.stop();
|
|
9134
|
+
this.stopped = false;
|
|
9135
|
+
this.armPing();
|
|
9136
|
+
}
|
|
9137
|
+
/** The socket answered: the next ping waits a full period. */
|
|
9138
|
+
pong() {
|
|
9139
|
+
if (this.stopped) return;
|
|
9140
|
+
this.clear("deadline");
|
|
9141
|
+
this.armPing();
|
|
9142
|
+
}
|
|
9143
|
+
stop() {
|
|
9144
|
+
this.stopped = true;
|
|
9145
|
+
this.clear("nextPing");
|
|
9146
|
+
this.clear("deadline");
|
|
9147
|
+
}
|
|
9148
|
+
armPing() {
|
|
9149
|
+
this.clear("nextPing");
|
|
9150
|
+
this.nextPing = unref(setTimeout(() => this.ping(), this.intervalMs));
|
|
9151
|
+
}
|
|
9152
|
+
ping() {
|
|
9153
|
+
if (this.stopped) return;
|
|
9154
|
+
this.nextPing = null;
|
|
9155
|
+
this.pingSentAt = this.now();
|
|
9156
|
+
this.options.ping();
|
|
9157
|
+
this.clear("deadline");
|
|
9158
|
+
this.deadline = unref(setTimeout(() => this.check(), this.timeoutMs));
|
|
9159
|
+
}
|
|
9160
|
+
check() {
|
|
9161
|
+
if (this.stopped) return;
|
|
9162
|
+
this.deadline = null;
|
|
9163
|
+
if (this.now() - this.pingSentAt - this.timeoutMs > SLEEP_SLACK_MS) {
|
|
9164
|
+
this.ping();
|
|
9165
|
+
return;
|
|
9166
|
+
}
|
|
9167
|
+
this.stop();
|
|
9168
|
+
this.options.onTimeout();
|
|
9169
|
+
}
|
|
9170
|
+
clear(which) {
|
|
9171
|
+
const timer = this[which];
|
|
9172
|
+
if (timer) clearTimeout(timer);
|
|
9173
|
+
this[which] = null;
|
|
9174
|
+
}
|
|
9175
|
+
};
|
|
9176
|
+
/** In Node, a timer must not keep the process alive on its own. */
|
|
9177
|
+
function unref(timer) {
|
|
9178
|
+
timer.unref?.();
|
|
9179
|
+
return timer;
|
|
9180
|
+
}
|
|
9108
9181
|
//#endregion
|
|
9109
9182
|
//#region ../src/io/live_outbox.ts
|
|
9110
9183
|
var LiveOutbox = class {
|
|
@@ -9384,10 +9457,43 @@ var AGENT_COLOR = "#c77dff";
|
|
|
9384
9457
|
var JOIN_TIMEOUT_MS = 1e4;
|
|
9385
9458
|
/** Close code the room uses for an agent without a valid licence. */
|
|
9386
9459
|
var CLOSE_LICENCE_REQUIRED = 4402;
|
|
9460
|
+
/** Close code the room uses when it has no place for another peer. */
|
|
9461
|
+
var CLOSE_ROOM_FULL = 1013;
|
|
9462
|
+
/** Close code (with reason "replaced") for a socket a newer one replaced. */
|
|
9463
|
+
var CLOSE_REPLACED = 1e3;
|
|
9464
|
+
/** How long a socket given up on may take to report its close. */
|
|
9465
|
+
var CLOSE_WAIT_MS = 2e3;
|
|
9466
|
+
var RECONNECT_BASE_MS = 1e3;
|
|
9467
|
+
var RECONNECT_MAX_MS = 1e4;
|
|
9468
|
+
/**
|
|
9469
|
+
* How long the link keeps trying to get back into the room after a drop,
|
|
9470
|
+
* join timeouts included: the attempts never outlast this window.
|
|
9471
|
+
*/
|
|
9472
|
+
var RECONNECT_WINDOW_MS = 45e3;
|
|
9387
9473
|
var LICENCE_MESSAGE = "Drawing live with an AI agent needs a Markasso Cloud licence. Get one at https://markasso.it/cloud, then set MARKASSO_LICENSE in this MCP server's environment.";
|
|
9388
9474
|
var LiveError = class extends Error {
|
|
9389
9475
|
name = "LiveError";
|
|
9390
9476
|
};
|
|
9477
|
+
/** A close the room means as final: retrying would only repeat it. */
|
|
9478
|
+
function isFinalClose(close) {
|
|
9479
|
+
return close.code === CLOSE_LICENCE_REQUIRED || close.code === CLOSE_ROOM_FULL || close.code === CLOSE_REPLACED && close.reason === "replaced";
|
|
9480
|
+
}
|
|
9481
|
+
/** " (code 1006: reason)" for a status line, empty when nothing is known. */
|
|
9482
|
+
function describeClose(close) {
|
|
9483
|
+
if (!close) return "";
|
|
9484
|
+
const parts = [close.code !== null ? `code ${close.code}` : "", close.reason].filter(Boolean);
|
|
9485
|
+
return parts.length > 0 ? ` (${parts.join(": ")})` : "";
|
|
9486
|
+
}
|
|
9487
|
+
/** One connection attempt that did not get into the room. */
|
|
9488
|
+
var AttemptFailed = class extends LiveError {
|
|
9489
|
+
close;
|
|
9490
|
+
final;
|
|
9491
|
+
constructor(message, close, final) {
|
|
9492
|
+
super(message);
|
|
9493
|
+
this.close = close;
|
|
9494
|
+
this.final = final;
|
|
9495
|
+
}
|
|
9496
|
+
};
|
|
9391
9497
|
/**
|
|
9392
9498
|
* Whether a live link points at the Markasso instance this server is set up
|
|
9393
9499
|
* for (`MARKASSO_URL`, default markasso.it). The licence key is a paid
|
|
@@ -9439,13 +9545,36 @@ var REJECTION_TEXT = {
|
|
|
9439
9545
|
};
|
|
9440
9546
|
var LiveLink = class LiveLink {
|
|
9441
9547
|
diagram;
|
|
9548
|
+
origin;
|
|
9442
9549
|
name;
|
|
9443
9550
|
room;
|
|
9444
9551
|
url;
|
|
9445
9552
|
socket = null;
|
|
9446
9553
|
peers = [];
|
|
9447
9554
|
selfId = "";
|
|
9448
|
-
|
|
9555
|
+
/**
|
|
9556
|
+
* One identity for the whole session, reused on every reconnect: the room
|
|
9557
|
+
* then replaces the stale socket instead of seeing a second peer, and
|
|
9558
|
+
* still accepts replies to requests it routed to this agent before a drop.
|
|
9559
|
+
*/
|
|
9560
|
+
clientId = randomClientId();
|
|
9561
|
+
_state = "connecting";
|
|
9562
|
+
/** How the last socket or attempt ended (see describeClose). */
|
|
9563
|
+
_lastClose = null;
|
|
9564
|
+
/**
|
|
9565
|
+
* Bumped for each socket and on leave: events of an older socket arriving
|
|
9566
|
+
* late are ignored, so they never act on the current connection.
|
|
9567
|
+
*/
|
|
9568
|
+
attemptId = 0;
|
|
9569
|
+
reconnectTimer = null;
|
|
9570
|
+
reconnectAttempt = 0;
|
|
9571
|
+
reconnectDeadline = 0;
|
|
9572
|
+
/** The close that started the current reconnect, for its notice. */
|
|
9573
|
+
dropClose = null;
|
|
9574
|
+
/** One-shot notes for the model's next tool answer (see takeNotices). */
|
|
9575
|
+
notices = [];
|
|
9576
|
+
/** Watches the socket in the room, when the room answers pings. */
|
|
9577
|
+
heartbeat = null;
|
|
9449
9578
|
inbox = [];
|
|
9450
9579
|
/**
|
|
9451
9580
|
* Delivered request ids the agent may answer, each with the elements the
|
|
@@ -9467,10 +9596,32 @@ var LiveLink = class LiveLink {
|
|
|
9467
9596
|
/** Set while replaying a remote undo/redo, which History re-emits. */
|
|
9468
9597
|
constructor(diagram, room, origin, name) {
|
|
9469
9598
|
this.diagram = diagram;
|
|
9599
|
+
this.origin = origin;
|
|
9470
9600
|
this.name = name;
|
|
9471
9601
|
this.room = room;
|
|
9472
9602
|
this.url = `${origin}/?live=${encodeURIComponent(room)}`;
|
|
9473
9603
|
}
|
|
9604
|
+
/** Only a link in the room sends edits, activity and answers. */
|
|
9605
|
+
get open() {
|
|
9606
|
+
return this._state === "connected";
|
|
9607
|
+
}
|
|
9608
|
+
get state() {
|
|
9609
|
+
return this._state;
|
|
9610
|
+
}
|
|
9611
|
+
get lastClose() {
|
|
9612
|
+
return this._lastClose;
|
|
9613
|
+
}
|
|
9614
|
+
/** Notes about the connection since the last call, each reported once. */
|
|
9615
|
+
takeNotices() {
|
|
9616
|
+
const taken = this.notices;
|
|
9617
|
+
this.notices = [];
|
|
9618
|
+
return taken;
|
|
9619
|
+
}
|
|
9620
|
+
/** Throws a LiveError that says why the room cannot be reached now. */
|
|
9621
|
+
assertConnected() {
|
|
9622
|
+
if (this._state === "reconnecting") throw new LiveError("The live session is reconnecting: try again in a few seconds.");
|
|
9623
|
+
if (!this.open) throw new LiveError("Not connected to the live room: call live_join again.");
|
|
9624
|
+
}
|
|
9474
9625
|
/**
|
|
9475
9626
|
* Attributes the agent's own edits to the request it is working on, when
|
|
9476
9627
|
* only one waits for an answer (see `answerable`). Deleted elements leave
|
|
@@ -9546,7 +9697,7 @@ var LiveLink = class LiveLink {
|
|
|
9546
9697
|
const live = new LiveLink(diagram, room, origin, name);
|
|
9547
9698
|
return {
|
|
9548
9699
|
link: live,
|
|
9549
|
-
...await live.
|
|
9700
|
+
...await live.openSocket((msg) => live.enterRoom(msg, true))
|
|
9550
9701
|
};
|
|
9551
9702
|
}
|
|
9552
9703
|
get connected() {
|
|
@@ -9580,15 +9731,21 @@ var LiveLink = class LiveLink {
|
|
|
9580
9731
|
await this.outbox.idle(timeoutMs);
|
|
9581
9732
|
await new Promise((resolve) => setTimeout(resolve, graceMs));
|
|
9582
9733
|
}
|
|
9583
|
-
/**
|
|
9734
|
+
/**
|
|
9735
|
+
* Requests received and ready to hand to the model. None while out of the
|
|
9736
|
+
* room: they stay queued until it is back, since telling their senders
|
|
9737
|
+
* they were received needs the room.
|
|
9738
|
+
*/
|
|
9584
9739
|
get pendingRequests() {
|
|
9585
|
-
return this.inbox.length;
|
|
9740
|
+
return this.open ? this.inbox.length : 0;
|
|
9586
9741
|
}
|
|
9587
9742
|
/**
|
|
9588
9743
|
* Hands every queued request to the caller (the model) and tells each
|
|
9589
|
-
* sender it was received. They can then be answered with `reply`.
|
|
9744
|
+
* sender it was received. They can then be answered with `reply`. Hands
|
|
9745
|
+
* nothing while out of the room (see pendingRequests).
|
|
9590
9746
|
*/
|
|
9591
9747
|
takeRequests() {
|
|
9748
|
+
if (!this.open) return [];
|
|
9592
9749
|
const taken = this.inbox;
|
|
9593
9750
|
this.inbox = [];
|
|
9594
9751
|
for (const request of taken) {
|
|
@@ -9611,25 +9768,38 @@ var LiveLink = class LiveLink {
|
|
|
9611
9768
|
}
|
|
9612
9769
|
/**
|
|
9613
9770
|
* Resolves with the queued requests as soon as there is one, or with none
|
|
9614
|
-
* after `timeoutMs
|
|
9771
|
+
* after `timeoutMs`, or once the session is lost or left. A drop the link
|
|
9772
|
+
* recovers from does not end the wait: it keeps the same deadline.
|
|
9615
9773
|
*/
|
|
9616
9774
|
async waitForRequests(timeoutMs) {
|
|
9617
|
-
|
|
9618
|
-
|
|
9619
|
-
|
|
9620
|
-
|
|
9621
|
-
|
|
9622
|
-
|
|
9623
|
-
this.waiters.delete(wake);
|
|
9624
|
-
this.waiting--;
|
|
9625
|
-
resolve();
|
|
9626
|
-
};
|
|
9627
|
-
const timer = setTimeout(wake, timeoutMs);
|
|
9628
|
-
this.waiters.add(wake);
|
|
9629
|
-
});
|
|
9775
|
+
const deadline = Date.now() + timeoutMs;
|
|
9776
|
+
while (this.recoverable && this.pendingRequests === 0) {
|
|
9777
|
+
const remaining = deadline - Date.now();
|
|
9778
|
+
if (remaining <= 0) break;
|
|
9779
|
+
await this.waitForWake(remaining);
|
|
9780
|
+
if (this.open) break;
|
|
9630
9781
|
}
|
|
9631
9782
|
return this.takeRequests();
|
|
9632
9783
|
}
|
|
9784
|
+
/** In the room, or on its way back. */
|
|
9785
|
+
get recoverable() {
|
|
9786
|
+
return this._state === "connected" || this._state === "reconnecting";
|
|
9787
|
+
}
|
|
9788
|
+
/** Waits for a request, a change of connection, or `timeoutMs`. */
|
|
9789
|
+
waitForWake(timeoutMs) {
|
|
9790
|
+
this.waiting++;
|
|
9791
|
+
this.updateActivity();
|
|
9792
|
+
return new Promise((resolve) => {
|
|
9793
|
+
const wake = () => {
|
|
9794
|
+
clearTimeout(timer);
|
|
9795
|
+
this.waiters.delete(wake);
|
|
9796
|
+
this.waiting--;
|
|
9797
|
+
resolve();
|
|
9798
|
+
};
|
|
9799
|
+
const timer = setTimeout(wake, timeoutMs);
|
|
9800
|
+
this.waiters.add(wake);
|
|
9801
|
+
});
|
|
9802
|
+
}
|
|
9633
9803
|
/**
|
|
9634
9804
|
* Sends an answer to the person behind a delivered request, pointing at
|
|
9635
9805
|
* `elements` when given, else at the elements the agent changed for it.
|
|
@@ -9637,7 +9807,7 @@ var LiveLink = class LiveLink {
|
|
|
9637
9807
|
* could not be told apart from another request's (`ambiguous`).
|
|
9638
9808
|
*/
|
|
9639
9809
|
reply(id, text, elements) {
|
|
9640
|
-
|
|
9810
|
+
this.assertConnected();
|
|
9641
9811
|
if (!this.answerable.has(id)) throw new LiveError(`Unknown request id "${id}".`);
|
|
9642
9812
|
const touched = this.answerable.get(id) ?? null;
|
|
9643
9813
|
const trimmed = text.trim();
|
|
@@ -9684,17 +9854,50 @@ var LiveLink = class LiveLink {
|
|
|
9684
9854
|
send(message) {
|
|
9685
9855
|
if (this.socket?.readyState === WebSocket.OPEN) this.socket.send(JSON.stringify(message));
|
|
9686
9856
|
}
|
|
9687
|
-
|
|
9688
|
-
|
|
9689
|
-
|
|
9690
|
-
|
|
9691
|
-
|
|
9857
|
+
/**
|
|
9858
|
+
* Opens one socket to the room and resolves with what `onInit` returns,
|
|
9859
|
+
* run as soon as the room's `init` arrives (in the same event, so no
|
|
9860
|
+
* remote command can slip in before it). Rejects with an AttemptFailed
|
|
9861
|
+
* when the attempt ends before that. Every listener belongs to this
|
|
9862
|
+
* attempt only: once another socket is opened or the link is left, this
|
|
9863
|
+
* socket's events are ignored.
|
|
9864
|
+
*/
|
|
9865
|
+
openSocket(onInit, timeoutMs = JOIN_TIMEOUT_MS) {
|
|
9866
|
+
const attempt = ++this.attemptId;
|
|
9867
|
+
const current = () => attempt === this.attemptId;
|
|
9868
|
+
const origin = this.origin;
|
|
9869
|
+
const url = `${origin.replace(/^http/, "ws")}/session/ws?room=${encodeURIComponent(this.room)}&clientId=${this.clientId}`;
|
|
9692
9870
|
return new Promise((resolve, reject) => {
|
|
9693
|
-
|
|
9694
|
-
|
|
9695
|
-
|
|
9696
|
-
}
|
|
9871
|
+
let socket;
|
|
9872
|
+
try {
|
|
9873
|
+
socket = new WebSocket(url);
|
|
9874
|
+
} catch (error) {
|
|
9875
|
+
reject(new AttemptFailed("Could not connect to the live room.", {
|
|
9876
|
+
code: null,
|
|
9877
|
+
reason: error instanceof Error ? error.message : String(error)
|
|
9878
|
+
}, false));
|
|
9879
|
+
return;
|
|
9880
|
+
}
|
|
9881
|
+
this.socket = socket;
|
|
9882
|
+
/** In the room: `init` arrived. */
|
|
9883
|
+
let admitted = false;
|
|
9884
|
+
/** The promise is settled, one way or the other. */
|
|
9885
|
+
let settled = false;
|
|
9886
|
+
const fail = (error) => {
|
|
9887
|
+
if (settled) return;
|
|
9888
|
+
settled = true;
|
|
9889
|
+
clearTimeout(timer);
|
|
9890
|
+
reject(error);
|
|
9891
|
+
try {
|
|
9892
|
+
socket.close();
|
|
9893
|
+
} catch {}
|
|
9894
|
+
};
|
|
9895
|
+
const timer = setTimeout(() => fail(new AttemptFailed("Timed out joining the live room.", {
|
|
9896
|
+
code: null,
|
|
9897
|
+
reason: "join timed out"
|
|
9898
|
+
}, false)), timeoutMs);
|
|
9697
9899
|
socket.addEventListener("open", () => {
|
|
9900
|
+
if (!current()) return;
|
|
9698
9901
|
const license = process.env.MARKASSO_LICENSE?.trim();
|
|
9699
9902
|
if (license && isConfiguredInstance(origin)) this.send({
|
|
9700
9903
|
type: "auth",
|
|
@@ -9707,6 +9910,7 @@ var LiveLink = class LiveLink {
|
|
|
9707
9910
|
});
|
|
9708
9911
|
});
|
|
9709
9912
|
socket.addEventListener("message", (event) => {
|
|
9913
|
+
if (!current()) return;
|
|
9710
9914
|
let msg;
|
|
9711
9915
|
try {
|
|
9712
9916
|
msg = JSON.parse(String(event.data));
|
|
@@ -9714,33 +9918,17 @@ var LiveLink = class LiveLink {
|
|
|
9714
9918
|
return;
|
|
9715
9919
|
}
|
|
9716
9920
|
if (msg.type === "init") {
|
|
9921
|
+
if (settled) return;
|
|
9922
|
+
admitted = true;
|
|
9923
|
+
settled = true;
|
|
9717
9924
|
clearTimeout(timer);
|
|
9718
|
-
|
|
9719
|
-
|
|
9720
|
-
|
|
9721
|
-
|
|
9722
|
-
|
|
9723
|
-
if (msg.commands.length > 0) {
|
|
9724
|
-
discarded = local.length;
|
|
9725
|
-
history.resetForLiveReplay(msg.commands.filter(isValidSessionCommand));
|
|
9726
|
-
} else if (local.length > 0) {
|
|
9727
|
-
for (const command of createCommands(local, { validOnly: true })) this.publish(command);
|
|
9728
|
-
seeded = true;
|
|
9729
|
-
}
|
|
9730
|
-
history.setOnCommand((command) => {
|
|
9731
|
-
if (this.open && isSessionCommand(command)) this.publish(command);
|
|
9732
|
-
});
|
|
9733
|
-
this.untrack();
|
|
9734
|
-
this.stopTracking = this.diagram.onLocalChange((changed, removed) => this.noteEdited(changed, removed));
|
|
9735
|
-
this.open = true;
|
|
9736
|
-
this.sendActivity(this.currentActivity());
|
|
9737
|
-
resolve({
|
|
9738
|
-
seeded,
|
|
9739
|
-
discarded
|
|
9740
|
-
});
|
|
9741
|
-
} else if (msg.type === "apply") {
|
|
9925
|
+
resolve(onInit(msg));
|
|
9926
|
+
if (msg.heartbeat === true) this.watch(socket, attempt);
|
|
9927
|
+
} else if (msg.type === "pong") this.heartbeat?.pong();
|
|
9928
|
+
else if (msg.type === "apply") {
|
|
9929
|
+
if (!admitted) return;
|
|
9742
9930
|
if (isSessionCommand(msg.command) && isValidSessionCommand(msg.command)) try {
|
|
9743
|
-
history.applyRemote(msg.command);
|
|
9931
|
+
this.diagram.history.applyRemote(msg.command);
|
|
9744
9932
|
} catch (error) {
|
|
9745
9933
|
console.error("Skipped a live command that failed to apply", error);
|
|
9746
9934
|
}
|
|
@@ -9748,31 +9936,171 @@ var LiveLink = class LiveLink {
|
|
|
9748
9936
|
else if (msg.type === "request") this.receiveRequest(msg);
|
|
9749
9937
|
else if (msg.type === "rejected" && typeof msg.reason === "string") {
|
|
9750
9938
|
if (!this.rejections.includes(msg.reason)) this.rejections.push(msg.reason);
|
|
9751
|
-
} else if (msg.type === "error" && !
|
|
9752
|
-
|
|
9753
|
-
|
|
9939
|
+
} else if (msg.type === "error" && !admitted) {
|
|
9940
|
+
const final = msg.error === "licence required" || msg.error === "room full";
|
|
9941
|
+
fail(new AttemptFailed(msg.error === "licence required" ? licenceMessage(origin) : `The live room refused to join: ${msg.error}.`, {
|
|
9942
|
+
code: null,
|
|
9943
|
+
reason: msg.error
|
|
9944
|
+
}, final));
|
|
9754
9945
|
}
|
|
9755
9946
|
});
|
|
9756
9947
|
socket.addEventListener("close", (event) => {
|
|
9757
|
-
|
|
9758
|
-
|
|
9759
|
-
|
|
9760
|
-
|
|
9761
|
-
|
|
9762
|
-
|
|
9763
|
-
|
|
9764
|
-
|
|
9948
|
+
if (!current()) return;
|
|
9949
|
+
const close = {
|
|
9950
|
+
code: typeof event.code === "number" ? event.code : null,
|
|
9951
|
+
reason: typeof event.reason === "string" ? event.reason : ""
|
|
9952
|
+
};
|
|
9953
|
+
if (!admitted) {
|
|
9954
|
+
fail(new AttemptFailed(close.code === CLOSE_LICENCE_REQUIRED ? licenceMessage(origin) : "The live room closed the connection.", close, isFinalClose(close)));
|
|
9955
|
+
return;
|
|
9956
|
+
}
|
|
9957
|
+
this.dropped(close);
|
|
9765
9958
|
});
|
|
9766
9959
|
socket.addEventListener("error", () => {
|
|
9767
|
-
if (!
|
|
9768
|
-
|
|
9769
|
-
|
|
9770
|
-
|
|
9960
|
+
if (!current() || admitted) return;
|
|
9961
|
+
fail(new AttemptFailed("Could not connect to the live room.", {
|
|
9962
|
+
code: null,
|
|
9963
|
+
reason: "socket error"
|
|
9964
|
+
}, false));
|
|
9771
9965
|
});
|
|
9772
9966
|
});
|
|
9773
9967
|
}
|
|
9968
|
+
/**
|
|
9969
|
+
* Takes the room's state on `init`. On the first join the room's log
|
|
9970
|
+
* replaces the local diagram, or the local diagram seeds an empty room;
|
|
9971
|
+
* on a reconnect the room is authoritative even when empty, and nothing
|
|
9972
|
+
* is published, so stale content never comes back.
|
|
9973
|
+
*/
|
|
9974
|
+
enterRoom(msg, first) {
|
|
9975
|
+
const history = this.diagram.history;
|
|
9976
|
+
this.selfId = msg.self;
|
|
9977
|
+
this.peers = msg.peers;
|
|
9978
|
+
const local = [...this.diagram.elements];
|
|
9979
|
+
let seeded = false;
|
|
9980
|
+
let discarded = 0;
|
|
9981
|
+
if (msg.commands.length > 0 || !first) {
|
|
9982
|
+
if (first) discarded = local.length;
|
|
9983
|
+
history.resetForLiveReplay(msg.commands.filter(isValidSessionCommand));
|
|
9984
|
+
} else if (local.length > 0) {
|
|
9985
|
+
for (const command of createCommands(local, { validOnly: true })) this.publish(command);
|
|
9986
|
+
seeded = true;
|
|
9987
|
+
}
|
|
9988
|
+
if (first) {
|
|
9989
|
+
history.setOnCommand((command) => {
|
|
9990
|
+
if (this.open && isSessionCommand(command)) this.publish(command);
|
|
9991
|
+
});
|
|
9992
|
+
this.untrack();
|
|
9993
|
+
this.stopTracking = this.diagram.onLocalChange((changed, removed) => {
|
|
9994
|
+
if (this.open) this.noteEdited(changed, removed);
|
|
9995
|
+
});
|
|
9996
|
+
}
|
|
9997
|
+
this._state = "connected";
|
|
9998
|
+
this.reconnectAttempt = 0;
|
|
9999
|
+
this.clearIdleTimer();
|
|
10000
|
+
this.sendActivity(this.currentActivity());
|
|
10001
|
+
if (this.inbox.length > 0) this.wakeWaiters();
|
|
10002
|
+
return {
|
|
10003
|
+
seeded,
|
|
10004
|
+
discarded
|
|
10005
|
+
};
|
|
10006
|
+
}
|
|
10007
|
+
/**
|
|
10008
|
+
* Pings the socket of this attempt while it is in the room. No answer in
|
|
10009
|
+
* time means the connection died without a close event: it is dropped
|
|
10010
|
+
* right away, as a close would (its own late events no longer count),
|
|
10011
|
+
* instead of waiting for a close that may take long to come.
|
|
10012
|
+
*/
|
|
10013
|
+
watch(socket, attempt) {
|
|
10014
|
+
this.heartbeat?.stop();
|
|
10015
|
+
this.heartbeat = new Heartbeat({
|
|
10016
|
+
ping: () => {
|
|
10017
|
+
if (attempt === this.attemptId) this.send({ type: "ping" });
|
|
10018
|
+
},
|
|
10019
|
+
onTimeout: () => {
|
|
10020
|
+
if (attempt !== this.attemptId || this._state !== "connected") return;
|
|
10021
|
+
try {
|
|
10022
|
+
socket.close();
|
|
10023
|
+
} catch {}
|
|
10024
|
+
setTimeout(() => {
|
|
10025
|
+
if (attempt !== this.attemptId || this._state !== "connected") return;
|
|
10026
|
+
this.attemptId++;
|
|
10027
|
+
this.dropped({
|
|
10028
|
+
code: null,
|
|
10029
|
+
reason: "heartbeat timeout"
|
|
10030
|
+
});
|
|
10031
|
+
}, CLOSE_WAIT_MS).unref?.();
|
|
10032
|
+
}
|
|
10033
|
+
});
|
|
10034
|
+
this.heartbeat.start();
|
|
10035
|
+
}
|
|
10036
|
+
stopWatching() {
|
|
10037
|
+
this.heartbeat?.stop();
|
|
10038
|
+
this.heartbeat = null;
|
|
10039
|
+
}
|
|
10040
|
+
/** The socket of a joined session closed: retry unless it is final. */
|
|
10041
|
+
dropped(close) {
|
|
10042
|
+
this.stopWatching();
|
|
10043
|
+
this._lastClose = close;
|
|
10044
|
+
this.dropClose = close;
|
|
10045
|
+
this.outbox.clear();
|
|
10046
|
+
this.clearIdleTimer();
|
|
10047
|
+
if (isFinalClose(close)) {
|
|
10048
|
+
this.lose();
|
|
10049
|
+
return;
|
|
10050
|
+
}
|
|
10051
|
+
this._state = "reconnecting";
|
|
10052
|
+
this.reconnectAttempt = 0;
|
|
10053
|
+
this.reconnectDeadline = Date.now() + RECONNECT_WINDOW_MS;
|
|
10054
|
+
this.scheduleReconnect();
|
|
10055
|
+
}
|
|
10056
|
+
scheduleReconnect() {
|
|
10057
|
+
const delay = Math.min(RECONNECT_BASE_MS * 2 ** this.reconnectAttempt, RECONNECT_MAX_MS);
|
|
10058
|
+
this.reconnectAttempt++;
|
|
10059
|
+
if (Date.now() + delay >= this.reconnectDeadline) {
|
|
10060
|
+
this.lose();
|
|
10061
|
+
return;
|
|
10062
|
+
}
|
|
10063
|
+
this.reconnectTimer = setTimeout(() => {
|
|
10064
|
+
this.reconnectTimer = null;
|
|
10065
|
+
this.reconnect();
|
|
10066
|
+
}, delay);
|
|
10067
|
+
}
|
|
10068
|
+
async reconnect() {
|
|
10069
|
+
if (this._state !== "reconnecting") return;
|
|
10070
|
+
const remaining = this.reconnectDeadline - Date.now();
|
|
10071
|
+
try {
|
|
10072
|
+
await this.openSocket((msg) => {
|
|
10073
|
+
this.notices.push(`The live session dropped${describeClose(this.dropClose)} and reconnected. The board was reloaded from the room, so your most recent edits may be missing: call diagram_read before editing further.`);
|
|
10074
|
+
return this.enterRoom(msg, false);
|
|
10075
|
+
}, Math.min(JOIN_TIMEOUT_MS, remaining));
|
|
10076
|
+
} catch (error) {
|
|
10077
|
+
if (this._state !== "reconnecting") return;
|
|
10078
|
+
if (error instanceof AttemptFailed) {
|
|
10079
|
+
this._lastClose = error.close;
|
|
10080
|
+
if (error.final) {
|
|
10081
|
+
this.lose();
|
|
10082
|
+
return;
|
|
10083
|
+
}
|
|
10084
|
+
} else console.error("Unexpected error while reconnecting", error);
|
|
10085
|
+
this.scheduleReconnect();
|
|
10086
|
+
}
|
|
10087
|
+
}
|
|
10088
|
+
/** Gives up on the room: waiting calls return, nothing is sent anymore. */
|
|
10089
|
+
lose() {
|
|
10090
|
+
this.stopWatching();
|
|
10091
|
+
this._state = "lost";
|
|
10092
|
+
this.cancelReconnect();
|
|
10093
|
+
this.untrack();
|
|
10094
|
+
this.clearIdleTimer();
|
|
10095
|
+
this.wakeWaiters();
|
|
10096
|
+
}
|
|
10097
|
+
cancelReconnect() {
|
|
10098
|
+
if (this.reconnectTimer) clearTimeout(this.reconnectTimer);
|
|
10099
|
+
this.reconnectTimer = null;
|
|
10100
|
+
}
|
|
9774
10101
|
/** Moves the agent's cursor so people see where it is working. */
|
|
9775
10102
|
pointAt(x, y) {
|
|
10103
|
+
if (!this.open) return;
|
|
9776
10104
|
this.send({
|
|
9777
10105
|
type: "cursor",
|
|
9778
10106
|
x,
|
|
@@ -9782,12 +10110,14 @@ var LiveLink = class LiveLink {
|
|
|
9782
10110
|
});
|
|
9783
10111
|
}
|
|
9784
10112
|
leave() {
|
|
9785
|
-
this.outbox.drain();
|
|
9786
|
-
this.
|
|
10113
|
+
if (this.open) this.outbox.drain();
|
|
10114
|
+
this._state = "left";
|
|
10115
|
+
this.stopWatching();
|
|
10116
|
+
this.attemptId++;
|
|
10117
|
+
this.cancelReconnect();
|
|
9787
10118
|
this.wakeWaiters();
|
|
9788
10119
|
this.untrack();
|
|
9789
10120
|
this.clearIdleTimer();
|
|
9790
|
-
this.diagram.history.setOnCommand(void 0);
|
|
9791
10121
|
this.socket?.close();
|
|
9792
10122
|
this.socket = null;
|
|
9793
10123
|
}
|
|
@@ -10110,6 +10440,14 @@ var edgeRefSchema = {
|
|
|
10110
10440
|
};
|
|
10111
10441
|
var diagram = new Diagram();
|
|
10112
10442
|
var live = null;
|
|
10443
|
+
/**
|
|
10444
|
+
* Bumped whenever the session changes (new or opened diagram, join, leave),
|
|
10445
|
+
* so a live_join that another change overtook while it was connecting
|
|
10446
|
+
* leaves its room instead of taking over.
|
|
10447
|
+
*/
|
|
10448
|
+
var sessionEpoch = 0;
|
|
10449
|
+
/** live_join calls run one at a time (see joinLive). */
|
|
10450
|
+
var joining = Promise.resolve();
|
|
10113
10451
|
function text(message) {
|
|
10114
10452
|
return { content: [{
|
|
10115
10453
|
type: "text",
|
|
@@ -10189,13 +10527,48 @@ function guarded(run) {
|
|
|
10189
10527
|
if (live && live.editCount > (live === before ? editsBefore : 0)) await live.settle(QUEUE_WAIT_MS, REFUSAL_WAIT_MS);
|
|
10190
10528
|
const refused = [before, live !== before ? live : null].map((link) => link?.takeRejections() ?? "").filter(Boolean);
|
|
10191
10529
|
if (refused.length > 0 && first) first.text += `\n${refused.join("\n")}`;
|
|
10530
|
+
if (live && first) {
|
|
10531
|
+
const edited = live.editCount > (live === before ? editsBefore : 0);
|
|
10532
|
+
const status = liveStatus(live, edited);
|
|
10533
|
+
if (status) first.text += `\n${status}`;
|
|
10534
|
+
}
|
|
10192
10535
|
return result;
|
|
10193
10536
|
});
|
|
10194
10537
|
}
|
|
10195
|
-
|
|
10196
|
-
|
|
10197
|
-
|
|
10198
|
-
|
|
10538
|
+
/**
|
|
10539
|
+
* The live session's status for the end of an answer, written after the
|
|
10540
|
+
* call's edits are settled: one-shot notes (a reconnect), a warning while
|
|
10541
|
+
* the room is out of reach, or where the edits of this call can be seen.
|
|
10542
|
+
*/
|
|
10543
|
+
function liveStatus(link, edited) {
|
|
10544
|
+
const lines = link.takeNotices();
|
|
10545
|
+
if (link.state === "reconnecting") lines.push("Warning: the live session dropped and is reconnecting: edits made now do not reach the room.");
|
|
10546
|
+
else if (link.state === "lost") lines.push(`Warning: the live session was lost${describeClose(link.lastClose)}; call live_join again.`);
|
|
10547
|
+
else if (edited && link.connected) lines.push(`Live: changes are visible to everyone in ${link.url}`);
|
|
10548
|
+
return lines.join("\n");
|
|
10549
|
+
}
|
|
10550
|
+
/**
|
|
10551
|
+
* Leaves the current room and joins the one at `link`. Joins run one at a
|
|
10552
|
+
* time, since two at once would both replay their room into the same
|
|
10553
|
+
* diagram; one overtaken by another session change leaves right away.
|
|
10554
|
+
*/
|
|
10555
|
+
function joinLive(link, name) {
|
|
10556
|
+
const run = async () => {
|
|
10557
|
+
live?.leave();
|
|
10558
|
+
live = null;
|
|
10559
|
+
const epoch = ++sessionEpoch;
|
|
10560
|
+
const target = diagram;
|
|
10561
|
+
const result = await LiveLink.join(target, link, name);
|
|
10562
|
+
if (epoch !== sessionEpoch || target !== diagram) {
|
|
10563
|
+
result.link.leave();
|
|
10564
|
+
throw new LiveError("The session changed while joining (another diagram or live call): the join was cancelled.");
|
|
10565
|
+
}
|
|
10566
|
+
live = result.link;
|
|
10567
|
+
return result;
|
|
10568
|
+
};
|
|
10569
|
+
const next = joining.then(run, run);
|
|
10570
|
+
joining = next.catch(() => void 0);
|
|
10571
|
+
return next;
|
|
10199
10572
|
}
|
|
10200
10573
|
/** Points the agent's cursor at the given nodes on a live board. */
|
|
10201
10574
|
function pointAtKeys(keys) {
|
|
@@ -10219,6 +10592,7 @@ server.registerTool("diagram_new", {
|
|
|
10219
10592
|
}, guarded(() => {
|
|
10220
10593
|
live?.leave();
|
|
10221
10594
|
live = null;
|
|
10595
|
+
sessionEpoch++;
|
|
10222
10596
|
diagram = new Diagram();
|
|
10223
10597
|
return "New empty diagram ready.";
|
|
10224
10598
|
}));
|
|
@@ -10230,6 +10604,7 @@ server.registerTool("diagram_open", {
|
|
|
10230
10604
|
const elements = /^https?:\/\//.test(source) ? await loadShareLink(source) : await loadMarkassoFile(source);
|
|
10231
10605
|
live?.leave();
|
|
10232
10606
|
live = null;
|
|
10607
|
+
sessionEpoch++;
|
|
10233
10608
|
diagram = new Diagram(elements);
|
|
10234
10609
|
return `Opened ${elements.length} elements.\n${describeDiagram(diagram)}`;
|
|
10235
10610
|
}));
|
|
@@ -10249,7 +10624,7 @@ server.registerTool("diagram_add_nodes", {
|
|
|
10249
10624
|
}, guarded(async ({ nodes }) => {
|
|
10250
10625
|
const keys = await diagram.addNodes(nodes);
|
|
10251
10626
|
pointAtKeys(keys);
|
|
10252
|
-
return `Added ${keys.length} nodes: ${keys.join(", ")}
|
|
10627
|
+
return `Added ${keys.length} nodes: ${keys.join(", ")}.`;
|
|
10253
10628
|
}));
|
|
10254
10629
|
server.registerTool("diagram_connect", {
|
|
10255
10630
|
title: "Connect nodes",
|
|
@@ -10258,7 +10633,7 @@ server.registerTool("diagram_connect", {
|
|
|
10258
10633
|
}, guarded(({ edges }) => {
|
|
10259
10634
|
const count = diagram.connect(edges);
|
|
10260
10635
|
pointAtKeys(edges.map((e) => e.to));
|
|
10261
|
-
return `Added ${count} connections
|
|
10636
|
+
return `Added ${count} connections.`;
|
|
10262
10637
|
}));
|
|
10263
10638
|
server.registerTool("diagram_update", {
|
|
10264
10639
|
title: "Update node",
|
|
@@ -10277,7 +10652,7 @@ server.registerTool("diagram_update", {
|
|
|
10277
10652
|
const { key, ...patch } = args;
|
|
10278
10653
|
diagram.update(key, patch);
|
|
10279
10654
|
pointAtKeys([key]);
|
|
10280
|
-
return `Updated ${key}
|
|
10655
|
+
return `Updated ${key}.`;
|
|
10281
10656
|
}));
|
|
10282
10657
|
server.registerTool("diagram_remove", {
|
|
10283
10658
|
title: "Remove nodes",
|
|
@@ -10285,7 +10660,7 @@ server.registerTool("diagram_remove", {
|
|
|
10285
10660
|
inputSchema: { keys: z.array(z.string()).min(1).max(200).describe("Keys of the nodes to remove") },
|
|
10286
10661
|
annotations: { destructiveHint: true }
|
|
10287
10662
|
}, guarded(({ keys }) => {
|
|
10288
|
-
return `Removed ${diagram.remove(keys)} nodes
|
|
10663
|
+
return `Removed ${diagram.remove(keys)} nodes.`;
|
|
10289
10664
|
}));
|
|
10290
10665
|
server.registerTool("diagram_disconnect", {
|
|
10291
10666
|
title: "Remove connections",
|
|
@@ -10293,7 +10668,7 @@ server.registerTool("diagram_disconnect", {
|
|
|
10293
10668
|
inputSchema: { edges: z.array(z.object(edgeRefSchema)).min(1).max(200).describe("Connections to remove") },
|
|
10294
10669
|
annotations: { destructiveHint: true }
|
|
10295
10670
|
}, guarded(({ edges }) => {
|
|
10296
|
-
return `Removed ${diagram.disconnect(edges)} connections
|
|
10671
|
+
return `Removed ${diagram.disconnect(edges)} connections.`;
|
|
10297
10672
|
}));
|
|
10298
10673
|
server.registerTool("diagram_update_edge", {
|
|
10299
10674
|
title: "Update connection",
|
|
@@ -10320,7 +10695,7 @@ server.registerTool("diagram_update_edge", {
|
|
|
10320
10695
|
...rest,
|
|
10321
10696
|
...newLabel !== void 0 && { label: newLabel }
|
|
10322
10697
|
});
|
|
10323
|
-
return `Updated the connection
|
|
10698
|
+
return `Updated the connection.`;
|
|
10324
10699
|
}));
|
|
10325
10700
|
server.registerTool("diagram_layout", {
|
|
10326
10701
|
title: "Auto layout",
|
|
@@ -10332,7 +10707,7 @@ server.registerTool("diagram_layout", {
|
|
|
10332
10707
|
}, guarded(({ direction, keys }) => {
|
|
10333
10708
|
const count = diagram.layout(direction ?? "TB", keys);
|
|
10334
10709
|
if (count === 0) return "Nothing to lay out.";
|
|
10335
|
-
return `Arranged ${count} nodes (${direction ?? "TB"}).${warningsSuffix(diagram)}
|
|
10710
|
+
return `Arranged ${count} nodes (${direction ?? "TB"}).${warningsSuffix(diagram)}`;
|
|
10336
10711
|
}));
|
|
10337
10712
|
server.registerTool("icons_search", {
|
|
10338
10713
|
title: "Search icons",
|
|
@@ -10438,12 +10813,10 @@ server.registerTool("live_join", {
|
|
|
10438
10813
|
},
|
|
10439
10814
|
annotations: { openWorldHint: true }
|
|
10440
10815
|
}, guarded(async ({ link, name }) => {
|
|
10441
|
-
|
|
10442
|
-
const result = await LiveLink.join(diagram, link, name ?? "AI agent");
|
|
10443
|
-
live = result.link;
|
|
10816
|
+
const result = await joinLive(link, name ?? "AI agent");
|
|
10444
10817
|
const notes = [result.seeded ? "The room was empty: your diagram was published to it." : "", result.discarded > 0 ? `The room already had content: it replaced your ${result.discarded} local elements.` : ""].filter(Boolean);
|
|
10445
10818
|
return [
|
|
10446
|
-
`Joined ${
|
|
10819
|
+
`Joined ${result.link.url}.`,
|
|
10447
10820
|
...notes,
|
|
10448
10821
|
describeDiagram(diagram)
|
|
10449
10822
|
].join("\n");
|
|
@@ -10456,6 +10829,7 @@ server.registerTool("live_leave", {
|
|
|
10456
10829
|
if (!live) return "Not in a live session.";
|
|
10457
10830
|
live.leave();
|
|
10458
10831
|
live = null;
|
|
10832
|
+
sessionEpoch++;
|
|
10459
10833
|
return "Left the live session.";
|
|
10460
10834
|
}));
|
|
10461
10835
|
server.registerTool("live_wait_request", {
|
|
@@ -10467,7 +10841,8 @@ server.registerTool("live_wait_request", {
|
|
|
10467
10841
|
const link = live;
|
|
10468
10842
|
const requests = await link.waitForRequests((timeoutSeconds ?? 50) * 1e3);
|
|
10469
10843
|
if (requests.length > 0) return formatRequests(requests).trimStart();
|
|
10470
|
-
if (
|
|
10844
|
+
if (link.state === "left") return "Stopped waiting: the live session was left.";
|
|
10845
|
+
if (link.state === "lost") return "Stopped waiting: the live session ended.";
|
|
10471
10846
|
return "No requests yet. Call live_wait_request again to keep listening, or live_leave to stop.";
|
|
10472
10847
|
}));
|
|
10473
10848
|
server.registerTool("live_reply", {
|
|
@@ -10480,6 +10855,7 @@ server.registerTool("live_reply", {
|
|
|
10480
10855
|
}
|
|
10481
10856
|
}, guarded(({ requestId, text, nodes }) => {
|
|
10482
10857
|
if (!live) return "Not in a live session.";
|
|
10858
|
+
live.assertConnected();
|
|
10483
10859
|
const { pointed, ambiguous } = live.reply(requestId, text, nodes ? diagram.idsOfKeys(nodes) : void 0);
|
|
10484
10860
|
if (ambiguous) return "Reply sent without pointing at elements: you changed the board while several requests were open, so the changes cannot be told apart. Pass nodes to point at what this request changed.";
|
|
10485
10861
|
return pointed > 0 ? `Reply sent, pointing at ${pointed} element${pointed === 1 ? "" : "s"}.` : "Reply sent.";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@markasso/mcp",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.2",
|
|
4
4
|
"mcpName": "io.github.AlbertoBarrago/markasso",
|
|
5
5
|
"description": "MCP server that lets AI agents create, read and edit Markasso diagrams, offline or live on a shared board",
|
|
6
6
|
"type": "module",
|