sideby 0.17.0 → 0.18.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 (95) hide show
  1. package/README.md +3 -1
  2. package/ai-tasks.json +90 -0
  3. package/dist/build.json +1 -1
  4. package/dist/src/cli/commands.d.ts +9 -1
  5. package/dist/src/cli/commands.js +349 -14
  6. package/dist/src/cli/json-schemas.js +157 -18
  7. package/dist/src/cli/main.js +56 -7
  8. package/dist/src/core/after-install.d.ts +54 -0
  9. package/dist/src/core/after-install.js +78 -0
  10. package/dist/src/core/ask.d.ts +73 -1
  11. package/dist/src/core/ask.js +106 -6
  12. package/dist/src/core/autostart.d.ts +38 -8
  13. package/dist/src/core/autostart.js +85 -39
  14. package/dist/src/core/brand-mark.d.ts +49 -11
  15. package/dist/src/core/brand-mark.js +116 -38
  16. package/dist/src/core/config.d.ts +5 -0
  17. package/dist/src/core/config.js +11 -1
  18. package/dist/src/core/desktop-app.d.ts +1 -1
  19. package/dist/src/core/desktop-app.js +2 -2
  20. package/dist/src/core/edit-settings.d.ts +24 -0
  21. package/dist/src/core/edit-settings.js +97 -0
  22. package/dist/src/core/handoff-brief.d.ts +4 -2
  23. package/dist/src/core/handoff-brief.js +6 -1
  24. package/dist/src/core/handoff-chain.d.ts +24 -0
  25. package/dist/src/core/handoff-chain.js +2 -0
  26. package/dist/src/core/handoff-run.d.ts +4 -3
  27. package/dist/src/core/handoff-run.js +14 -9
  28. package/dist/src/core/launch.d.ts +2 -0
  29. package/dist/src/core/launch.js +14 -10
  30. package/dist/src/core/local-build.d.ts +12 -5
  31. package/dist/src/core/local-build.js +49 -31
  32. package/dist/src/core/lock.d.ts +30 -0
  33. package/dist/src/core/lock.js +143 -0
  34. package/dist/src/core/quota-levels.d.ts +6 -1
  35. package/dist/src/core/quota-levels.js +16 -5
  36. package/dist/src/core/task-spec.d.ts +95 -0
  37. package/dist/src/core/task-spec.js +209 -0
  38. package/dist/src/core/terminal.js +6 -1
  39. package/dist/src/core/update.d.ts +27 -16
  40. package/dist/src/core/update.js +67 -93
  41. package/dist/src/desktop/icon.js +12 -15
  42. package/dist/src/families/claude/ai-task.d.ts +8 -0
  43. package/dist/src/families/claude/ai-task.js +38 -0
  44. package/dist/src/families/claude/handoff-hook.js +4 -4
  45. package/dist/src/families/claude/index.js +3 -0
  46. package/dist/src/families/codex/handoff-hook.js +4 -4
  47. package/dist/src/families/codex/index.js +9 -0
  48. package/dist/src/families/grok/handoff-hook.js +4 -4
  49. package/dist/src/families/grok/index.js +1 -0
  50. package/dist/src/families/pi/index.js +1 -0
  51. package/dist/src/handoff/hook.js +51 -30
  52. package/dist/src/handoff/levels.d.ts +24 -0
  53. package/dist/src/handoff/levels.js +70 -0
  54. package/dist/src/handoff/messages.d.ts +6 -3
  55. package/dist/src/handoff/messages.js +20 -7
  56. package/dist/src/handoff/reader.d.ts +6 -3
  57. package/dist/src/menubar/agent.d.ts +3 -0
  58. package/dist/src/menubar/agent.js +21 -0
  59. package/dist/src/menubar/script.d.ts +8 -0
  60. package/dist/src/menubar/script.js +457 -0
  61. package/dist/src/menubar/snapshot.d.ts +115 -0
  62. package/dist/src/menubar/snapshot.js +255 -0
  63. package/dist/src/menubar/strings.d.ts +87 -0
  64. package/dist/src/menubar/strings.js +88 -0
  65. package/dist/src/panel/background.js +12 -19
  66. package/dist/src/panel/handler.d.ts +12 -0
  67. package/dist/src/panel/handler.js +80 -80
  68. package/dist/src/panel/i18n.d.ts +33 -0
  69. package/dist/src/panel/i18n.js +66 -0
  70. package/dist/src/panel/page-logic.d.ts +105 -0
  71. package/dist/src/panel/page-logic.js +170 -15
  72. package/dist/src/panel/page-script.js +238 -109
  73. package/dist/src/panel/page-styles.js +42 -13
  74. package/dist/src/panel/page.d.ts +7 -0
  75. package/dist/src/panel/page.js +5 -0
  76. package/dist/src/runtime.d.ts +9 -1
  77. package/dist/src/runtime.js +11 -3
  78. package/dist/src/types.d.ts +14 -0
  79. package/package.json +4 -3
  80. package/schemas/ask-choices.json +168 -0
  81. package/schemas/ask-print.json +87 -0
  82. package/schemas/config.json +17 -1
  83. package/schemas/edit.json +119 -0
  84. package/schemas/error.json +4 -0
  85. package/schemas/families.json +4 -0
  86. package/schemas/list.json +4 -0
  87. package/schemas/local.json +1 -1
  88. package/schemas/menubar-snapshot.json +646 -0
  89. package/schemas/menubar-start.json +112 -0
  90. package/schemas/menubar.json +165 -0
  91. package/skills/sideby/SKILL.md +6 -2
  92. package/skills/sideby/references/ai-tasks.md +10 -0
  93. package/skills/sideby/references/handoff.md +3 -1
  94. package/skills/sideby/references/panel.md +6 -0
  95. package/skills/sideby/references/update.md +1 -1
@@ -0,0 +1,255 @@
1
+ import { openWindows, quotaLevel } from '../core/quota-levels.js';
2
+ import { fill, MENUBAR_STRINGS } from './strings.js';
3
+ export const DEFAULT_MENUBAR_HOTKEY = 'opt+cmd+s';
4
+ // macOS virtual key codes of the ANSI letters and digits (HIToolbox Events.h, kVK_ANSI_*).
5
+ const KEY_CODES = {
6
+ a: 0,
7
+ s: 1,
8
+ d: 2,
9
+ f: 3,
10
+ h: 4,
11
+ g: 5,
12
+ z: 6,
13
+ x: 7,
14
+ c: 8,
15
+ v: 9,
16
+ b: 11,
17
+ q: 12,
18
+ w: 13,
19
+ e: 14,
20
+ r: 15,
21
+ y: 16,
22
+ t: 17,
23
+ '1': 18,
24
+ '2': 19,
25
+ '3': 20,
26
+ '4': 21,
27
+ '6': 22,
28
+ '5': 23,
29
+ '9': 25,
30
+ '7': 26,
31
+ '8': 28,
32
+ '0': 29,
33
+ o: 31,
34
+ u: 32,
35
+ i: 34,
36
+ p: 35,
37
+ l: 37,
38
+ j: 38,
39
+ k: 40,
40
+ n: 45,
41
+ m: 46,
42
+ };
43
+ // Carbon modifier flags (cmdKey, shiftKey, optionKey, controlKey) and their signs, in the order macOS writes them.
44
+ const MODIFIERS = [
45
+ ['ctrl', 4096, '⌃'],
46
+ ['opt', 2048, '⌥'],
47
+ ['shift', 512, '⇧'],
48
+ ['cmd', 256, '⌘'],
49
+ ];
50
+ /** Config `menubar.hotkey` (`opt+cmd+s`, `off`) as the script registers it; null for `off` or a bad value. */
51
+ export function menubarHotkey(value) {
52
+ const parts = (value ?? DEFAULT_MENUBAR_HOTKEY).toLowerCase().split('+');
53
+ const key = parts.pop();
54
+ const keyCode = KEY_CODES[key];
55
+ if (keyCode === undefined || !parts.length)
56
+ return null;
57
+ const mods = MODIFIERS.filter(([name]) => parts.includes(name));
58
+ if (mods.length !== parts.length)
59
+ return null;
60
+ return {
61
+ keyCode,
62
+ modifiers: mods.reduce((n, [, flag]) => n + flag, 0),
63
+ label: mods.map(([, , sign]) => sign).join('') + key.toUpperCase(),
64
+ };
65
+ }
66
+ /** `2h 5m`, `3d 4h` or `12m` until `at`; at least one minute. */
67
+ export function untilText(at, now, s) {
68
+ const total = Math.max(1, Math.ceil((Date.parse(at) - now.getTime()) / 60000));
69
+ const d = Math.floor(total / 1440);
70
+ const h = Math.floor((total % 1440) / 60);
71
+ const m = total % 60;
72
+ if (d > 0)
73
+ return fill(s.daysHours, { d, h });
74
+ if (h > 0)
75
+ return fill(s.hoursMinutes, { h, m });
76
+ return fill(s.minutes, { m });
77
+ }
78
+ function row(a, plan, now, s) {
79
+ const recommended = Boolean(plan?.hasQuota && plan.pick === a.ref);
80
+ const startTip = fill(s.startTip, { account: a.ref });
81
+ const base = { ref: a.ref, label: a.name, recommended, tip: startTip, bars: null, reset: '' };
82
+ const noted = (note) => ({ ...base, detail: note, note, level: 'none' });
83
+ if (a.kind === 'api')
84
+ return noted(s.api);
85
+ if (a.login === 'logged-out')
86
+ return noted(s.signedOut);
87
+ const open = openWindows(a.quota, now);
88
+ if (a.quota.status !== 'ok' || open === null)
89
+ return noted(s.noData);
90
+ const windows = a.quota.windows
91
+ .filter((w) => !w.name)
92
+ .sort((x, y) => (x.windowMinutes ?? 0) - (y.windowMinutes ?? 0));
93
+ const bar = (w) => {
94
+ const percent = Math.round(open.find((o) => o.label === w.label)?.usedPercent ?? 0);
95
+ return { label: w.label, percent, level: quotaLevel(percent) };
96
+ };
97
+ // Two columns: the shortest window, then the fullest of the others, so a third window that is used up still shows.
98
+ const longer = windows
99
+ .slice(1)
100
+ .map(bar)
101
+ .reduce((best, b) => (!best || b.percent > best.percent ? b : best), null);
102
+ const bars = longer ? [bar(windows[0]), longer] : [null, bar(windows[0])];
103
+ const parts = windows.map((w) => fill(s.window, { label: w.label, p: bar(w).percent }));
104
+ const fullest = open.reduce((best, w) => (!best || w.usedPercent > best.usedPercent ? w : best), null);
105
+ const reset = fullest && fullest.usedPercent > 0 ? untilText(fullest.resetsAt, now, s) : '';
106
+ if (reset)
107
+ parts.push(fill(s.resets, { time: reset }));
108
+ const tips = open
109
+ .filter((w) => w.usedPercent > 0)
110
+ .map((w) => fill(s.resetsTip, { label: w.label, time: untilText(w.resetsAt, now, s) }));
111
+ return {
112
+ ...base,
113
+ detail: parts.join(s.sep),
114
+ level: quotaLevel(fullest?.usedPercent ?? 0),
115
+ tip: [...tips, startTip].join('\n'),
116
+ bars,
117
+ reset,
118
+ note: '',
119
+ };
120
+ }
121
+ /** The recommended row first; API key and signed-out rows into `more` while other rows remain to show. */
122
+ function columns(rows) {
123
+ const at = (i) => rows.map((r) => r.bars?.[i]?.label).find((l) => l !== undefined) ?? null;
124
+ return [at(0), at(1)];
125
+ }
126
+ function section(f, now, s) {
127
+ const logo = f.logo
128
+ ? {
129
+ path: f.logo.path,
130
+ ...(f.logo.fillRule ? { fillRule: f.logo.fillRule } : {}),
131
+ ...(f.logo.color ? { color: f.logo.color } : {}),
132
+ }
133
+ : null;
134
+ const all = f.accounts.map((a) => ({ a, r: row(a, f.plan, now, s) }));
135
+ const aside = (a) => a.kind === 'api' || a.login === 'logged-out';
136
+ // A Same Login is one quota, so one row, named like the Panel's: after the directory first in Display Order (the
137
+ // Panel's "Set as shown name" moves one there). It is recommended when any of its directories is. The other
138
+ // directories move into the submenu, where a click still starts them.
139
+ const shown = [];
140
+ const same = [];
141
+ const done = new Set();
142
+ for (const x of all.filter((y) => !aside(y.a))) {
143
+ if (done.has(x.a.ref))
144
+ continue;
145
+ const peers = new Set(x.a.sameLogin ?? []);
146
+ const group = all.filter((y) => y === x || (peers.has(y.a.ref) && !aside(y.a) && !done.has(y.a.ref)));
147
+ for (const y of group)
148
+ done.add(y.a.ref);
149
+ const lead = x;
150
+ const others = group.filter((y) => y !== lead);
151
+ shown.push(others.length
152
+ ? {
153
+ ...lead,
154
+ r: {
155
+ ...lead.r,
156
+ recommended: group.some((y) => y.r.recommended),
157
+ tip: [fill(s.sameLoginTip, { names: others.map((y) => y.a.name).join(', ') }), lead.r.tip].join('\n'),
158
+ },
159
+ }
160
+ : lead);
161
+ for (const y of others) {
162
+ const note = fill(s.sameLoginNote, { name: lead.a.name });
163
+ same.push({
164
+ ...y,
165
+ r: { ...y.r, bars: null, reset: '', note, detail: note, level: 'none', recommended: false },
166
+ });
167
+ }
168
+ }
169
+ const main = shown;
170
+ const rest = [...same, ...all.filter((x) => aside(x.a))];
171
+ const head = { family: f.id, title: f.title, logo };
172
+ if (!main.length)
173
+ return { ...head, columns: columns(rest.map((x) => x.r)), rows: rest.map((x) => x.r), more: null };
174
+ const rows = [...main.filter((x) => x.r.recommended), ...main.filter((x) => !x.r.recommended)].map((x) => x.r);
175
+ if (!rest.length)
176
+ return { ...head, columns: columns(rows), rows, more: null };
177
+ const api = rest.filter((x) => x.a.kind === 'api').length;
178
+ const out = rest.filter((x) => x.a.login === 'logged-out' && x.a.kind !== 'api').length;
179
+ const n = rest.length;
180
+ const title = fill(api === n ? s.moreApi : out === n ? s.moreSignedOut : s.moreMixed, { n });
181
+ return { ...head, columns: columns(rows), rows, more: { title, rows: rest.map((x) => x.r) } };
182
+ }
183
+ /**
184
+ * Each Family's number is the Quota Pressure of the account `sideby next` would pick, or, when every account is past
185
+ * the fail level, the lowest of theirs; the title shows the highest of these, so it turns red when some tool has
186
+ * nothing left to hand over to.
187
+ */
188
+ export function menubarSnapshot(families, now, lang, hotkey = null) {
189
+ const s = MENUBAR_STRINGS[lang];
190
+ let worst = null;
191
+ const sections = [];
192
+ for (const f of families) {
193
+ if (!f.accounts.length)
194
+ continue;
195
+ sections.push(section(f, now, s));
196
+ if (!f.plan?.hasQuota)
197
+ continue;
198
+ // The plan lists the usable accounts first, least used first; past them, the least used of the full ones.
199
+ // A pick without Quota of its own (`unknown`) says nothing about how much is left.
200
+ const best = f.plan.pick
201
+ ? f.plan.accounts.find((e) => e.ref === f.plan.pick)
202
+ : f.plan.accounts
203
+ .filter((e) => e.state === 'full' && e.pressure !== null)
204
+ .sort((x, y) => x.pressure - y.pressure)[0];
205
+ const value = best?.pressure ?? null;
206
+ if (best && value !== null && (!worst || value > worst.value))
207
+ worst = { value, ref: best.ref, tool: f.title };
208
+ }
209
+ const values = worst
210
+ ? { account: worst.ref, tool: worst.tool, p: Math.round(worst.value) }
211
+ : {};
212
+ const low = worst !== null && quotaLevel(worst.value) !== 'ok';
213
+ const { recommended, empty, loading, addAccount, resetsHeader, updatedJustNow, updatedMinutes, refreshing, refreshFailed, refresh, openPanel, hide, } = s;
214
+ return {
215
+ schemaVersion: 1,
216
+ title: low ? fill(s.title, values) : '',
217
+ level: worst ? quotaLevel(worst.value) : 'none',
218
+ tooltip: [
219
+ worst ? fill(low ? s.tooltipLow : s.tooltipOk, values) : s.tooltipEmpty,
220
+ ...(hotkey ? [fill(s.hotkeyTip, { key: hotkey.label })] : []),
221
+ ].join('\n'),
222
+ hotkey,
223
+ headline: worst ? fill(low ? s.headlineLow : s.headlineOk, values) : '',
224
+ card: worst
225
+ ? {
226
+ status: fill(low ? s.statusLow : s.statusOk, values),
227
+ detail: fill(low ? s.detailLow : s.detailOk, values),
228
+ level: quotaLevel(worst.value),
229
+ }
230
+ : null,
231
+ updatedAt: now.toISOString(),
232
+ lang,
233
+ sections,
234
+ labels: {
235
+ recommended,
236
+ empty,
237
+ loading,
238
+ addAccount,
239
+ resetsHeader,
240
+ updatedJustNow,
241
+ updatedMinutes,
242
+ refreshing,
243
+ refreshFailed,
244
+ refresh,
245
+ openPanel,
246
+ hide,
247
+ },
248
+ };
249
+ }
250
+ /** The language of the menu: config `panel.language`, else the system language the script passes, else English. */
251
+ export function menubarLang(configured, system) {
252
+ if (configured === 'en' || configured === 'zh')
253
+ return configured;
254
+ return /^zh/i.test(system ?? '') ? 'zh' : 'en';
255
+ }
@@ -0,0 +1,87 @@
1
+ export type MenubarLang = 'en' | 'zh';
2
+ export declare const MENUBAR_STRINGS: {
3
+ readonly en: {
4
+ readonly title: '{tool} {p}%';
5
+ readonly tooltipOk: 'All tools have plenty of quota ({account} in {tool} is highest at {p}% used) — click to see every account.';
6
+ readonly tooltipLow: '{tool} is running low: even its best account ({account}) has used {p}% of its quota — click to see every account.';
7
+ readonly tooltipEmpty: 'sideby — no quota data yet';
8
+ readonly hotkeyTip: 'Press {key} to open this menu from anywhere';
9
+ readonly headlineOk: 'Plenty of quota · highest is {account} ({tool}, {p}%)';
10
+ readonly statusOk: 'Plenty of quota';
11
+ readonly statusLow: '{tool} is running low';
12
+ readonly detailOk: 'Highest next account: {account} ({p}% used)';
13
+ readonly detailLow: '{account} is at {p}% · switch tools or wait';
14
+ readonly resetsHeader: 'Resets';
15
+ readonly headlineLow: '{tool} is running low · {account} is at {p}%';
16
+ readonly recommended: 'next';
17
+ readonly api: 'API key';
18
+ readonly signedOut: 'signed out';
19
+ readonly noData: 'no quota yet';
20
+ readonly window: '{label} {p}%';
21
+ readonly resets: 'resets in {time}';
22
+ readonly resetsTip: '{label} resets in {time}';
23
+ readonly moreApi: 'API keys ({n})';
24
+ readonly moreSignedOut: '{n} signed out';
25
+ readonly moreMixed: '{n} more';
26
+ readonly sameLoginNote: 'same login as {name}';
27
+ readonly sameLoginTip: 'Same login as {names} — they share this quota';
28
+ readonly sep: ' · ';
29
+ readonly minutes: '{m}m';
30
+ readonly hoursMinutes: '{h}h {m}m';
31
+ readonly daysHours: '{d}d {h}h';
32
+ readonly startTip: 'Start {account} in your terminal';
33
+ readonly empty: 'No accounts yet';
34
+ readonly loading: 'Loading quota…';
35
+ readonly addAccount: 'Add an Account…';
36
+ readonly updatedJustNow: 'Updated just now';
37
+ readonly updatedMinutes: 'Updated {n} min ago';
38
+ readonly refreshing: 'Refreshing…';
39
+ readonly refreshFailed: 'Could not refresh · showing older numbers';
40
+ readonly refresh: 'Refresh';
41
+ readonly openPanel: 'Open Panel';
42
+ readonly hide: 'Hide from Menu Bar';
43
+ };
44
+ readonly zh: {
45
+ readonly title: '{tool} {p}%';
46
+ readonly tooltipOk: '各工具额度充足(最高为 {tool} 的 {account},已用 {p}%),点击查看所有账号。';
47
+ readonly tooltipLow: '{tool} 额度即将用尽,连首选账号 {account} 也已用 {p}%,点击查看所有账号。';
48
+ readonly tooltipEmpty: 'sideby——暂无额度数据';
49
+ readonly hotkeyTip: '在任何地方按 {key} 即可打开此菜单';
50
+ readonly headlineOk: '额度充足 · 最高为 {account}({tool},已用 {p}%)';
51
+ readonly statusOk: '各工具额度充足';
52
+ readonly statusLow: '{tool} 额度吃紧';
53
+ readonly detailOk: '首选账号中最高:{account}(已用 {p}%)';
54
+ readonly detailLow: '首选 {account} 已用 {p}% · 可换工具或等重置';
55
+ readonly resetsHeader: '重置';
56
+ readonly headlineLow: '{tool} 额度告急 · 首选 {account} 已用 {p}%';
57
+ readonly recommended: '首选';
58
+ readonly api: 'API 密钥';
59
+ readonly signedOut: '未登录';
60
+ readonly noData: '暂无额度';
61
+ readonly window: '{label} {p}%';
62
+ readonly resets: '{time}后重置';
63
+ readonly resetsTip: '{label}额度将在 {time}后重置';
64
+ readonly moreApi: '{n} 个 API 密钥账号';
65
+ readonly moreSignedOut: '{n} 个未登录账号';
66
+ readonly moreMixed: '另外 {n} 个账号';
67
+ readonly sameLoginNote: '与 {name} 同一登录';
68
+ readonly sameLoginTip: '与 {names} 同一登录,共用这份额度';
69
+ readonly sep: ' · ';
70
+ readonly minutes: '{m}分';
71
+ readonly hoursMinutes: '{h}小时 {m}分';
72
+ readonly daysHours: '{d}天 {h}小时';
73
+ readonly startTip: '在终端里启动 {account}';
74
+ readonly empty: '还没有账号';
75
+ readonly loading: '正在读取额度…';
76
+ readonly addAccount: '添加账号…';
77
+ readonly updatedJustNow: '刚刚更新';
78
+ readonly updatedMinutes: '{n} 分钟前更新';
79
+ readonly refreshing: '正在刷新…';
80
+ readonly refreshFailed: '刷新失败 · 显示的是旧数据';
81
+ readonly refresh: '刷新';
82
+ readonly openPanel: '打开面板';
83
+ readonly hide: '从状态栏隐藏';
84
+ };
85
+ };
86
+ export type MenubarStrings = Record<keyof (typeof MENUBAR_STRINGS)['en'], string>;
87
+ export declare function fill(template: string, values: Record<string, string | number>): string;
@@ -0,0 +1,88 @@
1
+ // The words of the Menu Bar Item (spec §3.31), in the Panel's two languages. The menu bar script draws them as given.
2
+ export const MENUBAR_STRINGS = {
3
+ en: {
4
+ title: '{tool} {p}%',
5
+ tooltipOk: 'All tools have plenty of quota ({account} in {tool} is highest at {p}% used) — click to see every account.',
6
+ tooltipLow: '{tool} is running low: even its best account ({account}) has used {p}% of its quota — click to see every account.',
7
+ tooltipEmpty: 'sideby — no quota data yet',
8
+ hotkeyTip: 'Press {key} to open this menu from anywhere',
9
+ headlineOk: 'Plenty of quota · highest is {account} ({tool}, {p}%)',
10
+ statusOk: 'Plenty of quota',
11
+ statusLow: '{tool} is running low',
12
+ detailOk: 'Highest next account: {account} ({p}% used)',
13
+ detailLow: '{account} is at {p}% · switch tools or wait',
14
+ resetsHeader: 'Resets',
15
+ headlineLow: '{tool} is running low · {account} is at {p}%',
16
+ recommended: 'next',
17
+ api: 'API key',
18
+ signedOut: 'signed out',
19
+ noData: 'no quota yet',
20
+ window: '{label} {p}%',
21
+ resets: 'resets in {time}',
22
+ resetsTip: '{label} resets in {time}',
23
+ moreApi: 'API keys ({n})',
24
+ moreSignedOut: '{n} signed out',
25
+ moreMixed: '{n} more',
26
+ sameLoginNote: 'same login as {name}',
27
+ sameLoginTip: 'Same login as {names} — they share this quota',
28
+ sep: ' · ',
29
+ minutes: '{m}m',
30
+ hoursMinutes: '{h}h {m}m',
31
+ daysHours: '{d}d {h}h',
32
+ startTip: 'Start {account} in your terminal',
33
+ empty: 'No accounts yet',
34
+ loading: 'Loading quota…',
35
+ addAccount: 'Add an Account…',
36
+ updatedJustNow: 'Updated just now',
37
+ updatedMinutes: 'Updated {n} min ago',
38
+ refreshing: 'Refreshing…',
39
+ refreshFailed: 'Could not refresh · showing older numbers',
40
+ refresh: 'Refresh',
41
+ openPanel: 'Open Panel',
42
+ hide: 'Hide from Menu Bar',
43
+ },
44
+ zh: {
45
+ title: '{tool} {p}%',
46
+ tooltipOk: '各工具额度充足(最高为 {tool} 的 {account},已用 {p}%),点击查看所有账号。',
47
+ tooltipLow: '{tool} 额度即将用尽,连首选账号 {account} 也已用 {p}%,点击查看所有账号。',
48
+ tooltipEmpty: 'sideby——暂无额度数据',
49
+ hotkeyTip: '在任何地方按 {key} 即可打开此菜单',
50
+ headlineOk: '额度充足 · 最高为 {account}({tool},已用 {p}%)',
51
+ statusOk: '各工具额度充足',
52
+ statusLow: '{tool} 额度吃紧',
53
+ detailOk: '首选账号中最高:{account}(已用 {p}%)',
54
+ detailLow: '首选 {account} 已用 {p}% · 可换工具或等重置',
55
+ resetsHeader: '重置',
56
+ headlineLow: '{tool} 额度告急 · 首选 {account} 已用 {p}%',
57
+ recommended: '首选',
58
+ api: 'API 密钥',
59
+ signedOut: '未登录',
60
+ noData: '暂无额度',
61
+ window: '{label} {p}%',
62
+ resets: '{time}后重置',
63
+ resetsTip: '{label}额度将在 {time}后重置',
64
+ moreApi: '{n} 个 API 密钥账号',
65
+ moreSignedOut: '{n} 个未登录账号',
66
+ moreMixed: '另外 {n} 个账号',
67
+ sameLoginNote: '与 {name} 同一登录',
68
+ sameLoginTip: '与 {names} 同一登录,共用这份额度',
69
+ sep: ' · ',
70
+ minutes: '{m}分',
71
+ hoursMinutes: '{h}小时 {m}分',
72
+ daysHours: '{d}天 {h}小时',
73
+ startTip: '在终端里启动 {account}',
74
+ empty: '还没有账号',
75
+ loading: '正在读取额度…',
76
+ addAccount: '添加账号…',
77
+ updatedJustNow: '刚刚更新',
78
+ updatedMinutes: '{n} 分钟前更新',
79
+ refreshing: '正在刷新…',
80
+ refreshFailed: '刷新失败 · 显示的是旧数据',
81
+ refresh: '刷新',
82
+ openPanel: '打开面板',
83
+ hide: '从状态栏隐藏',
84
+ },
85
+ };
86
+ export function fill(template, values) {
87
+ return template.replace(/\{(\w+)\}/g, (m, key) => (key in values ? String(values[key]) : m));
88
+ }
@@ -5,12 +5,14 @@ import { execFile, spawn } from 'node:child_process';
5
5
  import { randomBytes } from 'node:crypto';
6
6
  import { mkdir, open, readFile, rename, rm, stat, writeFile } from 'node:fs/promises';
7
7
  import { join } from 'node:path';
8
+ import { takeFileLock } from '../core/lock.js';
8
9
  import { loginShellPath } from '../core/login-path.js';
9
10
  import { buildId } from '../core/version.js';
10
11
  import { DEFAULT_PANEL_PORT, isSidebyPanel, panelIdentity, panelOwner, startPanelServer, } from './server.js';
11
12
  export const DETACHED_FLAG = 'serve-detached';
12
13
  const START_TIMEOUT_MS = 20_000;
13
14
  const LOCK_STALE_MS = 30_000;
15
+ const LOCK_BEAT_MS = 5000;
14
16
  const STOP_TIMEOUT_MS = 5000;
15
17
  const LOG_MAX_BYTES = 1_000_000;
16
18
  export const pidFilePath = (p) => join(p.stateDir, 'panel.json');
@@ -100,32 +102,23 @@ async function stopOutdated(p, build) {
100
102
  return false;
101
103
  return (await stopBackgroundPanel(p)).status === 'stopped';
102
104
  }
103
- /** Takes the start lock, waiting while another launch holds it; a lock older than 30 s is taken over. */
105
+ /**
106
+ * Takes the start lock, waiting while another launch holds it. Its holder touches it every 5 s; a lock whose pid is gone
107
+ * or that nobody touched for 30 s is taken over, and after waiting START_TIMEOUT_MS this launch takes it regardless.
108
+ */
104
109
  async function withStartLock(p, fn) {
105
110
  const lock = lockPath(p);
106
- const deadline = Date.now() + START_TIMEOUT_MS;
107
- for (;;) {
108
- try {
109
- const fh = await open(lock, 'wx', 0o600);
110
- await fh.writeFile(String(process.pid));
111
- await fh.close();
112
- break;
113
- }
114
- catch (err) {
115
- if (err.code !== 'EEXIST')
116
- throw err;
117
- const age = await stat(lock).then((s) => Date.now() - s.mtimeMs, () => 0);
118
- if (age > LOCK_STALE_MS || Date.now() > deadline)
119
- await rm(lock, { force: true });
120
- else
121
- await sleep(200);
122
- }
111
+ const opts = { staleMs: LOCK_STALE_MS, beatMs: LOCK_BEAT_MS };
112
+ let release = await takeFileLock(lock, { ...opts, wait: { timeoutMs: START_TIMEOUT_MS, pollMs: 200 } });
113
+ while (!release) {
114
+ await rm(lock, { force: true });
115
+ release = await takeFileLock(lock, opts);
123
116
  }
124
117
  try {
125
118
  return await fn();
126
119
  }
127
120
  finally {
128
- await rm(lock, { force: true });
121
+ await release();
129
122
  }
130
123
  }
131
124
  async function openLog(p) {
@@ -40,6 +40,16 @@ export interface PanelHandlerOptions {
40
40
  * Panel then refuses `/api/update*` with 403.
41
41
  */
42
42
  updates?: boolean;
43
+ /**
44
+ * How the Panel's AI Tasks start (spec §3.28, ADR-0030). `{ bridge: 'postMessage' }` is for a Portal that offers
45
+ * AI Tasks on every page itself: inside a frame, the Panel hides its header "Let AI do it" button and every AI Task entry posts
46
+ * `{ type: 'local-tools:ask', version: 1, preset, native: { task, text, context }, text }` to `window.parent`, with
47
+ * the Panel's own origin as the target origin, instead of opening its dialog. Without it the Panel opens its own
48
+ * dialog. Checked when the handler is created: anything else throws a TypeError.
49
+ */
50
+ ask?: {
51
+ bridge: 'postMessage';
52
+ };
43
53
  }
44
54
  export type PanelHandler = ((req: IncomingMessage, res: ServerResponse) => Promise<boolean>) & {
45
55
  /** Per-instance secret the page sends as `x-sideby-token`. */
@@ -78,6 +88,8 @@ export type PanelFamily = FamilyInfo & {
78
88
  keyAlways?: boolean;
79
89
  /** What "Try it" runs (`sideby try`): the Host and the probe arguments; absent when it cannot be tried. */
80
90
  tryCommand?: string[];
91
+ /** AI Tasks start this Host with its permission checks on (`aiTaskArgs`, ADR-0031), so the AI dialog says so. */
92
+ aiTaskSafe?: true;
81
93
  };
82
94
  /** What the Panel shows about updates (spec §3.23); absent when the handler was created with `updates: false`. */
83
95
  export interface PanelUpdate {