@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,562 @@
1
+ // tool-palette.js — Customizable sidebar tool palette
2
+ //
3
+ // Renders the per-session and per-mate tool grids from a data registry,
4
+ // so users can reorder tools and hide ones they don't use. Preserves the
5
+ // original button IDs so existing click handlers (attached by sidebar.js,
6
+ // terminal.js, mcp-ui.js, etc.) keep working on the rendered nodes.
7
+ //
8
+ // Design notes (see issue #325 for the broader context):
9
+ // - Buttons are created once on init and never destroyed; hide/reorder
10
+ // moves existing DOM nodes, so event listeners bound elsewhere survive.
11
+ // - Edit mode is a state on the palette container (class="edit-mode").
12
+ // CSS shows the × remove affordance and enables drag reordering.
13
+ // - Preferences persist per-user via the /api/user/tool-palettes endpoint.
14
+ // An in-flight save is debounced so rapid drag reorders don't spam.
15
+
16
+ import { refreshIcons } from './icons.js';
17
+
18
+ // Registry order = default order for users who haven't customized. Users
19
+ // with saved preferences keep their own order (applyPreferences uses
20
+ // saved order first, then appends any registry entries the user's saved
21
+ // list doesn't mention). So changes here only affect fresh palettes.
22
+ // defaultHidden: true — tool starts in the hidden section for new users.
23
+ // Users who have saved preferences keep their own state.
24
+ var SESSION_TOOLS = [
25
+ { id: "file-browser-btn", icon: "folder-tree", label: "File browser" },
26
+ { id: "terminal-sidebar-btn", icon: "square-terminal", label: "Terminal", countId: "terminal-sidebar-count" },
27
+ { id: "sticky-notes-sidebar-btn", icon: "sticky-note", label: "Sticky Notes", countId: "sticky-notes-sidebar-count" },
28
+ { id: "import-cli-btn", icon: "import", label: "Import CLI" },
29
+ { id: "scheduler-btn", icon: "calendar-clock", label: "Scheduled Tasks", defaultHidden: true },
30
+ { id: "loop-tool-btn", icon: "repeat", label: "Loop", defaultHidden: true },
31
+ { id: "mcp-btn", icon: "cable", label: "MCP Servers", countId: "mcp-sidebar-count", defaultHidden: true },
32
+ { id: "skills-btn", icon: "puzzle", label: "Skills", defaultHidden: true },
33
+ ];
34
+
35
+ var PALETTES = {
36
+ session: {
37
+ tools: SESSION_TOOLS,
38
+ activeContainerId: "session-actions",
39
+ hiddenSectionId: "session-actions-hidden",
40
+ },
41
+ };
42
+
43
+ var _saveTimers = {};
44
+ var _draggingEl = null;
45
+ var _draggingPaletteName = null;
46
+
47
+ export function getSessionTools() {
48
+ return SESSION_TOOLS;
49
+ }
50
+
51
+ export function initToolPalettes() {
52
+ for (var name in PALETTES) {
53
+ buildPalette(name);
54
+ }
55
+
56
+ // Edit pills toggle edit mode on their associated palette.
57
+ var editBtns = document.querySelectorAll('.tool-palette-edit-btn');
58
+ for (var i = 0; i < editBtns.length; i++) {
59
+ (function (btn) {
60
+ btn.addEventListener('click', function () {
61
+ toggleEditMode(btn.dataset.palette);
62
+ });
63
+ })(editBtns[i]);
64
+ }
65
+
66
+ // Keyboard-hotkey hint pill. Matches the icon-strip shortcut pills
67
+ // in shape — just the chord, no prose. Clicking the pill opens the
68
+ // hotkey overlay, same as pressing the shortcut.
69
+ var isMac = /Mac|iPhone|iPod|iPad/i.test(navigator.platform);
70
+ var hintLabel = isMac ? '\u2318O' : 'Ctrl+O';
71
+ var hints = document.querySelectorAll('.sidebar-tools-hint');
72
+ for (var h = 0; h < hints.length; h++) {
73
+ hints[h].textContent = hintLabel;
74
+ hints[h].title = 'Show keyboard hotkeys (' + hintLabel + ')';
75
+ hints[h].addEventListener('click', function (e) {
76
+ // Stop the click from bubbling to the document-level dismiss
77
+ // handler below, which would immediately tear down the pick-mode
78
+ // we just entered.
79
+ e.stopPropagation();
80
+ if (_pickActive) exitToolPickMode();
81
+ else enterToolPickMode();
82
+ });
83
+ }
84
+
85
+ // Load saved preferences and apply to both palettes once buttons exist.
86
+ loadPreferences();
87
+
88
+ refreshIcons();
89
+ }
90
+
91
+ function buildPalette(name) {
92
+ var palette = PALETTES[name];
93
+ var active = document.getElementById(palette.activeContainerId);
94
+ if (!active) return;
95
+
96
+ // Scaffold the hidden section before building buttons so defaultHidden
97
+ // tools have somewhere to go.
98
+ var hiddenSection = document.getElementById(palette.hiddenSectionId);
99
+ var hiddenGrid = null;
100
+ if (hiddenSection) {
101
+ hiddenSection.innerHTML = '';
102
+ var labelEl = document.createElement('div');
103
+ labelEl.className = 'tool-palette-hidden-label';
104
+ labelEl.textContent = 'Add back';
105
+ hiddenGrid = document.createElement('div');
106
+ hiddenGrid.className = 'tool-palette-hidden-grid';
107
+ hiddenSection.appendChild(labelEl);
108
+ hiddenSection.appendChild(hiddenGrid);
109
+ }
110
+
111
+ // Create buttons with stable IDs. defaultHidden tools go into the
112
+ // hidden grid; others go into the active grid. applyPreferences()
113
+ // will rearrange for users who have saved their own layout.
114
+ for (var i = 0; i < palette.tools.length; i++) {
115
+ var tool = palette.tools[i];
116
+ var btn = buildToolButton(tool, name);
117
+ if (tool.defaultHidden && hiddenGrid) {
118
+ btn.draggable = false;
119
+ hiddenGrid.appendChild(btn);
120
+ } else {
121
+ active.appendChild(btn);
122
+ }
123
+ }
124
+
125
+ // Drag-reorder within the active container. Works in both normal and
126
+ // edit mode so users can rearrange without entering a separate mode
127
+ // (matches the macOS dock pattern). HTML5 drag has a small movement
128
+ // threshold, so quick clicks still activate the tool normally.
129
+ active.addEventListener('dragover', function (e) {
130
+ if (_draggingPaletteName !== name || !_draggingEl) return;
131
+ e.preventDefault();
132
+ if (e.dataTransfer) e.dataTransfer.dropEffect = 'move';
133
+ var after = getDragAfterElement(active, e.clientX, e.clientY);
134
+ if (after == null) {
135
+ if (_draggingEl.parentNode !== active || _draggingEl.nextSibling) {
136
+ active.appendChild(_draggingEl);
137
+ }
138
+ } else if (after !== _draggingEl) {
139
+ active.insertBefore(_draggingEl, after);
140
+ }
141
+ });
142
+ active.addEventListener('drop', function (e) {
143
+ if (_draggingPaletteName !== name) return;
144
+ e.preventDefault();
145
+ });
146
+ }
147
+
148
+ function buildToolButton(tool, paletteName) {
149
+ var btn = document.createElement('button');
150
+ btn.id = tool.id;
151
+ btn.type = 'button';
152
+ btn.className = 'palette-tile';
153
+ btn.title = tool.label;
154
+ btn.setAttribute('aria-label', tool.label);
155
+ btn.dataset.toolId = tool.id;
156
+ btn.dataset.palette = paletteName;
157
+ // Draggable at all times so users can reorder without entering edit
158
+ // mode. Drag is gated to the active grid in the handlers below; tiles
159
+ // in the hidden grid get draggable=false set on move.
160
+ btn.draggable = true;
161
+
162
+ var icon = document.createElement('i');
163
+ icon.setAttribute('data-lucide', tool.icon);
164
+ btn.appendChild(icon);
165
+
166
+ var labelEl = document.createElement('span');
167
+ labelEl.className = 'tool-btn-label';
168
+ labelEl.textContent = tool.label;
169
+ btn.appendChild(labelEl);
170
+
171
+ if (tool.countId) {
172
+ var count = document.createElement('span');
173
+ count.id = tool.countId;
174
+ count.className = 'sidebar-badge hidden';
175
+ btn.appendChild(count);
176
+ }
177
+
178
+ // × affordance — shown only in edit mode via CSS.
179
+ var remove = document.createElement('span');
180
+ remove.className = 'tool-palette-remove';
181
+ remove.setAttribute('role', 'button');
182
+ remove.setAttribute('aria-label', 'Remove ' + tool.label + ' from palette');
183
+ remove.textContent = '\u00D7';
184
+ btn.appendChild(remove);
185
+
186
+ // Click interceptor, registered before any other module attaches to
187
+ // the button by ID. stopImmediatePropagation prevents the tool's own
188
+ // handler from running in three cases: clicking the × in edit mode,
189
+ // clicking a tile in the hidden-grid (which should restore it), and
190
+ // clicking any tile while edit mode is active (tiles are inert then).
191
+ btn.addEventListener('click', function (e) {
192
+ if (e.target.closest('.tool-palette-remove')) {
193
+ e.preventDefault();
194
+ e.stopImmediatePropagation();
195
+ moveToHidden(paletteName, tool.id);
196
+ return;
197
+ }
198
+ var container = btn.parentNode;
199
+ if (!container) return;
200
+ if (container.classList.contains('tool-palette-hidden-grid')) {
201
+ e.preventDefault();
202
+ e.stopImmediatePropagation();
203
+ moveToActive(paletteName, tool.id);
204
+ return;
205
+ }
206
+ if (container.classList.contains('edit-mode')) {
207
+ e.preventDefault();
208
+ e.stopImmediatePropagation();
209
+ }
210
+ });
211
+
212
+ btn.addEventListener('contextmenu', function (e) {
213
+ e.preventDefault();
214
+ e.stopPropagation();
215
+ var container = btn.parentNode;
216
+ var inHidden = container && container.classList.contains('tool-palette-hidden-grid');
217
+ openPaletteContextMenu(e.clientX, e.clientY, inHidden ? [
218
+ { label: 'Add back to palette', action: function () { moveToActive(paletteName, tool.id); } },
219
+ ] : [
220
+ { label: 'Remove from palette', action: function () { moveToHidden(paletteName, tool.id); } },
221
+ { label: 'Edit palette\u2026', action: function () {
222
+ if (!document.getElementById(PALETTES[paletteName].activeContainerId).classList.contains('edit-mode')) {
223
+ toggleEditMode(paletteName);
224
+ }
225
+ } },
226
+ ]);
227
+ });
228
+
229
+ btn.addEventListener('dragstart', function (e) {
230
+ var container = btn.parentNode;
231
+ // Only allow drag from the active grid; hidden tiles are added back
232
+ // via click, not drag.
233
+ if (!container || container.id !== PALETTES[paletteName].activeContainerId) {
234
+ e.preventDefault();
235
+ return;
236
+ }
237
+ _draggingEl = btn;
238
+ _draggingPaletteName = paletteName;
239
+ e.dataTransfer.effectAllowed = 'move';
240
+ // Firefox requires data to be set for drag to start.
241
+ try { e.dataTransfer.setData('text/plain', tool.id); } catch (err) { /* ignore */ }
242
+ btn.classList.add('dragging');
243
+ });
244
+ btn.addEventListener('dragend', function () {
245
+ btn.classList.remove('dragging');
246
+ if (_draggingPaletteName) queueSave(_draggingPaletteName);
247
+ _draggingEl = null;
248
+ _draggingPaletteName = null;
249
+ });
250
+
251
+ return btn;
252
+ }
253
+
254
+ function toggleEditMode(name) {
255
+ var palette = PALETTES[name];
256
+ if (!palette) return;
257
+ var active = document.getElementById(palette.activeContainerId);
258
+ var hidden = document.getElementById(palette.hiddenSectionId);
259
+ var editBtn = document.querySelector('.tool-palette-edit-btn[data-palette="' + name + '"]');
260
+ if (!active || !editBtn) return;
261
+
262
+ var entering = !active.classList.contains('edit-mode');
263
+ active.classList.toggle('edit-mode', entering);
264
+ if (hidden) {
265
+ // Show hidden-section in edit mode if any items exist there (or to
266
+ // allow adding back even when empty, keep visible so the state is
267
+ // legible). We show whenever edit mode is on.
268
+ hidden.classList.toggle('hidden', !entering);
269
+ }
270
+ // Pencil icon stays; the .active class provides visual feedback by
271
+ // swapping to the accent background. No label text swap needed now.
272
+ editBtn.classList.toggle('active', entering);
273
+
274
+ if (!entering) queueSave(name);
275
+ }
276
+
277
+ function moveToHidden(name, toolId) {
278
+ var palette = PALETTES[name];
279
+ if (!palette) return;
280
+ var btn = document.getElementById(toolId);
281
+ var hiddenSection = document.getElementById(palette.hiddenSectionId);
282
+ if (!btn || !hiddenSection) return;
283
+ var grid = hiddenSection.querySelector('.tool-palette-hidden-grid');
284
+ if (!grid) return;
285
+ // Hidden tiles stay draggable=false so users don't drag them around
286
+ // before adding them back. The active-grid dragstart gate also
287
+ // enforces this.
288
+ btn.draggable = false;
289
+ grid.appendChild(btn);
290
+ queueSave(name);
291
+ }
292
+
293
+ function moveToActive(name, toolId) {
294
+ var palette = PALETTES[name];
295
+ if (!palette) return;
296
+ var btn = document.getElementById(toolId);
297
+ var active = document.getElementById(palette.activeContainerId);
298
+ if (!btn || !active) return;
299
+ active.appendChild(btn);
300
+ btn.draggable = true;
301
+ queueSave(name);
302
+ }
303
+
304
+ // (Hidden-tile clicks are handled in each button's own click listener,
305
+ // attached in buildToolButton before any external module attaches
306
+ // handlers by ID. See the stopImmediatePropagation logic there.)
307
+
308
+ // --- Right-click context menu ---
309
+
310
+ var _openMenu = null;
311
+
312
+ function openPaletteContextMenu(x, y, items) {
313
+ closePaletteContextMenu();
314
+ var menu = document.createElement('div');
315
+ menu.className = 'tool-palette-ctx-menu';
316
+ for (var i = 0; i < items.length; i++) {
317
+ (function (item) {
318
+ var btn = document.createElement('button');
319
+ btn.type = 'button';
320
+ btn.className = 'tool-palette-ctx-item';
321
+ btn.textContent = item.label;
322
+ btn.addEventListener('click', function (e) {
323
+ e.stopPropagation();
324
+ closePaletteContextMenu();
325
+ item.action();
326
+ });
327
+ menu.appendChild(btn);
328
+ })(items[i]);
329
+ }
330
+ document.body.appendChild(menu);
331
+ // Clamp to viewport.
332
+ var rect = menu.getBoundingClientRect();
333
+ var px = x, py = y;
334
+ if (px + rect.width > window.innerWidth - 4) px = window.innerWidth - rect.width - 4;
335
+ if (py + rect.height > window.innerHeight - 4) py = window.innerHeight - rect.height - 4;
336
+ menu.style.left = px + 'px';
337
+ menu.style.top = py + 'px';
338
+ _openMenu = menu;
339
+ }
340
+
341
+ function closePaletteContextMenu() {
342
+ if (_openMenu && _openMenu.parentNode) _openMenu.parentNode.removeChild(_openMenu);
343
+ _openMenu = null;
344
+ }
345
+
346
+ document.addEventListener('click', function () { closePaletteContextMenu(); });
347
+ document.addEventListener('keydown', function (e) {
348
+ if (e.key === 'Escape') closePaletteContextMenu();
349
+ });
350
+ window.addEventListener('blur', function () { closePaletteContextMenu(); });
351
+ window.addEventListener('resize', function () { closePaletteContextMenu(); });
352
+
353
+ // --- Hotkey overlay (Cmd/Ctrl + O) ---
354
+ //
355
+ // Press Cmd/Ctrl + O to reveal a hotkey badge on each visible palette
356
+ // tile. Then press the shown digit (1..9, 0) or letter (a..z) to
357
+ // activate that tool. Escape or clicking away dismisses. Inspired by
358
+ // Vimium / Superhuman-style "pick a tile with one keystroke" flows.
359
+
360
+ var _pickActive = false;
361
+ var HOTKEYS = (function () {
362
+ var k = [];
363
+ for (var i = 1; i <= 9; i++) k.push(String(i));
364
+ k.push('0');
365
+ for (var c = 97; c <= 122; c++) k.push(String.fromCharCode(c));
366
+ return k;
367
+ })();
368
+
369
+ function getVisiblePaletteName() {
370
+ // offsetParent is null when the element (or an ancestor) is hidden.
371
+ var sessionActive = document.getElementById('session-actions');
372
+ if (sessionActive && sessionActive.offsetParent !== null) return 'session';
373
+ return null;
374
+ }
375
+
376
+ function enterToolPickMode() {
377
+ if (_pickActive) return;
378
+ var name = getVisiblePaletteName();
379
+ if (!name) return;
380
+ var active = document.getElementById(PALETTES[name].activeContainerId);
381
+ if (!active) return;
382
+ var tiles = active.querySelectorAll('[data-tool-id]');
383
+ if (tiles.length === 0) return;
384
+ for (var i = 0; i < tiles.length && i < HOTKEYS.length; i++) {
385
+ var key = HOTKEYS[i];
386
+ tiles[i].dataset.hotkey = key;
387
+ var badge = document.createElement('span');
388
+ badge.className = 'tool-palette-hotkey-badge';
389
+ badge.textContent = key.toUpperCase();
390
+ tiles[i].appendChild(badge);
391
+ }
392
+ _pickActive = true;
393
+ }
394
+
395
+ function exitToolPickMode() {
396
+ if (!_pickActive) return;
397
+ _pickActive = false;
398
+ var badges = document.querySelectorAll('.tool-palette-hotkey-badge');
399
+ for (var i = 0; i < badges.length; i++) {
400
+ if (badges[i].parentNode) badges[i].parentNode.removeChild(badges[i]);
401
+ }
402
+ var marked = document.querySelectorAll('[data-hotkey]');
403
+ for (var j = 0; j < marked.length; j++) {
404
+ delete marked[j].dataset.hotkey;
405
+ }
406
+ }
407
+
408
+ // Capture phase so we reliably preempt typing and browser shortcuts
409
+ // when focus is in an input.
410
+ document.addEventListener('keydown', function (e) {
411
+ // Pick mode: consume the next printable key.
412
+ if (_pickActive) {
413
+ if (e.key === 'Escape') {
414
+ e.preventDefault();
415
+ exitToolPickMode();
416
+ return;
417
+ }
418
+ // Ignore modifier-only keys; react to single-character keys.
419
+ if (e.key.length !== 1) return;
420
+ if (e.ctrlKey || e.metaKey || e.altKey) return;
421
+ var key = e.key.toLowerCase();
422
+ var tile = document.querySelector('[data-hotkey="' + key + '"]');
423
+ if (tile) {
424
+ e.preventDefault();
425
+ exitToolPickMode();
426
+ tile.click();
427
+ } else {
428
+ // Unknown key exits pick mode without triggering a tile.
429
+ exitToolPickMode();
430
+ }
431
+ return;
432
+ }
433
+
434
+ // Enter pick mode on Cmd/Ctrl + O (letter O). Intercepted globally
435
+ // including inside text inputs — the browser's native Cmd+O is a
436
+ // file-picker dialog Clay never wants, so taking it over costs the
437
+ // user nothing and is especially useful when jumping to a tool from
438
+ // the session search box.
439
+ if ((e.metaKey || e.ctrlKey) && !e.altKey && !e.shiftKey && e.key && e.key.toLowerCase() === 'o') {
440
+ if (!getVisiblePaletteName()) return;
441
+ e.preventDefault();
442
+ enterToolPickMode();
443
+ }
444
+ }, true);
445
+
446
+ // Dismiss the overlay on click-away, blur, or viewport changes so it
447
+ // never lingers invisibly.
448
+ document.addEventListener('click', function () { if (_pickActive) exitToolPickMode(); });
449
+ window.addEventListener('blur', function () { if (_pickActive) exitToolPickMode(); });
450
+ window.addEventListener('resize', function () { if (_pickActive) exitToolPickMode(); });
451
+
452
+ function getDragAfterElement(container, x, y) {
453
+ var tiles = Array.prototype.slice.call(
454
+ container.querySelectorAll('[data-tool-id]:not(.dragging)')
455
+ );
456
+ var closest = null;
457
+ var closestDist = Number.POSITIVE_INFINITY;
458
+ for (var i = 0; i < tiles.length; i++) {
459
+ var rect = tiles[i].getBoundingClientRect();
460
+ // Pointer is "before" the tile if it's left of the tile's horizontal
461
+ // midpoint on the same row (or on a higher row).
462
+ var rowDelta = y - (rect.top + rect.height / 2);
463
+ var colDelta = x - (rect.left + rect.width / 2);
464
+ // Weight row heavier than column so flowing between rows works.
465
+ var dist = Math.abs(rowDelta) * 2 + Math.abs(colDelta);
466
+ var before = rowDelta < -rect.height / 2
467
+ || (Math.abs(rowDelta) <= rect.height / 2 && colDelta < 0);
468
+ if (before && dist < closestDist) {
469
+ closestDist = dist;
470
+ closest = tiles[i];
471
+ }
472
+ }
473
+ return closest;
474
+ }
475
+
476
+ // --- Persistence ---
477
+
478
+ function loadPreferences() {
479
+ fetch('/api/user/tool-palettes', { credentials: 'same-origin' })
480
+ .then(function (res) { return res.ok ? res.json() : null; })
481
+ .then(function (data) {
482
+ if (!data) return;
483
+ applyPreferences('session', data.session || null);
484
+ applyPreferences('mate', data.mate || null);
485
+ })
486
+ .catch(function () { /* first-time users have no saved prefs; ignore */ });
487
+ }
488
+
489
+ function applyPreferences(name, prefs) {
490
+ if (!prefs) return;
491
+ var palette = PALETTES[name];
492
+ if (!palette) return;
493
+ var active = document.getElementById(palette.activeContainerId);
494
+ var hiddenSection = document.getElementById(palette.hiddenSectionId);
495
+ var hiddenGrid = hiddenSection ? hiddenSection.querySelector('.tool-palette-hidden-grid') : null;
496
+ if (!active || !hiddenGrid) return;
497
+
498
+ var hiddenSet = {};
499
+ var hiddenList = prefs.hidden || [];
500
+ for (var i = 0; i < hiddenList.length; i++) hiddenSet[hiddenList[i]] = true;
501
+
502
+ var orderList = prefs.order || [];
503
+ // Any tools not in the stored order are appended in registry order so
504
+ // newly-added tools surface automatically for existing users.
505
+ var placed = {};
506
+ for (var j = 0; j < orderList.length; j++) {
507
+ var id = orderList[j];
508
+ if (hiddenSet[id]) continue;
509
+ var btn = document.getElementById(id);
510
+ if (btn) {
511
+ active.appendChild(btn);
512
+ placed[id] = true;
513
+ }
514
+ }
515
+ for (var k = 0; k < palette.tools.length; k++) {
516
+ var tid = palette.tools[k].id;
517
+ if (placed[tid] || hiddenSet[tid]) continue;
518
+ var tbtn = document.getElementById(tid);
519
+ if (tbtn) active.appendChild(tbtn);
520
+ }
521
+ for (var h = 0; h < hiddenList.length; h++) {
522
+ var hbtn = document.getElementById(hiddenList[h]);
523
+ if (hbtn) {
524
+ hbtn.draggable = false;
525
+ hiddenGrid.appendChild(hbtn);
526
+ }
527
+ }
528
+ }
529
+
530
+ function queueSave(name) {
531
+ if (_saveTimers[name]) clearTimeout(_saveTimers[name]);
532
+ _saveTimers[name] = setTimeout(function () {
533
+ _saveTimers[name] = null;
534
+ savePreferences(name);
535
+ }, 250);
536
+ }
537
+
538
+ function savePreferences(name) {
539
+ var palette = PALETTES[name];
540
+ if (!palette) return;
541
+ var active = document.getElementById(palette.activeContainerId);
542
+ var hiddenSection = document.getElementById(palette.hiddenSectionId);
543
+ var hiddenGrid = hiddenSection ? hiddenSection.querySelector('.tool-palette-hidden-grid') : null;
544
+ if (!active) return;
545
+
546
+ var order = [];
547
+ var activeTiles = active.querySelectorAll('[data-tool-id]');
548
+ for (var i = 0; i < activeTiles.length; i++) order.push(activeTiles[i].dataset.toolId);
549
+
550
+ var hidden = [];
551
+ if (hiddenGrid) {
552
+ var hiddenTiles = hiddenGrid.querySelectorAll('[data-tool-id]');
553
+ for (var j = 0; j < hiddenTiles.length; j++) hidden.push(hiddenTiles[j].dataset.toolId);
554
+ }
555
+
556
+ fetch('/api/user/tool-palettes', {
557
+ method: 'PUT',
558
+ credentials: 'same-origin',
559
+ headers: { 'Content-Type': 'application/json' },
560
+ body: JSON.stringify({ palette: name, order: order, hidden: hidden }),
561
+ }).catch(function () { /* save best-effort */ });
562
+ }