@markasso/mcp 0.9.0 → 0.10.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/dist/markasso-mcp.js +631 -105
- package/package.json +1 -1
package/dist/markasso-mcp.js
CHANGED
|
@@ -1993,6 +1993,20 @@ function getBundledIcon(ref) {
|
|
|
1993
1993
|
//#region ../src/io/agent_requests_protocol.ts
|
|
1994
1994
|
/** Longest reply text an agent can send back. */
|
|
1995
1995
|
var MAX_AGENT_REPLY_CHARS = 4e3;
|
|
1996
|
+
/**
|
|
1997
|
+
* The valid element ids of an untrusted `elements` field: strings of 1 to
|
|
1998
|
+
* MAX_ELEMENT_ID_CHARS characters, without duplicates, at most
|
|
1999
|
+
* MAX_REPLY_ELEMENTS. Anything else yields an empty list.
|
|
2000
|
+
*/
|
|
2001
|
+
function sanitizeReplyElements(value) {
|
|
2002
|
+
if (!Array.isArray(value)) return [];
|
|
2003
|
+
const ids = /* @__PURE__ */ new Set();
|
|
2004
|
+
for (const id of value.slice(0, 200)) {
|
|
2005
|
+
if (ids.size >= 50) break;
|
|
2006
|
+
if (typeof id === "string" && id.length > 0 && id.length <= 128) ids.add(id);
|
|
2007
|
+
}
|
|
2008
|
+
return [...ids];
|
|
2009
|
+
}
|
|
1996
2010
|
//#endregion
|
|
1997
2011
|
//#region ../src/core/geometry.ts
|
|
1998
2012
|
/**
|
|
@@ -3731,6 +3745,116 @@ function getConnectorHeads(el, vectorFor) {
|
|
|
3731
3745
|
});
|
|
3732
3746
|
return heads;
|
|
3733
3747
|
}
|
|
3748
|
+
/** Element types connectors attach to, and so must not run through. */
|
|
3749
|
+
var NODE_TYPES = /* @__PURE__ */ new Set([
|
|
3750
|
+
"rectangle",
|
|
3751
|
+
"ellipse",
|
|
3752
|
+
"rhombus",
|
|
3753
|
+
"shape",
|
|
3754
|
+
"stencil",
|
|
3755
|
+
"text",
|
|
3756
|
+
"image",
|
|
3757
|
+
"polygon"
|
|
3758
|
+
]);
|
|
3759
|
+
function isRouteNodeType(type) {
|
|
3760
|
+
return NODE_TYPES.has(type);
|
|
3761
|
+
}
|
|
3762
|
+
/**
|
|
3763
|
+
* Part of a node a connector must not cross: the rectangle inscribed in an
|
|
3764
|
+
* ellipse or rhombus (so passing near a corner of the bounding box is not a
|
|
3765
|
+
* crossing), the connector box otherwise, inset by a couple of pixels so a
|
|
3766
|
+
* stroke running along the border doesn't count.
|
|
3767
|
+
*/
|
|
3768
|
+
function nodeCoreRect(type, box) {
|
|
3769
|
+
const scale = type === "ellipse" ? Math.SQRT1_2 : type === "rhombus" ? .5 : 1;
|
|
3770
|
+
const w = box.w * scale - 4;
|
|
3771
|
+
const h = box.h * scale - 4;
|
|
3772
|
+
return {
|
|
3773
|
+
x: box.x + (box.w - w) / 2,
|
|
3774
|
+
y: box.y + (box.h - h) / 2,
|
|
3775
|
+
w: Math.max(0, w),
|
|
3776
|
+
h: Math.max(0, h)
|
|
3777
|
+
};
|
|
3778
|
+
}
|
|
3779
|
+
/** Length of segment a-b inside `r` (Liang-Barsky clipping). */
|
|
3780
|
+
function lengthInside(a, b, r) {
|
|
3781
|
+
const dx = b[0] - a[0];
|
|
3782
|
+
const dy = b[1] - a[1];
|
|
3783
|
+
let t0 = 0;
|
|
3784
|
+
let t1 = 1;
|
|
3785
|
+
const edges = [
|
|
3786
|
+
[-dx, a[0] - r.x],
|
|
3787
|
+
[dx, r.x + r.w - a[0]],
|
|
3788
|
+
[-dy, a[1] - r.y],
|
|
3789
|
+
[dy, r.y + r.h - a[1]]
|
|
3790
|
+
];
|
|
3791
|
+
for (const [p, q] of edges) {
|
|
3792
|
+
if (Math.abs(p) < 1e-9) {
|
|
3793
|
+
if (q < 0) return 0;
|
|
3794
|
+
continue;
|
|
3795
|
+
}
|
|
3796
|
+
const t = q / p;
|
|
3797
|
+
if (p < 0) t0 = Math.max(t0, t);
|
|
3798
|
+
else t1 = Math.min(t1, t);
|
|
3799
|
+
if (t0 > t1) return 0;
|
|
3800
|
+
}
|
|
3801
|
+
return (t1 - t0) * Math.hypot(dx, dy);
|
|
3802
|
+
}
|
|
3803
|
+
function containsRect(outer, inner) {
|
|
3804
|
+
return inner.x >= outer.x && inner.y >= outer.y && inner.x + inner.w <= outer.x + outer.w && inner.y + inner.h <= outer.y + outer.h;
|
|
3805
|
+
}
|
|
3806
|
+
/**
|
|
3807
|
+
* Nodes drawn around others: frames or groups. Connectors inside them are
|
|
3808
|
+
* expected, so they are no obstacles (and no "through node" warning). Only
|
|
3809
|
+
* a node holding another node that is not text counts: a box with a caption
|
|
3810
|
+
* inside is still a box. Nodes are swept in x order, so the check stays
|
|
3811
|
+
* close to linear on boards where few nodes overlap.
|
|
3812
|
+
*/
|
|
3813
|
+
function containerIds(nodes) {
|
|
3814
|
+
const ids = /* @__PURE__ */ new Set();
|
|
3815
|
+
const sorted = [...nodes].sort((a, b) => a.box.x - b.box.x);
|
|
3816
|
+
for (let i = 0; i < sorted.length; i++) {
|
|
3817
|
+
const outer = sorted[i];
|
|
3818
|
+
const right = outer.box.x + outer.box.w;
|
|
3819
|
+
let j = i;
|
|
3820
|
+
while (j > 0 && sorted[j - 1].box.x >= outer.box.x) j--;
|
|
3821
|
+
for (; j < sorted.length && sorted[j].box.x <= right; j++) {
|
|
3822
|
+
const inner = sorted[j];
|
|
3823
|
+
if (inner === outer || inner.type === "text") continue;
|
|
3824
|
+
if (containsRect(outer.box, inner.box)) {
|
|
3825
|
+
ids.add(outer.id);
|
|
3826
|
+
break;
|
|
3827
|
+
}
|
|
3828
|
+
}
|
|
3829
|
+
}
|
|
3830
|
+
return ids;
|
|
3831
|
+
}
|
|
3832
|
+
/**
|
|
3833
|
+
* Obstacles a route runs through: at least MIN_INSIDE of it inside their
|
|
3834
|
+
* core, its own end nodes excluded. Ids in obstacle order.
|
|
3835
|
+
*/
|
|
3836
|
+
function crossedObstacles(route, obstacles, ends) {
|
|
3837
|
+
const crossed = [];
|
|
3838
|
+
let minX = Infinity;
|
|
3839
|
+
let minY = Infinity;
|
|
3840
|
+
let maxX = -Infinity;
|
|
3841
|
+
let maxY = -Infinity;
|
|
3842
|
+
for (const [x, y] of route) {
|
|
3843
|
+
minX = Math.min(minX, x);
|
|
3844
|
+
minY = Math.min(minY, y);
|
|
3845
|
+
maxX = Math.max(maxX, x);
|
|
3846
|
+
maxY = Math.max(maxY, y);
|
|
3847
|
+
}
|
|
3848
|
+
for (const obstacle of obstacles) {
|
|
3849
|
+
const { core } = obstacle;
|
|
3850
|
+
if (core.x > maxX || core.y > maxY || core.x + core.w < minX || core.y + core.h < minY) continue;
|
|
3851
|
+
if (ends.has(obstacle.id)) continue;
|
|
3852
|
+
let inside = 0;
|
|
3853
|
+
for (let i = 1; i < route.length; i++) inside += lengthInside(route[i - 1], route[i], obstacle.core);
|
|
3854
|
+
if (inside >= 8) crossed.push(obstacle.id);
|
|
3855
|
+
}
|
|
3856
|
+
return crossed;
|
|
3857
|
+
}
|
|
3734
3858
|
//#endregion
|
|
3735
3859
|
//#region ../src/rendering/shape_geometry.ts
|
|
3736
3860
|
/** Samples per cubic segment: enough for smooth snapping at any zoom. */
|
|
@@ -4215,8 +4339,9 @@ function getElementPorts(el) {
|
|
|
4215
4339
|
*/
|
|
4216
4340
|
function resolveArrowEndpoints(el, allElements) {
|
|
4217
4341
|
let { x, y, x2, y2 } = el;
|
|
4218
|
-
const
|
|
4219
|
-
const
|
|
4342
|
+
const byId = elementsById(allElements);
|
|
4343
|
+
const startEl = el.startElementId ? byId.get(el.startElementId) ?? null : null;
|
|
4344
|
+
const endEl = el.endElementId ? byId.get(el.endElementId) ?? null : null;
|
|
4220
4345
|
const startPinned = startEl && el.startAnchor ? anchorToWorld(startEl, el.startAnchor) : null;
|
|
4221
4346
|
const endPinned = endEl && el.endAnchor ? anchorToWorld(endEl, el.endAnchor) : null;
|
|
4222
4347
|
const startB = startEl ? getElementBounds(startEl) : null;
|
|
@@ -4286,7 +4411,7 @@ function sideDirection(el, side) {
|
|
|
4286
4411
|
* floating end moves to the port facing `target`, so flowcharts get clean
|
|
4287
4412
|
* mid-side attachments instead of corner hits.
|
|
4288
4413
|
*/
|
|
4289
|
-
function elbowEnd(el, anchor, pinned, target, targetBox) {
|
|
4414
|
+
function elbowEnd(el, anchor, pinned, target, targetBox, forcedSide) {
|
|
4290
4415
|
const b = getElementBounds(el);
|
|
4291
4416
|
const box = getConnectorBox(el);
|
|
4292
4417
|
const cx = b.x + b.w / 2;
|
|
@@ -4301,7 +4426,7 @@ function elbowEnd(el, anchor, pinned, target, targetBox) {
|
|
|
4301
4426
|
};
|
|
4302
4427
|
}
|
|
4303
4428
|
const [lx, ly] = el.rotation ? rotatePoint(target[0], target[1], cx, cy, -el.rotation) : target;
|
|
4304
|
-
const side = targetBox && !el.rotation ? sideTowardBox(box, targetBox) : sideFacing(lx - cx, ly - cy, b.w / 2, b.h / 2);
|
|
4429
|
+
const side = forcedSide ?? (targetBox && !el.rotation ? sideTowardBox(box, targetBox) : sideFacing(lx - cx, ly - cy, b.w / 2, b.h / 2));
|
|
4305
4430
|
const port = getElementPorts(el)[side];
|
|
4306
4431
|
const dir = sideDirection(el, side);
|
|
4307
4432
|
return {
|
|
@@ -4315,20 +4440,21 @@ function elbowEnd(el, anchor, pinned, target, targetBox) {
|
|
|
4315
4440
|
* elements it is attached to: the side each end leaves by, the port it
|
|
4316
4441
|
* starts from, and a shared coordinate when two flat sides face each other.
|
|
4317
4442
|
*/
|
|
4318
|
-
function planElbowEnds(el, allElements) {
|
|
4443
|
+
function planElbowEnds(el, allElements, sides) {
|
|
4319
4444
|
const pts = resolveArrowEndpoints(el, allElements);
|
|
4320
4445
|
const start = [pts.x, pts.y];
|
|
4321
4446
|
const end = [pts.x2, pts.y2];
|
|
4322
|
-
const
|
|
4323
|
-
const
|
|
4447
|
+
const byId = elementsById(allElements);
|
|
4448
|
+
const startEl = el.startElementId ? byId.get(el.startElementId) : void 0;
|
|
4449
|
+
const endEl = el.endElementId ? byId.get(el.endElementId) : void 0;
|
|
4324
4450
|
const targetOf = (other, otherAnchor, otherPoint) => {
|
|
4325
4451
|
if (!other || otherAnchor) return otherPoint;
|
|
4326
4452
|
const [ocx, ocy] = getElementCenter(other);
|
|
4327
4453
|
return [ocx, ocy];
|
|
4328
4454
|
};
|
|
4329
4455
|
const boxOf = (other, otherAnchor) => other && !otherAnchor && !other.rotation ? getConnectorBox(other) : void 0;
|
|
4330
|
-
const s = startEl ? elbowEnd(startEl, el.startAnchor, start, targetOf(endEl, el.endAnchor, end), boxOf(endEl, el.endAnchor)) : null;
|
|
4331
|
-
const e = endEl ? elbowEnd(endEl, el.endAnchor, end, targetOf(startEl, el.startAnchor, start), boxOf(startEl, el.startAnchor)) : null;
|
|
4456
|
+
const s = startEl ? elbowEnd(startEl, el.startAnchor, start, targetOf(endEl, el.endAnchor, end), boxOf(endEl, el.endAnchor), sides?.start) : null;
|
|
4457
|
+
const e = endEl ? elbowEnd(endEl, el.endAnchor, end, targetOf(startEl, el.startAnchor, start), boxOf(startEl, el.startAnchor), sides?.end) : null;
|
|
4332
4458
|
let aligned = false;
|
|
4333
4459
|
if (s && e && startEl && endEl && !el.startAnchor && !el.endAnchor) {
|
|
4334
4460
|
const shared = sharedOffset(startEl, s.dir, endEl, e.dir);
|
|
@@ -4348,9 +4474,24 @@ function planElbowEnds(el, allElements) {
|
|
|
4348
4474
|
aligned
|
|
4349
4475
|
};
|
|
4350
4476
|
}
|
|
4477
|
+
var byIdCache = /* @__PURE__ */ new WeakMap();
|
|
4478
|
+
/**
|
|
4479
|
+
* Elements of an (immutable) array by id, built once per array: routing a
|
|
4480
|
+
* scene looks up both ends of every connector. The first element with an
|
|
4481
|
+
* id wins, as `Array.find` did.
|
|
4482
|
+
*/
|
|
4483
|
+
function elementsById(allElements) {
|
|
4484
|
+
let map = byIdCache.get(allElements);
|
|
4485
|
+
if (!map) {
|
|
4486
|
+
const built = /* @__PURE__ */ new Map();
|
|
4487
|
+
for (const el of allElements) if (!built.has(el.id)) built.set(el.id, el);
|
|
4488
|
+
map = built;
|
|
4489
|
+
byIdCache.set(allElements, map);
|
|
4490
|
+
}
|
|
4491
|
+
return map;
|
|
4492
|
+
}
|
|
4351
4493
|
/** Key of one connector end in the port slot map. */
|
|
4352
4494
|
var slotKey = (id, which) => `${id}:${which}`;
|
|
4353
|
-
var portSlotCache = /* @__PURE__ */ new WeakMap();
|
|
4354
4495
|
/** Space between two connector ends that share a side. */
|
|
4355
4496
|
var PORT_GAP = 20;
|
|
4356
4497
|
/** Clearance between a spread end and the end of its side (less on short sides). */
|
|
@@ -4405,22 +4546,15 @@ function spreadAlongSide(preferred, lo, hi) {
|
|
|
4405
4546
|
*
|
|
4406
4547
|
* Pinned ends, hidden connectors, self-loops and sides with a single port
|
|
4407
4548
|
* (see sideSpan) keep their planned point, and a side with a single
|
|
4408
|
-
* end is left alone. The result depends only on the
|
|
4409
|
-
* peer computes the same routes
|
|
4410
|
-
* immutable, so a frame resolving N connectors plans each of them once
|
|
4411
|
-
* more, not N times.
|
|
4549
|
+
* end is left alone. The result depends only on the plans, so every live
|
|
4550
|
+
* peer computes the same routes (see routeElbows, which caches them).
|
|
4412
4551
|
*/
|
|
4413
|
-
function elbowPortSlots(
|
|
4414
|
-
const cached = portSlotCache.get(allElements);
|
|
4415
|
-
if (cached) return cached;
|
|
4552
|
+
function elbowPortSlots(planned, steered = /* @__PURE__ */ new Set()) {
|
|
4416
4553
|
const groups = /* @__PURE__ */ new Map();
|
|
4417
4554
|
/** Connectors whose ends sharedOffset put on one coordinate. */
|
|
4418
4555
|
const aligned = [];
|
|
4419
|
-
for (const el of
|
|
4420
|
-
if (el.type !== "line" && el.type !== "arrow") continue;
|
|
4421
|
-
if (el.routing !== "elbow" || el.visible === false) continue;
|
|
4556
|
+
for (const { el, plan } of planned) {
|
|
4422
4557
|
if (el.startElementId && el.startElementId === el.endElementId) continue;
|
|
4423
|
-
const plan = planElbowEnds(el, allElements);
|
|
4424
4558
|
const ends = [[
|
|
4425
4559
|
plan.s,
|
|
4426
4560
|
plan.startEl,
|
|
@@ -4445,6 +4579,7 @@ function elbowPortSlots(allElements) {
|
|
|
4445
4579
|
node,
|
|
4446
4580
|
dir: end.dir,
|
|
4447
4581
|
at: end.point[axis],
|
|
4582
|
+
toward: other[axis],
|
|
4448
4583
|
angle: Math.atan2(other[axis] - end.point[axis], depth),
|
|
4449
4584
|
id: el.id
|
|
4450
4585
|
};
|
|
@@ -4470,7 +4605,8 @@ function elbowPortSlots(allElements) {
|
|
|
4470
4605
|
for (const m of members) {
|
|
4471
4606
|
const partner = partnerOf.get(m.key);
|
|
4472
4607
|
const there = partner === void 0 ? void 0 : placedAt.get(partner);
|
|
4473
|
-
|
|
4608
|
+
const own = steered.has(m.id) && members.length > 1 ? Math.min(Math.max(m.toward, lo), hi) : m.at;
|
|
4609
|
+
preferred.set(m, there !== void 0 && there >= lo && there <= hi ? there : own);
|
|
4474
4610
|
}
|
|
4475
4611
|
const pref = (m) => preferred.get(m);
|
|
4476
4612
|
const grid = (m) => Math.round(pref(m) * 100);
|
|
@@ -4481,7 +4617,6 @@ function elbowPortSlots(allElements) {
|
|
|
4481
4617
|
if (Math.abs(placed[i] - m.at) > .01) slots.set(m.key, placed[i]);
|
|
4482
4618
|
});
|
|
4483
4619
|
}
|
|
4484
|
-
portSlotCache.set(allElements, slots);
|
|
4485
4620
|
return slots;
|
|
4486
4621
|
}
|
|
4487
4622
|
/**
|
|
@@ -4515,21 +4650,286 @@ function sideOf(dir) {
|
|
|
4515
4650
|
* Polyline a connector is drawn along: its two resolved ends, or the full
|
|
4516
4651
|
* orthogonal route for `routing: 'elbow'`. Curved lines (cx/cy) are not
|
|
4517
4652
|
* covered here; callers keep their quadratic path for those.
|
|
4653
|
+
*
|
|
4654
|
+
* Elbows of the scene come from routeElbows (computed once per elements
|
|
4655
|
+
* array). A connector that is not in `allElements` as this very object (a
|
|
4656
|
+
* preview being drawn, an edited copy) is routed on its own; an edited copy
|
|
4657
|
+
* still attached exactly like the scene's connector of that id reuses its
|
|
4658
|
+
* port slots and side choice.
|
|
4518
4659
|
*/
|
|
4519
4660
|
function resolveConnectorRoute(el, allElements) {
|
|
4520
4661
|
if (el.routing !== "elbow") {
|
|
4521
4662
|
const pts = resolveArrowEndpoints(el, allElements);
|
|
4522
4663
|
return [[pts.x, pts.y], [pts.x2, pts.y2]];
|
|
4523
4664
|
}
|
|
4524
|
-
const
|
|
4525
|
-
const
|
|
4526
|
-
|
|
4665
|
+
const scene = routeElbows(allElements);
|
|
4666
|
+
const routed = scene.routes.get(el);
|
|
4667
|
+
if (routed) return routed.map((p) => [p[0], p[1]]);
|
|
4668
|
+
const original = elementsById(allElements).get(el.id);
|
|
4669
|
+
const same = original !== void 0 && (original.type === "line" || original.type === "arrow") && sameAttachment(original, el);
|
|
4670
|
+
return finishRoute({
|
|
4671
|
+
el,
|
|
4672
|
+
plan: planElbowEnds(el, allElements, same ? scene.sides.get(original) : void 0)
|
|
4673
|
+
}, same ? scene.slots : NO_SLOTS);
|
|
4674
|
+
}
|
|
4675
|
+
/** Whether two connectors attach to the same nodes in the same way. */
|
|
4676
|
+
function sameAttachment(a, b) {
|
|
4677
|
+
const anchor = (x) => x ? `${x.x},${x.y}` : "";
|
|
4678
|
+
return a.startElementId === b.startElementId && a.endElementId === b.endElementId && anchor(a.startAnchor) === anchor(b.startAnchor) && anchor(a.endAnchor) === anchor(b.endAnchor);
|
|
4679
|
+
}
|
|
4680
|
+
var routeCache = /* @__PURE__ */ new WeakMap();
|
|
4681
|
+
var NO_SIDES = /* @__PURE__ */ new Map();
|
|
4682
|
+
var NO_SLOTS = /* @__PURE__ */ new Map();
|
|
4683
|
+
/** The route of a planned elbow once its ends sit on their slots. */
|
|
4684
|
+
function finishRoute({ el, plan }, slots) {
|
|
4685
|
+
const s = plan.s && { ...plan.s };
|
|
4686
|
+
const e = plan.e && { ...plan.e };
|
|
4527
4687
|
const startSlot = slots.get(slotKey(el.id, "start"));
|
|
4528
4688
|
const endSlot = slots.get(slotKey(el.id, "end"));
|
|
4529
4689
|
if (s && plan.startEl && startSlot !== void 0) placeAlong(s, plan.startEl, startSlot);
|
|
4530
4690
|
if (e && plan.endEl && endSlot !== void 0) placeAlong(e, plan.endEl, endSlot);
|
|
4531
4691
|
return computeElbowRoute(s?.point ?? plan.start, s?.dir ?? null, e?.point ?? plan.end, e?.dir ?? null, s?.stub, e?.stub);
|
|
4532
4692
|
}
|
|
4693
|
+
/** Plans, port slots and routes of every visible elbow of a scene. */
|
|
4694
|
+
function routeScene(allElements, sides) {
|
|
4695
|
+
const planned = [];
|
|
4696
|
+
for (const el of allElements) {
|
|
4697
|
+
if (el.type !== "line" && el.type !== "arrow") continue;
|
|
4698
|
+
if (el.routing !== "elbow" || el.visible === false) continue;
|
|
4699
|
+
planned.push({
|
|
4700
|
+
el,
|
|
4701
|
+
plan: planElbowEnds(el, allElements, sides.get(el))
|
|
4702
|
+
});
|
|
4703
|
+
}
|
|
4704
|
+
const slots = elbowPortSlots(planned, new Set([...sides.keys()].map((el) => el.id)));
|
|
4705
|
+
const routes = /* @__PURE__ */ new Map();
|
|
4706
|
+
for (const p of planned) routes.set(p.el, finishRoute(p, slots));
|
|
4707
|
+
return {
|
|
4708
|
+
planned,
|
|
4709
|
+
slots,
|
|
4710
|
+
routes
|
|
4711
|
+
};
|
|
4712
|
+
}
|
|
4713
|
+
/**
|
|
4714
|
+
* The nodes routes should not run through (frames and groups excluded). A
|
|
4715
|
+
* rotated node counts with the box around its rotated shape: wider than the
|
|
4716
|
+
* shape itself, so a route that clears it never cuts the real outline.
|
|
4717
|
+
*/
|
|
4718
|
+
function sceneObstacles(allElements) {
|
|
4719
|
+
const nodes = allElements.filter((el) => el.visible !== false && isRouteNodeType(el.type)).map((el) => {
|
|
4720
|
+
const box = getConnectorBox(el);
|
|
4721
|
+
return {
|
|
4722
|
+
el,
|
|
4723
|
+
id: el.id,
|
|
4724
|
+
type: el.type,
|
|
4725
|
+
box: el.rotation ? rotateBoxOf(el, box) : box
|
|
4726
|
+
};
|
|
4727
|
+
});
|
|
4728
|
+
const containers = containerIds(nodes);
|
|
4729
|
+
return nodes.filter((n) => !containers.has(n.id)).map((n) => ({
|
|
4730
|
+
id: n.id,
|
|
4731
|
+
core: nodeCoreRect(n.el.rotation ? "rectangle" : n.type, n.box)
|
|
4732
|
+
}));
|
|
4733
|
+
}
|
|
4734
|
+
/** Nodes each elbow runs through, by connector. */
|
|
4735
|
+
function sceneCrossings(planned, routes, obstacles) {
|
|
4736
|
+
const crossings = /* @__PURE__ */ new Map();
|
|
4737
|
+
for (const { el } of planned) {
|
|
4738
|
+
const ends = new Set([el.startElementId, el.endElementId].filter((id) => id !== void 0));
|
|
4739
|
+
const crossed = crossedObstacles(routes.get(el), obstacles, ends);
|
|
4740
|
+
if (crossed.length > 0) crossings.set(el, crossed);
|
|
4741
|
+
}
|
|
4742
|
+
return crossings;
|
|
4743
|
+
}
|
|
4744
|
+
/**
|
|
4745
|
+
* The facing side pair on the other axis, for a connector the baseline
|
|
4746
|
+
* routes through a node: top and bottom when it left sideways, left and
|
|
4747
|
+
* right when it left vertically. Only for connectors between two distinct,
|
|
4748
|
+
* unrotated nodes with floating ends, and only when the boxes leave room on
|
|
4749
|
+
* that axis for both stubs. Null otherwise.
|
|
4750
|
+
*/
|
|
4751
|
+
function otherAxisSides({ el, plan }) {
|
|
4752
|
+
const { startEl, endEl, s } = plan;
|
|
4753
|
+
if (!startEl || !endEl || !s || startEl === endEl) return null;
|
|
4754
|
+
if (el.startAnchor || el.endAnchor || startEl.rotation || endEl.rotation) return null;
|
|
4755
|
+
const a = getConnectorBox(startEl);
|
|
4756
|
+
const b = getConnectorBox(endEl);
|
|
4757
|
+
const dx = b.x + b.w / 2 - (a.x + a.w / 2);
|
|
4758
|
+
const dy = b.y + b.h / 2 - (a.y + a.h / 2);
|
|
4759
|
+
if (s.dir.dx !== 0) {
|
|
4760
|
+
if (Math.abs(dy) - (a.h + b.h) / 2 < 32) return null;
|
|
4761
|
+
return dy > 0 ? {
|
|
4762
|
+
start: 2,
|
|
4763
|
+
end: 0
|
|
4764
|
+
} : {
|
|
4765
|
+
start: 0,
|
|
4766
|
+
end: 2
|
|
4767
|
+
};
|
|
4768
|
+
}
|
|
4769
|
+
if (Math.abs(dx) - (a.w + b.w) / 2 < 32) return null;
|
|
4770
|
+
return dx > 0 ? {
|
|
4771
|
+
start: 1,
|
|
4772
|
+
end: 3
|
|
4773
|
+
} : {
|
|
4774
|
+
start: 3,
|
|
4775
|
+
end: 1
|
|
4776
|
+
};
|
|
4777
|
+
}
|
|
4778
|
+
/** Number of `(connector, node)` crossings. */
|
|
4779
|
+
function crossingCount(crossings) {
|
|
4780
|
+
let count = 0;
|
|
4781
|
+
for (const nodes of crossings.values()) count += nodes.length;
|
|
4782
|
+
return count;
|
|
4783
|
+
}
|
|
4784
|
+
/** Whether `next` drops some node crossings of `base` and adds none. */
|
|
4785
|
+
function isStrictSubset(next, base) {
|
|
4786
|
+
for (const [el, nodes] of next) {
|
|
4787
|
+
const before = base.get(el);
|
|
4788
|
+
if (!before || nodes.some((node) => !before.includes(node))) return false;
|
|
4789
|
+
}
|
|
4790
|
+
return crossingCount(next) < crossingCount(base);
|
|
4791
|
+
}
|
|
4792
|
+
/**
|
|
4793
|
+
* Most side switches tried in one scene. Beyond it the scene keeps today's
|
|
4794
|
+
* routes: checking a batch costs time with every switch, and a board that
|
|
4795
|
+
* tangled is not fixed by one alternative side pair per connector.
|
|
4796
|
+
*/
|
|
4797
|
+
var MAX_SIDE_SWITCHES = 32;
|
|
4798
|
+
/** Samples of a curved line drawn by its control point. */
|
|
4799
|
+
var CURVE_SAMPLES$2 = 12;
|
|
4800
|
+
/** Every connector's drawn polyline, curves sampled, elbows from `routes`. */
|
|
4801
|
+
function connectorPolylines(allElements, routes) {
|
|
4802
|
+
const lines = /* @__PURE__ */ new Map();
|
|
4803
|
+
for (const el of allElements) {
|
|
4804
|
+
if (el.type !== "line" && el.type !== "arrow" || el.visible === false) continue;
|
|
4805
|
+
const routed = routes.get(el);
|
|
4806
|
+
if (routed) {
|
|
4807
|
+
lines.set(el, routed);
|
|
4808
|
+
continue;
|
|
4809
|
+
}
|
|
4810
|
+
const pts = resolveArrowEndpoints(el, allElements);
|
|
4811
|
+
if (el.type === "line" && el.cx !== void 0 && el.cy !== void 0) {
|
|
4812
|
+
const { cx, cy } = el;
|
|
4813
|
+
lines.set(el, Array.from({ length: 13 }, (_, i) => {
|
|
4814
|
+
const p = getQuadraticPoint(pts.x, pts.y, cx, cy, pts.x2, pts.y2, i / CURVE_SAMPLES$2);
|
|
4815
|
+
return [p.x, p.y];
|
|
4816
|
+
}));
|
|
4817
|
+
} else lines.set(el, [[pts.x, pts.y], [pts.x2, pts.y2]]);
|
|
4818
|
+
}
|
|
4819
|
+
return lines;
|
|
4820
|
+
}
|
|
4821
|
+
/**
|
|
4822
|
+
* Whether segments a-b and c-d cross. A segment's start counts when it is
|
|
4823
|
+
* an inner vertex of its polyline (`aInner`, `cInner`), so a crossing right
|
|
4824
|
+
* on a vertex is seen once, while connectors meeting at their own ends
|
|
4825
|
+
* (on a node's border) are not crossings.
|
|
4826
|
+
*/
|
|
4827
|
+
function segmentsCross$1(a, b, c, d, aInner, cInner) {
|
|
4828
|
+
const rx = b[0] - a[0];
|
|
4829
|
+
const ry = b[1] - a[1];
|
|
4830
|
+
const sx = d[0] - c[0];
|
|
4831
|
+
const sy = d[1] - c[1];
|
|
4832
|
+
const den = rx * sy - ry * sx;
|
|
4833
|
+
if (Math.abs(den) < 1e-9) return false;
|
|
4834
|
+
const u = ((c[0] - a[0]) * sy - (c[1] - a[1]) * sx) / den;
|
|
4835
|
+
const v = ((c[0] - a[0]) * ry - (c[1] - a[1]) * rx) / den;
|
|
4836
|
+
const slack = 1e-6;
|
|
4837
|
+
const within = (t, inner) => (inner ? t > -1e-6 : t > slack) && t < 1 - slack;
|
|
4838
|
+
return within(u, aInner) && within(v, cInner);
|
|
4839
|
+
}
|
|
4840
|
+
/** Shortest shared stretch of two collinear segments that reads as one line. */
|
|
4841
|
+
var MIN_OVERLAP$1 = 2;
|
|
4842
|
+
/**
|
|
4843
|
+
* Whether two axis-aligned segments run along the same line for a while:
|
|
4844
|
+
* drawn on top of each other they read as one connector.
|
|
4845
|
+
*/
|
|
4846
|
+
function segmentsOverlap(a, b, c, d) {
|
|
4847
|
+
const vertical = (p, q) => Math.abs(p[0] - q[0]) < .5;
|
|
4848
|
+
const horizontal = (p, q) => Math.abs(p[1] - q[1]) < .5;
|
|
4849
|
+
let along;
|
|
4850
|
+
if (vertical(a, b) && vertical(c, d) && Math.abs(a[0] - c[0]) < .5) along = 1;
|
|
4851
|
+
else if (horizontal(a, b) && horizontal(c, d) && Math.abs(a[1] - c[1]) < .5) along = 0;
|
|
4852
|
+
else return false;
|
|
4853
|
+
const lo = Math.max(Math.min(a[along], b[along]), Math.min(c[along], d[along]));
|
|
4854
|
+
return Math.min(Math.max(a[along], b[along]), Math.max(c[along], d[along])) - lo > MIN_OVERLAP$1;
|
|
4855
|
+
}
|
|
4856
|
+
/**
|
|
4857
|
+
* Crossings and shared stretches between the `changed` connectors and every
|
|
4858
|
+
* other connector.
|
|
4859
|
+
*/
|
|
4860
|
+
function connectorCrossings(lines, changed, limit = Infinity) {
|
|
4861
|
+
let count = 0;
|
|
4862
|
+
const all = [...lines.entries()];
|
|
4863
|
+
for (let ia = 0; ia < all.length; ia++) {
|
|
4864
|
+
const [a, pa] = all[ia];
|
|
4865
|
+
if (!changed.has(a)) continue;
|
|
4866
|
+
for (let ib = 0; ib < all.length; ib++) {
|
|
4867
|
+
const [b, pb] = all[ib];
|
|
4868
|
+
if (ib === ia || changed.has(b) && ib < ia) continue;
|
|
4869
|
+
for (let i = 1; i < pa.length; i++) for (let j = 1; j < pb.length; j++) if (segmentsCross$1(pa[i - 1], pa[i], pb[j - 1], pb[j], i > 1, j > 1) || segmentsOverlap(pa[i - 1], pa[i], pb[j - 1], pb[j])) {
|
|
4870
|
+
count++;
|
|
4871
|
+
if (count > limit) return count;
|
|
4872
|
+
}
|
|
4873
|
+
}
|
|
4874
|
+
}
|
|
4875
|
+
return count;
|
|
4876
|
+
}
|
|
4877
|
+
/**
|
|
4878
|
+
* Routes every visible elbow of a scene, once per (immutable) elements
|
|
4879
|
+
* array. The baseline is today's routing: side choice by free gap, ports
|
|
4880
|
+
* spread on shared sides, routes from their ends. A connector that runs
|
|
4881
|
+
* through a node may switch to the facing side pair on the other axis (a
|
|
4882
|
+
* fan-out leaving its hub by the bottom instead of sideways), when that
|
|
4883
|
+
* route alone crosses nothing. All switches are then applied together, the
|
|
4884
|
+
* ports spread again, and the result kept only if its node crossings are a
|
|
4885
|
+
* strict subset of the baseline's and the connectors whose routes changed
|
|
4886
|
+
* cross or run along no more connectors than before; otherwise the
|
|
4887
|
+
* baseline stands.
|
|
4888
|
+
* Scenes whose routes cross nothing are untouched, and the outcome depends
|
|
4889
|
+
* only on the scene, so live peers agree.
|
|
4890
|
+
*/
|
|
4891
|
+
function routeElbows(allElements) {
|
|
4892
|
+
const cached = routeCache.get(allElements);
|
|
4893
|
+
if (cached) return cached;
|
|
4894
|
+
const base = routeScene(allElements, NO_SIDES);
|
|
4895
|
+
let result = {
|
|
4896
|
+
...base,
|
|
4897
|
+
sides: NO_SIDES
|
|
4898
|
+
};
|
|
4899
|
+
const candidates = base.planned.filter((p) => otherAxisSides(p) !== null);
|
|
4900
|
+
if (candidates.length > 0) {
|
|
4901
|
+
const obstacles = sceneObstacles(allElements);
|
|
4902
|
+
const baseCrossings = sceneCrossings(base.planned, base.routes, obstacles);
|
|
4903
|
+
const proposals = /* @__PURE__ */ new Map();
|
|
4904
|
+
for (const p of candidates) {
|
|
4905
|
+
if (!baseCrossings.has(p.el)) continue;
|
|
4906
|
+
const pair = otherAxisSides(p);
|
|
4907
|
+
const alone = {
|
|
4908
|
+
el: p.el,
|
|
4909
|
+
plan: planElbowEnds(p.el, allElements, pair)
|
|
4910
|
+
};
|
|
4911
|
+
const route = finishRoute(alone, NO_SLOTS);
|
|
4912
|
+
if (sceneCrossings([alone], /* @__PURE__ */ new Map([[p.el, route]]), obstacles).size === 0) proposals.set(p.el, pair);
|
|
4913
|
+
}
|
|
4914
|
+
if (proposals.size > 0 && proposals.size <= MAX_SIDE_SWITCHES) {
|
|
4915
|
+
const next = routeScene(allElements, proposals);
|
|
4916
|
+
if (isStrictSubset(sceneCrossings(next.planned, next.routes, obstacles), baseCrossings)) {
|
|
4917
|
+
const changed = new Set(next.planned.filter((p) => {
|
|
4918
|
+
const a = base.routes.get(p.el);
|
|
4919
|
+
const b = next.routes.get(p.el);
|
|
4920
|
+
return a.length !== b.length || a.some((pt, i) => pt[0] !== b[i][0] || pt[1] !== b[i][1]);
|
|
4921
|
+
}).map((p) => p.el));
|
|
4922
|
+
const before = connectorCrossings(connectorPolylines(allElements, base.routes), changed);
|
|
4923
|
+
if (connectorCrossings(connectorPolylines(allElements, next.routes), changed, before) <= before) result = {
|
|
4924
|
+
...next,
|
|
4925
|
+
sides: proposals
|
|
4926
|
+
};
|
|
4927
|
+
}
|
|
4928
|
+
}
|
|
4929
|
+
}
|
|
4930
|
+
routeCache.set(allElements, result);
|
|
4931
|
+
return result;
|
|
4932
|
+
}
|
|
4533
4933
|
/**
|
|
4534
4934
|
* Stretch of the side a connector leaves `el` by where several ends can
|
|
4535
4935
|
* spread, along that side (x for top/bottom, y for left/right): a flat side
|
|
@@ -4744,7 +5144,7 @@ function segmentsOf$1(points) {
|
|
|
4744
5144
|
return points.slice(1).map((p, i) => [points[i], p]);
|
|
4745
5145
|
}
|
|
4746
5146
|
/** Length of the part of segment a→b inside `r` (Liang-Barsky clipping). */
|
|
4747
|
-
function clippedLength
|
|
5147
|
+
function clippedLength(a, b, r) {
|
|
4748
5148
|
const dx = b[0] - a[0];
|
|
4749
5149
|
const dy = b[1] - a[1];
|
|
4750
5150
|
let t0 = 0;
|
|
@@ -4849,7 +5249,7 @@ function bestBox(ownerId, points, w, h, strokes, placed, nodes) {
|
|
|
4849
5249
|
h: r.h - 2
|
|
4850
5250
|
};
|
|
4851
5251
|
let cost = 0;
|
|
4852
|
-
for (const s of strokes) if (s.owner !== ownerId) cost += clippedLength
|
|
5252
|
+
for (const s of strokes) if (s.owner !== ownerId) cost += clippedLength(s.seg[0], s.seg[1], inner) * 20;
|
|
4853
5253
|
for (const other of placed) cost += overlapArea(r, rectOf(other)) * 2;
|
|
4854
5254
|
for (const node of nodes) cost += overlapArea(inner, node) * 2;
|
|
4855
5255
|
cost += Math.max(0, -room) * 2;
|
|
@@ -5347,7 +5747,7 @@ function escapeXml(str) {
|
|
|
5347
5747
|
}
|
|
5348
5748
|
//#endregion
|
|
5349
5749
|
//#region package.json
|
|
5350
|
-
var version = "0.
|
|
5750
|
+
var version = "0.10.0";
|
|
5351
5751
|
//#endregion
|
|
5352
5752
|
//#region ../src/core/layout.ts
|
|
5353
5753
|
/** Space kept on both sides of a label between ranks. */
|
|
@@ -7872,6 +8272,17 @@ function elementLabel(el) {
|
|
|
7872
8272
|
if (el.type === "text") return el.content;
|
|
7873
8273
|
return "label" in el && el.label ? el.label : "";
|
|
7874
8274
|
}
|
|
8275
|
+
/**
|
|
8276
|
+
* Whether two versions of an element hold the same values, one level deep:
|
|
8277
|
+
* enough to tell a no-op patch (a label set to what it was) from an edit.
|
|
8278
|
+
* Nested values (points, styles) compare by reference, so a rewritten array
|
|
8279
|
+
* counts as a change even when equal: at worst one element too many.
|
|
8280
|
+
*/
|
|
8281
|
+
function sameFields(a, b) {
|
|
8282
|
+
const aKeys = Object.keys(a);
|
|
8283
|
+
if (aKeys.length !== Object.keys(b).length) return false;
|
|
8284
|
+
return aKeys.every((key) => a[key] === b[key]);
|
|
8285
|
+
}
|
|
7875
8286
|
var Diagram = class {
|
|
7876
8287
|
history;
|
|
7877
8288
|
/** Keys given by the agent; other elements get derived keys on read. */
|
|
@@ -7951,13 +8362,48 @@ var Diagram = class {
|
|
|
7951
8362
|
}
|
|
7952
8363
|
}
|
|
7953
8364
|
/** `findId`, or the error the model can act on. */
|
|
8365
|
+
/**
|
|
8366
|
+
* Element ids of the nodes `keys` names (as diagram_read shows them).
|
|
8367
|
+
* Throws on an unknown key, like every tool that takes keys.
|
|
8368
|
+
*/
|
|
8369
|
+
idsOfKeys(keys) {
|
|
8370
|
+
return keys.map((key) => this.idOf(key));
|
|
8371
|
+
}
|
|
7954
8372
|
idOf(key) {
|
|
7955
8373
|
const id = this.findId(key);
|
|
7956
8374
|
if (id !== void 0) return id;
|
|
7957
8375
|
throw new DiagramError(`Unknown node "${key}". Call diagram_read to see the keys.`);
|
|
7958
8376
|
}
|
|
8377
|
+
/** See onLocalChange. */
|
|
8378
|
+
changeListeners = /* @__PURE__ */ new Set();
|
|
8379
|
+
/**
|
|
8380
|
+
* Notified after each edit this diagram makes itself (never a remote one
|
|
8381
|
+
* from a live room) with the ids of the elements it created or really
|
|
8382
|
+
* changed (an edit that leaves every field as it was does not count), and
|
|
8383
|
+
* of those it deleted.
|
|
8384
|
+
*/
|
|
8385
|
+
onLocalChange(cb) {
|
|
8386
|
+
this.changeListeners.add(cb);
|
|
8387
|
+
return () => {
|
|
8388
|
+
this.changeListeners.delete(cb);
|
|
8389
|
+
};
|
|
8390
|
+
}
|
|
7959
8391
|
dispatch(command) {
|
|
8392
|
+
if (this.changeListeners.size === 0) {
|
|
8393
|
+
this.history.dispatch(command);
|
|
8394
|
+
return;
|
|
8395
|
+
}
|
|
8396
|
+
const before = new Map(this.elements.map((el) => [el.id, el]));
|
|
7960
8397
|
this.history.dispatch(command);
|
|
8398
|
+
const changed = /* @__PURE__ */ new Set();
|
|
8399
|
+
for (const el of this.elements) {
|
|
8400
|
+
const old = before.get(el.id);
|
|
8401
|
+
before.delete(el.id);
|
|
8402
|
+
if (old !== el && !(old && sameFields(old, el))) changed.add(el.id);
|
|
8403
|
+
}
|
|
8404
|
+
const removed = new Set(before.keys());
|
|
8405
|
+
if (changed.size === 0 && removed.size === 0) return;
|
|
8406
|
+
for (const listener of this.changeListeners) listener(changed, removed);
|
|
7961
8407
|
}
|
|
7962
8408
|
/** Bottom-left of the current content, where new nodes start a row. */
|
|
7963
8409
|
nextRowOrigin() {
|
|
@@ -8926,6 +9372,13 @@ async function writeFileAtomic(path, data) {
|
|
|
8926
9372
|
var MAX_INBOX = 20;
|
|
8927
9373
|
/** Delivered request ids the agent may still answer with `reply`. */
|
|
8928
9374
|
var MAX_ANSWERABLE = 100;
|
|
9375
|
+
/**
|
|
9376
|
+
* How long the agent must stay idle before the room hears it: a model that
|
|
9377
|
+
* calls live_wait_request again right after a timeout never shows as idle.
|
|
9378
|
+
*/
|
|
9379
|
+
var IDLE_GRACE_MS = 5e3;
|
|
9380
|
+
/** Elements remembered per request; well above what a reply carries. */
|
|
9381
|
+
var MAX_TRACKED_ELEMENTS = 500;
|
|
8929
9382
|
var ROOM_ID = /^[a-zA-Z0-9_-]{3,64}$/;
|
|
8930
9383
|
var AGENT_COLOR = "#c77dff";
|
|
8931
9384
|
var JOIN_TIMEOUT_MS = 1e4;
|
|
@@ -8994,7 +9447,21 @@ var LiveLink = class LiveLink {
|
|
|
8994
9447
|
selfId = "";
|
|
8995
9448
|
open = false;
|
|
8996
9449
|
inbox = [];
|
|
8997
|
-
|
|
9450
|
+
/**
|
|
9451
|
+
* Delivered request ids the agent may answer, each with the elements the
|
|
9452
|
+
* agent changed for it (sent with the reply), or null once that cannot be
|
|
9453
|
+
* told: edits made while several requests waited for an answer could be
|
|
9454
|
+
* for any of them, so none of them claims those edits.
|
|
9455
|
+
*/
|
|
9456
|
+
answerable = /* @__PURE__ */ new Map();
|
|
9457
|
+
/** Delivered requests still waiting for their first reply. */
|
|
9458
|
+
unanswered = /* @__PURE__ */ new Set();
|
|
9459
|
+
/** live_wait_request calls currently waiting. */
|
|
9460
|
+
waiting = 0;
|
|
9461
|
+
/** The activity the room last heard from this agent. */
|
|
9462
|
+
activitySent = null;
|
|
9463
|
+
idleTimer = null;
|
|
9464
|
+
stopTracking = null;
|
|
8998
9465
|
/** Pending `waitForRequests` calls, woken by a request or a disconnect. */
|
|
8999
9466
|
waiters = /* @__PURE__ */ new Set();
|
|
9000
9467
|
/** Set while replaying a remote undo/redo, which History re-emits. */
|
|
@@ -9004,6 +9471,76 @@ var LiveLink = class LiveLink {
|
|
|
9004
9471
|
this.room = room;
|
|
9005
9472
|
this.url = `${origin}/?live=${encodeURIComponent(room)}`;
|
|
9006
9473
|
}
|
|
9474
|
+
/**
|
|
9475
|
+
* Attributes the agent's own edits to the request it is working on, when
|
|
9476
|
+
* only one waits for an answer (see `answerable`). Deleted elements leave
|
|
9477
|
+
* every request, so they never fill the bound.
|
|
9478
|
+
*/
|
|
9479
|
+
noteEdited(changed, removed) {
|
|
9480
|
+
for (const requestId of this.answerable.keys()) {
|
|
9481
|
+
const touched = this.answerable.get(requestId);
|
|
9482
|
+
for (const id of removed) touched?.delete(id);
|
|
9483
|
+
}
|
|
9484
|
+
if (changed.size === 0) return;
|
|
9485
|
+
if (this.unanswered.size > 1) {
|
|
9486
|
+
for (const requestId of this.unanswered) this.answerable.set(requestId, null);
|
|
9487
|
+
return;
|
|
9488
|
+
}
|
|
9489
|
+
for (const requestId of this.unanswered) {
|
|
9490
|
+
const touched = this.answerable.get(requestId);
|
|
9491
|
+
if (!touched) continue;
|
|
9492
|
+
for (const id of changed) {
|
|
9493
|
+
if (touched.size >= MAX_TRACKED_ELEMENTS) break;
|
|
9494
|
+
touched.add(id);
|
|
9495
|
+
}
|
|
9496
|
+
}
|
|
9497
|
+
}
|
|
9498
|
+
/**
|
|
9499
|
+
* What the agent is doing for the people in the room: waiting for
|
|
9500
|
+
* requests, busy with delivered ones still unanswered, or neither.
|
|
9501
|
+
*/
|
|
9502
|
+
currentActivity() {
|
|
9503
|
+
if (this.waiting > 0) return "listening";
|
|
9504
|
+
if (this.unanswered.size > 0) return "working";
|
|
9505
|
+
return "idle";
|
|
9506
|
+
}
|
|
9507
|
+
/**
|
|
9508
|
+
* Tells the room when the activity changes. Going idle waits
|
|
9509
|
+
* IDLE_GRACE_MS, and is dropped if the agent is busy again by then.
|
|
9510
|
+
*/
|
|
9511
|
+
updateActivity() {
|
|
9512
|
+
if (!this.open) return;
|
|
9513
|
+
const next = this.currentActivity();
|
|
9514
|
+
if (next === "idle" && this.activitySent !== "idle") {
|
|
9515
|
+
if (this.idleTimer) return;
|
|
9516
|
+
this.idleTimer = setTimeout(() => {
|
|
9517
|
+
this.idleTimer = null;
|
|
9518
|
+
if (this.currentActivity() === "idle") this.sendActivity("idle");
|
|
9519
|
+
}, IDLE_GRACE_MS);
|
|
9520
|
+
this.idleTimer.unref?.();
|
|
9521
|
+
return;
|
|
9522
|
+
}
|
|
9523
|
+
this.clearIdleTimer();
|
|
9524
|
+
if (next !== this.activitySent) this.sendActivity(next);
|
|
9525
|
+
}
|
|
9526
|
+
sendActivity(activity) {
|
|
9527
|
+
this.activitySent = activity;
|
|
9528
|
+
this.send({
|
|
9529
|
+
type: "presence",
|
|
9530
|
+
name: this.name,
|
|
9531
|
+
color: AGENT_COLOR,
|
|
9532
|
+
activity
|
|
9533
|
+
});
|
|
9534
|
+
}
|
|
9535
|
+
clearIdleTimer() {
|
|
9536
|
+
if (this.idleTimer) clearTimeout(this.idleTimer);
|
|
9537
|
+
this.idleTimer = null;
|
|
9538
|
+
}
|
|
9539
|
+
/** Stops attributing edits (the session is over). */
|
|
9540
|
+
untrack() {
|
|
9541
|
+
this.stopTracking?.();
|
|
9542
|
+
this.stopTracking = null;
|
|
9543
|
+
}
|
|
9007
9544
|
static async join(diagram, link, name) {
|
|
9008
9545
|
const { room, origin } = parseLiveLink(link);
|
|
9009
9546
|
const live = new LiveLink(diagram, room, origin, name);
|
|
@@ -9055,18 +9592,21 @@ var LiveLink = class LiveLink {
|
|
|
9055
9592
|
const taken = this.inbox;
|
|
9056
9593
|
this.inbox = [];
|
|
9057
9594
|
for (const request of taken) {
|
|
9058
|
-
this.answerable.
|
|
9059
|
-
this.
|
|
9060
|
-
type: "agent_status",
|
|
9061
|
-
id: request.id,
|
|
9062
|
-
status: "received"
|
|
9063
|
-
});
|
|
9595
|
+
this.answerable.set(request.id, /* @__PURE__ */ new Set());
|
|
9596
|
+
this.unanswered.add(request.id);
|
|
9064
9597
|
}
|
|
9065
9598
|
while (this.answerable.size > MAX_ANSWERABLE) {
|
|
9066
|
-
const oldest = this.answerable.
|
|
9599
|
+
const oldest = this.answerable.keys().next().value;
|
|
9067
9600
|
if (oldest === void 0) break;
|
|
9068
9601
|
this.answerable.delete(oldest);
|
|
9602
|
+
this.unanswered.delete(oldest);
|
|
9069
9603
|
}
|
|
9604
|
+
this.updateActivity();
|
|
9605
|
+
for (const request of taken) this.send({
|
|
9606
|
+
type: "agent_status",
|
|
9607
|
+
id: request.id,
|
|
9608
|
+
status: "received"
|
|
9609
|
+
});
|
|
9070
9610
|
return taken;
|
|
9071
9611
|
}
|
|
9072
9612
|
/**
|
|
@@ -9074,29 +9614,49 @@ var LiveLink = class LiveLink {
|
|
|
9074
9614
|
* after `timeoutMs` or when the room disconnects.
|
|
9075
9615
|
*/
|
|
9076
9616
|
async waitForRequests(timeoutMs) {
|
|
9077
|
-
if (this.inbox.length === 0 && this.open)
|
|
9078
|
-
|
|
9079
|
-
|
|
9080
|
-
|
|
9081
|
-
|
|
9082
|
-
|
|
9083
|
-
|
|
9084
|
-
|
|
9085
|
-
|
|
9617
|
+
if (this.inbox.length === 0 && this.open) {
|
|
9618
|
+
this.waiting++;
|
|
9619
|
+
this.updateActivity();
|
|
9620
|
+
await new Promise((resolve) => {
|
|
9621
|
+
const wake = () => {
|
|
9622
|
+
clearTimeout(timer);
|
|
9623
|
+
this.waiters.delete(wake);
|
|
9624
|
+
this.waiting--;
|
|
9625
|
+
resolve();
|
|
9626
|
+
};
|
|
9627
|
+
const timer = setTimeout(wake, timeoutMs);
|
|
9628
|
+
this.waiters.add(wake);
|
|
9629
|
+
});
|
|
9630
|
+
}
|
|
9086
9631
|
return this.takeRequests();
|
|
9087
9632
|
}
|
|
9088
|
-
/**
|
|
9089
|
-
|
|
9633
|
+
/**
|
|
9634
|
+
* Sends an answer to the person behind a delivered request, pointing at
|
|
9635
|
+
* `elements` when given, else at the elements the agent changed for it.
|
|
9636
|
+
* Returns how many elements the reply points at, and whether the changes
|
|
9637
|
+
* could not be told apart from another request's (`ambiguous`).
|
|
9638
|
+
*/
|
|
9639
|
+
reply(id, text, elements) {
|
|
9090
9640
|
if (!this.open) throw new LiveError("Not connected to the live room.");
|
|
9091
9641
|
if (!this.answerable.has(id)) throw new LiveError(`Unknown request id "${id}".`);
|
|
9642
|
+
const touched = this.answerable.get(id) ?? null;
|
|
9092
9643
|
const trimmed = text.trim();
|
|
9093
9644
|
if (!trimmed || trimmed.length > 4e3) throw new LiveError(`A reply needs 1 to ${MAX_AGENT_REPLY_CHARS} characters.`);
|
|
9645
|
+
const present = new Set(this.diagram.elements.map((el) => el.id));
|
|
9646
|
+
const ids = sanitizeReplyElements([...elements ?? touched ?? []].filter((elementId) => present.has(elementId)));
|
|
9647
|
+
this.unanswered.delete(id);
|
|
9648
|
+
this.updateActivity();
|
|
9094
9649
|
this.send({
|
|
9095
9650
|
type: "agent_status",
|
|
9096
9651
|
id,
|
|
9097
9652
|
status: "replied",
|
|
9098
|
-
text: trimmed
|
|
9653
|
+
text: trimmed,
|
|
9654
|
+
...ids.length > 0 ? { elements: ids } : {}
|
|
9099
9655
|
});
|
|
9656
|
+
return {
|
|
9657
|
+
pointed: ids.length,
|
|
9658
|
+
ambiguous: !elements && touched === null
|
|
9659
|
+
};
|
|
9100
9660
|
}
|
|
9101
9661
|
wakeWaiters() {
|
|
9102
9662
|
for (const wake of [...this.waiters]) wake();
|
|
@@ -9170,7 +9730,10 @@ var LiveLink = class LiveLink {
|
|
|
9170
9730
|
history.setOnCommand((command) => {
|
|
9171
9731
|
if (this.open && isSessionCommand(command)) this.publish(command);
|
|
9172
9732
|
});
|
|
9733
|
+
this.untrack();
|
|
9734
|
+
this.stopTracking = this.diagram.onLocalChange((changed, removed) => this.noteEdited(changed, removed));
|
|
9173
9735
|
this.open = true;
|
|
9736
|
+
this.sendActivity(this.currentActivity());
|
|
9174
9737
|
resolve({
|
|
9175
9738
|
seeded,
|
|
9176
9739
|
discarded
|
|
@@ -9196,6 +9759,8 @@ var LiveLink = class LiveLink {
|
|
|
9196
9759
|
this.open = false;
|
|
9197
9760
|
this.outbox.clear();
|
|
9198
9761
|
history.setOnCommand(void 0);
|
|
9762
|
+
this.untrack();
|
|
9763
|
+
this.clearIdleTimer();
|
|
9199
9764
|
this.wakeWaiters();
|
|
9200
9765
|
});
|
|
9201
9766
|
socket.addEventListener("error", () => {
|
|
@@ -9220,6 +9785,8 @@ var LiveLink = class LiveLink {
|
|
|
9220
9785
|
this.outbox.drain();
|
|
9221
9786
|
this.open = false;
|
|
9222
9787
|
this.wakeWaiters();
|
|
9788
|
+
this.untrack();
|
|
9789
|
+
this.clearIdleTimer();
|
|
9223
9790
|
this.diagram.history.setOnCommand(void 0);
|
|
9224
9791
|
this.socket?.close();
|
|
9225
9792
|
this.socket = null;
|
|
@@ -9247,8 +9814,6 @@ var LiveLink = class LiveLink {
|
|
|
9247
9814
|
};
|
|
9248
9815
|
//#endregion
|
|
9249
9816
|
//#region src/warnings.ts
|
|
9250
|
-
/** Shortest run of a connector inside a node that counts as crossing it. */
|
|
9251
|
-
var MIN_INSIDE = 8;
|
|
9252
9817
|
/** Smallest overlap, in px on each axis, worth reporting. */
|
|
9253
9818
|
var MIN_OVERLAP = 2;
|
|
9254
9819
|
/** Crossings this close to a segment end are joins, not crossings. */
|
|
@@ -9274,56 +9839,11 @@ function polylineOf(el, elements) {
|
|
|
9274
9839
|
function segmentsOf(points) {
|
|
9275
9840
|
return points.slice(1).map((p, i) => [points[i], p]);
|
|
9276
9841
|
}
|
|
9277
|
-
/**
|
|
9278
|
-
* Part of a node a connector must not cross: the rectangle inscribed in an
|
|
9279
|
-
* ellipse or rhombus (so passing near a corner of the bounding box is not
|
|
9280
|
-
* reported), the connector box otherwise, inset by a couple of pixels so
|
|
9281
|
-
* a stroke running along the border doesn't count.
|
|
9282
|
-
*/
|
|
9283
|
-
function coreRect(el) {
|
|
9284
|
-
const b = getConnectorBox(el);
|
|
9285
|
-
const scale = el.type === "ellipse" ? Math.SQRT1_2 : el.type === "rhombus" ? .5 : 1;
|
|
9286
|
-
const w = b.w * scale - 4;
|
|
9287
|
-
const h = b.h * scale - 4;
|
|
9288
|
-
return {
|
|
9289
|
-
x: b.x + (b.w - w) / 2,
|
|
9290
|
-
y: b.y + (b.h - h) / 2,
|
|
9291
|
-
w: Math.max(0, w),
|
|
9292
|
-
h: Math.max(0, h)
|
|
9293
|
-
};
|
|
9294
|
-
}
|
|
9295
|
-
/** Length of segment a-b inside `r` (Liang-Barsky clipping). */
|
|
9296
|
-
function clippedLength(a, b, r) {
|
|
9297
|
-
const dx = b[0] - a[0];
|
|
9298
|
-
const dy = b[1] - a[1];
|
|
9299
|
-
let t0 = 0;
|
|
9300
|
-
let t1 = 1;
|
|
9301
|
-
const edges = [
|
|
9302
|
-
[-dx, a[0] - r.x],
|
|
9303
|
-
[dx, r.x + r.w - a[0]],
|
|
9304
|
-
[-dy, a[1] - r.y],
|
|
9305
|
-
[dy, r.y + r.h - a[1]]
|
|
9306
|
-
];
|
|
9307
|
-
for (const [p, q] of edges) {
|
|
9308
|
-
if (Math.abs(p) < 1e-9) {
|
|
9309
|
-
if (q < 0) return 0;
|
|
9310
|
-
continue;
|
|
9311
|
-
}
|
|
9312
|
-
const t = q / p;
|
|
9313
|
-
if (p < 0) t0 = Math.max(t0, t);
|
|
9314
|
-
else t1 = Math.min(t1, t);
|
|
9315
|
-
if (t0 > t1) return 0;
|
|
9316
|
-
}
|
|
9317
|
-
return (t1 - t0) * Math.hypot(dx, dy);
|
|
9318
|
-
}
|
|
9319
9842
|
function overlaps(a, b) {
|
|
9320
9843
|
const w = Math.min(a.x + a.w, b.x + b.w) - Math.max(a.x, b.x);
|
|
9321
9844
|
const h = Math.min(a.y + a.h, b.y + b.h) - Math.max(a.y, b.y);
|
|
9322
9845
|
return w >= MIN_OVERLAP && h >= MIN_OVERLAP;
|
|
9323
9846
|
}
|
|
9324
|
-
function contains(outer, inner) {
|
|
9325
|
-
return inner.x >= outer.x && inner.y >= outer.y && inner.x + inner.w <= outer.x + outer.w && inner.y + inner.h <= outer.y + outer.h;
|
|
9326
|
-
}
|
|
9327
9847
|
/**
|
|
9328
9848
|
* Whether two segments cross away from their ends. Connectors that share a
|
|
9329
9849
|
* node meet at its border, and an elbow's bends meet the next segment: both
|
|
@@ -9349,9 +9869,13 @@ function findWarnings(diagram) {
|
|
|
9349
9869
|
const keys = diagram.nodeKeys();
|
|
9350
9870
|
const nodes = elements.filter((el) => keys.has(el.id) && el.visible !== false);
|
|
9351
9871
|
const boxes = new Map(nodes.map((el) => [el.id, getConnectorBox(el)]));
|
|
9352
|
-
const containers =
|
|
9872
|
+
const containers = containerIds(nodes.map((el) => ({
|
|
9873
|
+
id: el.id,
|
|
9874
|
+
box: boxes.get(el.id),
|
|
9875
|
+
type: el.type
|
|
9876
|
+
})));
|
|
9353
9877
|
const solid = nodes.filter((el) => !containers.has(el.id));
|
|
9354
|
-
const cores = new Map(solid.map((el) => [el.id,
|
|
9878
|
+
const cores = new Map(solid.map((el) => [el.id, nodeCoreRect(el.type, boxes.get(el.id))]));
|
|
9355
9879
|
const edges = [];
|
|
9356
9880
|
for (const el of elements) {
|
|
9357
9881
|
if (!isConnector(el) || el.visible === false) continue;
|
|
@@ -9373,7 +9897,7 @@ function findWarnings(diagram) {
|
|
|
9373
9897
|
for (const edge of edges) for (const node of solid) {
|
|
9374
9898
|
if (node.id === edge.el.startElementId || node.id === edge.el.endElementId) continue;
|
|
9375
9899
|
const core = cores.get(node.id);
|
|
9376
|
-
if (edge.segments.reduce((sum, [a, b]) => sum +
|
|
9900
|
+
if (edge.segments.reduce((sum, [a, b]) => sum + lengthInside(a, b, core), 0) >= 8) throughNodes.push({
|
|
9377
9901
|
edge: edge.name,
|
|
9378
9902
|
node: keys.get(node.id)
|
|
9379
9903
|
});
|
|
@@ -9419,7 +9943,7 @@ function findWarnings(diagram) {
|
|
|
9419
9943
|
const ba = boxes.get(a.id);
|
|
9420
9944
|
for (const b of nodes.slice(i + 1)) {
|
|
9421
9945
|
const bb = boxes.get(b.id);
|
|
9422
|
-
if (overlaps(ba, bb) && !
|
|
9946
|
+
if (overlaps(ba, bb) && !containsRect(ba, bb) && !containsRect(bb, ba)) nodeOverlaps.push([keys.get(a.id), keys.get(b.id)]);
|
|
9423
9947
|
}
|
|
9424
9948
|
});
|
|
9425
9949
|
return {
|
|
@@ -9948,15 +10472,17 @@ server.registerTool("live_wait_request", {
|
|
|
9948
10472
|
}));
|
|
9949
10473
|
server.registerTool("live_reply", {
|
|
9950
10474
|
title: "Reply to a live request",
|
|
9951
|
-
description: "Answer a request from a person in the live room. The answer appears
|
|
10475
|
+
description: "Answer a request from a person in the live room. The answer appears in their chat with you, with a button that selects and zooms to the elements you changed for that request. When you handle several requests at once, pass nodes: changes made while more than one request is open are not attributed to any of them. Keep it short; the board itself shows what you changed.",
|
|
9952
10476
|
inputSchema: {
|
|
9953
10477
|
requestId: z.string().min(1).max(64).describe("Id of the request"),
|
|
9954
|
-
text: z.string().min(1).max(MAX_AGENT_REPLY_CHARS).describe("Short answer shown to the person")
|
|
10478
|
+
text: z.string().min(1).max(MAX_AGENT_REPLY_CHARS).describe("Short answer shown to the person"),
|
|
10479
|
+
nodes: z.array(z.string().min(1)).max(50).optional().describe("Keys of the nodes the answer is about (see diagram_read). Defaults to the elements you changed for this request.")
|
|
9955
10480
|
}
|
|
9956
|
-
}, guarded(({ requestId, text }) => {
|
|
10481
|
+
}, guarded(({ requestId, text, nodes }) => {
|
|
9957
10482
|
if (!live) return "Not in a live session.";
|
|
9958
|
-
live.reply(requestId, text);
|
|
9959
|
-
return "Reply sent.";
|
|
10483
|
+
const { pointed, ambiguous } = live.reply(requestId, text, nodes ? diagram.idsOfKeys(nodes) : void 0);
|
|
10484
|
+
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
|
+
return pointed > 0 ? `Reply sent, pointing at ${pointed} element${pointed === 1 ? "" : "s"}.` : "Reply sent.";
|
|
9960
10486
|
}));
|
|
9961
10487
|
await server.connect(new StdioServerTransport());
|
|
9962
10488
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@markasso/mcp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
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",
|