pi-subagent-manager 0.12.0 → 0.13.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/src/ui/ui.ts CHANGED
@@ -24,6 +24,7 @@ import {
24
24
  import {
25
25
  AGENT_COLORS,
26
26
  ConfigStore,
27
+ diffAgentSettings,
27
28
  parseAgentType,
28
29
  serializeAgentType,
29
30
  } from "../prefs/config.ts";
@@ -96,6 +97,29 @@ export function agentTypeLabel(type: string, icon?: string, nerdFontIcons = fals
96
97
  return `${prefix}${sanitizeText(type)}`;
97
98
  }
98
99
 
100
+ /** Nerd Fonts md-circle-slice-1..8: an equal-width, shape-changing progress cycle.
101
+ * Verified against https://raw.githubusercontent.com/ryanoasis/nerd-fonts/master/glyphnames.json.
102
+ */
103
+ export const AGENT_PROGRESS_INTERVAL = 180;
104
+ const AGENT_PROGRESS_FRAMES = Array.from({ length: 8 }, (_, index) =>
105
+ String.fromCodePoint(0xf0a9e + index),
106
+ );
107
+
108
+ /** Keep the configured role icon for settled threads and non-animated/RPC output. */
109
+ export function agentProgressIcon(
110
+ thread: Pick<ThreadView, "state" | "icon">,
111
+ nerdFontIcons: boolean,
112
+ now = Date.now(),
113
+ animate = true,
114
+ ): string | undefined {
115
+ if (!nerdFontIcons) return undefined;
116
+ if (animate && (thread.state === "starting" || thread.state === "running")) {
117
+ const frame = Math.floor(Math.max(0, now) / AGENT_PROGRESS_INTERVAL) % AGENT_PROGRESS_FRAMES.length;
118
+ return AGENT_PROGRESS_FRAMES[frame];
119
+ }
120
+ return thread.icon;
121
+ }
122
+
99
123
  export function agentTypeBadge(
100
124
  type: string,
101
125
  color: string | undefined,
@@ -203,7 +227,9 @@ export function renderThreads(
203
227
  : thread.state === "paused"
204
228
  ? "warning"
205
229
  : "accent";
206
- const badge = agentTypeBadge(thread.type, thread.color, theme, thread.icon, nerdFontIcons);
230
+ const badge = agentTypeBadge(
231
+ thread.type, thread.color, theme, agentProgressIcon(thread, nerdFontIcons), nerdFontIcons,
232
+ );
207
233
  const path = agentPath(thread.path, thread.color, theme);
208
234
  const state = theme.fg(stateColor, `[${sanitizeText(thread.state)}]`);
209
235
  const left = `${badge} ${path} ${state} ${sanitizeText(thread.status || thread.task)}`;
@@ -236,6 +262,8 @@ interface AgentWidgetRenderOptions {
236
262
  /** RPC widgets cannot open a terminal browser. */
237
263
  showBrowserHint?: boolean;
238
264
  nerdFontIcons?: boolean;
265
+ /** RPC snapshots use static role icons and never start an animation. */
266
+ animate?: boolean;
239
267
  }
240
268
 
241
269
  function agentWidgetStatus(
@@ -247,7 +275,8 @@ function agentWidgetStatus(
247
275
  ): string {
248
276
  const details = [
249
277
  omitted > 0 ? `+${omitted} more agents` : "",
250
- showBrowserHint ? AGENT_BROWSER_HINT : "",
278
+ showBrowserHint && running > 0 ? AGENT_BROWSER_HINT : "",
279
+ showBrowserHint ? "→ collapse" : "",
251
280
  ].filter(Boolean);
252
281
  return truncateToWidth(
253
282
  theme.fg("accent", `${running} running`) +
@@ -333,12 +362,6 @@ function branchPriorityComparator(
333
362
  (a < b ? -1 : a > b ? 1 : 0);
334
363
  }
335
364
 
336
- function activityIndent(prefix: string): string {
337
- if (prefix.endsWith("├─ ")) return prefix.slice(0, -3) + "│ ";
338
- if (prefix.endsWith("└─ ")) return prefix.slice(0, -3) + " ";
339
- return " ".repeat(prefix.length + 3);
340
- }
341
-
342
365
  function agentStateColor(
343
366
  state: ThreadView["state"],
344
367
  ): "error" | "warning" | "accent" {
@@ -354,23 +377,15 @@ function agentLine(
354
377
  width: number,
355
378
  theme: AgentBadgeTheme,
356
379
  nerdFontIcons: boolean,
380
+ animate: boolean,
357
381
  ): string {
358
382
  const thread = row.thread!;
359
- const left = `${row.prefix}${agentTypeBadge(thread.type, thread.color, theme, thread.icon, nerdFontIcons)} ${agentPath(thread.path, thread.color, theme)} ${theme.fg(agentStateColor(thread.state), `[${sanitizeText(thread.state)}]`)} ${sanitizeText(thread.task)}`;
360
- return fitLine(left, theme.fg("muted", threadMetrics(thread)), width);
361
- }
362
-
363
- function activityLine(
364
- row: StatusRow,
365
- width: number,
366
- theme: AgentBadgeTheme,
367
- ): string {
368
- const thread = row.thread!;
369
- return truncateToWidth(
370
- `${activityIndent(row.prefix)}${theme.fg("muted", sanitizeText(thread.status || thread.task))}`,
371
- width,
372
- "",
383
+ const badge = agentTypeBadge(
384
+ thread.type, thread.color, theme,
385
+ agentProgressIcon(thread, nerdFontIcons, Date.now(), animate), nerdFontIcons,
373
386
  );
387
+ const left = `${row.prefix}${badge} ${agentPath(thread.path, thread.color, theme)} ${theme.fg(agentStateColor(thread.state), `[${sanitizeText(thread.state)}]`)} ${sanitizeText(thread.task)}`;
388
+ return fitLine(left, theme.fg("muted", threadMetrics(thread)), width);
374
389
  }
375
390
 
376
391
  function placeholderLine(
@@ -398,7 +413,7 @@ function takeWidgetRows(rows: StatusRow[], budget: number): StatusRow[] {
398
413
  stack.push(index);
399
414
  }
400
415
  const rowCost = (row: StatusRow) =>
401
- row.path === WIDGET_ROOT ? 0 : row.thread ? 2 : 1;
416
+ row.path === WIDGET_ROOT ? 0 : 1;
402
417
  const candidates = rows
403
418
  .map((row, index) => ({ row, index }))
404
419
  .filter(({ row }) => row.thread && row.path !== WIDGET_ROOT)
@@ -441,7 +456,7 @@ export function renderAgentTree(
441
456
  threads: ThreadView[],
442
457
  width: number,
443
458
  theme: AgentBadgeTheme,
444
- { showBrowserHint = true, nerdFontIcons = false }: AgentWidgetRenderOptions = {},
459
+ { showBrowserHint = true, nerdFontIcons = false, animate = true }: AgentWidgetRenderOptions = {},
445
460
  ): string[] {
446
461
  const columns = Math.max(0, width);
447
462
  const rows = buildStatusTree(
@@ -470,10 +485,9 @@ export function renderAgentTree(
470
485
  lines.push(placeholderLine(row, columns, theme));
471
486
  continue;
472
487
  }
473
- lines.push(agentLine(row, columns, theme, nerdFontIcons));
474
- lines.push(activityLine(row, columns, theme));
488
+ lines.push(agentLine(row, columns, theme, nerdFontIcons, animate));
475
489
  }
476
- lines.push(agentWidgetStatus(live, omitted, columns, theme, showBrowserHint && live > 0));
490
+ lines.push(agentWidgetStatus(live, omitted, columns, theme, showBrowserHint));
477
491
  return lines;
478
492
  }
479
493
 
@@ -482,7 +496,7 @@ export function renderAgentSummary(
482
496
  threads: ThreadView[],
483
497
  width: number,
484
498
  theme: Pick<Theme, "fg">,
485
- { showBrowserHint = true }: AgentWidgetRenderOptions = {},
499
+ { showBrowserHint = true, nerdFontIcons = false, animate = true }: AgentWidgetRenderOptions = {},
486
500
  ): string[] {
487
501
  const agents = new Map<string, ThreadView>();
488
502
  for (const thread of threads) {
@@ -514,16 +528,17 @@ export function renderAgentSummary(
514
528
  ["paused", "warning"],
515
529
  ["completed", "success"],
516
530
  ];
531
+ const progress = counts.running > 0
532
+ ? agentProgressIcon({ state: "running" }, nerdFontIcons, Date.now(), animate)
533
+ : undefined;
517
534
  const left = statuses
518
535
  .filter(([state]) => state === "running" || counts[state] > 0)
519
- .map(([state, color]) => theme.fg(color, `${counts[state]} ${state}`))
520
- .join(theme.fg("muted", ", "));
536
+ .map(([state, color]) => theme.fg(color,
537
+ `${state === "running" && progress ? `${progress} ` : ""}${counts[state]} ${state}`))
538
+ .join(theme.fg("muted", ", ")) +
539
+ (showBrowserHint && counts.running > 0 ? theme.fg("muted", " · ← browser") : "");
521
540
  const right = theme.fg("muted", `↑${formatCount(input)} ↓${formatCount(output)}`);
522
- const columns = Math.max(0, width);
523
- const lines = [fitLine(left, right, columns)];
524
- if (showBrowserHint && counts.running > 0)
525
- lines.push(truncateToWidth(theme.fg("muted", AGENT_BROWSER_HINT), columns, ""));
526
- return lines;
541
+ return [fitLine(left, right, Math.max(0, width))];
527
542
  }
528
543
 
529
544
  export function updateWidget(
@@ -545,7 +560,7 @@ export function updateWidget(
545
560
  if (ctx.mode === "rpc") {
546
561
  ctx.ui.setWidget(
547
562
  "pi-subagent",
548
- render(threads, 80, ctx.ui.theme, { showBrowserHint: false, nerdFontIcons }),
563
+ render(threads, 80, ctx.ui.theme, { showBrowserHint: false, nerdFontIcons, animate: false }),
549
564
  AGENT_WIDGET_PLACEMENT,
550
565
  );
551
566
  return;
@@ -555,8 +570,8 @@ export function updateWidget(
555
570
  "pi-subagent",
556
571
  (tui) => {
557
572
  let timer: ReturnType<typeof setInterval> | undefined;
558
- if (mode === "full" && snapshot.some(isLive)) {
559
- timer = setInterval(() => tui.requestRender(), 1000);
573
+ if (snapshot.some(isLive) && (mode === "full" || nerdFontIcons)) {
574
+ timer = setInterval(() => tui.requestRender(), nerdFontIcons ? AGENT_PROGRESS_INTERVAL : 1000);
560
575
  timer.unref();
561
576
  }
562
577
  return {
@@ -972,7 +987,13 @@ function assertSaveDestination(
972
987
  type: AgentType,
973
988
  original: AgentType | undefined,
974
989
  scope: "user" | "project",
990
+ kind: AgentEditMode = "fork",
975
991
  ): void {
992
+ if (kind === "override" && original && type.name !== original.name) {
993
+ throw new Error(
994
+ "Settings overrides cannot rename the agent; fork it instead.",
995
+ );
996
+ }
976
997
  const entries = store.list();
977
998
  const sameName = entries.find((entry) => entry.name === type.name);
978
999
  if (sameName && (!original || type.name !== original.name)) {
@@ -980,7 +1001,7 @@ function assertSaveDestination(
980
1001
  `Agent type ${type.name} already exists; choose a different name.`,
981
1002
  );
982
1003
  }
983
- const destination = store.destination(type.name, scope, original);
1004
+ const destination = store.destination(type.name, scope, original, kind);
984
1005
  if (
985
1006
  existsSync(destination) &&
986
1007
  (!original?.filePath || resolve(original.filePath) !== destination)
@@ -989,9 +1010,23 @@ function assertSaveDestination(
989
1010
  `Refusing to overwrite existing definition ${destination}. Edit that definition instead.`,
990
1011
  );
991
1012
  }
1013
+ // A scope cannot hold both a fork and an override for one name (fail-closed).
1014
+ const otherKind: AgentEditMode = kind === "fork" ? "override" : "fork";
1015
+ const otherDestination = store.destination(type.name, scope, undefined, otherKind);
1016
+ if (
1017
+ otherDestination !== destination &&
1018
+ existsSync(otherDestination) &&
1019
+ (!original?.filePath || resolve(original.filePath) !== otherDestination)
1020
+ ) {
1021
+ const kept = kind === "fork" ? ".md (full fork)" : ".yml (settings override)";
1022
+ throw new Error(
1023
+ `Agent type ${type.name} already has a ${otherKind === "fork" ? ".md fork" : ".yml override"} in this scope (${otherDestination}). Remove it before saving a ${kept}.`,
1024
+ );
1025
+ }
992
1026
  }
993
1027
 
994
1028
  const EDIT_MENU_ACTIONS = [
1029
+ "Customization",
995
1030
  "name",
996
1031
  "description",
997
1032
  "models",
@@ -1010,9 +1045,12 @@ const EDIT_MENU_ACTIONS = [
1010
1045
  "Cancel",
1011
1046
  ] as const;
1012
1047
 
1048
+ type AgentEditMode = "fork" | "override";
1049
+
1013
1050
  type EditMenuAction = (typeof EDIT_MENU_ACTIONS)[number];
1014
1051
 
1015
1052
  const EDIT_FIELD_LABELS: Record<EditMenuAction, string> = {
1053
+ Customization: "Customization",
1016
1054
  name: "Name",
1017
1055
  description: "Description",
1018
1056
  models: "Models",
@@ -1032,6 +1070,8 @@ const EDIT_FIELD_LABELS: Record<EditMenuAction, string> = {
1032
1070
  };
1033
1071
 
1034
1072
  const EDIT_FIELD_HELP: Record<EditMenuAction, string> = {
1073
+ Customization:
1074
+ "Settings override tweaks bundled settings via <name>.yml and keeps receiving prompt updates. Fork copies everything into <name>.md and you own the prompt.",
1035
1075
  name: "Unique type identifier. Renaming retains the original file.",
1036
1076
  description: "Description shown when browsing and spawning agents.",
1037
1077
  models:
@@ -1090,6 +1130,48 @@ function modelSuggestionPrefill(suggestions: string[] | undefined): string {
1090
1130
  return (suggestions ?? []).map((name) => sanitizeText(name)).join("\n");
1091
1131
  }
1092
1132
 
1133
+ function customizationMenuValue(
1134
+ editMode: AgentEditMode | undefined,
1135
+ isNew: boolean,
1136
+ ): string {
1137
+ if (isNew) return "New full definition (.md)";
1138
+ if (editMode === "override") return "Settings override (.yml)";
1139
+ if (editMode === "fork") return "Full copy (.md)";
1140
+ return "Choose to unlock editing";
1141
+ }
1142
+
1143
+ /** Bundled definitions are read-only until the user picks fork or override. */
1144
+ function isPureBundled(original: AgentType | undefined): boolean {
1145
+ return !!original && !original.customization && original.source === "bundled";
1146
+ }
1147
+
1148
+ async function chooseCustomizationMode(
1149
+ ctx: ExtensionCommandContext,
1150
+ agentName: string,
1151
+ current?: AgentEditMode,
1152
+ ): Promise<AgentEditMode | undefined> {
1153
+ const value = await dialogMenu(
1154
+ ctx,
1155
+ `Customize ${sanitizeText(agentName)}`,
1156
+ [
1157
+ {
1158
+ id: "override",
1159
+ label: "Tweak settings",
1160
+ value: `${agentName}.yml`,
1161
+ help: "Override only settings (models, tools, thinking, color). The prompt stays bundled and keeps receiving updates. System prompt and name stay locked.",
1162
+ },
1163
+ {
1164
+ id: "fork",
1165
+ label: "Fork agent",
1166
+ value: `${agentName}.md`,
1167
+ help: "Copy the full definition including the system prompt. You own the prompt; bundled updates no longer apply. Everything is editable.",
1168
+ },
1169
+ ],
1170
+ { selectedId: current },
1171
+ );
1172
+ return value === "override" || value === "fork" ? value : undefined;
1173
+ }
1174
+
1093
1175
  function editMenuLabel(action: EditMenuAction, draft: AgentType): string {
1094
1176
  switch (action) {
1095
1177
  case "name":
@@ -1153,6 +1235,7 @@ async function editAgentTypesDialog(
1153
1235
  if (selection === undefined) return;
1154
1236
  const original =
1155
1237
  selection === "Create new type" ? undefined : store.get(selection);
1238
+ const isNew = !original;
1156
1239
  let draft: AgentType = original
1157
1240
  ? structuredClone(original)
1158
1241
  : { name: "new-agent", description: "New agent", systemPrompt: "" };
@@ -1162,8 +1245,24 @@ async function editAgentTypesDialog(
1162
1245
  original?.source === "project" && store.canSaveProject()
1163
1246
  ? "project"
1164
1247
  : "user";
1248
+ // Bundled definitions stay read-only until the user picks how to own them.
1249
+ let editMode: AgentEditMode | undefined = isNew
1250
+ ? "fork"
1251
+ : (original?.customization?.kind ?? undefined);
1252
+ if (!isNew && isPureBundled(original)) {
1253
+ const mode = await chooseCustomizationMode(ctx, original.name);
1254
+ if (!mode) continue;
1255
+ editMode = mode;
1256
+ }
1165
1257
  while (true) {
1166
- const dirty = !original || serializeAgentType(draft) !== originalContent;
1258
+ const baseForDiff =
1259
+ editMode === "override" && original
1260
+ ? (store.getBase(original.name) ?? original)
1261
+ : undefined;
1262
+ const dirty =
1263
+ editMode === "override" && baseForDiff
1264
+ ? Object.keys(diffAgentSettings(baseForDiff, draft)).length > 1
1265
+ : !original || serializeAgentType(draft) !== originalContent;
1167
1266
  const field = await dialogMenu(
1168
1267
  ctx,
1169
1268
  `Edit ${sanitizeText(draft.name)} (unsaved)`,
@@ -1175,23 +1274,29 @@ async function editAgentTypesDialog(
1175
1274
  label: EDIT_FIELD_LABELS[action],
1176
1275
  valueColor: action === "Save" && dirty ? "warning" as const : undefined,
1177
1276
  value:
1178
- action === "Save" && dirty
1179
- ? "(changes)"
1180
- : action === "Source"
1181
- ? (original?.source ?? "New draft")
1182
- : action === "Save scope"
1183
- ? saveScope === "user"
1184
- ? "Global"
1185
- : "Trusted project"
1186
- : action === "icon" && nerdFontIcons && draft.icon
1187
- ? draft.icon
1188
- : separator >= 0
1189
- ? decorated.slice(separator + 2)
1190
- : "",
1277
+ action === "Customization"
1278
+ ? customizationMenuValue(editMode, isNew)
1279
+ : action === "Save" && dirty
1280
+ ? "(changes)"
1281
+ : action === "Source"
1282
+ ? (original?.customization
1283
+ ? `${original.customization.scope} ${original.customization.kind} over ${original.baseSource ?? "bundled"}`
1284
+ : (original?.source ?? "New draft"))
1285
+ : action === "Save scope"
1286
+ ? saveScope === "user"
1287
+ ? "Global"
1288
+ : "Trusted project"
1289
+ : action === "icon" && nerdFontIcons && draft.icon
1290
+ ? draft.icon
1291
+ : separator >= 0
1292
+ ? decorated.slice(separator + 2)
1293
+ : "",
1191
1294
  help:
1192
1295
  action === "Source"
1193
- ? (original?.filePath ??
1194
- "Not saved yet. Bundled definitions are copied, never overwritten.")
1296
+ ? (original?.customization
1297
+ ? `Settings file: ${original.customization.filePath}. Base prompt: ${original.baseFilePath ?? "(bundled)"}.`
1298
+ : (original?.filePath ??
1299
+ "Not saved yet. Bundled definitions are copied, never overwritten."))
1195
1300
  : EDIT_FIELD_HELP[action],
1196
1301
  };
1197
1302
  }),
@@ -1204,12 +1309,74 @@ async function editAgentTypesDialog(
1204
1309
  selectedId = field;
1205
1310
  if (!field || field === "Cancel") break;
1206
1311
  try {
1312
+ if (field === "Customization") {
1313
+ if (isNew) {
1314
+ ctx.ui.notify("New types are always full definitions (.md).", "info");
1315
+ continue;
1316
+ }
1317
+ const mode = await chooseCustomizationMode(ctx, draft.name, editMode);
1318
+ if (!mode) continue;
1319
+ if (mode !== editMode && mode === "override" && original) {
1320
+ // Overrides carry no prompt body; drop any prompt edits.
1321
+ const base = store.getBase(original.name) ?? original;
1322
+ draft.systemPrompt = base.systemPrompt;
1323
+ ctx.ui.notify(
1324
+ "Switched to settings override: prompt edits were dropped (the bundled prompt applies).",
1325
+ "info",
1326
+ );
1327
+ }
1328
+ editMode = mode;
1329
+ continue;
1330
+ }
1331
+ if (!isNew && !editMode) {
1332
+ ctx.ui.notify(
1333
+ "Pick Customization first: tweak settings (.yml) or fork the agent (.md).",
1334
+ "error",
1335
+ );
1336
+ continue;
1337
+ }
1338
+ if (
1339
+ editMode === "override" &&
1340
+ (field === "name" ||
1341
+ field === "systemPrompt" ||
1342
+ field === "Edit frontmatter YAML" ||
1343
+ field === "External editor (entire Markdown)")
1344
+ ) {
1345
+ ctx.ui.notify(
1346
+ field === "name"
1347
+ ? "Settings overrides cannot rename the agent; fork it instead."
1348
+ : "The system prompt lives with the bundled definition; fork the agent to edit it.",
1349
+ "error",
1350
+ );
1351
+ continue;
1352
+ }
1207
1353
  if (field === "Save") {
1354
+ if (!editMode) continue;
1208
1355
  // Round-trip before saving: no partial or invalid configuration is accepted.
1209
1356
  const validated = parseAgentType(serializeAgentType(draft));
1210
1357
  const scope = await chooseSaveScope(ctx, store, saveScope);
1211
1358
  if (!scope) continue;
1212
- assertSaveDestination(store, validated, original, scope);
1359
+ assertSaveDestination(store, validated, original, scope, editMode);
1360
+ if (editMode === "override") {
1361
+ if (
1362
+ original?.customization?.kind === "fork" &&
1363
+ original.customization.scope === scope
1364
+ ) {
1365
+ store.removeCustomization(original.name, scope);
1366
+ }
1367
+ const saved = store.saveOverride(draft.name, validated, scope, original);
1368
+ ctx.ui.notify(
1369
+ `Saved settings override ${sanitizeText(saved.filePath ?? saved.name)}. Prompt and unset fields follow the base definition.`,
1370
+ "info",
1371
+ );
1372
+ break;
1373
+ }
1374
+ if (
1375
+ original?.customization?.kind === "override" &&
1376
+ original.customization.scope === scope
1377
+ ) {
1378
+ store.removeCustomization(original.name, scope);
1379
+ }
1213
1380
  const saved = store.save(validated, scope, original);
1214
1381
  ctx.ui.notify(
1215
1382
  `Saved ${sanitizeText(saved.filePath ?? saved.name)}${original && original.name !== saved.name ? " (original file retained)" : ""}.`,