@narumitw/pi-plan-mode 0.57.1 → 0.58.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 +68 -4
- package/dist/chunks/{chunk-57OBPS7P.js → chunk-HF7ZEYIC.js} +169 -164
- package/dist/chunks/chunk-HF7ZEYIC.js.map +7 -0
- package/dist/chunks/{interactive-ui-N2ZKBVNE.js → interactive-ui-UK7IGPOE.js} +312 -32
- package/dist/chunks/interactive-ui-UK7IGPOE.js.map +7 -0
- package/dist/index.ts +670 -212
- package/dist/index.ts.map +4 -4
- package/docs/settings.md +26 -2
- package/package.json +50 -51
- package/src/active-implementation-menu.ts +57 -60
- package/src/command.ts +22 -21
- package/src/completion-tool.ts +53 -56
- package/src/extension-runtime.ts +5 -5
- package/src/finalization-request.ts +40 -44
- package/src/fresh-handoff-coordinator.ts +52 -0
- package/src/fresh-implementation.ts +312 -205
- package/src/implementation-models.ts +43 -0
- package/src/implementation-retention.ts +109 -123
- package/src/message-transform.ts +218 -252
- package/src/mode-contract.ts +46 -49
- package/src/plan-action-controller.ts +139 -89
- package/src/plan-action-menus.ts +414 -177
- package/src/plan-export-controller.ts +23 -28
- package/src/plan-export-screen.ts +13 -13
- package/src/plan-export.ts +85 -102
- package/src/plan-launch-menu.ts +119 -119
- package/src/plan-mode.ts +2027 -1675
- package/src/presentation.ts +84 -108
- package/src/prompt.ts +1 -1
- package/src/question-tool.ts +239 -267
- package/src/required-tools.ts +15 -19
- package/src/saved-plan-menu.ts +82 -84
- package/src/saved-plan-preflight.ts +29 -33
- package/src/settings-menu.ts +489 -377
- package/src/settings.ts +403 -382
- package/src/state.ts +177 -146
- package/src/tool-policy.ts +634 -699
- package/src/tool-selection.ts +39 -44
- package/src/workflow-mutex.ts +66 -66
- package/dist/chunks/chunk-57OBPS7P.js.map +0 -7
- package/dist/chunks/interactive-ui-N2ZKBVNE.js.map +0 -7
package/src/settings-menu.ts
CHANGED
|
@@ -1,430 +1,542 @@
|
|
|
1
1
|
import type { ExtensionContext, ToolInfo } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import { defineMenu, type RunMenuResult, runMenu } from "@narumitw/pi-tui-kit";
|
|
2
|
+
import { defineMenu, type RunMenuResult, runMenu, sanitizeTerminalText } from "@narumitw/pi-tui-kit";
|
|
3
3
|
import { PLAN_MODE_COMPLETE_TOOL_NAME } from "./completion-tool.js";
|
|
4
|
+
import {
|
|
5
|
+
type AvailableImplementationModel,
|
|
6
|
+
findAvailableImplementationModel,
|
|
7
|
+
type ImplementationModelOverride,
|
|
8
|
+
snapshotAvailableImplementationModels,
|
|
9
|
+
} from "./implementation-models.js";
|
|
4
10
|
import { retentionLabel } from "./implementation-retention.js";
|
|
5
11
|
import { planExportDestination } from "./plan-export.js";
|
|
6
12
|
import { PLAN_MODE_QUESTION_TOOL_NAME } from "./question-tool.js";
|
|
7
13
|
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
14
|
+
configuredImplementationModel,
|
|
15
|
+
configuredImplementationPlanRetention,
|
|
16
|
+
configuredImplementationThinkingLevel,
|
|
17
|
+
configuredPlanExportPath,
|
|
18
|
+
configuredPlanModeToggleShortcut,
|
|
19
|
+
IMPLEMENTATION_PLAN_RETENTIONS,
|
|
20
|
+
IMPLEMENTATION_THINKING_LEVELS,
|
|
21
|
+
normalizeKeyId,
|
|
22
|
+
PLAN_MODE_THINKING_LEVELS,
|
|
23
|
+
type PlanModeSettings,
|
|
24
|
+
type PlanModeSettingsLoadResult,
|
|
25
|
+
type PlanModeSettingsPatch,
|
|
26
|
+
planModeSettingsPath,
|
|
27
|
+
readPlanModeSettings,
|
|
28
|
+
type UpdatePlanModeSettingsOptions,
|
|
29
|
+
updatePlanModeSettings,
|
|
21
30
|
} from "./settings.js";
|
|
22
31
|
import { canSelectToolInPlanMode } from "./tool-policy.js";
|
|
23
32
|
import { defaultPlanModeToolNames, toolPolicyLabel } from "./tool-selection.js";
|
|
24
33
|
|
|
25
34
|
interface SettingsMenuState {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
35
|
+
kind: "valid" | "invalid";
|
|
36
|
+
settings: PlanModeSettings;
|
|
37
|
+
notice?: string;
|
|
38
|
+
reason?: string;
|
|
30
39
|
}
|
|
31
40
|
|
|
32
41
|
export interface PlanModeSettingsMenuOptions {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
options?: UpdatePlanModeSettingsOptions,
|
|
43
|
-
) => Promise<PlanModeSettings>;
|
|
44
|
-
onSaved(settings: PlanModeSettings): void;
|
|
42
|
+
tools: readonly ToolInfo[];
|
|
43
|
+
activeToolNames?: readonly string[];
|
|
44
|
+
signal: AbortSignal;
|
|
45
|
+
isCurrent(): boolean;
|
|
46
|
+
settingsPath?: string;
|
|
47
|
+
legacySettingsPath?: string;
|
|
48
|
+
readSettings?: (settingsPath?: string) => Promise<PlanModeSettingsLoadResult>;
|
|
49
|
+
updateSettings?: (patch: PlanModeSettingsPatch, options?: UpdatePlanModeSettingsOptions) => Promise<PlanModeSettings>;
|
|
50
|
+
onSaved(settings: PlanModeSettings): void;
|
|
45
51
|
}
|
|
46
52
|
|
|
47
|
-
type Screen = "settings" | "tools" | "export" | "shortcut";
|
|
53
|
+
type Screen = "settings" | "tools" | "implementation-model" | "export" | "shortcut";
|
|
48
54
|
type Action =
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
55
|
+
| "set-thinking"
|
|
56
|
+
| "open-tools"
|
|
57
|
+
| "toggle-tool"
|
|
58
|
+
| "reset-tools"
|
|
59
|
+
| "set-retention"
|
|
60
|
+
| "open-implementation-model"
|
|
61
|
+
| "set-implementation-model"
|
|
62
|
+
| "set-implementation-thinking"
|
|
63
|
+
| "open-export"
|
|
64
|
+
| "set-export"
|
|
65
|
+
| "open-shortcut"
|
|
66
|
+
| "set-shortcut";
|
|
58
67
|
|
|
59
68
|
export async function showPlanModeSettings(
|
|
60
|
-
|
|
61
|
-
|
|
69
|
+
ctx: ExtensionContext,
|
|
70
|
+
options: PlanModeSettingsMenuOptions,
|
|
62
71
|
): Promise<RunMenuResult> {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
);
|
|
76
|
-
const toolsByItemId = new Map(tools.map((tool) => [toolItemIds.get(tool.name) as string, tool]));
|
|
72
|
+
const settingsPath = options.settingsPath ?? planModeSettingsPath();
|
|
73
|
+
const readSettings = options.readSettings ?? readPlanModeSettings;
|
|
74
|
+
const updateSettings = options.updateSettings ?? updatePlanModeSettings;
|
|
75
|
+
const activeToolNames = new Set(options.activeToolNames ?? options.tools.map((tool) => tool.name));
|
|
76
|
+
const tools = options.tools.filter(
|
|
77
|
+
(tool) => tool.name !== PLAN_MODE_QUESTION_TOOL_NAME && tool.name !== PLAN_MODE_COMPLETE_TOOL_NAME,
|
|
78
|
+
);
|
|
79
|
+
const toolItemIds = new Map(tools.map((tool, index) => [tool.name, `plan-settings-tool:${index}`]));
|
|
80
|
+
const toolsByItemId = new Map(tools.map((tool) => [toolItemIds.get(tool.name) as string, tool]));
|
|
81
|
+
const implementationModels = snapshotAvailableImplementationModels(ctx);
|
|
82
|
+
const modelItemIds = new Map(implementationModels.map((model, index) => [model, `plan-settings-model:${index}`]));
|
|
83
|
+
const modelsByItemId = new Map(implementationModels.map((model) => [modelItemIds.get(model) as string, model]));
|
|
77
84
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
85
|
+
const loadState = async (): Promise<SettingsMenuState> => {
|
|
86
|
+
const loaded = await readSettings(options.settingsPath);
|
|
87
|
+
if (loaded.kind === "invalid") {
|
|
88
|
+
return {
|
|
89
|
+
kind: "invalid",
|
|
90
|
+
settings: { thinkingLevel: "inherit" },
|
|
91
|
+
notice: loaded.notice,
|
|
92
|
+
reason: loaded.reason,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
kind: "valid",
|
|
97
|
+
settings: loaded.kind === "loaded" ? loaded.settings : { thinkingLevel: "inherit" },
|
|
98
|
+
notice: loaded.notice,
|
|
99
|
+
};
|
|
100
|
+
};
|
|
94
101
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
102
|
+
const menu = defineMenu<SettingsMenuState, Screen, Action, ExtensionContext>({
|
|
103
|
+
start: "settings",
|
|
104
|
+
screens: {
|
|
105
|
+
settings: ({ state }) =>
|
|
106
|
+
state.kind === "invalid"
|
|
107
|
+
? invalidScreen(settingsPath, state)
|
|
108
|
+
: {
|
|
109
|
+
kind: "settings",
|
|
110
|
+
title: "Plan Mode Settings",
|
|
111
|
+
lines: settingsLines(settingsPath, state.notice),
|
|
112
|
+
items: [
|
|
113
|
+
{
|
|
114
|
+
id: "thinkingLevel",
|
|
115
|
+
label: "Plan thinking",
|
|
116
|
+
description: "Set the thinking level when the next Plan workflow starts.",
|
|
117
|
+
currentValue: state.settings.thinkingLevel,
|
|
118
|
+
values: PLAN_MODE_THINKING_LEVELS,
|
|
119
|
+
action: "set-thinking",
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
id: "defaultPlanTools",
|
|
123
|
+
label: "Plan policy tools",
|
|
124
|
+
description: "Choose active tools or retain names to resolve before the first request.",
|
|
125
|
+
currentValue: defaultToolsValue(state.settings.defaultPlanTools),
|
|
126
|
+
action: "open-tools",
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
id: "implementationPlanRetention",
|
|
130
|
+
label: "Plan reinjection",
|
|
131
|
+
description:
|
|
132
|
+
"Choose how long Plan mode restores the exact plan when ordinary context no longer contains it.",
|
|
133
|
+
currentValue: retentionLabel(configuredImplementationPlanRetention(state.settings)),
|
|
134
|
+
values: IMPLEMENTATION_PLAN_RETENTIONS.map(retentionLabel),
|
|
135
|
+
action: "set-retention",
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
id: "defaultImplementationModel",
|
|
139
|
+
label: "Fresh model",
|
|
140
|
+
description: "Choose the default model for a fresh implementation session.",
|
|
141
|
+
currentValue: implementationModelValue(state.settings, implementationModels),
|
|
142
|
+
action: "open-implementation-model",
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
id: "defaultImplementationThinkingLevel",
|
|
146
|
+
label: "Fresh thinking",
|
|
147
|
+
description: "Choose the default thinking level for a fresh implementation session.",
|
|
148
|
+
currentValue: configuredImplementationThinkingLevel(state.settings) ?? "same as plan",
|
|
149
|
+
values: ["same as plan", ...IMPLEMENTATION_THINKING_LEVELS],
|
|
150
|
+
action: "set-implementation-thinking",
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
id: "defaultPlanExportPath",
|
|
154
|
+
label: "Export destination",
|
|
155
|
+
description: "Set the destination used when an export omits its path.",
|
|
156
|
+
currentValue: safeTerminalText(configuredPlanExportPath(state.settings)),
|
|
157
|
+
action: "open-export",
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
id: "toggleShortcut",
|
|
161
|
+
label: "Plan mode shortcut",
|
|
162
|
+
description: "Set the global shortcut used to toggle Plan mode.",
|
|
163
|
+
currentValue: configuredPlanModeToggleShortcut(state.settings) ?? "none",
|
|
164
|
+
action: "open-shortcut",
|
|
165
|
+
},
|
|
166
|
+
],
|
|
167
|
+
},
|
|
168
|
+
tools: ({ state }) => ({
|
|
169
|
+
kind: "multiSelect",
|
|
170
|
+
title: "Default Plan policy allowlist",
|
|
171
|
+
lines: [
|
|
172
|
+
"Changes apply when a later Plan workflow starts; model-visible tools stay unchanged.",
|
|
173
|
+
"Retained inactive names resolve before that workflow's first request.",
|
|
174
|
+
"Plan mode never activates tools, and non-built-ins run at user risk.",
|
|
175
|
+
],
|
|
176
|
+
enableSearch: true,
|
|
177
|
+
viewportSize: 10,
|
|
178
|
+
items: defaultToolItems(tools, state.settings.defaultPlanTools, activeToolNames, toolItemIds),
|
|
179
|
+
action: "toggle-tool",
|
|
180
|
+
actions: [
|
|
181
|
+
{
|
|
182
|
+
id: "reset-tools",
|
|
183
|
+
label: "Use automatic safe built-ins",
|
|
184
|
+
action: "reset-tools",
|
|
185
|
+
},
|
|
186
|
+
],
|
|
187
|
+
hint: "back",
|
|
188
|
+
}),
|
|
189
|
+
"implementation-model": ({ state }) => ({
|
|
190
|
+
kind: "choice",
|
|
191
|
+
title: "Fresh implementation model",
|
|
192
|
+
lines: ["Same as plan is the default and fallback when a configured model is unavailable."],
|
|
193
|
+
items: implementationModelItems(implementationModels, modelItemIds),
|
|
194
|
+
action: "set-implementation-model",
|
|
195
|
+
initialItemId: implementationModelItemId(
|
|
196
|
+
configuredImplementationModel(state.settings),
|
|
197
|
+
implementationModels,
|
|
198
|
+
modelItemIds,
|
|
199
|
+
),
|
|
200
|
+
enableSearch: true,
|
|
201
|
+
viewportSize: 10,
|
|
202
|
+
hint: "back",
|
|
203
|
+
}),
|
|
204
|
+
export: ({ state }) => {
|
|
205
|
+
const configured = configuredPlanExportPath(state.settings);
|
|
206
|
+
const destination = planExportDestination(configured, ctx.cwd);
|
|
207
|
+
return {
|
|
208
|
+
kind: "input",
|
|
209
|
+
title: "Export destination",
|
|
210
|
+
lines: [
|
|
211
|
+
`Configured: ${destination.configuredPath}`,
|
|
212
|
+
`Resolves here to: ${destination.resolvedPath}`,
|
|
213
|
+
"Submit an empty value to reset to PLAN.md. Changes affect the next export.",
|
|
214
|
+
],
|
|
215
|
+
placeholder: configured,
|
|
216
|
+
action: "set-export",
|
|
217
|
+
hint: "back",
|
|
218
|
+
};
|
|
219
|
+
},
|
|
220
|
+
shortcut: ({ state }) => ({
|
|
221
|
+
kind: "input",
|
|
222
|
+
title: "Plan mode shortcut",
|
|
223
|
+
lines: [
|
|
224
|
+
`Configured: ${configuredPlanModeToggleShortcut(state.settings) ?? "none"}`,
|
|
225
|
+
"Use Pi key identifiers.",
|
|
226
|
+
"Submit an empty value to clear the shortcut.",
|
|
227
|
+
"When unset, Plan mode has no global shortcut.",
|
|
228
|
+
],
|
|
229
|
+
placeholder: configuredPlanModeToggleShortcut(state.settings) ?? "",
|
|
230
|
+
action: "set-shortcut",
|
|
231
|
+
hint: "back",
|
|
232
|
+
}),
|
|
233
|
+
},
|
|
234
|
+
actions: {
|
|
235
|
+
"set-thinking": async ({ ctx: actionCtx, value, signal }) => {
|
|
236
|
+
if (!PLAN_MODE_THINKING_LEVELS.includes(value as (typeof PLAN_MODE_THINKING_LEVELS)[number])) {
|
|
237
|
+
return { kind: "rejected" };
|
|
238
|
+
}
|
|
239
|
+
return savePatch(
|
|
240
|
+
actionCtx,
|
|
241
|
+
{ thinkingLevel: value as PlanModeSettings["thinkingLevel"] },
|
|
242
|
+
signal,
|
|
243
|
+
`Plan mode thinking level: ${value}. Applies to the next Plan workflow.`,
|
|
244
|
+
);
|
|
245
|
+
},
|
|
246
|
+
"open-tools": async () => ({ kind: "to", screen: "tools" }),
|
|
247
|
+
"set-retention": async ({ ctx: actionCtx, value, signal }) => {
|
|
248
|
+
const implementationPlanRetention = retentionFromLabel(value);
|
|
249
|
+
if (!implementationPlanRetention) return { kind: "rejected" };
|
|
250
|
+
return savePatch(
|
|
251
|
+
actionCtx,
|
|
252
|
+
{ implementationPlanRetention },
|
|
253
|
+
signal,
|
|
254
|
+
`Plan reinjection: ${retentionLabel(implementationPlanRetention)}. Applies to the next Implement action.`,
|
|
255
|
+
);
|
|
256
|
+
},
|
|
257
|
+
"open-implementation-model": async () => ({
|
|
258
|
+
kind: "to",
|
|
259
|
+
screen: "implementation-model",
|
|
260
|
+
}),
|
|
261
|
+
"set-implementation-model": async ({ ctx: actionCtx, itemId, signal }) => {
|
|
262
|
+
const model = itemId ? modelsByItemId.get(itemId) : undefined;
|
|
263
|
+
if (itemId !== "same-as-plan" && !model) return { kind: "rejected" };
|
|
264
|
+
const defaultImplementationModel = model ? { provider: model.provider, modelId: model.id } : null;
|
|
265
|
+
const result = await savePatch(
|
|
266
|
+
actionCtx,
|
|
267
|
+
{ defaultImplementationModel },
|
|
268
|
+
signal,
|
|
269
|
+
model
|
|
270
|
+
? `Fresh implementation model: ${safeModelReference({ provider: model.provider, modelId: model.id })}.`
|
|
271
|
+
: "Fresh implementation model: same as plan.",
|
|
272
|
+
);
|
|
273
|
+
return result.kind === "stay" ? { kind: "to", screen: "settings" } : result;
|
|
274
|
+
},
|
|
275
|
+
"set-implementation-thinking": async ({ ctx: actionCtx, value, signal }) => {
|
|
276
|
+
if (value === "same as plan") {
|
|
277
|
+
return savePatch(
|
|
278
|
+
actionCtx,
|
|
279
|
+
{ defaultImplementationThinkingLevel: null },
|
|
280
|
+
signal,
|
|
281
|
+
"Fresh implementation thinking: same as plan.",
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
if (!IMPLEMENTATION_THINKING_LEVELS.includes(value as (typeof IMPLEMENTATION_THINKING_LEVELS)[number])) {
|
|
285
|
+
return { kind: "rejected" };
|
|
286
|
+
}
|
|
287
|
+
return savePatch(
|
|
288
|
+
actionCtx,
|
|
289
|
+
{
|
|
290
|
+
defaultImplementationThinkingLevel: value as PlanModeSettings["defaultImplementationThinkingLevel"],
|
|
291
|
+
},
|
|
292
|
+
signal,
|
|
293
|
+
`Fresh implementation thinking: ${value}.`,
|
|
294
|
+
);
|
|
295
|
+
},
|
|
296
|
+
"open-export": async () => ({ kind: "to", screen: "export" }),
|
|
297
|
+
"set-export": async ({ ctx: actionCtx, value, signal }) => {
|
|
298
|
+
const defaultPlanExportPath = value?.trim() || null;
|
|
299
|
+
const result = await savePatch(
|
|
300
|
+
actionCtx,
|
|
301
|
+
{ defaultPlanExportPath },
|
|
302
|
+
signal,
|
|
303
|
+
defaultPlanExportPath
|
|
304
|
+
? `Default Plan export destination: ${safeTerminalText(defaultPlanExportPath)}.`
|
|
305
|
+
: "Default Plan export destination reset to PLAN.md.",
|
|
306
|
+
);
|
|
307
|
+
return result.kind === "stay" ? { kind: "to", screen: "settings" } : result;
|
|
308
|
+
},
|
|
309
|
+
"open-shortcut": async () => ({ kind: "to", screen: "shortcut" }),
|
|
310
|
+
"set-shortcut": async ({ ctx: actionCtx, value, signal }) => {
|
|
311
|
+
const raw = value?.trim() || null;
|
|
312
|
+
if (raw && !normalizeKeyId(raw)) {
|
|
313
|
+
actionCtx.ui.notify(
|
|
314
|
+
`Invalid key identifier: ${safeTerminalText(raw)}. Use Pi key identifiers like ctrl+alt+p.`,
|
|
315
|
+
"warning",
|
|
316
|
+
);
|
|
317
|
+
return { kind: "stay" as const };
|
|
318
|
+
}
|
|
319
|
+
const toggleShortcut = raw as PlanModeSettingsPatch["toggleShortcut"];
|
|
320
|
+
const result = await savePatch(
|
|
321
|
+
actionCtx,
|
|
322
|
+
{ toggleShortcut },
|
|
323
|
+
signal,
|
|
324
|
+
toggleShortcut
|
|
325
|
+
? `Plan mode shortcut: ${safeTerminalText(toggleShortcut)}.`
|
|
326
|
+
: "Plan mode shortcut cleared (no global shortcut).",
|
|
327
|
+
);
|
|
328
|
+
return result.kind === "stay" ? { kind: "to", screen: "settings" } : result;
|
|
329
|
+
},
|
|
330
|
+
"toggle-tool": async ({ ctx: actionCtx, state, itemId, selected, signal }) => {
|
|
331
|
+
const tool = itemId ? toolsByItemId.get(itemId) : undefined;
|
|
332
|
+
if (!tool || !activeToolNames.has(tool.name) || !canSelectToolInPlanMode(tool)) {
|
|
333
|
+
return { kind: "rejected" };
|
|
334
|
+
}
|
|
335
|
+
const names = explicitToolNames(tools, state.settings.defaultPlanTools);
|
|
336
|
+
const next = selected ? Array.from(new Set([...names, tool.name])) : names.filter((name) => name !== tool.name);
|
|
337
|
+
return savePatch(
|
|
338
|
+
actionCtx,
|
|
339
|
+
{ defaultPlanTools: next },
|
|
340
|
+
signal,
|
|
341
|
+
`Default Plan policy: ${next.length === 0 ? "no optional tools" : `${next.length} allowed`}.`,
|
|
342
|
+
);
|
|
343
|
+
},
|
|
344
|
+
"reset-tools": async ({ ctx: actionCtx, state, signal }) => {
|
|
345
|
+
if (state.settings.defaultPlanTools === undefined) return { kind: "stay" };
|
|
346
|
+
return savePatch(
|
|
347
|
+
actionCtx,
|
|
348
|
+
{ defaultPlanTools: null },
|
|
349
|
+
signal,
|
|
350
|
+
"Default Plan-mode tools: automatic safe built-ins.",
|
|
351
|
+
);
|
|
352
|
+
},
|
|
353
|
+
},
|
|
354
|
+
});
|
|
291
355
|
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
356
|
+
return runMenu(ctx, menu, {
|
|
357
|
+
getState: loadState,
|
|
358
|
+
signal: options.signal,
|
|
359
|
+
isCurrent: options.isCurrent,
|
|
360
|
+
});
|
|
297
361
|
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
362
|
+
async function savePatch(
|
|
363
|
+
actionCtx: ExtensionContext,
|
|
364
|
+
patch: PlanModeSettingsPatch,
|
|
365
|
+
signal: AbortSignal,
|
|
366
|
+
successMessage: string,
|
|
367
|
+
) {
|
|
368
|
+
if (signal.aborted || !options.isCurrent()) return { kind: "rejected" as const };
|
|
369
|
+
try {
|
|
370
|
+
const saved = await updateSettings(patch, {
|
|
371
|
+
settingsPath: options.settingsPath,
|
|
372
|
+
legacySettingsPath: options.legacySettingsPath,
|
|
373
|
+
signal,
|
|
374
|
+
});
|
|
375
|
+
if (options.isCurrent()) options.onSaved(saved);
|
|
376
|
+
if (signal.aborted || !options.isCurrent()) return { kind: "rejected" as const };
|
|
377
|
+
actionCtx.ui.notify(successMessage, "info");
|
|
378
|
+
return { kind: "stay" as const };
|
|
379
|
+
} catch (error) {
|
|
380
|
+
if (!signal.aborted && options.isCurrent()) {
|
|
381
|
+
actionCtx.ui.notify(
|
|
382
|
+
`Could not save Plan mode settings; the previous value remains: ${safeTerminalText(formatError(error))}`,
|
|
383
|
+
"error",
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
return { kind: "rejected" as const };
|
|
387
|
+
}
|
|
388
|
+
}
|
|
325
389
|
}
|
|
326
390
|
|
|
327
391
|
function settingsLines(settingsPath: string, notice: string | undefined) {
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
392
|
+
return [
|
|
393
|
+
`User settings · ${safeTerminalText(settingsPath)}`,
|
|
394
|
+
"Plan defaults apply to the next workflow; reinjection and export choices apply to their next action.",
|
|
395
|
+
...(notice ? [safeTerminalText(notice)] : []),
|
|
396
|
+
];
|
|
333
397
|
}
|
|
334
398
|
|
|
335
399
|
function invalidScreen(settingsPath: string, state: SettingsMenuState) {
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
400
|
+
return {
|
|
401
|
+
kind: "detail" as const,
|
|
402
|
+
title: "Plan Mode Settings · Read only",
|
|
403
|
+
lines: [
|
|
404
|
+
`Invalid settings file. Fix ${safeTerminalText(settingsPath)} before saving.`,
|
|
405
|
+
safeTerminalText(state.reason ?? "The settings file is invalid."),
|
|
406
|
+
...(state.notice ? [safeTerminalText(state.notice)] : []),
|
|
407
|
+
],
|
|
408
|
+
hint: "back" as const,
|
|
409
|
+
};
|
|
346
410
|
}
|
|
347
411
|
|
|
348
412
|
function retentionFromLabel(value: string | undefined) {
|
|
349
|
-
|
|
413
|
+
return IMPLEMENTATION_PLAN_RETENTIONS.find((retention) => retentionLabel(retention) === value);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
function implementationModelValue(settings: PlanModeSettings, models: readonly AvailableImplementationModel[]) {
|
|
417
|
+
const configured = configuredImplementationModel(settings);
|
|
418
|
+
if (!configured) return "same as plan";
|
|
419
|
+
return findAvailableImplementationModel(models, configured)
|
|
420
|
+
? safeModelReference(configured)
|
|
421
|
+
: `same as plan · ${safeModelReference(configured)} unavailable`;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
function implementationModelItems(
|
|
425
|
+
models: readonly AvailableImplementationModel[],
|
|
426
|
+
itemIds: ReadonlyMap<AvailableImplementationModel, string>,
|
|
427
|
+
) {
|
|
428
|
+
return [
|
|
429
|
+
{
|
|
430
|
+
id: "same-as-plan",
|
|
431
|
+
label: "Same as plan",
|
|
432
|
+
description: "Use the planning session model.",
|
|
433
|
+
},
|
|
434
|
+
...models.map((model) => {
|
|
435
|
+
const reference = safeModelReference({ provider: model.provider, modelId: model.id });
|
|
436
|
+
const name = safeModelMetadata(model.name, "");
|
|
437
|
+
return {
|
|
438
|
+
id: itemIds.get(model) as string,
|
|
439
|
+
label: reference,
|
|
440
|
+
...(name ? { details: [`Model Name: ${name}`] } : {}),
|
|
441
|
+
searchText: [reference, name].filter(Boolean).join(" "),
|
|
442
|
+
};
|
|
443
|
+
}),
|
|
444
|
+
];
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
function implementationModelItemId(
|
|
448
|
+
configured: ImplementationModelOverride | undefined,
|
|
449
|
+
models: readonly AvailableImplementationModel[],
|
|
450
|
+
itemIds: ReadonlyMap<AvailableImplementationModel, string>,
|
|
451
|
+
) {
|
|
452
|
+
const model = findAvailableImplementationModel(models, configured);
|
|
453
|
+
return model ? itemIds.get(model) : "same-as-plan";
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
function safeModelReference(model: ImplementationModelOverride) {
|
|
457
|
+
return `${safeModelMetadata(model.modelId, "unknown model")} [${safeModelMetadata(model.provider, "unknown provider")}]`;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
function safeModelMetadata(value: unknown, fallback: string) {
|
|
461
|
+
if (typeof value !== "string") return fallback;
|
|
462
|
+
const safe = sanitizeTerminalText(value).trim() || fallback;
|
|
463
|
+
return [...safe].slice(0, 512).join("");
|
|
350
464
|
}
|
|
351
465
|
|
|
352
466
|
function defaultToolsValue(configured: string[] | undefined) {
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
467
|
+
if (configured === undefined) return "Automatic safe built-ins";
|
|
468
|
+
if (configured.length === 0) return "No optional tools";
|
|
469
|
+
return `${configured.length} selected`;
|
|
356
470
|
}
|
|
357
471
|
|
|
358
472
|
function defaultToolItems(
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
473
|
+
tools: readonly ToolInfo[],
|
|
474
|
+
configured: string[] | undefined,
|
|
475
|
+
activeToolNames: ReadonlySet<string>,
|
|
476
|
+
toolItemIds: ReadonlyMap<string, string>,
|
|
363
477
|
) {
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
478
|
+
const selected = new Set(explicitToolNames(tools, configured));
|
|
479
|
+
const availableNames = new Set(tools.map((tool) => tool.name));
|
|
480
|
+
const items = tools.map((tool) => {
|
|
481
|
+
const active = activeToolNames.has(tool.name);
|
|
482
|
+
const selectable = active && canSelectToolInPlanMode(tool);
|
|
483
|
+
const policy = active
|
|
484
|
+
? toolPolicyLabel(tool)
|
|
485
|
+
: selected.has(tool.name)
|
|
486
|
+
? "not active yet; retained for first-request resolution"
|
|
487
|
+
: "not active in this Pi session";
|
|
488
|
+
const description = tool.description ?? "No description available";
|
|
489
|
+
return {
|
|
490
|
+
id: toolItemIds.get(tool.name) as string,
|
|
491
|
+
label: tool.name,
|
|
492
|
+
description: `${policy} · ${description}`,
|
|
493
|
+
searchText: `${policy} ${description}`,
|
|
494
|
+
selected: selected.has(tool.name),
|
|
495
|
+
disabled: !selectable,
|
|
496
|
+
disabledReason: !active
|
|
497
|
+
? selected.has(tool.name)
|
|
498
|
+
? "Not active yet; retained and resolved before the first request"
|
|
499
|
+
: "Not active in Pi; Plan mode will not activate it"
|
|
500
|
+
: selectable
|
|
501
|
+
? undefined
|
|
502
|
+
: "Blocked by Plan-mode policy",
|
|
503
|
+
};
|
|
504
|
+
});
|
|
505
|
+
for (const [index, name] of (configured ?? []).entries()) {
|
|
506
|
+
if (availableNames.has(name)) continue;
|
|
507
|
+
const label = terminalToolName(name);
|
|
508
|
+
items.push({
|
|
509
|
+
id: `plan-settings-pending:${index}`,
|
|
510
|
+
label,
|
|
511
|
+
description: "pending registration · Retained and resolved before the first request",
|
|
512
|
+
searchText: `${label} pending registration retained settings first request`,
|
|
513
|
+
selected: true,
|
|
514
|
+
disabled: true,
|
|
515
|
+
disabledReason: "Not registered yet; reset defaults to remove retained names",
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
return items;
|
|
405
519
|
}
|
|
406
520
|
|
|
407
521
|
function explicitToolNames(tools: readonly ToolInfo[], configured: string[] | undefined) {
|
|
408
|
-
|
|
409
|
-
? defaultPlanModeToolNames([...tools], undefined)
|
|
410
|
-
: [...configured];
|
|
522
|
+
return configured === undefined ? defaultPlanModeToolNames([...tools], undefined) : [...configured];
|
|
411
523
|
}
|
|
412
524
|
|
|
413
525
|
function terminalToolName(value: string) {
|
|
414
|
-
|
|
415
|
-
|
|
526
|
+
const safe = safeTerminalText(value) || "(unnamed tool)";
|
|
527
|
+
return safe.length > 120 ? `${safe.slice(0, 119)}…` : safe;
|
|
416
528
|
}
|
|
417
529
|
|
|
418
530
|
function safeTerminalText(value: string) {
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
531
|
+
return [...value]
|
|
532
|
+
.map((character) => {
|
|
533
|
+
const codePoint = character.codePointAt(0) ?? 0;
|
|
534
|
+
return codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f) ? " " : character;
|
|
535
|
+
})
|
|
536
|
+
.join("")
|
|
537
|
+
.trim();
|
|
426
538
|
}
|
|
427
539
|
|
|
428
540
|
function formatError(error: unknown) {
|
|
429
|
-
|
|
541
|
+
return error instanceof Error ? error.message : String(error);
|
|
430
542
|
}
|