@narumitw/pi-plan-mode 0.35.0 → 0.38.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -2
- package/package.json +4 -1
- package/src/plan-mode.ts +155 -160
- package/src/selector-ui.ts +0 -95
package/README.md
CHANGED
|
@@ -53,7 +53,16 @@ pi -e ./extensions/pi-plan-mode
|
|
|
53
53
|
/plan exit
|
|
54
54
|
```
|
|
55
55
|
|
|
56
|
-
Use `/plan` to enter Plan mode before writing your planning prompt. Use `/plan <prompt>` to enter
|
|
56
|
+
Use `/plan` to enter Plan mode before writing your planning prompt. Use `/plan <prompt>` to enter
|
|
57
|
+
Plan mode and immediately submit `<prompt>` as the first Plan-mode user message. Use `/plan tools`
|
|
58
|
+
to choose which tools are active while Plan mode is enabled; the standard bounded multi-select shows
|
|
59
|
+
10 rows at a time, supports viewport paging, descriptions, and explicit unavailable rows for blocked
|
|
60
|
+
tools. The `plan_mode_question` tool keeps its one-off question/choice dialog because it is a
|
|
61
|
+
model-requested planning interaction, not command-menu navigation. `/plan show` displays the stored
|
|
62
|
+
plan without starting a model turn, `/plan finalize`
|
|
63
|
+
explicitly asks the agent to complete the plan or ask one remaining material question, and
|
|
64
|
+
`/plan implement` hands a stored plan to a normal implementation turn. `show` and `implement` fail
|
|
65
|
+
closed when no plan is stored; `finalize` requires active Plan mode.
|
|
57
66
|
|
|
58
67
|
When Plan mode is active, ask the agent to design the change. The agent may inspect files and run read-only commands, but it should not edit files or execute the implementation. It should explore first, then use structured questions when your preference or a tradeoff materially changes the plan.
|
|
59
68
|
|
|
@@ -63,7 +72,9 @@ Limited `bash` uses a fail-closed policy, including when an extension overrides
|
|
|
63
72
|
|
|
64
73
|
`plan_mode_question` follows Codex's `request_user_input` pattern: the agent can ask 1-3 concise questions, each with meaningful options and a free-form Other path. If you cancel or no interactive UI is available, the agent should ask a concise plain-text question or proceed only with a clearly stated low-risk assumption instead of prematurely producing a final plan.
|
|
65
74
|
|
|
66
|
-
Pi activates tools by tool name. The `/plan tools` selector stores selections by name and
|
|
75
|
+
Pi activates tools by tool name. The `/plan tools` standard selector stores selections by name and
|
|
76
|
+
shows each currently effective tool's source from Pi metadata, such as `built-in`, a user extension
|
|
77
|
+
path, or a project extension path. If an extension overrides a built-in tool with the same name, Pi exposes the effective tool for that name and the selector shows that source.
|
|
67
78
|
|
|
68
79
|
A complete Plan mode answer should appear only after the agent has resolved discoverable facts and high-impact user decisions. The agent must call `plan_mode_complete({ plan })` alone as its final action, passing the complete Markdown plan. The tool rejects empty or whitespace-only plans and plans longer than 50,000 JavaScript characters; it does not truncate. Its visible result contains the full plan, and versioned result details let the extension restore it safely from the active session branch.
|
|
69
80
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@narumitw/pi-plan-mode",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.38.0",
|
|
4
4
|
"description": "Pi extension that adds a Codex-like read-only /plan collaboration mode.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -41,5 +41,8 @@
|
|
|
41
41
|
"type": "git",
|
|
42
42
|
"url": "https://github.com/narumiruna/pi-extensions",
|
|
43
43
|
"directory": "extensions/pi-plan-mode"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@narumitw/pi-tui-kit": "<1"
|
|
44
47
|
}
|
|
45
48
|
}
|
package/src/plan-mode.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ExtensionAPI, ExtensionContext, ToolInfo } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { defineMenu, runMenu } from "@narumitw/pi-tui-kit";
|
|
2
3
|
import {
|
|
3
4
|
normalizePlanModeCompletion,
|
|
4
5
|
PLAN_MODE_COMPLETE_PARAMS,
|
|
@@ -25,7 +26,6 @@ import {
|
|
|
25
26
|
planModeQuestionAnswered,
|
|
26
27
|
planModeQuestionCancelled,
|
|
27
28
|
} from "./question-tool.js";
|
|
28
|
-
import { showPersistentSelector } from "./selector-ui.js";
|
|
29
29
|
import {
|
|
30
30
|
configuredThinkingLevel,
|
|
31
31
|
type PlanModeFixedThinkingLevel,
|
|
@@ -49,7 +49,7 @@ const PLAN_WIDGET_KEY = "plan-mode-plan";
|
|
|
49
49
|
const PROPOSED_PLAN_MESSAGE_TYPE = "proposed-plan";
|
|
50
50
|
const BLOCKED_BUILTIN_TOOLS = new Set(["edit", "write"]);
|
|
51
51
|
const DEFAULT_TOOLS = ["read", "bash", "edit", "write"];
|
|
52
|
-
const
|
|
52
|
+
const TOOL_SELECTOR_VIEWPORT_SIZE = 10;
|
|
53
53
|
|
|
54
54
|
type AgentSettledHandler = (event: unknown, ctx: ExtensionContext) => unknown;
|
|
55
55
|
|
|
@@ -77,10 +77,6 @@ interface ReadyPresentationIntent {
|
|
|
77
77
|
source: PlanCompletionSource;
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
type PlanToolSelectorValue =
|
|
81
|
-
| { kind: "tool"; tool: ToolInfo }
|
|
82
|
-
| { kind: "action"; action: "previous" | "next" | "done" };
|
|
83
|
-
|
|
84
80
|
const PLAN_COMMAND_COMPLETIONS: readonly CommandArgumentCompletion[] = [
|
|
85
81
|
{ value: "show", label: "show", description: "Show the completed plan" },
|
|
86
82
|
{ value: "finalize", label: "finalize", description: "Request a completed plan" },
|
|
@@ -96,6 +92,8 @@ export default function planMode(pi: ExtensionAPI) {
|
|
|
96
92
|
let previousTools: string[] | undefined;
|
|
97
93
|
let readyPresentationIntent: ReadyPresentationIntent | undefined;
|
|
98
94
|
let nextReadyPresentationNonce = 0;
|
|
95
|
+
let menuGeneration = 0;
|
|
96
|
+
let menuController = new AbortController();
|
|
99
97
|
|
|
100
98
|
pi.registerFlag("plan", {
|
|
101
99
|
description: "Start in Codex-like Plan mode",
|
|
@@ -217,6 +215,9 @@ export default function planMode(pi: ExtensionAPI) {
|
|
|
217
215
|
});
|
|
218
216
|
|
|
219
217
|
pi.on("session_start", async (_event, ctx) => {
|
|
218
|
+
menuGeneration += 1;
|
|
219
|
+
menuController.abort(new DOMException("Plan-mode session replaced", "AbortError"));
|
|
220
|
+
menuController = new AbortController();
|
|
220
221
|
readyPresentationIntent = undefined;
|
|
221
222
|
settings = { thinkingLevel: "inherit" };
|
|
222
223
|
const loadedSettings = await readPlanModeSettings();
|
|
@@ -248,6 +249,8 @@ export default function planMode(pi: ExtensionAPI) {
|
|
|
248
249
|
});
|
|
249
250
|
|
|
250
251
|
pi.on("session_shutdown", (_event, ctx) => {
|
|
252
|
+
menuGeneration += 1;
|
|
253
|
+
menuController.abort(new DOMException("Plan-mode session shut down", "AbortError"));
|
|
251
254
|
readyPresentationIntent = undefined;
|
|
252
255
|
captureManualThinkingLevel();
|
|
253
256
|
persistState();
|
|
@@ -529,55 +532,102 @@ export default function planMode(pi: ExtensionAPI) {
|
|
|
529
532
|
ctx.ui.notify(planStatusText(), "info");
|
|
530
533
|
return;
|
|
531
534
|
}
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
"
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
535
|
+
const generation = menuGeneration;
|
|
536
|
+
type Action = "show" | "finalize" | "implement" | "tools" | "stay" | "exit";
|
|
537
|
+
const menu = defineMenu<undefined, "main", Action, ExtensionContext>({
|
|
538
|
+
start: "main",
|
|
539
|
+
screens: {
|
|
540
|
+
main: () => ({
|
|
541
|
+
kind: "actions",
|
|
542
|
+
title: "Plan mode",
|
|
543
|
+
lines: [planStatusText()],
|
|
544
|
+
items: state.latestPlan
|
|
545
|
+
? [
|
|
546
|
+
{ id: "show", label: "Show latest proposed plan", action: "show" },
|
|
547
|
+
{ id: "implement", label: "Implement this plan", action: "implement" },
|
|
548
|
+
{ id: "tools", label: "Configure Plan-mode tools", action: "tools" },
|
|
549
|
+
{ id: "stay", label: "Stay in Plan mode", action: "stay" },
|
|
550
|
+
{ id: "exit", label: "Exit Plan mode", action: "exit" },
|
|
551
|
+
]
|
|
552
|
+
: [
|
|
553
|
+
{ id: "finalize", label: "Request final plan", action: "finalize" },
|
|
554
|
+
{ id: "tools", label: "Configure Plan-mode tools", action: "tools" },
|
|
555
|
+
{ id: "stay", label: "Stay in Plan mode", action: "stay" },
|
|
556
|
+
{ id: "exit", label: "Exit Plan mode", action: "exit" },
|
|
557
|
+
],
|
|
558
|
+
hint: "close",
|
|
559
|
+
}),
|
|
560
|
+
},
|
|
561
|
+
actions: {
|
|
562
|
+
show: async () => {
|
|
563
|
+
showStoredPlan(ctx);
|
|
564
|
+
return { kind: "close" };
|
|
565
|
+
},
|
|
566
|
+
finalize: async () => {
|
|
567
|
+
requestFinalPlan(ctx);
|
|
568
|
+
return { kind: "close" };
|
|
569
|
+
},
|
|
570
|
+
implement: async () => {
|
|
571
|
+
startImplementation(ctx);
|
|
572
|
+
return { kind: "close" };
|
|
573
|
+
},
|
|
574
|
+
tools: async () => {
|
|
575
|
+
await showToolSelector(ctx);
|
|
576
|
+
return { kind: "stay" };
|
|
577
|
+
},
|
|
578
|
+
stay: async () => {
|
|
579
|
+
updateUi(ctx);
|
|
580
|
+
return { kind: "close" };
|
|
581
|
+
},
|
|
582
|
+
exit: async () => {
|
|
583
|
+
exitPlanMode(ctx);
|
|
584
|
+
ctx.ui.notify("Plan mode disabled. Proposed plan discarded.", "info");
|
|
585
|
+
return { kind: "close" };
|
|
586
|
+
},
|
|
587
|
+
},
|
|
588
|
+
});
|
|
589
|
+
await runMenu(ctx, menu, {
|
|
590
|
+
getState: () => undefined,
|
|
591
|
+
signal: menuController.signal,
|
|
592
|
+
isCurrent: () => generation === menuGeneration && !menuController.signal.aborted,
|
|
593
|
+
});
|
|
565
594
|
}
|
|
566
595
|
|
|
567
596
|
async function showPlanReadyMenu(ctx: ExtensionContext) {
|
|
568
|
-
const
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
597
|
+
const generation = menuGeneration;
|
|
598
|
+
type Action = "implement" | "stay" | "exit";
|
|
599
|
+
const menu = defineMenu<undefined, "ready", Action, ExtensionContext>({
|
|
600
|
+
start: "ready",
|
|
601
|
+
screens: {
|
|
602
|
+
ready: () => ({
|
|
603
|
+
kind: "actions",
|
|
604
|
+
title: "Proposed plan ready. What next?",
|
|
605
|
+
items: [
|
|
606
|
+
{ id: "implement", label: "Implement this plan", action: "implement" },
|
|
607
|
+
{ id: "stay", label: "Stay in Plan mode", action: "stay" },
|
|
608
|
+
{ id: "exit", label: "Exit Plan mode", action: "exit" },
|
|
609
|
+
],
|
|
610
|
+
hint: "close",
|
|
611
|
+
}),
|
|
612
|
+
},
|
|
613
|
+
actions: {
|
|
614
|
+
implement: async () => {
|
|
615
|
+
startImplementation(ctx);
|
|
616
|
+
return { kind: "close" };
|
|
617
|
+
},
|
|
618
|
+
stay: async () => ({ kind: "close" }),
|
|
619
|
+
exit: async () => {
|
|
620
|
+
exitPlanMode(ctx);
|
|
621
|
+
ctx.ui.notify("Plan mode disabled. Proposed plan discarded.", "info");
|
|
622
|
+
return { kind: "close" };
|
|
623
|
+
},
|
|
624
|
+
},
|
|
625
|
+
});
|
|
626
|
+
await runMenu(ctx, menu, {
|
|
627
|
+
getState: () => undefined,
|
|
628
|
+
signal: menuController.signal,
|
|
629
|
+
isCurrent: () => generation === menuGeneration && !menuController.signal.aborted,
|
|
630
|
+
});
|
|
581
631
|
}
|
|
582
632
|
|
|
583
633
|
async function showToolSelector(ctx: ExtensionContext) {
|
|
@@ -585,112 +635,66 @@ export default function planMode(pi: ExtensionAPI) {
|
|
|
585
635
|
ctx.ui.notify(formatToolSummary(), "info");
|
|
586
636
|
return;
|
|
587
637
|
}
|
|
588
|
-
|
|
638
|
+
const generation = menuGeneration;
|
|
589
639
|
const tools = selectableTools();
|
|
590
|
-
const
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
640
|
+
const toolById = new Map(tools.map((tool, index) => [`${index}:${tool.name}`, tool]));
|
|
641
|
+
const menu = defineMenu<undefined, "tools", "toggle", ExtensionContext>({
|
|
642
|
+
start: "tools",
|
|
643
|
+
screens: {
|
|
644
|
+
tools: () => {
|
|
645
|
+
const selectedNames = planModeSelectedNames(tools);
|
|
646
|
+
return {
|
|
647
|
+
kind: "multiSelect",
|
|
648
|
+
title: "Plan-mode tools",
|
|
649
|
+
lines: ["Non-built-in tools run at user risk."],
|
|
650
|
+
viewportSize: TOOL_SELECTOR_VIEWPORT_SIZE,
|
|
651
|
+
items: tools.map((tool, index) => {
|
|
652
|
+
const selectable = canSelectToolInPlanMode(tool);
|
|
653
|
+
return {
|
|
654
|
+
id: `${index}:${tool.name}`,
|
|
655
|
+
label: tool.name,
|
|
656
|
+
description: `${toolPolicyLabel(tool)} · ${tool.description}`,
|
|
657
|
+
selected: selectedNames.has(tool.name),
|
|
658
|
+
disabled: !selectable,
|
|
659
|
+
disabledReason: selectable ? undefined : "Blocked by Plan-mode policy",
|
|
660
|
+
};
|
|
661
|
+
}),
|
|
662
|
+
action: "toggle",
|
|
663
|
+
hint: "close",
|
|
664
|
+
doneLabel: "Done",
|
|
665
|
+
};
|
|
666
|
+
},
|
|
616
667
|
},
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
668
|
+
actions: {
|
|
669
|
+
toggle: async ({ itemId, selected }) => {
|
|
670
|
+
const tool = toolById.get(itemId);
|
|
671
|
+
if (!tool || !canSelectToolInPlanMode(tool)) return { kind: "rejected" };
|
|
672
|
+
const names = planModeSelectedNames(tools);
|
|
673
|
+
if (selected) names.add(tool.name);
|
|
674
|
+
else names.delete(tool.name);
|
|
675
|
+
state = {
|
|
676
|
+
...state,
|
|
677
|
+
selectedToolNames: filterAvailableSelectedNames(Array.from(names), tools),
|
|
678
|
+
};
|
|
679
|
+
applyPlanModeTools();
|
|
680
|
+
persistState();
|
|
681
|
+
updateUi(ctx);
|
|
682
|
+
return { kind: "stay" };
|
|
683
|
+
},
|
|
629
684
|
},
|
|
630
|
-
);
|
|
631
|
-
|
|
632
|
-
|
|
685
|
+
});
|
|
686
|
+
await runMenu(ctx, menu, {
|
|
687
|
+
getState: () => undefined,
|
|
688
|
+
signal: menuController.signal,
|
|
689
|
+
isCurrent: () => generation === menuGeneration && !menuController.signal.aborted,
|
|
690
|
+
});
|
|
633
691
|
applyPlanModeTools();
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
async function showDialogToolSelector(ctx: ExtensionContext) {
|
|
639
|
-
let pageIndex = 0;
|
|
640
|
-
while (true) {
|
|
641
|
-
const tools = selectableTools();
|
|
642
|
-
const pageCount = toolSelectorPageCount(tools);
|
|
643
|
-
pageIndex = Math.min(pageIndex, pageCount - 1);
|
|
644
|
-
const pageStart = pageIndex * TOOL_SELECTOR_PAGE_SIZE;
|
|
645
|
-
const pageTools = tools.slice(pageStart, pageStart + TOOL_SELECTOR_PAGE_SIZE);
|
|
646
|
-
const selectedNames = planModeSelectedNames(tools);
|
|
647
|
-
const choices = pageTools.map((tool, index) =>
|
|
648
|
-
formatToolChoice(tool, selectedNames.has(tool.name), pageStart + index),
|
|
649
|
-
);
|
|
650
|
-
const previousChoice = "Previous page";
|
|
651
|
-
const nextChoice = "Next page";
|
|
652
|
-
const doneChoice = "Done";
|
|
653
|
-
const navigationChoices = [
|
|
654
|
-
...(pageIndex > 0 ? [previousChoice] : []),
|
|
655
|
-
...(pageIndex < pageCount - 1 ? [nextChoice] : []),
|
|
656
|
-
doneChoice,
|
|
657
|
-
];
|
|
658
|
-
const choice = await ctx.ui.select(
|
|
659
|
-
`Plan-mode tools (${pageIndex + 1}/${pageCount}). Non-built-in tools run at user risk.`,
|
|
660
|
-
[...choices, ...navigationChoices],
|
|
661
|
-
);
|
|
662
|
-
if (!choice || choice === doneChoice) return;
|
|
663
|
-
if (choice === previousChoice) {
|
|
664
|
-
pageIndex = Math.max(0, pageIndex - 1);
|
|
665
|
-
continue;
|
|
666
|
-
}
|
|
667
|
-
if (choice === nextChoice) {
|
|
668
|
-
pageIndex = Math.min(pageCount - 1, pageIndex + 1);
|
|
669
|
-
continue;
|
|
670
|
-
}
|
|
671
|
-
const tool = pageTools[choices.indexOf(choice)];
|
|
672
|
-
if (!tool) continue;
|
|
673
|
-
if (!canSelectToolInPlanMode(tool)) {
|
|
674
|
-
ctx.ui.notify(`${tool.name} is blocked in Plan mode.`, "warning");
|
|
675
|
-
continue;
|
|
676
|
-
}
|
|
677
|
-
togglePlanModeTool(tool, tools, ctx);
|
|
692
|
+
if (generation === menuGeneration) {
|
|
693
|
+
persistState();
|
|
694
|
+
updateUi(ctx);
|
|
678
695
|
}
|
|
679
696
|
}
|
|
680
697
|
|
|
681
|
-
function togglePlanModeTool(tool: ToolInfo, tools: ToolInfo[], ctx: ExtensionContext) {
|
|
682
|
-
const nextSelectedNames = planModeSelectedNames(tools);
|
|
683
|
-
if (nextSelectedNames.has(tool.name)) nextSelectedNames.delete(tool.name);
|
|
684
|
-
else nextSelectedNames.add(tool.name);
|
|
685
|
-
state = {
|
|
686
|
-
...state,
|
|
687
|
-
selectedToolNames: filterAvailableSelectedNames(Array.from(nextSelectedNames), tools),
|
|
688
|
-
};
|
|
689
|
-
applyPlanModeTools();
|
|
690
|
-
persistState();
|
|
691
|
-
updateUi(ctx);
|
|
692
|
-
}
|
|
693
|
-
|
|
694
698
|
function activatePlanModeTools() {
|
|
695
699
|
previousTools ??= withoutRequiredPlanModeTools(safeGetActiveTools());
|
|
696
700
|
applyPlanModeTools();
|
|
@@ -761,10 +765,6 @@ export default function planMode(pi: ExtensionAPI) {
|
|
|
761
765
|
.sort(compareTools);
|
|
762
766
|
}
|
|
763
767
|
|
|
764
|
-
function toolSelectorPageCount(tools: ToolInfo[]) {
|
|
765
|
-
return Math.max(1, Math.ceil(tools.length / TOOL_SELECTOR_PAGE_SIZE));
|
|
766
|
-
}
|
|
767
|
-
|
|
768
768
|
function safeGetAllTools() {
|
|
769
769
|
try {
|
|
770
770
|
return pi.getAllTools();
|
|
@@ -919,11 +919,6 @@ function compareTools(left: ToolInfo, right: ToolInfo) {
|
|
|
919
919
|
return left.name.localeCompare(right.name);
|
|
920
920
|
}
|
|
921
921
|
|
|
922
|
-
function formatToolChoice(tool: ToolInfo, selected: boolean, index: number) {
|
|
923
|
-
const marker = selected ? "[x]" : "[ ]";
|
|
924
|
-
return `${marker} ${index + 1}. ${tool.name} (${toolPolicyLabel(tool)})`;
|
|
925
|
-
}
|
|
926
|
-
|
|
927
922
|
function toolPolicyLabel(tool: ToolInfo) {
|
|
928
923
|
const policy = classifyPlanModeTool(tool);
|
|
929
924
|
if (policy === "read-only") return "built-in read-only";
|
package/src/selector-ui.ts
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
|
|
3
|
-
export interface PersistentSelectorRow<T> {
|
|
4
|
-
value: T;
|
|
5
|
-
label: string;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export interface PersistentSelectorView<T> {
|
|
9
|
-
title: string;
|
|
10
|
-
rows: PersistentSelectorRow<T>[];
|
|
11
|
-
help?: string;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export type PersistentSelectorActivation = "stay" | "reset" | "close";
|
|
15
|
-
|
|
16
|
-
export async function showPersistentSelector<T>(
|
|
17
|
-
ctx: ExtensionContext,
|
|
18
|
-
getView: () => PersistentSelectorView<T>,
|
|
19
|
-
onActivate: (value: T) => PersistentSelectorActivation,
|
|
20
|
-
): Promise<boolean> {
|
|
21
|
-
const result = await ctx.ui.custom<"closed" | undefined>((tui, theme, keybindings, done) => {
|
|
22
|
-
let selectedIndex = 0;
|
|
23
|
-
const currentView = () => {
|
|
24
|
-
const view = getView();
|
|
25
|
-
selectedIndex = Math.min(selectedIndex, Math.max(0, view.rows.length - 1));
|
|
26
|
-
return view;
|
|
27
|
-
};
|
|
28
|
-
const moveSelection = (delta: number) => {
|
|
29
|
-
const { rows } = currentView();
|
|
30
|
-
if (rows.length === 0) return;
|
|
31
|
-
selectedIndex = (selectedIndex + delta + rows.length) % rows.length;
|
|
32
|
-
};
|
|
33
|
-
const activateSelectedRow = () => {
|
|
34
|
-
const row = currentView().rows[selectedIndex];
|
|
35
|
-
if (!row) return;
|
|
36
|
-
const action = onActivate(row.value);
|
|
37
|
-
if (action === "reset") selectedIndex = 0;
|
|
38
|
-
else if (action === "close") done("closed");
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
return {
|
|
42
|
-
invalidate() {},
|
|
43
|
-
render(width: number) {
|
|
44
|
-
const view = currentView();
|
|
45
|
-
return [
|
|
46
|
-
theme.fg("accent", theme.bold(clipLine(view.title, width))),
|
|
47
|
-
"",
|
|
48
|
-
...view.rows.map((row, index) => {
|
|
49
|
-
const prefix = index === selectedIndex ? "› " : " ";
|
|
50
|
-
const line = clipLine(`${prefix}${row.label}`, width);
|
|
51
|
-
return index === selectedIndex ? theme.fg("accent", line) : line;
|
|
52
|
-
}),
|
|
53
|
-
"",
|
|
54
|
-
theme.fg(
|
|
55
|
-
"dim",
|
|
56
|
-
clipLine(view.help ?? "↑↓ navigate • Enter/Space toggle • Esc close", width),
|
|
57
|
-
),
|
|
58
|
-
];
|
|
59
|
-
},
|
|
60
|
-
handleInput(data: string) {
|
|
61
|
-
if (keybindings.matches(data, "tui.select.up")) {
|
|
62
|
-
moveSelection(-1);
|
|
63
|
-
tui.requestRender();
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
if (keybindings.matches(data, "tui.select.down")) {
|
|
67
|
-
moveSelection(1);
|
|
68
|
-
tui.requestRender();
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
if (keybindings.matches(data, "tui.select.pageUp")) {
|
|
72
|
-
selectedIndex = 0;
|
|
73
|
-
tui.requestRender();
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
if (keybindings.matches(data, "tui.select.pageDown")) {
|
|
77
|
-
selectedIndex = Math.max(0, currentView().rows.length - 1);
|
|
78
|
-
tui.requestRender();
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
if (keybindings.matches(data, "tui.select.confirm") || data === " ") {
|
|
82
|
-
activateSelectedRow();
|
|
83
|
-
tui.requestRender();
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
if (keybindings.matches(data, "tui.select.cancel")) done("closed");
|
|
87
|
-
},
|
|
88
|
-
};
|
|
89
|
-
});
|
|
90
|
-
return result === "closed";
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function clipLine(value: string, width: number) {
|
|
94
|
-
return Array.from(value).slice(0, Math.max(0, width)).join("");
|
|
95
|
-
}
|