@clagentic/console 1.0.0-beta.1

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 (221) hide show
  1. package/CREDITS.md +3 -0
  2. package/LICENSE +21 -0
  3. package/README.md +250 -0
  4. package/bin/cli.js +2812 -0
  5. package/lib/agents-favorites.js +164 -0
  6. package/lib/agents.js +282 -0
  7. package/lib/ask-user-mcp-server.js +120 -0
  8. package/lib/browser-mcp-server.js +484 -0
  9. package/lib/build-user-env.js +63 -0
  10. package/lib/certs/fullchain.pem +47 -0
  11. package/lib/certs/privkey.pem +5 -0
  12. package/lib/cli-sessions.js +265 -0
  13. package/lib/codex-defaults.js +18 -0
  14. package/lib/config.js +332 -0
  15. package/lib/crisis-safety.js +60 -0
  16. package/lib/daemon-projects.js +164 -0
  17. package/lib/daemon.js +1602 -0
  18. package/lib/dm.js +128 -0
  19. package/lib/ipc.js +125 -0
  20. package/lib/ipv4-only.js +39 -0
  21. package/lib/mcp-local.js +385 -0
  22. package/lib/notes.js +141 -0
  23. package/lib/os-users.js +529 -0
  24. package/lib/pages.js +1325 -0
  25. package/lib/project-connection.js +302 -0
  26. package/lib/project-external-trigger.js +236 -0
  27. package/lib/project-file-watch.js +120 -0
  28. package/lib/project-filesystem.js +528 -0
  29. package/lib/project-http.js +753 -0
  30. package/lib/project-image.js +95 -0
  31. package/lib/project-knowledge.js +88 -0
  32. package/lib/project-loop.js +1304 -0
  33. package/lib/project-mcp.js +374 -0
  34. package/lib/project-memory.js +39 -0
  35. package/lib/project-notifications.js +262 -0
  36. package/lib/project-sessions.js +1581 -0
  37. package/lib/project-user-mention.js +193 -0
  38. package/lib/project-user-message.js +628 -0
  39. package/lib/project.js +1303 -0
  40. package/lib/public/app.js +1031 -0
  41. package/lib/public/apple-touch-icon.png +0 -0
  42. package/lib/public/claude-code-avatar.png +0 -0
  43. package/lib/public/codex-avatar.png +0 -0
  44. package/lib/public/console-icon.png +0 -0
  45. package/lib/public/css/admin.css +736 -0
  46. package/lib/public/css/agent-picker.css +119 -0
  47. package/lib/public/css/base.css +235 -0
  48. package/lib/public/css/channel-layout.css +441 -0
  49. package/lib/public/css/command-palette.css +559 -0
  50. package/lib/public/css/debate.css +1609 -0
  51. package/lib/public/css/diff.css +139 -0
  52. package/lib/public/css/filebrowser.css +1976 -0
  53. package/lib/public/css/highlight.css +144 -0
  54. package/lib/public/css/home-hub.css +545 -0
  55. package/lib/public/css/icon-strip.css +1856 -0
  56. package/lib/public/css/input.css +1349 -0
  57. package/lib/public/css/loop.css +1248 -0
  58. package/lib/public/css/mention.css +317 -0
  59. package/lib/public/css/menus.css +857 -0
  60. package/lib/public/css/messages.css +1833 -0
  61. package/lib/public/css/mobile-nav.css +1332 -0
  62. package/lib/public/css/notifications-center.css +296 -0
  63. package/lib/public/css/overlays.css +1273 -0
  64. package/lib/public/css/playbook.css +264 -0
  65. package/lib/public/css/profile.css +397 -0
  66. package/lib/public/css/rewind.css +671 -0
  67. package/lib/public/css/scheduler-modal.css +1495 -0
  68. package/lib/public/css/scheduler.css +1420 -0
  69. package/lib/public/css/server-settings.css +909 -0
  70. package/lib/public/css/session-search.css +159 -0
  71. package/lib/public/css/sidebar.css +2012 -0
  72. package/lib/public/css/skills.css +789 -0
  73. package/lib/public/css/sticky-notes.css +854 -0
  74. package/lib/public/css/stt.css +155 -0
  75. package/lib/public/css/title-bar.css +674 -0
  76. package/lib/public/css/tooltip.css +67 -0
  77. package/lib/public/css/user-settings.css +702 -0
  78. package/lib/public/emoji-data.json +1 -0
  79. package/lib/public/favicon-banded-32.png +0 -0
  80. package/lib/public/favicon-banded.png +0 -0
  81. package/lib/public/favicon-dark.svg +32 -0
  82. package/lib/public/favicon.svg +32 -0
  83. package/lib/public/icon-192.png +0 -0
  84. package/lib/public/icon-512.png +0 -0
  85. package/lib/public/icon-banded-76.png +0 -0
  86. package/lib/public/icon-banded-96.png +0 -0
  87. package/lib/public/icon-mono.svg +25 -0
  88. package/lib/public/icons/email/gmail.svg +7 -0
  89. package/lib/public/icons/email/outlook.svg +35 -0
  90. package/lib/public/icons/email/yahoo.svg +1 -0
  91. package/lib/public/index.html +1946 -0
  92. package/lib/public/manifest.json +27 -0
  93. package/lib/public/markdown-worker.js +43 -0
  94. package/lib/public/marked.min.js +6 -0
  95. package/lib/public/modules/admin.js +954 -0
  96. package/lib/public/modules/agent-picker.js +429 -0
  97. package/lib/public/modules/app-connection.js +216 -0
  98. package/lib/public/modules/app-cursors.js +492 -0
  99. package/lib/public/modules/app-dm.js +265 -0
  100. package/lib/public/modules/app-favicon.js +231 -0
  101. package/lib/public/modules/app-header.js +242 -0
  102. package/lib/public/modules/app-home-hub.js +432 -0
  103. package/lib/public/modules/app-loop-ui.js +691 -0
  104. package/lib/public/modules/app-loop-wizard.js +439 -0
  105. package/lib/public/modules/app-messages.js +1415 -0
  106. package/lib/public/modules/app-misc.js +463 -0
  107. package/lib/public/modules/app-notifications.js +651 -0
  108. package/lib/public/modules/app-panels.js +1061 -0
  109. package/lib/public/modules/app-projects.js +875 -0
  110. package/lib/public/modules/app-rate-limit.js +529 -0
  111. package/lib/public/modules/app-rendering.js +739 -0
  112. package/lib/public/modules/app-skills-install.js +285 -0
  113. package/lib/public/modules/avatar.js +36 -0
  114. package/lib/public/modules/command-palette.js +491 -0
  115. package/lib/public/modules/console-logo.js +162 -0
  116. package/lib/public/modules/context-sources.js +365 -0
  117. package/lib/public/modules/diff.js +412 -0
  118. package/lib/public/modules/dom-refs.js +21 -0
  119. package/lib/public/modules/emoji-picker.js +222 -0
  120. package/lib/public/modules/events.js +21 -0
  121. package/lib/public/modules/filebrowser.js +1966 -0
  122. package/lib/public/modules/fileicons.js +172 -0
  123. package/lib/public/modules/icons.js +80 -0
  124. package/lib/public/modules/input.js +1102 -0
  125. package/lib/public/modules/longpress.js +74 -0
  126. package/lib/public/modules/markdown.js +486 -0
  127. package/lib/public/modules/mcp-ui.js +295 -0
  128. package/lib/public/modules/mention.js +1093 -0
  129. package/lib/public/modules/notifications.js +613 -0
  130. package/lib/public/modules/playbook.js +578 -0
  131. package/lib/public/modules/profile.js +594 -0
  132. package/lib/public/modules/project-settings.js +849 -0
  133. package/lib/public/modules/project-switcher.js +375 -0
  134. package/lib/public/modules/qrcode.js +87 -0
  135. package/lib/public/modules/rewind.js +381 -0
  136. package/lib/public/modules/scheduler-config.js +1611 -0
  137. package/lib/public/modules/scheduler-history.js +131 -0
  138. package/lib/public/modules/scheduler.js +1689 -0
  139. package/lib/public/modules/server-settings.js +842 -0
  140. package/lib/public/modules/session-search.js +456 -0
  141. package/lib/public/modules/settings-defaults.js +253 -0
  142. package/lib/public/modules/sidebar-mobile.js +1425 -0
  143. package/lib/public/modules/sidebar-projects.js +2022 -0
  144. package/lib/public/modules/sidebar-sessions.js +1548 -0
  145. package/lib/public/modules/sidebar-users.js +449 -0
  146. package/lib/public/modules/sidebar.js +348 -0
  147. package/lib/public/modules/skills.js +793 -0
  148. package/lib/public/modules/state.js +3 -0
  149. package/lib/public/modules/sticky-notes.js +1221 -0
  150. package/lib/public/modules/store.js +34 -0
  151. package/lib/public/modules/stt.js +266 -0
  152. package/lib/public/modules/terminal.js +987 -0
  153. package/lib/public/modules/theme.js +812 -0
  154. package/lib/public/modules/tool-palette.js +562 -0
  155. package/lib/public/modules/tools.js +2285 -0
  156. package/lib/public/modules/tooltip.js +150 -0
  157. package/lib/public/modules/user-settings.js +544 -0
  158. package/lib/public/modules/utils.js +56 -0
  159. package/lib/public/modules/ws-ref.js +26 -0
  160. package/lib/public/style.css +34 -0
  161. package/lib/public/sw.js +159 -0
  162. package/lib/push.js +149 -0
  163. package/lib/scheduler.js +471 -0
  164. package/lib/sdk-bridge.js +1792 -0
  165. package/lib/sdk-message-processor.js +740 -0
  166. package/lib/sdk-message-queue.js +42 -0
  167. package/lib/sdk-skill-discovery.js +131 -0
  168. package/lib/sdk-worker.js +677 -0
  169. package/lib/server-admin.js +712 -0
  170. package/lib/server-auth.js +737 -0
  171. package/lib/server-dm.js +170 -0
  172. package/lib/server-palette.js +104 -0
  173. package/lib/server-settings.js +553 -0
  174. package/lib/server-skills.js +280 -0
  175. package/lib/server.js +1390 -0
  176. package/lib/session-search.js +693 -0
  177. package/lib/sessions.js +946 -0
  178. package/lib/smtp.js +221 -0
  179. package/lib/terminal-manager.js +219 -0
  180. package/lib/terminal.js +39 -0
  181. package/lib/themes/ayu-light.json +9 -0
  182. package/lib/themes/catppuccin-latte.json +9 -0
  183. package/lib/themes/catppuccin-mocha.json +9 -0
  184. package/lib/themes/clagentic-dark.json +10 -0
  185. package/lib/themes/clagentic-light.json +10 -0
  186. package/lib/themes/dracula.json +9 -0
  187. package/lib/themes/everforest-light.json +9 -0
  188. package/lib/themes/everforest.json +9 -0
  189. package/lib/themes/github-light.json +9 -0
  190. package/lib/themes/gruvbox-dark.json +9 -0
  191. package/lib/themes/gruvbox-light.json +9 -0
  192. package/lib/themes/monokai.json +9 -0
  193. package/lib/themes/nord-light.json +9 -0
  194. package/lib/themes/nord.json +9 -0
  195. package/lib/themes/one-dark.json +9 -0
  196. package/lib/themes/one-light.json +9 -0
  197. package/lib/themes/rose-pine-dawn.json +9 -0
  198. package/lib/themes/rose-pine.json +9 -0
  199. package/lib/themes/solarized-dark.json +9 -0
  200. package/lib/themes/solarized-light.json +9 -0
  201. package/lib/themes/tokyo-night-light.json +9 -0
  202. package/lib/themes/tokyo-night.json +9 -0
  203. package/lib/updater.js +124 -0
  204. package/lib/user-presence.js +92 -0
  205. package/lib/users-auth.js +146 -0
  206. package/lib/users-permissions.js +118 -0
  207. package/lib/users-preferences.js +254 -0
  208. package/lib/users.js +465 -0
  209. package/lib/utils.js +64 -0
  210. package/lib/worktree.js +134 -0
  211. package/lib/ws-schema.js +464 -0
  212. package/lib/yoke/adapters/claude-worker.js +759 -0
  213. package/lib/yoke/adapters/claude.js +1599 -0
  214. package/lib/yoke/adapters/codex.js +1559 -0
  215. package/lib/yoke/codex-app-server.js +352 -0
  216. package/lib/yoke/index.js +319 -0
  217. package/lib/yoke/instructions.js +61 -0
  218. package/lib/yoke/interface.js +98 -0
  219. package/lib/yoke/mcp-bridge-server.js +302 -0
  220. package/lib/yoke/package.json +7 -0
  221. package/package.json +72 -0
@@ -0,0 +1,987 @@
1
+ import { iconHtml, refreshIcons } from './icons.js';
2
+ import { closeSidebar } from './sidebar.js';
3
+ import { closeFileViewer } from './filebrowser.js';
4
+ import { copyToClipboard } from './utils.js';
5
+ import { getTerminalTheme } from './theme.js';
6
+
7
+ var ctx;
8
+ var tabs = new Map(); // termId -> { id, title, exited, xterm, fitAddon, bodyEl }
9
+ var activeTabId = null;
10
+ var isOpen = false;
11
+ var ctrlActive = false;
12
+ var altActive = false;
13
+ var isTouchDevice = "ontouchstart" in window;
14
+ var viewportHandler = null;
15
+ var resizeObserver = null;
16
+
17
+ function disposeTab(tab) {
18
+ if (tab._webglAddon) {
19
+ try { tab._webglAddon.dispose(); } catch (e) {}
20
+ tab._webglAddon = null;
21
+ }
22
+ if (tab.xterm) {
23
+ tab.xterm.dispose();
24
+ tab.xterm = null;
25
+ }
26
+ tab.fitAddon = null;
27
+ if (tab.bodyEl && tab.bodyEl.parentNode) {
28
+ tab.bodyEl.parentNode.removeChild(tab.bodyEl);
29
+ tab.bodyEl = null;
30
+ }
31
+ }
32
+ var toolbarBound = false;
33
+ var termCtxMenu = null;
34
+
35
+ // --- Multi-line link provider ---
36
+ // xterm's WebLinksAddon only detects URLs on a single line.
37
+ // This provider reconstructs "logical lines" from wrapped buffer lines
38
+ // and detects URLs that span multiple rows.
39
+ function createMultiLineLinkProvider(xterm) {
40
+ var URL_RE = /https?:\/\/[^\s'"\]>)}{]+/g;
41
+
42
+ function getLogicalLine(buffer, y) {
43
+ // Walk backward to find the start of the logical line
44
+ var startY = y;
45
+ while (startY > 0) {
46
+ var line = buffer.getLine(startY);
47
+ if (!line || !line.isWrapped) break;
48
+ startY--;
49
+ }
50
+
51
+ // Walk forward to collect all wrapped continuation lines
52
+ var endY = startY;
53
+ var cols = xterm.cols;
54
+ while (endY < buffer.length - 1) {
55
+ var next = buffer.getLine(endY + 1);
56
+ if (!next || !next.isWrapped) break;
57
+ endY++;
58
+ }
59
+
60
+ // Build the full logical line text and track row boundaries
61
+ var text = "";
62
+ var rowOffsets = []; // { y, startOffset, length }
63
+ for (var row = startY; row <= endY; row++) {
64
+ var line = buffer.getLine(row);
65
+ if (!line) break;
66
+ var trimRight = (row === endY); // only trim trailing spaces on last row
67
+ var rowText = line.translateToString(trimRight);
68
+ rowOffsets.push({ y: row, startOffset: text.length, length: rowText.length });
69
+ text += rowText;
70
+ }
71
+
72
+ return { text: text, startY: startY, endY: endY, rowOffsets: rowOffsets };
73
+ }
74
+
75
+ function offsetToBufferPos(rowOffsets, offset) {
76
+ for (var i = 0; i < rowOffsets.length; i++) {
77
+ var ro = rowOffsets[i];
78
+ if (offset < ro.startOffset + ro.length) {
79
+ return { x: offset - ro.startOffset + 1, y: ro.y + 1 }; // 1-based
80
+ }
81
+ }
82
+ // Past end, clamp to last row
83
+ var last = rowOffsets[rowOffsets.length - 1];
84
+ return { x: last.length + 1, y: last.y + 1 };
85
+ }
86
+
87
+ return {
88
+ provideLinks: function (y, callback) {
89
+ var buffer = xterm.buffer.active;
90
+ // y is 1-based in provideLinks
91
+ var bufferY = y - 1;
92
+ var logical = getLogicalLine(buffer, bufferY);
93
+
94
+ // Only process if this logical line spans multiple rows
95
+ if (logical.startY === logical.endY) {
96
+ callback(undefined);
97
+ return;
98
+ }
99
+
100
+ // Only trigger on the first row of the logical line to avoid duplicates
101
+ if (bufferY !== logical.startY) {
102
+ callback(undefined);
103
+ return;
104
+ }
105
+
106
+ var links = [];
107
+ var match;
108
+ URL_RE.lastIndex = 0;
109
+ while ((match = URL_RE.exec(logical.text)) !== null) {
110
+ var urlStart = match.index;
111
+ var urlEnd = match.index + match[0].length - 1;
112
+
113
+ var startPos = offsetToBufferPos(logical.rowOffsets, urlStart);
114
+ var endPos = offsetToBufferPos(logical.rowOffsets, urlEnd);
115
+
116
+ // Only include if the URL actually spans multiple rows
117
+ if (startPos.y !== endPos.y) {
118
+ (function (url) {
119
+ links.push({
120
+ range: { start: startPos, end: endPos },
121
+ text: url,
122
+ activate: function () {
123
+ window.open(url, "_blank", "noopener");
124
+ },
125
+ });
126
+ })(match[0]);
127
+ }
128
+ }
129
+
130
+ callback(links.length > 0 ? links : undefined);
131
+ },
132
+ };
133
+ }
134
+
135
+ // --- Init ---
136
+ export function initTerminal(_ctx) {
137
+ ctx = _ctx;
138
+
139
+ // Close panel button
140
+ document.getElementById("terminal-close").addEventListener("click", function () {
141
+ closeTerminal();
142
+ });
143
+
144
+ // Fullscreen toggle
145
+ document.getElementById("terminal-fullscreen").addEventListener("click", function () {
146
+ var isFs = ctx.terminalContainerEl.classList.toggle("panel-fullscreen");
147
+ var icon = this.querySelector("[data-lucide]");
148
+ if (icon) {
149
+ icon.setAttribute("data-lucide", isFs ? "minimize-2" : "maximize-2");
150
+ refreshIcons();
151
+ }
152
+ fitTerminal();
153
+ });
154
+
155
+ // Header toggle button
156
+ var toggleBtn = document.getElementById("terminal-toggle-btn");
157
+ if (toggleBtn) {
158
+ toggleBtn.addEventListener("click", function () {
159
+ if (isOpen && !ctx.terminalContainerEl.classList.contains("hidden")) {
160
+ closeTerminal();
161
+ } else {
162
+ openTerminal();
163
+ }
164
+ });
165
+ }
166
+
167
+ // Sidebar terminal button — toggles open/close. Second click (or the
168
+ // Cmd+O hotkey on the same tile) dismisses the terminal panel.
169
+ var sidebarTermBtn = document.getElementById("terminal-sidebar-btn");
170
+ if (sidebarTermBtn) {
171
+ sidebarTermBtn.addEventListener("click", function () {
172
+ if (isOpen && !ctx.terminalContainerEl.classList.contains("hidden")) {
173
+ closeTerminal();
174
+ } else {
175
+ closeSidebar();
176
+ openTerminal();
177
+ }
178
+ });
179
+ }
180
+
181
+ // New tab button
182
+ var newTabBtn = document.getElementById("terminal-new-tab");
183
+ if (newTabBtn) {
184
+ newTabBtn.addEventListener("click", function () {
185
+ createNewTab();
186
+ });
187
+ }
188
+ }
189
+
190
+ // --- Open terminal panel ---
191
+ export function openTerminal() {
192
+ var container = ctx.terminalContainerEl;
193
+
194
+ // Hide file viewer if open (also unwatches)
195
+ closeFileViewer();
196
+
197
+ container.classList.remove("hidden");
198
+ isOpen = true;
199
+
200
+ // If no tabs exist, create one
201
+ if (tabs.size === 0) {
202
+ createNewTab();
203
+ return; // createNewTab will handle the rest via term_created
204
+ }
205
+
206
+ // Attach to active tab (or first available)
207
+ if (!activeTabId || !tabs.has(activeTabId)) {
208
+ activeTabId = tabs.keys().next().value;
209
+ }
210
+
211
+ activateTab(activeTabId);
212
+
213
+ // Mobile: close sidebar and hide tab bar
214
+ if (window.innerWidth <= 768) {
215
+ closeSidebar();
216
+ var mTabBar = document.getElementById("mobile-tab-bar");
217
+ if (mTabBar) mTabBar.classList.add("keyboard-hidden");
218
+ }
219
+
220
+ refreshIcons();
221
+ }
222
+
223
+ // --- Close terminal panel (hide, detach, but keep PTYs alive) ---
224
+ export function closeTerminal() {
225
+ var container = ctx.terminalContainerEl;
226
+ container.classList.remove("panel-fullscreen");
227
+ container.classList.add("hidden");
228
+ // Reset fullscreen icon
229
+ var fsIcon = document.querySelector("#terminal-fullscreen [data-lucide]");
230
+ if (fsIcon) {
231
+ fsIcon.setAttribute("data-lucide", "maximize-2");
232
+ refreshIcons();
233
+ }
234
+
235
+ // Detach from active tab
236
+ if (activeTabId && ctx.ws && ctx.connected) {
237
+ ctx.ws.send(JSON.stringify({ type: "term_detach", id: activeTabId }));
238
+ }
239
+
240
+ cleanupListeners();
241
+
242
+ // Hide toolbar
243
+ var toolbar = document.getElementById("terminal-toolbar");
244
+ if (toolbar) {
245
+ toolbar.classList.add("hidden");
246
+ var ctrlBtn = toolbar.querySelector("[data-key='ctrl']");
247
+ if (ctrlBtn) ctrlBtn.classList.remove("active");
248
+ var altBtn = toolbar.querySelector("[data-key='alt']");
249
+ if (altBtn) altBtn.classList.remove("active");
250
+ }
251
+ ctrlActive = false;
252
+ altActive = false;
253
+
254
+ // Mobile: restore tab bar
255
+ var mTabBar = document.getElementById("mobile-tab-bar");
256
+ if (mTabBar) mTabBar.classList.remove("keyboard-hidden");
257
+
258
+ isOpen = false;
259
+ }
260
+
261
+ // --- Create new tab ---
262
+ function createNewTab() {
263
+ if (!ctx.ws || !ctx.connected) return;
264
+
265
+ // Get current terminal body dimensions for cols/rows
266
+ var cols = 80;
267
+ var rows = 24;
268
+ if (activeTabId && tabs.has(activeTabId)) {
269
+ var activeTab = tabs.get(activeTabId);
270
+ if (activeTab.xterm) {
271
+ cols = activeTab.xterm.cols || 80;
272
+ rows = activeTab.xterm.rows || 24;
273
+ }
274
+ }
275
+
276
+ ctx.ws.send(JSON.stringify({ type: "term_create", cols: cols, rows: rows }));
277
+ }
278
+
279
+ // --- Close a tab (kill PTY) ---
280
+ function closeTab(termId) {
281
+ if (!ctx.ws || !ctx.connected) return;
282
+ ctx.ws.send(JSON.stringify({ type: "term_close", id: termId }));
283
+ }
284
+
285
+ // --- Activate a tab (show xterm, attach) ---
286
+ function activateTab(termId) {
287
+ var tab = tabs.get(termId);
288
+ if (!tab) return;
289
+
290
+ // Detach from old active
291
+ if (activeTabId && activeTabId !== termId && ctx.ws && ctx.connected) {
292
+ ctx.ws.send(JSON.stringify({ type: "term_detach", id: activeTabId }));
293
+ }
294
+
295
+ // Hide all tab bodies
296
+ for (var t of tabs.values()) {
297
+ if (t.bodyEl) t.bodyEl.style.display = "none";
298
+ }
299
+
300
+ activeTabId = termId;
301
+
302
+ // Lazy-create xterm instance
303
+ if (!tab.xterm) {
304
+ createXtermForTab(tab);
305
+ }
306
+
307
+ // Show this tab's body
308
+ if (tab.bodyEl) tab.bodyEl.style.display = "";
309
+
310
+ // Attach to server
311
+ if (ctx.ws && ctx.connected) {
312
+ ctx.ws.send(JSON.stringify({ type: "term_attach", id: termId }));
313
+ }
314
+
315
+ // Fit and focus
316
+ setupListeners();
317
+ fitTerminal();
318
+ // Re-fit after layout settles (flex may not have computed final size yet)
319
+ setTimeout(fitTerminal, 50);
320
+
321
+ if (tab.xterm) {
322
+ tab.xterm.focus();
323
+ }
324
+
325
+ // Show toolbar on touch devices
326
+ var toolbar = document.getElementById("terminal-toolbar");
327
+ if (toolbar && isTouchDevice) {
328
+ toolbar.classList.remove("hidden");
329
+ initToolbar(toolbar);
330
+ }
331
+
332
+ // Mobile viewport handling
333
+ if (window.visualViewport && !viewportHandler) {
334
+ viewportHandler = function () {
335
+ ctx.terminalContainerEl.style.height = window.visualViewport.height + "px";
336
+ fitTerminal();
337
+ };
338
+ window.visualViewport.addEventListener("resize", viewportHandler);
339
+ }
340
+
341
+ renderTabBar();
342
+ }
343
+
344
+ // --- Create xterm.js instance for a tab ---
345
+ function createXtermForTab(tab) {
346
+ if (typeof Terminal === "undefined") return;
347
+
348
+ var xterm = new Terminal({
349
+ cursorBlink: true,
350
+ fontSize: 13,
351
+ fontFamily: "'SF Mono', Menlo, Monaco, 'Courier New', monospace",
352
+ theme: getTerminalTheme(),
353
+ });
354
+
355
+ var fitAddon = null;
356
+ if (typeof FitAddon !== "undefined") {
357
+ fitAddon = new FitAddon.FitAddon();
358
+ xterm.loadAddon(fitAddon);
359
+ }
360
+
361
+ // Web links addon: make URLs clickable (single-line)
362
+ if (typeof WebLinksAddon !== "undefined") {
363
+ xterm.loadAddon(new WebLinksAddon.WebLinksAddon());
364
+ }
365
+
366
+ // Custom multi-line link provider: detect URLs that wrap across lines
367
+ xterm.registerLinkProvider(createMultiLineLinkProvider(xterm));
368
+
369
+ // Create a container div for this tab's terminal
370
+ var bodyEl = document.createElement("div");
371
+ bodyEl.className = "terminal-tab-body";
372
+ ctx.terminalBodyEl.appendChild(bodyEl);
373
+
374
+ xterm.open(bodyEl);
375
+
376
+ // WebGL addon: pixel-perfect rendering (eliminates gaps in block characters)
377
+ // Must be loaded after xterm.open() so the rendering context is available.
378
+ if (typeof WebglAddon !== "undefined") {
379
+ try {
380
+ var webgl = new WebglAddon.WebglAddon();
381
+ webgl.onContextLoss(function () {
382
+ webgl.dispose();
383
+ tab._webglAddon = null;
384
+ });
385
+ xterm.loadAddon(webgl);
386
+ tab._webglAddon = webgl;
387
+ } catch (e) {
388
+ // WebGL not available, fall back to DOM renderer
389
+ }
390
+ }
391
+
392
+ // Route input to server
393
+ xterm.onData(function (data) {
394
+ if (ctx.ws && ctx.connected) {
395
+ ctx.ws.send(JSON.stringify({ type: "term_input", id: tab.id, data: data }));
396
+ }
397
+ });
398
+
399
+ // Cmd/Ctrl+C copy and Cmd/Ctrl+V paste: intercept before xterm swallows the event
400
+ xterm.attachCustomKeyEventHandler(function (e) {
401
+ if (e.type !== "keydown") return true;
402
+ // Cmd/Ctrl+C: copy selection if any, otherwise send SIGINT
403
+ if ((e.ctrlKey || e.metaKey) && e.key === "c") {
404
+ var sel = xterm.getSelection();
405
+ if (sel) {
406
+ if (navigator.clipboard && navigator.clipboard.writeText) {
407
+ navigator.clipboard.writeText(sel).catch(function () {});
408
+ }
409
+ return false; // prevent xterm from handling
410
+ }
411
+ // No selection on macOS Cmd+C: do nothing (not SIGINT)
412
+ if (e.metaKey) return false;
413
+ }
414
+ // Cmd/Ctrl+V: let browser handle paste event
415
+ if ((e.ctrlKey || e.metaKey) && e.key === "v") {
416
+ return false; // let browser fire paste event
417
+ }
418
+ return true;
419
+ });
420
+
421
+ // Handle paste via browser paste event (works for Cmd+V, Ctrl+V, right-click paste)
422
+ bodyEl.addEventListener("paste", function (e) {
423
+ var text = (e.clipboardData || window.clipboardData).getData("text");
424
+ if (text) {
425
+ e.preventDefault();
426
+ if (ctx.ws && ctx.connected) {
427
+ ctx.ws.send(JSON.stringify({ type: "term_input", id: tab.id, data: text }));
428
+ }
429
+ }
430
+ });
431
+
432
+ // Right-click context menu
433
+ bodyEl.addEventListener("contextmenu", function (e) {
434
+ showTermCtxMenu(e, tab);
435
+ });
436
+
437
+ tab.xterm = xterm;
438
+ tab.fitAddon = fitAddon;
439
+ tab.bodyEl = bodyEl;
440
+ }
441
+
442
+ // --- Fit active terminal ---
443
+ var fitRafId = null;
444
+
445
+ function fitTerminal() {
446
+ if (fitRafId) cancelAnimationFrame(fitRafId);
447
+ fitRafId = requestAnimationFrame(function () {
448
+ fitRafId = null;
449
+ if (!activeTabId) return;
450
+ var tab = tabs.get(activeTabId);
451
+ if (!tab || !tab.fitAddon || !tab.xterm) return;
452
+
453
+ try {
454
+ tab.fitAddon.fit();
455
+ if (ctx.ws && ctx.connected) {
456
+ ctx.ws.send(JSON.stringify({
457
+ type: "term_resize",
458
+ id: activeTabId,
459
+ cols: tab.xterm.cols,
460
+ rows: tab.xterm.rows,
461
+ }));
462
+ }
463
+ } catch (e) {}
464
+ });
465
+ }
466
+
467
+ // --- Setup/cleanup resize listeners ---
468
+ function setupListeners() {
469
+ cleanupListeners();
470
+
471
+ window.addEventListener("resize", fitTerminal);
472
+
473
+ if (typeof ResizeObserver !== "undefined" && ctx.terminalBodyEl) {
474
+ resizeObserver = new ResizeObserver(function () {
475
+ fitTerminal();
476
+ });
477
+ resizeObserver.observe(ctx.terminalBodyEl);
478
+ }
479
+ }
480
+
481
+ function cleanupListeners() {
482
+ window.removeEventListener("resize", fitTerminal);
483
+
484
+ if (resizeObserver) {
485
+ resizeObserver.disconnect();
486
+ resizeObserver = null;
487
+ }
488
+
489
+ if (viewportHandler && window.visualViewport) {
490
+ window.visualViewport.removeEventListener("resize", viewportHandler);
491
+ viewportHandler = null;
492
+ }
493
+ ctx.terminalContainerEl.style.height = "";
494
+ }
495
+
496
+ // --- Render tab bar ---
497
+ function renderTabBar() {
498
+ var tabsEl = document.getElementById("terminal-tabs");
499
+ if (!tabsEl) return;
500
+
501
+ tabsEl.innerHTML = "";
502
+
503
+ for (var tab of tabs.values()) {
504
+ (function (t) {
505
+ var el = document.createElement("div");
506
+ el.className = "terminal-tab";
507
+ if (t.id === activeTabId) el.classList.add("active");
508
+ if (t.exited) el.classList.add("exited");
509
+
510
+ var label = document.createElement("span");
511
+ label.className = "terminal-tab-label";
512
+ label.textContent = t.title;
513
+ el.appendChild(label);
514
+
515
+ // Double-click label to rename
516
+ label.addEventListener("dblclick", function (e) {
517
+ e.stopPropagation();
518
+ startRenameTab(t, label);
519
+ });
520
+
521
+ var moreBtn = document.createElement("button");
522
+ moreBtn.className = "terminal-tab-more";
523
+ moreBtn.innerHTML = '<i data-lucide="ellipsis" style="width:12px;height:12px"></i>';
524
+ moreBtn.addEventListener("click", function (e) {
525
+ e.stopPropagation();
526
+ showTabContextMenu(e, t, label);
527
+ });
528
+ el.appendChild(moreBtn);
529
+
530
+ el.addEventListener("click", function () {
531
+ if (t.id !== activeTabId) {
532
+ activateTab(t.id);
533
+ }
534
+ });
535
+
536
+ tabsEl.appendChild(el);
537
+ })(tab);
538
+ }
539
+
540
+ updateTerminalBadge();
541
+ refreshIcons();
542
+ }
543
+
544
+ // --- Tab context menu (three-dot) ---
545
+ function showTabContextMenu(e, tab, labelEl) {
546
+ var existing = document.querySelector(".terminal-tab-ctx");
547
+ if (existing) existing.remove();
548
+
549
+ var menu = document.createElement("div");
550
+ menu.className = "terminal-tab-ctx";
551
+
552
+ var renameItem = document.createElement("button");
553
+ renameItem.className = "terminal-tab-ctx-item";
554
+ renameItem.innerHTML = '<i data-lucide="pencil" style="width:13px;height:13px"></i> Rename';
555
+ renameItem.addEventListener("click", function () {
556
+ menu.remove();
557
+ startRenameTab(tab, labelEl);
558
+ });
559
+ menu.appendChild(renameItem);
560
+
561
+ var closeItem = document.createElement("button");
562
+ closeItem.className = "terminal-tab-ctx-item terminal-tab-ctx-danger";
563
+ closeItem.innerHTML = '<i data-lucide="trash-2" style="width:13px;height:13px"></i> Close';
564
+ closeItem.addEventListener("click", function () {
565
+ menu.remove();
566
+ closeTab(tab.id);
567
+ });
568
+ menu.appendChild(closeItem);
569
+
570
+ document.body.appendChild(menu);
571
+ refreshIcons();
572
+
573
+ // Position near the button
574
+ var rect = e.currentTarget.getBoundingClientRect();
575
+ menu.style.top = (rect.bottom + 4) + "px";
576
+ menu.style.left = rect.left + "px";
577
+
578
+ // Dismiss on outside click
579
+ function dismiss(ev) {
580
+ if (!menu.contains(ev.target)) {
581
+ menu.remove();
582
+ document.removeEventListener("mousedown", dismiss, true);
583
+ }
584
+ }
585
+ setTimeout(function () {
586
+ document.addEventListener("mousedown", dismiss, true);
587
+ }, 0);
588
+ }
589
+
590
+ // --- Rename tab inline ---
591
+ function startRenameTab(tab, labelEl) {
592
+ var input = document.createElement("input");
593
+ input.className = "terminal-tab-rename";
594
+ input.value = tab.title;
595
+ input.maxLength = 50;
596
+
597
+ labelEl.replaceWith(input);
598
+ input.focus();
599
+ input.select();
600
+
601
+ function commit() {
602
+ var newTitle = input.value.trim();
603
+ if (newTitle && newTitle !== tab.title) {
604
+ tab.title = newTitle;
605
+ if (ctx.ws && ctx.connected) {
606
+ ctx.ws.send(JSON.stringify({ type: "term_rename", id: tab.id, title: newTitle }));
607
+ }
608
+ }
609
+ renderTabBar();
610
+ }
611
+
612
+ input.addEventListener("blur", commit);
613
+ input.addEventListener("keydown", function (e) {
614
+ if (e.key === "Enter") { input.blur(); }
615
+ if (e.key === "Escape") {
616
+ input.value = tab.title; // revert
617
+ input.blur();
618
+ }
619
+ e.stopPropagation();
620
+ });
621
+ }
622
+
623
+ // --- Update terminal count badge ---
624
+ function updateTerminalBadge() {
625
+ var countEl = document.getElementById("terminal-count");
626
+ var sidebarCountEl = document.getElementById("terminal-sidebar-count");
627
+
628
+ var count = 0;
629
+ for (var t of tabs.values()) {
630
+ if (!t.exited) count++;
631
+ }
632
+
633
+ if (countEl) {
634
+ if (count > 0) {
635
+ countEl.textContent = count;
636
+ countEl.classList.remove("hidden");
637
+ } else {
638
+ countEl.classList.add("hidden");
639
+ }
640
+ }
641
+
642
+ if (sidebarCountEl) {
643
+ if (count > 0) {
644
+ sidebarCountEl.textContent = count;
645
+ sidebarCountEl.classList.remove("hidden");
646
+ } else {
647
+ sidebarCountEl.classList.add("hidden");
648
+ }
649
+ }
650
+
651
+ var mobileCountEl = document.getElementById("mobile-terminal-count");
652
+ if (mobileCountEl) {
653
+ if (count > 0) {
654
+ mobileCountEl.textContent = count;
655
+ mobileCountEl.classList.remove("hidden");
656
+ } else {
657
+ mobileCountEl.classList.add("hidden");
658
+ }
659
+ }
660
+ }
661
+
662
+ // --- Handle server messages ---
663
+
664
+ export function handleTermList(msg) {
665
+ var serverTerminals = msg.terminals || [];
666
+ var serverIds = new Set();
667
+
668
+ // Add/update tabs from server list
669
+ for (var i = 0; i < serverTerminals.length; i++) {
670
+ var st = serverTerminals[i];
671
+ serverIds.add(st.id);
672
+
673
+ if (tabs.has(st.id)) {
674
+ var existing = tabs.get(st.id);
675
+ existing.title = st.title;
676
+ existing.exited = st.exited;
677
+ } else {
678
+ tabs.set(st.id, {
679
+ id: st.id,
680
+ title: st.title,
681
+ exited: st.exited,
682
+ xterm: null,
683
+ fitAddon: null,
684
+ bodyEl: null,
685
+ });
686
+ }
687
+ }
688
+
689
+ // Remove tabs no longer on server
690
+ for (var id of tabs.keys()) {
691
+ if (!serverIds.has(id)) {
692
+ var removed = tabs.get(id);
693
+ disposeTab(removed);
694
+ tabs.delete(id);
695
+ }
696
+ }
697
+
698
+ // If active tab was removed, switch to first available
699
+ if (activeTabId && !tabs.has(activeTabId)) {
700
+ activeTabId = null;
701
+ }
702
+
703
+ renderTabBar();
704
+
705
+ // If panel is open and we have tabs, re-attach
706
+ if (isOpen && tabs.size > 0) {
707
+ if (!activeTabId) {
708
+ activeTabId = tabs.keys().next().value;
709
+ }
710
+ activateTab(activeTabId);
711
+ }
712
+
713
+ // If panel is open and all tabs are gone, close panel
714
+ if (isOpen && tabs.size === 0) {
715
+ closeTerminal();
716
+ }
717
+ }
718
+
719
+ export function handleTermCreated(msg) {
720
+ // Switch to the newly created tab
721
+ if (msg.id && tabs.has(msg.id)) {
722
+ activateTab(msg.id);
723
+ }
724
+ }
725
+
726
+ export function handleTermOutput(msg) {
727
+ if (!msg.id) return;
728
+ var tab = tabs.get(msg.id);
729
+ if (tab && tab.xterm && msg.data) {
730
+ tab.xterm.write(msg.data);
731
+ }
732
+ }
733
+
734
+ export function handleTermResized(msg) {
735
+ if (!msg.id) return;
736
+ var tab = tabs.get(msg.id);
737
+ if (tab && tab.xterm && msg.cols > 0 && msg.rows > 0) {
738
+ tab.xterm.resize(msg.cols, msg.rows);
739
+ }
740
+ }
741
+
742
+ export function handleTermExited(msg) {
743
+ if (!msg.id) return;
744
+ var tab = tabs.get(msg.id);
745
+ if (tab) {
746
+ tab.exited = true;
747
+ if (tab.xterm) {
748
+ tab.xterm.write("\r\n\x1b[90m[Process exited]\x1b[0m\r\n");
749
+ }
750
+ renderTabBar();
751
+ }
752
+ }
753
+
754
+ export function handleTermClosed(msg) {
755
+ if (!msg.id) return;
756
+ var tab = tabs.get(msg.id);
757
+ if (tab) {
758
+ disposeTab(tab);
759
+ tabs.delete(msg.id);
760
+
761
+ if (activeTabId === msg.id) {
762
+ activeTabId = null;
763
+ if (tabs.size > 0) {
764
+ activeTabId = tabs.keys().next().value;
765
+ activateTab(activeTabId);
766
+ }
767
+ }
768
+
769
+ renderTabBar();
770
+
771
+ // Close panel if no tabs left
772
+ if (isOpen && tabs.size === 0) {
773
+ closeTerminal();
774
+ }
775
+ }
776
+ }
777
+
778
+ // --- Reset on reconnect ---
779
+ export function resetTerminals() {
780
+ // Dispose all xterm instances (server state survives, client re-syncs via term_list)
781
+ for (var tab of tabs.values()) {
782
+ disposeTab(tab);
783
+ }
784
+ tabs.clear();
785
+ activeTabId = null;
786
+ cleanupListeners();
787
+ renderTabBar();
788
+ }
789
+
790
+ export function sendTerminalCommand(command) {
791
+ if (!activeTabId || !tabs.has(activeTabId)) return;
792
+ if (ctx.ws && ctx.connected) {
793
+ ctx.ws.send(JSON.stringify({ type: "term_input", id: activeTabId, data: command }));
794
+ }
795
+ }
796
+
797
+ export function setTerminalTheme(xtermTheme) {
798
+ for (var tab of tabs.values()) {
799
+ if (tab.xterm) {
800
+ tab.xterm.options.theme = xtermTheme;
801
+ }
802
+ }
803
+ }
804
+
805
+ // --- Terminal context menu ---
806
+ function closeTermCtxMenu() {
807
+ if (termCtxMenu) {
808
+ termCtxMenu.remove();
809
+ termCtxMenu = null;
810
+ }
811
+ }
812
+
813
+ function showTermCtxMenu(e, tab) {
814
+ e.preventDefault();
815
+ e.stopPropagation();
816
+ closeTermCtxMenu();
817
+
818
+ var menu = document.createElement("div");
819
+ menu.className = "term-ctx-menu";
820
+
821
+ // Copy selection
822
+ var sel = tab.xterm ? tab.xterm.getSelection() : "";
823
+ if (sel) {
824
+ var copySelItem = document.createElement("button");
825
+ copySelItem.className = "term-ctx-item";
826
+ copySelItem.innerHTML = iconHtml("copy") + " <span>Copy</span>";
827
+ copySelItem.addEventListener("click", function (ev) {
828
+ ev.stopPropagation();
829
+ closeTermCtxMenu();
830
+ if (sel) copyToClipboard(sel);
831
+ });
832
+ menu.appendChild(copySelItem);
833
+ }
834
+
835
+ // Copy entire console
836
+ var copyItem = document.createElement("button");
837
+ copyItem.className = "term-ctx-item";
838
+ copyItem.innerHTML = iconHtml("clipboard-copy") + " <span>Copy Console</span>";
839
+ copyItem.addEventListener("click", function (ev) {
840
+ ev.stopPropagation();
841
+ closeTermCtxMenu();
842
+ if (!tab.xterm) return;
843
+ tab.xterm.selectAll();
844
+ var text = tab.xterm.getSelection();
845
+ tab.xterm.clearSelection();
846
+ if (text) copyToClipboard(text);
847
+ });
848
+ menu.appendChild(copyItem);
849
+
850
+ // Paste
851
+ var pasteItem = document.createElement("button");
852
+ pasteItem.className = "term-ctx-item";
853
+ pasteItem.innerHTML = iconHtml("clipboard-paste") + " <span>Paste</span>";
854
+ pasteItem.addEventListener("click", function (ev) {
855
+ ev.stopPropagation();
856
+ closeTermCtxMenu();
857
+ if (!tab.xterm) return;
858
+ if (navigator.clipboard && navigator.clipboard.readText) {
859
+ navigator.clipboard.readText().then(function (text) {
860
+ if (text && ctx.ws && ctx.connected) {
861
+ ctx.ws.send(JSON.stringify({ type: "term_input", id: tab.id, data: text }));
862
+ }
863
+ }).catch(function () { /* permission denied or not available */ });
864
+ }
865
+ });
866
+ menu.appendChild(pasteItem);
867
+
868
+ // Clear
869
+ var clearItem = document.createElement("button");
870
+ clearItem.className = "term-ctx-item";
871
+ clearItem.innerHTML = iconHtml("trash-2") + " <span>Clear Console</span>";
872
+ clearItem.addEventListener("click", function (ev) {
873
+ ev.stopPropagation();
874
+ closeTermCtxMenu();
875
+ if (!tab.xterm) return;
876
+ tab.xterm.clear();
877
+ });
878
+ menu.appendChild(clearItem);
879
+
880
+ // Position at mouse cursor
881
+ menu.style.left = e.clientX + "px";
882
+ menu.style.top = e.clientY + "px";
883
+ document.body.appendChild(menu);
884
+
885
+ // Clamp to viewport
886
+ var rect = menu.getBoundingClientRect();
887
+ if (rect.right > window.innerWidth) {
888
+ menu.style.left = (window.innerWidth - rect.width - 4) + "px";
889
+ }
890
+ if (rect.bottom > window.innerHeight) {
891
+ menu.style.top = (window.innerHeight - rect.height - 4) + "px";
892
+ }
893
+
894
+ termCtxMenu = menu;
895
+ refreshIcons();
896
+
897
+ // Close on outside click (next tick to avoid immediate trigger)
898
+ setTimeout(function () {
899
+ document.addEventListener("click", closeTermCtxMenu, { once: true });
900
+ }, 0);
901
+ }
902
+
903
+ // --- Mobile toolbar ---
904
+ var KEY_MAP = {
905
+ tab: "\t",
906
+ esc: "\x1b",
907
+ up: "\x1b[A",
908
+ down: "\x1b[B",
909
+ right: "\x1b[C",
910
+ left: "\x1b[D",
911
+ pipe: "|",
912
+ slash: "/",
913
+ tilde: "~",
914
+ };
915
+
916
+ function initToolbar(toolbar) {
917
+ if (!toolbarBound) {
918
+ toolbarBound = true;
919
+
920
+ toolbar.addEventListener("mousedown", function (e) { e.preventDefault(); });
921
+
922
+ toolbar.addEventListener("click", function (e) {
923
+ var btn = e.target.closest(".term-key");
924
+ if (!btn) return;
925
+
926
+ var tab = activeTabId ? tabs.get(activeTabId) : null;
927
+ if (!tab || !tab.xterm) return;
928
+
929
+ var key = btn.dataset.key;
930
+ if (!key) return;
931
+
932
+ if (key === "ctrl") {
933
+ ctrlActive = !ctrlActive;
934
+ btn.classList.toggle("active", ctrlActive);
935
+ return;
936
+ }
937
+
938
+ if (key === "alt") {
939
+ altActive = !altActive;
940
+ btn.classList.toggle("active", altActive);
941
+ return;
942
+ }
943
+
944
+ var seq = KEY_MAP[key];
945
+ if (!seq) return;
946
+
947
+ // Alt prefix: send ESC before the character
948
+ if (altActive) {
949
+ seq = "\x1b" + seq;
950
+ altActive = false;
951
+ var altBtn = toolbar.querySelector("[data-key='alt']");
952
+ if (altBtn) altBtn.classList.remove("active");
953
+ }
954
+
955
+ if (ctx.ws && ctx.connected) {
956
+ ctx.ws.send(JSON.stringify({ type: "term_input", id: activeTabId, data: seq }));
957
+ }
958
+
959
+ if (ctrlActive) {
960
+ ctrlActive = false;
961
+ var ctrlBtn = toolbar.querySelector("[data-key='ctrl']");
962
+ if (ctrlBtn) ctrlBtn.classList.remove("active");
963
+ }
964
+ });
965
+ }
966
+
967
+ // Attach Ctrl handler to active terminal
968
+ var tab = activeTabId ? tabs.get(activeTabId) : null;
969
+ if (tab && tab.xterm) {
970
+ tab.xterm.attachCustomKeyEventHandler(function (ev) {
971
+ if (ctrlActive && ev.type === "keydown" && ev.key.length === 1) {
972
+ var charCode = ev.key.toUpperCase().charCodeAt(0);
973
+ if (charCode >= 65 && charCode <= 90) {
974
+ var ctrlChar = String.fromCharCode(charCode - 64);
975
+ if (ctx.ws && ctx.connected) {
976
+ ctx.ws.send(JSON.stringify({ type: "term_input", id: activeTabId, data: ctrlChar }));
977
+ }
978
+ ctrlActive = false;
979
+ var ctrlBtn = document.querySelector("#terminal-toolbar [data-key='ctrl']");
980
+ if (ctrlBtn) ctrlBtn.classList.remove("active");
981
+ return false;
982
+ }
983
+ }
984
+ return true;
985
+ });
986
+ }
987
+ }