@narumitw/pi-plan-mode 0.49.3 → 0.50.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/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
+ }