pi-subagent-manager 0.1.1 → 0.3.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 +139 -17
- package/agents/architect.md +42 -0
- package/agents/coder.md +41 -0
- package/agents/reviewer.md +32 -0
- package/agents/tasker.md +38 -0
- package/agents/writer.md +34 -0
- package/docs/default-agents.md +105 -0
- package/docs/research-claude.md +258 -0
- package/docs/research-codex.md +143 -0
- package/docs/research-model-roles-coding.md +99 -0
- package/docs/research-model-roles-writing.md +78 -0
- package/package.json +8 -3
- package/src/agent-import.ts +245 -0
- package/src/config.ts +171 -42
- package/src/dialog.ts +631 -0
- package/src/import-discovery.ts +643 -0
- package/src/import-instructions.ts +156 -0
- package/src/import-picker.ts +231 -0
- package/src/index.ts +123 -29
- package/src/manager.ts +94 -5
- package/src/model-picker.ts +611 -0
- package/src/models.ts +60 -0
- package/src/runtime.ts +326 -64
- package/src/settings-ui.ts +173 -0
- package/src/settings.ts +269 -0
- package/src/status-ui.ts +288 -0
- package/src/thread-tree.ts +134 -0
- package/src/tools.ts +23 -11
- package/src/types.ts +31 -0
- package/src/ui.ts +924 -85
- package/agents/researcher.md +0 -14
- package/agents/worker.md +0 -6
package/src/ui.ts
CHANGED
|
@@ -7,12 +7,44 @@ import { parse as parseShell } from "shell-quote";
|
|
|
7
7
|
import {
|
|
8
8
|
type ExtensionCommandContext,
|
|
9
9
|
type ExtensionContext,
|
|
10
|
+
getSelectListTheme,
|
|
10
11
|
type Theme,
|
|
11
12
|
type ThemeColor,
|
|
12
13
|
} from "@earendil-works/pi-coding-agent";
|
|
13
|
-
import {
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
import {
|
|
15
|
+
Container,
|
|
16
|
+
SelectList,
|
|
17
|
+
Text,
|
|
18
|
+
colorToRgb,
|
|
19
|
+
rgbColor,
|
|
20
|
+
stripTerminalSequences,
|
|
21
|
+
truncateToWidth,
|
|
22
|
+
visibleWidth,
|
|
23
|
+
} from "@earendil-works/pi-tui";
|
|
24
|
+
import {
|
|
25
|
+
AGENT_COLORS,
|
|
26
|
+
ConfigStore,
|
|
27
|
+
parseAgentType,
|
|
28
|
+
serializeAgentType,
|
|
29
|
+
} from "./config.ts";
|
|
30
|
+
import { editModelPreferences, MODEL_EDITOR_CANCEL } from "./model-picker.ts";
|
|
31
|
+
import {
|
|
32
|
+
canOpenDialog,
|
|
33
|
+
dialogEditor,
|
|
34
|
+
dialogMenu,
|
|
35
|
+
dialogHeight,
|
|
36
|
+
frameDialog,
|
|
37
|
+
DIALOG_OPTIONS,
|
|
38
|
+
withDialogSession,
|
|
39
|
+
} from "./dialog.ts";
|
|
40
|
+
import { getModelPreferences } from "./models.ts";
|
|
41
|
+
import { buildStatusTree, type StatusRow } from "./thread-tree.ts";
|
|
42
|
+
import {
|
|
43
|
+
THINKING_LEVELS,
|
|
44
|
+
type AgentType,
|
|
45
|
+
type ThreadService,
|
|
46
|
+
type ThreadView,
|
|
47
|
+
} from "./types.ts";
|
|
16
48
|
|
|
17
49
|
export type ThreadController = Pick<
|
|
18
50
|
ThreadService,
|
|
@@ -24,10 +56,119 @@ export function sanitizeText(text: string): string {
|
|
|
24
56
|
return stripTerminalSequences(text).replace(/[\x00-\x1f\x7f-\x9f]/g, " ");
|
|
25
57
|
}
|
|
26
58
|
|
|
59
|
+
type AgentBadgeTheme = Pick<Theme, "fg" | "colors" | "style">;
|
|
60
|
+
|
|
61
|
+
/** Selected type token, or accent when unset or unknown. Shared by the pill and path. */
|
|
62
|
+
function agentColorToken(color: string | undefined): ThemeColor {
|
|
63
|
+
return AGENT_COLORS.includes(color as (typeof AGENT_COLORS)[number])
|
|
64
|
+
? (color as ThemeColor)
|
|
65
|
+
: "accent";
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Background pill with bold contrasting text. Label is sanitized; caller supplies brackets. */
|
|
69
|
+
function contrastPill(
|
|
70
|
+
label: string,
|
|
71
|
+
color: string | undefined,
|
|
72
|
+
theme: AgentBadgeTheme,
|
|
73
|
+
): string {
|
|
74
|
+
const background = theme.colors[agentColorToken(color)];
|
|
75
|
+
const { r, g, b } = colorToRgb(background);
|
|
76
|
+
const linear = (channel: number) => {
|
|
77
|
+
const value = channel / 255;
|
|
78
|
+
return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4;
|
|
79
|
+
};
|
|
80
|
+
const luminance =
|
|
81
|
+
0.2126 * linear(r) + 0.7152 * linear(g) + 0.0722 * linear(b);
|
|
82
|
+
// Pick whichever of black/white has the higher WCAG contrast ratio.
|
|
83
|
+
const foreground = luminance > Math.sqrt(0.0525) - 0.05 ? 0 : 255;
|
|
84
|
+
return theme.style(` ${sanitizeText(label)} `, {
|
|
85
|
+
bg: background,
|
|
86
|
+
fg: rgbColor(foreground, foreground, foreground),
|
|
87
|
+
bold: true,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function agentTypeBadge(
|
|
92
|
+
type: string,
|
|
93
|
+
color: string | undefined,
|
|
94
|
+
theme: AgentBadgeTheme,
|
|
95
|
+
): string {
|
|
96
|
+
return contrastPill(`[${type}]`, color, theme);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function agentPath(
|
|
100
|
+
path: string,
|
|
101
|
+
color: string | undefined,
|
|
102
|
+
theme: AgentBadgeTheme,
|
|
103
|
+
): string {
|
|
104
|
+
return theme.fg(agentColorToken(color), sanitizeText(path));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function metricCount(value: number | undefined): number {
|
|
108
|
+
return typeof value === "number" && Number.isFinite(value)
|
|
109
|
+
? Math.max(0, Math.floor(value))
|
|
110
|
+
: 0;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Settled active time plus the current live run, only while starting or running. */
|
|
114
|
+
function elapsedTotal(thread: ThreadView, now = Date.now()): number {
|
|
115
|
+
const live =
|
|
116
|
+
typeof thread.startedAt === "number" &&
|
|
117
|
+
Number.isFinite(thread.startedAt) &&
|
|
118
|
+
(thread.state === "starting" || thread.state === "running")
|
|
119
|
+
? now - thread.startedAt
|
|
120
|
+
: 0;
|
|
121
|
+
const total = metricCount(thread.elapsedMs) + live;
|
|
122
|
+
return Number.isFinite(total) ? total : 0;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function formatDuration(ms: number): string {
|
|
126
|
+
const total = Math.max(0, Math.floor(ms / 1000));
|
|
127
|
+
const seconds = total % 60;
|
|
128
|
+
const minutes = Math.floor(total / 60) % 60;
|
|
129
|
+
const hours = Math.floor(total / 3600) % 24;
|
|
130
|
+
const days = Math.floor(total / 86400);
|
|
131
|
+
if (days > 0) return `${days}d${hours}h`;
|
|
132
|
+
if (hours > 0) return `${hours}h${minutes}m`;
|
|
133
|
+
if (minutes > 0) return `${minutes}m${seconds}s`;
|
|
134
|
+
return `${seconds}s`;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const compactCount = new Intl.NumberFormat("en", {
|
|
138
|
+
notation: "compact",
|
|
139
|
+
maximumFractionDigits: 1,
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
function formatCount(value: number | undefined): string {
|
|
143
|
+
// Intl emits uppercase units (`1.2K`); the widget uses lowercase.
|
|
144
|
+
return compactCount.format(metricCount(value)).toLowerCase();
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function threadMetrics(thread: ThreadView, now = Date.now()): string {
|
|
148
|
+
return `${formatDuration(elapsedTotal(thread, now))} ↑${formatCount(thread.inputTokens)} ↓${formatCount(thread.outputTokens)}`;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Keep the right-hand counters intact; truncate the left side to the remaining columns. */
|
|
152
|
+
function fitLine(left: string, right: string, width: number): string {
|
|
153
|
+
if (width <= 0) return "";
|
|
154
|
+
const rightWidth = visibleWidth(right);
|
|
155
|
+
if (rightWidth >= width) return truncateToWidth(right, width, "");
|
|
156
|
+
const fitted = truncateToWidth(left, width - rightWidth - 1);
|
|
157
|
+
const pad = width - visibleWidth(fitted) - rightWidth;
|
|
158
|
+
return fitted + " ".repeat(Math.max(0, pad)) + right;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function isLive(thread: ThreadView): boolean {
|
|
162
|
+
return (
|
|
163
|
+
thread.path !== "/root" &&
|
|
164
|
+
(thread.state === "starting" || thread.state === "running")
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
|
|
27
168
|
export function renderThreads(
|
|
28
169
|
threads: ThreadView[],
|
|
29
170
|
width: number,
|
|
30
|
-
theme:
|
|
171
|
+
theme: AgentBadgeTheme,
|
|
31
172
|
): string[] {
|
|
32
173
|
const priority = (thread: ThreadView) =>
|
|
33
174
|
thread.state === "starting" || thread.state === "running"
|
|
@@ -38,27 +179,36 @@ export function renderThreads(
|
|
|
38
179
|
const visible = threads
|
|
39
180
|
.filter((thread) => thread.path !== "/root")
|
|
40
181
|
.sort(
|
|
41
|
-
(a, b) =>
|
|
182
|
+
(a, b) =>
|
|
183
|
+
priority(a) - priority(b) ||
|
|
184
|
+
b.updatedAt - a.updatedAt ||
|
|
185
|
+
b.createdAt - a.createdAt,
|
|
42
186
|
);
|
|
43
187
|
const limit = visible.length > 8 ? 7 : 8;
|
|
44
188
|
const lines = visible.slice(0, limit).map((thread) => {
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
: thread.state === "failed"
|
|
189
|
+
const stateColor =
|
|
190
|
+
thread.state === "failed"
|
|
48
191
|
? "error"
|
|
49
192
|
: thread.state === "paused"
|
|
50
193
|
? "warning"
|
|
51
194
|
: "accent";
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
195
|
+
const badge = agentTypeBadge(thread.type, thread.color, theme);
|
|
196
|
+
const path = agentPath(thread.path, thread.color, theme);
|
|
197
|
+
const state = theme.fg(stateColor, `[${sanitizeText(thread.state)}]`);
|
|
198
|
+
const left = `${badge} ${path} ${state} ${sanitizeText(thread.status || thread.task)}`;
|
|
199
|
+
return fitLine(
|
|
200
|
+
left,
|
|
201
|
+
theme.fg("muted", threadMetrics(thread)),
|
|
55
202
|
Math.max(0, width),
|
|
56
203
|
);
|
|
57
204
|
});
|
|
58
205
|
if (visible.length > limit) {
|
|
59
206
|
lines.push(
|
|
60
207
|
truncateToWidth(
|
|
61
|
-
theme.fg(
|
|
208
|
+
theme.fg(
|
|
209
|
+
"muted",
|
|
210
|
+
`+${visible.length - limit} more threads · /agents tree`,
|
|
211
|
+
),
|
|
62
212
|
Math.max(0, width),
|
|
63
213
|
),
|
|
64
214
|
);
|
|
@@ -66,19 +216,275 @@ export function renderThreads(
|
|
|
66
216
|
return lines;
|
|
67
217
|
}
|
|
68
218
|
|
|
69
|
-
|
|
219
|
+
const WIDGET_ROOT = "/root";
|
|
220
|
+
const MAX_WIDGET_LINES = 10;
|
|
221
|
+
const AGENT_WIDGET_PLACEMENT = { placement: "aboveEditor" as const };
|
|
222
|
+
|
|
223
|
+
function statePriority(state: ThreadView["state"] | undefined): number {
|
|
224
|
+
if (state === "starting" || state === "running") return 0;
|
|
225
|
+
if (state === "paused") return 1;
|
|
226
|
+
return 2;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function explicitParent(
|
|
230
|
+
path: string,
|
|
231
|
+
threadsByPath: Map<string, ThreadView>,
|
|
232
|
+
): string | null {
|
|
233
|
+
if (path === WIDGET_ROOT) return null;
|
|
234
|
+
const thread = threadsByPath.get(path);
|
|
235
|
+
if (thread)
|
|
236
|
+
return thread.parent && thread.parent !== path ? thread.parent : null;
|
|
237
|
+
const index = path.lastIndexOf("/");
|
|
238
|
+
return index > 0 ? path.slice(0, index) : null;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** Lowest state in the branch. Parent links define ancestry; cycles are guarded. */
|
|
242
|
+
function branchPriorityComparator(
|
|
243
|
+
threads: ThreadView[],
|
|
244
|
+
): (a: string, b: string) => number {
|
|
245
|
+
const threadsByPath = new Map<string, ThreadView>();
|
|
246
|
+
for (const thread of threads) {
|
|
247
|
+
if (thread?.path && !threadsByPath.has(thread.path))
|
|
248
|
+
threadsByPath.set(thread.path, thread);
|
|
249
|
+
}
|
|
250
|
+
const children = new Map<string, string[]>();
|
|
251
|
+
const link = (path: string, seen: Set<string>) => {
|
|
252
|
+
if (!path || seen.has(path)) return;
|
|
253
|
+
seen.add(path);
|
|
254
|
+
const parent = explicitParent(path, threadsByPath);
|
|
255
|
+
if (!parent || parent === path) return;
|
|
256
|
+
const list = children.get(parent) ?? [];
|
|
257
|
+
if (!list.includes(path)) list.push(path);
|
|
258
|
+
children.set(parent, list);
|
|
259
|
+
link(parent, seen);
|
|
260
|
+
};
|
|
261
|
+
link(WIDGET_ROOT, new Set());
|
|
262
|
+
for (const path of threadsByPath.keys()) link(path, new Set());
|
|
263
|
+
|
|
264
|
+
const subtree = new Map<string, number>();
|
|
265
|
+
const ancestors = new Map<string, number>();
|
|
266
|
+
const subtreeOf = (path: string, stack: Set<string>): number => {
|
|
267
|
+
const cached = subtree.get(path);
|
|
268
|
+
if (cached !== undefined) return cached;
|
|
269
|
+
const own = statePriority(threadsByPath.get(path)?.state);
|
|
270
|
+
if (stack.has(path)) return own;
|
|
271
|
+
stack.add(path);
|
|
272
|
+
let best = own;
|
|
273
|
+
for (const child of children.get(path) ?? [])
|
|
274
|
+
best = Math.min(best, subtreeOf(child, stack));
|
|
275
|
+
stack.delete(path);
|
|
276
|
+
subtree.set(path, best);
|
|
277
|
+
return best;
|
|
278
|
+
};
|
|
279
|
+
const ancestorOf = (path: string): number => {
|
|
280
|
+
const cached = ancestors.get(path);
|
|
281
|
+
if (cached !== undefined) return cached;
|
|
282
|
+
let best = 2;
|
|
283
|
+
const seen = new Set<string>();
|
|
284
|
+
let current: string | null = path;
|
|
285
|
+
while (current && !seen.has(current)) {
|
|
286
|
+
seen.add(current);
|
|
287
|
+
best = Math.min(best, statePriority(threadsByPath.get(current)?.state));
|
|
288
|
+
current = explicitParent(current, threadsByPath);
|
|
289
|
+
}
|
|
290
|
+
ancestors.set(path, best);
|
|
291
|
+
return best;
|
|
292
|
+
};
|
|
293
|
+
return (a, b) =>
|
|
294
|
+
subtreeOf(a, new Set()) - subtreeOf(b, new Set()) ||
|
|
295
|
+
ancestorOf(a) - ancestorOf(b) ||
|
|
296
|
+
(a < b ? -1 : a > b ? 1 : 0);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function activityIndent(prefix: string): string {
|
|
300
|
+
if (prefix.endsWith("├─ ")) return prefix.slice(0, -3) + "│ ";
|
|
301
|
+
if (prefix.endsWith("└─ ")) return prefix.slice(0, -3) + " ";
|
|
302
|
+
return " ".repeat(prefix.length + 3);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function agentStateColor(
|
|
306
|
+
state: ThreadView["state"],
|
|
307
|
+
): "error" | "warning" | "accent" {
|
|
308
|
+
return state === "failed"
|
|
309
|
+
? "error"
|
|
310
|
+
: state === "paused"
|
|
311
|
+
? "warning"
|
|
312
|
+
: "accent";
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function agentLine(
|
|
316
|
+
row: StatusRow,
|
|
317
|
+
width: number,
|
|
318
|
+
theme: AgentBadgeTheme,
|
|
319
|
+
): string {
|
|
320
|
+
const thread = row.thread!;
|
|
321
|
+
const left = `${row.prefix}${agentTypeBadge(thread.type, thread.color, theme)} ${agentPath(thread.path, thread.color, theme)} ${theme.fg(agentStateColor(thread.state), `[${sanitizeText(thread.state)}]`)} ${sanitizeText(thread.task)}`;
|
|
322
|
+
return fitLine(left, theme.fg("muted", threadMetrics(thread)), width);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
function activityLine(
|
|
326
|
+
row: StatusRow,
|
|
327
|
+
width: number,
|
|
328
|
+
theme: AgentBadgeTheme,
|
|
329
|
+
): string {
|
|
330
|
+
const thread = row.thread!;
|
|
331
|
+
return truncateToWidth(
|
|
332
|
+
`${activityIndent(row.prefix)}${theme.fg("muted", sanitizeText(thread.status || thread.task))}`,
|
|
333
|
+
width,
|
|
334
|
+
"",
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function placeholderLine(
|
|
339
|
+
row: StatusRow,
|
|
340
|
+
width: number,
|
|
341
|
+
theme: AgentBadgeTheme,
|
|
342
|
+
): string {
|
|
343
|
+
return truncateToWidth(
|
|
344
|
+
`${row.prefix}${theme.fg("muted", `${sanitizeText(row.path)} missing parent`)}`,
|
|
345
|
+
width,
|
|
346
|
+
"",
|
|
347
|
+
);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
function takeWidgetRows(rows: StatusRow[], budget: number): StatusRow[] {
|
|
351
|
+
const parents = new Map<number, number[]>();
|
|
352
|
+
const stack: number[] = [];
|
|
353
|
+
for (const [index, row] of rows.entries()) {
|
|
354
|
+
while (
|
|
355
|
+
stack.length &&
|
|
356
|
+
rows[stack[stack.length - 1]!]!.prefix.length >= row.prefix.length
|
|
357
|
+
)
|
|
358
|
+
stack.pop();
|
|
359
|
+
parents.set(index, [...stack]);
|
|
360
|
+
stack.push(index);
|
|
361
|
+
}
|
|
362
|
+
const rowCost = (row: StatusRow) =>
|
|
363
|
+
row.path === WIDGET_ROOT ? 0 : row.thread ? 2 : 1;
|
|
364
|
+
const candidates = rows
|
|
365
|
+
.map((row, index) => ({ row, index }))
|
|
366
|
+
.filter(({ row }) => row.thread && row.path !== WIDGET_ROOT)
|
|
367
|
+
.sort(
|
|
368
|
+
(a, b) =>
|
|
369
|
+
statePriority(a.row.thread!.state) -
|
|
370
|
+
statePriority(b.row.thread!.state) || a.index - b.index,
|
|
371
|
+
);
|
|
372
|
+
const chosen = new Set<number>();
|
|
373
|
+
let used = 0;
|
|
374
|
+
for (const { index } of candidates) {
|
|
375
|
+
const needed = [...parents.get(index)!, index].filter(
|
|
376
|
+
(ancestor) => !chosen.has(ancestor),
|
|
377
|
+
);
|
|
378
|
+
const cost = needed.reduce(
|
|
379
|
+
(total, ancestor) => total + rowCost(rows[ancestor]!),
|
|
380
|
+
0,
|
|
381
|
+
);
|
|
382
|
+
if (used + cost > budget) continue;
|
|
383
|
+
for (const ancestor of needed) chosen.add(ancestor);
|
|
384
|
+
used += cost;
|
|
385
|
+
}
|
|
386
|
+
// If a missing-parent chain cannot fit its agent, retain a useful tree prefix.
|
|
387
|
+
if (!chosen.size) {
|
|
388
|
+
for (const [index, row] of rows.entries()) {
|
|
389
|
+
const cost = rowCost(row);
|
|
390
|
+
if (used + cost > budget) break;
|
|
391
|
+
chosen.add(index);
|
|
392
|
+
used += cost;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
return rows.filter(
|
|
396
|
+
(row, index) => row.path !== WIDGET_ROOT && chosen.has(index),
|
|
397
|
+
);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/** Themed agent tree above the editor. Synthetic /root is the main conversation and is omitted. */
|
|
401
|
+
export function renderAgentTree(
|
|
402
|
+
threads: ThreadView[],
|
|
403
|
+
width: number,
|
|
404
|
+
theme: AgentBadgeTheme,
|
|
405
|
+
): string[] {
|
|
406
|
+
const columns = Math.max(0, width);
|
|
407
|
+
const rows = buildStatusTree(
|
|
408
|
+
threads,
|
|
409
|
+
new Set(),
|
|
410
|
+
branchPriorityComparator(threads),
|
|
411
|
+
);
|
|
412
|
+
const agents = rows.filter((row) => row.thread && row.path !== WIDGET_ROOT);
|
|
413
|
+
if (!agents.length) return [];
|
|
414
|
+
const live = agents.filter(
|
|
415
|
+
(row) =>
|
|
416
|
+
row.thread!.state === "starting" || row.thread!.state === "running",
|
|
417
|
+
).length;
|
|
418
|
+
const paused = agents.filter((row) => row.thread!.state === "paused").length;
|
|
419
|
+
const heading = fitLine(
|
|
420
|
+
theme.fg("accent", "Agents"),
|
|
421
|
+
theme.fg("muted", `${live} live · ${paused} paused`),
|
|
422
|
+
columns,
|
|
423
|
+
);
|
|
424
|
+
let visible = takeWidgetRows(rows, MAX_WIDGET_LINES - 1);
|
|
425
|
+
let omitted = agents.length - visible.filter((row) => row.thread).length;
|
|
426
|
+
if (omitted > 0) {
|
|
427
|
+
visible = takeWidgetRows(rows, MAX_WIDGET_LINES - 2);
|
|
428
|
+
omitted = agents.length - visible.filter((row) => row.thread).length;
|
|
429
|
+
}
|
|
430
|
+
const lines = [heading];
|
|
431
|
+
for (const row of visible) {
|
|
432
|
+
if (!row.thread) {
|
|
433
|
+
lines.push(placeholderLine(row, columns, theme));
|
|
434
|
+
continue;
|
|
435
|
+
}
|
|
436
|
+
lines.push(agentLine(row, columns, theme));
|
|
437
|
+
lines.push(activityLine(row, columns, theme));
|
|
438
|
+
}
|
|
439
|
+
if (omitted > 0) {
|
|
440
|
+
lines.push(
|
|
441
|
+
truncateToWidth(
|
|
442
|
+
theme.fg("muted", `+${omitted} more agents · /agents tree`),
|
|
443
|
+
columns,
|
|
444
|
+
"",
|
|
445
|
+
),
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
return lines;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
export function updateWidget(
|
|
452
|
+
ctx: ExtensionContext,
|
|
453
|
+
threads: ThreadView[],
|
|
454
|
+
): void {
|
|
70
455
|
if (!ctx.hasUI) return;
|
|
71
456
|
if (!threads.some((thread) => thread.path !== "/root")) {
|
|
72
|
-
ctx.ui.setWidget("pi-subagent", undefined,
|
|
457
|
+
ctx.ui.setWidget("pi-subagent", undefined, AGENT_WIDGET_PLACEMENT);
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
// RPC hosts accept string widgets only; a component factory is ignored.
|
|
461
|
+
if (ctx.mode === "rpc") {
|
|
462
|
+
ctx.ui.setWidget(
|
|
463
|
+
"pi-subagent",
|
|
464
|
+
renderAgentTree(threads, 80, ctx.ui.theme),
|
|
465
|
+
AGENT_WIDGET_PLACEMENT,
|
|
466
|
+
);
|
|
73
467
|
return;
|
|
74
468
|
}
|
|
469
|
+
const snapshot = threads.map((thread) => ({ ...thread }));
|
|
75
470
|
ctx.ui.setWidget(
|
|
76
471
|
"pi-subagent",
|
|
77
|
-
() =>
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
472
|
+
(tui) => {
|
|
473
|
+
let timer: ReturnType<typeof setInterval> | undefined;
|
|
474
|
+
if (snapshot.some(isLive)) {
|
|
475
|
+
timer = setInterval(() => tui.requestRender(), 1000);
|
|
476
|
+
timer.unref();
|
|
477
|
+
}
|
|
478
|
+
return {
|
|
479
|
+
render: (width) => renderAgentTree(snapshot, width, ctx.ui.theme),
|
|
480
|
+
invalidate: () => {},
|
|
481
|
+
dispose: () => {
|
|
482
|
+
if (timer) clearInterval(timer);
|
|
483
|
+
timer = undefined;
|
|
484
|
+
},
|
|
485
|
+
};
|
|
486
|
+
},
|
|
487
|
+
AGENT_WIDGET_PLACEMENT,
|
|
82
488
|
);
|
|
83
489
|
}
|
|
84
490
|
|
|
@@ -92,7 +498,9 @@ export async function showThreads(
|
|
|
92
498
|
while (true) {
|
|
93
499
|
try {
|
|
94
500
|
if (!selectedPath) {
|
|
95
|
-
const threads = controller
|
|
501
|
+
const threads = controller
|
|
502
|
+
.list()
|
|
503
|
+
.filter((thread) => thread.path !== "/root");
|
|
96
504
|
if (!threads.length) {
|
|
97
505
|
ctx.ui.notify("No subagent threads.", "info");
|
|
98
506
|
return;
|
|
@@ -109,7 +517,9 @@ export async function showThreads(
|
|
|
109
517
|
if (selectedPath === "/root")
|
|
110
518
|
throw new Error("/root is the current Pi thread, not a subagent.");
|
|
111
519
|
const thread = controller.get(selectedPath);
|
|
112
|
-
const children = controller
|
|
520
|
+
const children = controller
|
|
521
|
+
.list()
|
|
522
|
+
.filter((child) => child.parent === selectedPath);
|
|
113
523
|
const title = `${sanitizeText(thread.path)} [${sanitizeText(thread.state)}] ${sanitizeText(thread.status)}`;
|
|
114
524
|
const action = await ctx.ui.select(title, [
|
|
115
525
|
"View output",
|
|
@@ -135,7 +545,10 @@ export async function showThreads(
|
|
|
135
545
|
text.split("\n").map(sanitizeText).join("\n"),
|
|
136
546
|
);
|
|
137
547
|
} else if (action === "Send input / resume") {
|
|
138
|
-
const message = await ctx.ui.editor(
|
|
548
|
+
const message = await ctx.ui.editor(
|
|
549
|
+
"Send input / resume retained session",
|
|
550
|
+
"",
|
|
551
|
+
);
|
|
139
552
|
if (message?.trim()) await controller.steer(selectedPath, message);
|
|
140
553
|
} else if (action === "Stop") {
|
|
141
554
|
if (
|
|
@@ -152,10 +565,14 @@ export async function showThreads(
|
|
|
152
565
|
);
|
|
153
566
|
const selected = await ctx.ui.select("Children", labels);
|
|
154
567
|
if (selected !== undefined)
|
|
155
|
-
selectedPath =
|
|
568
|
+
selectedPath =
|
|
569
|
+
children[labels.indexOf(selected)]?.path ?? selectedPath;
|
|
156
570
|
}
|
|
157
571
|
} catch (error) {
|
|
158
|
-
ctx.ui.notify(
|
|
572
|
+
ctx.ui.notify(
|
|
573
|
+
sanitizeText(error instanceof Error ? error.message : String(error)),
|
|
574
|
+
"error",
|
|
575
|
+
);
|
|
159
576
|
return;
|
|
160
577
|
}
|
|
161
578
|
}
|
|
@@ -172,9 +589,15 @@ export function editorArguments(command: string): string[] {
|
|
|
172
589
|
return args as string[];
|
|
173
590
|
}
|
|
174
591
|
|
|
175
|
-
async function externalEdit(
|
|
176
|
-
|
|
177
|
-
|
|
592
|
+
async function externalEdit(
|
|
593
|
+
ctx: ExtensionCommandContext,
|
|
594
|
+
text: string,
|
|
595
|
+
): Promise<string> {
|
|
596
|
+
if (ctx.mode !== "tui")
|
|
597
|
+
throw new Error("External editing requires interactive TUI mode.");
|
|
598
|
+
const [command, ...args] = editorArguments(
|
|
599
|
+
process.env.VISUAL || process.env.EDITOR || "vi",
|
|
600
|
+
);
|
|
178
601
|
const directory = await mkdtemp(join(tmpdir(), "pi-subagent-"));
|
|
179
602
|
const file = join(directory, "agent.md");
|
|
180
603
|
try {
|
|
@@ -188,7 +611,9 @@ async function externalEdit(ctx: ExtensionCommandContext, text: string): Promise
|
|
|
188
611
|
});
|
|
189
612
|
if (result.error) throw result.error;
|
|
190
613
|
if (result.status !== 0)
|
|
191
|
-
throw new Error(
|
|
614
|
+
throw new Error(
|
|
615
|
+
`Editor exited with status ${result.status ?? result.signal}.`,
|
|
616
|
+
);
|
|
192
617
|
} finally {
|
|
193
618
|
tui.start();
|
|
194
619
|
tui.requestRender(true);
|
|
@@ -207,9 +632,24 @@ async function editToolList(
|
|
|
207
632
|
type: AgentType,
|
|
208
633
|
field: "allow" | "block",
|
|
209
634
|
): Promise<void> {
|
|
210
|
-
const
|
|
211
|
-
|
|
212
|
-
|
|
635
|
+
const modes = [
|
|
636
|
+
"Unset (use default policy)",
|
|
637
|
+
"Empty list",
|
|
638
|
+
"Enter exact tool names",
|
|
639
|
+
];
|
|
640
|
+
const current = type.tools?.[field];
|
|
641
|
+
const mode = await dialogMenu(
|
|
642
|
+
ctx,
|
|
643
|
+
`tools.${field} (${sanitizeText(toolListMenuValue(current))})`,
|
|
644
|
+
modes.map((label) => ({ id: label, label })),
|
|
645
|
+
{
|
|
646
|
+
selectedId:
|
|
647
|
+
current === undefined
|
|
648
|
+
? modes[0]
|
|
649
|
+
: current.length === 0
|
|
650
|
+
? modes[1]
|
|
651
|
+
: modes[2],
|
|
652
|
+
},
|
|
213
653
|
);
|
|
214
654
|
if (!mode) return;
|
|
215
655
|
if (mode === "Unset (use default policy)") {
|
|
@@ -218,9 +658,10 @@ async function editToolList(
|
|
|
218
658
|
const value =
|
|
219
659
|
mode === "Empty list"
|
|
220
660
|
? ""
|
|
221
|
-
: await
|
|
661
|
+
: await dialogEditor(
|
|
662
|
+
ctx,
|
|
222
663
|
`tools.${field}: comma-separated exact names`,
|
|
223
|
-
type.tools?.[field]?.join(", "),
|
|
664
|
+
type.tools?.[field]?.join(", ") ?? "",
|
|
224
665
|
);
|
|
225
666
|
if (value === undefined) return;
|
|
226
667
|
type.tools ??= {};
|
|
@@ -232,6 +673,159 @@ async function editToolList(
|
|
|
232
673
|
if (type.tools && !Object.keys(type.tools).length) delete type.tools;
|
|
233
674
|
}
|
|
234
675
|
|
|
676
|
+
const AGENT_COLOR_DEFAULT = "__default__";
|
|
677
|
+
const AGENT_COLOR_CANCEL = Symbol("agent-color-cancel");
|
|
678
|
+
|
|
679
|
+
class AgentColorPickerComponent extends Container {
|
|
680
|
+
private readonly items: {
|
|
681
|
+
value: string;
|
|
682
|
+
label: string;
|
|
683
|
+
description?: string;
|
|
684
|
+
}[];
|
|
685
|
+
private readonly preview: Text;
|
|
686
|
+
private readonly theme: Theme;
|
|
687
|
+
readonly selectList: SelectList;
|
|
688
|
+
|
|
689
|
+
constructor(
|
|
690
|
+
private readonly tui: {
|
|
691
|
+
requestRender(force?: boolean): void;
|
|
692
|
+
terminal?: { rows: number };
|
|
693
|
+
},
|
|
694
|
+
theme: Theme,
|
|
695
|
+
currentColor: AgentType["color"] | undefined,
|
|
696
|
+
private readonly agentName: string,
|
|
697
|
+
onSelect: (color: AgentType["color"] | undefined) => void,
|
|
698
|
+
onCancel: () => void,
|
|
699
|
+
) {
|
|
700
|
+
super();
|
|
701
|
+
this.theme = theme;
|
|
702
|
+
this.items = [
|
|
703
|
+
{
|
|
704
|
+
value: AGENT_COLOR_DEFAULT,
|
|
705
|
+
label: "Default (inherit)",
|
|
706
|
+
description: "Use the default accent background for the type pill",
|
|
707
|
+
},
|
|
708
|
+
...AGENT_COLORS.map((color) => ({
|
|
709
|
+
value: color,
|
|
710
|
+
label: contrastPill(color, color, theme),
|
|
711
|
+
description: `Type pill background: ${color}`,
|
|
712
|
+
})),
|
|
713
|
+
];
|
|
714
|
+
this.preview = new Text();
|
|
715
|
+
this.selectList = new SelectList(this.items, 10, getSelectListTheme(), {
|
|
716
|
+
minPrimaryColumnWidth: 18,
|
|
717
|
+
maxPrimaryColumnWidth: 28,
|
|
718
|
+
});
|
|
719
|
+
const selectedIndex = this.items.findIndex(
|
|
720
|
+
(item) => item.value === (currentColor ?? AGENT_COLOR_DEFAULT),
|
|
721
|
+
);
|
|
722
|
+
this.selectList.setSelectedIndex(selectedIndex >= 0 ? selectedIndex : 0);
|
|
723
|
+
this.selectList.onSelectionChange = (item) => {
|
|
724
|
+
this.updatePreview(tui, item.value);
|
|
725
|
+
};
|
|
726
|
+
this.selectList.onSelect = (item) => {
|
|
727
|
+
onSelect(
|
|
728
|
+
item.value === AGENT_COLOR_DEFAULT
|
|
729
|
+
? undefined
|
|
730
|
+
: (item.value as AgentType["color"]),
|
|
731
|
+
);
|
|
732
|
+
};
|
|
733
|
+
this.selectList.onCancel = onCancel;
|
|
734
|
+
this.updatePreview(
|
|
735
|
+
tui,
|
|
736
|
+
this.items[selectedIndex >= 0 ? selectedIndex : 0]!.value,
|
|
737
|
+
);
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
getItems(): readonly {
|
|
741
|
+
value: string;
|
|
742
|
+
label: string;
|
|
743
|
+
description?: string;
|
|
744
|
+
}[] {
|
|
745
|
+
return this.items;
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
render(width: number): string[] {
|
|
749
|
+
const height = dialogHeight(this.tui);
|
|
750
|
+
const budget = Math.max(1, height - 7);
|
|
751
|
+
const selected = this.items.findIndex(
|
|
752
|
+
(item) => item.value === this.selectList.getSelectedItem()?.value,
|
|
753
|
+
);
|
|
754
|
+
const start = Math.max(
|
|
755
|
+
0,
|
|
756
|
+
Math.min(selected - budget + 1, this.items.length - budget),
|
|
757
|
+
);
|
|
758
|
+
const body = [
|
|
759
|
+
` ${truncateToWidth(
|
|
760
|
+
this.preview
|
|
761
|
+
.render(1000)
|
|
762
|
+
.map((line) => line.trim())
|
|
763
|
+
.filter(Boolean)
|
|
764
|
+
.join(" "),
|
|
765
|
+
Math.max(1, width - 4),
|
|
766
|
+
"",
|
|
767
|
+
)}`,
|
|
768
|
+
"",
|
|
769
|
+
...this.items
|
|
770
|
+
.slice(start, start + budget)
|
|
771
|
+
.map(
|
|
772
|
+
(item, i) =>
|
|
773
|
+
`${start + i === selected ? "›" : " "} ${item.label} ${this.theme.fg("muted", item.description ?? "")}`,
|
|
774
|
+
),
|
|
775
|
+
];
|
|
776
|
+
return frameDialog(
|
|
777
|
+
this.theme,
|
|
778
|
+
width,
|
|
779
|
+
height,
|
|
780
|
+
"Agent color",
|
|
781
|
+
body,
|
|
782
|
+
"↑↓ preview · Enter apply · Esc cancel",
|
|
783
|
+
);
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
getPreview(): Text {
|
|
787
|
+
return this.preview;
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
getSelectList(): SelectList {
|
|
791
|
+
return this.selectList;
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
handleInput(keyData: string): void {
|
|
795
|
+
this.selectList.handleInput(keyData);
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
private updatePreview(
|
|
799
|
+
tui: { requestRender(force?: boolean): void },
|
|
800
|
+
value: string,
|
|
801
|
+
): void {
|
|
802
|
+
const color = value === AGENT_COLOR_DEFAULT ? undefined : value;
|
|
803
|
+
const badge = agentTypeBadge(this.agentName, color, this.theme);
|
|
804
|
+
const path = agentPath("/root/example-task", color, this.theme);
|
|
805
|
+
this.preview.setText(
|
|
806
|
+
`Preview: ${badge} ${path} ${this.theme.fg("accent", "[running]")} Working`,
|
|
807
|
+
);
|
|
808
|
+
tui.requestRender();
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
async function selectAgentColor(
|
|
813
|
+
ctx: ExtensionCommandContext,
|
|
814
|
+
currentColor: AgentType["color"] | undefined,
|
|
815
|
+
agentName: string,
|
|
816
|
+
): Promise<AgentType["color"] | undefined | typeof AGENT_COLOR_CANCEL> {
|
|
817
|
+
return ctx.ui.custom((tui, theme, _keys, done) => {
|
|
818
|
+
return new AgentColorPickerComponent(
|
|
819
|
+
tui,
|
|
820
|
+
theme,
|
|
821
|
+
currentColor,
|
|
822
|
+
agentName,
|
|
823
|
+
(value) => done(value),
|
|
824
|
+
() => done(AGENT_COLOR_CANCEL),
|
|
825
|
+
);
|
|
826
|
+
}, DIALOG_OPTIONS);
|
|
827
|
+
}
|
|
828
|
+
|
|
235
829
|
async function editDocument(
|
|
236
830
|
ctx: ExtensionCommandContext,
|
|
237
831
|
type: AgentType,
|
|
@@ -245,7 +839,8 @@ async function editDocument(
|
|
|
245
839
|
text = await externalEdit(ctx, text);
|
|
246
840
|
} else {
|
|
247
841
|
const end = text.indexOf("\n---", 4);
|
|
248
|
-
const yaml = await
|
|
842
|
+
const yaml = await dialogEditor(
|
|
843
|
+
ctx,
|
|
249
844
|
"Agent frontmatter YAML (Markdown body preserved)",
|
|
250
845
|
text.slice(4, end),
|
|
251
846
|
);
|
|
@@ -258,15 +853,28 @@ async function editDocument(
|
|
|
258
853
|
source: type.source,
|
|
259
854
|
};
|
|
260
855
|
} catch (error) {
|
|
261
|
-
const diagnostic = sanitizeText(
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
856
|
+
const diagnostic = sanitizeText(
|
|
857
|
+
error instanceof Error ? error.message : String(error),
|
|
858
|
+
);
|
|
859
|
+
ctx.ui.notify(
|
|
860
|
+
`Edit not accepted: ${diagnostic}. Original definition is unchanged.`,
|
|
861
|
+
"error",
|
|
862
|
+
);
|
|
863
|
+
const action = await dialogMenu(ctx, "Invalid edit — retry?", [
|
|
864
|
+
{
|
|
865
|
+
id: "Retry",
|
|
866
|
+
label: "Retry",
|
|
867
|
+
value: "Continue editing",
|
|
868
|
+
help: diagnostic,
|
|
869
|
+
},
|
|
870
|
+
{
|
|
871
|
+
id: "Cancel",
|
|
872
|
+
label: "Cancel",
|
|
873
|
+
value: "Restore previous definition",
|
|
874
|
+
help: diagnostic,
|
|
875
|
+
},
|
|
876
|
+
]);
|
|
877
|
+
if (action !== "Retry") return;
|
|
270
878
|
}
|
|
271
879
|
}
|
|
272
880
|
}
|
|
@@ -281,7 +889,9 @@ function assertSaveDestination(
|
|
|
281
889
|
const entries = store.list();
|
|
282
890
|
const sameName = entries.find((entry) => entry.name === type.name);
|
|
283
891
|
if (sameName && (!original || type.name !== original.name)) {
|
|
284
|
-
throw new Error(
|
|
892
|
+
throw new Error(
|
|
893
|
+
`Agent type ${type.name} already exists; choose a different name.`,
|
|
894
|
+
);
|
|
285
895
|
}
|
|
286
896
|
const destination = store.destination(type.name, scope, original);
|
|
287
897
|
if (
|
|
@@ -294,45 +904,209 @@ function assertSaveDestination(
|
|
|
294
904
|
}
|
|
295
905
|
}
|
|
296
906
|
|
|
907
|
+
const EDIT_MENU_ACTIONS = [
|
|
908
|
+
"name",
|
|
909
|
+
"description",
|
|
910
|
+
"models",
|
|
911
|
+
"modelSuggestions",
|
|
912
|
+
"thinkingLevel",
|
|
913
|
+
"tools.allow",
|
|
914
|
+
"tools.block",
|
|
915
|
+
"color",
|
|
916
|
+
"systemPrompt",
|
|
917
|
+
"Save scope",
|
|
918
|
+
"Source",
|
|
919
|
+
"Edit frontmatter YAML",
|
|
920
|
+
"External editor (entire Markdown)",
|
|
921
|
+
"Save",
|
|
922
|
+
"Cancel",
|
|
923
|
+
] as const;
|
|
924
|
+
|
|
925
|
+
type EditMenuAction = (typeof EDIT_MENU_ACTIONS)[number];
|
|
926
|
+
|
|
927
|
+
const EDIT_FIELD_LABELS: Record<EditMenuAction, string> = {
|
|
928
|
+
name: "Name",
|
|
929
|
+
description: "Description",
|
|
930
|
+
models: "Models",
|
|
931
|
+
modelSuggestions: "Model suggestions",
|
|
932
|
+
thinkingLevel: "Thinking level",
|
|
933
|
+
"tools.allow": "Allowed tools",
|
|
934
|
+
"tools.block": "Blocked tools",
|
|
935
|
+
color: "Color",
|
|
936
|
+
systemPrompt: "System prompt",
|
|
937
|
+
"Save scope": "Save scope",
|
|
938
|
+
Source: "Source (read-only)",
|
|
939
|
+
"Edit frontmatter YAML": "Frontmatter YAML",
|
|
940
|
+
"External editor (entire Markdown)": "External editor",
|
|
941
|
+
Save: "Save",
|
|
942
|
+
Cancel: "Cancel",
|
|
943
|
+
};
|
|
944
|
+
|
|
945
|
+
const EDIT_FIELD_HELP: Record<EditMenuAction, string> = {
|
|
946
|
+
name: "Unique type identifier. Renaming retains the original file.",
|
|
947
|
+
description: "Description shown when browsing and spawning agents.",
|
|
948
|
+
models:
|
|
949
|
+
"Ordered model preferences. Unavailable models are skipped at runtime. Advisory names in model suggestions never pin a model.",
|
|
950
|
+
modelSuggestions:
|
|
951
|
+
"Advisory display names to help pick a model. Not provider/model pins; never selected or required. One name per line; empty clears.",
|
|
952
|
+
thinkingLevel: "Reasoning effort. Default follows the parent session.",
|
|
953
|
+
"tools.allow":
|
|
954
|
+
"Unset uses default policy; an empty list explicitly allows no tools.",
|
|
955
|
+
"tools.block": "Exact tool names to remove from the allowed set.",
|
|
956
|
+
color: "Thread widget color with live preview.",
|
|
957
|
+
systemPrompt: "Edit the Markdown prompt body in a multiline dialog.",
|
|
958
|
+
"Save scope":
|
|
959
|
+
"Project definitions override global definitions with the same name.",
|
|
960
|
+
Source:
|
|
961
|
+
"Read-only source. Bundled definitions are copied, never overwritten.",
|
|
962
|
+
"Edit frontmatter YAML": "Edit metadata as YAML, preserving the prompt body.",
|
|
963
|
+
"External editor (entire Markdown)":
|
|
964
|
+
"Edit the complete definition using $VISUAL / $EDITOR.",
|
|
965
|
+
Save: "Validate and save. Existing sessions keep their original configuration.",
|
|
966
|
+
Cancel: "Close without saving any edits.",
|
|
967
|
+
};
|
|
968
|
+
|
|
969
|
+
async function chooseSaveScope(
|
|
970
|
+
ctx: ExtensionCommandContext,
|
|
971
|
+
store: ConfigStore,
|
|
972
|
+
current: "user" | "project",
|
|
973
|
+
): Promise<"user" | "project" | undefined> {
|
|
974
|
+
const choices = [
|
|
975
|
+
"Global",
|
|
976
|
+
...(store.canSaveProject() ? ["Trusted project"] : []),
|
|
977
|
+
];
|
|
978
|
+
const value = await dialogMenu(
|
|
979
|
+
ctx,
|
|
980
|
+
"Save scope",
|
|
981
|
+
choices.map((label) => ({ id: label, label })),
|
|
982
|
+
{ selectedId: current === "user" ? "Global" : "Trusted project" },
|
|
983
|
+
);
|
|
984
|
+
return value ? (value === "Global" ? "user" : "project") : undefined;
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
/** Omitted tool lists use the default policy; [] is an explicit empty list, not inheritance. */
|
|
988
|
+
function toolListMenuValue(names: string[] | undefined): string {
|
|
989
|
+
if (names === undefined) return "Unset (use default policy)";
|
|
990
|
+
if (names.length === 0) return "Empty list";
|
|
991
|
+
return names.join(", ");
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
function modelSuggestionLabel(suggestions: string[] | undefined): string {
|
|
995
|
+
if (suggestions === undefined || suggestions.length === 0) return "None";
|
|
996
|
+
return suggestions.join(", ");
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
function modelSuggestionPrefill(suggestions: string[] | undefined): string {
|
|
1000
|
+
return (suggestions ?? []).map((name) => sanitizeText(name)).join("\n");
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
function editMenuLabel(action: EditMenuAction, draft: AgentType): string {
|
|
1004
|
+
switch (action) {
|
|
1005
|
+
case "name":
|
|
1006
|
+
return `name: ${sanitizeText(draft.name)}`;
|
|
1007
|
+
case "description":
|
|
1008
|
+
return `description: ${sanitizeText(draft.description)}`;
|
|
1009
|
+
case "models": {
|
|
1010
|
+
const models = getModelPreferences(draft);
|
|
1011
|
+
return `models: ${sanitizeText(models?.join(", ") ?? "Default (inherit)")}`;
|
|
1012
|
+
}
|
|
1013
|
+
case "modelSuggestions":
|
|
1014
|
+
return `modelSuggestions: ${sanitizeText(modelSuggestionLabel(draft.modelSuggestions))}`;
|
|
1015
|
+
case "thinkingLevel":
|
|
1016
|
+
return `thinkingLevel: ${sanitizeText(draft.thinkingLevel ?? "Default (inherit)")}`;
|
|
1017
|
+
case "tools.allow":
|
|
1018
|
+
return `tools.allow: ${sanitizeText(toolListMenuValue(draft.tools?.allow))}`;
|
|
1019
|
+
case "tools.block":
|
|
1020
|
+
return `tools.block: ${sanitizeText(toolListMenuValue(draft.tools?.block))}`;
|
|
1021
|
+
case "color":
|
|
1022
|
+
return `color: ${sanitizeText(draft.color ?? "Default (inherit)")}`;
|
|
1023
|
+
case "systemPrompt":
|
|
1024
|
+
return `systemPrompt: ${draft.systemPrompt.split("\n").length} lines · ${draft.systemPrompt.length} characters`;
|
|
1025
|
+
default:
|
|
1026
|
+
return action;
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
|
|
297
1030
|
export async function editAgentTypes(
|
|
298
1031
|
ctx: ExtensionCommandContext,
|
|
299
1032
|
store: ConfigStore,
|
|
300
1033
|
): Promise<void> {
|
|
301
|
-
if (!ctx
|
|
1034
|
+
if (!canOpenDialog(ctx)) return;
|
|
1035
|
+
await withDialogSession(ctx, (scoped) => editAgentTypesDialog(scoped, store));
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
async function editAgentTypesDialog(
|
|
1039
|
+
ctx: ExtensionCommandContext,
|
|
1040
|
+
store: ConfigStore,
|
|
1041
|
+
): Promise<void> {
|
|
302
1042
|
while (true) {
|
|
303
1043
|
const types = store.list();
|
|
304
|
-
const
|
|
305
|
-
|
|
1044
|
+
const selection = await dialogMenu(ctx, "Agent types", [
|
|
1045
|
+
{
|
|
1046
|
+
id: "Create new type",
|
|
1047
|
+
label: "Create new type",
|
|
1048
|
+
value: "New definition",
|
|
1049
|
+
help: "Nothing is written until you save.",
|
|
1050
|
+
},
|
|
1051
|
+
...types.map((type) => ({
|
|
1052
|
+
id: type.name,
|
|
1053
|
+
label: type.name,
|
|
1054
|
+
value: type.description,
|
|
1055
|
+
help: `${type.source ?? "user"} · ${type.filePath ?? ""}`,
|
|
1056
|
+
})),
|
|
1057
|
+
]);
|
|
306
1058
|
if (selection === undefined) return;
|
|
307
|
-
const original =
|
|
1059
|
+
const original =
|
|
1060
|
+
selection === "Create new type" ? undefined : store.get(selection);
|
|
308
1061
|
let draft: AgentType = original
|
|
309
1062
|
? structuredClone(original)
|
|
310
1063
|
: { name: "new-agent", description: "New agent", systemPrompt: "" };
|
|
1064
|
+
let selectedId: string | undefined;
|
|
1065
|
+
let saveScope: "user" | "project" =
|
|
1066
|
+
original?.source === "project" && store.canSaveProject()
|
|
1067
|
+
? "project"
|
|
1068
|
+
: "user";
|
|
311
1069
|
while (true) {
|
|
312
|
-
const field = await
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
1070
|
+
const field = await dialogMenu(
|
|
1071
|
+
ctx,
|
|
1072
|
+
`Edit ${sanitizeText(draft.name)} (unsaved)`,
|
|
1073
|
+
EDIT_MENU_ACTIONS.map((action) => {
|
|
1074
|
+
const decorated = editMenuLabel(action, draft);
|
|
1075
|
+
const separator = decorated.indexOf(": ");
|
|
1076
|
+
return {
|
|
1077
|
+
id: action,
|
|
1078
|
+
label: EDIT_FIELD_LABELS[action],
|
|
1079
|
+
value:
|
|
1080
|
+
action === "Source"
|
|
1081
|
+
? (original?.source ?? "New draft")
|
|
1082
|
+
: action === "Save scope"
|
|
1083
|
+
? saveScope === "user"
|
|
1084
|
+
? "Global"
|
|
1085
|
+
: "Trusted project"
|
|
1086
|
+
: separator >= 0
|
|
1087
|
+
? decorated.slice(separator + 2)
|
|
1088
|
+
: "",
|
|
1089
|
+
help:
|
|
1090
|
+
action === "Source"
|
|
1091
|
+
? (original?.filePath ??
|
|
1092
|
+
"Not saved yet. Bundled definitions are copied, never overwritten.")
|
|
1093
|
+
: EDIT_FIELD_HELP[action],
|
|
1094
|
+
};
|
|
1095
|
+
}),
|
|
1096
|
+
{
|
|
1097
|
+
selectedId,
|
|
1098
|
+
saveId: "Save",
|
|
1099
|
+
footer: "↑↓/Tab fields · Enter edit · Ctrl+S save · Esc cancel",
|
|
1100
|
+
},
|
|
1101
|
+
);
|
|
1102
|
+
selectedId = field;
|
|
325
1103
|
if (!field || field === "Cancel") break;
|
|
326
1104
|
try {
|
|
327
1105
|
if (field === "Save") {
|
|
328
1106
|
// Round-trip before saving: no partial or invalid configuration is accepted.
|
|
329
1107
|
const validated = parseAgentType(serializeAgentType(draft));
|
|
330
|
-
const
|
|
331
|
-
|
|
332
|
-
store.canSaveProject() ? ["Global", "Trusted project"] : ["Global"],
|
|
333
|
-
);
|
|
334
|
-
if (!scopeChoice) continue;
|
|
335
|
-
const scope = scopeChoice === "Global" ? "user" : "project";
|
|
1108
|
+
const scope = await chooseSaveScope(ctx, store, saveScope);
|
|
1109
|
+
if (!scope) continue;
|
|
336
1110
|
assertSaveDestination(store, validated, original, scope);
|
|
337
1111
|
const saved = store.save(validated, scope, original);
|
|
338
1112
|
ctx.ui.notify(
|
|
@@ -341,7 +1115,22 @@ export async function editAgentTypes(
|
|
|
341
1115
|
);
|
|
342
1116
|
break;
|
|
343
1117
|
}
|
|
344
|
-
if (field === "
|
|
1118
|
+
if (field === "Save scope") {
|
|
1119
|
+
const scope = await chooseSaveScope(ctx, store, saveScope);
|
|
1120
|
+
if (scope) saveScope = scope;
|
|
1121
|
+
continue;
|
|
1122
|
+
}
|
|
1123
|
+
if (field === "Source") {
|
|
1124
|
+
ctx.ui.notify(
|
|
1125
|
+
original?.filePath ?? "This definition has not been saved yet.",
|
|
1126
|
+
"info",
|
|
1127
|
+
);
|
|
1128
|
+
continue;
|
|
1129
|
+
}
|
|
1130
|
+
if (
|
|
1131
|
+
field === "External editor (entire Markdown)" ||
|
|
1132
|
+
field === "Edit frontmatter YAML"
|
|
1133
|
+
) {
|
|
345
1134
|
const edited = await editDocument(
|
|
346
1135
|
ctx,
|
|
347
1136
|
draft,
|
|
@@ -351,32 +1140,82 @@ export async function editAgentTypes(
|
|
|
351
1140
|
continue;
|
|
352
1141
|
}
|
|
353
1142
|
const candidate = structuredClone(draft);
|
|
354
|
-
if (
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
1143
|
+
if (
|
|
1144
|
+
field === "name" ||
|
|
1145
|
+
field === "description" ||
|
|
1146
|
+
field === "systemPrompt"
|
|
1147
|
+
) {
|
|
1148
|
+
const originalValue = candidate[field] ?? "";
|
|
1149
|
+
// Editor prefill must not carry terminal controls. An unmodified or
|
|
1150
|
+
// SDK-trimmed submit keeps the original, including whitespace and CRLF.
|
|
1151
|
+
const prefill = originalValue
|
|
1152
|
+
.replace(/\r\n/g, "\n")
|
|
1153
|
+
.replace(/\r/g, "\n")
|
|
1154
|
+
.split("\n")
|
|
1155
|
+
.map(sanitizeText)
|
|
1156
|
+
.join("\n");
|
|
1157
|
+
const value = await dialogEditor(ctx, `Agent ${field}`, prefill);
|
|
1158
|
+
if (value === undefined) continue;
|
|
1159
|
+
candidate[field] =
|
|
1160
|
+
value === prefill || value === prefill.trim()
|
|
1161
|
+
? originalValue
|
|
1162
|
+
: value;
|
|
1163
|
+
} else if (field === "models") {
|
|
1164
|
+
const value = await editModelPreferences(
|
|
1165
|
+
ctx,
|
|
1166
|
+
getModelPreferences(candidate),
|
|
1167
|
+
);
|
|
1168
|
+
if (value === MODEL_EDITOR_CANCEL) continue;
|
|
1169
|
+
if (value.length === 0) {
|
|
1170
|
+
delete candidate.models;
|
|
1171
|
+
delete candidate.model;
|
|
1172
|
+
} else {
|
|
1173
|
+
candidate.models = [...value];
|
|
1174
|
+
delete candidate.model;
|
|
1175
|
+
}
|
|
1176
|
+
} else if (field === "modelSuggestions") {
|
|
1177
|
+
const prefill = modelSuggestionPrefill(candidate.modelSuggestions);
|
|
1178
|
+
const value = await dialogEditor(
|
|
1179
|
+
ctx,
|
|
1180
|
+
"Agent modelSuggestions (one display name per line)",
|
|
1181
|
+
prefill,
|
|
358
1182
|
);
|
|
359
1183
|
if (value === undefined) continue;
|
|
360
|
-
if (
|
|
361
|
-
|
|
1184
|
+
if (value === prefill || value === prefill.trim()) continue;
|
|
1185
|
+
const names = value
|
|
1186
|
+
.split("\n")
|
|
1187
|
+
.map((line) => line.trim())
|
|
1188
|
+
.filter((line) => line.length > 0);
|
|
1189
|
+
if (names.length === 0) delete candidate.modelSuggestions;
|
|
1190
|
+
else candidate.modelSuggestions = [...names];
|
|
362
1191
|
} else if (field === "thinkingLevel") {
|
|
363
|
-
const value = await
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
1192
|
+
const value = await dialogMenu(
|
|
1193
|
+
ctx,
|
|
1194
|
+
"Thinking level",
|
|
1195
|
+
["Default (inherit)", ...THINKING_LEVELS].map((label) => ({
|
|
1196
|
+
id: label,
|
|
1197
|
+
label,
|
|
1198
|
+
})),
|
|
1199
|
+
{ selectedId: candidate.thinkingLevel ?? "Default (inherit)" },
|
|
1200
|
+
);
|
|
367
1201
|
if (!value) continue;
|
|
368
1202
|
if (value === "Default (inherit)") delete candidate.thinkingLevel;
|
|
369
1203
|
else candidate.thinkingLevel = value as AgentType["thinkingLevel"];
|
|
370
1204
|
} else if (field === "color") {
|
|
371
|
-
const value = await
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
if (value ===
|
|
1205
|
+
const value = await selectAgentColor(
|
|
1206
|
+
ctx,
|
|
1207
|
+
candidate.color,
|
|
1208
|
+
candidate.name,
|
|
1209
|
+
);
|
|
1210
|
+
if (value === AGENT_COLOR_CANCEL) continue;
|
|
1211
|
+
if (value === undefined) delete candidate.color;
|
|
377
1212
|
else candidate.color = value;
|
|
378
1213
|
} else if (field === "tools.allow" || field === "tools.block") {
|
|
379
|
-
await editToolList(
|
|
1214
|
+
await editToolList(
|
|
1215
|
+
ctx,
|
|
1216
|
+
candidate,
|
|
1217
|
+
field === "tools.allow" ? "allow" : "block",
|
|
1218
|
+
);
|
|
380
1219
|
}
|
|
381
1220
|
parseAgentType(serializeAgentType(candidate));
|
|
382
1221
|
draft = candidate;
|