@lune_99/pi-subagent 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +57 -0
- package/LICENSE +21 -0
- package/README.md +185 -2
- package/docs/capture.json +30 -0
- package/docs/previews.md +22 -0
- package/docs/reference.md +264 -0
- package/docs/subagent-auto-tiers.png +0 -0
- package/docs/subagent-inspector.png +0 -0
- package/docs/subagent-overview.png +0 -0
- package/package.json +82 -5
- package/skills/create-lune-subagent/SKILL.md +190 -0
- package/src/auto-model.ts +601 -0
- package/src/catalog.ts +111 -0
- package/src/definition-file.ts +147 -0
- package/src/definitions.ts +330 -0
- package/src/dock/dock-alert.ts +78 -0
- package/src/dock/dock-contribution.ts +328 -0
- package/src/extension-tools.ts +46 -0
- package/src/index.ts +194 -0
- package/src/inspector/details.ts +119 -0
- package/src/inspector/inspector.ts +677 -0
- package/src/inspector/layout.ts +119 -0
- package/src/inspector/transcript.ts +735 -0
- package/src/inspector/viewport.ts +62 -0
- package/src/inspector/wrap.ts +136 -0
- package/src/models.ts +69 -0
- package/src/notify/notification-box.ts +408 -0
- package/src/notify/notifier.ts +586 -0
- package/src/paths.ts +34 -0
- package/src/render/lune-theme.ts +68 -0
- package/src/render/text.ts +121 -0
- package/src/run/guide.ts +31 -0
- package/src/run/report-tool.ts +90 -0
- package/src/run/run-file.ts +194 -0
- package/src/run/run-manager.ts +935 -0
- package/src/run/run-session.ts +307 -0
- package/src/run/run.ts +773 -0
- package/src/run/session-start.ts +283 -0
- package/src/run/types.ts +175 -0
- package/src/run/worktree.ts +138 -0
- package/src/schema.ts +9 -0
- package/src/settings/agents-menu.ts +376 -0
- package/src/settings/panel.ts +221 -0
- package/src/settings/schema.ts +75 -0
- package/src/settings/thinking-choices.ts +92 -0
- package/src/tools/format.ts +397 -0
- package/src/tools/subagent-tools.ts +355 -0
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import { defineTool, type Theme, type ToolDefinition } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { stripTerminalSequences, Text } from "@earendil-works/pi-tui";
|
|
3
|
+
import Type, { type TSchema } from "typebox";
|
|
4
|
+
|
|
5
|
+
import type { ToolCatalog } from "../catalog.ts";
|
|
6
|
+
import type { AgentDefinition } from "../definitions.ts";
|
|
7
|
+
import { AUTO_MODEL } from "../models.ts";
|
|
8
|
+
import { REPORT_TOOL_NAME } from "../run/report-tool.ts";
|
|
9
|
+
import type { RunManager, StartParams } from "../run/run-manager.ts";
|
|
10
|
+
import type { SendOutcome } from "../run/run.ts";
|
|
11
|
+
import { ISOLATION_MODES, THINKING_LEVELS } from "../run/types.ts";
|
|
12
|
+
import { stringEnum } from "../schema.ts";
|
|
13
|
+
import { formatRunList, formatRunStatus, formatStarted } from "./format.ts";
|
|
14
|
+
|
|
15
|
+
export const SUBAGENT_TOOL_NAMES = [
|
|
16
|
+
"subagent_start",
|
|
17
|
+
"subagent_send",
|
|
18
|
+
"subagent_status",
|
|
19
|
+
"subagent_stop",
|
|
20
|
+
] as const;
|
|
21
|
+
|
|
22
|
+
/** A catalog description keeps only the first sentence of each tool's own description. */
|
|
23
|
+
const CATALOG_DESCRIPTION_CHARS = 100;
|
|
24
|
+
|
|
25
|
+
const TASK_DESCRIPTION =
|
|
26
|
+
"The run's first message; it does not inherit your conversation. Give the goal, relevant " +
|
|
27
|
+
"context and paths, expected output, acceptance checks and action limits. For edits, assign " +
|
|
28
|
+
"the files or scope it owns.";
|
|
29
|
+
|
|
30
|
+
export interface SubagentToolsOptions {
|
|
31
|
+
manager: RunManager;
|
|
32
|
+
definitions: readonly AgentDefinition[];
|
|
33
|
+
/** `provider/id` references the `model` parameter allows. */
|
|
34
|
+
models: readonly string[];
|
|
35
|
+
/** Auto mode only: the schema has no `model`, and inline runs use `model: auto`. */
|
|
36
|
+
autoOnly: boolean;
|
|
37
|
+
catalog: ToolCatalog;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The main agent's four tools. Only `subagent_start` depends on the session, through its schema
|
|
42
|
+
* and the inline settings it names; it is rebuilt only when the allowed models or Auto mode only
|
|
43
|
+
* change.
|
|
44
|
+
*/
|
|
45
|
+
export function createSubagentTools(options: SubagentToolsOptions): ToolDefinition[] {
|
|
46
|
+
const { manager } = options;
|
|
47
|
+
|
|
48
|
+
const start = defineTool({
|
|
49
|
+
name: "subagent_start",
|
|
50
|
+
label: "Subagent Start",
|
|
51
|
+
description:
|
|
52
|
+
"Start a separate Pi session for a bounded task in the background. Delegate when " +
|
|
53
|
+
"independent execution, isolated context or a fresh verification pass improves speed " +
|
|
54
|
+
"or quality. Handle quick tasks and tightly coupled steps directly. Use agent to " +
|
|
55
|
+
"run a listed role with its own settings; give only task and title with it. Omit " +
|
|
56
|
+
`agent for an inline run that sets ${inlineSettings(options.autoOnly)} in the call` +
|
|
57
|
+
(options.autoOnly
|
|
58
|
+
? "; the user's auto settings choose its model and thinking. "
|
|
59
|
+
: ". ") +
|
|
60
|
+
'Use isolation "shared" to work in your current directory, including uncommitted ' +
|
|
61
|
+
'changes; use "worktree" for a separate Git checkout at HEAD, without uncommitted or ' +
|
|
62
|
+
"untracked files. Tool selection and worktrees are not a security sandbox. Call once " +
|
|
63
|
+
"per run; independent starts can run in parallel. The call returns the run ID " +
|
|
64
|
+
"immediately; each turn's report arrives as a message.",
|
|
65
|
+
promptSnippet: "Delegate bounded work to a background session and receive its report.",
|
|
66
|
+
promptGuidelines: [
|
|
67
|
+
"Give a run a brief title so the user can identify it in the /agents inspector and Dock.",
|
|
68
|
+
"Keep your work distinct from delegated work unless overlap is intentional verification. Coordinate shared-file edits so writers do not overlap.",
|
|
69
|
+
"Reports arrive when turns end; do not poll subagent_status. Continue other useful work, or end your turn if none remains.",
|
|
70
|
+
"When a run reports needs_input, answer with subagent_send if you can. Ask the user only when only the user can answer.",
|
|
71
|
+
"Pass a run's ID, never its name or title, to subagent_send, subagent_status and subagent_stop.",
|
|
72
|
+
"Validate findings and changes you rely on. Worktree changes stay there until explicitly reviewed and integrated; nothing merges automatically.",
|
|
73
|
+
],
|
|
74
|
+
parameters: Type.Object(startProperties(options), { additionalProperties: false }),
|
|
75
|
+
|
|
76
|
+
async execute(_toolCallId, params, signal, onUpdate, ctx) {
|
|
77
|
+
// The schema is built at runtime, so its static type is loose; Pi has validated the
|
|
78
|
+
// arguments against it.
|
|
79
|
+
const input = params as unknown as StartParams;
|
|
80
|
+
const run = await manager.start(input, ctx, {
|
|
81
|
+
signal,
|
|
82
|
+
onProgress: (text) =>
|
|
83
|
+
onUpdate?.({ content: [{ type: "text", text }], details: undefined }),
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
content: [{ type: "text", text: formatStarted(run) }],
|
|
88
|
+
details: undefined,
|
|
89
|
+
};
|
|
90
|
+
},
|
|
91
|
+
|
|
92
|
+
renderCall(args, theme) {
|
|
93
|
+
const input = args as Partial<StartParams>;
|
|
94
|
+
|
|
95
|
+
return callLine(theme, "subagent_start", [
|
|
96
|
+
input.agent ?? "inline",
|
|
97
|
+
preview(input.task),
|
|
98
|
+
]);
|
|
99
|
+
},
|
|
100
|
+
|
|
101
|
+
renderResult: renderText,
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
const send = defineTool({
|
|
105
|
+
name: "subagent_send",
|
|
106
|
+
label: "Subagent Send",
|
|
107
|
+
description:
|
|
108
|
+
"Send a message to a subagent run of the current branch. A running run receives it as " +
|
|
109
|
+
"a steer message in its current turn. An idle, failed or interrupted run starts a new " +
|
|
110
|
+
"turn with it, in the same session and working directory. Use it to answer a " +
|
|
111
|
+
"needs_input question, to correct a run, or to continue its work. Returns once the run " +
|
|
112
|
+
"accepted the message; a message arrives when the turn ends.",
|
|
113
|
+
promptSnippet: "Answer, steer or continue a subagent run.",
|
|
114
|
+
parameters: Type.Object({
|
|
115
|
+
runId: Type.String({
|
|
116
|
+
description:
|
|
117
|
+
"The run ID that subagent_start returned, such as 8416992c. Pass the ID, not " +
|
|
118
|
+
"the run's name or title.",
|
|
119
|
+
}),
|
|
120
|
+
message: Type.String({ description: "The message for the run." }),
|
|
121
|
+
}),
|
|
122
|
+
|
|
123
|
+
async execute(_toolCallId, params, signal, onUpdate) {
|
|
124
|
+
const run = manager.branchRun(params.runId);
|
|
125
|
+
const outcome = await manager.send(run, params.message, "main-agent", {
|
|
126
|
+
signal,
|
|
127
|
+
onProgress: (text) =>
|
|
128
|
+
onUpdate?.({ content: [{ type: "text", text }], details: undefined }),
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
return {
|
|
132
|
+
content: [{ type: "text", text: sendText(run.id, outcome) }],
|
|
133
|
+
details: undefined,
|
|
134
|
+
};
|
|
135
|
+
},
|
|
136
|
+
|
|
137
|
+
renderCall(args, theme) {
|
|
138
|
+
return callLine(theme, "subagent_send", [args.runId, preview(args.message)]);
|
|
139
|
+
},
|
|
140
|
+
|
|
141
|
+
renderResult: renderText,
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
const status = defineTool({
|
|
145
|
+
name: "subagent_status",
|
|
146
|
+
label: "Subagent Status",
|
|
147
|
+
description:
|
|
148
|
+
"Read subagent runs of the current branch without changing them: state, start values, " +
|
|
149
|
+
"workspace, token usage and the last turn's report. Without runId it lists every run " +
|
|
150
|
+
"with a preview of its report; with runId it returns that run's whole report.",
|
|
151
|
+
promptSnippet: "Read the state and last report of subagent runs.",
|
|
152
|
+
parameters: Type.Object({
|
|
153
|
+
runId: Type.Optional(
|
|
154
|
+
Type.String({
|
|
155
|
+
description:
|
|
156
|
+
"One run ID, such as 8416992c. Default: every run. Pass the ID, not the " +
|
|
157
|
+
"run's name or title.",
|
|
158
|
+
}),
|
|
159
|
+
),
|
|
160
|
+
}),
|
|
161
|
+
|
|
162
|
+
async execute(_toolCallId, params) {
|
|
163
|
+
const text =
|
|
164
|
+
params.runId === undefined
|
|
165
|
+
? formatRunList(manager.branchRuns(), manager.otherBranchRuns().length)
|
|
166
|
+
: formatRunStatus(manager.branchRun(params.runId));
|
|
167
|
+
|
|
168
|
+
return { content: [{ type: "text", text }], details: undefined };
|
|
169
|
+
},
|
|
170
|
+
|
|
171
|
+
renderCall(args, theme) {
|
|
172
|
+
return callLine(theme, "subagent_status", [args.runId ?? "all runs"]);
|
|
173
|
+
},
|
|
174
|
+
|
|
175
|
+
renderResult: renderText,
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
const stop = defineTool({
|
|
179
|
+
name: "subagent_stop",
|
|
180
|
+
label: "Subagent Stop",
|
|
181
|
+
description:
|
|
182
|
+
"Stop the current turn of a subagent run of the current branch. The run becomes " +
|
|
183
|
+
"interrupted; its session and worktree stay, and subagent_send continues it. You get " +
|
|
184
|
+
"no turn-end message for a run you stop; this result holds its state.",
|
|
185
|
+
promptSnippet: "Stop a running subagent run.",
|
|
186
|
+
parameters: Type.Object({
|
|
187
|
+
runId: Type.String({
|
|
188
|
+
description:
|
|
189
|
+
"The run ID that subagent_start returned, such as 8416992c. Pass the ID, not " +
|
|
190
|
+
"the run's name or title.",
|
|
191
|
+
}),
|
|
192
|
+
}),
|
|
193
|
+
|
|
194
|
+
async execute(_toolCallId, params) {
|
|
195
|
+
const run = manager.branchRun(params.runId);
|
|
196
|
+
const stopped = await manager.stop(run, "main-agent");
|
|
197
|
+
const text = stopped
|
|
198
|
+
? `Stopped run ${run.id}.\n\n${formatRunStatus(run)}`
|
|
199
|
+
: `Run ${run.id} is not running; its state is ${run.state}.`;
|
|
200
|
+
|
|
201
|
+
return { content: [{ type: "text", text }], details: undefined };
|
|
202
|
+
},
|
|
203
|
+
|
|
204
|
+
renderCall(args, theme) {
|
|
205
|
+
return callLine(theme, "subagent_stop", [args.runId]);
|
|
206
|
+
},
|
|
207
|
+
|
|
208
|
+
renderResult: renderText,
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
return [start, send, status, stop];
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** The agent comes first, so a call names its role before writing the task and inline settings. */
|
|
215
|
+
function startProperties(options: SubagentToolsOptions): Record<string, TSchema> {
|
|
216
|
+
const properties: Record<string, TSchema> = {};
|
|
217
|
+
const agents = options.definitions.map((definition) => definition.name);
|
|
218
|
+
|
|
219
|
+
if (agents.length > 0) {
|
|
220
|
+
properties.agent = Type.Optional(
|
|
221
|
+
stringEnum(agents, {
|
|
222
|
+
description:
|
|
223
|
+
"An agent from the subagents list. It runs with its definition's model, " +
|
|
224
|
+
"thinking, tools and isolation, so give only task and title with it. Leave " +
|
|
225
|
+
`it out to set ${options.autoOnly ? inlineSettings(true) : "these"} in this call.`,
|
|
226
|
+
}),
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
properties.title = Type.Optional(
|
|
231
|
+
Type.String({
|
|
232
|
+
description:
|
|
233
|
+
"A brief one-line title for this run, shown in the /agents inspector, Dock and " +
|
|
234
|
+
"run notifications. The subagent tools take a run's ID, never its title.",
|
|
235
|
+
}),
|
|
236
|
+
);
|
|
237
|
+
properties.task = Type.String({ description: TASK_DESCRIPTION });
|
|
238
|
+
|
|
239
|
+
const inlineOnly = agents.length > 0 ? "Only without agent. " : "";
|
|
240
|
+
|
|
241
|
+
if (!options.autoOnly && options.models.length > 0) {
|
|
242
|
+
properties.model = Type.Optional(
|
|
243
|
+
stringEnum([AUTO_MODEL, ...options.models], {
|
|
244
|
+
description:
|
|
245
|
+
`${inlineOnly}provider/id, or "${AUTO_MODEL}" to route both the model and ` +
|
|
246
|
+
"thinking for each task. Omit thinking with auto. Default: your current model.",
|
|
247
|
+
}),
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (!options.autoOnly) {
|
|
252
|
+
properties.thinking = Type.Optional(
|
|
253
|
+
stringEnum(THINKING_LEVELS, {
|
|
254
|
+
description:
|
|
255
|
+
`${inlineOnly}Only for a fixed or inherited model; omit with model auto, ` +
|
|
256
|
+
"which chooses both model and thinking. Default: your current level.",
|
|
257
|
+
}),
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
properties.tools = Type.Optional(
|
|
261
|
+
Type.Array(Type.String(), {
|
|
262
|
+
description: `${inlineOnly}${toolsDescription(options.catalog)}`,
|
|
263
|
+
}),
|
|
264
|
+
);
|
|
265
|
+
properties.isolation = Type.Optional(
|
|
266
|
+
stringEnum(ISOLATION_MODES, { description: `${inlineOnly}Default: "shared".` }),
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
return properties;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** The settings an inline run takes from the call; under Auto mode only, the model is auto. */
|
|
273
|
+
function inlineSettings(autoOnly: boolean): string {
|
|
274
|
+
return autoOnly ? "tools and isolation" : "model, thinking, tools and isolation";
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function toolsDescription(catalog: ToolCatalog): string {
|
|
278
|
+
const tools = catalog.tools
|
|
279
|
+
.filter((tool) => tool.name !== REPORT_TOOL_NAME)
|
|
280
|
+
.map((tool) => `${tool.name}: ${firstSentence(tool.description)}`);
|
|
281
|
+
|
|
282
|
+
return [
|
|
283
|
+
"Exact names of the task tools the run gets. Choose only tools needed for this task; " +
|
|
284
|
+
"prefer read-only tools for investigation. Default: the defaultTools setting. The " +
|
|
285
|
+
"run always gets report as well.",
|
|
286
|
+
"Available:",
|
|
287
|
+
...tools.map((line) => `- ${line}`),
|
|
288
|
+
"- mcp__<server>__<tool>: a tool of an MCP server configured for the run's working directory.",
|
|
289
|
+
].join("\n");
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function firstSentence(text: string): string {
|
|
293
|
+
const line = text.replace(/\s+/g, " ").trim();
|
|
294
|
+
const end = line.search(/[.!?](\s|$)/);
|
|
295
|
+
const sentence = end === -1 ? line : line.slice(0, end + 1);
|
|
296
|
+
|
|
297
|
+
return sentence.length > CATALOG_DESCRIPTION_CHARS
|
|
298
|
+
? `${sentence.slice(0, CATALOG_DESCRIPTION_CHARS - 1)}…`
|
|
299
|
+
: sentence;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function sendText(runId: string, outcome: SendOutcome): string {
|
|
303
|
+
switch (outcome) {
|
|
304
|
+
case "started":
|
|
305
|
+
return `Run ${runId} started a new turn with your message. A message arrives when the turn ends.`;
|
|
306
|
+
case "steered":
|
|
307
|
+
return `Run ${runId} is running; your message steers its current turn.`;
|
|
308
|
+
case "deferred":
|
|
309
|
+
return `Run ${runId} is ending its turn; your message starts the next turn after it.`;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function callLine(theme: Theme, tool: string, parts: readonly (string | undefined)[]): Text {
|
|
314
|
+
return new Text(callText(theme, tool, parts), 0, 0);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function callText(theme: Theme, tool: string, parts: readonly (string | undefined)[]): string {
|
|
318
|
+
const shown = shownParts(parts);
|
|
319
|
+
|
|
320
|
+
return (
|
|
321
|
+
theme.fg("toolTitle", theme.bold(tool)) +
|
|
322
|
+
(shown.length > 0 ? ` ${theme.fg("muted", shown.join(" · "))}` : "")
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function shownParts(parts: readonly (string | undefined)[]): string[] {
|
|
327
|
+
return parts.filter((part): part is string => part !== undefined && part !== "");
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** The call is drawn while the model still streams its arguments, so a value can be missing. */
|
|
331
|
+
function preview(text: unknown): string | undefined {
|
|
332
|
+
if (typeof text !== "string") {
|
|
333
|
+
return undefined;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
const line = stripTerminalSequences(text).replace(/\s+/g, " ").trim();
|
|
337
|
+
|
|
338
|
+
return line.length > 60 ? `${line.slice(0, 59)}…` : line;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** The result text: its first line, or all of it when expanded. */
|
|
342
|
+
function renderText(
|
|
343
|
+
result: { content: readonly { type: string; text?: string }[] },
|
|
344
|
+
options: { expanded: boolean },
|
|
345
|
+
theme: Theme,
|
|
346
|
+
context: { isError: boolean },
|
|
347
|
+
): Text {
|
|
348
|
+
const text = result.content
|
|
349
|
+
.map((part) => (part.type === "text" ? (part.text ?? "") : ""))
|
|
350
|
+
.join("\n")
|
|
351
|
+
.trim();
|
|
352
|
+
const shown = options.expanded ? text : (text.split("\n")[0] ?? "");
|
|
353
|
+
|
|
354
|
+
return new Text(theme.fg(context.isError ? "error" : "toolOutput", shown), 0, 0);
|
|
355
|
+
}
|