@nanocollective/roster 0.1.0-beta.3 → 0.1.0-beta.30

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 (135) hide show
  1. package/README.md +8 -0
  2. package/dist/agents-POITEZRI.js +232 -0
  3. package/dist/chunk-FF2WBNNW.js +370 -0
  4. package/dist/chunk-GDAJPOHK.js +562 -0
  5. package/dist/cli.js +3391 -1239
  6. package/dist/compose-5DBEHSEV.js +17 -0
  7. package/dist/connectors-5E5ZMGZV.js +23 -0
  8. package/dist/worker/assets/assets/css/astryx.css +948 -0
  9. package/dist/worker/assets/assets/css/base.css +51 -110
  10. package/dist/worker/assets/assets/css/brain.css +26 -2
  11. package/dist/worker/assets/assets/css/connectors.css +58 -0
  12. package/dist/worker/assets/assets/css/graph.css +9 -12
  13. package/dist/worker/assets/assets/css/health.css +4 -7
  14. package/dist/worker/assets/assets/css/home.css +145 -60
  15. package/dist/worker/assets/assets/css/inbox.css +11 -0
  16. package/dist/worker/assets/assets/css/layout.css +18 -22
  17. package/dist/worker/assets/assets/css/markdown.css +22 -1
  18. package/dist/worker/assets/assets/css/routines.css +40 -0
  19. package/dist/worker/assets/assets/css/runs.css +5 -3
  20. package/dist/worker/assets/assets/css/search.css +3 -6
  21. package/dist/worker/assets/assets/css/setup.css +3 -15
  22. package/dist/worker/assets/assets/js/api.js +3 -0
  23. package/dist/worker/assets/assets/js/app.js +25 -1
  24. package/dist/worker/assets/assets/js/astryx-ui.js +34 -0
  25. package/dist/worker/assets/assets/js/dom.js +1 -1
  26. package/dist/worker/assets/assets/js/homesort.js +66 -20
  27. package/dist/worker/assets/assets/js/icons.js +6 -0
  28. package/dist/worker/assets/assets/js/md.js +7 -2
  29. package/dist/worker/assets/assets/js/refcard.js +216 -0
  30. package/dist/worker/assets/assets/js/refresh.js +4 -2
  31. package/dist/worker/assets/assets/js/report.js +51 -0
  32. package/dist/worker/assets/assets/js/select.js +362 -0
  33. package/dist/worker/assets/assets/js/state.js +14 -1
  34. package/dist/worker/assets/assets/js/views/connectors.js +423 -0
  35. package/dist/worker/assets/assets/js/views/graph.js +67 -28
  36. package/dist/worker/assets/assets/js/views/home.js +378 -176
  37. package/dist/worker/assets/assets/js/views/inbox.js +68 -16
  38. package/dist/worker/assets/assets/js/views/org.js +2 -0
  39. package/dist/worker/assets/assets/js/views/people.js +109 -0
  40. package/dist/worker/assets/assets/js/views/routines.js +378 -0
  41. package/dist/worker/assets/assets/js/views/runs.js +3 -1
  42. package/dist/worker/assets/docs-images/brain.jpg +0 -0
  43. package/dist/worker/assets/docs-images/home.jpg +0 -0
  44. package/dist/worker/assets/docs-images/org.jpg +0 -0
  45. package/dist/worker/assets/docs-images/prompt.jpg +0 -0
  46. package/dist/worker/assets/docs-images/setup-org.jpg +0 -0
  47. package/dist/worker/assets/docs-images/setup-plan.jpg +0 -0
  48. package/dist/worker/assets/docs-images/staff.jpg +0 -0
  49. package/dist/worker/assets/index.html +19 -3
  50. package/dist/worker/index.js +4478 -1098
  51. package/docs/README.md +2 -0
  52. package/docs/architecture.md +23 -4
  53. package/docs/commands.md +111 -9
  54. package/docs/concepts.md +2 -1
  55. package/docs/connectors.md +169 -0
  56. package/docs/cost.md +1 -1
  57. package/docs/developing.md +3 -3
  58. package/docs/doctor-codes.md +8 -2
  59. package/docs/export.md +4 -3
  60. package/docs/extending.md +1 -1
  61. package/docs/getting-started.md +20 -3
  62. package/docs/hosting.md +67 -3
  63. package/docs/images/brain.jpg +0 -0
  64. package/docs/images/home.jpg +0 -0
  65. package/docs/images/org.jpg +0 -0
  66. package/docs/images/prompt.jpg +0 -0
  67. package/docs/images/setup-org.jpg +0 -0
  68. package/docs/images/setup-plan.jpg +0 -0
  69. package/docs/images/staff.jpg +0 -0
  70. package/docs/memory.md +3 -2
  71. package/docs/org-yaml.md +58 -5
  72. package/docs/portal.md +52 -11
  73. package/docs/prompts.md +3 -2
  74. package/docs/routines.md +65 -0
  75. package/docs/security.md +37 -4
  76. package/docs/session-workflow.md +31 -8
  77. package/docs/staff-yaml.md +7 -3
  78. package/docs/troubleshooting.md +1 -1
  79. package/docs/upgrading.md +1 -1
  80. package/docs/writing-a-charter.md +1 -1
  81. package/package.json +8 -1
  82. package/templates/brain/.gitattributes +3 -0
  83. package/templates/brain/.github/workflows/%%STAFF%%-daily.yaml +24 -15
  84. package/templates/brain/.github/workflows/%%STAFF%%-mention.yaml +12 -10
  85. package/templates/brain/CHARTER.md +2 -2
  86. package/templates/brain/STATE.md +16 -0
  87. package/templates/brain/memory/INDEX.md +1 -1
  88. package/templates/brain/staff.yaml +3 -0
  89. package/templates/ops/.gitattributes +3 -0
  90. package/templates/ops/.github/workflows/session.yaml +92 -13
  91. package/templates/ops/agents.mjs +38 -11
  92. package/templates/ops/compose.mjs +60 -2
  93. package/templates/ops/connectors.mjs +722 -0
  94. package/templates/ops/org/operating.md +10 -0
  95. package/templates/ops/org/voice.md +21 -3
  96. package/templates/ops/prompts/_identity.md +20 -0
  97. package/templates/ops/prompts/daily.md +70 -19
  98. package/templates/ops/prompts/mention.md +7 -2
  99. package/templates/ops/status-week.mjs +169 -0
  100. package/templates/portal/css/astryx.css +948 -0
  101. package/templates/portal/css/base.css +51 -110
  102. package/templates/portal/css/brain.css +26 -2
  103. package/templates/portal/css/connectors.css +58 -0
  104. package/templates/portal/css/graph.css +9 -12
  105. package/templates/portal/css/health.css +4 -7
  106. package/templates/portal/css/home.css +145 -60
  107. package/templates/portal/css/inbox.css +11 -0
  108. package/templates/portal/css/layout.css +18 -22
  109. package/templates/portal/css/markdown.css +22 -1
  110. package/templates/portal/css/routines.css +40 -0
  111. package/templates/portal/css/runs.css +5 -3
  112. package/templates/portal/css/search.css +3 -6
  113. package/templates/portal/css/setup.css +3 -15
  114. package/templates/portal/index.html +19 -3
  115. package/templates/portal/js/api.js +3 -0
  116. package/templates/portal/js/app.js +25 -1
  117. package/templates/portal/js/astryx-ui.js +34 -0
  118. package/templates/portal/js/dom.js +1 -1
  119. package/templates/portal/js/homesort.js +66 -20
  120. package/templates/portal/js/icons.js +6 -0
  121. package/templates/portal/js/md.js +7 -2
  122. package/templates/portal/js/refcard.js +216 -0
  123. package/templates/portal/js/refresh.js +4 -2
  124. package/templates/portal/js/report.js +51 -0
  125. package/templates/portal/js/select.js +362 -0
  126. package/templates/portal/js/state.js +14 -1
  127. package/templates/portal/js/views/connectors.js +423 -0
  128. package/templates/portal/js/views/graph.js +67 -28
  129. package/templates/portal/js/views/home.js +378 -176
  130. package/templates/portal/js/views/inbox.js +68 -16
  131. package/templates/portal/js/views/org.js +2 -0
  132. package/templates/portal/js/views/people.js +109 -0
  133. package/templates/portal/js/views/routines.js +378 -0
  134. package/templates/portal/js/views/runs.js +3 -1
  135. package/templates/skill/SKILL.md +115 -0
@@ -26,7 +26,7 @@ const LABEL_TONE = {
26
26
  * which coloured exactly one org's and left every other org's looking like an ordinary tag.
27
27
  * It comes off org.yaml now, so it is right wherever this runs.
28
28
  */
29
- function tone(label) {
29
+ export function tone(label) {
30
30
  if (LABEL_TONE[label]) return LABEL_TONE[label];
31
31
  const name = String(label).toLowerCase();
32
32
  return humansOf().some(
@@ -93,8 +93,11 @@ export function belongsTo(item, s) {
93
93
  }
94
94
 
95
95
  /* Threads read so far, by repo#number, kept for the session. Each is reused while its
96
- updatedAt matches the list's, so a thread is fetched again only when it has changed. */
96
+ updatedAt matches the list's, so a thread is fetched again only when it has changed, or once
97
+ the signed links to its images (good for five minutes) are about to run out. */
97
98
  const THREADS = new Map();
99
+ const THREAD_AT = new Map();
100
+ const THREAD_FRESH = 4 * 60_000;
98
101
 
99
102
  export const viewInbox = (m) => inboxScreen(m, {});
100
103
 
@@ -125,7 +128,13 @@ export function threadInto(host, ref) {
125
128
  THREADS.delete(item.repo + "#" + item.number);
126
129
  item.partial = true;
127
130
  }
128
- host.append(inboxScreen(el("div"), { prs: ref.kind === "pr", only: ref }));
131
+ /* A #175 in a report is as likely to be closed, or in a repo nobody watches, as it is to be
132
+ in the inbox. It is read on its own and kept beside the inbox rather than added to it,
133
+ where it would turn up as a row on Trackers. */
134
+ const stray = item
135
+ ? null
136
+ : { repo: ref.repo, number: ref.number, kind: ref.kind ?? "issue", labels: [], partial: true };
137
+ host.append(inboxScreen(el("div"), { prs: ref.kind === "pr", only: ref, stray }));
129
138
  }
130
139
 
131
140
  function inboxScreen(m, opts) {
@@ -385,11 +394,14 @@ function inboxScreen(m, opts) {
385
394
  }
386
395
  }
387
396
 
397
+ function find(ref) {
398
+ const same = (i) => i.repo === ref.repo && i.number === ref.number;
399
+ return (S.inbox?.items ?? []).find(same) ?? (opts.stray && same(opts.stray) ? opts.stray : null);
400
+ }
401
+
388
402
  /* Everything is already loaded, so opening a thread is a render, not a request. */
389
403
  function openThread(ref) {
390
- const item = (S.inbox?.items ?? []).find(
391
- (i) => i.repo === ref.repo && i.number === ref.number,
392
- );
404
+ const item = find(ref);
393
405
  if (!item) {
394
406
  viewer.replaceChildren(
395
407
  el("p", { className: "empty", textContent: "That one is no longer open. Refresh." }),
@@ -414,7 +426,7 @@ function inboxScreen(m, opts) {
414
426
  const key = item.repo + "#" + item.number;
415
427
  const kept = THREADS.get(key);
416
428
  const keep = { role: item.role, unread: item.unread };
417
- if (kept && kept.updatedAt === item.updatedAt) {
429
+ if (kept && kept.updatedAt === item.updatedAt && Date.now() - (THREAD_AT.get(key) ?? 0) < THREAD_FRESH) {
418
430
  Object.assign(item, kept, keep, { partial: false });
419
431
  } else {
420
432
  viewer.dataset.want = key;
@@ -423,6 +435,7 @@ function inboxScreen(m, opts) {
423
435
  .then((fresh) => {
424
436
  if (!fresh || fresh.error) throw new Error(fresh?.error ?? "could not read it");
425
437
  THREADS.set(key, fresh);
438
+ THREAD_AT.set(key, Date.now());
426
439
  Object.assign(item, fresh, keep, { partial: false });
427
440
  })
428
441
  .catch((e) => {
@@ -481,7 +494,22 @@ function inboxScreen(m, opts) {
481
494
  just happened. The title and the actions are in the header, so nothing you need is
482
495
  further down than the first screenful. */
483
496
  const from = pane.children.length;
484
- for (const node of timeline(item, openThread)) pane.append(node);
497
+ const nodes = timeline(item, openThread);
498
+ /* The newest message is what you came for, so it is marked and everything between it and
499
+ the opening post folds behind one button. What happened after it (closed, relabelled)
500
+ stays in view above it, and the opening post stays at the bottom, since it says what
501
+ was asked in the first place. */
502
+ const latest = nodes.findIndex((n) => n.classList?.contains("cmt"));
503
+ if (latest >= 0) {
504
+ nodes[latest].classList.add("latest");
505
+ nodes[latest].querySelector?.(".cwhen")?.before?.(el("span", { className: "pill clatest", textContent: "Latest" }));
506
+ const older = nodes.slice(latest + 1);
507
+ const said = older.filter((n) => n.classList?.contains("cmt")).length;
508
+ if (said) {
509
+ nodes.splice(latest + 1, older.length, earlier(older, said));
510
+ }
511
+ }
512
+ for (const node of nodes) pane.append(node);
485
513
  pane.append(
486
514
  comment({
487
515
  author: item.author, when: item.createdAt, body: item.body, first: true,
@@ -724,9 +752,11 @@ function inboxScreen(m, opts) {
724
752
  };
725
753
  buttons.push(reply);
726
754
 
755
+ /* Closing, in words a person uses: "close" read as shutting the panel. Light red and over
756
+ by Open in GitHub, away from Reply, so it is not pressed on the way to answering. */
727
757
  const closeBtn = el("button", {
728
- className: "ghbtn",
729
- textContent: item.state === "OPEN" ? "Close" : "Reopen",
758
+ className: "ghbtn" + (item.state === "OPEN" ? " danger" : ""),
759
+ textContent: item.state === "OPEN" ? "Mark as completed" : "Reopen",
730
760
  });
731
761
  closeBtn.onclick = async () => {
732
762
  const closing = item.state === "OPEN";
@@ -735,9 +765,9 @@ function inboxScreen(m, opts) {
735
765
  // Closing is the one thing here that is awkward to undo from a phone later, so it asks
736
766
  // — and since it is asking anyway, it takes a parting comment.
737
767
  body = await compose({
738
- title: "Close " + item.repo + " #" + item.number + "?",
739
- hint: "Anything you write here is posted as a comment first. Leave it empty to just close.",
740
- confirm: "Close it",
768
+ title: "Are you sure you want to mark #" + item.number + " as completed?",
769
+ hint: "This closes it. Anything you write here is posted as a comment first.",
770
+ confirm: "Mark as completed",
741
771
  allowEmpty: true,
742
772
  });
743
773
  if (body === null) return;
@@ -767,9 +797,8 @@ function inboxScreen(m, opts) {
767
797
  openThread({ repo: item.repo, number: item.number, kind: item.kind });
768
798
  } catch (e) { failed(e); }
769
799
  };
770
- buttons.push(closeBtn);
771
-
772
800
  const row = el("div", { className: "row tacts" }, buttons);
801
+ buttons.push(closeBtn);
773
802
 
774
803
  /* Merging cannot be undone with another click, so it asks first and names what it is about
775
804
  to merge. It does not ask *how*: squash, merge commit or rebase is a question about git
@@ -824,9 +853,11 @@ function inboxScreen(m, opts) {
824
853
  row.append(status);
825
854
  // Last, and pushed to the far end: it is the way out of this screen rather than one of the
826
855
  // things you do on it. Same shape as the rest, so the row reads as one set of controls.
856
+ closeBtn.classList.add("out");
827
857
  row.append(
858
+ closeBtn,
828
859
  el("a", {
829
- className: "ghbtn out",
860
+ className: "ghbtn",
830
861
  href: item.url,
831
862
  target: "_blank",
832
863
  rel: "noopener",
@@ -847,8 +878,11 @@ function inboxScreen(m, opts) {
847
878
  if (at >= 0) {
848
879
  const old = S.inbox.items[at];
849
880
  THREADS.set(item.repo + "#" + item.number, fresh);
881
+ THREAD_AT.set(item.repo + "#" + item.number, Date.now());
850
882
  // A single thread does not know which part of the org it is in, or whether it is unread.
851
883
  S.inbox.items[at] = { ...old, ...fresh, role: old.role, unread: old.unread, partial: false };
884
+ } else if (opts.stray && find(item) === opts.stray) {
885
+ Object.assign(opts.stray, fresh, { partial: false });
852
886
  }
853
887
  }
854
888
  } catch {
@@ -1058,6 +1092,24 @@ function timeline(item, openThread) {
1058
1092
  return out;
1059
1093
  }
1060
1094
 
1095
+ /** Older messages in a thread, behind one button that shows them all. */
1096
+ function earlier(nodes, said) {
1097
+ const box = el("div", { className: "tearlier" });
1098
+ const btn = el("button", {
1099
+ className: "tearlierbtn",
1100
+ textContent: "Show " + said + " earlier message" + (said === 1 ? "" : "s"),
1101
+ });
1102
+ btn.setAttribute("aria-expanded", "false");
1103
+ const body = el("div", {}, nodes);
1104
+ body.hidden = true;
1105
+ btn.onclick = () => {
1106
+ body.hidden = false;
1107
+ btn.remove();
1108
+ };
1109
+ box.append(btn, body);
1110
+ return box;
1111
+ }
1112
+
1061
1113
  function folded(events, item, openThread) {
1062
1114
  const box = el("div");
1063
1115
  const btn = el("button", { className: "tevmore" });
@@ -20,6 +20,7 @@ import { mdlite } from "../md.js";
20
20
  import { humansOf, S, writeHash } from "../state.js";
21
21
  import { yamlPre } from "../yaml.js";
22
22
  import { businessForm, fileModes, prioritiesForm } from "./orgedit.js";
23
+ import { peopleCard } from "./people.js";
23
24
  import { repoPicker } from "./repos.js";
24
25
 
25
26
  /* What the files roster itself ships are for, in one line each. A filename says nothing about
@@ -72,6 +73,7 @@ export function viewOrg(m) {
72
73
  kv("Staff", String(org.staff.length), org.staff.map((s) => s.handle).join(", ")),
73
74
  );
74
75
  m.append(facts);
76
+ m.append(peopleCard());
75
77
 
76
78
  /* Marking a product repo after setup. The picker only lived on the setup screen, so a repo
77
79
  created later meant typing `role: product` into org.yaml by hand. Folded, because it asks
@@ -0,0 +1,109 @@
1
+ /* People: who the staff answer to, and what each of them looks after. On the Org screen.
2
+ *
3
+ * Saving writes the humans list in org.yaml, and the server then puts each person's label on
4
+ * every tracker and tells the mention callers who may wake a staff member, so somebody added
5
+ * here can send work straight away. */
6
+
7
+ import { post } from "../api.js";
8
+ import { el } from "../dom.js";
9
+ import { icon } from "../icons.js";
10
+ import { refreshAll } from "../refresh.js";
11
+ import { S } from "../state.js";
12
+
13
+ export function peopleCard() {
14
+ const start = (S.data?.people?.length ? S.data.people : (S.data?.humans ?? [])).map((h) => ({
15
+ name: h.name ?? "",
16
+ github: h.github ?? "",
17
+ marker: h.marker ?? "",
18
+ role: h.role ?? "",
19
+ handles: (h.handles ?? []).join(", "),
20
+ view_only: Boolean(h.viewOnly),
21
+ }));
22
+ const rows = start.map((p) => ({ ...p }));
23
+
24
+ const list = el("div", { className: "peoplelist" });
25
+ const note = el("span", { className: "meta" });
26
+ const save = el("button", { className: "ghbtn primary", textContent: "Save" });
27
+ const add = el("button", { className: "ghbtn", textContent: "Add a person" });
28
+
29
+ const changed = () => JSON.stringify(rows) !== JSON.stringify(start);
30
+ const stamp = () => {
31
+ save.disabled = !changed();
32
+ };
33
+
34
+ function paint() {
35
+ list.replaceChildren(
36
+ el("div", { className: "peoplerow peoplehead", ariaHidden: "true" }, [
37
+ el("span", { textContent: "Name" }),
38
+ el("span", { textContent: "GitHub login" }),
39
+ el("span", { textContent: "Looks after" }),
40
+ el("span", { className: "peopleonlyhead", textContent: "Read only" }),
41
+ el("span"),
42
+ ]),
43
+ ...rows.map((p, i) => row(p, i)),
44
+ );
45
+ stamp();
46
+ }
47
+
48
+ function row(p, i) {
49
+ const input = (key, placeholder, label) => {
50
+ const box = el("input", { type: "text", value: p[key], placeholder, ariaLabel: label });
51
+ box.oninput = () => {
52
+ p[key] = box.value;
53
+ stamp();
54
+ };
55
+ return box;
56
+ };
57
+ const only = el("input", { type: "checkbox", checked: p.view_only, ariaLabel: "Read only" });
58
+ only.onchange = () => {
59
+ p.view_only = only.checked;
60
+ stamp();
61
+ };
62
+ // Quiet on purpose: one per row, and a column of red buttons outshouts the list it edits.
63
+ const remove = el("button", {
64
+ className: "iconbtn peopleremove",
65
+ title: "Remove",
66
+ ariaLabel: `Remove ${p.name || "this person"}`,
67
+ }, [icon("trash", "ic")]);
68
+ remove.onclick = () => {
69
+ rows.splice(i, 1);
70
+ paint();
71
+ };
72
+ return el("div", { className: "peoplerow" }, [
73
+ input("name", "Name", "Name"),
74
+ input("github", "login", "GitHub login"),
75
+ input("handles", "e.g. marketing, content", "Looks after"),
76
+ el("label", { className: "peopleonly" }, [only]),
77
+ remove,
78
+ ]);
79
+ }
80
+
81
+ add.onclick = () => {
82
+ rows.push({ name: "", github: "", marker: "", role: "", handles: "", view_only: false });
83
+ paint();
84
+ list.querySelector(".peoplerow:last-child input")?.focus();
85
+ };
86
+
87
+ save.onclick = async () => {
88
+ save.disabled = true;
89
+ note.className = "meta";
90
+ note.textContent = "Saving…";
91
+ try {
92
+ const r = await post({ people: rows.filter((p) => p.github.trim() || p.name.trim()) }, "/api/people");
93
+ note.textContent = r.warnings?.length ? "Saved, but: " + r.warnings.join("; ") : "Saved.";
94
+ if (r.warnings?.length) note.className = "meta warn";
95
+ await refreshAll(false);
96
+ } catch (e) {
97
+ note.textContent = String(e.message || e);
98
+ note.className = "meta err";
99
+ stamp();
100
+ }
101
+ };
102
+
103
+ paint();
104
+ return el("div", { className: "card peoplecard" }, [
105
+ el("h2", { textContent: "People" }),
106
+ list,
107
+ el("div", { className: "row" }, [add, save, note]),
108
+ ]);
109
+ }
@@ -0,0 +1,378 @@
1
+ /* The Routines screen: when each staff member wakes, and what they do then.
2
+ *
3
+ * Every staff member has a daily round. A routine is one more wake-up with its own job: a time,
4
+ * a name, and what to do in the person's own words. Saving writes the staff member's
5
+ * staff.yaml, routines/<id>.md and the daily caller in one commit, as you.
6
+ */
7
+
8
+ import { post } from "../api.js";
9
+ import { sheet } from "../dialog.js";
10
+ import { el, grow } from "../dom.js";
11
+ import { icon } from "../icons.js";
12
+ import { render } from "../router.js";
13
+
14
+ /* The last answer, so a repaint after Save does not flash an empty screen. */
15
+ let last = null;
16
+
17
+ export function viewRoutines(m) {
18
+ const add = el("button", { className: "ghbtn primary", textContent: "New routine" });
19
+ m.append(el("div", { className: "routhead" }, [el("h1", { textContent: "Routines" }), add]));
20
+ const body = el("div");
21
+ m.append(body);
22
+ if (last) draw(body, last, add);
23
+ else body.append(el("p", { className: "sub", textContent: "Loading…" }));
24
+ fetch("/api/routines")
25
+ .then((r) => r.json())
26
+ .then((d) => {
27
+ if (d.error) throw new Error(d.error);
28
+ last = d;
29
+ body.replaceChildren();
30
+ draw(body, d, add);
31
+ })
32
+ .catch((err) => body.replaceChildren(el("p", { className: "err", textContent: String(err.message || err) })));
33
+ }
34
+
35
+ function draw(body, d, add) {
36
+ add.onclick = () => edit(d, null, null);
37
+ add.disabled = !d.staff.length;
38
+ if (!d.staff.length) {
39
+ body.append(el("p", { className: "sub", textContent: "Hire a staff member first." }));
40
+ return;
41
+ }
42
+ for (const s of d.staff) {
43
+ const list = el("div", { className: "connlist routlist" });
44
+ list.append(dailyRow(s, d));
45
+ for (const r of s.routines) list.append(routineRow(s, r, d));
46
+ const more = el("button", { className: "routadd" }, [icon("plus"), el("span", { textContent: "Add a routine for " + s.name })]);
47
+ more.onclick = () => edit(d, s, null);
48
+ list.append(more);
49
+ body.append(el("h2", { className: "connhead", textContent: s.name }), list);
50
+ }
51
+ }
52
+
53
+ function dailyRow(s, d) {
54
+ const change = el("button", { className: "ghbtn", textContent: "Change time" });
55
+ change.onclick = () => editDaily(d, s);
56
+ return el("div", { className: "connrow routrow" }, [
57
+ el("span", { className: "connicon" }, [icon("refresh")]),
58
+ el("div", { className: "conntext" }, [
59
+ el("b", { textContent: "Daily round" }),
60
+ el("span", { className: "routwhen", textContent: s.dailyWhen }),
61
+ ]),
62
+ el("div", { className: "row connacts" }, [change]),
63
+ ]);
64
+ }
65
+
66
+ function routineRow(s, r, d) {
67
+ const run = el("button", { className: "ghbtn", textContent: "Run now" });
68
+ run.onclick = () => runNow(s, r, run);
69
+ const change = el("button", { className: "ghbtn", textContent: "Edit" });
70
+ change.onclick = () => edit(d, s, r);
71
+ const remove = el("button", { className: "ghbtn", textContent: "Remove" });
72
+ remove.onclick = () => confirmRemove(s, r, remove);
73
+ return el("div", { className: "connrow routrow" }, [
74
+ el("span", { className: "connicon" }, [icon("clock")]),
75
+ el("div", { className: "conntext" }, [
76
+ el("b", { textContent: r.title }),
77
+ el("span", { className: "routwhen", textContent: r.when }),
78
+ r.body ? el("span", { className: "routbody", textContent: r.body }) : null,
79
+ ].filter(Boolean)),
80
+ el("div", { className: "row connacts" }, [run, change, remove]),
81
+ ]);
82
+ }
83
+
84
+ async function runNow(s, r, btn) {
85
+ btn.disabled = true;
86
+ btn.textContent = "Starting…";
87
+ try {
88
+ const res = await post({ staff: s.handle, routine: r.id }, "/api/run/start");
89
+ btn.replaceWith(el("a", { className: "ghbtn", href: res.url, target: "_blank", rel: "noopener", textContent: "Started" }));
90
+ } catch (err) {
91
+ btn.disabled = false;
92
+ btn.textContent = "Run now";
93
+ btn.title = String(err.message || err);
94
+ }
95
+ }
96
+
97
+ async function confirmRemove(s, r, btn) {
98
+ if (btn.dataset.sure !== "1") {
99
+ btn.dataset.sure = "1";
100
+ btn.textContent = "Remove " + r.title + "?";
101
+ btn.classList.add("danger");
102
+ return;
103
+ }
104
+ btn.disabled = true;
105
+ btn.textContent = "Removing…";
106
+ try {
107
+ await save(s, s.daily, s.routines.filter((x) => x.id !== r.id));
108
+ last = null;
109
+ render();
110
+ } catch (err) {
111
+ btn.textContent = String(err.message || err);
112
+ }
113
+ }
114
+
115
+ /** One staff member's whole schedule, as the server wants it. */
116
+ function save(s, daily, routines) {
117
+ return post(
118
+ {
119
+ staff: s.handle,
120
+ daily,
121
+ routines: routines.map((r) => ({ id: r.id, title: r.title, body: r.body, schedule: r.schedule })),
122
+ },
123
+ "/api/routines/save",
124
+ );
125
+ }
126
+
127
+ /* --------------------------------- editors --------------------------------- */
128
+
129
+ function edit(d, s, r) {
130
+ let box = null;
131
+ const node = routineForm(d, s, r, () => box?.close());
132
+ box = sheet({ node, onClose: () => {} });
133
+ if (!box) document.querySelector("#main")?.append(node);
134
+ }
135
+
136
+ function editDaily(d, s) {
137
+ let box = null;
138
+ const close = () => box?.close();
139
+ const out = el("div", { className: "routform" });
140
+ out.append(el("h2", { className: "sheettitle", textContent: s.name + "'s daily round" }));
141
+ const when = whenField(s.daily);
142
+ out.append(when.node);
143
+ const status = el("div");
144
+ const ok = el("button", { className: "ghbtn primary", textContent: "Save" });
145
+ const cancel = el("button", { className: "ghbtn", textContent: "Cancel" });
146
+ cancel.onclick = close;
147
+ ok.onclick = async () => {
148
+ ok.disabled = true;
149
+ ok.textContent = "Saving…";
150
+ try {
151
+ await save(s, when.value(), s.routines);
152
+ last = null;
153
+ close();
154
+ render();
155
+ } catch (err) {
156
+ ok.disabled = false;
157
+ ok.textContent = "Save";
158
+ status.replaceChildren(el("p", { className: "err", textContent: String(err.message || err) }));
159
+ }
160
+ };
161
+ out.append(status, el("div", { className: "row connfoot" }, [ok, cancel]));
162
+ box = sheet({ node: out, onClose: () => {} });
163
+ if (!box) document.querySelector("#main")?.append(out);
164
+ }
165
+
166
+ function routineForm(d, s, r, close) {
167
+ const out = el("div", { className: "routform" });
168
+ out.append(el("h2", { className: "sheettitle", textContent: r ? r.title : "New routine" }));
169
+
170
+ let who = s ?? d.staff[0];
171
+ const pick = el("div", { className: "routwho" });
172
+ if (!r) {
173
+ for (const p of d.staff) {
174
+ const chip = el("button", { className: "routchip" + (p === who ? " on" : ""), textContent: p.name, type: "button" });
175
+ chip.onclick = () => {
176
+ who = p;
177
+ for (const c of pick.children) c.classList.toggle("on", c === chip);
178
+ say();
179
+ };
180
+ pick.append(chip);
181
+ }
182
+ out.append(field("Who", pick));
183
+ }
184
+
185
+ const name = el("input", { type: "text", value: r?.title ?? "", placeholder: "Monday plan", spellcheck: true });
186
+ out.append(field("Name", name));
187
+
188
+ const when = whenField(r?.schedule ?? "0 9 * * 1");
189
+ out.append(when.node);
190
+
191
+ const todo = el("textarea", {
192
+ className: "routtodo",
193
+ value: r?.body ?? "",
194
+ rows: 8,
195
+ placeholder: "Plan the week. Read last week's status issue and this month's priorities, then open one issue per thing to ship this week, ranked.",
196
+ });
197
+ out.append(field("What to do", todo));
198
+
199
+ const preview = el("div", { className: "routpreview" });
200
+ out.append(preview);
201
+ const say = () => {
202
+ const text = todo.value.trim();
203
+ preview.replaceChildren(
204
+ el("span", { className: "connicon" }, [icon("clock")]),
205
+ el("div", {}, [
206
+ el("b", { textContent: describe(when.value()) + ", " + who.name + " wakes up and:" }),
207
+ el("p", { textContent: text || "…" }),
208
+ ]),
209
+ );
210
+ };
211
+ name.oninput = say;
212
+ todo.oninput = () => {
213
+ grow(todo);
214
+ say();
215
+ };
216
+ when.onchange(say);
217
+ say();
218
+
219
+ const status = el("div");
220
+ const ok = el("button", { className: "ghbtn primary", textContent: "Save" });
221
+ const cancel = el("button", { className: "ghbtn", textContent: "Cancel" });
222
+ cancel.onclick = close;
223
+ ok.onclick = async () => {
224
+ const next = { id: r?.id, title: name.value.trim(), body: todo.value, schedule: when.value() };
225
+ if (!next.title) return status.replaceChildren(el("p", { className: "err", textContent: "Give it a name." }));
226
+ if (!next.body.trim()) return status.replaceChildren(el("p", { className: "err", textContent: "Say what to do." }));
227
+ ok.disabled = true;
228
+ ok.textContent = "Saving…";
229
+ try {
230
+ const routines = r ? who.routines.map((x) => (x.id === r.id ? next : x)) : [...who.routines, next];
231
+ await save(who, who.daily, routines);
232
+ last = null;
233
+ close();
234
+ render();
235
+ } catch (err) {
236
+ ok.disabled = false;
237
+ ok.textContent = "Save";
238
+ status.replaceChildren(el("p", { className: "err", textContent: String(err.message || err) }));
239
+ }
240
+ };
241
+ out.append(status, el("div", { className: "row connfoot" }, [ok, cancel]));
242
+ return out;
243
+ }
244
+
245
+ function field(label, control) {
246
+ return el("div", { className: "field routfield" }, [el("span", { textContent: label }), control]);
247
+ }
248
+
249
+ /* --------------------------------- the time --------------------------------- */
250
+
251
+ const DAYS = [
252
+ ["1", "Mon"],
253
+ ["2", "Tue"],
254
+ ["3", "Wed"],
255
+ ["4", "Thu"],
256
+ ["5", "Fri"],
257
+ ["6", "Sat"],
258
+ ["0", "Sun"],
259
+ ];
260
+
261
+ /**
262
+ * When, as days and a time, or a day of the month, rather than as cron. The value is still a
263
+ * cron, because that is what goes in the workflow. A cron the picker cannot show is kept as
264
+ * typed, in a box of its own.
265
+ */
266
+ function whenField(cron) {
267
+ const parsed = parseCron(cron);
268
+ const listeners = [];
269
+ const changed = () => {
270
+ for (const f of listeners) f();
271
+ said.textContent = describe(value()) + local(time.value);
272
+ };
273
+
274
+ const repeat = el("select");
275
+ repeat.append(
276
+ el("option", { value: "week", textContent: "Days of the week" }),
277
+ el("option", { value: "month", textContent: "Day of the month" }),
278
+ el("option", { value: "cron", textContent: "Cron" }),
279
+ );
280
+ repeat.value = parsed.kind;
281
+
282
+ const days = new Set(parsed.days);
283
+ const dayRow = el("div", { className: "routdays" });
284
+ for (const [v, label] of DAYS) {
285
+ const chip = el("button", { className: "routchip" + (days.has(v) ? " on" : ""), textContent: label, type: "button" });
286
+ chip.onclick = () => {
287
+ days.has(v) ? days.delete(v) : days.add(v);
288
+ chip.classList.toggle("on", days.has(v));
289
+ changed();
290
+ };
291
+ dayRow.append(chip);
292
+ }
293
+ const dom = el("input", { type: "number", min: "1", max: "28", value: String(parsed.dom), style: "width:80px" });
294
+ const time = el("input", { type: "time", step: "60", value: parsed.time, style: "width:130px" });
295
+ const raw = el("input", { type: "text", value: cron, spellcheck: false, placeholder: "0 9 * * 1" });
296
+ const said = el("small", { className: "routsaid" });
297
+
298
+ const domRow = el("div", { className: "row" }, [el("span", { className: "meta", textContent: "Day" }), dom]);
299
+ const timeRow = el("div", { className: "row" }, [time, el("span", { className: "meta", textContent: "UTC" })]);
300
+ const show = () => {
301
+ dayRow.hidden = repeat.value !== "week";
302
+ domRow.hidden = repeat.value !== "month";
303
+ timeRow.hidden = repeat.value === "cron";
304
+ raw.hidden = repeat.value !== "cron";
305
+ };
306
+ repeat.onchange = () => {
307
+ show();
308
+ changed();
309
+ };
310
+ for (const input of [dom, time, raw]) input.oninput = changed;
311
+
312
+ function value() {
313
+ if (repeat.value === "cron") return raw.value.trim();
314
+ const [h, m] = (time.value || "09:00").split(":").map(Number);
315
+ if (repeat.value === "month") return `${m} ${h} ${Math.min(28, Math.max(1, Number(dom.value) || 1))} * *`;
316
+ const picked = DAYS.map(([v]) => v).filter((v) => days.has(v));
317
+ return `${m} ${h} * * ${dayList(picked)}`;
318
+ }
319
+
320
+ show();
321
+ said.textContent = describe(value()) + local(time.value);
322
+ const node = el("div", { className: "field routfield" }, [
323
+ el("span", { textContent: "When" }),
324
+ el("div", { className: "routwhenpick" }, [repeat, dayRow, domRow, timeRow, raw, said]),
325
+ ]);
326
+ return { node, value, onchange: (f) => listeners.push(f) };
327
+ }
328
+
329
+ /** Days for cron, in its own short forms where there is one. */
330
+ function dayList(picked) {
331
+ const set = new Set(picked);
332
+ if (set.size === 0 || set.size === 7) return "*";
333
+ if (set.size === 5 && ["1", "2", "3", "4", "5"].every((d) => set.has(d))) return "1-5";
334
+ return [...set].map(Number).sort((a, b) => a - b).join(",");
335
+ }
336
+
337
+ export function parseCron(cron) {
338
+ const [min, hour, dom, mon, dow] = String(cron).trim().split(/\s+/);
339
+ const plain = /^\d+$/.test(min ?? "") && /^\d+$/.test(hour ?? "") && mon === "*";
340
+ const time = plain ? `${hour.padStart(2, "0")}:${min.padStart(2, "0")}` : "09:00";
341
+ if (plain && dom === "*") {
342
+ const days =
343
+ dow === "*" ? ["0", "1", "2", "3", "4", "5", "6"] : dow === "1-5" ? ["1", "2", "3", "4", "5"] : /^[0-6](,[0-6])*$/.test(dow) ? dow.split(",") : null;
344
+ if (days) return { kind: "week", days, time, dom: 1 };
345
+ }
346
+ if (plain && /^\d+$/.test(dom ?? "") && dow === "*") return { kind: "month", days: ["1"], time, dom: Number(dom) };
347
+ return { kind: "cron", days: ["1"], time, dom: 1 };
348
+ }
349
+
350
+ const DAY_NAME = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
351
+
352
+ /** The same words the server gives a schedule, so the preview and the list agree. */
353
+ export function describe(cron) {
354
+ const p = parseCron(cron);
355
+ const at = "at " + p.time + " UTC";
356
+ if (p.kind === "month") return `On the ${ordinal(p.dom)} of each month ${at}`;
357
+ if (p.kind === "cron") return cron || "No time yet";
358
+ const d = cron.trim().split(/\s+/)[4];
359
+ if (d === "*") return "Every day " + at;
360
+ if (d === "1-5") return "Weekdays " + at;
361
+ if (d === "0,6" || d === "6,0") return "Weekends " + at;
362
+ const names = p.days.map((x) => DAY_NAME[Number(x)] + "s");
363
+ return (names.length > 1 ? names.slice(0, -1).join(", ") + " and " + names.at(-1) : names[0]) + " " + at;
364
+ }
365
+
366
+ function ordinal(n) {
367
+ const tens = n % 100;
368
+ return n + (tens >= 11 && tens <= 13 ? "th" : (["th", "st", "nd", "rd"][n % 10] ?? "th"));
369
+ }
370
+
371
+ /** What that UTC time is where the person reading it is, when the two differ. */
372
+ function local(hhmm) {
373
+ if (!hhmm) return "";
374
+ const [h, m] = hhmm.split(":").map(Number);
375
+ const at = new Date(Date.UTC(2026, 0, 5, h, m));
376
+ const here = at.toLocaleTimeString(undefined, { hour: "2-digit", minute: "2-digit" });
377
+ return here === hhmm ? "" : " · " + here + " where you are";
378
+ }