pi-subagent-manager 1.10.0 → 1.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -9
- package/dist/index.d.ts +2 -0
- package/dist/index.js +790 -0
- package/dist/orch/checklist-integration.js +176 -0
- package/dist/orch/context-entries.js +6 -0
- package/dist/orch/cost-ledger.js +38 -0
- package/dist/orch/diagnostics.js +86 -0
- package/dist/orch/extensions.js +66 -0
- package/dist/orch/housekeeping-usage.js +22 -0
- package/dist/orch/housekeeping.js +248 -0
- package/dist/orch/inherited-tools.js +57 -0
- package/dist/orch/mailbox.js +195 -0
- package/dist/orch/manager.js +970 -0
- package/dist/orch/paths.js +69 -0
- package/dist/orch/prompt.js +30 -0
- package/dist/orch/registry.js +115 -0
- package/dist/orch/runtime.js +835 -0
- package/dist/orch/tools.js +112 -0
- package/dist/orch/transcript.js +165 -0
- package/dist/prefs/agent-import.js +188 -0
- package/dist/prefs/config.js +878 -0
- package/dist/prefs/import-discovery.js +547 -0
- package/{src/prefs/import-instructions.ts → dist/prefs/import-instructions.js} +0 -1
- package/dist/prefs/models.js +56 -0
- package/dist/prefs/settings.js +491 -0
- package/dist/types.js +1 -0
- package/dist/ui/agent-loader.js +63 -0
- package/dist/ui/agent-navigation-editor.js +168 -0
- package/dist/ui/cost-footer.js +209 -0
- package/dist/ui/dialog.js +505 -0
- package/dist/ui/import-picker.js +191 -0
- package/dist/ui/live-agent-view.js +527 -0
- package/dist/ui/model-picker.js +529 -0
- package/dist/ui/settings-ui.js +671 -0
- package/dist/ui/status-ui.js +470 -0
- package/dist/ui/thread-tree.js +142 -0
- package/dist/ui/tool-picker.js +331 -0
- package/dist/ui/transcript-tool.js +91 -0
- package/dist/ui/ui.js +1019 -0
- package/package.json +17 -5
- package/docs/assets/subagent-manager-demo.gif +0 -0
- package/docs/async-lifecycle.md +0 -153
- package/docs/custom-agents.md +0 -154
- package/docs/default-agents.md +0 -95
- package/docs/demo.md +0 -63
- package/docs/development.md +0 -74
- package/docs/importing-agents.md +0 -41
- package/docs/pi-footer.md +0 -128
- package/docs/research-claude.md +0 -258
- package/docs/research-codex.md +0 -143
- package/docs/research-default-agent-models.md +0 -121
- package/docs/research-model-audit-2026-10.md +0 -154
- package/docs/research-model-roles-coding.md +0 -99
- package/docs/research-model-roles-writing.md +0 -78
- package/docs/research-researcher-workflow.md +0 -97
- package/docs/settings.md +0 -293
- package/docs/subagent-extension-limitations.md +0 -98
- package/docs/threads.md +0 -73
- package/docs/tools-and-ui.md +0 -129
- package/src/index.ts +0 -920
- package/src/orch/checklist-integration.ts +0 -230
- package/src/orch/context-entries.ts +0 -15
- package/src/orch/cost-ledger.ts +0 -56
- package/src/orch/diagnostics.ts +0 -95
- package/src/orch/extensions.ts +0 -83
- package/src/orch/housekeeping-usage.ts +0 -47
- package/src/orch/housekeeping.ts +0 -348
- package/src/orch/inherited-tools.ts +0 -84
- package/src/orch/mailbox.ts +0 -261
- package/src/orch/manager.ts +0 -1084
- package/src/orch/paths.ts +0 -73
- package/src/orch/prompt.ts +0 -39
- package/src/orch/registry.ts +0 -168
- package/src/orch/runtime.ts +0 -1041
- package/src/orch/tools.ts +0 -190
- package/src/orch/transcript.ts +0 -186
- package/src/prefs/agent-import.ts +0 -245
- package/src/prefs/config.ts +0 -1017
- package/src/prefs/import-discovery.ts +0 -643
- package/src/prefs/models.ts +0 -74
- package/src/prefs/settings.ts +0 -614
- package/src/types.ts +0 -271
- package/src/ui/agent-loader.ts +0 -98
- package/src/ui/agent-navigation-editor.ts +0 -233
- package/src/ui/cost-footer.ts +0 -230
- package/src/ui/dialog.ts +0 -678
- package/src/ui/import-picker.ts +0 -231
- package/src/ui/live-agent-view.ts +0 -600
- package/src/ui/model-picker.ts +0 -701
- package/src/ui/settings-ui.ts +0 -830
- package/src/ui/status-ui.ts +0 -628
- package/src/ui/thread-tree.ts +0 -173
- package/src/ui/tool-picker.ts +0 -398
- package/src/ui/transcript-tool.ts +0 -142
- package/src/ui/ui.ts +0 -1407
package/dist/index.js
ADDED
|
@@ -0,0 +1,790 @@
|
|
|
1
|
+
import { CAPABILITY_PROBE_CHANNEL, respondDiagnosticCapability, diagnosticAgentText, } from "./orch/diagnostics.js";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { buildSessionContext, getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
4
|
+
import { fuzzyFilter } from "@earendil-works/pi-tui";
|
|
5
|
+
import { ConfigStore } from "./prefs/config.js";
|
|
6
|
+
import { IMPORT_REQUEST_PREFIX, markImportOffered, offerAgentImport, } from "./prefs/agent-import.js";
|
|
7
|
+
import { ThreadManager } from "./orch/manager.js";
|
|
8
|
+
import { inheritedContextEntries } from "./orch/context-entries.js";
|
|
9
|
+
import { loadManagerSettings } from "./prefs/settings.js";
|
|
10
|
+
import { configureAgents } from "./ui/settings-ui.js";
|
|
11
|
+
import { AgentNavigationController } from "./ui/status-ui.js";
|
|
12
|
+
import { AgentNavigationEditor } from "./ui/agent-navigation-editor.js";
|
|
13
|
+
import { createDriverFactory } from "./orch/runtime.js";
|
|
14
|
+
import { createHousekeepingCompleter, HousekeepingSummarizer, resolveHousekeepingModel, } from "./orch/housekeeping.js";
|
|
15
|
+
import { createInheritedToolSource } from "./orch/inherited-tools.js";
|
|
16
|
+
import { subagentPrompt } from "./orch/prompt.js";
|
|
17
|
+
import { ChecklistMetaMirror, checklistStatus } from "./orch/checklist-integration.js";
|
|
18
|
+
import { agentTools } from "./orch/tools.js";
|
|
19
|
+
import { editAgentTypes, updateWidget } from "./ui/ui.js";
|
|
20
|
+
import { canOpenDialog, dialogMenu } from "./ui/dialog.js";
|
|
21
|
+
import { COST_ENTRY, CostLedger } from "./orch/cost-ledger.js";
|
|
22
|
+
import { readRegistry, RegistryWriter } from "./orch/registry.js";
|
|
23
|
+
import { CostFooterController } from "./ui/cost-footer.js";
|
|
24
|
+
const ROOT_MAILBOX_ENTRY = "pi-subagent:root-mailbox:v1";
|
|
25
|
+
const ROOT_SUMMARY_MESSAGE = "pi-subagent:final-recap";
|
|
26
|
+
const ROOT_WAKE_MESSAGE = "pi-subagent:wake";
|
|
27
|
+
export default function piSubagent(pi) {
|
|
28
|
+
let capabilityActive = true;
|
|
29
|
+
pi.events.on(CAPABILITY_PROBE_CHANNEL, (payload) => {
|
|
30
|
+
if (capabilityActive && limits.subagentMode !== "off")
|
|
31
|
+
respondDiagnosticCapability(payload);
|
|
32
|
+
});
|
|
33
|
+
let context;
|
|
34
|
+
const inheritedTools = createInheritedToolSource(pi);
|
|
35
|
+
let store = new ConfigStore({
|
|
36
|
+
cwd: process.cwd(),
|
|
37
|
+
agentDir: getAgentDir(),
|
|
38
|
+
includeProject: false,
|
|
39
|
+
});
|
|
40
|
+
let manager;
|
|
41
|
+
let costLedger = new CostLedger();
|
|
42
|
+
const costFooter = new CostFooterController(pi, () => costLedger.totalUsd, () => limits.nerdFontIcons, () => limits.costDisplay, () => limits.costValue, () => limits.costIcon);
|
|
43
|
+
const refreshCostFooter = (ctx) => costFooter.refresh(ctx);
|
|
44
|
+
const rollupCost = (thread) => {
|
|
45
|
+
costLedger.settle(thread, (record) => pi.appendEntry(COST_ENTRY, record));
|
|
46
|
+
if (context)
|
|
47
|
+
refreshCostFooter(context);
|
|
48
|
+
};
|
|
49
|
+
let limits = loadManagerSettings({
|
|
50
|
+
cwd: process.cwd(),
|
|
51
|
+
agentDir: getAgentDir(),
|
|
52
|
+
includeProject: false,
|
|
53
|
+
}).settings;
|
|
54
|
+
const housekeeping = new HousekeepingSummarizer({
|
|
55
|
+
isEnabled: () => limits.subagentMode !== "off" &&
|
|
56
|
+
limits.housekeepingEnabled &&
|
|
57
|
+
limits.housekeepingModel !== "" &&
|
|
58
|
+
context !== undefined &&
|
|
59
|
+
resolveHousekeepingModel(context, limits.housekeepingModel) !== undefined,
|
|
60
|
+
complete: createHousekeepingCompleter(() => context, () => limits.housekeepingModel, refreshCostFooter),
|
|
61
|
+
apply: (path, run, summary) => manager?.setSummary(path, run, summary) ?? false,
|
|
62
|
+
clear: (path, run) => void manager?.clearSummary(path, run),
|
|
63
|
+
onFailure: (message) => context?.ui.notify(`Housekeeping summaries failed (${message}); showing normal agent status instead.`, "warning"),
|
|
64
|
+
});
|
|
65
|
+
let housekeepingKey = "";
|
|
66
|
+
const syncHousekeeping = () => {
|
|
67
|
+
const key = `${limits.subagentMode === "off"}|${limits.housekeepingEnabled}|${limits.housekeepingModel}`;
|
|
68
|
+
if (key === housekeepingKey)
|
|
69
|
+
return;
|
|
70
|
+
housekeepingKey = key;
|
|
71
|
+
housekeeping.cancelAll();
|
|
72
|
+
manager?.clearSummaries();
|
|
73
|
+
};
|
|
74
|
+
const loadLimits = (ctx) => {
|
|
75
|
+
const loaded = loadManagerSettings({
|
|
76
|
+
cwd: ctx.cwd,
|
|
77
|
+
agentDir: getAgentDir(),
|
|
78
|
+
includeProject: ctx.isProjectTrusted(),
|
|
79
|
+
});
|
|
80
|
+
limits = loaded.settings;
|
|
81
|
+
syncHousekeeping();
|
|
82
|
+
refreshCostFooter(ctx);
|
|
83
|
+
return loaded.diagnostics;
|
|
84
|
+
};
|
|
85
|
+
const checklistMirror = new ChecklistMetaMirror(() => getAgentDir());
|
|
86
|
+
const currentChecklistStatus = (ctx) => checklistStatus(pi, checklistMirror, ctx);
|
|
87
|
+
let generation = 0;
|
|
88
|
+
let rootTurnEnded = false;
|
|
89
|
+
const hiddenWidgetThreads = new Set();
|
|
90
|
+
let widgetCollapsed = false;
|
|
91
|
+
let rootTurnRunning = false;
|
|
92
|
+
let suspendingSummaries = false;
|
|
93
|
+
let rootTurnTimer;
|
|
94
|
+
let wakeRequested = false;
|
|
95
|
+
const cancelSummary = () => {
|
|
96
|
+
if (rootTurnTimer !== undefined)
|
|
97
|
+
clearTimeout(rootTurnTimer);
|
|
98
|
+
rootTurnTimer = undefined;
|
|
99
|
+
wakeRequested = false;
|
|
100
|
+
rootTurnRunning = false;
|
|
101
|
+
};
|
|
102
|
+
const returnToWidget = () => {
|
|
103
|
+
if (!widgetCollapsed)
|
|
104
|
+
return;
|
|
105
|
+
widgetCollapsed = false;
|
|
106
|
+
if (context)
|
|
107
|
+
refreshWidget(context);
|
|
108
|
+
};
|
|
109
|
+
let navigation = new AgentNavigationController(returnToWidget);
|
|
110
|
+
let navigationEditor;
|
|
111
|
+
let navigationEditorFactory;
|
|
112
|
+
let navigationGeneration = 0;
|
|
113
|
+
const closeNavigation = (disposeEditor = false) => {
|
|
114
|
+
navigationGeneration++;
|
|
115
|
+
navigation.close();
|
|
116
|
+
if (disposeEditor) {
|
|
117
|
+
navigationEditor?.dispose();
|
|
118
|
+
navigationEditor = undefined;
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
const installNavigationEditor = (ctx, seedHistory) => {
|
|
122
|
+
if (!ctx.hasUI || ctx.mode !== "tui" || !ctx.ui.setEditorComponent)
|
|
123
|
+
return;
|
|
124
|
+
const existingFactory = ctx.ui.getEditorComponent?.();
|
|
125
|
+
if (existingFactory && existingFactory !== navigationEditorFactory)
|
|
126
|
+
return;
|
|
127
|
+
if (ctx.ui.getEditorText?.())
|
|
128
|
+
return;
|
|
129
|
+
const history = buildSessionContext(ctx.sessionManager.getBranch()).messages.flatMap((message) => {
|
|
130
|
+
if (message.role !== "user")
|
|
131
|
+
return [];
|
|
132
|
+
const content = message.content;
|
|
133
|
+
return [
|
|
134
|
+
typeof content === "string"
|
|
135
|
+
? content
|
|
136
|
+
: content
|
|
137
|
+
.filter((block) => block.type === "text")
|
|
138
|
+
.map((block) => block.text)
|
|
139
|
+
.join(""),
|
|
140
|
+
];
|
|
141
|
+
});
|
|
142
|
+
navigationEditorFactory = (tui, theme, keys) => {
|
|
143
|
+
const editor = new AgentNavigationEditor(tui, theme, keys, {
|
|
144
|
+
initialHistory: seedHistory ? history : undefined,
|
|
145
|
+
generation: () => navigationGeneration,
|
|
146
|
+
canOpen: () => manager !== undefined &&
|
|
147
|
+
context?.mode === "tui" &&
|
|
148
|
+
limits.subagentMode !== "off" &&
|
|
149
|
+
!navigation.isOpen &&
|
|
150
|
+
manager
|
|
151
|
+
.list()
|
|
152
|
+
.some((thread) => thread.path !== "/root" &&
|
|
153
|
+
(thread.state === "starting" || thread.state === "running")),
|
|
154
|
+
openTree: () => manager && context
|
|
155
|
+
? navigation.open(context, manager.scope("/root"), undefined, limits.nerdFontIcons, limits.loaderStyle)
|
|
156
|
+
: undefined,
|
|
157
|
+
canCollapse: () => manager !== undefined &&
|
|
158
|
+
context?.mode === "tui" &&
|
|
159
|
+
limits.subagentMode !== "off" &&
|
|
160
|
+
!navigation.isOpen &&
|
|
161
|
+
!widgetCollapsed &&
|
|
162
|
+
limits.widgetMode === "full" &&
|
|
163
|
+
widgetThreads().some((thread) => thread.path !== "/root"),
|
|
164
|
+
collapseWidget: () => {
|
|
165
|
+
widgetCollapsed = true;
|
|
166
|
+
if (context)
|
|
167
|
+
refreshWidget(context);
|
|
168
|
+
},
|
|
169
|
+
canExpand: () => manager !== undefined &&
|
|
170
|
+
context?.mode === "tui" &&
|
|
171
|
+
limits.subagentMode !== "off" &&
|
|
172
|
+
!navigation.isOpen &&
|
|
173
|
+
widgetCollapsed &&
|
|
174
|
+
limits.widgetMode === "full" &&
|
|
175
|
+
widgetThreads().some((thread) => thread.path !== "/root"),
|
|
176
|
+
expandWidget: () => {
|
|
177
|
+
widgetCollapsed = false;
|
|
178
|
+
if (context)
|
|
179
|
+
refreshWidget(context);
|
|
180
|
+
},
|
|
181
|
+
onError: (error) => context?.ui.notify(error instanceof Error ? error.message : String(error), "error"),
|
|
182
|
+
});
|
|
183
|
+
navigationEditor = editor;
|
|
184
|
+
return editor;
|
|
185
|
+
};
|
|
186
|
+
ctx.ui.setEditorComponent(navigationEditorFactory);
|
|
187
|
+
};
|
|
188
|
+
const registryWriter = new RegistryWriter((customType, data) => pi.appendEntry(customType, data));
|
|
189
|
+
let migrationRequested = false;
|
|
190
|
+
let importInProgress = false;
|
|
191
|
+
const importAgents = async (ctx, firstRun) => {
|
|
192
|
+
if (limits.subagentMode === "orchestration") {
|
|
193
|
+
if (!firstRun)
|
|
194
|
+
ctx.ui.notify("Agent import requires Opportunistic mode. Change Subagent Mode in /agents settings.", "info");
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
if (importInProgress)
|
|
198
|
+
return;
|
|
199
|
+
importInProgress = true;
|
|
200
|
+
try {
|
|
201
|
+
migrationRequested =
|
|
202
|
+
(await offerAgentImport(pi, ctx, {
|
|
203
|
+
agentDir: getAgentDir(),
|
|
204
|
+
store,
|
|
205
|
+
firstRun,
|
|
206
|
+
})) || migrationRequested;
|
|
207
|
+
}
|
|
208
|
+
finally {
|
|
209
|
+
importInProgress = false;
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
const requireManager = () => {
|
|
213
|
+
if (!manager)
|
|
214
|
+
throw new Error("Subagent threads are not initialized; start a Pi session first");
|
|
215
|
+
return manager;
|
|
216
|
+
};
|
|
217
|
+
const widgetThreads = () => requireManager()
|
|
218
|
+
.list()
|
|
219
|
+
.filter((thread) => {
|
|
220
|
+
if (thread.state === "starting" || thread.state === "running")
|
|
221
|
+
hiddenWidgetThreads.delete(thread.path);
|
|
222
|
+
return !hiddenWidgetThreads.has(thread.path);
|
|
223
|
+
});
|
|
224
|
+
const requireContext = () => {
|
|
225
|
+
if (!context)
|
|
226
|
+
throw new Error("No active Pi session");
|
|
227
|
+
return context;
|
|
228
|
+
};
|
|
229
|
+
const persist = () => {
|
|
230
|
+
if (!manager)
|
|
231
|
+
return;
|
|
232
|
+
registryWriter.write(requireContext().sessionManager.getSessionId(), manager.saved());
|
|
233
|
+
};
|
|
234
|
+
const warnDelivery = (error) => {
|
|
235
|
+
if (limits.subagentMode === "off")
|
|
236
|
+
return;
|
|
237
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
238
|
+
context?.ui.notify(`Subagent delivery failed: ${message}`, "warning");
|
|
239
|
+
};
|
|
240
|
+
const sendRootNotification = (notification) => {
|
|
241
|
+
if (limits.subagentMode === "off" || context?.isIdle?.() === false)
|
|
242
|
+
return;
|
|
243
|
+
pi.sendMessage({
|
|
244
|
+
customType: "pi-subagent:update",
|
|
245
|
+
content: notification.content,
|
|
246
|
+
display: false,
|
|
247
|
+
details: notification.details,
|
|
248
|
+
}, { triggerTurn: false });
|
|
249
|
+
if (notification.trigger === true)
|
|
250
|
+
wakeRequested = true;
|
|
251
|
+
};
|
|
252
|
+
const pendingSummaries = (ctx) => {
|
|
253
|
+
const entries = ctx.sessionManager.getBranch();
|
|
254
|
+
const summarized = new Set(entries.flatMap((entry) => {
|
|
255
|
+
if (entry.type !== "custom_message" || entry.customType !== ROOT_SUMMARY_MESSAGE)
|
|
256
|
+
return [];
|
|
257
|
+
const ids = entry.details?.mailboxIds;
|
|
258
|
+
return Array.isArray(ids) ? ids.filter((id) => typeof id === "string") : [];
|
|
259
|
+
}));
|
|
260
|
+
return entries.flatMap((entry) => {
|
|
261
|
+
if (entry.type !== "custom" || entry.customType !== ROOT_MAILBOX_ENTRY)
|
|
262
|
+
return [];
|
|
263
|
+
const notification = entry.data;
|
|
264
|
+
return notification?.rootSessionId === ctx.sessionManager.getSessionId() &&
|
|
265
|
+
notification.finalRecap === true &&
|
|
266
|
+
typeof notification.content === "string" &&
|
|
267
|
+
typeof notification.details?.mailboxId === "string" &&
|
|
268
|
+
!summarized.has(notification.details.mailboxId)
|
|
269
|
+
? [notification]
|
|
270
|
+
: [];
|
|
271
|
+
});
|
|
272
|
+
};
|
|
273
|
+
const pendingRootNotifications = (ctx) => {
|
|
274
|
+
const entries = ctx.sessionManager.getBranch();
|
|
275
|
+
const delivered = new Set(entries.flatMap((entry) => {
|
|
276
|
+
if (entry.type !== "custom_message" || entry.customType !== "pi-subagent:update")
|
|
277
|
+
return [];
|
|
278
|
+
const id = entry.details?.mailboxId;
|
|
279
|
+
return typeof id === "string" ? [id] : [];
|
|
280
|
+
}));
|
|
281
|
+
return entries.flatMap((entry) => {
|
|
282
|
+
if (entry.type !== "custom" || entry.customType !== ROOT_MAILBOX_ENTRY)
|
|
283
|
+
return [];
|
|
284
|
+
const notification = entry.data;
|
|
285
|
+
if (notification?.rootSessionId !== ctx.sessionManager.getSessionId() ||
|
|
286
|
+
typeof notification.content !== "string" ||
|
|
287
|
+
typeof notification.details?.mailboxId !== "string" ||
|
|
288
|
+
delivered.has(notification.details.mailboxId))
|
|
289
|
+
return [];
|
|
290
|
+
delivered.add(notification.details.mailboxId);
|
|
291
|
+
return [notification];
|
|
292
|
+
});
|
|
293
|
+
};
|
|
294
|
+
const scheduleRootTurn = (ctx) => {
|
|
295
|
+
if (limits.subagentMode === "off" ||
|
|
296
|
+
rootTurnRunning ||
|
|
297
|
+
suspendingSummaries ||
|
|
298
|
+
rootTurnTimer !== undefined)
|
|
299
|
+
return;
|
|
300
|
+
if (!wakeRequested &&
|
|
301
|
+
!(limits.finalRecap &&
|
|
302
|
+
(manager?.outstandingAsync() ?? 0) === 0 &&
|
|
303
|
+
pendingSummaries(ctx).length > 0) &&
|
|
304
|
+
!pendingRootNotifications(ctx).some((notification) => notification.trigger === true))
|
|
305
|
+
return;
|
|
306
|
+
const currentGeneration = generation;
|
|
307
|
+
rootTurnTimer = setTimeout(() => {
|
|
308
|
+
rootTurnTimer = undefined;
|
|
309
|
+
if (currentGeneration !== generation ||
|
|
310
|
+
context?.sessionManager !== ctx.sessionManager ||
|
|
311
|
+
limits.subagentMode === "off" ||
|
|
312
|
+
suspendingSummaries ||
|
|
313
|
+
rootTurnRunning)
|
|
314
|
+
return;
|
|
315
|
+
if (ctx.isIdle() === false) {
|
|
316
|
+
scheduleRootTurn(ctx);
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
drainRootMailbox(ctx);
|
|
320
|
+
const notifications = limits.finalRecap && (manager?.outstandingAsync() ?? 0) === 0
|
|
321
|
+
? pendingSummaries(ctx)
|
|
322
|
+
: [];
|
|
323
|
+
if (!notifications.length && !wakeRequested)
|
|
324
|
+
return;
|
|
325
|
+
const wasWakeRequested = wakeRequested;
|
|
326
|
+
wakeRequested = false;
|
|
327
|
+
rootTurnRunning = true;
|
|
328
|
+
try {
|
|
329
|
+
pi.sendMessage(notifications.length
|
|
330
|
+
? {
|
|
331
|
+
customType: ROOT_SUMMARY_MESSAGE,
|
|
332
|
+
content: "The asynchronous subagent batch has finished. Collect and inspect its results " +
|
|
333
|
+
"(use agent_output if more detail is needed), then continue any unfinished work in the user's task. " +
|
|
334
|
+
"If the overall task is complete, give a concise final recap covering results and failures. " +
|
|
335
|
+
"Do not spawn or resume agents solely to summarize.\nAgents: " +
|
|
336
|
+
[
|
|
337
|
+
...new Set(notifications.map((notification) => notification.details.path)),
|
|
338
|
+
].join(", "),
|
|
339
|
+
display: false,
|
|
340
|
+
details: {
|
|
341
|
+
mailboxIds: notifications.map((notification) => notification.details.mailboxId),
|
|
342
|
+
},
|
|
343
|
+
}
|
|
344
|
+
: {
|
|
345
|
+
customType: ROOT_WAKE_MESSAGE,
|
|
346
|
+
content: "Asynchronous subagent event(s) arrived (see the preceding subagent notifications). " +
|
|
347
|
+
"Collect results with agent_output and continue any unfinished work; recap when the overall task is complete. " +
|
|
348
|
+
"For a timeout event decide to wait, steer, stop or re-arm with agent_steer using path and timeoutMs only " +
|
|
349
|
+
"(omit message; resets the reporting timer from now without interrupting the running agent).",
|
|
350
|
+
display: false,
|
|
351
|
+
details: {},
|
|
352
|
+
}, { triggerTurn: true });
|
|
353
|
+
}
|
|
354
|
+
catch (error) {
|
|
355
|
+
rootTurnRunning = false;
|
|
356
|
+
wakeRequested = wasWakeRequested;
|
|
357
|
+
warnDelivery(error);
|
|
358
|
+
}
|
|
359
|
+
}, ctx.isIdle() === false ? 100 : 0);
|
|
360
|
+
rootTurnTimer.unref?.();
|
|
361
|
+
};
|
|
362
|
+
const drainRootMailbox = (ctx) => {
|
|
363
|
+
if (limits.subagentMode === "off" || ctx.isIdle?.() === false)
|
|
364
|
+
return;
|
|
365
|
+
for (const notification of pendingRootNotifications(ctx))
|
|
366
|
+
sendRootNotification(notification);
|
|
367
|
+
};
|
|
368
|
+
const restoreRootMailbox = (ctx) => {
|
|
369
|
+
drainRootMailbox(ctx);
|
|
370
|
+
scheduleRootTurn(ctx);
|
|
371
|
+
};
|
|
372
|
+
pi.on("agent_settled", async (_event, ctx) => {
|
|
373
|
+
if (context?.sessionManager === ctx.sessionManager) {
|
|
374
|
+
rootTurnRunning = false;
|
|
375
|
+
restoreRootMailbox(ctx);
|
|
376
|
+
}
|
|
377
|
+
});
|
|
378
|
+
const delivery = (event) => {
|
|
379
|
+
if (event.kind === "change" || event.kind === "metrics")
|
|
380
|
+
return;
|
|
381
|
+
const thread = event.thread;
|
|
382
|
+
const fmtMs = (ms) => `${Math.round(ms / 1000)}s`;
|
|
383
|
+
const elapsed = (thread.elapsedMs ?? 0) + (thread.startedAt ? Date.now() - thread.startedAt : 0);
|
|
384
|
+
const message = event.kind === "diagnostic"
|
|
385
|
+
? diagnosticAgentText(thread.path, event.diagnostic)
|
|
386
|
+
: event.kind === "update"
|
|
387
|
+
? `Progress from ${thread.path}: ${event.message}`
|
|
388
|
+
: event.kind === "timeout"
|
|
389
|
+
? `Agent ${thread.path} hit its ${fmtMs(event.timeoutMs)} spawn timeout and is STILL RUNNING (not stopped). ` +
|
|
390
|
+
`State: ${thread.state}; status: ${thread.status}; elapsed: ${fmtMs(elapsed)}; ` +
|
|
391
|
+
`tokens: ${thread.inputTokens ?? 0} in / ${thread.outputTokens ?? 0} out.\n` +
|
|
392
|
+
`Recent output:\n${event.recentOutput || "(none yet)"}\n` +
|
|
393
|
+
"Decide: agent_steer with timeoutMs only (no message) to be notified again later without disturbing it, agent_wait for it, agent_steer to redirect, agent_stop to cancel, or let it continue."
|
|
394
|
+
: thread.state === "completed"
|
|
395
|
+
? `Agent ${thread.path} completed. Final answer:\n${thread.output?.slice(0, 16000) ?? "(no text)"}${(thread.output?.length ?? 0) > 16000 ? "\n[Output truncated; use agent_output for more.]" : ""}`
|
|
396
|
+
: `Agent ${thread.path} is ${thread.state}: ${thread.status}. ${thread.state === "paused" ? "No answer handback; send input to resume the same session." : "Session retained for further input."}`;
|
|
397
|
+
try {
|
|
398
|
+
if (event.recipient === "/root") {
|
|
399
|
+
const recap = limits.finalRecap &&
|
|
400
|
+
!suspendingSummaries &&
|
|
401
|
+
event.kind === "settled" &&
|
|
402
|
+
event.async &&
|
|
403
|
+
["completed", "failed", "stopped"].includes(thread.state);
|
|
404
|
+
const notification = {
|
|
405
|
+
rootSessionId: requireContext().sessionManager.getSessionId(),
|
|
406
|
+
content: message,
|
|
407
|
+
finalRecap: recap,
|
|
408
|
+
trigger: !suspendingSummaries &&
|
|
409
|
+
(event.kind === "timeout" || (event.kind === "settled" && event.async && !recap)),
|
|
410
|
+
details: {
|
|
411
|
+
mailboxId: randomUUID(),
|
|
412
|
+
path: thread.path,
|
|
413
|
+
state: thread.state,
|
|
414
|
+
},
|
|
415
|
+
};
|
|
416
|
+
pi.appendEntry(ROOT_MAILBOX_ENTRY, notification);
|
|
417
|
+
restoreRootMailbox(requireContext());
|
|
418
|
+
}
|
|
419
|
+
else {
|
|
420
|
+
void requireManager().deliver(event.recipient, message).catch(warnDelivery);
|
|
421
|
+
if (event.kind === "settled")
|
|
422
|
+
restoreRootMailbox(requireContext());
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
catch (error) {
|
|
426
|
+
warnDelivery(error);
|
|
427
|
+
}
|
|
428
|
+
};
|
|
429
|
+
const makeDriverFactory = () => createDriverFactory(requireContext, () => limits.modelSelection, () => limits.toolFiltering, () => inheritedTools.snapshot(), () => limits.subagentExtensions);
|
|
430
|
+
let driverFactory = makeDriverFactory();
|
|
431
|
+
const toolsFor = (path) => agentTools(requireManager, path, () => store.list(), (type) => driverFactory.resolveAgentSettings(type, path));
|
|
432
|
+
const rootTools = toolsFor("/root");
|
|
433
|
+
let toolsEnabled;
|
|
434
|
+
const syncTools = () => {
|
|
435
|
+
const enabled = limits.subagentMode !== "off";
|
|
436
|
+
if (toolsEnabled === enabled)
|
|
437
|
+
return;
|
|
438
|
+
toolsEnabled = enabled;
|
|
439
|
+
for (const tool of rootTools) {
|
|
440
|
+
pi.registerTool({
|
|
441
|
+
...tool,
|
|
442
|
+
exposure: enabled ? "direct" : "hidden",
|
|
443
|
+
prepareLoadout: inheritedTools.captureLoadout,
|
|
444
|
+
execute: (...args) => {
|
|
445
|
+
if (limits.subagentMode === "off")
|
|
446
|
+
throw new Error("Subagent Mode is off");
|
|
447
|
+
inheritedTools.captureContext(args[4]);
|
|
448
|
+
return tool.execute(...args);
|
|
449
|
+
},
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
};
|
|
453
|
+
const refreshWidget = (ctx) => {
|
|
454
|
+
if (limits.subagentMode === "off") {
|
|
455
|
+
if (ctx.hasUI)
|
|
456
|
+
ctx.ui.setWidget("pi-subagent", undefined);
|
|
457
|
+
}
|
|
458
|
+
else
|
|
459
|
+
updateWidget(ctx, widgetThreads(), widgetCollapsed ? "minimal" : limits.widgetMode, limits.nerdFontIcons, rootTurnEnded, limits.loaderStyle, widgetCollapsed && limits.widgetMode === "full");
|
|
460
|
+
};
|
|
461
|
+
pi.on("agent_start", async (_event, ctx) => {
|
|
462
|
+
if (manager && !rootTurnRunning) {
|
|
463
|
+
for (const thread of manager.list()) {
|
|
464
|
+
if (thread.state !== "starting" && thread.state !== "running")
|
|
465
|
+
hiddenWidgetThreads.add(thread.path);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
rootTurnEnded = false;
|
|
469
|
+
if (manager)
|
|
470
|
+
refreshWidget(ctx);
|
|
471
|
+
});
|
|
472
|
+
pi.on("agent_end", async (_event, ctx) => {
|
|
473
|
+
rootTurnEnded = true;
|
|
474
|
+
if (manager)
|
|
475
|
+
refreshWidget(ctx);
|
|
476
|
+
refreshCostFooter(ctx);
|
|
477
|
+
if (limits.subagentMode === "off" || !migrationRequested)
|
|
478
|
+
return;
|
|
479
|
+
store.reload();
|
|
480
|
+
if (store.diagnostics.length)
|
|
481
|
+
context?.ui.notify(store.diagnostics.join("\n"), "warning");
|
|
482
|
+
});
|
|
483
|
+
pi.on("message_end", async (_event, ctx) => {
|
|
484
|
+
refreshCostFooter(ctx);
|
|
485
|
+
});
|
|
486
|
+
pi.on("tool_execution_end", async (_event, ctx) => {
|
|
487
|
+
refreshCostFooter(ctx);
|
|
488
|
+
});
|
|
489
|
+
syncTools();
|
|
490
|
+
pi.on("before_agent_start", async (event, ctx) => {
|
|
491
|
+
const prompt = subagentPrompt(limits, limits.checklistIntegration &&
|
|
492
|
+
limits.subagentMode !== "off" &&
|
|
493
|
+
currentChecklistStatus(ctx).eligible);
|
|
494
|
+
if (!prompt)
|
|
495
|
+
return;
|
|
496
|
+
if (limits.subagentMode === "opportunistic" &&
|
|
497
|
+
"prompt" in event &&
|
|
498
|
+
event.prompt.startsWith(IMPORT_REQUEST_PREFIX)) {
|
|
499
|
+
try {
|
|
500
|
+
markImportOffered(getAgentDir());
|
|
501
|
+
}
|
|
502
|
+
catch (error) {
|
|
503
|
+
ctx.ui.notify(`Could not save import onboarding state: ${String(error)}`, "warning");
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
return {
|
|
507
|
+
systemPrompt: `${event.systemPrompt}\n\n${prompt}`,
|
|
508
|
+
};
|
|
509
|
+
});
|
|
510
|
+
const attachSession = async (ctx, installEditor = false, seedHistory = true) => {
|
|
511
|
+
cancelSummary();
|
|
512
|
+
housekeeping.cancelAll();
|
|
513
|
+
const token = ++generation;
|
|
514
|
+
closeNavigation(installEditor);
|
|
515
|
+
navigation = new AgentNavigationController(returnToWidget);
|
|
516
|
+
if (manager)
|
|
517
|
+
await manager.shutdown();
|
|
518
|
+
inheritedTools.reset();
|
|
519
|
+
checklistMirror.reset(ctx);
|
|
520
|
+
context = ctx;
|
|
521
|
+
costLedger = new CostLedger();
|
|
522
|
+
costLedger.restore(ctx.sessionManager.getEntries());
|
|
523
|
+
costFooter.reset(ctx);
|
|
524
|
+
rootTurnEnded = false;
|
|
525
|
+
if (installEditor) {
|
|
526
|
+
widgetCollapsed = false;
|
|
527
|
+
hiddenWidgetThreads.clear();
|
|
528
|
+
}
|
|
529
|
+
store = new ConfigStore({
|
|
530
|
+
cwd: ctx.cwd,
|
|
531
|
+
agentDir: getAgentDir(),
|
|
532
|
+
includeProject: ctx.isProjectTrusted(),
|
|
533
|
+
});
|
|
534
|
+
registryWriter.reset();
|
|
535
|
+
const settingsDiagnostics = loadLimits(ctx);
|
|
536
|
+
syncTools();
|
|
537
|
+
driverFactory = makeDriverFactory();
|
|
538
|
+
const instance = new ThreadManager({
|
|
539
|
+
...limits,
|
|
540
|
+
createDriver: driverFactory,
|
|
541
|
+
rootSnapshot: () => buildSessionContext(requireContext().sessionManager.getBranch()).messages,
|
|
542
|
+
rootContextEntries: () => inheritedContextEntries(requireContext().sessionManager.getBranch()),
|
|
543
|
+
getType: (name) => store.get(name),
|
|
544
|
+
toolsFor,
|
|
545
|
+
onEventError: (error, event) => {
|
|
546
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
547
|
+
context?.ui.notify(`Subagent ${event.kind} notification failed: ${message}`, "warning");
|
|
548
|
+
},
|
|
549
|
+
onActivity: (activity) => {
|
|
550
|
+
if (token === generation)
|
|
551
|
+
housekeeping.notify(activity);
|
|
552
|
+
},
|
|
553
|
+
onEvent: (event) => {
|
|
554
|
+
if (token !== generation)
|
|
555
|
+
return;
|
|
556
|
+
if (event.kind === "update" ||
|
|
557
|
+
(event.thread.state !== "starting" && event.thread.state !== "running"))
|
|
558
|
+
housekeeping.settle(event.thread.path);
|
|
559
|
+
refreshWidget(requireContext());
|
|
560
|
+
if (event.kind !== "metrics")
|
|
561
|
+
persist();
|
|
562
|
+
if (event.kind === "settled")
|
|
563
|
+
rollupCost(event.thread);
|
|
564
|
+
else
|
|
565
|
+
refreshCostFooter(requireContext());
|
|
566
|
+
delivery(event);
|
|
567
|
+
},
|
|
568
|
+
});
|
|
569
|
+
manager = instance;
|
|
570
|
+
if (installEditor)
|
|
571
|
+
installNavigationEditor(ctx, seedHistory);
|
|
572
|
+
try {
|
|
573
|
+
const saved = readRegistry(ctx.sessionManager.getBranch());
|
|
574
|
+
if (saved?.rootSessionId === ctx.sessionManager.getSessionId())
|
|
575
|
+
instance.restore(saved.threads);
|
|
576
|
+
else if (saved)
|
|
577
|
+
persist();
|
|
578
|
+
}
|
|
579
|
+
catch (error) {
|
|
580
|
+
if (limits.subagentMode !== "off")
|
|
581
|
+
ctx.ui.notify(`Could not restore subagent registry: ${String(error)}`, "error");
|
|
582
|
+
}
|
|
583
|
+
for (const saved of instance.saved())
|
|
584
|
+
rollupCost({ ...saved.view, updatedAt: Date.now() });
|
|
585
|
+
restoreRootMailbox(ctx);
|
|
586
|
+
refreshWidget(ctx);
|
|
587
|
+
const diagnostics = [...store.diagnostics, ...settingsDiagnostics];
|
|
588
|
+
if (limits.subagentMode !== "off" && diagnostics.length)
|
|
589
|
+
ctx.ui.notify(diagnostics.join("\n"), "warning");
|
|
590
|
+
};
|
|
591
|
+
pi.on("session_start", async (event, ctx) => {
|
|
592
|
+
capabilityActive = true;
|
|
593
|
+
await attachSession(ctx, true, event.reason !== "startup");
|
|
594
|
+
if (limits.subagentMode !== "off")
|
|
595
|
+
await importAgents(ctx, true);
|
|
596
|
+
});
|
|
597
|
+
pi.on("session_tree", async (_event, ctx) => attachSession(ctx));
|
|
598
|
+
const stopWorkingThreads = async () => {
|
|
599
|
+
cancelSummary();
|
|
600
|
+
closeNavigation();
|
|
601
|
+
suspendingSummaries = true;
|
|
602
|
+
try {
|
|
603
|
+
if (!manager)
|
|
604
|
+
return;
|
|
605
|
+
for (const thread of manager.list()) {
|
|
606
|
+
const state = manager.get(thread.path).state;
|
|
607
|
+
if (state === "starting" || state === "running")
|
|
608
|
+
await manager.stop("/root", thread.path);
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
finally {
|
|
612
|
+
cancelSummary();
|
|
613
|
+
suspendingSummaries = false;
|
|
614
|
+
}
|
|
615
|
+
};
|
|
616
|
+
pi.on("session_before_tree", stopWorkingThreads);
|
|
617
|
+
pi.on("session_before_switch", stopWorkingThreads);
|
|
618
|
+
pi.on("session_before_fork", stopWorkingThreads);
|
|
619
|
+
pi.on("session_shutdown", async () => {
|
|
620
|
+
capabilityActive = false;
|
|
621
|
+
cancelSummary();
|
|
622
|
+
housekeeping.cancelAll();
|
|
623
|
+
generation++;
|
|
624
|
+
closeNavigation(true);
|
|
625
|
+
await manager?.shutdown();
|
|
626
|
+
costFooter.clear();
|
|
627
|
+
registryWriter.reset();
|
|
628
|
+
persist();
|
|
629
|
+
if (context?.hasUI)
|
|
630
|
+
context.ui.setWidget("pi-subagent", undefined);
|
|
631
|
+
manager = undefined;
|
|
632
|
+
context = undefined;
|
|
633
|
+
});
|
|
634
|
+
const agentSubcommands = ["tree", "settings", "types", "import", "reload", "reap"];
|
|
635
|
+
const spawnModes = ["fork", "fresh"];
|
|
636
|
+
const spawnFromCommand = async (ctx, mode, typeName, task) => {
|
|
637
|
+
if (limits.subagentMode === "off") {
|
|
638
|
+
ctx.ui.notify("Subagent Mode is off. Change it in /agents settings.", "warning");
|
|
639
|
+
return;
|
|
640
|
+
}
|
|
641
|
+
const current = requireManager();
|
|
642
|
+
if (!store.list().some((type) => type.name === typeName)) {
|
|
643
|
+
ctx.ui.notify(`Unknown agent type: ${typeName}`, "error");
|
|
644
|
+
return;
|
|
645
|
+
}
|
|
646
|
+
const base = typeName.replace(/[^A-Za-z0-9_-]/g, "-").replace(/^[^A-Za-z0-9]+/, "") || "agent";
|
|
647
|
+
const taken = new Set(current.list().map((thread) => thread.path));
|
|
648
|
+
const prefix = mode === "fork" ? "/root/" : "/";
|
|
649
|
+
let n = 1;
|
|
650
|
+
while (taken.has(`${prefix}${base}-${n}`))
|
|
651
|
+
n++;
|
|
652
|
+
const path = `${prefix}${base}-${n}`;
|
|
653
|
+
try {
|
|
654
|
+
await current.scope("/root").spawn({ path, type: typeName, task, wait: false });
|
|
655
|
+
persist();
|
|
656
|
+
refreshWidget(ctx);
|
|
657
|
+
ctx.ui.notify(`Started ${typeName} (${mode === "fork" ? "forked context" : "fresh context"}) at ${path}`, "info");
|
|
658
|
+
}
|
|
659
|
+
catch (error) {
|
|
660
|
+
ctx.ui.notify(error instanceof Error ? error.message : String(error), "error");
|
|
661
|
+
}
|
|
662
|
+
};
|
|
663
|
+
pi.registerCommand("agents", {
|
|
664
|
+
description: "Configure agent settings, show the live tree, or run /agents fork:<agent> | fresh:<agent> <task>",
|
|
665
|
+
getArgumentCompletions: (argumentText) => {
|
|
666
|
+
const text = argumentText.trimStart();
|
|
667
|
+
const completed = text.match(/^(\S+)(\s+)([\s\S]*)$/);
|
|
668
|
+
if (completed) {
|
|
669
|
+
const command = completed[1].toLowerCase();
|
|
670
|
+
if (command === "tree") {
|
|
671
|
+
const paths = (manager?.list() ?? [])
|
|
672
|
+
.map((thread) => thread.path)
|
|
673
|
+
.filter((path) => path !== "/root");
|
|
674
|
+
const matched = fuzzyFilter(paths, completed[3].trimStart(), (path) => path);
|
|
675
|
+
if (matched.length === 0)
|
|
676
|
+
return null;
|
|
677
|
+
return matched.map((path) => ({
|
|
678
|
+
value: `tree ${path}`,
|
|
679
|
+
label: path,
|
|
680
|
+
}));
|
|
681
|
+
}
|
|
682
|
+
if (agentSubcommands.includes(command))
|
|
683
|
+
return null;
|
|
684
|
+
if (/^(fork|fresh):/.test(command))
|
|
685
|
+
return null;
|
|
686
|
+
}
|
|
687
|
+
const lower = text.toLowerCase();
|
|
688
|
+
const colon = lower.indexOf(":");
|
|
689
|
+
if (lower === "f")
|
|
690
|
+
return spawnModes.map((mode) => ({ value: `${mode}:`, label: `${mode}:` }));
|
|
691
|
+
const modes = spawnModes.filter((mode) => colon >= 0 ? lower.slice(0, colon) === mode : lower.length >= 2 && mode.startsWith(lower));
|
|
692
|
+
if (modes.length) {
|
|
693
|
+
const names = store.list().map((type) => type.name);
|
|
694
|
+
const filter = colon >= 0 ? lower.slice(colon + 1) : "";
|
|
695
|
+
const matched = filter ? fuzzyFilter(names, filter, (name) => name) : names;
|
|
696
|
+
const out = modes.flatMap((mode) => matched.map((name) => ({
|
|
697
|
+
value: `${mode}:${name}`,
|
|
698
|
+
label: `${mode}:${name}`,
|
|
699
|
+
})));
|
|
700
|
+
if (out.length)
|
|
701
|
+
return out;
|
|
702
|
+
}
|
|
703
|
+
const matched = fuzzyFilter([...agentSubcommands], text, (name) => name);
|
|
704
|
+
if (matched.length === 0)
|
|
705
|
+
return null;
|
|
706
|
+
return matched.map((value) => ({ value, label: value }));
|
|
707
|
+
},
|
|
708
|
+
handler: async (args, ctx) => {
|
|
709
|
+
const [command, ...rest] = args.trim().split(/\s+/);
|
|
710
|
+
const spawnMatch = args.trim().match(/^(fork|fresh):(\S+)(?:\s+([\s\S]*))?$/);
|
|
711
|
+
if (spawnMatch) {
|
|
712
|
+
const task = spawnMatch[3]?.trim();
|
|
713
|
+
if (!task)
|
|
714
|
+
ctx.ui.notify(`Usage: /agents ${spawnMatch[1]}:${spawnMatch[2]} <task>`, "warning");
|
|
715
|
+
else
|
|
716
|
+
await spawnFromCommand(ctx, spawnMatch[1], spawnMatch[2], task);
|
|
717
|
+
}
|
|
718
|
+
else if (command === "import") {
|
|
719
|
+
await importAgents(ctx, false);
|
|
720
|
+
}
|
|
721
|
+
else if (command === "types") {
|
|
722
|
+
await editAgentTypes(ctx, store, limits.nerdFontIcons, {
|
|
723
|
+
getAllTools: () => pi.getAllTools(),
|
|
724
|
+
toolFiltering: limits.toolFiltering,
|
|
725
|
+
resolveAgentSettings: (type) => driverFactory.resolveAgentSettings(type, "/root"),
|
|
726
|
+
});
|
|
727
|
+
store.reload();
|
|
728
|
+
}
|
|
729
|
+
else if (command === "reload") {
|
|
730
|
+
store.reload();
|
|
731
|
+
const diagnostics = [...store.diagnostics, ...loadLimits(ctx)];
|
|
732
|
+
requireManager().setLimits(limits);
|
|
733
|
+
syncTools();
|
|
734
|
+
refreshWidget(ctx);
|
|
735
|
+
restoreRootMailbox(ctx);
|
|
736
|
+
ctx.ui.notify(diagnostics.length
|
|
737
|
+
? diagnostics.join("\n")
|
|
738
|
+
: `Loaded ${store.list().length} agent types; maximum ${limits.maxLevels} levels`, diagnostics.length ? "warning" : "info");
|
|
739
|
+
}
|
|
740
|
+
else if (!command || command === "settings") {
|
|
741
|
+
await configureAgents(ctx, {
|
|
742
|
+
store,
|
|
743
|
+
settings: limits,
|
|
744
|
+
agentDir: getAgentDir(),
|
|
745
|
+
getAllTools: () => pi.getAllTools(),
|
|
746
|
+
checklistStatus: () => currentChecklistStatus(ctx),
|
|
747
|
+
resolveAgentSettings: (type) => driverFactory.resolveAgentSettings(type, "/root"),
|
|
748
|
+
apply: () => {
|
|
749
|
+
const diagnostics = loadLimits(ctx);
|
|
750
|
+
requireManager().setLimits(limits);
|
|
751
|
+
syncTools();
|
|
752
|
+
refreshWidget(ctx);
|
|
753
|
+
restoreRootMailbox(ctx);
|
|
754
|
+
if (diagnostics.length)
|
|
755
|
+
ctx.ui.notify(diagnostics.join("\n"), "warning");
|
|
756
|
+
},
|
|
757
|
+
});
|
|
758
|
+
}
|
|
759
|
+
else if (command === "reap") {
|
|
760
|
+
const current = requireManager();
|
|
761
|
+
const candidates = current.reapCandidates();
|
|
762
|
+
if (!candidates.length) {
|
|
763
|
+
ctx.ui.notify("No completed agents eligible for reaping.", "info");
|
|
764
|
+
return;
|
|
765
|
+
}
|
|
766
|
+
if (!canOpenDialog(ctx))
|
|
767
|
+
return;
|
|
768
|
+
closeNavigation();
|
|
769
|
+
const warning = `${candidates.length} completed agent${candidates.length === 1 ? "" : "s"} will be reaped.\nYou will not be able to resume them anymore.`;
|
|
770
|
+
const choice = await dialogMenu(ctx, "Reap completed agents?", [
|
|
771
|
+
{ id: "confirm", label: "Confirm", help: warning },
|
|
772
|
+
{ id: "cancel", label: "Cancel", help: warning },
|
|
773
|
+
], { selectedId: "cancel" });
|
|
774
|
+
if (choice !== "confirm" || manager !== current)
|
|
775
|
+
return;
|
|
776
|
+
const reaped = current.reap(candidates);
|
|
777
|
+
persist();
|
|
778
|
+
refreshWidget(ctx);
|
|
779
|
+
ctx.ui.notify(reaped.length
|
|
780
|
+
? `Reaped ${reaped.length} completed agent(s); their sessions can no longer be resumed.`
|
|
781
|
+
: "No completed agents eligible for reaping.", "info");
|
|
782
|
+
}
|
|
783
|
+
else if (command === "tree") {
|
|
784
|
+
await navigation.open(ctx, requireManager().scope("/root"), rest.join(" ") || undefined, limits.nerdFontIcons, limits.loaderStyle);
|
|
785
|
+
}
|
|
786
|
+
else
|
|
787
|
+
ctx.ui.notify("Usage: /agents [tree [path] | settings | types | import | reload | reap | fork:<agent> <task> | fresh:<agent> <task>]", "warning");
|
|
788
|
+
},
|
|
789
|
+
});
|
|
790
|
+
}
|