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
@@ -0,0 +1,261 @@
1
+ // Text helpers for Session History readers (spec §3.33): reading a record file line by line, and keeping only what a
2
+ // person typed or a model replied. Injected context (system reminders, environment blocks, instruction files) is cut
3
+ // out, so it never reaches the History Index (ADR-0042).
4
+ import { createReadStream } from 'node:fs';
5
+ import { readdir, stat } from 'node:fs/promises';
6
+ /** The longest record line read; a longer one (a huge tool output, never a typed prompt) is skipped unread. */
7
+ export const MAX_LINE_BYTES = 16 * 1024 * 1024;
8
+ /**
9
+ * Every non-empty line of a file, streamed: a huge file never sits in memory at once, a line over `MAX_LINE_BYTES`
10
+ * is dropped as it streams by, and stopping early closes the file.
11
+ */
12
+ export async function* fileLines(path) {
13
+ const input = createReadStream(path);
14
+ let parts = [];
15
+ let size = 0;
16
+ let skipping = false;
17
+ const take = () => {
18
+ const line = Buffer.concat(parts).toString('utf8');
19
+ parts = [];
20
+ size = 0;
21
+ return line.endsWith('\r') ? line.slice(0, -1) : line;
22
+ };
23
+ try {
24
+ for await (const chunk of input) {
25
+ let from = 0;
26
+ for (let nl = chunk.indexOf(10); nl !== -1; nl = chunk.indexOf(10, from)) {
27
+ if (skipping)
28
+ skipping = false;
29
+ else if (size + nl - from > MAX_LINE_BYTES) {
30
+ parts = [];
31
+ size = 0;
32
+ }
33
+ else {
34
+ parts.push(chunk.subarray(from, nl));
35
+ const line = take();
36
+ if (line)
37
+ yield line;
38
+ }
39
+ from = nl + 1;
40
+ }
41
+ if (from < chunk.length && !skipping) {
42
+ parts.push(chunk.subarray(from));
43
+ size += chunk.length - from;
44
+ if (size > MAX_LINE_BYTES) {
45
+ skipping = true;
46
+ parts = [];
47
+ size = 0;
48
+ }
49
+ }
50
+ }
51
+ if (!skipping && parts.length) {
52
+ const line = take();
53
+ if (line)
54
+ yield line;
55
+ }
56
+ }
57
+ finally {
58
+ input.destroy();
59
+ }
60
+ }
61
+ /** A JSON object from one record line, or undefined for a line that is cut, still being written or not an object. */
62
+ export function parseRecord(line) {
63
+ try {
64
+ const v = JSON.parse(line);
65
+ return v && typeof v === 'object' && !Array.isArray(v) ? v : undefined;
66
+ }
67
+ catch {
68
+ return undefined;
69
+ }
70
+ }
71
+ export const str = (v) => (typeof v === 'string' && v ? v : undefined);
72
+ /** The `text` of every `{ type: <kind>, text }` block of a message's content; a string content is its own text. */
73
+ export function blockText(content, kinds = ['text']) {
74
+ if (typeof content === 'string')
75
+ return content;
76
+ if (!Array.isArray(content))
77
+ return '';
78
+ const out = [];
79
+ for (const b of content) {
80
+ if (b && typeof b === 'object' && kinds.includes(b.type)) {
81
+ const t = b.text;
82
+ if (typeof t === 'string')
83
+ out.push(t);
84
+ }
85
+ }
86
+ return out.join('\n');
87
+ }
88
+ // Blocks a Host or a tool puts into a "user" turn that the person did not type.
89
+ const INJECTED_TAG = /<\/?(system-reminder|environment_context|user_instructions|permissions[\w-]*|local-command-stdout|local-command-stderr|local-command-caveat|command-message|task-notification|ide_selection|ide_opened_file|turn_aborted|skill)\b[^>]*>/g;
90
+ /**
91
+ * `text` without its injected blocks. Blocks may nest (a reminder quoting a reminder), so each one ends at the close
92
+ * tag that balances its own open tag; a block left open runs to the end of the text, which is cut too.
93
+ */
94
+ export function stripInjected(text) {
95
+ let out = '';
96
+ let at = 0;
97
+ let open;
98
+ for (const m of text.matchAll(INJECTED_TAG)) {
99
+ const closing = m[0].startsWith('</');
100
+ const name = m[1];
101
+ if (!open) {
102
+ if (closing)
103
+ continue;
104
+ out += text.slice(at, m.index);
105
+ open = { name, depth: 1 };
106
+ }
107
+ else if (name === open.name) {
108
+ open.depth += closing ? -1 : 1;
109
+ if (!open.depth) {
110
+ open = undefined;
111
+ at = m.index + m[0].length;
112
+ }
113
+ }
114
+ }
115
+ return open ? out : out + text.slice(at);
116
+ }
117
+ // A whole turn that is injected when it starts with one of these.
118
+ const INJECTED_START = /^\s*(#\s*AGENTS\.md instructions|<INSTRUCTIONS>|Caveat: The messages below|\[Request interrupted)/;
119
+ /**
120
+ * What a person typed in a "user" turn: injected blocks cut out, a slash command shown as `/name args`, a shell
121
+ * escape as `!cmd`. Empty when nothing typed is left.
122
+ */
123
+ export function typedText(text) {
124
+ if (INJECTED_START.test(text))
125
+ return '';
126
+ // Injected blocks go first: a reminder may quote a command or a shell escape the person never typed.
127
+ const typed = stripInjected(text);
128
+ const name = /<command-name>([\s\S]*?)<\/command-name>/.exec(typed)?.[1]?.trim();
129
+ if (name) {
130
+ const args = /<command-args>([\s\S]*?)<\/command-args>/.exec(typed)?.[1]?.trim();
131
+ return [name.startsWith('/') ? name : `/${name}`, args].filter(Boolean).join(' ');
132
+ }
133
+ const bash = /<bash-input>([\s\S]*?)<\/bash-input>/.exec(typed)?.[1]?.trim();
134
+ if (bash)
135
+ return `!${bash}`;
136
+ // Codex wraps a pasted image as <image name=[Image #1] path="..."> ... </image>; the tags are not typed.
137
+ return typed
138
+ .replace(/<bash-std(out|err)>[\s\S]*?<\/bash-std\1>/g, '')
139
+ .replace(/<\/?image\b[^>]*>/g, '')
140
+ .trim();
141
+ }
142
+ /** The most text of one Session that is kept for search, in characters; the rest is counted but not kept. */
143
+ export const SESSION_TEXT_MAX = 4 * 1024 * 1024;
144
+ /** The first `max` UTF-16 units of `t`, one fewer when the cut would leave half of a surrogate pair. */
145
+ export function cutText(t, max) {
146
+ if (t.length <= max)
147
+ return t;
148
+ const end = max > 0 && /[\uD800-\uDBFF]/.test(t[max - 1]) ? max - 1 : max;
149
+ return t.slice(0, end);
150
+ }
151
+ /** Collects a Session's prompts and replies while a reader parses it, keeping at most `SESSION_TEXT_MAX` of text. */
152
+ export class SessionText {
153
+ prompts = [];
154
+ replies = [];
155
+ /** Prompts plus replies, kept or not. */
156
+ count = 0;
157
+ /** The first prompt, kept whole for the title. */
158
+ first;
159
+ /** True once some text was left out for the limit. */
160
+ cut = false;
161
+ #left;
162
+ constructor(max = SESSION_TEXT_MAX) {
163
+ this.#left = max;
164
+ }
165
+ prompt(t) {
166
+ this.first ??= t;
167
+ this.#keep(this.prompts, t);
168
+ }
169
+ reply(t) {
170
+ this.#keep(this.replies, t);
171
+ }
172
+ #keep(list, t) {
173
+ this.count++;
174
+ if (t.length > this.#left)
175
+ this.cut = true;
176
+ if (this.#left <= 0)
177
+ return;
178
+ const kept = cutText(t, this.#left);
179
+ this.#left -= kept.length;
180
+ list.push(kept);
181
+ }
182
+ }
183
+ /**
184
+ * Every string, number and boolean inside a JSON value, one per line: the text of a tool's input or output, without
185
+ * the `type` names of content blocks.
186
+ */
187
+ export function jsonText(v) {
188
+ const out = [];
189
+ const walk = (x, depth) => {
190
+ if (typeof x === 'string')
191
+ out.push(x);
192
+ else if (typeof x === 'number' || typeof x === 'boolean')
193
+ out.push(String(x));
194
+ else if (Array.isArray(x)) {
195
+ if (depth < 32)
196
+ for (const y of x)
197
+ walk(y, depth + 1);
198
+ }
199
+ else if (x && typeof x === 'object' && depth < 32)
200
+ // A content block's `type` (`{ type: 'text', text }`) names the block, it is not text of the tool.
201
+ for (const [k, y] of Object.entries(x))
202
+ if (!(k === 'type' && 'text' in x))
203
+ walk(y, depth + 1);
204
+ };
205
+ walk(v, 0);
206
+ return out.join('\n');
207
+ }
208
+ /** The longest Session Title sideby shows; the Host's own titles are cut the same way. */
209
+ export const TITLE_MAX = 120;
210
+ /** A one-line title from a prompt: its first non-empty line, spaces collapsed, cut at `TITLE_MAX` characters. */
211
+ export function titleFromText(text) {
212
+ const line = text.split('\n').find((l) => l.trim()) ?? '';
213
+ const one = line.replace(/\s+/g, ' ').trim();
214
+ const chars = [...one];
215
+ return chars.length > TITLE_MAX ? `${chars.slice(0, TITLE_MAX - 1).join('')}…` : one;
216
+ }
217
+ /** ISO time of a value a Host wrote as an ISO string or epoch milliseconds/seconds; undefined when it is neither. */
218
+ export function isoTime(v) {
219
+ if (typeof v === 'number' && Number.isFinite(v))
220
+ return new Date(v < 1e12 ? v * 1000 : v).toISOString();
221
+ if (typeof v === 'string' && v) {
222
+ const t = Date.parse(v);
223
+ return Number.isNaN(t) ? undefined : new Date(t).toISOString();
224
+ }
225
+ return undefined;
226
+ }
227
+ /** A record file as a `HistoryFile`, signed by size, mtime and inode; undefined when it vanished meanwhile. */
228
+ export async function historyFile(path, extra = {}) {
229
+ const sigOf = async (p) => {
230
+ try {
231
+ const st = await stat(p);
232
+ return st.isFile() ? { sig: `${st.size}:${st.mtimeMs}:${st.ino}`, mtimeMs: st.mtimeMs } : undefined;
233
+ }
234
+ catch (err) {
235
+ if (err.code === 'ENOENT')
236
+ return undefined;
237
+ throw err;
238
+ }
239
+ };
240
+ const main = await sigOf(path);
241
+ if (!main)
242
+ return undefined;
243
+ const also = extra.also ? await sigOf(extra.also) : undefined;
244
+ return {
245
+ path,
246
+ sig: also ? `${main.sig}|${also.sig}` : main.sig,
247
+ mtimeMs: Math.max(main.mtimeMs, also?.mtimeMs ?? 0),
248
+ ...(extra.parent ? { parent: extra.parent } : {}),
249
+ };
250
+ }
251
+ /** The entries of a folder; empty when it does not exist, other errors thrown. */
252
+ export async function dirEntries(dir) {
253
+ try {
254
+ return await readdir(dir, { withFileTypes: true });
255
+ }
256
+ catch (err) {
257
+ if (err.code === 'ENOENT' || err.code === 'ENOTDIR')
258
+ return [];
259
+ throw err;
260
+ }
261
+ }
@@ -0,0 +1,86 @@
1
+ import { type AutostartContext } from '../core/autostart.ts';
2
+ import { type MenubarHotkey } from './snapshot.ts';
3
+ export type HotkeyProblemCode = 'hotkey.invalid' | 'hotkey.needs-modifier' | 'hotkey.reserved';
4
+ export type HotkeyCheck = {
5
+ ok: true;
6
+ keys: string;
7
+ hotkey: MenubarHotkey | null;
8
+ } | {
9
+ ok: false;
10
+ code: HotkeyProblemCode;
11
+ message: string;
12
+ };
13
+ /**
14
+ * A value for config `menubar.hotkey`, as typed by a user or sent by the Panel: `off`, `default`, or modifiers and one
15
+ * letter or digit in any order and case (`Cmd+Opt+K`). Returns it in the written order (`opt+cmd+k`), or why not.
16
+ */
17
+ export declare function checkHotkey(value: string): HotkeyCheck;
18
+ /** What the menu bar script last said about registering the shortcut; kept so the Panel and CLI can say it. */
19
+ export interface HotkeyState {
20
+ /** The shortcut it tried, as macOS writes it (`⌥⌘S`). */
21
+ label: string;
22
+ /** False: macOS did not take it (another app or macOS holds it). Another app's same keys usually go unnoticed. */
23
+ registered: boolean;
24
+ at: string;
25
+ }
26
+ /** What the menu bar script last said about its icon: whether macOS shows it in the menu bar right now. */
27
+ export interface IconState {
28
+ /** False: the menu bar has room for it no longer (it sits behind «), or another app covers it. */
29
+ visible: boolean;
30
+ at: string;
31
+ }
32
+ /** Everything the menu bar script reports, kept by Node in `<state>/menubar.json`; the script itself writes no file. */
33
+ export interface MenubarState {
34
+ hotkey?: HotkeyState;
35
+ icon?: IconState;
36
+ }
37
+ export declare function readMenubarState(stateDir: string): Promise<MenubarState>;
38
+ /** Merges `patch` into the kept state. */
39
+ export declare function writeMenubarState(stateDir: string, patch: MenubarState): Promise<void>;
40
+ export declare function readHotkeyState(stateDir: string): Promise<HotkeyState | null>;
41
+ export declare function writeHotkeyState(stateDir: string, state: HotkeyState): Promise<void>;
42
+ /** The icon as the Panel and `sideby menubar --json` show it; null when the script has not said (or said long ago). */
43
+ export declare function iconView(state: IconState | undefined, now: Date): {
44
+ visible: boolean | null;
45
+ };
46
+ /**
47
+ * Tells a running menu bar item to take a changed setting now (such as its shortcut), through a macOS distributed
48
+ * notification: it reads its snapshot again within a second or two, with no restart. Posted by the system's
49
+ * `osascript`, only for the login user's own session (never from a test or a demo HOME). True when it was posted.
50
+ */
51
+ export declare function nudgeMenubar(ctx: AutostartContext): Promise<boolean>;
52
+ /** The shortcut as `sideby menubar hotkey --json` and the Panel show it. */
53
+ export interface HotkeyView {
54
+ /** As config holds it, or the default: `opt+x`, `off`. */
55
+ keys: string;
56
+ /** As macOS writes it (`⌥⌘S`); empty when off. */
57
+ label: string;
58
+ enabled: boolean;
59
+ isDefault: boolean;
60
+ /** Whether the menu bar item could register it; null when it has not said yet (or it is off). */
61
+ registered: boolean | null;
62
+ /** A value config allows but a new choice would not (such as shift alone); it still works. */
63
+ problem?: {
64
+ code: HotkeyProblemCode;
65
+ message: string;
66
+ };
67
+ }
68
+ export declare function hotkeyView(configured: string | undefined, state: HotkeyState | null): HotkeyView;
69
+ /** System Settings › Menu Bar, where macOS 26 and later let each app's menu bar item show (or not). */
70
+ export declare const MENU_BAR_SETTINGS_URL = "x-apple.systempreferences:com.apple.ControlCenter-Settings.extension";
71
+ /** Opens System Settings at the Menu Bar page with `/usr/bin/open`; true when it opened. */
72
+ export declare function openMenuBarSettings(ctx: AutostartContext): Promise<boolean>;
73
+ /**
74
+ * `sideby menubar hotkey <keys|off|default>` and the Panel's recorder: checks the value, writes config
75
+ * `menubar.hotkey` (removes it for the default), and tells a running menu bar item, which takes the new key within seconds.
76
+ */
77
+ export declare function setMenubarHotkey(opts: {
78
+ configFile: string;
79
+ stateDir: string;
80
+ value: string;
81
+ ctx: AutostartContext;
82
+ }): Promise<{
83
+ changed: boolean;
84
+ nudged: boolean;
85
+ hotkey: HotkeyView;
86
+ }>;
@@ -0,0 +1,159 @@
1
+ // The Menu Bar Item's shortcut (spec §3.31, ADR-0034): which combinations a user may choose, and whether the menu bar
2
+ // script could register the one chosen. The script reports that through `menubar snapshot --hotkey ok|taken`, and
3
+ // only Node writes it down, to `<state>/menubar.json`.
4
+ import { mkdir, readFile } from 'node:fs/promises';
5
+ import { join } from 'node:path';
6
+ import { defaultExec, launchctlNote } from '../core/autostart.js';
7
+ import { editConfigPath, loadConfig } from '../core/config.js';
8
+ import { UserError } from '../core/errors.js';
9
+ import { writeFileAtomic } from '../core/fs-safe.js';
10
+ import { MENUBAR_RELOAD } from './script.js';
11
+ import { DEFAULT_MENUBAR_HOTKEY, menubarHotkey } from './snapshot.js';
12
+ /**
13
+ * Combinations macOS or nearly every app already uses, in the order `checkHotkey` writes them; a menu shortcut there
14
+ * would take them away. A list, because macOS tells no one: `RegisterEventHotKey` accepts even ⌘⇧3 from a second
15
+ * process (macOS 27, 2026-10-20), and its own shortcuts are in `com.apple.symbolichotkeys` only once changed.
16
+ */
17
+ const RESERVED = {
18
+ 'cmd+q': 'quits the app in front',
19
+ 'cmd+w': 'closes the window in front',
20
+ 'cmd+h': 'hides the app in front',
21
+ 'cmd+m': 'minimizes the window in front',
22
+ 'cmd+c': 'copies',
23
+ 'cmd+v': 'pastes',
24
+ 'cmd+x': 'cuts',
25
+ 'cmd+z': 'undoes',
26
+ 'cmd+a': 'selects all',
27
+ 'cmd+s': 'saves',
28
+ 'shift+cmd+3': 'takes a screenshot',
29
+ 'shift+cmd+4': 'takes a screenshot',
30
+ 'shift+cmd+5': 'opens the screenshot tool',
31
+ 'opt+cmd+d': 'shows and hides the Dock',
32
+ 'opt+cmd+h': 'hides the other apps',
33
+ 'ctrl+cmd+q': 'locks the screen',
34
+ 'ctrl+cmd+f': 'turns full screen on and off',
35
+ };
36
+ const ORDER = ['ctrl', 'opt', 'shift', 'cmd'];
37
+ /**
38
+ * A value for config `menubar.hotkey`, as typed by a user or sent by the Panel: `off`, `default`, or modifiers and one
39
+ * letter or digit in any order and case (`Cmd+Opt+K`). Returns it in the written order (`opt+cmd+k`), or why not.
40
+ */
41
+ export function checkHotkey(value) {
42
+ const v = value.trim().toLowerCase().replace(/\s+/g, '');
43
+ if (v === 'off')
44
+ return { ok: true, keys: 'off', hotkey: null };
45
+ if (v === 'default' || v === DEFAULT_MENUBAR_HOTKEY)
46
+ return { ok: true, keys: DEFAULT_MENUBAR_HOTKEY, hotkey: menubarHotkey(DEFAULT_MENUBAR_HOTKEY) };
47
+ const parts = v.split('+').map((p) => (p === 'option' || p === 'alt' ? 'opt' : p === 'command' ? 'cmd' : p));
48
+ const key = parts.pop() ?? '';
49
+ const mods = ORDER.filter((m) => parts.includes(m));
50
+ const keys = [...mods, key].join('+');
51
+ const hotkey = parts.length && mods.length === parts.length ? menubarHotkey(keys) : null;
52
+ if (!hotkey)
53
+ return {
54
+ ok: false,
55
+ code: 'hotkey.invalid',
56
+ message: `"${value}" is not a shortcut sideby can use; write modifiers and one letter or digit, such as opt+x, or off`,
57
+ };
58
+ // ⌥ alone is allowed: it only types a rare symbol (⌥X types ≈), and two keys are what one hand presses easily.
59
+ if (!mods.includes('cmd') && !mods.includes('ctrl') && !mods.includes('opt'))
60
+ return {
61
+ ok: false,
62
+ code: 'hotkey.needs-modifier',
63
+ message: `${hotkey.label} would fire while you type; add opt, cmd or ctrl, such as opt+${key}`,
64
+ };
65
+ const reserved = RESERVED[keys];
66
+ if (reserved)
67
+ return {
68
+ ok: false,
69
+ code: 'hotkey.reserved',
70
+ message: `${hotkey.label} ${reserved} in macOS; pick another, such as ctrl+opt+${key}`,
71
+ };
72
+ return { ok: true, keys, hotkey };
73
+ }
74
+ const stateFile = (stateDir) => join(stateDir, 'menubar.json');
75
+ export async function readMenubarState(stateDir) {
76
+ try {
77
+ const v = JSON.parse(await readFile(stateFile(stateDir), 'utf8'));
78
+ const h = v.hotkey;
79
+ const i = v.icon;
80
+ return {
81
+ ...(h && typeof h.label === 'string' && typeof h.registered === 'boolean' ? { hotkey: h } : {}),
82
+ ...(i && typeof i.visible === 'boolean' && typeof i.at === 'string' ? { icon: i } : {}),
83
+ };
84
+ }
85
+ catch {
86
+ return {};
87
+ }
88
+ }
89
+ /** Merges `patch` into the kept state. */
90
+ export async function writeMenubarState(stateDir, patch) {
91
+ const next = { ...(await readMenubarState(stateDir)), ...patch };
92
+ await mkdir(stateDir, { recursive: true, mode: 0o700 });
93
+ await writeFileAtomic(stateFile(stateDir), `${JSON.stringify(next, null, 2)}\n`);
94
+ }
95
+ export async function readHotkeyState(stateDir) {
96
+ return (await readMenubarState(stateDir)).hotkey ?? null;
97
+ }
98
+ export function writeHotkeyState(stateDir, state) {
99
+ return writeMenubarState(stateDir, { hotkey: state });
100
+ }
101
+ /** The icon as the Panel and `sideby menubar --json` show it; null when the script has not said (or said long ago). */
102
+ export function iconView(state, now) {
103
+ // A report older than a few refreshes may no longer hold (the item was turned off, the Mac slept).
104
+ const fresh = state && now.getTime() - Date.parse(state.at) < 15 * 60_000;
105
+ return { visible: fresh ? state.visible : null };
106
+ }
107
+ /**
108
+ * Tells a running menu bar item to take a changed setting now (such as its shortcut), through a macOS distributed
109
+ * notification: it reads its snapshot again within a second or two, with no restart. Posted by the system's
110
+ * `osascript`, only for the login user's own session (never from a test or a demo HOME). True when it was posted.
111
+ */
112
+ export async function nudgeMenubar(ctx) {
113
+ if ((ctx.platform ?? process.platform) !== 'darwin' || launchctlNote(ctx))
114
+ return false;
115
+ const post = `ObjC.import('Foundation'); $.NSDistributedNotificationCenter.defaultCenter.postNotificationNameObjectUserInfoDeliverImmediately($('${MENUBAR_RELOAD}'), $(), $(), true); ''`;
116
+ const r = await (ctx.exec ?? defaultExec)('/usr/bin/osascript', ['-l', 'JavaScript', '-e', post]);
117
+ return r.code === 0;
118
+ }
119
+ export function hotkeyView(configured, state) {
120
+ const keys = configured ?? DEFAULT_MENUBAR_HOTKEY;
121
+ const hotkey = menubarHotkey(keys);
122
+ const label = hotkey?.label ?? '';
123
+ const check = keys === 'off' ? null : checkHotkey(keys);
124
+ return {
125
+ keys,
126
+ label,
127
+ enabled: hotkey !== null,
128
+ isDefault: keys === DEFAULT_MENUBAR_HOTKEY,
129
+ registered: hotkey && state && state.label === label ? state.registered : null,
130
+ ...(check && !check.ok ? { problem: { code: check.code, message: check.message } } : {}),
131
+ };
132
+ }
133
+ /** System Settings › Menu Bar, where macOS 26 and later let each app's menu bar item show (or not). */
134
+ export const MENU_BAR_SETTINGS_URL = 'x-apple.systempreferences:com.apple.ControlCenter-Settings.extension';
135
+ /** Opens System Settings at the Menu Bar page with `/usr/bin/open`; true when it opened. */
136
+ export async function openMenuBarSettings(ctx) {
137
+ if ((ctx.platform ?? process.platform) !== 'darwin')
138
+ return false;
139
+ const r = await (ctx.exec ?? defaultExec)('/usr/bin/open', [MENU_BAR_SETTINGS_URL]);
140
+ return r.code === 0;
141
+ }
142
+ /**
143
+ * `sideby menubar hotkey <keys|off|default>` and the Panel's recorder: checks the value, writes config
144
+ * `menubar.hotkey` (removes it for the default), and tells a running menu bar item, which takes the new key within seconds.
145
+ */
146
+ export async function setMenubarHotkey(opts) {
147
+ const check = checkHotkey(opts.value);
148
+ if (!check.ok)
149
+ throw new UserError(check.message, { code: check.code });
150
+ const next = check.keys === DEFAULT_MENUBAR_HOTKEY ? undefined : check.keys;
151
+ const r = await editConfigPath(opts.configFile, 'menubar.hotkey', next);
152
+ const nudged = r.changed ? await nudgeMenubar(opts.ctx) : false;
153
+ const config = await loadConfig(opts.configFile);
154
+ return {
155
+ changed: r.changed,
156
+ nudged,
157
+ hotkey: hotkeyView(config.menubar?.hotkey, await readHotkeyState(opts.stateDir)),
158
+ };
159
+ }
@@ -5,4 +5,6 @@ export declare const TICK_SECONDS = 5;
5
5
  export declare const STALE_SECONDS: number;
6
6
  /** A snapshot still running after this is ended, so a stuck one cannot stop the refreshes. */
7
7
  export declare const SNAPSHOT_TIMEOUT_SECONDS = 60;
8
+ /** The macOS distributed notification that makes a running item read its snapshot again (`nudgeMenubar`). */
9
+ export declare const MENUBAR_RELOAD = "dev.sideby.menubar.reload";
8
10
  export declare const MENUBAR_SCRIPT: string;