@tintinweb/pi-subagents 0.6.3 → 0.7.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/CHANGELOG.md +22 -0
- package/README.md +54 -10
- package/dist/agent-manager.d.ts +23 -1
- package/dist/agent-manager.js +33 -2
- package/dist/agent-runner.d.ts +27 -0
- package/dist/agent-runner.js +28 -4
- package/dist/index.js +199 -50
- package/dist/schedule-store.d.ts +36 -0
- package/dist/schedule-store.js +144 -0
- package/dist/schedule.d.ts +109 -0
- package/dist/schedule.js +338 -0
- package/dist/settings.d.ts +10 -0
- package/dist/settings.js +5 -0
- package/dist/types.d.ts +46 -0
- package/dist/ui/agent-widget.d.ts +15 -8
- package/dist/ui/agent-widget.js +28 -7
- package/dist/ui/conversation-viewer.js +6 -8
- package/dist/ui/schedule-menu.d.ts +16 -0
- package/dist/ui/schedule-menu.js +95 -0
- package/dist/usage.d.ts +50 -0
- package/dist/usage.js +49 -0
- package/package.json +10 -6
- package/src/agent-manager.ts +55 -2
- package/src/agent-runner.ts +43 -5
- package/src/index.ts +207 -41
- package/src/schedule-store.ts +143 -0
- package/src/schedule.ts +365 -0
- package/src/settings.ts +14 -0
- package/src/types.ts +52 -0
- package/src/ui/agent-widget.ts +36 -6
- package/src/ui/conversation-viewer.ts +6 -6
- package/src/ui/schedule-menu.ts +104 -0
- package/src/usage.ts +60 -0
- package/.github/workflows/ci.yml +0 -21
- package/biome.json +0 -26
- package/dist/ui/conversation-viewer.test.d.ts +0 -1
- package/dist/ui/conversation-viewer.test.js +0 -254
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* schedule.ts — `SubagentScheduler`: timer-driven dispatcher of scheduled subagents.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors the engine shape of pi-cron-schedule/src/scheduler.ts:
|
|
5
|
+
* - two-Map split (jobs = croner Cron, intervals = setInterval/setTimeout)
|
|
6
|
+
* - addJob/removeJob/updateJob/scheduleJob/unscheduleJob/executeJob
|
|
7
|
+
* - static parsers for cron / "+10m" / "5m" / ISO formats
|
|
8
|
+
*
|
|
9
|
+
* Differences vs pi-cron-schedule:
|
|
10
|
+
* - Persistence is via ScheduleStore (PID-locked, session-scoped, atomic).
|
|
11
|
+
* - `executeJob` calls `manager.spawn(..., { bypassQueue: true })` instead
|
|
12
|
+
* of dispatching a user message — schedule fires bypass maxConcurrent so
|
|
13
|
+
* a 5-minute interval can't be deferred behind 4 long-running agents.
|
|
14
|
+
* - Result delivery is implicit: spawn → background completion → existing
|
|
15
|
+
* `subagent-notification` followUp path. No new delivery code.
|
|
16
|
+
*/
|
|
17
|
+
import type { ExtensionAPI, ExtensionContext } from "@mariozechner/pi-coding-agent";
|
|
18
|
+
import type { AgentManager } from "./agent-manager.js";
|
|
19
|
+
import type { ScheduleStore } from "./schedule-store.js";
|
|
20
|
+
import type { IsolationMode, ScheduledSubagent, SubagentType, ThinkingLevel } from "./types.js";
|
|
21
|
+
/** Event emitted on `pi.events` for cross-extension consumers. */
|
|
22
|
+
export type ScheduleChangeEvent = {
|
|
23
|
+
type: "added";
|
|
24
|
+
job: ScheduledSubagent;
|
|
25
|
+
} | {
|
|
26
|
+
type: "removed";
|
|
27
|
+
jobId: string;
|
|
28
|
+
} | {
|
|
29
|
+
type: "updated";
|
|
30
|
+
job: ScheduledSubagent;
|
|
31
|
+
} | {
|
|
32
|
+
type: "fired";
|
|
33
|
+
jobId: string;
|
|
34
|
+
agentId: string;
|
|
35
|
+
name: string;
|
|
36
|
+
} | {
|
|
37
|
+
type: "error";
|
|
38
|
+
jobId: string;
|
|
39
|
+
error: string;
|
|
40
|
+
};
|
|
41
|
+
/** Params accepted at job creation — ID, timestamps, and state are derived. */
|
|
42
|
+
export interface NewJobInput {
|
|
43
|
+
name: string;
|
|
44
|
+
description: string;
|
|
45
|
+
schedule: string;
|
|
46
|
+
subagent_type: SubagentType;
|
|
47
|
+
prompt: string;
|
|
48
|
+
model?: string;
|
|
49
|
+
thinking?: ThinkingLevel;
|
|
50
|
+
max_turns?: number;
|
|
51
|
+
isolated?: boolean;
|
|
52
|
+
isolation?: IsolationMode;
|
|
53
|
+
}
|
|
54
|
+
export declare class SubagentScheduler {
|
|
55
|
+
private jobs;
|
|
56
|
+
private intervals;
|
|
57
|
+
private store;
|
|
58
|
+
private pi;
|
|
59
|
+
private ctx;
|
|
60
|
+
private manager;
|
|
61
|
+
/** Start the scheduler: bind to a session's store and arm enabled jobs. */
|
|
62
|
+
start(pi: ExtensionAPI, ctx: ExtensionContext, manager: AgentManager, store: ScheduleStore): void;
|
|
63
|
+
/** Stop all timers; drop refs. Safe to call repeatedly. */
|
|
64
|
+
stop(): void;
|
|
65
|
+
/** True if start() has bound a store and the scheduler is active. */
|
|
66
|
+
isActive(): boolean;
|
|
67
|
+
list(): ScheduledSubagent[];
|
|
68
|
+
/**
|
|
69
|
+
* Build a `ScheduledSubagent` from user input. Validates the schedule
|
|
70
|
+
* format and tags `scheduleType`. Throws on invalid input.
|
|
71
|
+
*/
|
|
72
|
+
buildJob(input: NewJobInput): ScheduledSubagent;
|
|
73
|
+
/** Add a job, persist, and arm if enabled. Returns the stored job. */
|
|
74
|
+
addJob(input: NewJobInput): ScheduledSubagent;
|
|
75
|
+
removeJob(id: string): boolean;
|
|
76
|
+
/** Toggle / mutate a job. Re-arms based on the new `enabled` state. */
|
|
77
|
+
updateJob(id: string, patch: Partial<ScheduledSubagent>): ScheduledSubagent | undefined;
|
|
78
|
+
/** Next-run time as ISO, or undefined if not currently armed. */
|
|
79
|
+
getNextRun(jobId: string): string | undefined;
|
|
80
|
+
private scheduleJob;
|
|
81
|
+
private unscheduleJob;
|
|
82
|
+
/**
|
|
83
|
+
* Fire a job: persist running state, spawn (bypassing the concurrency
|
|
84
|
+
* queue), persist completion. Fire-and-forget: the timer tick returns
|
|
85
|
+
* immediately so other jobs keep firing.
|
|
86
|
+
*/
|
|
87
|
+
private executeJob;
|
|
88
|
+
private emit;
|
|
89
|
+
private requireStore;
|
|
90
|
+
/**
|
|
91
|
+
* Sniff a schedule string and tag its type. Throws on invalid input.
|
|
92
|
+
* Order matters: relative ("+10m") and interval ("5m") both match digit+unit;
|
|
93
|
+
* relative requires the leading "+" to disambiguate.
|
|
94
|
+
*/
|
|
95
|
+
static detectSchedule(s: string): {
|
|
96
|
+
type: "cron" | "once" | "interval";
|
|
97
|
+
intervalMs?: number;
|
|
98
|
+
normalized: string;
|
|
99
|
+
};
|
|
100
|
+
/** 6-field cron — 'second minute hour dom month dow'. */
|
|
101
|
+
static validateCronExpression(expr: string): {
|
|
102
|
+
valid: boolean;
|
|
103
|
+
error?: string;
|
|
104
|
+
};
|
|
105
|
+
/** "+10s"/"+5m"/"+1h"/"+2d" → ISO timestamp. */
|
|
106
|
+
static parseRelativeTime(s: string): string | null;
|
|
107
|
+
/** "10s"/"5m"/"1h"/"2d" → milliseconds. */
|
|
108
|
+
static parseInterval(s: string): number | null;
|
|
109
|
+
}
|
package/dist/schedule.js
ADDED
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* schedule.ts — `SubagentScheduler`: timer-driven dispatcher of scheduled subagents.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors the engine shape of pi-cron-schedule/src/scheduler.ts:
|
|
5
|
+
* - two-Map split (jobs = croner Cron, intervals = setInterval/setTimeout)
|
|
6
|
+
* - addJob/removeJob/updateJob/scheduleJob/unscheduleJob/executeJob
|
|
7
|
+
* - static parsers for cron / "+10m" / "5m" / ISO formats
|
|
8
|
+
*
|
|
9
|
+
* Differences vs pi-cron-schedule:
|
|
10
|
+
* - Persistence is via ScheduleStore (PID-locked, session-scoped, atomic).
|
|
11
|
+
* - `executeJob` calls `manager.spawn(..., { bypassQueue: true })` instead
|
|
12
|
+
* of dispatching a user message — schedule fires bypass maxConcurrent so
|
|
13
|
+
* a 5-minute interval can't be deferred behind 4 long-running agents.
|
|
14
|
+
* - Result delivery is implicit: spawn → background completion → existing
|
|
15
|
+
* `subagent-notification` followUp path. No new delivery code.
|
|
16
|
+
*/
|
|
17
|
+
import { Cron } from "croner";
|
|
18
|
+
import { nanoid } from "nanoid";
|
|
19
|
+
import { resolveModel } from "./model-resolver.js";
|
|
20
|
+
export class SubagentScheduler {
|
|
21
|
+
jobs = new Map();
|
|
22
|
+
intervals = new Map();
|
|
23
|
+
store;
|
|
24
|
+
pi;
|
|
25
|
+
ctx;
|
|
26
|
+
manager;
|
|
27
|
+
/** Start the scheduler: bind to a session's store and arm enabled jobs. */
|
|
28
|
+
start(pi, ctx, manager, store) {
|
|
29
|
+
this.pi = pi;
|
|
30
|
+
this.ctx = ctx;
|
|
31
|
+
this.manager = manager;
|
|
32
|
+
this.store = store;
|
|
33
|
+
for (const job of store.list()) {
|
|
34
|
+
if (job.enabled)
|
|
35
|
+
this.scheduleJob(job);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/** Stop all timers; drop refs. Safe to call repeatedly. */
|
|
39
|
+
stop() {
|
|
40
|
+
for (const cron of this.jobs.values())
|
|
41
|
+
cron.stop();
|
|
42
|
+
this.jobs.clear();
|
|
43
|
+
for (const t of this.intervals.values())
|
|
44
|
+
clearTimeout(t);
|
|
45
|
+
this.intervals.clear();
|
|
46
|
+
this.store = undefined;
|
|
47
|
+
this.pi = undefined;
|
|
48
|
+
this.ctx = undefined;
|
|
49
|
+
this.manager = undefined;
|
|
50
|
+
}
|
|
51
|
+
/** True if start() has bound a store and the scheduler is active. */
|
|
52
|
+
isActive() {
|
|
53
|
+
return this.store !== undefined;
|
|
54
|
+
}
|
|
55
|
+
list() {
|
|
56
|
+
return this.store?.list() ?? [];
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Build a `ScheduledSubagent` from user input. Validates the schedule
|
|
60
|
+
* format and tags `scheduleType`. Throws on invalid input.
|
|
61
|
+
*/
|
|
62
|
+
buildJob(input) {
|
|
63
|
+
const detected = SubagentScheduler.detectSchedule(input.schedule);
|
|
64
|
+
return {
|
|
65
|
+
id: nanoid(10),
|
|
66
|
+
name: input.name,
|
|
67
|
+
description: input.description,
|
|
68
|
+
schedule: detected.normalized,
|
|
69
|
+
scheduleType: detected.type,
|
|
70
|
+
intervalMs: detected.intervalMs,
|
|
71
|
+
subagent_type: input.subagent_type,
|
|
72
|
+
prompt: input.prompt,
|
|
73
|
+
model: input.model,
|
|
74
|
+
thinking: input.thinking,
|
|
75
|
+
max_turns: input.max_turns,
|
|
76
|
+
isolated: input.isolated,
|
|
77
|
+
isolation: input.isolation,
|
|
78
|
+
enabled: true,
|
|
79
|
+
createdAt: new Date().toISOString(),
|
|
80
|
+
runCount: 0,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/** Add a job, persist, and arm if enabled. Returns the stored job. */
|
|
84
|
+
addJob(input) {
|
|
85
|
+
const store = this.requireStore();
|
|
86
|
+
if (store.hasName(input.name)) {
|
|
87
|
+
throw new Error(`A scheduled job named "${input.name}" already exists.`);
|
|
88
|
+
}
|
|
89
|
+
const job = this.buildJob(input);
|
|
90
|
+
store.add(job);
|
|
91
|
+
if (job.enabled)
|
|
92
|
+
this.scheduleJob(job);
|
|
93
|
+
this.emit({ type: "added", job });
|
|
94
|
+
return job;
|
|
95
|
+
}
|
|
96
|
+
removeJob(id) {
|
|
97
|
+
const store = this.requireStore();
|
|
98
|
+
if (!store.get(id))
|
|
99
|
+
return false;
|
|
100
|
+
this.unscheduleJob(id);
|
|
101
|
+
const ok = store.remove(id);
|
|
102
|
+
if (ok)
|
|
103
|
+
this.emit({ type: "removed", jobId: id });
|
|
104
|
+
return ok;
|
|
105
|
+
}
|
|
106
|
+
/** Toggle / mutate a job. Re-arms based on the new `enabled` state. */
|
|
107
|
+
updateJob(id, patch) {
|
|
108
|
+
const store = this.requireStore();
|
|
109
|
+
const updated = store.update(id, patch);
|
|
110
|
+
if (!updated)
|
|
111
|
+
return undefined;
|
|
112
|
+
this.unscheduleJob(id);
|
|
113
|
+
if (updated.enabled)
|
|
114
|
+
this.scheduleJob(updated);
|
|
115
|
+
this.emit({ type: "updated", job: updated });
|
|
116
|
+
return updated;
|
|
117
|
+
}
|
|
118
|
+
/** Next-run time as ISO, or undefined if not currently armed. */
|
|
119
|
+
getNextRun(jobId) {
|
|
120
|
+
const cron = this.jobs.get(jobId);
|
|
121
|
+
if (cron)
|
|
122
|
+
return cron.nextRun()?.toISOString();
|
|
123
|
+
const job = this.store?.get(jobId);
|
|
124
|
+
if (!job?.enabled)
|
|
125
|
+
return undefined;
|
|
126
|
+
if (job.scheduleType === "once")
|
|
127
|
+
return job.schedule;
|
|
128
|
+
if (job.scheduleType === "interval" && job.intervalMs) {
|
|
129
|
+
// Before the first fire there's no `lastRun`, so fall back to "now" —
|
|
130
|
+
// accurate at create time (setInterval was just armed) and within
|
|
131
|
+
// intervalMs of correct in any pre-first-fire view.
|
|
132
|
+
const base = job.lastRun ? new Date(job.lastRun).getTime() : Date.now();
|
|
133
|
+
return new Date(base + job.intervalMs).toISOString();
|
|
134
|
+
}
|
|
135
|
+
return undefined;
|
|
136
|
+
}
|
|
137
|
+
// ── Scheduling primitives ────────────────────────────────────────────
|
|
138
|
+
scheduleJob(job) {
|
|
139
|
+
const store = this.store;
|
|
140
|
+
if (!store)
|
|
141
|
+
return;
|
|
142
|
+
try {
|
|
143
|
+
if (job.scheduleType === "interval" && job.intervalMs) {
|
|
144
|
+
const t = setInterval(() => this.executeJob(job.id), job.intervalMs);
|
|
145
|
+
this.intervals.set(job.id, t);
|
|
146
|
+
}
|
|
147
|
+
else if (job.scheduleType === "once") {
|
|
148
|
+
const target = new Date(job.schedule).getTime();
|
|
149
|
+
const delay = target - Date.now();
|
|
150
|
+
if (delay > 0) {
|
|
151
|
+
const t = setTimeout(() => {
|
|
152
|
+
this.executeJob(job.id);
|
|
153
|
+
// Auto-disable one-shots after they fire (mirrors pi-cron-schedule)
|
|
154
|
+
store.update(job.id, { enabled: false });
|
|
155
|
+
const updated = store.get(job.id);
|
|
156
|
+
if (updated)
|
|
157
|
+
this.emit({ type: "updated", job: updated });
|
|
158
|
+
}, delay);
|
|
159
|
+
this.intervals.set(job.id, t);
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
// Past timestamp — disable, mark error, never fire
|
|
163
|
+
store.update(job.id, { enabled: false, lastStatus: "error" });
|
|
164
|
+
this.emit({ type: "error", jobId: job.id, error: `Scheduled time ${job.schedule} is in the past` });
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
const cron = new Cron(job.schedule, () => this.executeJob(job.id));
|
|
169
|
+
this.jobs.set(job.id, cron);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
catch (err) {
|
|
173
|
+
this.emit({ type: "error", jobId: job.id, error: err instanceof Error ? err.message : String(err) });
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
unscheduleJob(id) {
|
|
177
|
+
const cron = this.jobs.get(id);
|
|
178
|
+
if (cron) {
|
|
179
|
+
cron.stop();
|
|
180
|
+
this.jobs.delete(id);
|
|
181
|
+
}
|
|
182
|
+
const t = this.intervals.get(id);
|
|
183
|
+
if (t) {
|
|
184
|
+
clearTimeout(t);
|
|
185
|
+
clearInterval(t);
|
|
186
|
+
this.intervals.delete(id);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Fire a job: persist running state, spawn (bypassing the concurrency
|
|
191
|
+
* queue), persist completion. Fire-and-forget: the timer tick returns
|
|
192
|
+
* immediately so other jobs keep firing.
|
|
193
|
+
*/
|
|
194
|
+
executeJob(id) {
|
|
195
|
+
const store = this.store;
|
|
196
|
+
const pi = this.pi;
|
|
197
|
+
const ctx = this.ctx;
|
|
198
|
+
const manager = this.manager;
|
|
199
|
+
if (!store || !pi || !ctx || !manager)
|
|
200
|
+
return;
|
|
201
|
+
const job = store.get(id);
|
|
202
|
+
if (!job?.enabled)
|
|
203
|
+
return;
|
|
204
|
+
store.update(id, { lastStatus: "running" });
|
|
205
|
+
// Resolve model at fire time — registry contents may have changed since the
|
|
206
|
+
// job was created (auth added/removed). Fall back silently to spawn-default
|
|
207
|
+
// if resolution fails; the spawn path handles undefined model gracefully.
|
|
208
|
+
let resolvedModel;
|
|
209
|
+
if (job.model) {
|
|
210
|
+
const r = resolveModel(job.model, ctx.modelRegistry);
|
|
211
|
+
if (typeof r !== "string")
|
|
212
|
+
resolvedModel = r;
|
|
213
|
+
}
|
|
214
|
+
let agentId;
|
|
215
|
+
try {
|
|
216
|
+
agentId = manager.spawn(pi, ctx, job.subagent_type, job.prompt, {
|
|
217
|
+
description: job.description,
|
|
218
|
+
isBackground: true,
|
|
219
|
+
bypassQueue: true,
|
|
220
|
+
model: resolvedModel,
|
|
221
|
+
maxTurns: job.max_turns,
|
|
222
|
+
isolated: job.isolated,
|
|
223
|
+
thinkingLevel: job.thinking,
|
|
224
|
+
isolation: job.isolation,
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
catch (err) {
|
|
228
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
229
|
+
store.update(id, { lastRun: new Date().toISOString(), lastStatus: "error" });
|
|
230
|
+
this.emit({ type: "error", jobId: id, error });
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
this.emit({ type: "fired", jobId: id, agentId, name: job.name });
|
|
234
|
+
const record = manager.getRecord(agentId);
|
|
235
|
+
const finalize = (status) => {
|
|
236
|
+
const next = this.getNextRun(id);
|
|
237
|
+
const current = store.get(id);
|
|
238
|
+
store.update(id, {
|
|
239
|
+
lastRun: new Date().toISOString(),
|
|
240
|
+
lastStatus: status,
|
|
241
|
+
runCount: (current?.runCount ?? 0) + 1,
|
|
242
|
+
nextRun: next,
|
|
243
|
+
});
|
|
244
|
+
};
|
|
245
|
+
// AgentManager's promise resolves either way (its .catch returns ""), so we
|
|
246
|
+
// can't infer success/failure from the promise — read record.status instead.
|
|
247
|
+
// Terminal states: completed/steered = success; error/aborted/stopped = error.
|
|
248
|
+
if (record?.promise) {
|
|
249
|
+
record.promise
|
|
250
|
+
.then(() => {
|
|
251
|
+
const r = manager.getRecord(agentId);
|
|
252
|
+
const failed = r?.status === "error" || r?.status === "aborted" || r?.status === "stopped";
|
|
253
|
+
finalize(failed ? "error" : "success");
|
|
254
|
+
})
|
|
255
|
+
.catch(() => finalize("error"));
|
|
256
|
+
}
|
|
257
|
+
else {
|
|
258
|
+
// Spawn returned without a promise (defensive — bypassQueue path always sets one).
|
|
259
|
+
finalize("success");
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
emit(event) {
|
|
263
|
+
if (this.pi)
|
|
264
|
+
this.pi.events.emit("subagents:scheduled", event);
|
|
265
|
+
}
|
|
266
|
+
requireStore() {
|
|
267
|
+
if (!this.store)
|
|
268
|
+
throw new Error("Scheduler not started — no active session.");
|
|
269
|
+
return this.store;
|
|
270
|
+
}
|
|
271
|
+
// ── Format detection / parsers (statics — pure) ──────────────────────
|
|
272
|
+
/**
|
|
273
|
+
* Sniff a schedule string and tag its type. Throws on invalid input.
|
|
274
|
+
* Order matters: relative ("+10m") and interval ("5m") both match digit+unit;
|
|
275
|
+
* relative requires the leading "+" to disambiguate.
|
|
276
|
+
*/
|
|
277
|
+
static detectSchedule(s) {
|
|
278
|
+
const trimmed = s.trim();
|
|
279
|
+
// "+10m" — relative one-shot
|
|
280
|
+
const rel = SubagentScheduler.parseRelativeTime(trimmed);
|
|
281
|
+
if (rel !== null)
|
|
282
|
+
return { type: "once", normalized: rel };
|
|
283
|
+
// "5m" — interval
|
|
284
|
+
const ivl = SubagentScheduler.parseInterval(trimmed);
|
|
285
|
+
if (ivl !== null)
|
|
286
|
+
return { type: "interval", intervalMs: ivl, normalized: trimmed };
|
|
287
|
+
// ISO timestamp — one-shot. Reject past timestamps upfront so we never
|
|
288
|
+
// create a dead-on-arrival record (scheduleJob's safety net still catches
|
|
289
|
+
// micro-races from `+0s`-style relatives).
|
|
290
|
+
if (/^\d{4}-\d{2}-\d{2}T/.test(trimmed)) {
|
|
291
|
+
const d = new Date(trimmed);
|
|
292
|
+
if (!Number.isNaN(d.getTime())) {
|
|
293
|
+
if (d.getTime() <= Date.now()) {
|
|
294
|
+
throw new Error(`Scheduled time ${d.toISOString()} is in the past.`);
|
|
295
|
+
}
|
|
296
|
+
return { type: "once", normalized: d.toISOString() };
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
// Cron — 6-field
|
|
300
|
+
const cronCheck = SubagentScheduler.validateCronExpression(trimmed);
|
|
301
|
+
if (cronCheck.valid)
|
|
302
|
+
return { type: "cron", normalized: trimmed };
|
|
303
|
+
throw new Error(`Invalid schedule "${s}". Use 6-field cron (e.g. "0 0 9 * * 1" — 9am every Monday), interval ("5m"/"1h"), or one-shot ("+10m" / ISO).`);
|
|
304
|
+
}
|
|
305
|
+
/** 6-field cron — 'second minute hour dom month dow'. */
|
|
306
|
+
static validateCronExpression(expr) {
|
|
307
|
+
const fields = expr.trim().split(/\s+/);
|
|
308
|
+
if (fields.length !== 6) {
|
|
309
|
+
return {
|
|
310
|
+
valid: false,
|
|
311
|
+
error: `Cron must have 6 fields (second minute hour dom month dow), got ${fields.length}. Example: "0 0 9 * * 1" for 9am every Monday.`,
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
try {
|
|
315
|
+
// Croner validates by construction.
|
|
316
|
+
new Cron(expr, () => { });
|
|
317
|
+
return { valid: true };
|
|
318
|
+
}
|
|
319
|
+
catch (e) {
|
|
320
|
+
return { valid: false, error: e instanceof Error ? e.message : "Invalid cron expression" };
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
/** "+10s"/"+5m"/"+1h"/"+2d" → ISO timestamp. */
|
|
324
|
+
static parseRelativeTime(s) {
|
|
325
|
+
const m = s.match(/^\+(\d+)(s|m|h|d)$/);
|
|
326
|
+
if (!m)
|
|
327
|
+
return null;
|
|
328
|
+
const ms = parseInt(m[1], 10) * { s: 1000, m: 60_000, h: 3_600_000, d: 86_400_000 }[m[2]];
|
|
329
|
+
return new Date(Date.now() + ms).toISOString();
|
|
330
|
+
}
|
|
331
|
+
/** "10s"/"5m"/"1h"/"2d" → milliseconds. */
|
|
332
|
+
static parseInterval(s) {
|
|
333
|
+
const m = s.match(/^(\d+)(s|m|h|d)$/);
|
|
334
|
+
if (!m)
|
|
335
|
+
return null;
|
|
336
|
+
return parseInt(m[1], 10) * { s: 1000, m: 60_000, h: 3_600_000, d: 86_400_000 }[m[2]];
|
|
337
|
+
}
|
|
338
|
+
}
|
package/dist/settings.d.ts
CHANGED
|
@@ -9,6 +9,15 @@ export interface SubagentsSettings {
|
|
|
9
9
|
defaultMaxTurns?: number;
|
|
10
10
|
graceTurns?: number;
|
|
11
11
|
defaultJoinMode?: JoinMode;
|
|
12
|
+
/**
|
|
13
|
+
* Master switch for the schedule subagent feature. Defaults to `true`.
|
|
14
|
+
* When `false`: the `Agent` tool's `schedule` param + its guideline are
|
|
15
|
+
* stripped from the tool spec at registration (zero LLM-context cost), the
|
|
16
|
+
* scheduler doesn't bind to the session, and the `/agents → Scheduled jobs`
|
|
17
|
+
* menu entry is hidden. Schema-level removal applies at extension load
|
|
18
|
+
* (next pi session); runtime menu/runtime-fire short-circuit is immediate.
|
|
19
|
+
*/
|
|
20
|
+
schedulingEnabled?: boolean;
|
|
12
21
|
}
|
|
13
22
|
/** Setter hooks used by applySettings to wire persisted values into in-memory state. */
|
|
14
23
|
export interface SettingsAppliers {
|
|
@@ -16,6 +25,7 @@ export interface SettingsAppliers {
|
|
|
16
25
|
setDefaultMaxTurns: (n: number) => void;
|
|
17
26
|
setGraceTurns: (n: number) => void;
|
|
18
27
|
setDefaultJoinMode: (mode: JoinMode) => void;
|
|
28
|
+
setSchedulingEnabled: (b: boolean) => void;
|
|
19
29
|
}
|
|
20
30
|
/** Emit callback — a subset of `pi.events.emit` to keep helpers testable. */
|
|
21
31
|
export type SettingsEmit = (event: string, payload: unknown) => void;
|
package/dist/settings.js
CHANGED
|
@@ -35,6 +35,9 @@ function sanitize(raw) {
|
|
|
35
35
|
if (typeof r.defaultJoinMode === "string" && VALID_JOIN_MODES.has(r.defaultJoinMode)) {
|
|
36
36
|
out.defaultJoinMode = r.defaultJoinMode;
|
|
37
37
|
}
|
|
38
|
+
if (typeof r.schedulingEnabled === "boolean") {
|
|
39
|
+
out.schedulingEnabled = r.schedulingEnabled;
|
|
40
|
+
}
|
|
38
41
|
return out;
|
|
39
42
|
}
|
|
40
43
|
function globalPath() {
|
|
@@ -90,6 +93,8 @@ export function applySettings(s, appliers) {
|
|
|
90
93
|
appliers.setGraceTurns(s.graceTurns);
|
|
91
94
|
if (s.defaultJoinMode)
|
|
92
95
|
appliers.setDefaultJoinMode(s.defaultJoinMode);
|
|
96
|
+
if (typeof s.schedulingEnabled === "boolean")
|
|
97
|
+
appliers.setSchedulingEnabled(s.schedulingEnabled);
|
|
93
98
|
}
|
|
94
99
|
/**
|
|
95
100
|
* Format the user-facing toast for a settings mutation. Pure function —
|
package/dist/types.d.ts
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import type { ThinkingLevel } from "@mariozechner/pi-agent-core";
|
|
5
5
|
import type { AgentSession } from "@mariozechner/pi-coding-agent";
|
|
6
|
+
import type { LifetimeUsage } from "./usage.js";
|
|
6
7
|
export type { ThinkingLevel };
|
|
7
8
|
/** Agent type: any string name (built-in defaults or user-defined). */
|
|
8
9
|
export type SubagentType = string;
|
|
@@ -82,6 +83,14 @@ export interface AgentRecord {
|
|
|
82
83
|
outputFile?: string;
|
|
83
84
|
/** Cleanup function for the output file stream subscription. */
|
|
84
85
|
outputCleanup?: () => void;
|
|
86
|
+
/**
|
|
87
|
+
* Lifetime usage breakdown, accumulated via `message_end` events. Survives
|
|
88
|
+
* compaction. Total = input + output + cacheWrite (cacheRead deliberately
|
|
89
|
+
* excluded — see issue #38). Initialized to zeros at spawn.
|
|
90
|
+
*/
|
|
91
|
+
lifetimeUsage: LifetimeUsage;
|
|
92
|
+
/** Number of times this agent's session has compacted. Initialized to 0 at spawn. */
|
|
93
|
+
compactionCount: number;
|
|
85
94
|
}
|
|
86
95
|
/** Details attached to custom notification messages for visual rendering. */
|
|
87
96
|
export interface NotificationDetails {
|
|
@@ -104,3 +113,40 @@ export interface EnvInfo {
|
|
|
104
113
|
branch: string;
|
|
105
114
|
platform: string;
|
|
106
115
|
}
|
|
116
|
+
/**
|
|
117
|
+
* A subagent spawn registered to fire on a schedule.
|
|
118
|
+
*
|
|
119
|
+
* Stored at `<cwd>/.pi/subagent-schedules/<sessionId>.json`. Session-scoped:
|
|
120
|
+
* survives `/resume` but resets on `/new`, mirroring pi-chonky-tasks.
|
|
121
|
+
*/
|
|
122
|
+
export interface ScheduledSubagent {
|
|
123
|
+
id: string;
|
|
124
|
+
/** Unique within store. Defaults to `description`. */
|
|
125
|
+
name: string;
|
|
126
|
+
description: string;
|
|
127
|
+
/** Raw user input — cron expr | "+10m" | ISO | "5m". */
|
|
128
|
+
schedule: string;
|
|
129
|
+
scheduleType: "cron" | "once" | "interval";
|
|
130
|
+
/** Computed at create time for interval/once. */
|
|
131
|
+
intervalMs?: number;
|
|
132
|
+
subagent_type: SubagentType;
|
|
133
|
+
prompt: string;
|
|
134
|
+
model?: string;
|
|
135
|
+
thinking?: ThinkingLevel;
|
|
136
|
+
max_turns?: number;
|
|
137
|
+
isolated?: boolean;
|
|
138
|
+
isolation?: IsolationMode;
|
|
139
|
+
enabled: boolean;
|
|
140
|
+
/** ISO timestamp. */
|
|
141
|
+
createdAt: string;
|
|
142
|
+
lastRun?: string;
|
|
143
|
+
lastStatus?: "success" | "error" | "running";
|
|
144
|
+
/** Refreshed on every fire and on store load. */
|
|
145
|
+
nextRun?: string;
|
|
146
|
+
runCount: number;
|
|
147
|
+
}
|
|
148
|
+
export interface ScheduleStoreData {
|
|
149
|
+
/** For future migrations. */
|
|
150
|
+
version: 1;
|
|
151
|
+
jobs: ScheduledSubagent[];
|
|
152
|
+
}
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import type { AgentManager } from "../agent-manager.js";
|
|
8
8
|
import type { SubagentType } from "../types.js";
|
|
9
|
+
import { type LifetimeUsage, type SessionLike } from "../usage.js";
|
|
9
10
|
/** Braille spinner frames for animated running indicator. */
|
|
10
11
|
export declare const SPINNER: string[];
|
|
11
12
|
/** Statuses that indicate an error/non-success outcome (used for linger behavior and icon rendering). */
|
|
@@ -27,19 +28,14 @@ export type UICtx = {
|
|
|
27
28
|
export interface AgentActivity {
|
|
28
29
|
activeTools: Map<string, string>;
|
|
29
30
|
toolUses: number;
|
|
30
|
-
tokens: string;
|
|
31
31
|
responseText: string;
|
|
32
|
-
session?:
|
|
33
|
-
getSessionStats(): {
|
|
34
|
-
tokens: {
|
|
35
|
-
total: number;
|
|
36
|
-
};
|
|
37
|
-
};
|
|
38
|
-
};
|
|
32
|
+
session?: SessionLike;
|
|
39
33
|
/** Current turn count. */
|
|
40
34
|
turnCount: number;
|
|
41
35
|
/** Effective max turns for this agent (undefined = unlimited). */
|
|
42
36
|
maxTurns?: number;
|
|
37
|
+
/** Lifetime usage breakdown — see LifetimeUsage docs. */
|
|
38
|
+
lifetimeUsage: LifetimeUsage;
|
|
43
39
|
}
|
|
44
40
|
/** Metadata attached to Agent tool results for custom rendering. */
|
|
45
41
|
export interface AgentDetails {
|
|
@@ -67,6 +63,17 @@ export interface AgentDetails {
|
|
|
67
63
|
}
|
|
68
64
|
/** Format a token count compactly: "33.8k token", "1.2M token". */
|
|
69
65
|
export declare function formatTokens(count: number): string;
|
|
66
|
+
/**
|
|
67
|
+
* Token count with optional context-fill % and compaction-count annotations.
|
|
68
|
+
* Thresholds for percent: <70% dim, 70–85% warning, ≥85% error.
|
|
69
|
+
* Compaction count rendered as `↻N` in dim.
|
|
70
|
+
*
|
|
71
|
+
* "12.3k token" — no annotations
|
|
72
|
+
* "12.3k token (45%)" — percent only
|
|
73
|
+
* "12.3k token (↻2)" — compactions only (e.g. right after compact)
|
|
74
|
+
* "12.3k token (45% · ↻2)" — both
|
|
75
|
+
*/
|
|
76
|
+
export declare function formatSessionTokens(tokens: number, percent: number | null, theme: Theme, compactions?: number): string;
|
|
70
77
|
/** Format turn count with optional max limit: "⟳5≤30" or "⟳5". */
|
|
71
78
|
export declare function formatTurns(turnCount: number, maxTurns?: number | null): string;
|
|
72
79
|
/** Format milliseconds as human-readable duration. */
|
package/dist/ui/agent-widget.js
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import { truncateToWidth } from "@mariozechner/pi-tui";
|
|
8
8
|
import { getConfig } from "../agent-types.js";
|
|
9
|
+
import { getLifetimeTotal, getSessionContextPercent } from "../usage.js";
|
|
9
10
|
// ---- Constants ----
|
|
10
11
|
/** Maximum number of rendered lines before overflow collapse kicks in. */
|
|
11
12
|
const MAX_WIDGET_LINES = 12;
|
|
@@ -32,6 +33,30 @@ export function formatTokens(count) {
|
|
|
32
33
|
return `${(count / 1_000).toFixed(1)}k token`;
|
|
33
34
|
return `${count} token`;
|
|
34
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Token count with optional context-fill % and compaction-count annotations.
|
|
38
|
+
* Thresholds for percent: <70% dim, 70–85% warning, ≥85% error.
|
|
39
|
+
* Compaction count rendered as `↻N` in dim.
|
|
40
|
+
*
|
|
41
|
+
* "12.3k token" — no annotations
|
|
42
|
+
* "12.3k token (45%)" — percent only
|
|
43
|
+
* "12.3k token (↻2)" — compactions only (e.g. right after compact)
|
|
44
|
+
* "12.3k token (45% · ↻2)" — both
|
|
45
|
+
*/
|
|
46
|
+
export function formatSessionTokens(tokens, percent, theme, compactions = 0) {
|
|
47
|
+
const tokenStr = formatTokens(tokens);
|
|
48
|
+
const annot = [];
|
|
49
|
+
if (percent !== null) {
|
|
50
|
+
const color = percent >= 85 ? "error" : percent >= 70 ? "warning" : "dim";
|
|
51
|
+
annot.push(theme.fg(color, `${Math.round(percent)}%`));
|
|
52
|
+
}
|
|
53
|
+
if (compactions > 0) {
|
|
54
|
+
annot.push(theme.fg("dim", `↻${compactions}`));
|
|
55
|
+
}
|
|
56
|
+
if (annot.length === 0)
|
|
57
|
+
return tokenStr;
|
|
58
|
+
return `${tokenStr} (${annot.join(" · ")})`;
|
|
59
|
+
}
|
|
35
60
|
/** Format turn count with optional max limit: "⟳5≤30" or "⟳5". */
|
|
36
61
|
export function formatTurns(turnCount, maxTurns) {
|
|
37
62
|
return maxTurns != null ? `⟳${turnCount}≤${maxTurns}` : `⟳${turnCount}`;
|
|
@@ -222,13 +247,9 @@ export class AgentWidget {
|
|
|
222
247
|
const elapsed = formatMs(Date.now() - a.startedAt);
|
|
223
248
|
const bg = this.agentActivity.get(a.id);
|
|
224
249
|
const toolUses = bg?.toolUses ?? a.toolUses;
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
tokenText = formatTokens(bg.session.getSessionStats().tokens.total);
|
|
229
|
-
}
|
|
230
|
-
catch { /* */ }
|
|
231
|
-
}
|
|
250
|
+
const tokens = getLifetimeTotal(bg?.lifetimeUsage);
|
|
251
|
+
const contextPercent = getSessionContextPercent(bg?.session);
|
|
252
|
+
const tokenText = tokens > 0 ? formatSessionTokens(tokens, contextPercent, theme, a.compactionCount) : "";
|
|
232
253
|
const parts = [];
|
|
233
254
|
if (bg)
|
|
234
255
|
parts.push(formatTurns(bg.turnCount, bg.maxTurns));
|