@ferris1225/pi-subagents 6.0.3 → 6.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +42 -0
- package/README.md +83 -200
- package/index.ts +8 -13
- package/package.json +1 -1
- package/src/configuration/config.ts +2 -6
- package/src/configuration/role-migration.ts +3 -14
- package/src/configuration/setup.ts +66 -57
- package/src/configuration/ui.ts +335 -64
- package/src/delegation/agents.ts +5 -6
- package/src/delegation/builtin-agents.ts +10 -31
- package/src/delegation/dispatch.ts +52 -165
- package/src/delegation/prompt.ts +29 -82
- package/src/execution/background.ts +2 -2
- package/src/execution/run-control.ts +1 -1
- package/src/execution/spawn.ts +2 -30
- package/src/isolation/worktree.ts +0 -14
- package/src/lifecycle/cancellation.ts +4 -8
- package/src/lifecycle/completion.ts +9 -21
- package/src/lifecycle/dispatch-core.ts +7 -7
- package/src/lifecycle/durable.ts +5 -29
- package/src/lifecycle/runtime.ts +11 -63
- package/src/lifecycle/stop.ts +1 -2
- package/src/lifecycle/thread-lifecycle.ts +38 -83
- package/src/lifecycle/thread-restore.ts +1 -3
- package/src/lifecycle/thread-shared.ts +10 -27
- package/src/lifecycle/tools.ts +24 -114
- package/src/presentation/announcements.ts +8 -6
- package/src/presentation/format.ts +39 -14
- package/src/presentation/monitor.ts +697 -758
- package/src/presentation/widget.ts +34 -142
- package/src/delegation/ownership.ts +0 -98
- package/src/delegation/phase-scope.ts +0 -186
- package/src/workflow/acceptance.ts +0 -67
- package/src/workflow/durable.ts +0 -228
- package/src/workflow/engine.ts +0 -816
- package/src/workflow/plan.ts +0 -409
- package/src/workflow/state.ts +0 -55
- package/src/workflow/templates.ts +0 -156
- package/src/workflow/tool.ts +0 -254
|
@@ -24,7 +24,7 @@ import {
|
|
|
24
24
|
resolveThinkingLevel,
|
|
25
25
|
supportedThinkingLevels,
|
|
26
26
|
} from "./models.ts";
|
|
27
|
-
import {
|
|
27
|
+
import { promptRoleGrid, promptSelectOne, type RoleGridFocus, type RoleGridRow, type SetupItem } from "./ui.ts";
|
|
28
28
|
|
|
29
29
|
const ROLE_DEFAULT = "__role_default__";
|
|
30
30
|
const THINKING_LEVEL_HINTS: Record<ThinkingLevel, string> = {
|
|
@@ -37,6 +37,12 @@ const THINKING_LEVEL_HINTS: Record<ThinkingLevel, string> = {
|
|
|
37
37
|
max: "strongest reasoning",
|
|
38
38
|
};
|
|
39
39
|
|
|
40
|
+
const ROLE_PURPOSE: Record<string, string> = {
|
|
41
|
+
scout: "Research with citations",
|
|
42
|
+
artisan: "Implement and verify",
|
|
43
|
+
sentinel: "Review and challenge claims",
|
|
44
|
+
};
|
|
45
|
+
|
|
40
46
|
async function configExists(configPath: string): Promise<boolean> {
|
|
41
47
|
try {
|
|
42
48
|
await stat(configPath);
|
|
@@ -50,40 +56,36 @@ function roleModel(ctx: ExtensionContext, config: SubagentsConfig, name: string)
|
|
|
50
56
|
return findModelByRef(availableModelsInScope(ctx), config.agentModels[name]) ?? ctx.model;
|
|
51
57
|
}
|
|
52
58
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
skeptic: "Challenge a named claim",
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
function roleSettings(ctx: ExtensionContext, config: SubagentsConfig, name: string): SetupItem[] {
|
|
61
|
-
const selected = config.agentModels[name];
|
|
62
|
-
const model = findModelByRef(availableModelsInScope(ctx), selected);
|
|
63
|
-
const modelLabel = selected
|
|
64
|
-
? model ? model.name + " · " + model.provider : selected + " (unavailable)"
|
|
65
|
-
: "Follow main (dynamic)";
|
|
66
|
-
const preferred = config.agentThinkingLevels[name] ?? roleThinkingLevel(name);
|
|
67
|
-
const effective = resolveThinkingLevel(model ?? ctx.model, preferred);
|
|
68
|
-
const source = config.agentThinkingLevels[name] ? "override" : "role default";
|
|
69
|
-
const thinking = effective + (effective !== preferred ? " (" + preferred + " requested)" : "") + " · " + source;
|
|
70
|
-
return [
|
|
71
|
-
{ value: "model", label: "Model: " + modelLabel, description: selected
|
|
72
|
-
? "Pinned reference: " + selected + (model ? ". Falls back to main if it fails." : ". Unavailable; using main.")
|
|
73
|
-
: "Follows the main window's model, including future changes. Now: " + (currentModelRef(ctx) ?? "Pi default") + ". Choose a specific model to pin it." },
|
|
74
|
-
{ value: "thinking", label: "Thinking: " + thinking,
|
|
75
|
-
description: "Reasoning effort: " + thinking + ". Role default: " + roleThinkingLevel(name) + ". Model capabilities limit the effective level; your saved preference is kept when changing models." },
|
|
76
|
-
{ value: "reset", label: "Reset defaults", description: "Restore only " + name + " to Follow main and role-default thinking (" + roleThinkingLevel(name) + "). Saves immediately." },
|
|
77
|
-
];
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function roleItems(ctx: ExtensionContext, config: SubagentsConfig): SetupItem[] {
|
|
59
|
+
/** One row per role: every model and thinking choice stays visible together in the standalone modal panel. */
|
|
60
|
+
function roleRows(ctx: ExtensionContext, config: SubagentsConfig): RoleGridRow[] {
|
|
61
|
+
const mainRef = currentModelRef(ctx) ?? "Pi default";
|
|
62
|
+
const models = availableModelsInScope(ctx);
|
|
81
63
|
return BUILTIN_AGENT_NAMES.map((name) => {
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
|
|
64
|
+
const profile = agentProfile(name)!;
|
|
65
|
+
const selected = config.agentModels[name];
|
|
66
|
+
const model = findModelByRef(models, selected);
|
|
67
|
+
const available = !selected || Boolean(model);
|
|
68
|
+
const modelLabel = selected
|
|
69
|
+
? (model ? model.name + " · " + model.provider + (model.name !== selected ? " (" + selected + ")" : "") : selected + " (unavailable)")
|
|
70
|
+
: "Follow main (dynamic)";
|
|
71
|
+
const preferred = config.agentThinkingLevels[name] ?? roleThinkingLevel(name);
|
|
72
|
+
const effective = resolveThinkingLevel(model ?? ctx.model, preferred);
|
|
73
|
+
const clamped = effective !== preferred ? " (" + preferred + " requested)" : "";
|
|
74
|
+
const overridden = Boolean(config.agentThinkingLevels[name]);
|
|
75
|
+
return {
|
|
76
|
+
role: name,
|
|
77
|
+
purpose: ROLE_PURPOSE[name],
|
|
78
|
+
thinkingOverridden: overridden,
|
|
79
|
+
model: modelLabel,
|
|
80
|
+
thinking: effective + clamped,
|
|
81
|
+
description: profile.remark + " Model: " + (selected
|
|
82
|
+
? "pinned reference " + selected + (available ? "; falls back to main if it fails." : "; unavailable, using main.")
|
|
83
|
+
: "follows main (" + mainRef + ") dynamically.")
|
|
84
|
+
+ " Thinking: role default " + roleThinkingLevel(name) + ", clamped to the model-supported levels. Reset restores " + name + " to Follow main and that default. * marks an override.",
|
|
85
|
+
};
|
|
85
86
|
});
|
|
86
87
|
}
|
|
88
|
+
|
|
87
89
|
async function editSetting(
|
|
88
90
|
ctx: ExtensionContext,
|
|
89
91
|
config: SubagentsConfig,
|
|
@@ -99,7 +101,7 @@ async function editSetting(
|
|
|
99
101
|
delete next.agentThinkingLevels[name];
|
|
100
102
|
} else if (field === "model") {
|
|
101
103
|
const choice = await promptSelectOne(ctx, name + " · Model",
|
|
102
|
-
"Follow main = dynamic · Others = pinned",
|
|
104
|
+
"Follow main = dynamic · Others = pinned · Esc back without changes",
|
|
103
105
|
buildModelPickerItems({ models: availableModelsInScope(ctx), configuredRef: config.agentModels[name], mainRef: currentModelRef(ctx) }),
|
|
104
106
|
config.agentModels[name] ?? CURRENT_MAIN_MODEL);
|
|
105
107
|
if (choice === undefined) return undefined;
|
|
@@ -113,17 +115,31 @@ async function editSetting(
|
|
|
113
115
|
ctx.ui.notify("Choose a model before changing thinking; its supported levels are not available yet.", "info");
|
|
114
116
|
return undefined;
|
|
115
117
|
}
|
|
116
|
-
const options = [
|
|
117
|
-
{ value: ROLE_DEFAULT, label: "Role default", description: preferredDefault + (effectiveDefault !== preferredDefault ? " → " + effectiveDefault + " on this model" : "") },
|
|
118
|
-
...supported.map((level) => ({ value: level, label: level, description: THINKING_LEVEL_HINTS[level] })),
|
|
119
|
-
];
|
|
120
118
|
const current = config.agentThinkingLevels[name];
|
|
119
|
+
const options: SetupItem[] = [
|
|
120
|
+
{
|
|
121
|
+
value: ROLE_DEFAULT,
|
|
122
|
+
label: "Role default",
|
|
123
|
+
description: preferredDefault + (effectiveDefault !== preferredDefault ? " → " + effectiveDefault + " on this model" : ""),
|
|
124
|
+
saved: !current,
|
|
125
|
+
},
|
|
126
|
+
...supported.map((level) => ({
|
|
127
|
+
value: level,
|
|
128
|
+
label: level,
|
|
129
|
+
description: THINKING_LEVEL_HINTS[level],
|
|
130
|
+
saved: current === level,
|
|
131
|
+
})),
|
|
132
|
+
];
|
|
121
133
|
if (current && !supported.includes(current)) {
|
|
122
|
-
options.push({
|
|
123
|
-
|
|
134
|
+
options.push({
|
|
135
|
+
value: current,
|
|
136
|
+
label: current + " (runs as " + resolveThinkingLevel(model, current) + ")",
|
|
137
|
+
description: "Saved preference; this model clamps it. Keep it for other models, or choose a supported level.",
|
|
138
|
+
saved: true,
|
|
139
|
+
});
|
|
124
140
|
}
|
|
125
141
|
const choice = await promptSelectOne(ctx, name + " · Thinking",
|
|
126
|
-
"Reasoning effort · saved preferences survive model changes",
|
|
142
|
+
"Reasoning effort · saved preferences survive model changes · Esc back",
|
|
127
143
|
options, current ?? ROLE_DEFAULT);
|
|
128
144
|
if (choice === undefined) return undefined;
|
|
129
145
|
if (choice === ROLE_DEFAULT) delete next.agentThinkingLevels[name];
|
|
@@ -134,8 +150,7 @@ async function editSetting(
|
|
|
134
150
|
return next;
|
|
135
151
|
}
|
|
136
152
|
|
|
137
|
-
|
|
138
|
-
/** The same role overview serves first install and later edits. */
|
|
153
|
+
/** Standalone modal panel for first install and later edits. */
|
|
139
154
|
export async function runSetup(ctx: ExtensionContext, configPath: string = getConfigPath()): Promise<void> {
|
|
140
155
|
if (ctx.mode !== "tui") {
|
|
141
156
|
ctx.ui.notify("/subagents-setup requires Pi's interactive TUI.", "error");
|
|
@@ -143,23 +158,17 @@ export async function runSetup(ctx: ExtensionContext, configPath: string = getCo
|
|
|
143
158
|
}
|
|
144
159
|
try {
|
|
145
160
|
let config = await loadConfig(configPath);
|
|
146
|
-
let
|
|
147
|
-
const settingFocus: Record<string, string> = {};
|
|
161
|
+
let focus: RoleGridFocus = {};
|
|
148
162
|
let status = "Changes save immediately";
|
|
149
163
|
while (true) {
|
|
150
|
-
const
|
|
151
|
-
if (
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
if (!next) continue;
|
|
159
|
-
await saveConfig(next, configPath);
|
|
160
|
-
config = next;
|
|
161
|
-
status = "Saved " + name + (field === "reset" ? " defaults" : " " + field);
|
|
162
|
-
}
|
|
164
|
+
const choice = await promptRoleGrid(ctx, roleRows(ctx, config), focus, status);
|
|
165
|
+
if (choice === undefined) return;
|
|
166
|
+
focus = { row: choice.row, column: choice.column };
|
|
167
|
+
const next = await editSetting(ctx, config, choice.role, choice.field);
|
|
168
|
+
if (!next) continue;
|
|
169
|
+
await saveConfig(next, configPath);
|
|
170
|
+
config = next;
|
|
171
|
+
status = "Saved " + choice.role + (choice.field === "reset" ? " defaults" : " " + choice.field);
|
|
163
172
|
}
|
|
164
173
|
} catch (error) {
|
|
165
174
|
ctx.ui.notify("pi-subagents setup failed: " + errorMessage(error), "error");
|
package/src/configuration/ui.ts
CHANGED
|
@@ -1,37 +1,80 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/** Standalone modal panels for /subagents-setup role settings and model selection. */
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
4
|
Input, fuzzyFilter, matchesKey, truncateToWidth, visibleWidth, wrapTextWithAnsi,
|
|
5
|
-
type Component, type Focusable, type Keybinding, type KeybindingsManager, type SelectItem, type TUI,
|
|
5
|
+
type Component, type Focusable, type Keybinding, type KeybindingsManager, type OverlayOptions, type SelectItem, type TUI,
|
|
6
6
|
} from "@earendil-works/pi-tui";
|
|
7
7
|
import type { ExtensionCommandContext, Theme } from "@earendil-works/pi-coding-agent";
|
|
8
8
|
|
|
9
9
|
type PickerContext = Pick<ExtensionCommandContext, "mode" | "ui">;
|
|
10
|
-
const PAGE_SIZE =
|
|
10
|
+
const PAGE_SIZE = 10;
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
export const SETUP_OVERLAY_OPTIONS: OverlayOptions = {
|
|
13
|
+
anchor: "center",
|
|
14
|
+
width: "86%",
|
|
15
|
+
minWidth: 72,
|
|
16
|
+
maxHeight: "88%",
|
|
17
|
+
margin: { top: 1, bottom: 1, left: 2, right: 2 },
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/** Modal height budget on the full terminal canvas instead of the input dock. */
|
|
14
21
|
function setupHeight(tui: TUI): number {
|
|
15
|
-
|
|
22
|
+
const termRows = tui.terminal?.rows ?? 28;
|
|
23
|
+
const maxRows = Math.floor(termRows * 0.88);
|
|
24
|
+
return Math.max(6, Math.min(maxRows, termRows - 2));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function padToWidth(text: string, width: number): string {
|
|
28
|
+
const truncated = truncateToWidth(text, width, "…");
|
|
29
|
+
return truncated + " ".repeat(Math.max(0, width - visibleWidth(truncated)));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function framePanel(theme: Theme, width: number, title: string, subtitle: string | undefined, body: string[], footer: string[]): string[] {
|
|
33
|
+
if (width <= 0) return [];
|
|
34
|
+
if (width < 20) {
|
|
35
|
+
return [theme.bold(title), ...(subtitle ? [theme.fg("dim", subtitle)] : []), ...body, ...footer]
|
|
36
|
+
.map((line) => truncateToWidth(line, width, "…"));
|
|
37
|
+
}
|
|
38
|
+
const border = (text: string) => theme.fg("border", text);
|
|
39
|
+
const innerWidth = Math.max(1, width - 4);
|
|
40
|
+
const topInner = Math.max(1, width - 2);
|
|
41
|
+
const titleBadge = truncateToWidth(" " + title + " ", topInner, "…");
|
|
42
|
+
const titleWidth = visibleWidth(titleBadge);
|
|
43
|
+
const topFill = Math.max(0, topInner - titleWidth - 1);
|
|
44
|
+
const row = (line = "") => border("│") + " " + padToWidth(line, innerWidth) + " " + border("│");
|
|
45
|
+
const divider = border("├" + "─".repeat(topInner) + "┤");
|
|
46
|
+
|
|
47
|
+
const out: string[] = [
|
|
48
|
+
border("╭─") + theme.fg("accent", theme.bold(titleBadge)) + border("─".repeat(topFill) + "╮"),
|
|
49
|
+
];
|
|
50
|
+
if (subtitle) out.push(row(theme.fg("muted", subtitle)));
|
|
51
|
+
out.push(divider);
|
|
52
|
+
for (const line of body) out.push(row(line));
|
|
53
|
+
if (footer.length) {
|
|
54
|
+
out.push(divider);
|
|
55
|
+
for (const line of footer) out.push(row(line));
|
|
56
|
+
}
|
|
57
|
+
out.push(border("╰" + "─".repeat(topInner) + "╯"));
|
|
58
|
+
return out;
|
|
16
59
|
}
|
|
17
60
|
|
|
18
61
|
export interface SetupItem extends SelectItem {
|
|
19
62
|
/** Saved state is independent of the navigation cursor. */
|
|
20
63
|
saved?: boolean;
|
|
21
64
|
}
|
|
65
|
+
|
|
22
66
|
interface PickerOptions {
|
|
23
67
|
title: string;
|
|
24
68
|
hint: string;
|
|
25
69
|
initialValue?: string;
|
|
26
|
-
searchable: boolean;
|
|
27
70
|
}
|
|
28
71
|
|
|
29
72
|
/** Input owns Unicode editing, paste, horizontal scrolling and the IME cursor.
|
|
30
|
-
*
|
|
73
|
+
* Selection, paging, and Tab detail inspection run inside a standalone modal panel. */
|
|
31
74
|
class Picker implements Component, Focusable {
|
|
32
75
|
private _focused = false;
|
|
33
76
|
private cursor = 0;
|
|
34
|
-
private pageRows =
|
|
77
|
+
private pageRows = PAGE_SIZE;
|
|
35
78
|
private details = false;
|
|
36
79
|
private detailTop = 0;
|
|
37
80
|
private filterFocus?: string;
|
|
@@ -64,11 +107,11 @@ class Picker implements Component, Focusable {
|
|
|
64
107
|
get focused(): boolean { return this._focused; }
|
|
65
108
|
set focused(value: boolean) {
|
|
66
109
|
this._focused = value;
|
|
67
|
-
this.search.focused = value &&
|
|
110
|
+
this.search.focused = value && !this.details;
|
|
68
111
|
}
|
|
69
112
|
|
|
70
113
|
private isSaved(item: SetupItem): boolean {
|
|
71
|
-
return item.saved ??
|
|
114
|
+
return item.saved ?? item.value === this.options.initialValue;
|
|
72
115
|
}
|
|
73
116
|
|
|
74
117
|
private keyLabel(action: Keybinding): string {
|
|
@@ -88,58 +131,96 @@ class Picker implements Component, Focusable {
|
|
|
88
131
|
render(width: number): string[] {
|
|
89
132
|
if (width <= 0) return [];
|
|
90
133
|
const theme = this.theme;
|
|
134
|
+
const dim = (text: string) => theme.fg("dim", text);
|
|
91
135
|
const muted = (text: string) => theme.fg("muted", text);
|
|
92
136
|
const height = setupHeight(this.tui);
|
|
93
|
-
const
|
|
94
|
-
const
|
|
95
|
-
|
|
96
|
-
if (!(height === 3 && this.options.searchable && !this.details)) {
|
|
97
|
-
lines.push(theme.bold(this.options.title + (this.details ? " · Details" : "")));
|
|
98
|
-
}
|
|
99
|
-
if (!this.details && this.options.hint && height >= 10) lines.push(muted(this.options.hint));
|
|
100
|
-
if (!this.details && this.options.searchable) lines.push(...this.search.render(width));
|
|
137
|
+
const framed = width >= 20 && height >= 10;
|
|
138
|
+
const innerWidth = framed ? Math.max(1, width - 4) : width;
|
|
139
|
+
|
|
101
140
|
const move = this.keyLabel("tui.select.up") + "/" + this.keyLabel("tui.select.down");
|
|
102
141
|
const confirm = this.keyLabel("tui.select.confirm");
|
|
103
142
|
const cancel = this.keyLabel("tui.select.cancel");
|
|
104
143
|
const pageKeys = this.keyLabel("tui.select.pageUp") + "/" + this.keyLabel("tui.select.pageDown");
|
|
105
144
|
const page = pageKeys === "PgUp/PgDn" ? "PgUp/Dn" : pageKeys;
|
|
145
|
+
const matchCount = this.filtered.length ? (this.cursor + 1) + "/" + this.filtered.length + " matches" : "0 results";
|
|
146
|
+
|
|
147
|
+
if (!framed && height < 8) {
|
|
148
|
+
this.pageRows = 1;
|
|
149
|
+
const item = this.filtered[this.cursor];
|
|
150
|
+
return [
|
|
151
|
+
theme.bold(this.options.title + (this.details ? " · Details" : "")),
|
|
152
|
+
...(this.details ? [] : this.search.render(innerWidth)),
|
|
153
|
+
item ? theme.fg("accent", "› ") + theme.underline(item.label + (this.isSaved(item) ? " [saved]" : "")) : "No matches. Clear or change the search.",
|
|
154
|
+
].map((line) => truncateToWidth(line, width, "…"));
|
|
155
|
+
}
|
|
156
|
+
|
|
106
157
|
const navigation = move + " move · " + confirm + " select · " + cancel + " back";
|
|
107
|
-
const selectionHint = visibleWidth(navigation) <=
|
|
108
|
-
const footer =
|
|
109
|
-
?
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
158
|
+
const selectionHint = visibleWidth(navigation) <= innerWidth ? navigation : confirm + " select · " + cancel + " back · " + move;
|
|
159
|
+
const footer = this.details
|
|
160
|
+
? (innerWidth >= 56
|
|
161
|
+
? [dim(move + " scroll · " + page + " page · Tab list · " + confirm + " select · " + cancel + " back")]
|
|
162
|
+
: [dim(move + " scroll · Tab list · " + cancel + " back"), dim(page + " page · " + confirm + " select")])
|
|
163
|
+
: (innerWidth >= 64
|
|
164
|
+
? [dim(selectionHint + " · " + matchCount + " · " + page + " · Tab details")]
|
|
165
|
+
: [dim(selectionHint), dim(matchCount + " · " + page + " · Tab details")]);
|
|
166
|
+
|
|
167
|
+
const titleText = "Subagents · " + this.options.title + (this.details ? " · Details" : "");
|
|
168
|
+
const subtitle = !this.details && this.options.hint ? this.options.hint : undefined;
|
|
169
|
+
const chromeRows = framed ? (subtitle ? 4 : 3) + footer.length + 1 : 2 + footer.length;
|
|
170
|
+
|
|
115
171
|
if (this.details) {
|
|
172
|
+
const budget = Math.max(1, height - chromeRows);
|
|
116
173
|
this.pageRows = budget;
|
|
117
|
-
const details = this.detailLines(
|
|
174
|
+
const details = this.detailLines(innerWidth);
|
|
118
175
|
this.detailTop = Math.max(0, Math.min(this.detailTop, details.length - budget));
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
const start = Math.max(0, Math.min(this.cursor - Math.floor(this.pageRows / 2), this.filtered.length - this.pageRows));
|
|
123
|
-
const visible = this.filtered.slice(start, start + this.pageRows);
|
|
124
|
-
if (!visible.length) lines.push(muted(this.search.getValue() ? "No matches. Clear or change the search." : "No choices available."));
|
|
125
|
-
for (const [index, item] of visible.entries()) {
|
|
126
|
-
const active = start + index === this.cursor;
|
|
127
|
-
const suffix = this.isSaved(item) ? " [saved]" : "";
|
|
128
|
-
const label = truncateToWidth(item.label, Math.max(0, width - 2 - visibleWidth(suffix)), "…") + suffix;
|
|
129
|
-
lines.push(active ? theme.fg("accent", "› ") + theme.underline(theme.bold(label)) : " " + label);
|
|
130
|
-
}
|
|
131
|
-
// Menus show the active role/setting's explanation. Choice lists only
|
|
132
|
-
// use spare rows for context, never displacing another choice.
|
|
133
|
-
const spare = Math.min(3, height - lines.length - footer.length);
|
|
134
|
-
const item = this.filtered[this.cursor];
|
|
135
|
-
if (spare > 0 && item?.description) {
|
|
136
|
-
const wrapped = wrapTextWithAnsi(item.description, width);
|
|
137
|
-
const preview = wrapped.slice(0, spare);
|
|
138
|
-
if (wrapped.length > spare) preview[spare - 1] = truncateToWidth(preview[spare - 1]! + "…", width, "…");
|
|
139
|
-
lines.push(...preview.map(muted));
|
|
176
|
+
const body = details.slice(this.detailTop, this.detailTop + budget);
|
|
177
|
+
if (!framed) {
|
|
178
|
+
return [theme.bold(titleText), ...body, ...footer].map((line) => truncateToWidth(line, width, "…"));
|
|
140
179
|
}
|
|
180
|
+
return framePanel(theme, width, titleText, subtitle, body, footer);
|
|
141
181
|
}
|
|
142
|
-
|
|
182
|
+
|
|
183
|
+
const searchLines = [...this.search.render(innerWidth), ""];
|
|
184
|
+
const item = this.filtered[this.cursor];
|
|
185
|
+
const previewLines = item?.description
|
|
186
|
+
? ["", ...wrapTextWithAnsi(item.description, innerWidth).slice(0, 3).map((line, idx, arr) => {
|
|
187
|
+
const allWrapped = wrapTextWithAnsi(item.description!, innerWidth);
|
|
188
|
+
if (idx === arr.length - 1 && allWrapped.length > arr.length) {
|
|
189
|
+
return muted(truncateToWidth(line + "…", innerWidth, "…"));
|
|
190
|
+
}
|
|
191
|
+
return muted(line);
|
|
192
|
+
})]
|
|
193
|
+
: [];
|
|
194
|
+
|
|
195
|
+
const availAfterSearch = Math.max(1, height - chromeRows - searchLines.length);
|
|
196
|
+
const minListRows = Math.min(Math.max(1, this.filtered.length), Math.min(6, availAfterSearch));
|
|
197
|
+
const detailBudget = Math.max(0, Math.min(previewLines.length, availAfterSearch - minListRows));
|
|
198
|
+
const visibleDetails = previewLines.slice(0, detailBudget);
|
|
199
|
+
this.pageRows = Math.max(1, Math.min(PAGE_SIZE, availAfterSearch - visibleDetails.length));
|
|
200
|
+
const start = Math.max(0, Math.min(this.cursor - Math.floor(this.pageRows / 2), this.filtered.length - this.pageRows));
|
|
201
|
+
const visible = this.filtered.slice(start, start + this.pageRows);
|
|
202
|
+
|
|
203
|
+
const body: string[] = [...searchLines];
|
|
204
|
+
if (!visible.length) {
|
|
205
|
+
body.push(dim(this.search.getValue() ? "No matches. Clear or change the search." : "No choices available."));
|
|
206
|
+
}
|
|
207
|
+
for (const [index, choice] of visible.entries()) {
|
|
208
|
+
const selected = start + index === this.cursor;
|
|
209
|
+
const suffix = this.isSaved(choice) ? " [saved]" : "";
|
|
210
|
+
const label = truncateToWidth(choice.label, Math.max(0, innerWidth - 2 - visibleWidth(suffix)), "…") + suffix;
|
|
211
|
+
body.push(selected ? theme.fg("accent", "› ") + theme.underline(theme.bold(label)) : " " + label);
|
|
212
|
+
}
|
|
213
|
+
body.push(...visibleDetails);
|
|
214
|
+
|
|
215
|
+
if (!framed) {
|
|
216
|
+
return [
|
|
217
|
+
theme.bold(titleText),
|
|
218
|
+
...(subtitle && height >= 12 ? [muted(subtitle)] : []),
|
|
219
|
+
...body,
|
|
220
|
+
...footer,
|
|
221
|
+
].map((line) => truncateToWidth(line, width, "…"));
|
|
222
|
+
}
|
|
223
|
+
return framePanel(theme, width, titleText, subtitle, body, footer);
|
|
143
224
|
}
|
|
144
225
|
|
|
145
226
|
handleInput(data: string): void {
|
|
@@ -175,7 +256,7 @@ class Picker implements Component, Focusable {
|
|
|
175
256
|
this.cursor = Math.max(0, this.cursor - this.pageRows);
|
|
176
257
|
} else if (kb.matches(data, "tui.select.pageDown")) {
|
|
177
258
|
this.cursor = Math.max(0, Math.min(count - 1, this.cursor + this.pageRows));
|
|
178
|
-
} else
|
|
259
|
+
} else {
|
|
179
260
|
const before = this.search.getValue();
|
|
180
261
|
this.search.handleInput(data);
|
|
181
262
|
if (before !== this.search.getValue()) {
|
|
@@ -194,25 +275,215 @@ class Picker implements Component, Focusable {
|
|
|
194
275
|
invalidate(): void { this.search.invalidate(); }
|
|
195
276
|
}
|
|
196
277
|
|
|
197
|
-
|
|
278
|
+
/** Single-select modal panel with fuzzy search, [saved] badge, Tab details, and paging. Esc returns without a change. */
|
|
279
|
+
export function promptSelectOne(
|
|
280
|
+
ctx: PickerContext, title: string, hint: string, items: SetupItem[], initialValue?: string,
|
|
281
|
+
): Promise<string | undefined> {
|
|
198
282
|
if (ctx.mode !== "tui") {
|
|
199
283
|
ctx.ui.notify("/subagents-setup requires Pi's interactive TUI.", "error");
|
|
200
284
|
return Promise.resolve(undefined);
|
|
201
285
|
}
|
|
202
|
-
return ctx.ui.custom<string | undefined>(
|
|
203
|
-
new Picker(items,
|
|
286
|
+
return ctx.ui.custom<string | undefined>(
|
|
287
|
+
(tui, theme, keybindings, done) => new Picker(items, { title, hint, initialValue }, tui, theme, keybindings, done),
|
|
288
|
+
{ overlay: true, overlayOptions: SETUP_OVERLAY_OPTIONS },
|
|
289
|
+
);
|
|
204
290
|
}
|
|
205
291
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
292
|
+
export interface RoleGridRow {
|
|
293
|
+
role: string;
|
|
294
|
+
purpose?: string;
|
|
295
|
+
model: string;
|
|
296
|
+
/** Effective level, optionally followed by the requested level when clamped. */
|
|
297
|
+
thinking: string;
|
|
298
|
+
thinkingOverridden?: boolean;
|
|
299
|
+
description: string;
|
|
211
300
|
}
|
|
301
|
+
export interface RoleGridFocus { row?: number; column?: number; }
|
|
302
|
+
const GRID_COLUMNS = ["model", "thinking", "reset"] as const;
|
|
303
|
+
export type RoleGridColumn = (typeof GRID_COLUMNS)[number];
|
|
304
|
+
export interface RoleGridChoice { row: number; column: number; role: string; field: RoleGridColumn; }
|
|
212
305
|
|
|
213
|
-
/**
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
306
|
+
/** Standalone modal panel with a role register beside the selected role's settings;
|
|
307
|
+
* narrow terminals stack the same regions. Full effective values are never squeezed into grid cells. */
|
|
308
|
+
class RoleGrid implements Component {
|
|
309
|
+
private row: number;
|
|
310
|
+
private column: number;
|
|
311
|
+
private scrollTop = 0;
|
|
312
|
+
private pageRows = 1;
|
|
313
|
+
private viewport = "";
|
|
314
|
+
private revealField = true;
|
|
315
|
+
|
|
316
|
+
private readonly rows: RoleGridRow[];
|
|
317
|
+
private readonly status: string;
|
|
318
|
+
private readonly tui: TUI;
|
|
319
|
+
private readonly theme: Theme;
|
|
320
|
+
private readonly keybindings: KeybindingsManager;
|
|
321
|
+
private readonly done: (choice: RoleGridChoice | undefined) => void;
|
|
322
|
+
|
|
323
|
+
constructor(
|
|
324
|
+
rows: RoleGridRow[], status: string, focus: RoleGridFocus, tui: TUI, theme: Theme,
|
|
325
|
+
keybindings: KeybindingsManager, done: (choice: RoleGridChoice | undefined) => void,
|
|
326
|
+
) {
|
|
327
|
+
this.rows = rows;
|
|
328
|
+
this.status = status;
|
|
329
|
+
this.tui = tui;
|
|
330
|
+
this.theme = theme;
|
|
331
|
+
this.keybindings = keybindings;
|
|
332
|
+
this.done = done;
|
|
333
|
+
this.row = Math.min(Math.max(0, focus.row ?? 0), Math.max(0, rows.length - 1));
|
|
334
|
+
this.column = Math.min(Math.max(0, focus.column ?? 0), GRID_COLUMNS.length - 1);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
private settings(width: number): { lines: string[]; start: number; end: number } {
|
|
338
|
+
const focused = this.rows[this.row];
|
|
339
|
+
if (!focused) return { lines: [this.theme.fg("dim", "No roles configured.")], start: 0, end: 1 };
|
|
340
|
+
const theme = this.theme;
|
|
341
|
+
const dim = (text: string) => theme.fg("dim", text);
|
|
342
|
+
const field = GRID_COLUMNS[this.column]!;
|
|
343
|
+
const label = field === "thinking" ? "Thinking (reasoning effort)" : field === "reset" ? "Reset" : "Model";
|
|
344
|
+
const lines = [theme.bold(focused.role + " · " + label), ""];
|
|
345
|
+
const starts: number[] = [];
|
|
346
|
+
const setting = (column: number, name: string, hint: string): void => {
|
|
347
|
+
starts.push(lines.length);
|
|
348
|
+
const active = this.column === column;
|
|
349
|
+
lines.push((active ? theme.fg("accent", "› ") : " ") + (active ? theme.underline(theme.bold(name)) : name) + dim(" · " + hint));
|
|
350
|
+
};
|
|
351
|
+
const value = (text: string): void => {
|
|
352
|
+
lines.push(...wrapTextWithAnsi(text, Math.max(1, width - 2)).map((line) => " " + line));
|
|
353
|
+
};
|
|
354
|
+
setting(0, "Model", "Enter edit");
|
|
355
|
+
value(focused.model);
|
|
356
|
+
setting(1, "Thinking", "effective");
|
|
357
|
+
value(focused.thinking);
|
|
358
|
+
lines.push(dim(" Source: " + (focused.thinkingOverridden ? "override *" : "role default")));
|
|
359
|
+
setting(2, "Reset", "Enter restore defaults");
|
|
360
|
+
const end = lines.length;
|
|
361
|
+
lines.push("", ...wrapTextWithAnsi(focused.description, width).map(dim));
|
|
362
|
+
return { lines, start: starts[this.column]!, end: starts[this.column + 1] ?? end };
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
render(width: number): string[] {
|
|
366
|
+
if (width <= 0) return [];
|
|
367
|
+
const theme = this.theme;
|
|
368
|
+
const dim = (text: string) => theme.fg("dim", text);
|
|
369
|
+
const height = setupHeight(this.tui);
|
|
370
|
+
const framed = width >= 20 && height >= 10;
|
|
371
|
+
const innerWidth = framed ? Math.max(1, width - 4) : width;
|
|
372
|
+
const wide = innerWidth >= 72;
|
|
373
|
+
// The two panes close exactly: roleWidth + 3-column gutter + detailWidth = innerWidth.
|
|
374
|
+
const roleWidth = wide ? Math.min(28, Math.max(20, Math.floor(innerWidth * 0.28))) : innerWidth;
|
|
375
|
+
const roles: string[] = [];
|
|
376
|
+
for (const [index, role] of this.rows.entries()) {
|
|
377
|
+
const selected = index === this.row;
|
|
378
|
+
const title = role.purpose && wide ? role.role + " · " + role.purpose : role.role;
|
|
379
|
+
const clippedTitle = truncateToWidth(title, Math.max(1, roleWidth - 2), "…");
|
|
380
|
+
const name = selected ? theme.bold(clippedTitle) : clippedTitle;
|
|
381
|
+
roles.push((selected ? theme.fg("accent", "› ") : " ") + name);
|
|
382
|
+
if (wide) {
|
|
383
|
+
const summary = role.model + " · " + role.thinking + (role.thinkingOverridden ? " *" : "");
|
|
384
|
+
roles.push(dim(" " + truncateToWidth(summary, roleWidth - 2, "…")));
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
const details = this.settings(wide ? innerWidth - roleWidth - 3 : innerWidth);
|
|
388
|
+
const detailOffset = wide ? 0 : roles.length + 1;
|
|
389
|
+
const body: string[] = [];
|
|
390
|
+
const divider = dim(" │ ");
|
|
391
|
+
if (wide) {
|
|
392
|
+
for (let i = 0; i < Math.max(roles.length, details.lines.length); i++) {
|
|
393
|
+
const left = truncateToWidth(roles[i] ?? "", roleWidth, "…");
|
|
394
|
+
body.push(left + " ".repeat(Math.max(0, roleWidth - visibleWidth(left))) + divider + (details.lines[i] ?? ""));
|
|
395
|
+
}
|
|
396
|
+
} else {
|
|
397
|
+
body.push(...roles, "", ...details.lines);
|
|
398
|
+
}
|
|
399
|
+
const footer = innerWidth >= 52
|
|
400
|
+
? [dim("↑/↓ role · ←/→ field · Enter edit · Esc close")]
|
|
401
|
+
: [dim("↑/↓ role · ←/→ field"), dim("Enter edit · Esc close")];
|
|
402
|
+
|
|
403
|
+
const chromeRows = framed ? 4 + footer.length + 1 : 3 + footer.length + 1;
|
|
404
|
+
if (body.length + chromeRows <= height) {
|
|
405
|
+
this.scrollTop = 0;
|
|
406
|
+
this.revealField = true;
|
|
407
|
+
if (!framed) {
|
|
408
|
+
return [theme.bold("Subagents · Roles"), dim(this.status), "", ...body, "", ...footer]
|
|
409
|
+
.map((line) => truncateToWidth(line, width, "…"));
|
|
410
|
+
}
|
|
411
|
+
return framePanel(theme, width, "Subagents · Roles", this.status, body, footer);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// Pin the selected identity while scrolling the existing pane content.
|
|
415
|
+
// The field's complete value and provenance remain reachable by paging.
|
|
416
|
+
const compact = height < 10;
|
|
417
|
+
const header = [details.lines[0]!];
|
|
418
|
+
const controls = innerWidth >= 58
|
|
419
|
+
? [dim("↑/↓ role · ←/→ field · PgUp/Dn scroll · Enter edit · Esc close")]
|
|
420
|
+
: compact
|
|
421
|
+
? [dim("PgUp/Dn scroll · Enter edit · Esc")]
|
|
422
|
+
: [dim("↑/↓ role · ←/→ field"), dim("PgUp/Dn scroll · Enter edit · Esc")];
|
|
423
|
+
const scrollChrome = framed ? 4 + header.length + controls.length + 1 : header.length + controls.length;
|
|
424
|
+
this.pageRows = Math.max(1, height - scrollChrome);
|
|
425
|
+
const viewport = innerWidth + ":" + this.pageRows;
|
|
426
|
+
if (this.revealField || this.viewport !== viewport) {
|
|
427
|
+
const start = detailOffset + details.start;
|
|
428
|
+
const end = detailOffset + details.end;
|
|
429
|
+
// One-line viewports show the value rather than repeating its label.
|
|
430
|
+
this.scrollTop = end - start > this.pageRows ? start + Number(this.pageRows === 1 && end > start + 1)
|
|
431
|
+
: Math.max(0, end - this.pageRows);
|
|
432
|
+
}
|
|
433
|
+
this.viewport = viewport;
|
|
434
|
+
this.revealField = false;
|
|
435
|
+
this.scrollTop = Math.max(0, Math.min(this.scrollTop, body.length - this.pageRows));
|
|
436
|
+
const visibleBody = [...header, ...body.slice(this.scrollTop, this.scrollTop + this.pageRows)];
|
|
437
|
+
if (!framed) {
|
|
438
|
+
return [...visibleBody, ...controls].map((line) => truncateToWidth(line, width, "…"));
|
|
439
|
+
}
|
|
440
|
+
return framePanel(theme, width, "Subagents · Roles", this.status, visibleBody, controls);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
handleInput(data: string): void {
|
|
444
|
+
const kb = this.keybindings;
|
|
445
|
+
const rows = this.rows.length;
|
|
446
|
+
const previousRow = this.row;
|
|
447
|
+
const previousColumn = this.column;
|
|
448
|
+
if (kb.matches(data, "tui.select.pageUp")) {
|
|
449
|
+
this.scrollTop -= this.pageRows;
|
|
450
|
+
} else if (kb.matches(data, "tui.select.pageDown")) {
|
|
451
|
+
this.scrollTop += this.pageRows;
|
|
452
|
+
} else if (kb.matches(data, "tui.select.up")) {
|
|
453
|
+
if (rows) this.row = (this.row - 1 + rows) % rows;
|
|
454
|
+
} else if (kb.matches(data, "tui.select.down")) {
|
|
455
|
+
if (rows) this.row = (this.row + 1) % rows;
|
|
456
|
+
} else if (matchesKey(data, "left") || kb.matches(data, "tui.editor.cursorLeft")) {
|
|
457
|
+
this.column = (this.column - 1 + GRID_COLUMNS.length) % GRID_COLUMNS.length;
|
|
458
|
+
} else if (matchesKey(data, "right") || kb.matches(data, "tui.editor.cursorRight")) {
|
|
459
|
+
this.column = (this.column + 1) % GRID_COLUMNS.length;
|
|
460
|
+
} else if (kb.matches(data, "tui.select.confirm")) {
|
|
461
|
+
const focused = this.rows[this.row];
|
|
462
|
+
if (focused) {
|
|
463
|
+
this.done({ row: this.row, column: this.column, role: focused.role, field: GRID_COLUMNS[this.column]! });
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
} else if (kb.matches(data, "tui.select.cancel")) {
|
|
467
|
+
this.done(undefined);
|
|
468
|
+
return;
|
|
469
|
+
} else return;
|
|
470
|
+
if (this.row !== previousRow || this.column !== previousColumn) this.revealField = true;
|
|
471
|
+
this.tui.requestRender();
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
invalidate(): void {}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/** Role × setting modal panel. Esc closes without a change. */
|
|
478
|
+
export function promptRoleGrid(
|
|
479
|
+
ctx: PickerContext, rows: RoleGridRow[], focus: RoleGridFocus = {}, status = "Changes save immediately",
|
|
480
|
+
): Promise<RoleGridChoice | undefined> {
|
|
481
|
+
if (ctx.mode !== "tui") {
|
|
482
|
+
ctx.ui.notify("/subagents-setup requires Pi's interactive TUI.", "error");
|
|
483
|
+
return Promise.resolve(undefined);
|
|
484
|
+
}
|
|
485
|
+
return ctx.ui.custom<RoleGridChoice | undefined>(
|
|
486
|
+
(tui, theme, keybindings, done) => new RoleGrid(rows, status, focus, tui, theme, keybindings, done),
|
|
487
|
+
{ overlay: true, overlayOptions: SETUP_OVERLAY_OPTIONS },
|
|
488
|
+
);
|
|
218
489
|
}
|