@tintinweb/pi-subagents 0.6.3 → 0.7.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/CHANGELOG.md +37 -0
- package/README.md +55 -11
- package/dist/agent-manager.d.ts +23 -1
- package/dist/agent-manager.js +71 -20
- package/dist/agent-runner.d.ts +27 -0
- package/dist/agent-runner.js +28 -4
- package/dist/index.js +236 -72
- 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 +90 -20
- package/src/agent-runner.ts +43 -5
- package/src/index.ts +239 -63
- 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
package/src/agent-runner.ts
CHANGED
|
@@ -103,6 +103,17 @@ export interface RunOptions {
|
|
|
103
103
|
onSessionCreated?: (session: AgentSession) => void;
|
|
104
104
|
/** Called at the end of each agentic turn with the cumulative count. */
|
|
105
105
|
onTurnEnd?: (turnCount: number) => void;
|
|
106
|
+
/**
|
|
107
|
+
* Called once per assistant message_end with that message's usage delta.
|
|
108
|
+
* Lets callers maintain a lifetime accumulator that survives compaction
|
|
109
|
+
* (which replaces session.state.messages and resets stats-derived sums).
|
|
110
|
+
*/
|
|
111
|
+
onAssistantUsage?: (usage: { input: number; output: number; cacheWrite: number }) => void;
|
|
112
|
+
/**
|
|
113
|
+
* Called when the session successfully compacts. `tokensBefore` is upstream's
|
|
114
|
+
* pre-compaction context size estimate. Aborted compactions don't fire.
|
|
115
|
+
*/
|
|
116
|
+
onCompaction?: (info: { reason: "manual" | "threshold" | "overflow"; tokensBefore: number }) => void;
|
|
106
117
|
}
|
|
107
118
|
|
|
108
119
|
export interface RunResult {
|
|
@@ -343,6 +354,17 @@ export async function runAgent(
|
|
|
343
354
|
if (event.type === "tool_execution_end") {
|
|
344
355
|
options.onToolActivity?.({ type: "end", toolName: event.toolName });
|
|
345
356
|
}
|
|
357
|
+
if (event.type === "message_end" && event.message.role === "assistant") {
|
|
358
|
+
const u = (event.message as any).usage;
|
|
359
|
+
if (u) options.onAssistantUsage?.({
|
|
360
|
+
input: u.input ?? 0,
|
|
361
|
+
output: u.output ?? 0,
|
|
362
|
+
cacheWrite: u.cacheWrite ?? 0,
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
if (event.type === "compaction_end" && !event.aborted && event.result) {
|
|
366
|
+
options.onCompaction?.({ reason: event.reason, tokensBefore: event.result.tokensBefore });
|
|
367
|
+
}
|
|
346
368
|
});
|
|
347
369
|
|
|
348
370
|
const collector = collectResponseText(session);
|
|
@@ -375,15 +397,31 @@ export async function runAgent(
|
|
|
375
397
|
export async function resumeAgent(
|
|
376
398
|
session: AgentSession,
|
|
377
399
|
prompt: string,
|
|
378
|
-
options: {
|
|
400
|
+
options: {
|
|
401
|
+
onToolActivity?: (activity: ToolActivity) => void;
|
|
402
|
+
onAssistantUsage?: (usage: { input: number; output: number; cacheWrite: number }) => void;
|
|
403
|
+
onCompaction?: (info: { reason: "manual" | "threshold" | "overflow"; tokensBefore: number }) => void;
|
|
404
|
+
signal?: AbortSignal;
|
|
405
|
+
} = {},
|
|
379
406
|
): Promise<string> {
|
|
380
407
|
const collector = collectResponseText(session);
|
|
381
408
|
const cleanupAbort = forwardAbortSignal(session, options.signal);
|
|
382
409
|
|
|
383
|
-
const
|
|
410
|
+
const unsubEvents = (options.onToolActivity || options.onAssistantUsage || options.onCompaction)
|
|
384
411
|
? session.subscribe((event: AgentSessionEvent) => {
|
|
385
|
-
if (event.type === "tool_execution_start") options.onToolActivity
|
|
386
|
-
if (event.type === "tool_execution_end") options.onToolActivity
|
|
412
|
+
if (event.type === "tool_execution_start") options.onToolActivity?.({ type: "start", toolName: event.toolName });
|
|
413
|
+
if (event.type === "tool_execution_end") options.onToolActivity?.({ type: "end", toolName: event.toolName });
|
|
414
|
+
if (event.type === "message_end" && event.message.role === "assistant") {
|
|
415
|
+
const u = (event.message as any).usage;
|
|
416
|
+
if (u) options.onAssistantUsage?.({
|
|
417
|
+
input: u.input ?? 0,
|
|
418
|
+
output: u.output ?? 0,
|
|
419
|
+
cacheWrite: u.cacheWrite ?? 0,
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
if (event.type === "compaction_end" && !event.aborted && event.result) {
|
|
423
|
+
options.onCompaction?.({ reason: event.reason, tokensBefore: event.result.tokensBefore });
|
|
424
|
+
}
|
|
387
425
|
})
|
|
388
426
|
: () => {};
|
|
389
427
|
|
|
@@ -391,7 +429,7 @@ export async function resumeAgent(
|
|
|
391
429
|
await session.prompt(prompt);
|
|
392
430
|
} finally {
|
|
393
431
|
collector.unsubscribe();
|
|
394
|
-
|
|
432
|
+
unsubEvents();
|
|
395
433
|
cleanupAbort();
|
|
396
434
|
}
|
|
397
435
|
|
package/src/index.ts
CHANGED
|
@@ -24,6 +24,8 @@ import { GroupJoinManager } from "./group-join.js";
|
|
|
24
24
|
import { resolveAgentInvocationConfig, resolveJoinMode } from "./invocation-config.js";
|
|
25
25
|
import { type ModelRegistry, resolveModel } from "./model-resolver.js";
|
|
26
26
|
import { createOutputFilePath, streamToOutputFile, writeInitialEntry } from "./output-file.js";
|
|
27
|
+
import { SubagentScheduler } from "./schedule.js";
|
|
28
|
+
import { resolveStorePath, ScheduleStore } from "./schedule-store.js";
|
|
27
29
|
import { applyAndEmitLoaded, type SubagentsSettings, saveAndEmitChanged } from "./settings.js";
|
|
28
30
|
import { type AgentConfig, type AgentRecord, type JoinMode, type NotificationDetails, type SubagentType } from "./types.js";
|
|
29
31
|
import {
|
|
@@ -40,6 +42,8 @@ import {
|
|
|
40
42
|
SPINNER,
|
|
41
43
|
type UICtx,
|
|
42
44
|
} from "./ui/agent-widget.js";
|
|
45
|
+
import { showSchedulesMenu } from "./ui/schedule-menu.js";
|
|
46
|
+
import { addUsage, getLifetimeTotal, getSessionContextPercent, type LifetimeUsage } from "./usage.js";
|
|
43
47
|
|
|
44
48
|
// ---- Shared helpers ----
|
|
45
49
|
|
|
@@ -48,10 +52,10 @@ function textResult(msg: string, details?: AgentDetails) {
|
|
|
48
52
|
return { content: [{ type: "text" as const, text: msg }], details: details as any };
|
|
49
53
|
}
|
|
50
54
|
|
|
51
|
-
/**
|
|
52
|
-
function
|
|
53
|
-
|
|
54
|
-
|
|
55
|
+
/** Format an agent's lifetime token total, or "" when zero. */
|
|
56
|
+
function formatLifetimeTokens(o: { lifetimeUsage: LifetimeUsage }): string {
|
|
57
|
+
const t = getLifetimeTotal(o.lifetimeUsage);
|
|
58
|
+
return t > 0 ? formatTokens(t) : "";
|
|
55
59
|
}
|
|
56
60
|
|
|
57
61
|
/**
|
|
@@ -59,7 +63,15 @@ function safeFormatTokens(session: { getSessionStats(): { tokens: { total: numbe
|
|
|
59
63
|
* Used by both foreground and background paths to avoid duplication.
|
|
60
64
|
*/
|
|
61
65
|
function createActivityTracker(maxTurns?: number, onStreamUpdate?: () => void) {
|
|
62
|
-
const state: AgentActivity = {
|
|
66
|
+
const state: AgentActivity = {
|
|
67
|
+
activeTools: new Map(),
|
|
68
|
+
toolUses: 0,
|
|
69
|
+
turnCount: 1,
|
|
70
|
+
maxTurns,
|
|
71
|
+
responseText: "",
|
|
72
|
+
session: undefined,
|
|
73
|
+
lifetimeUsage: { input: 0, output: 0, cacheWrite: 0 },
|
|
74
|
+
};
|
|
63
75
|
|
|
64
76
|
const callbacks = {
|
|
65
77
|
onToolActivity: (activity: { type: "start" | "end"; toolName: string }) => {
|
|
@@ -71,7 +83,6 @@ function createActivityTracker(maxTurns?: number, onStreamUpdate?: () => void) {
|
|
|
71
83
|
}
|
|
72
84
|
state.toolUses++;
|
|
73
85
|
}
|
|
74
|
-
state.tokens = safeFormatTokens(state.session);
|
|
75
86
|
onStreamUpdate?.();
|
|
76
87
|
},
|
|
77
88
|
onTextDelta: (_delta: string, fullText: string) => {
|
|
@@ -85,6 +96,10 @@ function createActivityTracker(maxTurns?: number, onStreamUpdate?: () => void) {
|
|
|
85
96
|
onSessionCreated: (session: any) => {
|
|
86
97
|
state.session = session;
|
|
87
98
|
},
|
|
99
|
+
onAssistantUsage: (usage: { input: number; output: number; cacheWrite: number }) => {
|
|
100
|
+
addUsage(state.lifetimeUsage, usage);
|
|
101
|
+
onStreamUpdate?.();
|
|
102
|
+
},
|
|
88
103
|
};
|
|
89
104
|
|
|
90
105
|
return { state, callbacks };
|
|
@@ -120,13 +135,10 @@ function escapeXml(s: string): string {
|
|
|
120
135
|
function formatTaskNotification(record: AgentRecord, resultMaxLen: number): string {
|
|
121
136
|
const status = getStatusLabel(record.status, record.error);
|
|
122
137
|
const durationMs = record.completedAt ? record.completedAt - record.startedAt : 0;
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
totalTokens = stats.tokens?.total ?? 0;
|
|
128
|
-
}
|
|
129
|
-
} catch { /* session stats unavailable */ }
|
|
138
|
+
const totalTokens = getLifetimeTotal(record.lifetimeUsage);
|
|
139
|
+
const contextPercent = getSessionContextPercent(record.session);
|
|
140
|
+
const ctxXml = contextPercent !== null ? `<context_percent>${Math.round(contextPercent)}</context_percent>` : "";
|
|
141
|
+
const compactXml = record.compactionCount ? `<compactions>${record.compactionCount}</compactions>` : "";
|
|
130
142
|
|
|
131
143
|
const resultPreview = record.result
|
|
132
144
|
? record.result.length > resultMaxLen
|
|
@@ -142,7 +154,7 @@ function formatTaskNotification(record: AgentRecord, resultMaxLen: number): stri
|
|
|
142
154
|
`<status>${escapeXml(status)}</status>`,
|
|
143
155
|
`<summary>Agent "${escapeXml(record.description)}" ${record.status}</summary>`,
|
|
144
156
|
`<result>${escapeXml(resultPreview)}</result>`,
|
|
145
|
-
`<usage><total_tokens>${totalTokens}</total_tokens><tool_uses>${record.toolUses}</tool_uses
|
|
157
|
+
`<usage><total_tokens>${totalTokens}</total_tokens><tool_uses>${record.toolUses}</tool_uses>${ctxXml}${compactXml}<duration_ms>${durationMs}</duration_ms></usage>`,
|
|
146
158
|
`</task-notification>`,
|
|
147
159
|
].filter(Boolean).join('\n');
|
|
148
160
|
}
|
|
@@ -150,14 +162,14 @@ function formatTaskNotification(record: AgentRecord, resultMaxLen: number): stri
|
|
|
150
162
|
/** Build AgentDetails from a base + record-specific fields. */
|
|
151
163
|
function buildDetails(
|
|
152
164
|
base: Pick<AgentDetails, "displayName" | "description" | "subagentType" | "modelName" | "tags">,
|
|
153
|
-
record: { toolUses: number; startedAt: number; completedAt?: number; status: string; error?: string; id?: string; session?: any },
|
|
165
|
+
record: { toolUses: number; startedAt: number; completedAt?: number; status: string; error?: string; id?: string; session?: any; lifetimeUsage: LifetimeUsage },
|
|
154
166
|
activity?: AgentActivity,
|
|
155
167
|
overrides?: Partial<AgentDetails>,
|
|
156
168
|
): AgentDetails {
|
|
157
169
|
return {
|
|
158
170
|
...base,
|
|
159
171
|
toolUses: record.toolUses,
|
|
160
|
-
tokens:
|
|
172
|
+
tokens: formatLifetimeTokens(record),
|
|
161
173
|
turnCount: activity?.turnCount,
|
|
162
174
|
maxTurns: activity?.maxTurns,
|
|
163
175
|
durationMs: (record.completedAt ?? Date.now()) - record.startedAt,
|
|
@@ -170,10 +182,7 @@ function buildDetails(
|
|
|
170
182
|
|
|
171
183
|
/** Build notification details for the custom message renderer. */
|
|
172
184
|
function buildNotificationDetails(record: AgentRecord, resultMaxLen: number, activity?: AgentActivity): NotificationDetails {
|
|
173
|
-
|
|
174
|
-
try {
|
|
175
|
-
if (record.session) totalTokens = record.session.getSessionStats().tokens?.total ?? 0;
|
|
176
|
-
} catch {}
|
|
185
|
+
const totalTokens = getLifetimeTotal(record.lifetimeUsage);
|
|
177
186
|
|
|
178
187
|
return {
|
|
179
188
|
id: record.id,
|
|
@@ -266,7 +275,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
266
275
|
cancelNudge(key);
|
|
267
276
|
pendingNudges.set(key, setTimeout(() => {
|
|
268
277
|
pendingNudges.delete(key);
|
|
269
|
-
send();
|
|
278
|
+
try { send(); } catch { /* ignore stale completion side-effect errors */ }
|
|
270
279
|
}, delay));
|
|
271
280
|
}
|
|
272
281
|
|
|
@@ -337,17 +346,15 @@ export default function (pi: ExtensionAPI) {
|
|
|
337
346
|
/** Helper: build event data for lifecycle events from an AgentRecord. */
|
|
338
347
|
function buildEventData(record: AgentRecord) {
|
|
339
348
|
const durationMs = record.completedAt ? record.completedAt - record.startedAt : Date.now() - record.startedAt;
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
}
|
|
350
|
-
} catch { /* session stats unavailable */ }
|
|
349
|
+
// All three fields are lifetime-accumulated (Σ over every assistant message_end),
|
|
350
|
+
// so they survive compaction together — input + output ≤ total always.
|
|
351
|
+
// tokens is omitted when nothing was ever produced (e.g. agent errored before
|
|
352
|
+
// any message_end fired), preserving prior payload shape.
|
|
353
|
+
const u = record.lifetimeUsage;
|
|
354
|
+
const total = getLifetimeTotal(u);
|
|
355
|
+
const tokens = total > 0
|
|
356
|
+
? { input: u.input, output: u.output, total }
|
|
357
|
+
: undefined;
|
|
351
358
|
return {
|
|
352
359
|
id: record.id,
|
|
353
360
|
type: record.type,
|
|
@@ -408,6 +415,16 @@ export default function (pi: ExtensionAPI) {
|
|
|
408
415
|
type: record.type,
|
|
409
416
|
description: record.description,
|
|
410
417
|
});
|
|
418
|
+
}, (record, info) => {
|
|
419
|
+
// Emit compacted event when agent's session compacts (preserves count on record).
|
|
420
|
+
pi.events.emit("subagents:compacted", {
|
|
421
|
+
id: record.id,
|
|
422
|
+
type: record.type,
|
|
423
|
+
description: record.description,
|
|
424
|
+
reason: info.reason,
|
|
425
|
+
tokensBefore: info.tokensBefore,
|
|
426
|
+
compactionCount: record.compactionCount,
|
|
427
|
+
});
|
|
411
428
|
});
|
|
412
429
|
|
|
413
430
|
// Expose manager via Symbol.for() global registry for cross-package access.
|
|
@@ -424,13 +441,38 @@ export default function (pi: ExtensionAPI) {
|
|
|
424
441
|
// --- Cross-extension RPC via pi.events ---
|
|
425
442
|
let currentCtx: ExtensionContext | undefined;
|
|
426
443
|
|
|
427
|
-
//
|
|
444
|
+
// ---- Subagent scheduler ----
|
|
445
|
+
// Session-scoped: store is constructed inside session_start once sessionId
|
|
446
|
+
// is available. Mirrors pi-chonky-tasks's session-scoped task store —
|
|
447
|
+
// schedules reset on /new, restore on /resume.
|
|
448
|
+
const scheduler = new SubagentScheduler();
|
|
449
|
+
|
|
450
|
+
function startScheduler(ctx: ExtensionContext) {
|
|
451
|
+
try {
|
|
452
|
+
const sessionId = ctx.sessionManager?.getSessionId?.();
|
|
453
|
+
if (!sessionId) return; // sessionId not yet available — try again on next event
|
|
454
|
+
const path = resolveStorePath(ctx.cwd, sessionId);
|
|
455
|
+
const store = new ScheduleStore(path);
|
|
456
|
+
scheduler.start(pi, ctx, manager, store);
|
|
457
|
+
pi.events.emit("subagents:scheduler_ready", { sessionId, jobCount: store.list().length });
|
|
458
|
+
} catch (err) {
|
|
459
|
+
// Scheduling is non-essential — log and move on so the rest of the
|
|
460
|
+
// extension keeps working if e.g. .pi/ is unwritable.
|
|
461
|
+
console.warn("[pi-subagents] Failed to start scheduler:", err);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
// Capture ctx from session_start for RPC spawn handler + start the scheduler.
|
|
428
466
|
pi.on("session_start", async (_event, ctx) => {
|
|
429
467
|
currentCtx = ctx;
|
|
430
|
-
manager.clearCompleted();
|
|
468
|
+
manager.clearCompleted();
|
|
469
|
+
if (isSchedulingEnabled() && !scheduler.isActive()) startScheduler(ctx);
|
|
431
470
|
});
|
|
432
471
|
|
|
433
|
-
pi.on("session_before_switch", () => {
|
|
472
|
+
pi.on("session_before_switch", () => {
|
|
473
|
+
manager.clearCompleted();
|
|
474
|
+
scheduler.stop();
|
|
475
|
+
});
|
|
434
476
|
|
|
435
477
|
const { unsubPing: unsubPingRpc, unsubSpawn: unsubSpawnRpc, unsubStop: unsubStopRpc } = registerRpcHandlers({
|
|
436
478
|
events: pi.events,
|
|
@@ -450,6 +492,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
450
492
|
unsubPingRpc();
|
|
451
493
|
currentCtx = undefined;
|
|
452
494
|
delete (globalThis as any)[MANAGER_KEY];
|
|
495
|
+
scheduler.stop();
|
|
453
496
|
manager.abortAll();
|
|
454
497
|
for (const timer of pendingNudges.values()) clearTimeout(timer);
|
|
455
498
|
pendingNudges.clear();
|
|
@@ -464,6 +507,16 @@ export default function (pi: ExtensionAPI) {
|
|
|
464
507
|
function getDefaultJoinMode(): JoinMode { return defaultJoinMode; }
|
|
465
508
|
function setDefaultJoinMode(mode: JoinMode) { defaultJoinMode = mode; }
|
|
466
509
|
|
|
510
|
+
// Master switch for the schedule subagent feature. Defaults to enabled.
|
|
511
|
+
// Read once at extension init (before tool registration) so the Agent tool's
|
|
512
|
+
// param schema reflects the persisted setting. Runtime toggles via /agents
|
|
513
|
+
// → Settings short-circuit the menu entry + the execute-time addJob path
|
|
514
|
+
// immediately, but the schema-level removal only takes effect on next
|
|
515
|
+
// extension load (next pi session). Documented in CHANGELOG/README.
|
|
516
|
+
let schedulingEnabled = true;
|
|
517
|
+
function isSchedulingEnabled(): boolean { return schedulingEnabled; }
|
|
518
|
+
function setSchedulingEnabled(b: boolean) { schedulingEnabled = b; }
|
|
519
|
+
|
|
467
520
|
// ---- Batch tracking for smart join mode ----
|
|
468
521
|
// Collects background agent IDs spawned in the current turn for smart grouping.
|
|
469
522
|
// Uses a debounced timer: each new agent resets the 100ms window so that all
|
|
@@ -557,12 +610,35 @@ export default function (pi: ExtensionAPI) {
|
|
|
557
610
|
setDefaultMaxTurns,
|
|
558
611
|
setGraceTurns,
|
|
559
612
|
setDefaultJoinMode,
|
|
613
|
+
setSchedulingEnabled,
|
|
560
614
|
},
|
|
561
615
|
(event, payload) => pi.events.emit(event, payload),
|
|
562
616
|
);
|
|
563
617
|
|
|
564
618
|
// ---- Agent tool ----
|
|
565
619
|
|
|
620
|
+
// Schedule param + its guideline are gated on `schedulingEnabled` (read once
|
|
621
|
+
// at registration; flipping the setting later requires next pi session for
|
|
622
|
+
// the schema to update). Defining the shape once and spreading it via Partial
|
|
623
|
+
// preserves Type.Object's inference when present and produces a
|
|
624
|
+
// `schedule`-free schema when absent — zero LLM-context cost in disabled mode.
|
|
625
|
+
const scheduleParamShape = {
|
|
626
|
+
schedule: Type.Optional(
|
|
627
|
+
Type.String({
|
|
628
|
+
description:
|
|
629
|
+
'Opt-in only — fire later instead of now. Omit to run immediately (the default, almost always correct). ' +
|
|
630
|
+
'Formats: 6-field cron ("0 0 9 * * 1" = 9am Mon), interval ("5m"/"1h"), one-shot ("+10m" or ISO). ' +
|
|
631
|
+
'Forces run_in_background; incompatible with inherit_context and resume. Returns job ID.',
|
|
632
|
+
}),
|
|
633
|
+
),
|
|
634
|
+
};
|
|
635
|
+
const scheduleParam: Partial<typeof scheduleParamShape> =
|
|
636
|
+
isSchedulingEnabled() ? scheduleParamShape : {};
|
|
637
|
+
|
|
638
|
+
const scheduleGuideline = isSchedulingEnabled()
|
|
639
|
+
? `\n- Use \`schedule\` only when the user explicitly asked for scheduled / recurring / delayed execution (e.g. "every Monday", "in an hour"). Don't auto-schedule from vague intent like "monitor X" — run once now or ask.`
|
|
640
|
+
: "";
|
|
641
|
+
|
|
566
642
|
pi.registerTool(defineTool({
|
|
567
643
|
name: "Agent",
|
|
568
644
|
label: "Agent",
|
|
@@ -586,7 +662,7 @@ Guidelines:
|
|
|
586
662
|
- Use model to specify a different model (as "provider/modelId", or fuzzy e.g. "haiku", "sonnet").
|
|
587
663
|
- Use thinking to control extended thinking level.
|
|
588
664
|
- Use inherit_context if the agent needs the parent conversation history.
|
|
589
|
-
- Use isolation: "worktree" to run the agent in an isolated git worktree (safe parallel file modifications)
|
|
665
|
+
- Use isolation: "worktree" to run the agent in an isolated git worktree (safe parallel file modifications).${scheduleGuideline}`,
|
|
590
666
|
parameters: Type.Object({
|
|
591
667
|
prompt: Type.String({
|
|
592
668
|
description: "The task for the agent to perform.",
|
|
@@ -639,6 +715,7 @@ Guidelines:
|
|
|
639
715
|
description: 'Set to "worktree" to run the agent in a temporary git worktree (isolated copy of the repo). Changes are saved to a branch on completion.',
|
|
640
716
|
}),
|
|
641
717
|
),
|
|
718
|
+
...scheduleParam,
|
|
642
719
|
}),
|
|
643
720
|
|
|
644
721
|
// ---- Custom rendering: Claude Code style ----
|
|
@@ -792,6 +869,47 @@ Guidelines:
|
|
|
792
869
|
tags: agentTags.length > 0 ? agentTags : undefined,
|
|
793
870
|
};
|
|
794
871
|
|
|
872
|
+
// ---- Schedule: register a job, don't spawn now ----
|
|
873
|
+
if (params.schedule) {
|
|
874
|
+
if (!isSchedulingEnabled()) {
|
|
875
|
+
return textResult("Scheduling is disabled in this project. Enable via /agents → Settings → Scheduling.");
|
|
876
|
+
}
|
|
877
|
+
if (params.resume) {
|
|
878
|
+
return textResult("Cannot combine `schedule` with `resume` — schedules create fresh agents.");
|
|
879
|
+
}
|
|
880
|
+
if (params.inherit_context) {
|
|
881
|
+
return textResult("Cannot combine `schedule` with `inherit_context` — there is no parent conversation at fire time.");
|
|
882
|
+
}
|
|
883
|
+
if (params.run_in_background === false) {
|
|
884
|
+
return textResult("Cannot combine `schedule` with `run_in_background: false` — scheduled jobs always run in background.");
|
|
885
|
+
}
|
|
886
|
+
if (!scheduler.isActive()) {
|
|
887
|
+
return textResult("Scheduler is not active in this session yet. Try again after the session has fully started.");
|
|
888
|
+
}
|
|
889
|
+
try {
|
|
890
|
+
const job = scheduler.addJob({
|
|
891
|
+
name: params.description as string,
|
|
892
|
+
description: params.description as string,
|
|
893
|
+
schedule: params.schedule as string,
|
|
894
|
+
subagent_type: subagentType,
|
|
895
|
+
prompt: params.prompt as string,
|
|
896
|
+
model: params.model as string | undefined,
|
|
897
|
+
thinking: thinking,
|
|
898
|
+
max_turns: effectiveMaxTurns,
|
|
899
|
+
isolated: isolated,
|
|
900
|
+
isolation: isolation,
|
|
901
|
+
});
|
|
902
|
+
const next = scheduler.getNextRun(job.id);
|
|
903
|
+
return textResult(
|
|
904
|
+
`Scheduled "${job.name}" (id: ${job.id}, type: ${job.scheduleType}). ` +
|
|
905
|
+
`Next run: ${next ?? "(unknown)"}. ` +
|
|
906
|
+
`Manage via /agents → Scheduled jobs.`,
|
|
907
|
+
);
|
|
908
|
+
} catch (err) {
|
|
909
|
+
return textResult(err instanceof Error ? err.message : String(err));
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
|
|
795
913
|
// Resume existing agent
|
|
796
914
|
if (params.resume) {
|
|
797
915
|
const existing = manager.getRecord(params.resume);
|
|
@@ -828,17 +946,21 @@ Guidelines:
|
|
|
828
946
|
}
|
|
829
947
|
};
|
|
830
948
|
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
949
|
+
try {
|
|
950
|
+
id = manager.spawn(pi, ctx, subagentType, params.prompt, {
|
|
951
|
+
description: params.description,
|
|
952
|
+
model,
|
|
953
|
+
maxTurns: effectiveMaxTurns,
|
|
954
|
+
isolated,
|
|
955
|
+
inheritContext,
|
|
956
|
+
thinkingLevel: thinking,
|
|
957
|
+
isBackground: true,
|
|
958
|
+
isolation,
|
|
959
|
+
...bgCallbacks,
|
|
960
|
+
});
|
|
961
|
+
} catch (err) {
|
|
962
|
+
return textResult(err instanceof Error ? err.message : String(err));
|
|
963
|
+
}
|
|
842
964
|
|
|
843
965
|
// Set output file + join mode synchronously after spawn, before the
|
|
844
966
|
// event loop yields — onSessionCreated is async so this is safe.
|
|
@@ -898,7 +1020,7 @@ Guidelines:
|
|
|
898
1020
|
const details: AgentDetails = {
|
|
899
1021
|
...detailBase,
|
|
900
1022
|
toolUses: fgState.toolUses,
|
|
901
|
-
tokens: fgState
|
|
1023
|
+
tokens: formatLifetimeTokens(fgState),
|
|
902
1024
|
turnCount: fgState.turnCount,
|
|
903
1025
|
maxTurns: fgState.maxTurns,
|
|
904
1026
|
durationMs: Date.now() - startedAt,
|
|
@@ -936,16 +1058,23 @@ Guidelines:
|
|
|
936
1058
|
|
|
937
1059
|
streamUpdate();
|
|
938
1060
|
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
1061
|
+
let record: AgentRecord;
|
|
1062
|
+
try {
|
|
1063
|
+
record = await manager.spawnAndWait(pi, ctx, subagentType, params.prompt, {
|
|
1064
|
+
description: params.description,
|
|
1065
|
+
model,
|
|
1066
|
+
maxTurns: effectiveMaxTurns,
|
|
1067
|
+
isolated,
|
|
1068
|
+
inheritContext,
|
|
1069
|
+
thinkingLevel: thinking,
|
|
1070
|
+
isolation,
|
|
1071
|
+
signal,
|
|
1072
|
+
...fgCallbacks,
|
|
1073
|
+
});
|
|
1074
|
+
} catch (err) {
|
|
1075
|
+
clearInterval(spinnerInterval);
|
|
1076
|
+
return textResult(err instanceof Error ? err.message : String(err));
|
|
1077
|
+
}
|
|
949
1078
|
|
|
950
1079
|
clearInterval(spinnerInterval);
|
|
951
1080
|
|
|
@@ -956,7 +1085,7 @@ Guidelines:
|
|
|
956
1085
|
}
|
|
957
1086
|
|
|
958
1087
|
// Get final token count
|
|
959
|
-
const tokenText =
|
|
1088
|
+
const tokenText = formatLifetimeTokens(fgState);
|
|
960
1089
|
|
|
961
1090
|
const details = buildDetails(detailBase, record, fgState, { tokens: tokenText });
|
|
962
1091
|
|
|
@@ -1019,12 +1148,17 @@ Guidelines:
|
|
|
1019
1148
|
|
|
1020
1149
|
const displayName = getDisplayName(record.type);
|
|
1021
1150
|
const duration = formatDuration(record.startedAt, record.completedAt);
|
|
1022
|
-
const tokens =
|
|
1023
|
-
const
|
|
1151
|
+
const tokens = formatLifetimeTokens(record);
|
|
1152
|
+
const contextPercent = getSessionContextPercent(record.session);
|
|
1153
|
+
const statsParts = [`Tool uses: ${record.toolUses}`];
|
|
1154
|
+
if (tokens) statsParts.push(tokens);
|
|
1155
|
+
if (contextPercent !== null) statsParts.push(`Context: ${Math.round(contextPercent)}%`);
|
|
1156
|
+
if (record.compactionCount) statsParts.push(`Compactions: ${record.compactionCount}`);
|
|
1157
|
+
statsParts.push(`Duration: ${duration}`);
|
|
1024
1158
|
|
|
1025
1159
|
let output =
|
|
1026
1160
|
`Agent: ${record.id}\n` +
|
|
1027
|
-
`Type: ${displayName} | Status: ${record.status} | ${
|
|
1161
|
+
`Type: ${displayName} | Status: ${record.status} | ${statsParts.join(" | ")}\n` +
|
|
1028
1162
|
`Description: ${record.description}\n\n`;
|
|
1029
1163
|
|
|
1030
1164
|
if (record.status === "running") {
|
|
@@ -1088,7 +1222,17 @@ Guidelines:
|
|
|
1088
1222
|
try {
|
|
1089
1223
|
await steerAgent(record.session, params.message);
|
|
1090
1224
|
pi.events.emit("subagents:steered", { id: record.id, message: params.message });
|
|
1091
|
-
|
|
1225
|
+
const tokens = formatLifetimeTokens(record);
|
|
1226
|
+
const contextPercent = getSessionContextPercent(record.session);
|
|
1227
|
+
const stateParts: string[] = [];
|
|
1228
|
+
if (tokens) stateParts.push(tokens);
|
|
1229
|
+
stateParts.push(`${record.toolUses} tool ${record.toolUses === 1 ? "use" : "uses"}`);
|
|
1230
|
+
if (contextPercent !== null) stateParts.push(`context ${Math.round(contextPercent)}% full`);
|
|
1231
|
+
if (record.compactionCount) stateParts.push(`${record.compactionCount} compaction${record.compactionCount === 1 ? "" : "s"}`);
|
|
1232
|
+
return textResult(
|
|
1233
|
+
`Steering message sent to agent ${record.id}. The agent will process it after its current tool execution.\n` +
|
|
1234
|
+
`Current state: ${stateParts.join(" · ")}`,
|
|
1235
|
+
);
|
|
1092
1236
|
} catch (err) {
|
|
1093
1237
|
return textResult(`Failed to steer agent: ${err instanceof Error ? err.message : String(err)}`);
|
|
1094
1238
|
}
|
|
@@ -1140,6 +1284,12 @@ Guidelines:
|
|
|
1140
1284
|
options.push(`Agent types (${allNames.length})`);
|
|
1141
1285
|
}
|
|
1142
1286
|
|
|
1287
|
+
// Scheduled jobs entry (always present when scheduler is active)
|
|
1288
|
+
if (scheduler.isActive()) {
|
|
1289
|
+
const jobCount = scheduler.list().length;
|
|
1290
|
+
options.push(`Scheduled jobs (${jobCount})`);
|
|
1291
|
+
}
|
|
1292
|
+
|
|
1143
1293
|
// Actions
|
|
1144
1294
|
options.push("Create new agent");
|
|
1145
1295
|
options.push("Settings");
|
|
@@ -1163,6 +1313,9 @@ Guidelines:
|
|
|
1163
1313
|
} else if (choice.startsWith("Agent types (")) {
|
|
1164
1314
|
await showAllAgentsList(ctx);
|
|
1165
1315
|
await showAgentsMenu(ctx);
|
|
1316
|
+
} else if (choice.startsWith("Scheduled jobs (")) {
|
|
1317
|
+
await showSchedulesMenu(ctx, scheduler);
|
|
1318
|
+
await showAgentsMenu(ctx);
|
|
1166
1319
|
} else if (choice === "Create new agent") {
|
|
1167
1320
|
await showCreateWizard(ctx);
|
|
1168
1321
|
} else if (choice === "Settings") {
|
|
@@ -1626,6 +1779,7 @@ ${systemPrompt}
|
|
|
1626
1779
|
defaultMaxTurns: getDefaultMaxTurns() ?? 0,
|
|
1627
1780
|
graceTurns: getGraceTurns(),
|
|
1628
1781
|
defaultJoinMode: getDefaultJoinMode(),
|
|
1782
|
+
schedulingEnabled: isSchedulingEnabled(),
|
|
1629
1783
|
};
|
|
1630
1784
|
}
|
|
1631
1785
|
|
|
@@ -1635,6 +1789,7 @@ ${systemPrompt}
|
|
|
1635
1789
|
`Default max turns (current: ${getDefaultMaxTurns() ?? "unlimited"})`,
|
|
1636
1790
|
`Grace turns (current: ${getGraceTurns()})`,
|
|
1637
1791
|
`Join mode (current: ${getDefaultJoinMode()})`,
|
|
1792
|
+
`Scheduling (current: ${isSchedulingEnabled() ? "enabled" : "disabled"})`,
|
|
1638
1793
|
]);
|
|
1639
1794
|
if (!choice) return;
|
|
1640
1795
|
|
|
@@ -1685,6 +1840,27 @@ ${systemPrompt}
|
|
|
1685
1840
|
setDefaultJoinMode(mode);
|
|
1686
1841
|
notifyApplied(ctx, `Default join mode set to ${mode}`);
|
|
1687
1842
|
}
|
|
1843
|
+
} else if (choice.startsWith("Scheduling")) {
|
|
1844
|
+
const val = await ctx.ui.select(
|
|
1845
|
+
"Schedule subagent feature",
|
|
1846
|
+
[
|
|
1847
|
+
"enabled — Agent tool accepts a `schedule` param; /agents → Scheduled jobs visible",
|
|
1848
|
+
"disabled — `schedule` removed from Agent tool spec (no LLM-context cost); menu hidden",
|
|
1849
|
+
],
|
|
1850
|
+
);
|
|
1851
|
+
if (val) {
|
|
1852
|
+
const enabled = val.startsWith("enabled");
|
|
1853
|
+
if (enabled === isSchedulingEnabled()) {
|
|
1854
|
+
ctx.ui.notify(`Scheduling already ${enabled ? "enabled" : "disabled"}.`, "info");
|
|
1855
|
+
} else {
|
|
1856
|
+
setSchedulingEnabled(enabled);
|
|
1857
|
+
if (!enabled) scheduler.stop(); // immediate kill — outstanding fires stop ticking
|
|
1858
|
+
notifyApplied(
|
|
1859
|
+
ctx,
|
|
1860
|
+
`Scheduling ${enabled ? "enabled" : "disabled"}. Tool spec change takes effect on next pi session.`,
|
|
1861
|
+
);
|
|
1862
|
+
}
|
|
1863
|
+
}
|
|
1688
1864
|
}
|
|
1689
1865
|
}
|
|
1690
1866
|
|