@narumitw/pi-btw 0.58.1 → 0.59.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 +2 -0
- package/dist/index.ts +284 -365
- package/dist/index.ts.map +3 -3
- package/package.json +52 -53
- package/src/bring-to-main.ts +494 -547
- package/src/btw.ts +814 -856
- package/src/fullscreen-ui.ts +673 -674
- package/src/keybindings.ts +228 -270
- package/src/main-tree-picker.ts +309 -318
- package/src/menu.ts +416 -454
- package/src/settings.ts +203 -219
- package/src/side-thread.ts +173 -201
- package/src/text.ts +21 -23
- package/src/transcript-markdown.ts +65 -0
- package/src/transcript-pager.ts +611 -662
package/src/menu.ts
CHANGED
|
@@ -1,509 +1,471 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
ExtensionCommandContext,
|
|
3
|
-
KeybindingsManager,
|
|
4
|
-
Theme,
|
|
5
|
-
} from "@earendil-works/pi-coding-agent";
|
|
1
|
+
import type { ExtensionCommandContext, KeybindingsManager, Theme } from "@earendil-works/pi-coding-agent";
|
|
6
2
|
import type { Component, TUI } from "@earendil-works/pi-tui";
|
|
7
3
|
import type { MenuContext, RunMenuResult } from "@narumitw/pi-tui-kit";
|
|
8
4
|
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
5
|
+
BTW_SHORTCUT_ACTIONS,
|
|
6
|
+
type BtwShortcutAction,
|
|
7
|
+
normalizeBtwKey,
|
|
8
|
+
resolveBtwShortcuts,
|
|
9
|
+
validateBtwShortcutEdit,
|
|
14
10
|
} from "./keybindings.js";
|
|
15
11
|
import {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
12
|
+
type BtwSettings,
|
|
13
|
+
type BtwSettingsPatch,
|
|
14
|
+
btwSettingsPath,
|
|
15
|
+
effectiveFullscreenCopyOnSelect,
|
|
16
|
+
effectiveRememberThinkingLevelChanges,
|
|
17
|
+
readBtwSettings,
|
|
18
|
+
type UpdateBtwSettingsOptions,
|
|
19
|
+
updateBtwSettings,
|
|
24
20
|
} from "./settings.js";
|
|
25
21
|
import { BTW_THINKING_LEVELS, type BtwThinkingLevel } from "./side-thread.js";
|
|
26
22
|
import { formatKeyLabel, sanitizeSingleLine } from "./text.js";
|
|
27
23
|
|
|
28
24
|
interface BtwMenuState {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
25
|
+
kind: "valid" | "invalid";
|
|
26
|
+
settings: BtwSettings;
|
|
27
|
+
reason?: string;
|
|
32
28
|
}
|
|
33
29
|
|
|
34
30
|
export interface BtwResumeThreadSummary {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
31
|
+
id: string;
|
|
32
|
+
title: string;
|
|
33
|
+
questionCount: number;
|
|
38
34
|
}
|
|
39
35
|
|
|
40
36
|
export interface ShowBtwCommandMenuOptions {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
patch: BtwSettingsPatch,
|
|
48
|
-
options: UpdateBtwSettingsOptions,
|
|
49
|
-
) => Promise<BtwSettings>;
|
|
37
|
+
currentThinkingLevel: BtwThinkingLevel;
|
|
38
|
+
availableThinkingLevels: readonly BtwThinkingLevel[];
|
|
39
|
+
resumeThreads?: readonly BtwResumeThreadSummary[];
|
|
40
|
+
settingsPath?: string;
|
|
41
|
+
readSettings?: typeof readBtwSettings;
|
|
42
|
+
updateSettings?: (patch: BtwSettingsPatch, options: UpdateBtwSettingsOptions) => Promise<BtwSettings>;
|
|
50
43
|
}
|
|
51
44
|
|
|
52
|
-
export type BtwCommandMenuResult =
|
|
53
|
-
| "start"
|
|
54
|
-
| "tree"
|
|
55
|
-
| "closed"
|
|
56
|
-
| { kind: "resume"; threadId: string };
|
|
45
|
+
export type BtwCommandMenuResult = "start" | "tree" | "closed" | { kind: "resume"; threadId: string };
|
|
57
46
|
|
|
58
47
|
type BtwMenuScreen = "main" | "resume" | "settings" | "invalid" | "shortcut" | "shortcut-input";
|
|
59
48
|
type BtwMenuAction =
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
49
|
+
| "start"
|
|
50
|
+
| "start-tree"
|
|
51
|
+
| "resume"
|
|
52
|
+
| "set-thinking"
|
|
53
|
+
| "set-remember"
|
|
54
|
+
| "set-fullscreen-copy"
|
|
55
|
+
| "edit-shortcut"
|
|
56
|
+
| "save-shortcut"
|
|
57
|
+
| "reset-shortcut";
|
|
69
58
|
const SAME_AS_MAIN_THREAD = "Same as main thread";
|
|
70
59
|
type BtwCustomOptions = Parameters<ExtensionCommandContext["ui"]["custom"]>[1];
|
|
71
60
|
|
|
72
61
|
type BtwCustomFactory<T> = (
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
62
|
+
tui: TUI,
|
|
63
|
+
theme: Theme,
|
|
64
|
+
keybindings: KeybindingsManager,
|
|
65
|
+
done: (result: T) => void,
|
|
77
66
|
) => Component;
|
|
78
67
|
|
|
79
68
|
export async function showBtwCommandMenu(
|
|
80
|
-
|
|
81
|
-
|
|
69
|
+
ctx: ExtensionCommandContext,
|
|
70
|
+
options: ShowBtwCommandMenuOptions,
|
|
82
71
|
): Promise<BtwCommandMenuResult> {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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
|
-
validateCurrent: (settings) => {
|
|
153
|
-
const conflict = validate(settings);
|
|
154
|
-
if (conflict) throw new Error(conflict);
|
|
155
|
-
},
|
|
156
|
-
},
|
|
157
|
-
);
|
|
158
|
-
if (signal.aborted) return { kind: "rejected" } as const;
|
|
159
|
-
notifySafely(ctx, "Pi BTW shortcut saved; applies when opening or resuming BTW.", "info");
|
|
160
|
-
return { kind: "back" } as const;
|
|
161
|
-
} catch (error) {
|
|
162
|
-
if (!signal.aborted) notifySaveFailure(ctx, error);
|
|
163
|
-
return { kind: "rejected" } as const;
|
|
164
|
-
}
|
|
165
|
-
};
|
|
72
|
+
if (ctx.mode !== "tui") return "closed";
|
|
73
|
+
const { defineMenu, runMenu } = await import("@narumitw/pi-tui-kit");
|
|
74
|
+
if (ctx.signal?.aborted) return "closed";
|
|
75
|
+
const settingsPath = options.settingsPath ?? btwSettingsPath();
|
|
76
|
+
const readSettings = options.readSettings ?? readBtwSettings;
|
|
77
|
+
const updateSettings = options.updateSettings ?? updateBtwSettings;
|
|
78
|
+
const levels =
|
|
79
|
+
options.availableThinkingLevels.length > 0
|
|
80
|
+
? [...options.availableThinkingLevels]
|
|
81
|
+
: (["off"] satisfies BtwThinkingLevel[]);
|
|
82
|
+
const displaySettingsPath = sanitizeSingleLine(settingsPath);
|
|
83
|
+
const resumeThreads = options.resumeThreads ?? [];
|
|
84
|
+
let startSelected = false;
|
|
85
|
+
let treeSelected = false;
|
|
86
|
+
let resumedThreadId: string | undefined;
|
|
87
|
+
let keybindings: KeybindingsManager | undefined;
|
|
88
|
+
let shortcut: BtwShortcutAction = "exit";
|
|
89
|
+
const shortcutLabels: Record<BtwShortcutAction, string> = {
|
|
90
|
+
exit: "Exit shortcut",
|
|
91
|
+
cycleThinkingLevel: "Cycle thinking level shortcut",
|
|
92
|
+
bringToMain: "Bring to main shortcut",
|
|
93
|
+
};
|
|
94
|
+
const shortcutValue = (settings: BtwSettings, action: BtwShortcutAction): string => {
|
|
95
|
+
if (!keybindings) return "Default";
|
|
96
|
+
const effective = resolveBtwShortcuts(settings.keybindings, keybindings, effectiveFullscreenCopyOnSelect(settings));
|
|
97
|
+
const configured = settings.keybindings?.[action];
|
|
98
|
+
if (configured !== undefined && !effective.keys[action].includes(configured)) {
|
|
99
|
+
return `Fallback (${effective.label(action)}; saved ${formatKeyLabel(configured)})`;
|
|
100
|
+
}
|
|
101
|
+
const source = configured === undefined ? (action === "cycleThinkingLevel" ? "Inherit Pi" : "Default") : "Custom";
|
|
102
|
+
return `${source} (${effective.label(action)})`;
|
|
103
|
+
};
|
|
104
|
+
const saveShortcut = async (state: BtwMenuState, value: string | undefined, signal: AbortSignal) => {
|
|
105
|
+
if (!keybindings || state.kind !== "valid" || signal.aborted) return { kind: "rejected" } as const;
|
|
106
|
+
const action = shortcut;
|
|
107
|
+
const manager = keybindings;
|
|
108
|
+
const validate = (settings: BtwSettings) =>
|
|
109
|
+
validateBtwShortcutEdit(
|
|
110
|
+
action,
|
|
111
|
+
value,
|
|
112
|
+
settings.keybindings ?? {},
|
|
113
|
+
manager,
|
|
114
|
+
effectiveFullscreenCopyOnSelect(settings),
|
|
115
|
+
);
|
|
116
|
+
const error = validate(state.settings);
|
|
117
|
+
if (error) {
|
|
118
|
+
notifySafely(ctx, error, "error");
|
|
119
|
+
return { kind: "rejected" } as const;
|
|
120
|
+
}
|
|
121
|
+
try {
|
|
122
|
+
await updateSettings(
|
|
123
|
+
{ keybindings: { [action]: value === undefined ? undefined : normalizeBtwKey(value) } },
|
|
124
|
+
{
|
|
125
|
+
settingsPath,
|
|
126
|
+
signal,
|
|
127
|
+
validateCurrent: (settings) => {
|
|
128
|
+
const conflict = validate(settings);
|
|
129
|
+
if (conflict) throw new Error(conflict);
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
);
|
|
133
|
+
if (signal.aborted) return { kind: "rejected" } as const;
|
|
134
|
+
notifySafely(ctx, "Pi BTW shortcut saved; applies when opening or resuming BTW.", "info");
|
|
135
|
+
return { kind: "back" } as const;
|
|
136
|
+
} catch (error) {
|
|
137
|
+
if (!signal.aborted) notifySaveFailure(ctx, error);
|
|
138
|
+
return { kind: "rejected" } as const;
|
|
139
|
+
}
|
|
140
|
+
};
|
|
166
141
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
const value = effectiveRememberThinkingLevelChanges(settings) ? "On" : "Off";
|
|
188
|
-
return settings.thinkingLevel === undefined ? `${value} (fixed levels only)` : value;
|
|
189
|
-
};
|
|
142
|
+
const loadState = async (): Promise<BtwMenuState> => {
|
|
143
|
+
const loaded = await readSettings(settingsPath);
|
|
144
|
+
if (loaded.kind === "invalid") {
|
|
145
|
+
return { kind: "invalid", settings: {}, reason: loaded.reason };
|
|
146
|
+
}
|
|
147
|
+
return { kind: "valid", settings: loaded.kind === "loaded" ? loaded.settings : {} };
|
|
148
|
+
};
|
|
149
|
+
const currentMainThinkingLevel = clampToAvailableThinkingLevel(options.currentThinkingLevel, levels);
|
|
150
|
+
const displayThinkingLevel = (settings: BtwSettings): string =>
|
|
151
|
+
settings.thinkingLevel === undefined
|
|
152
|
+
? SAME_AS_MAIN_THREAD
|
|
153
|
+
: clampToAvailableThinkingLevel(settings.thinkingLevel, levels);
|
|
154
|
+
const displayThinkingSummary = (settings: BtwSettings): string =>
|
|
155
|
+
settings.thinkingLevel === undefined
|
|
156
|
+
? `${SAME_AS_MAIN_THREAD} (currently ${currentMainThinkingLevel})`
|
|
157
|
+
: displayThinkingLevel(settings);
|
|
158
|
+
const displayRememberSummary = (settings: BtwSettings): string => {
|
|
159
|
+
const value = effectiveRememberThinkingLevelChanges(settings) ? "On" : "Off";
|
|
160
|
+
return settings.thinkingLevel === undefined ? `${value} (fixed levels only)` : value;
|
|
161
|
+
};
|
|
190
162
|
|
|
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
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
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
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
if (signal.aborted) return { kind: "rejected" };
|
|
381
|
-
notifySafely(ctx, `Copy selection automatically: ${value}.`, "info");
|
|
382
|
-
return { kind: "stay" };
|
|
383
|
-
} catch (error) {
|
|
384
|
-
if (!signal.aborted) notifySaveFailure(ctx, error);
|
|
385
|
-
return { kind: "rejected" };
|
|
386
|
-
}
|
|
387
|
-
},
|
|
388
|
-
},
|
|
389
|
-
});
|
|
163
|
+
const menu = defineMenu<BtwMenuState, BtwMenuScreen, BtwMenuAction, MenuContext>({
|
|
164
|
+
start: "main",
|
|
165
|
+
screens: {
|
|
166
|
+
main: ({ state }) => ({
|
|
167
|
+
kind: "actions",
|
|
168
|
+
title: "Pi BTW",
|
|
169
|
+
lines: [
|
|
170
|
+
`Thinking: ${displayThinkingSummary(state.settings)} · Remember changes: ${displayRememberSummary(state.settings)}`,
|
|
171
|
+
`Copy on select: ${effectiveFullscreenCopyOnSelect(state.settings) ? "On" : "Off"}`,
|
|
172
|
+
],
|
|
173
|
+
items: [
|
|
174
|
+
{
|
|
175
|
+
id: "start",
|
|
176
|
+
label: "Start side thread",
|
|
177
|
+
description: "Open an empty side thread",
|
|
178
|
+
action: "start",
|
|
179
|
+
},
|
|
180
|
+
{
|
|
181
|
+
id: "start-tree",
|
|
182
|
+
label: "Start from main thread tree…",
|
|
183
|
+
description: "Choose context without switching the main branch",
|
|
184
|
+
action: "start-tree",
|
|
185
|
+
},
|
|
186
|
+
...(resumeThreads.length > 0
|
|
187
|
+
? [
|
|
188
|
+
{
|
|
189
|
+
id: "resume" as const,
|
|
190
|
+
label: "Resume side thread",
|
|
191
|
+
description: "Continue an in-memory side thread",
|
|
192
|
+
to: "resume" as const,
|
|
193
|
+
},
|
|
194
|
+
]
|
|
195
|
+
: []),
|
|
196
|
+
{
|
|
197
|
+
id: "settings",
|
|
198
|
+
label: "Settings",
|
|
199
|
+
description: "Choose thinking, keybindings, and selection copying",
|
|
200
|
+
to: state.kind === "invalid" ? "invalid" : "settings",
|
|
201
|
+
},
|
|
202
|
+
],
|
|
203
|
+
hint: "close",
|
|
204
|
+
}),
|
|
205
|
+
resume: () => ({
|
|
206
|
+
kind: "choice",
|
|
207
|
+
title: "Resume BTW side thread",
|
|
208
|
+
enableSearch: true,
|
|
209
|
+
items: resumeThreads.map((thread) => ({
|
|
210
|
+
id: thread.id,
|
|
211
|
+
label: thread.title,
|
|
212
|
+
description: `${thread.questionCount} ${thread.questionCount === 1 ? "question" : "questions"}`,
|
|
213
|
+
})),
|
|
214
|
+
action: "resume",
|
|
215
|
+
viewportSize: 10,
|
|
216
|
+
hint: "back",
|
|
217
|
+
}),
|
|
218
|
+
settings: ({ state }) => ({
|
|
219
|
+
kind: "settings",
|
|
220
|
+
title: "Pi BTW Settings",
|
|
221
|
+
lines: [`User settings · ${displaySettingsPath}`],
|
|
222
|
+
items: [
|
|
223
|
+
{
|
|
224
|
+
id: "thinkingLevel",
|
|
225
|
+
label: "Thinking level",
|
|
226
|
+
description: `Set the starting level for future pi-btw side threads. Currently ${currentMainThinkingLevel}.`,
|
|
227
|
+
currentValue: displayThinkingLevel(state.settings),
|
|
228
|
+
values: [SAME_AS_MAIN_THREAD, ...levels],
|
|
229
|
+
action: "set-thinking",
|
|
230
|
+
},
|
|
231
|
+
{
|
|
232
|
+
id: "rememberThinkingLevelChanges",
|
|
233
|
+
label: "Remember thinking level changes",
|
|
234
|
+
description: "Save shortcut changes for fixed thinking levels to pi-btw.json.",
|
|
235
|
+
currentValue: effectiveRememberThinkingLevelChanges(state.settings) ? "On" : "Off",
|
|
236
|
+
values: ["On", "Off"],
|
|
237
|
+
action: "set-remember",
|
|
238
|
+
},
|
|
239
|
+
{
|
|
240
|
+
id: "fullscreenCopyOnSelect",
|
|
241
|
+
label: "Copy selection automatically",
|
|
242
|
+
description: "Copy mouse selections immediately instead of with the configured copy key.",
|
|
243
|
+
currentValue: effectiveFullscreenCopyOnSelect(state.settings) ? "On" : "Off",
|
|
244
|
+
values: ["On", "Off"],
|
|
245
|
+
action: "set-fullscreen-copy",
|
|
246
|
+
},
|
|
247
|
+
...BTW_SHORTCUT_ACTIONS.map((action) => ({
|
|
248
|
+
id: action,
|
|
249
|
+
label: shortcutLabels[action],
|
|
250
|
+
description: "Edit a BTW-only key combination or restore its default. Ctrl+C always hard-cancels.",
|
|
251
|
+
currentValue: shortcutValue(state.settings, action),
|
|
252
|
+
action: "edit-shortcut" as const,
|
|
253
|
+
})),
|
|
254
|
+
],
|
|
255
|
+
}),
|
|
256
|
+
shortcut: ({ state }) => ({
|
|
257
|
+
kind: "actions",
|
|
258
|
+
title: shortcutLabels[shortcut],
|
|
259
|
+
lines: [shortcutValue(state.settings, shortcut), "Ctrl+C always hard-cancels BTW."],
|
|
260
|
+
items: [
|
|
261
|
+
{ id: "edit", label: "Edit key combination…", to: "shortcut-input" },
|
|
262
|
+
{ id: "reset", label: "Restore default", action: "reset-shortcut" },
|
|
263
|
+
],
|
|
264
|
+
hint: "back",
|
|
265
|
+
}),
|
|
266
|
+
"shortcut-input": () => ({
|
|
267
|
+
kind: "input",
|
|
268
|
+
title: shortcutLabels[shortcut],
|
|
269
|
+
lines: ["Type a key name, not the shortcut itself. For example: ctrl+q or f6."],
|
|
270
|
+
placeholder: "Key combination",
|
|
271
|
+
action: "save-shortcut",
|
|
272
|
+
hint: "back",
|
|
273
|
+
}),
|
|
274
|
+
invalid: ({ state }) => ({
|
|
275
|
+
kind: "detail",
|
|
276
|
+
title: "Pi BTW Settings · Read only",
|
|
277
|
+
lines: [
|
|
278
|
+
`Invalid settings file. Fix ${displaySettingsPath} before saving.`,
|
|
279
|
+
sanitizeSingleLine(state.reason ?? "The settings file is invalid."),
|
|
280
|
+
],
|
|
281
|
+
hint: "back",
|
|
282
|
+
}),
|
|
283
|
+
},
|
|
284
|
+
actions: {
|
|
285
|
+
"edit-shortcut": ({ itemId }) => {
|
|
286
|
+
if (!BTW_SHORTCUT_ACTIONS.includes(itemId as BtwShortcutAction)) return { kind: "rejected" };
|
|
287
|
+
shortcut = itemId as BtwShortcutAction;
|
|
288
|
+
return { kind: "to", screen: "shortcut" };
|
|
289
|
+
},
|
|
290
|
+
"save-shortcut": ({ state, value, signal }) => saveShortcut(state, value?.trim() ?? "", signal),
|
|
291
|
+
"reset-shortcut": ({ state, signal }) => saveShortcut(state, undefined, signal),
|
|
292
|
+
start: async () => {
|
|
293
|
+
startSelected = true;
|
|
294
|
+
return { kind: "close" };
|
|
295
|
+
},
|
|
296
|
+
"start-tree": async () => {
|
|
297
|
+
treeSelected = true;
|
|
298
|
+
return { kind: "close" };
|
|
299
|
+
},
|
|
300
|
+
resume: async ({ itemId }: { itemId: string }) => {
|
|
301
|
+
if (!resumeThreads.some((thread) => thread.id === itemId)) {
|
|
302
|
+
return { kind: "rejected" } as const;
|
|
303
|
+
}
|
|
304
|
+
resumedThreadId = itemId;
|
|
305
|
+
return { kind: "close" } as const;
|
|
306
|
+
},
|
|
307
|
+
"set-thinking": async ({ value, signal }) => {
|
|
308
|
+
if (!value) return { kind: "rejected" };
|
|
309
|
+
const patch =
|
|
310
|
+
value === SAME_AS_MAIN_THREAD
|
|
311
|
+
? ({ thinkingLevel: undefined } satisfies BtwSettingsPatch)
|
|
312
|
+
: levels.includes(value as BtwThinkingLevel)
|
|
313
|
+
? ({ thinkingLevel: value as BtwThinkingLevel } satisfies BtwSettingsPatch)
|
|
314
|
+
: undefined;
|
|
315
|
+
if (!patch) return { kind: "rejected" };
|
|
316
|
+
try {
|
|
317
|
+
await updateSettings(patch, { settingsPath, signal });
|
|
318
|
+
if (signal.aborted) return { kind: "rejected" };
|
|
319
|
+
notifySafely(ctx, `Pi BTW thinking level: ${value}.`, "info");
|
|
320
|
+
return { kind: "stay" };
|
|
321
|
+
} catch (error) {
|
|
322
|
+
if (!signal.aborted) notifySaveFailure(ctx, error);
|
|
323
|
+
return { kind: "rejected" };
|
|
324
|
+
}
|
|
325
|
+
},
|
|
326
|
+
"set-remember": async ({ value, signal }) => {
|
|
327
|
+
if (value !== "On" && value !== "Off") return { kind: "rejected" };
|
|
328
|
+
try {
|
|
329
|
+
await updateSettings({ rememberThinkingLevelChanges: value === "On" }, { settingsPath, signal });
|
|
330
|
+
if (signal.aborted) return { kind: "rejected" };
|
|
331
|
+
notifySafely(ctx, `Remember thinking level changes: ${value}.`, "info");
|
|
332
|
+
return { kind: "stay" };
|
|
333
|
+
} catch (error) {
|
|
334
|
+
if (!signal.aborted) notifySaveFailure(ctx, error);
|
|
335
|
+
return { kind: "rejected" };
|
|
336
|
+
}
|
|
337
|
+
},
|
|
338
|
+
"set-fullscreen-copy": async ({ value, signal }) => {
|
|
339
|
+
if (value !== "On" && value !== "Off") return { kind: "rejected" };
|
|
340
|
+
try {
|
|
341
|
+
await updateSettings({ fullscreenCopyOnSelect: value === "On" }, { settingsPath, signal });
|
|
342
|
+
if (signal.aborted) return { kind: "rejected" };
|
|
343
|
+
notifySafely(ctx, `Copy selection automatically: ${value}.`, "info");
|
|
344
|
+
return { kind: "stay" };
|
|
345
|
+
} catch (error) {
|
|
346
|
+
if (!signal.aborted) notifySaveFailure(ctx, error);
|
|
347
|
+
return { kind: "rejected" };
|
|
348
|
+
}
|
|
349
|
+
},
|
|
350
|
+
},
|
|
351
|
+
});
|
|
390
352
|
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
353
|
+
const result = await runBtwMenuPreservingEditor(
|
|
354
|
+
ctx,
|
|
355
|
+
(menuContext) => runMenu(menuContext, menu, { getState: loadState }),
|
|
356
|
+
(manager) => {
|
|
357
|
+
keybindings = manager;
|
|
358
|
+
},
|
|
359
|
+
);
|
|
360
|
+
if (result.kind !== "closed" || result.reason !== "close") return "closed";
|
|
361
|
+
if (resumedThreadId) return { kind: "resume", threadId: resumedThreadId };
|
|
362
|
+
if (treeSelected) return "tree";
|
|
363
|
+
return startSelected ? "start" : "closed";
|
|
402
364
|
}
|
|
403
365
|
|
|
404
366
|
export async function showBtwCustomPreservingEditor<T>(
|
|
405
|
-
|
|
406
|
-
|
|
367
|
+
ctx: ExtensionCommandContext,
|
|
368
|
+
factory: BtwCustomFactory<T>,
|
|
407
369
|
): Promise<T | undefined> {
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
370
|
+
let liveEditorText = ctx.ui.getEditorText();
|
|
371
|
+
let completed = false;
|
|
372
|
+
const result = await ctx.ui.custom<T>((tui, theme, keybindings, done) =>
|
|
373
|
+
factory(tui, theme, keybindings, (value) => {
|
|
374
|
+
try {
|
|
375
|
+
liveEditorText = ctx.ui.getEditorText();
|
|
376
|
+
} catch {
|
|
377
|
+
// Keep completion finite if session replacement invalidates the editor context.
|
|
378
|
+
}
|
|
379
|
+
completed = true;
|
|
380
|
+
done(value);
|
|
381
|
+
}),
|
|
382
|
+
);
|
|
383
|
+
if (completed) {
|
|
384
|
+
try {
|
|
385
|
+
if (ctx.ui.getEditorText() !== liveEditorText) ctx.ui.setEditorText(liveEditorText);
|
|
386
|
+
} catch {
|
|
387
|
+
// A replaced context owns a different editor and must not receive stale restoration.
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
return result;
|
|
429
391
|
}
|
|
430
392
|
|
|
431
393
|
export async function runBtwMenuPreservingEditor(
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
394
|
+
ctx: ExtensionCommandContext,
|
|
395
|
+
run: (menuContext: MenuContext) => Promise<RunMenuResult>,
|
|
396
|
+
onKeybindings?: (keybindings: KeybindingsManager) => void,
|
|
435
397
|
): Promise<RunMenuResult> {
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
398
|
+
let liveEditorText = ctx.ui.getEditorText();
|
|
399
|
+
let completed = false;
|
|
400
|
+
const ui = new Proxy(ctx.ui, {
|
|
401
|
+
get(target, property) {
|
|
402
|
+
if (property === "custom") {
|
|
403
|
+
return <Value>(factory: BtwCustomFactory<Value>, customOptions?: BtwCustomOptions) =>
|
|
404
|
+
target.custom<Value>((tui, theme, keybindings, done) => {
|
|
405
|
+
onKeybindings?.(keybindings);
|
|
406
|
+
return factory(tui, theme, keybindings, (value) => {
|
|
407
|
+
try {
|
|
408
|
+
liveEditorText = target.getEditorText();
|
|
409
|
+
} catch {
|
|
410
|
+
// Keep completion finite if session replacement invalidates the editor context.
|
|
411
|
+
}
|
|
412
|
+
completed = true;
|
|
413
|
+
done(value);
|
|
414
|
+
});
|
|
415
|
+
}, customOptions);
|
|
416
|
+
}
|
|
417
|
+
const value = Reflect.get(target, property, target) as unknown;
|
|
418
|
+
return typeof value === "function" ? value.bind(target) : value;
|
|
419
|
+
},
|
|
420
|
+
});
|
|
421
|
+
const result = await run({ mode: ctx.mode, hasUI: ctx.hasUI, ui });
|
|
422
|
+
if (result.kind !== "stale" && completed) {
|
|
423
|
+
try {
|
|
424
|
+
if (ctx.ui.getEditorText() !== liveEditorText) ctx.ui.setEditorText(liveEditorText);
|
|
425
|
+
} catch {
|
|
426
|
+
// A replaced context owns a different editor and must not receive stale restoration.
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
return result;
|
|
468
430
|
}
|
|
469
431
|
|
|
470
432
|
function clampToAvailableThinkingLevel(
|
|
471
|
-
|
|
472
|
-
|
|
433
|
+
requested: BtwThinkingLevel,
|
|
434
|
+
available: readonly BtwThinkingLevel[],
|
|
473
435
|
): BtwThinkingLevel {
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
436
|
+
if (available.includes(requested)) return requested;
|
|
437
|
+
const requestedIndex = BTW_THINKING_LEVELS.indexOf(requested);
|
|
438
|
+
for (let index = requestedIndex; index < BTW_THINKING_LEVELS.length; index += 1) {
|
|
439
|
+
const candidate = BTW_THINKING_LEVELS[index];
|
|
440
|
+
if (candidate && available.includes(candidate)) return candidate;
|
|
441
|
+
}
|
|
442
|
+
for (let index = requestedIndex - 1; index >= 0; index -= 1) {
|
|
443
|
+
const candidate = BTW_THINKING_LEVELS[index];
|
|
444
|
+
if (candidate && available.includes(candidate)) return candidate;
|
|
445
|
+
}
|
|
446
|
+
return available[0] ?? "off";
|
|
485
447
|
}
|
|
486
448
|
|
|
487
449
|
function notifySaveFailure(ctx: ExtensionCommandContext, error: unknown): void {
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
450
|
+
notifySafely(
|
|
451
|
+
ctx,
|
|
452
|
+
`Pi BTW settings were not saved; the previous value remains active: ${formatError(error)}`,
|
|
453
|
+
"error",
|
|
454
|
+
);
|
|
493
455
|
}
|
|
494
456
|
|
|
495
457
|
function notifySafely(
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
458
|
+
ctx: ExtensionCommandContext,
|
|
459
|
+
message: string,
|
|
460
|
+
level: Parameters<ExtensionCommandContext["ui"]["notify"]>[1],
|
|
499
461
|
): void {
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
462
|
+
try {
|
|
463
|
+
ctx.ui.notify(sanitizeSingleLine(message), level);
|
|
464
|
+
} catch {
|
|
465
|
+
// A completed save remains valid if its command context was replaced before notification.
|
|
466
|
+
}
|
|
505
467
|
}
|
|
506
468
|
|
|
507
469
|
function formatError(error: unknown): string {
|
|
508
|
-
|
|
470
|
+
return error instanceof Error ? error.message : String(error);
|
|
509
471
|
}
|