pi-subagent-manager 0.1.0 → 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/README.md +51 -8
- package/agents/architect.md +7 -0
- package/package.json +1 -1
- package/src/config.ts +157 -42
- package/src/index.ts +22 -6
- package/src/manager.ts +27 -3
- package/src/model-picker.ts +509 -0
- package/src/models.ts +54 -0
- package/src/runtime.ts +264 -57
- package/src/settings.ts +124 -0
- package/src/tools.ts +4 -3
- package/src/types.ts +8 -0
- package/src/ui.ts +221 -40
package/src/ui.ts
CHANGED
|
@@ -5,13 +5,25 @@ import { tmpdir } from "node:os";
|
|
|
5
5
|
import { join, resolve } from "node:path";
|
|
6
6
|
import { parse as parseShell } from "shell-quote";
|
|
7
7
|
import {
|
|
8
|
+
DynamicBorder,
|
|
8
9
|
type ExtensionCommandContext,
|
|
9
10
|
type ExtensionContext,
|
|
11
|
+
getSelectListTheme,
|
|
10
12
|
type Theme,
|
|
11
13
|
type ThemeColor,
|
|
12
14
|
} from "@earendil-works/pi-coding-agent";
|
|
13
|
-
import {
|
|
15
|
+
import {
|
|
16
|
+
Container,
|
|
17
|
+
SelectList,
|
|
18
|
+
Text,
|
|
19
|
+
colorToRgb,
|
|
20
|
+
rgbColor,
|
|
21
|
+
stripTerminalSequences,
|
|
22
|
+
truncateToWidth,
|
|
23
|
+
} from "@earendil-works/pi-tui";
|
|
14
24
|
import { AGENT_COLORS, ConfigStore, parseAgentType, serializeAgentType } from "./config.ts";
|
|
25
|
+
import { editModelPreferences, MODEL_EDITOR_CANCEL } from "./model-picker.ts";
|
|
26
|
+
import { getModelPreferences } from "./models.ts";
|
|
15
27
|
import { THINKING_LEVELS, type AgentType, type ThreadService, type ThreadView } from "./types.ts";
|
|
16
28
|
|
|
17
29
|
export type ThreadController = Pick<
|
|
@@ -24,10 +36,33 @@ export function sanitizeText(text: string): string {
|
|
|
24
36
|
return stripTerminalSequences(text).replace(/[\x00-\x1f\x7f-\x9f]/g, " ");
|
|
25
37
|
}
|
|
26
38
|
|
|
39
|
+
type AgentBadgeTheme = Pick<Theme, "fg" | "colors" | "style">;
|
|
40
|
+
|
|
41
|
+
/** Use the theme's concrete token color as a background, with readable text. */
|
|
42
|
+
function agentNameBadge(name: string, color: string | undefined, theme: AgentBadgeTheme): string {
|
|
43
|
+
const token: ThemeColor = AGENT_COLORS.includes(color as (typeof AGENT_COLORS)[number])
|
|
44
|
+
? (color as ThemeColor)
|
|
45
|
+
: "accent";
|
|
46
|
+
const background = theme.colors[token];
|
|
47
|
+
const { r, g, b } = colorToRgb(background);
|
|
48
|
+
const linear = (channel: number) => {
|
|
49
|
+
const value = channel / 255;
|
|
50
|
+
return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4;
|
|
51
|
+
};
|
|
52
|
+
const luminance = 0.2126 * linear(r) + 0.7152 * linear(g) + 0.0722 * linear(b);
|
|
53
|
+
// Pick whichever of black/white has the higher WCAG contrast ratio.
|
|
54
|
+
const foreground = luminance > Math.sqrt(0.0525) - 0.05 ? 0 : 255;
|
|
55
|
+
return theme.style(` ${sanitizeText(name)} `, {
|
|
56
|
+
bg: background,
|
|
57
|
+
fg: rgbColor(foreground, foreground, foreground),
|
|
58
|
+
bold: true,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
27
62
|
export function renderThreads(
|
|
28
63
|
threads: ThreadView[],
|
|
29
64
|
width: number,
|
|
30
|
-
theme:
|
|
65
|
+
theme: AgentBadgeTheme,
|
|
31
66
|
): string[] {
|
|
32
67
|
const priority = (thread: ThreadView) =>
|
|
33
68
|
thread.state === "starting" || thread.state === "running"
|
|
@@ -42,16 +77,12 @@ export function renderThreads(
|
|
|
42
77
|
);
|
|
43
78
|
const limit = visible.length > 8 ? 7 : 8;
|
|
44
79
|
const lines = visible.slice(0, limit).map((thread) => {
|
|
45
|
-
const
|
|
46
|
-
?
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
: thread.state === "paused"
|
|
50
|
-
? "warning"
|
|
51
|
-
: "accent";
|
|
52
|
-
const label = `${sanitizeText(thread.path)} [${sanitizeText(thread.state)}]`;
|
|
80
|
+
const stateColor =
|
|
81
|
+
thread.state === "failed" ? "error" : thread.state === "paused" ? "warning" : "accent";
|
|
82
|
+
const badge = agentNameBadge(thread.path, thread.color, theme);
|
|
83
|
+
const state = theme.fg(stateColor, `[${sanitizeText(thread.state)}]`);
|
|
53
84
|
return truncateToWidth(
|
|
54
|
-
`${
|
|
85
|
+
`${badge} ${state} ${sanitizeText(thread.status || thread.task)}`,
|
|
55
86
|
Math.max(0, width),
|
|
56
87
|
);
|
|
57
88
|
});
|
|
@@ -208,7 +239,7 @@ async function editToolList(
|
|
|
208
239
|
field: "allow" | "block",
|
|
209
240
|
): Promise<void> {
|
|
210
241
|
const mode = await ctx.ui.select(
|
|
211
|
-
`tools.${field} (${type.tools?.[field]
|
|
242
|
+
`tools.${field} (${sanitizeText(toolListMenuValue(type.tools?.[field]))})`,
|
|
212
243
|
["Unset (use default policy)", "Empty list", "Enter exact tool names"],
|
|
213
244
|
);
|
|
214
245
|
if (!mode) return;
|
|
@@ -218,9 +249,9 @@ async function editToolList(
|
|
|
218
249
|
const value =
|
|
219
250
|
mode === "Empty list"
|
|
220
251
|
? ""
|
|
221
|
-
: await ctx.ui.
|
|
252
|
+
: await ctx.ui.editor(
|
|
222
253
|
`tools.${field}: comma-separated exact names`,
|
|
223
|
-
type.tools?.[field]?.join(", "),
|
|
254
|
+
type.tools?.[field]?.join(", ") ?? "",
|
|
224
255
|
);
|
|
225
256
|
if (value === undefined) return;
|
|
226
257
|
type.tools ??= {};
|
|
@@ -232,6 +263,107 @@ async function editToolList(
|
|
|
232
263
|
if (type.tools && !Object.keys(type.tools).length) delete type.tools;
|
|
233
264
|
}
|
|
234
265
|
|
|
266
|
+
const AGENT_COLOR_DEFAULT = "__default__";
|
|
267
|
+
const AGENT_COLOR_CANCEL = Symbol("agent-color-cancel");
|
|
268
|
+
|
|
269
|
+
class AgentColorPickerComponent extends Container {
|
|
270
|
+
private readonly items: { value: string; label: string; description?: string }[];
|
|
271
|
+
private readonly preview: Text;
|
|
272
|
+
private readonly theme: Theme;
|
|
273
|
+
readonly selectList: SelectList;
|
|
274
|
+
|
|
275
|
+
constructor(
|
|
276
|
+
tui: { requestRender(force?: boolean): void },
|
|
277
|
+
theme: Theme,
|
|
278
|
+
currentColor: AgentType["color"] | undefined,
|
|
279
|
+
private readonly agentName: string,
|
|
280
|
+
onSelect: (color: AgentType["color"] | undefined) => void,
|
|
281
|
+
onCancel: () => void,
|
|
282
|
+
) {
|
|
283
|
+
super();
|
|
284
|
+
this.theme = theme;
|
|
285
|
+
this.items = [
|
|
286
|
+
{
|
|
287
|
+
value: AGENT_COLOR_DEFAULT,
|
|
288
|
+
label: "Default (inherit)",
|
|
289
|
+
description: "Use the default accent background for the agent name",
|
|
290
|
+
},
|
|
291
|
+
...AGENT_COLORS.map((color) => ({
|
|
292
|
+
value: color,
|
|
293
|
+
label: agentNameBadge(color, color, theme),
|
|
294
|
+
description: `Agent name background: ${color}`,
|
|
295
|
+
})),
|
|
296
|
+
];
|
|
297
|
+
this.preview = new Text();
|
|
298
|
+
this.selectList = new SelectList(this.items, 10, getSelectListTheme(), {
|
|
299
|
+
minPrimaryColumnWidth: 18,
|
|
300
|
+
maxPrimaryColumnWidth: 28,
|
|
301
|
+
});
|
|
302
|
+
const selectedIndex = this.items.findIndex(
|
|
303
|
+
(item) => item.value === (currentColor ?? AGENT_COLOR_DEFAULT),
|
|
304
|
+
);
|
|
305
|
+
this.selectList.setSelectedIndex(selectedIndex >= 0 ? selectedIndex : 0);
|
|
306
|
+
this.selectList.onSelectionChange = (item) => {
|
|
307
|
+
this.updatePreview(tui, item.value);
|
|
308
|
+
};
|
|
309
|
+
this.selectList.onSelect = (item) => {
|
|
310
|
+
onSelect(item.value === AGENT_COLOR_DEFAULT ? undefined : (item.value as AgentType["color"]));
|
|
311
|
+
};
|
|
312
|
+
this.selectList.onCancel = onCancel;
|
|
313
|
+
this.addChild(new DynamicBorder((text) => theme.fg("muted", text)));
|
|
314
|
+
this.updatePreview(tui, this.items[selectedIndex >= 0 ? selectedIndex : 0]!.value);
|
|
315
|
+
this.addChild(this.preview);
|
|
316
|
+
this.addChild(this.selectList);
|
|
317
|
+
this.addChild(new DynamicBorder((text) => theme.fg("muted", text)));
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
getItems(): readonly { value: string; label: string; description?: string }[] {
|
|
321
|
+
return this.items;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
getPreview(): Text {
|
|
325
|
+
return this.preview;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
getSelectList(): SelectList {
|
|
329
|
+
return this.selectList;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
handleInput(keyData: string): void {
|
|
333
|
+
this.selectList.handleInput(keyData);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
private updatePreview(tui: { requestRender(force?: boolean): void }, value: string): void {
|
|
337
|
+
const badge = agentNameBadge(
|
|
338
|
+
`/${this.agentName}`,
|
|
339
|
+
value === AGENT_COLOR_DEFAULT ? undefined : value,
|
|
340
|
+
this.theme,
|
|
341
|
+
);
|
|
342
|
+
this.preview.setText(`Preview: ${badge} ${this.theme.fg("accent", "[running]")} Working`);
|
|
343
|
+
tui.requestRender();
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
async function selectAgentColor(
|
|
348
|
+
ctx: ExtensionCommandContext,
|
|
349
|
+
currentColor: AgentType["color"] | undefined,
|
|
350
|
+
agentName: string,
|
|
351
|
+
): Promise<AgentType["color"] | undefined | typeof AGENT_COLOR_CANCEL> {
|
|
352
|
+
return ctx.ui.custom(
|
|
353
|
+
(tui, theme, _keys, done) => {
|
|
354
|
+
return new AgentColorPickerComponent(
|
|
355
|
+
tui,
|
|
356
|
+
theme,
|
|
357
|
+
currentColor,
|
|
358
|
+
agentName,
|
|
359
|
+
(value) => done(value),
|
|
360
|
+
() => done(AGENT_COLOR_CANCEL),
|
|
361
|
+
);
|
|
362
|
+
},
|
|
363
|
+
{ overlay: true },
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
|
|
235
367
|
async function editDocument(
|
|
236
368
|
ctx: ExtensionCommandContext,
|
|
237
369
|
type: AgentType,
|
|
@@ -294,6 +426,52 @@ function assertSaveDestination(
|
|
|
294
426
|
}
|
|
295
427
|
}
|
|
296
428
|
|
|
429
|
+
const EDIT_MENU_ACTIONS = [
|
|
430
|
+
"name",
|
|
431
|
+
"description",
|
|
432
|
+
"models",
|
|
433
|
+
"thinkingLevel",
|
|
434
|
+
"tools.allow",
|
|
435
|
+
"tools.block",
|
|
436
|
+
"color",
|
|
437
|
+
"Edit frontmatter YAML",
|
|
438
|
+
"External editor (entire Markdown)",
|
|
439
|
+
"Save",
|
|
440
|
+
"Cancel",
|
|
441
|
+
] as const;
|
|
442
|
+
|
|
443
|
+
type EditMenuAction = (typeof EDIT_MENU_ACTIONS)[number];
|
|
444
|
+
|
|
445
|
+
/** Omitted tool lists use the default policy; [] is an explicit empty list, not inheritance. */
|
|
446
|
+
function toolListMenuValue(names: string[] | undefined): string {
|
|
447
|
+
if (names === undefined) return "Unset (use default policy)";
|
|
448
|
+
if (names.length === 0) return "Empty list";
|
|
449
|
+
return names.join(", ");
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
function editMenuLabel(action: EditMenuAction, draft: AgentType): string {
|
|
453
|
+
switch (action) {
|
|
454
|
+
case "name":
|
|
455
|
+
return `name: ${sanitizeText(draft.name)}`;
|
|
456
|
+
case "description":
|
|
457
|
+
return `description: ${sanitizeText(draft.description)}`;
|
|
458
|
+
case "models": {
|
|
459
|
+
const models = getModelPreferences(draft);
|
|
460
|
+
return `models: ${sanitizeText(models?.join(", ") ?? "Default (inherit)")}`;
|
|
461
|
+
}
|
|
462
|
+
case "thinkingLevel":
|
|
463
|
+
return `thinkingLevel: ${sanitizeText(draft.thinkingLevel ?? "Default (inherit)")}`;
|
|
464
|
+
case "tools.allow":
|
|
465
|
+
return `tools.allow: ${sanitizeText(toolListMenuValue(draft.tools?.allow))}`;
|
|
466
|
+
case "tools.block":
|
|
467
|
+
return `tools.block: ${sanitizeText(toolListMenuValue(draft.tools?.block))}`;
|
|
468
|
+
case "color":
|
|
469
|
+
return `color: ${sanitizeText(draft.color ?? "Default (inherit)")}`;
|
|
470
|
+
default:
|
|
471
|
+
return action;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
297
475
|
export async function editAgentTypes(
|
|
298
476
|
ctx: ExtensionCommandContext,
|
|
299
477
|
store: ConfigStore,
|
|
@@ -309,19 +487,10 @@ export async function editAgentTypes(
|
|
|
309
487
|
? structuredClone(original)
|
|
310
488
|
: { name: "new-agent", description: "New agent", systemPrompt: "" };
|
|
311
489
|
while (true) {
|
|
312
|
-
const
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
"thinkingLevel",
|
|
317
|
-
"tools.allow",
|
|
318
|
-
"tools.block",
|
|
319
|
-
"color",
|
|
320
|
-
"Edit frontmatter YAML",
|
|
321
|
-
"External editor (entire Markdown)",
|
|
322
|
-
"Save",
|
|
323
|
-
"Cancel",
|
|
324
|
-
]);
|
|
490
|
+
const labels = EDIT_MENU_ACTIONS.map((action) => editMenuLabel(action, draft));
|
|
491
|
+
const selected = await ctx.ui.select(`Edit ${sanitizeText(draft.name)} (unsaved)`, labels);
|
|
492
|
+
if (selected === undefined) break;
|
|
493
|
+
const field = EDIT_MENU_ACTIONS[labels.indexOf(selected)];
|
|
325
494
|
if (!field || field === "Cancel") break;
|
|
326
495
|
try {
|
|
327
496
|
if (field === "Save") {
|
|
@@ -351,14 +520,29 @@ export async function editAgentTypes(
|
|
|
351
520
|
continue;
|
|
352
521
|
}
|
|
353
522
|
const candidate = structuredClone(draft);
|
|
354
|
-
if (field === "name" || field === "description"
|
|
355
|
-
const
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
523
|
+
if (field === "name" || field === "description") {
|
|
524
|
+
const originalValue = candidate[field] ?? "";
|
|
525
|
+
// Editor prefill must not carry terminal controls. An unmodified or
|
|
526
|
+
// SDK-trimmed submit keeps the original, including whitespace and CRLF.
|
|
527
|
+
const prefill = originalValue
|
|
528
|
+
.replace(/\r\n/g, "\n")
|
|
529
|
+
.replace(/\r/g, "\n")
|
|
530
|
+
.split("\n")
|
|
531
|
+
.map(sanitizeText)
|
|
532
|
+
.join("\n");
|
|
533
|
+
const value = await ctx.ui.editor(`Agent ${field}`, prefill);
|
|
359
534
|
if (value === undefined) continue;
|
|
360
|
-
|
|
361
|
-
|
|
535
|
+
candidate[field] = value === prefill || value === prefill.trim() ? originalValue : value;
|
|
536
|
+
} else if (field === "models") {
|
|
537
|
+
const value = await editModelPreferences(ctx, getModelPreferences(candidate));
|
|
538
|
+
if (value === MODEL_EDITOR_CANCEL) continue;
|
|
539
|
+
if (value.length === 0) {
|
|
540
|
+
delete candidate.models;
|
|
541
|
+
delete candidate.model;
|
|
542
|
+
} else {
|
|
543
|
+
candidate.models = [...value];
|
|
544
|
+
delete candidate.model;
|
|
545
|
+
}
|
|
362
546
|
} else if (field === "thinkingLevel") {
|
|
363
547
|
const value = await ctx.ui.select("Thinking level", [
|
|
364
548
|
"Default (inherit)",
|
|
@@ -368,12 +552,9 @@ export async function editAgentTypes(
|
|
|
368
552
|
if (value === "Default (inherit)") delete candidate.thinkingLevel;
|
|
369
553
|
else candidate.thinkingLevel = value as AgentType["thinkingLevel"];
|
|
370
554
|
} else if (field === "color") {
|
|
371
|
-
const value = await ctx.
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
]);
|
|
375
|
-
if (!value) continue;
|
|
376
|
-
if (value === "Default") delete candidate.color;
|
|
555
|
+
const value = await selectAgentColor(ctx, candidate.color, candidate.name);
|
|
556
|
+
if (value === AGENT_COLOR_CANCEL) continue;
|
|
557
|
+
if (value === undefined) delete candidate.color;
|
|
377
558
|
else candidate.color = value;
|
|
378
559
|
} else if (field === "tools.allow" || field === "tools.block") {
|
|
379
560
|
await editToolList(ctx, candidate, field === "tools.allow" ? "allow" : "block");
|