@lune_99/pi-subagent 0.0.0-stage → 0.1.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 +24 -0
- package/LICENSE +21 -0
- package/README.md +169 -2
- package/docs/capture.json +30 -0
- package/docs/previews.md +22 -0
- package/docs/reference.md +240 -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 +186 -0
- package/src/auto-model.ts +592 -0
- package/src/catalog.ts +111 -0
- package/src/definition-file.ts +112 -0
- package/src/definitions.ts +324 -0
- package/src/dock/dock-alert.ts +78 -0
- package/src/dock/dock-contribution.ts +318 -0
- package/src/extension-tools.ts +46 -0
- package/src/index.ts +168 -0
- package/src/inspector/details.ts +113 -0
- package/src/inspector/inspector.ts +653 -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 +54 -0
- package/src/notify/notification-box.ts +381 -0
- package/src/notify/notifier.ts +569 -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 +26 -0
- package/src/run/report-tool.ts +90 -0
- package/src/run/run-file.ts +193 -0
- package/src/run/run-manager.ts +838 -0
- package/src/run/run-session.ts +307 -0
- package/src/run/run.ts +681 -0
- package/src/run/session-start.ts +283 -0
- package/src/run/types.ts +167 -0
- package/src/run/worktree.ts +138 -0
- package/src/schema.ts +9 -0
- package/src/settings/agents-menu.ts +363 -0
- package/src/settings/panel.ts +179 -0
- package/src/settings/schema.ts +71 -0
- package/src/tools/format.ts +375 -0
- package/src/tools/subagent-tools.ts +467 -0
|
@@ -0,0 +1,467 @@
|
|
|
1
|
+
import type { ThinkingLevel } from "@earendil-works/pi-agent-core";
|
|
2
|
+
import { defineTool, type Theme, type ToolDefinition } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { Text } from "@earendil-works/pi-tui";
|
|
4
|
+
import Type, { type TSchema } from "typebox";
|
|
5
|
+
|
|
6
|
+
import type { ToolCatalog } from "../catalog.ts";
|
|
7
|
+
import type { AgentDefinition } from "../definitions.ts";
|
|
8
|
+
import { AUTO_MODEL } from "../models.ts";
|
|
9
|
+
import { REPORT_TOOL_NAME } from "../run/report-tool.ts";
|
|
10
|
+
import type { RunManager, StartParams } from "../run/run-manager.ts";
|
|
11
|
+
import type { SendOutcome } from "../run/run.ts";
|
|
12
|
+
import { ISOLATION_MODES, THINKING_LEVELS, type Isolation } from "../run/types.ts";
|
|
13
|
+
import { stringEnum } from "../schema.ts";
|
|
14
|
+
import {
|
|
15
|
+
formatRunList,
|
|
16
|
+
formatRunStatus,
|
|
17
|
+
formatStarted,
|
|
18
|
+
formatStartedBatch,
|
|
19
|
+
type StartOutcome,
|
|
20
|
+
} from "./format.ts";
|
|
21
|
+
|
|
22
|
+
export const SUBAGENT_TOOL_NAMES = [
|
|
23
|
+
"subagent_start",
|
|
24
|
+
"subagent_send",
|
|
25
|
+
"subagent_status",
|
|
26
|
+
"subagent_stop",
|
|
27
|
+
] as const;
|
|
28
|
+
|
|
29
|
+
/** A catalog description keeps only the first sentence of each tool's own description. */
|
|
30
|
+
const CATALOG_DESCRIPTION_CHARS = 100;
|
|
31
|
+
|
|
32
|
+
/** One run's start values: the top level of `subagent_start`, or one entry of its `tasks`. */
|
|
33
|
+
interface RunInput {
|
|
34
|
+
task: string;
|
|
35
|
+
agent?: string;
|
|
36
|
+
model?: string;
|
|
37
|
+
thinking?: ThinkingLevel;
|
|
38
|
+
tools?: string[];
|
|
39
|
+
isolation?: Isolation;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface StartInput extends Partial<RunInput> {
|
|
43
|
+
tasks?: RunInput[];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The start values that a `tasks` call takes in each entry, not at the top level. */
|
|
47
|
+
const RUN_FIELDS = ["agent", "model", "thinking", "tools", "isolation"] as const;
|
|
48
|
+
|
|
49
|
+
const TASK_DESCRIPTION =
|
|
50
|
+
"The run's first message; it does not inherit your conversation. Give the goal, relevant " +
|
|
51
|
+
"context and paths, expected output, acceptance checks and action limits. For edits, assign " +
|
|
52
|
+
"the files or scope it owns.";
|
|
53
|
+
|
|
54
|
+
export interface SubagentToolsOptions {
|
|
55
|
+
manager: RunManager;
|
|
56
|
+
definitions: readonly AgentDefinition[];
|
|
57
|
+
/** `provider/id` references the `model` parameter allows. */
|
|
58
|
+
models: readonly string[];
|
|
59
|
+
catalog: ToolCatalog;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The main agent's four tools. Their descriptions are fixed; only the `subagent_start` schema
|
|
64
|
+
* depends on the session, and it is built once at `session_start` and kept for the session.
|
|
65
|
+
*/
|
|
66
|
+
export function createSubagentTools(options: SubagentToolsOptions): ToolDefinition[] {
|
|
67
|
+
const { manager } = options;
|
|
68
|
+
|
|
69
|
+
const start = defineTool({
|
|
70
|
+
name: "subagent_start",
|
|
71
|
+
label: "Subagent Start",
|
|
72
|
+
description:
|
|
73
|
+
"Start a separate Pi session for a bounded task in the background. Delegate when " +
|
|
74
|
+
"independent execution, isolated context or a fresh verification pass improves speed " +
|
|
75
|
+
"or quality. Handle quick tasks and tightly coupled steps directly. Use agent to " +
|
|
76
|
+
'reuse a listed role, or omit it for an inline task. Use isolation "shared" to work ' +
|
|
77
|
+
'in your current directory, including uncommitted changes; use "worktree" for a ' +
|
|
78
|
+
"separate Git checkout at HEAD, without uncommitted or untracked files. Tool selection " +
|
|
79
|
+
"and worktrees are not a security sandbox. Give task for one run or tasks for " +
|
|
80
|
+
"independent runs started together. The call returns run IDs immediately; each turn's " +
|
|
81
|
+
"report arrives as a message.",
|
|
82
|
+
promptSnippet: "Delegate bounded work to a background session and receive its report.",
|
|
83
|
+
promptGuidelines: [
|
|
84
|
+
"Keep your work distinct from delegated work unless overlap is intentional verification. Coordinate shared-file edits so writers do not overlap.",
|
|
85
|
+
"Reports arrive when turns end; do not poll subagent_status. Continue other useful work, or end your turn if none remains.",
|
|
86
|
+
"When a run reports needs_input, answer with subagent_send if you can. Ask the user only when only the user can answer.",
|
|
87
|
+
"Validate findings and changes you rely on. Worktree changes stay there until explicitly reviewed and integrated; nothing merges automatically.",
|
|
88
|
+
],
|
|
89
|
+
parameters: Type.Object(startProperties(options)),
|
|
90
|
+
|
|
91
|
+
async execute(_toolCallId, params, signal, onUpdate, ctx) {
|
|
92
|
+
// The schema is built at runtime, so its static type is loose; Pi has validated the
|
|
93
|
+
// arguments against it.
|
|
94
|
+
const input = params as unknown as StartInput;
|
|
95
|
+
const progress = (text: string): void =>
|
|
96
|
+
onUpdate?.({ content: [{ type: "text", text }], details: undefined });
|
|
97
|
+
|
|
98
|
+
if (input.tasks === undefined) {
|
|
99
|
+
if (input.task === undefined) {
|
|
100
|
+
throw new Error("Give task for one run, or tasks for several runs.");
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const run = await manager.start(startParams(input.task, input), ctx, {
|
|
104
|
+
signal,
|
|
105
|
+
onProgress: progress,
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
return {
|
|
109
|
+
content: [{ type: "text", text: formatStarted(run) }],
|
|
110
|
+
details: undefined,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const entries = input.tasks;
|
|
115
|
+
|
|
116
|
+
checkBatch(input, entries);
|
|
117
|
+
|
|
118
|
+
// Each entry starts on its own, as parallel calls would: a failed one undoes only its
|
|
119
|
+
// own work, because a run that started cannot be taken back.
|
|
120
|
+
const steps = entries.map(() => "Starting");
|
|
121
|
+
const outcomes = await Promise.all(
|
|
122
|
+
entries.map(async (entry, index): Promise<StartOutcome> => {
|
|
123
|
+
try {
|
|
124
|
+
const run = await manager.start(startParams(entry.task, entry), ctx, {
|
|
125
|
+
signal,
|
|
126
|
+
onProgress: (text) => {
|
|
127
|
+
steps[index] = text;
|
|
128
|
+
progress(batchProgress(steps));
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
steps[index] = `Started run ${run.name} (${run.id})`;
|
|
133
|
+
|
|
134
|
+
return { run };
|
|
135
|
+
} catch (error) {
|
|
136
|
+
steps[index] = "Failed";
|
|
137
|
+
|
|
138
|
+
return { error: error instanceof Error ? error.message : String(error) };
|
|
139
|
+
} finally {
|
|
140
|
+
progress(batchProgress(steps));
|
|
141
|
+
}
|
|
142
|
+
}),
|
|
143
|
+
);
|
|
144
|
+
const text = formatStartedBatch(outcomes);
|
|
145
|
+
|
|
146
|
+
if (outcomes.every((outcome) => "error" in outcome)) {
|
|
147
|
+
throw new Error(text);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return { content: [{ type: "text", text }], details: undefined };
|
|
151
|
+
},
|
|
152
|
+
|
|
153
|
+
renderCall(args, theme) {
|
|
154
|
+
const input = args as Partial<StartInput>;
|
|
155
|
+
|
|
156
|
+
if (Array.isArray(input.tasks)) {
|
|
157
|
+
return batchCall(theme, input.tasks);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return callLine(theme, "subagent_start", [
|
|
161
|
+
input.agent ?? "inline",
|
|
162
|
+
preview(input.task),
|
|
163
|
+
]);
|
|
164
|
+
},
|
|
165
|
+
|
|
166
|
+
renderResult: renderText,
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
const send = defineTool({
|
|
170
|
+
name: "subagent_send",
|
|
171
|
+
label: "Subagent Send",
|
|
172
|
+
description:
|
|
173
|
+
"Send a message to a subagent run of the current branch. A running run receives it as " +
|
|
174
|
+
"a steer message in its current turn. An idle, failed or interrupted run starts a new " +
|
|
175
|
+
"turn with it, in the same session and working directory. Use it to answer a " +
|
|
176
|
+
"needs_input question, to correct a run, or to continue its work. Returns once the run " +
|
|
177
|
+
"accepted the message; a message arrives when the turn ends.",
|
|
178
|
+
promptSnippet: "Answer, steer or continue a subagent run.",
|
|
179
|
+
parameters: Type.Object({
|
|
180
|
+
runId: Type.String({ description: "The run ID that subagent_start returned." }),
|
|
181
|
+
message: Type.String({ description: "The message for the run." }),
|
|
182
|
+
}),
|
|
183
|
+
|
|
184
|
+
async execute(_toolCallId, params, signal, onUpdate) {
|
|
185
|
+
const run = manager.branchRun(params.runId);
|
|
186
|
+
const outcome = await manager.send(run, params.message, "main-agent", {
|
|
187
|
+
signal,
|
|
188
|
+
onProgress: (text) =>
|
|
189
|
+
onUpdate?.({ content: [{ type: "text", text }], details: undefined }),
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
return {
|
|
193
|
+
content: [{ type: "text", text: sendText(run.name, run.id, outcome) }],
|
|
194
|
+
details: undefined,
|
|
195
|
+
};
|
|
196
|
+
},
|
|
197
|
+
|
|
198
|
+
renderCall(args, theme) {
|
|
199
|
+
return callLine(theme, "subagent_send", [args.runId, preview(args.message)]);
|
|
200
|
+
},
|
|
201
|
+
|
|
202
|
+
renderResult: renderText,
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
const status = defineTool({
|
|
206
|
+
name: "subagent_status",
|
|
207
|
+
label: "Subagent Status",
|
|
208
|
+
description:
|
|
209
|
+
"Read subagent runs of the current branch without changing them: state, start values, " +
|
|
210
|
+
"workspace, token usage and the last turn's report. Without runId it lists every run " +
|
|
211
|
+
"with a preview of its report; with runId it returns that run's whole report.",
|
|
212
|
+
promptSnippet: "Read the state and last report of subagent runs.",
|
|
213
|
+
parameters: Type.Object({
|
|
214
|
+
runId: Type.Optional(Type.String({ description: "One run ID. Default: every run." })),
|
|
215
|
+
}),
|
|
216
|
+
|
|
217
|
+
async execute(_toolCallId, params) {
|
|
218
|
+
const text =
|
|
219
|
+
params.runId === undefined
|
|
220
|
+
? formatRunList(manager.branchRuns(), manager.otherBranchRuns().length)
|
|
221
|
+
: formatRunStatus(manager.branchRun(params.runId));
|
|
222
|
+
|
|
223
|
+
return { content: [{ type: "text", text }], details: undefined };
|
|
224
|
+
},
|
|
225
|
+
|
|
226
|
+
renderCall(args, theme) {
|
|
227
|
+
return callLine(theme, "subagent_status", [args.runId ?? "all runs"]);
|
|
228
|
+
},
|
|
229
|
+
|
|
230
|
+
renderResult: renderText,
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
const stop = defineTool({
|
|
234
|
+
name: "subagent_stop",
|
|
235
|
+
label: "Subagent Stop",
|
|
236
|
+
description:
|
|
237
|
+
"Stop the current turn of a subagent run of the current branch. The run becomes " +
|
|
238
|
+
"interrupted; its session and worktree stay, and subagent_send continues it. You get " +
|
|
239
|
+
"no turn-end message for a run you stop; this result holds its state.",
|
|
240
|
+
promptSnippet: "Stop a running subagent run.",
|
|
241
|
+
parameters: Type.Object({
|
|
242
|
+
runId: Type.String({ description: "The run ID that subagent_start returned." }),
|
|
243
|
+
}),
|
|
244
|
+
|
|
245
|
+
async execute(_toolCallId, params) {
|
|
246
|
+
const run = manager.branchRun(params.runId);
|
|
247
|
+
const stopped = await manager.stop(run, "main-agent");
|
|
248
|
+
const text = stopped
|
|
249
|
+
? `Stopped run ${run.name} (${run.id}).\n\n${formatRunStatus(run)}`
|
|
250
|
+
: `Run ${run.name} (${run.id}) is not running; its state is ${run.state}.`;
|
|
251
|
+
|
|
252
|
+
return { content: [{ type: "text", text }], details: undefined };
|
|
253
|
+
},
|
|
254
|
+
|
|
255
|
+
renderCall(args, theme) {
|
|
256
|
+
return callLine(theme, "subagent_stop", [args.runId]);
|
|
257
|
+
},
|
|
258
|
+
|
|
259
|
+
renderResult: renderText,
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
return [start, send, status, stop];
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function startProperties(options: SubagentToolsOptions): Record<string, TSchema> {
|
|
266
|
+
return {
|
|
267
|
+
task: Type.Optional(
|
|
268
|
+
Type.String({ description: `${TASK_DESCRIPTION} Leave it out when you give tasks.` }),
|
|
269
|
+
),
|
|
270
|
+
...runOptions(options, toolsDescription(options.catalog)),
|
|
271
|
+
tasks: Type.Optional(
|
|
272
|
+
Type.Array(
|
|
273
|
+
Type.Object({
|
|
274
|
+
task: Type.String({ description: TASK_DESCRIPTION }),
|
|
275
|
+
// The catalog is long, so an entry points to it instead of repeating it.
|
|
276
|
+
...runOptions(
|
|
277
|
+
options,
|
|
278
|
+
"Exact names of the task tools the run gets, from the list of the " +
|
|
279
|
+
"top-level tools parameter. Default: the agent's tools, then the " +
|
|
280
|
+
"defaultTools setting.",
|
|
281
|
+
),
|
|
282
|
+
}),
|
|
283
|
+
{
|
|
284
|
+
description:
|
|
285
|
+
"Independent runs started together instead of task. Set agent, model, " +
|
|
286
|
+
"thinking, tools and isolation per entry, not at the top level. A failed " +
|
|
287
|
+
"start does not stop the other runs.",
|
|
288
|
+
},
|
|
289
|
+
),
|
|
290
|
+
),
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/** The optional start values of one run, at the top level and in each `tasks` entry. */
|
|
295
|
+
function runOptions(options: SubagentToolsOptions, toolsText: string): Record<string, TSchema> {
|
|
296
|
+
const properties: Record<string, TSchema> = {};
|
|
297
|
+
const agents = options.definitions.map((definition) => definition.name);
|
|
298
|
+
|
|
299
|
+
if (agents.length > 0) {
|
|
300
|
+
properties.agent = Type.Optional(
|
|
301
|
+
stringEnum(agents, {
|
|
302
|
+
description:
|
|
303
|
+
"An agent from the subagents list. Leave it out to set up the run in this call.",
|
|
304
|
+
}),
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
if (options.models.length > 0) {
|
|
309
|
+
properties.model = Type.Optional(
|
|
310
|
+
stringEnum([AUTO_MODEL, ...options.models], {
|
|
311
|
+
description:
|
|
312
|
+
`provider/id, or "${AUTO_MODEL}" to let a classifier rate the task and use ` +
|
|
313
|
+
"the model set for its tier; it also sets thinking when the call and the " +
|
|
314
|
+
"agent do not. Default: the agent's model, then your current model.",
|
|
315
|
+
}),
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
properties.thinking = Type.Optional(
|
|
320
|
+
stringEnum(THINKING_LEVELS, {
|
|
321
|
+
description:
|
|
322
|
+
"Default: the agent's thinking level, then the classifier's level for model " +
|
|
323
|
+
"auto, then your current level.",
|
|
324
|
+
}),
|
|
325
|
+
);
|
|
326
|
+
properties.tools = Type.Optional(Type.Array(Type.String(), { description: toolsText }));
|
|
327
|
+
properties.isolation = Type.Optional(
|
|
328
|
+
stringEnum(ISOLATION_MODES, {
|
|
329
|
+
description: 'Default: the agent\'s isolation, then "shared".',
|
|
330
|
+
}),
|
|
331
|
+
);
|
|
332
|
+
|
|
333
|
+
return properties;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
function toolsDescription(catalog: ToolCatalog): string {
|
|
337
|
+
const tools = catalog.tools
|
|
338
|
+
.filter((tool) => tool.name !== REPORT_TOOL_NAME)
|
|
339
|
+
.map((tool) => `${tool.name}: ${firstSentence(tool.description)}`);
|
|
340
|
+
|
|
341
|
+
return [
|
|
342
|
+
"Exact names of the task tools the run gets. Choose only tools needed for this task; " +
|
|
343
|
+
"prefer read-only tools for investigation. Default: the agent's tools, then the " +
|
|
344
|
+
"defaultTools setting. The run always gets report as well.",
|
|
345
|
+
"Available:",
|
|
346
|
+
...tools.map((line) => `- ${line}`),
|
|
347
|
+
"- mcp__<server>__<tool>: a tool of an MCP server configured for the run's working directory.",
|
|
348
|
+
].join("\n");
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function firstSentence(text: string): string {
|
|
352
|
+
const line = text.replace(/\s+/g, " ").trim();
|
|
353
|
+
const end = line.search(/[.!?](\s|$)/);
|
|
354
|
+
const sentence = end === -1 ? line : line.slice(0, end + 1);
|
|
355
|
+
|
|
356
|
+
return sentence.length > CATALOG_DESCRIPTION_CHARS
|
|
357
|
+
? `${sentence.slice(0, CATALOG_DESCRIPTION_CHARS - 1)}…`
|
|
358
|
+
: sentence;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
function startParams(task: string, values: Omit<RunInput, "task">): StartParams {
|
|
362
|
+
return {
|
|
363
|
+
task,
|
|
364
|
+
agent: values.agent,
|
|
365
|
+
model: values.model,
|
|
366
|
+
thinking: values.thinking,
|
|
367
|
+
tools: values.tools,
|
|
368
|
+
isolation: values.isolation,
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/** A `tasks` call takes start values only in its entries, so a mixed call starts nothing. */
|
|
373
|
+
function checkBatch(input: StartInput, entries: readonly RunInput[]): void {
|
|
374
|
+
if (input.task !== undefined) {
|
|
375
|
+
throw new Error("Give task for one run or tasks for several runs, not both.");
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
const topLevel = RUN_FIELDS.filter((field) => input[field] !== undefined);
|
|
379
|
+
|
|
380
|
+
if (topLevel.length > 0) {
|
|
381
|
+
throw new Error(
|
|
382
|
+
`With tasks, set ${topLevel.join(", ")} in each entry instead of at the top level.`,
|
|
383
|
+
);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
if (entries.length === 0) {
|
|
387
|
+
throw new Error("tasks has no entries.");
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
function batchProgress(steps: readonly string[]): string {
|
|
392
|
+
return [
|
|
393
|
+
`Starting ${steps.length} run${steps.length === 1 ? "" : "s"}`,
|
|
394
|
+
...steps.map((step, index) => `tasks[${index}]: ${step}`),
|
|
395
|
+
].join("\n");
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
function sendText(name: string, runId: string, outcome: SendOutcome): string {
|
|
399
|
+
switch (outcome) {
|
|
400
|
+
case "started":
|
|
401
|
+
return `Run ${name} (${runId}) started a new turn with your message. A message arrives when the turn ends.`;
|
|
402
|
+
case "steered":
|
|
403
|
+
return `Run ${name} (${runId}) is running; your message steers its current turn.`;
|
|
404
|
+
case "deferred":
|
|
405
|
+
return `Run ${name} (${runId}) is ending its turn; your message starts the next turn after it.`;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
function callLine(theme: Theme, tool: string, parts: readonly (string | undefined)[]): Text {
|
|
410
|
+
return new Text(callText(theme, tool, parts), 0, 0);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
function callText(theme: Theme, tool: string, parts: readonly (string | undefined)[]): string {
|
|
414
|
+
const shown = shownParts(parts);
|
|
415
|
+
|
|
416
|
+
return (
|
|
417
|
+
theme.fg("toolTitle", theme.bold(tool)) +
|
|
418
|
+
(shown.length > 0 ? ` ${theme.fg("muted", shown.join(" · "))}` : "")
|
|
419
|
+
);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/** A `tasks` call: the run count, then one line per entry. An entry can still be streaming. */
|
|
423
|
+
function batchCall(theme: Theme, entries: readonly unknown[]): Text {
|
|
424
|
+
const lines = entries.map((entry) => {
|
|
425
|
+
const run = (typeof entry === "object" && entry !== null ? entry : {}) as Partial<RunInput>;
|
|
426
|
+
const parts = shownParts([
|
|
427
|
+
typeof run.agent === "string" ? run.agent : "inline",
|
|
428
|
+
preview(run.task),
|
|
429
|
+
]);
|
|
430
|
+
|
|
431
|
+
return ` ${theme.fg("muted", parts.join(" · "))}`;
|
|
432
|
+
});
|
|
433
|
+
const count = `${entries.length} run${entries.length === 1 ? "" : "s"}`;
|
|
434
|
+
|
|
435
|
+
return new Text([callText(theme, "subagent_start", [count]), ...lines].join("\n"), 0, 0);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
function shownParts(parts: readonly (string | undefined)[]): string[] {
|
|
439
|
+
return parts.filter((part): part is string => part !== undefined && part !== "");
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/** The call is drawn while the model still streams its arguments, so a value can be missing. */
|
|
443
|
+
function preview(text: unknown): string | undefined {
|
|
444
|
+
if (typeof text !== "string") {
|
|
445
|
+
return undefined;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
const line = text.replace(/\s+/g, " ").trim();
|
|
449
|
+
|
|
450
|
+
return line.length > 60 ? `${line.slice(0, 59)}…` : line;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/** The result text: its first line, or all of it when expanded. */
|
|
454
|
+
function renderText(
|
|
455
|
+
result: { content: readonly { type: string; text?: string }[] },
|
|
456
|
+
options: { expanded: boolean },
|
|
457
|
+
theme: Theme,
|
|
458
|
+
context: { isError: boolean },
|
|
459
|
+
): Text {
|
|
460
|
+
const text = result.content
|
|
461
|
+
.map((part) => (part.type === "text" ? (part.text ?? "") : ""))
|
|
462
|
+
.join("\n")
|
|
463
|
+
.trim();
|
|
464
|
+
const shown = options.expanded ? text : (text.split("\n")[0] ?? "");
|
|
465
|
+
|
|
466
|
+
return new Text(theme.fg(context.isError ? "error" : "toolOutput", shown), 0, 0);
|
|
467
|
+
}
|