@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,249 @@
1
+ const ANSI_KEYS = [
2
+ 'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white',
3
+ 'brightBlack', 'brightRed', 'brightGreen', 'brightYellow', 'brightBlue', 'brightMagenta', 'brightCyan', 'brightWhite'
4
+ ];
5
+ const SPECIAL_KEYS = ['foreground', 'background', 'cursor'];
6
+ const DARK_ANSI_PALETTE = {
7
+ black: '#1c201c',
8
+ red: '#f87171',
9
+ green: '#4ade80',
10
+ yellow: '#facc15',
11
+ blue: '#60a5fa',
12
+ magenta: '#c084fc',
13
+ cyan: '#22d3ee',
14
+ white: '#e6ece4',
15
+ brightBlack: '#6b736a',
16
+ brightRed: '#fca5a5',
17
+ brightGreen: '#86efac',
18
+ brightYellow: '#fde047',
19
+ brightBlue: '#93c5fd',
20
+ brightMagenta: '#d8b4fe',
21
+ brightCyan: '#67e8f9',
22
+ brightWhite: '#ffffff'
23
+ };
24
+ const LIGHT_ANSI_PALETTE = {
25
+ black: '#1f2328',
26
+ red: '#cf222e',
27
+ green: '#116329',
28
+ yellow: '#9a6700',
29
+ blue: '#0969da',
30
+ magenta: '#8250df',
31
+ cyan: '#1b7c83',
32
+ white: '#6e7781',
33
+ brightBlack: '#57606a',
34
+ brightRed: '#a40e26',
35
+ brightGreen: '#1a7f37',
36
+ brightYellow: '#bf8700',
37
+ brightBlue: '#218bff',
38
+ brightMagenta: '#a475f9',
39
+ brightCyan: '#3192aa',
40
+ brightWhite: '#8c959f'
41
+ };
42
+ function colorIndex(value) {
43
+ if (!/^\d{1,3}$/.test(value))
44
+ return undefined;
45
+ const parsed = Number(value);
46
+ return parsed >= 0 && parsed < 256 ? parsed : undefined;
47
+ }
48
+ function oscColor(value) {
49
+ const rgb = /^rgb:([\da-f]{1,4})\/([\da-f]{1,4})\/([\da-f]{1,4})$/i.exec(value);
50
+ let channels;
51
+ if (rgb !== null) {
52
+ const r = rgb[1];
53
+ const g = rgb[2];
54
+ const b = rgb[3];
55
+ if (!r || !g || !b)
56
+ return undefined;
57
+ channels = [r, g, b];
58
+ if (!channels.every((c) => c.length === r.length))
59
+ return undefined;
60
+ }
61
+ else {
62
+ if (!/^#(?:[\da-f]{3}|[\da-f]{6}|[\da-f]{9}|[\da-f]{12})$/i.test(value))
63
+ return undefined;
64
+ const width = (value.length - 1) / 3;
65
+ channels = [
66
+ value.slice(1, width + 1),
67
+ value.slice(width + 1, 2 * width + 1),
68
+ value.slice(2 * width + 1)
69
+ ];
70
+ }
71
+ const hex = channels.map((channel) => {
72
+ const n = rgb === null
73
+ ? Number.parseInt(channel.padEnd(2, '0').slice(0, 2), 16)
74
+ : Math.round((Number.parseInt(channel, 16) * 255) / (16 ** channel.length - 1));
75
+ return n.toString(16).padStart(2, '0');
76
+ }).join('');
77
+ return `#${hex}`;
78
+ }
79
+ /**
80
+ * Tracks application OSC color overrides (OSC 4/10/11/12/104/110-112) separately from
81
+ * Causyne Studio theme defaults so light/dark theme switches preserve program palettes.
82
+ */
83
+ export class TerminalTheme {
84
+ terminal;
85
+ defaults = {};
86
+ indexed = new Map();
87
+ special = new Map();
88
+ subscriptions;
89
+ constructor(terminal) {
90
+ this.terminal = terminal;
91
+ const observe = (code, change) => terminal.parser.registerOscHandler(code, (data) => {
92
+ change(data);
93
+ return false;
94
+ });
95
+ this.subscriptions = [
96
+ observe(4, (data) => {
97
+ const parts = data.split(';');
98
+ for (let i = 0; i + 1 < parts.length; i += 2) {
99
+ const idxRaw = parts[i];
100
+ const colRaw = parts[i + 1];
101
+ if (idxRaw === undefined || colRaw === undefined)
102
+ continue;
103
+ const index = colorIndex(idxRaw);
104
+ const color = oscColor(colRaw);
105
+ if (index !== undefined && color !== undefined)
106
+ this.indexed.set(index, color);
107
+ }
108
+ }),
109
+ observe(104, (data) => {
110
+ if (data === '') {
111
+ this.indexed.clear();
112
+ }
113
+ else {
114
+ for (const part of data.split(';')) {
115
+ const index = colorIndex(part);
116
+ if (index !== undefined)
117
+ this.indexed.delete(index);
118
+ }
119
+ }
120
+ this.apply();
121
+ }),
122
+ ...SPECIAL_KEYS.flatMap((key, offset) => [
123
+ observe(10 + offset, (data) => {
124
+ const parts = data.split(';');
125
+ for (let i = 0; i < parts.length; i += 1) {
126
+ const target = SPECIAL_KEYS[offset + i];
127
+ const part = parts[i];
128
+ if (target === undefined || part === undefined)
129
+ continue;
130
+ const color = oscColor(part);
131
+ if (color !== undefined)
132
+ this.special.set(target, color);
133
+ }
134
+ }),
135
+ observe(110 + offset, () => {
136
+ this.special.delete(key);
137
+ this.apply();
138
+ })
139
+ ])
140
+ ];
141
+ }
142
+ update(background, foreground, isLight) {
143
+ const basePalette = isLight ? LIGHT_ANSI_PALETTE : DARK_ANSI_PALETTE;
144
+ if (this.defaults.background === background
145
+ && this.defaults.foreground === foreground
146
+ && this.defaults.blue === basePalette.blue) {
147
+ return;
148
+ }
149
+ this.defaults = {
150
+ ...basePalette,
151
+ background,
152
+ foreground,
153
+ cursor: foreground,
154
+ cursorAccent: background,
155
+ selectionBackground: isLight ? 'rgba(9, 105, 218, 0.24)' : 'rgba(147, 197, 253, 0.28)',
156
+ selectionInactiveBackground: isLight ? 'rgba(9, 105, 218, 0.14)' : 'rgba(147, 197, 253, 0.16)'
157
+ };
158
+ this.apply();
159
+ }
160
+ get cursor() {
161
+ return this.special.get('cursor') ?? this.defaults.cursor ?? '#e6ece4';
162
+ }
163
+ dispose() {
164
+ for (const sub of this.subscriptions)
165
+ sub.dispose();
166
+ }
167
+ apply() {
168
+ const theme = { ...this.defaults };
169
+ for (const [k, v] of this.special) {
170
+ theme[k] = v;
171
+ }
172
+ for (const [index, color] of this.indexed) {
173
+ const key = ANSI_KEYS[index];
174
+ if (key !== undefined) {
175
+ theme[key] = color;
176
+ }
177
+ else {
178
+ const ext = theme.extendedAnsi ?? [];
179
+ ext[index - ANSI_KEYS.length] = color;
180
+ theme.extendedAnsi = ext;
181
+ }
182
+ }
183
+ this.terminal.options.theme = theme;
184
+ }
185
+ }
186
+ function colorChannels(color) {
187
+ if (color.startsWith('#') && color.length >= 7) {
188
+ return [
189
+ Number.parseInt(color.slice(1, 3), 16),
190
+ Number.parseInt(color.slice(3, 5), 16),
191
+ Number.parseInt(color.slice(5, 7), 16)
192
+ ];
193
+ }
194
+ const matches = color.match(/\d+(?:\.\d+)?/g);
195
+ return matches ? matches.map(Number) : [0, 0, 0];
196
+ }
197
+ function luminance(color) {
198
+ const channels = colorChannels(color);
199
+ const r = (channels[0] ?? 0) / 255;
200
+ const g = (channels[1] ?? 0) / 255;
201
+ const b = (channels[2] ?? 0) / 255;
202
+ const lin = (v) => (v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4);
203
+ return 0.2126 * lin(r) + 0.7152 * lin(g) + 0.0722 * lin(b);
204
+ }
205
+ function opaqueColor(color, background) {
206
+ const channels = colorChannels(color);
207
+ const alpha = channels[3] ?? 1;
208
+ if (alpha >= 1)
209
+ return color;
210
+ const surface = colorChannels(background());
211
+ const rgb = [0, 1, 2].map((i) => Math.round((channels[i] ?? 0) * alpha + (surface[i] ?? 0) * (1 - alpha)));
212
+ return `rgb(${rgb.join(', ')})`;
213
+ }
214
+ /**
215
+ * Keeps the xterm DOM cursor legible against default, ANSI, true-color, and inverse cells.
216
+ */
217
+ export function observeTerminalCursor(terminal, node, preferredCursor) {
218
+ return terminal.onRender(() => {
219
+ const cursor = node.querySelector('.xterm-cursor');
220
+ if (!cursor)
221
+ return;
222
+ cursor.classList.remove('xterm-cursor');
223
+ let background = '#0d100d';
224
+ let foreground = '#e6ece4';
225
+ try {
226
+ const style = window.getComputedStyle(cursor);
227
+ foreground = style.color || foreground;
228
+ const fallbackSurface = () => {
229
+ const scrollEl = node.querySelector('.xterm-scrollable-element');
230
+ return scrollEl
231
+ ? window.getComputedStyle(scrollEl).backgroundColor
232
+ : window.getComputedStyle(node).backgroundColor;
233
+ };
234
+ background = opaqueColor(style.backgroundColor, fallbackSurface);
235
+ }
236
+ finally {
237
+ cursor.classList.add('xterm-cursor');
238
+ }
239
+ const preferred = opaqueColor(preferredCursor(), () => background);
240
+ const bgLum = luminance(background);
241
+ const prefLum = luminance(preferred);
242
+ const contrast = (Math.max(bgLum, prefLum) + 0.05) / (Math.min(bgLum, prefLum) + 0.05);
243
+ const fill = contrast >= 3 ? preferred : (bgLum > 0.179 ? '#000000' : '#ffffff');
244
+ node.style.setProperty('--terminal-cursor', fill);
245
+ node.style.setProperty('--terminal-cursor-accent', luminance(fill) > 0.179 ? '#000000' : '#ffffff');
246
+ node.style.setProperty('--terminal-cursor-cell-background', background);
247
+ node.style.setProperty('--terminal-cursor-cell-foreground', foreground);
248
+ });
249
+ }
@@ -0,0 +1,258 @@
1
+ import type { IDisposable, ITheme, Terminal } from '@xterm/xterm';
2
+
3
+ const ANSI_KEYS = [
4
+ 'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white',
5
+ 'brightBlack', 'brightRed', 'brightGreen', 'brightYellow', 'brightBlue', 'brightMagenta', 'brightCyan', 'brightWhite'
6
+ ] as const;
7
+
8
+ const SPECIAL_KEYS = ['foreground', 'background', 'cursor'] as const;
9
+ type SpecialKey = (typeof SPECIAL_KEYS)[number];
10
+
11
+ const DARK_ANSI_PALETTE: ITheme = {
12
+ black: '#1c201c',
13
+ red: '#f87171',
14
+ green: '#4ade80',
15
+ yellow: '#facc15',
16
+ blue: '#60a5fa',
17
+ magenta: '#c084fc',
18
+ cyan: '#22d3ee',
19
+ white: '#e6ece4',
20
+ brightBlack: '#6b736a',
21
+ brightRed: '#fca5a5',
22
+ brightGreen: '#86efac',
23
+ brightYellow: '#fde047',
24
+ brightBlue: '#93c5fd',
25
+ brightMagenta: '#d8b4fe',
26
+ brightCyan: '#67e8f9',
27
+ brightWhite: '#ffffff'
28
+ };
29
+
30
+ const LIGHT_ANSI_PALETTE: ITheme = {
31
+ black: '#1f2328',
32
+ red: '#cf222e',
33
+ green: '#116329',
34
+ yellow: '#9a6700',
35
+ blue: '#0969da',
36
+ magenta: '#8250df',
37
+ cyan: '#1b7c83',
38
+ white: '#6e7781',
39
+ brightBlack: '#57606a',
40
+ brightRed: '#a40e26',
41
+ brightGreen: '#1a7f37',
42
+ brightYellow: '#bf8700',
43
+ brightBlue: '#218bff',
44
+ brightMagenta: '#a475f9',
45
+ brightCyan: '#3192aa',
46
+ brightWhite: '#8c959f'
47
+ };
48
+
49
+ function colorIndex(value: string): number | undefined {
50
+ if (!/^\d{1,3}$/.test(value)) return undefined;
51
+ const parsed = Number(value);
52
+ return parsed >= 0 && parsed < 256 ? parsed : undefined;
53
+ }
54
+
55
+ function oscColor(value: string): string | undefined {
56
+ const rgb = /^rgb:([\da-f]{1,4})\/([\da-f]{1,4})\/([\da-f]{1,4})$/i.exec(value);
57
+ let channels: string[];
58
+ if (rgb !== null) {
59
+ const r = rgb[1];
60
+ const g = rgb[2];
61
+ const b = rgb[3];
62
+ if (!r || !g || !b) return undefined;
63
+ channels = [r, g, b];
64
+ if (!channels.every((c) => c.length === r.length)) return undefined;
65
+ } else {
66
+ if (!/^#(?:[\da-f]{3}|[\da-f]{6}|[\da-f]{9}|[\da-f]{12})$/i.test(value)) return undefined;
67
+ const width = (value.length - 1) / 3;
68
+ channels = [
69
+ value.slice(1, width + 1),
70
+ value.slice(width + 1, 2 * width + 1),
71
+ value.slice(2 * width + 1)
72
+ ];
73
+ }
74
+ const hex = channels.map((channel) => {
75
+ const n = rgb === null
76
+ ? Number.parseInt(channel.padEnd(2, '0').slice(0, 2), 16)
77
+ : Math.round((Number.parseInt(channel, 16) * 255) / (16 ** channel.length - 1));
78
+ return n.toString(16).padStart(2, '0');
79
+ }).join('');
80
+ return `#${hex}`;
81
+ }
82
+
83
+ /**
84
+ * Tracks application OSC color overrides (OSC 4/10/11/12/104/110-112) separately from
85
+ * Causyne Studio theme defaults so light/dark theme switches preserve program palettes.
86
+ */
87
+ export class TerminalTheme implements IDisposable {
88
+ private readonly terminal: Terminal;
89
+ private defaults: ITheme = {};
90
+ private readonly indexed = new Map<number, string>();
91
+ private readonly special = new Map<SpecialKey, string>();
92
+ private readonly subscriptions: IDisposable[];
93
+
94
+ constructor(terminal: Terminal) {
95
+ this.terminal = terminal;
96
+ const observe = (code: number, change: (data: string) => void): IDisposable =>
97
+ terminal.parser.registerOscHandler(code, (data) => {
98
+ change(data);
99
+ return false;
100
+ });
101
+ this.subscriptions = [
102
+ observe(4, (data) => {
103
+ const parts = data.split(';');
104
+ for (let i = 0; i + 1 < parts.length; i += 2) {
105
+ const idxRaw = parts[i];
106
+ const colRaw = parts[i + 1];
107
+ if (idxRaw === undefined || colRaw === undefined) continue;
108
+ const index = colorIndex(idxRaw);
109
+ const color = oscColor(colRaw);
110
+ if (index !== undefined && color !== undefined) this.indexed.set(index, color);
111
+ }
112
+ }),
113
+ observe(104, (data) => {
114
+ if (data === '') {
115
+ this.indexed.clear();
116
+ } else {
117
+ for (const part of data.split(';')) {
118
+ const index = colorIndex(part);
119
+ if (index !== undefined) this.indexed.delete(index);
120
+ }
121
+ }
122
+ this.apply();
123
+ }),
124
+ ...SPECIAL_KEYS.flatMap((key, offset) => [
125
+ observe(10 + offset, (data) => {
126
+ const parts = data.split(';');
127
+ for (let i = 0; i < parts.length; i += 1) {
128
+ const target = SPECIAL_KEYS[offset + i];
129
+ const part = parts[i];
130
+ if (target === undefined || part === undefined) continue;
131
+ const color = oscColor(part);
132
+ if (color !== undefined) this.special.set(target, color);
133
+ }
134
+ }),
135
+ observe(110 + offset, () => {
136
+ this.special.delete(key);
137
+ this.apply();
138
+ })
139
+ ])
140
+ ];
141
+ }
142
+
143
+ update(background: string, foreground: string, isLight: boolean): void {
144
+ const basePalette = isLight ? LIGHT_ANSI_PALETTE : DARK_ANSI_PALETTE;
145
+ if (
146
+ this.defaults.background === background
147
+ && this.defaults.foreground === foreground
148
+ && this.defaults.blue === basePalette.blue
149
+ ) {
150
+ return;
151
+ }
152
+ this.defaults = {
153
+ ...basePalette,
154
+ background,
155
+ foreground,
156
+ cursor: foreground,
157
+ cursorAccent: background,
158
+ selectionBackground: isLight ? 'rgba(9, 105, 218, 0.24)' : 'rgba(147, 197, 253, 0.28)',
159
+ selectionInactiveBackground: isLight ? 'rgba(9, 105, 218, 0.14)' : 'rgba(147, 197, 253, 0.16)'
160
+ };
161
+ this.apply();
162
+ }
163
+
164
+ get cursor(): string {
165
+ return this.special.get('cursor') ?? this.defaults.cursor ?? '#e6ece4';
166
+ }
167
+
168
+ dispose(): void {
169
+ for (const sub of this.subscriptions) sub.dispose();
170
+ }
171
+
172
+ private apply(): void {
173
+ const theme: ITheme = { ...this.defaults };
174
+ for (const [k, v] of this.special) {
175
+ theme[k] = v;
176
+ }
177
+ for (const [index, color] of this.indexed) {
178
+ const key = ANSI_KEYS[index];
179
+ if (key !== undefined) {
180
+ theme[key] = color;
181
+ } else {
182
+ const ext = theme.extendedAnsi ?? [];
183
+ ext[index - ANSI_KEYS.length] = color;
184
+ theme.extendedAnsi = ext;
185
+ }
186
+ }
187
+ this.terminal.options.theme = theme;
188
+ }
189
+ }
190
+
191
+ function colorChannels(color: string): number[] {
192
+ if (color.startsWith('#') && color.length >= 7) {
193
+ return [
194
+ Number.parseInt(color.slice(1, 3), 16),
195
+ Number.parseInt(color.slice(3, 5), 16),
196
+ Number.parseInt(color.slice(5, 7), 16)
197
+ ];
198
+ }
199
+ const matches = color.match(/\d+(?:\.\d+)?/g);
200
+ return matches ? matches.map(Number) : [0, 0, 0];
201
+ }
202
+
203
+ function luminance(color: string): number {
204
+ const channels = colorChannels(color);
205
+ const r = (channels[0] ?? 0) / 255;
206
+ const g = (channels[1] ?? 0) / 255;
207
+ const b = (channels[2] ?? 0) / 255;
208
+ const lin = (v: number): number => (v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4);
209
+ return 0.2126 * lin(r) + 0.7152 * lin(g) + 0.0722 * lin(b);
210
+ }
211
+
212
+ function opaqueColor(color: string, background: () => string): string {
213
+ const channels = colorChannels(color);
214
+ const alpha = channels[3] ?? 1;
215
+ if (alpha >= 1) return color;
216
+ const surface = colorChannels(background());
217
+ const rgb = [0, 1, 2].map((i) => Math.round((channels[i] ?? 0) * alpha + (surface[i] ?? 0) * (1 - alpha)));
218
+ return `rgb(${rgb.join(', ')})`;
219
+ }
220
+
221
+ /**
222
+ * Keeps the xterm DOM cursor legible against default, ANSI, true-color, and inverse cells.
223
+ */
224
+ export function observeTerminalCursor(
225
+ terminal: Terminal,
226
+ node: HTMLElement,
227
+ preferredCursor: () => string
228
+ ): IDisposable {
229
+ return terminal.onRender(() => {
230
+ const cursor = node.querySelector<HTMLElement>('.xterm-cursor');
231
+ if (!cursor) return;
232
+ cursor.classList.remove('xterm-cursor');
233
+ let background = '#0d100d';
234
+ let foreground = '#e6ece4';
235
+ try {
236
+ const style = window.getComputedStyle(cursor);
237
+ foreground = style.color || foreground;
238
+ const fallbackSurface = (): string => {
239
+ const scrollEl = node.querySelector<HTMLElement>('.xterm-scrollable-element');
240
+ return scrollEl
241
+ ? window.getComputedStyle(scrollEl).backgroundColor
242
+ : window.getComputedStyle(node).backgroundColor;
243
+ };
244
+ background = opaqueColor(style.backgroundColor, fallbackSurface);
245
+ } finally {
246
+ cursor.classList.add('xterm-cursor');
247
+ }
248
+ const preferred = opaqueColor(preferredCursor(), () => background);
249
+ const bgLum = luminance(background);
250
+ const prefLum = luminance(preferred);
251
+ const contrast = (Math.max(bgLum, prefLum) + 0.05) / (Math.min(bgLum, prefLum) + 0.05);
252
+ const fill = contrast >= 3 ? preferred : (bgLum > 0.179 ? '#000000' : '#ffffff');
253
+ node.style.setProperty('--terminal-cursor', fill);
254
+ node.style.setProperty('--terminal-cursor-accent', luminance(fill) > 0.179 ? '#000000' : '#ffffff');
255
+ node.style.setProperty('--terminal-cursor-cell-background', background);
256
+ node.style.setProperty('--terminal-cursor-cell-foreground', foreground);
257
+ });
258
+ }