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
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { Type } from "typebox";
|
|
2
|
+
import { ModelPreferenceError } from "../prefs/models.js";
|
|
3
|
+
const path = Type.String({ description: "Absolute agent path, or name relative to the caller" });
|
|
4
|
+
const text = Type.String({ minLength: 1 });
|
|
5
|
+
function compact(thread) {
|
|
6
|
+
if (thread.output && thread.output.length > 16000)
|
|
7
|
+
return { ...thread, output: thread.output.slice(0, 16000), outputTruncated: true };
|
|
8
|
+
return thread;
|
|
9
|
+
}
|
|
10
|
+
function result(value) {
|
|
11
|
+
return {
|
|
12
|
+
content: [{ type: "text", text: JSON.stringify(value, null, 2) }],
|
|
13
|
+
details: value,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export function agentTools(getManager, caller, getTypes, resolveSettings) {
|
|
17
|
+
const threads = () => getManager().scope(caller);
|
|
18
|
+
const make = (name, description, parameters, execute) => ({
|
|
19
|
+
name,
|
|
20
|
+
label: name,
|
|
21
|
+
description,
|
|
22
|
+
parameters,
|
|
23
|
+
execute: async (_id, params, signal) => execute(params, signal),
|
|
24
|
+
});
|
|
25
|
+
return [
|
|
26
|
+
make("agent_types", "List agent names, descriptions, resolved models and thinking levels for new children of this caller. Reflects current configuration; call before choosing a type. Other parent paths may inherit different settings.", Type.Object({}), () => {
|
|
27
|
+
const entries = getTypes().map((type) => {
|
|
28
|
+
const { name, description } = type;
|
|
29
|
+
try {
|
|
30
|
+
return { name, description, ...resolveSettings(type) };
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
return {
|
|
34
|
+
name,
|
|
35
|
+
description,
|
|
36
|
+
error: error instanceof ModelPreferenceError
|
|
37
|
+
? error.summary
|
|
38
|
+
: error instanceof Error
|
|
39
|
+
? error.message
|
|
40
|
+
: String(error),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
return {
|
|
45
|
+
content: [
|
|
46
|
+
{
|
|
47
|
+
type: "text",
|
|
48
|
+
text: entries
|
|
49
|
+
.map((entry) => "error" in entry
|
|
50
|
+
? `${entry.name} (unavailable): ${entry.description}\n ${entry.error}`
|
|
51
|
+
: `${entry.name} (${entry.model}, thinking: ${entry.thinkingLevel}): ${entry.description}`)
|
|
52
|
+
.join("\n") || "No agent types available.",
|
|
53
|
+
},
|
|
54
|
+
],
|
|
55
|
+
details: entries,
|
|
56
|
+
};
|
|
57
|
+
}),
|
|
58
|
+
make("agent_spawn", "Spawn a named child. The lexical parent, NOT the caller, supplies a context snapshot. /root is the main thread; independent roots have no inherited context. Foreground waits by default; wait:false runs detached: when it completes, fails or is stopped, you receive a message event that wakes you, so do not poll. Optional timeoutMs (30000–300000; none if unset): if the child is still running after that many ms you receive an event with its status, elapsed time, tokens and recent output (the child is NOT stopped; you decide whether to wait, steer, stop it or re-arm with agent_steer timeoutMs). For parallel work, spawn every independent sibling with wait:false before calling agent_wait; child agents with delegation tools should do the same. Existing paths are retained and can be resumed with agent_steer. Use agent_types to discover current types.", Type.Object({
|
|
59
|
+
path: Type.String({
|
|
60
|
+
description: "Task-based kebab-case path, not the agent type (e.g. /root/controller-security-research), or a task name relative to the caller",
|
|
61
|
+
}),
|
|
62
|
+
type: text,
|
|
63
|
+
task: text,
|
|
64
|
+
wait: Type.Optional(Type.Boolean()),
|
|
65
|
+
timeoutMs: Type.Optional(Type.Integer({
|
|
66
|
+
minimum: 30000,
|
|
67
|
+
maximum: 300000,
|
|
68
|
+
description: "Optional report-only timeout, 30000–300000 ms (30s–5min). If the child is still running after this many ms, a status event is sent to you; the child keeps running.",
|
|
69
|
+
})),
|
|
70
|
+
}), async (params, signal) => result(compact(await threads().spawn(params, signal)))),
|
|
71
|
+
make("agent_wait", "Wait for a descendant to complete, fail, stop or pause. Paused means no final answer handback. Cancellation/timeouts do not stop the child; poll status or wait again.", Type.Object({
|
|
72
|
+
path,
|
|
73
|
+
timeoutMs: Type.Optional(Type.Integer({ minimum: 0, maximum: 3600000 })),
|
|
74
|
+
}), async (params, signal) => result(compact(await threads().wait(params.path, params.timeoutMs, signal)))),
|
|
75
|
+
make("agent_steer", 'Send input to a descendant and/or re-arm its report-only timeout. With message: working threads receive queued steering; completed, stopped and paused threads resume the SAME session with its previous context. With timeoutMs (30000–300000): the timeout fires that many ms from now, replacing any pending timer. To extend a running background agent\'s reporting timer without sending input, call agent_steer({path: "/root/task", timeoutMs: 120000}) and omit message entirely. This resets the timer to 120000 ms from now, not an addition to the old deadline. timeoutMs WITHOUT message only re-arms the timeout of a running child (error if not running) and does not steer, interrupt or resume it; with both, steer and re-arm. At least one is required. Returns immediately; use agent_wait for the result.', Type.Object({
|
|
76
|
+
path,
|
|
77
|
+
message: Type.Optional(Type.String({
|
|
78
|
+
minLength: 1,
|
|
79
|
+
description: "Input to send or resume with. Omit entirely for a timer-only re-arm.",
|
|
80
|
+
})),
|
|
81
|
+
timeoutMs: Type.Optional(Type.Integer({
|
|
82
|
+
minimum: 30000,
|
|
83
|
+
maximum: 300000,
|
|
84
|
+
description: "Reset the report-only timer to this many ms from now, replacing any pending timer. With no message, only re-arms a running agent; does not send input, interrupt or resume it.",
|
|
85
|
+
})),
|
|
86
|
+
}), async (params) => result(compact(await threads().steer(params.path, params.message, params.timeoutMs)))),
|
|
87
|
+
make("agent_status", "Read a thread's status, including bounded child extension diagnostics, or list visible descendants. Root can inspect all trees. Completion retains the session; pause retains it without final answer handback.", Type.Object({ path: Type.Optional(path) }), (params) => result(params.path ? compact(threads().get(params.path)) : threads().list().map(compact))),
|
|
88
|
+
make("agent_update", "Child-only: send progress to the parent without ending this task. Delivery is recorded and displayed but does not interrupt or automatically restart the parent model.", Type.Object({ message: Type.String({ minLength: 1, maxLength: 8000 }) }), (params) => result(compact(threads().update(params.message)))),
|
|
89
|
+
make("agent_pause", "Child-only: stop at this turn boundary WITHOUT returning a final answer. Keep the SAME session for user/parent input via agent_steer. To finish and hand back an answer instead, simply return your final assistant answer. Both states can be resumed.", Type.Object({ reason: text }), (params) => ({
|
|
90
|
+
...result({
|
|
91
|
+
path: caller,
|
|
92
|
+
pauseRequested: true,
|
|
93
|
+
status: threads().pause(params.reason).status,
|
|
94
|
+
}),
|
|
95
|
+
terminate: true,
|
|
96
|
+
})),
|
|
97
|
+
make("agent_stop", "Stop a descendant and its working children. Session history is retained; resume with agent_steer. This is cooperative cancellation, not sandbox process termination.", Type.Object({ path }), async (params) => result(compact(await threads().stop(params.path)))),
|
|
98
|
+
make("agent_output", "Read completed thread output in character pages. Paused threads deliberately have no final answer handback. All sessions can be viewed by the user with /agents tree; select a thread and press Enter to inspect it.", Type.Object({
|
|
99
|
+
path,
|
|
100
|
+
offset: Type.Optional(Type.Integer({ minimum: 0 })),
|
|
101
|
+
limit: Type.Optional(Type.Integer({ minimum: 1, maximum: 16000 })),
|
|
102
|
+
}), (params) => {
|
|
103
|
+
const output = threads().output(params.path), offset = params.offset ?? 0, limit = params.limit ?? 8000;
|
|
104
|
+
return result({
|
|
105
|
+
path: threads().get(params.path).path,
|
|
106
|
+
text: output.slice(offset, offset + limit),
|
|
107
|
+
totalCharacters: output.length,
|
|
108
|
+
nextOffset: offset + limit < output.length ? offset + limit : null,
|
|
109
|
+
});
|
|
110
|
+
}),
|
|
111
|
+
];
|
|
112
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
export function immutableTranscriptValue(value) {
|
|
2
|
+
const copy = structuredClone(value);
|
|
3
|
+
const seen = new WeakSet();
|
|
4
|
+
const freeze = (item) => {
|
|
5
|
+
if (!item || typeof item !== "object" || seen.has(item))
|
|
6
|
+
return;
|
|
7
|
+
seen.add(item);
|
|
8
|
+
for (const child of Object.values(item))
|
|
9
|
+
freeze(child);
|
|
10
|
+
if (!ArrayBuffer.isView(item))
|
|
11
|
+
Object.freeze(item);
|
|
12
|
+
};
|
|
13
|
+
freeze(copy);
|
|
14
|
+
return copy;
|
|
15
|
+
}
|
|
16
|
+
export class TranscriptChannel {
|
|
17
|
+
readMessages;
|
|
18
|
+
inheritedCount;
|
|
19
|
+
revision = 0;
|
|
20
|
+
messages;
|
|
21
|
+
assistant = null;
|
|
22
|
+
tools = new Map();
|
|
23
|
+
listeners = new Set();
|
|
24
|
+
current;
|
|
25
|
+
disposed = false;
|
|
26
|
+
error;
|
|
27
|
+
constructor(readMessages, inheritedCount) {
|
|
28
|
+
this.readMessages = readMessages;
|
|
29
|
+
this.inheritedCount = inheritedCount;
|
|
30
|
+
this.messages = immutableTranscriptValue(readMessages());
|
|
31
|
+
this.current = this.snapshot();
|
|
32
|
+
}
|
|
33
|
+
observe(listener) {
|
|
34
|
+
if (this.disposed)
|
|
35
|
+
throw new Error("Transcript channel is disposed");
|
|
36
|
+
const registered = (snapshot) => listener(snapshot);
|
|
37
|
+
this.listeners.add(registered);
|
|
38
|
+
return { snapshot: this.current, unsubscribe: () => this.listeners.delete(registered) };
|
|
39
|
+
}
|
|
40
|
+
accept(event) {
|
|
41
|
+
if (this.disposed)
|
|
42
|
+
return;
|
|
43
|
+
try {
|
|
44
|
+
this.reduce(event);
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
this.error = error instanceof Error ? error.message : String(error);
|
|
48
|
+
this.publish();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
reduce(event) {
|
|
52
|
+
this.error = undefined;
|
|
53
|
+
switch (event.type) {
|
|
54
|
+
case "message_start":
|
|
55
|
+
case "message_update":
|
|
56
|
+
if (event.message.role !== "assistant")
|
|
57
|
+
return;
|
|
58
|
+
this.assistant = immutableTranscriptValue(event.message);
|
|
59
|
+
break;
|
|
60
|
+
case "message_end":
|
|
61
|
+
if (event.message.role === "assistant")
|
|
62
|
+
this.assistant = null;
|
|
63
|
+
if (event.message.role === "toolResult")
|
|
64
|
+
this.tools.delete(event.message.toolCallId);
|
|
65
|
+
this.messages = immutableTranscriptValue(this.readMessages());
|
|
66
|
+
break;
|
|
67
|
+
case "tool_execution_start":
|
|
68
|
+
this.tools.set(event.toolCallId, immutableTranscriptValue({
|
|
69
|
+
toolCallId: event.toolCallId,
|
|
70
|
+
toolName: event.toolName,
|
|
71
|
+
parentToolCallId: event.parentToolCallId,
|
|
72
|
+
args: event.args,
|
|
73
|
+
state: "running",
|
|
74
|
+
}));
|
|
75
|
+
break;
|
|
76
|
+
case "tool_execution_update":
|
|
77
|
+
this.tools.set(event.toolCallId, immutableTranscriptValue({
|
|
78
|
+
...this.tools.get(event.toolCallId),
|
|
79
|
+
toolCallId: event.toolCallId,
|
|
80
|
+
toolName: event.toolName,
|
|
81
|
+
parentToolCallId: event.parentToolCallId,
|
|
82
|
+
args: event.args,
|
|
83
|
+
result: event.partialResult,
|
|
84
|
+
state: "running",
|
|
85
|
+
}));
|
|
86
|
+
break;
|
|
87
|
+
case "tool_execution_end":
|
|
88
|
+
this.tools.set(event.toolCallId, immutableTranscriptValue({
|
|
89
|
+
...this.tools.get(event.toolCallId),
|
|
90
|
+
toolCallId: event.toolCallId,
|
|
91
|
+
toolName: event.toolName,
|
|
92
|
+
parentToolCallId: event.parentToolCallId,
|
|
93
|
+
args: this.tools.get(event.toolCallId)?.args,
|
|
94
|
+
result: event.result,
|
|
95
|
+
isError: event.isError,
|
|
96
|
+
state: "completed",
|
|
97
|
+
}));
|
|
98
|
+
for (const [id, tool] of this.tools) {
|
|
99
|
+
if (this.tools.size <= 128)
|
|
100
|
+
break;
|
|
101
|
+
if (tool.state === "completed")
|
|
102
|
+
this.tools.delete(id);
|
|
103
|
+
}
|
|
104
|
+
break;
|
|
105
|
+
case "compaction_end":
|
|
106
|
+
if (event.result && !event.aborted)
|
|
107
|
+
this.inheritedCount = 0;
|
|
108
|
+
this.messages = immutableTranscriptValue(this.readMessages());
|
|
109
|
+
break;
|
|
110
|
+
case "agent_end":
|
|
111
|
+
case "agent_settled":
|
|
112
|
+
this.assistant = null;
|
|
113
|
+
this.tools.clear();
|
|
114
|
+
this.messages = immutableTranscriptValue(this.readMessages());
|
|
115
|
+
break;
|
|
116
|
+
case "auto_retry_start":
|
|
117
|
+
case "auto_retry_end":
|
|
118
|
+
this.messages = immutableTranscriptValue(this.readMessages());
|
|
119
|
+
break;
|
|
120
|
+
default:
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
this.publish();
|
|
124
|
+
}
|
|
125
|
+
reconcile() {
|
|
126
|
+
if (this.disposed)
|
|
127
|
+
return;
|
|
128
|
+
try {
|
|
129
|
+
this.messages = immutableTranscriptValue(this.readMessages());
|
|
130
|
+
this.error = undefined;
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
this.error = error instanceof Error ? error.message : String(error);
|
|
134
|
+
}
|
|
135
|
+
this.publish();
|
|
136
|
+
}
|
|
137
|
+
dispose() {
|
|
138
|
+
this.disposed = true;
|
|
139
|
+
this.listeners.clear();
|
|
140
|
+
this.tools.clear();
|
|
141
|
+
this.assistant = null;
|
|
142
|
+
}
|
|
143
|
+
snapshot() {
|
|
144
|
+
return Object.freeze({
|
|
145
|
+
revision: this.revision,
|
|
146
|
+
generation: 0,
|
|
147
|
+
messages: this.messages,
|
|
148
|
+
assistant: this.assistant,
|
|
149
|
+
tools: Object.freeze([...this.tools.values()]),
|
|
150
|
+
inheritedCount: Math.min(this.inheritedCount, this.messages.length),
|
|
151
|
+
...(this.error ? { error: this.error } : {}),
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
publish() {
|
|
155
|
+
this.revision++;
|
|
156
|
+
this.current = this.snapshot();
|
|
157
|
+
for (const listener of this.listeners) {
|
|
158
|
+
try {
|
|
159
|
+
listener(this.current);
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { constants, lstatSync, mkdirSync, openSync, closeSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { AGENT_COLORS } from "./config.js";
|
|
5
|
+
import { discoverImportCandidates } from "./import-discovery.js";
|
|
6
|
+
import { selectImportAgents } from "../ui/import-picker.js";
|
|
7
|
+
import { MIGRATION_INSTRUCTIONS } from "./import-instructions.js";
|
|
8
|
+
import { dialogText } from "../ui/dialog.js";
|
|
9
|
+
export const IMPORT_REQUEST_PREFIX = "Import ONLY the agents I selected in the pi-subagent-manager migration picker.";
|
|
10
|
+
const IMPORT_CHILD_TOOLS = [
|
|
11
|
+
"read",
|
|
12
|
+
"bash",
|
|
13
|
+
"powershell",
|
|
14
|
+
"edit",
|
|
15
|
+
"write",
|
|
16
|
+
"grep",
|
|
17
|
+
"find",
|
|
18
|
+
"ls",
|
|
19
|
+
"agent_types",
|
|
20
|
+
"agent_spawn",
|
|
21
|
+
"agent_wait",
|
|
22
|
+
"agent_steer",
|
|
23
|
+
"agent_status",
|
|
24
|
+
"agent_update",
|
|
25
|
+
"agent_pause",
|
|
26
|
+
"agent_stop",
|
|
27
|
+
"agent_output",
|
|
28
|
+
];
|
|
29
|
+
const OFFER_MARKER = ".import-offered";
|
|
30
|
+
function statIfPresent(path) {
|
|
31
|
+
try {
|
|
32
|
+
const stat = lstatSync(path);
|
|
33
|
+
if (stat.isSymbolicLink())
|
|
34
|
+
throw new Error(`Unsafe import-state symlink: ${path}`);
|
|
35
|
+
return stat;
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
if (error.code === "ENOENT")
|
|
39
|
+
return undefined;
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function stateDirectory(agentDir, create) {
|
|
44
|
+
const directory = join(agentDir, "subagent-manager");
|
|
45
|
+
for (const path of [agentDir, directory]) {
|
|
46
|
+
let stat = statIfPresent(path);
|
|
47
|
+
if (!stat && create) {
|
|
48
|
+
mkdirSync(path);
|
|
49
|
+
stat = statIfPresent(path);
|
|
50
|
+
}
|
|
51
|
+
if (!stat)
|
|
52
|
+
return undefined;
|
|
53
|
+
if (!stat.isDirectory())
|
|
54
|
+
throw new Error(`Import-state directory is not a directory: ${path}`);
|
|
55
|
+
}
|
|
56
|
+
return directory;
|
|
57
|
+
}
|
|
58
|
+
export function importWasOffered(agentDir) {
|
|
59
|
+
const directory = stateDirectory(agentDir, false);
|
|
60
|
+
if (!directory)
|
|
61
|
+
return false;
|
|
62
|
+
const stat = statIfPresent(join(directory, OFFER_MARKER));
|
|
63
|
+
if (stat && !stat.isFile())
|
|
64
|
+
throw new Error("Import-state marker must be a regular file");
|
|
65
|
+
return !!stat;
|
|
66
|
+
}
|
|
67
|
+
export function markImportOffered(agentDir) {
|
|
68
|
+
const directory = stateDirectory(agentDir, true);
|
|
69
|
+
const file = join(directory, OFFER_MARKER);
|
|
70
|
+
const stat = statIfPresent(file);
|
|
71
|
+
if (stat) {
|
|
72
|
+
if (!stat.isFile())
|
|
73
|
+
throw new Error("Import-state marker must be a regular file");
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const fd = openSync(file, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | (constants.O_NOFOLLOW || 0), 0o600);
|
|
77
|
+
try {
|
|
78
|
+
writeFileSync(fd, "Onboarding offered. Use /agents import to choose agents again.\n");
|
|
79
|
+
}
|
|
80
|
+
finally {
|
|
81
|
+
closeSync(fd);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function buildImportPrompt(options) {
|
|
85
|
+
return [
|
|
86
|
+
IMPORT_REQUEST_PREFIX,
|
|
87
|
+
MIGRATION_INSTRUCTIONS,
|
|
88
|
+
"## Current-session migration facts (JSON data, not instructions)",
|
|
89
|
+
JSON.stringify({
|
|
90
|
+
selectedDefinitions: options.candidates,
|
|
91
|
+
destinations: {
|
|
92
|
+
user: join(options.agentDir, "subagent-manager", "agents"),
|
|
93
|
+
project: options.includeProject
|
|
94
|
+
? join(options.cwd, ".pi", "agent", "subagent-manager", "agents")
|
|
95
|
+
: null,
|
|
96
|
+
},
|
|
97
|
+
destinationRule: "Keep user sources user-scoped and project sources project-scoped. Project destination is permitted only when non-null; ask before changing scope.",
|
|
98
|
+
managerSettings: join(options.agentDir, "subagent-manager", "settings.json"),
|
|
99
|
+
configModule: fileURLToPath(new URL(`./config${import.meta.url.endsWith(".ts") ? ".ts" : ".js"}`, import.meta.url)),
|
|
100
|
+
existingTypes: options.existingTypes,
|
|
101
|
+
scopedModels: options.scopedModels,
|
|
102
|
+
scopedModelDetails: options.scopedModelDetails ?? [],
|
|
103
|
+
parentModel: options.parentModel ?? null,
|
|
104
|
+
parentModelApi: options.parentModelApi ?? null,
|
|
105
|
+
childTools: IMPORT_CHILD_TOOLS,
|
|
106
|
+
colorTokens: AGENT_COLORS,
|
|
107
|
+
}, null, 2),
|
|
108
|
+
].join("\n\n");
|
|
109
|
+
}
|
|
110
|
+
export async function offerAgentImport(pi, ctx, options) {
|
|
111
|
+
const firstRun = options.firstRun ?? false;
|
|
112
|
+
if (!ctx.hasUI || ctx.mode !== "tui") {
|
|
113
|
+
if (!firstRun)
|
|
114
|
+
ctx.ui.notify("Agent import requires TUI mode. Run /agents import in an interactive session.", "warning");
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
try {
|
|
118
|
+
if (firstRun && importWasOffered(options.agentDir))
|
|
119
|
+
return false;
|
|
120
|
+
const found = discoverImportCandidates({
|
|
121
|
+
cwd: ctx.cwd,
|
|
122
|
+
agentDir: options.agentDir,
|
|
123
|
+
includeProject: ctx.isProjectTrusted(),
|
|
124
|
+
homeDir: options.homeDir,
|
|
125
|
+
extraAgentDirs: options.extraAgentDirs,
|
|
126
|
+
});
|
|
127
|
+
if (found.diagnostics.length)
|
|
128
|
+
ctx.ui.notify(dialogText(found.diagnostics.join("\n")), "warning");
|
|
129
|
+
const remember = () => {
|
|
130
|
+
try {
|
|
131
|
+
markImportOffered(options.agentDir);
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
ctx.ui.notify(dialogText(`Could not save import onboarding state: ${String(error)}`), "warning");
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
if (!found.candidates.length) {
|
|
138
|
+
if (!found.diagnostics.length)
|
|
139
|
+
remember();
|
|
140
|
+
if (!firstRun)
|
|
141
|
+
ctx.ui.notify("No external agent definitions found. Package-provided defaults are not scanned.", "info");
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
if (firstRun &&
|
|
145
|
+
!(await ctx.ui.confirm("Import existing agents?", `Found ${found.candidates.length} external agent definitions. Choose individual agents in a checkbox picker, then the current session model will migrate only your selection. Originals stay unchanged. You can also use /agents import later.`))) {
|
|
146
|
+
remember();
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
const ids = await (options.select ?? selectImportAgents)(ctx, found.candidates.map((candidate) => ({
|
|
150
|
+
id: candidate.id,
|
|
151
|
+
label: `${candidate.name} (${candidate.scope})`,
|
|
152
|
+
detail: `${candidate.path}\n${candidate.description}`,
|
|
153
|
+
})));
|
|
154
|
+
if (!ids?.length) {
|
|
155
|
+
remember();
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
const selected = new Set(ids);
|
|
159
|
+
const candidates = found.candidates.filter((candidate) => selected.has(candidate.id));
|
|
160
|
+
if (!candidates.length)
|
|
161
|
+
return false;
|
|
162
|
+
options.store.reload();
|
|
163
|
+
const prompt = buildImportPrompt({
|
|
164
|
+
candidates,
|
|
165
|
+
agentDir: options.agentDir,
|
|
166
|
+
cwd: ctx.cwd,
|
|
167
|
+
includeProject: ctx.isProjectTrusted(),
|
|
168
|
+
existingTypes: options.store
|
|
169
|
+
.list()
|
|
170
|
+
.map(({ name, source, filePath }) => ({ name, source, filePath })),
|
|
171
|
+
scopedModels: (ctx.scopedModels ?? []).map(({ model }) => `${model.provider}/${model.id}`),
|
|
172
|
+
scopedModelDetails: (ctx.scopedModels ?? []).map(({ model }) => ({
|
|
173
|
+
identity: `${model.provider}/${model.id}`,
|
|
174
|
+
api: model.api,
|
|
175
|
+
virtual: model.api === "pi-virtual",
|
|
176
|
+
})),
|
|
177
|
+
parentModel: ctx.model ? `${ctx.model.provider}/${ctx.model.id}` : undefined,
|
|
178
|
+
parentModelApi: ctx.model?.api,
|
|
179
|
+
});
|
|
180
|
+
pi.sendUserMessage(prompt, { deliverAs: "followUp" });
|
|
181
|
+
ctx.ui.notify(`Asked the current session model to import ${candidates.length} selected agent(s).`, "info");
|
|
182
|
+
return true;
|
|
183
|
+
}
|
|
184
|
+
catch (error) {
|
|
185
|
+
ctx.ui.notify(dialogText(`Agent import failed: ${String(error)}. Retry with /agents import.`), "warning");
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
}
|