@narumitw/pi-plan-mode 0.47.1 → 0.49.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.
@@ -0,0 +1,122 @@
1
+ import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
2
+ import { defineMenu, runMenu } from "@narumitw/pi-tui-kit";
3
+
4
+ export interface PlanLaunchTool {
5
+ name: string;
6
+ description: string;
7
+ searchText: string;
8
+ disabled: boolean;
9
+ disabledReason?: string;
10
+ }
11
+
12
+ interface PlanLaunchMenuOptions {
13
+ statusText: string;
14
+ toolSummary(selectedNames: ReadonlySet<string>): string;
15
+ getSelectedNames(): ReadonlySet<string>;
16
+ tools: readonly PlanLaunchTool[];
17
+ signal: AbortSignal;
18
+ isCurrent(): boolean;
19
+ initialScreen?: "main" | "tools";
20
+ start(signal: AbortSignal): void;
21
+ startWithTools(toolNames: string[], signal: AbortSignal): void;
22
+ settings(signal: AbortSignal): Promise<boolean>;
23
+ }
24
+
25
+ export async function showPlanLaunchMenu(ctx: ExtensionContext, options: PlanLaunchMenuOptions) {
26
+ type Screen = "main" | "tools" | "help";
27
+ type Action = "start" | "toggle-tool" | "start-with-tools" | "settings";
28
+ const selectedNames = new Set(options.getSelectedNames());
29
+ let draftChanged = false;
30
+ const menu = defineMenu<undefined, Screen, Action, ExtensionContext>({
31
+ start: options.initialScreen ?? "main",
32
+ screens: {
33
+ main: () => ({
34
+ kind: "actions",
35
+ title: "Plan mode",
36
+ lines: [options.statusText, options.toolSummary(selectedNames)],
37
+ items: [
38
+ { id: "start", label: "Start Plan mode", action: "start" },
39
+ { id: "tools", label: "Choose tools, then start…", to: "tools" },
40
+ { id: "settings", label: "Settings", action: "settings" },
41
+ { id: "help", label: "How Plan mode works", to: "help" },
42
+ ],
43
+ hint: "close",
44
+ }),
45
+ tools: () => ({
46
+ kind: "multiSelect",
47
+ title: "Choose Plan-mode tools",
48
+ lines: [
49
+ "Changes apply only when you start Plan mode.",
50
+ "Non-built-in tools run at user risk.",
51
+ ],
52
+ enableSearch: true,
53
+ viewportSize: 10,
54
+ items: options.tools.map((tool) => ({
55
+ id: tool.name,
56
+ label: tool.name,
57
+ description: tool.description,
58
+ searchText: tool.searchText,
59
+ selected: selectedNames.has(tool.name),
60
+ disabled: tool.disabled,
61
+ disabledReason: tool.disabledReason,
62
+ })),
63
+ action: "toggle-tool",
64
+ actions: [
65
+ {
66
+ id: "start-with-tools",
67
+ label: "Done — start Plan mode",
68
+ action: "start-with-tools",
69
+ },
70
+ ],
71
+ hint: "back",
72
+ }),
73
+ help: () => ({
74
+ kind: "detail",
75
+ title: "How Plan mode works",
76
+ lines: [
77
+ "Plan mode uses read-only exploration to understand the project before implementation.",
78
+ "The agent can ask important decision questions, then returns a complete implementation-ready plan.",
79
+ "File mutation stays blocked until you explicitly choose to implement the completed plan.",
80
+ ],
81
+ hint: "back",
82
+ }),
83
+ },
84
+ actions: {
85
+ start: async ({ signal }) => {
86
+ if (signal.aborted || !options.isCurrent()) return { kind: "rejected" };
87
+ options.start(signal);
88
+ return { kind: "close" };
89
+ },
90
+ "toggle-tool": async ({ itemId, selected, signal }) => {
91
+ if (signal.aborted || !options.isCurrent()) return { kind: "rejected" };
92
+ const tool = options.tools.find((candidate) => candidate.name === itemId);
93
+ if (!tool || tool.disabled) return { kind: "rejected" };
94
+ if (selected) selectedNames.add(tool.name);
95
+ else selectedNames.delete(tool.name);
96
+ draftChanged = true;
97
+ return { kind: "stay" };
98
+ },
99
+ "start-with-tools": async ({ signal }) => {
100
+ if (signal.aborted || !options.isCurrent()) return { kind: "rejected" };
101
+ options.startWithTools(Array.from(selectedNames), signal);
102
+ return { kind: "close" };
103
+ },
104
+ settings: async ({ signal }) => {
105
+ if (signal.aborted || !options.isCurrent()) return { kind: "rejected" };
106
+ const close = await options.settings(signal);
107
+ if (signal.aborted || !options.isCurrent()) return { kind: "rejected" };
108
+ if (close) return { kind: "close" };
109
+ if (!draftChanged) {
110
+ selectedNames.clear();
111
+ for (const name of options.getSelectedNames()) selectedNames.add(name);
112
+ }
113
+ return { kind: "stay" };
114
+ },
115
+ },
116
+ });
117
+ await runMenu(ctx, menu, {
118
+ getState: () => undefined,
119
+ signal: options.signal,
120
+ isCurrent: options.isCurrent,
121
+ });
122
+ }