pi-subagent-manager 1.1.0 → 1.2.1
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 +4 -4
- package/agents/architect.md +1 -1
- package/agents/coder.md +1 -1
- package/agents/researcher.md +1 -1
- package/agents/reviewer.md +1 -1
- package/agents/tasker.md +1 -1
- package/agents/writer.md +1 -1
- package/docs/default-agents.md +24 -24
- package/package.json +10 -5
- package/src/index.ts +17 -4
- package/src/orch/checklist-integration.ts +33 -9
- package/src/orch/diagnostics.ts +3 -1
- package/src/orch/housekeeping.ts +1 -4
- package/src/orch/inherited-tools.ts +2 -2
- package/src/orch/manager.ts +8 -3
- package/src/orch/prompt.ts +3 -1
- package/src/orch/runtime.ts +28 -15
- package/src/prefs/config.ts +47 -149
- package/src/prefs/models.ts +2 -5
- package/src/prefs/settings.ts +121 -55
- package/src/ui/agent-navigation-editor.ts +20 -10
- package/src/ui/cost-footer.ts +5 -2
- package/src/ui/dialog.ts +52 -59
- package/src/ui/model-picker.ts +17 -5
- package/src/ui/settings-ui.ts +364 -217
- package/src/ui/status-ui.ts +14 -11
- package/src/ui/ui.ts +51 -26
package/src/prefs/settings.ts
CHANGED
|
@@ -29,7 +29,14 @@ export type CostValue = (typeof COST_VALUES)[number];
|
|
|
29
29
|
export const COST_ICONS = ["money", "coins", "wallet"] as const;
|
|
30
30
|
export type CostIcon = (typeof COST_ICONS)[number];
|
|
31
31
|
|
|
32
|
-
export const LOADER_STYLES = [
|
|
32
|
+
export const LOADER_STYLES = [
|
|
33
|
+
"circle",
|
|
34
|
+
"braille",
|
|
35
|
+
"hourglass",
|
|
36
|
+
"ascii",
|
|
37
|
+
"ascii-star",
|
|
38
|
+
"ascii-pulse",
|
|
39
|
+
] as const;
|
|
33
40
|
export type LoaderStyle = (typeof LOADER_STYLES)[number];
|
|
34
41
|
|
|
35
42
|
export const TOOL_FILTERING_MODES = ["allowed", "all-except-blocked", "all"] as const;
|
|
@@ -97,7 +104,23 @@ export const isHousekeepingModel = (value: unknown): value is string =>
|
|
|
97
104
|
typeof value === "string" && (value === "" || HOUSEKEEPING_MODEL.test(value));
|
|
98
105
|
|
|
99
106
|
const KEYS = [
|
|
100
|
-
"maxLevels",
|
|
107
|
+
"maxLevels",
|
|
108
|
+
"maxConcurrent",
|
|
109
|
+
"maxThreads",
|
|
110
|
+
"modelSelection",
|
|
111
|
+
"subagentMode",
|
|
112
|
+
"toolFiltering",
|
|
113
|
+
"widgetMode",
|
|
114
|
+
"costDisplay",
|
|
115
|
+
"costValue",
|
|
116
|
+
"costIcon",
|
|
117
|
+
"nerdFontIcons",
|
|
118
|
+
"loaderStyle",
|
|
119
|
+
"finalRecap",
|
|
120
|
+
"subagentExtensions",
|
|
121
|
+
"checklistIntegration",
|
|
122
|
+
"housekeepingEnabled",
|
|
123
|
+
"housekeepingModel",
|
|
101
124
|
] as const;
|
|
102
125
|
const MAX_LEVELS = 32;
|
|
103
126
|
|
|
@@ -107,9 +130,7 @@ function errorCode(error: unknown): string | undefined {
|
|
|
107
130
|
: undefined;
|
|
108
131
|
}
|
|
109
132
|
|
|
110
|
-
function lstatIfPresent(
|
|
111
|
-
path: string,
|
|
112
|
-
): ReturnType<typeof lstatSync> | undefined {
|
|
133
|
+
function lstatIfPresent(path: string): ReturnType<typeof lstatSync> | undefined {
|
|
113
134
|
try {
|
|
114
135
|
return lstatSync(path);
|
|
115
136
|
} catch (error) {
|
|
@@ -118,9 +139,7 @@ function lstatIfPresent(
|
|
|
118
139
|
}
|
|
119
140
|
}
|
|
120
141
|
|
|
121
|
-
function assertNotSymlinkPath(
|
|
122
|
-
path: string,
|
|
123
|
-
): ReturnType<typeof lstatSync> | undefined {
|
|
142
|
+
function assertNotSymlinkPath(path: string): ReturnType<typeof lstatSync> | undefined {
|
|
124
143
|
const stat = lstatIfPresent(path);
|
|
125
144
|
if (stat?.isSymbolicLink()) throw new Error(`Unsafe symlink path: ${path}`);
|
|
126
145
|
return stat;
|
|
@@ -135,12 +154,20 @@ function positiveSafeInteger(value: unknown): value is number {
|
|
|
135
154
|
}
|
|
136
155
|
|
|
137
156
|
function requirement(key: (typeof KEYS)[number]): string {
|
|
138
|
-
if (key === "subagentExtensions")
|
|
139
|
-
|
|
157
|
+
if (key === "subagentExtensions")
|
|
158
|
+
return "subagentExtensions must be a boolean or an array of absolute file paths";
|
|
159
|
+
if (
|
|
160
|
+
key === "nerdFontIcons" ||
|
|
161
|
+
key === "finalRecap" ||
|
|
162
|
+
key === "checklistIntegration" ||
|
|
163
|
+
key === "housekeepingEnabled"
|
|
164
|
+
)
|
|
165
|
+
return `${key} must be a boolean`;
|
|
140
166
|
if (key === "housekeepingModel") return "housekeepingModel must be empty or provider/model-id";
|
|
141
167
|
if (key === "subagentMode") return "subagentMode must be off, opportunistic or orchestration";
|
|
142
168
|
if (key === "widgetMode") return "widgetMode must be full or minimal";
|
|
143
|
-
if (key === "costDisplay")
|
|
169
|
+
if (key === "costDisplay")
|
|
170
|
+
return "costDisplay must be pi-footer-event, pi-footer-status or pi-status";
|
|
144
171
|
if (key === "costValue") return "costValue must be subagents or total";
|
|
145
172
|
if (key === "costIcon") return "costIcon must be money, coins or wallet";
|
|
146
173
|
if (key === "loaderStyle") return `loaderStyle must be one of: ${LOADER_STYLES.join(", ")}`;
|
|
@@ -171,21 +198,40 @@ function parseSettings(content: string): Partial<ManagerSettings> {
|
|
|
171
198
|
if (typeof record.scopedModelFiltering !== "boolean")
|
|
172
199
|
throw new Error("scopedModelFiltering must be a boolean");
|
|
173
200
|
if (!Object.hasOwn(record, "modelSelection"))
|
|
174
|
-
layer.modelSelection = record.scopedModelFiltering
|
|
201
|
+
layer.modelSelection = record.scopedModelFiltering
|
|
202
|
+
? "pick-first-scoped"
|
|
203
|
+
: "pick-first-available";
|
|
175
204
|
}
|
|
176
205
|
for (const key of KEYS) {
|
|
177
206
|
if (!Object.hasOwn(record, key)) continue;
|
|
178
207
|
const value = record[key];
|
|
179
|
-
if (
|
|
208
|
+
if (
|
|
209
|
+
key === "nerdFontIcons" ||
|
|
210
|
+
key === "finalRecap" ||
|
|
211
|
+
key === "checklistIntegration" ||
|
|
212
|
+
key === "housekeepingEnabled"
|
|
213
|
+
) {
|
|
180
214
|
if (typeof value !== "boolean") throw new Error(requirement(key));
|
|
181
215
|
layer[key] = value;
|
|
182
216
|
continue;
|
|
183
217
|
}
|
|
184
218
|
if (key === "subagentExtensions") {
|
|
185
|
-
if (
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
219
|
+
if (
|
|
220
|
+
typeof value !== "boolean" &&
|
|
221
|
+
!(
|
|
222
|
+
Array.isArray(value) &&
|
|
223
|
+
value.every(
|
|
224
|
+
(entry) =>
|
|
225
|
+
typeof entry === "string" &&
|
|
226
|
+
entry.length > 0 &&
|
|
227
|
+
resolve(entry) === entry &&
|
|
228
|
+
!entry.includes("\0"),
|
|
229
|
+
)
|
|
230
|
+
)
|
|
231
|
+
)
|
|
232
|
+
throw new Error(requirement(key));
|
|
233
|
+
layer.subagentExtensions =
|
|
234
|
+
typeof value === "boolean" ? value : [...new Set(value as string[])].sort();
|
|
189
235
|
continue;
|
|
190
236
|
}
|
|
191
237
|
if (key === "housekeepingModel") {
|
|
@@ -266,8 +312,7 @@ function settingsFileExists(entry: SettingsLayer): boolean {
|
|
|
266
312
|
}
|
|
267
313
|
const stat = assertNotSymlinkPath(entry.filePath);
|
|
268
314
|
if (!stat) return false;
|
|
269
|
-
if (!stat.isFile())
|
|
270
|
-
throw new Error(`Settings must be a regular file: ${entry.filePath}`);
|
|
315
|
+
if (!stat.isFile()) throw new Error(`Settings must be a regular file: ${entry.filePath}`);
|
|
271
316
|
return true;
|
|
272
317
|
}
|
|
273
318
|
|
|
@@ -329,11 +374,7 @@ export function loadManagerSettings(options: ManagerSettingsOptions): {
|
|
|
329
374
|
}
|
|
330
375
|
|
|
331
376
|
function serializedSettings(settings: ManagerSettings): string {
|
|
332
|
-
if (
|
|
333
|
-
settings === null ||
|
|
334
|
-
typeof settings !== "object" ||
|
|
335
|
-
Array.isArray(settings)
|
|
336
|
-
) {
|
|
377
|
+
if (settings === null || typeof settings !== "object" || Array.isArray(settings)) {
|
|
337
378
|
throw new Error("Settings must be a JSON object");
|
|
338
379
|
}
|
|
339
380
|
for (const key of Object.keys(settings)) {
|
|
@@ -379,8 +420,7 @@ function ensureScopeDirectories(directories: string[]): void {
|
|
|
379
420
|
|
|
380
421
|
function assertRegularFileOrAbsent(filePath: string): void {
|
|
381
422
|
const stat = assertNotSymlinkPath(filePath);
|
|
382
|
-
if (stat && !stat.isFile())
|
|
383
|
-
throw new Error(`Settings must be a regular file: ${filePath}`);
|
|
423
|
+
if (stat && !stat.isFile()) throw new Error(`Settings must be a regular file: ${filePath}`);
|
|
384
424
|
}
|
|
385
425
|
|
|
386
426
|
function exclusiveCreateFlags(): number {
|
|
@@ -405,8 +445,7 @@ function writeAtomically(entry: SettingsLayer, content: string, overwrite = true
|
|
|
405
445
|
for (const directory of entry.directories) assertExistingDirectory(directory);
|
|
406
446
|
assertRegularFileOrAbsent(filePath);
|
|
407
447
|
const tempStat = assertNotSymlinkPath(temporary);
|
|
408
|
-
if (!tempStat?.isFile())
|
|
409
|
-
throw new Error(`Settings must be a regular file: ${temporary}`);
|
|
448
|
+
if (!tempStat?.isFile()) throw new Error(`Settings must be a regular file: ${temporary}`);
|
|
410
449
|
if (overwrite) renameSync(temporary, filePath);
|
|
411
450
|
else {
|
|
412
451
|
try {
|
|
@@ -452,24 +491,34 @@ export function saveManagerSettings(options: {
|
|
|
452
491
|
return entry.filePath;
|
|
453
492
|
}
|
|
454
493
|
|
|
455
|
-
function assertExpectedLayers(
|
|
494
|
+
function assertExpectedLayers(
|
|
495
|
+
current: ManagerSettingsLayers,
|
|
496
|
+
expected: ManagerSettingsLayers,
|
|
497
|
+
): void {
|
|
456
498
|
if (
|
|
457
499
|
current.scope !== expected.scope ||
|
|
458
|
-
KEYS.some(
|
|
459
|
-
|
|
500
|
+
KEYS.some(
|
|
501
|
+
(key) =>
|
|
502
|
+
!settingsValueEqual(current.global[key], expected.global[key]) ||
|
|
503
|
+
!settingsValueEqual(current.project[key], expected.project[key]),
|
|
504
|
+
) ||
|
|
460
505
|
current.diagnostics.length !== expected.diagnostics.length ||
|
|
461
506
|
current.diagnostics.some((diagnostic, index) => diagnostic !== expected.diagnostics[index])
|
|
462
507
|
) {
|
|
463
|
-
throw new Error(
|
|
508
|
+
throw new Error(
|
|
509
|
+
"Settings changed on disk. Reopen settings before saving or switching scope to avoid stale inherited values.",
|
|
510
|
+
);
|
|
464
511
|
}
|
|
465
512
|
}
|
|
466
513
|
|
|
467
514
|
/** Persist only values that change the selected scope's inherited settings. */
|
|
468
|
-
export function saveManagerOverrides(
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
515
|
+
export function saveManagerOverrides(
|
|
516
|
+
options: ManagerSettingsOptions & {
|
|
517
|
+
scope: ManagerSaveScope;
|
|
518
|
+
settings: ManagerSettings;
|
|
519
|
+
expectedLayers?: ManagerSettingsLayers;
|
|
520
|
+
},
|
|
521
|
+
): string {
|
|
473
522
|
if (options.scope !== "user" && options.scope !== "project")
|
|
474
523
|
throw new Error("Invalid settings scope");
|
|
475
524
|
if (options.scope === "project" && !options.includeProject)
|
|
@@ -480,29 +529,40 @@ export function saveManagerOverrides(options: ManagerSettingsOptions & {
|
|
|
480
529
|
if (options.expectedLayers) assertExpectedLayers(current, options.expectedLayers);
|
|
481
530
|
if (options.scope === "project") {
|
|
482
531
|
const globalPath = layer(options.agentDir, ["subagent-manager"]).filePath;
|
|
483
|
-
const globalDiagnostics = current.diagnostics.filter((diagnostic) =>
|
|
532
|
+
const globalDiagnostics = current.diagnostics.filter((diagnostic) =>
|
|
533
|
+
diagnostic.startsWith(`${globalPath}:`),
|
|
534
|
+
);
|
|
484
535
|
if (globalDiagnostics.length > 0)
|
|
485
|
-
throw new Error(
|
|
536
|
+
throw new Error(
|
|
537
|
+
`Cannot save project overrides while global settings are invalid. Fix global settings and reopen settings. ${globalDiagnostics.join("; ")}`,
|
|
538
|
+
);
|
|
486
539
|
}
|
|
487
|
-
const inherited =
|
|
488
|
-
|
|
489
|
-
|
|
540
|
+
const inherited =
|
|
541
|
+
options.scope === "user"
|
|
542
|
+
? DEFAULT_MANAGER_SETTINGS
|
|
543
|
+
: { ...DEFAULT_MANAGER_SETTINGS, ...current.global };
|
|
490
544
|
const overrides = Object.fromEntries(
|
|
491
|
-
KEYS.filter((key) => !settingsValueEqual(parsed[key], inherited[key])).map((key) => [
|
|
545
|
+
KEYS.filter((key) => !settingsValueEqual(parsed[key], inherited[key])).map((key) => [
|
|
546
|
+
key,
|
|
547
|
+
parsed[key],
|
|
548
|
+
]),
|
|
492
549
|
);
|
|
493
|
-
const entry =
|
|
494
|
-
|
|
495
|
-
|
|
550
|
+
const entry =
|
|
551
|
+
options.scope === "user"
|
|
552
|
+
? layer(options.agentDir, ["subagent-manager"])
|
|
553
|
+
: layer(options.cwd, [".pi", "agent", "subagent-manager"]);
|
|
496
554
|
ensureScopeDirectories(entry.directories);
|
|
497
555
|
writeAtomically(entry, `${JSON.stringify(overrides, null, 2)}\n`);
|
|
498
556
|
return entry.filePath;
|
|
499
557
|
}
|
|
500
558
|
|
|
501
559
|
/** Confirmed scope transition: create an empty project layer or remove only its file. */
|
|
502
|
-
export function setManagerProjectSettings(
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
560
|
+
export function setManagerProjectSettings(
|
|
561
|
+
options: ManagerSettingsOptions & {
|
|
562
|
+
enabled: boolean;
|
|
563
|
+
expectedLayers?: ManagerSettingsLayers;
|
|
564
|
+
},
|
|
565
|
+
): string {
|
|
506
566
|
if (!options.includeProject) throw new Error("Project settings are not enabled/trusted");
|
|
507
567
|
if (typeof options.enabled !== "boolean") throw new Error("Invalid project settings state");
|
|
508
568
|
if (options.expectedLayers)
|
|
@@ -522,7 +582,10 @@ export function setManagerProjectSettings(options: ManagerSettingsOptions & {
|
|
|
522
582
|
}
|
|
523
583
|
|
|
524
584
|
/** Resolved settings.json path for a scope; the file may not exist. */
|
|
525
|
-
export function managerSettingsPath(
|
|
585
|
+
export function managerSettingsPath(
|
|
586
|
+
options: ManagerSettingsOptions,
|
|
587
|
+
scope: ManagerSaveScope,
|
|
588
|
+
): string {
|
|
526
589
|
if (scope === "project") {
|
|
527
590
|
if (!options.includeProject) throw new Error("Project settings are not enabled/trusted");
|
|
528
591
|
return layer(options.cwd, [".pi", "agent", "subagent-manager"]).filePath;
|
|
@@ -531,16 +594,19 @@ export function managerSettingsPath(options: ManagerSettingsOptions, scope: Mana
|
|
|
531
594
|
}
|
|
532
595
|
|
|
533
596
|
/** Restore a scope's defaults by deleting its settings file (nothing is written). A missing file is not an error. */
|
|
534
|
-
export function deleteManagerSettings(
|
|
597
|
+
export function deleteManagerSettings(
|
|
598
|
+
options: ManagerSettingsOptions & { scope: ManagerSaveScope },
|
|
599
|
+
): {
|
|
535
600
|
filePath: string;
|
|
536
601
|
deleted: boolean;
|
|
537
602
|
} {
|
|
538
603
|
if (options.scope !== "user" && options.scope !== "project")
|
|
539
604
|
throw new Error("Invalid settings scope");
|
|
540
605
|
const filePath = managerSettingsPath(options, options.scope);
|
|
541
|
-
const entry =
|
|
542
|
-
|
|
543
|
-
|
|
606
|
+
const entry =
|
|
607
|
+
options.scope === "project"
|
|
608
|
+
? layer(options.cwd, [".pi", "agent", "subagent-manager"])
|
|
609
|
+
: layer(options.agentDir, ["subagent-manager"]);
|
|
544
610
|
// Malformed JSON is removable; symlinks and non-files are refused by settingsFileExists.
|
|
545
611
|
if (!settingsFileExists(entry)) return { filePath, deleted: false };
|
|
546
612
|
unlinkSync(entry.filePath);
|
|
@@ -92,8 +92,11 @@ export class AgentNavigationEditor extends CustomEditor {
|
|
|
92
92
|
|
|
93
93
|
override handleInput(data: string): void {
|
|
94
94
|
const outer = this.dispatchDepth === 0;
|
|
95
|
-
const direction = this.ordinaryArrow(data, "left")
|
|
96
|
-
|
|
95
|
+
const direction = this.ordinaryArrow(data, "left")
|
|
96
|
+
? "left"
|
|
97
|
+
: this.ordinaryArrow(data, "right")
|
|
98
|
+
? "right"
|
|
99
|
+
: undefined;
|
|
97
100
|
if (outer && (!direction || (this.pending && this.pending !== direction))) this.inputEpoch++;
|
|
98
101
|
const previousPaste = this.pastePending;
|
|
99
102
|
const wasPaste = previousPaste || data.includes("\x1b[200~");
|
|
@@ -105,10 +108,12 @@ export class AgentNavigationEditor extends CustomEditor {
|
|
|
105
108
|
const cursor = this.getCursor();
|
|
106
109
|
const text = this.getText();
|
|
107
110
|
const lines = text.split("\n");
|
|
108
|
-
const atBoundary =
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
111
|
+
const atBoundary =
|
|
112
|
+
direction === "left"
|
|
113
|
+
? cursor.line === 0 && cursor.col === 0
|
|
114
|
+
: direction === "right" &&
|
|
115
|
+
cursor.line === lines.length - 1 &&
|
|
116
|
+
cursor.col === lines.at(-1)!.length;
|
|
112
117
|
const eligible =
|
|
113
118
|
outer &&
|
|
114
119
|
direction !== undefined &&
|
|
@@ -180,7 +185,8 @@ export class AgentNavigationEditor extends CustomEditor {
|
|
|
180
185
|
const action = (): "expand" | "tree" | "collapse" | undefined => {
|
|
181
186
|
if (direction === "right")
|
|
182
187
|
return this.navigation.collapseWidget && (this.navigation.canCollapse?.() ?? false)
|
|
183
|
-
? "collapse"
|
|
188
|
+
? "collapse"
|
|
189
|
+
: undefined;
|
|
184
190
|
if (this.navigation.expandWidget && (this.navigation.canExpand?.() ?? false)) return "expand";
|
|
185
191
|
return this.navigation.canOpen() ? "tree" : undefined;
|
|
186
192
|
};
|
|
@@ -207,9 +213,13 @@ export class AgentNavigationEditor extends CustomEditor {
|
|
|
207
213
|
try {
|
|
208
214
|
// Call within the checked microtask: an extra Promise.then would leave
|
|
209
215
|
// a gap in which root ownership could change after validation.
|
|
210
|
-
void Promise.resolve(
|
|
211
|
-
|
|
212
|
-
|
|
216
|
+
void Promise.resolve(
|
|
217
|
+
chosen === "tree"
|
|
218
|
+
? this.navigation.openTree()
|
|
219
|
+
: chosen === "expand"
|
|
220
|
+
? this.navigation.expandWidget!()
|
|
221
|
+
: this.navigation.collapseWidget!(),
|
|
222
|
+
)
|
|
213
223
|
.catch((error: unknown) => this.navigation.onError?.(error))
|
|
214
224
|
.finally(() => {
|
|
215
225
|
this.pending = undefined;
|
package/src/ui/cost-footer.ts
CHANGED
|
@@ -103,7 +103,9 @@ export class CostFooterController {
|
|
|
103
103
|
return;
|
|
104
104
|
}
|
|
105
105
|
if (this.subagentCost() <= 0 || !ctx.hasUI || ctx.mode !== "tui") return;
|
|
106
|
-
this.requestRender = installCostFooter(ctx, this.subagentCost, (mainCost) =>
|
|
106
|
+
this.requestRender = installCostFooter(ctx, this.subagentCost, (mainCost) =>
|
|
107
|
+
this.formatCost(mainCost),
|
|
108
|
+
);
|
|
107
109
|
this.installed = true;
|
|
108
110
|
}
|
|
109
111
|
|
|
@@ -176,7 +178,8 @@ export function installCostFooter(
|
|
|
176
178
|
const subscription =
|
|
177
179
|
ctx.model &&
|
|
178
180
|
(ctx.model.provider === "kimi-coding" || ctx.modelRegistry?.isUsingOAuth(ctx.model));
|
|
179
|
-
const costText =
|
|
181
|
+
const costText =
|
|
182
|
+
displayCost?.(totals.cost) ?? `$${(totals.cost + subagentCost()).toFixed(3)}`;
|
|
180
183
|
parts.push(`${costText}${subscription ? " (sub)" : ""}`);
|
|
181
184
|
const context = ctx.getContextUsage();
|
|
182
185
|
if (context) {
|
package/src/ui/dialog.ts
CHANGED
|
@@ -36,6 +36,8 @@ export interface DialogRow {
|
|
|
36
36
|
/** Trusted formatter for a fixed three-cell middle column, styled at render time. */
|
|
37
37
|
renderIndicators?: (theme: Theme) => string;
|
|
38
38
|
value?: string;
|
|
39
|
+
/** Optional second line beneath the value, clipped to the same column. */
|
|
40
|
+
valueDetail?: string;
|
|
39
41
|
valueColor?: Parameters<Theme["fg"]>[0];
|
|
40
42
|
help?: string;
|
|
41
43
|
}
|
|
@@ -72,18 +74,14 @@ export function frameDialog(
|
|
|
72
74
|
const clipped = truncateToWidth(text, Math.max(0, columns), "", true);
|
|
73
75
|
return clipped + " ".repeat(Math.max(0, columns - visibleWidth(clipped)));
|
|
74
76
|
};
|
|
75
|
-
if (width < 4 || height < 4)
|
|
76
|
-
return [fit(theme.fg("muted", dialogText(title)), width)];
|
|
77
|
+
if (width < 4 || height < 4) return [fit(theme.fg("muted", dialogText(title)), width)];
|
|
77
78
|
const inner = width - 2;
|
|
78
79
|
const line = (text: string) =>
|
|
79
80
|
theme.fg("border", "│") + fit(text, inner) + theme.fg("border", "│");
|
|
80
81
|
const heading = truncateToWidth(` ${dialogText(title)} `, inner, "");
|
|
81
82
|
const top = theme.fg(
|
|
82
83
|
"border",
|
|
83
|
-
"╭" +
|
|
84
|
-
heading +
|
|
85
|
-
"─".repeat(Math.max(0, inner - visibleWidth(heading))) +
|
|
86
|
-
"╮",
|
|
84
|
+
"╭" + heading + "─".repeat(Math.max(0, inner - visibleWidth(heading))) + "╮",
|
|
87
85
|
);
|
|
88
86
|
// Pad to the full frame so centered overlays keep the same bounds between views.
|
|
89
87
|
const budget = height - 4;
|
|
@@ -176,8 +174,7 @@ export function dialogEditor(
|
|
|
176
174
|
prefill: string,
|
|
177
175
|
): Promise<string | undefined> {
|
|
178
176
|
return ctx.ui.custom(
|
|
179
|
-
(host, theme, _keys, done) =>
|
|
180
|
-
new DialogEditor(host, theme, title, prefill, done),
|
|
177
|
+
(host, theme, _keys, done) => new DialogEditor(host, theme, title, prefill, done),
|
|
181
178
|
DIALOG_OPTIONS,
|
|
182
179
|
);
|
|
183
180
|
}
|
|
@@ -207,23 +204,20 @@ export class DialogMenu {
|
|
|
207
204
|
handleInput(data: string): void {
|
|
208
205
|
const keys = getKeybindings();
|
|
209
206
|
if (keys.matches(data, "tui.select.cancel")) return this.done(undefined);
|
|
210
|
-
if (
|
|
207
|
+
if (
|
|
208
|
+
keys.matches(data, "tui.select.confirm") ||
|
|
209
|
+
(this.spaceSelect && matchesKey(data, Key.space))
|
|
210
|
+
)
|
|
211
211
|
return this.done(this.getSelectedId());
|
|
212
|
-
if (this.saveId && matchesKey(data, Key.ctrl("s")))
|
|
213
|
-
return this.done(this.saveId);
|
|
212
|
+
if (this.saveId && matchesKey(data, Key.ctrl("s"))) return this.done(this.saveId);
|
|
214
213
|
let offset = 0;
|
|
215
214
|
if (keys.matches(data, "tui.select.up")) offset = -1;
|
|
216
|
-
else if (keys.matches(data, "tui.select.down") || matchesKey(data, Key.tab))
|
|
217
|
-
offset = 1;
|
|
215
|
+
else if (keys.matches(data, "tui.select.down") || matchesKey(data, Key.tab)) offset = 1;
|
|
218
216
|
else if (matchesKey(data, Key.pageUp)) offset = -this.viewport;
|
|
219
217
|
else if (matchesKey(data, Key.pageDown)) offset = this.viewport;
|
|
220
218
|
else if (matchesKey(data, Key.home)) this.selected = 0;
|
|
221
|
-
else if (matchesKey(data, Key.end))
|
|
222
|
-
|
|
223
|
-
this.selected = Math.max(
|
|
224
|
-
0,
|
|
225
|
-
Math.min(this.rows.length - 1, this.selected + offset),
|
|
226
|
-
);
|
|
219
|
+
else if (matchesKey(data, Key.end)) this.selected = Math.max(0, this.rows.length - 1);
|
|
220
|
+
this.selected = Math.max(0, Math.min(this.rows.length - 1, this.selected + offset));
|
|
227
221
|
this.host.requestRender();
|
|
228
222
|
}
|
|
229
223
|
invalidate(): void {}
|
|
@@ -239,13 +233,11 @@ export class DialogMenu {
|
|
|
239
233
|
.split(/\r\n?|\n/)
|
|
240
234
|
.flatMap((line) => wrapTextWithAnsi(dialogText(line), Math.max(1, inner - 1)))
|
|
241
235
|
.slice(0, Math.max(1, height - 7 - legend.length));
|
|
242
|
-
|
|
236
|
+
const rowHeight = this.rows.some((row) => row.valueDetail !== undefined) ? 2 : 1;
|
|
237
|
+
this.viewport = Math.max(1, Math.floor((height - 6 - help.length - legend.length) / rowHeight));
|
|
243
238
|
const start = Math.max(
|
|
244
239
|
0,
|
|
245
|
-
Math.min(
|
|
246
|
-
this.selected - this.viewport + 1,
|
|
247
|
-
this.rows.length - this.viewport,
|
|
248
|
-
),
|
|
240
|
+
Math.min(this.selected - this.viewport + 1, this.rows.length - this.viewport),
|
|
249
241
|
);
|
|
250
242
|
const hasIndicators = this.rows.some((row) => row.renderIndicators);
|
|
251
243
|
const indicatorWidth = 3;
|
|
@@ -260,9 +252,9 @@ export class DialogMenu {
|
|
|
260
252
|
"dim",
|
|
261
253
|
` ${this.rows.length ? this.selected + 1 : 0}/${this.rows.length} · Field / ${hasIndicators ? "Saved / " : ""}Value`,
|
|
262
254
|
),
|
|
263
|
-
...this.rows.slice(start, start + this.viewport).
|
|
255
|
+
...this.rows.slice(start, start + this.viewport).flatMap((row, i) => {
|
|
264
256
|
const selected = start + i === this.selected;
|
|
265
|
-
const color = (text: string) => selected ? this.theme.fg("accent", text) : text;
|
|
257
|
+
const color = (text: string) => (selected ? this.theme.fg("accent", text) : text);
|
|
266
258
|
const plainLabel = dialogText(row.label);
|
|
267
259
|
const label = truncateToWidth(
|
|
268
260
|
row.renderLabel ? row.renderLabel(plainLabel, this.theme) : plainLabel,
|
|
@@ -271,14 +263,27 @@ export class DialogMenu {
|
|
|
271
263
|
true,
|
|
272
264
|
);
|
|
273
265
|
const padding = " ".repeat(Math.max(0, labelWidth - visibleWidth(label)));
|
|
274
|
-
const indicators = truncateToWidth(
|
|
266
|
+
const indicators = truncateToWidth(
|
|
267
|
+
row.renderIndicators?.(this.theme) ?? "",
|
|
268
|
+
indicatorWidth,
|
|
269
|
+
"",
|
|
270
|
+
true,
|
|
271
|
+
);
|
|
275
272
|
const indicatorPadding = " ".repeat(Math.max(0, indicatorWidth - visibleWidth(indicators)));
|
|
276
273
|
const value = dialogText(row.value ?? "");
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
274
|
+
const primary =
|
|
275
|
+
color(`${selected ? "›" : " "} `) +
|
|
276
|
+
(row.renderLabel ? label : color(label)) +
|
|
277
|
+
color(`${padding} │ `) +
|
|
278
|
+
(hasIndicators ? indicators + color(`${indicatorPadding} │ `) : "") +
|
|
279
|
+
(row.valueColor ? this.theme.fg(row.valueColor, value) : color(value));
|
|
280
|
+
if (rowHeight === 1) return [primary];
|
|
281
|
+
const valueStart = 2 + labelWidth + 3 + (hasIndicators ? indicatorWidth + 3 : 0);
|
|
282
|
+
const detail = truncateToWidth(
|
|
283
|
+
dialogText(row.valueDetail ?? ""),
|
|
284
|
+
Math.max(0, inner - valueStart),
|
|
285
|
+
);
|
|
286
|
+
return [primary, " ".repeat(valueStart) + this.theme.fg("muted", detail)];
|
|
282
287
|
}),
|
|
283
288
|
"",
|
|
284
289
|
...help.map((line) => this.theme.fg("muted", ` ${line}`)),
|
|
@@ -289,14 +294,7 @@ export class DialogMenu {
|
|
|
289
294
|
while (body.length < budget) body.push("");
|
|
290
295
|
body.push(...legend);
|
|
291
296
|
}
|
|
292
|
-
return frameDialog(
|
|
293
|
-
this.theme,
|
|
294
|
-
width,
|
|
295
|
-
height,
|
|
296
|
-
this.title,
|
|
297
|
-
body,
|
|
298
|
-
this.footer,
|
|
299
|
-
);
|
|
297
|
+
return frameDialog(this.theme, width, height, this.title, body, this.footer);
|
|
300
298
|
}
|
|
301
299
|
}
|
|
302
300
|
|
|
@@ -313,7 +311,13 @@ export async function dialogMenu(
|
|
|
313
311
|
ctx: ExtensionContext,
|
|
314
312
|
title: string,
|
|
315
313
|
rows: DialogRow[],
|
|
316
|
-
options: {
|
|
314
|
+
options: {
|
|
315
|
+
selectedId?: string;
|
|
316
|
+
footer?: string;
|
|
317
|
+
saveId?: string;
|
|
318
|
+
renderLegend?: (theme: Theme) => string;
|
|
319
|
+
spaceSelect?: boolean;
|
|
320
|
+
} = {},
|
|
317
321
|
): Promise<string | undefined> {
|
|
318
322
|
return ctx.ui.custom(
|
|
319
323
|
(host, theme, _keys, done) =>
|
|
@@ -399,9 +403,7 @@ const dialogScopes = new WeakSet<ExtensionContext>();
|
|
|
399
403
|
|
|
400
404
|
function isPromise<T>(value: T | Promise<T>): value is Promise<T> {
|
|
401
405
|
return (
|
|
402
|
-
typeof value === "object" &&
|
|
403
|
-
value !== null &&
|
|
404
|
-
typeof (value as Promise<T>).then === "function"
|
|
406
|
+
typeof value === "object" && value !== null && typeof (value as Promise<T>).then === "function"
|
|
405
407
|
);
|
|
406
408
|
}
|
|
407
409
|
|
|
@@ -470,16 +472,14 @@ export class DialogSession {
|
|
|
470
472
|
const resolveOnce = (value: T) => {
|
|
471
473
|
if (settled) return;
|
|
472
474
|
settled = true;
|
|
473
|
-
if (this.pending?.generation === generation)
|
|
474
|
-
this.pending.settled = true;
|
|
475
|
+
if (this.pending?.generation === generation) this.pending.settled = true;
|
|
475
476
|
resolve(value);
|
|
476
477
|
};
|
|
477
478
|
const rejectOnce = (error: unknown) => {
|
|
478
479
|
if (settled) return;
|
|
479
480
|
settled = true;
|
|
480
481
|
this.inputLocked = true;
|
|
481
|
-
if (this.pending?.generation === generation)
|
|
482
|
-
this.pending.settled = true;
|
|
482
|
+
if (this.pending?.generation === generation) this.pending.settled = true;
|
|
483
483
|
reject(error);
|
|
484
484
|
};
|
|
485
485
|
this.pending = {
|
|
@@ -508,8 +508,7 @@ export class DialogSession {
|
|
|
508
508
|
void produced.then(
|
|
509
509
|
(component) => adopt(component),
|
|
510
510
|
(error: unknown) => {
|
|
511
|
-
if (settled || this.closed || generation !== this.generation)
|
|
512
|
-
return;
|
|
511
|
+
if (settled || this.closed || generation !== this.generation) return;
|
|
513
512
|
rejectOnce(error);
|
|
514
513
|
},
|
|
515
514
|
);
|
|
@@ -574,8 +573,7 @@ export class DialogSession {
|
|
|
574
573
|
return;
|
|
575
574
|
}
|
|
576
575
|
this.replaceChild(component);
|
|
577
|
-
this.inputLocked =
|
|
578
|
-
this.pending?.generation === generation ? this.pending.settled : true;
|
|
576
|
+
this.inputLocked = this.pending?.generation === generation ? this.pending.settled : true;
|
|
579
577
|
this.host.requestRender();
|
|
580
578
|
}
|
|
581
579
|
|
|
@@ -603,18 +601,13 @@ export class DialogSession {
|
|
|
603
601
|
}
|
|
604
602
|
}
|
|
605
603
|
|
|
606
|
-
export function scopeDialogContext<T extends ExtensionContext>(
|
|
607
|
-
ctx: T,
|
|
608
|
-
session: DialogSession,
|
|
609
|
-
): T {
|
|
604
|
+
export function scopeDialogContext<T extends ExtensionContext>(ctx: T, session: DialogSession): T {
|
|
610
605
|
const source = ctx.ui;
|
|
611
606
|
const originalCustom = source.custom.bind(source);
|
|
612
607
|
const ui = Object.create(source) as ExtensionUIContext;
|
|
613
608
|
ui.custom = ((factory, options) => {
|
|
614
609
|
if (options?.overlay) return session.mount(factory);
|
|
615
|
-
return originalCustom(factory, options).finally(() =>
|
|
616
|
-
session.restoreFocus(),
|
|
617
|
-
);
|
|
610
|
+
return originalCustom(factory, options).finally(() => session.restoreFocus());
|
|
618
611
|
}) as ExtensionUIContext["custom"];
|
|
619
612
|
// Pi exposes ctx.ui as a getter; assignment cannot shadow an inherited accessor.
|
|
620
613
|
// Define an own property while keeping the remaining context getters live.
|
package/src/ui/model-picker.ts
CHANGED
|
@@ -524,7 +524,9 @@ export class HousekeepingModelPickerComponent extends Container {
|
|
|
524
524
|
(model) => modelSearchText(model.name),
|
|
525
525
|
],
|
|
526
526
|
});
|
|
527
|
-
this.scopedIdentities = new Set(
|
|
527
|
+
this.scopedIdentities = new Set(
|
|
528
|
+
options.scopedModels.map((entry) => modelIdentity(entry.model)),
|
|
529
|
+
);
|
|
528
530
|
this.current = options.current ?? "";
|
|
529
531
|
this.onDone = options.onDone;
|
|
530
532
|
this.onCancel = options.onCancel;
|
|
@@ -554,7 +556,10 @@ export class HousekeepingModelPickerComponent extends Container {
|
|
|
554
556
|
const hint = this.scopedIdentities.size
|
|
555
557
|
? "Scoped models first, then every other available model. Type to search."
|
|
556
558
|
: "No scoped models in this session; all available models are listed. Type to search.";
|
|
557
|
-
const header = [
|
|
559
|
+
const header = [
|
|
560
|
+
truncateToWidth(this.theme.fg("muted", hint), inner, ""),
|
|
561
|
+
...this.searchInput.render(inner),
|
|
562
|
+
];
|
|
558
563
|
const budget = Math.max(1, height - 4 - header.length);
|
|
559
564
|
const display: (HousekeepingRow | string)[] = [];
|
|
560
565
|
let previous: string | undefined;
|
|
@@ -564,7 +569,10 @@ export class HousekeepingModelPickerComponent extends Container {
|
|
|
564
569
|
display.push(row);
|
|
565
570
|
}
|
|
566
571
|
const selectedRow = this.rows[this.selected];
|
|
567
|
-
const index = Math.max(
|
|
572
|
+
const index = Math.max(
|
|
573
|
+
0,
|
|
574
|
+
display.findIndex((row) => row === selectedRow),
|
|
575
|
+
);
|
|
568
576
|
const start = Math.max(0, Math.min(index - budget + 1, display.length - budget));
|
|
569
577
|
const lines = display.slice(start, start + budget).map((row) => {
|
|
570
578
|
if (typeof row === "string") return this.theme.fg("muted", row);
|
|
@@ -642,8 +650,12 @@ export class HousekeepingModelPickerComponent extends Container {
|
|
|
642
650
|
},
|
|
643
651
|
]
|
|
644
652
|
: []),
|
|
645
|
-
...matches
|
|
646
|
-
|
|
653
|
+
...matches
|
|
654
|
+
.filter((m) => this.scopedIdentities.has(modelIdentity(m)))
|
|
655
|
+
.map((m) => row(m, SCOPED_GROUP)),
|
|
656
|
+
...matches
|
|
657
|
+
.filter((m) => !this.scopedIdentities.has(modelIdentity(m)))
|
|
658
|
+
.map((m) => row(m, OTHER_GROUP)),
|
|
647
659
|
];
|
|
648
660
|
const previous = this.rows[this.selected]?.value;
|
|
649
661
|
this.rows = rows;
|