Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Team map hand-offs: arrow at the receiving bot, go around cards in between

AdityaUmale2079da21a992
diff --git a/src/components/TeamCanvas.settle.dom.test.ts b/src/components/TeamCanvas.settle.dom.test.ts
index c16285bc59..94b45425e9 100644
--- a/src/components/TeamCanvas.settle.dom.test.ts
+++ b/src/components/TeamCanvas.settle.dom.test.ts
@@ -160,4 +160,5 @@ it("draws a line between bots that hand work to each other, labelled with what t
expect(lines.map((line) => line.dataset.state)).toEqual(["running", "connected"]);
expect(lines.every((line) => /^M [\d.]+ [\d.]+ C /.test(line.getAttribute("d") ?? ""))).toBe(true);
expect([...world.querySelectorAll("[data-handoff-label]")].map((label) => label.textContent)).toEqual(["Running"]);
+ expect(world.querySelectorAll("[data-handoff-arrow]")).toHaveLength(1);
});
diff --git a/src/components/TeamCanvas.tsx b/src/components/TeamCanvas.tsx
index a1bda5ae25..f16381f709 100644
--- a/src/components/TeamCanvas.tsx
+++ b/src/components/TeamCanvas.tsx
@@ -520,6 +520,7 @@ export function TeamCanvas({ sections, canManage, onMove, onInstructions, onMemo
{handoffs.map((line) => <path key={line.key} data-handoff-line data-state={line.state} d={line.path} fill="none" strokeLinecap="round"
strokeWidth={line.state === "connected" ? 1.5 : 2.25} strokeDasharray={line.state === "connected" ? undefined : "7 5"}
className={line.state === "running" ? "animate-link-flow stroke-success/80" : line.state === "queued" ? "stroke-warning/80" : "stroke-ink-secondary/35"} />)}
+ {handoffs.map((line) => line.arrow && <path key={`${line.key}:arrow`} data-handoff-arrow d={line.arrow} className={line.state === "running" ? "fill-success/80" : "fill-warning/80"} />)}
</svg>
{handoffs.filter((line) => line.state !== "connected").map((line) => <span key={line.key} data-handoff-label style={{ left: line.label.x, top: line.label.y }}
className={cn("absolute -translate-x-1/2 -translate-y-1/2 whitespace-nowrap rounded-full bg-panel px-2 py-0.5 text-[11px] font-medium shadow-sm ring-1",
diff --git a/src/lib/team-canvas-handoffs.test.ts b/src/lib/team-canvas-handoffs.test.ts
index c152d07aef..e66a3e145d 100644
--- a/src/lib/team-canvas-handoffs.test.ts
+++ b/src/lib/team-canvas-handoffs.test.ts
@@ -36,9 +36,21 @@ describe("handoffLines", () => {
it("joins side-by-side cards from the giver's facing edge to the receiver's, labelled mid-way", () => {
expect(handoffLines([{ sourceBotId: "a", targetBotId: "b", state: "running" }], boxes)).toEqual([{
- key: "a:b", state: "running", path: "M 100 25 C 200 25 200 125 300 125", label: { x: 200, y: 75 },
+ key: "a:b", state: "running", path: "M 100 25 C 200 25 200 125 300 125", arrow: "M 300 125 L 291 129.5 L 291 120.5 Z", label: { x: 200, y: 75 },
}]);
- expect(handoffLines([{ sourceBotId: "b", targetBotId: "a", state: "queued" }], boxes)[0].path).toBe("M 300 125 C 200 125 200 25 100 25");
+ const back = handoffLines([{ sourceBotId: "b", targetBotId: "a", state: "queued" }], boxes)[0];
+ expect(back.path).toBe("M 300 125 C 200 125 200 25 100 25");
+ expect(back.arrow).toBe("M 100 25 L 109 20.5 L 109 29.5 Z");
+ });
+
+ it("goes around a card standing between the two bots, label and all", () => {
+ const stack = new Map([["a", box(0, 0)], ["b", box(0, 100)], ["c", box(0, 200)]]);
+ const [line] = handoffLines([{ sourceBotId: "a", targetBotId: "c", state: "running" }], stack);
+ expect(line.path).toBe("M 100 25 C 148 25 148 225 100 225");
+ expect(line.arrow).toBe("M 100 225 L 109 220.5 L 109 229.5 Z");
+ expect(line.label).toEqual({ x: 136, y: 125 });
+ expect(line.label.x).toBeGreaterThan(stack.get("b")!.x + stack.get("b")!.width);
+ expect(handoffLines([{ sourceBotId: "a", targetBotId: "b", state: "running" }], stack)[0].path).toBe("M 50 50 C 50 75 50 75 50 100");
});
it("joins stacked cards bottom to top, and skips bots that are not on the canvas", () => {
diff --git a/src/lib/team-canvas-handoffs.ts b/src/lib/team-canvas-handoffs.ts
index dad3fe4d66..2615e26b6c 100644
--- a/src/lib/team-canvas-handoffs.ts
+++ b/src/lib/team-canvas-handoffs.ts
@@ -8,6 +8,8 @@ export interface HandoffLine {
key: string;
state: TeamMapEdge["state"];
path: string;
+ /** A filled arrow at the receiving bot, for running and queued hand-offs. */
+ arrow?: string;
/** Where the line's label sits: the middle of the curve. */
label: Point;
}
@@ -43,28 +45,94 @@ export function botCardBoxes(
return boxes;
}
-/** A curved line from the bot handing work on to the bot receiving it, edge to facing edge. */
+const DETOUR = 48;
+const ARROW_LENGTH = 9, ARROW_HALF_WIDTH = 4.5;
+
+interface Curve { start: Point; first: Point; second: Point; end: Point }
+
+const curvePoint = ({ start, first, second, end }: Curve, t: number): Point => {
+ const u = 1 - t;
+ return {
+ x: u * u * u * start.x + 3 * u * u * t * first.x + 3 * u * t * t * second.x + t * t * t * end.x,
+ y: u * u * u * start.y + 3 * u * u * t * first.y + 3 * u * t * t * second.y + t * t * t * end.y,
+ };
+};
+
+const inside = (point: Point, box: CardBox) => point.x > box.x && point.x < box.x + box.width && point.y > box.y && point.y < box.y + box.height;
+
+/** Whether the curve runs through a card it does not start or end at. */
+const crossesCards = (curve: Curve, others: CardBox[]) => others.some((box) => {
+ for (let step = 1; step < 20; step++) if (inside(curvePoint(curve, step / 20), box)) return true;
+ return false;
+});
+
+/** Edge to facing edge, bending half-way between the two cards. */
+function directCurve(from: CardBox, to: CardBox): Curve {
+ const sideBySide = from.x + from.width <= to.x || to.x + to.width <= from.x;
+ const forward = sideBySide ? from.x < to.x : from.y < to.y;
+ const anchor = (box: CardBox, leaving: boolean): Point => {
+ const far = leaving === forward;
+ return sideBySide
+ ? { x: far ? box.x + box.width : box.x, y: box.y + box.height / 2 }
+ : { x: box.x + box.width / 2, y: far ? box.y + box.height : box.y };
+ };
+ const start = anchor(from, true), end = anchor(to, false);
+ const bend = sideBySide ? (end.x - start.x) / 2 : (end.y - start.y) / 2;
+ const first = sideBySide ? { x: start.x + bend, y: start.y } : { x: start.x, y: start.y + bend };
+ const second = sideBySide ? { x: end.x - bend, y: end.y } : { x: end.x, y: end.y - bend };
+ return { start, first, second, end };
+}
+
+/** Out of one side of both cards and back in, clear of every card in between. */
+function detours(from: CardBox, to: CardBox, blocking: CardBox[]): Curve[] {
+ const all = [from, to, ...blocking];
+ const right = Math.max(...all.map((box) => box.x + box.width)) + DETOUR, left = Math.min(...all.map((box) => box.x)) - DETOUR;
+ const bottom = Math.max(...all.map((box) => box.y + box.height)) + DETOUR, top = Math.min(...all.map((box) => box.y)) - DETOUR;
+ const middleY = (box: CardBox) => box.y + box.height / 2, middleX = (box: CardBox) => box.x + box.width / 2;
+ const sideways = (edge: (box: CardBox) => number, out: number): Curve => ({
+ start: { x: edge(from), y: middleY(from) }, first: { x: out, y: middleY(from) },
+ second: { x: out, y: middleY(to) }, end: { x: edge(to), y: middleY(to) },
+ });
+ const upDown = (edge: (box: CardBox) => number, out: number): Curve => ({
+ start: { x: middleX(from), y: edge(from) }, first: { x: middleX(from), y: out },
+ second: { x: middleX(to), y: out }, end: { x: middleX(to), y: edge(to) },
+ });
+ return [
+ sideways((box) => box.x + box.width, right), sideways((box) => box.x, left),
+ upDown((box) => box.y + box.height, bottom), upDown((box) => box.y, top),
+ ];
+}
+
+/** A small filled triangle at the receiving end, pointing the way the work goes. */
+function arrowHead({ second, end, start }: Curve): string {
+ const from = second.x === end.x && second.y === end.y ? start : second;
+ const length = Math.hypot(end.x - from.x, end.y - from.y) || 1;
+ const dx = (end.x - from.x) / length, dy = (end.y - from.y) / length;
+ const baseX = end.x - dx * ARROW_LENGTH, baseY = end.y - dy * ARROW_LENGTH;
+ const round = (value: number) => Math.round(value * 100) / 100;
+ return `M ${round(end.x)} ${round(end.y)} L ${round(baseX - dy * ARROW_HALF_WIDTH)} ${round(baseY + dx * ARROW_HALF_WIDTH)} L ${round(baseX + dy * ARROW_HALF_WIDTH)} ${round(baseY - dx * ARROW_HALF_WIDTH)} Z`;
+}
+
+/**
+ * A curved line from the bot handing work on to the bot receiving it, edge to facing edge.
+ * When that curve would run through another bot's card it goes around the cards instead.
+ * Running and queued hand-offs end in an arrow at the receiving bot; a plain connection has none.
+ */
export function handoffLines(edges: TeamMapEdge[], boxes: Map<string, CardBox>): HandoffLine[] {
return edges.flatMap((edge) => {
const from = boxes.get(edge.sourceBotId), to = boxes.get(edge.targetBotId);
if (!from || !to || edge.sourceBotId === edge.targetBotId) return [];
- const sideBySide = from.x + from.width <= to.x || to.x + to.width <= from.x;
- const forward = sideBySide ? from.x < to.x : from.y < to.y;
- const anchor = (box: CardBox, leaving: boolean): Point => {
- const far = leaving === forward;
- return sideBySide
- ? { x: far ? box.x + box.width : box.x, y: box.y + box.height / 2 }
- : { x: box.x + box.width / 2, y: far ? box.y + box.height : box.y };
- };
- const start = anchor(from, true), end = anchor(to, false);
- const bend = sideBySide ? (end.x - start.x) / 2 : (end.y - start.y) / 2;
- const first = sideBySide ? { x: start.x + bend, y: start.y } : { x: start.x, y: start.y + bend };
- const second = sideBySide ? { x: end.x - bend, y: end.y } : { x: end.x, y: end.y - bend };
+ const others = [...boxes].filter(([id]) => id !== edge.sourceBotId && id !== edge.targetBotId).map(([, box]) => box);
+ const direct = directCurve(from, to);
+ const blocking = others.filter((box) => crossesCards(direct, [box]));
+ const curve = blocking.length ? detours(from, to, blocking).find((route) => !crossesCards(route, others)) ?? direct : direct;
+ const { start, first, second, end } = curve;
return [{
key: `${edge.sourceBotId}:${edge.targetBotId}`,
state: edge.state,
path: `M ${start.x} ${start.y} C ${first.x} ${first.y} ${second.x} ${second.y} ${end.x} ${end.y}`,
- label: { x: (start.x + 3 * first.x + 3 * second.x + end.x) / 8, y: (start.y + 3 * first.y + 3 * second.y + end.y) / 8 },
+ ...(edge.state === "connected" ? {} : { arrow: arrowHead(curve) }),
+ label: curvePoint(curve, 0.5),
}];
});
}