@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,209 @@
1
+ import { dom } from './dom.js';
2
+ import { apiUrl, authHeaders, state, updateComposerMetrics } from './state.js';
3
+ import type { WorkspaceEntry } from './types.js';
4
+
5
+ export interface ComposerCallbacks {
6
+ submitPrompt(promptText: string, attachList: string[]): Promise<void>;
7
+ stopActiveRun(): Promise<void>;
8
+ runCliCommand(rawInput: string, target: 'studio' | 'chat'): Promise<void>;
9
+ }
10
+
11
+ let callbacks: ComposerCallbacks = {
12
+ submitPrompt: async () => {},
13
+ stopActiveRun: async () => {},
14
+ runCliCommand: async () => {}
15
+ };
16
+
17
+ export function configureComposerCallbacks(cbs: ComposerCallbacks): void {
18
+ callbacks = cbs;
19
+ }
20
+
21
+ export function renderAttachmentChips(): void {
22
+ dom.composerChipsEl.textContent = '';
23
+ dom.composerChipsEl.classList.toggle('has-items', state.attachments.length > 0);
24
+ for (const rel of state.attachments) {
25
+ const chip = document.createElement('span');
26
+ chip.className = 'chip';
27
+ const label = document.createElement('span');
28
+ label.textContent = `@${rel}`;
29
+ const rm = document.createElement('button');
30
+ rm.type = 'button';
31
+ rm.textContent = 'x';
32
+ rm.setAttribute('aria-label', `Remove ${rel}`);
33
+ rm.addEventListener('click', () => {
34
+ state.attachments = state.attachments.filter((item) => item !== rel);
35
+ renderAttachmentChips();
36
+ });
37
+ chip.appendChild(label);
38
+ chip.appendChild(rm);
39
+ dom.composerChipsEl.appendChild(chip);
40
+ }
41
+ }
42
+
43
+ export function attachWorkspaceFile(relPath: string): void {
44
+ if (!relPath || state.attachments.includes(relPath) || state.attachments.length >= 6) return;
45
+ state.attachments.push(relPath);
46
+ renderAttachmentChips();
47
+ dom.promptInput.focus();
48
+ }
49
+
50
+ function applySlashChoice(cmdName: string): void {
51
+ dom.promptInput.value = `/${cmdName} `;
52
+ state.mentionItems = [];
53
+ state.mentionStart = -1;
54
+ dom.mentionPopover.classList.remove('open');
55
+ updateComposerMetrics();
56
+ dom.promptInput.focus();
57
+ }
58
+
59
+ function applyMentionChoice(relPath: string): void {
60
+ const pos = dom.promptInput.selectionStart || dom.promptInput.value.length;
61
+ if (state.mentionStart >= 0 && pos >= state.mentionStart) {
62
+ dom.promptInput.value = dom.promptInput.value.slice(0, state.mentionStart) + dom.promptInput.value.slice(pos);
63
+ }
64
+ attachWorkspaceFile(relPath);
65
+ state.mentionItems = [];
66
+ state.mentionStart = -1;
67
+ dom.mentionPopover.classList.remove('open');
68
+ updateComposerMetrics();
69
+ }
70
+
71
+ function renderMentionList(): void {
72
+ dom.mentionPopover.textContent = '';
73
+ if (state.mentionItems.length === 0) {
74
+ dom.mentionPopover.classList.remove('open');
75
+ return;
76
+ }
77
+ dom.mentionPopover.classList.add('open');
78
+ state.mentionItems.forEach((item, idx) => {
79
+ const btn = document.createElement('button');
80
+ btn.type = 'button';
81
+ btn.className = `mention-item${idx === state.mentionIndex ? ' selected' : ''}`;
82
+ if (state.mentionMode === 'command') {
83
+ const cmdName = item.name || '';
84
+ btn.textContent = `/${cmdName}${item.description ? ` — ${item.description.slice(0, 62)}` : ''}`;
85
+ btn.addEventListener('click', () => applySlashChoice(cmdName));
86
+ } else {
87
+ const filePath = item.path || '';
88
+ btn.textContent = `@${filePath}`;
89
+ btn.addEventListener('click', () => applyMentionChoice(filePath));
90
+ }
91
+ dom.mentionPopover.appendChild(btn);
92
+ });
93
+ }
94
+
95
+ async function checkAtMention(): Promise<void> {
96
+ const val = dom.promptInput.value || '';
97
+ const trimmedLeft = val.trimStart();
98
+ if (trimmedLeft.startsWith('/') && !trimmedLeft.includes(' ') && state.cliCommands.length > 0) {
99
+ const q = trimmedLeft.slice(1).toLowerCase();
100
+ state.mentionMode = 'command';
101
+ const prefixHits = state.cliCommands.filter((c) => c.name.toLowerCase().startsWith(q));
102
+ const nameHits = state.cliCommands.filter((c) => {
103
+ const n = c.name.toLowerCase();
104
+ return !n.startsWith(q) && n.includes(q);
105
+ });
106
+ const descHits = state.cliCommands.filter((c) => {
107
+ const n = c.name.toLowerCase();
108
+ return !n.includes(q) && (c.description || '').toLowerCase().includes(q);
109
+ });
110
+ state.mentionItems = prefixHits.concat(nameHits, descHits).slice(0, 10);
111
+ state.mentionIndex = 0;
112
+ renderMentionList();
113
+ return;
114
+ }
115
+ const pos = dom.promptInput.selectionStart || val.length;
116
+ const before = val.slice(0, pos);
117
+ const atIdx = before.lastIndexOf('@');
118
+ if (atIdx < 0 || (atIdx > 0 && !/\s/.test(before[atIdx - 1] || ''))) {
119
+ state.mentionItems = [];
120
+ state.mentionStart = -1;
121
+ dom.mentionPopover.classList.remove('open');
122
+ return;
123
+ }
124
+ const query = before.slice(atIdx + 1);
125
+ if (/\s/.test(query)) { dom.mentionPopover.classList.remove('open'); return; }
126
+ state.mentionMode = 'file';
127
+ state.mentionStart = atIdx;
128
+ const res = await fetch(apiUrl(`/api/files?q=${encodeURIComponent(query)}`), { headers: authHeaders() }).catch(() => null);
129
+ if (!res || !res.ok) return;
130
+ const data = await res.json() as { entries?: WorkspaceEntry[] };
131
+ state.mentionItems = (data.entries || []).filter((x) => x.type === 'file').slice(0, 8);
132
+ state.mentionIndex = 0;
133
+ renderMentionList();
134
+ }
135
+
136
+ export function bindComposerEvents(): void {
137
+ dom.composerPill.addEventListener('click', (e) => {
138
+ if (e.target === dom.composerPill) dom.promptInput.focus();
139
+ });
140
+ dom.promptInput.addEventListener('compositionstart', () => { state.isComposing = true; });
141
+ dom.promptInput.addEventListener('compositionend', () => { state.isComposing = false; });
142
+ dom.promptInput.addEventListener('input', () => {
143
+ updateComposerMetrics();
144
+ void checkAtMention();
145
+ });
146
+ dom.promptInput.addEventListener('keydown', (e) => {
147
+ if (dom.mentionPopover.classList.contains('open') && state.mentionItems.length > 0) {
148
+ if (e.key === 'ArrowDown') {
149
+ e.preventDefault();
150
+ state.mentionIndex = (state.mentionIndex + 1) % state.mentionItems.length;
151
+ renderMentionList();
152
+ return;
153
+ }
154
+ if (e.key === 'ArrowUp') {
155
+ e.preventDefault();
156
+ state.mentionIndex = (state.mentionIndex - 1 + state.mentionItems.length) % state.mentionItems.length;
157
+ renderMentionList();
158
+ return;
159
+ }
160
+ if (e.key === 'Enter' || e.key === 'Tab') {
161
+ e.preventDefault();
162
+ const chosen = state.mentionItems[state.mentionIndex];
163
+ if (chosen) {
164
+ if (state.mentionMode === 'command' && chosen.name) applySlashChoice(chosen.name);
165
+ else if (chosen.path) applyMentionChoice(chosen.path);
166
+ }
167
+ return;
168
+ }
169
+ if (e.key === 'Escape') {
170
+ e.preventDefault();
171
+ dom.mentionPopover.classList.remove('open');
172
+ return;
173
+ }
174
+ }
175
+ if (e.key === 'Enter' && !e.shiftKey && !e.isComposing && !state.isComposing) {
176
+ e.preventDefault();
177
+ dom.chatForm.requestSubmit();
178
+ }
179
+ });
180
+
181
+ dom.chatForm.addEventListener('submit', (e) => {
182
+ e.preventDefault();
183
+ if (state.activeController) { void callbacks.stopActiveRun(); return; }
184
+ const text = (dom.promptInput.value || '').trim();
185
+ if (!text) return;
186
+ const toAttach = state.attachments.slice();
187
+ dom.promptInput.value = '';
188
+ state.attachments = [];
189
+ renderAttachmentChips();
190
+ updateComposerMetrics();
191
+ if (text.startsWith('/') && /^\/[a-z?][a-z0-9_-]*/i.test(text)) {
192
+ void callbacks.runCliCommand(text, 'chat');
193
+ return;
194
+ }
195
+ void callbacks.submitPrompt(text, toAttach);
196
+ });
197
+
198
+ dom.sendBtn.addEventListener('click', (e) => {
199
+ if (state.activeController) {
200
+ e.preventDefault();
201
+ void callbacks.stopActiveRun();
202
+ }
203
+ });
204
+ dom.heroHelpBtn.addEventListener('click', () => {
205
+ dom.promptInput.value = 'Inspect this local workspace and summarize its architecture and next steps.';
206
+ updateComposerMetrics();
207
+ dom.promptInput.focus();
208
+ });
209
+ }
@@ -0,0 +1,176 @@
1
+ import { dom } from './dom.js';
2
+ import { apiUrl, authHeaders, state } from './state.js';
3
+ let callbacks = {
4
+ openFile: async () => { },
5
+ saveCurrentSessionWorkspace: () => { }
6
+ };
7
+ export function configureDiffPanelCallbacks(cbs) {
8
+ callbacks = cbs;
9
+ }
10
+ export function syncDiffControlsUi() {
11
+ const isSessionScope = state.diffScope === 'session' && Boolean(state.sessionId);
12
+ if (dom.diffScopeSessionBtn) {
13
+ dom.diffScopeSessionBtn.classList.toggle('active', isSessionScope);
14
+ dom.diffScopeSessionBtn.disabled = !state.sessionId;
15
+ }
16
+ if (dom.diffScopeWorkspaceBtn) {
17
+ dom.diffScopeWorkspaceBtn.classList.toggle('active', !isSessionScope);
18
+ }
19
+ if (dom.diffStagedToggle) {
20
+ dom.diffStagedToggle.style.display = isSessionScope ? 'none' : 'inline-block';
21
+ dom.diffStagedToggle.textContent = state.diffStaged ? 'Staged' : 'Working';
22
+ }
23
+ }
24
+ export async function loadDiff() {
25
+ const seq = state.sessionSwitchSeq;
26
+ const scope = state.diffScope === 'session' && state.sessionId ? 'session' : 'workspace';
27
+ syncDiffControlsUi();
28
+ const params = new URLSearchParams();
29
+ params.set('scope', scope);
30
+ if (state.sessionId)
31
+ params.set('sessionId', state.sessionId);
32
+ if (scope === 'workspace' && state.diffStaged)
33
+ params.set('staged', '1');
34
+ const res = await fetch(apiUrl(`/api/diff?${params.toString()}`), { headers: authHeaders() }).catch(() => null);
35
+ if (!res || !res.ok || seq !== state.sessionSwitchSeq)
36
+ return;
37
+ const data = await res.json().catch(() => null);
38
+ if (!data || seq !== state.sessionSwitchSeq)
39
+ return;
40
+ dom.diffListEl.textContent = '';
41
+ const wsCount = Number(data.workspaceFileCount || 0);
42
+ const sessCount = Number(data.sessionFileCount || 0);
43
+ if (dom.diffScopeSessionBtn) {
44
+ dom.diffScopeSessionBtn.textContent = state.sessionId ? `This chat (${sessCount})` : 'This chat';
45
+ }
46
+ if (dom.diffScopeWorkspaceBtn) {
47
+ dom.diffScopeWorkspaceBtn.textContent = `Workspace (${wsCount})`;
48
+ }
49
+ if (!data.isGitRepo && scope === 'workspace') {
50
+ dom.diffSummaryEl.textContent = 'Not a Git repository.';
51
+ return;
52
+ }
53
+ const files = data.files || [];
54
+ const addTot = Number(data.totalAdditions || 0);
55
+ const delTot = Number(data.totalDeletions || 0);
56
+ dom.diffSummaryEl.textContent = (addTot || delTot)
57
+ ? `+${addTot} / -${delTot}`
58
+ : (files.length > 0 ? `${files.length}${files.length === 1 ? ' file' : ' files'}` : '');
59
+ if (files.length === 0) {
60
+ const empty = document.createElement('div');
61
+ empty.className = 'diff-empty-box';
62
+ const activeSessObj = state.sessions.find((x) => x.id === state.sessionId);
63
+ const activeTitle = activeSessObj && activeSessObj.title
64
+ ? `"${activeSessObj.title.slice(0, 48)}"`
65
+ : 'this conversation';
66
+ const msg = document.createElement('div');
67
+ msg.textContent = scope === 'session'
68
+ ? `No file changes in ${activeTitle}.`
69
+ : (state.diffStaged ? 'No staged changes.' : 'Working tree clean.');
70
+ empty.appendChild(msg);
71
+ if (scope === 'session' && wsCount > 0) {
72
+ const switchBtn = document.createElement('button');
73
+ switchBtn.type = 'button';
74
+ switchBtn.className = 'btn-ghost';
75
+ switchBtn.textContent = `View workspace changes (${wsCount})`;
76
+ switchBtn.addEventListener('click', () => {
77
+ state.diffScope = 'workspace';
78
+ callbacks.saveCurrentSessionWorkspace();
79
+ syncDiffControlsUi();
80
+ void loadDiff();
81
+ });
82
+ empty.appendChild(switchBtn);
83
+ }
84
+ dom.diffListEl.appendChild(empty);
85
+ return;
86
+ }
87
+ for (const f of files) {
88
+ const card = document.createElement('details');
89
+ card.className = 'diff-file';
90
+ card.open = files.length <= 6;
91
+ const head = document.createElement('summary');
92
+ head.className = 'diff-file-head';
93
+ const p = document.createElement('span');
94
+ p.className = 'diff-file-path';
95
+ p.title = f.path;
96
+ const badgeChar = f.status && f.status[0] ? f.status[0].toUpperCase() : 'M';
97
+ p.textContent = `[${badgeChar}] ${f.path}`;
98
+ const right = document.createElement('span');
99
+ right.className = 'diff-file-right';
100
+ if (f.additions || f.deletions) {
101
+ const add = document.createElement('span');
102
+ add.className = 'diff-add';
103
+ add.textContent = `+${f.additions || 0}`;
104
+ const del = document.createElement('span');
105
+ del.className = 'diff-del';
106
+ del.textContent = `-${f.deletions || 0}`;
107
+ right.appendChild(add);
108
+ right.appendChild(del);
109
+ }
110
+ if (f.existsOnDisk !== false) {
111
+ const openBtn = document.createElement('button');
112
+ openBtn.type = 'button';
113
+ openBtn.className = 'diff-open-btn';
114
+ openBtn.textContent = 'Open';
115
+ openBtn.addEventListener('click', (e) => {
116
+ e.preventDefault();
117
+ e.stopPropagation();
118
+ void callbacks.openFile(f.path);
119
+ });
120
+ right.appendChild(openBtn);
121
+ }
122
+ head.appendChild(p);
123
+ head.appendChild(right);
124
+ card.appendChild(head);
125
+ if (Array.isArray(f.hunks) && f.hunks.length > 0) {
126
+ const pre = document.createElement('div');
127
+ pre.className = 'diff-lines';
128
+ for (const h of f.hunks) {
129
+ const hh = document.createElement('div');
130
+ hh.className = 'diff-line hunk';
131
+ hh.textContent = h.header;
132
+ pre.appendChild(hh);
133
+ for (const ln of h.lines || []) {
134
+ const row = document.createElement('div');
135
+ row.className = `diff-line${ln.startsWith('+') ? ' plus' : ln.startsWith('-') ? ' minus' : ''}`;
136
+ row.textContent = ln;
137
+ pre.appendChild(row);
138
+ }
139
+ }
140
+ card.appendChild(pre);
141
+ }
142
+ else if (f.note) {
143
+ const note = document.createElement('div');
144
+ note.className = 'diff-note';
145
+ note.textContent = f.note;
146
+ card.appendChild(note);
147
+ }
148
+ dom.diffListEl.appendChild(card);
149
+ }
150
+ }
151
+ export function bindDiffPanelEvents() {
152
+ if (dom.diffScopeSessionBtn) {
153
+ dom.diffScopeSessionBtn.addEventListener('click', () => {
154
+ if (!state.sessionId)
155
+ return;
156
+ state.diffScope = 'session';
157
+ callbacks.saveCurrentSessionWorkspace();
158
+ syncDiffControlsUi();
159
+ void loadDiff();
160
+ });
161
+ }
162
+ if (dom.diffScopeWorkspaceBtn) {
163
+ dom.diffScopeWorkspaceBtn.addEventListener('click', () => {
164
+ state.diffScope = 'workspace';
165
+ callbacks.saveCurrentSessionWorkspace();
166
+ syncDiffControlsUi();
167
+ void loadDiff();
168
+ });
169
+ }
170
+ dom.diffStagedToggle.addEventListener('click', () => {
171
+ state.diffStaged = !state.diffStaged;
172
+ syncDiffControlsUi();
173
+ void loadDiff();
174
+ });
175
+ dom.refreshDiffBtn.addEventListener('click', () => { void loadDiff(); });
176
+ }
@@ -0,0 +1,203 @@
1
+ import { dom } from './dom.js';
2
+ import { apiUrl, authHeaders, state } from './state.js';
3
+
4
+ interface DiffFilePayload {
5
+ path: string;
6
+ status?: string;
7
+ additions?: number;
8
+ deletions?: number;
9
+ existsOnDisk?: boolean;
10
+ note?: string;
11
+ hunks?: Array<{ header: string; lines?: string[] }>;
12
+ }
13
+
14
+ interface DiffResponsePayload {
15
+ isGitRepo?: boolean;
16
+ workspaceFileCount?: number;
17
+ sessionFileCount?: number;
18
+ totalAdditions?: number;
19
+ totalDeletions?: number;
20
+ files?: DiffFilePayload[];
21
+ }
22
+
23
+ export interface DiffPanelCallbacks {
24
+ openFile(relPath: string): Promise<void>;
25
+ saveCurrentSessionWorkspace(): void;
26
+ }
27
+
28
+ let callbacks: DiffPanelCallbacks = {
29
+ openFile: async () => {},
30
+ saveCurrentSessionWorkspace: () => {}
31
+ };
32
+
33
+ export function configureDiffPanelCallbacks(cbs: DiffPanelCallbacks): void {
34
+ callbacks = cbs;
35
+ }
36
+
37
+ export function syncDiffControlsUi(): void {
38
+ const isSessionScope = state.diffScope === 'session' && Boolean(state.sessionId);
39
+ if (dom.diffScopeSessionBtn) {
40
+ dom.diffScopeSessionBtn.classList.toggle('active', isSessionScope);
41
+ dom.diffScopeSessionBtn.disabled = !state.sessionId;
42
+ }
43
+ if (dom.diffScopeWorkspaceBtn) {
44
+ dom.diffScopeWorkspaceBtn.classList.toggle('active', !isSessionScope);
45
+ }
46
+ if (dom.diffStagedToggle) {
47
+ dom.diffStagedToggle.style.display = isSessionScope ? 'none' : 'inline-block';
48
+ dom.diffStagedToggle.textContent = state.diffStaged ? 'Staged' : 'Working';
49
+ }
50
+ }
51
+
52
+ export async function loadDiff(): Promise<void> {
53
+ const seq = state.sessionSwitchSeq;
54
+ const scope: 'session' | 'workspace' = state.diffScope === 'session' && state.sessionId ? 'session' : 'workspace';
55
+ syncDiffControlsUi();
56
+ const params = new URLSearchParams();
57
+ params.set('scope', scope);
58
+ if (state.sessionId) params.set('sessionId', state.sessionId);
59
+ if (scope === 'workspace' && state.diffStaged) params.set('staged', '1');
60
+ const res = await fetch(apiUrl(`/api/diff?${params.toString()}`), { headers: authHeaders() }).catch(() => null);
61
+ if (!res || !res.ok || seq !== state.sessionSwitchSeq) return;
62
+ const data = await res.json().catch(() => null) as DiffResponsePayload | null;
63
+ if (!data || seq !== state.sessionSwitchSeq) return;
64
+
65
+ dom.diffListEl.textContent = '';
66
+ const wsCount = Number(data.workspaceFileCount || 0);
67
+ const sessCount = Number(data.sessionFileCount || 0);
68
+ if (dom.diffScopeSessionBtn) {
69
+ dom.diffScopeSessionBtn.textContent = state.sessionId ? `This chat (${sessCount})` : 'This chat';
70
+ }
71
+ if (dom.diffScopeWorkspaceBtn) {
72
+ dom.diffScopeWorkspaceBtn.textContent = `Workspace (${wsCount})`;
73
+ }
74
+ if (!data.isGitRepo && scope === 'workspace') {
75
+ dom.diffSummaryEl.textContent = 'Not a Git repository.';
76
+ return;
77
+ }
78
+ const files = data.files || [];
79
+ const addTot = Number(data.totalAdditions || 0);
80
+ const delTot = Number(data.totalDeletions || 0);
81
+ dom.diffSummaryEl.textContent = (addTot || delTot)
82
+ ? `+${addTot} / -${delTot}`
83
+ : (files.length > 0 ? `${files.length}${files.length === 1 ? ' file' : ' files'}` : '');
84
+
85
+ if (files.length === 0) {
86
+ const empty = document.createElement('div');
87
+ empty.className = 'diff-empty-box';
88
+ const activeSessObj = state.sessions.find((x) => x.id === state.sessionId);
89
+ const activeTitle = activeSessObj && activeSessObj.title
90
+ ? `"${activeSessObj.title.slice(0, 48)}"`
91
+ : 'this conversation';
92
+ const msg = document.createElement('div');
93
+ msg.textContent = scope === 'session'
94
+ ? `No file changes in ${activeTitle}.`
95
+ : (state.diffStaged ? 'No staged changes.' : 'Working tree clean.');
96
+ empty.appendChild(msg);
97
+ if (scope === 'session' && wsCount > 0) {
98
+ const switchBtn = document.createElement('button');
99
+ switchBtn.type = 'button';
100
+ switchBtn.className = 'btn-ghost';
101
+ switchBtn.textContent = `View workspace changes (${wsCount})`;
102
+ switchBtn.addEventListener('click', () => {
103
+ state.diffScope = 'workspace';
104
+ callbacks.saveCurrentSessionWorkspace();
105
+ syncDiffControlsUi();
106
+ void loadDiff();
107
+ });
108
+ empty.appendChild(switchBtn);
109
+ }
110
+ dom.diffListEl.appendChild(empty);
111
+ return;
112
+ }
113
+
114
+ for (const f of files) {
115
+ const card = document.createElement('details');
116
+ card.className = 'diff-file';
117
+ card.open = files.length <= 6;
118
+ const head = document.createElement('summary');
119
+ head.className = 'diff-file-head';
120
+ const p = document.createElement('span');
121
+ p.className = 'diff-file-path';
122
+ p.title = f.path;
123
+ const badgeChar = f.status && f.status[0] ? f.status[0].toUpperCase() : 'M';
124
+ p.textContent = `[${badgeChar}] ${f.path}`;
125
+ const right = document.createElement('span');
126
+ right.className = 'diff-file-right';
127
+ if (f.additions || f.deletions) {
128
+ const add = document.createElement('span');
129
+ add.className = 'diff-add';
130
+ add.textContent = `+${f.additions || 0}`;
131
+ const del = document.createElement('span');
132
+ del.className = 'diff-del';
133
+ del.textContent = `-${f.deletions || 0}`;
134
+ right.appendChild(add);
135
+ right.appendChild(del);
136
+ }
137
+ if (f.existsOnDisk !== false) {
138
+ const openBtn = document.createElement('button');
139
+ openBtn.type = 'button';
140
+ openBtn.className = 'diff-open-btn';
141
+ openBtn.textContent = 'Open';
142
+ openBtn.addEventListener('click', (e) => {
143
+ e.preventDefault();
144
+ e.stopPropagation();
145
+ void callbacks.openFile(f.path);
146
+ });
147
+ right.appendChild(openBtn);
148
+ }
149
+ head.appendChild(p);
150
+ head.appendChild(right);
151
+ card.appendChild(head);
152
+
153
+ if (Array.isArray(f.hunks) && f.hunks.length > 0) {
154
+ const pre = document.createElement('div');
155
+ pre.className = 'diff-lines';
156
+ for (const h of f.hunks) {
157
+ const hh = document.createElement('div');
158
+ hh.className = 'diff-line hunk';
159
+ hh.textContent = h.header;
160
+ pre.appendChild(hh);
161
+ for (const ln of h.lines || []) {
162
+ const row = document.createElement('div');
163
+ row.className = `diff-line${ln.startsWith('+') ? ' plus' : ln.startsWith('-') ? ' minus' : ''}`;
164
+ row.textContent = ln;
165
+ pre.appendChild(row);
166
+ }
167
+ }
168
+ card.appendChild(pre);
169
+ } else if (f.note) {
170
+ const note = document.createElement('div');
171
+ note.className = 'diff-note';
172
+ note.textContent = f.note;
173
+ card.appendChild(note);
174
+ }
175
+ dom.diffListEl.appendChild(card);
176
+ }
177
+ }
178
+
179
+ export function bindDiffPanelEvents(): void {
180
+ if (dom.diffScopeSessionBtn) {
181
+ dom.diffScopeSessionBtn.addEventListener('click', () => {
182
+ if (!state.sessionId) return;
183
+ state.diffScope = 'session';
184
+ callbacks.saveCurrentSessionWorkspace();
185
+ syncDiffControlsUi();
186
+ void loadDiff();
187
+ });
188
+ }
189
+ if (dom.diffScopeWorkspaceBtn) {
190
+ dom.diffScopeWorkspaceBtn.addEventListener('click', () => {
191
+ state.diffScope = 'workspace';
192
+ callbacks.saveCurrentSessionWorkspace();
193
+ syncDiffControlsUi();
194
+ void loadDiff();
195
+ });
196
+ }
197
+ dom.diffStagedToggle.addEventListener('click', () => {
198
+ state.diffStaged = !state.diffStaged;
199
+ syncDiffControlsUi();
200
+ void loadDiff();
201
+ });
202
+ dom.refreshDiffBtn.addEventListener('click', () => { void loadDiff(); });
203
+ }