openclacky 1.5.25 → 1.5.26

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 (91) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +61 -0
  3. data/lib/clacky/agent/chunk_index.rb +2 -1
  4. data/lib/clacky/agent/message_compressor_helper.rb +20 -2
  5. data/lib/clacky/agent/session_serializer.rb +28 -1
  6. data/lib/clacky/agent/skill_manager.rb +2 -8
  7. data/lib/clacky/agent/time_machine.rb +108 -127
  8. data/lib/clacky/agent/tool_executor.rb +2 -11
  9. data/lib/clacky/agent.rb +15 -19
  10. data/lib/clacky/agent_config.rb +14 -4
  11. data/lib/clacky/billing/media_usage.rb +69 -0
  12. data/lib/clacky/billing/platform_billing.rb +5 -0
  13. data/lib/clacky/brand_config.rb +16 -215
  14. data/lib/clacky/default_extensions/ext-studio/agents/ext-developer/system_prompt.md +2 -80
  15. data/lib/clacky/default_extensions/ext-studio/panels/studio/view.js +1 -1
  16. data/lib/clacky/default_extensions/ext-studio/skills/ext-develop/SKILL.md +10 -96
  17. data/lib/clacky/default_extensions/git/panels/git/view.js +208 -69
  18. data/lib/clacky/default_extensions/knowledge/README.md +1 -1
  19. data/lib/clacky/default_extensions/knowledge/README.zh.md +1 -1
  20. data/lib/clacky/default_extensions/knowledge/lib/importer.rb +1 -1
  21. data/lib/clacky/default_extensions/knowledge/panels/kb/view.js +3 -3
  22. data/lib/clacky/default_extensions/meeting/api/handler.rb +23 -2
  23. data/lib/clacky/default_extensions/meeting/panels/meeting/view.js +194 -36
  24. data/lib/clacky/default_extensions/preview/icon.svg +3 -2
  25. data/lib/clacky/default_extensions/secretary/panels/work/view.js +108 -33
  26. data/lib/clacky/default_extensions/side-chat/README.md +40 -0
  27. data/lib/clacky/default_extensions/side-chat/README.zh.md +40 -0
  28. data/lib/clacky/default_extensions/side-chat/api/handler.rb +90 -0
  29. data/lib/clacky/default_extensions/side-chat/api/runtime.rb +411 -0
  30. data/lib/clacky/default_extensions/side-chat/ext.yml +16 -0
  31. data/lib/clacky/default_extensions/side-chat/icon.svg +14 -0
  32. data/lib/clacky/default_extensions/side-chat/panels/side-chat/view.js +779 -0
  33. data/lib/clacky/default_extensions/terminal/README.md +36 -0
  34. data/lib/clacky/default_extensions/terminal/README.zh.md +36 -0
  35. data/lib/clacky/default_extensions/terminal/api/handler.rb +121 -0
  36. data/lib/clacky/default_extensions/terminal/api/runtime.rb +397 -0
  37. data/lib/clacky/default_extensions/terminal/ext.yml +16 -0
  38. data/lib/clacky/default_extensions/terminal/icon.svg +13 -0
  39. data/lib/clacky/default_extensions/terminal/panels/terminal/view.js +351 -0
  40. data/lib/clacky/default_extensions/terminal/vendor/LICENSE +21 -0
  41. data/lib/clacky/default_extensions/terminal/vendor/addon-fit.js +2 -0
  42. data/lib/clacky/default_extensions/terminal/vendor/xterm.css +218 -0
  43. data/lib/clacky/default_extensions/terminal/vendor/xterm.js +2 -0
  44. data/lib/clacky/default_skills/media-gen/SKILL.md +30 -9
  45. data/lib/clacky/extension/api_extension.rb +12 -0
  46. data/lib/clacky/locales/en.rb +14 -11
  47. data/lib/clacky/locales/i18n.rb +25 -0
  48. data/lib/clacky/locales/zh.rb +14 -11
  49. data/lib/clacky/media/base.rb +11 -1
  50. data/lib/clacky/media/openai_compat.rb +271 -3
  51. data/lib/clacky/message_format/open_ai.rb +17 -0
  52. data/lib/clacky/prompts/base.md +3 -0
  53. data/lib/clacky/providers.rb +33 -20
  54. data/lib/clacky/reasoning_levels.rb +75 -0
  55. data/lib/clacky/server/channel/channel_ui_controller.rb +12 -5
  56. data/lib/clacky/server/git_panel.rb +40 -18
  57. data/lib/clacky/server/http_server.rb +62 -71
  58. data/lib/clacky/server/session_registry.rb +3 -0
  59. data/lib/clacky/ui2/markdown_renderer.rb +153 -0
  60. data/lib/clacky/ui2/strings_cjk_patch.rb +134 -124
  61. data/lib/clacky/utils/file_link_normalizer.rb +30 -3
  62. data/lib/clacky/utils/frontmatter.rb +63 -0
  63. data/lib/clacky/utils/model_pricing.rb +67 -9
  64. data/lib/clacky/version.rb +1 -1
  65. data/lib/clacky/web/app.css +722 -274
  66. data/lib/clacky/web/app.js +2 -0
  67. data/lib/clacky/web/components/chat-composer.js +504 -0
  68. data/lib/clacky/web/components/chat-navigator.js +1 -1
  69. data/lib/clacky/web/components/model-picker.js +319 -20
  70. data/lib/clacky/web/components/quote-select.js +97 -39
  71. data/lib/clacky/web/core/aside.js +137 -27
  72. data/lib/clacky/web/core/ext.js +658 -81
  73. data/lib/clacky/web/features/brand/store.js +0 -4
  74. data/lib/clacky/web/features/brand/view.js +2 -99
  75. data/lib/clacky/web/features/extensions/store.js +3 -1
  76. data/lib/clacky/web/features/extensions/view.js +14 -15
  77. data/lib/clacky/web/features/new-session/store.js +4 -0
  78. data/lib/clacky/web/features/new-session/view.js +27 -4
  79. data/lib/clacky/web/features/skills/store.js +7 -5
  80. data/lib/clacky/web/features/skills/view.js +65 -16
  81. data/lib/clacky/web/features/version/view.js +1 -1
  82. data/lib/clacky/web/features/workspace/store.js +7 -3
  83. data/lib/clacky/web/features/workspace/view.js +46 -5
  84. data/lib/clacky/web/i18n.js +104 -58
  85. data/lib/clacky/web/index.html +21 -22
  86. data/lib/clacky/web/lib/box-diagram.js +239 -0
  87. data/lib/clacky/web/sessions.js +117 -151
  88. data/lib/clacky/web/settings.js +23 -16
  89. data/lib/clacky/web/ws-dispatcher.js +4 -1
  90. data/lib/clacky.rb +2 -0
  91. metadata +24 -1
@@ -34,8 +34,15 @@ const QuoteSelect = (() => {
34
34
  '<path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/>' +
35
35
  '<path d="M13 8H7"/><path d="M17 12H7"/>';
36
36
 
37
- let _btn = null; // floating quote button (lazily created, recycled)
38
- let _snap = null; // selection snapshot captured when the button appears
37
+ // Lucide "panel-right" — same glyph as the aside chrome, so the action reads
38
+ // as "send this to the right-hand panel".
39
+ const ICON_SIDE_CHAT =
40
+ '<rect width="18" height="18" x="3" y="3" rx="2"/>' +
41
+ '<path d="M15 3v18"/>';
42
+
43
+ let _bar = null; // floating action bar (lazily created, recycled)
44
+ let _barLang = null; // language the bar's labels were last rendered in
45
+ let _snap = null; // selection snapshot captured when the bar appears
39
46
  let _pending = null; // mouseup validation timer
40
47
  let _scrollFrame = null; // coalesce scroll-driven anchor updates
41
48
 
@@ -49,6 +56,13 @@ const QuoteSelect = (() => {
49
56
  return el.closest(".msg-user-wrap, .msg-assistant");
50
57
  }
51
58
 
59
+ // Scroll container a message belongs to. Main chat and the side chat panel
60
+ // share the .chat-messages-scroll class, so selections in either one resolve
61
+ // to their own pane — a quote must not survive a scroll in the other pane.
62
+ function _paneOf(root) {
63
+ return root && root.closest(".chat-messages-scroll");
64
+ }
65
+
52
66
  // Message role + 1-based chat index, counted among quotable messages only
53
67
  // (user / assistant), so the index stays consistent with rendered history.
54
68
  function _msgMeta(root) {
@@ -110,8 +124,8 @@ const QuoteSelect = (() => {
110
124
  // ── Serialization ────────────────────────────────────────────────────────
111
125
  // Read the staged quotes straight off the composer — document order, which is
112
126
  // the order the user arranged them in.
113
- function list() {
114
- const el = _input();
127
+ function list(el) {
128
+ el = el || _input();
115
129
  if (!el) return [];
116
130
  return Array.prototype.map.call(el.querySelectorAll("." + CHIP_CLASS), function (chip) {
117
131
  return {
@@ -125,8 +139,8 @@ const QuoteSelect = (() => {
125
139
  // Whole staged list → reference objects for the outgoing payload. The excerpt
126
140
  // is the point of the reference, so it rides along verbatim; `label` is the
127
141
  // provenance line ("Assistant · #82") the badge tooltip shows.
128
- function toReferences() {
129
- return list().map(function (quote) {
142
+ function toReferences(el) {
143
+ return list(el).map(function (quote) {
130
144
  return { type: "quote", label: _signature(quote), text: _cleanText(quote.text) };
131
145
  }).filter(function (ref) { return ref.text; });
132
146
  }
@@ -159,8 +173,8 @@ const QuoteSelect = (() => {
159
173
 
160
174
  // Drop a chip in at the caret (end of composer when the caret is elsewhere)
161
175
  // and leave the caret after it, ready for typing.
162
- function _insert(chip) {
163
- const el = _input();
176
+ function _insert(chip, el) {
177
+ el = el || _input();
164
178
  if (!el) return;
165
179
  const sel = window.getSelection();
166
180
  const range = (sel && sel.rangeCount && el.contains(sel.anchorNode))
@@ -193,28 +207,30 @@ const QuoteSelect = (() => {
193
207
  el.dispatchEvent(new Event("input", { bubbles: true }));
194
208
  }
195
209
 
196
- function stage(snap) {
210
+ // `el` lets a second composer (the side chat) reuse the same chip mechanics;
211
+ // it defaults to the main input.
212
+ function stage(snap, el) {
197
213
  const text = _cleanText(snap.text);
198
214
  if (!text) return;
199
- _insert(_buildChip({ role: snap.role, index: snap.index, text: text }));
215
+ _insert(_buildChip({ role: snap.role, index: snap.index, text: text }), el);
200
216
  }
201
217
 
202
218
  // Replay saved quotes into the composer (session draft switching).
203
- function restore(items) {
204
- clear();
219
+ function restore(items, el) {
220
+ clear(el);
205
221
  (items || []).forEach(function (item) {
206
222
  if (item && item.text) {
207
223
  _insert(_buildChip({
208
224
  role: item.role || "user",
209
225
  index: Number(item.index) || 0,
210
226
  text: String(item.text),
211
- }));
227
+ }), el);
212
228
  }
213
229
  });
214
230
  }
215
231
 
216
- function clear() {
217
- const el = _input();
232
+ function clear(el) {
233
+ el = el || _input();
218
234
  if (!el) return;
219
235
  Array.prototype.forEach.call(el.querySelectorAll("." + CHIP_CLASS), function (chip) {
220
236
  if (typeof Composer !== "undefined" && Composer.removeChip) Composer.removeChip(chip);
@@ -232,46 +248,74 @@ const QuoteSelect = (() => {
232
248
  }
233
249
 
234
250
  // ── Button lifecycle ─────────────────────────────────────────────────────
235
- function _createBtn() {
251
+ // The floating affordance is a bar, not a single button: quoting into the
252
+ // composer and asking about the same selection in the side chat are both
253
+ // reasonable next moves, so both live one click away.
254
+ function _createAction(kind, iconPaths, handler) {
236
255
  const btn = document.createElement("button");
237
256
  btn.type = "button";
238
257
  btn.className = "quote-select-btn";
258
+ btn.dataset.action = kind;
239
259
  btn.disabled = false;
240
260
  btn.innerHTML =
241
- `<svg xmlns="http://www.w3.org/2000/svg" width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round">${ICON_QUOTE}</svg>` +
242
- `<span>${escapeHtml(I18n.t("chat.quote.btn"))}</span>`;
261
+ `<svg xmlns="http://www.w3.org/2000/svg" width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round">${iconPaths}</svg>` +
262
+ `<span></span>`;
243
263
  // Keep the textarea focus / selection alive: a bare mousedown on a button
244
264
  // collapses the selection before click fires.
245
265
  btn.addEventListener("mousedown", e => e.preventDefault());
246
- btn.addEventListener("click", _onQuoteClick);
247
- document.body.appendChild(btn);
266
+ btn.addEventListener("click", handler);
248
267
  return btn;
249
268
  }
250
269
 
270
+ function _createBar() {
271
+ const bar = document.createElement("div");
272
+ bar.className = "quote-select-bar";
273
+ bar.appendChild(_createAction("quote", ICON_QUOTE, _onQuoteClick));
274
+ bar.appendChild(_createAction("side-chat", ICON_SIDE_CHAT, _onSideChatClick));
275
+ document.body.appendChild(bar);
276
+ return bar;
277
+ }
278
+
251
279
  function _show(rect) {
252
- if (!_btn) _btn = _createBtn();
253
- // The button is a single recycled element — when it was first created it
254
- // baked in the language of that moment. Sync the label with the current
255
- // language on every show so switching en/zh mid-session stays consistent.
256
- // (textContent assignment, no escaping needed.)
257
- const labelQ = _btn.querySelector("span");
258
- if (labelQ) labelQ.textContent = I18n.t("chat.quote.btn");
259
- // The button is display:none until visible — measuring its rect while
260
- // hidden returns 0×0, which would misplace it by its own height. Make it
261
- // visible first, then measure and position (all synchronous, no flicker).
262
- _btn.classList.add("visible");
263
- const btnRect = _btn.getBoundingClientRect();
264
- let top = rect.top - btnRect.height - 10; // 10px above the selection
280
+ if (!_bar) _bar = _createBar();
281
+ // The bar is a single recycled element — when it was first created it baked
282
+ // in the language of that moment. Sync the labels whenever the language
283
+ // changes so switching en/zh mid-session stays consistent, without
284
+ // touching the DOM on every scroll-driven reposition.
285
+ const lang = I18n.lang();
286
+ if (lang !== _barLang) {
287
+ _barLang = lang;
288
+ _bar.querySelector('[data-action="quote"] span').textContent = I18n.t("chat.quote.btn");
289
+ _bar.querySelector('[data-action="side-chat"] span').textContent = I18n.t("chat.quote.sideChat");
290
+ }
291
+ const sideBtn = _bar.querySelector('[data-action="side-chat"]');
292
+ // The side chat panel is optional (workspace-only build, extension-free
293
+ // host); drop the action instead of offering a dead button. A selection
294
+ // inside the panel itself has nowhere to go — that branch is already
295
+ // read-only — so the action stays off there too.
296
+ const insidePanel = !!(_snap && _snap.root && _snap.root.closest(".sidechat"));
297
+ sideBtn.hidden = insidePanel ||
298
+ !(window.Clacky && Clacky.SideChat && Clacky.SideChat.available());
299
+ // The bar is display:none until visible — measuring its rect while hidden
300
+ // returns 0×0, which would misplace it by its own height. Make it visible
301
+ // first, then measure and position (all synchronous, no flicker).
302
+ _bar.classList.add("visible");
303
+ const barRect = _bar.getBoundingClientRect();
304
+ let top = rect.top - barRect.height - 10; // 10px above the selection
265
305
  if (top < 8) top = rect.bottom + 10; // flip below when clipped at top
266
- _btn.style.top = top + "px";
306
+ _bar.style.top = top + "px";
267
307
  // Horizontal center of the selection; CSS translateX(-50%) finishes it.
268
- _btn.style.left = (rect.left + rect.width / 2) + "px";
308
+ // The bar carries two actions now, so a selection near the viewport edge
309
+ // would push it half off-screen — clamp the center instead.
310
+ const half = barRect.width / 2;
311
+ const maxCenter = window.innerWidth - half - 8;
312
+ _bar.style.left = Math.max(half + 8, Math.min(maxCenter, rect.left + rect.width / 2)) + "px";
269
313
  }
270
314
 
271
315
  function _hide() {
272
316
  if (_pending) { clearTimeout(_pending); _pending = null; }
273
317
  if (_scrollFrame) { cancelAnimationFrame(_scrollFrame); _scrollFrame = null; }
274
- if (_btn) _btn.classList.remove("visible");
318
+ if (_bar) _bar.classList.remove("visible");
275
319
  _snap = null;
276
320
  }
277
321
 
@@ -293,7 +337,7 @@ const QuoteSelect = (() => {
293
337
  // visible; only discard it once its source has actually left the viewport.
294
338
  function _syncAnchor() {
295
339
  if (!_snap || !_snap.range || !_snap.root) return;
296
- const messages = document.getElementById("messages");
340
+ const messages = _paneOf(_snap.root);
297
341
  if (!messages || !messages.contains(_snap.root)) return _hide();
298
342
 
299
343
  const rect = _snap.range.getBoundingClientRect();
@@ -325,6 +369,20 @@ const QuoteSelect = (() => {
325
369
  stage(snap);
326
370
  }
327
371
 
372
+ // Hand the selection to the side chat panel instead of the main composer.
373
+ // Same snapshot shape as `stage()`, but the text becomes the panel's opening
374
+ // question context rather than a staged chip.
375
+ function _onSideChatClick(e) {
376
+ e.preventDefault();
377
+ e.stopPropagation();
378
+ if (!_snap) return _hide();
379
+ const snap = _snap;
380
+ _hide();
381
+ const sideChat = window.Clacky && Clacky.SideChat;
382
+ if (!sideChat) return;
383
+ sideChat.ask({ role: snap.role, index: snap.index, text: _cleanText(snap.text) });
384
+ }
385
+
328
386
  // ── Selection validation ─────────────────────────────────────────────────
329
387
  // Runs 60ms after mouseup/touchend so the browser has finished updating the
330
388
  // selection (dbl-click word selection, drag end, etc.).
@@ -341,7 +399,7 @@ const QuoteSelect = (() => {
341
399
  const r1 = _msgRoot(range.startContainer);
342
400
  const r2 = _msgRoot(range.endContainer);
343
401
  if (!r1 || r1 !== r2) return _hide(); // cross-message selection: no unique provenance
344
- const messages = document.getElementById("messages");
402
+ const messages = _paneOf(r1);
345
403
  if (!messages || !messages.contains(r1)) return _hide();
346
404
 
347
405
  const text = sel.toString().trim();
@@ -356,7 +414,7 @@ const QuoteSelect = (() => {
356
414
  }
357
415
 
358
416
  function _onDocMouseDown(e) {
359
- if (_btn && e.target !== _btn && !_btn.contains(e.target)) _hide();
417
+ if (_bar && !_bar.contains(e.target)) _hide();
360
418
  }
361
419
 
362
420
  // ── Init ─────────────────────────────────────────────────────────────────
@@ -1,8 +1,8 @@
1
- // ── Session aside chrome — resize / collapse / opener ─────────────────────
1
+ // ── Session aside shell — resize / collapse / opener ─────────────────────
2
2
  //
3
3
  // Host-owned controls for the right column (#session-aside). The tab bar and
4
4
  // bodies inside are rendered by Clacky.ext; this only drives the surrounding
5
- // chrome so slot re-renders never disturb width or collapse state.
5
+ // shell so slot re-renders never disturb width or collapse state.
6
6
  //
7
7
  // - drag #session-aside-resize to change width (persisted)
8
8
  // - panels ask for extra width via Clacky.Aside.requestWidth(); the clamp
@@ -19,10 +19,23 @@
19
19
  "use strict";
20
20
 
21
21
  (() => {
22
- const WIDTH_KEY = "clacky.aside.width";
23
- const OPEN_KEY = "clacky.aside.open";
22
+ const SESSION_WIDTH_PREFIX = "clacky.aside.width."; // + sessionId → px
23
+ const OPEN_KEY = "clacky.aside.open"; // global default — the last choice
24
+ const SESSION_OPEN_PREFIX = "clacky.aside.open."; // + sessionId → "1" | "0"
24
25
  const FULLSCREEN_KEY = "clacky.aside.fullscreen";
25
- const MIN_W = 256;
26
+ const DEFAULT_SESSION_KEY = "__default__";
27
+ // Open/closed and width are both remembered per session: one conversation
28
+ // keeps a diff open and needs the room, the next keeps a file list and does
29
+ // not. Width has no shared fallback — a conversation that was never dragged
30
+ // starts at the CSS default, otherwise every session would look like it
31
+ // shares whatever the previous one was left at.
32
+ let activeSessionId = null;
33
+ let boundSessionId = null;
34
+ // An empty slot hides the mode without touching the stored preference: the
35
+ // panels render a frame after init, and clearing the flag there would throw
36
+ // away the fullscreen the user left behind before a reload.
37
+ let fullscreenSuppressed = false;
38
+ const MIN_W = 296;
26
39
  // Narrower than this the chat column stops being readable — composer and
27
40
  // message bubbles fold into a one-character-wide strip. Once the aside
28
41
  // crosses the line it takes the whole workspace instead.
@@ -40,7 +53,7 @@
40
53
  const $ = (id) => document.getElementById(id);
41
54
 
42
55
  // Custom properties resolve to the authored token, not px — the CSS default
43
- // is 16rem, so it needs converting. Every width set here (and the persisted
56
+ // is 18.5rem, so it needs converting. Every width set here (and the persisted
44
57
  // one) is already px.
45
58
  function currentWidth() {
46
59
  const aside = $("session-aside");
@@ -58,22 +71,44 @@
58
71
  return n;
59
72
  }
60
73
 
61
- function setWidth(px) {
74
+ function writeAsideWidth(px) {
62
75
  const aside = $("session-aside");
63
76
  if (aside) aside.style.setProperty("--session-aside-width", px + "px");
77
+ }
78
+
79
+ function setWidth(px) {
80
+ writeAsideWidth(px);
64
81
  syncChatCollapse();
65
82
  }
66
83
 
67
- // The aside wins the whole workspace as soon as it would leave the chat
68
- // column below CHAT_MIN_W. Width itself stays driven by the drag value, so
69
- // pulling the handle back past the threshold restores the split at once.
84
+ // The aside takes the whole workspace as soon as it would leave the chat
85
+ // column below CHAT_MIN_W. That is the fullscreen state, not a second one, so
86
+ // the button, Esc and the divider share a single exit. Widths are written
87
+ // directly — setWidth() calls back in here and would recurse.
70
88
  function syncChatCollapse() {
71
89
  const panel = $("chat-panel");
72
- if (!panel) return;
90
+ if (!panel || isNarrow() || !isOpen() || slotEmpty() || isFullscreen()) return;
73
91
  const panelWidth = panel.getBoundingClientRect().width;
74
- const collapsed = !isNarrow() && isOpen() && !slotEmpty() &&
75
- panelWidth > CHAT_MIN_W && panelWidth - currentWidth() < CHAT_MIN_W;
76
- panel.classList.toggle("chat-collapsed", collapsed);
92
+ if (panelWidth <= CHAT_MIN_W) return;
93
+ if (panelWidth - currentWidth() >= CHAT_MIN_W) return;
94
+ const legal = panelWidth - CHAT_MIN_W;
95
+ if (legal >= MIN_W) writeAsideWidth(legal);
96
+ setFullscreen(true);
97
+ }
98
+
99
+ // Fullscreen paints over the pixel width, so the split has to be pulled back
100
+ // inside the floor on exit — otherwise syncChatCollapse would re-enter it.
101
+ // The pulled-back width is the one the user now sees, so it is persisted: a
102
+ // reload has to land on the same split instead of the pre-fullscreen width
103
+ // that no longer fits. One pixel of slack keeps sub-pixel rounding on reload
104
+ // from counting as an overflow.
105
+ function clampSplitWidth() {
106
+ const panel = $("chat-panel");
107
+ if (!panel || isNarrow()) return;
108
+ const legal = panel.getBoundingClientRect().width - CHAT_MIN_W - 1;
109
+ if (legal < MIN_W || currentWidth() <= legal) return;
110
+ writeAsideWidth(legal);
111
+ persistWidth(legal);
77
112
  }
78
113
 
79
114
  // Reconcile temporary requests centrally so nested panels can close in any
@@ -115,7 +150,7 @@
115
150
  if (opts && opts.persist) {
116
151
  const preferred = Math.max(currentWidth(), target);
117
152
  setWidth(preferred);
118
- try { localStorage.setItem(WIDTH_KEY, String(preferred)); } catch (_e) { /* non-fatal */ }
153
+ persistWidth(preferred);
119
154
  if (widthRequests.size > 0) widthBeforeRequests = preferred;
120
155
  return null;
121
156
  }
@@ -144,27 +179,69 @@
144
179
  return !slot || slot.childElementCount === 0;
145
180
  }
146
181
 
182
+ function sessionKey() {
183
+ return activeSessionId == null || activeSessionId === "" ? DEFAULT_SESSION_KEY : String(activeSessionId);
184
+ }
185
+
186
+ function readWidth() {
187
+ try {
188
+ const own = parseFloat(localStorage.getItem(SESSION_WIDTH_PREFIX + sessionKey()));
189
+ if (isFinite(own) && own >= MIN_W) return own;
190
+ } catch (_e) { /* private mode — fall back to the CSS default */ }
191
+ return null;
192
+ }
193
+
194
+ function persistWidth(px) {
195
+ try { localStorage.setItem(SESSION_WIDTH_PREFIX + sessionKey(), String(px)); } catch (_e) { /* ignore */ }
196
+ }
197
+
198
+ function readSessionOpen() {
199
+ try {
200
+ const raw = localStorage.getItem(SESSION_OPEN_PREFIX + sessionKey());
201
+ if (raw === "1") return true;
202
+ if (raw === "0") return false;
203
+ } catch (_e) { /* private mode — treat as no record */ }
204
+ return null;
205
+ }
206
+
207
+ function writeSessionOpen(open) {
208
+ try { localStorage.setItem(SESSION_OPEN_PREFIX + sessionKey(), open ? "1" : "0"); } catch (_e) { /* ignore */ }
209
+ }
210
+
147
211
  function applyOpenState() {
148
212
  const aside = $("session-aside");
149
213
  const opener = $("btn-aside-open");
150
214
  const overlay = $("workspace-overlay");
151
215
  if (!aside) return;
152
- let open = false;
153
- try {
154
- const stored = localStorage.getItem(OPEN_KEY);
155
- open = stored === null ? false : stored !== "0";
156
- } catch (_e) { /* ignore */ }
216
+ const open = isOpen();
157
217
  const empty = slotEmpty();
158
218
  aside.classList.toggle("collapsed", !open);
159
219
  if (opener) opener.style.display = (!open && !empty) ? "" : "none";
160
220
  if (overlay) overlay.classList.toggle("active", open && !empty);
161
221
  // A fullscreen class with an empty slot would show an empty column over
162
222
  // the whole workspace (the fullscreen rule outranks the empty-slot one).
163
- if (empty) setFullscreen(false);
223
+ fullscreenSuppressed = empty;
224
+ applyFullscreenState();
225
+ syncChatCollapse();
226
+ }
227
+
228
+ // initResize only runs once per page load, so a switch has to swap the
229
+ // visible number itself. A session with no record drops the inline override
230
+ // rather than keeping the previous session's width — otherwise a conversation
231
+ // that was never dragged looks like it shares the one it came from.
232
+ function applyStoredWidth() {
233
+ const saved = readWidth();
234
+ const aside = $("session-aside");
235
+ if (aside) {
236
+ if (saved === null || saved > maxW()) aside.style.removeProperty("--session-aside-width");
237
+ else aside.style.setProperty("--session-aside-width", saved + "px");
238
+ }
164
239
  syncChatCollapse();
165
240
  }
166
241
 
167
242
  function isOpen() {
243
+ const own = readSessionOpen();
244
+ if (own !== null) return own;
168
245
  try {
169
246
  const stored = localStorage.getItem(OPEN_KEY);
170
247
  return stored !== null && stored !== "0";
@@ -172,12 +249,44 @@
172
249
  }
173
250
 
174
251
  function setOpen(open) {
252
+ writeSessionOpen(!!open);
253
+ // Mirrors the last choice globally, so a session with no record yet opens
254
+ // the way the user last left the column.
175
255
  try { localStorage.setItem(OPEN_KEY, open ? "1" : "0"); } catch (_e) { /* ignore */ }
176
256
  if (!open) setFullscreen(false);
177
257
  applyOpenState();
178
258
  }
179
259
 
260
+ // Host calls this on every session switch. Fullscreen is left behind: it is
261
+ // about the screen you were concentrating on, not about the conversation.
262
+ function onSession(sessionId) {
263
+ const next = sessionId == null || sessionId === "" ? null : String(sessionId);
264
+ if (next === activeSessionId) return;
265
+ activeSessionId = next;
266
+ // Fullscreen is about the screen you were concentrating on, not about the
267
+ // conversation: switching leaves it behind, but the first bind after a
268
+ // reload must keep what the user left on screen.
269
+ const switching = boundSessionId !== null && next !== boundSessionId;
270
+ if (next !== null) boundSessionId = next;
271
+ // Leaving fullscreen on a switch must not re-clamp the width it was
272
+ // hiding: the incoming session brings its own, and clamping here would
273
+ // overwrite a number the user never saw go wrong.
274
+ if (switching) setFullscreen(false, { keepWidth: true });
275
+ // Freeze the inherited default into this session's own record, so it keeps
276
+ // its state even after another session moves the shared fallback.
277
+ if (readSessionOpen() === null) writeSessionOpen(isOpen());
278
+ applyStoredWidth();
279
+ applyOpenState();
280
+ }
281
+
282
+ function forgetSession(sessionId) {
283
+ if (sessionId == null || sessionId === "") return;
284
+ try { localStorage.removeItem(SESSION_OPEN_PREFIX + String(sessionId)); } catch (_e) { /* ignore */ }
285
+ try { localStorage.removeItem(SESSION_WIDTH_PREFIX + String(sessionId)); } catch (_e) { /* ignore */ }
286
+ }
287
+
180
288
  function isFullscreen() {
289
+ if (fullscreenSuppressed) return false;
181
290
  try { return localStorage.getItem(FULLSCREEN_KEY) === "1"; } catch (_e) { return false; }
182
291
  }
183
292
 
@@ -258,7 +367,7 @@
258
367
  }
259
368
  }
260
369
 
261
- function setFullscreen(on) {
370
+ function setFullscreen(on, opts) {
262
371
  on = !!on;
263
372
  const wasFullscreen = isFullscreen();
264
373
  if (on === wasFullscreen) {
@@ -277,6 +386,7 @@
277
386
  applyFullscreenState();
278
387
  if (!on) {
279
388
  if (aside) void aside.offsetWidth;
389
+ if (!(opts && opts.keepWidth)) clampSplitWidth();
280
390
  restoreChatScrollPosition(fullscreenChatSnapshot);
281
391
  fullscreenChatSnapshot = null;
282
392
  if (aside) aside.style.transition = previousTransition;
@@ -288,10 +398,8 @@
288
398
  const handle = $("session-aside-resize");
289
399
  if (!aside || !handle) return;
290
400
 
291
- try {
292
- const saved = parseFloat(localStorage.getItem(WIDTH_KEY));
293
- if (saved >= MIN_W && saved <= maxW()) setWidth(saved);
294
- } catch (_e) { /* ignore */ }
401
+ const saved = readWidth();
402
+ if (saved !== null && saved <= maxW()) setWidth(saved);
295
403
 
296
404
  let dragging = false;
297
405
  let startX = 0;
@@ -323,7 +431,7 @@
323
431
  document.body.style.cursor = "";
324
432
  document.body.style.userSelect = "";
325
433
  const w = currentWidth();
326
- try { localStorage.setItem(WIDTH_KEY, w); } catch (_e) { /* ignore */ }
434
+ persistWidth(w);
327
435
  if (widthRequests.size > 0) {
328
436
  widthBeforeRequests = w;
329
437
  appliedRequestWidth = null;
@@ -341,6 +449,8 @@
341
449
  close: () => setOpen(false),
342
450
  fullscreen: (on) => setFullscreen(on !== false),
343
451
  requestWidth: requestWidth,
452
+ onSession: onSession,
453
+ forgetSession: forgetSession,
344
454
  });
345
455
  if (window.Clacky) Clacky.Aside = publicApi();
346
456