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.
- package/README.md +3 -1
- package/ai-tasks.json +90 -0
- package/dist/build.json +1 -1
- package/dist/src/cli/commands.d.ts +9 -1
- package/dist/src/cli/commands.js +349 -14
- package/dist/src/cli/json-schemas.js +157 -18
- package/dist/src/cli/main.js +56 -7
- package/dist/src/core/after-install.d.ts +54 -0
- package/dist/src/core/after-install.js +78 -0
- package/dist/src/core/ask.d.ts +73 -1
- package/dist/src/core/ask.js +106 -6
- package/dist/src/core/autostart.d.ts +38 -8
- package/dist/src/core/autostart.js +85 -39
- package/dist/src/core/brand-mark.d.ts +49 -11
- package/dist/src/core/brand-mark.js +116 -38
- package/dist/src/core/config.d.ts +5 -0
- package/dist/src/core/config.js +11 -1
- package/dist/src/core/desktop-app.d.ts +1 -1
- package/dist/src/core/desktop-app.js +2 -2
- package/dist/src/core/edit-settings.d.ts +24 -0
- package/dist/src/core/edit-settings.js +97 -0
- package/dist/src/core/handoff-brief.d.ts +4 -2
- package/dist/src/core/handoff-brief.js +6 -1
- package/dist/src/core/handoff-chain.d.ts +24 -0
- package/dist/src/core/handoff-chain.js +2 -0
- package/dist/src/core/handoff-run.d.ts +4 -3
- package/dist/src/core/handoff-run.js +14 -9
- package/dist/src/core/launch.d.ts +2 -0
- package/dist/src/core/launch.js +14 -10
- package/dist/src/core/local-build.d.ts +12 -5
- package/dist/src/core/local-build.js +49 -31
- package/dist/src/core/lock.d.ts +30 -0
- package/dist/src/core/lock.js +143 -0
- package/dist/src/core/quota-levels.d.ts +6 -1
- package/dist/src/core/quota-levels.js +16 -5
- package/dist/src/core/task-spec.d.ts +95 -0
- package/dist/src/core/task-spec.js +209 -0
- package/dist/src/core/terminal.js +6 -1
- package/dist/src/core/update.d.ts +27 -16
- package/dist/src/core/update.js +67 -93
- package/dist/src/desktop/icon.js +12 -15
- package/dist/src/families/claude/ai-task.d.ts +8 -0
- package/dist/src/families/claude/ai-task.js +38 -0
- package/dist/src/families/claude/handoff-hook.js +4 -4
- package/dist/src/families/claude/index.js +3 -0
- package/dist/src/families/codex/handoff-hook.js +4 -4
- package/dist/src/families/codex/index.js +9 -0
- package/dist/src/families/grok/handoff-hook.js +4 -4
- package/dist/src/families/grok/index.js +1 -0
- package/dist/src/families/pi/index.js +1 -0
- package/dist/src/handoff/hook.js +51 -30
- package/dist/src/handoff/levels.d.ts +24 -0
- package/dist/src/handoff/levels.js +70 -0
- package/dist/src/handoff/messages.d.ts +6 -3
- package/dist/src/handoff/messages.js +20 -7
- package/dist/src/handoff/reader.d.ts +6 -3
- package/dist/src/menubar/agent.d.ts +3 -0
- package/dist/src/menubar/agent.js +21 -0
- package/dist/src/menubar/script.d.ts +8 -0
- package/dist/src/menubar/script.js +457 -0
- package/dist/src/menubar/snapshot.d.ts +115 -0
- package/dist/src/menubar/snapshot.js +255 -0
- package/dist/src/menubar/strings.d.ts +87 -0
- package/dist/src/menubar/strings.js +88 -0
- package/dist/src/panel/background.js +12 -19
- package/dist/src/panel/handler.d.ts +12 -0
- package/dist/src/panel/handler.js +80 -80
- package/dist/src/panel/i18n.d.ts +33 -0
- package/dist/src/panel/i18n.js +66 -0
- package/dist/src/panel/page-logic.d.ts +105 -0
- package/dist/src/panel/page-logic.js +170 -15
- package/dist/src/panel/page-script.js +238 -109
- package/dist/src/panel/page-styles.js +42 -13
- package/dist/src/panel/page.d.ts +7 -0
- package/dist/src/panel/page.js +5 -0
- package/dist/src/runtime.d.ts +9 -1
- package/dist/src/runtime.js +11 -3
- package/dist/src/types.d.ts +14 -0
- package/package.json +4 -3
- package/schemas/ask-choices.json +168 -0
- package/schemas/ask-print.json +87 -0
- package/schemas/config.json +17 -1
- package/schemas/edit.json +119 -0
- package/schemas/error.json +4 -0
- package/schemas/families.json +4 -0
- package/schemas/list.json +4 -0
- package/schemas/local.json +1 -1
- package/schemas/menubar-snapshot.json +646 -0
- package/schemas/menubar-start.json +112 -0
- package/schemas/menubar.json +165 -0
- package/skills/sideby/SKILL.md +6 -2
- package/skills/sideby/references/ai-tasks.md +10 -0
- package/skills/sideby/references/handoff.md +3 -1
- package/skills/sideby/references/panel.md +6 -0
- package/skills/sideby/references/update.md +1 -1
package/dist/src/core/ask.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { randomBytes } from 'node:crypto';
|
|
2
2
|
import { constants } from 'node:fs';
|
|
3
|
-
import { lstat, mkdtemp, open, readdir, rm, unlink } from 'node:fs/promises';
|
|
4
|
-
import { dirname, isAbsolute, join } from 'node:path';
|
|
3
|
+
import { lstat, mkdtemp, open, readdir, realpath, rm, stat, unlink } from 'node:fs/promises';
|
|
4
|
+
import { dirname, isAbsolute, join, sep } from 'node:path';
|
|
5
5
|
import { AI_TASK_KINDS, stripSecrets, taskPrompt } from '../panel/page-logic.js';
|
|
6
6
|
import { checkAttachmentFiles, copyAttachments, MAX_ATTACHMENTS, privateDir, removeOldAttachments, stagedAttachments, takeStaged, } from './attachments.js';
|
|
7
7
|
import { editConfigAsk, editConfigTerminal } from './config.js';
|
|
@@ -40,6 +40,31 @@ export async function pickTaskAccount(rt, opts = {}) {
|
|
|
40
40
|
const account = all.find((a) => a.ref === plan.pick);
|
|
41
41
|
return account ? { account } : { reason: 'all-full' };
|
|
42
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Why `cwd` cannot be the folder of a Portal's task (ADR-0030), or its real path: it must be an absolute path whose
|
|
45
|
+
* real path is an existing directory inside the real `home`. A link, or a link in an ancestor, counts where it points.
|
|
46
|
+
*/
|
|
47
|
+
export async function realCwdInHome(cwd, home) {
|
|
48
|
+
if (!isAbsolute(cwd) || cwd.includes('\0'))
|
|
49
|
+
return { why: 'is not an absolute path' };
|
|
50
|
+
let real;
|
|
51
|
+
try {
|
|
52
|
+
real = await realpath(cwd);
|
|
53
|
+
if (!(await stat(real)).isDirectory())
|
|
54
|
+
return { why: 'is not a folder' };
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
return { why: 'does not exist' };
|
|
58
|
+
}
|
|
59
|
+
const realHome = await realpath(home).catch(() => home);
|
|
60
|
+
if (real !== realHome && !real.startsWith(realHome + sep))
|
|
61
|
+
return { why: 'is outside your home' };
|
|
62
|
+
return { real };
|
|
63
|
+
}
|
|
64
|
+
/** A Portal's task folder that stopped being a folder inside the HOME after it was checked. */
|
|
65
|
+
export function movedCwdError(why) {
|
|
66
|
+
return new UserError(`The task folder ${why} now; it changed after the task was prepared. Check the folder, then start the task again.`, { code: 'bad-cwd' });
|
|
67
|
+
}
|
|
43
68
|
export async function taskWorkDir(paths = resolvePaths()) {
|
|
44
69
|
return privateDir(join(paths.stateDir, 'ai'));
|
|
45
70
|
}
|
|
@@ -150,12 +175,13 @@ export async function readTask(id, paths = resolvePaths()) {
|
|
|
150
175
|
throw invalid();
|
|
151
176
|
const task = JSON.parse(await handle.readFile('utf8'));
|
|
152
177
|
if (!task ||
|
|
153
|
-
!['prompt', 'login'].includes(task.kind) ||
|
|
178
|
+
!['prompt', 'login', 'run'].includes(task.kind) ||
|
|
154
179
|
typeof task.account !== 'string' ||
|
|
155
180
|
typeof task.cwd !== 'string' ||
|
|
156
181
|
!isAbsolute(task.cwd) ||
|
|
157
182
|
(task.kind === 'prompt' && typeof task.prompt !== 'string') ||
|
|
158
183
|
(task.attachments !== undefined && !validAttachments(task.attachments)) ||
|
|
184
|
+
(task.cwdInHome !== undefined && task.cwdInHome !== true) ||
|
|
159
185
|
// Paths are all there or all absent (a task written before they were recorded), and the state folder is the
|
|
160
186
|
// one the task was read from.
|
|
161
187
|
!((task.home === undefined && task.configDir === undefined && task.stateDir === undefined) ||
|
|
@@ -213,6 +239,54 @@ export async function setCustomTerminal(rt, template) {
|
|
|
213
239
|
const r = await editConfigTerminal(rt.paths.configFile, { custom: template, preferred: 'custom' });
|
|
214
240
|
rt.config.terminal = r.after;
|
|
215
241
|
}
|
|
242
|
+
/**
|
|
243
|
+
* The `context` of an AI Task as a page or a Task Spec sends it (spec §3.28): only the strings and findings a Task
|
|
244
|
+
* Prompt shows; anything else is refused through `fail`, which makes the caller's error (a 400, a Task Spec error).
|
|
245
|
+
*/
|
|
246
|
+
export function taskContext(c, fail) {
|
|
247
|
+
if (c === undefined || c === null)
|
|
248
|
+
return undefined;
|
|
249
|
+
if (typeof c !== 'object' || Array.isArray(c))
|
|
250
|
+
throw fail('"context" must be an object');
|
|
251
|
+
const out = {};
|
|
252
|
+
const str = (v, what) => {
|
|
253
|
+
if (typeof v !== 'string')
|
|
254
|
+
throw fail(`${what} must be a string`);
|
|
255
|
+
return v;
|
|
256
|
+
};
|
|
257
|
+
for (const [k, v] of Object.entries(c)) {
|
|
258
|
+
if (k === 'account' || k === 'family' || k === 'quotaReason' || k === 'version' || k === 'terminalCustom')
|
|
259
|
+
out[k] = str(v, `"context.${k}"`);
|
|
260
|
+
else if (k === 'terminals') {
|
|
261
|
+
if (!Array.isArray(v) || v.length > 50)
|
|
262
|
+
throw fail('"context.terminals" must be a list');
|
|
263
|
+
out.terminals = v.map((x) => str(x, 'each of "context.terminals"'));
|
|
264
|
+
}
|
|
265
|
+
else if (k === 'handoff') {
|
|
266
|
+
if (!Array.isArray(v) || v.length > 100)
|
|
267
|
+
throw fail('"context.handoff" must be a list');
|
|
268
|
+
out.handoff = v.map((x) => str(x, 'each of "context.handoff"'));
|
|
269
|
+
}
|
|
270
|
+
else if (k === 'findings') {
|
|
271
|
+
if (!Array.isArray(v) || v.length > 100)
|
|
272
|
+
throw fail('"context.findings" must be a list');
|
|
273
|
+
out.findings = v.map((f) => {
|
|
274
|
+
if (!f || typeof f !== 'object' || Array.isArray(f) || typeof f.message !== 'string')
|
|
275
|
+
throw fail('each finding needs a "message"');
|
|
276
|
+
const one = {
|
|
277
|
+
message: f.message,
|
|
278
|
+
};
|
|
279
|
+
for (const key of ['code', 'account', 'item', 'hint'])
|
|
280
|
+
if (f[key] !== undefined && f[key] !== null)
|
|
281
|
+
one[key] = str(f[key], `"finding.${key}"`);
|
|
282
|
+
return one;
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
else
|
|
286
|
+
throw fail(`unknown context "${k}"`);
|
|
287
|
+
}
|
|
288
|
+
return out;
|
|
289
|
+
}
|
|
216
290
|
/** The attachments a prompt shown before the task exists lists: uploads still staged, the user's files in place. */
|
|
217
291
|
export async function previewAttachments(rt, input) {
|
|
218
292
|
if (input.uploads?.length)
|
|
@@ -228,6 +302,10 @@ async function takeAttachments(rt, input, taskId) {
|
|
|
228
302
|
return copyAttachments(rt.paths, input.files, taskId);
|
|
229
303
|
return [];
|
|
230
304
|
}
|
|
305
|
+
/** The language of a Task Prompt: the one asked for, else Chinese under a `zh` locale, else English. */
|
|
306
|
+
export function taskLang(rt, lang) {
|
|
307
|
+
return lang ?? (/^zh/i.test(rt.env.LC_ALL || rt.env.LC_MESSAGES || rt.env.LANG || '') ? 'zh' : 'en');
|
|
308
|
+
}
|
|
231
309
|
export function promptForTask(rt, input, attachments = []) {
|
|
232
310
|
if (input.task && !AI_TASK_KINDS.includes(input.task))
|
|
233
311
|
throw new UserError(`Choose a task: ${AI_TASK_KINDS.join(', ')}; rerun \`sideby ask\`.`);
|
|
@@ -236,7 +314,7 @@ export function promptForTask(rt, input, attachments = []) {
|
|
|
236
314
|
...input,
|
|
237
315
|
task: input.task ?? 'free',
|
|
238
316
|
attachments: attachments.map(({ path, kind }) => ({ path, kind })),
|
|
239
|
-
lang:
|
|
317
|
+
lang: taskLang(rt, input.lang),
|
|
240
318
|
}, (text) => {
|
|
241
319
|
const cleaned = stripSecrets(text);
|
|
242
320
|
removedSecrets ||= cleaned.removed;
|
|
@@ -244,7 +322,7 @@ export function promptForTask(rt, input, attachments = []) {
|
|
|
244
322
|
});
|
|
245
323
|
return { prompt, removedSecrets };
|
|
246
324
|
}
|
|
247
|
-
const PICK_DETAIL = {
|
|
325
|
+
export const PICK_DETAIL = {
|
|
248
326
|
'no-accounts': 'No account yet: create one with `sideby new <family> --next`, or paste the prompt into any AI.',
|
|
249
327
|
'none-signed-in': 'No account is signed in: run `sideby list`, then `sideby login <account>`; or paste the prompt into any AI.',
|
|
250
328
|
'all-full': 'Every account has used its quota: wait for a reset (`sideby quota`), or paste the prompt into any AI.',
|
|
@@ -254,6 +332,14 @@ async function openTask(rt, task, terminal, launcher = askLauncher(), taskId = n
|
|
|
254
332
|
const id = terminal ?? choices.guess.id;
|
|
255
333
|
if (!TERMINALS.some((t) => t.id === id))
|
|
256
334
|
throw new UserError(`Unknown terminal; choose one of ${TERMINALS.map((t) => t.id).join(', ')}, or run \`sideby terminal\` to see which are installed.`);
|
|
335
|
+
// A Portal's folder was checked before the Account and terminal were chosen; it may have been swapped since.
|
|
336
|
+
if (task.cwdInHome) {
|
|
337
|
+
const now = await realCwdInHome(task.cwd, rt.paths.home);
|
|
338
|
+
if ('why' in now)
|
|
339
|
+
throw movedCwdError(now.why);
|
|
340
|
+
if (now.real !== task.cwd)
|
|
341
|
+
throw movedCwdError('points somewhere else');
|
|
342
|
+
}
|
|
257
343
|
await writeTask(task, rt.paths, taskId);
|
|
258
344
|
const problem = launcherProblem(launcher, rt.paths.stateDir);
|
|
259
345
|
// An unsafe path cannot go into the command; the user pastes it where sideby is on PATH.
|
|
@@ -290,6 +376,14 @@ export async function startAiTask(rt, input) {
|
|
|
290
376
|
const taskId = newTaskId();
|
|
291
377
|
const attachments = await takeAttachments(rt, input, taskId);
|
|
292
378
|
const { prompt, removedSecrets } = promptForTask(rt, input, attachments);
|
|
379
|
+
return openPromptTask(rt, input, { taskId, prompt, removedSecrets, attachments });
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* Picks the Account and opens the Preferred Terminal for a written Task Prompt, as every AI Task does (spec §3.28):
|
|
383
|
+
* sideby's own, and one a Portal hands over in a Task Spec (ADR-0030), which names its own `cwd`.
|
|
384
|
+
*/
|
|
385
|
+
export async function openPromptTask(rt, input, written) {
|
|
386
|
+
const { taskId, prompt, removedSecrets, attachments } = written;
|
|
293
387
|
const pick = await pickTaskAccount(rt, input);
|
|
294
388
|
// Without an account the prompt is copied instead; the saved attachments stay where it says, until the cleanup.
|
|
295
389
|
if ('reason' in pick)
|
|
@@ -317,7 +411,8 @@ export async function startAiTask(rt, input) {
|
|
|
317
411
|
kind: 'prompt',
|
|
318
412
|
account: pick.account.ref,
|
|
319
413
|
prompt,
|
|
320
|
-
cwd: await taskWorkDir(rt.paths),
|
|
414
|
+
cwd: written.cwd ?? (await taskWorkDir(rt.paths)),
|
|
415
|
+
...(written.cwd ? { cwdInHome: true } : {}),
|
|
321
416
|
};
|
|
322
417
|
if (attachments.length)
|
|
323
418
|
task.attachments = attachments.map(({ path, kind }) => ({ path, kind }));
|
|
@@ -331,6 +426,11 @@ export async function openLoginInTerminal(rt, ref) {
|
|
|
331
426
|
const account = await rt.resolve(ref);
|
|
332
427
|
return openTask(rt, { kind: 'login', account: account.ref, cwd: await taskWorkDir(rt.paths) });
|
|
333
428
|
}
|
|
429
|
+
/** Starts an Account in the Preferred Terminal as `sideby run` would, in the HOME folder (spec §3.31). */
|
|
430
|
+
export async function openRunInTerminal(rt, ref) {
|
|
431
|
+
const account = await rt.resolve(ref);
|
|
432
|
+
return openTask(rt, { kind: 'run', account: account.ref, cwd: rt.paths.home });
|
|
433
|
+
}
|
|
334
434
|
/**
|
|
335
435
|
* What `try` will run for an Account, computed as `sideby run` would (Family defaults, config arguments) but without
|
|
336
436
|
* reading its Secret File or running `launch.before` hooks: nothing runs before the user agrees.
|
|
@@ -1,7 +1,30 @@
|
|
|
1
|
+
import type { Env } from '../types.ts';
|
|
1
2
|
import { type AskLauncher } from './terminal.ts';
|
|
2
3
|
export declare const AUTOSTART_LABEL = "dev.sideby.panel";
|
|
3
4
|
/** Present in every agent sideby writes; a file without it is someone else's. */
|
|
4
5
|
export declare const AUTOSTART_MARKER = "Written by `sideby autostart`";
|
|
6
|
+
/** One LaunchAgent sideby can write: the Panel's (`autostart`) or the menu bar item's (`menubar`, ADR-0034). */
|
|
7
|
+
export interface LoginAgent {
|
|
8
|
+
label: string;
|
|
9
|
+
/** Present in every file of this agent sideby writes; a file without it is someone else's. */
|
|
10
|
+
marker: string;
|
|
11
|
+
/** The sideby subcommand that turns it on and off, for messages. */
|
|
12
|
+
command: string;
|
|
13
|
+
/** The comment after the marker: what the agent starts. */
|
|
14
|
+
comment: string;
|
|
15
|
+
programArguments: (l: AskLauncher) => string[];
|
|
16
|
+
/** The recorded paths that must still exist (state `stale` otherwise). */
|
|
17
|
+
launcherPaths: (args: readonly string[]) => string[];
|
|
18
|
+
/** What starts at the next login when launchd could not be told now. */
|
|
19
|
+
later: string;
|
|
20
|
+
/** Restarted by launchd when it exits with an error (a long-running process); a clean exit stays out. */
|
|
21
|
+
restartOnCrash?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Variables the agent's process gets, taken from the environment that turns it on: launchd starts jobs with only
|
|
24
|
+
* `/usr/bin:/bin:/usr/sbin:/sbin`, so Hosts installed elsewhere would not be found.
|
|
25
|
+
*/
|
|
26
|
+
environment?: (env: Env) => Record<string, string>;
|
|
27
|
+
}
|
|
5
28
|
export type AutostartState = 'off' | 'on' | 'stale' | 'foreign';
|
|
6
29
|
/** Runs a program and resolves with its exit code and output; never rejects. */
|
|
7
30
|
export type AutostartExec = (file: string, args: readonly string[]) => Promise<{
|
|
@@ -17,6 +40,8 @@ export interface AutostartContext {
|
|
|
17
40
|
uid?: number;
|
|
18
41
|
/** The home launchd reads LaunchAgents from (the login user's); `launchctl` runs only when `home` is it. */
|
|
19
42
|
loginHome?: string;
|
|
43
|
+
/** The environment an agent's `environment` takes its variables from; this process's by default. */
|
|
44
|
+
env?: Env;
|
|
20
45
|
}
|
|
21
46
|
export interface AutostartStatus {
|
|
22
47
|
/** False on a system sideby cannot start at login yet; `reason` says so. */
|
|
@@ -59,29 +84,34 @@ export interface AutostartResult {
|
|
|
59
84
|
loaded: boolean;
|
|
60
85
|
note?: string;
|
|
61
86
|
}
|
|
87
|
+
export declare const PANEL_AGENT: LoginAgent;
|
|
88
|
+
export declare const agentPath: (home: string, agent: LoginAgent) => string;
|
|
62
89
|
export declare const autostartPath: (home: string) => string;
|
|
63
90
|
export declare const programArgumentsFor: (l: AskLauncher) => string[];
|
|
64
|
-
/**
|
|
65
|
-
|
|
91
|
+
/**
|
|
92
|
+
* The agent's file. The Panel's starts once at login, never again by itself, and outlives the job; an agent with
|
|
93
|
+
* `restartOnCrash` is the long-running process itself, which launchd starts again only after a failed exit.
|
|
94
|
+
*/
|
|
95
|
+
export declare function plistContent(args: readonly string[], agent?: LoginAgent, environment?: Record<string, string>): string;
|
|
66
96
|
/** The ProgramArguments of a file sideby wrote; null when it holds none. */
|
|
67
97
|
export declare function parseProgramArguments(content: string): string[] | null;
|
|
68
98
|
/** Whether a sideby agent (with the marker) is installed; reads only that one file. */
|
|
69
|
-
export declare function autostartInstalled(home: string): Promise<boolean>;
|
|
70
|
-
export declare function autostartStatus(ctx: AutostartContext): Promise<AutostartStatus>;
|
|
99
|
+
export declare function autostartInstalled(home: string, agent?: LoginAgent): Promise<boolean>;
|
|
100
|
+
export declare function autostartStatus(ctx: AutostartContext, agent?: LoginAgent): Promise<AutostartStatus>;
|
|
71
101
|
/** Why the agent cannot start this launcher, with what to do instead; null when it can. */
|
|
72
|
-
export declare function autostartLauncherProblem(l: AskLauncher): Promise<{
|
|
102
|
+
export declare function autostartLauncherProblem(l: AskLauncher, command?: string): Promise<{
|
|
73
103
|
code: string;
|
|
74
104
|
message: string;
|
|
75
105
|
} | null>;
|
|
76
106
|
/** What `on` or `off` would change; writes nothing. */
|
|
77
|
-
export declare function planAutostart(ctx: AutostartContext, action: 'on' | 'off'): Promise<AutostartPlan>;
|
|
107
|
+
export declare function planAutostart(ctx: AutostartContext, action: 'on' | 'off', agent?: LoginAgent): Promise<AutostartPlan>;
|
|
78
108
|
/** Carries out a plan the user confirmed. Refuses, changing nothing, when the file changed since it was planned. */
|
|
79
|
-
export declare function applyAutostart(ctx: AutostartContext, plan: AutostartPlan): Promise<AutostartResult>;
|
|
109
|
+
export declare function applyAutostart(ctx: AutostartContext, plan: AutostartPlan, agent?: LoginAgent): Promise<AutostartResult>;
|
|
80
110
|
/**
|
|
81
111
|
* After `sideby update` or `pnpm local`: rewrites an existing sideby agent so it starts `launcher`. Never creates one,
|
|
82
112
|
* never touches a foreign file, and never throws; `rewritten` says whether the file changed.
|
|
83
113
|
*/
|
|
84
|
-
export declare function refreshAutostart(ctx: AutostartContext): Promise<{
|
|
114
|
+
export declare function refreshAutostart(ctx: AutostartContext, agent?: LoginAgent): Promise<{
|
|
85
115
|
rewritten: boolean;
|
|
86
116
|
state: AutostartState;
|
|
87
117
|
note?: string;
|
|
@@ -18,29 +18,56 @@ const defaultExec = (file, args) => new Promise((resolve) => {
|
|
|
18
18
|
resolve({ code, output: `${stdout}${stderr}` });
|
|
19
19
|
});
|
|
20
20
|
});
|
|
21
|
-
export const
|
|
21
|
+
export const PANEL_AGENT = {
|
|
22
|
+
label: AUTOSTART_LABEL,
|
|
23
|
+
marker: AUTOSTART_MARKER,
|
|
24
|
+
command: 'autostart',
|
|
25
|
+
comment: 'starts the sideby panel in the background at login',
|
|
26
|
+
programArguments: (l) => programArgumentsFor(l),
|
|
27
|
+
launcherPaths: (args) => args.slice(0, 2),
|
|
28
|
+
later: 'the panel starts at your next login, or now with `sideby ui --background`',
|
|
29
|
+
};
|
|
30
|
+
export const agentPath = (home, agent) => join(home, 'Library', 'LaunchAgents', `${agent.label}.plist`);
|
|
31
|
+
export const autostartPath = (home) => agentPath(home, PANEL_AGENT);
|
|
22
32
|
const xml = (s) => s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
23
33
|
const unxml = (s) => s.replace(/</g, '<').replace(/>/g, '>').replace(/&/g, '&');
|
|
24
34
|
export const programArgumentsFor = (l) => [l.node, l.entry, 'ui', '--background', '--no-open'];
|
|
25
|
-
/**
|
|
26
|
-
|
|
35
|
+
/**
|
|
36
|
+
* The agent's file. The Panel's starts once at login, never again by itself, and outlives the job; an agent with
|
|
37
|
+
* `restartOnCrash` is the long-running process itself, which launchd starts again only after a failed exit.
|
|
38
|
+
*/
|
|
39
|
+
export function plistContent(args, agent = PANEL_AGENT, environment = {}) {
|
|
40
|
+
const vars = Object.entries(environment);
|
|
27
41
|
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
28
42
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
29
|
-
<!-- ${
|
|
43
|
+
<!-- ${agent.marker}: ${agent.comment}. Turn it off with \`sideby ${agent.command} off\`. -->
|
|
30
44
|
<plist version="1.0">
|
|
31
45
|
<dict>
|
|
32
46
|
<key>Label</key>
|
|
33
|
-
<string>${
|
|
47
|
+
<string>${agent.label}</string>
|
|
34
48
|
<key>ProgramArguments</key>
|
|
35
49
|
<array>
|
|
36
50
|
${args.map((a) => ` <string>${xml(a)}</string>`).join('\n')}
|
|
37
51
|
</array>
|
|
38
|
-
|
|
52
|
+
${vars.length
|
|
53
|
+
? ` <key>EnvironmentVariables</key>
|
|
54
|
+
<dict>
|
|
55
|
+
${vars.map(([k, v]) => ` <key>${xml(k)}</key>\n <string>${xml(v)}</string>`).join('\n')}
|
|
56
|
+
</dict>
|
|
57
|
+
`
|
|
58
|
+
: ''} <key>RunAtLoad</key>
|
|
39
59
|
<true/>
|
|
40
60
|
<key>KeepAlive</key>
|
|
41
|
-
|
|
61
|
+
${agent.restartOnCrash
|
|
62
|
+
? ` <dict>
|
|
63
|
+
<key>SuccessfulExit</key>
|
|
64
|
+
<false/>
|
|
65
|
+
</dict>
|
|
66
|
+
<key>ThrottleInterval</key>
|
|
67
|
+
<integer>30</integer>`
|
|
68
|
+
: ` <false/>
|
|
42
69
|
<key>AbandonProcessGroup</key>
|
|
43
|
-
<true
|
|
70
|
+
<true/>`}
|
|
44
71
|
</dict>
|
|
45
72
|
</plist>
|
|
46
73
|
`;
|
|
@@ -65,12 +92,12 @@ async function readAgent(path) {
|
|
|
65
92
|
}
|
|
66
93
|
}
|
|
67
94
|
/** Whether a sideby agent (with the marker) is installed; reads only that one file. */
|
|
68
|
-
export async function autostartInstalled(home) {
|
|
69
|
-
const content = await readAgent(
|
|
70
|
-
return content?.includes(
|
|
95
|
+
export async function autostartInstalled(home, agent = PANEL_AGENT) {
|
|
96
|
+
const content = await readAgent(agentPath(home, agent)).catch(() => null);
|
|
97
|
+
return content?.includes(agent.marker) ?? false;
|
|
71
98
|
}
|
|
72
|
-
export async function autostartStatus(ctx) {
|
|
73
|
-
const path =
|
|
99
|
+
export async function autostartStatus(ctx, agent = PANEL_AGENT) {
|
|
100
|
+
const path = agentPath(ctx.home, agent);
|
|
74
101
|
const supported = platformOf(ctx) === 'darwin';
|
|
75
102
|
const base = { supported, ...(supported ? {} : { reason: UNSUPPORTED }), path, current: false };
|
|
76
103
|
if (!supported)
|
|
@@ -78,15 +105,15 @@ export async function autostartStatus(ctx) {
|
|
|
78
105
|
const content = await readAgent(path);
|
|
79
106
|
if (content === null)
|
|
80
107
|
return { ...base, state: 'off' };
|
|
81
|
-
const args = content.includes(
|
|
108
|
+
const args = content.includes(agent.marker) ? parseProgramArguments(content) : null;
|
|
82
109
|
if (!args)
|
|
83
110
|
return { ...base, state: 'foreign' };
|
|
84
111
|
const missing = [];
|
|
85
|
-
for (const p of
|
|
112
|
+
for (const p of agent.launcherPaths(args))
|
|
86
113
|
if (!(await statOrNull(p)))
|
|
87
114
|
missing.push(p);
|
|
88
115
|
const launcher = ctx.launcher ?? askLauncher();
|
|
89
|
-
const want =
|
|
116
|
+
const want = agent.programArguments(launcher);
|
|
90
117
|
const current = args.length === want.length && args.every((a, i) => a === want[i]);
|
|
91
118
|
return {
|
|
92
119
|
...base,
|
|
@@ -105,22 +132,22 @@ async function inTempDir(path) {
|
|
|
105
132
|
return roots.some((r) => path.startsWith(r));
|
|
106
133
|
}
|
|
107
134
|
/** Why the agent cannot start this launcher, with what to do instead; null when it can. */
|
|
108
|
-
export async function autostartLauncherProblem(l) {
|
|
135
|
+
export async function autostartLauncherProblem(l, command = 'autostart') {
|
|
109
136
|
const p = launcherProblem(l);
|
|
110
137
|
if (p?.reason === 'npx-cache')
|
|
111
138
|
return {
|
|
112
139
|
code: 'npx-cache',
|
|
113
|
-
message:
|
|
140
|
+
message: `sideby runs from the npx cache, which npm may clear, so it cannot start at login from there; install it with \`npm i -g sideby\`, then run \`sideby ${command} on\` again`,
|
|
114
141
|
};
|
|
115
142
|
if (p)
|
|
116
143
|
return {
|
|
117
144
|
code: 'unsafe-path',
|
|
118
|
-
message:
|
|
145
|
+
message: `the path of sideby or Node has characters a login item cannot run safely; install sideby with \`npm i -g sideby\` in an ordinary folder, then run \`sideby ${command} on\` again`,
|
|
119
146
|
};
|
|
120
147
|
if ((await inTempDir(l.entry)) || (await inTempDir(l.node)))
|
|
121
148
|
return {
|
|
122
149
|
code: 'temp-dir',
|
|
123
|
-
message:
|
|
150
|
+
message: `sideby or Node runs from a temporary folder that the system empties, so it cannot start at login from there; install it with \`npm i -g sideby\`, then run \`sideby ${command} on\` again`,
|
|
124
151
|
};
|
|
125
152
|
return null;
|
|
126
153
|
}
|
|
@@ -131,10 +158,14 @@ const bootstrap = (ctx, path) => [
|
|
|
131
158
|
`gui/${uidOf(ctx)}`,
|
|
132
159
|
path,
|
|
133
160
|
];
|
|
134
|
-
const bootout = (ctx) => [
|
|
161
|
+
const bootout = (ctx, agent) => [
|
|
162
|
+
'launchctl',
|
|
163
|
+
'bootout',
|
|
164
|
+
`gui/${uidOf(ctx)}/${agent.label}`,
|
|
165
|
+
];
|
|
135
166
|
/** What `on` or `off` would change; writes nothing. */
|
|
136
|
-
export async function planAutostart(ctx, action) {
|
|
137
|
-
const status = await autostartStatus(ctx);
|
|
167
|
+
export async function planAutostart(ctx, action, agent = PANEL_AGENT) {
|
|
168
|
+
const status = await autostartStatus(ctx, agent);
|
|
138
169
|
const path = status.path;
|
|
139
170
|
const hash = status.supported ? await fileHash(path) : null;
|
|
140
171
|
const plan = (extra) => ({
|
|
@@ -152,25 +183,39 @@ export async function planAutostart(ctx, action) {
|
|
|
152
183
|
return plan({
|
|
153
184
|
blocked: {
|
|
154
185
|
code: 'foreign',
|
|
155
|
-
message: `${path} was not written by sideby, so sideby leaves it alone; look at it, and remove or rename it yourself before \`sideby
|
|
186
|
+
message: `${path} was not written by sideby, so sideby leaves it alone; look at it, and remove or rename it yourself before \`sideby ${agent.command} ${action}\` if it is not needed`,
|
|
156
187
|
},
|
|
157
188
|
});
|
|
158
189
|
if (action === 'off')
|
|
159
|
-
return status.state === 'off' ? plan({}) : plan({ changed: true, commands: [bootout(ctx)] });
|
|
190
|
+
return status.state === 'off' ? plan({}) : plan({ changed: true, commands: [bootout(ctx, agent)] });
|
|
160
191
|
const launcher = ctx.launcher ?? askLauncher();
|
|
161
|
-
const blocked = await autostartLauncherProblem(launcher);
|
|
192
|
+
const blocked = await autostartLauncherProblem(launcher, agent.command);
|
|
162
193
|
if (blocked)
|
|
163
194
|
return plan({ blocked });
|
|
164
|
-
const programArguments =
|
|
165
|
-
const content = plistContent(programArguments);
|
|
195
|
+
const programArguments = agent.programArguments(launcher);
|
|
196
|
+
const content = plistContent(programArguments, agent, agent.environment?.(ctx.env ?? process.env));
|
|
166
197
|
const same = hash !== null && hash === sha256(content);
|
|
198
|
+
// The file alone does not start it: launchd may have let it go (a `launchctl bootout`, a failed start), and then
|
|
199
|
+
// only loading it again brings it back.
|
|
200
|
+
const loaded = !same || (await agentLoaded(ctx, agent));
|
|
167
201
|
return plan({
|
|
168
|
-
changed: !same,
|
|
202
|
+
changed: !same || !loaded,
|
|
169
203
|
content,
|
|
170
204
|
programArguments,
|
|
171
|
-
commands: same
|
|
205
|
+
commands: same
|
|
206
|
+
? loaded
|
|
207
|
+
? []
|
|
208
|
+
: [bootstrap(ctx, path)]
|
|
209
|
+
: [...(status.state === 'off' ? [] : [bootout(ctx, agent)]), bootstrap(ctx, path)],
|
|
172
210
|
});
|
|
173
211
|
}
|
|
212
|
+
/** Whether launchd holds the agent now; asks only for the login home, and counts any other HOME as loaded. */
|
|
213
|
+
async function agentLoaded(ctx, agent) {
|
|
214
|
+
if (launchctlNote(ctx))
|
|
215
|
+
return true;
|
|
216
|
+
const r = await (ctx.exec ?? defaultExec)('launchctl', ['print', `gui/${uidOf(ctx)}/${agent.label}`]).catch(() => null);
|
|
217
|
+
return r === null || r.code === 0;
|
|
218
|
+
}
|
|
174
219
|
/** Whether launchctl may run: only for the login user's own LaunchAgents, never for a demo or test HOME. */
|
|
175
220
|
function launchctlNote(ctx) {
|
|
176
221
|
let login = ctx.loginHome;
|
|
@@ -186,7 +231,7 @@ function launchctlNote(ctx) {
|
|
|
186
231
|
: `HOME (${ctx.home}) is not your login home, so launchd was not told; it reads this file only from your login home`;
|
|
187
232
|
}
|
|
188
233
|
/** Carries out a plan the user confirmed. Refuses, changing nothing, when the file changed since it was planned. */
|
|
189
|
-
export async function applyAutostart(ctx, plan) {
|
|
234
|
+
export async function applyAutostart(ctx, plan, agent = PANEL_AGENT) {
|
|
190
235
|
if (plan.blocked)
|
|
191
236
|
throw new UserError(plan.blocked.message, { code: plan.blocked.code });
|
|
192
237
|
const path = plan.path;
|
|
@@ -206,13 +251,14 @@ export async function applyAutostart(ctx, plan) {
|
|
|
206
251
|
const note = launchctlNote(ctx);
|
|
207
252
|
const run = async (cmd) => (note ? null : exec(cmd[0], cmd.slice(1)));
|
|
208
253
|
if (plan.action === 'off') {
|
|
209
|
-
//
|
|
210
|
-
|
|
254
|
+
// The file goes first: bootout ends the job's whole process group, which holds this process when the menu bar
|
|
255
|
+
// item's own "Hide" runs it. Not loaded (already out, or never in) is fine: the file starts it at the next login.
|
|
211
256
|
await rm(path, { force: true });
|
|
257
|
+
await run(bootout(ctx, agent));
|
|
212
258
|
return { changed: true, state: 'off', path, loaded: note === null, ...(note ? { note } : {}) };
|
|
213
259
|
}
|
|
214
260
|
if (plan.status.state !== 'off')
|
|
215
|
-
await run(bootout(ctx));
|
|
261
|
+
await run(bootout(ctx, agent));
|
|
216
262
|
await mkdir(dirname(path), { recursive: true, mode: 0o755 });
|
|
217
263
|
// launchd skips an agent that others can write; 644 is what it expects.
|
|
218
264
|
if (!(await writeIfUnchanged(path, plan.hash, plan.content, 0o644)))
|
|
@@ -230,24 +276,24 @@ export async function applyAutostart(ctx, plan) {
|
|
|
230
276
|
if (note)
|
|
231
277
|
result.note = note;
|
|
232
278
|
else if (r && r.code !== 0)
|
|
233
|
-
result.note = `launchctl bootstrap failed (exit ${r.code});
|
|
279
|
+
result.note = `launchctl bootstrap failed (exit ${r.code}); ${agent.later}`;
|
|
234
280
|
return result;
|
|
235
281
|
}
|
|
236
282
|
/**
|
|
237
283
|
* After `sideby update` or `pnpm local`: rewrites an existing sideby agent so it starts `launcher`. Never creates one,
|
|
238
284
|
* never touches a foreign file, and never throws; `rewritten` says whether the file changed.
|
|
239
285
|
*/
|
|
240
|
-
export async function refreshAutostart(ctx) {
|
|
286
|
+
export async function refreshAutostart(ctx, agent = PANEL_AGENT) {
|
|
241
287
|
let state = 'off';
|
|
242
288
|
try {
|
|
243
|
-
const status = await autostartStatus(ctx);
|
|
289
|
+
const status = await autostartStatus(ctx, agent);
|
|
244
290
|
state = status.state;
|
|
245
291
|
if (status.state !== 'on' && status.state !== 'stale')
|
|
246
292
|
return { rewritten: false, state: status.state };
|
|
247
|
-
const plan = await planAutostart(ctx, 'on');
|
|
293
|
+
const plan = await planAutostart(ctx, 'on', agent);
|
|
248
294
|
if (plan.blocked)
|
|
249
295
|
return { rewritten: false, state: status.state, note: plan.blocked.message };
|
|
250
|
-
const r = await applyAutostart(ctx, plan);
|
|
296
|
+
const r = await applyAutostart(ctx, plan, agent);
|
|
251
297
|
return { rewritten: r.changed, state: r.state, ...(r.note ? { note: r.note } : {}) };
|
|
252
298
|
}
|
|
253
299
|
catch (err) {
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/** A colour by name, or a fixed hex colour for a prop. */
|
|
2
|
+
export type Paint = 'ink' | 'light' | `#${string}`;
|
|
3
|
+
/** An outlined shape of one buddy, as SVG path data (absolute M, L, H, Q, C, Z) on its 64-unit grid. */
|
|
2
4
|
export interface BuddyPath {
|
|
3
5
|
d: string;
|
|
4
6
|
/** `body`, `shade`: the buddy's own colours; `none`: an open line. */
|
|
5
|
-
fill: 'body' | 'shade' | 'none';
|
|
6
|
-
/** Line colour; `
|
|
7
|
-
stroke?:
|
|
7
|
+
fill: 'body' | 'shade' | 'none' | Paint;
|
|
8
|
+
/** Line colour; `none` draws no line. Default ink. */
|
|
9
|
+
stroke?: Paint | 'none';
|
|
8
10
|
/** Line width on the 64-unit grid, before the buddy's scale. Default: the outline width. */
|
|
9
11
|
width?: number;
|
|
10
12
|
}
|
|
@@ -16,36 +18,46 @@ export interface BuddyDot {
|
|
|
16
18
|
ry: number;
|
|
17
19
|
fill: 'ink' | 'light';
|
|
18
20
|
}
|
|
19
|
-
/**
|
|
21
|
+
/** The body both buddies share, back to front: feet, arms, body; then the eyes. No forehead `>`: that belongs to Nika. */
|
|
20
22
|
export declare const BUDDY: {
|
|
21
23
|
paths: readonly BuddyPath[];
|
|
22
24
|
eyes: readonly BuddyDot[];
|
|
23
25
|
};
|
|
26
|
+
/** What one buddy wears and how it smiles: `behind` the body, its `mouth`, and `above` the face. */
|
|
27
|
+
export interface BuddyProps {
|
|
28
|
+
behind: readonly BuddyPath[];
|
|
29
|
+
mouth: BuddyPath;
|
|
30
|
+
above: readonly BuddyPath[];
|
|
31
|
+
}
|
|
32
|
+
/** Sai (blue): a red cape behind the shoulders, a gold crown with a red jewel, a small smile. */
|
|
33
|
+
export declare const SAI: BuddyProps;
|
|
34
|
+
/** Bai (amber): a green spare battery on its back, a bent antenna with a ball, an open laugh. */
|
|
35
|
+
export declare const BAI: BuddyProps;
|
|
24
36
|
/** The eye highlights sit towards where the buddy looks: `1` right, `-1` left. */
|
|
25
37
|
export declare function highlights(look: 1 | -1): BuddyDot[];
|
|
26
38
|
export interface BuddyColors {
|
|
27
39
|
body: string;
|
|
28
40
|
shade: string;
|
|
29
|
-
chevron: string;
|
|
30
41
|
}
|
|
31
42
|
export declare const MARK_COLORS: {
|
|
32
43
|
readonly blue: {
|
|
33
44
|
readonly body: '#3f8fc4';
|
|
34
45
|
readonly shade: '#29658e';
|
|
35
|
-
readonly chevron: '#f2b35e';
|
|
36
46
|
};
|
|
37
47
|
readonly amber: {
|
|
38
48
|
readonly body: '#f2b35e';
|
|
39
49
|
readonly shade: '#c98a35';
|
|
40
|
-
readonly chevron: '#3f8fc4';
|
|
41
50
|
};
|
|
42
51
|
readonly ink: '#1e2a44';
|
|
43
52
|
readonly light: '#f6f0e5';
|
|
44
53
|
};
|
|
45
54
|
export interface PlacedBuddy {
|
|
46
55
|
colors: BuddyColors;
|
|
56
|
+
props: BuddyProps;
|
|
47
57
|
/** Where the buddy looks (and which way its eye highlights sit). */
|
|
48
58
|
look: 1 | -1;
|
|
59
|
+
/** Mirror the props left to right, so what is worn on the back stays behind a buddy that turns the other way. */
|
|
60
|
+
mirror?: boolean;
|
|
49
61
|
/** 64-unit grid → mark units: translate by (x, y), then scale. */
|
|
50
62
|
x: number;
|
|
51
63
|
y: number;
|
|
@@ -64,6 +76,27 @@ export declare const MARK_VIEWBOX: {
|
|
|
64
76
|
readonly y: 3;
|
|
65
77
|
readonly size: 69;
|
|
66
78
|
};
|
|
79
|
+
/** Path data mirrored on the 64-unit grid (x → 64 − x); absolute M, L, H, Q, C, Z only. */
|
|
80
|
+
export declare function mirrorPath(d: string): string;
|
|
81
|
+
/** One shape of a placed buddy with its colours resolved; `fill` undefined inherits the surrounding fill. */
|
|
82
|
+
export interface BuddyShape {
|
|
83
|
+
d: string;
|
|
84
|
+
fill: string | undefined;
|
|
85
|
+
stroke: string;
|
|
86
|
+
width?: number;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* One buddy's shapes, back to front: the props behind, the body, the mouth, the eyes, the props above.
|
|
90
|
+
* `bodyFill`: `undefined` keeps the buddy's body colour, `null` leaves the body unfilled so it inherits.
|
|
91
|
+
*/
|
|
92
|
+
export declare function buddyShapes(b: PlacedBuddy, bodyFill?: string | null): {
|
|
93
|
+
paths: BuddyShape[];
|
|
94
|
+
above: BuddyShape[];
|
|
95
|
+
dots: BuddyDot[];
|
|
96
|
+
};
|
|
97
|
+
/** One placed buddy as SVG, drawn inside a group that sets the ink outline (`MARK_OUTLINE`). */
|
|
98
|
+
export declare function buddySvg(b: PlacedBuddy, line: number, bodyFill?: string | null): string;
|
|
99
|
+
export declare const MARK_OUTLINE: string;
|
|
67
100
|
/**
|
|
68
101
|
* The mark as SVG children (no outer <svg>). With `front` omitted the blue buddy's body has no fill of its own, so it
|
|
69
102
|
* takes the surrounding `fill` (the Panel's `--logo-fg`, which Portals set with `logoColor`); everything else is fixed.
|
|
@@ -74,8 +107,13 @@ export declare const MARK_SVG: string;
|
|
|
74
107
|
/** `<basePath>/icon.svg`: the whole mark on a transparent square, for a Portal's tab or menu (ADR-0006 amendment). */
|
|
75
108
|
export declare const ICON_SVG: string;
|
|
76
109
|
/**
|
|
77
|
-
* `<basePath>/favicon.svg`: the small cut for sizes below about 24 px,
|
|
78
|
-
*
|
|
79
|
-
*
|
|
110
|
+
* `<basePath>/favicon.svg`: the small cut for sizes below about 24 px, for a 16 pt browser tab (ADR-0022): Sai alone
|
|
111
|
+
* on a 32 grid at half scale, his body, eyes, cape and crown; no feet, arms or mouth. The same cut as the tab icons
|
|
112
|
+
* of the brand design sheet.
|
|
80
113
|
*/
|
|
81
114
|
export declare const FAVICON_SVG: string;
|
|
115
|
+
/**
|
|
116
|
+
* The Menu Bar Item's icon (spec §3.31): the favicon's small cut of Sai in colour, 18 pt square. It keeps its colours
|
|
117
|
+
* in light and dark mode, as the brand's tab icons do; the body's dark outline keeps it visible on both.
|
|
118
|
+
*/
|
|
119
|
+
export declare const MENUBAR_ICON_SVG: string;
|