@narumitw/pi-btw 0.60.2 → 0.61.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 +21 -4
- package/dist/index.ts +1685 -1029
- package/dist/index.ts.map +4 -4
- package/docs/workflows.md +10 -2
- package/package.json +1 -1
- package/src/btw.ts +43 -160
- package/src/conversation-context.ts +74 -0
- package/src/fullscreen-ui.ts +196 -7
- package/src/main-thread-updates.ts +40 -0
- package/src/menu.ts +34 -2
- package/src/settings.ts +49 -0
- package/src/side-thread.ts +40 -37
- package/src/transcript-pager.ts +9 -1
- package/src/workspace-layout.ts +559 -0
package/src/fullscreen-ui.ts
CHANGED
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
Key,
|
|
13
13
|
type OverlayHandle,
|
|
14
14
|
parseKey,
|
|
15
|
+
type ScrollView,
|
|
15
16
|
type TUI,
|
|
16
17
|
TuiAltScreen,
|
|
17
18
|
type TuiInputListener,
|
|
@@ -19,7 +20,10 @@ import {
|
|
|
19
20
|
truncateToWidth,
|
|
20
21
|
} from "@earendil-works/pi-tui";
|
|
21
22
|
import { type BtwKeybindingOverrides, BtwPasteGuard, resolveBtwShortcuts, setBtwShortcuts } from "./keybindings.js";
|
|
23
|
+
import type { BtwMainThreadUpdateSubscription } from "./main-thread-updates.js";
|
|
24
|
+
import type { BtwLayout } from "./settings.js";
|
|
22
25
|
import { formatKeyLabel, sanitizeSingleLine } from "./text.js";
|
|
26
|
+
import { type BtwFullscreenLayoutComponent, BtwMainThreadInput, BtwSplitPane } from "./workspace-layout.js";
|
|
23
27
|
|
|
24
28
|
type BtwCustomOptions = Parameters<ExtensionCommandContext["ui"]["custom"]>[1];
|
|
25
29
|
type BtwCustomFactory<T> = (
|
|
@@ -32,17 +36,23 @@ type BtwCustomFactory<T> = (
|
|
|
32
36
|
type BtwFullscreenTui = TUI & {
|
|
33
37
|
flash?: (message: string, durationMs?: number) => void;
|
|
34
38
|
setLayoutRoot(component: Component | undefined): void;
|
|
39
|
+
hasFocusedOverlay?(): boolean;
|
|
35
40
|
addInputListenerBeforeAll?(listener: TuiInputListener): () => void;
|
|
36
41
|
addInputListenerBeforeViewport?(listener: TuiInputListener): () => void;
|
|
42
|
+
setViewportTarget?(scrollView: ScrollView | undefined): void;
|
|
37
43
|
};
|
|
38
44
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
45
|
+
type FocusInspectableTui = TUI & {
|
|
46
|
+
getFocusedComponent?(): Component | null;
|
|
47
|
+
};
|
|
42
48
|
|
|
43
49
|
export interface BtwFullscreenOptions {
|
|
44
50
|
keybindings?: BtwKeybindingOverrides;
|
|
45
51
|
copyOnSelect?: boolean;
|
|
52
|
+
layout?: BtwLayout;
|
|
53
|
+
sidePaneRatio?: number;
|
|
54
|
+
persistSidePaneRatio?(ratio: number, signal: AbortSignal): Promise<void>;
|
|
55
|
+
subscribeMainThreadUpdates?: BtwMainThreadUpdateSubscription;
|
|
46
56
|
}
|
|
47
57
|
|
|
48
58
|
export type BtwFullscreenTuiFactory = (
|
|
@@ -158,6 +168,12 @@ class BtwTuiAltScreen extends TuiAltScreen {
|
|
|
158
168
|
return this.isOverlayFocused();
|
|
159
169
|
}
|
|
160
170
|
|
|
171
|
+
setViewportTarget(scrollView: ScrollView | undefined): void {
|
|
172
|
+
if (!scrollView) return;
|
|
173
|
+
const layout = Reflect.get(this, "currentLayout") as { primaryScrollView?: ScrollView } | undefined;
|
|
174
|
+
if (layout) layout.primaryScrollView = scrollView;
|
|
175
|
+
}
|
|
176
|
+
|
|
161
177
|
override addInputListener(listener: TuiInputListener): () => void {
|
|
162
178
|
let listeners = btwInputListeners.get(this);
|
|
163
179
|
if (!listeners) {
|
|
@@ -419,7 +435,14 @@ class BtwFullscreenHost<T> implements Component {
|
|
|
419
435
|
private parentRestoreQueued = false;
|
|
420
436
|
private parentRestorePromise: Promise<void> | undefined;
|
|
421
437
|
private cleanupError: unknown;
|
|
438
|
+
private removeMainThreadUpdateListener: (() => void) | undefined;
|
|
439
|
+
private mainThreadRefreshTimer: ReturnType<typeof setTimeout> | undefined;
|
|
440
|
+
private readonly mainThreadInput: BtwMainThreadInput;
|
|
422
441
|
private readonly lifetimeController = new AbortController();
|
|
442
|
+
private readonly pendingSidePaneWrites = new Set<Promise<void>>();
|
|
443
|
+
private sidePaneRatio: number | undefined;
|
|
444
|
+
private sidePaneWriteGeneration = 0;
|
|
445
|
+
private confirmedSidePaneWriteGeneration = 0;
|
|
423
446
|
|
|
424
447
|
constructor(
|
|
425
448
|
private readonly parent: TUI,
|
|
@@ -431,6 +454,16 @@ class BtwFullscreenHost<T> implements Component {
|
|
|
431
454
|
private readonly createTui: BtwFullscreenTuiFactory,
|
|
432
455
|
private readonly options: BtwFullscreenOptions,
|
|
433
456
|
) {
|
|
457
|
+
const initialMainInput = getFocusedComponent(parent);
|
|
458
|
+
this.mainThreadInput = new BtwMainThreadInput(
|
|
459
|
+
initialMainInput,
|
|
460
|
+
() => {
|
|
461
|
+
const target = getFocusedComponent(parent);
|
|
462
|
+
return target === this ? initialMainInput : target;
|
|
463
|
+
},
|
|
464
|
+
() => this.fullscreen?.requestRender(),
|
|
465
|
+
);
|
|
466
|
+
this.sidePaneRatio = options.sidePaneRatio;
|
|
434
467
|
queueMicrotask(() => void this.start());
|
|
435
468
|
}
|
|
436
469
|
|
|
@@ -464,6 +497,7 @@ class BtwFullscreenHost<T> implements Component {
|
|
|
464
497
|
this.fullscreen = this.createTui(this.parent, this.theme, this.keybindings, this.options);
|
|
465
498
|
this.fullscreenCreated = true;
|
|
466
499
|
this.fullscreen.start();
|
|
500
|
+
this.watchMainThreadUpdates();
|
|
467
501
|
const shortcuts = resolveBtwShortcuts(
|
|
468
502
|
this.options.keybindings,
|
|
469
503
|
this.keybindings,
|
|
@@ -507,8 +541,11 @@ class BtwFullscreenHost<T> implements Component {
|
|
|
507
541
|
}
|
|
508
542
|
return { consume: true };
|
|
509
543
|
});
|
|
510
|
-
|
|
544
|
+
const value = await this.run(this.createContext());
|
|
545
|
+
await this.waitForSidePaneWrites();
|
|
546
|
+
outcome = { kind: "completed", value };
|
|
511
547
|
} catch (error) {
|
|
548
|
+
await this.waitForSidePaneWrites();
|
|
512
549
|
outcome = { kind: "failed", error };
|
|
513
550
|
}
|
|
514
551
|
|
|
@@ -547,7 +584,32 @@ class BtwFullscreenHost<T> implements Component {
|
|
|
547
584
|
});
|
|
548
585
|
}
|
|
549
586
|
|
|
587
|
+
private watchMainThreadUpdates(): void {
|
|
588
|
+
if ((this.options.layout ?? "fullscreen") === "fullscreen") return;
|
|
589
|
+
this.removeMainThreadUpdateListener = this.options.subscribeMainThreadUpdates?.(() => {
|
|
590
|
+
this.mainThreadInput.refreshTarget();
|
|
591
|
+
if (this.mainThreadRefreshTimer || this.disposed || this.finished) return;
|
|
592
|
+
this.mainThreadRefreshTimer = setTimeout(() => {
|
|
593
|
+
this.mainThreadRefreshTimer = undefined;
|
|
594
|
+
if (!this.disposed && !this.finished) this.fullscreen?.requestRender();
|
|
595
|
+
}, 0);
|
|
596
|
+
this.mainThreadRefreshTimer.unref();
|
|
597
|
+
});
|
|
598
|
+
}
|
|
599
|
+
|
|
550
600
|
private restoreParent(): void {
|
|
601
|
+
if (this.mainThreadRefreshTimer) {
|
|
602
|
+
clearTimeout(this.mainThreadRefreshTimer);
|
|
603
|
+
this.mainThreadRefreshTimer = undefined;
|
|
604
|
+
}
|
|
605
|
+
const removeMainThreadUpdateListener = this.removeMainThreadUpdateListener;
|
|
606
|
+
this.removeMainThreadUpdateListener = undefined;
|
|
607
|
+
try {
|
|
608
|
+
removeMainThreadUpdateListener?.();
|
|
609
|
+
} catch (error) {
|
|
610
|
+
this.cleanupError ??= error;
|
|
611
|
+
}
|
|
612
|
+
this.mainThreadInput.dispose();
|
|
551
613
|
const removeUpstreamAbortListener = this.removeUpstreamAbortListener;
|
|
552
614
|
this.removeUpstreamAbortListener = undefined;
|
|
553
615
|
try {
|
|
@@ -629,6 +691,8 @@ class BtwFullscreenHost<T> implements Component {
|
|
|
629
691
|
return new Promise<Value>((resolve, reject) => {
|
|
630
692
|
let component: (Component & { dispose?(): void }) | undefined;
|
|
631
693
|
let overlay: OverlayHandle | undefined;
|
|
694
|
+
let splitPane: BtwSplitPane | undefined;
|
|
695
|
+
let removePaneFocusListener: (() => void) | undefined;
|
|
632
696
|
let mounted = false;
|
|
633
697
|
let layoutMounted = false;
|
|
634
698
|
let factorySettled = false;
|
|
@@ -649,12 +713,21 @@ class BtwFullscreenHost<T> implements Component {
|
|
|
649
713
|
};
|
|
650
714
|
const unmount = () => {
|
|
651
715
|
let cleanupError: unknown;
|
|
716
|
+
const removeFocusListener = removePaneFocusListener;
|
|
717
|
+
removePaneFocusListener = undefined;
|
|
718
|
+
try {
|
|
719
|
+
removeFocusListener?.();
|
|
720
|
+
} catch (error) {
|
|
721
|
+
cleanupError = error;
|
|
722
|
+
}
|
|
723
|
+
splitPane?.dispose();
|
|
724
|
+
splitPane = undefined;
|
|
652
725
|
try {
|
|
653
726
|
if (overlay) overlay.hide();
|
|
654
727
|
else if (mounted && layoutMounted) fullscreen.setLayoutRoot(undefined);
|
|
655
728
|
else if (mounted && component) fullscreen.removeChild(component);
|
|
656
729
|
} catch (error) {
|
|
657
|
-
cleanupError
|
|
730
|
+
cleanupError ??= error;
|
|
658
731
|
}
|
|
659
732
|
if (overlay || mounted) {
|
|
660
733
|
try {
|
|
@@ -728,7 +801,7 @@ class BtwFullscreenHost<T> implements Component {
|
|
|
728
801
|
return;
|
|
729
802
|
}
|
|
730
803
|
Promise.resolve(created)
|
|
731
|
-
.then((value) => {
|
|
804
|
+
.then(async (value) => {
|
|
732
805
|
component = value;
|
|
733
806
|
factorySettled = true;
|
|
734
807
|
if (promiseSettled) {
|
|
@@ -739,6 +812,22 @@ class BtwFullscreenHost<T> implements Component {
|
|
|
739
812
|
complete();
|
|
740
813
|
return;
|
|
741
814
|
}
|
|
815
|
+
const workspaceLayout = this.options.layout ?? "fullscreen";
|
|
816
|
+
if (!options?.overlay && workspaceLayout !== "fullscreen") {
|
|
817
|
+
await this.waitForSidePaneWrites();
|
|
818
|
+
if (promiseSettled) {
|
|
819
|
+
disposeComponent();
|
|
820
|
+
return;
|
|
821
|
+
}
|
|
822
|
+
if (closed) {
|
|
823
|
+
complete();
|
|
824
|
+
return;
|
|
825
|
+
}
|
|
826
|
+
if (this.disposed || this.finished || this.fullscreen !== fullscreen) {
|
|
827
|
+
fail(new FullscreenUiDisposedError());
|
|
828
|
+
return;
|
|
829
|
+
}
|
|
830
|
+
}
|
|
742
831
|
if (options?.overlay) {
|
|
743
832
|
const overlayOptions =
|
|
744
833
|
typeof options.overlayOptions === "function" ? options.overlayOptions() : options.overlayOptions;
|
|
@@ -747,7 +836,38 @@ class BtwFullscreenHost<T> implements Component {
|
|
|
747
836
|
} else {
|
|
748
837
|
fullscreen.clear();
|
|
749
838
|
mounted = true;
|
|
750
|
-
if (
|
|
839
|
+
if (workspaceLayout !== "fullscreen") {
|
|
840
|
+
layoutMounted = true;
|
|
841
|
+
const sideLayout = isFullscreenLayoutComponent(component) ? component.getFullscreenLayout() : component;
|
|
842
|
+
splitPane = new BtwSplitPane({
|
|
843
|
+
sideComponent: component,
|
|
844
|
+
sideLayout,
|
|
845
|
+
mainThread: this.parent,
|
|
846
|
+
mainLayout: getParentFullscreenLayout(this.parent),
|
|
847
|
+
mainInput: this.mainThreadInput,
|
|
848
|
+
sideScrollView: isFullscreenLayoutComponent(component) ? component.getPrimaryScrollView?.() : undefined,
|
|
849
|
+
layout: workspaceLayout,
|
|
850
|
+
theme: this.theme,
|
|
851
|
+
terminalColumns: () => fullscreen.terminal.columns,
|
|
852
|
+
terminalRows: () => fullscreen.terminal.rows,
|
|
853
|
+
hasFocusedOverlay: () => fullscreen.hasFocusedOverlay?.() ?? false,
|
|
854
|
+
setFocus: (target) => fullscreen.setFocus(target),
|
|
855
|
+
setViewportTarget: (target) => fullscreen.setViewportTarget?.(target),
|
|
856
|
+
requestRender: () => fullscreen.requestRender(),
|
|
857
|
+
sidePaneRatio: this.sidePaneRatio,
|
|
858
|
+
...(this.options.persistSidePaneRatio
|
|
859
|
+
? { persistSidePaneRatio: (ratio: number) => this.persistSidePaneRatio(ratio) }
|
|
860
|
+
: {}),
|
|
861
|
+
});
|
|
862
|
+
const addPaneFocusListener =
|
|
863
|
+
fullscreen.addInputListenerBeforeViewport?.bind(fullscreen) ??
|
|
864
|
+
fullscreen.addInputListener.bind(fullscreen);
|
|
865
|
+
removePaneFocusListener = addPaneFocusListener((data) => {
|
|
866
|
+
const consumed = splitPane?.handleTerminalInput(data);
|
|
867
|
+
return consumed ? { consume: true } : undefined;
|
|
868
|
+
});
|
|
869
|
+
fullscreen.setLayoutRoot(splitPane.getFullscreenLayout());
|
|
870
|
+
} else if (isFullscreenLayoutComponent(component)) {
|
|
751
871
|
layoutMounted = true;
|
|
752
872
|
fullscreen.setLayoutRoot(component.getFullscreenLayout());
|
|
753
873
|
} else {
|
|
@@ -760,6 +880,75 @@ class BtwFullscreenHost<T> implements Component {
|
|
|
760
880
|
.catch(fail);
|
|
761
881
|
});
|
|
762
882
|
}
|
|
883
|
+
|
|
884
|
+
private persistSidePaneRatio(ratio: number): Promise<void> {
|
|
885
|
+
const persist = this.options.persistSidePaneRatio;
|
|
886
|
+
if (!persist) {
|
|
887
|
+
this.sidePaneRatio = ratio;
|
|
888
|
+
return Promise.resolve();
|
|
889
|
+
}
|
|
890
|
+
const writeGeneration = ++this.sidePaneWriteGeneration;
|
|
891
|
+
let task!: Promise<void>;
|
|
892
|
+
task = Promise.resolve()
|
|
893
|
+
.then(() => persist(ratio, this.lifetimeController.signal))
|
|
894
|
+
.then(() => {
|
|
895
|
+
if (this.lifetimeController.signal.aborted || writeGeneration <= this.confirmedSidePaneWriteGeneration) {
|
|
896
|
+
return;
|
|
897
|
+
}
|
|
898
|
+
this.confirmedSidePaneWriteGeneration = writeGeneration;
|
|
899
|
+
this.sidePaneRatio = ratio;
|
|
900
|
+
})
|
|
901
|
+
.catch((error: unknown) => {
|
|
902
|
+
if (!this.lifetimeController.signal.aborted) {
|
|
903
|
+
const message = sanitizeSingleLine(
|
|
904
|
+
`Pi BTW pane width was not saved; the previous value remains active: ${formatError(error)}`,
|
|
905
|
+
);
|
|
906
|
+
try {
|
|
907
|
+
this.ctx.ui.notify(message, "error");
|
|
908
|
+
} catch {
|
|
909
|
+
// A replaced command context must not prevent rollback or cleanup.
|
|
910
|
+
}
|
|
911
|
+
this.fullscreen?.flash?.(message);
|
|
912
|
+
}
|
|
913
|
+
throw error;
|
|
914
|
+
})
|
|
915
|
+
.finally(() => this.pendingSidePaneWrites.delete(task));
|
|
916
|
+
this.pendingSidePaneWrites.add(task);
|
|
917
|
+
return task;
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
private async waitForSidePaneWrites(): Promise<void> {
|
|
921
|
+
while (this.pendingSidePaneWrites.size > 0) {
|
|
922
|
+
await Promise.allSettled([...this.pendingSidePaneWrites]);
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
function formatError(error: unknown): string {
|
|
928
|
+
return error instanceof Error ? error.message : String(error);
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
function getFocusedComponent(tui: TUI): Component | null {
|
|
932
|
+
return (tui as FocusInspectableTui).getFocusedComponent?.() ?? null;
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
// Pi does not expose the mounted fullscreen layout root. Reusing this runtime
|
|
936
|
+
// field lets Pi's own layout engine constrain its transcript and dock to the pane.
|
|
937
|
+
function getParentFullscreenLayout(tui: TUI): Component | undefined {
|
|
938
|
+
if (tui.mode !== "fullscreen") return undefined;
|
|
939
|
+
const layoutRoot = Reflect.get(tui, "layoutRoot") as unknown;
|
|
940
|
+
return isComponent(layoutRoot) ? layoutRoot : undefined;
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
function isComponent(value: unknown): value is Component {
|
|
944
|
+
return (
|
|
945
|
+
typeof value === "object" &&
|
|
946
|
+
value !== null &&
|
|
947
|
+
"render" in value &&
|
|
948
|
+
typeof value.render === "function" &&
|
|
949
|
+
"invalidate" in value &&
|
|
950
|
+
typeof value.invalidate === "function"
|
|
951
|
+
);
|
|
763
952
|
}
|
|
764
953
|
|
|
765
954
|
function isFullscreenLayoutComponent(component: Component): component is BtwFullscreenLayoutComponent {
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
export type BtwMainThreadUpdateSubscription = (listener: () => void) => () => void;
|
|
4
|
+
type SessionManager = ExtensionContext["sessionManager"];
|
|
5
|
+
|
|
6
|
+
export function registerBtwMainThreadUpdates(
|
|
7
|
+
pi: ExtensionAPI,
|
|
8
|
+
): (sessionManager: SessionManager, listener: () => void) => () => void {
|
|
9
|
+
const listeners = new WeakMap<SessionManager, Set<() => void>>();
|
|
10
|
+
const notify = (ctx: ExtensionContext) => {
|
|
11
|
+
for (const listener of listeners.get(ctx.sessionManager) ?? []) listener();
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
pi.on("session_info_changed", (_event, ctx) => notify(ctx));
|
|
15
|
+
pi.on("session_compact", (_event, ctx) => notify(ctx));
|
|
16
|
+
pi.on("session_tree", (_event, ctx) => notify(ctx));
|
|
17
|
+
pi.on("agent_start", (_event, ctx) => notify(ctx));
|
|
18
|
+
pi.on("agent_end", (_event, ctx) => notify(ctx));
|
|
19
|
+
pi.on("agent_settled", (_event, ctx) => notify(ctx));
|
|
20
|
+
pi.on("turn_start", (_event, ctx) => notify(ctx));
|
|
21
|
+
pi.on("turn_end", (_event, ctx) => notify(ctx));
|
|
22
|
+
pi.on("message_start", (_event, ctx) => notify(ctx));
|
|
23
|
+
pi.on("message_update", (_event, ctx) => notify(ctx));
|
|
24
|
+
pi.on("message_end", (_event, ctx) => notify(ctx));
|
|
25
|
+
pi.on("tool_execution_start", (_event, ctx) => notify(ctx));
|
|
26
|
+
pi.on("tool_execution_update", (_event, ctx) => notify(ctx));
|
|
27
|
+
pi.on("tool_execution_end", (_event, ctx) => notify(ctx));
|
|
28
|
+
pi.on("model_select", (_event, ctx) => notify(ctx));
|
|
29
|
+
pi.on("thinking_level_select", (_event, ctx) => notify(ctx));
|
|
30
|
+
|
|
31
|
+
return (sessionManager, listener) => {
|
|
32
|
+
const active = listeners.get(sessionManager) ?? new Set<() => void>();
|
|
33
|
+
active.add(listener);
|
|
34
|
+
listeners.set(sessionManager, active);
|
|
35
|
+
return () => {
|
|
36
|
+
active.delete(listener);
|
|
37
|
+
if (active.size === 0) listeners.delete(sessionManager);
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
}
|
package/src/menu.ts
CHANGED
|
@@ -15,9 +15,11 @@ import {
|
|
|
15
15
|
validateBtwShortcutEdit,
|
|
16
16
|
} from "./keybindings.js";
|
|
17
17
|
import {
|
|
18
|
+
type BtwLayout,
|
|
18
19
|
type BtwSettings,
|
|
19
20
|
type BtwSettingsPatch,
|
|
20
21
|
btwSettingsPath,
|
|
22
|
+
effectiveBtwLayout,
|
|
21
23
|
effectiveFullscreenCopyOnSelect,
|
|
22
24
|
effectiveRememberThinkingLevelChanges,
|
|
23
25
|
parseBtwModelReference,
|
|
@@ -65,10 +67,17 @@ type BtwMenuAction =
|
|
|
65
67
|
| "set-thinking"
|
|
66
68
|
| "set-remember"
|
|
67
69
|
| "set-fullscreen-copy"
|
|
70
|
+
| "set-layout"
|
|
68
71
|
| "edit-shortcut"
|
|
69
72
|
| "save-shortcut"
|
|
70
73
|
| "reset-shortcut";
|
|
71
74
|
const SAME_AS_MAIN_THREAD = "Same as main thread";
|
|
75
|
+
const BTW_LAYOUT_LABELS: Record<BtwLayout, string> = {
|
|
76
|
+
fullscreen: "Fullscreen",
|
|
77
|
+
"left-pane": "Side thread left",
|
|
78
|
+
"right-pane": "Side thread right",
|
|
79
|
+
};
|
|
80
|
+
const BTW_LAYOUT_VALUES = Object.values(BTW_LAYOUT_LABELS);
|
|
72
81
|
type BtwCustomOptions = Parameters<ExtensionCommandContext["ui"]["custom"]>[1];
|
|
73
82
|
|
|
74
83
|
type BtwCustomFactory<T> = (
|
|
@@ -331,7 +340,7 @@ export async function showBtwCommandMenu(
|
|
|
331
340
|
lines: [
|
|
332
341
|
`Model: ${displayModelValue(state.settings)}`,
|
|
333
342
|
`Thinking: ${displayThinkingSummary(state.settings)} · Remember changes: ${displayRememberSummary(state.settings)}`,
|
|
334
|
-
`Copy on select: ${effectiveFullscreenCopyOnSelect(state.settings) ? "On" : "Off"}`,
|
|
343
|
+
`Layout: ${BTW_LAYOUT_LABELS[effectiveBtwLayout(state.settings)]} · Copy on select: ${effectiveFullscreenCopyOnSelect(state.settings) ? "On" : "Off"}`,
|
|
335
344
|
],
|
|
336
345
|
items: [
|
|
337
346
|
{
|
|
@@ -359,7 +368,7 @@ export async function showBtwCommandMenu(
|
|
|
359
368
|
{
|
|
360
369
|
id: "settings",
|
|
361
370
|
label: "Settings",
|
|
362
|
-
description: "Choose model, thinking, keybindings, and selection copying",
|
|
371
|
+
description: "Choose model, thinking, layout, keybindings, and selection copying",
|
|
363
372
|
to: state.kind === "invalid" ? "invalid" : "settings",
|
|
364
373
|
},
|
|
365
374
|
],
|
|
@@ -414,6 +423,14 @@ export async function showBtwCommandMenu(
|
|
|
414
423
|
values: ["On", "Off"],
|
|
415
424
|
action: "set-fullscreen-copy",
|
|
416
425
|
},
|
|
426
|
+
{
|
|
427
|
+
id: "layout",
|
|
428
|
+
label: "Side-thread layout",
|
|
429
|
+
description: "Use the full workspace or place BTW beside the live, click-to-focus main thread.",
|
|
430
|
+
currentValue: BTW_LAYOUT_LABELS[effectiveBtwLayout(state.settings)],
|
|
431
|
+
values: BTW_LAYOUT_VALUES,
|
|
432
|
+
action: "set-layout",
|
|
433
|
+
},
|
|
417
434
|
...BTW_SHORTCUT_ACTIONS.map((action) => ({
|
|
418
435
|
id: action,
|
|
419
436
|
label: shortcutLabels[action],
|
|
@@ -555,6 +572,21 @@ export async function showBtwCommandMenu(
|
|
|
555
572
|
return { kind: "rejected" };
|
|
556
573
|
}
|
|
557
574
|
},
|
|
575
|
+
"set-layout": async ({ value, signal }) => {
|
|
576
|
+
const layout = Object.entries(BTW_LAYOUT_LABELS).find(([, label]) => label === value)?.[0] as
|
|
577
|
+
| BtwLayout
|
|
578
|
+
| undefined;
|
|
579
|
+
if (!layout) return { kind: "rejected" };
|
|
580
|
+
try {
|
|
581
|
+
await updateSettings({ layout }, { settingsPath, signal });
|
|
582
|
+
if (signal.aborted) return { kind: "rejected" };
|
|
583
|
+
notifySafely(ctx, `Pi BTW layout: ${BTW_LAYOUT_LABELS[layout]}. Applies when BTW next opens.`, "info");
|
|
584
|
+
return { kind: "stay" };
|
|
585
|
+
} catch (error) {
|
|
586
|
+
if (!signal.aborted) notifySaveFailure(ctx, error);
|
|
587
|
+
return { kind: "rejected" };
|
|
588
|
+
}
|
|
589
|
+
},
|
|
558
590
|
},
|
|
559
591
|
});
|
|
560
592
|
|
package/src/settings.ts
CHANGED
|
@@ -7,8 +7,14 @@ import { BTW_SHORTCUT_ACTIONS, type BtwKeybindingOverrides, normalizeBtwKey } fr
|
|
|
7
7
|
import { BTW_THINKING_LEVELS, type BtwThinkingLevel } from "./side-thread.js";
|
|
8
8
|
|
|
9
9
|
export const BTW_SETTINGS_FILE = "pi-btw.json";
|
|
10
|
+
export const BTW_LAYOUTS = ["fullscreen", "left-pane", "right-pane"] as const;
|
|
11
|
+
export type BtwLayout = (typeof BTW_LAYOUTS)[number];
|
|
12
|
+
export const DEFAULT_BTW_LAYOUT: BtwLayout = "fullscreen";
|
|
10
13
|
export const DEFAULT_FULLSCREEN_COPY_ON_SELECT = true;
|
|
11
14
|
export const DEFAULT_REMEMBER_THINKING_LEVEL_CHANGES = true;
|
|
15
|
+
export const DEFAULT_BTW_SIDE_PANE_RATIO = 0.5;
|
|
16
|
+
export const MIN_BTW_SIDE_PANE_RATIO = 0.2;
|
|
17
|
+
export const MAX_BTW_SIDE_PANE_RATIO = 0.8;
|
|
12
18
|
const MAX_SETTINGS_BYTES = 64 * 1024;
|
|
13
19
|
|
|
14
20
|
export interface BtwSettings {
|
|
@@ -17,6 +23,8 @@ export interface BtwSettings {
|
|
|
17
23
|
thinkingLevel?: BtwThinkingLevel;
|
|
18
24
|
rememberThinkingLevelChanges?: boolean;
|
|
19
25
|
fullscreenCopyOnSelect?: boolean;
|
|
26
|
+
layout?: BtwLayout;
|
|
27
|
+
sidePaneRatio?: number;
|
|
20
28
|
}
|
|
21
29
|
|
|
22
30
|
export type BtwSettingsLoadResult =
|
|
@@ -30,6 +38,8 @@ export interface BtwSettingsPatch {
|
|
|
30
38
|
thinkingLevel?: BtwThinkingLevel;
|
|
31
39
|
rememberThinkingLevelChanges?: boolean;
|
|
32
40
|
fullscreenCopyOnSelect?: boolean;
|
|
41
|
+
layout?: BtwLayout;
|
|
42
|
+
sidePaneRatio?: number;
|
|
33
43
|
}
|
|
34
44
|
|
|
35
45
|
export interface UpdateBtwSettingsOptions {
|
|
@@ -83,6 +93,16 @@ export function normalizeBtwSettings(value: unknown): BtwSettings | undefined {
|
|
|
83
93
|
if (typeof copyOnSelect !== "boolean") return undefined;
|
|
84
94
|
settings.fullscreenCopyOnSelect = copyOnSelect;
|
|
85
95
|
}
|
|
96
|
+
if (Object.hasOwn(value, "layout")) {
|
|
97
|
+
const layout = Reflect.get(value, "layout");
|
|
98
|
+
if (!isBtwLayout(layout)) return undefined;
|
|
99
|
+
settings.layout = layout;
|
|
100
|
+
}
|
|
101
|
+
if (Object.hasOwn(value, "sidePaneRatio")) {
|
|
102
|
+
const sidePaneRatio = Reflect.get(value, "sidePaneRatio");
|
|
103
|
+
if (!isBtwSidePaneRatio(sidePaneRatio)) return undefined;
|
|
104
|
+
settings.sidePaneRatio = sidePaneRatio;
|
|
105
|
+
}
|
|
86
106
|
return settings;
|
|
87
107
|
}
|
|
88
108
|
|
|
@@ -93,6 +113,10 @@ export function parseBtwModelReference(reference: string): { provider: string; m
|
|
|
93
113
|
return { provider: reference.slice(0, separator), modelId: reference.slice(separator + 1) };
|
|
94
114
|
}
|
|
95
115
|
|
|
116
|
+
export function effectiveBtwLayout(settings: BtwSettings): BtwLayout {
|
|
117
|
+
return settings.layout ?? DEFAULT_BTW_LAYOUT;
|
|
118
|
+
}
|
|
119
|
+
|
|
96
120
|
export function effectiveFullscreenCopyOnSelect(settings: BtwSettings): boolean {
|
|
97
121
|
return settings.fullscreenCopyOnSelect ?? DEFAULT_FULLSCREEN_COPY_ON_SELECT;
|
|
98
122
|
}
|
|
@@ -101,6 +125,10 @@ export function effectiveRememberThinkingLevelChanges(settings: BtwSettings): bo
|
|
|
101
125
|
return settings.rememberThinkingLevelChanges ?? DEFAULT_REMEMBER_THINKING_LEVEL_CHANGES;
|
|
102
126
|
}
|
|
103
127
|
|
|
128
|
+
export function effectiveBtwSidePaneRatio(settings: BtwSettings): number {
|
|
129
|
+
return isBtwSidePaneRatio(settings.sidePaneRatio) ? settings.sidePaneRatio : DEFAULT_BTW_SIDE_PANE_RATIO;
|
|
130
|
+
}
|
|
131
|
+
|
|
104
132
|
export async function readBtwSettings(settingsPath = btwSettingsPath()): Promise<BtwSettingsLoadResult> {
|
|
105
133
|
await awaitBtwSettingsWrites(settingsPath);
|
|
106
134
|
return readBtwSettingsUncoordinated(settingsPath);
|
|
@@ -270,6 +298,14 @@ function applyBtwSettingsPatch(current: SettingsDocument, patch: BtwSettingsPatc
|
|
|
270
298
|
if (patch.fullscreenCopyOnSelect === undefined) delete updated.fullscreenCopyOnSelect;
|
|
271
299
|
else updated.fullscreenCopyOnSelect = patch.fullscreenCopyOnSelect;
|
|
272
300
|
}
|
|
301
|
+
if (Object.hasOwn(patch, "layout")) {
|
|
302
|
+
if (patch.layout === undefined) delete updated.layout;
|
|
303
|
+
else updated.layout = patch.layout;
|
|
304
|
+
}
|
|
305
|
+
if (Object.hasOwn(patch, "sidePaneRatio")) {
|
|
306
|
+
if (patch.sidePaneRatio === undefined) delete updated.sidePaneRatio;
|
|
307
|
+
else updated.sidePaneRatio = patch.sidePaneRatio;
|
|
308
|
+
}
|
|
273
309
|
return updated;
|
|
274
310
|
}
|
|
275
311
|
|
|
@@ -281,6 +317,19 @@ function isBtwThinkingLevel(value: unknown): value is BtwThinkingLevel {
|
|
|
281
317
|
return BTW_THINKING_LEVELS.includes(value as BtwThinkingLevel);
|
|
282
318
|
}
|
|
283
319
|
|
|
320
|
+
function isBtwLayout(value: unknown): value is BtwLayout {
|
|
321
|
+
return BTW_LAYOUTS.includes(value as BtwLayout);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function isBtwSidePaneRatio(value: unknown): value is number {
|
|
325
|
+
return (
|
|
326
|
+
typeof value === "number" &&
|
|
327
|
+
Number.isFinite(value) &&
|
|
328
|
+
value >= MIN_BTW_SIDE_PANE_RATIO &&
|
|
329
|
+
value <= MAX_BTW_SIDE_PANE_RATIO
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
|
|
284
333
|
function invalidSettingsError(settingsPath: string, reason: string): Error {
|
|
285
334
|
return new Error(`pi-btw settings at ${settingsPath} are invalid: ${reason}`);
|
|
286
335
|
}
|