@clagentic/console 1.12.0 → 1.13.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 (243) hide show
  1. package/README.md +7 -1
  2. package/bin/cli.js +18 -15
  3. package/lib/agent-roster-ws.js +38 -13
  4. package/lib/agent-roster.js +111 -12
  5. package/lib/agents.js +68 -33
  6. package/lib/atomic-install.js +334 -0
  7. package/lib/build-changes.js +33 -4
  8. package/lib/build-sha.json +2 -2
  9. package/lib/build-update-offer.js +12 -0
  10. package/lib/chat-width.js +31 -0
  11. package/lib/cli/agent-usage.js +126 -0
  12. package/lib/cli/arg-validation.js +1 -1
  13. package/lib/cli/compact-output.js +69 -0
  14. package/lib/cli/ipc-subcommands.js +92 -67
  15. package/lib/cli/output.js +28 -0
  16. package/lib/cli/restart-verb.js +132 -0
  17. package/lib/cli/schedule-input-subcommands.js +45 -8
  18. package/lib/cli/session-subcommands.js +37 -11
  19. package/lib/cli/settings-subcommands.js +74 -0
  20. package/lib/cli-transcript-title.js +56 -3
  21. package/lib/client-files.js +79 -0
  22. package/lib/daemon.js +100 -28
  23. package/lib/default-avatar-color.js +4 -0
  24. package/lib/drain.js +58 -10
  25. package/lib/mcp-tool-input.js +2 -2
  26. package/lib/model-families.js +49 -1
  27. package/lib/notify/type-registry.js +4 -0
  28. package/lib/notify/types.js +7 -4
  29. package/lib/pages.js +13 -63
  30. package/lib/permission-summary.js +12 -16
  31. package/lib/plan-usage-reader.js +70 -0
  32. package/lib/project-connection.js +4 -2
  33. package/lib/project-filesystem.js +6 -7
  34. package/lib/project-notifications.js +55 -8
  35. package/lib/project-prompt-expiry-settings.js +42 -0
  36. package/lib/project-sessions.js +52 -0
  37. package/lib/project-settings-gate.js +98 -0
  38. package/lib/project-user-mention.js +3 -2
  39. package/lib/project.js +1 -1
  40. package/lib/prompt-expiry-settings.js +49 -0
  41. package/lib/prompt-expiry.js +24 -3
  42. package/lib/prompt-kinds/codecs.js +6 -2
  43. package/lib/public/app.js +36 -36
  44. package/lib/public/css/admin.css +156 -170
  45. package/lib/public/css/agent-messages.css +24 -24
  46. package/lib/public/css/agent-picker.css +90 -38
  47. package/lib/public/css/agent-roster.css +61 -27
  48. package/lib/public/css/at-agents.css +10 -10
  49. package/lib/public/css/base.css +207 -184
  50. package/lib/public/css/chat-layout.css +228 -0
  51. package/lib/public/css/command-palette.css +86 -88
  52. package/lib/public/css/diagnostics.css +78 -204
  53. package/lib/public/css/diff.css +10 -10
  54. package/lib/public/css/filebrowser.css +32 -60
  55. package/lib/public/css/home-hub.css +100 -100
  56. package/lib/public/css/icon-strip.css +195 -238
  57. package/lib/public/css/input.css +191 -212
  58. package/lib/public/css/login-panel.css +16 -16
  59. package/lib/public/css/loop.css +96 -187
  60. package/lib/public/css/mention.css +49 -110
  61. package/lib/public/css/menus.css +162 -169
  62. package/lib/public/css/messages.css +49 -123
  63. package/lib/public/css/mobile-nav.css +207 -264
  64. package/lib/public/css/notifications-center.css +45 -124
  65. package/lib/public/css/overlay.css +269 -0
  66. package/lib/public/css/overlays.css +134 -156
  67. package/lib/public/css/playbook.css +41 -41
  68. package/lib/public/css/profile.css +43 -112
  69. package/lib/public/css/rewind.css +112 -112
  70. package/lib/public/css/scheduler-modal.css +27 -135
  71. package/lib/public/css/scheduler.css +204 -210
  72. package/lib/public/css/server-settings.css +173 -239
  73. package/lib/public/css/session-search.css +20 -20
  74. package/lib/public/css/settings-nav.css +197 -0
  75. package/lib/public/css/sidebar.css +213 -304
  76. package/lib/public/css/skills.css +121 -135
  77. package/lib/public/css/sticky-notes.css +114 -200
  78. package/lib/public/css/stt.css +17 -26
  79. package/lib/public/css/team-panel.css +36 -36
  80. package/lib/public/css/team-view.css +9 -9
  81. package/lib/public/css/title-bar.css +83 -100
  82. package/lib/public/css/tooltip.css +10 -10
  83. package/lib/public/css/user-settings.css +78 -131
  84. package/lib/public/css/user-shell.css +125 -29
  85. package/lib/public/index.html +178 -177
  86. package/lib/public/manifest.json +0 -2
  87. package/lib/public/modules/admin.js +54 -92
  88. package/lib/public/modules/agent-avatar.js +134 -3
  89. package/lib/public/modules/agent-messages.js +2 -1
  90. package/lib/public/modules/agent-picker.js +23 -21
  91. package/lib/public/modules/agent-roster-groups.js +128 -0
  92. package/lib/public/modules/agent-roster.js +40 -11
  93. package/lib/public/modules/app-connection.js +13 -1
  94. package/lib/public/modules/app-cursors.js +9 -9
  95. package/lib/public/modules/app-favicon.js +8 -16
  96. package/lib/public/modules/app-header.js +28 -19
  97. package/lib/public/modules/app-history-replay.js +67 -4
  98. package/lib/public/modules/app-home-hub.js +15 -0
  99. package/lib/public/modules/app-loop-ui.js +41 -47
  100. package/lib/public/modules/app-loop-wizard.js +9 -10
  101. package/lib/public/modules/app-messages.js +32 -36
  102. package/lib/public/modules/app-misc.js +22 -92
  103. package/lib/public/modules/app-modals.js +98 -0
  104. package/lib/public/modules/app-notifications.js +124 -135
  105. package/lib/public/modules/app-panels.js +28 -32
  106. package/lib/public/modules/app-projects.js +30 -40
  107. package/lib/public/modules/app-rate-limit.js +26 -9
  108. package/lib/public/modules/app-rendering.js +128 -110
  109. package/lib/public/modules/app-skills-install.js +6 -4
  110. package/lib/public/modules/assistant-identity.js +55 -0
  111. package/lib/public/modules/at-agents.js +3 -0
  112. package/lib/public/modules/build-guard.js +25 -11
  113. package/lib/public/modules/chat-width-choice.js +75 -0
  114. package/lib/public/modules/chat-width-prefs.js +52 -0
  115. package/lib/public/modules/chat-width.js +102 -0
  116. package/lib/public/modules/collapsible-text.js +36 -4
  117. package/lib/public/modules/command-palette.js +28 -34
  118. package/lib/public/modules/context-sources.js +3 -5
  119. package/lib/public/modules/diagnostics.js +130 -129
  120. package/lib/public/modules/info-panel.js +31 -0
  121. package/lib/public/modules/injected-identity.js +8 -8
  122. package/lib/public/modules/input-list-menu.js +39 -0
  123. package/lib/public/modules/input.js +22 -21
  124. package/lib/public/modules/markdown.js +10 -11
  125. package/lib/public/modules/mcp-ui.js +4 -14
  126. package/lib/public/modules/mention.js +100 -229
  127. package/lib/public/modules/message-header.js +80 -0
  128. package/lib/public/modules/message-participants.js +39 -0
  129. package/lib/public/modules/mobile-action-sheet.js +46 -0
  130. package/lib/public/modules/mobile-nav-sheet.js +104 -0
  131. package/lib/public/modules/model-families.js +41 -4
  132. package/lib/public/modules/note-order.js +39 -0
  133. package/lib/public/modules/notifications.js +35 -52
  134. package/lib/public/modules/overlay-history.js +73 -19
  135. package/lib/public/modules/overlay-menu.js +123 -0
  136. package/lib/public/modules/overlay-modal.js +186 -0
  137. package/lib/public/modules/overlay-registry.js +392 -0
  138. package/lib/public/modules/overlay-stack.js +110 -0
  139. package/lib/public/modules/overlay.js +117 -0
  140. package/lib/public/modules/popover-position.js +18 -2
  141. package/lib/public/modules/preference-sync.js +28 -0
  142. package/lib/public/modules/profile.js +24 -49
  143. package/lib/public/modules/project-settings.js +27 -172
  144. package/lib/public/modules/prompt-expiry-controls.js +46 -0
  145. package/lib/public/modules/prompt-kinds/command-summary.js +149 -0
  146. package/lib/public/modules/prompt-kinds/permission.js +70 -92
  147. package/lib/public/modules/prompts.js +1 -2
  148. package/lib/public/modules/qrcode.js +6 -21
  149. package/lib/public/modules/queued-button.js +15 -2
  150. package/lib/public/modules/rewind.js +9 -3
  151. package/lib/public/modules/schedule-time.js +32 -7
  152. package/lib/public/modules/scheduled-outbox.js +51 -18
  153. package/lib/public/modules/scheduler-config.js +87 -127
  154. package/lib/public/modules/scheduler.js +29 -26
  155. package/lib/public/modules/serialized-save.js +79 -0
  156. package/lib/public/modules/server-settings.js +75 -65
  157. package/lib/public/modules/session-grants.js +16 -19
  158. package/lib/public/modules/session-list-avatars.js +98 -0
  159. package/lib/public/modules/session-vendor-state.js +86 -0
  160. package/lib/public/modules/settings-access.js +35 -0
  161. package/lib/public/modules/settings-hub.js +125 -0
  162. package/lib/public/modules/settings-nav.js +182 -0
  163. package/lib/public/modules/shell-line.js +24 -0
  164. package/lib/public/modules/sidebar-mobile.js +151 -195
  165. package/lib/public/modules/sidebar-projects.js +88 -129
  166. package/lib/public/modules/sidebar-sessions.js +66 -34
  167. package/lib/public/modules/sidebar.js +2 -2
  168. package/lib/public/modules/skills.js +11 -26
  169. package/lib/public/modules/sticky-notes.js +19 -16
  170. package/lib/public/modules/stt.js +9 -23
  171. package/lib/public/modules/terminal.js +86 -90
  172. package/lib/public/modules/theme-choice.js +126 -0
  173. package/lib/public/modules/theme-colors.js +19 -0
  174. package/lib/public/modules/theme-prefs.js +121 -0
  175. package/lib/public/modules/theme.js +227 -605
  176. package/lib/public/modules/tool-palette.js +7 -17
  177. package/lib/public/modules/tools.js +8 -10
  178. package/lib/public/modules/user-settings.js +59 -115
  179. package/lib/public/modules/user-shell.js +9 -2
  180. package/lib/public/modules/utils.js +10 -12
  181. package/lib/public/modules/vendor-picker.js +4 -2
  182. package/lib/public/modules/vendor-registry.js +1 -2
  183. package/lib/public/style.css +3 -1
  184. package/lib/public/sw.js +9 -2
  185. package/lib/public/theme-core.js +411 -0
  186. package/lib/request-body.js +70 -0
  187. package/lib/sdk-bridge.js +227 -88
  188. package/lib/sdk-message-processor.js +44 -16
  189. package/lib/server-admin.js +198 -200
  190. package/lib/server-auth.js +16 -13
  191. package/lib/server-dm.js +4 -3
  192. package/lib/server-settings.js +177 -130
  193. package/lib/server.js +87 -36
  194. package/lib/session-background-tasks.js +79 -0
  195. package/lib/session-directory.js +138 -13
  196. package/lib/session-input-ipc.js +2 -0
  197. package/lib/session-input.js +39 -5
  198. package/lib/session-list-avatars.js +16 -0
  199. package/lib/session-reclaim.js +37 -25
  200. package/lib/session-vendor.js +37 -0
  201. package/lib/sessions.js +99 -34
  202. package/lib/settings-ipc.js +60 -0
  203. package/lib/settings-registry.js +86 -0
  204. package/lib/terminal-manager.js +17 -5
  205. package/lib/theme-files.js +132 -0
  206. package/lib/theme-seed.js +136 -0
  207. package/lib/themes/clagentic-dark.json +13 -6
  208. package/lib/themes/clagentic-light.json +12 -5
  209. package/lib/updater.js +6 -4
  210. package/lib/usage-store.js +97 -10
  211. package/lib/users-preferences.js +118 -19
  212. package/lib/users.js +19 -10
  213. package/lib/vendor-catalog.js +130 -0
  214. package/lib/ws-schema.js +4 -2
  215. package/lib/yoke/adapters/claude.js +81 -1
  216. package/lib/yoke/adapters/codex-user-shell.js +87 -23
  217. package/lib/yoke/adapters/codex.js +89 -5
  218. package/lib/yoke/codex-binary.js +59 -0
  219. package/package.json +1 -1
  220. package/scripts/check-test-count.js +5 -0
  221. package/scripts/install-local-test.js +50 -9
  222. package/scripts/write-build-sha.js +18 -2
  223. package/lib/public/css/channel-layout.css +0 -329
  224. package/lib/themes/ayu-light.json +0 -9
  225. package/lib/themes/catppuccin-latte.json +0 -9
  226. package/lib/themes/catppuccin-mocha.json +0 -9
  227. package/lib/themes/dracula.json +0 -9
  228. package/lib/themes/everforest-light.json +0 -9
  229. package/lib/themes/everforest.json +0 -9
  230. package/lib/themes/github-light.json +0 -9
  231. package/lib/themes/gruvbox-dark.json +0 -9
  232. package/lib/themes/gruvbox-light.json +0 -9
  233. package/lib/themes/monokai.json +0 -9
  234. package/lib/themes/nord-light.json +0 -9
  235. package/lib/themes/nord.json +0 -9
  236. package/lib/themes/one-dark.json +0 -9
  237. package/lib/themes/one-light.json +0 -9
  238. package/lib/themes/rose-pine-dawn.json +0 -9
  239. package/lib/themes/rose-pine.json +0 -9
  240. package/lib/themes/solarized-dark.json +0 -9
  241. package/lib/themes/solarized-light.json +0 -9
  242. package/lib/themes/tokyo-night-light.json +0 -9
  243. package/lib/themes/tokyo-night.json +0 -9
@@ -1,301 +1,103 @@
1
1
  import { iconHtml, refreshIcons } from './icons.js';
2
2
  import { setTerminalTheme } from './terminal.js';
3
3
  import { updateMermaidTheme } from './markdown.js';
4
+ import {
5
+ DARK_ID, LIGHT_ID,
6
+ migrateStoredTheme, resolveThemeId, reconcileServerPrefs
7
+ } from './theme-prefs.js';
8
+ import { createSerializedRequestSaver } from './serialized-save.js';
9
+ import { loadProfilePreference } from './preference-sync.js';
10
+ import {
11
+ CLAGENTIC_DARK, CLAGENTIC_LIGHT,
12
+ deriveVars, deriveTerminalTheme, deriveMermaidVars, inferVariant, readTokens
13
+ } from './theme-colors.js';
14
+
15
+ // Every colour here comes from a theme's palette through the one derivation in
16
+ // ../theme-core.js (see theme-colors.js). This module holds no colour value.
4
17
 
5
- // --- Color utilities ---
6
-
7
- function hexToRgb(hex) {
8
- var h = hex.replace("#", "");
9
- return {
10
- r: parseInt(h.substring(0, 2), 16),
11
- g: parseInt(h.substring(2, 4), 16),
12
- b: parseInt(h.substring(4, 6), 16)
13
- };
14
- }
15
-
16
- function rgbToHex(r, g, b) {
17
- return "#" + [r, g, b].map(function (v) {
18
- var c = Math.max(0, Math.min(255, Math.round(v)));
19
- return c.toString(16).padStart(2, "0");
20
- }).join("");
21
- }
18
+ // --- State ---
19
+ // All themes loaded from server: bundled + custom, keyed by id
20
+ // The two Clagentic themes are always present, so there is always something
21
+ // to apply; /api/themes then adds the custom themes from the console's themes
22
+ // folder (<console home>/themes/). Prototype-less so an id such as
23
+ // "constructor" is never a hit.
24
+ var themes = Object.create(null);
25
+ themes[DARK_ID] = CLAGENTIC_DARK;
26
+ themes[LIGHT_ID] = CLAGENTIC_LIGHT;
27
+ var customSet = Object.create(null); // ids that came from the custom themes folder
28
+ var themesLoaded = false;
29
+ // A choice made on this page outranks a profile reply that was already in flight.
30
+ var chosenThisPage = false;
31
+ var currentThemeId = DARK_ID;
32
+ var changeCallbacks = [];
33
+ var STORAGE_KEY = "clagentic-theme"; // id last applied; pairs with -vars and -variant (first-paint cache)
34
+ var MODE_KEY = "clagentic-mode"; // "light" | "dark"; absent follows the system
35
+ var CUSTOM_KEY = "clagentic-theme-custom"; // id of the chosen custom theme; absent when none
36
+ var MIGRATED_KEY = "clagentic-theme-migrated"; // set once the retired-theme migration has run
37
+ // index.html's first-paint script reads this value; change both together.
38
+ var MIGRATION_VERSION = "3";
39
+ // Keys earlier builds wrote for themes the console no longer has. The
40
+ // migration reads the brand, then removes all of these.
41
+ var LEGACY_BRAND_KEY = "clagentic-brand";
42
+ var LEGACY_KEYS = ["clagentic-brand", "clagentic-skin", "clay-theme", "clay-theme-vars", "clay-theme-variant"];
22
43
 
23
- function darken(hex, amount) {
24
- var c = hexToRgb(hex);
25
- var f = 1 - amount;
26
- return rgbToHex(c.r * f, c.g * f, c.b * f);
27
- }
44
+ // --- Helpers ---
28
45
 
29
- function lighten(hex, amount) {
30
- var c = hexToRgb(hex);
31
- return rgbToHex(
32
- c.r + (255 - c.r) * amount,
33
- c.g + (255 - c.g) * amount,
34
- c.b + (255 - c.b) * amount
35
- );
46
+ // Storage can be unavailable (private mode, quota); a theme choice then lasts
47
+ // for the page only, which is the right degradation.
48
+ function lsGet(key) {
49
+ try { return localStorage.getItem(key); } catch (e) { return null; }
36
50
  }
37
51
 
38
- function mixColors(hex1, hex2, weight) {
39
- var c1 = hexToRgb(hex1);
40
- var c2 = hexToRgb(hex2);
41
- var w = weight;
42
- return rgbToHex(
43
- c1.r * w + c2.r * (1 - w),
44
- c1.g * w + c2.g * (1 - w),
45
- c1.b * w + c2.b * (1 - w)
46
- );
52
+ function lsSet(key, value) {
53
+ try { localStorage.setItem(key, value); } catch (e) {}
47
54
  }
48
55
 
49
- function hexToRgba(hex, alpha) {
50
- var c = hexToRgb(hex);
51
- return "rgba(" + c.r + ", " + c.g + ", " + c.b + ", " + alpha + ")";
56
+ function lsRemove(key) {
57
+ try { localStorage.removeItem(key); } catch (e) {}
52
58
  }
53
59
 
54
- function luminance(hex) {
55
- var c = hexToRgb(hex);
56
- return (0.299 * c.r + 0.587 * c.g + 0.114 * c.b) / 255;
60
+ function getTheme(id) {
61
+ return themes[id] || null;
57
62
  }
58
63
 
59
- // --- Clagentic Dark default: exact CSS values for initial render (before API loads) ---
60
- var defaultExactVars = {
61
- "--bg": "#060810",
62
- "--bg-alt": "#111827",
63
- "--text": "#E0EAF4",
64
- "--text-secondary": "#C8D4E0",
65
- "--text-muted": "#7A8FA6",
66
- "--text-dimmer": "#3D5068",
67
- "--accent": "#00CFFF",
68
- "--accent-hover": "#33D8FF",
69
- "--accent-bg": "rgba(0, 207, 255, 0.12)",
70
- "--code-bg": "#05070E",
71
- "--border": "#1E2E48",
72
- "--border-subtle": "#101726",
73
- "--input-bg": "#172338",
74
- "--user-bubble": "#151F31",
75
- "--error": "#FF4D6A",
76
- "--success": "#29D9A8",
77
- "--warning": "#4A7FE8",
78
- "--sidebar-bg": "#04060B",
79
- "--sidebar-hover": "#0A0E19",
80
- "--sidebar-active": "#162134",
81
- "--filebrowser-bg": "#09090D",
82
- "--filebrowser-border": "#121B2C",
83
- "--accent-8": "rgba(0, 207, 255, 0.08)",
84
- "--accent-12": "rgba(0, 207, 255, 0.12)",
85
- "--accent-15": "rgba(0, 207, 255, 0.15)",
86
- "--accent-20": "rgba(0, 207, 255, 0.20)",
87
- "--accent-25": "rgba(0, 207, 255, 0.25)",
88
- "--accent-30": "rgba(0, 207, 255, 0.30)",
89
- "--accent2": "#4A7FE8",
90
- "--accent2-hover": "#6B97F0",
91
- "--accent2-bg": "rgba(74, 127, 232, 0.12)",
92
- "--accent2-8": "rgba(74, 127, 232, 0.08)",
93
- "--accent2-12": "rgba(74, 127, 232, 0.12)",
94
- "--accent2-15": "rgba(74, 127, 232, 0.15)",
95
- "--accent2-20": "rgba(74, 127, 232, 0.20)",
96
- "--accent2-25": "rgba(74, 127, 232, 0.25)",
97
- "--accent2-30": "rgba(74, 127, 232, 0.30)",
98
- "--error-8": "rgba(255, 77, 106, 0.08)",
99
- "--error-12": "rgba(255, 77, 106, 0.12)",
100
- "--error-15": "rgba(255, 77, 106, 0.15)",
101
- "--error-25": "rgba(255, 77, 106, 0.25)",
102
- "--success-8": "rgba(41, 217, 168, 0.08)",
103
- "--success-12": "rgba(41, 217, 168, 0.12)",
104
- "--success-15": "rgba(41, 217, 168, 0.15)",
105
- "--success-25": "rgba(41, 217, 168, 0.25)",
106
- "--warning-bg": "rgba(74, 127, 232, 0.12)",
107
- "--overlay-rgb": "255,255,255",
108
- "--shadow-rgb": "0,0,0",
109
- "--hl-comment": "#3D5068",
110
- "--hl-keyword": "#A855F7",
111
- "--hl-string": "#29D9A8",
112
- "--hl-number": "#00CFFF",
113
- "--hl-function": "#4A7FE8",
114
- "--hl-variable": "#FF4D6A",
115
- "--hl-type": "#4A7FE8",
116
- "--hl-constant": "#00CFFF",
117
- "--hl-tag": "#FF4D6A",
118
- "--hl-attr": "#4A7FE8",
119
- "--hl-regexp": "#7B3FE4",
120
- "--hl-meta": "#6366F1",
121
- "--hl-builtin": "#00CFFF",
122
- "--hl-symbol": "#6366F1",
123
- "--hl-addition": "#29D9A8",
124
- "--hl-deletion": "#FF4D6A"
125
- };
126
-
127
- // Minimal Clagentic Dark palette for getThemeColor before API loads
128
- var defaultFallback = {
129
- name: "Clagentic Dark", variant: "dark",
130
- base00: "060810", base01: "111827", base02: "1E2E48", base03: "3D5068",
131
- base04: "7A8FA6", base05: "C8D4E0", base06: "E0EAF4", base07: "F0F6FF",
132
- base08: "FF4D6A", base09: "00CFFF", base0A: "4A7FE8", base0B: "29D9A8",
133
- base0C: "7B3FE4", base0D: "4A7FE8", base0E: "A855F7", base0F: "6366F1",
134
- accent2: "4A7FE8"
135
- };
136
-
137
- // --- Compute CSS variables from a base16 palette ---
138
- function computeVars(theme) {
139
- var b = {};
140
- var keys = ["base00","base01","base02","base03","base04","base05","base06","base07",
141
- "base08","base09","base0A","base0B","base0C","base0D","base0E","base0F"];
142
- for (var i = 0; i < keys.length; i++) {
143
- b[keys[i]] = "#" + theme[keys[i]];
144
- }
145
-
146
- var isLight = theme.variant === "light";
147
- var accent2 = theme.accent2 ? "#" + theme.accent2 : b.base0D;
148
-
149
- return {
150
- "--bg": b.base00,
151
- "--bg-alt": b.base01,
152
- "--text": b.base06,
153
- "--text-secondary": b.base05,
154
- "--text-muted": b.base04,
155
- "--text-dimmer": b.base03,
156
- "--accent": b.base09,
157
- "--accent-hover": isLight ? darken(b.base09, 0.12) : lighten(b.base09, 0.12),
158
- "--accent-bg": hexToRgba(b.base09, 0.12),
159
- "--code-bg": isLight ? darken(b.base00, 0.03) : darken(b.base00, 0.15),
160
- "--border": b.base02,
161
- "--border-subtle": mixColors(b.base00, b.base02, 0.6),
162
- "--input-bg": isLight ? darken(b.base00, 0.04) : mixColors(b.base01, b.base02, 0.5),
163
- "--ask-mate-bg": isLight ? mixColors("#ffffff", darken(b.base00, 0.04), 0.6) : mixColors(b.base00, mixColors(b.base01, b.base02, 0.5), 0.6),
164
- "--user-bubble": isLight ? darken(b.base01, 0.03) : mixColors(b.base01, b.base02, 0.3),
165
- "--error": b.base08,
166
- "--success": b.base0B,
167
- "--warning": b.base0A,
168
- "--sidebar-bg": isLight ? darken(b.base00, 0.03) : darken(b.base00, 0.30),
169
- "--sidebar-hover": isLight ? darken(b.base00, 0.07) : mixColors(b.base00, b.base01, 0.4),
170
- "--sidebar-active": isLight ? darken(b.base01, 0.06) : mixColors(b.base01, b.base02, 0.4),
171
- "--filebrowser-bg": isLight ? lighten(b.base00, 0.03) : lighten(b.base00, 0.04),
172
- "--filebrowser-border": isLight ? darken(b.base00, 0.08) : mixColors(b.base02, b.base00, 0.5),
173
- "--accent-8": hexToRgba(b.base09, 0.08),
174
- "--accent-12": hexToRgba(b.base09, 0.12),
175
- "--accent-15": hexToRgba(b.base09, 0.15),
176
- "--accent-20": hexToRgba(b.base09, 0.20),
177
- "--accent-25": hexToRgba(b.base09, 0.25),
178
- "--accent-30": hexToRgba(b.base09, 0.30),
179
- "--accent2": accent2,
180
- "--accent2-hover": isLight ? darken(accent2, 0.12) : lighten(accent2, 0.12),
181
- "--accent2-bg": hexToRgba(accent2, 0.12),
182
- "--accent2-8": hexToRgba(accent2, 0.08),
183
- "--accent2-12": hexToRgba(accent2, 0.12),
184
- "--accent2-15": hexToRgba(accent2, 0.15),
185
- "--accent2-20": hexToRgba(accent2, 0.20),
186
- "--accent2-25": hexToRgba(accent2, 0.25),
187
- "--accent2-30": hexToRgba(accent2, 0.30),
188
- "--error-8": hexToRgba(b.base08, 0.08),
189
- "--error-12": hexToRgba(b.base08, 0.12),
190
- "--error-15": hexToRgba(b.base08, 0.15),
191
- "--error-25": hexToRgba(b.base08, 0.25),
192
- "--success-8": hexToRgba(b.base0B, 0.08),
193
- "--success-12": hexToRgba(b.base0B, 0.12),
194
- "--success-15": hexToRgba(b.base0B, 0.15),
195
- "--success-25": hexToRgba(b.base0B, 0.25),
196
- "--warning-bg": hexToRgba(b.base0A, 0.12),
197
- "--overlay-rgb": isLight ? "0,0,0" : "255,255,255",
198
- "--shadow-rgb": "0,0,0",
199
- "--hl-comment": b.base03,
200
- "--hl-keyword": b.base0E,
201
- "--hl-string": b.base0B,
202
- "--hl-number": b.base09,
203
- "--hl-function": b.base0D,
204
- "--hl-variable": b.base08,
205
- "--hl-type": b.base0A,
206
- "--hl-constant": b.base09,
207
- "--hl-tag": b.base08,
208
- "--hl-attr": b.base0D,
209
- "--hl-regexp": b.base0C,
210
- "--hl-meta": b.base0F,
211
- "--hl-builtin": b.base09,
212
- "--hl-symbol": b.base0F,
213
- "--hl-addition": b.base0B,
214
- "--hl-deletion": b.base08
215
- };
64
+ function isCustom(id) {
65
+ return !!customSet[id];
216
66
  }
217
67
 
218
- function computeTerminalTheme(theme) {
219
- var b = {};
220
- var keys = ["base00","base01","base02","base03","base04","base05","base06","base07",
221
- "base08","base09","base0A","base0B","base0C","base0D","base0E","base0F"];
222
- for (var i = 0; i < keys.length; i++) {
223
- b[keys[i]] = "#" + theme[keys[i]];
224
- }
68
+ // Best effort: the choice is already applied here, and the next profile load
69
+ // reconciles with the server. The mode and the custom theme id are one choice
70
+ // on the server (a mode clears the id), so both go through one saver and a quick
71
+ // second choice replaces the first as a whole (see serialized-save.js).
72
+ var saveThemeChoice = createSerializedRequestSaver();
73
+ var MODE_ROUTE = "/api/user/theme-mode";
74
+ var ID_ROUTE = "/api/user/theme-id";
225
75
 
226
- var isLight = theme.variant === "light";
227
- return {
228
- background: isLight ? darken(b.base00, 0.03) : darken(b.base00, 0.15),
229
- foreground: b.base05,
230
- cursor: b.base06,
231
- selectionBackground: hexToRgba(b.base02, 0.5),
232
- black: isLight ? b.base07 : b.base00,
233
- red: b.base08,
234
- green: b.base0B,
235
- yellow: b.base0A,
236
- blue: b.base0D,
237
- magenta: b.base0E,
238
- cyan: b.base0C,
239
- white: isLight ? b.base00 : b.base05,
240
- brightBlack: b.base03,
241
- brightRed: isLight ? darken(b.base08, 0.1) : lighten(b.base08, 0.1),
242
- brightGreen: isLight ? darken(b.base0B, 0.1) : lighten(b.base0B, 0.1),
243
- brightYellow: isLight ? darken(b.base0A, 0.1) : lighten(b.base0A, 0.1),
244
- brightBlue: isLight ? darken(b.base0D, 0.1) : lighten(b.base0D, 0.1),
245
- brightMagenta: isLight ? darken(b.base0E, 0.1) : lighten(b.base0E, 0.1),
246
- brightCyan: isLight ? darken(b.base0C, 0.1) : lighten(b.base0C, 0.1),
247
- brightWhite: b.base07
248
- };
249
- }
250
-
251
- function computeMermaidVars(theme) {
252
- var vars = currentThemeId === "clagentic-dark" && !themesLoaded ? defaultExactVars : computeVars(theme);
253
- var isLight = theme.variant === "light";
254
- return {
255
- darkMode: !isLight,
256
- background: vars["--code-bg"],
257
- primaryColor: vars["--accent"],
258
- primaryTextColor: vars["--text"],
259
- primaryBorderColor: vars["--border"],
260
- lineColor: vars["--text-muted"],
261
- secondaryColor: vars["--bg-alt"],
262
- tertiaryColor: vars["--bg"]
263
- };
76
+ function getStoredMode() {
77
+ var m = lsGet(MODE_KEY);
78
+ return m === "light" || m === "dark" ? m : null;
264
79
  }
265
80
 
266
- // --- State ---
267
- // All themes loaded from server: bundled + custom, keyed by id
268
- var themes = {};
269
- var customSet = {}; // ids that came from ~/.clagentic/themes/
270
- var themesLoaded = false;
271
- var currentThemeId = "clagentic-dark";
272
- var changeCallbacks = [];
273
- var STORAGE_KEY = "clagentic-theme";
274
- var MODE_KEY = "clagentic-mode"; // "light" | "dark" | null (system)
275
- var SKIN_KEY = "clagentic-skin"; // theme id within current variant pair
276
- var WIDE_KEY = "clagentic-wide-view"; // cached from server, "bubble" | "channel"
277
- var BRAND_KEY = "clagentic-brand"; // "clagentic" | "classic"
278
-
279
- // Brand pairs: maps brand+mode → theme id
280
- var BRAND_PAIRS = {
281
- "clagentic": { dark: "clagentic-dark", light: "clagentic-light" },
282
- "classic": { dark: "dracula", light: "ayu-light" }
283
- };
284
-
285
- // --- Helpers ---
286
-
287
- function getTheme(id) {
288
- return themes[id] || (id === "dracula" ? defaultFallback : null);
81
+ // The theme id to apply now: the chosen custom theme when it is loaded, else
82
+ // the Clagentic theme of the user's mode.
83
+ function pickThemeId() {
84
+ return resolveThemeId({
85
+ customId: lsGet(CUSTOM_KEY),
86
+ mode: getStoredMode(),
87
+ systemMode: getSystemMode(),
88
+ isCustom: isCustom
89
+ });
289
90
  }
290
91
 
291
- function isCustom(id) {
292
- return !!customSet[id];
92
+ // Re-applies the right theme once the theme list is known.
93
+ function reapply() {
94
+ if (themesLoaded) applyTheme(pickThemeId());
293
95
  }
294
96
 
295
97
  // --- Public API ---
296
98
 
297
99
  export function getCurrentTheme() {
298
- return getTheme(currentThemeId) || defaultFallback;
100
+ return getTheme(currentThemeId) || CLAGENTIC_DARK;
299
101
  }
300
102
 
301
103
  export function getThemeId() {
@@ -308,22 +110,24 @@ export function getThemeColor(baseKey) {
308
110
  }
309
111
 
310
112
  export function getComputedVar(varName) {
311
- if (currentThemeId === "clagentic-dark" && !themesLoaded) return defaultExactVars[varName] || "";
312
- var theme = getCurrentTheme();
313
- var vars = computeVars(theme);
314
- return vars[varName] || "";
113
+ return deriveVars(getCurrentTheme())[varName] || "";
315
114
  }
316
115
 
317
116
  export function getTerminalTheme() {
318
- return computeTerminalTheme(getCurrentTheme());
117
+ return deriveTerminalTheme(getCurrentTheme());
319
118
  }
320
119
 
321
120
  export function getMermaidThemeVars() {
322
- return computeMermaidVars(getCurrentTheme());
121
+ return deriveMermaidVars(getCurrentTheme());
323
122
  }
324
123
 
124
+ // Returns a function that removes the listener again.
325
125
  export function onThemeChange(fn) {
326
126
  changeCallbacks.push(fn);
127
+ return function () {
128
+ var at = changeCallbacks.indexOf(fn);
129
+ if (at !== -1) changeCallbacks.splice(at, 1);
130
+ };
327
131
  }
328
132
 
329
133
  export function getThemes() {
@@ -334,13 +138,38 @@ export function getThemes() {
334
138
  return all;
335
139
  }
336
140
 
337
- export function applyTheme(themeId, fromPicker) {
141
+ // The custom themes the user can pick: [{ id, name, variant, swatches }].
142
+ export function getCustomThemes() {
143
+ var list = [];
144
+ for (var id in customSet) {
145
+ var t = themes[id];
146
+ if (!t) continue;
147
+ list.push({
148
+ id: id, name: t.name, variant: t.variant,
149
+ swatches: ["base00", "base01", "base09", "base0B", "base0D"].map(function (k) { return "#" + t[k]; })
150
+ });
151
+ }
152
+ return list;
153
+ }
154
+
155
+ // What is chosen now: mode is "light", "dark" or null (follow the system);
156
+ // customId is the chosen custom theme when it is loaded, else null.
157
+ export function getThemeSelection() {
158
+ var customId = lsGet(CUSTOM_KEY);
159
+ return { mode: getStoredMode(), customId: customId && isCustom(customId) ? customId : null };
160
+ }
161
+
162
+ // Applies a theme by id. An id that is not loaded is never drawn: the
163
+ // Clagentic theme of the user's mode is applied instead.
164
+ export function applyTheme(themeId) {
338
165
  var theme = getTheme(themeId);
339
- if (!theme) themeId = "dracula";
340
- theme = getTheme(themeId);
166
+ if (!theme) {
167
+ themeId = pickThemeId();
168
+ theme = getTheme(themeId);
169
+ }
341
170
  currentThemeId = themeId;
342
171
 
343
- var vars = (themeId === "clagentic-dark" && !themesLoaded) ? defaultExactVars : computeVars(theme);
172
+ var vars = deriveVars(theme);
344
173
  var root = document.documentElement;
345
174
  var varNames = Object.keys(vars);
346
175
  for (var i = 0; i < varNames.length; i++) {
@@ -354,34 +183,27 @@ export function applyTheme(themeId, fromPicker) {
354
183
  var meta = document.querySelector('meta[name="theme-color"]');
355
184
  if (meta) meta.setAttribute("content", vars["--bg"]);
356
185
 
357
- updatePickerActive(themeId);
358
-
359
186
  try { updateMascotSvgs(vars, isLight); } catch (e) {}
360
187
 
361
- var termTheme = computeTerminalTheme(theme);
188
+ var termTheme = deriveTerminalTheme(theme);
362
189
  try { setTerminalTheme(termTheme); } catch (e) {}
363
190
 
364
- var mermaidVars = computeMermaidVars(theme);
191
+ var mermaidVars = deriveMermaidVars(theme);
365
192
  try { updateMermaidTheme(mermaidVars); } catch (e) {}
366
193
 
367
- try {
368
- localStorage.setItem(STORAGE_KEY, themeId);
369
- localStorage.setItem(STORAGE_KEY + "-vars", JSON.stringify(vars));
370
- localStorage.setItem(STORAGE_KEY + "-variant", theme.variant || "dark");
371
- } catch (e) {}
372
-
373
- // When picked from skin selector, save as skin preference and sync mode
374
- if (fromPicker) {
375
- try {
376
- localStorage.setItem(SKIN_KEY, themeId);
377
- localStorage.setItem(MODE_KEY, isLight ? "light" : "dark");
378
- } catch (e) {}
379
- }
194
+ lsSet(STORAGE_KEY, themeId);
195
+ lsSet(STORAGE_KEY + "-vars", JSON.stringify(vars));
196
+ lsSet(STORAGE_KEY + "-variant", theme.variant || "dark");
380
197
 
381
198
  updateToggleIcon();
382
199
 
383
- for (var j = 0; j < changeCallbacks.length; j++) {
384
- try { changeCallbacks[j](themeId, vars); } catch (e) {}
200
+ // A snapshot: a listener that unsubscribes while this runs must not make the
201
+ // next one in the live array get skipped.
202
+ var listeners = changeCallbacks.slice();
203
+ for (var j = 0; j < listeners.length; j++) {
204
+ try { listeners[j](themeId, vars); } catch (e) {
205
+ console.error("theme listener failed:", e);
206
+ }
385
207
  }
386
208
  }
387
209
 
@@ -402,34 +224,30 @@ function loadThemes() {
402
224
  var custom = data.custom || {};
403
225
  var id;
404
226
 
405
- // Bundled themes first
227
+ // The console's own themes first
406
228
  for (id in bundled) {
407
229
  if (validateTheme(bundled[id])) {
408
230
  themes[id] = bundled[id];
409
231
  }
410
232
  }
411
- // Custom themes override bundled
233
+ // Custom themes add to them and never replace one of the console's own
412
234
  for (id in custom) {
235
+ if (themes[id] && !customSet[id]) continue;
413
236
  if (validateTheme(custom[id])) {
414
237
  themes[id] = custom[id];
415
238
  customSet[id] = true;
416
239
  }
417
240
  }
418
241
 
419
- // Ensure dracula always exists
420
- if (!themes.dracula) themes.dracula = defaultFallback;
421
-
422
242
  themesLoaded = true;
423
243
 
424
- // Rebuild picker if already created
425
- if (pickerEl) rebuildPicker();
426
-
427
- // Always apply the current theme now that real data is loaded
428
- // (before this, only defaultExactVars was used as fallback)
429
- applyTheme(currentThemeId);
244
+ // Apply the right theme now that the custom list is known, and tell the
245
+ // listeners (Appearance lists the custom themes on this signal).
246
+ applyTheme(pickThemeId());
430
247
  }).catch(function () {
431
- // API unavailable — keep dracula fallback
432
- themes.dracula = defaultFallback;
248
+ // API unavailable: the two Clagentic themes are built in, so a later
249
+ // apply still works, and a custom choice stays stored for the next load.
250
+ // What the first-paint script drew stays on screen meanwhile.
433
251
  themesLoaded = true;
434
252
  });
435
253
  }
@@ -443,8 +261,9 @@ function validateTheme(t) {
443
261
  if (!t[keys[i]] || !/^[0-9a-fA-F]{6}$/.test(t[keys[i]])) return false;
444
262
  }
445
263
  if (t.variant && t.variant !== "dark" && t.variant !== "light") return false;
264
+ if (readTokens(t.tokens) === null) return false;
446
265
  if (!t.variant) {
447
- t.variant = luminance("#" + t.base00) > 0.5 ? "light" : "dark";
266
+ t.variant = inferVariant(t.base00);
448
267
  }
449
268
  return true;
450
269
  }
@@ -459,89 +278,43 @@ function getSystemMode() {
459
278
  return "dark";
460
279
  }
461
280
 
462
- // Returns the effective mode: user override or system
463
- function getEffectiveMode() {
464
- var saved = null;
465
- try { saved = localStorage.getItem(MODE_KEY); } catch (e) {}
466
- if (saved === "light" || saved === "dark") return saved;
467
- return getSystemMode();
468
- }
469
-
470
- // Get/set the active brand ("clagentic" | "classic")
471
- export function getBrand() {
472
- var b = null;
473
- try { b = localStorage.getItem(BRAND_KEY); } catch (e) {}
474
- return (b === "classic") ? "classic" : "clagentic";
475
- }
476
-
477
- export function setBrand(brand) {
478
- var b = (brand === "classic") ? "classic" : "clagentic";
479
- try { localStorage.setItem(BRAND_KEY, b); } catch (e) {}
480
- // Switch to the brand's theme for the current mode
481
- var mode = getEffectiveMode();
482
- var tid = BRAND_PAIRS[b][mode] || BRAND_PAIRS["clagentic"][mode];
483
- applyTheme(tid, true);
484
- // Persist to server so mobile/cross-device stays in sync
485
- fetch("/api/user/theme-brand", {
486
- method: "PUT",
487
- headers: { "Content-Type": "application/json" },
488
- body: JSON.stringify({ themeBrand: b }),
489
- }).catch(function () {});
490
- }
491
-
492
- // Map a mode to the appropriate theme id using the active brand
493
- function themeIdForMode(mode) {
494
- var brand = getBrand();
495
- var pair = BRAND_PAIRS[brand] || BRAND_PAIRS["clagentic"];
496
- return pair[mode] || pair["dark"];
497
- }
498
-
499
- // --- Wide view ---
500
-
501
- export function isWideView() {
502
- try {
503
- var v = localStorage.getItem(WIDE_KEY);
504
- return v === null ? true : v !== "bubble"; // default: Channel (wide)
505
- } catch (e) { return true; }
506
- }
507
-
508
- export function getChatLayout() {
509
- try {
510
- var v = localStorage.getItem(WIDE_KEY);
511
- return v === "bubble" ? "bubble" : "channel";
512
- } catch (e) { return "channel"; }
513
- }
514
-
515
- export function setChatLayout(layout) {
516
- var val = (layout === "bubble") ? "bubble" : "channel";
517
- try { localStorage.setItem(WIDE_KEY, val); } catch (e) {}
518
- document.body.classList.toggle("wide-view", val === "channel");
519
- }
520
-
521
- export function setWideView(enabled) {
522
- setChatLayout(enabled ? "channel" : "bubble");
281
+ // Choose Dark ("dark"), Light ("light") or Follow system (null). Any of the
282
+ // three replaces a custom theme. Saved on the server so the choice reaches the
283
+ // user's other devices.
284
+ export function setThemeMode(mode) {
285
+ var val = mode === "light" || mode === "dark" ? mode : null;
286
+ if (val) lsSet(MODE_KEY, val); else lsRemove(MODE_KEY);
287
+ lsRemove(CUSTOM_KEY);
288
+ chosenThisPage = true;
289
+ applyTheme(pickThemeId());
290
+ saveThemeChoice([{ url: MODE_ROUTE, bodyKey: "themeMode", value: val }]);
291
+ }
292
+
293
+ // Choose a custom theme by id. Returns false when no such custom theme is
294
+ // loaded. Saved on the server, like the mode.
295
+ export function setCustomTheme(id) {
296
+ if (!isCustom(id)) return false;
297
+ chosenThisPage = true;
298
+ lsSet(CUSTOM_KEY, id);
299
+ applyTheme(id);
300
+ // The mode first, then the id: the server clears the id when it takes a mode.
301
+ // Sent together, a quick earlier mode choice that this one replaces is not lost.
302
+ saveThemeChoice([
303
+ { url: MODE_ROUTE, bodyKey: "themeMode", value: getStoredMode() },
304
+ { url: ID_ROUTE, bodyKey: "themeId", value: id }
305
+ ]);
306
+ return true;
523
307
  }
524
308
 
525
- // Toggle between light and dark
309
+ // Toggle between light and dark, from whatever is showing now (a custom theme
310
+ // counts by its variant). The toggle is an explicit Dark or Light choice.
526
311
  export function toggleDarkMode() {
527
- var current = getEffectiveMode();
528
- var next = current === "dark" ? "light" : "dark";
529
- try { localStorage.setItem(MODE_KEY, next); } catch (e) {}
530
- var tid = themeIdForMode(next);
531
- applyTheme(tid);
532
- updateToggleIcon();
533
- // Persist to server so mobile/cross-device stays in sync
534
- fetch("/api/user/theme-mode", {
535
- method: "PUT",
536
- headers: { "Content-Type": "application/json" },
537
- body: JSON.stringify({ themeMode: next }),
538
- }).catch(function () {});
312
+ setThemeMode(getCurrentTheme().variant === "light" ? "dark" : "light");
539
313
  }
540
314
 
541
315
  // Update all theme toggle checkboxes
542
316
  function updateToggleIcon() {
543
- var mode = getEffectiveMode();
544
- var isLight = mode === "light";
317
+ var isLight = getCurrentTheme().variant === "light";
545
318
  // Legacy top-bar toggle (may not exist)
546
319
  var checkbox = document.getElementById("theme-toggle-check");
547
320
  if (checkbox) checkbox.checked = isLight;
@@ -560,221 +333,86 @@ function updateToggleIcon() {
560
333
  refreshIcons();
561
334
  }
562
335
 
563
- // --- Theme picker UI ---
564
- var pickerEl = null;
565
-
566
- function updatePickerActive(themeId) {
567
- if (!pickerEl) return;
568
- var items = pickerEl.querySelectorAll(".theme-picker-item");
569
- for (var i = 0; i < items.length; i++) {
570
- var item = items[i];
571
- if (item.dataset.theme === themeId) {
572
- item.classList.add("active");
573
- } else {
574
- item.classList.remove("active");
575
- }
576
- }
577
- }
578
-
579
- function createThemeItem(id, theme) {
580
- var item = document.createElement("button");
581
- item.className = "theme-picker-item";
582
- if (id === currentThemeId) item.className += " active";
583
- item.dataset.theme = id;
584
-
585
- var swatches = document.createElement("span");
586
- swatches.className = "theme-swatches";
587
- var previewKeys = ["base00", "base01", "base09", "base0B", "base0D"];
588
- for (var j = 0; j < previewKeys.length; j++) {
589
- var dot = document.createElement("span");
590
- dot.className = "theme-swatch";
591
- dot.style.background = "#" + theme[previewKeys[j]];
592
- swatches.appendChild(dot);
593
- }
594
- item.appendChild(swatches);
595
-
596
- var label = document.createElement("span");
597
- label.className = "theme-picker-label";
598
- label.textContent = theme.name;
599
- item.appendChild(label);
600
-
601
- var check = document.createElement("span");
602
- check.className = "theme-picker-check";
603
- check.textContent = "\u2713";
604
- item.appendChild(check);
605
-
606
- item.addEventListener("click", function (e) {
607
- e.stopPropagation();
608
- applyTheme(id, true);
609
- });
610
-
611
- return item;
612
- }
613
-
614
- function buildPickerContent() {
615
- pickerEl.innerHTML = "";
616
-
617
- var darkIds = [];
618
- var lightIds = [];
619
- var customIds = [];
620
- var themeIds = Object.keys(themes);
621
- for (var i = 0; i < themeIds.length; i++) {
622
- var id = themeIds[i];
623
- if (isCustom(id)) {
624
- customIds.push(id);
625
- } else if (themes[id].variant === "light") {
626
- lightIds.push(id);
627
- } else {
628
- darkIds.push(id);
629
- }
630
- }
631
-
632
- // Default themes always first in their section
633
- function pinFirst(arr, pinId) {
634
- var idx = arr.indexOf(pinId);
635
- if (idx > 0) { arr.splice(idx, 1); arr.unshift(pinId); }
636
- }
637
- pinFirst(darkIds, "dracula");
638
- pinFirst(darkIds, "clagentic-dark");
639
- pinFirst(lightIds, "ayu-light");
640
- pinFirst(lightIds, "clagentic-light");
641
-
642
- // Dark section
643
- if (darkIds.length > 0) {
644
- var darkHeader = document.createElement("div");
645
- darkHeader.className = "theme-picker-header";
646
- darkHeader.textContent = "Dark";
647
- pickerEl.appendChild(darkHeader);
648
-
649
- var darkList = document.createElement("div");
650
- darkList.className = "theme-picker-section";
651
- for (var d = 0; d < darkIds.length; d++) {
652
- darkList.appendChild(createThemeItem(darkIds[d], themes[darkIds[d]]));
653
- }
654
- pickerEl.appendChild(darkList);
655
- }
656
-
657
- // Light section
658
- if (lightIds.length > 0) {
659
- var lightHeader = document.createElement("div");
660
- lightHeader.className = "theme-picker-header";
661
- lightHeader.textContent = "Light";
662
- pickerEl.appendChild(lightHeader);
663
-
664
- var lightList = document.createElement("div");
665
- lightList.className = "theme-picker-section";
666
- for (var l = 0; l < lightIds.length; l++) {
667
- lightList.appendChild(createThemeItem(lightIds[l], themes[lightIds[l]]));
668
- }
669
- pickerEl.appendChild(lightList);
670
- }
671
-
672
- // Custom section
673
- if (customIds.length > 0) {
674
- var customHeader = document.createElement("div");
675
- customHeader.className = "theme-picker-header";
676
- customHeader.textContent = "Custom";
677
- pickerEl.appendChild(customHeader);
678
-
679
- var customList = document.createElement("div");
680
- customList.className = "theme-picker-section";
681
- for (var c = 0; c < customIds.length; c++) {
682
- customList.appendChild(createThemeItem(customIds[c], themes[customIds[c]]));
683
- }
684
- pickerEl.appendChild(customList);
685
- }
686
- }
687
-
688
- function createThemePicker() {
689
- if (pickerEl) return pickerEl;
690
-
691
- pickerEl = document.createElement("div");
692
- pickerEl.className = "theme-picker";
693
- pickerEl.id = "theme-picker";
694
-
695
- buildPickerContent();
696
- return pickerEl;
697
- }
698
-
699
- function rebuildPicker() {
700
- if (!pickerEl) return;
701
- buildPickerContent();
702
- }
703
-
704
- var pickerVisible = false;
705
-
706
- function togglePicker() {
707
- // Legacy — no longer used as floating popover
708
- // Picker is now embedded in server settings
709
- }
710
-
711
- function closePicker() {
712
- // Legacy — no longer needed
713
- }
714
-
715
- // Apply server-supplied theme preferences (themeMode, themeBrand) from /api/profile.
716
- // Call this after profile loads. Server values take precedence over localStorage.
717
- export function applyServerThemePrefs(themeMode, themeBrand) {
336
+ // Apply the theme preferences the server holds for this user (themeMode,
337
+ // themeId from /api/profile). Call it after the profile loads. The server is
338
+ // authoritative, so a choice made on another device wins, including Follow
339
+ // system (a null mode) and a custom theme.
340
+ export function applyServerThemePrefs(server) {
341
+ var prefs = reconcileServerPrefs(server);
718
342
  var changed = false;
719
- if (themeBrand && (themeBrand === "classic" || themeBrand === "clagentic")) {
720
- var local = null;
721
- try { local = localStorage.getItem(BRAND_KEY); } catch (e) {}
722
- if (local !== themeBrand) {
723
- try { localStorage.setItem(BRAND_KEY, themeBrand); } catch (e) {}
724
- changed = true;
725
- }
343
+ if (prefs.mode !== getStoredMode()) {
344
+ if (prefs.mode) lsSet(MODE_KEY, prefs.mode); else lsRemove(MODE_KEY);
345
+ changed = true;
726
346
  }
727
- if (themeMode && (themeMode === "light" || themeMode === "dark")) {
728
- var localMode = null;
729
- try { localMode = localStorage.getItem(MODE_KEY); } catch (e) {}
730
- if (localMode !== themeMode) {
731
- try { localStorage.setItem(MODE_KEY, themeMode); } catch (e) {}
732
- changed = true;
733
- }
347
+ var localCustom = lsGet(CUSTOM_KEY);
348
+ if (prefs.customId !== localCustom) {
349
+ if (prefs.customId) lsSet(CUSTOM_KEY, prefs.customId); else lsRemove(CUSTOM_KEY);
350
+ changed = true;
734
351
  }
735
- if (changed) {
736
- var mode = getEffectiveMode();
737
- var tid = themeIdForMode(mode);
738
- applyTheme(tid);
739
- updateToggleIcon();
352
+ // Before the theme list loads a custom id cannot be checked; loadThemes
353
+ // applies the right theme as soon as it can.
354
+ if (changed) reapply();
355
+ }
356
+
357
+ // The load-time reconciliation. The profile reports no choice both for a user
358
+ // who never chose and for one who chose Follow system, so with neither a mode
359
+ // nor a custom theme on the server this device's own choice stands; a Follow
360
+ // system choice made elsewhere still reaches this device when Settings opens.
361
+ function applyLoadedThemePrefs(profile) {
362
+ if (chosenThisPage) return;
363
+ var prefs = reconcileServerPrefs(profile);
364
+ if (!prefs.mode && !prefs.customId) return;
365
+ applyServerThemePrefs(profile);
366
+ }
367
+
368
+ // One-time move off the retired themes. Maps the stored theme id (and the
369
+ // Classic brand) to the Clagentic theme of the same variant, drops the keys
370
+ // that only the retired themes used, and records that it ran, so it never
371
+ // runs twice. index.html's first-paint script skips the old cached palette
372
+ // until this has run.
373
+ function runThemeMigration() {
374
+ if (lsGet(MIGRATED_KEY) === MIGRATION_VERSION) return;
375
+ var migrated = migrateStoredTheme({
376
+ id: lsGet(STORAGE_KEY),
377
+ variant: lsGet(STORAGE_KEY + "-variant"),
378
+ brand: lsGet(LEGACY_BRAND_KEY),
379
+ mode: getStoredMode()
380
+ });
381
+ if (migrated.changed) {
382
+ lsSet(STORAGE_KEY, migrated.id);
383
+ lsSet(STORAGE_KEY + "-variant", migrated.variant);
740
384
  }
385
+ // The cached variables belong to an earlier palette or derivation; the next
386
+ // apply writes the current ones.
387
+ lsRemove(STORAGE_KEY + "-vars");
388
+ for (var i = 0; i < LEGACY_KEYS.length; i++) lsRemove(LEGACY_KEYS[i]);
389
+ lsSet(MIGRATED_KEY, MIGRATION_VERSION);
741
390
  }
742
391
 
743
392
  // --- Init ---
744
393
  export function initTheme() {
745
- // Determine initial theme from saved mode + skin, or system preference
746
- var saved = null;
747
- try { saved = localStorage.getItem(STORAGE_KEY); } catch (e) {}
748
-
749
- if (saved) {
750
- currentThemeId = saved;
751
- } else {
752
- // No saved theme — use system mode
753
- var mode = getSystemMode();
754
- currentThemeId = mode === "light" ? "clagentic-light" : "clagentic-dark";
755
- }
394
+ runThemeMigration();
756
395
 
757
- // Apply wide view state from localStorage
758
- document.body.classList.toggle("wide-view", isWideView());
396
+ // Until the theme list loads only the two Clagentic themes are known, so
397
+ // start from the one that matches the user's mode.
398
+ currentThemeId = pickThemeId();
759
399
 
760
400
  // Load all themes from server, then apply properly
761
401
  loadThemes();
762
402
 
763
- // Theme toggle is now in User Settings (us-theme-toggle)
764
- // Wired up in user-settings.js initUserSettings()
403
+ // Then the user's saved choice from the server, so another device's theme
404
+ // applies at load without opening Settings.
405
+ loadProfilePreference("theme", applyLoadedThemePrefs);
406
+
407
+ // The Appearance choice is built by theme-choice.js (user-settings.js and
408
+ // the phone More sheet); the toggle here is the user island button.
765
409
 
766
- // Listen for system preference changes (only applies if user has no manual override)
410
+ // Follow system: with no Dark or Light choice and no custom theme, track the
411
+ // system setting, including a change while the console is open.
767
412
  if (window.matchMedia) {
768
413
  var mq = window.matchMedia("(prefers-color-scheme: dark)");
769
414
  var handler = function () {
770
- var userMode = null;
771
- try { userMode = localStorage.getItem(MODE_KEY); } catch (e) {}
772
- if (!userMode) {
773
- // No manual override — follow system
774
- var sysMode = getSystemMode();
775
- var tid = themeIdForMode(sysMode);
776
- applyTheme(tid);
777
- }
415
+ if (!getStoredMode()) reapply();
778
416
  };
779
417
  if (mq.addEventListener) {
780
418
  mq.addEventListener("change", handler);
@@ -794,19 +432,3 @@ export function initTheme() {
794
432
  });
795
433
  }
796
434
  }
797
-
798
- // --- Settings picker (for appearance section in server settings) ---
799
- export function openSettingsThemePicker(containerEl) {
800
- if (!containerEl) return;
801
-
802
- if (!pickerEl) {
803
- createThemePicker();
804
- }
805
-
806
- // Move picker into settings container if not already there
807
- if (pickerEl.parentNode !== containerEl) {
808
- containerEl.innerHTML = "";
809
- containerEl.appendChild(pickerEl);
810
- }
811
- pickerEl.classList.add("visible");
812
- }