@narumitw/pi-plan-mode 0.49.2 → 0.50.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 +17 -5
- package/package.json +8 -5
- package/src/plan-mode.ts +129 -15
- package/src/settings-menu.ts +47 -2
- package/src/settings.ts +91 -0
package/README.md
CHANGED
|
@@ -39,7 +39,7 @@ pi -e npm:@narumitw/pi-plan-mode
|
|
|
39
39
|
Try this package locally from the repository root:
|
|
40
40
|
|
|
41
41
|
```bash
|
|
42
|
-
pi -e ./
|
|
42
|
+
pi -e ./packages/pi-plan-mode
|
|
43
43
|
```
|
|
44
44
|
|
|
45
45
|
## 🚀 Usage
|
|
@@ -164,8 +164,12 @@ You can also exit directly. Before implementation, direct exit discards the late
|
|
|
164
164
|
|
|
165
165
|
## ⚙️ Settings
|
|
166
166
|
|
|
167
|
-
Open **Settings** from an inactive `/plan` menu to edit one flat group of
|
|
168
|
-
|
|
167
|
+
Open **Settings** from an inactive `/plan` menu to edit one flat group of five workflow choices: **Plan thinking**, **Plan tools**, **After Implement**, **Export destination**, and **Plan mode shortcut**.
|
|
168
|
+
You can also edit `$PI_CODING_AGENT_DIR/pi-plan-mode.json` (normally `~/.pi/agent/pi-plan-mode.json`) manually.
|
|
169
|
+
`safeSubcommands` remains JSON-only.
|
|
170
|
+
You can change the Plan-mode shortcut with `toggleShortcut` as long as the file remains JSON-only and uses a valid key string.
|
|
171
|
+
The file is optional, is read at session start and reloaded automatically when changed, and is created only after an explicit Settings save or manual edit.
|
|
172
|
+
When omitted, the shortcut is disabled by default.
|
|
169
173
|
```json
|
|
170
174
|
{
|
|
171
175
|
"thinkingLevel": "inherit",
|
|
@@ -175,7 +179,8 @@ Open **Settings** from an inactive `/plan` menu to edit one flat group of four w
|
|
|
175
179
|
"safeSubcommands": {
|
|
176
180
|
"git": ["status", "log", "rev-parse", "blame"],
|
|
177
181
|
"gh": ["pr view", "pr list", "issue view", "issue list"]
|
|
178
|
-
}
|
|
182
|
+
},
|
|
183
|
+
"toggleShortcut": "<your_key>"
|
|
179
184
|
}
|
|
180
185
|
```
|
|
181
186
|
|
|
@@ -199,6 +204,13 @@ Changing this setting applies to the next Implement action only. Each active imp
|
|
|
199
204
|
|
|
200
205
|
The existing no-overwrite, cancellation, and atomic Plan-state behavior is unchanged. A failed save rolls the row back to its previous value; a failed or cancelled export preserves the plan and target. Long previews wrap or truncate to the available terminal width without changing the raw path used by the action.
|
|
201
206
|
|
|
207
|
+
### Toggle shortcut
|
|
208
|
+
|
|
209
|
+
`toggleShortcut` controls the global Plan-mode keybinding used by the TUI shortcut.
|
|
210
|
+
Omit this setting to keep the shortcut disabled.
|
|
211
|
+
Set `toggleShortcut` to the key string you want.
|
|
212
|
+
Avoid values that conflict with editor shortcuts.
|
|
213
|
+
|
|
202
214
|
### Safe shell subcommands
|
|
203
215
|
|
|
204
216
|
`safeSubcommands` adds reviewed command validators to limited `bash`; it is not a raw shell allowlist. Only the following exact values are accepted:
|
|
@@ -264,7 +276,7 @@ This extension maps Codex's `ModeKind::Plan` behavior onto Pi's extension API:
|
|
|
264
276
|
## 🗂️ Package layout
|
|
265
277
|
|
|
266
278
|
```txt
|
|
267
|
-
|
|
279
|
+
packages/pi-plan-mode/
|
|
268
280
|
├── src/
|
|
269
281
|
│ ├── index.ts # Pi package entrypoint
|
|
270
282
|
│ ├── plan-mode.ts # Extension registration, mode state, and UI loading boundary
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@narumitw/pi-plan-mode",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.50.0",
|
|
4
4
|
"description": "Pi extension that adds a Codex-like read-only /plan collaboration mode.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -22,6 +22,9 @@
|
|
|
22
22
|
"./src/index.ts"
|
|
23
23
|
]
|
|
24
24
|
},
|
|
25
|
+
"piExtension": {
|
|
26
|
+
"lifecycle": "stable"
|
|
27
|
+
},
|
|
25
28
|
"scripts": {
|
|
26
29
|
"check": "biome check . && npm run typecheck",
|
|
27
30
|
"format": "biome check --write .",
|
|
@@ -32,15 +35,15 @@
|
|
|
32
35
|
"@earendil-works/pi-tui": "*"
|
|
33
36
|
},
|
|
34
37
|
"devDependencies": {
|
|
35
|
-
"@biomejs/biome": "2.5.
|
|
36
|
-
"@earendil-works/pi-coding-agent": "0.
|
|
37
|
-
"@earendil-works/pi-tui": "0.
|
|
38
|
+
"@biomejs/biome": "2.5.8",
|
|
39
|
+
"@earendil-works/pi-coding-agent": "0.84.2",
|
|
40
|
+
"@earendil-works/pi-tui": "0.84.2",
|
|
38
41
|
"typescript": "7.0.2"
|
|
39
42
|
},
|
|
40
43
|
"repository": {
|
|
41
44
|
"type": "git",
|
|
42
45
|
"url": "https://github.com/narumiruna/pi-extensions",
|
|
43
|
-
"directory": "
|
|
46
|
+
"directory": "packages/pi-plan-mode"
|
|
44
47
|
},
|
|
45
48
|
"dependencies": {
|
|
46
49
|
"@narumitw/pi-tui-kit": "^0.49.1"
|
package/src/plan-mode.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { watch } from "node:fs";
|
|
3
|
+
import { basename, dirname } from "node:path";
|
|
2
4
|
import type {
|
|
3
5
|
ExtensionAPI,
|
|
4
6
|
ExtensionCommandContext,
|
|
@@ -50,8 +52,10 @@ import {
|
|
|
50
52
|
import {
|
|
51
53
|
awaitPlanModeSettingsWrites,
|
|
52
54
|
configuredImplementationPlanRetention,
|
|
55
|
+
configuredPlanModeToggleShortcut,
|
|
53
56
|
configuredThinkingLevel,
|
|
54
57
|
type PlanModeSettings,
|
|
58
|
+
planModeSettingsPath,
|
|
55
59
|
readPlanModeSettings,
|
|
56
60
|
} from "./settings.js";
|
|
57
61
|
import { type PlanCompletionSource, type PlanModeState, restorePlanModeState } from "./state.js";
|
|
@@ -97,8 +101,11 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
97
101
|
}
|
|
98
102
|
return interactiveUiPromise;
|
|
99
103
|
};
|
|
104
|
+
const explicitPlanModeSettingsPath = dependencies.settingsPath;
|
|
100
105
|
let state: PlanModeState = { enabled: false, awaitingAction: false };
|
|
101
106
|
let settings: PlanModeSettings = { thinkingLevel: "inherit" };
|
|
107
|
+
let toggleShortcut: ReturnType<typeof configuredPlanModeToggleShortcut>;
|
|
108
|
+
const clearPlanModeShortcutHandler = () => {};
|
|
102
109
|
let previousTools: string[] | undefined;
|
|
103
110
|
let readyPresentationIntent: ReadyPresentationIntent | undefined;
|
|
104
111
|
let latestCommandContext: ExtensionCommandContext | undefined;
|
|
@@ -107,6 +114,8 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
107
114
|
let workflowGeneration = 0;
|
|
108
115
|
let refreshStateBeforeFirstAgentStart = false;
|
|
109
116
|
let menuController = new AbortController();
|
|
117
|
+
let settingsWatch: ReturnType<typeof watch> | undefined;
|
|
118
|
+
let settingsReloadTimer: ReturnType<typeof setTimeout> | undefined;
|
|
110
119
|
const implementationRetention = createImplementationRetentionCoordinator();
|
|
111
120
|
const persistState = () => pi.appendEntry<PlanModeState>(STATE_ENTRY_TYPE, state);
|
|
112
121
|
const planExports = createPlanExportController({
|
|
@@ -255,15 +264,8 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
255
264
|
return;
|
|
256
265
|
}
|
|
257
266
|
if (command === "exit" || command === "off") {
|
|
258
|
-
|
|
259
|
-
? "Active implementation plan cleared."
|
|
260
|
-
: state.savedPlan
|
|
261
|
-
? "Saved plan cleared."
|
|
262
|
-
: state.latestPlan
|
|
263
|
-
? "Plan mode disabled. Proposed plan discarded."
|
|
264
|
-
: "Plan mode disabled.";
|
|
267
|
+
ctx.ui.notify(planModeDisableNotification(), "info");
|
|
265
268
|
exitPlanMode(ctx);
|
|
266
|
-
ctx.ui.notify(notification, "info");
|
|
267
269
|
return;
|
|
268
270
|
}
|
|
269
271
|
if (command === "tools") {
|
|
@@ -309,6 +311,98 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
309
311
|
},
|
|
310
312
|
});
|
|
311
313
|
|
|
314
|
+
const applyPlanModeShortcut = (
|
|
315
|
+
nextShortcut: ReturnType<typeof configuredPlanModeToggleShortcut>,
|
|
316
|
+
) => {
|
|
317
|
+
if (toggleShortcut && toggleShortcut !== nextShortcut) {
|
|
318
|
+
pi.registerShortcut(toggleShortcut, {
|
|
319
|
+
handler: clearPlanModeShortcutHandler,
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
if (!nextShortcut) {
|
|
323
|
+
toggleShortcut = undefined;
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
if (toggleShortcut === nextShortcut) return;
|
|
327
|
+
pi.registerShortcut(nextShortcut, {
|
|
328
|
+
description: "Toggle Plan mode",
|
|
329
|
+
handler: (ctx) => {
|
|
330
|
+
togglePlanMode(ctx);
|
|
331
|
+
},
|
|
332
|
+
});
|
|
333
|
+
toggleShortcut = nextShortcut;
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
const readPlanModeRuntimeSettings = async () => {
|
|
337
|
+
return dependencies.readSettings?.() ?? readPlanModeSettings(explicitPlanModeSettingsPath);
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
const applyPlanModeSettings = async (
|
|
341
|
+
generation: number,
|
|
342
|
+
ctx: ExtensionContext | undefined,
|
|
343
|
+
showWarnings: boolean,
|
|
344
|
+
) => {
|
|
345
|
+
const loadedSettings = await readPlanModeRuntimeSettings();
|
|
346
|
+
if (generation !== menuGeneration || menuController.signal.aborted) {
|
|
347
|
+
return undefined;
|
|
348
|
+
}
|
|
349
|
+
if (loadedSettings.kind === "loaded") {
|
|
350
|
+
settings = loadedSettings.settings;
|
|
351
|
+
} else {
|
|
352
|
+
settings = { thinkingLevel: "inherit" };
|
|
353
|
+
}
|
|
354
|
+
applyPlanModeShortcut(configuredPlanModeToggleShortcut(settings));
|
|
355
|
+
if (!ctx || !showWarnings) return loadedSettings;
|
|
356
|
+
if (loadedSettings.kind === "invalid") {
|
|
357
|
+
ctx.ui.notify(`pi-plan-mode settings ignored: ${loadedSettings.reason}`, "warning");
|
|
358
|
+
}
|
|
359
|
+
if (loadedSettings.notice) {
|
|
360
|
+
ctx.ui.notify(loadedSettings.notice, "warning");
|
|
361
|
+
}
|
|
362
|
+
return loadedSettings;
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
const stopPlanModeSettingsWatch = () => {
|
|
366
|
+
if (settingsReloadTimer) {
|
|
367
|
+
clearTimeout(settingsReloadTimer);
|
|
368
|
+
settingsReloadTimer = undefined;
|
|
369
|
+
}
|
|
370
|
+
settingsWatch?.close();
|
|
371
|
+
settingsWatch = undefined;
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
const schedulePlanModeSettingsReload = (generation: number) => {
|
|
375
|
+
if (settingsReloadTimer) {
|
|
376
|
+
clearTimeout(settingsReloadTimer);
|
|
377
|
+
settingsReloadTimer = undefined;
|
|
378
|
+
}
|
|
379
|
+
settingsReloadTimer = setTimeout(() => {
|
|
380
|
+
settingsReloadTimer = undefined;
|
|
381
|
+
void applyPlanModeSettings(generation, undefined, false);
|
|
382
|
+
}, 75);
|
|
383
|
+
};
|
|
384
|
+
|
|
385
|
+
const startPlanModeSettingsWatch = (generation: number) => {
|
|
386
|
+
stopPlanModeSettingsWatch();
|
|
387
|
+
if (dependencies.readSettings) return;
|
|
388
|
+
const pathToWatch = explicitPlanModeSettingsPath ?? planModeSettingsPath();
|
|
389
|
+
try {
|
|
390
|
+
const directory = dirname(pathToWatch);
|
|
391
|
+
const fileName = basename(pathToWatch);
|
|
392
|
+
const watcher = watch(directory, { persistent: false }, (event, changedFile) => {
|
|
393
|
+
if (event !== "rename" && event !== "change") return;
|
|
394
|
+
if (!changedFile || changedFile.toString() !== fileName) return;
|
|
395
|
+
schedulePlanModeSettingsReload(generation);
|
|
396
|
+
});
|
|
397
|
+
watcher.on("error", () => {
|
|
398
|
+
stopPlanModeSettingsWatch();
|
|
399
|
+
});
|
|
400
|
+
settingsWatch = watcher;
|
|
401
|
+
} catch {
|
|
402
|
+
stopPlanModeSettingsWatch();
|
|
403
|
+
}
|
|
404
|
+
};
|
|
405
|
+
|
|
312
406
|
pi.on("session_start", async (event, ctx) => {
|
|
313
407
|
const generation = ++menuGeneration;
|
|
314
408
|
refreshStateBeforeFirstAgentStart = event.reason === "new";
|
|
@@ -320,13 +414,9 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
320
414
|
settings = { thinkingLevel: "inherit" };
|
|
321
415
|
restoreState(ctx);
|
|
322
416
|
implementationRetention.restore(state.activeImplementation);
|
|
323
|
-
|
|
417
|
+
await applyPlanModeSettings(generation, ctx, true);
|
|
324
418
|
if (generation !== menuGeneration || menuController.signal.aborted) return;
|
|
325
|
-
|
|
326
|
-
else if (loadedSettings.kind === "invalid") {
|
|
327
|
-
ctx.ui.notify(`pi-plan-mode settings ignored: ${loadedSettings.reason}`, "warning");
|
|
328
|
-
}
|
|
329
|
-
if (loadedSettings.notice) ctx.ui.notify(loadedSettings.notice, "warning");
|
|
419
|
+
startPlanModeSettingsWatch(generation);
|
|
330
420
|
const persistFlagActivation = pi.getFlag("plan") === true && !state.enabled;
|
|
331
421
|
if (persistFlagActivation) {
|
|
332
422
|
state = state.savedPlan
|
|
@@ -376,6 +466,7 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
376
466
|
restoreTools();
|
|
377
467
|
restoreThinkingLevel();
|
|
378
468
|
}
|
|
469
|
+
stopPlanModeSettingsWatch();
|
|
379
470
|
clearUi(ctx);
|
|
380
471
|
});
|
|
381
472
|
|
|
@@ -609,6 +700,27 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
609
700
|
return completedPlanIsCurrent(intent) && readyPresentationIntent?.nonce === intent.nonce;
|
|
610
701
|
}
|
|
611
702
|
|
|
703
|
+
function togglePlanMode(ctx: ExtensionContext) {
|
|
704
|
+
if (state.enabled) {
|
|
705
|
+
ctx.ui.notify(planModeDisableNotification(), "info");
|
|
706
|
+
exitPlanMode(ctx);
|
|
707
|
+
return;
|
|
708
|
+
}
|
|
709
|
+
if (savedPlanBlocksNewWorkflow(ctx, state.savedPlan !== undefined)) return;
|
|
710
|
+
enterPlanMode(ctx);
|
|
711
|
+
ctx.ui.notify("Plan mode enabled. I will explore and plan, but not modify files.", "info");
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
function planModeDisableNotification() {
|
|
715
|
+
return state.activeImplementation
|
|
716
|
+
? "Active implementation plan cleared."
|
|
717
|
+
: state.savedPlan
|
|
718
|
+
? "Saved plan cleared."
|
|
719
|
+
: state.latestPlan
|
|
720
|
+
? "Plan mode disabled. Proposed plan discarded."
|
|
721
|
+
: "Plan mode disabled.";
|
|
722
|
+
}
|
|
723
|
+
|
|
612
724
|
function requestFinalPlan(ctx: ExtensionContext) {
|
|
613
725
|
if (!state.enabled) {
|
|
614
726
|
ctx.ui.notify("Plan mode is not active. Use /plan first.", "warning");
|
|
@@ -817,7 +929,9 @@ export default function planMode(pi: ExtensionAPI, dependencies: PlanModeDepende
|
|
|
817
929
|
isCurrent,
|
|
818
930
|
settingsPath: dependencies.settingsPath,
|
|
819
931
|
onSaved: (saved) => {
|
|
820
|
-
if (isCurrent())
|
|
932
|
+
if (!isCurrent()) return;
|
|
933
|
+
settings = saved;
|
|
934
|
+
applyPlanModeShortcut(configuredPlanModeToggleShortcut(saved));
|
|
821
935
|
},
|
|
822
936
|
...(dependencies.readSettings
|
|
823
937
|
? { readSettings: async () => dependencies.readSettings?.() ?? { kind: "missing" } }
|
package/src/settings-menu.ts
CHANGED
|
@@ -7,7 +7,9 @@ import { PLAN_MODE_QUESTION_TOOL_NAME } from "./question-tool.js";
|
|
|
7
7
|
import {
|
|
8
8
|
configuredImplementationPlanRetention,
|
|
9
9
|
configuredPlanExportPath,
|
|
10
|
+
configuredPlanModeToggleShortcut,
|
|
10
11
|
IMPLEMENTATION_PLAN_RETENTIONS,
|
|
12
|
+
normalizeKeyId,
|
|
11
13
|
PLAN_MODE_THINKING_LEVELS,
|
|
12
14
|
type PlanModeSettings,
|
|
13
15
|
type PlanModeSettingsLoadResult,
|
|
@@ -41,7 +43,7 @@ export interface PlanModeSettingsMenuOptions {
|
|
|
41
43
|
onSaved(settings: PlanModeSettings): void;
|
|
42
44
|
}
|
|
43
45
|
|
|
44
|
-
type Screen = "settings" | "tools" | "export";
|
|
46
|
+
type Screen = "settings" | "tools" | "export" | "shortcut";
|
|
45
47
|
type Action =
|
|
46
48
|
| "set-thinking"
|
|
47
49
|
| "open-tools"
|
|
@@ -49,7 +51,9 @@ type Action =
|
|
|
49
51
|
| "reset-tools"
|
|
50
52
|
| "set-retention"
|
|
51
53
|
| "open-export"
|
|
52
|
-
| "set-export"
|
|
54
|
+
| "set-export"
|
|
55
|
+
| "open-shortcut"
|
|
56
|
+
| "set-shortcut";
|
|
53
57
|
|
|
54
58
|
export async function showPlanModeSettings(
|
|
55
59
|
ctx: ExtensionContext,
|
|
@@ -124,6 +128,13 @@ export async function showPlanModeSettings(
|
|
|
124
128
|
currentValue: safeTerminalText(configuredPlanExportPath(state.settings)),
|
|
125
129
|
action: "open-export",
|
|
126
130
|
},
|
|
131
|
+
{
|
|
132
|
+
id: "toggleShortcut",
|
|
133
|
+
label: "Plan mode shortcut",
|
|
134
|
+
description: "Set the global shortcut used to toggle Plan mode.",
|
|
135
|
+
currentValue: configuredPlanModeToggleShortcut(state.settings) ?? "none",
|
|
136
|
+
action: "open-shortcut",
|
|
137
|
+
},
|
|
127
138
|
],
|
|
128
139
|
},
|
|
129
140
|
tools: ({ state }) => ({
|
|
@@ -162,6 +173,19 @@ export async function showPlanModeSettings(
|
|
|
162
173
|
hint: "back",
|
|
163
174
|
};
|
|
164
175
|
},
|
|
176
|
+
shortcut: ({ state }) => ({
|
|
177
|
+
kind: "input",
|
|
178
|
+
title: "Plan mode shortcut",
|
|
179
|
+
lines: [
|
|
180
|
+
`Configured: ${configuredPlanModeToggleShortcut(state.settings) ?? "none"}`,
|
|
181
|
+
"Use Pi key identifiers.",
|
|
182
|
+
"Submit an empty value to clear the shortcut.",
|
|
183
|
+
"When unset, Plan mode has no global shortcut.",
|
|
184
|
+
],
|
|
185
|
+
placeholder: configuredPlanModeToggleShortcut(state.settings) ?? "",
|
|
186
|
+
action: "set-shortcut",
|
|
187
|
+
hint: "back",
|
|
188
|
+
}),
|
|
165
189
|
},
|
|
166
190
|
actions: {
|
|
167
191
|
"set-thinking": async ({ ctx: actionCtx, value, signal }) => {
|
|
@@ -201,6 +225,27 @@ export async function showPlanModeSettings(
|
|
|
201
225
|
);
|
|
202
226
|
return result.kind === "stay" ? { kind: "to", screen: "settings" } : result;
|
|
203
227
|
},
|
|
228
|
+
"open-shortcut": async () => ({ kind: "to", screen: "shortcut" }),
|
|
229
|
+
"set-shortcut": async ({ ctx: actionCtx, value, signal }) => {
|
|
230
|
+
const raw = value?.trim() || null;
|
|
231
|
+
if (raw && !normalizeKeyId(raw)) {
|
|
232
|
+
actionCtx.ui.notify(
|
|
233
|
+
`Invalid key identifier: ${safeTerminalText(raw)}. Use Pi key identifiers like ctrl+alt+p.`,
|
|
234
|
+
"warning",
|
|
235
|
+
);
|
|
236
|
+
return { kind: "stay" as const };
|
|
237
|
+
}
|
|
238
|
+
const toggleShortcut = raw as PlanModeSettingsPatch["toggleShortcut"];
|
|
239
|
+
const result = await savePatch(
|
|
240
|
+
actionCtx,
|
|
241
|
+
{ toggleShortcut },
|
|
242
|
+
signal,
|
|
243
|
+
toggleShortcut
|
|
244
|
+
? `Plan mode shortcut: ${safeTerminalText(toggleShortcut)}.`
|
|
245
|
+
: "Plan mode shortcut cleared (no global shortcut).",
|
|
246
|
+
);
|
|
247
|
+
return result.kind === "stay" ? { kind: "to", screen: "settings" } : result;
|
|
248
|
+
},
|
|
204
249
|
"toggle-tool": async ({ ctx: actionCtx, state, itemId, selected, signal }) => {
|
|
205
250
|
const tool = tools.find((candidate) => candidate.name === itemId);
|
|
206
251
|
if (!tool || !canSelectToolInPlanMode(tool)) return { kind: "rejected" };
|
package/src/settings.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { constants } from "node:fs";
|
|
|
3
3
|
import { mkdir, open, rename, rm, writeFile } from "node:fs/promises";
|
|
4
4
|
import { basename, dirname, join } from "node:path";
|
|
5
5
|
import { getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
6
|
+
import type { KeyId } from "@earendil-works/pi-tui";
|
|
6
7
|
import {
|
|
7
8
|
SAFE_GH_SUBCOMMAND_PATHS,
|
|
8
9
|
SAFE_GIT_SUBCOMMANDS,
|
|
@@ -30,6 +31,60 @@ export const IMPLEMENTATION_PLAN_RETENTIONS = [
|
|
|
30
31
|
"clear-after-first-run",
|
|
31
32
|
] as const;
|
|
32
33
|
export const DEFAULT_PLAN_EXPORT_PATH = "PLAN.md";
|
|
34
|
+
const MODIFIERS = new Set(["ctrl", "shift", "alt", "super"]);
|
|
35
|
+
const BASE_KEYS = new Set([
|
|
36
|
+
..."abcdefghijklmnopqrstuvwxyz0123456789",
|
|
37
|
+
"`",
|
|
38
|
+
"-",
|
|
39
|
+
"=",
|
|
40
|
+
"[",
|
|
41
|
+
"]",
|
|
42
|
+
"\\",
|
|
43
|
+
";",
|
|
44
|
+
"'",
|
|
45
|
+
",",
|
|
46
|
+
".",
|
|
47
|
+
"/",
|
|
48
|
+
"!",
|
|
49
|
+
"@",
|
|
50
|
+
"#",
|
|
51
|
+
"$",
|
|
52
|
+
"%",
|
|
53
|
+
"^",
|
|
54
|
+
"&",
|
|
55
|
+
"*",
|
|
56
|
+
"(",
|
|
57
|
+
")",
|
|
58
|
+
"_",
|
|
59
|
+
"+",
|
|
60
|
+
"|",
|
|
61
|
+
"~",
|
|
62
|
+
"{",
|
|
63
|
+
"}",
|
|
64
|
+
":",
|
|
65
|
+
"<",
|
|
66
|
+
">",
|
|
67
|
+
"?",
|
|
68
|
+
"escape",
|
|
69
|
+
"esc",
|
|
70
|
+
"enter",
|
|
71
|
+
"return",
|
|
72
|
+
"tab",
|
|
73
|
+
"space",
|
|
74
|
+
"backspace",
|
|
75
|
+
"delete",
|
|
76
|
+
"insert",
|
|
77
|
+
"clear",
|
|
78
|
+
"home",
|
|
79
|
+
"end",
|
|
80
|
+
"pageup",
|
|
81
|
+
"pagedown",
|
|
82
|
+
"up",
|
|
83
|
+
"down",
|
|
84
|
+
"left",
|
|
85
|
+
"right",
|
|
86
|
+
...Array.from({ length: 12 }, (_unused, index) => `f${index + 1}`),
|
|
87
|
+
]);
|
|
33
88
|
const MAX_PLAN_EXPORT_PATH_LENGTH = 4096;
|
|
34
89
|
|
|
35
90
|
export type PlanModeThinkingLevel = (typeof PLAN_MODE_THINKING_LEVELS)[number];
|
|
@@ -41,12 +96,14 @@ export interface PlanModeSettings {
|
|
|
41
96
|
implementationPlanRetention?: ImplementationPlanRetention;
|
|
42
97
|
defaultPlanExportPath?: string;
|
|
43
98
|
safeSubcommands?: SafeSubcommands;
|
|
99
|
+
toggleShortcut?: KeyId;
|
|
44
100
|
}
|
|
45
101
|
export interface PlanModeSettingsPatch {
|
|
46
102
|
thinkingLevel?: PlanModeThinkingLevel;
|
|
47
103
|
defaultPlanTools?: readonly string[] | null;
|
|
48
104
|
implementationPlanRetention?: ImplementationPlanRetention;
|
|
49
105
|
defaultPlanExportPath?: string | null;
|
|
106
|
+
toggleShortcut?: KeyId | null;
|
|
50
107
|
}
|
|
51
108
|
export interface UpdatePlanModeSettingsOptions {
|
|
52
109
|
settingsPath?: string;
|
|
@@ -110,6 +167,11 @@ export function normalizePlanModeSettings(value: unknown): PlanModeSettings | un
|
|
|
110
167
|
if (!defaultPlanExportPath) return undefined;
|
|
111
168
|
settings.defaultPlanExportPath = defaultPlanExportPath;
|
|
112
169
|
}
|
|
170
|
+
if (Object.hasOwn(value, "toggleShortcut")) {
|
|
171
|
+
const toggleShortcut = normalizeKeyId(Reflect.get(value, "toggleShortcut"));
|
|
172
|
+
if (!toggleShortcut) return undefined;
|
|
173
|
+
settings.toggleShortcut = toggleShortcut;
|
|
174
|
+
}
|
|
113
175
|
if (Object.hasOwn(value, "safeSubcommands")) {
|
|
114
176
|
const safeSubcommands = normalizeSafeSubcommands(Reflect.get(value, "safeSubcommands"));
|
|
115
177
|
if (!safeSubcommands) return undefined;
|
|
@@ -145,6 +207,27 @@ function normalizePlanExportPath(value: unknown) {
|
|
|
145
207
|
return normalized;
|
|
146
208
|
}
|
|
147
209
|
|
|
210
|
+
export function normalizeKeyId(value: unknown): KeyId | undefined {
|
|
211
|
+
if (typeof value !== "string") return undefined;
|
|
212
|
+
const normalized = value.trim().toLowerCase();
|
|
213
|
+
const base = [...BASE_KEYS]
|
|
214
|
+
.sort((left, right) => right.length - left.length)
|
|
215
|
+
.find((candidate) => normalized === candidate || normalized.endsWith(`+${candidate}`));
|
|
216
|
+
if (!base) return undefined;
|
|
217
|
+
const prefix = normalized.slice(0, normalized.length - base.length);
|
|
218
|
+
if (!prefix) return base as KeyId;
|
|
219
|
+
if (/^f(?:[1-9]|1[0-2])$/.test(base) || !prefix.endsWith("+")) return undefined;
|
|
220
|
+
const modifiers = prefix.slice(0, -1).split("+");
|
|
221
|
+
if (
|
|
222
|
+
modifiers.length === 0 ||
|
|
223
|
+
modifiers.some((modifier) => !MODIFIERS.has(modifier)) ||
|
|
224
|
+
new Set(modifiers).size !== modifiers.length
|
|
225
|
+
) {
|
|
226
|
+
return undefined;
|
|
227
|
+
}
|
|
228
|
+
return normalized as KeyId;
|
|
229
|
+
}
|
|
230
|
+
|
|
148
231
|
function normalizeSafeSubcommands(value: unknown): SafeSubcommands | undefined {
|
|
149
232
|
if (!isSettingsDocument(value)) return undefined;
|
|
150
233
|
if (Object.keys(value).some((key) => key !== "git" && key !== "gh")) return undefined;
|
|
@@ -227,6 +310,10 @@ export function updatePlanModeSettings(
|
|
|
227
310
|
else if (patch.defaultPlanExportPath !== undefined) {
|
|
228
311
|
updated.defaultPlanExportPath = patch.defaultPlanExportPath;
|
|
229
312
|
}
|
|
313
|
+
if (patch.toggleShortcut === null) delete updated.toggleShortcut;
|
|
314
|
+
else if (patch.toggleShortcut !== undefined) {
|
|
315
|
+
updated.toggleShortcut = patch.toggleShortcut;
|
|
316
|
+
}
|
|
230
317
|
const settings = normalizePlanModeSettings(updated);
|
|
231
318
|
if (!settings) throw invalidSettingsError(settingsPath, "invalid settings shape");
|
|
232
319
|
await publishSettings(settingsPath, updated, options.signal, options.beforeRename);
|
|
@@ -404,3 +491,7 @@ export function configuredImplementationPlanRetention(
|
|
|
404
491
|
export function configuredPlanExportPath(settings: PlanModeSettings) {
|
|
405
492
|
return settings.defaultPlanExportPath ?? DEFAULT_PLAN_EXPORT_PATH;
|
|
406
493
|
}
|
|
494
|
+
|
|
495
|
+
export function configuredPlanModeToggleShortcut(settings: PlanModeSettings): KeyId | undefined {
|
|
496
|
+
return settings.toggleShortcut;
|
|
497
|
+
}
|