pi-better-subagents 0.23.3 → 0.25.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/catalog-ui.ts CHANGED
@@ -15,6 +15,8 @@ export interface CatalogEditor {
15
15
  settings(): AgentSessionOverrides;
16
16
  change(id: string, key: AgentSettingKey, value: string | null): void;
17
17
  save(id: string): { ok: boolean; message: string };
18
+ resetToBundled?(entry: OperationView): { ok: boolean; message: string };
19
+ resetDefinition?(entry: OperationView): { ok: boolean; message: string };
18
20
  }
19
21
 
20
22
  function stateLabel(entry: OperationView): string {
@@ -137,11 +139,15 @@ export function createCatalogComponent(
137
139
  getHeight: () => number = () => 30,
138
140
  ) {
139
141
  let selectedId = initialId ?? catalog.entries[0]?.id;
140
- let mode: "list" | "detail" | "prompt" = initialId ? "detail" : "list";
142
+ let mode: "list" | "detail" | "prompt" | "files" = initialId ? "detail" : "list";
141
143
  let query = "";
142
144
  let scroll = 0;
143
145
  let settingRow = 0;
144
146
  let settingPositions: number[] = [];
147
+ let resetPointerRows: number[] = [];
148
+ let resetPointerEntry: OperationView | undefined;
149
+ let pressedReset: OperationView | undefined;
150
+ let pressedResetMode: "detail" | "prompt" | undefined;
145
151
  let revealSetting = false;
146
152
  let picker: SelectList | undefined;
147
153
  let filterPicker: ((filter: string) => void) | undefined;
@@ -162,9 +168,30 @@ export function createCatalogComponent(
162
168
  report(result.message, !result.ok);
163
169
  } catch (cause) { report(String(cause instanceof Error ? cause.message : cause), true); }
164
170
  }
171
+ function canReset(entry: OperationView | undefined): boolean {
172
+ return !!editor?.resetToBundled && !!entry && !Object.values(entry.preferenceSources ?? {}).some(source => source.scope === "project")
173
+ && (entry.identity.scope === "bundled" || entry.identity.scope === "user" && entry.shadowed.some(source => source.scope === "bundled"));
174
+ }
175
+ function canResetDefinition(entry: OperationView | undefined): boolean {
176
+ return !!editor?.resetDefinition && !!entry && (entry.identity.scope === "bundled"
177
+ || entry.identity.scope === "user" && entry.shadowed.some(source => source.scope === "bundled"));
178
+ }
179
+ function reset(entry = selected(), definition = false) {
180
+ if (!entry || !(definition ? canResetDefinition(entry) : canReset(entry))) return;
181
+ try {
182
+ const result = definition ? editor!.resetDefinition!(entry) : editor!.resetToBundled!(entry);
183
+ refresh();
184
+ settingRow = 0;
185
+ scroll = 0;
186
+ resetPointerRows = [];
187
+ resetPointerEntry = undefined;
188
+ report(result.message, !result.ok);
189
+ } catch (cause) { report(cause instanceof Error ? cause.message : String(cause), true); }
190
+ }
165
191
  function edit() {
166
192
  const current = selected();
167
193
  if (!current || !editor) return;
194
+ if (settingRow === 2) { reset(current); return; }
168
195
  if (!current.definitionValid) { report("Repair the catalog definition before editing.", true); return; }
169
196
  const key: AgentSettingKey = settingRow === 0 ? "model" : "effort";
170
197
  // Pi treats an empty scope as unrestricted. Scoped choices never include
@@ -211,22 +238,47 @@ export function createCatalogComponent(
211
238
  [entry.id, entry.identity.name, entry.identity.description, entry.role?.id]
212
239
  .some((value) => value?.toLowerCase().includes(query.toLowerCase())));
213
240
  const selected = () => filtered().find((entry) => entry.id === selectedId);
214
- const detailRows = (entry: OperationView, width: number, controls: string[] = []) => (mode === "prompt"
215
- ? [`${entry.identity.name || entry.id} · effective instructions`, "", ...(entry.instructions?.split("\n") ?? ["(none)"])]
216
- : catalogDetailLines(entry, !!editor, width, controls, theme))
217
- .flatMap((text) => text ? wrapLine(text, Math.max(1, width - 2)) : [""]);
241
+ const detailRows = (entry: OperationView, width: number, controls: string[] = []) => {
242
+ let lines: string[];
243
+ if (mode === "prompt") lines = [
244
+ ...(canResetDefinition(entry) ? [theme.fg("accent", "> Reset to bundled"), theme.fg("dim", " Restores the prompt; keeps model/effort choices."), ""] : []),
245
+ theme.fg("muted", `Source: ${entry.identity.scope ?? "unknown"} definition`), "", ...(entry.instructions?.split("\n") ?? ["(No resolved instructions)"]),
246
+ ];
247
+ else if (mode === "files") lines = [
248
+ theme.fg("muted", "Definition"), entry.identity.path ?? "(none)", "",
249
+ theme.fg("muted", "Saved preferences"),
250
+ ...([...new Set(Object.values(entry.preferenceSources ?? {}).map(source => `${source.scope}: ${source.path}`))].length
251
+ ? [...new Set(Object.values(entry.preferenceSources ?? {}).map(source => `${source.scope}: ${source.path}`))] : ["No saved preferences for this definition."]),
252
+ "", theme.fg("muted", "Session edits"), ...JSON.stringify(editor?.settings()[entry.id] ?? {}, null, 2).split("\n"),
253
+ ...(entry.shadowed.length ? ["", theme.fg("muted", "Shadowed definitions"), ...entry.shadowed.map(source => `${source.scope}: ${source.path}`)] : []),
254
+ ];
255
+ else lines = [
256
+ ...(editor ? controls : (["model", "effort"] as const).flatMap(key => settingLines(key === "model" ? "Model" : "Effort", entry.fields?.[key].value ?? "(unset)", entry.fields?.[key].inherited || entry.fields?.[key].source === "role-default" ? "inherited" : "catalog", Math.max(1, width - 4), " ", theme))),
257
+ "", theme.fg("dim", "Model and effort save separately from instructions."), "",
258
+ `ID ${entry.id}`, `Scope ${entry.identity.scope ?? "unknown"}`,
259
+ ...(entry.role?.id && entry.identity.kind === "agent" ? [`Role ${entry.role.id}`] : []),
260
+ `Tier ${entry.fields?.tier.value ?? "(unset)"}`,
261
+ `Actual ${entry.actualModel ?? "not resolved"}${entry.actualEffort ? ` · ${entry.actualEffort}` : ""}`,
262
+ "", theme.fg("muted", "Launch resolution"), entry.launchabilityReason,
263
+ ...(entry.validation.length ? ["", theme.fg("muted", "Diagnostics"), ...entry.validation.map(item => `${item.blocking ? "!" : "·"} ${item.message}`)] : []),
264
+ ...(entry.restrictions.length ? ["", theme.fg("muted", "Execution restrictions"), ...entry.restrictions.map(item => `${item.honored ? "honored" : "not honored"} ${item.name}`)] : []),
265
+ ];
266
+ return lines.flatMap(text => text ? wrapLine(text, Math.max(1, width - 4)).map(row => ` ${row}`) : [""]);
267
+ };
218
268
 
219
269
 
220
270
  return {
221
271
  get focused() { return focused; },
222
272
  set focused(value: boolean) { focused = value; search.focused = value && !!picker && pickerKey === "model"; },
223
273
  render(width: number): string[] {
274
+ resetPointerRows = [];
275
+ resetPointerEntry = undefined;
224
276
  const safeWidth = Math.max(1, width);
225
277
  const height = Math.max(1, Math.floor(getHeight()));
226
- const footerRows = (editor && mode !== "prompt" ? 2 : 0) + (message ? 2 : 0);
278
+ const footerRows = (editor ? 2 : 0) + (message ? 2 : 0);
227
279
  const diagnosticRows = Math.min(catalog.diagnostics.length, 3);
228
280
  const listSize = Math.max(1, height - 5 - footerRows - diagnosticRows - 1);
229
- const detailSize = Math.max(1, height - 5 - footerRows);
281
+ const detailSize = Math.max(1, height - 6 - footerRows);
230
282
  const fit = (lines: string[]) => {
231
283
  const result = lines.slice(0, height).map(line);
232
284
  while (result.length < height) result.push("");
@@ -254,13 +306,18 @@ export function createCatalogComponent(
254
306
  const own = editor?.settings()[current.id];
255
307
  const ownSession = Object.hasOwn(own ?? {}, key);
256
308
  const roleSession = field?.inherited && Object.hasOwn(editor?.settings()[current.role?.id ?? ""] ?? {}, key);
257
- const origin = ownSession && own?.[key] === null ? "inherit / session" : ownSession ? "session" : roleSession ? "inherited / session" : field?.inherited ? "inherited" : "catalog";
309
+ const saved = current.preferenceSources?.[key];
310
+ const inherited = current.identity.kind === "agent" && field?.inherited;
311
+ const origin = ownSession && own?.[key] === null ? "inherit / session" : ownSession ? "session" : roleSession ? "inherited / session"
312
+ : saved?.bundled ? "bundled" : saved ? `${inherited ? "inherited / " : ""}${saved.scope} config`
313
+ : current.identity.scope === "bundled" && !inherited ? "bundled" : field?.inherited ? "inherited" : "catalog";
258
314
  return settingLines(label, field?.value ?? "(unset)", origin, Math.max(1, safeWidth - 4), settingRow === index ? "> " : " ", theme);
259
315
  }),
316
+ ...(canReset(current) ? [theme.fg(settingRow === 2 ? "accent" : "muted", `${settingRow === 2 ? "> " : " "}Reset to bundled`), theme.fg("dim", " Restores model/effort; keeps the role prompt.")] : []),
260
317
  ] : [];
261
318
  const detailLines = current ? detailRows(current, safeWidth, controls) : [];
262
319
  if (editor && mode === "detail") {
263
- settingPositions = ["Model", "Effort"].map((label) => detailLines.findIndex((text) =>
320
+ settingPositions = ["Model", "Effort", ...(canReset(current) ? ["Reset to bundled"] : [])].map((label) => detailLines.findIndex((text) =>
264
321
  new RegExp(`^ [> ] ${label}(?:\\s|$)`).test(text.replace(/\x1b\[[0-9;]*m/g, ""))));
265
322
  if (revealSetting && settingPositions[settingRow] >= 0) {
266
323
  const position = settingPositions[settingRow];
@@ -270,9 +327,21 @@ export function createCatalogComponent(
270
327
  revealSetting = false;
271
328
  }
272
329
  scroll = Math.max(0, Math.min(scroll, detailLines.length - detailSize));
330
+ if (mode === "detail" && current && canReset(current)) {
331
+ const position = settingPositions[2];
332
+ if (position >= scroll && position < scroll + detailSize) {
333
+ resetPointerRows = [4 + position - scroll];
334
+ resetPointerEntry = current;
335
+ }
336
+ }
337
+ if (mode === "prompt" && current && canResetDefinition(current) && scroll === 0) {
338
+ resetPointerRows = [4];
339
+ resetPointerEntry = current;
340
+ }
273
341
  const lines = mode !== "list" && current ? [
274
- theme.fg("accent", mode === "prompt" ? "Agents / prompt" : editor ? `Agents / ${current.identity.name || current.id}` : "Agents / inspect"),
275
- theme.fg("dim", mode === "prompt" ? "← details · ↑↓ scroll · Esc back" : `${editor ? "Enter change · " : ""}p prompt · ← list · ↑↓ ${editor ? "select / scroll" : "scroll"} · Esc back`),
342
+ theme.fg("accent", `Agents / ${current.identity.name || current.id}`),
343
+ ([['detail', 'Settings'], ['prompt', 'Instructions'], ['files', 'Files']] as const).map(([tab, label]) => theme.fg(mode === tab ? "accent" : "dim", mode === tab ? `[${label}]` : label)).join(" "),
344
+ theme.fg("dim", safeWidth < 60 ? "Tab switch · ↑↓ move · Enter · Esc" : `Tab / Shift+Tab switch · ↑↓ ${mode === "detail" && editor ? "select / scroll · Enter change" : mode === "prompt" && canResetDefinition(current) ? "scroll · Enter reset at top" : "scroll"} · Esc list`),
276
345
  "",
277
346
 
278
347
  ...detailLines.slice(scroll, scroll + detailSize).map((text, i) =>
@@ -297,7 +366,7 @@ export function createCatalogComponent(
297
366
  ...catalog.diagnostics.slice(0, 2).map((diagnostic) => theme.fg("warning", `! ${diagnostic.message}`)),
298
367
  ...(catalog.diagnostics.length > 2 ? [theme.fg("warning", `! ${catalog.diagnostics.length - 2} more catalog diagnostics`)] : []),
299
368
  ];
300
- if (editor && mode !== "prompt") lines.push("", theme.fg("dim", "ctrl+s Save selected defaults"));
369
+ if (editor) lines.push("", theme.fg("dim", "Ctrl+S Save defaults"));
301
370
  if (message) lines.push("", theme.fg(error ? "error" : "muted", message));
302
371
  return fit(lines);
303
372
  },
@@ -312,15 +381,25 @@ export function createCatalogComponent(
312
381
  return;
313
382
  }
314
383
  if (editor && matchesKey(data, "ctrl+s")) { save(); return; }
384
+ if (mode !== "list" && (matchesKey(data, "tab") || matchesKey(data, "shift+tab"))) {
385
+ const tabs = ["detail", "prompt", "files"] as const;
386
+ const index = tabs.indexOf(mode);
387
+ mode = tabs[(index + (matchesKey(data, "shift+tab") ? 2 : 1)) % 3];
388
+ scroll = 0;
389
+ revealSetting = mode === "detail";
390
+ pressedReset = undefined;
391
+ requestRender();
392
+ return;
393
+ }
315
394
  if (matchesKey(data, "escape") || matchesKey(data, "left")) {
316
- if (mode === "prompt") { mode = "detail"; scroll = 0; }
395
+ if (mode === "prompt" || mode === "files") { mode = "detail"; scroll = 0; revealSetting = true; }
317
396
  else if (mode === "detail") { mode = "list"; scroll = 0; }
318
397
  else if (query) { query = ""; selectedId = catalog.entries[0]?.id; }
319
398
  else { close(); return; }
320
399
  } else if (matchesKey(data, "up") || matchesKey(data, "down")) {
321
400
  const delta = matchesKey(data, "down") ? 1 : -1;
322
- if (mode === "detail" && editor && scroll <= (settingPositions[1] ?? 0)
323
- && ((delta > 0 && settingRow === 0) || (delta < 0 && settingRow === 1))) {
401
+ if (mode === "detail" && editor && scroll <= (settingPositions.at(-1) ?? 0)
402
+ && ((delta > 0 && settingRow < settingPositions.length - 1) || (delta < 0 && settingRow > 0))) {
324
403
  settingRow += delta;
325
404
  revealSetting = true;
326
405
  }
@@ -339,6 +418,8 @@ export function createCatalogComponent(
339
418
  revealSetting = true;
340
419
  } else if (editor && mode === "detail" && (matchesKey(data, "enter") || data === " ")) {
341
420
  try { edit(); } catch (cause) { report(cause instanceof Error ? cause.message : String(cause), true); }
421
+ } else if (editor && mode === "prompt" && scroll === 0 && (matchesKey(data, "enter") || data === " ")) {
422
+ reset(selected(), true);
342
423
  } else if (mode === "list" && matchesKey(data, "backspace")) {
343
424
  query = query.slice(0, -1);
344
425
  selectedId = filtered()[0]?.id;
@@ -348,6 +429,22 @@ export function createCatalogComponent(
348
429
  }
349
430
  requestRender();
350
431
  },
432
+ handleMouse(event: { type: string; button: string; x: number; y: number; width: number; shift?: boolean; alt?: boolean; ctrl?: boolean }) {
433
+ const entry = resetPointerEntry;
434
+ const hit = !picker && (mode === "detail" || mode === "prompt") && entry && event.button === "left" && !event.shift && !event.alt && !event.ctrl
435
+ && event.x >= 0 && event.x < event.width && resetPointerRows.includes(event.y);
436
+ if (event.type === "press") {
437
+ pressedReset = hit ? entry : undefined;
438
+ pressedResetMode = hit ? mode as "detail" | "prompt" : undefined;
439
+ return hit ? { handled: true as const, render: false } : undefined;
440
+ }
441
+ if (event.type !== "click") return undefined;
442
+ const pressed = pressedReset;
443
+ pressedReset = undefined;
444
+ if (!hit || !pressed || pressedResetMode !== mode || pressed.id !== entry.id || pressed.winningSource?.contentDigest !== entry.winningSource?.contentDigest) return undefined;
445
+ reset(pressed, mode === "prompt");
446
+ return { handled: true as const, render: true };
447
+ },
351
448
  invalidate() { picker?.invalidate(); search.invalidate(); },
352
449
  };
353
450
  }