sideby 0.29.0 → 0.30.1
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/dist/build.json +1 -1
- package/dist/src/cli/commands.d.ts +10 -0
- package/dist/src/cli/commands.js +306 -8
- package/dist/src/cli/json-schemas.js +75 -1
- package/dist/src/cli/main.js +83 -4
- package/dist/src/core/config.d.ts +25 -0
- package/dist/src/core/config.js +34 -0
- package/dist/src/core/fs-safe.d.ts +22 -1
- package/dist/src/core/fs-safe.js +68 -1
- package/dist/src/core/handoff-run.d.ts +2 -0
- package/dist/src/core/handoff-run.js +12 -6
- package/dist/src/core/launch.d.ts +6 -1
- package/dist/src/core/messages.js +9 -31
- package/dist/src/core/network.d.ts +5 -2
- package/dist/src/core/network.js +15 -45
- package/dist/src/core/search-catalog.d.ts +1 -1
- package/dist/src/core/search-catalog.js +37 -0
- package/dist/src/core/session-hooks.d.ts +33 -0
- package/dist/src/core/session-hooks.js +92 -0
- package/dist/src/core/unattended-ended.d.ts +62 -0
- package/dist/src/core/unattended-ended.js +275 -0
- package/dist/src/core/unattended-pending.d.ts +56 -0
- package/dist/src/core/unattended-pending.js +182 -0
- package/dist/src/core/unattended-state.d.ts +129 -0
- package/dist/src/core/unattended-state.js +373 -0
- package/dist/src/core/unattended.d.ts +142 -0
- package/dist/src/core/unattended.js +349 -0
- package/dist/src/panel/background.js +8 -0
- package/dist/src/panel/handler.d.ts +37 -0
- package/dist/src/panel/handler.js +153 -1
- package/dist/src/panel/i18n.d.ts +97 -0
- package/dist/src/panel/i18n.js +194 -0
- package/dist/src/panel/page-logic.d.ts +129 -0
- package/dist/src/panel/page-logic.js +166 -0
- package/dist/src/panel/page-script.js +638 -7
- package/dist/src/panel/page-styles.js +69 -0
- package/dist/src/panel/page.js +2 -0
- package/dist/src/plugins/bus.js +1 -0
- package/dist/src/runtime.d.ts +17 -1
- package/dist/src/runtime.js +78 -15
- package/dist/src/types.d.ts +48 -0
- package/dist/src/unattended/hook.d.ts +46 -0
- package/dist/src/unattended/hook.js +288 -0
- package/package.json +1 -1
- package/schemas/config.json +76 -0
- package/schemas/unattended-defer.json +143 -0
- package/schemas/unattended-pending.json +154 -0
- package/schemas/unattended.json +291 -0
- package/skills/sideby/SKILL.md +2 -1
- package/skills/sideby/references/unattended.md +58 -0
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
export declare const DAYS: readonly ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'];
|
|
2
|
+
export type Day = (typeof DAYS)[number];
|
|
3
|
+
/** One entry of the Schedule: local days, start and end (`HH:MM`); an end before the start runs past midnight. */
|
|
4
|
+
export interface UnattendedSlot {
|
|
5
|
+
name?: string;
|
|
6
|
+
days: Day[];
|
|
7
|
+
start: string;
|
|
8
|
+
end: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The manual state: on until `until`, or paused (during a Slot) until `until`. A pause without `until` was made while
|
|
12
|
+
* the Schedule covers every hour: it lasts until the user turns it on again, or until the Schedule changes so that the
|
|
13
|
+
* stretch it was made in ends. `at` is when the user chose it.
|
|
14
|
+
*/
|
|
15
|
+
export interface UnattendedOverride {
|
|
16
|
+
mode: 'on' | 'pause';
|
|
17
|
+
/** Always set for `on`. */
|
|
18
|
+
until?: string;
|
|
19
|
+
at: string;
|
|
20
|
+
}
|
|
21
|
+
export type UnattendedReason = 'slot' | 'manual' | 'session' | 'paused' | 'off';
|
|
22
|
+
export interface UnattendedState {
|
|
23
|
+
on: boolean;
|
|
24
|
+
/** slot: a Slot turned it on; manual: the user did; session: only this session (`--unattended`); paused: off until the Slot ends. */
|
|
25
|
+
reason: UnattendedReason;
|
|
26
|
+
/**
|
|
27
|
+
* When it turns off (on), or comes back (paused). Absent for a session alone, and when the Schedule covers every hour:
|
|
28
|
+
* then it is on with no end, or paused until the user turns it on.
|
|
29
|
+
*/
|
|
30
|
+
until?: string;
|
|
31
|
+
/** The Slot it is in: its name, or its days and times when it has none. */
|
|
32
|
+
slot?: string;
|
|
33
|
+
/** That Slot's number in the Schedule, from 1 (as `sideby unattended` numbers them). */
|
|
34
|
+
slotNumber?: number;
|
|
35
|
+
/** The next time a Slot turns it on by itself, and which Slot (its label and number). */
|
|
36
|
+
next?: {
|
|
37
|
+
at: string;
|
|
38
|
+
slot: string;
|
|
39
|
+
slotNumber?: number;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/** A stretch of time it is on, in milliseconds since epoch. */
|
|
43
|
+
export interface Stretch {
|
|
44
|
+
start: number;
|
|
45
|
+
end: number;
|
|
46
|
+
reason: 'slot' | 'manual';
|
|
47
|
+
slot?: string;
|
|
48
|
+
/** The Slot's number in the Schedule, from 1. */
|
|
49
|
+
slotNumber?: number;
|
|
50
|
+
}
|
|
51
|
+
/** Set in the environment of a session started with `--unattended`: on for that session, whatever the Schedule says. */
|
|
52
|
+
export declare const UNATTENDED_ENV = "SIDEBY_UNATTENDED";
|
|
53
|
+
/** The longest the user can turn it on by hand. */
|
|
54
|
+
export declare const MAX_MANUAL_MS: number;
|
|
55
|
+
export declare const unattendedDir: (stateDir: string) => string;
|
|
56
|
+
/** `8:05` or `08:05` as `08:05`; undefined for anything else. */
|
|
57
|
+
export declare function parseClock(text: string): string | undefined;
|
|
58
|
+
/**
|
|
59
|
+
* Days as the CLI takes them: `mon-fri`, `sat,sun`, `fri-mon` (a range may wrap past Sunday), `weekdays`, `weekends`,
|
|
60
|
+
* `daily`. In week order; undefined when a part is not a day.
|
|
61
|
+
*/
|
|
62
|
+
export declare function parseDays(text: string): Day[] | undefined;
|
|
63
|
+
/** `mon-fri`, `sat,sun`, `fri-mon`: the shortest way to write the days, in week order (a run may wrap past Sunday). */
|
|
64
|
+
export declare function daysText(days: readonly Day[]): string;
|
|
65
|
+
/** What a Slot is called on screen: its name, or its days and times. */
|
|
66
|
+
export declare function slotLabel(slot: UnattendedSlot): string;
|
|
67
|
+
/** A Slot checked by hand, the way `unattended-hook` reads the config: anything malformed is left out. */
|
|
68
|
+
export declare function parseSlot(value: unknown): UnattendedSlot | undefined;
|
|
69
|
+
/** The Schedule from config's `unattended.schedule`; a malformed Slot is left out, a malformed list is empty. */
|
|
70
|
+
export declare function parseSchedule(value: unknown): UnattendedSlot[];
|
|
71
|
+
/** The manual state from its file's content; undefined when it is not one. */
|
|
72
|
+
export declare function parseOverride(value: unknown): UnattendedOverride | undefined;
|
|
73
|
+
/**
|
|
74
|
+
* Every time a Slot is on that overlaps `[from, to)`, in local time: a Slot whose end is before its start runs past
|
|
75
|
+
* midnight and belongs to its start day; one whose end equals its start lasts the whole day (24 hours).
|
|
76
|
+
*/
|
|
77
|
+
export declare function slotOccurrences(schedule: readonly UnattendedSlot[], from: number, to: number): Stretch[];
|
|
78
|
+
/** Joins stretches that overlap or touch; each keeps how its first part began. */
|
|
79
|
+
export declare function mergeStretches(list: readonly Stretch[]): Stretch[];
|
|
80
|
+
/**
|
|
81
|
+
* The Schedule's stretch running at `at` (joined Slots count as one), and whether it never ends: it fills the whole
|
|
82
|
+
* window laid out around `at`, so the Schedule covers every hour.
|
|
83
|
+
*/
|
|
84
|
+
export declare function scheduleStretchAt(schedule: readonly UnattendedSlot[], at: number): {
|
|
85
|
+
stretch: Stretch;
|
|
86
|
+
endless: boolean;
|
|
87
|
+
} | undefined;
|
|
88
|
+
/**
|
|
89
|
+
* When it was or will be on for the whole computer within `[from, to)`: the Slots and the manual "on" joined (the
|
|
90
|
+
* later end wins), the pause taken out. A stretch that runs past the window is cut at its edge.
|
|
91
|
+
*/
|
|
92
|
+
export declare function unattendedTimeline(schedule: readonly UnattendedSlot[], override: UnattendedOverride | undefined, from: number, to: number): Stretch[];
|
|
93
|
+
/**
|
|
94
|
+
* When `on --slot-end` ends: the end of the Slot it is in now, else of the next one (joined Slots count as one).
|
|
95
|
+
* Undefined when there is no Slot, or when the Schedule covers every hour and so never ends.
|
|
96
|
+
*/
|
|
97
|
+
export declare function slotEnd(schedule: readonly UnattendedSlot[], now: Date): number | undefined;
|
|
98
|
+
/** Whether it is on now, why, until when, and when a Slot turns it on next. */
|
|
99
|
+
export declare function unattendedState(input: {
|
|
100
|
+
schedule: readonly UnattendedSlot[];
|
|
101
|
+
override?: UnattendedOverride | undefined;
|
|
102
|
+
now: Date;
|
|
103
|
+
/** The session was started with `--unattended` (`SIDEBY_UNATTENDED=1`). */
|
|
104
|
+
session?: boolean;
|
|
105
|
+
}): UnattendedState;
|
|
106
|
+
/** Reads a small JSON file that must be a plain file (opened without following a link); undefined otherwise. */
|
|
107
|
+
export declare function readSmallJson(file: string, max?: number): Promise<unknown>;
|
|
108
|
+
/** What the config file says about Unattended Mode, read by hand (no schema): whether it has the section, and its Schedule. */
|
|
109
|
+
export declare function readUnattendedConfig(configFile: string): Promise<{
|
|
110
|
+
configured: boolean;
|
|
111
|
+
schedule: UnattendedSlot[];
|
|
112
|
+
}>;
|
|
113
|
+
/**
|
|
114
|
+
* The manual state as the file holds it, expired or not; undefined when there is none, it is not one, or
|
|
115
|
+
* `<state>/unattended` is not a real folder of the user that nobody else can write to.
|
|
116
|
+
*/
|
|
117
|
+
export declare function readUnattendedOverride(stateDir: string): Promise<UnattendedOverride | undefined>;
|
|
118
|
+
/**
|
|
119
|
+
* `<state>/unattended`, made (mode 700) when missing and narrowed to 700 when others can read it. Throws
|
|
120
|
+
* `UnsafeFolderError` (its message says what to do) when it is a link, someone else's, or writable by others: nothing
|
|
121
|
+
* of Unattended Mode is written through such a folder.
|
|
122
|
+
*/
|
|
123
|
+
export declare function ensureUnattendedDir(stateDir: string): Promise<string>;
|
|
124
|
+
/** Writes the manual state (mode 600, atomically). */
|
|
125
|
+
export declare function writeUnattendedOverride(stateDir: string, override: UnattendedOverride): Promise<void>;
|
|
126
|
+
/** Removes the manual state. */
|
|
127
|
+
export declare function clearUnattendedOverride(stateDir: string): Promise<void>;
|
|
128
|
+
/** A time as the user reads it: `08:00` today, `tomorrow 08:00`, else `Mon 08:00` (or in Chinese). */
|
|
129
|
+
export declare function clockText(at: number, now: number, zh?: boolean): string;
|
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
// Unattended Mode (spec §3.37, ADR-0058): whether it is on now. One pure function decides it for the CLI, the Panel and
|
|
2
|
+
// `unattended-hook`, from the Schedule (config `unattended.schedule`), the manual state kept in
|
|
3
|
+
// `<state>/unattended/override.json` and the session flag. A leaf module (Node built-ins and core/fs-safe.ts only):
|
|
4
|
+
// `unattended-hook` runs it on every tool call.
|
|
5
|
+
import { readFile, rm } from 'node:fs/promises';
|
|
6
|
+
import { join } from 'node:path';
|
|
7
|
+
import { ownedFolder, readSmallFile, writeFileAtomic } from './fs-safe.js';
|
|
8
|
+
export const DAYS = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'];
|
|
9
|
+
/** Set in the environment of a session started with `--unattended`: on for that session, whatever the Schedule says. */
|
|
10
|
+
export const UNATTENDED_ENV = 'SIDEBY_UNATTENDED';
|
|
11
|
+
/** The longest the user can turn it on by hand. */
|
|
12
|
+
export const MAX_MANUAL_MS = 72 * 60 * 60 * 1000;
|
|
13
|
+
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
14
|
+
/**
|
|
15
|
+
* How far around a moment the Schedule is laid out. A weekly Schedule with any gap has one in every 7 days (8 with a
|
|
16
|
+
* clock change), so a stretch that fills this whole window never ends: the Schedule covers every hour.
|
|
17
|
+
*/
|
|
18
|
+
const BACK_MS = 8 * DAY_MS;
|
|
19
|
+
const AHEAD_MS = 9 * DAY_MS;
|
|
20
|
+
const TIME = /^([01]\d|2[0-3]):([0-5]\d)$/;
|
|
21
|
+
const NAME_MAX = 60;
|
|
22
|
+
export const unattendedDir = (stateDir) => join(stateDir, 'unattended');
|
|
23
|
+
const overrideFile = (stateDir) => join(unattendedDir(stateDir), 'override.json');
|
|
24
|
+
/** `8:05` or `08:05` as `08:05`; undefined for anything else. */
|
|
25
|
+
export function parseClock(text) {
|
|
26
|
+
const m = /^(\d{1,2}):(\d{2})$/.exec(text.trim());
|
|
27
|
+
if (!m)
|
|
28
|
+
return undefined;
|
|
29
|
+
const value = `${m[1].padStart(2, '0')}:${m[2]}`;
|
|
30
|
+
return TIME.test(value) ? value : undefined;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Days as the CLI takes them: `mon-fri`, `sat,sun`, `fri-mon` (a range may wrap past Sunday), `weekdays`, `weekends`,
|
|
34
|
+
* `daily`. In week order; undefined when a part is not a day.
|
|
35
|
+
*/
|
|
36
|
+
export function parseDays(text) {
|
|
37
|
+
const words = {
|
|
38
|
+
weekdays: DAYS.slice(0, 5),
|
|
39
|
+
weekends: DAYS.slice(5),
|
|
40
|
+
weekend: DAYS.slice(5),
|
|
41
|
+
daily: DAYS,
|
|
42
|
+
everyday: DAYS,
|
|
43
|
+
all: DAYS,
|
|
44
|
+
};
|
|
45
|
+
const out = new Set();
|
|
46
|
+
const parts = text
|
|
47
|
+
.toLowerCase()
|
|
48
|
+
.split(',')
|
|
49
|
+
.map((p) => p.trim());
|
|
50
|
+
if (parts.some((p) => !p))
|
|
51
|
+
return undefined;
|
|
52
|
+
for (const part of parts) {
|
|
53
|
+
if (words[part]) {
|
|
54
|
+
for (const d of words[part])
|
|
55
|
+
out.add(d);
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
const range = part.split('-').map((p) => p.trim());
|
|
59
|
+
if (range.length > 2)
|
|
60
|
+
return undefined;
|
|
61
|
+
const from = dayOf(range[0]);
|
|
62
|
+
const to = range.length === 2 ? dayOf(range[1]) : from;
|
|
63
|
+
if (!from || !to)
|
|
64
|
+
return undefined;
|
|
65
|
+
for (let i = DAYS.indexOf(from);; i = (i + 1) % 7) {
|
|
66
|
+
out.add(DAYS[i]);
|
|
67
|
+
if (DAYS[i] === to)
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return DAYS.filter((d) => out.has(d));
|
|
72
|
+
}
|
|
73
|
+
const LONG_DAYS = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];
|
|
74
|
+
/** `mon`, `mond` and `monday` are Monday; `mo` and `monx` are not a day. */
|
|
75
|
+
function dayOf(s) {
|
|
76
|
+
const i = s.length >= 3 ? LONG_DAYS.findIndex((d) => d.startsWith(s)) : -1;
|
|
77
|
+
return i < 0 ? undefined : DAYS[i];
|
|
78
|
+
}
|
|
79
|
+
/** `mon-fri`, `sat,sun`, `fri-mon`: the shortest way to write the days, in week order (a run may wrap past Sunday). */
|
|
80
|
+
export function daysText(days) {
|
|
81
|
+
const on = DAYS.map((d) => days.includes(d));
|
|
82
|
+
if (on.every(Boolean))
|
|
83
|
+
return 'daily';
|
|
84
|
+
// Start right after a day that is off, so a run past Sunday (fri-mon) stays one run.
|
|
85
|
+
const first = on.indexOf(false);
|
|
86
|
+
const runs = [];
|
|
87
|
+
for (let k = 1; k <= 7; k++) {
|
|
88
|
+
const i = (first + k) % 7;
|
|
89
|
+
if (!on[i])
|
|
90
|
+
continue;
|
|
91
|
+
const prev = (i + 6) % 7;
|
|
92
|
+
if (k > 1 && on[prev] && runs.length)
|
|
93
|
+
runs.at(-1).push(DAYS[i]);
|
|
94
|
+
else
|
|
95
|
+
runs.push([DAYS[i]]);
|
|
96
|
+
}
|
|
97
|
+
runs.sort((a, b) => DAYS.indexOf(a[0]) - DAYS.indexOf(b[0]));
|
|
98
|
+
return runs
|
|
99
|
+
.map((r) => (r.length === 1 ? r[0] : r.length === 2 ? `${r[0]},${r[1]}` : `${r[0]}-${r.at(-1)}`))
|
|
100
|
+
.join(',');
|
|
101
|
+
}
|
|
102
|
+
/** What a Slot is called on screen: its name, or its days and times. */
|
|
103
|
+
export function slotLabel(slot) {
|
|
104
|
+
return slot.name || `${daysText(slot.days)} ${slot.start}-${slot.end}`;
|
|
105
|
+
}
|
|
106
|
+
/** A Slot checked by hand, the way `unattended-hook` reads the config: anything malformed is left out. */
|
|
107
|
+
export function parseSlot(value) {
|
|
108
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
109
|
+
return undefined;
|
|
110
|
+
const v = value;
|
|
111
|
+
if (typeof v.start !== 'string' || typeof v.end !== 'string' || !TIME.test(v.start) || !TIME.test(v.end))
|
|
112
|
+
return undefined;
|
|
113
|
+
if (!Array.isArray(v.days))
|
|
114
|
+
return undefined;
|
|
115
|
+
const days = DAYS.filter((d) => v.days.includes(d));
|
|
116
|
+
// Every entry a day, none twice.
|
|
117
|
+
if (!days.length || days.length !== v.days.length)
|
|
118
|
+
return undefined;
|
|
119
|
+
const name = typeof v.name === 'string' && v.name.trim() ? v.name.trim().slice(0, NAME_MAX) : undefined;
|
|
120
|
+
return { ...(name ? { name } : {}), days, start: v.start, end: v.end };
|
|
121
|
+
}
|
|
122
|
+
/** The Schedule from config's `unattended.schedule`; a malformed Slot is left out, a malformed list is empty. */
|
|
123
|
+
export function parseSchedule(value) {
|
|
124
|
+
if (!Array.isArray(value))
|
|
125
|
+
return [];
|
|
126
|
+
return value.map(parseSlot).filter((s) => s !== undefined);
|
|
127
|
+
}
|
|
128
|
+
/** The manual state from its file's content; undefined when it is not one. */
|
|
129
|
+
export function parseOverride(value) {
|
|
130
|
+
if (!value || typeof value !== 'object')
|
|
131
|
+
return undefined;
|
|
132
|
+
const v = value;
|
|
133
|
+
if (v.mode !== 'on' && v.mode !== 'pause')
|
|
134
|
+
return undefined;
|
|
135
|
+
if (typeof v.at !== 'string')
|
|
136
|
+
return undefined;
|
|
137
|
+
const at = Date.parse(v.at);
|
|
138
|
+
if (!Number.isFinite(at))
|
|
139
|
+
return undefined;
|
|
140
|
+
if (v.until === undefined && v.mode === 'pause')
|
|
141
|
+
return { mode: 'pause', at: new Date(at).toISOString() };
|
|
142
|
+
if (typeof v.until !== 'string')
|
|
143
|
+
return undefined;
|
|
144
|
+
const until = Date.parse(v.until);
|
|
145
|
+
if (!Number.isFinite(until) || until <= at)
|
|
146
|
+
return undefined;
|
|
147
|
+
return { mode: v.mode, until: new Date(until).toISOString(), at: new Date(at).toISOString() };
|
|
148
|
+
}
|
|
149
|
+
const clockMinutes = (hhmm) => Number(hhmm.slice(0, 2)) * 60 + Number(hhmm.slice(3));
|
|
150
|
+
/**
|
|
151
|
+
* Every time a Slot is on that overlaps `[from, to)`, in local time: a Slot whose end is before its start runs past
|
|
152
|
+
* midnight and belongs to its start day; one whose end equals its start lasts the whole day (24 hours).
|
|
153
|
+
*/
|
|
154
|
+
export function slotOccurrences(schedule, from, to) {
|
|
155
|
+
const out = [];
|
|
156
|
+
if (!schedule.length || to <= from)
|
|
157
|
+
return out;
|
|
158
|
+
const base = new Date(from);
|
|
159
|
+
const [y, m, d] = [base.getFullYear(), base.getMonth(), base.getDate() - 1];
|
|
160
|
+
for (let i = 0;; i++) {
|
|
161
|
+
const day = new Date(y, m, d + i);
|
|
162
|
+
if (day.getTime() >= to)
|
|
163
|
+
break;
|
|
164
|
+
const name = DAYS[(day.getDay() + 6) % 7];
|
|
165
|
+
for (const [n, slot] of schedule.entries()) {
|
|
166
|
+
if (!slot.days.includes(name))
|
|
167
|
+
continue;
|
|
168
|
+
const s = clockMinutes(slot.start);
|
|
169
|
+
const e = clockMinutes(slot.end);
|
|
170
|
+
const start = new Date(y, m, d + i, Math.floor(s / 60), s % 60).getTime();
|
|
171
|
+
const end = new Date(y, m, d + i + (e <= s ? 1 : 0), Math.floor(e / 60), e % 60).getTime();
|
|
172
|
+
if (end > from && start < to)
|
|
173
|
+
out.push({ start, end, reason: 'slot', slot: slotLabel(slot), slotNumber: n + 1 });
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return out;
|
|
177
|
+
}
|
|
178
|
+
/** Joins stretches that overlap or touch; each keeps how its first part began. */
|
|
179
|
+
export function mergeStretches(list) {
|
|
180
|
+
const out = [];
|
|
181
|
+
for (const s of [...list].sort((a, b) => a.start - b.start || b.end - a.end)) {
|
|
182
|
+
const last = out.at(-1);
|
|
183
|
+
if (last && s.start <= last.end)
|
|
184
|
+
last.end = Math.max(last.end, s.end);
|
|
185
|
+
else
|
|
186
|
+
out.push({ ...s });
|
|
187
|
+
}
|
|
188
|
+
return out;
|
|
189
|
+
}
|
|
190
|
+
function withoutPause(list, pause) {
|
|
191
|
+
if (!pause)
|
|
192
|
+
return [...list];
|
|
193
|
+
return list.flatMap((s) => {
|
|
194
|
+
if (s.end <= pause.start || s.start >= pause.end)
|
|
195
|
+
return [s];
|
|
196
|
+
const parts = [];
|
|
197
|
+
if (s.start < pause.start)
|
|
198
|
+
parts.push({ ...s, end: pause.start });
|
|
199
|
+
if (s.end > pause.end)
|
|
200
|
+
parts.push({ ...s, start: pause.end });
|
|
201
|
+
return parts;
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
/** The manual "on" as a stretch, at most 72 hours long whatever the file says. */
|
|
205
|
+
function manualStretch(o) {
|
|
206
|
+
if (o?.mode !== 'on' || !o.until)
|
|
207
|
+
return undefined;
|
|
208
|
+
const start = Date.parse(o.at);
|
|
209
|
+
return { start, end: Math.min(Date.parse(o.until), start + MAX_MANUAL_MS), reason: 'manual' };
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* The Schedule's stretch running at `at` (joined Slots count as one), and whether it never ends: it fills the whole
|
|
213
|
+
* window laid out around `at`, so the Schedule covers every hour.
|
|
214
|
+
*/
|
|
215
|
+
export function scheduleStretchAt(schedule, at) {
|
|
216
|
+
const [from, to] = [at - BACK_MS, at + AHEAD_MS];
|
|
217
|
+
const stretch = mergeStretches(slotOccurrences(schedule, from, to)).find((s) => s.start <= at && at < s.end);
|
|
218
|
+
return stretch ? { stretch, endless: stretch.start <= from && stretch.end >= to } : undefined;
|
|
219
|
+
}
|
|
220
|
+
/** The pause as a span; an open pause (no `until`) ends with the Schedule's stretch it was made in, which may be never. */
|
|
221
|
+
function pauseSpan(o, schedule) {
|
|
222
|
+
if (o?.mode !== 'pause')
|
|
223
|
+
return undefined;
|
|
224
|
+
const start = Date.parse(o.at);
|
|
225
|
+
if (o.until)
|
|
226
|
+
return { start, end: Date.parse(o.until) };
|
|
227
|
+
const s = scheduleStretchAt(schedule, start);
|
|
228
|
+
return { start, end: !s ? start : s.endless ? Number.POSITIVE_INFINITY : s.stretch.end };
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* When it was or will be on for the whole computer within `[from, to)`: the Slots and the manual "on" joined (the
|
|
232
|
+
* later end wins), the pause taken out. A stretch that runs past the window is cut at its edge.
|
|
233
|
+
*/
|
|
234
|
+
export function unattendedTimeline(schedule, override, from, to) {
|
|
235
|
+
const manual = manualStretch(override);
|
|
236
|
+
const pieces = [...slotOccurrences(schedule, from, to), ...(manual ? [manual] : [])];
|
|
237
|
+
return withoutPause(mergeStretches(pieces), pauseSpan(override, schedule)).filter((s) => s.end > from && s.start < to);
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* When `on --slot-end` ends: the end of the Slot it is in now, else of the next one (joined Slots count as one).
|
|
241
|
+
* Undefined when there is no Slot, or when the Schedule covers every hour and so never ends.
|
|
242
|
+
*/
|
|
243
|
+
export function slotEnd(schedule, now) {
|
|
244
|
+
const t = now.getTime();
|
|
245
|
+
const [from, to] = [t - BACK_MS, t + AHEAD_MS];
|
|
246
|
+
const s = mergeStretches(slotOccurrences(schedule, from, to)).find((x) => x.end > t);
|
|
247
|
+
return !s || (s.start <= from && s.end >= to) ? undefined : s.end;
|
|
248
|
+
}
|
|
249
|
+
/** Whether it is on now, why, until when, and when a Slot turns it on next. */
|
|
250
|
+
export function unattendedState(input) {
|
|
251
|
+
const t = input.now.getTime();
|
|
252
|
+
const [from, to] = [t - BACK_MS, t + AHEAD_MS];
|
|
253
|
+
const override = input.override;
|
|
254
|
+
const pieces = slotOccurrences(input.schedule, from, to);
|
|
255
|
+
const pause = pauseSpan(override, input.schedule);
|
|
256
|
+
const current = unattendedTimeline(input.schedule, override, from, to).find((s) => s.start <= t && t < s.end);
|
|
257
|
+
const inSlot = pieces.find((s) => s.start <= t && t < s.end);
|
|
258
|
+
const slotNow = inSlot?.slot;
|
|
259
|
+
const slotHere = inSlot ? { slot: inSlot.slot, slotNumber: inSlot.slotNumber } : {};
|
|
260
|
+
const after = current ? current.end : t;
|
|
261
|
+
const upcoming = withoutPause(mergeStretches(pieces), pause).find((s) => s.start > t && s.start >= after);
|
|
262
|
+
const next = upcoming
|
|
263
|
+
? {
|
|
264
|
+
next: {
|
|
265
|
+
at: iso(upcoming.start),
|
|
266
|
+
slot: upcoming.slot ?? '',
|
|
267
|
+
...(upcoming.slotNumber ? { slotNumber: upcoming.slotNumber } : {}),
|
|
268
|
+
},
|
|
269
|
+
}
|
|
270
|
+
: {};
|
|
271
|
+
if (current) {
|
|
272
|
+
const manual = manualStretch(override);
|
|
273
|
+
const byHand = manual !== undefined && manual.start <= t && t < manual.end;
|
|
274
|
+
// A stretch that reaches the end of the window is the Schedule covering every hour: it has no end.
|
|
275
|
+
return {
|
|
276
|
+
on: true,
|
|
277
|
+
reason: byHand ? 'manual' : 'slot',
|
|
278
|
+
...(current.end < to ? { until: iso(current.end) } : {}),
|
|
279
|
+
...(slotNow ? slotHere : {}),
|
|
280
|
+
...next,
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
if (input.session)
|
|
284
|
+
return { on: true, reason: 'session', ...next };
|
|
285
|
+
if (pause && pause.start <= t && t < pause.end)
|
|
286
|
+
return {
|
|
287
|
+
on: false,
|
|
288
|
+
reason: 'paused',
|
|
289
|
+
...(Number.isFinite(pause.end) ? { until: iso(pause.end) } : {}),
|
|
290
|
+
...(slotNow ? slotHere : {}),
|
|
291
|
+
...next,
|
|
292
|
+
};
|
|
293
|
+
return { on: false, reason: 'off', ...next };
|
|
294
|
+
}
|
|
295
|
+
function iso(ms) {
|
|
296
|
+
return new Date(ms).toISOString();
|
|
297
|
+
}
|
|
298
|
+
/** Reads a small JSON file that must be a plain file (opened without following a link); undefined otherwise. */
|
|
299
|
+
export async function readSmallJson(file, max = 1024 * 1024) {
|
|
300
|
+
const text = await readSmallFile(file, max);
|
|
301
|
+
if (text === undefined)
|
|
302
|
+
return undefined;
|
|
303
|
+
try {
|
|
304
|
+
return JSON.parse(text);
|
|
305
|
+
}
|
|
306
|
+
catch {
|
|
307
|
+
return undefined;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
/** What the config file says about Unattended Mode, read by hand (no schema): whether it has the section, and its Schedule. */
|
|
311
|
+
export async function readUnattendedConfig(configFile) {
|
|
312
|
+
// The config may be a link a dotfiles manager keeps; reading through it is fine.
|
|
313
|
+
try {
|
|
314
|
+
const text = await readFile(configFile, 'utf8');
|
|
315
|
+
if (text.length > 4 * 1024 * 1024)
|
|
316
|
+
return { configured: false, schedule: [] };
|
|
317
|
+
const config = JSON.parse(text);
|
|
318
|
+
const section = config && typeof config === 'object' ? config.unattended : undefined;
|
|
319
|
+
if (!section || typeof section !== 'object')
|
|
320
|
+
return { configured: false, schedule: [] };
|
|
321
|
+
return { configured: true, schedule: parseSchedule(section.schedule) };
|
|
322
|
+
}
|
|
323
|
+
catch {
|
|
324
|
+
return { configured: false, schedule: [] };
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* The manual state as the file holds it, expired or not; undefined when there is none, it is not one, or
|
|
329
|
+
* `<state>/unattended` is not a real folder of the user that nobody else can write to.
|
|
330
|
+
*/
|
|
331
|
+
export async function readUnattendedOverride(stateDir) {
|
|
332
|
+
const dir = await ownedFolder(stateDir, ['unattended'], { create: false }).catch(() => null);
|
|
333
|
+
return dir ? parseOverride(await readSmallJson(overrideFile(stateDir), 4096)) : undefined;
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* `<state>/unattended`, made (mode 700) when missing and narrowed to 700 when others can read it. Throws
|
|
337
|
+
* `UnsafeFolderError` (its message says what to do) when it is a link, someone else's, or writable by others: nothing
|
|
338
|
+
* of Unattended Mode is written through such a folder.
|
|
339
|
+
*/
|
|
340
|
+
export async function ensureUnattendedDir(stateDir) {
|
|
341
|
+
return (await ownedFolder(stateDir, ['unattended'], { create: true, private: true }));
|
|
342
|
+
}
|
|
343
|
+
/** Writes the manual state (mode 600, atomically). */
|
|
344
|
+
export async function writeUnattendedOverride(stateDir, override) {
|
|
345
|
+
await ensureUnattendedDir(stateDir);
|
|
346
|
+
await writeFileAtomic(overrideFile(stateDir), `${JSON.stringify(override)}\n`, 0o600);
|
|
347
|
+
}
|
|
348
|
+
/** Removes the manual state. */
|
|
349
|
+
export async function clearUnattendedOverride(stateDir) {
|
|
350
|
+
if (!(await ownedFolder(stateDir, ['unattended'], { create: false })))
|
|
351
|
+
return;
|
|
352
|
+
await rm(overrideFile(stateDir), { force: true });
|
|
353
|
+
}
|
|
354
|
+
/** A time as the user reads it: `08:00` today, `tomorrow 08:00`, else `Mon 08:00` (or in Chinese). */
|
|
355
|
+
export function clockText(at, now, zh = false) {
|
|
356
|
+
const d = new Date(at);
|
|
357
|
+
const hhmm = `${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}`;
|
|
358
|
+
const day = (x) => new Date(x.getFullYear(), x.getMonth(), x.getDate()).getTime();
|
|
359
|
+
const diff = Math.round((day(d) - day(new Date(now))) / DAY_MS);
|
|
360
|
+
if (diff === 0)
|
|
361
|
+
return hhmm;
|
|
362
|
+
if (diff === 1)
|
|
363
|
+
return zh ? `明天 ${hhmm}` : `tomorrow ${hhmm}`;
|
|
364
|
+
const names = zh
|
|
365
|
+
? ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
|
|
366
|
+
: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
|
|
367
|
+
const date = diff > 6 || diff < 0
|
|
368
|
+
? zh
|
|
369
|
+
? ` ${d.getMonth() + 1}月${d.getDate()}日`
|
|
370
|
+
: ` ${d.getMonth() + 1}/${d.getDate()}`
|
|
371
|
+
: '';
|
|
372
|
+
return `${names[d.getDay()]}${date} ${hhmm}`;
|
|
373
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import type { Env } from '../types.ts';
|
|
2
|
+
import type { Paths } from './paths.ts';
|
|
3
|
+
import { type SessionHookPart } from './session-hooks.ts';
|
|
4
|
+
import { type UnattendedOverride, type UnattendedSlot, type UnattendedState } from './unattended-state.ts';
|
|
5
|
+
/** Families whose sessions Unattended Mode covers in v1. */
|
|
6
|
+
export declare const UNATTENDED_FAMILIES: readonly string[];
|
|
7
|
+
/**
|
|
8
|
+
* covered: sessions sideby starts get the hook; needs-trust: they get it, and Codex runs it once the user trusts it in
|
|
9
|
+
* `/hooks`; not-covered: the Family has no hook sideby can add (Grok, pi), or the feature was never used.
|
|
10
|
+
*/
|
|
11
|
+
export type UnattendedCoverage = 'covered' | 'needs-trust' | 'not-covered';
|
|
12
|
+
/** Everything `sideby unattended --json` prints and the Panel shows. */
|
|
13
|
+
export interface UnattendedStatus extends UnattendedState {
|
|
14
|
+
/** The config has an `unattended` section, so sessions sideby starts get the hook. */
|
|
15
|
+
configured: boolean;
|
|
16
|
+
schedule: UnattendedSlot[];
|
|
17
|
+
/** Open Pending Decisions. */
|
|
18
|
+
pending: number;
|
|
19
|
+
coverage: Record<string, UnattendedCoverage>;
|
|
20
|
+
/** When `on --slot-end` would end: the end of the current or next Slot, when that is within 72 hours. */
|
|
21
|
+
slotEnd?: string;
|
|
22
|
+
/** The manual state in effect now: on until, or paused until. */
|
|
23
|
+
manual?: UnattendedOverride;
|
|
24
|
+
/**
|
|
25
|
+
* Why sideby cannot keep its state: `<state>/unattended` is a link, someone else's, or others can write to it. The
|
|
26
|
+
* message says what to do; until then the manual state and Pending Decisions are neither read nor written.
|
|
27
|
+
*/
|
|
28
|
+
problem?: string;
|
|
29
|
+
}
|
|
30
|
+
/** What a change did, and the command that puts back what was there. */
|
|
31
|
+
export interface UnattendedChange {
|
|
32
|
+
changed: boolean;
|
|
33
|
+
status: UnattendedStatus;
|
|
34
|
+
undo?: string;
|
|
35
|
+
}
|
|
36
|
+
/** The Weeknights preset: Monday to Friday, 23:00 to 08:00. */
|
|
37
|
+
export declare const WEEKNIGHTS: UnattendedSlot;
|
|
38
|
+
/** The full status. `env` matters only for a session started with `--unattended`. */
|
|
39
|
+
export declare function unattendedStatus(opts: {
|
|
40
|
+
paths: Paths;
|
|
41
|
+
families: readonly string[];
|
|
42
|
+
env?: Env;
|
|
43
|
+
now?: Date;
|
|
44
|
+
/** Only read: old done Pending Decisions are not deleted (a read-only Panel). */
|
|
45
|
+
readOnly?: boolean;
|
|
46
|
+
}): Promise<UnattendedStatus>;
|
|
47
|
+
/** How long to turn it on for, as the CLI and the Panel take it. At most one of the three. */
|
|
48
|
+
export interface OnChoice {
|
|
49
|
+
/** `4h`, `90m` or `1h30m`. */
|
|
50
|
+
for?: string;
|
|
51
|
+
/** `07:30` (the next time it is 07:30) or an ISO date and time. */
|
|
52
|
+
until?: string;
|
|
53
|
+
/** Until the current or next Slot ends. */
|
|
54
|
+
slotEnd?: boolean;
|
|
55
|
+
}
|
|
56
|
+
/** `4h`, `90m`, `1h30m` in milliseconds; undefined for anything else. */
|
|
57
|
+
export declare function parseDuration(text: string): number | undefined;
|
|
58
|
+
/**
|
|
59
|
+
* When a manual "on" would end, or why it cannot (the message says what to do). Without a choice: until the current
|
|
60
|
+
* or next Slot ends when that is within 72 hours, else 12 hours.
|
|
61
|
+
*/
|
|
62
|
+
export declare function onUntil(choice: OnChoice, schedule: readonly UnattendedSlot[], now: Date): {
|
|
63
|
+
until: number;
|
|
64
|
+
} | {
|
|
65
|
+
error: string;
|
|
66
|
+
};
|
|
67
|
+
/** Makes the config section, which starts adding the hook to sessions sideby starts. */
|
|
68
|
+
export declare function ensureUnattendedConfig(configFile: string): Promise<void>;
|
|
69
|
+
/**
|
|
70
|
+
* Turns it on until `until` (at most 72 hours), creating the config section the first time. A Slot that ends later
|
|
71
|
+
* keeps it on longer. Callers then run `checkUnattendedEnded`, so a stretch this ends is noticed at once.
|
|
72
|
+
*/
|
|
73
|
+
export declare function turnUnattendedOn(opts: {
|
|
74
|
+
paths: Paths;
|
|
75
|
+
families: readonly string[];
|
|
76
|
+
until: number;
|
|
77
|
+
now?: Date;
|
|
78
|
+
}): Promise<UnattendedChange>;
|
|
79
|
+
/**
|
|
80
|
+
* Turns it off: outside a Slot it is off at once; in a Slot it pauses until that Slot ends, and the next Slot turns it
|
|
81
|
+
* on as usual. Callers then run `checkUnattendedEnded`.
|
|
82
|
+
*/
|
|
83
|
+
export declare function turnUnattendedOff(opts: {
|
|
84
|
+
paths: Paths;
|
|
85
|
+
families: readonly string[];
|
|
86
|
+
now?: Date;
|
|
87
|
+
}): Promise<UnattendedChange>;
|
|
88
|
+
/**
|
|
89
|
+
* Adds a Slot (at position `at`, 1-based, else last), creating the config section. A Slot with the same days and
|
|
90
|
+
* times is not added twice.
|
|
91
|
+
*/
|
|
92
|
+
export declare function addUnattendedSlot(opts: {
|
|
93
|
+
paths: Paths;
|
|
94
|
+
families: readonly string[];
|
|
95
|
+
slot: UnattendedSlot;
|
|
96
|
+
at?: number;
|
|
97
|
+
now?: Date;
|
|
98
|
+
}): Promise<UnattendedChange>;
|
|
99
|
+
/** Removes Slot `n` (1-based, as `sideby unattended` numbers them). Throws when there is none. */
|
|
100
|
+
export declare function removeUnattendedSlot(opts: {
|
|
101
|
+
paths: Paths;
|
|
102
|
+
families: readonly string[];
|
|
103
|
+
n: number;
|
|
104
|
+
now?: Date;
|
|
105
|
+
}): Promise<UnattendedChange>;
|
|
106
|
+
/**
|
|
107
|
+
* Replaces the whole Schedule (the Panel's editor writes each change this way), creating the config section. Every
|
|
108
|
+
* Slot is checked as the config schema does; a bad one is refused with what to fix. `previous` is the Schedule it
|
|
109
|
+
* replaced, which the Panel's Undo writes back.
|
|
110
|
+
*/
|
|
111
|
+
export declare function setUnattendedSchedule(opts: {
|
|
112
|
+
paths: Paths;
|
|
113
|
+
families: readonly string[];
|
|
114
|
+
schedule: readonly unknown[];
|
|
115
|
+
now?: Date;
|
|
116
|
+
}): Promise<UnattendedChange & {
|
|
117
|
+
previous: UnattendedSlot[];
|
|
118
|
+
}>;
|
|
119
|
+
/** The preset names `schedule preset` takes, with what each adds. */
|
|
120
|
+
export declare const UNATTENDED_PRESETS: Readonly<Record<string, UnattendedSlot>>;
|
|
121
|
+
/** The Slot `schedule add` was given, or why it cannot be one (the message says what to do). */
|
|
122
|
+
export declare function slotFromOptions(o: {
|
|
123
|
+
days?: string;
|
|
124
|
+
from?: string;
|
|
125
|
+
to?: string;
|
|
126
|
+
name?: string;
|
|
127
|
+
}): UnattendedSlot | {
|
|
128
|
+
error: string;
|
|
129
|
+
};
|
|
130
|
+
/** How a time reads in the CLI (`08:00`, `tomorrow 08:00`, `Mon 08:00`). */
|
|
131
|
+
export declare const unattendedClock: (iso: string, now: Date, zh?: boolean) => string;
|
|
132
|
+
/**
|
|
133
|
+
* Unattended Mode's session hooks: before the ask tool in Claude and Codex, and on every permission request Claude
|
|
134
|
+
* Code is about to show (Codex has no such event for it; its own approval prompts are not covered).
|
|
135
|
+
*/
|
|
136
|
+
export declare function unattendedHookPart(): SessionHookPart;
|
|
137
|
+
/** The line a Claude Launch prints when the user's own `--settings` leaves no room for the hook. */
|
|
138
|
+
export declare function unattendedSettingsLine(env: Env): string;
|
|
139
|
+
/** The line a Launch prints when `--unattended` asks for a Family Unattended Mode does not cover. */
|
|
140
|
+
export declare function unattendedFamilyLine(family: string, env: Env): string;
|
|
141
|
+
/** Once per Codex Account: Codex runs the hook only after the user trusts it in `/hooks`. */
|
|
142
|
+
export declare function codexUnattendedTrustLine(stateDir: string, ref: string, env: Env): Promise<string | undefined>;
|