sideby 0.19.1 → 0.21.0

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 (159) hide show
  1. package/README.md +7 -4
  2. package/ai-tasks.json +28 -0
  3. package/dist/build.json +1 -1
  4. package/dist/src/cli/commands.d.ts +22 -1
  5. package/dist/src/cli/commands.js +815 -21
  6. package/dist/src/cli/format.d.ts +4 -0
  7. package/dist/src/cli/format.js +42 -4
  8. package/dist/src/cli/json-schemas.js +334 -4
  9. package/dist/src/cli/main.js +159 -13
  10. package/dist/src/core/accounts.d.ts +6 -0
  11. package/dist/src/core/accounts.js +12 -0
  12. package/dist/src/core/autostart.d.ts +11 -0
  13. package/dist/src/core/autostart.js +32 -5
  14. package/dist/src/core/browser-profiles.d.ts +23 -2
  15. package/dist/src/core/browser-profiles.js +148 -37
  16. package/dist/src/core/claude-settings-file.d.ts +18 -0
  17. package/dist/src/core/claude-settings-file.js +52 -3
  18. package/dist/src/core/config.d.ts +5 -0
  19. package/dist/src/core/config.js +12 -1
  20. package/dist/src/core/edit-settings.d.ts +7 -0
  21. package/dist/src/core/edit-settings.js +10 -5
  22. package/dist/src/core/history-group.d.ts +28 -0
  23. package/dist/src/core/history-group.js +47 -0
  24. package/dist/src/core/history-index.d.ts +145 -0
  25. package/dist/src/core/history-index.js +447 -0
  26. package/dist/src/core/history-project.d.ts +13 -0
  27. package/dist/src/core/history-project.js +87 -0
  28. package/dist/src/core/history.d.ts +210 -0
  29. package/dist/src/core/history.js +952 -0
  30. package/dist/src/core/inbound.d.ts +62 -0
  31. package/dist/src/core/inbound.js +215 -0
  32. package/dist/src/core/launch.d.ts +9 -1
  33. package/dist/src/core/launch.js +58 -24
  34. package/dist/src/core/live-sessions-copy.d.ts +5 -0
  35. package/dist/src/core/live-sessions-copy.js +46 -0
  36. package/dist/src/core/live-sessions-procs.d.ts +89 -0
  37. package/dist/src/core/live-sessions-procs.js +296 -0
  38. package/dist/src/core/live-sessions-title.d.ts +28 -0
  39. package/dist/src/core/live-sessions-title.js +80 -0
  40. package/dist/src/core/live-sessions.d.ts +68 -0
  41. package/dist/src/core/live-sessions.js +163 -0
  42. package/dist/src/core/login-check.d.ts +15 -0
  43. package/dist/src/core/login-check.js +32 -0
  44. package/dist/src/core/messages.d.ts +83 -0
  45. package/dist/src/core/messages.js +433 -0
  46. package/dist/src/core/project-categories.d.ts +14 -0
  47. package/dist/src/core/project-categories.js +48 -0
  48. package/dist/src/core/quota-fetch.d.ts +3 -3
  49. package/dist/src/core/quota-fetch.js +5 -5
  50. package/dist/src/core/quota-levels.d.ts +5 -0
  51. package/dist/src/core/quota-levels.js +5 -0
  52. package/dist/src/core/remote-control.d.ts +2 -0
  53. package/dist/src/core/remote-control.js +34 -59
  54. package/dist/src/core/run-link.d.ts +76 -0
  55. package/dist/src/core/run-link.js +245 -0
  56. package/dist/src/core/same-login.d.ts +4 -3
  57. package/dist/src/core/same-login.js +10 -7
  58. package/dist/src/core/session-select.d.ts +31 -0
  59. package/dist/src/core/session-select.js +44 -0
  60. package/dist/src/families/browser.d.ts +7 -18
  61. package/dist/src/families/browser.js +19 -8
  62. package/dist/src/families/claude/deliver.d.ts +2 -0
  63. package/dist/src/families/claude/deliver.js +43 -0
  64. package/dist/src/families/claude/history.d.ts +2 -0
  65. package/dist/src/families/claude/history.js +192 -0
  66. package/dist/src/families/claude/live-sessions.d.ts +13 -0
  67. package/dist/src/families/claude/live-sessions.js +117 -0
  68. package/dist/src/families/claude/messaging.d.ts +69 -0
  69. package/dist/src/families/claude/messaging.js +165 -0
  70. package/dist/src/families/claude/mod.d.ts +26 -0
  71. package/dist/src/families/claude/mod.js +105 -0
  72. package/dist/src/families/claude/session-files.d.ts +21 -0
  73. package/dist/src/families/claude/session-files.js +54 -0
  74. package/dist/src/families/codex/deliver.d.ts +5 -0
  75. package/dist/src/families/codex/deliver.js +76 -0
  76. package/dist/src/families/codex/history.d.ts +7 -0
  77. package/dist/src/families/codex/history.js +174 -0
  78. package/dist/src/families/codex/index.js +2 -0
  79. package/dist/src/families/codex/live-sessions.d.ts +12 -0
  80. package/dist/src/families/codex/live-sessions.js +193 -0
  81. package/dist/src/families/codex/quota.d.ts +3 -0
  82. package/dist/src/families/codex/quota.js +123 -0
  83. package/dist/src/families/codex/rate-limits.js +2 -0
  84. package/dist/src/families/codex/rollout.js +1 -1
  85. package/dist/src/families/grok/browser.d.ts +3 -0
  86. package/dist/src/families/grok/browser.js +5 -0
  87. package/dist/src/families/grok/history.d.ts +2 -0
  88. package/dist/src/families/grok/history.js +157 -0
  89. package/dist/src/families/grok/live-sessions.d.ts +4 -0
  90. package/dist/src/families/grok/live-sessions.js +80 -0
  91. package/dist/src/families/grok/login-key.d.ts +14 -0
  92. package/dist/src/families/grok/login-key.js +37 -0
  93. package/dist/src/families/history.d.ts +20 -0
  94. package/dist/src/families/history.js +81 -0
  95. package/dist/src/families/live-sessions.d.ts +26 -0
  96. package/dist/src/families/live-sessions.js +9 -0
  97. package/dist/src/families/login-keys.js +5 -1
  98. package/dist/src/families/messaging.d.ts +27 -0
  99. package/dist/src/families/messaging.js +6 -0
  100. package/dist/src/families/pi/history.d.ts +2 -0
  101. package/dist/src/families/pi/history.js +124 -0
  102. package/dist/src/families/pi/live-sessions.d.ts +2 -0
  103. package/dist/src/families/pi/live-sessions.js +70 -0
  104. package/dist/src/families/shared/history-text.d.ts +65 -0
  105. package/dist/src/families/shared/history-text.js +261 -0
  106. package/dist/src/menubar/hotkey.d.ts +86 -0
  107. package/dist/src/menubar/hotkey.js +159 -0
  108. package/dist/src/menubar/script.d.ts +2 -0
  109. package/dist/src/menubar/script.js +118 -22
  110. package/dist/src/menubar/snapshot.d.ts +8 -4
  111. package/dist/src/menubar/snapshot.js +45 -13
  112. package/dist/src/menubar/strings.d.ts +24 -6
  113. package/dist/src/menubar/strings.js +24 -6
  114. package/dist/src/panel/handler.d.ts +69 -0
  115. package/dist/src/panel/handler.js +686 -15
  116. package/dist/src/panel/i18n.d.ts +305 -3
  117. package/dist/src/panel/i18n.js +622 -18
  118. package/dist/src/panel/orca.d.ts +48 -0
  119. package/dist/src/panel/orca.js +153 -0
  120. package/dist/src/panel/page-logic.d.ts +260 -13
  121. package/dist/src/panel/page-logic.js +497 -1
  122. package/dist/src/panel/page-script.js +2309 -204
  123. package/dist/src/panel/page-styles.js +398 -32
  124. package/dist/src/panel/page.js +1 -0
  125. package/dist/src/plugins/check.js +16 -4
  126. package/dist/src/plugins/loader.d.ts +8 -1
  127. package/dist/src/plugins/loader.js +72 -7
  128. package/dist/src/runtime.d.ts +5 -0
  129. package/dist/src/runtime.js +27 -5
  130. package/dist/src/types.d.ts +116 -1
  131. package/dist/testing/index.d.ts +5 -0
  132. package/dist/testing/index.js +5 -0
  133. package/mods/claude/.claude-plugin/plugin.json +7 -0
  134. package/mods/claude/hooks/hooks.json +1 -0
  135. package/mods/claude/hooks/register.js +261 -0
  136. package/package.json +5 -2
  137. package/schemas/autostart.json +74 -1
  138. package/schemas/browser-set.json +110 -0
  139. package/schemas/browser.json +22 -2
  140. package/schemas/config.json +26 -1
  141. package/schemas/history-index.json +209 -0
  142. package/schemas/history-projects.json +133 -0
  143. package/schemas/history-show.json +275 -0
  144. package/schemas/history.json +410 -0
  145. package/schemas/menubar-hotkey.json +139 -0
  146. package/schemas/menubar-snapshot.json +36 -2
  147. package/schemas/menubar.json +74 -1
  148. package/schemas/messages-inbound.json +258 -0
  149. package/schemas/messages.json +164 -0
  150. package/schemas/send.json +165 -0
  151. package/schemas/sessions-copy.json +113 -0
  152. package/schemas/sessions-open-run.json +97 -0
  153. package/schemas/sessions.json +237 -0
  154. package/skills/sideby/SKILL.md +10 -5
  155. package/skills/sideby/references/accounts.md +11 -0
  156. package/skills/sideby/references/ai-tasks.md +17 -1
  157. package/skills/sideby/references/panel.md +6 -4
  158. package/skills/sideby/references/quota.md +2 -2
  159. package/skills/sideby/references/sessions.md +79 -0
@@ -137,6 +137,20 @@ export function inDisplayOrder(items, key, order) {
137
137
  .sort((x, y) => x.r - y.r || x.i - y.i)
138
138
  .map((x) => x.t);
139
139
  }
140
+ /**
141
+ * The categories of the History side list in the order the user dragged them to: the saved ones first, then the
142
+ * others in the order `seen` gives (latest Session first). The empty category (Projects without one) stays last.
143
+ */
144
+ export function categoryOrder(seen, saved) {
145
+ const has = new Set(seen);
146
+ const out = [];
147
+ for (const c of [...saved, ...seen])
148
+ if (c && has.has(c) && !out.includes(c))
149
+ out.push(c);
150
+ if (has.has(''))
151
+ out.push('');
152
+ return out;
153
+ }
140
154
  /**
141
155
  * The Display Order after a drag (spec §3.25). `all` is every key of the list in the current Display Order, hidden
142
156
  * ones included; `shown` is what the screen showed, in its order, as units (one key, or a Same Login group that moves
@@ -313,6 +327,40 @@ export function mergeDaily(lists) {
313
327
  .sort()
314
328
  .map((date) => ({ date, totalTokens: sums[date] || 0 }));
315
329
  }
330
+ /**
331
+ * One usage for the directories of a Same Login (spec §3.21): their token counts and days added up, their newest
332
+ * use. Directories without readable usage are left out; with none readable it is the first one's usage as it is.
333
+ */
334
+ export function mergeUsage(usages) {
335
+ const ok = usages.filter((u) => Boolean(u && u.status === 'ok'));
336
+ if (ok.length < 2)
337
+ return ok[0] || usages[0];
338
+ const sum = (k) => ok.reduce((n, u) => n + (Number(u[k]) || 0), 0);
339
+ const at = (u) => {
340
+ const v = Date.parse(u.lastActivityAt || '');
341
+ return Number.isFinite(v) ? v : -1;
342
+ };
343
+ const newest = ok.reduce((x, y) => (at(y) > at(x) ? y : x));
344
+ const days = {};
345
+ for (const u of ok)
346
+ for (const d of u.daily || [])
347
+ if (d && typeof d.date === 'string')
348
+ days[d.date] = (days[d.date] || 0) + (Number(d.totalTokens) || 0);
349
+ return {
350
+ ...ok[0],
351
+ status: 'ok',
352
+ sessions: sum('sessions'),
353
+ totalTokens: sum('totalTokens'),
354
+ inputTokens: sum('inputTokens'),
355
+ outputTokens: sum('outputTokens'),
356
+ cacheReadTokens: sum('cacheReadTokens'),
357
+ cacheWriteTokens: sum('cacheWriteTokens'),
358
+ daily: Object.keys(days)
359
+ .sort()
360
+ .map((date) => ({ date, totalTokens: days[date] || 0 })),
361
+ lastActivityAt: newest.lastActivityAt,
362
+ };
363
+ }
316
364
  /** The soonest Quota reset still ahead of `now`, with the window label and Account. */
317
365
  export function nextReset(accounts, now) {
318
366
  let best = null;
@@ -546,6 +594,178 @@ export function updatedAge(ms) {
546
594
  return { key: 'time.hAgo', n: h };
547
595
  return { key: 'time.dAgo', n: Math.floor(h / 24) };
548
596
  }
597
+ /**
598
+ * The Sessions page order: a Session that needs the user first, then the most recent activity (`updatedAt`, else
599
+ * `startedAt`), then the label, so two answers with the same Sessions keep the same order.
600
+ */
601
+ export function sortSessions(list) {
602
+ const at = (s) => {
603
+ const ts = Date.parse(s.updatedAt || s.startedAt || '');
604
+ return Number.isFinite(ts) ? ts : 0;
605
+ };
606
+ const label = (s) => s.label || s.sessionId;
607
+ return list
608
+ .slice()
609
+ .sort((a, b) => (a.status === 'needs-you' ? 0 : 1) - (b.status === 'needs-you' ? 0 : 1) ||
610
+ at(b) - at(a) ||
611
+ (label(a) < label(b) ? -1 : label(a) > label(b) ? 1 : 0));
612
+ }
613
+ /**
614
+ * The order a refresh shows: Sessions already on screen keep their places (`shown`, their ids in screen order), so a
615
+ * Session that just answered does not jump past another. A Session that needs the user still goes first, and one not
616
+ * shown before comes ahead of the shown ones, in `sortSessions` order.
617
+ */
618
+ export function steadyOrder(shown, list) {
619
+ const sorted = sortSessions(list);
620
+ if (!shown.length)
621
+ return sorted;
622
+ const pos = new Map(shown.map((id, i) => [id, i]));
623
+ const needs = (s) => (s.status === 'needs-you' ? 0 : 1);
624
+ const rank = sorted.map((s, i) => ({
625
+ s,
626
+ seen: pos.has(s.sessionId) ? 1 : 0,
627
+ at: pos.get(s.sessionId) ?? i,
628
+ }));
629
+ rank.sort((a, b) => needs(a.s) - needs(b.s) || a.seen - b.seen || a.at - b.at);
630
+ return rank.map((x) => x.s);
631
+ }
632
+ /** How long ago a Session was last active, as a message key and count; null when it has no time at all. */
633
+ export function sessionAge(s, now) {
634
+ const ts = Date.parse(s.updatedAt || s.startedAt || '');
635
+ return Number.isFinite(ts) ? updatedAge(Math.max(0, now - ts)) : null;
636
+ }
637
+ /**
638
+ * The three parts of a Session Label as the row shows them: the Account's short name (bold), the Host's title (the
639
+ * working directory's name when the Host gave none) and the first 8 characters of the id (monospace).
640
+ */
641
+ export function sessionLabelParts(s) {
642
+ const dir = (s.cwd || '').replace(/\/+$/, '').split('/').pop() || '';
643
+ return {
644
+ account: s.accountLabel || s.account,
645
+ title: (s.title || '').trim() || dir,
646
+ id: s.sessionId.slice(0, 8),
647
+ };
648
+ }
649
+ /**
650
+ * What the "Send to it" dialog shows after a send: `done` (the Host took it; `hint` is any note, shown as a yellow
651
+ * hint), `approve` (it took it, but the target shows it only once the user approves it there; `reason` says how, ADR-0045),
652
+ * `paste` (the user pastes `pasteText`; `reason` says why, `jump` offers the
653
+ * target's Orca tab) or `stored` (kept as a file only; `pasteText` for reference). `key` is the headline's message key.
654
+ */
655
+ export function sendResultView(m, orca) {
656
+ const pasteText = m.pasteText || '';
657
+ const note = (m.note || '').trim();
658
+ if (m.status === 'delivered' && m.needsApproval)
659
+ return { kind: 'approve', key: 'send.approve', pasteText, reason: note, hint: '', jump: false };
660
+ if (m.status === 'delivered' || m.status === 'read')
661
+ return {
662
+ kind: 'done',
663
+ key: m.status === 'read' ? 'send.read' : 'send.done',
664
+ pasteText,
665
+ reason: '',
666
+ hint: note,
667
+ jump: false,
668
+ };
669
+ if (m.status === 'paste-needed')
670
+ return {
671
+ kind: 'paste',
672
+ key: 'send.paste',
673
+ pasteText,
674
+ reason: note,
675
+ hint: '',
676
+ jump: Boolean(orca && pasteText),
677
+ };
678
+ return { kind: 'stored', key: 'send.stored', pasteText, reason: note, hint: '', jump: false };
679
+ }
680
+ /** The chip a Sessions row shows for the latest Message to it, or null when there is none worth showing. */
681
+ export function messageChip(status, needsApproval) {
682
+ // Taken by the Host, but shown only once the user approves it in the target's terminal (ADR-0045).
683
+ if (status === 'delivered' && needsApproval)
684
+ return { key: 'send.chip.approve', tone: 'warn' };
685
+ if (status === 'delivered')
686
+ return { key: 'send.chip.delivered', tone: 'accent' };
687
+ if (status === 'read')
688
+ return { key: 'send.chip.read', tone: 'ok' };
689
+ if (status === 'paste-needed')
690
+ return { key: 'send.chip.paste', tone: 'warn' };
691
+ return null;
692
+ }
693
+ /** A Session Label without its short id: `cc002 · knip cleanup · 61230c3d` → `cc002 · knip cleanup`. */
694
+ export function shortSessionLabel(label) {
695
+ const parts = (label || '').split(' · ').filter(Boolean);
696
+ if (parts.length > 1 && /^[0-9a-f]{8}$/i.test(parts[parts.length - 1]))
697
+ parts.pop();
698
+ return parts.join(' · ');
699
+ }
700
+ /**
701
+ * What changed between two readings of the latest Messages, for the Panel's notices (spec §3.32): `new` (a Message
702
+ * reached a Session), `approve` (it reached one that shows it only once the user approves it, ADR-0045), `read` (its
703
+ * Session read it) or `paste` (it waits for the user to paste it). The first reading (`before` null) only sets the
704
+ * baseline, so opening the Panel announces nothing that was already there.
705
+ */
706
+ export function messageNews(before, after) {
707
+ if (!before)
708
+ return [];
709
+ const out = [];
710
+ for (const [sessionId, m] of Object.entries(after)) {
711
+ // A kept Message that is not delivered yet says nothing; it is announced once it arrives or needs a paste.
712
+ if (m.status === 'stored')
713
+ continue;
714
+ const was = before[sessionId];
715
+ const same = was && was.id === m.id;
716
+ if (same && was.status === m.status)
717
+ continue;
718
+ const fresh = !same || was.status === 'stored';
719
+ const kind = m.status === 'paste-needed'
720
+ ? 'paste'
721
+ : fresh
722
+ ? m.status === 'delivered' && m.needsApproval
723
+ ? 'approve'
724
+ : 'new'
725
+ : m.status === 'read'
726
+ ? 'read'
727
+ : null;
728
+ if (kind)
729
+ out.push({ kind, sessionId, id: m.id, to: shortSessionLabel(m.to), from: shortSessionLabel(m.from) });
730
+ }
731
+ return out;
732
+ }
733
+ /**
734
+ * Whether "Send to it" must wait: any screenshot that failed (refused, or its upload broke) stops the send, so a
735
+ * message never goes without a picture the user added. `names` lists them for the note; null lets it go.
736
+ */
737
+ export function sendBlock(files) {
738
+ const failed = files.filter((f) => f.status === 'error').map((f) => f.name);
739
+ return failed.length ? { key: 'send.blocked', names: failed.join(', ') } : null;
740
+ }
741
+ /**
742
+ * Why "Send to it" refuses a file before uploading it, as a message key, or null when it may go: images only (PNG,
743
+ * JPEG, GIF, WebP), at most 20 MB each, at most 5. `kept` is how many the dialog already holds. The server checks all
744
+ * three again; this only says it sooner.
745
+ */
746
+ export function sendFileProblem(file, kept) {
747
+ if (kept >= 5)
748
+ return 'send.max';
749
+ if (!['image/png', 'image/jpeg', 'image/gif', 'image/webp'].includes(file.type || ''))
750
+ return 'send.notImage';
751
+ if (file.size > 20 * 1024 * 1024)
752
+ return 'send.tooBig';
753
+ return null;
754
+ }
755
+ /**
756
+ * A working directory short enough for one row: whole when it fits in `max` characters, else its root (`~` or the
757
+ * first folder) and its last two folders around an ellipsis, else only the end. Empty when there is none.
758
+ */
759
+ export function shortCwd(cwd, max = 44) {
760
+ if (!cwd)
761
+ return '';
762
+ if (cwd.length <= max)
763
+ return cwd;
764
+ const parts = cwd.split('/');
765
+ const head = parts[0] === '' ? `/${parts[1] || ''}` : parts[0];
766
+ const short = `${head}/…/${parts.slice(-2).join('/')}`;
767
+ return short.length <= max && parts.length > 4 ? short : `…${cwd.slice(-(max - 1))}`;
768
+ }
549
769
  /**
550
770
  * The Panel's language: a host `lang` of `en` or `zh` wins; `auto` uses the viewer's saved choice, then the browser
551
771
  * language (`zh*` gives Chinese, anything else English).
@@ -691,6 +911,8 @@ export const AI_TASK_KINDS = [
691
911
  'plugin',
692
912
  'terminal',
693
913
  'handoff',
914
+ 'menubar',
915
+ 'hotkey',
694
916
  ];
695
917
  /**
696
918
  * The message keys of the AI dialog for `task` (spec §3.28 "每个入口的提示"): its title, the text box placeholder and
@@ -791,6 +1013,10 @@ export function taskPrompt(input, strip) {
791
1013
  plugin: '帮我写一个 sideby 插件或 hook。',
792
1014
  terminal: '帮我让 sideby 能在我用的终端里打开 AI 任务:写一条自定义终端命令。',
793
1015
  handoff: '帮我调好 sideby 的自动接力:根据我的账号推荐什么时候接力(阈值)和接力的顺序。',
1016
+ menubar: 'sideby 的状态栏额度图标在 macOS 顶栏里看不到,请帮我找出原因并让它显示出来。',
1017
+ hotkey: '帮我给 sideby 的状态栏菜单挑一个单手按着顺手、又没被别的程序占用的快捷键,并替我设好。',
1018
+ pHotkey: '先运行 `sideby menubar hotkey --json` 看现在用的组合。再查本机已经被占的组合:`defaults read com.apple.symbolichotkeys AppleSymbolicHotKeys` 里 enabled 为 1 的系统快捷键,以及 `/Applications` 里装了哪些常用全局快捷键的 App(Raycast、Alfred、ChatGPT、Claude 等),按它们的默认快捷键判断;不要打开它们的配置文件(里面可能有密钥),拿不准就问我。问我习惯用哪只手、平时常按哪些快捷键,然后推荐 2 到 3 个按起来顺手、又没被占的组合(两个键最顺手,比如 ⌥X;必须带 ⌥、⌘ 或 ⌃),说清每个会占掉什么(比如 ⌥X 不能再打出 ≈)。我选定后运行 `sideby menubar hotkey <组合>`,几秒内生效,不用重启;再请我按一下,确认能打开菜单。按了没反应,说明别的 App 先占了,换下一个。不要改系统或别的 App 的快捷键设置。',
1019
+ pMenubar: '先运行 `sideby menubar --json`。`state` 为 `off`:它被关掉了(常见于菜单里的「从状态栏隐藏…」),给我看 `sideby menubar on` 的预览,我同意后加 `--yes`。`state` 为 `on` 但 `loaded` 为 `false`:macOS 没在运行它,`sideby menubar on --yes` 只是重新启动,不改文件。`loaded` 为 `true`:它在运行但被挡住了,依次查:状态栏是否太满或被刘海挡住(按住 ⌘ 拖动图标);有没有 Ice、Bartender、Hidden Bar 这类菜单栏整理工具把它收起来了(查 `/Applications`,告诉我去它的设置里把 sideby(显示为 osascript)挪出来);macOS 26 及以上要在「系统设置 › 菜单栏」里允许 osascript(`sideby menubar --json` 里的快捷键 `hotkey.label` 可以在任何地方打开菜单,用来确认它在运行)。不要结束或重启任何不是 sideby 的进程。',
794
1020
  cHandoff: '自动接力现在的设置(来自 `sideby handoff status --json`):',
795
1021
  pHandoff: '先运行 `sideby handoff status --json` 和 `sideby quota --json`,看清每个账号的额度来源和用量。只用 `sideby handoff enable …` 或 `sideby config set handoff.<键> <值>` 改设置;先不加 `--yes` 给我看预览,我同意后再加 `--yes`。没有额度数据的账号排在顺序后面,并告诉我怎么让它显示额度。',
796
1022
  cAccount: '账号:',
@@ -830,6 +1056,10 @@ export function taskPrompt(input, strip) {
830
1056
  plugin: 'Help me write a sideby plugin or hook.',
831
1057
  terminal: 'Help sideby open AI Tasks in the terminal I use: write a custom terminal command.',
832
1058
  handoff: "Help me tune sideby's Auto Handoff: from my accounts, recommend when to hand over (the threshold) and in what order.",
1059
+ menubar: "I can't see sideby's quota icon in the macOS menu bar. Find out why and make it show.",
1060
+ hotkey: "Pick a shortcut for sideby's menu bar menu that is easy to press with one hand and that nothing else uses, and set it for me.",
1061
+ pHotkey: 'Run `sideby menubar hotkey --json` first to see the current shortcut. Then find the combinations already taken on this Mac: the system shortcuts with enabled 1 in `defaults read com.apple.symbolichotkeys AppleSymbolicHotKeys`, and the apps in /Applications that usually hold a global shortcut (Raycast, Alfred, ChatGPT, Claude and the like), judged by their default shortcuts; do not open their config files (they may hold keys), and ask me when unsure. Ask which hand I use and which shortcuts I press often, then recommend 2 or 3 combinations that are easy to press and free (two keys are easiest, such as ⌥X; each needs ⌥, ⌘ or ⌃), and say what each takes away (⌥X no longer types ≈). Once I pick one, run `sideby menubar hotkey <keys>`: it works within seconds, with no restart; then ask me to press it and check the menu opens. If nothing happens another app took it first, so try the next. Never change the shortcuts of macOS or another app.',
1062
+ pMenubar: 'Run `sideby menubar --json` first. `state` is `off`: it was turned off (often with "Hide from Menu Bar…" in its menu); show me the preview of `sideby menubar on`, and add `--yes` after I agree. `state` is `on` with `loaded: false`: macOS is not running it; `sideby menubar on --yes` only starts it again and changes no file. `loaded: true`: it runs but is hidden; check in turn whether the menu bar is too full or the notch covers it (hold ⌘ and drag icons), whether a menu bar organizer such as Ice, Bartender or Hidden Bar tucked it away (look in /Applications, and tell me where in its settings to move sideby, shown as osascript, out), and, on macOS 26 and later, whether osascript is allowed under System Settings › Menu Bar. The shortcut in `hotkey.label` opens the menu from anywhere, which shows it is running. Never end or restart a process that is not sideby\'s.',
833
1063
  cHandoff: 'Auto Handoff now (from `sideby handoff status --json`):',
834
1064
  pHandoff: 'Run `sideby handoff status --json` and `sideby quota --json` first to see where each account gets its quota and how much is used. Change settings only with `sideby handoff enable …` or `sideby config set handoff.<key> <value>`; show me the preview without `--yes` first, and add `--yes` only after I agree. Put accounts without quota data last in the order, and tell me how to make them show it.',
835
1065
  cAccount: 'Account: ',
@@ -979,6 +1209,31 @@ export function panelMigration(config, local) {
979
1209
  * The line under the start-at-login switch (spec §3.29) as a message key, whether it is on, whether the switch can
980
1210
  * be moved, and the action beside it: `fix` turns it on again so it starts this sideby. Read-only: no switch, no action.
981
1211
  */
1212
+ /**
1213
+ * A key press in the Panel's shortcut recorder as config `menubar.hotkey` writes it (`ctrl+opt+s`), or null while only
1214
+ * modifiers are held or the key is not a letter or digit. Reads `code`, not `key`: on a Mac ⌥S types "ß".
1215
+ */
1216
+ export function hotkeyFromKey(e) {
1217
+ const m = /^(?:Key([A-Z])|Digit([0-9]))$/.exec(e.code);
1218
+ if (!m)
1219
+ return null;
1220
+ const mods = [e.ctrlKey && 'ctrl', e.altKey && 'opt', e.shiftKey && 'shift', e.metaKey && 'cmd'].filter(Boolean);
1221
+ if (!mods.length)
1222
+ return null;
1223
+ return [...mods, (m[1] || m[2]).toLowerCase()].join('+');
1224
+ }
1225
+ /**
1226
+ * Whether a key press in the shortcut recorder is only a modifier being held (`wait`: the letter is still to come) or
1227
+ * a key the menu bar shortcut cannot use (`unsupported`: an arrow, F-key, Space and the like), so the recorder can say
1228
+ * so at once instead of looking stuck. A letter or digit is neither (see `hotkeyFromKey`).
1229
+ */
1230
+ export function hotkeyPressKind(code) {
1231
+ if (/^(?:Key[A-Z]|Digit[0-9])$/.test(code))
1232
+ return 'key';
1233
+ if (/^(?:Meta|Control|Alt|Shift|OS|CapsLock|Fn)/.test(code) || code === '')
1234
+ return 'wait';
1235
+ return 'unsupported';
1236
+ }
982
1237
  export function autostartNote(s, readOnly) {
983
1238
  if (!s)
984
1239
  return { key: 'as.checking', on: false, toggle: false, action: null };
@@ -987,7 +1242,14 @@ export function autostartNote(s, readOnly) {
987
1242
  if (s.state === 'foreign')
988
1243
  return { key: 'as.foreign', on: false, toggle: false, action: null };
989
1244
  const on = s.state === 'on' || s.state === 'stale';
990
- const fix = s.state === 'stale' ? 'as.stale' : on && !s.current ? 'as.other' : null;
1245
+ // Not running comes after stale (that fix starts it too) and before another copy (the icon is what is missing).
1246
+ const fix = s.state === 'stale'
1247
+ ? 'as.stale'
1248
+ : on && s.loaded === false
1249
+ ? 'as.notRunning'
1250
+ : on && !s.current
1251
+ ? 'as.other'
1252
+ : null;
991
1253
  return { key: fix || 'as.hint', on, toggle: !readOnly, action: fix && !readOnly ? 'fix' : null };
992
1254
  }
993
1255
  /**
@@ -1078,3 +1340,237 @@ export function widthFollower(shown, tierOf, apply, settleMs, timers) {
1078
1340
  timer = timers.set(settle, settleMs);
1079
1341
  };
1080
1342
  }
1343
+ // ---------- Session History (spec §3.33) ----------
1344
+ /** When a History row was last active: "x min ago" within a week, then its date (key `time.on`). */
1345
+ /**
1346
+ * Whether the end of a list is close enough to load its next page: within `margin` px of the bottom of what can be
1347
+ * seen, which is the viewport where the list's box grows with its rows (the page scrolls, not the box).
1348
+ */
1349
+ export function tailInView(tailTop, boxBottom, viewHeight, margin = 200) {
1350
+ return tailTop < Math.min(boxBottom, viewHeight) + margin;
1351
+ }
1352
+ export function historyWhen(iso, now) {
1353
+ const ts = Date.parse(iso || '');
1354
+ if (!Number.isFinite(ts))
1355
+ return null;
1356
+ const ms = Math.max(0, now - ts);
1357
+ if (ms < 7 * 86_400_000)
1358
+ return updatedAge(ms);
1359
+ const d = new Date(ts);
1360
+ const pad = (n) => String(n).padStart(2, '0');
1361
+ return { key: 'time.on', date: d.getFullYear() + '-' + pad(d.getMonth() + 1) + '-' + pad(d.getDate()) };
1362
+ }
1363
+ /** `text` cut into the parts that match one of `words` (any case) and the parts between, for highlighting. */
1364
+ export function highlightParts(text, words) {
1365
+ const low = text.toLowerCase();
1366
+ const marks = [];
1367
+ for (const w of words) {
1368
+ const lw = w.toLowerCase();
1369
+ if (!lw)
1370
+ continue;
1371
+ for (let at = low.indexOf(lw); at >= 0; at = low.indexOf(lw, at + lw.length))
1372
+ marks.push([at, at + lw.length]);
1373
+ }
1374
+ marks.sort((a, b) => a[0] - b[0] || b[1] - a[1]);
1375
+ const out = [];
1376
+ let pos = 0;
1377
+ for (const [from, to] of marks) {
1378
+ if (to <= pos)
1379
+ continue;
1380
+ const start = Math.max(from, pos);
1381
+ if (start > pos)
1382
+ out.push({ text: text.slice(pos, start), hit: false });
1383
+ out.push({ text: text.slice(start, to), hit: true });
1384
+ pos = to;
1385
+ }
1386
+ if (pos < text.length)
1387
+ out.push({ text: text.slice(pos), hit: false });
1388
+ return out;
1389
+ }
1390
+ /**
1391
+ * What an empty History list says, and which way out it offers: a wider time range, searching tool output too.
1392
+ * Null while there are rows.
1393
+ */
1394
+ export function historyEmpty(v) {
1395
+ if (v.total > 0)
1396
+ return null;
1397
+ const searching = v.q.trim() !== '';
1398
+ const widen = v.since !== 'all';
1399
+ if (v.state === 'building')
1400
+ return { key: 'hist.emptyBuilding', widen: false, tools: false };
1401
+ if (v.unreadable)
1402
+ return { key: 'hist.emptyUnreadable', widen: false, tools: false };
1403
+ if (v.accounts === 0)
1404
+ return { key: 'hist.emptyNoAccounts', widen: false, tools: false, accounts: true };
1405
+ if (v.stored === 0 && !searching)
1406
+ return { key: 'hist.emptyNone', widen: false, tools: false };
1407
+ if (searching && v.project)
1408
+ return { key: 'hist.emptyMatchProject', widen, tools: !v.tools, everywhere: true };
1409
+ if (searching)
1410
+ return { key: 'hist.emptyMatch', widen, tools: !v.tools };
1411
+ if (v.project)
1412
+ return { key: 'hist.emptyProject', widen, tools: false };
1413
+ return { key: widen ? 'hist.emptySince' : 'hist.emptyNone', widen, tools: false };
1414
+ }
1415
+ /** A Session's messages for reading, with each run of tool calls folded into one line naming the tools. */
1416
+ export function foldToolCalls(messages) {
1417
+ const out = [];
1418
+ for (const m of messages) {
1419
+ const last = out[out.length - 1];
1420
+ if (m.role !== 'tool')
1421
+ out.push(m);
1422
+ else if (last && last.role === 'tools')
1423
+ last.tools.push(m.tool || m.text);
1424
+ else
1425
+ out.push(m.at
1426
+ ? { role: 'tools', tools: [m.tool || m.text], n: m.n, at: m.at }
1427
+ : { role: 'tools', tools: [m.tool || m.text], n: m.n });
1428
+ }
1429
+ return out;
1430
+ }
1431
+ /**
1432
+ * Splits a message into Markdown blocks: headings, fenced code, quotes, lists, GitHub tables, rules and paragraphs.
1433
+ * An unclosed fence (a message cut short) runs to the end. Anything else is a paragraph, line breaks kept.
1434
+ */
1435
+ export function mdBlocks(text) {
1436
+ const lines = text.replace(/\r\n?/g, '\n').split('\n');
1437
+ const out = [];
1438
+ const cells = (line) => {
1439
+ let s = line.trim();
1440
+ if (s.startsWith('|'))
1441
+ s = s.slice(1);
1442
+ if (s.endsWith('|') && !s.endsWith('\\|'))
1443
+ s = s.slice(0, -1);
1444
+ return s.split(/(?<!\\)\|/).map((c) => c.trim().replace(/\\\|/g, '|'));
1445
+ };
1446
+ const isRule = (line) => /^\s*\|?\s*:?-+:?\s*(\|\s*:?-+:?\s*)*\|?\s*$/.test(line) && line.includes('-');
1447
+ const listItem = (line) => /^\s{0,3}([-*+]|\d{1,9}[.)])\s+(.*)$/.exec(line);
1448
+ let para = [];
1449
+ const flush = () => {
1450
+ if (para.length)
1451
+ out.push({ type: 'p', text: para.join('\n') });
1452
+ para = [];
1453
+ };
1454
+ let i = 0;
1455
+ while (i < lines.length) {
1456
+ const line = lines[i];
1457
+ const fence = /^\s{0,3}(`{3,}|~{3,})\s*([\w+-]*)/.exec(line);
1458
+ if (fence) {
1459
+ flush();
1460
+ const body = [];
1461
+ i++;
1462
+ while (i < lines.length && !lines[i].trim().startsWith(fence[1]))
1463
+ body.push(lines[i++]);
1464
+ i++;
1465
+ out.push({ type: 'code', lang: fence[2] || '', text: body.join('\n') });
1466
+ continue;
1467
+ }
1468
+ if (!line.trim()) {
1469
+ flush();
1470
+ i++;
1471
+ continue;
1472
+ }
1473
+ const head = /^\s{0,3}(#{1,6})\s+(.*?)\s*#*\s*$/.exec(line);
1474
+ if (head) {
1475
+ flush();
1476
+ out.push({ type: 'h', level: head[1].length, text: head[2] });
1477
+ i++;
1478
+ continue;
1479
+ }
1480
+ if (/^\s{0,3}([-*_])(\s*\1){2,}\s*$/.test(line)) {
1481
+ flush();
1482
+ out.push({ type: 'hr' });
1483
+ i++;
1484
+ continue;
1485
+ }
1486
+ if (line.includes('|') && i + 1 < lines.length && isRule(lines[i + 1])) {
1487
+ const headCells = cells(line);
1488
+ const align = cells(lines[i + 1]).map((c) => c.startsWith(':') && c.endsWith(':')
1489
+ ? 'center'
1490
+ : c.endsWith(':')
1491
+ ? 'right'
1492
+ : c.startsWith(':')
1493
+ ? 'left'
1494
+ : '');
1495
+ if (align.length === headCells.length) {
1496
+ flush();
1497
+ const rows = [];
1498
+ i += 2;
1499
+ while (i < lines.length && lines[i].includes('|') && lines[i].trim()) {
1500
+ const r = cells(lines[i++]);
1501
+ rows.push(headCells.map((_, k) => r[k] ?? ''));
1502
+ }
1503
+ out.push({ type: 'table', head: headCells, align, rows });
1504
+ continue;
1505
+ }
1506
+ }
1507
+ if (/^\s{0,3}>/.test(line)) {
1508
+ flush();
1509
+ const body = [];
1510
+ while (i < lines.length && /^\s{0,3}>/.test(lines[i]))
1511
+ body.push(lines[i++].replace(/^\s{0,3}>\s?/, ''));
1512
+ out.push({ type: 'quote', text: body.join('\n') });
1513
+ continue;
1514
+ }
1515
+ const item = listItem(line);
1516
+ // A list may start right under a paragraph line; a numbered one only from 1, as in CommonMark.
1517
+ if (item && (!para.length || !/\d/.test(item[1]) || /^1\D/.test(item[1]))) {
1518
+ flush();
1519
+ const ordered = /\d/.test(item[1]);
1520
+ const items = [];
1521
+ while (i < lines.length) {
1522
+ const m = listItem(lines[i]);
1523
+ if (m && /\d/.test(m[1]) === ordered)
1524
+ items.push(m[2]);
1525
+ else if (items.length && lines[i].trim() && /^\s{2,}/.test(lines[i]))
1526
+ items[items.length - 1] += '\n' + lines[i].trim();
1527
+ else
1528
+ break;
1529
+ i++;
1530
+ }
1531
+ out.push({ type: 'list', ordered, items });
1532
+ continue;
1533
+ }
1534
+ para.push(line);
1535
+ i++;
1536
+ }
1537
+ flush();
1538
+ return out;
1539
+ }
1540
+ /** Inline Markdown in one block: `code`, **bold**, *italic*, and [links](https://…) with a web address only. */
1541
+ export function mdInline(text) {
1542
+ const out = [];
1543
+ const re = /(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)|\*\*([^*\n]+?)\*\*|__([^_\n]+?)__|\*([^*\s][^*\n]*?)\*|\[([^\]\n]+)\]\((https?:\/\/[^\s)]+)\)/g;
1544
+ let last = 0;
1545
+ for (const m of text.matchAll(re)) {
1546
+ if (m.index > last)
1547
+ out.push({ t: 'text', text: text.slice(last, m.index) });
1548
+ if (m[1])
1549
+ out.push({ t: 'code', text: m[2].trim() || m[2] });
1550
+ else if (m[3] || m[4])
1551
+ out.push({ t: 'b', text: (m[3] || m[4]) });
1552
+ else if (m[5])
1553
+ out.push({ t: 'i', text: m[5] });
1554
+ else
1555
+ out.push({ t: 'a', text: m[6], href: m[7] });
1556
+ last = m.index + m[0].length;
1557
+ }
1558
+ if (last < text.length)
1559
+ out.push({ t: 'text', text: text.slice(last) });
1560
+ return out;
1561
+ }
1562
+ /** The History filters saved before a reload, each dropped back to its default when it is missing or malformed. */
1563
+ export function histFilters(raw) {
1564
+ const o = raw && typeof raw === 'object' ? raw : {};
1565
+ const str = (v) => (typeof v === 'string' && v.length <= 1000 ? v : '');
1566
+ const since = str(o.since);
1567
+ return {
1568
+ q: str(o.q),
1569
+ since: ['today', '7d', '30d', 'all'].includes(since) ? since : '30d',
1570
+ family: str(o.family),
1571
+ account: str(o.account),
1572
+ project: str(o.project),
1573
+ category: str(o.category),
1574
+ scripts: o.scripts === true,
1575
+ };
1576
+ }