synabun 2.4.2 → 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 (220) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/README.md +1 -1
  3. package/docs/browser-extension-bridge.md +83 -34
  4. package/lib/release-npm-ci.mjs +23 -1
  5. package/mcp-server/dist/http.d.ts.map +1 -1
  6. package/mcp-server/dist/http.js +8 -1
  7. package/mcp-server/dist/http.js.map +1 -1
  8. package/mcp-server/dist/services/identity.d.ts +2 -0
  9. package/mcp-server/dist/services/identity.d.ts.map +1 -1
  10. package/mcp-server/dist/services/identity.js +4 -0
  11. package/mcp-server/dist/services/identity.js.map +1 -1
  12. package/mcp-server/dist/services/memory-client.d.ts +5 -0
  13. package/mcp-server/dist/services/memory-client.d.ts.map +1 -1
  14. package/mcp-server/dist/services/memory-client.js +21 -2
  15. package/mcp-server/dist/services/memory-client.js.map +1 -1
  16. package/mcp-server/dist/services/memory-writer.d.ts +3 -0
  17. package/mcp-server/dist/services/memory-writer.d.ts.map +1 -1
  18. package/mcp-server/dist/services/memory-writer.js +7 -1
  19. package/mcp-server/dist/services/memory-writer.js.map +1 -1
  20. package/mcp-server/dist/services/neural-interface.d.ts +11 -1
  21. package/mcp-server/dist/services/neural-interface.d.ts.map +1 -1
  22. package/mcp-server/dist/services/neural-interface.js +68 -18
  23. package/mcp-server/dist/services/neural-interface.js.map +1 -1
  24. package/mcp-server/dist/services/typesafe-config.d.ts +38 -2
  25. package/mcp-server/dist/services/typesafe-config.d.ts.map +1 -1
  26. package/mcp-server/dist/services/typesafe-config.js +260 -8
  27. package/mcp-server/dist/services/typesafe-config.js.map +1 -1
  28. package/mcp-server/dist/tools/agent-definition.d.ts +2 -2
  29. package/mcp-server/dist/tools/agents.d.ts +3 -1
  30. package/mcp-server/dist/tools/agents.d.ts.map +1 -1
  31. package/mcp-server/dist/tools/agents.js +11 -6
  32. package/mcp-server/dist/tools/agents.js.map +1 -1
  33. package/mcp-server/dist/tools/browser-extension-setup.d.ts.map +1 -1
  34. package/mcp-server/dist/tools/browser-extension-setup.js +8 -2
  35. package/mcp-server/dist/tools/browser-extension-setup.js.map +1 -1
  36. package/mcp-server/dist/tools/browser-observe.d.ts +11 -0
  37. package/mcp-server/dist/tools/browser-observe.d.ts.map +1 -1
  38. package/mcp-server/dist/tools/browser-observe.js +113 -27
  39. package/mcp-server/dist/tools/browser-observe.js.map +1 -1
  40. package/mcp-server/dist/tools/browser-readers.d.ts +6 -0
  41. package/mcp-server/dist/tools/browser-readers.d.ts.map +1 -0
  42. package/mcp-server/dist/tools/browser-readers.js +17 -0
  43. package/mcp-server/dist/tools/browser-readers.js.map +1 -0
  44. package/mcp-server/dist/tools/remember.d.ts.map +1 -1
  45. package/mcp-server/dist/tools/remember.js +6 -2
  46. package/mcp-server/dist/tools/remember.js.map +1 -1
  47. package/mcp-server/dist/tools/youtube/analytics.d.ts +4 -0
  48. package/mcp-server/dist/tools/youtube/analytics.d.ts.map +1 -1
  49. package/mcp-server/dist/tools/youtube/analytics.js +25 -20
  50. package/mcp-server/dist/tools/youtube/analytics.js.map +1 -1
  51. package/mcp-server/dist/tools/youtube/helpers.d.ts +5 -1
  52. package/mcp-server/dist/tools/youtube/helpers.d.ts.map +1 -1
  53. package/mcp-server/dist/tools/youtube/helpers.js +7 -3
  54. package/mcp-server/dist/tools/youtube/helpers.js.map +1 -1
  55. package/mcp-server/dist/tools/youtube/navigate.d.ts +2 -0
  56. package/mcp-server/dist/tools/youtube/navigate.d.ts.map +1 -1
  57. package/mcp-server/dist/tools/youtube/navigate.js +5 -3
  58. package/mcp-server/dist/tools/youtube/navigate.js.map +1 -1
  59. package/mcp-server/dist/tools/youtube/playlist.js +2 -2
  60. package/mcp-server/dist/tools/youtube/playlist.js.map +1 -1
  61. package/mcp-server/dist/tools/youtube/readers.d.ts +4 -0
  62. package/mcp-server/dist/tools/youtube/readers.d.ts.map +1 -0
  63. package/mcp-server/dist/tools/youtube/readers.js +15 -0
  64. package/mcp-server/dist/tools/youtube/readers.js.map +1 -0
  65. package/mcp-server/package-lock.json +5 -5
  66. package/mcp-server/package.json +1 -1
  67. package/mcp-server/src/http.ts +8 -1
  68. package/mcp-server/src/services/identity.ts +5 -0
  69. package/mcp-server/src/services/memory-client.ts +32 -12
  70. package/mcp-server/src/services/memory-writer.ts +5 -2
  71. package/mcp-server/src/services/neural-interface.ts +66 -19
  72. package/mcp-server/src/services/typesafe-config.ts +268 -7
  73. package/mcp-server/src/tools/agents.ts +12 -6
  74. package/mcp-server/src/tools/browser-extension-setup.ts +8 -2
  75. package/mcp-server/src/tools/browser-observe.ts +117 -28
  76. package/mcp-server/src/tools/browser-readers.ts +19 -0
  77. package/mcp-server/src/tools/remember.ts +6 -1
  78. package/mcp-server/src/tools/youtube/analytics.ts +26 -28
  79. package/mcp-server/src/tools/youtube/helpers.ts +8 -2
  80. package/mcp-server/src/tools/youtube/navigate.ts +6 -7
  81. package/mcp-server/src/tools/youtube/playlist.ts +2 -2
  82. package/mcp-server/src/tools/youtube/readers.ts +15 -0
  83. package/neural-interface/i18n/en.json +189 -1
  84. package/neural-interface/i18n/pt-BR.json +196 -8
  85. package/neural-interface/lib/assistant-api.js +123 -0
  86. package/neural-interface/lib/assistant-brains/claude.js +10 -0
  87. package/neural-interface/lib/assistant-brains/opencode.js +121 -7
  88. package/neural-interface/lib/assistant-dispatch.js +302 -44
  89. package/neural-interface/lib/assistant-envelope.js +40 -0
  90. package/neural-interface/lib/assistant-memory.js +3 -1
  91. package/neural-interface/lib/assistant-persona.js +6 -0
  92. package/neural-interface/lib/assistant-router.js +18 -8
  93. package/neural-interface/lib/assistant-runtime.js +182 -11
  94. package/neural-interface/lib/assistant-task-prompt.js +15 -2
  95. package/neural-interface/lib/browser-connection-state.js +20 -0
  96. package/neural-interface/lib/browser-extension-adapter.js +221 -17
  97. package/neural-interface/lib/browser-extension-archive.js +149 -0
  98. package/neural-interface/lib/browser-extension-bridge.js +46 -10
  99. package/neural-interface/lib/browser-extension-folder.js +635 -76
  100. package/neural-interface/lib/browser-extension-package.js +201 -0
  101. package/neural-interface/lib/browser-extension-pin.js +84 -0
  102. package/neural-interface/lib/browser-extension-pin.json +35 -0
  103. package/{browser-extension/protocol.js → neural-interface/lib/browser-extension-protocol.js} +111 -13
  104. package/neural-interface/lib/browser-extension-setup.js +108 -19
  105. package/neural-interface/lib/browser-extension-update.js +104 -0
  106. package/neural-interface/lib/browser-extraction.js +38 -14
  107. package/neural-interface/lib/claude-bg-tasks.js +183 -0
  108. package/neural-interface/lib/claude-history.js +12 -1
  109. package/neural-interface/lib/cli-install.js +23 -3
  110. package/neural-interface/lib/cli-update-planner.js +67 -4
  111. package/neural-interface/lib/confined-fs.js +25 -0
  112. package/neural-interface/lib/coordination/operations.js +2 -0
  113. package/neural-interface/lib/db.js +39 -16
  114. package/neural-interface/lib/editor-completion-opencode.js +2 -2
  115. package/neural-interface/lib/git-bash/manager.js +28 -3
  116. package/neural-interface/lib/git-status-runner.js +31 -3
  117. package/neural-interface/lib/loop-inventory.js +162 -0
  118. package/neural-interface/lib/loop-stall.js +74 -4
  119. package/neural-interface/lib/memory-session-capture.js +157 -66
  120. package/neural-interface/lib/native-loop-providers.js +82 -13
  121. package/neural-interface/lib/native-loop-runtime.js +23 -2
  122. package/neural-interface/lib/opencode-protocol.js +4 -2
  123. package/neural-interface/lib/opencode-shell-read.js +107 -0
  124. package/neural-interface/lib/opencode-stop-failures.js +40 -0
  125. package/neural-interface/lib/opencode-turn-release.js +173 -0
  126. package/neural-interface/lib/opencode-v2-client.js +4 -1
  127. package/neural-interface/lib/opencode-v2-ws-requests.js +34 -0
  128. package/neural-interface/lib/opencode2-client.js +341 -6
  129. package/neural-interface/lib/opencode2-translate.js +161 -4
  130. package/neural-interface/lib/peer-identity.js +62 -16
  131. package/neural-interface/lib/session-cache-builder.js +66 -18
  132. package/neural-interface/lib/session-capture-gate.js +42 -0
  133. package/neural-interface/lib/session-indexer.js +28 -20
  134. package/neural-interface/lib/teams/scheduler.js +3 -2
  135. package/neural-interface/lib/terminal-token-redaction.js +83 -0
  136. package/neural-interface/lib/ui-blob-api.js +14 -5
  137. package/neural-interface/lib/ui-blob-store.js +302 -39
  138. package/neural-interface/lib/whatsapp/bridge.js +52 -6
  139. package/neural-interface/package-lock.json +5 -5
  140. package/neural-interface/package.json +1 -1
  141. package/neural-interface/public/extension-setup.html +6 -2
  142. package/neural-interface/public/shared/agy/agy-panel.js +38 -10
  143. package/neural-interface/public/shared/assistant/asst-agents-dock.js +4 -0
  144. package/neural-interface/public/shared/assistant/asst-api.js +67 -0
  145. package/neural-interface/public/shared/assistant/asst-composer.js +85 -12
  146. package/neural-interface/public/shared/assistant/asst-panel.js +354 -11
  147. package/neural-interface/public/shared/assistant/asst-rack-styles.js +23 -0
  148. package/neural-interface/public/shared/assistant/asst-render.js +343 -10
  149. package/neural-interface/public/shared/assistant/asst-scenes.js +12 -0
  150. package/neural-interface/public/shared/assistant/asst-shell-logic.js +200 -0
  151. package/neural-interface/public/shared/assistant/asst-shell.js +111 -0
  152. package/neural-interface/public/shared/assistant/asst-sidepanel.js +18 -1
  153. package/neural-interface/public/shared/assistant/asst-state.js +241 -2
  154. package/neural-interface/public/shared/assistant/asst-steer.js +294 -0
  155. package/neural-interface/public/shared/assistant/asst-styles.js +21 -0
  156. package/neural-interface/public/shared/bg-tasks/bg-tasks-claude.js +147 -0
  157. package/neural-interface/public/shared/bg-tasks/bg-tasks-model.js +527 -0
  158. package/neural-interface/public/shared/bg-tasks/bg-tasks-panel.js +161 -0
  159. package/neural-interface/public/shared/bg-tasks/bg-tasks-view.js +508 -0
  160. package/neural-interface/public/shared/browser-extension-updates.js +88 -0
  161. package/neural-interface/public/shared/cdx/cdx-tabs.js +25 -0
  162. package/neural-interface/public/shared/cp/cp-bg-tasks.js +130 -0
  163. package/neural-interface/public/shared/extension-setup/es-page.js +25 -12
  164. package/neural-interface/public/shared/extension-setup/es-view.js +25 -10
  165. package/neural-interface/public/shared/extension-update-view.js +34 -0
  166. package/neural-interface/public/shared/mtc/mtc-panel.js +17 -2
  167. package/neural-interface/public/shared/ocp-v2/ocp-v2-events.js +5 -0
  168. package/neural-interface/public/shared/ocp-v2/ocp-v2-manager.js +1 -0
  169. package/neural-interface/public/shared/ocp-v2/ocp-v2-panel.js +34 -2
  170. package/neural-interface/public/shared/ocp-v2/ocp-v2-rehydrate.js +7 -1
  171. package/neural-interface/public/shared/ocp-v2/ocp-v2-render.js +26 -1
  172. package/neural-interface/public/shared/ocp-v2/ocp-v2-send.js +39 -6
  173. package/neural-interface/public/shared/ocp-v2/ocp-v2-session-actions.js +20 -1
  174. package/neural-interface/public/shared/ocp-v2/ocp-v2-shell-block.js +165 -0
  175. package/neural-interface/public/shared/ocp-v2/ocp-v2-shell-logic.js +350 -0
  176. package/neural-interface/public/shared/ocp-v2/ocp-v2-shell.js +108 -0
  177. package/neural-interface/public/shared/ocp-v2/ocp-v2-state.js +49 -3
  178. package/neural-interface/public/shared/ocp-v2/ocp-v2-styles.js +40 -0
  179. package/neural-interface/public/shared/ocp-v2/ocp-v2-tools-logic.js +20 -5
  180. package/neural-interface/public/shared/ocp-v2/ocp-v2-tools.js +7 -1
  181. package/neural-interface/public/shared/ocp-v2/ocp-v2-ws.js +7 -2
  182. package/neural-interface/public/shared/settings/settings-browser-extension.js +61 -13
  183. package/neural-interface/public/shared/settings/settings-extension-update.js +114 -0
  184. package/neural-interface/public/shared/shell-stop-view.js +83 -0
  185. package/neural-interface/public/shared/styles.css +4 -0
  186. package/neural-interface/public/shared/tour/tour-actor.js +14 -3
  187. package/neural-interface/public/shared/tray-auto-close.js +36 -6
  188. package/neural-interface/public/shared/ui-claude-panel.js +130 -1
  189. package/neural-interface/public/shared/ui-native-loop-router.js +45 -3
  190. package/neural-interface/public/shared/ui-notifications.js +23 -4
  191. package/neural-interface/public/shared/ui-scroll-follow.js +4 -0
  192. package/neural-interface/public/shared/ui-settings-entry.js +6 -0
  193. package/neural-interface/public/shared/ui-tray-columns.js +9 -2
  194. package/neural-interface/public/shared/worker-end.js +18 -0
  195. package/neural-interface/scripts/patch-node-pty.mjs +151 -0
  196. package/neural-interface/scripts/rebuild-pty.js +10 -0
  197. package/neural-interface/server.js +725 -308
  198. package/npm-shrinkwrap.json +5 -5
  199. package/package.json +1 -2
  200. package/browser-extension/content.js +0 -231
  201. package/browser-extension/engine.js +0 -658
  202. package/browser-extension/icons/icon-128.png +0 -0
  203. package/browser-extension/icons/icon-16.png +0 -0
  204. package/browser-extension/icons/icon-32.png +0 -0
  205. package/browser-extension/icons/icon-48.png +0 -0
  206. package/browser-extension/icons/status-off-16.png +0 -0
  207. package/browser-extension/icons/status-off-32.png +0 -0
  208. package/browser-extension/icons/status-off-48.png +0 -0
  209. package/browser-extension/icons/status-on-16.png +0 -0
  210. package/browser-extension/icons/status-on-32.png +0 -0
  211. package/browser-extension/icons/status-on-48.png +0 -0
  212. package/browser-extension/manifest.json +0 -58
  213. package/browser-extension/popup-strings.js +0 -250
  214. package/browser-extension/popup-view.js +0 -151
  215. package/browser-extension/popup.css +0 -166
  216. package/browser-extension/popup.html +0 -108
  217. package/browser-extension/popup.js +0 -359
  218. package/browser-extension/service-worker.js +0 -283
  219. package/browser-extension/setup-content.js +0 -36
  220. package/browser-extension/tokens.css +0 -326
@@ -0,0 +1,294 @@
1
+ // ═══════════════════════════════════════════
2
+ // SynaBun Assistant — steer: redirect work that is under way
3
+ // ═══════════════════════════════════════════
4
+ // A steer cuts the current turn and sends new words at once, instead of
5
+ // queueing them behind it. Two targets:
6
+ // - the Assistant's own turn (direct work in this panel): POST
7
+ // /api/assistant/steer without a runId; the server interrupts the brain's
8
+ // turn and sends the text as the next user message;
9
+ // - one dispatched worker run: the same route with its runId; the server
10
+ // interrupts that worker's turn (its session stays warm) and follows up.
11
+ // Server contract (lib/, the other slice): → { ok, target: { kind: 'run' |
12
+ // 'brain', runId? }, interruptedTurn }; 400 empty text, 404 unknown run or
13
+ // session, 409 { code: 'RUN_NOT_ACTIVE' }. A worker emits a run event of type
14
+ // 'steered' { runId, text, at, interruptedTurn }; the interrupted turn ends
15
+ // with reason 'steered' (never a failure, never a completion). The brain's
16
+ // stream carries a 'steered' notice { target: 'brain', text, at }.
17
+ //
18
+ // No DOM: `node --test` loads this file as it is
19
+ // (tests/assistant-steer-panel.test.mjs). The panel loads it on demand
20
+ // (asst-panel.js steerModule: in idle time after the page is shown or at the
21
+ // first steer); what the panel draws with from the first frame (STEER_COPY,
22
+ // steerTranslator, the composer's keys, the chip, the steering mark) is in
23
+ // asst-state.js, re-exported here.
24
+
25
+ import { STEER_CLEARED, STEER_COPY, STEER_MARK_MAX_MS, canSteerRun, composerKeyIntent, isRunOutputEvent, isSteeredEnd, isSteering, settleSteering, steerNoticeText, steerTokensOf, steerTranslator, targetChipText } from './asst-state.js';
26
+
27
+ export { STEER_CLEARED, STEER_COPY, STEER_MARK_MAX_MS, canSteerRun, composerKeyIntent, isRunOutputEvent, isSteeredEnd, isSteering, settleSteering, steerNoticeText, steerTokensOf, steerTranslator, targetChipText };
28
+
29
+ /** The body of POST /api/assistant/steer; null when there is nothing to send. */
30
+ export function steerRequestBody({ sessionId, text, runId = null } = {}) {
31
+ const words = String(text ?? '').trim();
32
+ if (!sessionId || !words) return null;
33
+ const body = { assistantSessionId: String(sessionId), text: words };
34
+ if (runId) body.runId = String(runId);
35
+ return body;
36
+ }
37
+
38
+ /**
39
+ * POST the steer. Never throws: → { ok: true, target, interruptedTurn } or { ok: false, status, code, error }.
40
+ * `code` is the server's (RUN_NOT_ACTIVE…), or 'UNAVAILABLE' for a server that has no such route yet (404 without a
41
+ * JSON code, or 405), or 'NETWORK'.
42
+ */
43
+ export async function postSteer(body, { fetchImpl = (typeof fetch === 'function' ? fetch : null) } = {}) {
44
+ if (!body) return { ok: false, status: 400, code: 'EMPTY', error: 'empty text' };
45
+ if (typeof fetchImpl !== 'function') return { ok: false, status: 0, code: 'NETWORK', error: 'no fetch' };
46
+ let res;
47
+ try {
48
+ res = await fetchImpl('/api/assistant/steer', {
49
+ method: 'POST',
50
+ credentials: 'same-origin',
51
+ headers: { 'Content-Type': 'application/json', 'X-SynaBun-UI': '1' },
52
+ body: JSON.stringify(body),
53
+ });
54
+ } catch (error) {
55
+ return { ok: false, status: 0, code: 'NETWORK', error: error?.message || String(error) };
56
+ }
57
+ let data = null;
58
+ try { data = await res.json(); } catch { data = null; }
59
+ if (res.ok && data && data.ok !== false) {
60
+ return { ok: true, target: data.target || { kind: body.runId ? 'run' : 'brain', runId: body.runId || undefined }, interruptedTurn: data.interruptedTurn === true };
61
+ }
62
+ const code = data?.code || data?.reason || ((res.status === 404 && !data) || res.status === 405 ? 'UNAVAILABLE' : null);
63
+ return { ok: false, status: res.status, code, error: data?.error || data?.message || `HTTP ${res.status}` };
64
+ }
65
+
66
+ /**
67
+ * What the panel does with a failed steer: { kind, message, offerBrain }.
68
+ * kind: 'ended' (409 RUN_NOT_ACTIVE: offer the Assistant), 'unavailable' (an older server: queue), 'empty', 'error'.
69
+ */
70
+ export function steerFailureView(result, t = steerTranslator()) {
71
+ const code = String(result?.code || '');
72
+ if (code === 'RUN_NOT_ACTIVE' || (result?.status === 409 && !code)) return { kind: 'ended', message: t('runEnded'), offerBrain: true, offerLabel: t('sendToAssistant') };
73
+ if (code === 'UNAVAILABLE') return { kind: 'unavailable', message: t('unavailable'), offerBrain: false };
74
+ if (code === 'EMPTY' || result?.status === 400) return { kind: 'empty', message: t('emptyText'), offerBrain: false };
75
+ return { kind: 'error', message: t('failed', { error: result?.error || code || '?' }), offerBrain: false };
76
+ }
77
+
78
+
79
+ /**
80
+ * A 'steered' notice or run event out of any packet that may carry one, normalized to
81
+ * { target: 'brain' | 'run', runId, text, at, interruptedTurn }, or null.
82
+ * Shapes: { type: 'steered', … } (a stream event, a run event), { type: 'synabun.steered', … },
83
+ * { event: { type: 'steered', … } } and { event: 'steered', … } (a dispatch packet naming its event).
84
+ */
85
+ export function steeredEventOf(packet) {
86
+ if (!packet || typeof packet !== 'object') return null;
87
+ let ev = null;
88
+ if (packet.type === 'steered' || packet.type === 'synabun.steered') ev = packet;
89
+ else if (packet.event && typeof packet.event === 'object' && (packet.event.type === 'steered' || packet.event.type === 'synabun.steered')) ev = { ...packet, ...packet.event };
90
+ else if (packet.event === 'steered' || packet.kind === 'steered' || packet.reason === 'steered') ev = packet; // the dispatcher: assistant:dispatch { reason: 'steered', run, text, at }
91
+ if (!ev) return null;
92
+ const runId = ev.runId || ev.run?.runId || packet.runId || packet.run?.runId || null;
93
+ const target = ev.target === 'brain' || (!runId && ev.target !== 'run') ? 'brain' : 'run';
94
+ const at = Number.isFinite(Date.parse(ev.at)) ? new Date(ev.at).toISOString() : (Number.isFinite(Number(ev.at)) && Number(ev.at) > 0 ? new Date(Number(ev.at)).toISOString() : null);
95
+ return { target, runId: target === 'run' ? runId : null, text: String(ev.text ?? ''), at, interruptedTurn: ev.interruptedTurn === true };
96
+ }
97
+
98
+ /** The patch that marks a run steered now (the panel's own mark until the steer's turn shows output: settleSteering). */
99
+ export function steeringPatch(run, { text = '', at = Date.now() } = {}) {
100
+ return {
101
+ steering: true,
102
+ steeredAt: new Date(at).toISOString(),
103
+ steerText: String(text || ''),
104
+ steerIteration: Number.isFinite(Number(run?.currentIteration)) ? Number(run.currentIteration) : null,
105
+ steerTokens: steerTokensOf(run),
106
+ steerTurnStarted: false,
107
+ };
108
+ }
109
+
110
+ /**
111
+ * The panel's steer logic, with every page part injected (asst-panel.js wires it; the tests pass stand-ins):
112
+ * deps: { sessionId() → id, isRunning() → bool, steerable() → bool, setSteerable(bool),
113
+ * post(body) → postSteer result, queue(payload) → 'queued', send(payload) → 'sent' | false,
114
+ * promptText(payload) → string, getRun(runId), runLabel(run) → "#2 title",
115
+ * patchRun(runId, patch), setActivity(runId, activity | null),
116
+ * notice(text, { kind: 'steer' | 'warn' | 'error', action?: { label, run() } }) → row,
117
+ * setTarget(target | null, text?), focusComposer(), t, now() }
118
+ * Returns { submit(payload), steerRun(runId, text), targetRun(runId, text), onSteered(packet), onTurnStarted(runId) }.
119
+ */
120
+ export function createSteerController(deps = {}) {
121
+ const t = deps.t || steerTranslator();
122
+ const now = typeof deps.now === 'function' ? deps.now : () => Date.now();
123
+ // Notices this panel drew itself: the server's 'steered' echo of the same steer draws nothing more.
124
+ const drawn = [];
125
+ const remember = (key) => { drawn.push({ key, at: now() }); while (drawn.length > 20) drawn.shift(); };
126
+ const seen = (key) => drawn.some((d) => d.key === key && now() - d.at < 120_000);
127
+ // The server keeps at most 500 characters of a steer's text in its event.
128
+ const keyOf = (runId, text) => JSON.stringify([runId || 'brain', String(text || '').replace(/\s+/g, ' ').trim().slice(0, 480)]);
129
+
130
+ const setTimer = typeof deps.setTimer === 'function' ? deps.setTimer : (fn, ms) => { const id = setTimeout(fn, ms); id?.unref?.(); return id; };
131
+ const clearTimer = typeof deps.clearTimer === 'function' ? deps.clearTimer : (id) => clearTimeout(id);
132
+ const markTimers = new Map(); // runId → the timer that takes a mark away STEER_MARK_MAX_MS after it
133
+
134
+ /** "Steering…" goes: the steer's turn shows output, the run ended, or the mark is too old. */
135
+ function clearMark(runId) {
136
+ const timer = markTimers.get(runId);
137
+ if (timer != null) { clearTimer(timer); markTimers.delete(runId); }
138
+ const run = deps.getRun?.(runId);
139
+ if (!run || run.steering !== true) return false;
140
+ deps.patchRun?.(runId, { ...STEER_CLEARED });
141
+ deps.setActivity?.(runId, null);
142
+ return true;
143
+ }
144
+
145
+ function markRunSteering(runId, text) {
146
+ const run = deps.getRun?.(runId) || null;
147
+ const patch = steeringPatch(run, { text, at: now() });
148
+ deps.patchRun?.(runId, patch);
149
+ // The stage and the tray say the steer (the run's activity line) until the steer's turn shows something of its own.
150
+ deps.setActivity?.(runId, { kind: 'steer', name: 'steer', step: steerNoticeText(text, t), at: now() });
151
+ // Never longer than STEER_MARK_MAX_MS: a new steer starts the wait again.
152
+ const previous = markTimers.get(runId);
153
+ if (previous != null) clearTimer(previous);
154
+ markTimers.set(runId, setTimer(() => {
155
+ markTimers.delete(runId);
156
+ if (deps.getRun?.(runId)?.steeredAt === patch.steeredAt) clearMark(runId);
157
+ }, STEER_MARK_MAX_MS + 50));
158
+ }
159
+ /** A run this panel has not marked for a steer yet (a mark seen only in a descriptor has no steeredAt). */
160
+ const freshMark = (run) => !isSteering(run) || !run.steeredAt;
161
+
162
+ /** A failed steer: what the panel says, and the way on ("Send it to the Assistant" after 409). */
163
+ function failed(result, payload, runId) {
164
+ const view = steerFailureView(result, t);
165
+ if (view.kind === 'unavailable') {
166
+ deps.setSteerable?.(false);
167
+ deps.notice?.(view.message, { kind: 'warn' });
168
+ // An older server: the Assistant's own turn queues as before; a run steer cannot be done.
169
+ if (!runId && deps.isRunning?.()) return deps.queue?.(payload) ?? 'queued';
170
+ return false;
171
+ }
172
+ if (view.kind === 'ended') {
173
+ deps.setTarget?.(null);
174
+ const retry = { ...payload, steerTarget: null };
175
+ deps.notice?.(view.message, {
176
+ kind: 'warn',
177
+ action: {
178
+ label: view.offerLabel,
179
+ run: async () => {
180
+ const outcome = await submit({ ...retry, mode: deps.isRunning?.() ? 'steer' : null });
181
+ deps.focusComposer?.();
182
+ return outcome;
183
+ },
184
+ },
185
+ });
186
+ return 'ended';
187
+ }
188
+ deps.notice?.(view.message, { kind: 'error' });
189
+ return false; // the words stay in the composer
190
+ }
191
+
192
+ async function steerBrain(payload) {
193
+ const text = deps.promptText ? deps.promptText(payload) : String(payload?.text || '');
194
+ const body = steerRequestBody({ sessionId: deps.sessionId?.(), text });
195
+ if (!body) return false;
196
+ const result = await deps.post(body);
197
+ if (!result?.ok) return failed(result, payload, null);
198
+ // The server's 'steered' packet may have come before this answer: then it drew the notice already.
199
+ if (seen(keyOf(null, body.text))) return 'steered';
200
+ remember(keyOf(null, body.text));
201
+ // The brain's reply was cut (interruptedTurn): the transcript draws a break before the notice.
202
+ deps.notice?.(steerNoticeText(payload?.display ?? payload?.text ?? body.text, t), { kind: 'steer', cut: result.interruptedTurn !== false });
203
+ return 'steered';
204
+ }
205
+
206
+ async function steerRun(runId, text, payload = null) {
207
+ const body = steerRequestBody({ sessionId: deps.sessionId?.(), text, runId });
208
+ if (!body) return false;
209
+ const result = await deps.post(body);
210
+ if (!result?.ok) return failed(result, payload || { text }, runId);
211
+ const echoed = seen(keyOf(runId, body.text));
212
+ remember(keyOf(runId, body.text));
213
+ const current = deps.getRun?.(runId);
214
+ if (!echoed || freshMark(current)) markRunSteering(runId, body.text);
215
+ if (echoed) return 'steered';
216
+ const run = deps.getRun?.(runId) || null;
217
+ deps.notice?.(steerNoticeText(body.text, t, { run: deps.runLabel?.(run) || runId }), { kind: 'steer' });
218
+ return 'steered';
219
+ }
220
+
221
+ /**
222
+ * The composer's send. 'steer' while the Assistant works → its turn is cut; a target → that run;
223
+ * 'queue' (or a server that cannot steer) → the queue; images cannot ride a steer, so they queue.
224
+ */
225
+ async function submit(payload = {}) {
226
+ const target = payload.steerTarget?.runId || null;
227
+ if (target) {
228
+ const text = deps.promptText ? deps.promptText(payload) : String(payload.text || '');
229
+ return steerRun(target, text, payload);
230
+ }
231
+ if (!deps.isRunning?.()) return deps.send?.(payload);
232
+ const wantsSteer = payload.mode === 'steer' && deps.steerable?.() !== false && !(payload.images && payload.images.length);
233
+ if (!wantsSteer) return deps.queue?.(payload);
234
+ return steerBrain(payload);
235
+ }
236
+
237
+ /** The composer's target chip for a run, with what was typed in the run's own input carried over. */
238
+ function targetRun(runId, text = '', { index = null } = {}) {
239
+ const run = deps.getRun?.(runId) || { runId };
240
+ if (!canSteerRun(run)) return false;
241
+ deps.setTarget?.({ runId, title: run.title || run.task || runId, index }, text);
242
+ deps.focusComposer?.();
243
+ return true;
244
+ }
245
+
246
+ /** A 'steered' notice or run event from the server (this window's steer, another window's, the brain's agent_send). */
247
+ function onSteered(packet) {
248
+ const ev = steeredEventOf(packet);
249
+ if (!ev) return false;
250
+ const key = keyOf(ev.target === 'run' ? ev.runId : null, ev.text);
251
+ // A packet that names the steer without its words (a replayed or slimmed copy) marks the run, never a notice of
252
+ // its own: the notice is drawn once, with the text, by the packet or the answer that carries it.
253
+ const hasText = !!ev.text.trim();
254
+ if (ev.target === 'run' && ev.runId) {
255
+ const run = deps.getRun?.(ev.runId);
256
+ if (!run) return false;
257
+ if (freshMark(run) || (hasText && !seen(key))) markRunSteering(ev.runId, ev.text);
258
+ if (hasText && !seen(key)) { remember(key); deps.notice?.(steerNoticeText(ev.text, t, { run: deps.runLabel?.(run) || ev.runId }), { kind: 'steer' }); }
259
+ return true;
260
+ }
261
+ if (hasText && !seen(key)) { remember(key); deps.notice?.(steerNoticeText(ev.text, t), { kind: 'steer', cut: ev.interruptedTurn }); }
262
+ return true;
263
+ }
264
+
265
+ /**
266
+ * The run began a turn (the steer's): "Steering…" stays until that turn shows something (onRunOutput, onRunTokens),
267
+ * so it is seen at all. True when this was the start of the steer's turn.
268
+ */
269
+ function onTurnStarted(runId) {
270
+ const run = deps.getRun?.(runId);
271
+ if (!isSteering(run) || run.steerTurnStarted === true) return false;
272
+ deps.patchRun?.(runId, { steerTurnStarted: true, steerTokens: steerTokensOf(run) });
273
+ return true;
274
+ }
275
+
276
+ /** A provider event of the run: the steer's turn's first text, call or thinking takes "Steering…" away. */
277
+ function onRunOutput(runId, payload = null, activity = null) {
278
+ const run = deps.getRun?.(runId);
279
+ if (!isSteering(run) || run.steerTurnStarted !== true) return false;
280
+ if (payload && !isRunOutputEvent(payload, activity)) return false;
281
+ return clearMark(runId);
282
+ }
283
+
284
+ /** The run's tokens as a usage packet counts them: more than at the start of the steer's turn is its output. */
285
+ function onRunTokens(runId, total) {
286
+ const run = deps.getRun?.(runId);
287
+ if (!isSteering(run) || run.steerTurnStarted !== true) return false;
288
+ const at = Number(run.steerTokens);
289
+ if (!(Number(total) > (Number.isFinite(at) ? at : 0))) return false;
290
+ return clearMark(runId);
291
+ }
292
+
293
+ return { submit, steerRun, targetRun, onSteered, onTurnStarted, onRunOutput, onRunTokens, clearMark };
294
+ }
@@ -220,6 +220,8 @@ export function injectAssistantStyles() {
220
220
  /* Icon-only actions: bare squares with their row's corners until hovered, the glyph grid-centred. */
221
221
  :is(.asst-iconbtn, .asst-tb-btn.asst-attach) { display: grid; place-items: center; width: var(--cap-h); padding: 0; background: transparent; box-shadow: none; color: var(--asst-text-3); }
222
222
  .asst-iconbtn:has(> .asst-count:not([hidden])) { display: inline-flex; width: auto; gap: 4px; padding: 0 2px 0 calc(var(--cap-px) - 2px); }
223
+ /* Background tasks: its "⟳ N" count sits on the icon's corner (bg-tasks-view.js), ringed in the bar's background. */
224
+ .asst-act-bgtasks { --sbt-badge-ring: var(--asst-card); }
223
225
  /* States */
224
226
  :is(.asst-dd, .asst-tb-btn, .asst-iconbtn, .asst-btn, .asst-cost):hover:not(:disabled) {
225
227
  background: var(--asst-fill-2);
@@ -469,6 +471,7 @@ export function injectAssistantStyles() {
469
471
  .asst-status { color: var(--asst-text-3); font-size: 12px; }
470
472
  .asst-status.info { color: var(--asst-text-2); }
471
473
  .asst-status.error { color: var(--asst-live); }
474
+ .asst-status .asst-stop-anyway { margin-left: 8px; vertical-align: baseline; }
472
475
  .asst-divider { display: flex; align-items: center; gap: 10px; color: var(--asst-text-3); font-size: 11px; }
473
476
  .asst-divider::before, .asst-divider::after { content: ''; flex: 1; height: 1px; background: var(--asst-line); }
474
477
 
@@ -2068,6 +2071,24 @@ export function injectAssistantStyles() {
2068
2071
  /* ── System notices ─────────────────────────────────────────────── */
2069
2072
  .asst-status { white-space: pre-wrap; overflow-wrap: anywhere; }
2070
2073
  .asst-status.warn { color: var(--asst-warn); }
2074
+
2075
+ /* ── Steer (asst-steer.js): Queue · Steer beside Stop, the target chip, the notice, the card's input ── */
2076
+ .asst-steer-actions { display: inline-flex; align-items: center; gap: 4px; margin-left: auto; }
2077
+ .asst-steer-actions[hidden] { display: none; }
2078
+ .asst-composer-actions .asst-steer-actions:not([hidden]) + .asst-send { margin-left: 0; }
2079
+ .asst-steer-actions .asst-queue-btn { width: auto; padding: 0 8px; color: var(--asst-text-2); font-size: 12px; }
2080
+ .asst-steer-actions .asst-steer-btn { height: var(--cap-h); padding: 0 10px; font-size: 12px; font-weight: 500; }
2081
+ .asst-composer-target { display: flex; padding: 10px 12px 0; }
2082
+ .asst-composer-target[hidden] { display: none; }
2083
+ .asst-composer-target .asst-steer-target { max-width: 100%; color: var(--asst-text); box-shadow: inset 0 0 0 1px var(--asst-line-2); }
2084
+ .asst-status.asst-steer-notice[data-steer="steer"] { padding-left: 8px; border-left: 2px solid var(--asst-line-2); color: var(--asst-text-2); }
2085
+ .asst-divider.asst-steer-break { gap: 0; margin: 4px 0; }
2086
+ .asst-status.asst-steer-notice .asst-steer-retry { margin-left: 8px; vertical-align: baseline; }
2087
+ .asst-run-steer { --cap-h: var(--asst-pill-h-sm); --cap-r: var(--asst-pill-r-sm); grid-column: 1 / -1; display: flex; align-items: center; gap: 4px; margin-top: 6px; min-width: 0; }
2088
+ .asst-run-steer-input { flex: 1; min-width: 0; height: var(--cap-h); padding: 0 8px; border: 0; border-radius: var(--cap-r); outline: none; background: transparent; box-shadow: inset 0 0 0 1px var(--asst-line-2); color: var(--asst-text); font: inherit; font-size: 12.5px; }
2089
+ .asst-run-steer-input:focus-visible { box-shadow: inset 0 0 0 1px var(--asst-text-2); }
2090
+ .asst-run-steer-input::placeholder { color: var(--asst-text-3); }
2091
+ .asst-run-steer .asst-tb-btn svg { width: 14px; height: 14px; }
2071
2092
  .asst-msg.asst-msg-error .asst-msg-content { padding: 6px 10px; border-radius: var(--asst-r-field); box-shadow: inset 2px 0 0 var(--asst-live), inset 0 0 0 1px color-mix(in srgb, var(--asst-live) 28%, transparent); }
2072
2093
  .asst-local-output { padding-left: 12px; border-left: 2px solid var(--asst-line-2); }
2073
2094
  .asst-local-output .asst-md { --sbo-text: var(--asst-text-2); --sbo-size: 13px; }
@@ -0,0 +1,147 @@
1
+ // ─────────────────────────────────────────────────────────────────────────────
2
+ // Claude Code → background-task facts (no DOM)
3
+ //
4
+ // The one adapter between Claude Code and the provider-free registry
5
+ // (bg-tasks-model.js). It reads the same things from the two places they come
6
+ // from: an Agent SDK message on its way to a panel (`tool_use_result`, a user
7
+ // message with `origin.kind: 'task-notification'`, system task events), and a
8
+ // line of the session's transcript (`toolUseResult`, `queue-operation` lines).
9
+ // The page folds live messages, the server (lib/claude-bg-tasks.js) folds the
10
+ // transcript, and both get the same registry.
11
+ //
12
+ // What the CLI says, as checked on 2.1.33x:
13
+ // Bash run_in_background → toolUseResult.backgroundTaskId, and the result text
14
+ // "Command running in background with ID: b7l…. Output is being written to: /…/tasks/b7l….output. …"
15
+ // Monitor → toolUseResult { taskId, timeoutMs, persistent }, text "Monitor started (task bku…, expires in 20s …)"
16
+ // every event, end and expiry → a queued <task-notification> (task-id, tool-use-id, output-file, status,
17
+ // summary, event), and the SDK's system/task_notification for the end
18
+ // ─────────────────────────────────────────────────────────────────────────────
19
+
20
+ import { parseTaskNotifications, kindOfTaskType } from './bg-tasks-model.js';
21
+
22
+ const obj = (v) => (v && typeof v === 'object' && !Array.isArray(v) ? v : null);
23
+ const at = (value) => { const ms = Date.parse(value); return Number.isFinite(ms) ? ms : undefined; };
24
+
25
+ function textOf(content) {
26
+ if (typeof content === 'string') return content;
27
+ if (!Array.isArray(content)) return '';
28
+ return content.map(b => (typeof b === 'string' ? b : (b?.type === 'text' ? (b.text || '') : ''))).filter(Boolean).join('\n');
29
+ }
30
+
31
+ /** The output file a result's text names ("Output is being written to: <path>."). */
32
+ export function outputFileOf(text) {
33
+ const m = /Output is being written to:\s*(\S+?\.output)\b/i.exec(String(text || ''));
34
+ return m ? m[1] : '';
35
+ }
36
+
37
+ /** The task id a result names: the typed field first, then the sentence the CLI prints. */
38
+ export function resultTaskIdOf(typed, text) {
39
+ const s = obj(typed);
40
+ if (s && typeof s.backgroundTaskId === 'string' && s.backgroundTaskId) return s.backgroundTaskId;
41
+ if (s && typeof s.taskId === 'string' && s.taskId) return s.taskId;
42
+ const src = String(text || '');
43
+ const m = /running in background with ID:?\s*([A-Za-z0-9_-]{4,})/i.exec(src) || /Monitor started \(task\s+([A-Za-z0-9_-]{4,})/i.exec(src);
44
+ return m ? m[1] : '';
45
+ }
46
+
47
+ function callFacts(content, when) {
48
+ const facts = [];
49
+ for (const b of Array.isArray(content) ? content : []) {
50
+ if (b?.type !== 'tool_use' || !b.id) continue;
51
+ const i = obj(b.input) || {};
52
+ if (b.name === 'Bash') {
53
+ facts.push({ kind: 'call', toolUseId: b.id, type: 'shell', description: i.description || '', command: i.command || '', background: i.run_in_background === true, at: when });
54
+ } else if (b.name === 'Monitor') {
55
+ facts.push({ kind: 'call', toolUseId: b.id, type: 'watch', description: i.description || '', command: i.command || i.ws?.url || '', timeoutMs: i.timeout_ms, at: when });
56
+ } else if ((b.name === 'TaskStop' || b.name === 'KillShell' || b.name === 'KillBash') && (i.task_id || i.shell_id)) {
57
+ facts.push({ kind: 'stopping', taskId: String(i.task_id || i.shell_id), by: 'agent', at: when });
58
+ }
59
+ }
60
+ return facts;
61
+ }
62
+
63
+ function resultFacts(content, typed, when) {
64
+ const facts = [];
65
+ const results = (Array.isArray(content) ? content : []).filter(b => b?.type === 'tool_result' && b.tool_use_id);
66
+ for (const b of results) {
67
+ const text = textOf(b.content);
68
+ // The typed output belongs to the message's one result.
69
+ const own = results.length === 1 ? typed : null;
70
+ const taskId = resultTaskIdOf(own, text);
71
+ if (!taskId) continue;
72
+ const t = obj(own) || {};
73
+ facts.push({
74
+ kind: 'result', toolUseId: b.tool_use_id, taskId, outputFile: outputFileOf(text),
75
+ timeoutMs: Number(t.timeoutMs) || 0, persistent: t.persistent === true, isError: b.is_error === true, at: when,
76
+ });
77
+ }
78
+ return facts;
79
+ }
80
+
81
+ function noticeFacts(text, when, uidBase) {
82
+ return parseTaskNotifications(text).map((n, i) => ({
83
+ kind: 'notice', ...n, at: when, uid: uidBase ? `${uidBase}:${i}` : '',
84
+ }));
85
+ }
86
+
87
+ /**
88
+ * Facts from one Agent SDK message as the bridge relays it (a panel's `event`, the Assistant's packet `event`).
89
+ * Subagents' calls count too: the task ids are the session's.
90
+ */
91
+ export function claudeFactsFromEvent(ev, { now = Date.now() } = {}) {
92
+ if (!ev || typeof ev !== 'object') return [];
93
+ switch (ev.type) {
94
+ case 'assistant':
95
+ return callFacts(ev.message?.content, now);
96
+ case 'user': {
97
+ const content = ev.message?.content;
98
+ const facts = resultFacts(content, ev.tool_use_result, now);
99
+ // A turn a background task started (its queued report): the notice, read once per message.
100
+ const text = textOf(content);
101
+ if (text.includes('<task-notification>')) facts.push(...noticeFacts(text, now, ev.uuid ? `u:${ev.uuid}` : ''));
102
+ return facts;
103
+ }
104
+ case 'system':
105
+ switch (ev.subtype) {
106
+ case 'task_started':
107
+ return [{ kind: 'started', taskId: ev.task_id, toolUseId: ev.tool_use_id, type: ev.task_type || '', description: ev.description || '', at: now }];
108
+ case 'task_notification':
109
+ // The end as the SDK reports it. The same end also comes as a queued notice; both fold into one end.
110
+ return [{ kind: 'notice', taskId: ev.task_id, toolUseId: ev.tool_use_id || '', status: ev.status || '', summary: ev.summary || '', outputFile: ev.output_file || '', reason: ev.reason || '', event: '', at: now, uid: ev.uuid ? `n:${ev.uuid}` : '' }];
111
+ case 'task_updated': {
112
+ const p = obj(ev.patch) || {};
113
+ return [{ kind: 'updated', taskId: ev.task_id, status: p.status || '', error: p.error || '', endedAt: p.end_time, at: now }];
114
+ }
115
+ case 'background_tasks_changed':
116
+ return [{ kind: 'live', tasks: (Array.isArray(ev.tasks) ? ev.tasks : []).map(t => ({ id: t?.task_id, type: t?.task_type, description: t?.description, ambient: t?.ambient === true })), at: now }];
117
+ default:
118
+ return [];
119
+ }
120
+ default:
121
+ return [];
122
+ }
123
+ }
124
+
125
+ /** The live set as a reattach reports it (`backgroundTasks` of reattach_result). */
126
+ export function claudeLiveFact(tasks, now = Date.now()) {
127
+ return { kind: 'live', tasks: (Array.isArray(tasks) ? tasks : []).map(t => ({ id: t?.task_id || t?.id, type: t?.task_type || t?.type, description: t?.description, ambient: t?.ambient === true })), at: now };
128
+ }
129
+
130
+ /**
131
+ * Facts from one line of a session's transcript (JSONL, parsed). Every report is read from its
132
+ * `queue-operation` enqueue line, the one line each delivery has (the attachment that folds it into a turn
133
+ * repeats it).
134
+ */
135
+ export function claudeFactsFromTranscriptLine(line, { now = Date.now() } = {}) {
136
+ if (!line || typeof line !== 'object') return [];
137
+ const when = at(line.timestamp) ?? now;
138
+ if (line.type === 'assistant') return callFacts(line.message?.content, when);
139
+ if (line.type === 'user') return resultFacts(line.message?.content, line.toolUseResult ?? line.tool_use_result, when);
140
+ if (line.type === 'queue-operation' && line.operation === 'enqueue' && typeof line.content === 'string') {
141
+ return noticeFacts(line.content, when, '');
142
+ }
143
+ return [];
144
+ }
145
+
146
+ /** Whether a task type the SDK names is one this view lists. */
147
+ export const isListedClaudeTaskType = (type) => !!kindOfTaskType(type);