@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.
- package/bin/causyne.js +1 -1
- package/docs/THIRD_PARTY_NOTICES.md +36 -2
- package/package.json +6 -4
- package/src/acp/protocol.ts +19 -1
- package/src/acp/server.js +282 -8
- package/src/acp/server.ts +284 -8
- package/src/agent/abortableDelay.js +2 -2
- package/src/agent/abortableDelay.ts +2 -2
- package/src/agent/errorClassifier.js +9 -1
- package/src/agent/errorClassifier.ts +8 -1
- package/src/agentLoop.js +142 -53
- package/src/agentLoop.ts +151 -50
- package/src/approvalPolicy.js +34 -10
- package/src/approvalPolicy.ts +42 -11
- package/src/browser/causyneBrowser.js +61 -3
- package/src/browser/causyneBrowser.ts +65 -3
- package/src/browser/urlPolicy.js +14 -1
- package/src/browser/urlPolicy.ts +12 -1
- package/src/cliMode.js +21 -9
- package/src/cliMode.ts +20 -8
- package/src/commands.js +365 -45
- package/src/commands.ts +403 -45
- package/src/config.js +19 -4
- package/src/config.ts +18 -4
- package/src/core/contextCompactor.js +54 -7
- package/src/core/contextCompactor.ts +63 -7
- package/src/core/goalLoop.js +257 -0
- package/src/core/goalLoop.ts +310 -0
- package/src/entrypoint.js +135 -3
- package/src/entrypoint.ts +136 -3
- package/src/game/causyneCoreBridge.js +686 -0
- package/src/game/causyneCoreBridge.ts +702 -0
- package/src/game/gameLauncher.js +287 -0
- package/src/game/gameLauncher.ts +352 -0
- package/src/game/gameProvisioner.js +425 -0
- package/src/game/gameProvisioner.ts +488 -0
- package/src/headlessCli.js +19 -12
- package/src/headlessCli.ts +22 -8
- package/src/infra/atomicWriter.js +40 -4
- package/src/infra/atomicWriter.ts +36 -4
- package/src/launchIntent.js +73 -3
- package/src/launchIntent.ts +76 -3
- package/src/lsp/lspClient.js +83 -12
- package/src/lsp/lspClient.ts +75 -12
- package/src/mcp/mcpRegistry.js +75 -8
- package/src/mcp/mcpRegistry.ts +70 -7
- package/src/memory/contextBudget.js +2 -0
- package/src/memory/contextBudget.ts +1 -0
- package/src/memory/episodicStore.js +40 -0
- package/src/memory/episodicStore.ts +46 -0
- package/src/memory.js +67 -0
- package/src/memory.ts +65 -0
- package/src/modelVariants.js +45 -0
- package/src/modelVariants.ts +54 -0
- package/src/outputRecovery.js +3 -2
- package/src/outputRecovery.ts +4 -2
- package/src/permissionPresets.js +327 -0
- package/src/permissionPresets.ts +361 -0
- package/src/personalization.js +365 -0
- package/src/personalization.ts +471 -0
- package/src/projectInstructions.js +30 -10
- package/src/projectInstructions.ts +31 -12
- package/src/providers/acp.js +3 -2
- package/src/providers/acp.ts +3 -2
- package/src/providers/antigravity.js +64 -9
- package/src/providers/antigravity.ts +75 -9
- package/src/providers/byokHttp.js +7 -1
- package/src/providers/byokHttp.ts +6 -1
- package/src/providers/byokStream.js +21 -8
- package/src/providers/byokStream.ts +17 -7
- package/src/providers/dashscope-byok.js +21 -5
- package/src/providers/dashscope-byok.ts +29 -11
- package/src/providers/moonshot.js +26 -7
- package/src/providers/moonshot.ts +34 -13
- package/src/providers/muse.js +6 -0
- package/src/providers/muse.ts +8 -0
- package/src/providers/ollama.js +39 -7
- package/src/providers/ollama.ts +36 -7
- package/src/providers/openai-compatible.js +16 -3
- package/src/providers/openai-compatible.ts +13 -3
- package/src/providers/openai.js +62 -4
- package/src/providers/openai.ts +50 -4
- package/src/providers/openrouter.js +19 -4
- package/src/providers/openrouter.ts +21 -4
- package/src/providers/qwen-byok.js +21 -5
- package/src/providers/qwen-byok.ts +29 -11
- package/src/providers/xai.js +26 -10
- package/src/providers/xai.ts +34 -16
- package/src/reporting.js +4 -2
- package/src/reporting.ts +4 -2
- package/src/sandbox.js +34 -0
- package/src/sandbox.ts +43 -0
- package/src/scheduler/scheduler.js +372 -0
- package/src/scheduler/scheduler.ts +433 -0
- package/src/sessionSearchIndex.js +267 -0
- package/src/sessionSearchIndex.ts +311 -0
- package/src/skillsManager.js +2 -3
- package/src/skillsManager.ts +2 -3
- package/src/toolPacks.js +6 -0
- package/src/toolPacks.ts +6 -0
- package/src/tools/applyPatch.js +1 -1
- package/src/tools/applyPatch.ts +1 -1
- package/src/tools/generateMediaViaApi.js +4 -3
- package/src/tools/generateMediaViaApi.ts +4 -3
- package/src/tools/memoryWrite.js +15 -1
- package/src/tools/memoryWrite.ts +16 -1
- package/src/tools/nativeEngine.js +2 -2
- package/src/tools/nativeEngine.ts +2 -2
- package/src/tools/openSourceToolProvisioner.js +2 -2
- package/src/tools/openSourceToolProvisioner.ts +2 -2
- package/src/tools/osSandbox.js +1 -1
- package/src/tools/osSandbox.ts +1 -1
- package/src/tools/payloadSanitizer.js +0 -1
- package/src/tools/payloadSanitizer.ts +0 -1
- package/src/tools/pdfReader.js +122 -20
- package/src/tools/pdfReader.ts +111 -20
- package/src/tools/presentDeliverable.js +103 -0
- package/src/tools/presentDeliverable.ts +127 -0
- package/src/tools/ptcRuntime.js +444 -0
- package/src/tools/ptcRuntime.ts +490 -0
- package/src/tools/ptyTerminal.js +1108 -0
- package/src/tools/ptyTerminal.ts +1245 -0
- package/src/tools/registry.js +21 -12
- package/src/tools/registry.ts +21 -12
- package/src/tools/toolOutputStore.js +26 -4
- package/src/tools/toolOutputStore.ts +22 -4
- package/src/tools/webFetch.js +158 -11
- package/src/tools/webFetch.ts +145 -11
- package/src/tools/webSearch.js +123 -36
- package/src/tools/webSearch.ts +124 -33
- package/src/tui/banner.js +5 -1
- package/src/tui/banner.ts +6 -1
- package/src/tui/chatRenderer.js +1 -2
- package/src/tui/chatRenderer.ts +1 -2
- package/src/tui/completion.js +1 -0
- package/src/tui/completion.ts +1 -0
- package/src/tui/composerControls.js +111 -27
- package/src/tui/composerControls.ts +124 -28
- package/src/tui/dialogs.js +3 -16
- package/src/tui/dialogs.ts +3 -16
- package/src/tui/home.js +1 -0
- package/src/tui/home.ts +1 -0
- package/src/tui/invocation.js +1 -2
- package/src/tui/invocation.ts +1 -2
- package/src/tui/modelPicker.js +22 -12
- package/src/tui/modelPicker.ts +22 -12
- package/src/tui/overlay.js +2 -5
- package/src/tui/overlay.ts +2 -5
- package/src/tui/prompt.js +6 -4
- package/src/tui/prompt.ts +6 -4
- package/src/tui/selectList.js +4 -12
- package/src/tui/selectList.ts +4 -12
- package/src/tui/setup.js +22 -9
- package/src/tui/setup.ts +26 -9
- package/src/tui/statusLine.ts +2 -0
- package/src/web/client/browserPanel.js +905 -0
- package/src/web/client/browserPanel.ts +872 -0
- package/src/web/client/chatStream.js +321 -0
- package/src/web/client/chatStream.ts +297 -0
- package/src/web/client/cliPanel.js +165 -0
- package/src/web/client/cliPanel.ts +171 -0
- package/src/web/client/composer.js +204 -0
- package/src/web/client/composer.ts +209 -0
- package/src/web/client/diffPanel.js +176 -0
- package/src/web/client/diffPanel.ts +203 -0
- package/src/web/client/dom.js +271 -0
- package/src/web/client/dom.ts +272 -0
- package/src/web/client/filesAndCanvas.js +233 -0
- package/src/web/client/filesAndCanvas.ts +240 -0
- package/src/web/client/gameLauncherUi.js +78 -0
- package/src/web/client/gameLauncherUi.ts +78 -0
- package/src/web/client/main.js +316 -0
- package/src/web/client/main.ts +391 -0
- package/src/web/client/markdown.js +364 -0
- package/src/web/client/markdown.ts +373 -0
- package/src/web/client/modelPicker.js +455 -0
- package/src/web/client/modelPicker.ts +451 -0
- package/src/web/client/permissionPresetsUi.js +307 -0
- package/src/web/client/permissionPresetsUi.ts +333 -0
- package/src/web/client/sessions.js +353 -0
- package/src/web/client/sessions.ts +372 -0
- package/src/web/client/settingsModal.js +824 -0
- package/src/web/client/settingsModal.ts +959 -0
- package/src/web/client/state.js +280 -0
- package/src/web/client/state.ts +259 -0
- package/src/web/client/streamSmoother.js +127 -0
- package/src/web/client/streamSmoother.ts +119 -0
- package/src/web/client/studioLayout.js +89 -0
- package/src/web/client/studioLayout.ts +103 -0
- package/src/web/client/studioShell.js +660 -0
- package/src/web/client/studioShell.ts +672 -0
- package/src/web/client/studioStart.js +77 -0
- package/src/web/client/studioStart.ts +89 -0
- package/src/web/client/terminalPanel.js +606 -0
- package/src/web/client/terminalPanel.ts +632 -0
- package/src/web/client/terminalTheme.js +249 -0
- package/src/web/client/terminalTheme.ts +258 -0
- package/src/web/client/turnView.js +934 -0
- package/src/web/client/turnView.ts +981 -0
- package/src/web/client/types.js +1 -0
- package/src/web/client/types.ts +322 -0
- package/src/web/client/validators.js +147 -0
- package/src/web/client/validators.ts +140 -0
- package/src/web/server/activeRuns.js +152 -0
- package/src/web/server/activeRuns.ts +171 -0
- package/src/web/server/routesBrowser.js +409 -0
- package/src/web/server/routesBrowser.ts +435 -0
- package/src/web/server/routesChat.js +304 -0
- package/src/web/server/routesChat.ts +347 -0
- package/src/web/server/routesCore.js +472 -0
- package/src/web/server/routesCore.ts +522 -0
- package/src/web/server/routesSettings.js +344 -0
- package/src/web/server/routesSettings.ts +397 -0
- package/src/web/server/routesTerminal.js +162 -0
- package/src/web/server/routesTerminal.ts +182 -0
- package/src/web/server/routesWorkspace.js +510 -0
- package/src/web/server/routesWorkspace.ts +546 -0
- package/src/web/server/security.js +98 -0
- package/src/web/server/security.ts +108 -0
- package/src/web/server/sessionTranscript.js +586 -0
- package/src/web/server/sessionTranscript.ts +566 -0
- package/src/web/server/staticAssets.js +104 -0
- package/src/web/server/staticAssets.ts +116 -0
- package/src/web/server/types.js +1 -0
- package/src/web/server/types.ts +135 -0
- package/src/web/server/workspaceFiles.js +368 -0
- package/src/web/server/workspaceFiles.ts +363 -0
- package/src/web/skybridgeServer.js +78 -2448
- package/src/web/skybridgeServer.ts +120 -2600
- package/src/web/skybridgeUi.js +20 -450
- package/src/web/skybridgeUi.ts +24 -455
- package/src/web/styles/agent.css +380 -0
- package/src/web/styles/composer.css +193 -0
- package/src/web/styles/overlays.css +935 -0
- package/src/web/styles/shell.css +387 -0
- package/src/web/styles/studio.css +951 -0
- package/src/web/styles/thread.css +221 -0
- package/src/web/styles/tokens.css +94 -0
- package/src/web/ui/brandMark.js +73 -0
- package/src/web/ui/brandMark.ts +82 -0
- package/src/web/ui/modalsTemplate.js +388 -0
- package/src/web/ui/modalsTemplate.ts +388 -0
- package/src/web/ui/sidebarTemplate.js +63 -0
- package/src/web/ui/sidebarTemplate.ts +64 -0
- package/src/web/ui/studioTemplate.js +256 -0
- package/src/web/ui/studioTemplate.ts +256 -0
- package/src/web/ui/workspaceTemplate.js +72 -0
- package/src/web/ui/workspaceTemplate.ts +73 -0
- package/src/worktreeManager.js +16 -1
- package/src/worktreeManager.ts +22 -1
- package/src/web/skybridgeClientScript.js +0 -3951
- package/src/web/skybridgeClientScript.ts +0 -3952
- package/src/web/skybridgeStyles.js +0 -827
- 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
|
+
}
|