synabun 2.4.3 → 2.4.4

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 (153) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/README.md +1 -1
  3. package/docs/browser-extension-bridge.md +61 -32
  4. package/mcp-server/dist/http.d.ts.map +1 -1
  5. package/mcp-server/dist/http.js +8 -1
  6. package/mcp-server/dist/http.js.map +1 -1
  7. package/mcp-server/dist/services/identity.d.ts +2 -0
  8. package/mcp-server/dist/services/identity.d.ts.map +1 -1
  9. package/mcp-server/dist/services/identity.js +4 -0
  10. package/mcp-server/dist/services/identity.js.map +1 -1
  11. package/mcp-server/dist/services/memory-client.d.ts +5 -0
  12. package/mcp-server/dist/services/memory-client.d.ts.map +1 -1
  13. package/mcp-server/dist/services/memory-client.js +21 -2
  14. package/mcp-server/dist/services/memory-client.js.map +1 -1
  15. package/mcp-server/dist/services/memory-writer.d.ts +3 -0
  16. package/mcp-server/dist/services/memory-writer.d.ts.map +1 -1
  17. package/mcp-server/dist/services/memory-writer.js +7 -1
  18. package/mcp-server/dist/services/memory-writer.js.map +1 -1
  19. package/mcp-server/dist/services/neural-interface.d.ts +11 -1
  20. package/mcp-server/dist/services/neural-interface.d.ts.map +1 -1
  21. package/mcp-server/dist/services/neural-interface.js +67 -17
  22. package/mcp-server/dist/services/neural-interface.js.map +1 -1
  23. package/mcp-server/dist/services/typesafe-config.d.ts +38 -2
  24. package/mcp-server/dist/services/typesafe-config.d.ts.map +1 -1
  25. package/mcp-server/dist/services/typesafe-config.js +260 -8
  26. package/mcp-server/dist/services/typesafe-config.js.map +1 -1
  27. package/mcp-server/dist/tools/agent-definition.d.ts +2 -2
  28. package/mcp-server/dist/tools/agents.d.ts +3 -1
  29. package/mcp-server/dist/tools/agents.d.ts.map +1 -1
  30. package/mcp-server/dist/tools/agents.js +11 -6
  31. package/mcp-server/dist/tools/agents.js.map +1 -1
  32. package/mcp-server/dist/tools/browser-observe.d.ts +11 -0
  33. package/mcp-server/dist/tools/browser-observe.d.ts.map +1 -1
  34. package/mcp-server/dist/tools/browser-observe.js +113 -27
  35. package/mcp-server/dist/tools/browser-observe.js.map +1 -1
  36. package/mcp-server/dist/tools/browser-readers.d.ts +6 -0
  37. package/mcp-server/dist/tools/browser-readers.d.ts.map +1 -0
  38. package/mcp-server/dist/tools/browser-readers.js +17 -0
  39. package/mcp-server/dist/tools/browser-readers.js.map +1 -0
  40. package/mcp-server/dist/tools/remember.d.ts.map +1 -1
  41. package/mcp-server/dist/tools/remember.js +6 -2
  42. package/mcp-server/dist/tools/remember.js.map +1 -1
  43. package/mcp-server/dist/tools/youtube/analytics.d.ts +4 -0
  44. package/mcp-server/dist/tools/youtube/analytics.d.ts.map +1 -1
  45. package/mcp-server/dist/tools/youtube/analytics.js +25 -20
  46. package/mcp-server/dist/tools/youtube/analytics.js.map +1 -1
  47. package/mcp-server/dist/tools/youtube/helpers.d.ts +5 -1
  48. package/mcp-server/dist/tools/youtube/helpers.d.ts.map +1 -1
  49. package/mcp-server/dist/tools/youtube/helpers.js +7 -3
  50. package/mcp-server/dist/tools/youtube/helpers.js.map +1 -1
  51. package/mcp-server/dist/tools/youtube/navigate.d.ts +2 -0
  52. package/mcp-server/dist/tools/youtube/navigate.d.ts.map +1 -1
  53. package/mcp-server/dist/tools/youtube/navigate.js +5 -3
  54. package/mcp-server/dist/tools/youtube/navigate.js.map +1 -1
  55. package/mcp-server/dist/tools/youtube/playlist.js +2 -2
  56. package/mcp-server/dist/tools/youtube/playlist.js.map +1 -1
  57. package/mcp-server/dist/tools/youtube/readers.d.ts +4 -0
  58. package/mcp-server/dist/tools/youtube/readers.d.ts.map +1 -0
  59. package/mcp-server/dist/tools/youtube/readers.js +15 -0
  60. package/mcp-server/dist/tools/youtube/readers.js.map +1 -0
  61. package/mcp-server/package-lock.json +2 -2
  62. package/mcp-server/package.json +1 -1
  63. package/mcp-server/src/http.ts +8 -1
  64. package/mcp-server/src/services/identity.ts +5 -0
  65. package/mcp-server/src/services/memory-client.ts +32 -12
  66. package/mcp-server/src/services/memory-writer.ts +5 -2
  67. package/mcp-server/src/services/neural-interface.ts +65 -18
  68. package/mcp-server/src/services/typesafe-config.ts +268 -7
  69. package/mcp-server/src/tools/agents.ts +12 -6
  70. package/mcp-server/src/tools/browser-observe.ts +117 -28
  71. package/mcp-server/src/tools/browser-readers.ts +19 -0
  72. package/mcp-server/src/tools/remember.ts +6 -1
  73. package/mcp-server/src/tools/youtube/analytics.ts +26 -28
  74. package/mcp-server/src/tools/youtube/helpers.ts +8 -2
  75. package/mcp-server/src/tools/youtube/navigate.ts +6 -7
  76. package/mcp-server/src/tools/youtube/playlist.ts +2 -2
  77. package/mcp-server/src/tools/youtube/readers.ts +15 -0
  78. package/neural-interface/i18n/en.json +139 -0
  79. package/neural-interface/i18n/pt-BR.json +139 -0
  80. package/neural-interface/lib/assistant-api.js +90 -0
  81. package/neural-interface/lib/assistant-brains/claude.js +10 -0
  82. package/neural-interface/lib/assistant-dispatch.js +276 -42
  83. package/neural-interface/lib/assistant-memory.js +3 -1
  84. package/neural-interface/lib/assistant-persona.js +6 -0
  85. package/neural-interface/lib/assistant-router.js +18 -8
  86. package/neural-interface/lib/assistant-runtime.js +87 -1
  87. package/neural-interface/lib/assistant-task-prompt.js +15 -2
  88. package/neural-interface/lib/browser-connection-state.js +20 -0
  89. package/neural-interface/lib/browser-extension-adapter.js +221 -15
  90. package/neural-interface/lib/browser-extension-bridge.js +40 -7
  91. package/neural-interface/lib/browser-extension-folder.js +50 -11
  92. package/neural-interface/lib/browser-extension-package.js +6 -5
  93. package/neural-interface/lib/browser-extension-pin.json +15 -13
  94. package/neural-interface/lib/browser-extension-protocol.js +111 -13
  95. package/neural-interface/lib/browser-extension-setup.js +7 -5
  96. package/neural-interface/lib/browser-extension-update.js +104 -0
  97. package/neural-interface/lib/browser-extraction.js +38 -14
  98. package/neural-interface/lib/claude-bg-tasks.js +183 -0
  99. package/neural-interface/lib/claude-history.js +12 -1
  100. package/neural-interface/lib/confined-fs.js +25 -0
  101. package/neural-interface/lib/coordination/operations.js +2 -0
  102. package/neural-interface/lib/db.js +39 -16
  103. package/neural-interface/lib/editor-completion-opencode.js +2 -2
  104. package/neural-interface/lib/git-bash/manager.js +28 -3
  105. package/neural-interface/lib/git-status-runner.js +31 -3
  106. package/neural-interface/lib/loop-inventory.js +162 -0
  107. package/neural-interface/lib/loop-stall.js +74 -4
  108. package/neural-interface/lib/memory-session-capture.js +157 -66
  109. package/neural-interface/lib/native-loop-providers.js +73 -13
  110. package/neural-interface/lib/native-loop-runtime.js +3 -0
  111. package/neural-interface/lib/opencode-protocol.js +4 -2
  112. package/neural-interface/lib/peer-identity.js +62 -16
  113. package/neural-interface/lib/session-cache-builder.js +66 -18
  114. package/neural-interface/lib/session-capture-gate.js +42 -0
  115. package/neural-interface/lib/session-indexer.js +28 -20
  116. package/neural-interface/lib/teams/scheduler.js +3 -2
  117. package/neural-interface/lib/ui-blob-api.js +14 -5
  118. package/neural-interface/lib/ui-blob-store.js +302 -39
  119. package/neural-interface/package-lock.json +2 -2
  120. package/neural-interface/package.json +1 -1
  121. package/neural-interface/public/extension-setup.html +1 -0
  122. package/neural-interface/public/shared/assistant/asst-agents-dock.js +4 -0
  123. package/neural-interface/public/shared/assistant/asst-api.js +42 -0
  124. package/neural-interface/public/shared/assistant/asst-composer.js +85 -12
  125. package/neural-interface/public/shared/assistant/asst-panel.js +285 -10
  126. package/neural-interface/public/shared/assistant/asst-render.js +205 -4
  127. package/neural-interface/public/shared/assistant/asst-scenes.js +12 -0
  128. package/neural-interface/public/shared/assistant/asst-sidepanel.js +18 -1
  129. package/neural-interface/public/shared/assistant/asst-state.js +241 -2
  130. package/neural-interface/public/shared/assistant/asst-steer.js +294 -0
  131. package/neural-interface/public/shared/assistant/asst-styles.js +20 -0
  132. package/neural-interface/public/shared/bg-tasks/bg-tasks-claude.js +147 -0
  133. package/neural-interface/public/shared/bg-tasks/bg-tasks-model.js +527 -0
  134. package/neural-interface/public/shared/bg-tasks/bg-tasks-panel.js +161 -0
  135. package/neural-interface/public/shared/bg-tasks/bg-tasks-view.js +508 -0
  136. package/neural-interface/public/shared/browser-extension-updates.js +88 -0
  137. package/neural-interface/public/shared/cp/cp-bg-tasks.js +130 -0
  138. package/neural-interface/public/shared/extension-setup/es-page.js +18 -11
  139. package/neural-interface/public/shared/extension-update-view.js +34 -0
  140. package/neural-interface/public/shared/ocp-v2/ocp-v2-shell-logic.js +8 -4
  141. package/neural-interface/public/shared/settings/settings-browser-extension.js +10 -4
  142. package/neural-interface/public/shared/settings/settings-extension-update.js +114 -0
  143. package/neural-interface/public/shared/styles.css +4 -0
  144. package/neural-interface/public/shared/ui-claude-panel.js +110 -1
  145. package/neural-interface/public/shared/ui-notifications.js +23 -4
  146. package/neural-interface/public/shared/ui-settings-entry.js +6 -0
  147. package/neural-interface/public/shared/ui-tray-columns.js +9 -2
  148. package/neural-interface/public/shared/worker-end.js +18 -0
  149. package/neural-interface/scripts/patch-node-pty.mjs +151 -0
  150. package/neural-interface/scripts/rebuild-pty.js +10 -0
  151. package/neural-interface/server.js +483 -280
  152. package/npm-shrinkwrap.json +2 -2
  153. package/package.json +1 -1
@@ -0,0 +1,161 @@
1
+ // ─────────────────────────────────────────────────────────────────────────────
2
+ // Background tasks of one panel tab: registry, rebuild, kept choices, view
3
+ //
4
+ // What a panel tab needs, in one place, whatever the provider: it hands in how
5
+ // its provider's messages become facts (`factsOf`, an adapter such as
6
+ // bg-tasks-claude.js), where the server keeps the rebuilt list and the output
7
+ // (`api`), and where the view goes (`host`). The panel then only says what it
8
+ // receives (`onEvent`, `onLive`, `ended`) and draws the badges `onChange`
9
+ // reports. Rebuilt from the server when the tab attaches (the transcript or
10
+ // the server's record), so a reload or a restart never starts from nothing.
11
+ // ─────────────────────────────────────────────────────────────────────────────
12
+
13
+ import { applyBgFact, bgPrefsOf, clearFinishedBg, createBgRegistry, mergeRebuiltBg, noticeCardView, runningBgCount } from './bg-tasks-model.js';
14
+ import { createBgTasksView, installBgTasksStyles, renderBgNoticeCard } from './bg-tasks-view.js';
15
+
16
+ const PREFS_PREFIX = 'synabun-bg-tasks:';
17
+
18
+ /**
19
+ * createBgTasksPanel({ key, factsOf, api, storage, t, onChange, onShownChange })
20
+ * key what the kept choices are saved under (`<provider>:<session id>`); setKey() when it is known
21
+ * factsOf(ev) → facts (the provider's adapter)
22
+ * api { list() → Promise<{ tasks, live? }>, output(q) → Promise<page>, stop(taskId) → Promise<{ ok, error }> }
23
+ * storage getItem / setItem / removeItem (the app's server-synced storage)
24
+ * onChange({ running, total }) the counts changed (badges)
25
+ */
26
+ export function createBgTasksPanel({ key = '', factsOf, liveFactOf = null, api, storage = null, t = null, overlay = true, onChange = () => {}, onShownChange = () => {} } = {}) {
27
+ installBgTasksStyles();
28
+ let reg = createBgRegistry();
29
+ let storageKey = key ? PREFS_PREFIX + key : '';
30
+ let view = null;
31
+ let lastCounts = '';
32
+ let rebuilding = null;
33
+ let outputSupported = null; // false: the server has no output route yet (a restart of :3344 brings it)
34
+
35
+ const readPrefs = () => {
36
+ if (!storageKey || !storage) return null;
37
+ try { return JSON.parse(storage.getItem(storageKey) || 'null'); } catch { return null; }
38
+ };
39
+ const writePrefs = () => {
40
+ if (!storageKey || !storage) return;
41
+ const prefs = bgPrefsOf(reg);
42
+ try {
43
+ if (!prefs.cleared.length && !prefs.ownerStopped.length) storage.removeItem?.(storageKey);
44
+ else storage.setItem(storageKey, JSON.stringify(prefs));
45
+ } catch { /* storage full or gone: the view still works */ }
46
+ };
47
+ // What the page kept is applied as soon as it is known: a cleared task never shows again.
48
+ const applyPrefs = () => { const prefs = readPrefs(); for (const id of prefs?.cleared || []) reg.cleared.add(id); return prefs; };
49
+ applyPrefs();
50
+
51
+ function changed() {
52
+ const counts = { running: runningBgCount(reg), total: reg.tasks.size };
53
+ const sig = `${counts.running}/${counts.total}/${reg.version}`;
54
+ if (view?.isShown()) view.render();
55
+ if (sig === lastCounts) return;
56
+ lastCounts = sig;
57
+ try { onChange(counts); } catch { /* a badge that failed to draw */ }
58
+ }
59
+
60
+ function ensureView(host) {
61
+ if (view) { if (host && view.el.parentElement !== host) host.appendChild(view.el); return view; }
62
+ view = createBgTasksView({
63
+ registry: () => reg,
64
+ readOutput: (q) => api.output(q),
65
+ outputSupported: () => outputSupported !== false,
66
+ stopTask: async (taskId) => {
67
+ applyBgFact(reg, { kind: 'stopping', taskId, by: 'owner' });
68
+ writePrefs();
69
+ changed();
70
+ let out;
71
+ try { out = await api.stop(taskId); } catch (err) { out = { ok: false, error: err?.message || String(err) }; }
72
+ if (!out?.ok) { applyBgFact(reg, { kind: 'stop-failed', taskId, error: out?.error || 'unknown error' }); writePrefs(); changed(); }
73
+ return out;
74
+ },
75
+ onClear: () => { clearFinishedBg(reg); writePrefs(); changed(); },
76
+ onBack: () => setShown(false),
77
+ t,
78
+ });
79
+ view.el.classList.add(overlay ? 'sbt-overlay' : 'sbt-inline');
80
+ if (host) host.appendChild(view.el);
81
+ return view;
82
+ }
83
+
84
+ function setShown(next, host = null) {
85
+ const v = ensureView(host);
86
+ v.setShown(!!next);
87
+ try { onShownChange(!!next); } catch { /* ignore */ }
88
+ }
89
+
90
+ return {
91
+ registry: () => reg,
92
+ counts: () => ({ running: runningBgCount(reg), total: reg.tasks.size }),
93
+ /** The kept choices' key (the session id arrives after the tab exists). */
94
+ setKey(next) {
95
+ const k = next ? PREFS_PREFIX + next : '';
96
+ if (k === storageKey) return;
97
+ storageKey = k;
98
+ applyPrefs();
99
+ },
100
+ /**
101
+ * One message of the provider's stream. → the task-notification cards it carries (`noticeCardView`), each
102
+ * `{ view, task }`; a repeated end (the same end told twice) is left out.
103
+ */
104
+ onEvent(ev) {
105
+ let facts = [];
106
+ try { facts = factsOf(ev) || []; } catch { facts = []; }
107
+ if (!facts.length) return [];
108
+ const cards = [];
109
+ for (const fact of facts) {
110
+ const out = applyBgFact(reg, fact);
111
+ if (out?.notice && !out.notice.repeat) cards.push({ view: noticeCardView(out.notice, out.task), task: out.task });
112
+ }
113
+ changed();
114
+ return cards;
115
+ },
116
+ /** The live set (a reattach's list, `background_tasks_changed`). */
117
+ onLive(tasks) {
118
+ const fact = liveFactOf ? liveFactOf(tasks) : { kind: 'live', tasks };
119
+ applyBgFact(reg, fact);
120
+ changed();
121
+ },
122
+ /** The process that ran the tasks is gone. */
123
+ ended() { applyBgFact(reg, { kind: 'ended-all' }); changed(); },
124
+ /** Rebuild from the server (the transcript), keeping what the page knew and chose. */
125
+ async rebuild() {
126
+ if (!api?.list) return false;
127
+ if (rebuilding) return rebuilding;
128
+ rebuilding = (async () => {
129
+ let data = null;
130
+ try { data = await api.list(); } catch { data = null; }
131
+ if (data?.unsupported) { outputSupported = false; view?.render(); return false; }
132
+ if (data && data.supported !== false) outputSupported = true;
133
+ if (!data || !Array.isArray(data.tasks)) return false;
134
+ const merged = mergeRebuiltBg(reg, data, applyPrefs());
135
+ if (Array.isArray(data.live)) applyBgFact(reg, liveFactOf ? liveFactOf(data.live) : { kind: 'live', tasks: data.live });
136
+ changed();
137
+ return merged;
138
+ })().finally(() => { rebuilding = null; });
139
+ return rebuilding;
140
+ },
141
+ /** A new conversation in the same tab: nothing of the old one is listed. */
142
+ reset(nextKey = '') {
143
+ reg = createBgRegistry();
144
+ storageKey = nextKey ? PREFS_PREFIX + nextKey : '';
145
+ applyPrefs();
146
+ changed();
147
+ },
148
+ show(host, taskId = null) { setShown(true, host); if (taskId) view.open(taskId); },
149
+ hide() { if (view) setShown(false); },
150
+ isShown: () => !!view?.isShown(),
151
+ toggle(host) { if (view?.isShown()) setShown(false); else setShown(true, host); return !!view?.isShown(); },
152
+ /** A card for a notice of the transcript (`cards` from onEvent). */
153
+ card(cardView, onOpen) { return renderBgNoticeCard(cardView, { t, onOpen }); },
154
+ /** The tab closed: its kept choices go with it. */
155
+ dispose({ forget = false } = {}) {
156
+ view?.dispose();
157
+ view = null;
158
+ if (forget && storageKey && storage) { try { storage.removeItem?.(storageKey); } catch { /* ignore */ } }
159
+ },
160
+ };
161
+ }
@@ -0,0 +1,508 @@
1
+ // ─────────────────────────────────────────────────────────────────────────────
2
+ // The Background tasks view (DOM)
3
+ //
4
+ // One module for every panel that runs an agent with background work: the
5
+ // Claude Code side panel and the Assistant panel today, Codex, OpenCode and
6
+ // Antigravity once their adapters feed the same registry (bg-tasks-model.js).
7
+ // It draws:
8
+ // paintBgBadge(host, count) the "⟳ N" badge of a header button or a tray pill
9
+ // createBgTasksView({ … }) the list that takes the transcript's place: per task its kind, status,
10
+ // time, exit code, its live output (follow, jump to latest, open full output)
11
+ // and the events a watch delivered; Stop (asked first) and Clear finished
12
+ // renderBgNoticeCard(view, { … }) a task-notification as a compact card of the transcript
13
+ // The output is the OpenCode panel's terminal block and follower (ocp-v2-shell-block.js,
14
+ // ocp-v2-shell-logic.js): read in pages from the panel's own route, only while a task is open in a view on
15
+ // screen, at most once a second. Everything a task printed goes in as text, never as markup.
16
+ // ─────────────────────────────────────────────────────────────────────────────
17
+
18
+ import { createTerminalBlock } from '../ocp-v2/ocp-v2-shell-block.js';
19
+ import { createShellFollower, formatBytes } from '../ocp-v2/ocp-v2-shell-logic.js';
20
+ import { BG_TASKS_COPY, bgTaskRows, fillBgCopy, isBgRunning } from './bg-tasks-model.js';
21
+ import { BG_RUNNING_ATTR } from '../ui-tray-columns.js';
22
+
23
+ /** No read of a running task's output comes sooner than this after the last one brought nothing new. */
24
+ export const BG_OUTPUT_POLL_MS = 1000;
25
+ const BG_OUTPUT_POLL_MAX_MS = 4000;
26
+ const STYLE_ID = 'sbt-styles';
27
+
28
+ export const BG_TASKS_CSS = `
29
+ .sbt-scope, .sbt-view, .sbt-notice, .sbt-badge {
30
+ /* DESIGN.md status roles (success, warning, danger); the rest are the app's own tokens. */
31
+ --sbt-ok: #86efac; --sbt-warn: #fbbf24; --sbt-err: #fca5a5;
32
+ }
33
+ .sbt-badge { display: inline-flex; align-items: center; gap: 3px; font: 500 10px/1 'JetBrains Mono', ui-monospace, monospace;
34
+ color: var(--accent-gold); background: var(--accent-gold-bg); border: 1px solid var(--accent-gold-border);
35
+ border-radius: var(--r-pill); padding: 2px 6px; cursor: pointer; white-space: nowrap; flex-shrink: 0; }
36
+ .sbt-badge:hover { border-color: var(--accent-gold); }
37
+ .sbt-badge[hidden] { display: none; }
38
+ .sbt-badge-spin { display: inline-block; animation: sbt-spin 1.6s linear infinite; }
39
+ @keyframes sbt-spin { to { transform: rotate(360deg); } }
40
+ @media (prefers-reduced-motion: reduce) { .sbt-badge-spin { animation: none; } }
41
+ .term-minimized-pill .sbt-badge { margin-left: 4px; padding: 1px 5px; font-size: 9px; }
42
+ /* On a header button (paintBgBadge corner: true): a notification count on the icon's top-right corner, out of the
43
+ row's flow, so the button keeps its size and the row its height. Gold is DESIGN.md accent, the figure its background;
44
+ the ring is the header's own colour (--sbt-badge-ring, set by each panel) so the count reads on top of the icon. */
45
+ .sbt-badge.sbt-badge-corner { position: absolute; top: -4px; right: -5px; z-index: 1; box-sizing: border-box; justify-content: center;
46
+ gap: 0; min-width: 14px; height: 14px; padding: 0 3px; border: 0; border-radius: 7px;
47
+ font: 600 9px/1 'JetBrains Mono', ui-monospace, monospace; font-variant-numeric: tabular-nums;
48
+ color: #050505; background: #ffd700; box-shadow: 0 0 0 1.5px var(--sbt-badge-ring, #050505); }
49
+ .sbt-badge.sbt-badge-corner .sbt-badge-spin { display: none; }
50
+
51
+ .sbt-view { display: flex; flex-direction: column; min-height: 0; height: 100%; color: var(--t-primary); font-size: var(--fs-xs, 12px); }
52
+ .sbt-view[hidden] { display: none; }
53
+ /* In place of the transcript: over it, in the same frame (DESIGN.md neutral 900). */
54
+ .sbt-overlay { position: absolute; inset: 0; z-index: 6; background: #0a0a0c; border-radius: inherit; }
55
+ /* Or in the transcript's own place in a column (the Assistant): it takes the room the transcript had. */
56
+ .sbt-inline { flex: 1 1 auto; min-height: 0; height: auto; }
57
+ .sbt-head { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--b-subtle); flex-shrink: 0; }
58
+ .sbt-title { font: 600 13px/1.3 'Space Grotesk', Inter, sans-serif; color: var(--t-bright); margin-right: auto; }
59
+ .sbt-count { color: var(--t-secondary); font-weight: 400; margin-left: 6px; }
60
+ .sbt-btn { font: 500 11px/1 Inter, sans-serif; color: var(--t-primary); background: var(--s-light); border: 1px solid var(--b-light);
61
+ border-radius: 5px; padding: 5px 8px; cursor: pointer; }
62
+ .sbt-btn:hover:not(:disabled) { background: var(--s-hover); border-color: var(--b-hover); color: var(--t-bright); }
63
+ .sbt-btn:disabled { opacity: .5; cursor: default; }
64
+ .sbt-btn[hidden] { display: none; }
65
+ .sbt-btn-danger { color: var(--sbt-err); }
66
+ .sbt-btn-danger:hover:not(:disabled) { color: var(--sbt-err); border-color: var(--sbt-err); }
67
+ .sbt-list { flex: 1; min-height: 0; overflow: auto; padding: 8px 10px 12px; display: flex; flex-direction: column; gap: 6px; }
68
+ .sbt-empty { color: var(--t-secondary); padding: 16px 4px; }
69
+ .sbt-row { border: 1px solid var(--b-subtle); border-radius: 9px; background: var(--s-subtle); }
70
+ .sbt-row.sbt-finished { opacity: .6; }
71
+ .sbt-row.sbt-finished:hover, .sbt-row.sbt-finished.sbt-open { opacity: .85; }
72
+ .sbt-row.sbt-flash { border-color: var(--accent-gold-border); }
73
+ .sbt-row-head { display: flex; align-items: center; gap: 6px; width: 100%; padding: 7px 8px; background: none; border: 0; color: inherit;
74
+ font: inherit; text-align: left; cursor: pointer; border-radius: 9px; }
75
+ .sbt-row-head:hover { background: var(--s-hover); }
76
+ .sbt-row-head:focus-visible { outline: 1px solid #93c5fd; outline-offset: -1px; }
77
+ .sbt-desc { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--t-bright); }
78
+ .sbt-chip { font: 600 9px/1 'JetBrains Mono', ui-monospace, monospace; padding: 3px 6px; border-radius: 0; background: var(--s-light);
79
+ color: var(--t-secondary); white-space: nowrap; flex-shrink: 0; text-transform: lowercase; }
80
+ .sbt-chip-running, .sbt-chip-unknown { color: var(--accent-gold); background: var(--accent-gold-bg); }
81
+ .sbt-chip-completed { color: var(--sbt-ok); }
82
+ .sbt-chip-failed { color: var(--sbt-err); }
83
+ .sbt-chip-stopped, .sbt-chip-expired, .sbt-chip-ended { color: var(--sbt-warn); }
84
+ .sbt-chip-stale { color: var(--sbt-warn); }
85
+ .sbt-time, .sbt-exit { font: 400 10px/1 'JetBrains Mono', ui-monospace, monospace; color: var(--t-secondary); white-space: nowrap; flex-shrink: 0; }
86
+ .sbt-exit-bad { color: var(--sbt-err); }
87
+ .sbt-meta { padding: 0 8px 7px 8px; color: var(--t-secondary); display: flex; flex-wrap: wrap; gap: 4px 10px; }
88
+ .sbt-meta:empty { display: none; }
89
+ .sbt-meta .sbt-last { font-family: 'JetBrains Mono', ui-monospace, monospace; color: var(--t-primary); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }
90
+ .sbt-detail { padding: 0 8px 8px; display: flex; flex-direction: column; gap: 6px; }
91
+ .sbt-detail[hidden] { display: none; }
92
+ .sbt-cmd { margin: 0; font: 400 11px/1.45 'JetBrains Mono', ui-monospace, monospace; color: var(--t-primary); background: var(--s-light);
93
+ border-radius: 5px; padding: 6px 8px; white-space: pre-wrap; word-break: break-word; max-height: 96px; overflow: auto; }
94
+ .sbt-actions { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
95
+ .sbt-confirm { color: var(--t-bright); }
96
+ .sbt-section { font: 600 10px/1 Inter, sans-serif; color: var(--t-secondary); text-transform: uppercase; letter-spacing: .04em; margin-top: 2px; }
97
+ .sbt-events { margin: 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: 2px; max-height: 160px; overflow: auto; }
98
+ .sbt-events li { font: 400 11px/1.4 'JetBrains Mono', ui-monospace, monospace; color: var(--t-primary); white-space: pre-wrap; word-break: break-word;
99
+ border-left: 2px solid var(--b-light); padding-left: 6px; }
100
+ .sbt-events time { color: var(--t-muted); margin-right: 6px; }
101
+ .sbt-term { display: flex; flex-direction: column; gap: 4px; }
102
+ .sbt-term-note { display: flex; align-items: center; gap: 8px; color: var(--t-secondary); }
103
+ .sbt-term-note[hidden] { display: none; }
104
+ .sbt-term-out { margin: 0; font: 400 11px/1.45 'JetBrains Mono', ui-monospace, monospace; color: var(--t-primary); background: rgba(0,0,0,.28);
105
+ border: 1px solid var(--b-subtle); border-radius: 5px; padding: 6px 8px; white-space: pre-wrap; word-break: break-word; max-height: 260px; overflow: auto; }
106
+ .sbt-term-out[hidden] { display: none; }
107
+ .sbt-term-foot { display: flex; align-items: center; gap: 6px; color: var(--t-secondary); min-height: 20px; }
108
+ .sbt-term-state { margin-right: auto; }
109
+
110
+ .sbt-notice { display: flex; align-items: center; gap: 6px; margin: 4px 0; padding: 5px 8px; border: 1px solid var(--b-subtle); border-radius: 9px;
111
+ background: var(--s-subtle); color: var(--t-secondary); font-size: var(--fs-xs, 12px); cursor: pointer; text-align: left; width: 100%; font-family: inherit; }
112
+ .sbt-notice:hover { border-color: var(--b-hover); color: var(--t-primary); }
113
+ .sbt-notice:focus-visible { outline: 1px solid #93c5fd; outline-offset: -1px; }
114
+ .sbt-notice-icon { color: var(--accent-gold); flex-shrink: 0; }
115
+ .sbt-notice-desc { color: var(--t-primary); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 45%; }
116
+ .sbt-notice-detail { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-family: 'JetBrains Mono', ui-monospace, monospace; font-size: 11px; }
117
+ .sbt-notice.sbt-stale { opacity: .6; }
118
+ `;
119
+
120
+ /** Puts the view's style sheet in a document once. */
121
+ export function installBgTasksStyles(doc = globalThis.document) {
122
+ if (!doc?.head || doc.getElementById(STYLE_ID)) return;
123
+ const style = doc.createElement('style');
124
+ style.id = STYLE_ID;
125
+ style.textContent = BG_TASKS_CSS;
126
+ doc.head.appendChild(style);
127
+ }
128
+
129
+ /** A translator over the app's i18n `t` that falls back to the English copy. */
130
+ export function bgWords(t) {
131
+ return (key, params = {}) => {
132
+ const full = `bgTasks.${key}`;
133
+ let text = '';
134
+ try { text = typeof t === 'function' ? t(full, params) : ''; } catch { text = ''; }
135
+ return text && text !== full ? String(text) : fillBgCopy(key, params);
136
+ };
137
+ }
138
+
139
+ function el(tag, className, text) {
140
+ const node = document.createElement(tag);
141
+ if (className) node.className = className;
142
+ if (text != null) node.textContent = text;
143
+ return node;
144
+ }
145
+
146
+ /**
147
+ * The "⟳ N" badge inside `host` (a header button, a tray pill): shown while `count` > 0. A click opens the view
148
+ * (`onClick`) and does not reach the host's own click. `corner`: a header button's form, a count on the icon's corner
149
+ * (above 9 it reads "9+"); without it the badge sits in the host's row (a tray pill).
150
+ */
151
+ export function paintBgBadge(host, count, { t = null, onClick = null, corner = false } = {}) {
152
+ if (!host) return null;
153
+ const word = bgWords(t);
154
+ let badge = host.querySelector(':scope > .sbt-badge');
155
+ const n = Math.max(0, Number(count) || 0);
156
+ if (!badge) {
157
+ if (!n) return null;
158
+ badge = el('span', 'sbt-badge');
159
+ badge.setAttribute('role', 'button');
160
+ badge.tabIndex = 0;
161
+ badge.innerHTML = '<span class="sbt-badge-spin" aria-hidden="true">⟳</span><span class="sbt-badge-n"></span>';
162
+ const open = (event) => { event.stopPropagation(); event.preventDefault(); badge._onClick?.(); };
163
+ badge.addEventListener('click', open);
164
+ badge.addEventListener('keydown', (event) => { if (event.key === 'Enter' || event.key === ' ') open(event); });
165
+ const close = host.querySelector(':scope > .term-minimized-pill-close');
166
+ if (close) host.insertBefore(badge, close); else host.appendChild(badge);
167
+ }
168
+ badge._onClick = onClick;
169
+ badge.hidden = !n;
170
+ badge.classList.toggle('sbt-badge-corner', !!corner);
171
+ badge.querySelector('.sbt-badge-n').textContent = corner && n > 9 ? '9+' : String(n);
172
+ const title = word('badgeTitle', { count: n });
173
+ badge.title = title;
174
+ badge.setAttribute('aria-label', title);
175
+ badge.dataset.count = String(n);
176
+ // The tray reads the count on the host itself (ui-tray-columns.js trayColumnOf): Running while it is above 0.
177
+ if (n) { if (host.getAttribute(BG_RUNNING_ATTR) !== String(n)) host.setAttribute(BG_RUNNING_ATTR, String(n)); }
178
+ else if (host.hasAttribute(BG_RUNNING_ATTR)) host.removeAttribute(BG_RUNNING_ATTR);
179
+ return badge;
180
+ }
181
+
182
+ const TERM_CLASSES = Object.freeze({
183
+ wrap: 'sbt-term', note: 'sbt-term-note', noteText: 'sbt-term-note-text', load: 'sbt-btn sbt-term-load',
184
+ out: 'sbt-term-out', foot: 'sbt-term-foot', state: 'sbt-term-state', jump: 'sbt-btn sbt-term-jump', copy: 'sbt-btn sbt-term-copy',
185
+ });
186
+
187
+ const timeOf = (ms) => { try { return new Date(ms).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', second: '2-digit' }); } catch { return ''; } };
188
+
189
+ /**
190
+ * createBgTasksView({ registry, readOutput, stopTask, onClear, onBack, t })
191
+ * registry() the tab's registry (bg-tasks-model.js)
192
+ * readOutput({ taskId, cursor, tail, limit, running }) → { status, data: { output, cursor, size, from } } or a refusal
193
+ * stopTask(taskId) → Promise<{ ok, error }> the runtime's own stop (the panel's socket or its route)
194
+ * onClear() the owner cleared finished tasks (the panel saves what it keeps)
195
+ * onBack() back to the conversation
196
+ * → { el, render(), open(taskId), setShown(bool), dispose() }
197
+ */
198
+ export function createBgTasksView({ registry, readOutput, stopTask, outputSupported = () => true, onClear = () => {}, onBack = () => {}, onStopped = () => {}, t = null } = {}) {
199
+ installBgTasksStyles();
200
+ const word = bgWords(t);
201
+ const root = el('section', 'sbt-scope sbt-view');
202
+ root.setAttribute('role', 'region');
203
+ root.setAttribute('aria-label', word('title'));
204
+ root.hidden = true;
205
+ const head = el('div', 'sbt-head');
206
+ const title = el('div', 'sbt-title', word('title'));
207
+ const count = el('span', 'sbt-count');
208
+ title.appendChild(count);
209
+ const clear = el('button', 'sbt-btn sbt-clear', word('clearFinished'));
210
+ clear.type = 'button';
211
+ const back = el('button', 'sbt-btn sbt-back', word('back'));
212
+ back.type = 'button';
213
+ head.append(title, clear, back);
214
+ const list = el('div', 'sbt-list');
215
+ list.setAttribute('role', 'list');
216
+ root.append(head, list);
217
+
218
+ const rows = new Map(); // taskId → { el, parts, open, confirming, output }
219
+ let shown = false;
220
+ let ticker = null;
221
+ let disposed = false;
222
+
223
+ clear.addEventListener('click', () => { onClear(); render(); });
224
+ back.addEventListener('click', () => onBack());
225
+
226
+ const reg = () => { try { return registry?.() || null; } catch { return null; } };
227
+
228
+ function outputFor(id, entry) {
229
+ if (entry.output) return entry.output;
230
+ const listeners = new Set();
231
+ const output = { view: null, follower: null, block: null, place: { follow: true, top: 0 }, listeners };
232
+ const tell = () => { for (const fn of [...listeners]) { try { fn(); } catch { /* a drawn block that is gone */ } } };
233
+ const running = () => isBgRunning(reg()?.tasks?.get(id));
234
+ output.start = () => {
235
+ if (output.follower || disposed) return;
236
+ output.follower = createShellFollower({
237
+ api: { shellOutput: ({ shellId, cursor, tail, limit }) => readOutput({ taskId: shellId, cursor, tail, limit, running: running() }) },
238
+ sessionId: 'bg', shellId: id,
239
+ isRunning: running,
240
+ isCurrent: () => shown && entry.open && !disposed,
241
+ onUpdate: (view) => { output.view = view; tell(); },
242
+ pollMinMs: BG_OUTPUT_POLL_MS, pollMaxMs: BG_OUTPUT_POLL_MAX_MS,
243
+ });
244
+ output.follower.start();
245
+ };
246
+ output.stop = () => { output.follower?.stop(); output.follower = null; };
247
+ output.block = createTerminalBlock({
248
+ shellId: id,
249
+ read: () => ({ view: output.view, shell: { status: running() ? 'running' : 'done' } }),
250
+ subscribe: (fn) => { listeners.add(fn); return () => listeners.delete(fn); },
251
+ loadAll: () => output.follower?.loadAll(),
252
+ place: output.place,
253
+ classes: TERM_CLASSES,
254
+ showEvent: 'sbt-term-show',
255
+ words: {
256
+ load: word('loadFull'), loading: word('loading'), jump: word('jump'), copy: word('copy'), copied: word('copied'),
257
+ label: word('outputLabel'), noOutputYet: word('noOutputYet'), noOutput: word('noOutput'), reading: word('loading'), running: '',
258
+ note: (view) => {
259
+ if (!view) return '';
260
+ if (view.gone) return word('outputGone');
261
+ const params = { shown: formatBytes(view.text.length), size: formatBytes(view.size) };
262
+ if (view.capped) return word('outputCapped', params);
263
+ if (view.partial) return word('outputPartial', params);
264
+ return '';
265
+ },
266
+ },
267
+ });
268
+ output.tell = tell;
269
+ entry.output = output;
270
+ return output;
271
+ }
272
+
273
+ function buildRow(id) {
274
+ const row = el('div', 'sbt-row');
275
+ row.setAttribute('role', 'listitem');
276
+ row.dataset.taskId = id;
277
+ const headBtn = el('button', 'sbt-row-head');
278
+ headBtn.type = 'button';
279
+ headBtn.setAttribute('aria-expanded', 'false');
280
+ const desc = el('span', 'sbt-desc');
281
+ const kind = el('span', 'sbt-chip sbt-kind');
282
+ const status = el('span', 'sbt-chip sbt-status');
283
+ const timeEl = el('span', 'sbt-time');
284
+ const exit = el('span', 'sbt-exit');
285
+ headBtn.append(desc, kind, status, exit, timeEl);
286
+ const meta = el('div', 'sbt-meta');
287
+ const detail = el('div', 'sbt-detail');
288
+ detail.hidden = true;
289
+ const cmd = el('pre', 'sbt-cmd');
290
+ const actions = el('div', 'sbt-actions');
291
+ const stop = el('button', 'sbt-btn sbt-btn-danger sbt-stop', word('stop'));
292
+ stop.type = 'button';
293
+ const confirmText = el('span', 'sbt-confirm', word('stopConfirm'));
294
+ const yes = el('button', 'sbt-btn sbt-btn-danger sbt-stop-yes', word('stopYes'));
295
+ yes.type = 'button';
296
+ const no = el('button', 'sbt-btn sbt-stop-no', word('stopNo'));
297
+ no.type = 'button';
298
+ actions.append(stop, confirmText, yes, no);
299
+ const termHost = el('div', 'sbt-term-host');
300
+ const eventsLabel = el('div', 'sbt-section', word('events'));
301
+ const events = el('ul', 'sbt-events');
302
+ detail.append(cmd, actions, termHost, eventsLabel, events);
303
+ row.append(headBtn, meta, detail);
304
+ const entry = { el: row, open: false, confirming: false, output: null, parts: { headBtn, desc, kind, status, timeEl, exit, meta, detail, cmd, stop, confirmText, yes, no, termHost, eventsLabel, events } };
305
+ headBtn.addEventListener('click', () => { entry.open = !entry.open; entry.confirming = false; render(); });
306
+ stop.addEventListener('click', () => { entry.confirming = true; render(); yes.focus(); });
307
+ no.addEventListener('click', () => { entry.confirming = false; render(); stop.focus(); });
308
+ yes.addEventListener('click', async () => {
309
+ entry.confirming = false;
310
+ yes.disabled = true;
311
+ render();
312
+ let out = null;
313
+ try { out = await stopTask(id); } catch (err) { out = { ok: false, error: err?.message || String(err) }; }
314
+ onStopped(id, out);
315
+ render();
316
+ });
317
+ rows.set(id, entry);
318
+ return entry;
319
+ }
320
+
321
+ function paintRow(entry, r) {
322
+ const p = entry.parts;
323
+ entry.el.classList.toggle('sbt-finished', r.finished);
324
+ entry.el.classList.toggle('sbt-open', entry.open);
325
+ p.headBtn.setAttribute('aria-expanded', String(entry.open));
326
+ p.desc.textContent = r.description;
327
+ p.desc.title = r.command || r.description;
328
+ p.kind.textContent = word(r.kindKey);
329
+ p.status.textContent = word(r.statusKey);
330
+ p.status.className = `sbt-chip sbt-status sbt-chip-${r.status}`;
331
+ p.timeEl.textContent = r.duration;
332
+ p.exit.textContent = r.exitCode != null ? word('exitCode', { code: r.exitCode }) : '';
333
+ p.exit.hidden = r.exitCode == null;
334
+ p.exit.classList.toggle('sbt-exit-bad', r.exitCode != null && r.exitCode !== 0);
335
+ // The line under the head: what happened to it, a watch's deadline and count, its last word.
336
+ const metaParts = [];
337
+ if (r.note) metaParts.push(word(r.note.key, r.note.params || {}));
338
+ if (r.expires) metaParts.push(word(r.expires.key, r.expires.params));
339
+ if (r.type === 'watch' && r.eventCount) metaParts.push(r.eventCount === 1 ? word('eventCountOne') : word('eventCount', { count: r.eventCount }));
340
+ p.meta.textContent = '';
341
+ for (const text of metaParts) p.meta.appendChild(el('span', '', text));
342
+ const last = r.type === 'watch' ? r.lastEvent : (r.finished ? r.summary : '');
343
+ if (last) p.meta.appendChild(el('span', 'sbt-last', last));
344
+ p.detail.hidden = !entry.open;
345
+ if (!entry.open) {
346
+ entry.output?.stop();
347
+ return;
348
+ }
349
+ p.cmd.textContent = r.command || r.description;
350
+ p.cmd.hidden = !r.command;
351
+ const confirming = entry.confirming && r.canStop;
352
+ p.stop.hidden = !r.canStop || confirming;
353
+ p.confirmText.hidden = !confirming;
354
+ p.yes.hidden = !confirming;
355
+ p.yes.disabled = false;
356
+ p.no.hidden = !confirming;
357
+ p.stop.parentElement.hidden = !r.canStop;
358
+ // Its output, read while the task is open and the view on screen (a server without the route says so instead).
359
+ const canRead = outputSupported();
360
+ let restartNote = p.termHost.parentElement?.querySelector(':scope > .sbt-term-restart');
361
+ if (!canRead && r.hasOutput) {
362
+ if (!restartNote) { restartNote = el('div', 'sbt-term-note sbt-term-restart', word('needsRestart')); p.termHost.before(restartNote); }
363
+ } else restartNote?.remove();
364
+ if (r.hasOutput && canRead) {
365
+ const output = outputFor(r.id, entry);
366
+ if (!output.block.isConnected) p.termHost.appendChild(output.block);
367
+ if (shown) output.start(); else output.stop();
368
+ if (output.follower && !r.running) output.follower.poke();
369
+ output.tell();
370
+ }
371
+ p.termHost.hidden = !r.hasOutput || !canRead;
372
+ const isWatch = r.type === 'watch';
373
+ p.eventsLabel.hidden = !isWatch;
374
+ p.events.hidden = !isWatch;
375
+ if (isWatch) {
376
+ p.events.textContent = '';
377
+ if (!r.events.length) p.events.appendChild(el('li', '', word('noEvents')));
378
+ for (const e of r.events) {
379
+ const li = el('li');
380
+ if (e.at) li.appendChild(el('time', '', timeOf(e.at)));
381
+ li.appendChild(document.createTextNode(e.text));
382
+ p.events.appendChild(li);
383
+ }
384
+ p.events.scrollTop = p.events.scrollHeight;
385
+ }
386
+ }
387
+
388
+ function render() {
389
+ if (disposed) return;
390
+ const r = reg();
391
+ const list_ = bgTaskRows(r);
392
+ const running = list_.filter(x => x.running).length;
393
+ count.textContent = list_.length ? `${running}/${list_.length}` : '';
394
+ clear.hidden = !list_.some(x => x.finished);
395
+ const keep = new Set(list_.map(x => x.id));
396
+ for (const [id, entry] of rows) {
397
+ if (keep.has(id)) continue;
398
+ entry.output?.stop();
399
+ entry.el.remove();
400
+ rows.delete(id);
401
+ }
402
+ let empty = list.querySelector(':scope > .sbt-empty');
403
+ if (!list_.length) {
404
+ if (!empty) { empty = el('div', 'sbt-empty', word('empty')); list.appendChild(empty); }
405
+ } else if (empty) empty.remove();
406
+ let prev = null;
407
+ for (const x of list_) {
408
+ const entry = rows.get(x.id) || buildRow(x.id);
409
+ paintRow(entry, x);
410
+ // In order, moving only what is out of place (an open block keeps its scroll).
411
+ const want = prev ? prev.nextSibling : list.firstChild;
412
+ if (want !== entry.el) list.insertBefore(entry.el, want);
413
+ prev = entry.el;
414
+ }
415
+ }
416
+
417
+ function setShown(next) {
418
+ shown = !!next;
419
+ root.hidden = !shown;
420
+ clearInterval(ticker);
421
+ ticker = null;
422
+ if (shown) {
423
+ // The running times move once a second while the view is on screen, and only then.
424
+ ticker = setInterval(() => { if (shown && !document.hidden) render(); }, 1000);
425
+ }
426
+ render();
427
+ if (shown) for (const entry of rows.values()) if (entry.open && entry.output?.block) entry.output.block.dispatchEvent(new Event('sbt-term-show'));
428
+ }
429
+
430
+ return {
431
+ el: root,
432
+ render,
433
+ setShown,
434
+ isShown: () => shown,
435
+ /** Open (and show) one task's row. */
436
+ open(taskId) {
437
+ const entry = rows.get(taskId) || (reg()?.tasks?.has(taskId) ? buildRow(taskId) : null);
438
+ if (entry) entry.open = true;
439
+ render();
440
+ if (entry?.el?.isConnected) {
441
+ entry.el.classList.add('sbt-flash');
442
+ setTimeout(() => entry.el.classList.remove('sbt-flash'), 1200);
443
+ try { entry.el.scrollIntoView({ block: 'nearest' }); } catch { /* not laid out */ }
444
+ }
445
+ },
446
+ dispose() {
447
+ disposed = true;
448
+ clearInterval(ticker);
449
+ for (const entry of rows.values()) entry.output?.stop();
450
+ rows.clear();
451
+ root.remove();
452
+ },
453
+ };
454
+ }
455
+
456
+ /**
457
+ * A task-notification of the transcript, as a card linked to its task (noticeCardView). A click (or Enter) opens
458
+ * the task in the Background tasks view (`onOpen(taskId)`).
459
+ */
460
+ export function renderBgNoticeCard(view, { t = null, onOpen = null } = {}) {
461
+ installBgTasksStyles();
462
+ const word = bgWords(t);
463
+ const card = el('button', 'sbt-scope sbt-notice');
464
+ card.type = 'button';
465
+ card.dataset.taskId = view.taskId;
466
+ card.dataset.staleText = word('noticeStale');
467
+ card.title = word('noticeOpen');
468
+ card.appendChild(el('span', 'sbt-notice-icon', '⟳'));
469
+ card.appendChild(el('span', 'sbt-chip', word(view.kindKey)));
470
+ card.appendChild(el('span', 'sbt-notice-desc', view.description));
471
+ if (view.isEvent) card.appendChild(el('span', 'sbt-chip sbt-chip-running', word('noticeEvent')));
472
+ else if (view.statusKey) {
473
+ const status = el('span', `sbt-chip sbt-chip-${view.statusKey.replace(/^status/, '').toLowerCase()}`, word(view.statusKey));
474
+ card.appendChild(status);
475
+ }
476
+ if (view.exitCode != null && !view.isEvent) card.appendChild(el('span', `sbt-exit${view.exitCode !== 0 ? ' sbt-exit-bad' : ''}`, word('exitCode', { code: view.exitCode })));
477
+ card.appendChild(el('span', 'sbt-notice-detail', view.detail || ''));
478
+ if (view.stale) {
479
+ card.classList.add('sbt-stale');
480
+ card.appendChild(el('span', 'sbt-chip sbt-chip-stale', word('noticeStale')));
481
+ }
482
+ // Without `onOpen` the transcript's one listener opens it (a card put back from a stored snapshot has none).
483
+ if (onOpen) card.addEventListener('click', (event) => { event.stopPropagation(); onOpen(view.taskId); });
484
+ return card;
485
+ }
486
+
487
+ /**
488
+ * Replayed cards (`data-event="1"`, `data-at`) of an event that came after its task had ended are marked stale,
489
+ * once the registry knows when that was.
490
+ */
491
+ export function markStaleNoticeCards(container, reg) {
492
+ if (!container?.querySelectorAll || !reg?.tasks) return 0;
493
+ let n = 0;
494
+ for (const card of container.querySelectorAll('.sbt-notice[data-event="1"][data-at]:not(.sbt-stale)')) {
495
+ const task = reg.tasks.get(card.dataset.taskId);
496
+ const at = Number(card.dataset.at) || 0;
497
+ if (!task?.endedAt || !(at > task.endedAt)) continue;
498
+ card.classList.add('sbt-stale');
499
+ const chip = document.createElement('span');
500
+ chip.className = 'sbt-chip sbt-chip-stale';
501
+ chip.textContent = card.dataset.staleText || BG_TASKS_COPY.noticeStale;
502
+ card.appendChild(chip);
503
+ n += 1;
504
+ }
505
+ return n;
506
+ }
507
+
508
+ export { BG_TASKS_COPY };