@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,280 @@
1
+ import { dom, maybeById } from './dom.js';
2
+ export const DRAFT_KEY = 'causyne/web/composerDraft/v1';
3
+ export const THEME_KEY = 'causyne/web/theme/v1';
4
+ export const DENSITY_KEY = 'causyne/web/density/v1';
5
+ export const AUTOSCROLL_KEY = 'causyne/web/autoScroll/v1';
6
+ export const PERMISSION_PRESET_KEY = 'causyne/web/permissionPreset/v1';
7
+ export const LEGACY_MODE_KEY = 'causyne/web/autonomyMode/v1';
8
+ export const STUDIO_WIDTH_KEY = 'causyne/web/studioWidth/v1';
9
+ export const DEFAULT_STUDIO_W = 480;
10
+ let autoScrollEnabled = true;
11
+ export function isAutoScrollEnabled() {
12
+ return autoScrollEnabled;
13
+ }
14
+ export function applyDensity(density) {
15
+ const next = density === 'compact' ? 'compact' : 'comfortable';
16
+ document.documentElement.setAttribute('data-density', next);
17
+ try {
18
+ window.localStorage.setItem(DENSITY_KEY, next);
19
+ }
20
+ catch { /* ignore */ }
21
+ }
22
+ export function applyAutoScrollPref(enabled) {
23
+ autoScrollEnabled = Boolean(enabled);
24
+ try {
25
+ window.localStorage.setItem(AUTOSCROLL_KEY, autoScrollEnabled ? '1' : '0');
26
+ }
27
+ catch { /* ignore */ }
28
+ }
29
+ const hashParams = new URLSearchParams((window.location.hash || '').replace(/^#/, ''));
30
+ export const TOKEN_STORE_KEY = `causyne/web/token:${window.location.origin}`;
31
+ let sessionToken = hashParams.get('token') || '';
32
+ if (sessionToken) {
33
+ try {
34
+ window.sessionStorage.setItem(TOKEN_STORE_KEY, sessionToken);
35
+ }
36
+ catch { /* ignore */ }
37
+ try {
38
+ window.history.replaceState(null, '', window.location.pathname + window.location.search);
39
+ }
40
+ catch { /* ignore */ }
41
+ }
42
+ else {
43
+ try {
44
+ sessionToken = window.sessionStorage.getItem(TOKEN_STORE_KEY) || '';
45
+ }
46
+ catch { /* ignore */ }
47
+ }
48
+ let apiBase = '';
49
+ try {
50
+ const qs = new URLSearchParams(window.location.search || '');
51
+ const bridge = (qs.get('bridge') || '').trim();
52
+ if (bridge) {
53
+ const parsed = new URL(bridge);
54
+ const host = parsed.hostname.toLowerCase();
55
+ if (parsed.protocol === 'http:' && (host === '127.0.0.1' || host === 'localhost')) {
56
+ apiBase = parsed.origin;
57
+ }
58
+ }
59
+ }
60
+ catch {
61
+ // Ignore invalid bridge parameter.
62
+ }
63
+ export function apiUrl(p) {
64
+ return apiBase + p;
65
+ }
66
+ export function authHeaders(extra) {
67
+ return Object.assign({ Authorization: `Bearer ${sessionToken}` }, extra || {});
68
+ }
69
+ export const state = {
70
+ sessionId: null,
71
+ sessions: [],
72
+ models: null,
73
+ modelTab: null,
74
+ modelQuery: '',
75
+ modelExpandedPeers: new Set(),
76
+ modelSelectedIndex: 0,
77
+ modelRefreshing: false,
78
+ modelLiveHydrated: false,
79
+ modelNotice: '',
80
+ activeProvider: 'unconfigured',
81
+ activeModel: 'default',
82
+ permissionPreset: 'workspace-write',
83
+ defaultPermissionPreset: 'workspace-write',
84
+ workspaceRoot: '',
85
+ lastScrollTop: 0,
86
+ attachments: [],
87
+ currentDir: '',
88
+ parentDir: '',
89
+ studioTab: 'start',
90
+ studioOpenTabs: ['start'],
91
+ studioSplit: false,
92
+ studioSecondaryTab: 'terminal',
93
+ studioSplitRatio: 0.5,
94
+ studioExpanded: false,
95
+ studioWidth: DEFAULT_STUDIO_W,
96
+ terminalSessions: [],
97
+ activeTerminalId: null,
98
+ availableShells: [],
99
+ canvasPreviewMode: false,
100
+ canvasDirty: false,
101
+ artifactVersions: new Map(),
102
+ sessionWorkspaces: new Map(),
103
+ sessionSwitchSeq: 0,
104
+ usageTokens: 0,
105
+ diffScope: 'session',
106
+ browserEngine: 'agent',
107
+ browserMode: 'empty',
108
+ browserUrl: '',
109
+ browserSrcdoc: '',
110
+ browserViewport: 'desktop',
111
+ browserFrameStrictSandbox: false,
112
+ liveBrowserActive: false,
113
+ liveBrowserVisible: false,
114
+ liveBrowserRuntime: null,
115
+ liveBrowserPref: 'auto',
116
+ liveStreamMode: 'idle',
117
+ liveStreamAbort: null,
118
+ liveStreamConnected: false,
119
+ liveFps: 0,
120
+ pointerDragging: false,
121
+ liveBrowserWidth: 1280,
122
+ liveBrowserHeight: 800,
123
+ controlOwner: 'human',
124
+ cliCommands: [],
125
+ mentionMode: 'file',
126
+ mentionItems: [],
127
+ mentionIndex: 0,
128
+ mentionStart: -1,
129
+ diffStaged: false,
130
+ isComposing: false,
131
+ stickToBottom: true,
132
+ activeController: null,
133
+ activeTurn: null,
134
+ activePrompt: '',
135
+ gameStatus: null,
136
+ gameLaunching: false
137
+ };
138
+ export function setLiveness(active, label) {
139
+ const isActive = Boolean(active);
140
+ dom.livenessPill.classList.toggle('active', isActive);
141
+ dom.appEl.classList.toggle('is-processing', isActive);
142
+ if (label)
143
+ dom.livenessText.textContent = label;
144
+ if (state.activeTurn && typeof state.activeTurn.setActivity === 'function') {
145
+ state.activeTurn.setActivity(isActive, label || 'Thinking...');
146
+ }
147
+ }
148
+ export function applyTheme(theme) {
149
+ const next = theme === 'light' ? 'light' : 'dark';
150
+ document.documentElement.setAttribute('data-theme', next);
151
+ if (dom.themeToggleBtn)
152
+ dom.themeToggleBtn.textContent = next === 'light' ? 'Dark' : 'Light';
153
+ try {
154
+ window.localStorage.setItem(THEME_KEY, next);
155
+ }
156
+ catch { /* ignore */ }
157
+ }
158
+ export function clampStudioWidth(raw) {
159
+ const n = Number(raw);
160
+ const sidebarW = dom.appEl.classList.contains('sidebar-collapsed') || window.innerWidth <= 960 ? 0 : 256;
161
+ const minW = 320;
162
+ const maxByChat = Math.max(minW, window.innerWidth - sidebarW - 360);
163
+ const maxW = Math.max(minW, Math.min(Math.floor(window.innerWidth * 0.72), maxByChat));
164
+ const base = Number.isFinite(n) && n > 0 ? Math.round(n) : DEFAULT_STUDIO_W;
165
+ return Math.max(minW, Math.min(maxW, base));
166
+ }
167
+ export function syncExpandedStudioWidth() {
168
+ const sidebarW = dom.appEl.classList.contains('sidebar-collapsed') || window.innerWidth <= 960 ? 0 : 256;
169
+ const available = Math.max(360, window.innerWidth - sidebarW - 380);
170
+ const expandedW = Math.min(1040, Math.max(360, available));
171
+ document.documentElement.style.setProperty('--studio-expanded-w', `${expandedW}px`);
172
+ }
173
+ export function applyStudioWidth(px, persist) {
174
+ const clamped = clampStudioWidth(px);
175
+ state.studioWidth = clamped;
176
+ document.documentElement.style.setProperty('--studio-w', `${clamped}px`);
177
+ syncExpandedStudioWidth();
178
+ if (dom.studioRoot) {
179
+ dom.studioRoot.style.flexBasis = '';
180
+ dom.studioRoot.style.width = '';
181
+ }
182
+ if (persist) {
183
+ try {
184
+ window.localStorage.setItem(STUDIO_WIDTH_KEY, String(clamped));
185
+ }
186
+ catch { /* ignore */ }
187
+ }
188
+ }
189
+ export function setStageMode(hasMessages) {
190
+ dom.stageEl.classList.toggle('is-empty', !hasMessages);
191
+ dom.stageEl.classList.toggle('has-messages', hasMessages);
192
+ }
193
+ export function updateComposerMetrics() {
194
+ dom.promptInput.style.height = 'auto';
195
+ const nextH = Math.min(200, Math.max(26, dom.promptInput.scrollHeight));
196
+ dom.promptInput.style.height = `${nextH}px`;
197
+ const hasText = Boolean((dom.promptInput.value || '').trim());
198
+ const streaming = state.activeController !== null;
199
+ dom.sendBtn.classList.toggle('ready', !streaming && hasText);
200
+ dom.sendBtn.classList.toggle('stopping', streaming);
201
+ dom.sendBtn.setAttribute('aria-label', streaming ? 'Stop response' : 'Send message');
202
+ const iconSend = maybeById('icon-send');
203
+ const iconStop = maybeById('icon-stop');
204
+ if (iconSend)
205
+ iconSend.style.display = streaming ? 'none' : 'block';
206
+ if (iconStop)
207
+ iconStop.style.display = streaming ? 'block' : 'none';
208
+ try {
209
+ if (dom.promptInput.value)
210
+ window.localStorage.setItem(DRAFT_KEY, dom.promptInput.value);
211
+ else
212
+ window.localStorage.removeItem(DRAFT_KEY);
213
+ }
214
+ catch { /* ignore */ }
215
+ }
216
+ export function scrollToBottom(force, smooth = false) {
217
+ if (!dom.threadWrapEl)
218
+ return;
219
+ if (force || (autoScrollEnabled && state.stickToBottom)) {
220
+ if (smooth && typeof dom.threadWrapEl.scrollTo === 'function') {
221
+ dom.threadWrapEl.scrollTo({ top: dom.threadWrapEl.scrollHeight, behavior: 'smooth' });
222
+ }
223
+ else {
224
+ dom.threadWrapEl.scrollTop = dom.threadWrapEl.scrollHeight;
225
+ }
226
+ state.lastScrollTop = dom.threadWrapEl.scrollTop;
227
+ state.stickToBottom = true;
228
+ dom.scrollBottomBtn.classList.remove('visible');
229
+ }
230
+ }
231
+ export function syncUsagePill(tokens) {
232
+ state.usageTokens = Number(tokens) > 0 ? Number(tokens) : 0;
233
+ if (dom.usagePill) {
234
+ if (state.usageTokens > 0) {
235
+ dom.usagePill.textContent = `${state.usageTokens.toLocaleString()} tok`;
236
+ dom.usagePill.classList.add('visible');
237
+ }
238
+ else {
239
+ dom.usagePill.textContent = '';
240
+ dom.usagePill.classList.remove('visible');
241
+ }
242
+ }
243
+ }
244
+ export function restorePersistedPreferences() {
245
+ try {
246
+ const savedTheme = window.localStorage.getItem(THEME_KEY);
247
+ if (savedTheme === 'light' || savedTheme === 'dark')
248
+ applyTheme(savedTheme);
249
+ const savedDensity = window.localStorage.getItem(DENSITY_KEY);
250
+ if (savedDensity === 'compact' || savedDensity === 'comfortable')
251
+ applyDensity(savedDensity);
252
+ const savedAutoScroll = window.localStorage.getItem(AUTOSCROLL_KEY);
253
+ if (savedAutoScroll === '0' || savedAutoScroll === '1')
254
+ applyAutoScrollPref(savedAutoScroll === '1');
255
+ const savedPreset = window.localStorage.getItem(PERMISSION_PRESET_KEY);
256
+ if (savedPreset === 'read-only' || savedPreset === 'workspace-write' || savedPreset === 'danger-full-access') {
257
+ state.permissionPreset = savedPreset;
258
+ state.defaultPermissionPreset = savedPreset;
259
+ }
260
+ else {
261
+ const legacyMode = window.localStorage.getItem(LEGACY_MODE_KEY);
262
+ if (legacyMode === 'A')
263
+ state.permissionPreset = 'read-only';
264
+ else if (legacyMode === 'C')
265
+ state.permissionPreset = 'danger-full-access';
266
+ else if (legacyMode === 'B')
267
+ state.permissionPreset = 'workspace-write';
268
+ state.defaultPermissionPreset = state.permissionPreset;
269
+ }
270
+ const savedDraft = window.localStorage.getItem(DRAFT_KEY);
271
+ if (savedDraft)
272
+ dom.promptInput.value = savedDraft;
273
+ const savedW = window.localStorage.getItem(STUDIO_WIDTH_KEY);
274
+ if (savedW)
275
+ applyStudioWidth(savedW, false);
276
+ }
277
+ catch { /* ignore */ }
278
+ if (window.innerWidth <= 960)
279
+ dom.appEl.classList.add('sidebar-collapsed');
280
+ }
@@ -0,0 +1,259 @@
1
+ import { dom, maybeById } from './dom.js';
2
+ import type { AppState } from './types.js';
3
+
4
+ export const DRAFT_KEY = 'causyne/web/composerDraft/v1';
5
+ export const THEME_KEY = 'causyne/web/theme/v1';
6
+ export const DENSITY_KEY = 'causyne/web/density/v1';
7
+ export const AUTOSCROLL_KEY = 'causyne/web/autoScroll/v1';
8
+ export const PERMISSION_PRESET_KEY = 'causyne/web/permissionPreset/v1';
9
+ export const LEGACY_MODE_KEY = 'causyne/web/autonomyMode/v1';
10
+ export const STUDIO_WIDTH_KEY = 'causyne/web/studioWidth/v1';
11
+ export const DEFAULT_STUDIO_W = 480;
12
+
13
+ let autoScrollEnabled = true;
14
+
15
+ export function isAutoScrollEnabled(): boolean {
16
+ return autoScrollEnabled;
17
+ }
18
+
19
+ export function applyDensity(density: string): void {
20
+ const next = density === 'compact' ? 'compact' : 'comfortable';
21
+ document.documentElement.setAttribute('data-density', next);
22
+ try { window.localStorage.setItem(DENSITY_KEY, next); } catch { /* ignore */ }
23
+ }
24
+
25
+ export function applyAutoScrollPref(enabled: boolean): void {
26
+ autoScrollEnabled = Boolean(enabled);
27
+ try { window.localStorage.setItem(AUTOSCROLL_KEY, autoScrollEnabled ? '1' : '0'); } catch { /* ignore */ }
28
+ }
29
+
30
+ const hashParams = new URLSearchParams((window.location.hash || '').replace(/^#/, ''));
31
+ export const TOKEN_STORE_KEY = `causyne/web/token:${window.location.origin}`;
32
+
33
+ let sessionToken = hashParams.get('token') || '';
34
+ if (sessionToken) {
35
+ try { window.sessionStorage.setItem(TOKEN_STORE_KEY, sessionToken); } catch { /* ignore */ }
36
+ try { window.history.replaceState(null, '', window.location.pathname + window.location.search); } catch { /* ignore */ }
37
+ } else {
38
+ try { sessionToken = window.sessionStorage.getItem(TOKEN_STORE_KEY) || ''; } catch { /* ignore */ }
39
+ }
40
+
41
+ let apiBase = '';
42
+ try {
43
+ const qs = new URLSearchParams(window.location.search || '');
44
+ const bridge = (qs.get('bridge') || '').trim();
45
+ if (bridge) {
46
+ const parsed = new URL(bridge);
47
+ const host = parsed.hostname.toLowerCase();
48
+ if (parsed.protocol === 'http:' && (host === '127.0.0.1' || host === 'localhost')) {
49
+ apiBase = parsed.origin;
50
+ }
51
+ }
52
+ } catch {
53
+ // Ignore invalid bridge parameter.
54
+ }
55
+
56
+ export function apiUrl(p: string): string {
57
+ return apiBase + p;
58
+ }
59
+
60
+ export function authHeaders(extra?: Record<string, string>): Record<string, string> {
61
+ return Object.assign({ Authorization: `Bearer ${sessionToken}` }, extra || {});
62
+ }
63
+
64
+ export const state: AppState = {
65
+ sessionId: null,
66
+ sessions: [],
67
+ models: null,
68
+ modelTab: null,
69
+ modelQuery: '',
70
+ modelExpandedPeers: new Set<string>(),
71
+ modelSelectedIndex: 0,
72
+ modelRefreshing: false,
73
+ modelLiveHydrated: false,
74
+ modelNotice: '',
75
+ activeProvider: 'unconfigured',
76
+ activeModel: 'default',
77
+ permissionPreset: 'workspace-write',
78
+ defaultPermissionPreset: 'workspace-write',
79
+ workspaceRoot: '',
80
+ lastScrollTop: 0,
81
+ attachments: [],
82
+ currentDir: '',
83
+ parentDir: '',
84
+ studioTab: 'start',
85
+ studioOpenTabs: ['start'],
86
+ studioSplit: false,
87
+ studioSecondaryTab: 'terminal',
88
+ studioSplitRatio: 0.5,
89
+ studioExpanded: false,
90
+ studioWidth: DEFAULT_STUDIO_W,
91
+ terminalSessions: [],
92
+ activeTerminalId: null,
93
+ availableShells: [],
94
+ canvasPreviewMode: false,
95
+ canvasDirty: false,
96
+ artifactVersions: new Map(),
97
+ sessionWorkspaces: new Map(),
98
+ sessionSwitchSeq: 0,
99
+ usageTokens: 0,
100
+ diffScope: 'session',
101
+ browserEngine: 'agent',
102
+ browserMode: 'empty',
103
+ browserUrl: '',
104
+ browserSrcdoc: '',
105
+ browserViewport: 'desktop',
106
+ browserFrameStrictSandbox: false,
107
+ liveBrowserActive: false,
108
+ liveBrowserVisible: false,
109
+ liveBrowserRuntime: null,
110
+ liveBrowserPref: 'auto',
111
+ liveStreamMode: 'idle',
112
+ liveStreamAbort: null,
113
+ liveStreamConnected: false,
114
+ liveFps: 0,
115
+ pointerDragging: false,
116
+ liveBrowserWidth: 1280,
117
+ liveBrowserHeight: 800,
118
+ controlOwner: 'human',
119
+ cliCommands: [],
120
+ mentionMode: 'file',
121
+ mentionItems: [],
122
+ mentionIndex: 0,
123
+ mentionStart: -1,
124
+ diffStaged: false,
125
+ isComposing: false,
126
+ stickToBottom: true,
127
+ activeController: null,
128
+ activeTurn: null,
129
+ activePrompt: '',
130
+ gameStatus: null,
131
+ gameLaunching: false
132
+ };
133
+
134
+ export function setLiveness(active: boolean, label: string): void {
135
+ const isActive = Boolean(active);
136
+ dom.livenessPill.classList.toggle('active', isActive);
137
+ dom.appEl.classList.toggle('is-processing', isActive);
138
+ if (label) dom.livenessText.textContent = label;
139
+ if (state.activeTurn && typeof state.activeTurn.setActivity === 'function') {
140
+ state.activeTurn.setActivity(isActive, label || 'Thinking...');
141
+ }
142
+ }
143
+
144
+ export function applyTheme(theme: string): void {
145
+ const next = theme === 'light' ? 'light' : 'dark';
146
+ document.documentElement.setAttribute('data-theme', next);
147
+ if (dom.themeToggleBtn) dom.themeToggleBtn.textContent = next === 'light' ? 'Dark' : 'Light';
148
+ try { window.localStorage.setItem(THEME_KEY, next); } catch { /* ignore */ }
149
+ }
150
+
151
+ export function clampStudioWidth(raw: unknown): number {
152
+ const n = Number(raw);
153
+ const sidebarW = dom.appEl.classList.contains('sidebar-collapsed') || window.innerWidth <= 960 ? 0 : 256;
154
+ const minW = 320;
155
+ const maxByChat = Math.max(minW, window.innerWidth - sidebarW - 360);
156
+ const maxW = Math.max(minW, Math.min(Math.floor(window.innerWidth * 0.72), maxByChat));
157
+ const base = Number.isFinite(n) && n > 0 ? Math.round(n) : DEFAULT_STUDIO_W;
158
+ return Math.max(minW, Math.min(maxW, base));
159
+ }
160
+
161
+ export function syncExpandedStudioWidth(): void {
162
+ const sidebarW = dom.appEl.classList.contains('sidebar-collapsed') || window.innerWidth <= 960 ? 0 : 256;
163
+ const available = Math.max(360, window.innerWidth - sidebarW - 380);
164
+ const expandedW = Math.min(1040, Math.max(360, available));
165
+ document.documentElement.style.setProperty('--studio-expanded-w', `${expandedW}px`);
166
+ }
167
+
168
+ export function applyStudioWidth(px: number | string, persist: boolean): void {
169
+ const clamped = clampStudioWidth(px);
170
+ state.studioWidth = clamped;
171
+ document.documentElement.style.setProperty('--studio-w', `${clamped}px`);
172
+ syncExpandedStudioWidth();
173
+ if (dom.studioRoot) {
174
+ dom.studioRoot.style.flexBasis = '';
175
+ dom.studioRoot.style.width = '';
176
+ }
177
+ if (persist) {
178
+ try { window.localStorage.setItem(STUDIO_WIDTH_KEY, String(clamped)); } catch { /* ignore */ }
179
+ }
180
+ }
181
+
182
+ export function setStageMode(hasMessages: boolean): void {
183
+ dom.stageEl.classList.toggle('is-empty', !hasMessages);
184
+ dom.stageEl.classList.toggle('has-messages', hasMessages);
185
+ }
186
+
187
+ export function updateComposerMetrics(): void {
188
+ dom.promptInput.style.height = 'auto';
189
+ const nextH = Math.min(200, Math.max(26, dom.promptInput.scrollHeight));
190
+ dom.promptInput.style.height = `${nextH}px`;
191
+ const hasText = Boolean((dom.promptInput.value || '').trim());
192
+ const streaming = state.activeController !== null;
193
+ dom.sendBtn.classList.toggle('ready', !streaming && hasText);
194
+ dom.sendBtn.classList.toggle('stopping', streaming);
195
+ dom.sendBtn.setAttribute('aria-label', streaming ? 'Stop response' : 'Send message');
196
+ const iconSend = maybeById('icon-send');
197
+ const iconStop = maybeById('icon-stop');
198
+ if (iconSend) iconSend.style.display = streaming ? 'none' : 'block';
199
+ if (iconStop) iconStop.style.display = streaming ? 'block' : 'none';
200
+ try {
201
+ if (dom.promptInput.value) window.localStorage.setItem(DRAFT_KEY, dom.promptInput.value);
202
+ else window.localStorage.removeItem(DRAFT_KEY);
203
+ } catch { /* ignore */ }
204
+ }
205
+
206
+ export function scrollToBottom(force: boolean, smooth = false): void {
207
+ if (!dom.threadWrapEl) return;
208
+ if (force || (autoScrollEnabled && state.stickToBottom)) {
209
+ if (smooth && typeof dom.threadWrapEl.scrollTo === 'function') {
210
+ dom.threadWrapEl.scrollTo({ top: dom.threadWrapEl.scrollHeight, behavior: 'smooth' });
211
+ } else {
212
+ dom.threadWrapEl.scrollTop = dom.threadWrapEl.scrollHeight;
213
+ }
214
+ state.lastScrollTop = dom.threadWrapEl.scrollTop;
215
+ state.stickToBottom = true;
216
+ dom.scrollBottomBtn.classList.remove('visible');
217
+ }
218
+ }
219
+
220
+ export function syncUsagePill(tokens: number): void {
221
+ state.usageTokens = Number(tokens) > 0 ? Number(tokens) : 0;
222
+ if (dom.usagePill) {
223
+ if (state.usageTokens > 0) {
224
+ dom.usagePill.textContent = `${state.usageTokens.toLocaleString()} tok`;
225
+ dom.usagePill.classList.add('visible');
226
+ } else {
227
+ dom.usagePill.textContent = '';
228
+ dom.usagePill.classList.remove('visible');
229
+ }
230
+ }
231
+ }
232
+
233
+ export function restorePersistedPreferences(): void {
234
+ try {
235
+ const savedTheme = window.localStorage.getItem(THEME_KEY);
236
+ if (savedTheme === 'light' || savedTheme === 'dark') applyTheme(savedTheme);
237
+ const savedDensity = window.localStorage.getItem(DENSITY_KEY);
238
+ if (savedDensity === 'compact' || savedDensity === 'comfortable') applyDensity(savedDensity);
239
+ const savedAutoScroll = window.localStorage.getItem(AUTOSCROLL_KEY);
240
+ if (savedAutoScroll === '0' || savedAutoScroll === '1') applyAutoScrollPref(savedAutoScroll === '1');
241
+ const savedPreset = window.localStorage.getItem(PERMISSION_PRESET_KEY);
242
+ if (savedPreset === 'read-only' || savedPreset === 'workspace-write' || savedPreset === 'danger-full-access') {
243
+ state.permissionPreset = savedPreset;
244
+ state.defaultPermissionPreset = savedPreset;
245
+ } else {
246
+ const legacyMode = window.localStorage.getItem(LEGACY_MODE_KEY);
247
+ if (legacyMode === 'A') state.permissionPreset = 'read-only';
248
+ else if (legacyMode === 'C') state.permissionPreset = 'danger-full-access';
249
+ else if (legacyMode === 'B') state.permissionPreset = 'workspace-write';
250
+ state.defaultPermissionPreset = state.permissionPreset;
251
+ }
252
+ const savedDraft = window.localStorage.getItem(DRAFT_KEY);
253
+ if (savedDraft) dom.promptInput.value = savedDraft;
254
+ const savedW = window.localStorage.getItem(STUDIO_WIDTH_KEY);
255
+ if (savedW) applyStudioWidth(savedW, false);
256
+ } catch { /* ignore */ }
257
+
258
+ if (window.innerWidth <= 960) dom.appEl.classList.add('sidebar-collapsed');
259
+ }
@@ -0,0 +1,127 @@
1
+ function solveBarrierBisect(fn, lo, hi) {
2
+ if (lo >= hi)
3
+ return hi;
4
+ let a = lo;
5
+ let b = hi;
6
+ if (fn(a) >= 0)
7
+ return a;
8
+ if (fn(b) <= 0)
9
+ return b;
10
+ for (let k = 0; k < 22 && (b - a) > 0.05; k += 1) {
11
+ const mid = (a + b) * 0.5;
12
+ if (fn(mid) <= 0)
13
+ a = mid;
14
+ else
15
+ b = mid;
16
+ }
17
+ return a;
18
+ }
19
+ export function createStreamSmoother(onRender) {
20
+ let fullText = '';
21
+ let shownLen = 0;
22
+ let vel = 120;
23
+ let lastT = 0;
24
+ let startMs = 0;
25
+ const arrivals = [[-9999, 0]];
26
+ let done = false;
27
+ let rafId = 0;
28
+ const alpha = 0.99;
29
+ const gamma = 1e-5;
30
+ const reducedMotion = Boolean(window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches);
31
+ function step() {
32
+ rafId = 0;
33
+ const total = fullText.length;
34
+ if (reducedMotion || total === 0) {
35
+ shownLen = total;
36
+ onRender(fullText, !done, shownLen);
37
+ return;
38
+ }
39
+ if (shownLen >= total) {
40
+ if (done)
41
+ onRender(fullText, false, total);
42
+ return;
43
+ }
44
+ const nowSec = (performance.now() - startMs) / 1000;
45
+ const dt = Math.max(nowSec - lastT, 1e-4);
46
+ const upper = total + (done ? 120 : 0);
47
+ const lagTarget = 0.9 * nowSec - 0.25;
48
+ let minChars = 0;
49
+ for (let idx = arrivals.length - 1; idx >= 0; idx -= 1) {
50
+ const pair = arrivals[idx];
51
+ if (pair[0] < lagTarget) {
52
+ minChars = pair[1];
53
+ break;
54
+ }
55
+ }
56
+ const lo = Math.max(shownLen, Math.min(total, done ? total : minChars));
57
+ const nextX = done && (total - shownLen) <= 18
58
+ ? total
59
+ : solveBarrierBisect((x) => {
60
+ const d = (x - shownLen) / dt;
61
+ return 2 * (done ? gamma * 0.01 : gamma) * (1 / dt) * (d - vel)
62
+ - 1 / Math.max(1e-4, x - lo)
63
+ + 1 / Math.max(1e-4, upper - x);
64
+ }, lo, upper);
65
+ const clamped = Math.min(total, Math.max(shownLen + 1, Math.round(nextX)));
66
+ const instV = (clamped - shownLen) / dt;
67
+ vel = Math.max(45, Math.min(2400, alpha * vel + (1 - alpha) * instV));
68
+ shownLen = clamped;
69
+ lastT = nowSec;
70
+ onRender(fullText.slice(0, shownLen), !done || shownLen < total, shownLen);
71
+ if (shownLen < total)
72
+ rafId = window.requestAnimationFrame(step);
73
+ else if (done)
74
+ onRender(fullText, false, total);
75
+ }
76
+ function schedule() {
77
+ if (!rafId)
78
+ rafId = window.requestAnimationFrame(step);
79
+ }
80
+ return {
81
+ push(delta) {
82
+ if (!delta)
83
+ return;
84
+ if (!startMs) {
85
+ startMs = performance.now();
86
+ lastT = 0;
87
+ }
88
+ fullText += delta;
89
+ const tSec = (performance.now() - startMs) / 1000;
90
+ arrivals.push([tSec, fullText.length]);
91
+ if (arrivals.length > 120)
92
+ arrivals.shift();
93
+ schedule();
94
+ },
95
+ setFull(text, streaming) {
96
+ fullText = String(text || '');
97
+ if (!streaming) {
98
+ if (rafId) {
99
+ window.cancelAnimationFrame(rafId);
100
+ rafId = 0;
101
+ }
102
+ done = true;
103
+ shownLen = fullText.length;
104
+ onRender(fullText, false, shownLen);
105
+ return;
106
+ }
107
+ if (!startMs) {
108
+ startMs = performance.now();
109
+ lastT = 0;
110
+ }
111
+ arrivals.push([(performance.now() - startMs) / 1000, fullText.length]);
112
+ schedule();
113
+ },
114
+ flush() {
115
+ done = true;
116
+ if (rafId) {
117
+ window.cancelAnimationFrame(rafId);
118
+ rafId = 0;
119
+ }
120
+ shownLen = fullText.length;
121
+ onRender(fullText, false, shownLen);
122
+ },
123
+ getText() {
124
+ return fullText;
125
+ }
126
+ };
127
+ }