@causyne/agent 1.20.124 → 1.20.128

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 (254) hide show
  1. package/bin/causyne.js +1 -1
  2. package/docs/THIRD_PARTY_NOTICES.md +36 -2
  3. package/package.json +6 -4
  4. package/src/acp/protocol.ts +19 -1
  5. package/src/acp/server.js +282 -8
  6. package/src/acp/server.ts +284 -8
  7. package/src/agent/abortableDelay.js +2 -2
  8. package/src/agent/abortableDelay.ts +2 -2
  9. package/src/agent/errorClassifier.js +9 -1
  10. package/src/agent/errorClassifier.ts +8 -1
  11. package/src/agentLoop.js +142 -53
  12. package/src/agentLoop.ts +151 -50
  13. package/src/approvalPolicy.js +34 -10
  14. package/src/approvalPolicy.ts +42 -11
  15. package/src/browser/causyneBrowser.js +61 -3
  16. package/src/browser/causyneBrowser.ts +65 -3
  17. package/src/browser/urlPolicy.js +14 -1
  18. package/src/browser/urlPolicy.ts +12 -1
  19. package/src/cliMode.js +21 -9
  20. package/src/cliMode.ts +20 -8
  21. package/src/commands.js +365 -45
  22. package/src/commands.ts +403 -45
  23. package/src/config.js +19 -4
  24. package/src/config.ts +18 -4
  25. package/src/core/contextCompactor.js +54 -7
  26. package/src/core/contextCompactor.ts +63 -7
  27. package/src/core/goalLoop.js +257 -0
  28. package/src/core/goalLoop.ts +310 -0
  29. package/src/entrypoint.js +135 -3
  30. package/src/entrypoint.ts +136 -3
  31. package/src/game/causyneCoreBridge.js +686 -0
  32. package/src/game/causyneCoreBridge.ts +702 -0
  33. package/src/game/gameLauncher.js +287 -0
  34. package/src/game/gameLauncher.ts +352 -0
  35. package/src/game/gameProvisioner.js +425 -0
  36. package/src/game/gameProvisioner.ts +488 -0
  37. package/src/headlessCli.js +19 -12
  38. package/src/headlessCli.ts +22 -8
  39. package/src/infra/atomicWriter.js +40 -4
  40. package/src/infra/atomicWriter.ts +36 -4
  41. package/src/launchIntent.js +73 -3
  42. package/src/launchIntent.ts +76 -3
  43. package/src/lsp/lspClient.js +83 -12
  44. package/src/lsp/lspClient.ts +75 -12
  45. package/src/mcp/mcpRegistry.js +75 -8
  46. package/src/mcp/mcpRegistry.ts +70 -7
  47. package/src/memory/contextBudget.js +2 -0
  48. package/src/memory/contextBudget.ts +1 -0
  49. package/src/memory/episodicStore.js +40 -0
  50. package/src/memory/episodicStore.ts +46 -0
  51. package/src/memory.js +67 -0
  52. package/src/memory.ts +65 -0
  53. package/src/modelVariants.js +45 -0
  54. package/src/modelVariants.ts +54 -0
  55. package/src/outputRecovery.js +3 -2
  56. package/src/outputRecovery.ts +4 -2
  57. package/src/permissionPresets.js +327 -0
  58. package/src/permissionPresets.ts +361 -0
  59. package/src/personalization.js +365 -0
  60. package/src/personalization.ts +471 -0
  61. package/src/projectInstructions.js +30 -10
  62. package/src/projectInstructions.ts +31 -12
  63. package/src/providers/acp.js +3 -2
  64. package/src/providers/acp.ts +3 -2
  65. package/src/providers/antigravity.js +64 -9
  66. package/src/providers/antigravity.ts +75 -9
  67. package/src/providers/byokHttp.js +7 -1
  68. package/src/providers/byokHttp.ts +6 -1
  69. package/src/providers/byokStream.js +21 -8
  70. package/src/providers/byokStream.ts +17 -7
  71. package/src/providers/dashscope-byok.js +21 -5
  72. package/src/providers/dashscope-byok.ts +29 -11
  73. package/src/providers/moonshot.js +26 -7
  74. package/src/providers/moonshot.ts +34 -13
  75. package/src/providers/muse.js +6 -0
  76. package/src/providers/muse.ts +8 -0
  77. package/src/providers/ollama.js +39 -7
  78. package/src/providers/ollama.ts +36 -7
  79. package/src/providers/openai-compatible.js +16 -3
  80. package/src/providers/openai-compatible.ts +13 -3
  81. package/src/providers/openai.js +62 -4
  82. package/src/providers/openai.ts +50 -4
  83. package/src/providers/openrouter.js +19 -4
  84. package/src/providers/openrouter.ts +21 -4
  85. package/src/providers/qwen-byok.js +21 -5
  86. package/src/providers/qwen-byok.ts +29 -11
  87. package/src/providers/xai.js +26 -10
  88. package/src/providers/xai.ts +34 -16
  89. package/src/reporting.js +4 -2
  90. package/src/reporting.ts +4 -2
  91. package/src/sandbox.js +34 -0
  92. package/src/sandbox.ts +43 -0
  93. package/src/scheduler/scheduler.js +372 -0
  94. package/src/scheduler/scheduler.ts +433 -0
  95. package/src/sessionSearchIndex.js +267 -0
  96. package/src/sessionSearchIndex.ts +311 -0
  97. package/src/skillsManager.js +2 -3
  98. package/src/skillsManager.ts +2 -3
  99. package/src/toolPacks.js +6 -0
  100. package/src/toolPacks.ts +6 -0
  101. package/src/tools/applyPatch.js +1 -1
  102. package/src/tools/applyPatch.ts +1 -1
  103. package/src/tools/generateMediaViaApi.js +4 -3
  104. package/src/tools/generateMediaViaApi.ts +4 -3
  105. package/src/tools/memoryWrite.js +15 -1
  106. package/src/tools/memoryWrite.ts +16 -1
  107. package/src/tools/nativeEngine.js +2 -2
  108. package/src/tools/nativeEngine.ts +2 -2
  109. package/src/tools/openSourceToolProvisioner.js +2 -2
  110. package/src/tools/openSourceToolProvisioner.ts +2 -2
  111. package/src/tools/osSandbox.js +1 -1
  112. package/src/tools/osSandbox.ts +1 -1
  113. package/src/tools/payloadSanitizer.js +0 -1
  114. package/src/tools/payloadSanitizer.ts +0 -1
  115. package/src/tools/pdfReader.js +122 -20
  116. package/src/tools/pdfReader.ts +111 -20
  117. package/src/tools/presentDeliverable.js +103 -0
  118. package/src/tools/presentDeliverable.ts +127 -0
  119. package/src/tools/ptcRuntime.js +444 -0
  120. package/src/tools/ptcRuntime.ts +490 -0
  121. package/src/tools/ptyTerminal.js +1108 -0
  122. package/src/tools/ptyTerminal.ts +1245 -0
  123. package/src/tools/registry.js +21 -12
  124. package/src/tools/registry.ts +21 -12
  125. package/src/tools/toolOutputStore.js +26 -4
  126. package/src/tools/toolOutputStore.ts +22 -4
  127. package/src/tools/webFetch.js +158 -11
  128. package/src/tools/webFetch.ts +145 -11
  129. package/src/tools/webSearch.js +123 -36
  130. package/src/tools/webSearch.ts +124 -33
  131. package/src/tui/banner.js +5 -1
  132. package/src/tui/banner.ts +6 -1
  133. package/src/tui/chatRenderer.js +1 -2
  134. package/src/tui/chatRenderer.ts +1 -2
  135. package/src/tui/completion.js +1 -0
  136. package/src/tui/completion.ts +1 -0
  137. package/src/tui/composerControls.js +111 -27
  138. package/src/tui/composerControls.ts +124 -28
  139. package/src/tui/dialogs.js +3 -16
  140. package/src/tui/dialogs.ts +3 -16
  141. package/src/tui/home.js +1 -0
  142. package/src/tui/home.ts +1 -0
  143. package/src/tui/invocation.js +1 -2
  144. package/src/tui/invocation.ts +1 -2
  145. package/src/tui/modelPicker.js +22 -12
  146. package/src/tui/modelPicker.ts +22 -12
  147. package/src/tui/overlay.js +2 -5
  148. package/src/tui/overlay.ts +2 -5
  149. package/src/tui/prompt.js +6 -4
  150. package/src/tui/prompt.ts +6 -4
  151. package/src/tui/selectList.js +4 -12
  152. package/src/tui/selectList.ts +4 -12
  153. package/src/tui/setup.js +22 -9
  154. package/src/tui/setup.ts +26 -9
  155. package/src/tui/statusLine.ts +2 -0
  156. package/src/web/client/browserPanel.js +905 -0
  157. package/src/web/client/browserPanel.ts +872 -0
  158. package/src/web/client/chatStream.js +321 -0
  159. package/src/web/client/chatStream.ts +297 -0
  160. package/src/web/client/cliPanel.js +165 -0
  161. package/src/web/client/cliPanel.ts +171 -0
  162. package/src/web/client/composer.js +204 -0
  163. package/src/web/client/composer.ts +209 -0
  164. package/src/web/client/diffPanel.js +176 -0
  165. package/src/web/client/diffPanel.ts +203 -0
  166. package/src/web/client/dom.js +271 -0
  167. package/src/web/client/dom.ts +272 -0
  168. package/src/web/client/filesAndCanvas.js +233 -0
  169. package/src/web/client/filesAndCanvas.ts +240 -0
  170. package/src/web/client/gameLauncherUi.js +78 -0
  171. package/src/web/client/gameLauncherUi.ts +78 -0
  172. package/src/web/client/main.js +316 -0
  173. package/src/web/client/main.ts +391 -0
  174. package/src/web/client/markdown.js +364 -0
  175. package/src/web/client/markdown.ts +373 -0
  176. package/src/web/client/modelPicker.js +455 -0
  177. package/src/web/client/modelPicker.ts +451 -0
  178. package/src/web/client/permissionPresetsUi.js +307 -0
  179. package/src/web/client/permissionPresetsUi.ts +333 -0
  180. package/src/web/client/sessions.js +353 -0
  181. package/src/web/client/sessions.ts +372 -0
  182. package/src/web/client/settingsModal.js +824 -0
  183. package/src/web/client/settingsModal.ts +959 -0
  184. package/src/web/client/state.js +280 -0
  185. package/src/web/client/state.ts +259 -0
  186. package/src/web/client/streamSmoother.js +127 -0
  187. package/src/web/client/streamSmoother.ts +119 -0
  188. package/src/web/client/studioLayout.js +89 -0
  189. package/src/web/client/studioLayout.ts +103 -0
  190. package/src/web/client/studioShell.js +660 -0
  191. package/src/web/client/studioShell.ts +672 -0
  192. package/src/web/client/studioStart.js +77 -0
  193. package/src/web/client/studioStart.ts +89 -0
  194. package/src/web/client/terminalPanel.js +606 -0
  195. package/src/web/client/terminalPanel.ts +632 -0
  196. package/src/web/client/terminalTheme.js +249 -0
  197. package/src/web/client/terminalTheme.ts +258 -0
  198. package/src/web/client/turnView.js +934 -0
  199. package/src/web/client/turnView.ts +981 -0
  200. package/src/web/client/types.js +1 -0
  201. package/src/web/client/types.ts +322 -0
  202. package/src/web/client/validators.js +147 -0
  203. package/src/web/client/validators.ts +140 -0
  204. package/src/web/server/activeRuns.js +152 -0
  205. package/src/web/server/activeRuns.ts +171 -0
  206. package/src/web/server/routesBrowser.js +409 -0
  207. package/src/web/server/routesBrowser.ts +435 -0
  208. package/src/web/server/routesChat.js +304 -0
  209. package/src/web/server/routesChat.ts +347 -0
  210. package/src/web/server/routesCore.js +472 -0
  211. package/src/web/server/routesCore.ts +522 -0
  212. package/src/web/server/routesSettings.js +344 -0
  213. package/src/web/server/routesSettings.ts +397 -0
  214. package/src/web/server/routesTerminal.js +162 -0
  215. package/src/web/server/routesTerminal.ts +182 -0
  216. package/src/web/server/routesWorkspace.js +510 -0
  217. package/src/web/server/routesWorkspace.ts +546 -0
  218. package/src/web/server/security.js +98 -0
  219. package/src/web/server/security.ts +108 -0
  220. package/src/web/server/sessionTranscript.js +586 -0
  221. package/src/web/server/sessionTranscript.ts +566 -0
  222. package/src/web/server/staticAssets.js +104 -0
  223. package/src/web/server/staticAssets.ts +116 -0
  224. package/src/web/server/types.js +1 -0
  225. package/src/web/server/types.ts +135 -0
  226. package/src/web/server/workspaceFiles.js +368 -0
  227. package/src/web/server/workspaceFiles.ts +363 -0
  228. package/src/web/skybridgeServer.js +78 -2448
  229. package/src/web/skybridgeServer.ts +120 -2600
  230. package/src/web/skybridgeUi.js +20 -450
  231. package/src/web/skybridgeUi.ts +24 -455
  232. package/src/web/styles/agent.css +380 -0
  233. package/src/web/styles/composer.css +193 -0
  234. package/src/web/styles/overlays.css +935 -0
  235. package/src/web/styles/shell.css +387 -0
  236. package/src/web/styles/studio.css +951 -0
  237. package/src/web/styles/thread.css +221 -0
  238. package/src/web/styles/tokens.css +94 -0
  239. package/src/web/ui/brandMark.js +73 -0
  240. package/src/web/ui/brandMark.ts +82 -0
  241. package/src/web/ui/modalsTemplate.js +388 -0
  242. package/src/web/ui/modalsTemplate.ts +388 -0
  243. package/src/web/ui/sidebarTemplate.js +63 -0
  244. package/src/web/ui/sidebarTemplate.ts +64 -0
  245. package/src/web/ui/studioTemplate.js +256 -0
  246. package/src/web/ui/studioTemplate.ts +256 -0
  247. package/src/web/ui/workspaceTemplate.js +72 -0
  248. package/src/web/ui/workspaceTemplate.ts +73 -0
  249. package/src/worktreeManager.js +16 -1
  250. package/src/worktreeManager.ts +22 -1
  251. package/src/web/skybridgeClientScript.js +0 -3951
  252. package/src/web/skybridgeClientScript.ts +0 -3952
  253. package/src/web/skybridgeStyles.js +0 -827
  254. package/src/web/skybridgeStyles.ts +0 -828
@@ -0,0 +1,165 @@
1
+ import { copyText, dom } from './dom.js';
2
+ import { apiUrl, authHeaders, scrollToBottom, setStageMode, state } from './state.js';
3
+ import { openStudio } from './studioShell.js';
4
+ import { openInCanvas } from './filesAndCanvas.js';
5
+ const QUICK_CLI_COMMANDS = [
6
+ '/status', '/doctor', '/map', '/personalization', '/rules', '/memory status',
7
+ '/checkpoints', '/undo', '/worktree list', '/pr', '/tasks', '/mcp',
8
+ '/skills', '/peers', '/plugins', '/slop', '/policy', '/audit', '/eval'
9
+ ];
10
+ let callbacks = {
11
+ startFreshChat: async () => { },
12
+ appendUserTurn: () => { },
13
+ loadHealth: async () => { }
14
+ };
15
+ export function configureCliPanelCallbacks(cbs) {
16
+ callbacks = cbs;
17
+ }
18
+ export function renderCliQuickGrid() {
19
+ dom.cliQuickGrid.textContent = '';
20
+ for (const cmd of QUICK_CLI_COMMANDS) {
21
+ const btn = document.createElement('button');
22
+ btn.type = 'button';
23
+ btn.className = 'cli-chip';
24
+ btn.textContent = cmd;
25
+ btn.addEventListener('click', () => { void runCliCommand(cmd, 'studio'); });
26
+ dom.cliQuickGrid.appendChild(btn);
27
+ }
28
+ }
29
+ export async function loadCliCommands() {
30
+ const res = await fetch(apiUrl('/api/commands'), { headers: authHeaders() }).catch(() => null);
31
+ if (!res || !res.ok)
32
+ return;
33
+ const data = await res.json();
34
+ state.cliCommands = data.commands || [];
35
+ }
36
+ export async function loadCliOverview() {
37
+ const res = await fetch(apiUrl('/api/cli/overview'), { headers: authHeaders() }).catch(() => null);
38
+ if (!res || !res.ok)
39
+ return;
40
+ const d = await res.json();
41
+ const parts = [
42
+ `${d.commandsCount || 0} commands`,
43
+ `${d.skillsCount || 0} skills`
44
+ ];
45
+ const cps = (d.checkpoints || []).length;
46
+ if (cps > 0)
47
+ parts.push(`${cps} checkpoints`);
48
+ const mcps = (d.mcpServers || []).length;
49
+ if (mcps > 0)
50
+ parts.push(`${mcps} MCP`);
51
+ const tasks = (d.tasks || []).length;
52
+ if (tasks > 0)
53
+ parts.push(`${tasks} running`);
54
+ dom.cliStatsEl.textContent = parts.join(' · ');
55
+ }
56
+ function appendCliTurnCard(cmdLine, title, output, ok) {
57
+ setStageMode(true);
58
+ const turn = document.createElement('div');
59
+ turn.className = 'turn assistant';
60
+ const card = document.createElement('div');
61
+ card.className = 'cli-turn-card';
62
+ const head = document.createElement('div');
63
+ head.className = 'cli-turn-head';
64
+ const left = document.createElement('span');
65
+ left.textContent = (title || cmdLine) + (ok ? '' : ' (failed)');
66
+ if (!ok)
67
+ left.style.color = 'var(--err)';
68
+ const right = document.createElement('div');
69
+ right.style.display = 'inline-flex';
70
+ right.style.gap = '6px';
71
+ const copyBtn = document.createElement('button');
72
+ copyBtn.type = 'button';
73
+ copyBtn.className = 'code-btn';
74
+ copyBtn.textContent = 'Copy';
75
+ copyBtn.addEventListener('click', () => copyText(output || '', copyBtn));
76
+ const canvasBtn = document.createElement('button');
77
+ canvasBtn.type = 'button';
78
+ canvasBtn.className = 'code-btn';
79
+ canvasBtn.textContent = 'Canvas';
80
+ canvasBtn.addEventListener('click', () => openInCanvas('cli-output.md', output || '', false));
81
+ right.appendChild(copyBtn);
82
+ right.appendChild(canvasBtn);
83
+ head.appendChild(left);
84
+ head.appendChild(right);
85
+ const pre = document.createElement('pre');
86
+ pre.className = 'cli-turn-body';
87
+ pre.textContent = output || '(no output)';
88
+ card.appendChild(head);
89
+ card.appendChild(pre);
90
+ turn.appendChild(card);
91
+ dom.threadEl.appendChild(turn);
92
+ scrollToBottom(true);
93
+ }
94
+ export async function runCliCommand(rawInput, target) {
95
+ const trimmed = String(rawInput || '').trim();
96
+ if (!trimmed)
97
+ return;
98
+ if (trimmed === '/new' || trimmed === '/clear') {
99
+ await callbacks.startFreshChat();
100
+ return;
101
+ }
102
+ if (trimmed === '/browser') {
103
+ openStudio('browser');
104
+ return;
105
+ }
106
+ if (trimmed === '/diff') {
107
+ openStudio('diff');
108
+ }
109
+ if (target === 'studio') {
110
+ dom.cliStatusEl.textContent = `Running ${trimmed}...`;
111
+ dom.cliConsoleEl.textContent = `Executing ${trimmed}...`;
112
+ }
113
+ else {
114
+ callbacks.appendUserTurn(trimmed, []);
115
+ }
116
+ const res = await fetch(apiUrl('/api/command'), {
117
+ method: 'POST',
118
+ headers: authHeaders({ 'Content-Type': 'application/json' }),
119
+ body: JSON.stringify({ input: trimmed })
120
+ }).catch(() => null);
121
+ if (!res) {
122
+ if (target === 'studio')
123
+ dom.cliConsoleEl.textContent = 'Command request failed.';
124
+ else
125
+ appendCliTurnCard(trimmed, trimmed, 'Command request failed.', false);
126
+ return;
127
+ }
128
+ const data = await res.json().catch(() => ({ ok: false, output: `HTTP ${res.status}` }));
129
+ const outText = data.output || data.error || '(done)';
130
+ if (target === 'studio') {
131
+ dom.cliConsoleEl.textContent = outText;
132
+ dom.cliStatusEl.textContent = (data.ok ? 'OK · ' : 'Failed · ') + trimmed;
133
+ void loadCliOverview();
134
+ }
135
+ else {
136
+ appendCliTurnCard(trimmed, data.title || trimmed, outText, Boolean(data.ok));
137
+ }
138
+ void callbacks.loadHealth();
139
+ }
140
+ export async function runZeroSpendProbe() {
141
+ dom.cliStatusEl.textContent = 'Testing connections...';
142
+ dom.cliConsoleEl.textContent = 'Checking local Ollama and configured providers...';
143
+ const res = await fetch(apiUrl('/api/doctor/probe'), { method: 'POST', headers: authHeaders() }).catch(() => null);
144
+ if (!res || !res.ok) {
145
+ dom.cliConsoleEl.textContent = 'Connection test failed.';
146
+ return;
147
+ }
148
+ const data = await res.json();
149
+ dom.cliConsoleEl.textContent = data.report || 'Completed.';
150
+ dom.cliStatusEl.textContent = `${data.summary?.passedCount || 0} connected`;
151
+ }
152
+ export function bindCliPanelEvents() {
153
+ dom.cliRunBtn.addEventListener('click', () => {
154
+ void runCliCommand(dom.cliCmdInput.value, 'studio');
155
+ dom.cliCmdInput.value = '';
156
+ });
157
+ dom.cliCmdInput.addEventListener('keydown', (e) => {
158
+ if (e.key === 'Enter') {
159
+ e.preventDefault();
160
+ void runCliCommand(dom.cliCmdInput.value, 'studio');
161
+ dom.cliCmdInput.value = '';
162
+ }
163
+ });
164
+ dom.cliProbeBtn.addEventListener('click', () => { void runZeroSpendProbe(); });
165
+ }
@@ -0,0 +1,171 @@
1
+ import { copyText, dom } from './dom.js';
2
+ import { apiUrl, authHeaders, scrollToBottom, setStageMode, state } from './state.js';
3
+ import { openStudio } from './studioShell.js';
4
+ import { openInCanvas } from './filesAndCanvas.js';
5
+ import type { CliCommandEntry } from './types.js';
6
+
7
+ const QUICK_CLI_COMMANDS: readonly string[] = [
8
+ '/status', '/doctor', '/map', '/personalization', '/rules', '/memory status',
9
+ '/checkpoints', '/undo', '/worktree list', '/pr', '/tasks', '/mcp',
10
+ '/skills', '/peers', '/plugins', '/slop', '/policy', '/audit', '/eval'
11
+ ];
12
+
13
+ export interface CliPanelCallbacks {
14
+ startFreshChat(): Promise<void>;
15
+ appendUserTurn(prompt: string, attachments: string[], animate?: boolean): void;
16
+ loadHealth(): Promise<void>;
17
+ }
18
+
19
+ let callbacks: CliPanelCallbacks = {
20
+ startFreshChat: async () => {},
21
+ appendUserTurn: () => {},
22
+ loadHealth: async () => {}
23
+ };
24
+
25
+ export function configureCliPanelCallbacks(cbs: CliPanelCallbacks): void {
26
+ callbacks = cbs;
27
+ }
28
+
29
+ export function renderCliQuickGrid(): void {
30
+ dom.cliQuickGrid.textContent = '';
31
+ for (const cmd of QUICK_CLI_COMMANDS) {
32
+ const btn = document.createElement('button');
33
+ btn.type = 'button';
34
+ btn.className = 'cli-chip';
35
+ btn.textContent = cmd;
36
+ btn.addEventListener('click', () => { void runCliCommand(cmd, 'studio'); });
37
+ dom.cliQuickGrid.appendChild(btn);
38
+ }
39
+ }
40
+
41
+ export async function loadCliCommands(): Promise<void> {
42
+ const res = await fetch(apiUrl('/api/commands'), { headers: authHeaders() }).catch(() => null);
43
+ if (!res || !res.ok) return;
44
+ const data = await res.json() as { commands?: CliCommandEntry[] };
45
+ state.cliCommands = data.commands || [];
46
+ }
47
+
48
+ export async function loadCliOverview(): Promise<void> {
49
+ const res = await fetch(apiUrl('/api/cli/overview'), { headers: authHeaders() }).catch(() => null);
50
+ if (!res || !res.ok) return;
51
+ const d = await res.json() as {
52
+ commandsCount?: number;
53
+ skillsCount?: number;
54
+ checkpoints?: unknown[];
55
+ mcpServers?: unknown[];
56
+ tasks?: unknown[];
57
+ };
58
+ const parts = [
59
+ `${d.commandsCount || 0} commands`,
60
+ `${d.skillsCount || 0} skills`
61
+ ];
62
+ const cps = (d.checkpoints || []).length;
63
+ if (cps > 0) parts.push(`${cps} checkpoints`);
64
+ const mcps = (d.mcpServers || []).length;
65
+ if (mcps > 0) parts.push(`${mcps} MCP`);
66
+ const tasks = (d.tasks || []).length;
67
+ if (tasks > 0) parts.push(`${tasks} running`);
68
+ dom.cliStatsEl.textContent = parts.join(' · ');
69
+ }
70
+
71
+ function appendCliTurnCard(cmdLine: string, title: string, output: string, ok: boolean): void {
72
+ setStageMode(true);
73
+ const turn = document.createElement('div');
74
+ turn.className = 'turn assistant';
75
+ const card = document.createElement('div');
76
+ card.className = 'cli-turn-card';
77
+ const head = document.createElement('div');
78
+ head.className = 'cli-turn-head';
79
+ const left = document.createElement('span');
80
+ left.textContent = (title || cmdLine) + (ok ? '' : ' (failed)');
81
+ if (!ok) left.style.color = 'var(--err)';
82
+ const right = document.createElement('div');
83
+ right.style.display = 'inline-flex';
84
+ right.style.gap = '6px';
85
+ const copyBtn = document.createElement('button');
86
+ copyBtn.type = 'button';
87
+ copyBtn.className = 'code-btn';
88
+ copyBtn.textContent = 'Copy';
89
+ copyBtn.addEventListener('click', () => copyText(output || '', copyBtn));
90
+ const canvasBtn = document.createElement('button');
91
+ canvasBtn.type = 'button';
92
+ canvasBtn.className = 'code-btn';
93
+ canvasBtn.textContent = 'Canvas';
94
+ canvasBtn.addEventListener('click', () => openInCanvas('cli-output.md', output || '', false));
95
+ right.appendChild(copyBtn);
96
+ right.appendChild(canvasBtn);
97
+ head.appendChild(left);
98
+ head.appendChild(right);
99
+ const pre = document.createElement('pre');
100
+ pre.className = 'cli-turn-body';
101
+ pre.textContent = output || '(no output)';
102
+ card.appendChild(head);
103
+ card.appendChild(pre);
104
+ turn.appendChild(card);
105
+ dom.threadEl.appendChild(turn);
106
+ scrollToBottom(true);
107
+ }
108
+
109
+ export async function runCliCommand(rawInput: string, target: 'studio' | 'chat'): Promise<void> {
110
+ const trimmed = String(rawInput || '').trim();
111
+ if (!trimmed) return;
112
+ if (trimmed === '/new' || trimmed === '/clear') { await callbacks.startFreshChat(); return; }
113
+ if (trimmed === '/browser') { openStudio('browser'); return; }
114
+ if (trimmed === '/diff') { openStudio('diff'); }
115
+ if (target === 'studio') {
116
+ dom.cliStatusEl.textContent = `Running ${trimmed}...`;
117
+ dom.cliConsoleEl.textContent = `Executing ${trimmed}...`;
118
+ } else {
119
+ callbacks.appendUserTurn(trimmed, []);
120
+ }
121
+ const res = await fetch(apiUrl('/api/command'), {
122
+ method: 'POST',
123
+ headers: authHeaders({ 'Content-Type': 'application/json' }),
124
+ body: JSON.stringify({ input: trimmed })
125
+ }).catch(() => null);
126
+ if (!res) {
127
+ if (target === 'studio') dom.cliConsoleEl.textContent = 'Command request failed.';
128
+ else appendCliTurnCard(trimmed, trimmed, 'Command request failed.', false);
129
+ return;
130
+ }
131
+ const data = await res.json().catch(() => ({ ok: false, output: `HTTP ${res.status}` })) as {
132
+ ok?: boolean;
133
+ title?: string;
134
+ output?: string;
135
+ error?: string;
136
+ };
137
+ const outText = data.output || data.error || '(done)';
138
+ if (target === 'studio') {
139
+ dom.cliConsoleEl.textContent = outText;
140
+ dom.cliStatusEl.textContent = (data.ok ? 'OK · ' : 'Failed · ') + trimmed;
141
+ void loadCliOverview();
142
+ } else {
143
+ appendCliTurnCard(trimmed, data.title || trimmed, outText, Boolean(data.ok));
144
+ }
145
+ void callbacks.loadHealth();
146
+ }
147
+
148
+ export async function runZeroSpendProbe(): Promise<void> {
149
+ dom.cliStatusEl.textContent = 'Testing connections...';
150
+ dom.cliConsoleEl.textContent = 'Checking local Ollama and configured providers...';
151
+ const res = await fetch(apiUrl('/api/doctor/probe'), { method: 'POST', headers: authHeaders() }).catch(() => null);
152
+ if (!res || !res.ok) { dom.cliConsoleEl.textContent = 'Connection test failed.'; return; }
153
+ const data = await res.json() as { report?: string; summary?: { passedCount?: number } };
154
+ dom.cliConsoleEl.textContent = data.report || 'Completed.';
155
+ dom.cliStatusEl.textContent = `${data.summary?.passedCount || 0} connected`;
156
+ }
157
+
158
+ export function bindCliPanelEvents(): void {
159
+ dom.cliRunBtn.addEventListener('click', () => {
160
+ void runCliCommand(dom.cliCmdInput.value, 'studio');
161
+ dom.cliCmdInput.value = '';
162
+ });
163
+ dom.cliCmdInput.addEventListener('keydown', (e) => {
164
+ if (e.key === 'Enter') {
165
+ e.preventDefault();
166
+ void runCliCommand(dom.cliCmdInput.value, 'studio');
167
+ dom.cliCmdInput.value = '';
168
+ }
169
+ });
170
+ dom.cliProbeBtn.addEventListener('click', () => { void runZeroSpendProbe(); });
171
+ }
@@ -0,0 +1,204 @@
1
+ import { dom } from './dom.js';
2
+ import { apiUrl, authHeaders, state, updateComposerMetrics } from './state.js';
3
+ let callbacks = {
4
+ submitPrompt: async () => { },
5
+ stopActiveRun: async () => { },
6
+ runCliCommand: async () => { }
7
+ };
8
+ export function configureComposerCallbacks(cbs) {
9
+ callbacks = cbs;
10
+ }
11
+ export function renderAttachmentChips() {
12
+ dom.composerChipsEl.textContent = '';
13
+ dom.composerChipsEl.classList.toggle('has-items', state.attachments.length > 0);
14
+ for (const rel of state.attachments) {
15
+ const chip = document.createElement('span');
16
+ chip.className = 'chip';
17
+ const label = document.createElement('span');
18
+ label.textContent = `@${rel}`;
19
+ const rm = document.createElement('button');
20
+ rm.type = 'button';
21
+ rm.textContent = 'x';
22
+ rm.setAttribute('aria-label', `Remove ${rel}`);
23
+ rm.addEventListener('click', () => {
24
+ state.attachments = state.attachments.filter((item) => item !== rel);
25
+ renderAttachmentChips();
26
+ });
27
+ chip.appendChild(label);
28
+ chip.appendChild(rm);
29
+ dom.composerChipsEl.appendChild(chip);
30
+ }
31
+ }
32
+ export function attachWorkspaceFile(relPath) {
33
+ if (!relPath || state.attachments.includes(relPath) || state.attachments.length >= 6)
34
+ return;
35
+ state.attachments.push(relPath);
36
+ renderAttachmentChips();
37
+ dom.promptInput.focus();
38
+ }
39
+ function applySlashChoice(cmdName) {
40
+ dom.promptInput.value = `/${cmdName} `;
41
+ state.mentionItems = [];
42
+ state.mentionStart = -1;
43
+ dom.mentionPopover.classList.remove('open');
44
+ updateComposerMetrics();
45
+ dom.promptInput.focus();
46
+ }
47
+ function applyMentionChoice(relPath) {
48
+ const pos = dom.promptInput.selectionStart || dom.promptInput.value.length;
49
+ if (state.mentionStart >= 0 && pos >= state.mentionStart) {
50
+ dom.promptInput.value = dom.promptInput.value.slice(0, state.mentionStart) + dom.promptInput.value.slice(pos);
51
+ }
52
+ attachWorkspaceFile(relPath);
53
+ state.mentionItems = [];
54
+ state.mentionStart = -1;
55
+ dom.mentionPopover.classList.remove('open');
56
+ updateComposerMetrics();
57
+ }
58
+ function renderMentionList() {
59
+ dom.mentionPopover.textContent = '';
60
+ if (state.mentionItems.length === 0) {
61
+ dom.mentionPopover.classList.remove('open');
62
+ return;
63
+ }
64
+ dom.mentionPopover.classList.add('open');
65
+ state.mentionItems.forEach((item, idx) => {
66
+ const btn = document.createElement('button');
67
+ btn.type = 'button';
68
+ btn.className = `mention-item${idx === state.mentionIndex ? ' selected' : ''}`;
69
+ if (state.mentionMode === 'command') {
70
+ const cmdName = item.name || '';
71
+ btn.textContent = `/${cmdName}${item.description ? ` — ${item.description.slice(0, 62)}` : ''}`;
72
+ btn.addEventListener('click', () => applySlashChoice(cmdName));
73
+ }
74
+ else {
75
+ const filePath = item.path || '';
76
+ btn.textContent = `@${filePath}`;
77
+ btn.addEventListener('click', () => applyMentionChoice(filePath));
78
+ }
79
+ dom.mentionPopover.appendChild(btn);
80
+ });
81
+ }
82
+ async function checkAtMention() {
83
+ const val = dom.promptInput.value || '';
84
+ const trimmedLeft = val.trimStart();
85
+ if (trimmedLeft.startsWith('/') && !trimmedLeft.includes(' ') && state.cliCommands.length > 0) {
86
+ const q = trimmedLeft.slice(1).toLowerCase();
87
+ state.mentionMode = 'command';
88
+ const prefixHits = state.cliCommands.filter((c) => c.name.toLowerCase().startsWith(q));
89
+ const nameHits = state.cliCommands.filter((c) => {
90
+ const n = c.name.toLowerCase();
91
+ return !n.startsWith(q) && n.includes(q);
92
+ });
93
+ const descHits = state.cliCommands.filter((c) => {
94
+ const n = c.name.toLowerCase();
95
+ return !n.includes(q) && (c.description || '').toLowerCase().includes(q);
96
+ });
97
+ state.mentionItems = prefixHits.concat(nameHits, descHits).slice(0, 10);
98
+ state.mentionIndex = 0;
99
+ renderMentionList();
100
+ return;
101
+ }
102
+ const pos = dom.promptInput.selectionStart || val.length;
103
+ const before = val.slice(0, pos);
104
+ const atIdx = before.lastIndexOf('@');
105
+ if (atIdx < 0 || (atIdx > 0 && !/\s/.test(before[atIdx - 1] || ''))) {
106
+ state.mentionItems = [];
107
+ state.mentionStart = -1;
108
+ dom.mentionPopover.classList.remove('open');
109
+ return;
110
+ }
111
+ const query = before.slice(atIdx + 1);
112
+ if (/\s/.test(query)) {
113
+ dom.mentionPopover.classList.remove('open');
114
+ return;
115
+ }
116
+ state.mentionMode = 'file';
117
+ state.mentionStart = atIdx;
118
+ const res = await fetch(apiUrl(`/api/files?q=${encodeURIComponent(query)}`), { headers: authHeaders() }).catch(() => null);
119
+ if (!res || !res.ok)
120
+ return;
121
+ const data = await res.json();
122
+ state.mentionItems = (data.entries || []).filter((x) => x.type === 'file').slice(0, 8);
123
+ state.mentionIndex = 0;
124
+ renderMentionList();
125
+ }
126
+ export function bindComposerEvents() {
127
+ dom.composerPill.addEventListener('click', (e) => {
128
+ if (e.target === dom.composerPill)
129
+ dom.promptInput.focus();
130
+ });
131
+ dom.promptInput.addEventListener('compositionstart', () => { state.isComposing = true; });
132
+ dom.promptInput.addEventListener('compositionend', () => { state.isComposing = false; });
133
+ dom.promptInput.addEventListener('input', () => {
134
+ updateComposerMetrics();
135
+ void checkAtMention();
136
+ });
137
+ dom.promptInput.addEventListener('keydown', (e) => {
138
+ if (dom.mentionPopover.classList.contains('open') && state.mentionItems.length > 0) {
139
+ if (e.key === 'ArrowDown') {
140
+ e.preventDefault();
141
+ state.mentionIndex = (state.mentionIndex + 1) % state.mentionItems.length;
142
+ renderMentionList();
143
+ return;
144
+ }
145
+ if (e.key === 'ArrowUp') {
146
+ e.preventDefault();
147
+ state.mentionIndex = (state.mentionIndex - 1 + state.mentionItems.length) % state.mentionItems.length;
148
+ renderMentionList();
149
+ return;
150
+ }
151
+ if (e.key === 'Enter' || e.key === 'Tab') {
152
+ e.preventDefault();
153
+ const chosen = state.mentionItems[state.mentionIndex];
154
+ if (chosen) {
155
+ if (state.mentionMode === 'command' && chosen.name)
156
+ applySlashChoice(chosen.name);
157
+ else if (chosen.path)
158
+ applyMentionChoice(chosen.path);
159
+ }
160
+ return;
161
+ }
162
+ if (e.key === 'Escape') {
163
+ e.preventDefault();
164
+ dom.mentionPopover.classList.remove('open');
165
+ return;
166
+ }
167
+ }
168
+ if (e.key === 'Enter' && !e.shiftKey && !e.isComposing && !state.isComposing) {
169
+ e.preventDefault();
170
+ dom.chatForm.requestSubmit();
171
+ }
172
+ });
173
+ dom.chatForm.addEventListener('submit', (e) => {
174
+ e.preventDefault();
175
+ if (state.activeController) {
176
+ void callbacks.stopActiveRun();
177
+ return;
178
+ }
179
+ const text = (dom.promptInput.value || '').trim();
180
+ if (!text)
181
+ return;
182
+ const toAttach = state.attachments.slice();
183
+ dom.promptInput.value = '';
184
+ state.attachments = [];
185
+ renderAttachmentChips();
186
+ updateComposerMetrics();
187
+ if (text.startsWith('/') && /^\/[a-z?][a-z0-9_-]*/i.test(text)) {
188
+ void callbacks.runCliCommand(text, 'chat');
189
+ return;
190
+ }
191
+ void callbacks.submitPrompt(text, toAttach);
192
+ });
193
+ dom.sendBtn.addEventListener('click', (e) => {
194
+ if (state.activeController) {
195
+ e.preventDefault();
196
+ void callbacks.stopActiveRun();
197
+ }
198
+ });
199
+ dom.heroHelpBtn.addEventListener('click', () => {
200
+ dom.promptInput.value = 'Inspect this local workspace and summarize its architecture and next steps.';
201
+ updateComposerMetrics();
202
+ dom.promptInput.focus();
203
+ });
204
+ }