@nanocollective/roster 0.1.0-alpha.8 → 0.1.0-beta.1

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.
Files changed (145) hide show
  1. package/README.md +5 -0
  2. package/dist/cli.js +4084 -1936
  3. package/dist/worker/assets/assets/css/base.css +312 -0
  4. package/dist/worker/assets/assets/css/brain.css +116 -0
  5. package/dist/worker/assets/assets/css/diff.css +33 -0
  6. package/dist/worker/assets/assets/css/graph.css +39 -0
  7. package/dist/worker/assets/assets/css/health.css +75 -0
  8. package/dist/worker/assets/assets/css/home.css +101 -0
  9. package/dist/worker/assets/assets/css/inbox.css +199 -0
  10. package/dist/worker/assets/assets/css/layout.css +175 -0
  11. package/dist/worker/assets/assets/css/markdown.css +96 -0
  12. package/dist/worker/assets/assets/css/runs.css +13 -0
  13. package/dist/worker/assets/assets/css/search.css +48 -0
  14. package/dist/worker/assets/assets/css/setup.css +194 -0
  15. package/dist/worker/assets/assets/js/api.js +161 -0
  16. package/dist/worker/assets/assets/js/app.js +465 -0
  17. package/dist/worker/assets/assets/js/dialog.js +196 -0
  18. package/dist/worker/assets/assets/js/dom.js +131 -0
  19. package/dist/worker/assets/assets/js/homesort.js +174 -0
  20. package/dist/worker/assets/assets/js/icons.js +142 -0
  21. package/dist/worker/assets/assets/js/inflight.js +18 -0
  22. package/dist/worker/assets/assets/js/lightbox.js +273 -0
  23. package/dist/worker/assets/assets/js/md.js +406 -0
  24. package/dist/worker/assets/assets/js/mdedit.js +84 -0
  25. package/dist/worker/assets/assets/js/mention.js +264 -0
  26. package/dist/worker/assets/assets/js/readiness.js +70 -0
  27. package/dist/worker/assets/assets/js/refresh.js +197 -0
  28. package/dist/worker/assets/assets/js/router.js +20 -0
  29. package/dist/worker/assets/assets/js/search.js +366 -0
  30. package/dist/worker/assets/assets/js/state.js +213 -0
  31. package/dist/worker/assets/assets/js/textdiff.js +96 -0
  32. package/dist/worker/assets/assets/js/views/app.js +145 -0
  33. package/dist/worker/assets/assets/js/views/brain.js +260 -0
  34. package/dist/worker/assets/assets/js/views/changed.js +157 -0
  35. package/dist/worker/assets/assets/js/views/checklist.js +115 -0
  36. package/dist/worker/assets/assets/js/views/credential.js +84 -0
  37. package/dist/worker/assets/assets/js/views/docs.js +174 -0
  38. package/dist/worker/assets/assets/js/views/files.js +139 -0
  39. package/dist/worker/assets/assets/js/views/graph.js +436 -0
  40. package/dist/worker/assets/assets/js/views/health.js +304 -0
  41. package/dist/worker/assets/assets/js/views/hire.js +593 -0
  42. package/dist/worker/assets/assets/js/views/home.js +585 -0
  43. package/dist/worker/assets/assets/js/views/inbox.js +1558 -0
  44. package/dist/worker/assets/assets/js/views/memory.js +150 -0
  45. package/dist/worker/assets/assets/js/views/org.js +195 -0
  46. package/dist/worker/assets/assets/js/views/orgedit.js +240 -0
  47. package/dist/worker/assets/assets/js/views/paste.js +216 -0
  48. package/dist/worker/assets/assets/js/views/prompt.js +457 -0
  49. package/dist/worker/assets/assets/js/views/repos.js +97 -0
  50. package/dist/worker/assets/assets/js/views/runonce.js +94 -0
  51. package/dist/worker/assets/assets/js/views/runs.js +182 -0
  52. package/dist/worker/assets/assets/js/views/setup.js +557 -0
  53. package/dist/worker/assets/assets/js/views/staff.js +291 -0
  54. package/dist/worker/assets/assets/js/views/todo.js +62 -0
  55. package/dist/worker/assets/assets/js/yaml.js +134 -0
  56. package/dist/worker/assets/docs-images/brain.jpg +0 -0
  57. package/dist/worker/assets/docs-images/home.jpg +0 -0
  58. package/dist/worker/assets/docs-images/org.jpg +0 -0
  59. package/dist/worker/assets/docs-images/prompt.jpg +0 -0
  60. package/dist/worker/assets/docs-images/setup-org.jpg +0 -0
  61. package/dist/worker/assets/docs-images/setup-plan.jpg +0 -0
  62. package/dist/worker/assets/docs-images/staff.jpg +0 -0
  63. package/dist/worker/assets/index.html +82 -0
  64. package/dist/worker/index.js +4430 -0
  65. package/docs/README.md +2 -2
  66. package/docs/architecture.md +1 -1
  67. package/docs/charters/analyst.md +65 -0
  68. package/docs/charters/community.md +63 -0
  69. package/docs/charters/designer.md +65 -0
  70. package/docs/charters/devops.md +65 -0
  71. package/docs/charters/pm.md +70 -0
  72. package/docs/charters/qa.md +65 -0
  73. package/docs/charters/writer.md +63 -0
  74. package/docs/commands.md +13 -3
  75. package/docs/concepts.md +33 -20
  76. package/docs/cost.md +3 -3
  77. package/docs/developing.md +27 -9
  78. package/docs/doctor-codes.md +3 -1
  79. package/docs/export.md +2 -0
  80. package/docs/getting-started.md +44 -27
  81. package/docs/hosting.md +89 -43
  82. package/docs/images/brain.jpg +0 -0
  83. package/docs/images/home.jpg +0 -0
  84. package/docs/images/org.jpg +0 -0
  85. package/docs/images/prompt.jpg +0 -0
  86. package/docs/images/staff.jpg +0 -0
  87. package/docs/manual-steps.md +3 -2
  88. package/docs/org-yaml.md +3 -0
  89. package/docs/portal.md +143 -45
  90. package/docs/prompts.md +6 -0
  91. package/docs/security.md +57 -11
  92. package/docs/session-workflow.md +14 -0
  93. package/docs/staff-yaml.md +17 -1
  94. package/docs/upgrading.md +3 -3
  95. package/docs/writing-a-charter.md +19 -6
  96. package/package.json +4 -3
  97. package/templates/brain/.github/workflows/%%STAFF%%-daily.yaml +20 -1
  98. package/templates/brain/.github/workflows/%%STAFF%%-mention.yaml +53 -13
  99. package/templates/brain/staff.yaml +4 -1
  100. package/templates/briefs/amend.md +4 -3
  101. package/templates/briefs/priorities.md +46 -0
  102. package/templates/ops/.github/workflows/session.yaml +137 -3
  103. package/templates/ops/compose.mjs +35 -8
  104. package/templates/ops/org/operating.md +29 -10
  105. package/templates/ops/prompts/daily.md +23 -4
  106. package/templates/ops/prompts/mention.md +19 -0
  107. package/templates/ops/run-record.mjs +5 -3
  108. package/templates/portal/css/base.css +34 -0
  109. package/templates/portal/css/health.css +14 -1
  110. package/templates/portal/css/home.css +101 -0
  111. package/templates/portal/css/inbox.css +21 -4
  112. package/templates/portal/css/layout.css +29 -3
  113. package/templates/portal/css/markdown.css +19 -0
  114. package/templates/portal/css/search.css +48 -0
  115. package/templates/portal/css/setup.css +79 -4
  116. package/templates/portal/index.html +15 -9
  117. package/templates/portal/js/api.js +30 -4
  118. package/templates/portal/js/app.js +90 -7
  119. package/templates/portal/js/dialog.js +36 -0
  120. package/templates/portal/js/homesort.js +174 -0
  121. package/templates/portal/js/icons.js +41 -0
  122. package/templates/portal/js/inflight.js +18 -0
  123. package/templates/portal/js/md.js +5 -2
  124. package/templates/portal/js/mdedit.js +84 -0
  125. package/templates/portal/js/readiness.js +70 -0
  126. package/templates/portal/js/refresh.js +10 -2
  127. package/templates/portal/js/search.js +366 -0
  128. package/templates/portal/js/state.js +11 -5
  129. package/templates/portal/js/views/app.js +1 -2
  130. package/templates/portal/js/views/brain.js +1 -1
  131. package/templates/portal/js/views/checklist.js +19 -4
  132. package/templates/portal/js/views/credential.js +42 -51
  133. package/templates/portal/js/views/health.js +11 -6
  134. package/templates/portal/js/views/hire.js +593 -0
  135. package/templates/portal/js/views/home.js +585 -0
  136. package/templates/portal/js/views/inbox.js +237 -68
  137. package/templates/portal/js/views/org.js +31 -111
  138. package/templates/portal/js/views/orgedit.js +240 -0
  139. package/templates/portal/js/views/paste.js +111 -66
  140. package/templates/portal/js/views/prompt.js +94 -40
  141. package/templates/portal/js/views/repos.js +10 -10
  142. package/templates/portal/js/views/runs.js +21 -4
  143. package/templates/portal/js/views/setup.js +130 -163
  144. package/templates/portal/js/views/staff.js +181 -301
  145. package/templates/portal/js/views/todo.js +62 -0
@@ -0,0 +1,216 @@
1
+ /* Copy a prompt, paste it into whatever you use, paste the answer back.
2
+ *
3
+ * The two files nothing can generate — org/business.md and every CHARTER.md — have always been
4
+ * handed off with "paste this into your AI". The half that was missing is the way back: the
5
+ * model has no filesystem, so its answer is a message, and a message has to become a file.
6
+ *
7
+ * Nothing here writes. The panel parses, shows a diff, and waits for a second click. */
8
+
9
+ import { getBrief, parsePaste, saveFile } from "../api.js";
10
+ import { mdlite } from "../md.js";
11
+ import { el, toClipboard } from "../dom.js";
12
+ import { unifiedDiff, diffStat } from "../textdiff.js";
13
+
14
+ /**
15
+ * @param {{kind: string, staff?: string, title?: string, about?: string, onSaved?: () => void}} opts
16
+ * `about` is a sentence on what the role does, added to a charter brief.
17
+ */
18
+ export function paste(opts) {
19
+ const box = el("div", { className: "paste" });
20
+ if (opts.title !== "") box.append(el("h3", { textContent: opts.title ?? "Write it with your own AI" }));
21
+
22
+ const status = el("p", { className: "sub", textContent: "Building the prompt…" });
23
+ box.append(status);
24
+
25
+ /* A charter brief carries a worked example to model the shape on, matched to the role. The
26
+ choice is on the page because the match is a guess, and "none" is a fair answer. */
27
+ const model = el("select", { hidden: true });
28
+ const modelRow = el("label", { className: "sub", hidden: true }, ["Example to follow: ", model]);
29
+ box.append(modelRow);
30
+
31
+ const actions = el("div", { className: "row" });
32
+ const copy = el("button", { className: "btn primary", textContent: "Copy the prompt" });
33
+ const show = el("button", { className: "btn", textContent: "Show it" });
34
+ actions.append(copy, show);
35
+ box.append(actions);
36
+
37
+ const preview = el("pre", { className: "briefpreview", hidden: true });
38
+ box.append(preview);
39
+
40
+ const answer = el("textarea", {
41
+ className: "pastebox",
42
+ placeholder:
43
+ "Paste its whole reply here.",
44
+ });
45
+ answer.rows = 8;
46
+
47
+ const check = el("button", { className: "btn", textContent: "Check the answer", disabled: true });
48
+ const result = el("div", { className: "pasteresult" });
49
+
50
+ let brief = null;
51
+
52
+ const NAMES = {
53
+ cto: "the CTO example",
54
+ cmo: "the CMO example",
55
+ support: "the support example",
56
+ pm: "the product manager example",
57
+ designer: "the designer example",
58
+ qa: "the QA example",
59
+ devops: "the DevOps example",
60
+ writer: "the writer example",
61
+ analyst: "the analyst example",
62
+ community: "the community example",
63
+ none: "no example",
64
+ };
65
+ model.onchange = () => {
66
+ copy.disabled = true;
67
+ getBrief(opts.kind, opts.staff, model.value, opts.about)
68
+ .then((data) => {
69
+ if (data.error) throw new Error(data.error);
70
+ brief = data;
71
+ if (!preview.hidden) preview.textContent = brief.text;
72
+ })
73
+ .catch((err) => {
74
+ status.className = "err";
75
+ status.textContent = String(err.message || err);
76
+ })
77
+ .finally(() => {
78
+ copy.disabled = false;
79
+ });
80
+ };
81
+
82
+ getBrief(opts.kind, opts.staff, undefined, opts.about)
83
+ .then((data) => {
84
+ if (data.error) throw new Error(data.error);
85
+ brief = data;
86
+ if (data.examples) {
87
+ model.replaceChildren(...data.examples.map((e) => el("option", { value: e, textContent: NAMES[e] ?? e })));
88
+ model.value = data.example ?? "none";
89
+ model.hidden = modelRow.hidden = false;
90
+ }
91
+ status.textContent =
92
+ "Copy the prompt into Claude or ChatGPT and answer its questions.";
93
+ box.append(
94
+ el("p", { className: "sub", textContent: "Then paste its reply here:" }),
95
+ answer,
96
+ el("div", { className: "row" }, [check]),
97
+ result,
98
+ );
99
+ check.disabled = false;
100
+ })
101
+ .catch((err) => {
102
+ status.className = "err";
103
+ status.textContent = String(err.message || err);
104
+ copy.disabled = show.disabled = true;
105
+ });
106
+
107
+ copy.onclick = () => brief && toClipboard(brief.text, copy, "Copied");
108
+ show.onclick = () => {
109
+ preview.hidden = !preview.hidden;
110
+ preview.textContent = brief?.text ?? "";
111
+ show.textContent = preview.hidden ? "Show it" : "Hide it";
112
+ };
113
+
114
+ check.onclick = async () => {
115
+ result.replaceChildren(el("p", { className: "sub", textContent: "Reading…" }));
116
+ let data;
117
+ try {
118
+ data = await parsePaste(opts.kind, opts.staff, answer.value);
119
+ } catch (err) {
120
+ result.replaceChildren(el("p", { className: "err", textContent: String(err.message || err) }));
121
+ return;
122
+ }
123
+ showPasteResult(result, data, answer.value, opts.onSaved);
124
+ };
125
+
126
+ return box;
127
+ }
128
+
129
+ /**
130
+ * What a pasted reply turned into: problems first, then each file with its diff and a Save.
131
+ * Shared by every copy-a-prompt, paste-the-answer loop, whichever endpoint parsed it.
132
+ */
133
+ export function showPasteResult(result, data, answerText, onSaved) {
134
+ result.replaceChildren();
135
+
136
+ /* Problems first, each with the sentence that gets a better answer. A model that replied in
137
+ prose is the common case and it is not the person's mistake, so it reads as a next step
138
+ rather than as an error. */
139
+ for (const p of data.problems ?? []) {
140
+ const note = el("div", { className: "problem" });
141
+ note.append(el("b", { textContent: p.message }));
142
+ if (p.retry) {
143
+ const retry = el("pre", { className: "cmd", textContent: p.retry });
144
+ const again = el("button", { className: "btn", textContent: "Copy this reply" });
145
+ again.onclick = () => toClipboard(p.retry, again, "Copied");
146
+ note.append(retry, again);
147
+ }
148
+ result.append(note);
149
+ }
150
+
151
+ // What the AI said around the file: its caveats and "check these" are worth reading first.
152
+ const notes = String(answerText ?? "")
153
+ .replace(/<<<ROSTER FILE [^>]+>>>[\s\S]*?<<<ROSTER END>>>/g, "")
154
+ .trim();
155
+
156
+ for (const file of data.files ?? []) {
157
+ const card = el("div", { className: "pastefile" });
158
+ const name = file.path.split("/").slice(-2).join("/");
159
+ if (file.unchanged) {
160
+ card.append(el("p", { className: "verdict", textContent: "✓ Same as what's already in " + name + ". Nothing to save." }));
161
+ result.append(card);
162
+ continue;
163
+ }
164
+ card.append(
165
+ el("p", {
166
+ className: "verdict" + (file.writable ? " ok" : " bad"),
167
+ textContent: file.writable
168
+ ? "✓ Looks good. Saving replaces " + name + " with this:"
169
+ : "✗ " + file.path + " can't be written from here.",
170
+ }),
171
+ );
172
+ card.append(el("div", { className: "md doc pastepreview", innerHTML: mdlite(file.text) }));
173
+
174
+ if (notes) {
175
+ const said = el("details", { className: "pastenotes", open: true });
176
+ said.append(el("summary", { textContent: "Your AI also said" }), el("div", { className: "md doc", innerHTML: mdlite(notes) }));
177
+ card.append(said);
178
+ }
179
+
180
+ const diff = el("pre", { className: "diff", hidden: true });
181
+ diff.textContent = unifiedDiff(file.before, file.text, file.path);
182
+ const stat = diffStat(file.before, file.text);
183
+ const toggle = el("button", { className: "ghbtn", textContent: `Show changes (+${stat.added} −${stat.removed})` });
184
+ toggle.onclick = () => {
185
+ diff.hidden = !diff.hidden;
186
+ toggle.textContent = (diff.hidden ? "Show changes" : "Hide changes") + ` (+${stat.added} −${stat.removed})`;
187
+ };
188
+
189
+ const row = el("div", { className: "row" });
190
+ if (file.writable) {
191
+ const save = el("button", { className: "btn primary", textContent: "Save" });
192
+ save.onclick = async () => {
193
+ save.disabled = true;
194
+ save.textContent = "Saving…";
195
+ try {
196
+ await saveFile(file.path, file.text, `portal: write ${file.path}`, file.before);
197
+ save.textContent = "Saved";
198
+ card.classList.add("saved");
199
+ onSaved?.();
200
+ } catch (err) {
201
+ save.disabled = false;
202
+ save.textContent = "Save";
203
+ card.append(el("p", { className: "err", textContent: String(err.message || err) }));
204
+ }
205
+ };
206
+ row.append(save);
207
+ }
208
+ row.append(toggle);
209
+ card.append(row, diff);
210
+ result.append(card);
211
+ }
212
+
213
+ if (!(data.problems ?? []).length && !(data.files ?? []).length) {
214
+ result.append(el("p", { className: "verdict bad", textContent: "✗ No file found in that reply." }));
215
+ }
216
+ }
@@ -0,0 +1,457 @@
1
+ /* What a staff member is actually sent, and the files it was made of.
2
+ *
3
+ * The composition is `roster prompt <handle> --kind daily` on the CLI, and it was the one
4
+ * thing about these agents you could not see without a terminal. Composed by the tenant's own
5
+ * compose.mjs on the server, so there is no second implementation to drift.
6
+ *
7
+ * The distinction the screen exists to make: most of a charter is NOT in the prompt. The
8
+ * prompt tells the agent to go and read it. Editing CHARTER.md changes behaviour without
9
+ * changing a byte of the composed text, and nothing said so before.
10
+ */
11
+
12
+ import { highlighted } from "../mdedit.js";
13
+ import { getFile, post } from "../api.js";
14
+ import { askYes, sheet } from "../dialog.js";
15
+ import { el, esc, grow, kb, toClipboard } from "../dom.js";
16
+ import { icon } from "../icons.js";
17
+ import { mdlite } from "../md.js";
18
+ import { render } from "../router.js";
19
+ import { S, staff, writeHash } from "../state.js";
20
+ import { diffStat, unifiedDiff } from "../textdiff.js";
21
+ import { renderDiff } from "./changed.js";
22
+ import { showPasteResult } from "./paste.js";
23
+
24
+ const KINDS = [
25
+ ["daily", "Daily", "the scheduled run"],
26
+ ["mention", "Mention", "someone typed @them in an issue"],
27
+ ];
28
+
29
+ /**
30
+ * Fix a prompt with your own AI, in a side sheet: what is wrong, what you want changed, the
31
+ * prompt as it is today, a brief to copy, and a box for the reply. The reply is parsed into
32
+ * whole files and shown as diffs; nothing is saved until you press Save on one.
33
+ *
34
+ * @param {{handle: string, name?: string, kind: string, want?: string, title?: string,
35
+ * detail?: string}} o
36
+ * @returns {{box: object | null, body: HTMLElement, want: HTMLTextAreaElement}} what it built,
37
+ * so a caller (or a test, where there is no <dialog>) can see it.
38
+ */
39
+ export function amendSheet(o) {
40
+ const body = el("div", { className: "amend" });
41
+ const x = el("button", { className: "iconbtn", title: "Close (Esc)", ariaLabel: "Close" });
42
+ x.append(icon("close"));
43
+ body.append(el("div", { className: "sidebar-x" }, [x]));
44
+
45
+ body.append(el("h3", { className: "sheettitle", textContent: o.title ?? "Change what they are told" }));
46
+ if (o.detail) body.append(el("p", { className: "sub", textContent: o.detail }));
47
+
48
+ const want = el("textarea", {
49
+ className: "pastebox",
50
+ rows: 3,
51
+ value: o.want ?? "",
52
+ placeholder: "stop opening decision issues for anything reversible",
53
+ });
54
+ body.append(el("label", { className: "amendlabel", textContent: "1. What you want changed" }), want);
55
+
56
+ const copy = el("button", { className: "btn primary", textContent: "Copy the prompt" });
57
+ const copied = el("span", { className: "meta" });
58
+ copy.onclick = async () => {
59
+ copy.disabled = true;
60
+ try {
61
+ const url =
62
+ "/api/amend?staff=" + encodeURIComponent(o.handle) +
63
+ "&kind=" + encodeURIComponent(o.kind) +
64
+ "&want=" + encodeURIComponent(want.value);
65
+ const text = await (await fetch(url, { cache: "no-store" })).text();
66
+ await toClipboard(text, copy, "Copy the prompt");
67
+ copied.textContent = "Paste it into Claude, ChatGPT or any other AI.";
68
+ copied.className = "meta";
69
+ } catch (e) {
70
+ copied.textContent = String(e.message || e);
71
+ copied.className = "meta err";
72
+ } finally {
73
+ copy.disabled = false;
74
+ }
75
+ };
76
+ body.append(
77
+ el("label", { className: "amendlabel", textContent: "2. Copy the prompt into your AI" }),
78
+ el("div", { className: "row" }, [copy, copied]),
79
+ );
80
+
81
+ // The prompt this is about, to read before deciding what to ask for.
82
+ const today = el("details", { className: "amendtoday" });
83
+ const summary = el("summary", { textContent: "The " + o.kind + " prompt as it is today" });
84
+ const text = el("pre", { className: "briefpreview", textContent: "Composing…" });
85
+ today.append(summary, text);
86
+ body.append(today);
87
+ fetch("/api/prompt?staff=" + encodeURIComponent(o.handle) + "&kind=" + encodeURIComponent(o.kind), { cache: "no-store" })
88
+ .then((r) => r.json())
89
+ .then((v) => {
90
+ if (v.error) throw new Error(v.error);
91
+ const words = String(v.composed ?? "").split(/\s+/).filter(Boolean).length;
92
+ summary.textContent = "The " + o.kind + " prompt as it is today · " + words.toLocaleString() + " words";
93
+ text.textContent = v.composed ?? "";
94
+ })
95
+ .catch((e) => {
96
+ text.textContent = String(e.message || e);
97
+ });
98
+
99
+ const answer = el("textarea", { className: "pastebox", rows: 8, placeholder: "Paste its whole reply here." });
100
+ const check = el("button", { className: "btn", textContent: "Check the answer" });
101
+ const result = el("div", { className: "pasteresult" });
102
+ check.onclick = async () => {
103
+ if (!answer.value.trim()) return;
104
+ result.replaceChildren(el("p", { className: "sub", textContent: "Reading…" }));
105
+ try {
106
+ const data = await post({ staff: o.handle, kind: o.kind, answer: answer.value }, "/api/amend/check");
107
+ showPasteResult(result, data, answer.value, () => {});
108
+ } catch (e) {
109
+ result.replaceChildren(el("p", { className: "err", textContent: String(e.message || e) }));
110
+ }
111
+ };
112
+ body.append(
113
+ el("label", { className: "amendlabel", textContent: "3. Paste its reply" }),
114
+ answer,
115
+ el("div", { className: "row" }, [check]),
116
+ result,
117
+ );
118
+
119
+ const box = sheet({ node: body, side: true });
120
+ if (box) x.onclick = () => box.close();
121
+ return { box, body, want };
122
+ }
123
+
124
+ export function viewPrompt(m) {
125
+ const s = staff();
126
+ if (!S.promptKind) S.promptKind = "daily";
127
+
128
+ m.append(el("h1", { textContent: s.name + " · prompt" }));
129
+ const sub = el("p", { className: "sub", textContent: "Composing…" });
130
+ m.append(sub);
131
+
132
+ const kind = el("select");
133
+ for (const [id, label, why] of KINDS) {
134
+ kind.append(el("option", { value: id, textContent: label + " — " + why }));
135
+ }
136
+ kind.value = S.promptKind;
137
+ kind.onchange = () => {
138
+ S.promptKind = kind.value;
139
+ S.promptOpen = null;
140
+ writeHash(false);
141
+ load();
142
+ };
143
+ /* The point of the screen: you can see the prompt, and you can get help changing it
144
+ without first working out which of eight files to open. */
145
+ const help = el("button", { className: "ghbtn primary", textContent: "Change this with your AI" });
146
+ help.onclick = () => copyAmend("");
147
+ const copy = el("button", { className: "ghbtn", textContent: "Copy the prompt" });
148
+ copy.onclick = () => {
149
+ if (view?.composed) toClipboard(view.composed, copy, "Copy the prompt");
150
+ };
151
+ const note = el("span", { className: "meta" });
152
+ m.append(el("div", { className: "row", style: "margin-bottom:16px" }, [kind, help, copy, note]));
153
+
154
+ const copyAmend = (want) => amendSheet({ handle: s.handle, name: s.name, kind: S.promptKind, want });
155
+
156
+ const split = el("div", { className: "split" });
157
+ const tree = el("div", { className: "tree" });
158
+ const viewer = el("div", { className: "viewer" });
159
+ split.append(tree, viewer);
160
+ m.append(split);
161
+
162
+ let view = null;
163
+ load();
164
+
165
+ async function load() {
166
+ tree.replaceChildren(el("p", { className: "empty", textContent: "Composing…" }));
167
+ viewer.replaceChildren();
168
+ try {
169
+ const url = "/api/prompt?staff=" + encodeURIComponent(s.handle) + "&kind=" + S.promptKind;
170
+ view = await (await fetch(url, { cache: "no-store" })).json();
171
+ } catch (e) {
172
+ tree.replaceChildren(el("p", { className: "empty err", textContent: e.message }));
173
+ return;
174
+ }
175
+ if (view.error) {
176
+ sub.textContent = "This prompt does not compose.";
177
+ tree.replaceChildren();
178
+ viewer.replaceChildren(
179
+ el("div", { className: "notice" }, [
180
+ "compose.mjs refused: " + view.error,
181
+ ]),
182
+ );
183
+ return;
184
+ }
185
+ paint();
186
+ }
187
+
188
+ function paint() {
189
+ const chars = view.composed.length;
190
+ const words = view.composed.split(/\s+/).filter(Boolean).length;
191
+ sub.innerHTML =
192
+ "What <b>" + esc(s.name) + "</b> is sent on a <b>" + esc(S.promptKind) +
193
+ "</b> run: " + words.toLocaleString() + " words, " + chars.toLocaleString() +
194
+ " characters, assembled from " + view.layers.filter((l) => !l.missing).length +
195
+ " files. This is the text, not a description of it.";
196
+
197
+ tree.replaceChildren();
198
+
199
+ const composed = group("The prompt");
200
+ composed.append(row({ key: "composed", label: "Composed", right: kb(chars) }));
201
+ tree.append(composed);
202
+
203
+ const inlined = group("Inlined, in order");
204
+ for (const l of view.layers) inlined.append(layerRow(l));
205
+ tree.append(inlined);
206
+
207
+ /* No Problems section. This screen answers "what is sent"; whether what is sent is any
208
+ good is a health question, and it is asked and answered on Health. Having both here
209
+ made the tree beside the prompt half prompt and half complaints. */
210
+ const named = group("Named, not inlined");
211
+ named.append(
212
+ el("p", {
213
+ className: "treenote",
214
+ textContent:
215
+ "The prompt tells the agent to open these. Editing one changes what it does without changing the prompt.",
216
+ }),
217
+ );
218
+ for (const l of view.runtime) named.append(layerRow(l));
219
+ tree.append(named);
220
+
221
+ open(S.promptOpen ?? "composed");
222
+ }
223
+
224
+ function group(title, right) {
225
+ const g = el("div", { className: "navgroup" });
226
+ const h = el("div", { className: "ghead", textContent: title });
227
+ if (right) h.append(el("i", { textContent: right }));
228
+ g.append(h);
229
+ return g;
230
+ }
231
+
232
+ function layerRow(l) {
233
+ return row({
234
+ key: l.path,
235
+ label: l.rel.replace(/^staff:/, ""),
236
+ right: l.missing ? "absent" : kb(l.bytes),
237
+ note: l.repo,
238
+ dim: l.missing,
239
+ });
240
+ }
241
+
242
+ function row({ key, label, right, note, dim }) {
243
+ const b = el("button", { className: "tfile tlayer" + (dim ? " dim" : ""), title: key });
244
+ b.dataset.key = key;
245
+ const t = el("span", { className: "t" });
246
+ t.append(el("span", { className: "lname", textContent: label }));
247
+ if (note) t.append(el("span", { className: "lrepo", textContent: note }));
248
+ b.append(t);
249
+ if (right) b.append(el("i", { textContent: right }));
250
+ b.setAttribute("aria-current", String((S.promptOpen ?? "composed") === key));
251
+ b.onclick = () => open(key);
252
+ return b;
253
+ }
254
+
255
+ /* Returns a promise: `showLayer` fetches, and anything that wants to add to the pane after
256
+ opening a file has to wait for it. Prepending before the fetch resolved is why the
257
+ "what your edit did" strip appeared and then vanished. */
258
+ function open(key) {
259
+ S.promptOpen = key;
260
+ writeHash(false);
261
+ for (const o of tree.querySelectorAll(".tfile")) {
262
+ o.setAttribute("aria-current", String(o.dataset.key === key));
263
+ }
264
+ if (key === "composed") return showComposed();
265
+ const layer = [...view.layers, ...view.runtime].find((l) => l.path === key);
266
+ return layer ? showLayer(layer) : undefined;
267
+ }
268
+
269
+ /* ------------------------------ the prompt ----------------------------- */
270
+
271
+ function showComposed() {
272
+ viewer.replaceChildren();
273
+ const head = el("div", { className: "row", style: "margin-bottom:12px" });
274
+ head.append(
275
+ el("span", {
276
+ className: "meta",
277
+ textContent: "composed by " + S.data.name + "'s own compose.mjs · " + S.promptKind,
278
+ }),
279
+ );
280
+ const toggle = el("button", { className: "ghbtn", textContent: "Show source" });
281
+ head.append(toggle);
282
+ viewer.append(head);
283
+
284
+ const body = el("div", { className: "md doc" });
285
+ body.innerHTML = mdlite(view.composed);
286
+ viewer.append(body);
287
+
288
+ let source = false;
289
+ toggle.onclick = () => {
290
+ source = !source;
291
+ toggle.textContent = source ? "Show rendered" : "Show source";
292
+ if (source) {
293
+ body.className = "";
294
+ body.replaceChildren(el("pre", { className: "code", textContent: view.composed }));
295
+ } else {
296
+ body.className = "md doc";
297
+ body.replaceChildren();
298
+ body.innerHTML = mdlite(view.composed);
299
+ }
300
+ };
301
+ }
302
+
303
+ /** What the edit did, once, at the top of the pane: the commit, and the composed diff. */
304
+ function showEffect(saved, before, after) {
305
+ const box = el("div", { className: "effect" });
306
+ const head = el("div", { className: "row" });
307
+ head.append(
308
+ el("span", {
309
+ className: saved.pushed ? "meta ok" : "meta warn",
310
+ textContent: saved.pushed
311
+ ? "committed and pushed · " + saved.sha
312
+ : "committed " + saved.sha + ", but the push failed: " + (saved.note ?? ""),
313
+ }),
314
+ );
315
+
316
+ const stat = diffStat(before, after);
317
+ const patch = unifiedDiff(before, after, "the " + S.promptKind + " prompt");
318
+ if (!patch) {
319
+ head.append(
320
+ el("span", { className: "meta", textContent: "· the composed prompt is unchanged" }),
321
+ );
322
+ box.append(head);
323
+ viewer.prepend(box);
324
+ return;
325
+ }
326
+
327
+ const toggle = el("button", { className: "tevmore" });
328
+ toggle.setAttribute("aria-expanded", "false");
329
+ toggle.append(
330
+ icon("chevron", "caret"),
331
+ el("span", {
332
+ textContent:
333
+ "the " + S.promptKind + " prompt: +" + stat.added + " −" + stat.removed + " lines",
334
+ }),
335
+ );
336
+ const body = el("div", {}, renderDiff(patch, null));
337
+ body.hidden = true;
338
+ toggle.onclick = () => {
339
+ body.hidden = !body.hidden;
340
+ toggle.setAttribute("aria-expanded", String(!body.hidden));
341
+ };
342
+ box.append(head, toggle, body);
343
+ viewer.prepend(box);
344
+ }
345
+
346
+ /* ------------------------------ one layer ------------------------------ */
347
+
348
+ async function showLayer(layer) {
349
+ viewer.replaceChildren(el("p", { className: "empty", textContent: "Loading…" }));
350
+ if (layer.missing) {
351
+ viewer.replaceChildren(
352
+ el("p", {
353
+ className: "empty",
354
+ textContent: layer.optional
355
+ ? layer.rel + " is optional and this staff member does not have one."
356
+ : layer.rel + " is missing.",
357
+ }),
358
+ );
359
+ return;
360
+ }
361
+
362
+ let text;
363
+ try {
364
+ text = await getFile(layer.path);
365
+ } catch (e) {
366
+ viewer.replaceChildren(el("p", { className: "empty err", textContent: e.message }));
367
+ return;
368
+ }
369
+ render(layer, text);
370
+ }
371
+
372
+ function render(layer, text) {
373
+ viewer.replaceChildren();
374
+
375
+ const head = el("div", { className: "row", style: "margin-bottom:12px" });
376
+ head.append(
377
+ el("span", { className: "meta", textContent: layer.path + " · " + kb(text.length) }),
378
+ );
379
+ const editBtn = el("button", { className: "ghbtn", textContent: "Edit" });
380
+ if (layer.editable) head.append(editBtn);
381
+ viewer.append(head);
382
+
383
+ const body = el("div", { className: "md doc" });
384
+ body.innerHTML = mdlite(text);
385
+ viewer.append(body);
386
+
387
+ editBtn.onclick = () => edit(layer, text);
388
+ }
389
+
390
+ /* Editing writes to a repo the agents run from, so the confirm names the repo and says
391
+ who picks it up. `org/` reaches every staff member; a brain file reaches one. */
392
+ function edit(layer, text) {
393
+ viewer.replaceChildren();
394
+ const shared = /^(org|prompts)\//.test(layer.rel);
395
+ const reach = shared
396
+ ? "every staff member on their next run"
397
+ : s.name + " on their next run";
398
+
399
+ viewer.append(
400
+ el("div", { className: "row", style: "margin-bottom:10px" }, [
401
+ el("span", { className: "meta", textContent: layer.path }),
402
+ ]),
403
+ );
404
+
405
+ const ta = el("textarea", { value: text, className: "editor" });
406
+ const hl = highlighted(ta, "md");
407
+ viewer.append(hl);
408
+ grow(ta);
409
+ hl.repaint();
410
+ ta.addEventListener("input", () => grow(ta));
411
+
412
+ const status = el("span", { className: "meta" });
413
+ const save = el("button", { className: "ghbtn primary", textContent: "Save and commit" });
414
+ const cancel = el("button", { className: "ghbtn", textContent: "Cancel" });
415
+ viewer.append(
416
+ el("div", { className: "row", style: "margin-top:10px" }, [save, cancel, status]),
417
+ );
418
+ viewer.append(
419
+ el("p", {
420
+ className: "editnote",
421
+ textContent: "Commits to " + layer.repo + " and pushes. Picked up by " + reach + ".",
422
+ }),
423
+ );
424
+
425
+ cancel.onclick = () => render(layer, text);
426
+
427
+ save.onclick = async () => {
428
+ if (ta.value === text) {
429
+ status.textContent = "nothing changed";
430
+ return;
431
+ }
432
+ const yes = await askYes({
433
+ title: "Commit " + layer.path + " to " + layer.repo + " and push?",
434
+ hint: "Picked up by " + reach + ".",
435
+ confirm: "Commit and push",
436
+ });
437
+ if (!yes) return;
438
+ save.disabled = cancel.disabled = true;
439
+ status.textContent = "committing…";
440
+ status.className = "meta";
441
+ const before = view.composed;
442
+ try {
443
+ const r = await post({ path: layer.path, text: ta.value }, "/api/save");
444
+ // Recompose: the point of editing a layer is what it does to the prompt, and a file
445
+ // diff answers a question nobody asked. A line added to one fragment can land three
446
+ // times or not at all.
447
+ await load();
448
+ await open(layer.path);
449
+ showEffect(r, before, view.composed);
450
+ } catch (e) {
451
+ status.textContent = e.message;
452
+ status.className = "meta err";
453
+ save.disabled = cancel.disabled = false;
454
+ }
455
+ };
456
+ }
457
+ }