pi-chassis 0.0.0-stage → 0.7.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/CHANGELOG.md +118 -0
- package/LICENSE +21 -0
- package/README.md +51 -2
- package/THIRD_PARTY_NOTICES +30 -0
- package/dist/chassis/alert.d.ts +50 -0
- package/dist/chassis/alert.js +107 -0
- package/dist/chassis/api.d.ts +411 -0
- package/dist/chassis/api.js +676 -0
- package/dist/chassis/bash-output.d.ts +89 -0
- package/dist/chassis/bash-output.js +157 -0
- package/dist/chassis/brand.d.ts +99 -0
- package/dist/chassis/brand.js +79 -0
- package/dist/chassis/client.d.ts +87 -0
- package/dist/chassis/client.js +271 -0
- package/dist/chassis/commands.d.ts +60 -0
- package/dist/chassis/commands.js +211 -0
- package/dist/chassis/editor.d.ts +70 -0
- package/dist/chassis/editor.js +213 -0
- package/dist/chassis/footer.d.ts +14 -0
- package/dist/chassis/footer.js +136 -0
- package/dist/chassis/header.d.ts +12 -0
- package/dist/chassis/header.js +38 -0
- package/dist/chassis/home.d.ts +48 -0
- package/dist/chassis/home.js +184 -0
- package/dist/chassis/icons.d.ts +203 -0
- package/dist/chassis/icons.js +190 -0
- package/dist/chassis/index.d.ts +21 -0
- package/dist/chassis/index.js +582 -0
- package/dist/chassis/layout.d.ts +40 -0
- package/dist/chassis/layout.js +264 -0
- package/dist/chassis/message-modes.d.ts +21 -0
- package/dist/chassis/message-modes.js +49 -0
- package/dist/chassis/messages.d.ts +75 -0
- package/dist/chassis/messages.js +392 -0
- package/dist/chassis/modes.d.ts +21 -0
- package/dist/chassis/modes.js +99 -0
- package/dist/chassis/palette.d.ts +84 -0
- package/dist/chassis/palette.js +235 -0
- package/dist/chassis/peek.d.ts +61 -0
- package/dist/chassis/peek.js +156 -0
- package/dist/chassis/prompt-row.d.ts +42 -0
- package/dist/chassis/prompt-row.js +68 -0
- package/dist/chassis/provider-view.d.ts +34 -0
- package/dist/chassis/provider-view.js +107 -0
- package/dist/chassis/provider.d.ts +69 -0
- package/dist/chassis/provider.js +73 -0
- package/dist/chassis/readonly-command.d.ts +19 -0
- package/dist/chassis/readonly-command.js +454 -0
- package/dist/chassis/sanitize.d.ts +42 -0
- package/dist/chassis/sanitize.js +103 -0
- package/dist/chassis/session-info.d.ts +39 -0
- package/dist/chassis/session-info.js +107 -0
- package/dist/chassis/sidebar.d.ts +48 -0
- package/dist/chassis/sidebar.js +273 -0
- package/dist/chassis/state.d.ts +38 -0
- package/dist/chassis/state.js +19 -0
- package/dist/chassis/telemetry.d.ts +60 -0
- package/dist/chassis/telemetry.js +191 -0
- package/dist/chassis/tools.d.ts +112 -0
- package/dist/chassis/tools.js +588 -0
- package/dist/chassis/version.d.ts +2 -0
- package/dist/chassis/version.js +20 -0
- package/dist/diff/git-diff.d.ts +71 -0
- package/dist/diff/git-diff.js +325 -0
- package/dist/diff/host.d.ts +34 -0
- package/dist/diff/host.js +50 -0
- package/dist/diff/index.d.ts +13 -0
- package/dist/diff/index.js +86 -0
- package/dist/diff/render.d.ts +67 -0
- package/dist/diff/render.js +196 -0
- package/dist/diff/types.d.ts +60 -0
- package/dist/diff/types.js +4 -0
- package/dist/diff/view.d.ts +118 -0
- package/dist/diff/view.js +528 -0
- package/dist/git/flows.d.ts +92 -0
- package/dist/git/flows.js +415 -0
- package/dist/git/forge.d.ts +26 -0
- package/dist/git/forge.js +41 -0
- package/dist/git/index.d.ts +20 -0
- package/dist/git/index.js +121 -0
- package/dist/git/repo.d.ts +64 -0
- package/dist/git/repo.js +186 -0
- package/dist/git/run.d.ts +38 -0
- package/dist/git/run.js +127 -0
- package/dist/git/settings.d.ts +8 -0
- package/dist/git/settings.js +38 -0
- package/dist/git/text.d.ts +51 -0
- package/dist/git/text.js +109 -0
- package/dist/widgets/container/detect.d.ts +23 -0
- package/dist/widgets/container/detect.js +67 -0
- package/dist/widgets/container/index.d.ts +9 -0
- package/dist/widgets/container/index.js +26 -0
- package/dist/widgets/mcp/index.d.ts +13 -0
- package/dist/widgets/mcp/index.js +50 -0
- package/dist/widgets/mcp/status.d.ts +50 -0
- package/dist/widgets/mcp/status.js +70 -0
- package/dist/widgets/session-todos/index.d.ts +12 -0
- package/dist/widgets/session-todos/index.js +52 -0
- package/dist/widgets/subagents/index.d.ts +16 -0
- package/dist/widgets/subagents/index.js +173 -0
- package/dist/widgets/subagents/status.d.ts +61 -0
- package/dist/widgets/subagents/status.js +198 -0
- package/dist/widgets/todo/index.d.ts +13 -0
- package/dist/widgets/todo/index.js +65 -0
- package/dist/widgets/todo/parse.d.ts +29 -0
- package/dist/widgets/todo/parse.js +122 -0
- package/docs/branding.md +25 -0
- package/docs/development.md +22 -0
- package/docs/diff-api.md +18 -0
- package/docs/git.md +23 -0
- package/docs/internals.md +13 -0
- package/docs/provider-status.md +23 -0
- package/docs/status-widgets.md +157 -0
- package/docs/tool-rows.md +50 -0
- package/docs/usage.md +62 -0
- package/docs/widgets.md +113 -0
- package/extensions/chassis/alert.ts +127 -0
- package/extensions/chassis/api.ts +967 -0
- package/extensions/chassis/bash-output.ts +186 -0
- package/extensions/chassis/brand.ts +151 -0
- package/extensions/chassis/client.ts +284 -0
- package/extensions/chassis/commands.ts +244 -0
- package/extensions/chassis/editor.ts +253 -0
- package/extensions/chassis/footer.ts +131 -0
- package/extensions/chassis/header.ts +43 -0
- package/extensions/chassis/home.ts +186 -0
- package/extensions/chassis/icons.ts +220 -0
- package/extensions/chassis/index.ts +605 -0
- package/extensions/chassis/layout.ts +298 -0
- package/extensions/chassis/message-modes.ts +48 -0
- package/extensions/chassis/messages.ts +403 -0
- package/extensions/chassis/modes.ts +108 -0
- package/extensions/chassis/palette.ts +258 -0
- package/extensions/chassis/peek.ts +167 -0
- package/extensions/chassis/prompt-row.ts +86 -0
- package/extensions/chassis/provider-view.ts +118 -0
- package/extensions/chassis/provider.ts +115 -0
- package/extensions/chassis/readonly-command.ts +440 -0
- package/extensions/chassis/sanitize.ts +108 -0
- package/extensions/chassis/session-info.ts +127 -0
- package/extensions/chassis/sidebar.ts +285 -0
- package/extensions/chassis/state.ts +50 -0
- package/extensions/chassis/telemetry.ts +222 -0
- package/extensions/chassis/tools.ts +618 -0
- package/extensions/chassis/version.ts +20 -0
- package/extensions/diff/git-diff.ts +365 -0
- package/extensions/diff/host.ts +58 -0
- package/extensions/diff/index.ts +101 -0
- package/extensions/diff/render.ts +217 -0
- package/extensions/diff/types.ts +67 -0
- package/extensions/diff/view.ts +523 -0
- package/extensions/git/flows.ts +485 -0
- package/extensions/git/forge.ts +54 -0
- package/extensions/git/index.ts +117 -0
- package/extensions/git/repo.ts +200 -0
- package/extensions/git/run.ts +143 -0
- package/extensions/git/settings.ts +38 -0
- package/extensions/git/text.ts +133 -0
- package/extensions/widgets/container/detect.ts +81 -0
- package/extensions/widgets/container/index.ts +29 -0
- package/extensions/widgets/mcp/index.ts +53 -0
- package/extensions/widgets/mcp/status.ts +94 -0
- package/extensions/widgets/session-todos/index.ts +55 -0
- package/extensions/widgets/subagents/index.ts +170 -0
- package/extensions/widgets/subagents/status.ts +237 -0
- package/extensions/widgets/todo/index.ts +66 -0
- package/extensions/widgets/todo/parse.ts +119 -0
- package/package.json +91 -4
- package/scripts/link-pi.mjs +76 -0
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sidebar layout.
|
|
3
|
+
*
|
|
4
|
+
* Fullscreen mode: pi renders one layout root (a VStack of transcript + input dock). We wrap it in
|
|
5
|
+
* an HStack with the sidebar as a fixed-width right column, which is what OpenCode looks like.
|
|
6
|
+
* There is no extension API for this: it relies on the TUI's `layoutRoot` field and
|
|
7
|
+
* `setLayoutRoot()` (pi-tui ViewportTUI), verified on pi 1.1.0. If either is missing, or pi
|
|
8
|
+
* swaps the root back (renderer switch), the guard widget re-wraps it or we fall back.
|
|
9
|
+
*
|
|
10
|
+
* Regular mode has no layout root, so the sidebar is a toggleable overlay instead.
|
|
11
|
+
*
|
|
12
|
+
* Home screen: until the session has a message, the same root swaps to OpenCode's home layout
|
|
13
|
+
* (home.ts): logo, centred prompt, tip and a bottom bar, with no transcript or sidebar. The root
|
|
14
|
+
* picks its layout node each frame, so the switch happens on the first message with no remount.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import type { ExtensionContext, Theme } from "@earendil-works/pi-coding-agent";
|
|
18
|
+
import { type Component, HStack, type OverlayHandle, type TUI, VStack } from "@earendil-works/pi-tui";
|
|
19
|
+
import { HOME_PROMPT_WIDTH, renderHomeFooter, renderLogo, renderTip } from "./home.ts";
|
|
20
|
+
import { runWidgetClick } from "./api.ts";
|
|
21
|
+
import { renderSidebar, type SidebarHit } from "./sidebar.ts";
|
|
22
|
+
import type { UiState } from "./state.ts";
|
|
23
|
+
|
|
24
|
+
const ORIGINAL = Symbol.for("pi-chassis/original-root");
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The tree to search for pi's own components. Once chassis has wrapped the layout root in its
|
|
28
|
+
* sidebar split, `tui.layoutRoot` is a plain object with no children; pi's real root hangs off it
|
|
29
|
+
* under a symbol. Without it, a search for message and tool components finds nothing.
|
|
30
|
+
*/
|
|
31
|
+
export function piRootOf(tui: unknown): unknown {
|
|
32
|
+
const root = (tui as any)?.layoutRoot ?? tui;
|
|
33
|
+
return root?.[ORIGINAL] ?? root;
|
|
34
|
+
}
|
|
35
|
+
/** pi-tui's layout-node hook (layout-node.ts); not exported, but a registered symbol. */
|
|
36
|
+
const LAYOUT_NODE = Symbol.for("@earendil-works/pi-tui/layout-node");
|
|
37
|
+
const GUARD_WIDGET = "chassis-layout";
|
|
38
|
+
const GUTTER = 2;
|
|
39
|
+
/** Blank column used as horizontal padding around the transcript and prompt. */
|
|
40
|
+
const gutter: Component = { invalidate() {}, render: () => [] };
|
|
41
|
+
/** Blank rows (or columns); the size comes from the stack entry. */
|
|
42
|
+
const blank = (): Component => ({ invalidate() {}, render: () => [] });
|
|
43
|
+
|
|
44
|
+
type LayoutCapable = Component & { [LAYOUT_NODE]?: () => { entries?: readonly { component: Component }[] } };
|
|
45
|
+
|
|
46
|
+
/** pi's input dock: the second entry of its root VStack (transcript, dock). */
|
|
47
|
+
function findDock(root: Component): Component | undefined {
|
|
48
|
+
try {
|
|
49
|
+
const node = (root as LayoutCapable)[LAYOUT_NODE]?.();
|
|
50
|
+
const entries = node?.entries;
|
|
51
|
+
return entries && entries.length === 2 ? entries[1]!.component : undefined;
|
|
52
|
+
} catch {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
interface ViewportLike {
|
|
58
|
+
layoutRoot?: Component & { [ORIGINAL]?: Component };
|
|
59
|
+
setLayoutRoot?: (c: Component | undefined) => void;
|
|
60
|
+
terminal: { rows: number; columns: number };
|
|
61
|
+
requestRender(): void;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface LayoutOptions {
|
|
65
|
+
width: number;
|
|
66
|
+
/** Hide the split sidebar below this terminal width. */
|
|
67
|
+
minTerminalWidth: number;
|
|
68
|
+
piVersion: string;
|
|
69
|
+
/** True while the home screen should show (no message in the session yet). */
|
|
70
|
+
home: () => boolean;
|
|
71
|
+
/** Tip index for this launch. */
|
|
72
|
+
tipIndex: number;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface LayoutHandle {
|
|
76
|
+
/** True when the sidebar is currently on screen. */
|
|
77
|
+
isShown(): boolean;
|
|
78
|
+
toggle(): void;
|
|
79
|
+
dispose(): void;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function installLayout(ctx: ExtensionContext, state: UiState, opts: LayoutOptions): LayoutHandle {
|
|
83
|
+
let tui: (TUI & ViewportLike) | undefined;
|
|
84
|
+
let theme: Theme | undefined;
|
|
85
|
+
let ourRoot: (Component & { [ORIGINAL]?: Component }) | undefined;
|
|
86
|
+
let overlay: { handle?: OverlayHandle; close: () => void } | undefined;
|
|
87
|
+
let disposed = false;
|
|
88
|
+
|
|
89
|
+
const termWidth = () => tui?.terminal.columns ?? process.stdout.columns ?? 120;
|
|
90
|
+
const splitVisible = () => state.sidebarVisible && termWidth() >= opts.minTerminalWidth;
|
|
91
|
+
|
|
92
|
+
// The clickable rows of the last render. Only this leaf handles the mouse: the layout root has no
|
|
93
|
+
// handleMouse on purpose (see below), and a move or a click on any other row is left unhandled.
|
|
94
|
+
const hits: SidebarHit[] = [];
|
|
95
|
+
const hitAt = (y: number) => hits.find((h) => h.y === y);
|
|
96
|
+
const sidebar: Component = {
|
|
97
|
+
invalidate() {},
|
|
98
|
+
render(width: number) {
|
|
99
|
+
if (!theme) return [];
|
|
100
|
+
return renderSidebar(state, theme, width, tui?.terminal.rows ?? 40, opts.piVersion, hits);
|
|
101
|
+
},
|
|
102
|
+
handleMouse(e) {
|
|
103
|
+
if (e.button !== "left" || (e.type !== "press" && e.type !== "click")) return undefined;
|
|
104
|
+
const hit = hitAt(e.y);
|
|
105
|
+
if (!hit) return undefined;
|
|
106
|
+
// A press claims the gesture so the click arrives; only the click acts, and neither repaints.
|
|
107
|
+
if (e.type === "click" && state.ctx) {
|
|
108
|
+
const ctx = state.ctx;
|
|
109
|
+
void runWidgetClick(hit.widgetId, hit.index, ctx).then((msg) => {
|
|
110
|
+
if (msg) ctx.ui.notify(msg, "warning");
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
return { handled: true, render: false };
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const wrap = () => {
|
|
118
|
+
if (!tui || disposed || typeof tui.setLayoutRoot !== "function") return false;
|
|
119
|
+
const current = tui.layoutRoot;
|
|
120
|
+
if (!current) return false;
|
|
121
|
+
if (current === ourRoot) return true;
|
|
122
|
+
// A root we built earlier (previous session or /reload): unwrap to pi's own root.
|
|
123
|
+
const original = current[ORIGINAL] ?? current;
|
|
124
|
+
// OpenCode pads the session column 2 columns on each side (routes/session/index.tsx).
|
|
125
|
+
const split = new HStack(
|
|
126
|
+
[
|
|
127
|
+
{ component: gutter, basis: GUTTER, grow: 0, shrink: 0 },
|
|
128
|
+
{ component: original, basis: 0, grow: 1, shrink: 1, minSize: 20 },
|
|
129
|
+
{ component: gutter, basis: GUTTER, grow: 0, shrink: 0 },
|
|
130
|
+
{
|
|
131
|
+
component: sidebar,
|
|
132
|
+
basis: opts.width,
|
|
133
|
+
grow: 0,
|
|
134
|
+
shrink: 0,
|
|
135
|
+
visible: (vp) => state.sidebarVisible && vp.width >= opts.minTerminalWidth,
|
|
136
|
+
},
|
|
137
|
+
],
|
|
138
|
+
{ align: "stretch" },
|
|
139
|
+
);
|
|
140
|
+
const home = buildHome(original);
|
|
141
|
+
const pick = (): Component => (home && opts.home() ? home : split);
|
|
142
|
+
// One root for both layouts: pi-tui asks for the layout node every frame.
|
|
143
|
+
// No handleMouse here on purpose: pi-tui routes mouse events to the leaf components through
|
|
144
|
+
// the cached layout frame. A handleMouse on the root makes it dispatch to the stack's
|
|
145
|
+
// Container.handleMouse, which re-renders the whole transcript on every event (fullscreen
|
|
146
|
+
// reports every pointer move), pinning a CPU core and freezing long sessions.
|
|
147
|
+
const root = {
|
|
148
|
+
invalidate() {
|
|
149
|
+
split.invalidate();
|
|
150
|
+
home?.invalidate();
|
|
151
|
+
},
|
|
152
|
+
render: (w: number) => pick().render(w),
|
|
153
|
+
[LAYOUT_NODE]: () => (pick() as LayoutCapable)[LAYOUT_NODE]!(),
|
|
154
|
+
} as unknown as Component & { [ORIGINAL]?: Component };
|
|
155
|
+
root[ORIGINAL] = original;
|
|
156
|
+
ourRoot = root;
|
|
157
|
+
tui.setLayoutRoot(root);
|
|
158
|
+
return true;
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* OpenCode's home route, centred vertically: logo, the prompt (pi's whole input dock) centred at
|
|
163
|
+
* most 75 columns wide (with the brand line under it), a tip 3 rows below it, flexible space, and the bottom bar.
|
|
164
|
+
*/
|
|
165
|
+
const buildHome = (original: Component): Component | undefined => {
|
|
166
|
+
const dock = findDock(original);
|
|
167
|
+
if (!dock) return undefined;
|
|
168
|
+
const logo: Component = { invalidate() {}, render: (w) => (theme ? renderLogo(theme, w) : []) };
|
|
169
|
+
const tip: Component = { invalidate() {}, render: (w) => (theme ? ["", "", "", ...renderTip(theme, w, opts.tipIndex)] : []) };
|
|
170
|
+
const footer: Component = { invalidate() {}, render: (w) => (theme ? renderHomeFooter(state, theme, w, opts.piVersion) : []) };
|
|
171
|
+
// Left padding is sized each frame: pi-tui hands spare width to grow entries unevenly (the
|
|
172
|
+
// first gets the larger share), so two grow spacers wouldn't centre the prompt.
|
|
173
|
+
const leftPad = { component: blank(), basis: 0, grow: 0, shrink: 1 };
|
|
174
|
+
const promptRow = new HStack(
|
|
175
|
+
[
|
|
176
|
+
{ component: blank(), basis: GUTTER, grow: 0, shrink: 0 },
|
|
177
|
+
leftPad,
|
|
178
|
+
{ component: dock, basis: HOME_PROMPT_WIDTH, grow: 0, shrink: 1, minSize: 20 },
|
|
179
|
+
{ component: blank(), basis: 0, grow: 1, shrink: 1 },
|
|
180
|
+
{ component: blank(), basis: GUTTER, grow: 0, shrink: 0 },
|
|
181
|
+
],
|
|
182
|
+
{ align: "start" },
|
|
183
|
+
);
|
|
184
|
+
const entries = (promptRow as unknown as { entries: { basis?: number }[] }).entries;
|
|
185
|
+
const centre = () => {
|
|
186
|
+
const free = termWidth() - GUTTER * 2 - HOME_PROMPT_WIDTH;
|
|
187
|
+
if (entries[1]) entries[1].basis = Math.max(0, Math.floor(free / 2));
|
|
188
|
+
};
|
|
189
|
+
// The top space is sized each frame from the content height so the logo, prompt and tip sit
|
|
190
|
+
// in the middle of the screen (grow spacers split spare rows unevenly, as above).
|
|
191
|
+
const contentHeight = (w: number) => logo.render(w).length + 1 + promptRow.render(w).length + tip.render(w).length;
|
|
192
|
+
const stack = new VStack([
|
|
193
|
+
{ component: blank(), basis: 0, grow: 0, shrink: 1, minSize: 0 },
|
|
194
|
+
{ component: logo, basis: "auto", grow: 0, shrink: 0 },
|
|
195
|
+
{ component: blank(), basis: 1, grow: 0, shrink: 1, minSize: 0 },
|
|
196
|
+
{ component: promptRow, basis: "auto", grow: 0, shrink: 1, minSize: 3 },
|
|
197
|
+
{ component: tip, basis: "auto", grow: 0, shrink: 1, minSize: 0 },
|
|
198
|
+
{ component: blank(), basis: 0, grow: 1, shrink: 1, minSize: 0 },
|
|
199
|
+
{ component: footer, basis: "auto", grow: 0, shrink: 0 },
|
|
200
|
+
]);
|
|
201
|
+
// VStack keeps its own copy of the entries; update that copy.
|
|
202
|
+
const rows = (stack as unknown as { entries: { basis?: number }[] }).entries;
|
|
203
|
+
const middle = () => {
|
|
204
|
+
const w = termWidth();
|
|
205
|
+
const free = (tui?.terminal.rows ?? 40) - contentHeight(w);
|
|
206
|
+
if (rows[0]) rows[0].basis = Math.max(0, Math.floor(free / 2));
|
|
207
|
+
};
|
|
208
|
+
const node = (stack as unknown as LayoutCapable)[LAYOUT_NODE]!.bind(stack);
|
|
209
|
+
return Object.assign(stack, {
|
|
210
|
+
[LAYOUT_NODE]: () => {
|
|
211
|
+
centre();
|
|
212
|
+
middle();
|
|
213
|
+
return node();
|
|
214
|
+
},
|
|
215
|
+
});
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
const unwrap = () => {
|
|
219
|
+
if (!tui || typeof tui.setLayoutRoot !== "function") return;
|
|
220
|
+
const current = tui.layoutRoot;
|
|
221
|
+
if (current && current === ourRoot && current[ORIGINAL]) tui.setLayoutRoot(current[ORIGINAL]);
|
|
222
|
+
ourRoot = undefined;
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
// The guard is an empty widget inside pi's own layout. It renders every frame, so it notices
|
|
226
|
+
// when pi has replaced the root (e.g. after a renderer switch) and re-wraps on the next tick.
|
|
227
|
+
ctx.ui.setWidget(
|
|
228
|
+
GUARD_WIDGET,
|
|
229
|
+
(t, th) => {
|
|
230
|
+
tui = t as TUI & ViewportLike;
|
|
231
|
+
theme = th;
|
|
232
|
+
if (wrap()) state.layout = "split";
|
|
233
|
+
else state.layout = "overlay";
|
|
234
|
+
return {
|
|
235
|
+
invalidate() {},
|
|
236
|
+
render() {
|
|
237
|
+
theme = th;
|
|
238
|
+
if (!disposed && state.layout === "split" && tui?.layoutRoot !== ourRoot) {
|
|
239
|
+
queueMicrotask(() => {
|
|
240
|
+
if (!wrap()) state.layout = "overlay";
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
return [];
|
|
244
|
+
},
|
|
245
|
+
};
|
|
246
|
+
},
|
|
247
|
+
{ placement: "belowEditor" },
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
const openOverlay = () => {
|
|
251
|
+
if (overlay || !theme) return;
|
|
252
|
+
const entry: { handle?: OverlayHandle; close: () => void } = { close: () => {} };
|
|
253
|
+
overlay = entry;
|
|
254
|
+
void ctx.ui
|
|
255
|
+
.custom<void>(
|
|
256
|
+
(t, th, _kb, done) => {
|
|
257
|
+
entry.close = () => done();
|
|
258
|
+
return {
|
|
259
|
+
invalidate() {},
|
|
260
|
+
render: (w: number) => renderSidebar(state, th, w, Math.min(t.terminal.rows - 2, 40), opts.piVersion),
|
|
261
|
+
};
|
|
262
|
+
},
|
|
263
|
+
{
|
|
264
|
+
overlay: true,
|
|
265
|
+
overlayOptions: { anchor: "top-right", width: opts.width, maxHeight: "90%", nonCapturing: true, margin: { top: 1, right: 1 } },
|
|
266
|
+
onHandle: (h) => {
|
|
267
|
+
entry.handle = h;
|
|
268
|
+
},
|
|
269
|
+
},
|
|
270
|
+
)
|
|
271
|
+
.finally(() => {
|
|
272
|
+
if (overlay === entry) overlay = undefined;
|
|
273
|
+
});
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
return {
|
|
277
|
+
isShown: () => (state.layout === "split" ? splitVisible() && !opts.home() : overlay !== undefined),
|
|
278
|
+
toggle() {
|
|
279
|
+
if (state.layout === "split") {
|
|
280
|
+
state.sidebarVisible = !state.sidebarVisible;
|
|
281
|
+
tui?.requestRender();
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
if (overlay) overlay.close();
|
|
285
|
+
else openOverlay();
|
|
286
|
+
},
|
|
287
|
+
dispose() {
|
|
288
|
+
disposed = true;
|
|
289
|
+
overlay?.close();
|
|
290
|
+
unwrap();
|
|
291
|
+
try {
|
|
292
|
+
ctx.ui.setWidget(GUARD_WIDGET, undefined);
|
|
293
|
+
} catch {
|
|
294
|
+
// stale ctx
|
|
295
|
+
}
|
|
296
|
+
},
|
|
297
|
+
};
|
|
298
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which mode each user message was sent in, read from the session.
|
|
3
|
+
*
|
|
4
|
+
* chassis writes a `chassis-mode` custom entry at every Build/Plan switch, so the mode of a
|
|
5
|
+
* message is the last such entry before it on the branch (build when there is none). The result is
|
|
6
|
+
* one mode per user message box that pi draws, in order, so the Nth box in the chat is the Nth
|
|
7
|
+
* mode. pi draws a box for a user message only when it has text, and for a skill block only when
|
|
8
|
+
* the block carries a message of its own (the skill itself is a different component).
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { parseSkillBlock } from "@earendil-works/pi-coding-agent";
|
|
12
|
+
import { MODE_ENTRY } from "./modes.ts";
|
|
13
|
+
import type { AgentMode } from "./state.ts";
|
|
14
|
+
|
|
15
|
+
/** The text pi shows for a user message: its text blocks joined, as pi's getUserMessageText does. */
|
|
16
|
+
function userText(message: { content?: unknown }): string {
|
|
17
|
+
const c = message.content;
|
|
18
|
+
if (typeof c === "string") return c;
|
|
19
|
+
if (!Array.isArray(c)) return "";
|
|
20
|
+
return c.filter((b: any) => b?.type === "text").map((b: any) => String(b.text ?? "")).join("");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Whether pi draws a user message box for this message. */
|
|
24
|
+
export function drawsUserBox(message: { role?: string; content?: unknown }): boolean {
|
|
25
|
+
if (message.role !== "user") return false;
|
|
26
|
+
const text = userText(message);
|
|
27
|
+
if (!text) return false;
|
|
28
|
+
const skill = parseSkillBlock(text);
|
|
29
|
+
return skill ? Boolean(skill.userMessage) : true;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The mode of every user message box, in order. `branch` is the whole branch from the root;
|
|
34
|
+
* `contextIds` are the ids of the entries pi draws (after a compaction, older ones are left out,
|
|
35
|
+
* but their mode entries still count).
|
|
36
|
+
*/
|
|
37
|
+
export function userMessageModes(branch: readonly any[], contextIds: ReadonlySet<string>): AgentMode[] {
|
|
38
|
+
const modes: AgentMode[] = [];
|
|
39
|
+
let mode: AgentMode = "build";
|
|
40
|
+
for (const e of branch) {
|
|
41
|
+
if (e?.type === "custom" && e.customType === MODE_ENTRY) {
|
|
42
|
+
if (e.data?.mode === "plan" || e.data?.mode === "build") mode = e.data.mode;
|
|
43
|
+
} else if (e?.type === "message" && contextIds.has(e.id) && drawsUserBox(e.message ?? {})) {
|
|
44
|
+
modes.push(mode);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return modes;
|
|
48
|
+
}
|