@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,672 @@
1
+ import { dom } from './dom.js';
2
+ import {
3
+ DEFAULT_STUDIO_W,
4
+ applyStudioWidth,
5
+ state,
6
+ syncExpandedStudioWidth
7
+ } from './state.js';
8
+ import {
9
+ clampSplitRatio,
10
+ halvesFitWidth,
11
+ isValidStudioTab,
12
+ loadPersistedStudioLayout,
13
+ normalizeOpenTabs,
14
+ persistStudioLayout,
15
+ pickDistinctSecondaryTab
16
+ } from './studioLayout.js';
17
+
18
+ export interface StudioTabHandlers {
19
+ onFilesTab(): void;
20
+ onDiffTab(): void;
21
+ onCliTab(): void;
22
+ onTerminalActivate(): void;
23
+ onTerminalDeactivate(): void;
24
+ onTerminalClose(): void;
25
+ onBrowserActivate(): void;
26
+ onBrowserDeactivate(): void;
27
+ onBrowserClose(): void;
28
+ onLayoutResized(): void;
29
+ }
30
+
31
+ const DEFAULT_TAB_LABELS: Record<string, string> = {
32
+ start: 'Start',
33
+ files: 'Files',
34
+ canvas: 'Editor',
35
+ terminal: 'Terminal',
36
+ browser: 'Browser',
37
+ diff: 'Changes',
38
+ cli: 'Commands'
39
+ };
40
+
41
+ const dynamicTabTitles = new Map<string, string>();
42
+ let lastActiveStudioTab: string | null = null;
43
+
44
+ let tabHandlers: StudioTabHandlers = {
45
+ onFilesTab: () => {},
46
+ onDiffTab: () => {},
47
+ onCliTab: () => {},
48
+ onTerminalActivate: () => {},
49
+ onTerminalDeactivate: () => {},
50
+ onTerminalClose: () => {},
51
+ onBrowserActivate: () => {},
52
+ onBrowserDeactivate: () => {},
53
+ onBrowserClose: () => {},
54
+ onLayoutResized: () => {}
55
+ };
56
+
57
+ export function configureStudioTabHandlers(handlers: StudioTabHandlers): void {
58
+ tabHandlers = handlers;
59
+ }
60
+
61
+ export function closeAllPopovers(): void {
62
+ dom.modelPopover.classList.remove('open');
63
+ dom.plusPopover.classList.remove('open');
64
+ dom.permPopover.classList.remove('open');
65
+ dom.permTriggerBtn.classList.remove('open');
66
+ dom.mentionPopover.classList.remove('open');
67
+ dom.browserMorePopover.classList.remove('open');
68
+ dom.modelTriggerBtn.setAttribute('aria-expanded', 'false');
69
+ dom.plusBtn.setAttribute('aria-expanded', 'false');
70
+ dom.permTriggerBtn.setAttribute('aria-expanded', 'false');
71
+ dom.browserMoreBtn.setAttribute('aria-expanded', 'false');
72
+ }
73
+
74
+ export function positionPopover(pop: HTMLElement, anchor: HTMLElement, preferUp: boolean): void {
75
+ const rect = anchor.getBoundingClientRect();
76
+ pop.classList.add('open');
77
+ const pw = pop.offsetWidth || 300;
78
+ const ph = pop.offsetHeight || 220;
79
+ let left = rect.left;
80
+ if (left + pw > window.innerWidth - 10) left = rect.right - pw;
81
+ left = Math.max(10, Math.min(window.innerWidth - pw - 10, left));
82
+ let top = preferUp ? (rect.top - ph - 8) : (rect.bottom + 8);
83
+ if (top < 10) top = rect.bottom + 8;
84
+ if (top + ph > window.innerHeight - 10) top = Math.max(10, window.innerHeight - ph - 10);
85
+ pop.style.left = `${left}px`;
86
+ pop.style.top = `${top}px`;
87
+ }
88
+
89
+ export function isStudioTabVisible(tab: string): boolean {
90
+ return state.studioTab === tab || (state.studioSplit && state.studioSecondaryTab === tab);
91
+ }
92
+
93
+ function createSvgElement(tag: string, attrs: Record<string, string>): SVGElement {
94
+ const el = document.createElementNS('http://www.w3.org/2000/svg', tag);
95
+ for (const [k, v] of Object.entries(attrs)) {
96
+ el.setAttribute(k, v);
97
+ }
98
+ return el;
99
+ }
100
+
101
+ function createTabIconSvg(tab: string): SVGElement {
102
+ const svg = createSvgElement('svg', {
103
+ class: 'studio-tab-icon',
104
+ viewBox: '0 0 24 24',
105
+ 'aria-hidden': 'true'
106
+ });
107
+ if (tab === 'start') {
108
+ svg.appendChild(createSvgElement('circle', { cx: '12', cy: '12', r: '9' }));
109
+ svg.appendChild(createSvgElement('polygon', { points: '16.24 7.76 14.12 14.12 7.76 16.24 9.88 9.88 16.24 7.76' }));
110
+ } else if (tab === 'files') {
111
+ svg.appendChild(createSvgElement('path', { d: 'M3 7a2 2 0 0 1 2-2h5l2 2h7a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2Z' }));
112
+ } else if (tab === 'canvas') {
113
+ svg.appendChild(createSvgElement('path', { d: 'M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z' }));
114
+ svg.appendChild(createSvgElement('polyline', { points: '14 2 14 8 20 8' }));
115
+ } else if (tab === 'terminal') {
116
+ svg.appendChild(createSvgElement('polyline', { points: '4 17 10 11 4 5' }));
117
+ svg.appendChild(createSvgElement('line', { x1: '12', y1: '19', x2: '20', y2: '19' }));
118
+ } else if (tab === 'browser') {
119
+ svg.appendChild(createSvgElement('circle', { cx: '12', cy: '12', r: '9' }));
120
+ svg.appendChild(createSvgElement('line', { x1: '3', y1: '12', x2: '21', y2: '12' }));
121
+ svg.appendChild(createSvgElement('path', { d: 'M12 3a14 14 0 0 1 0 18M12 3a14 14 0 0 0 0 18' }));
122
+ } else if (tab === 'diff') {
123
+ svg.appendChild(createSvgElement('circle', { cx: '18', cy: '18', r: '3' }));
124
+ svg.appendChild(createSvgElement('circle', { cx: '6', cy: '6', r: '3' }));
125
+ svg.appendChild(createSvgElement('path', { d: 'M6 9v6a3 3 0 0 0 3 3h6' }));
126
+ } else {
127
+ svg.appendChild(createSvgElement('polyline', { points: '9 18 15 12 9 6' }));
128
+ }
129
+ return svg;
130
+ }
131
+
132
+ function createCloseIconSvg(): SVGElement {
133
+ const svg = createSvgElement('svg', {
134
+ viewBox: '0 0 24 24',
135
+ 'aria-hidden': 'true'
136
+ });
137
+ svg.appendChild(createSvgElement('line', { x1: '18', y1: '6', x2: '6', y2: '18' }));
138
+ svg.appendChild(createSvgElement('line', { x1: '6', y1: '6', x2: '18', y2: '18' }));
139
+ return svg;
140
+ }
141
+
142
+ function tabDisplayTitle(tab: string): string {
143
+ const dynamic = dynamicTabTitles.get(tab);
144
+ if (dynamic && dynamic.trim()) return dynamic.trim();
145
+ return DEFAULT_TAB_LABELS[tab] || tab;
146
+ }
147
+
148
+ function persistCurrentLayout(): void {
149
+ persistStudioLayout({
150
+ openTabs: state.studioOpenTabs,
151
+ primaryTab: state.studioTab,
152
+ splitActive: state.studioSplit,
153
+ secondaryTab: state.studioSecondaryTab,
154
+ splitRatio: state.studioSplitRatio
155
+ });
156
+ }
157
+
158
+ function applySplitRatioVar(): void {
159
+ const ratio = clampSplitRatio(state.studioSplitRatio);
160
+ state.studioSplitRatio = ratio;
161
+ const leftPct = `${(ratio * 100).toFixed(2)}%`;
162
+ const rightPct = `${((1 - ratio) * 100).toFixed(2)}%`;
163
+ if (dom.studioPanesGrid) {
164
+ dom.studioPanesGrid.style.setProperty('--studio-split-left', leftPct);
165
+ dom.studioPanesGrid.style.setProperty('--studio-split-right', rightPct);
166
+ }
167
+ }
168
+
169
+ export function setStudioTabDynamicTitle(tab: string, title: string | null): void {
170
+ if (!isValidStudioTab(tab)) return;
171
+ const clean = typeof title === 'string' ? title.trim().slice(0, 40) : '';
172
+ const prev = dynamicTabTitles.get(tab) || '';
173
+ if (clean === prev) return;
174
+ if (clean) dynamicTabTitles.set(tab, clean);
175
+ else dynamicTabTitles.delete(tab);
176
+ const labelEl = dom.studioTabs?.querySelector<HTMLElement>(`.studio-tab[data-tab="${tab}"] .studio-tab-label`);
177
+ if (labelEl) {
178
+ const nextText = tabDisplayTitle(tab);
179
+ labelEl.textContent = nextText;
180
+ const chipEl = labelEl.closest<HTMLElement>('.studio-tab');
181
+ if (chipEl) chipEl.title = nextText;
182
+ }
183
+ }
184
+
185
+ function scrollActiveTabIntoView(): void {
186
+ const strip = dom.studioTabs;
187
+ if (!strip) return;
188
+ const activeChip = strip.querySelector<HTMLElement>('.studio-tab.active');
189
+ if (!activeChip) return;
190
+ const stripRect = strip.getBoundingClientRect();
191
+ if (stripRect.width <= 0) return;
192
+ const chipRect = activeChip.getBoundingClientRect();
193
+ if (chipRect.left < stripRect.left) {
194
+ strip.scrollLeft = Math.max(0, strip.scrollLeft - (stripRect.left - chipRect.left) - 6);
195
+ } else if (chipRect.right > stripRect.right) {
196
+ strip.scrollLeft = strip.scrollLeft + (chipRect.right - stripRect.right) + 6;
197
+ }
198
+ }
199
+
200
+ export function renderStudioTabs(): void {
201
+ const strip = dom.studioTabs;
202
+ if (!strip) return;
203
+ const effectiveSplit = state.studioSplit && state.studioSecondaryTab !== state.studioTab;
204
+ const openTabs = normalizeOpenTabs(
205
+ state.studioOpenTabs,
206
+ state.studioTab,
207
+ effectiveSplit,
208
+ state.studioSecondaryTab
209
+ );
210
+ state.studioOpenTabs = openTabs;
211
+
212
+ const isQuietLoneStart = openTabs.length === 1 && openTabs[0] === 'start' && !effectiveSplit;
213
+ strip.textContent = '';
214
+
215
+ openTabs.forEach((tab, index) => {
216
+ if (index > 0) {
217
+ const slot = document.createElement('span');
218
+ slot.className = 'studio-tab-slot';
219
+ slot.setAttribute('aria-hidden', 'true');
220
+ strip.appendChild(slot);
221
+ }
222
+
223
+ const isPrimary = tab === state.studioTab;
224
+ const isSecondary = effectiveSplit && tab === state.studioSecondaryTab;
225
+ const closable = !isQuietLoneStart;
226
+ const labelText = tabDisplayTitle(tab);
227
+
228
+ const chip = document.createElement('div');
229
+ chip.className = 'studio-tab';
230
+ if (isPrimary) chip.classList.add('active');
231
+ if (isSecondary) chip.classList.add('split-secondary');
232
+ if (isQuietLoneStart) chip.classList.add('quiet');
233
+ chip.setAttribute('data-tab', tab);
234
+ chip.setAttribute('role', 'tab');
235
+ chip.setAttribute('aria-selected', isPrimary ? 'true' : 'false');
236
+ chip.tabIndex = isPrimary ? 0 : -1;
237
+ chip.title = labelText;
238
+
239
+ const titleWrap = document.createElement('span');
240
+ titleWrap.className = 'studio-tab-title';
241
+ titleWrap.appendChild(createTabIconSvg(tab));
242
+ const label = document.createElement('span');
243
+ label.className = 'studio-tab-label';
244
+ label.textContent = labelText;
245
+ titleWrap.appendChild(label);
246
+ chip.appendChild(titleWrap);
247
+
248
+ chip.addEventListener('click', (e) => {
249
+ const target = e.target as HTMLElement | null;
250
+ if (target && target.closest('.studio-tab-close')) return;
251
+ setStudioTab(tab);
252
+ });
253
+ chip.addEventListener('keydown', (e) => {
254
+ if (e.key === 'Enter' || e.key === ' ') {
255
+ e.preventDefault();
256
+ setStudioTab(tab);
257
+ }
258
+ });
259
+ if (closable) {
260
+ chip.addEventListener('auxclick', (e) => {
261
+ if (e.button === 1) {
262
+ e.preventDefault();
263
+ closeStudioTab(tab);
264
+ }
265
+ });
266
+
267
+ const closeBtn = document.createElement('button');
268
+ closeBtn.type = 'button';
269
+ closeBtn.className = 'studio-tab-close';
270
+ closeBtn.setAttribute('aria-label', `Close ${labelText}`);
271
+ closeBtn.title = `Close ${labelText}`;
272
+ closeBtn.appendChild(createCloseIconSvg());
273
+ closeBtn.addEventListener('click', (e) => {
274
+ e.stopPropagation();
275
+ closeStudioTab(tab);
276
+ });
277
+ chip.appendChild(closeBtn);
278
+ }
279
+
280
+ strip.appendChild(chip);
281
+ });
282
+
283
+ scrollActiveTabIntoView();
284
+ window.requestAnimationFrame(scrollActiveTabIntoView);
285
+ }
286
+
287
+ function syncStudioPanes(): void {
288
+ const effectiveSplit = state.studioSplit && state.studioSecondaryTab !== state.studioTab;
289
+ dom.appEl.classList.toggle('studio-split', effectiveSplit);
290
+ applySplitRatioVar();
291
+ if (dom.studioSplitBar) dom.studioSplitBar.classList.toggle('open', effectiveSplit);
292
+ if (dom.studioSplitBtn) {
293
+ dom.studioSplitBtn.classList.toggle('active', effectiveSplit);
294
+ dom.studioSplitBtn.setAttribute('aria-pressed', effectiveSplit ? 'true' : 'false');
295
+ }
296
+ if (dom.studioSecondarySelect && dom.studioSecondarySelect.value !== state.studioSecondaryTab) {
297
+ dom.studioSecondarySelect.value = state.studioSecondaryTab;
298
+ }
299
+
300
+ renderStudioTabs();
301
+
302
+ document.querySelectorAll('.studio-pane').forEach((p) => {
303
+ const t = p.getAttribute('data-pane') || '';
304
+ const isPrimary = t === state.studioTab;
305
+ const isSecondary = effectiveSplit && t === state.studioSecondaryTab;
306
+ p.classList.toggle('active', isPrimary || isSecondary);
307
+ p.classList.toggle('pane-primary', isPrimary);
308
+ p.classList.toggle('pane-secondary', isSecondary);
309
+ });
310
+ }
311
+
312
+ function notifyVisibleTabs(): void {
313
+ if (!dom.appEl.classList.contains('studio-open')) return;
314
+ if (isStudioTabVisible('files')) tabHandlers.onFilesTab();
315
+ if (isStudioTabVisible('diff')) tabHandlers.onDiffTab();
316
+ if (isStudioTabVisible('cli')) tabHandlers.onCliTab();
317
+ if (isStudioTabVisible('terminal')) tabHandlers.onTerminalActivate();
318
+ else tabHandlers.onTerminalDeactivate();
319
+
320
+ if (isStudioTabVisible('browser') && state.browserEngine === 'agent') {
321
+ tabHandlers.onBrowserActivate();
322
+ } else {
323
+ tabHandlers.onBrowserDeactivate();
324
+ }
325
+ tabHandlers.onLayoutResized();
326
+ }
327
+
328
+ export function syncPanelChromeState(): void {
329
+ const isOpen = dom.appEl.classList.contains('studio-open');
330
+ dom.studioToggleBtn.classList.toggle('active', isOpen);
331
+ dom.studioToggleBtn.setAttribute('aria-expanded', isOpen ? 'true' : 'false');
332
+ if (dom.studioExpandBtn) {
333
+ dom.studioExpandBtn.classList.toggle('active', state.studioExpanded);
334
+ dom.studioExpandBtn.title = state.studioExpanded ? 'Restore panel width' : 'Expand panel (⌘⌥↵)';
335
+ }
336
+ const navPairs: ReadonlyArray<readonly [HTMLElement | null, string]> = [
337
+ [dom.navFilesBtn, 'files'],
338
+ [dom.navCanvasBtn, 'canvas'],
339
+ [dom.navTerminalBtn, 'terminal'],
340
+ [dom.navBrowserBtn, 'browser'],
341
+ [dom.navCliBtn, 'cli'],
342
+ [dom.navDiffBtn, 'diff']
343
+ ];
344
+ for (const [btn, tab] of navPairs) {
345
+ if (!btn) continue;
346
+ btn.classList.toggle('active', isOpen && isStudioTabVisible(tab));
347
+ }
348
+ }
349
+
350
+ export function restoreStudioLayoutPreference(): void {
351
+ const saved = loadPersistedStudioLayout();
352
+ state.studioOpenTabs = saved.openTabs;
353
+ state.studioTab = saved.primaryTab;
354
+ state.studioSplit = saved.splitActive;
355
+ state.studioSecondaryTab = saved.secondaryTab;
356
+ state.studioSplitRatio = saved.splitRatio;
357
+ syncStudioPanes();
358
+ }
359
+
360
+ export function setStudioTab(tab: string): void {
361
+ const nextTab = isValidStudioTab(tab) ? tab : 'start';
362
+ if (state.studioTab && state.studioTab !== nextTab && state.studioTab !== 'start') {
363
+ lastActiveStudioTab = state.studioTab;
364
+ }
365
+ if (!state.studioOpenTabs.includes(nextTab)) {
366
+ // Opening a concrete tool from a lone Start tab replaces Start smoothly,
367
+ // while opening Start via "+" appends it as a launch tab.
368
+ if (
369
+ nextTab !== 'start'
370
+ && state.studioOpenTabs.length === 1
371
+ && state.studioOpenTabs[0] === 'start'
372
+ && !state.studioSplit
373
+ ) {
374
+ state.studioOpenTabs = [nextTab];
375
+ } else {
376
+ state.studioOpenTabs = [...state.studioOpenTabs, nextTab];
377
+ }
378
+ }
379
+ if (state.studioSplit && nextTab === state.studioSecondaryTab) {
380
+ state.studioSecondaryTab = pickDistinctSecondaryTab(nextTab, state.studioTab);
381
+ }
382
+ state.studioTab = nextTab;
383
+ persistCurrentLayout();
384
+ syncStudioPanes();
385
+ syncPanelChromeState();
386
+ notifyVisibleTabs();
387
+ }
388
+
389
+ export function closeStudioTab(tab: string): void {
390
+ if (!isValidStudioTab(tab)) return;
391
+ const idx = state.studioOpenTabs.indexOf(tab);
392
+ if (idx === -1) return;
393
+
394
+ if (tab === 'browser') {
395
+ dynamicTabTitles.delete('browser');
396
+ tabHandlers.onBrowserClose();
397
+ } else if (tab === 'terminal') {
398
+ dynamicTabTitles.delete('terminal');
399
+ tabHandlers.onTerminalClose();
400
+ } else if (tab === 'canvas') {
401
+ dynamicTabTitles.delete('canvas');
402
+ }
403
+
404
+ const remaining = state.studioOpenTabs.filter((t) => t !== tab);
405
+ if (remaining.length === 0) {
406
+ state.studioOpenTabs = ['start'];
407
+ state.studioTab = 'start';
408
+ state.studioSplit = false;
409
+ } else {
410
+ state.studioOpenTabs = remaining;
411
+ if (state.studioSplit) {
412
+ if (remaining.length < 2) {
413
+ state.studioSplit = false;
414
+ state.studioTab = remaining[0] || 'start';
415
+ } else if (tab === state.studioTab) {
416
+ const nextPrimary = remaining.find((t) => t !== state.studioSecondaryTab) || remaining[0] || 'start';
417
+ state.studioTab = nextPrimary;
418
+ } else if (tab === state.studioSecondaryTab) {
419
+ const nextSecondary = remaining.find((t) => t !== state.studioTab) || pickDistinctSecondaryTab(state.studioTab);
420
+ state.studioSecondaryTab = nextSecondary;
421
+ }
422
+ } else if (tab === state.studioTab) {
423
+ const nextIdx = Math.min(idx, remaining.length - 1);
424
+ state.studioTab = remaining[nextIdx] || 'start';
425
+ }
426
+ }
427
+
428
+ persistCurrentLayout();
429
+ syncStudioPanes();
430
+ syncPanelChromeState();
431
+ notifyVisibleTabs();
432
+ }
433
+
434
+ export function setSecondaryStudioTab(tab: string): void {
435
+ if (!isValidStudioTab(tab)) return;
436
+ if (!state.studioOpenTabs.includes(tab)) {
437
+ state.studioOpenTabs = [...state.studioOpenTabs, tab];
438
+ }
439
+ if (tab === state.studioTab) {
440
+ const prevPrimary = state.studioTab;
441
+ state.studioTab = state.studioSecondaryTab;
442
+ state.studioSecondaryTab = prevPrimary;
443
+ } else {
444
+ state.studioSecondaryTab = tab;
445
+ }
446
+ persistCurrentLayout();
447
+ syncStudioPanes();
448
+ syncPanelChromeState();
449
+ notifyVisibleTabs();
450
+ }
451
+
452
+ export function toggleStudioSplit(force?: boolean): void {
453
+ const nextSplit = typeof force === 'boolean' ? force : !state.studioSplit;
454
+ if (nextSplit) {
455
+ if (!dom.appEl.classList.contains('studio-open')) {
456
+ openStudio(state.studioTab || 'start');
457
+ }
458
+ const studioRectW = dom.studioRoot?.getBoundingClientRect().width || state.studioWidth;
459
+ if (!halvesFitWidth(studioRectW) && !state.studioExpanded) {
460
+ state.studioExpanded = true;
461
+ if (window.innerWidth > 960 && window.innerWidth < 1380) {
462
+ dom.appEl.classList.add('sidebar-collapsed');
463
+ }
464
+ syncExpandedStudioWidth();
465
+ dom.appEl.classList.toggle('studio-expanded', true);
466
+ }
467
+ const recentCandidate = lastActiveStudioTab
468
+ && lastActiveStudioTab !== state.studioTab
469
+ && state.studioOpenTabs.includes(lastActiveStudioTab)
470
+ ? lastActiveStudioTab
471
+ : undefined;
472
+ const otherOpen = recentCandidate
473
+ || state.studioOpenTabs.find((t) => t !== state.studioTab && t !== 'start')
474
+ || state.studioOpenTabs.find((t) => t !== state.studioTab);
475
+ state.studioSecondaryTab = pickDistinctSecondaryTab(state.studioTab, otherOpen || state.studioSecondaryTab);
476
+ if (!state.studioOpenTabs.includes(state.studioSecondaryTab)) {
477
+ state.studioOpenTabs = [...state.studioOpenTabs, state.studioSecondaryTab];
478
+ }
479
+ }
480
+ state.studioSplit = nextSplit;
481
+ persistCurrentLayout();
482
+ syncStudioPanes();
483
+ syncPanelChromeState();
484
+ notifyVisibleTabs();
485
+ }
486
+
487
+ export function swapStudioSplitPanes(): void {
488
+ if (!state.studioSplit) return;
489
+ const prevPrimary = state.studioTab;
490
+ state.studioTab = state.studioSecondaryTab;
491
+ state.studioSecondaryTab = prevPrimary;
492
+ persistCurrentLayout();
493
+ syncStudioPanes();
494
+ syncPanelChromeState();
495
+ notifyVisibleTabs();
496
+ }
497
+
498
+ export function openStudio(tab?: string): void {
499
+ closeAllPopovers();
500
+ if (window.innerWidth <= 960) dom.appEl.classList.add('sidebar-collapsed');
501
+ if (dom.studioRoot) {
502
+ dom.studioRoot.style.flexBasis = '';
503
+ dom.studioRoot.style.width = '';
504
+ }
505
+ applyStudioWidth(state.studioWidth || DEFAULT_STUDIO_W, false);
506
+ dom.appEl.classList.add('studio-open');
507
+ dom.appEl.classList.toggle('studio-expanded', Boolean(state.studioExpanded));
508
+ if (tab) {
509
+ setStudioTab(tab);
510
+ } else {
511
+ syncStudioPanes();
512
+ syncPanelChromeState();
513
+ notifyVisibleTabs();
514
+ }
515
+ setTimeout(scrollActiveTabIntoView, 220);
516
+ }
517
+
518
+ export function closeStudio(): void {
519
+ state.studioExpanded = false;
520
+ dom.appEl.classList.remove('studio-open', 'studio-expanded', 'studio-resizing');
521
+ dom.studioResizer.classList.remove('dragging', 'snap-close');
522
+ if (dom.studioRoot) {
523
+ dom.studioRoot.style.flexBasis = '';
524
+ dom.studioRoot.style.width = '';
525
+ }
526
+ tabHandlers.onBrowserDeactivate();
527
+ tabHandlers.onTerminalDeactivate();
528
+ syncPanelChromeState();
529
+ }
530
+
531
+ export function toggleStudioNav(tab: string): void {
532
+ if (dom.appEl.classList.contains('studio-open') && state.studioTab === tab && !state.studioSplit) {
533
+ closeStudio();
534
+ } else {
535
+ openStudio(tab);
536
+ }
537
+ }
538
+
539
+ export function toggleStudioMaximize(): void {
540
+ if (!dom.appEl.classList.contains('studio-open')) return;
541
+ state.studioExpanded = !state.studioExpanded;
542
+ if (state.studioExpanded && window.innerWidth > 960 && window.innerWidth < 1380) {
543
+ dom.appEl.classList.add('sidebar-collapsed');
544
+ }
545
+ syncExpandedStudioWidth();
546
+ dom.appEl.classList.toggle('studio-expanded', state.studioExpanded);
547
+ syncPanelChromeState();
548
+ tabHandlers.onLayoutResized();
549
+ }
550
+
551
+ let resizing = false;
552
+ let snapClosePending = false;
553
+ let resizePointerId: number | null = null;
554
+ let resizeStartWidth = DEFAULT_STUDIO_W;
555
+
556
+ export function isStudioResizing(): boolean {
557
+ return resizing;
558
+ }
559
+
560
+ export function endStudioResize(cancelled: boolean): void {
561
+ if (!resizing) return;
562
+ resizing = false;
563
+ const shouldSnapClose = !cancelled && snapClosePending;
564
+ snapClosePending = false;
565
+ dom.appEl.classList.remove('studio-resizing');
566
+ dom.studioResizer.classList.remove('dragging', 'snap-close');
567
+ if (resizePointerId !== null) {
568
+ try { dom.studioResizer.releasePointerCapture(resizePointerId); } catch { /* ignore */ }
569
+ resizePointerId = null;
570
+ }
571
+ if (shouldSnapClose) {
572
+ applyStudioWidth(resizeStartWidth, false);
573
+ closeStudio();
574
+ } else if (cancelled) {
575
+ applyStudioWidth(resizeStartWidth, false);
576
+ tabHandlers.onLayoutResized();
577
+ } else {
578
+ applyStudioWidth(state.studioWidth, true);
579
+ tabHandlers.onLayoutResized();
580
+ }
581
+ }
582
+
583
+ export function bindStudioResizer(): void {
584
+ dom.studioResizer.addEventListener('pointerdown', (e) => {
585
+ if (!dom.appEl.classList.contains('studio-open') || state.studioExpanded) return;
586
+ e.preventDefault();
587
+ resizing = true;
588
+ snapClosePending = false;
589
+ resizeStartWidth = state.studioWidth || DEFAULT_STUDIO_W;
590
+ resizePointerId = e.pointerId;
591
+ dom.appEl.classList.add('studio-resizing');
592
+ dom.studioResizer.classList.add('dragging');
593
+ try { dom.studioResizer.setPointerCapture(e.pointerId); } catch { /* ignore */ }
594
+ });
595
+ dom.studioResizer.addEventListener('pointermove', (e) => {
596
+ if (!resizing) return;
597
+ const rawW = window.innerWidth - e.clientX;
598
+ if (rawW < 210) {
599
+ snapClosePending = true;
600
+ dom.studioResizer.classList.add('snap-close');
601
+ return;
602
+ }
603
+ snapClosePending = false;
604
+ dom.studioResizer.classList.remove('snap-close');
605
+ applyStudioWidth(rawW, false);
606
+ });
607
+ dom.studioResizer.addEventListener('pointerup', () => endStudioResize(false));
608
+ dom.studioResizer.addEventListener('pointercancel', () => endStudioResize(true));
609
+ dom.studioResizer.addEventListener('dblclick', () => {
610
+ if (!dom.appEl.classList.contains('studio-open')) return;
611
+ state.studioExpanded = false;
612
+ dom.appEl.classList.remove('studio-expanded');
613
+ applyStudioWidth(DEFAULT_STUDIO_W, true);
614
+ syncPanelChromeState();
615
+ tabHandlers.onLayoutResized();
616
+ });
617
+ window.addEventListener('resize', () => {
618
+ closeAllPopovers();
619
+ if (dom.appEl.classList.contains('studio-open') && window.innerWidth > 960) {
620
+ applyStudioWidth(state.studioWidth, false);
621
+ tabHandlers.onLayoutResized();
622
+ }
623
+ });
624
+ }
625
+
626
+ export function bindStudioSplitDivider(): void {
627
+ const divider = dom.studioSplitDivider;
628
+ const grid = dom.studioPanesGrid;
629
+ if (!divider || !grid) return;
630
+
631
+ let splitDragging = false;
632
+ let splitPointerId: number | null = null;
633
+
634
+ const stopSplitDrag = (persist: boolean): void => {
635
+ if (!splitDragging) return;
636
+ splitDragging = false;
637
+ dom.appEl.classList.remove('studio-resizing');
638
+ divider.classList.remove('dragging');
639
+ if (splitPointerId !== null) {
640
+ try { divider.releasePointerCapture(splitPointerId); } catch { /* ignore */ }
641
+ splitPointerId = null;
642
+ }
643
+ if (persist) persistCurrentLayout();
644
+ tabHandlers.onLayoutResized();
645
+ };
646
+
647
+ divider.addEventListener('pointerdown', (e) => {
648
+ if (!state.studioSplit) return;
649
+ e.preventDefault();
650
+ splitDragging = true;
651
+ splitPointerId = e.pointerId;
652
+ dom.appEl.classList.add('studio-resizing');
653
+ divider.classList.add('dragging');
654
+ try { divider.setPointerCapture(e.pointerId); } catch { /* ignore */ }
655
+ });
656
+ divider.addEventListener('pointermove', (e) => {
657
+ if (!splitDragging) return;
658
+ const rect = grid.getBoundingClientRect();
659
+ if (rect.width <= 0) return;
660
+ const rawRatio = (e.clientX - rect.left) / rect.width;
661
+ state.studioSplitRatio = clampSplitRatio(rawRatio);
662
+ applySplitRatioVar();
663
+ });
664
+ divider.addEventListener('pointerup', () => stopSplitDrag(true));
665
+ divider.addEventListener('pointercancel', () => stopSplitDrag(false));
666
+ divider.addEventListener('dblclick', () => {
667
+ state.studioSplitRatio = 0.5;
668
+ applySplitRatioVar();
669
+ persistCurrentLayout();
670
+ tabHandlers.onLayoutResized();
671
+ });
672
+ }