kaizen-agent 1.59.1 → 1.60.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/cli/add-project.test.ts +169 -0
- package/cli/add-project.ts +132 -0
- package/cli/board.test.ts +15 -0
- package/cli/chat.ts +1 -1
- package/cli/dashboard.ts +6 -2
- package/cli/install.test.ts +1 -1
- package/cli/install.ts +18 -9
- package/cli/pty.test.ts +82 -3
- package/cli/pty.ts +122 -6
- package/cli/restart.test.ts +169 -0
- package/cli/settings.test.ts +35 -0
- package/cli/settings.ts +69 -18
- package/cli/state.test.ts +119 -3
- package/cli/state.ts +171 -13
- package/cli/usage.test.ts +130 -1
- package/cli/web-accept.test.ts +54 -0
- package/cli/web-symlink.test.ts +28 -0
- package/cli/web.ts +320 -100
- package/cli/worktree.test.ts +43 -1
- package/cli/worktree.ts +97 -2
- package/cli/writeback.ts +1 -1
- package/package.json +1 -1
- package/skills/kaizen/SKILL.md +5 -1
- package/skills/kaizen/config.default.yml +15 -2
- package/skills/kaizen/settings.md +1 -0
- package/skills/kaizen/spec-main.md +28 -2
- package/skills/kaizen-help/SKILL.md +1 -0
- package/web/board/add-project.ts +136 -0
- package/web/board/api.ts +1 -1
- package/web/board/badge.ts +5 -5
- package/web/board/cards.ts +11 -3
- package/web/board/context.ts +1 -0
- package/web/board/fixes.ts +3 -2
- package/web/board/git.ts +2 -0
- package/web/board/home.ts +7 -1
- package/web/board/ideas.ts +20 -3
- package/web/board/live.ts +1 -1
- package/web/board/main.ts +26 -9
- package/web/board/panel.ts +18 -3
- package/web/board/phone.ts +26 -3
- package/web/board/projects.ts +6 -85
- package/web/board/run-pick.ts +41 -0
- package/web/board/run-view.ts +261 -104
- package/web/board/settings.ts +42 -5
- package/web/board/store.ts +3 -1
- package/web/board/toast.ts +4 -1
- package/web/board/views.ts +6 -2
- package/web/board/workspace.ts +125 -38
- package/web/board/ws-board-ui.ts +84 -0
- package/web/board/ws-board.test.ts +71 -0
- package/web/board/ws-board.ts +45 -0
- package/web/board/ws-tabs.test.ts +171 -0
- package/web/board/ws-tabs.ts +170 -0
- package/web/board.html +92 -43
- package/web/board.js +44 -43
- package/web/notebook.js +24 -25
- package/web/notes.html +26 -21
- package/web/term.js +1 -1
- package/web/video.html +26 -21
- package/web/video.js +56 -57
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { expect, test } from "bun:test";
|
|
2
|
+
import { agentStatus, agentsHtml, forgetAgents, lineOf, settleIn, toggleOpen, wsAgents, SETTLE_MS, awaiting, dueWord, chosen, loopbackUrl, toOpen, stageKeys, stageTabs, finished, historyOf, liveIn, migrate, pickable, wsKey } from "./ws-tabs.ts";
|
|
3
|
+
|
|
4
|
+
const run = (id: string, status: string, o: any = {}) => ({ kind: "run", state: "/p/.kaizen", id, status, moved: 1, ws: "/p", ...o });
|
|
5
|
+
const wsOf = (c) => c.ws;
|
|
6
|
+
const cards = [
|
|
7
|
+
run("a", "waiting"),
|
|
8
|
+
run("b", "building"),
|
|
9
|
+
run("c", "done", { moved: 5 }),
|
|
10
|
+
run("d", "abandoned", { moved: 9 }),
|
|
11
|
+
run("e", "done", { accept: "needs" }), // finished, waiting on an approval: still a tab
|
|
12
|
+
run("f", "building", { ws: "/p/.kaizen/wt/x" }),
|
|
13
|
+
run("g", "done", { moved: 7, archived: true }),
|
|
14
|
+
{ kind: "idea", state: "/p/.kaizen", text: "t", kind: "run", pty: { key: "k" }, ws: "/p" },
|
|
15
|
+
{ kind: "idea", state: "/p/.kaizen", text: "no agent", ws: "/p" },
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
test("live runs get terminals; an idea only with its agent; a run waiting on approval stays", () => {
|
|
19
|
+
expect(liveIn(cards, "/p/.kaizen", "/p", wsOf).map((c) => c.id ?? c.text)).toEqual(["a", "b", "e", "t"]);
|
|
20
|
+
expect(liveIn(cards, "/p/.kaizen", "/p/.kaizen/wt/x", wsOf).map((c) => c.id)).toEqual(["f"]);
|
|
21
|
+
expect(finished(cards[4])).toBe(false);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test("History: done and abandoned runs of the workspace, newest first, nothing else", () => {
|
|
25
|
+
expect(historyOf(cards, "/p/.kaizen", "/p", wsOf).map((c) => c.id)).toEqual(["d", "g", "c"]);
|
|
26
|
+
expect(historyOf(cards, "/p/.kaizen", "/p/.kaizen/wt/x", wsOf)).toEqual([]);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("migrate: a run's saved layout moves to its workspace; unknown keys stay; nothing is lost", () => {
|
|
30
|
+
const key = (c) => (c.kind === "idea" ? "idea:" + c.state + ":" + c.text : "run:" + c.state + ":" + c.id);
|
|
31
|
+
const all: any = {
|
|
32
|
+
"run:/p/.kaizen:a": { tabs: [{ id: "local" }], hidden: ["agent:1"], screens: [{ id: "s1", tree: "local", focus: "local" }], on: "s1" },
|
|
33
|
+
"run:/p/.kaizen:b": { tabs: [{ id: "local" }, { id: "preview" }], hidden: [], screens: [{ id: "s2", tree: { dir: "cols", kids: ["local", "preview"], sizes: [1, 1] }, focus: "local" }], on: "s2" },
|
|
34
|
+
"run:/p/.kaizen:gone": { tabs: [{ id: "note:x" }], hidden: [], screens: [], on: null },
|
|
35
|
+
"kz-other": { tabs: [], hidden: [], screens: [], on: null },
|
|
36
|
+
};
|
|
37
|
+
expect(migrate(all, cards, key, wsOf)).toBe(true);
|
|
38
|
+
const to = all[wsKey("/p/.kaizen", "/p")];
|
|
39
|
+
expect(to.tabs.map((t) => t.id)).toEqual(["local", "preview"]);
|
|
40
|
+
expect(to.hidden).toEqual(["agent:1"]);
|
|
41
|
+
// "local" is in the first screen already: the second keeps only "preview"
|
|
42
|
+
expect(to.screens.map((s) => s.tree)).toEqual(["local", "preview"]);
|
|
43
|
+
expect(to.on).toBe("s1");
|
|
44
|
+
expect(Object.keys(all).sort()).toEqual(["kz-other", "run:/p/.kaizen:gone", wsKey("/p/.kaizen", "/p")].sort());
|
|
45
|
+
expect(migrate(all, cards, key, wsOf)).toBe(false); // once
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test("awaiting: waiting, accept-needed and asking runs are not closable; a building run is", () => {
|
|
49
|
+
expect(awaiting(run("a", "waiting"))).toBe(true);
|
|
50
|
+
expect(awaiting(run("e", "done", { accept: "needs" }))).toBe(true);
|
|
51
|
+
expect(awaiting(run("p", "building", { kind: "run", pty: { alive: true, asking: true } }))).toBe(true);
|
|
52
|
+
expect(awaiting(run("b", "building"))).toBe(false);
|
|
53
|
+
expect(awaiting(run("c", "done"))).toBe(false);
|
|
54
|
+
});
|
|
55
|
+
test("stageKeys: recorded impl/review terminals; plan is the unclaimed first agent until a stage is launched", () => {
|
|
56
|
+
expect(stageKeys({ kind: "run", column: 1, pty: { all: ["a"] } })).toEqual({ plan: "a" });
|
|
57
|
+
// A lone unrecorded session is named by the run's current stage, never a wrong one; plain when that is unknown.
|
|
58
|
+
expect(stageKeys({ kind: "run", column: 2, pty: { all: ["a"] } })).toEqual({ impl: "a" });
|
|
59
|
+
expect(stageKeys({ kind: "run", column: 3, pty: { all: ["a"] } })).toEqual({ review: "a" });
|
|
60
|
+
expect(stageKeys({ kind: "run", column: 4, pty: { all: ["a"] } })).toEqual({});
|
|
61
|
+
expect(stageTabs({ kind: "run", column: 2, pty: { all: ["a"] } }, "Claude Code")).toEqual([{ key: "a", title: "Build · Claude Code" }]);
|
|
62
|
+
expect(stageTabs({ kind: "run", column: 4, pty: { all: ["a"] } }, "Claude Code")).toEqual([{ key: "a", title: "Claude Code" }]);
|
|
63
|
+
expect(stageKeys({ kind: "run", pty: { all: ["a", "b"], stages: { impl: "b" } } })).toEqual({ impl: "b" });
|
|
64
|
+
expect(stageKeys({ kind: "run", pty: { all: ["b", "c"], stages: { impl: "b", review: "c" } } })).toEqual({ impl: "b", review: "c" });
|
|
65
|
+
expect(stageKeys({ kind: "run", pty: { all: ["x"], stages: { impl: "gone" } } })).toEqual({});
|
|
66
|
+
expect(stageKeys({ kind: "run", pty: { all: ["p", "b"], stages: { plan: "p", impl: "b" } } })).toEqual({ plan: "p", impl: "b" });
|
|
67
|
+
expect(stageKeys({ kind: "run", pty: { all: ["p", "x"], stages: { plan: "p" } } })).toEqual({ plan: "p" });
|
|
68
|
+
expect(stageKeys({ kind: "idea", pty: { all: ["a"] } })).toEqual({});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("loopbackUrl: the first localhost address in a Preview; other hosts and none give undefined", () => {
|
|
72
|
+
expect(loopbackUrl("Run `bun dev`, open http://localhost:5173/app) now")).toBe("http://localhost:5173/app");
|
|
73
|
+
expect(loopbackUrl("see [x](http://127.0.0.1:8080)")).toBe("http://127.0.0.1:8080");
|
|
74
|
+
expect(loopbackUrl("http://[::1]:3000/")).toBe("http://[::1]:3000/");
|
|
75
|
+
expect(loopbackUrl("https://example.com/ and ")).toBeUndefined();
|
|
76
|
+
expect(loopbackUrl(null)).toBeUndefined();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("toOpen: a run that moves into review or done opens the Browser once; others do not", () => {
|
|
80
|
+
const cs = [run("a", "running", { column: 2 }), run("b", "waiting", { column: 3 }), run("c", "done", { column: 4 }), run("e", "abandoned", { column: 4 })];
|
|
81
|
+
const key = (c) => c.id;
|
|
82
|
+
expect(toOpen(cs, new Set(), [], key).map(key)).toEqual(["b", "c"]);
|
|
83
|
+
expect(toOpen(cs, new Set(["b"]), [], key).map(key)).toEqual(["c"]); // b was already reviewing
|
|
84
|
+
expect(toOpen(cs, new Set(), ["c"], key).map(key)).toEqual(["b"]); // c already opened once
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("pickable: waiting first, then newest; done+approved, abandoned, archived and ideas are not listed", () => {
|
|
88
|
+
const cs = [run("old", "building", { moved: 1 }), run("new", "building", { moved: 9 }), run("w", "waiting", { moved: 2 }), run("e", "done", { accept: "needs", moved: 3 }), ...cards.slice(2, 4), cards[6], cards[7], cards[5]];
|
|
89
|
+
expect(pickable(cs, "/p/.kaizen", "/p", wsOf).map((c) => c.id)).toEqual(["e", "w", "new", "old"]);
|
|
90
|
+
expect(pickable(cs, "/p/.kaizen", "/p/.kaizen/wt/x", wsOf).map((c) => c.id)).toEqual(["f"]);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test("chosen: the pick stays while pickable or History, else the first, else none", () => {
|
|
94
|
+
const live = [run("w", "waiting"), run("b", "building")],
|
|
95
|
+
hist = [run("d", "done")];
|
|
96
|
+
expect(chosen(live, hist, "b")?.id).toBe("b"); // sticky: a waiting run does not replace it
|
|
97
|
+
expect(chosen(live, hist, "d")?.id).toBe("d"); // History pick, read-only
|
|
98
|
+
expect(chosen(live, hist, "gone")?.id).toBe("w");
|
|
99
|
+
expect(chosen(live, hist)?.id).toBe("w"); // a fresh visit drops the pick: back to waiting first
|
|
100
|
+
expect(chosen([], hist, "x")).toBeNull();
|
|
101
|
+
expect(chosen([], [], null)).toBeNull();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test("stageTabs: stage terminals are named by stage, the rest numbered by the agent", () => {
|
|
105
|
+
const c = { kind: "run", pty: { all: ["p", "b", "x", "y"], stages: { plan: "p", impl: "b" } } };
|
|
106
|
+
expect(stageTabs(c, "Claude")).toEqual([{ key: "p", title: "Plan · Claude" }, { key: "b", title: "Build · Claude" }, { key: "x", title: "Claude 1" }, { key: "y", title: "Claude 2" }]);
|
|
107
|
+
expect(stageTabs({ kind: "run", pty: { all: ["r", "z"], stages: { impl: "z", review: "r" } } }, "Codex").map((t) => t.title)).toEqual(["Review · Codex", "Build · Codex"]);
|
|
108
|
+
expect(stageTabs({ kind: "idea", pty: { key: "k" } }, "Claude")).toEqual([{ key: "k", title: "Claude" }]);
|
|
109
|
+
expect(stageTabs({ kind: "run" }, "Claude")).toEqual([]);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("a done, approved run with a commit or push still to do stays pickable (waiting); once pushed it is History", () => {
|
|
113
|
+
const cs = [run("u", "done", { git: "pending", moved: 1 }), run("p", "done", { git: "pushed" }), run("n", "done", { accept: "needs", moved: 0 }), run("x", "done", { git: "pending", archived: true })];
|
|
114
|
+
expect(pickable(cs, "/p/.kaizen", "/p", wsOf).map((c) => c.id)).toEqual(["u", "n"]);
|
|
115
|
+
expect(historyOf(cs, "/p/.kaizen", "/p", wsOf).map((c) => c.id)).toEqual(["p", "x"]);
|
|
116
|
+
expect(dueWord(cs[0])).toBe("commit");
|
|
117
|
+
expect(dueWord({ ...cs[0], gitDue: "push" })).toBe("push");
|
|
118
|
+
expect(awaiting(cs[0])).toBe(true);
|
|
119
|
+
expect(awaiting(cs[1])).toBe(false);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
const ag = (key: string, o: any = {}) => ({ key, dir: "/p/.kaizen", ws: "/p", agent: "claude", name: "Claude", asking: false, busy: false, ...o });
|
|
123
|
+
|
|
124
|
+
test("agent status: working at once, done only after 3 s quiet, asking first and immediate", () => {
|
|
125
|
+
const seen = new Map<string, number>();
|
|
126
|
+
expect(agentStatus(ag("a", { busy: true }), 0, seen)).toBe("working");
|
|
127
|
+
expect(agentStatus({ ...ag("a"), born: 5 }, 100, seen)).toBe("done"); // the same key, started again: not the old one
|
|
128
|
+
expect(agentStatus(ag("a"), 1000, seen)).toBe("working"); // a burst, then 1 s of silence
|
|
129
|
+
expect(settleIn([ag("a")], 1000, seen)).toBe(SETTLE_MS - 1000);
|
|
130
|
+
expect(agentStatus(ag("a"), SETTLE_MS - 1, seen)).toBe("working");
|
|
131
|
+
expect(agentStatus(ag("a"), SETTLE_MS, seen)).toBe("done");
|
|
132
|
+
expect(settleIn([ag("a")], SETTLE_MS, seen)).toBe(Infinity);
|
|
133
|
+
expect(agentStatus(ag("a", { busy: true }), 5000, seen)).toBe("working");
|
|
134
|
+
expect(agentStatus(ag("a", { asking: true, busy: true }), 5001, seen)).toBe("asking");
|
|
135
|
+
expect(agentStatus(ag("never"), 1, seen)).toBe("done"); // an idle agent never seen busy
|
|
136
|
+
forgetAgents([ag("never")], seen);
|
|
137
|
+
expect([...seen.keys()]).toEqual([]);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
test("a workspace's agents: its own only, asking then working then done; a lost worktree falls back to the main row", () => {
|
|
141
|
+
const seen = new Map<string, number>(),
|
|
142
|
+
wsOf = (c) => (c.ws === "/gone" ? "/p" : c.ws);
|
|
143
|
+
const list = [ag("d1"), ag("w1", { busy: true }), ag("q1", { asking: true }), ag("x", { ws: "/p/wt" }), ag("y", { dir: "/o/.kaizen" }), ag("g", { ws: "/gone" })];
|
|
144
|
+
expect(wsAgents(list, "/p/.kaizen", "/p", wsOf, 0, seen).map((r) => r.a.key + ":" + r.st)).toEqual(["q1:asking", "w1:working", "d1:done", "g:done"]);
|
|
145
|
+
expect(wsAgents(list, "/p/.kaizen", "/p/wt", wsOf, 0, seen).map((r) => r.a.key)).toEqual(["x"]);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test("expanded state toggles per workspace", () => {
|
|
149
|
+
const m: Record<string, true> = {};
|
|
150
|
+
expect(Object.keys(toggleOpen(m, "/p/.kaizen", "/p"))).toEqual(["/p/.kaizen\0/p"]);
|
|
151
|
+
expect(toggleOpen(m, "/p/.kaizen", "/p")).toEqual({});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
test("card html: one agent has no chevron; several group chips; expanded lists rows; the typed or agent line is escaped", () => {
|
|
155
|
+
const rows = (...a: any[]) => wsAgents(a, "/p/.kaizen", "/p", (c) => c.ws, 0, new Map());
|
|
156
|
+
const one = String(agentsHtml(rows(ag("a", { at: 1 })), 1e5, false, "/p/.kaizen", "/p"));
|
|
157
|
+
expect(one).toContain("Done - Claude");
|
|
158
|
+
expect(one).not.toContain("wchev");
|
|
159
|
+
const many = String(agentsHtml(rows(ag("a"), ag("b", { busy: true }), ag("c", { agent: "x", name: "Other" })), 0, false, "/p/.kaizen", "/p"));
|
|
160
|
+
expect(many.match(/class="chip"/g)!.length).toBe(2); // working, done
|
|
161
|
+
expect(many.indexOf("si build")).toBeLessThan(many.indexOf("si done"));
|
|
162
|
+
expect(many.match(/data-term=/g)!.length).toBe(3);
|
|
163
|
+
expect(many).toContain('aria-expanded="false"');
|
|
164
|
+
const evil = '<img src=x onerror="alert(1)"> & <b>x</b>';
|
|
165
|
+
const open = String(agentsHtml(rows(ag("a", { last: evil }), ag("b")), 0, true, "/p/.kaizen", "/p"));
|
|
166
|
+
expect(open).toContain("2 agents");
|
|
167
|
+
expect(open).toContain("<img src=x onerror="alert(1)"> & <b>x</b>");
|
|
168
|
+
expect(open).not.toContain("<img");
|
|
169
|
+
expect(open).not.toContain("<b>");
|
|
170
|
+
expect(lineOf(rows(ag("z"))[0])).toBe("Done - Claude");
|
|
171
|
+
});
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
// The workspace centre's logic without the DOM, so it is tested alone (ws-tabs.test.ts):
|
|
2
|
+
// which runs the panel's picker lists, which are History, how stage terminals are named, and
|
|
3
|
+
// how the old per-run tab layouts move to their workspace, and the sidebar card's agent chips and rows.
|
|
4
|
+
// A screen's layout is a tmux-style tree: a leaf is an item id; a split holds two or more
|
|
5
|
+
// children side by side ("cols") or stacked ("rows"), with their relative sizes.
|
|
6
|
+
import { html } from "./dom.ts";
|
|
7
|
+
import { age } from "./labels.ts";
|
|
8
|
+
export type Node = string | { dir: "cols" | "rows"; kids: Node[]; sizes: number[] };
|
|
9
|
+
export const leaves = (n: Node): string[] => (typeof n === "string" ? [n] : n.kids.flatMap(leaves));
|
|
10
|
+
// Drops ids no longer open; a split left with one child becomes that child.
|
|
11
|
+
export function prune(n: Node, keep: (id: string) => boolean): Node | null {
|
|
12
|
+
if (typeof n === "string") return keep(n) ? n : null;
|
|
13
|
+
const kids: Node[] = [],
|
|
14
|
+
sizes: number[] = [];
|
|
15
|
+
n.kids.forEach((k, i) => {
|
|
16
|
+
const p = prune(k, keep);
|
|
17
|
+
if (p) (kids.push(p), sizes.push(n.sizes[i] ?? 1));
|
|
18
|
+
});
|
|
19
|
+
return !kids.length ? null : kids.length === 1 ? kids[0] : { dir: n.dir, kids, sizes };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// A workspace's key in the saved tab layouts (localStorage kz-runtabs).
|
|
23
|
+
export const wsKey = (dir: string, ws: string) => `ws:${dir}:${ws}`;
|
|
24
|
+
// A done, approved run whose commit or push is still to do (the card's git mark): it stays in the picker.
|
|
25
|
+
export const gitDue = (c) => c.kind === "run" && c.status === "done" && c.accept !== "needs" && !c.archived && c.git === "pending";
|
|
26
|
+
export const dueWord = (c) => (c.gitDue === "push" ? "push" : "commit");
|
|
27
|
+
// A run that is over: abandoned, or done and neither waiting on an approval nor on its commit. It is History.
|
|
28
|
+
export const finished = (c) => c.kind === "run" && (c.status === "abandoned" || (c.status === "done" && c.accept !== "needs" && !gitDue(c)));
|
|
29
|
+
const here = (c, dir: string, ws: string, wsOfCard) => c.state === dir && wsOfCard(c) === ws;
|
|
30
|
+
// The cards whose agent terminals get tabs in a workspace: runs not over, and ideas whose agent is open.
|
|
31
|
+
export const liveIn = (cards: any[], dir: string, ws: string, wsOfCard) =>
|
|
32
|
+
cards.filter((c) => !c.archived && here(c, dir, ws, wsOfCard) && (c.kind === "idea" ? !!c.pty : c.kind === "run" && !finished(c)));
|
|
33
|
+
// History: the workspace's finished runs, newest first.
|
|
34
|
+
export const historyOf = (cards: any[], dir: string, ws: string, wsOfCard) =>
|
|
35
|
+
cards.filter((c) => finished(c) && here(c, dir, ws, wsOfCard)).sort((a, b) => (b.moved ?? 0) - (a.moved ?? 0));
|
|
36
|
+
|
|
37
|
+
type Saved = { tabs: any[]; hidden: string[]; screens: { id: string; tree: Node; focus: string }[]; on: string | null };
|
|
38
|
+
// Moves each card's old saved layout (keyed by the card) to its workspace's key. A key no
|
|
39
|
+
// card on the board owns is left as it is. Returns whether anything moved.
|
|
40
|
+
export function migrate(all: Record<string, Saved>, cards: any[], keyOf: (c) => string, wsOfCard): boolean {
|
|
41
|
+
let moved = false;
|
|
42
|
+
for (const c of cards) {
|
|
43
|
+
const old = keyOf(c),
|
|
44
|
+
from = all[old];
|
|
45
|
+
if (!from) continue;
|
|
46
|
+
const to = (all[wsKey(c.state, wsOfCard(c))] ||= { tabs: [], hidden: [], screens: [], on: null });
|
|
47
|
+
const placed = new Set(to.screens.flatMap((s) => leaves(s.tree)));
|
|
48
|
+
for (const t of from.tabs ?? []) if (!to.tabs.some((x) => x.id === t.id)) to.tabs.push(t);
|
|
49
|
+
for (const h of from.hidden ?? []) if (!to.hidden.includes(h)) to.hidden.push(h);
|
|
50
|
+
// An item sits in one screen only: a leaf the workspace already shows is dropped here.
|
|
51
|
+
for (const s of from.screens ?? []) {
|
|
52
|
+
const tree = s.tree && prune(s.tree, (id) => !placed.has(id));
|
|
53
|
+
if (!tree) continue;
|
|
54
|
+
leaves(tree).forEach((id) => placed.add(id));
|
|
55
|
+
to.screens.push({ ...s, tree, focus: leaves(tree).includes(s.focus) ? s.focus : leaves(tree)[0] });
|
|
56
|
+
}
|
|
57
|
+
to.on ??= from.on;
|
|
58
|
+
delete all[old];
|
|
59
|
+
moved = true;
|
|
60
|
+
}
|
|
61
|
+
return moved;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// A run that waits on the user (an approval, a question, a done run to accept).
|
|
65
|
+
export const awaiting = (c) => c.kind === "run" && (c.status === "waiting" || (c.status === "done" && c.accept === "needs") || gitDue(c) || !!(c.pty?.alive && c.pty.asking));
|
|
66
|
+
// The panel's picker: the workspace's runs not over (waiting on you first, then the newest), as the
|
|
67
|
+
// sidebar count shows them.
|
|
68
|
+
export const pickable = (cards: any[], dir: string, ws: string, wsOfCard) =>
|
|
69
|
+
liveIn(cards, dir, ws, wsOfCard)
|
|
70
|
+
.filter((c) => c.kind === "run")
|
|
71
|
+
.sort((a, b) => +awaiting(b) - +awaiting(a) || (b.moved ?? 0) - (a.moved ?? 0));
|
|
72
|
+
// The run the panel shows: the one picked while it is still pickable or History, else the first
|
|
73
|
+
// waiting/newest, else none.
|
|
74
|
+
export const chosen = (live: any[], hist: any[], picked?: string | null, keyOf: (c) => string = (c) => c.id) =>
|
|
75
|
+
[...live, ...hist].find((c) => keyOf(c) === picked) ?? live[0] ?? null;
|
|
76
|
+
|
|
77
|
+
// The terminal the board launched for each stage of a run: plan/impl/review sub-tab -> pty key.
|
|
78
|
+
// plan, impl and review are recorded when the board launches them (the idea's agent once its run
|
|
79
|
+
// exists, /decide, /fix). A plan that was not recorded falls back to the session no stage claims,
|
|
80
|
+
// and only until a later stage has been launched.
|
|
81
|
+
export function stageKeys(c): Record<string, string> {
|
|
82
|
+
if (c?.kind !== "run") return {};
|
|
83
|
+
const all: string[] = c?.pty?.all ?? [],
|
|
84
|
+
st = c?.pty?.stages ?? {},
|
|
85
|
+
out: Record<string, string> = {};
|
|
86
|
+
for (const s of ["impl", "review"]) if (st[s] && all.includes(st[s])) out[s] = st[s];
|
|
87
|
+
if (st.plan && all.includes(st.plan)) out.plan = st.plan;
|
|
88
|
+
// Fallback for a terminal the board did not record (a run it did not start): the lone unclaimed agent
|
|
89
|
+
// is named by the run's current stage (column 1 plan, 2 build, 3 review); at any other point it stays plain.
|
|
90
|
+
const first = all.find((k) => !Object.values(st).includes(k)),
|
|
91
|
+
now = { 1: "plan", 2: "impl", 3: "review" }[c.column as number];
|
|
92
|
+
if (first && now && !Object.keys(st).length) out[now] = first;
|
|
93
|
+
return out;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// A run's terminals as centre tabs: those a stage claimed are named "Plan · Claude", "Build · Claude",
|
|
97
|
+
// "Review · Claude"; the rest keep the agent's name, numbered when there are several.
|
|
98
|
+
export function stageTabs(c, agent: string): { key: string; title: string }[] {
|
|
99
|
+
const word = { plan: "Plan", impl: "Build", review: "Review" },
|
|
100
|
+
all: string[] = c?.pty ? c.pty.all ?? [c.pty.key] : [],
|
|
101
|
+
by = new Map(Object.entries(stageKeys(c)).map(([s, k]) => [k, word[s]])),
|
|
102
|
+
free = all.filter((k) => !by.has(k));
|
|
103
|
+
return all.map((key) => ({ key, title: by.has(key) ? `${by.get(key)} · ${agent}` : agent + (free.length > 1 ? " " + (free.indexOf(key) + 1) : "") }));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// A run at review or done: its localhost Preview may open the Browser.
|
|
107
|
+
export const reviewing = (c) => c.kind === "run" && (c.column === 3 || c.status === "done");
|
|
108
|
+
// The first localhost address in a run's ## Preview.
|
|
109
|
+
export const loopbackUrl = (pv?: string | null) => (pv ? /https?:\/\/(?:localhost|127\.0\.0\.1|\[::1\])(?::\d+)?[^\s)`'"<>]*/.exec(pv)?.[0] : undefined);
|
|
110
|
+
// Runs that moved into review or done since the last tick (`was` = the keys reviewing then) and have not
|
|
111
|
+
// yet opened the Browser: once per run id (`seen`).
|
|
112
|
+
export const toOpen = (cards: any[], was: Set<string>, seen: string[], keyOf: (c) => string) => cards.filter((c) => reviewing(c) && !was.has(keyOf(c)) && !seen.includes(keyOf(c)));
|
|
113
|
+
|
|
114
|
+
// ---- the sidebar card's agents (/state `agents`, one entry per open agent terminal)
|
|
115
|
+
export type Agent = { key: string; dir: string; ws?: string; agent?: string; name: string; run?: string; asking: boolean; busy: boolean; last?: string; at?: number; born?: number };
|
|
116
|
+
export type Status = "asking" | "working" | "done";
|
|
117
|
+
// Output flickers: entering "working" is immediate, leaving it for "done" waits this long after the last busy tick.
|
|
118
|
+
export const SETTLE_MS = 3000;
|
|
119
|
+
const seenBusy = new Map<string, number>(); // pty key + start time -> when it was last seen busy
|
|
120
|
+
const id = (a: Agent) => a.key + "@" + (a.born ?? 0); // a restarted pty under the same key is a new agent
|
|
121
|
+
const ORDER: Record<Status, number> = { asking: 0, working: 1, done: 2 };
|
|
122
|
+
const WORD: Record<Status, string> = { asking: "Asking", working: "Working", done: "Done" };
|
|
123
|
+
const DOT: Record<Status, string> = { asking: "wait", working: "build", done: "done" }; // the .si icons
|
|
124
|
+
// asking comes first and is immediate both ways (the server already waits for the screen to settle).
|
|
125
|
+
export function agentStatus(a: Agent, now: number, seen = seenBusy): Status {
|
|
126
|
+
if (a.asking) return "asking";
|
|
127
|
+
if (a.busy) return (seen.set(id(a), now), "working");
|
|
128
|
+
return now - (seen.get(id(a)) ?? -Infinity) < SETTLE_MS ? "working" : "done";
|
|
129
|
+
}
|
|
130
|
+
// Milliseconds until an agent now held at "working" may turn "done" (to redraw then); Infinity when none is.
|
|
131
|
+
export const settleIn = (as: Agent[], now: number, seen = seenBusy) =>
|
|
132
|
+
Math.min(Infinity, ...as.filter((a) => !a.asking && !a.busy && seen.has(id(a))).map((a) => SETTLE_MS - (now - seen.get(id(a))!)).filter((x) => x > 0));
|
|
133
|
+
// Forgets agents that are gone, so the memory stays as small as the open agents.
|
|
134
|
+
export const forgetAgents = (as: Agent[], seen = seenBusy) => { const live = new Set(as.map(id)); for (const k of seen.keys()) if (!live.has(k)) seen.delete(k); };
|
|
135
|
+
export type ARow = { a: Agent; st: Status };
|
|
136
|
+
// The agents of one workspace (the card's `ws` resolved as the rows resolve it), asking first, then working, then done.
|
|
137
|
+
export const wsAgents = (as: Agent[], dir: string, ws: string, wsOf, now: number, seen = seenBusy): ARow[] =>
|
|
138
|
+
as.filter((a) => a.dir === dir && wsOf({ state: a.dir, ws: a.ws }) === ws)
|
|
139
|
+
.map((a) => ({ a, st: agentStatus(a, now, seen) }))
|
|
140
|
+
.sort((x, y) => ORDER[x.st] - ORDER[y.st]);
|
|
141
|
+
// An expanded card's row text: the agent's latest output line, else the status and the agent.
|
|
142
|
+
export const lineOf = ({ a, st }: ARow) => a.last || `${WORD[st]} - ${a.name}`;
|
|
143
|
+
const ago = (t: number | undefined, now: number) => (!t ? "" : now - t < 60e3 ? "now" : age(t, now));
|
|
144
|
+
// Expanded cards, remembered per workspace (localStorage kz-wsopen): the map of dir\0ws to true.
|
|
145
|
+
export const openKey = (dir: string, ws: string) => dir + "\0" + ws;
|
|
146
|
+
export function toggleOpen(m: Record<string, true>, dir: string, ws: string) {
|
|
147
|
+
const k = openKey(dir, ws);
|
|
148
|
+
if (m[k]) delete m[k];
|
|
149
|
+
else m[k] = true;
|
|
150
|
+
return m;
|
|
151
|
+
}
|
|
152
|
+
const SPRITE = ["claude", "codex", "gemini"];
|
|
153
|
+
const mark = (a: Agent) => html`<svg aria-hidden="true"><use href="#i-${SPRITE.includes(a.agent ?? "") ? a.agent : "ai"}"/></svg>`;
|
|
154
|
+
const dot = (st: Status) => html`<i class="si ${DOT[st]}"></i>`;
|
|
155
|
+
const CHEV = html`<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M6 9l6 6 6-6" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>`;
|
|
156
|
+
// Line 3 of a workspace card. Every string goes through `html`, which escapes: the typed line and the agent's own
|
|
157
|
+
// output are untrusted text. data-term opens that agent's terminal; data-wopen only toggles the list.
|
|
158
|
+
export function agentsHtml(rows: ARow[], now: number, open: boolean, dir: string, ws: string) {
|
|
159
|
+
if (!rows.length) return "";
|
|
160
|
+
const at = html`data-dir="${dir}" data-ws="${ws}"`;
|
|
161
|
+
if (rows.length === 1) {
|
|
162
|
+
const r = rows[0];
|
|
163
|
+
return html`<div class="wag"><button type="button" class="aone" data-term="${r.a.key}" ${at} title="${r.a.name}: open its terminal">${dot(r.st)}${mark(r.a)}<span class="atx">${WORD[r.st]} - ${r.a.name}</span><small>${ago(r.a.at, now)}</small></button></div>`;
|
|
164
|
+
}
|
|
165
|
+
const chev = html`<button type="button" class="wchev${open ? " on" : ""}" data-wopen ${at} aria-expanded="${String(open)}" aria-label="${String(rows.length)} agents: ${open ? "hide" : "show"} the list">${CHEV}</button>`;
|
|
166
|
+
if (open)
|
|
167
|
+
return html`<div class="wag"><span class="aht">${String(rows.length)} agents</span>${chev}</div><div class="alist">${rows.map((r) => html`<button type="button" class="arow" data-term="${r.a.key}" ${at} title="${lineOf(r)}">${dot(r.st)}${mark(r.a)}<span class="atx">${lineOf(r)}</span><small>${ago(r.a.at, now)}</small></button>`)}</div>`;
|
|
168
|
+
const chips = (["asking", "working", "done"] as Status[]).map((st) => ({ st, rs: rows.filter((r) => r.st === st) })).filter((g) => g.rs.length);
|
|
169
|
+
return html`<div class="wag"><span class="chips">${chips.map((g) => html`<span class="chip">${dot(g.st)}${g.rs.map((r) => html`<button type="button" class="amark" data-term="${r.a.key}" ${at} title="${r.a.name} - ${WORD[g.st]}: open its terminal" aria-label="${r.a.name} - ${WORD[g.st]}: open its terminal">${mark(r.a)}</button>`)}</span>`)}</span>${chev}</div>`;
|
|
170
|
+
}
|