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,605 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* chassis: an OpenCode-style TUI for pi, with a brand that another package can replace (brand.ts).
|
|
3
|
+
*
|
|
4
|
+
* Pieces (each in its own file):
|
|
5
|
+
* layout.ts right sidebar (fullscreen split, overlay fallback) ctrl+x b
|
|
6
|
+
* home.ts OpenCode-style home screen (logo, centred prompt, tip, bottom bar)
|
|
7
|
+
* sidebar.ts session title, context/cost, status widgets, files, todo
|
|
8
|
+
* footer.ts one-line status bar under the prompt
|
|
9
|
+
* editor.ts left-rail prompt with mode badge + model, leader key, Tab mode switch
|
|
10
|
+
* commands.ts ctrl+x leader actions and the ctrl+p command palette
|
|
11
|
+
* modes.ts Build / Plan (read-only) agent modes
|
|
12
|
+
* tools.ts one-line tool rows ("→ Read x.ts 120 lines")
|
|
13
|
+
* messages.ts user prompts with a coloured left bar
|
|
14
|
+
* peek.ts thinking peek in the hidden-thinking label
|
|
15
|
+
* telemetry.ts per-run TPS/TTFT/tokens/cost summary
|
|
16
|
+
* api.ts the widget registry other extensions use (registerWidget)
|
|
17
|
+
*
|
|
18
|
+
* Commands: /chassis [on|off|sidebar|status]
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import {
|
|
22
|
+
type ExtensionAPI,
|
|
23
|
+
type ExtensionContext,
|
|
24
|
+
SessionManager,
|
|
25
|
+
type Theme,
|
|
26
|
+
VERSION,
|
|
27
|
+
} from "@earendil-works/pi-coding-agent";
|
|
28
|
+
import type { TUI } from "@earendil-works/pi-tui";
|
|
29
|
+
import { iconSet, iconSource, saveIconSetting, setIconSetting } from "./icons.ts";
|
|
30
|
+
import { alert as ringAlert, type AlertReason, describeAlert, loadAlert, parseAlertArg, plainTitle, runOutcome, saveAlert, shouldAlert } from "./alert.ts";
|
|
31
|
+
import { formatWidgetList, getRegistry, installHost, invalidateWidgets, refreshWidget, setWidgetDisabled, widgetActions, widgetActionValue, widgetInfo } from "./api.ts";
|
|
32
|
+
import {
|
|
33
|
+
CommandPalette,
|
|
34
|
+
leaderActions,
|
|
35
|
+
leaderHelpLines,
|
|
36
|
+
paletteItems,
|
|
37
|
+
runPaletteValue,
|
|
38
|
+
type LeaderAction,
|
|
39
|
+
} from "./commands.ts";
|
|
40
|
+
import { PromptEditor } from "./editor.ts";
|
|
41
|
+
import { installFooter } from "./footer.ts";
|
|
42
|
+
import { renderHeader } from "./header.ts";
|
|
43
|
+
import { PLACEHOLDERS, TIPS } from "./home.ts";
|
|
44
|
+
import { installLayout, type LayoutHandle, piRootOf } from "./layout.ts";
|
|
45
|
+
import { installMessageStyle, setChatContainer } from "./messages.ts";
|
|
46
|
+
import { installModes } from "./modes.ts";
|
|
47
|
+
import { fit, sanitize, spinnerFrame } from "./palette.ts";
|
|
48
|
+
import { ThinkingPeek } from "./peek.ts";
|
|
49
|
+
import { readGit } from "./session-info.ts";
|
|
50
|
+
import type { ModelParts } from "./prompt-row.ts";
|
|
51
|
+
import { SIDEBAR_WIDTH } from "./sidebar.ts";
|
|
52
|
+
import { createState } from "./state.ts";
|
|
53
|
+
import { formatTelemetry, TelemetryTracker } from "./telemetry.ts";
|
|
54
|
+
import { installToolRenderers } from "./tools.ts";
|
|
55
|
+
|
|
56
|
+
// pi's built-in slash commands: pi.getCommands() only lists extension, prompt and skill commands.
|
|
57
|
+
const BUILTIN_COMMANDS: { name: string; description: string }[] = [
|
|
58
|
+
{ name: "settings", description: "Open settings menu" },
|
|
59
|
+
{ name: "model", description: "Select model" },
|
|
60
|
+
{ name: "tree", description: "Navigate session tree" },
|
|
61
|
+
{ name: "thinking", description: "Set thinking level" },
|
|
62
|
+
{ name: "scoped-models", description: "Enable/disable models for cycling" },
|
|
63
|
+
{ name: "export", description: "Export session" },
|
|
64
|
+
{ name: "import", description: "Import a session from JSONL" },
|
|
65
|
+
{ name: "share", description: "Share session as a secret gist" },
|
|
66
|
+
{ name: "copy", description: "Copy last agent message" },
|
|
67
|
+
{ name: "name", description: "Set session display name" },
|
|
68
|
+
{ name: "session", description: "Show session info and stats" },
|
|
69
|
+
{ name: "changelog", description: "Show changelog entries" },
|
|
70
|
+
{ name: "hotkeys", description: "Show all keyboard shortcuts" },
|
|
71
|
+
{ name: "fork", description: "Fork from a previous user message" },
|
|
72
|
+
{ name: "clone", description: "Duplicate the current session" },
|
|
73
|
+
{ name: "trust", description: "Save project trust decision" },
|
|
74
|
+
{ name: "login", description: "Configure provider authentication" },
|
|
75
|
+
{ name: "logout", description: "Remove provider authentication" },
|
|
76
|
+
{ name: "new", description: "Start a new session" },
|
|
77
|
+
{ name: "compact", description: "Compact the session context" },
|
|
78
|
+
{ name: "resume", description: "Resume a different session" },
|
|
79
|
+
{ name: "reload", description: "Reload extensions, themes, keybindings" },
|
|
80
|
+
{ name: "quit", description: "Quit pi" },
|
|
81
|
+
];
|
|
82
|
+
|
|
83
|
+
const LEADER_TIMEOUT_MS = 2000;
|
|
84
|
+
const SIDEBAR_MIN_TERMINAL_WIDTH = 116; // keeps the transcript as wide as it was at the old 42-column sidebar and 110 columns
|
|
85
|
+
const GIT_POLL_MS = 4000;
|
|
86
|
+
|
|
87
|
+
type CommandSpec = Parameters<ExtensionAPI["registerCommand"]>[1];
|
|
88
|
+
|
|
89
|
+
export default function (pi: ExtensionAPI) {
|
|
90
|
+
const state = createState();
|
|
91
|
+
const registry = getRegistry();
|
|
92
|
+
installHost();
|
|
93
|
+
const telemetry = new TelemetryTracker();
|
|
94
|
+
|
|
95
|
+
let enabled = true;
|
|
96
|
+
let alertSettings = loadAlert();
|
|
97
|
+
/** Set while the window title carries an alert mark, so the next prompt clears it. */
|
|
98
|
+
let titleMarked = false;
|
|
99
|
+
/** How the last run ended, from agent_end, for the alert at agent_settled. */
|
|
100
|
+
let lastOutcome: "finished" | "failed" | "aborted" = "finished";
|
|
101
|
+
let active = false;
|
|
102
|
+
let theme: Theme | undefined;
|
|
103
|
+
let tui: TUI | undefined;
|
|
104
|
+
let editor: PromptEditor | undefined;
|
|
105
|
+
let layout: LayoutHandle | undefined;
|
|
106
|
+
let cleanupFooter: (() => void) | undefined;
|
|
107
|
+
let cleanupMessages: (() => void) | undefined;
|
|
108
|
+
let requestFooter: (() => void) | undefined;
|
|
109
|
+
let workingTimer: ReturnType<typeof setInterval> | undefined;
|
|
110
|
+
|
|
111
|
+
const peek = new ThinkingPeek(
|
|
112
|
+
() => tui,
|
|
113
|
+
() => Math.max(10, (tui?.terminal.columns ?? 100) - (layout?.isShown() ? SIDEBAR_WIDTH : 0) - 4),
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
const render = () => {
|
|
117
|
+
requestFooter?.();
|
|
118
|
+
tui?.requestRender();
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
/** pi's chat container: the Container whose children include message/tool components. */
|
|
122
|
+
const findChatContainer = (): { children: unknown[] } | undefined => {
|
|
123
|
+
const stack: any[] = [piRootOf(tui)];
|
|
124
|
+
const seen = new Set<object>();
|
|
125
|
+
while (stack.length) {
|
|
126
|
+
const v = stack.pop();
|
|
127
|
+
if (!v || typeof v !== "object" || seen.has(v)) continue;
|
|
128
|
+
seen.add(v);
|
|
129
|
+
const kids = v.children;
|
|
130
|
+
if (Array.isArray(kids)) {
|
|
131
|
+
if (kids.some((k: any) => /^(UserMessage|AssistantMessage|ToolExecution)Component$/.test(k?.constructor?.name ?? ""))) return v;
|
|
132
|
+
for (const k of kids) stack.push(k);
|
|
133
|
+
}
|
|
134
|
+
if (v.child) stack.push(v.child);
|
|
135
|
+
}
|
|
136
|
+
return undefined;
|
|
137
|
+
};
|
|
138
|
+
let foundChat: unknown;
|
|
139
|
+
const refreshChatContainer = () => {
|
|
140
|
+
const c = findChatContainer();
|
|
141
|
+
if (!c) return;
|
|
142
|
+
setChatContainer(c);
|
|
143
|
+
// Messages drawn before the chat was found (a resumed session) guessed their mode colour;
|
|
144
|
+
// draw them again now that they can be matched to the session.
|
|
145
|
+
if (c !== foundChat) {
|
|
146
|
+
foundChat = c;
|
|
147
|
+
tui?.requestRender();
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
registry.requestRender = render;
|
|
151
|
+
|
|
152
|
+
installToolRenderers(pi);
|
|
153
|
+
|
|
154
|
+
const modes = installModes(pi, state, render);
|
|
155
|
+
|
|
156
|
+
const submit = (text: string) => {
|
|
157
|
+
// pi's command dispatcher is the editor's onSubmit (wired by pi when it installs our editor).
|
|
158
|
+
const fn = (editor as unknown as { onSubmit?: (t: string) => void })?.onSubmit;
|
|
159
|
+
if (fn) fn(text);
|
|
160
|
+
else state.ctx?.ui.setEditorText(text);
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
const showLeaderHelp = () => {
|
|
164
|
+
const ctx = state.ctx;
|
|
165
|
+
if (!ctx || !theme) return;
|
|
166
|
+
void ctx.ui.custom<void>(
|
|
167
|
+
(_t, th, _kb, done) => {
|
|
168
|
+
const lines = leaderHelpLines(actions, th);
|
|
169
|
+
return {
|
|
170
|
+
invalidate() {},
|
|
171
|
+
handleInput: () => done(),
|
|
172
|
+
render: (w: number) => {
|
|
173
|
+
const inner = w - 4;
|
|
174
|
+
const b = (s: string) => th.fg("borderMuted", s);
|
|
175
|
+
return [b(`╭${"─".repeat(w - 2)}╮`), ...lines.map((l) => `${b("│")} ${fit(l, inner)} ${b("│")}`), b(`╰${"─".repeat(w - 2)}╯`)];
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
},
|
|
179
|
+
{ overlay: true, overlayOptions: { anchor: "center", width: 56 } },
|
|
180
|
+
);
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
const actions: LeaderAction[] = leaderActions({
|
|
184
|
+
toggleSidebar: () => {
|
|
185
|
+
layout?.toggle();
|
|
186
|
+
render();
|
|
187
|
+
},
|
|
188
|
+
cycleMode: () => {
|
|
189
|
+
if (state.ctx) modes.cycle(state.ctx);
|
|
190
|
+
},
|
|
191
|
+
showHelp: showLeaderHelp,
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
const runLeader = (data: string): boolean => {
|
|
195
|
+
const key = data.length === 1 ? data.toLowerCase() : undefined;
|
|
196
|
+
const action = actions.find((a) => a.key === key);
|
|
197
|
+
if (!action) return false;
|
|
198
|
+
if (typeof action.run === "function") action.run();
|
|
199
|
+
else submit(action.run);
|
|
200
|
+
return true;
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
const openPalette = async (ctx: ExtensionContext) => {
|
|
204
|
+
const widgetItems = widgetActions().map((a) => ({
|
|
205
|
+
value: widgetActionValue(a.widgetId, a.id),
|
|
206
|
+
label: `${a.widgetTitle}: ${a.label}`,
|
|
207
|
+
description: a.description,
|
|
208
|
+
hint: "widget",
|
|
209
|
+
}));
|
|
210
|
+
const items = paletteItems(pi, BUILTIN_COMMANDS, actions, widgetItems);
|
|
211
|
+
const value = await ctx.ui.custom<string | undefined>(
|
|
212
|
+
(_t, th, _kb, done) => new CommandPalette(items, th, done),
|
|
213
|
+
{ overlay: true, overlayOptions: { anchor: "top-center", width: "60%", minWidth: 50, offsetY: 3, maxHeight: "70%" } },
|
|
214
|
+
);
|
|
215
|
+
if (value) runPaletteValue(value, ctx, actions, submit);
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
const modelParts = (): ModelParts => {
|
|
219
|
+
const m = state.ctx?.model;
|
|
220
|
+
if (!m) return { model: "no model" };
|
|
221
|
+
const provider = m.provider ? m.provider[0]!.toUpperCase() + m.provider.slice(1) : undefined;
|
|
222
|
+
return { model: m.name ?? m.id, provider, effort: m.reasoning ? pi.getThinkingLevel() : undefined };
|
|
223
|
+
};
|
|
224
|
+
/** One line for the startup header: "Claude Opus 5.5 · Anthropic · medium". */
|
|
225
|
+
const modelLabel = (): string => {
|
|
226
|
+
const p = modelParts();
|
|
227
|
+
return [p.model, p.provider, p.effort].filter(Boolean).join(" · ");
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
/** True once the current branch has a user or assistant message. */
|
|
231
|
+
const sessionStarted = (): boolean => {
|
|
232
|
+
const c = state.ctx;
|
|
233
|
+
if (!c) return false;
|
|
234
|
+
try {
|
|
235
|
+
return (c.sessionManager.getBranch() as any[]).some(
|
|
236
|
+
(e) => e.type === "message" && (e.message?.role === "user" || e.message?.role === "assistant"),
|
|
237
|
+
);
|
|
238
|
+
} catch {
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
/** Most recent other session with messages in this directory (home screen resume hint). */
|
|
244
|
+
const refreshLastSession = async (ctx: ExtensionContext) => {
|
|
245
|
+
try {
|
|
246
|
+
const current = ctx.sessionManager.getSessionId();
|
|
247
|
+
const list = await SessionManager.list(ctx.cwd, ctx.sessionManager.getSessionDir());
|
|
248
|
+
const last = list
|
|
249
|
+
.filter((s) => s.id !== current && s.messageCount > 0)
|
|
250
|
+
.sort((a, b) => b.modified.getTime() - a.modified.getTime())[0];
|
|
251
|
+
if (state.ctx !== ctx) return;
|
|
252
|
+
const label = sanitize(last?.name || last?.firstMessage || "");
|
|
253
|
+
state.lastSession = last ? { path: last.path, label: label || "untitled", modified: last.modified } : undefined;
|
|
254
|
+
render();
|
|
255
|
+
} catch {
|
|
256
|
+
// listing is best-effort
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
const refreshGit = async (ctx: ExtensionContext) => {
|
|
261
|
+
const info = await readGit(ctx.cwd);
|
|
262
|
+
if (state.ctx !== ctx) return;
|
|
263
|
+
// Repaint only on a change: this also runs on a timer.
|
|
264
|
+
if (JSON.stringify(info) === JSON.stringify(state.git)) return;
|
|
265
|
+
state.git = info;
|
|
266
|
+
render();
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Git changes outside pi (a commit or checkout in another terminal, edits by other tools) fire
|
|
271
|
+
* no event, so poll. readGit skips optional locks, so this never blocks the user's own git.
|
|
272
|
+
*/
|
|
273
|
+
let gitTimer: ReturnType<typeof setInterval> | undefined;
|
|
274
|
+
let gitBusy = false;
|
|
275
|
+
const startGitPoll = (ctx: ExtensionContext) => {
|
|
276
|
+
stopGitPoll();
|
|
277
|
+
gitTimer = setInterval(() => {
|
|
278
|
+
if (gitBusy || state.ctx !== ctx) return;
|
|
279
|
+
gitBusy = true;
|
|
280
|
+
void refreshGit(ctx).finally(() => (gitBusy = false));
|
|
281
|
+
}, GIT_POLL_MS);
|
|
282
|
+
gitTimer.unref?.();
|
|
283
|
+
};
|
|
284
|
+
const stopGitPoll = () => {
|
|
285
|
+
if (gitTimer) clearInterval(gitTimer);
|
|
286
|
+
gitTimer = undefined;
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
const install = (ctx: ExtensionContext) => {
|
|
290
|
+
if (active || !enabled || ctx.mode !== "tui") return;
|
|
291
|
+
active = true;
|
|
292
|
+
const placeholder = PLACEHOLDERS[Math.floor(Math.random() * PLACEHOLDERS.length)];
|
|
293
|
+
|
|
294
|
+
ctx.ui.setHeader((t, th) => {
|
|
295
|
+
tui = t;
|
|
296
|
+
theme = th;
|
|
297
|
+
// OpenCode shows its logo on the home screen only; once the session has a message the
|
|
298
|
+
// header collapses so the transcript starts at the top.
|
|
299
|
+
return {
|
|
300
|
+
invalidate() {},
|
|
301
|
+
render: (w: number) =>
|
|
302
|
+
sessionStarted() || state.home() ? [] : renderHeader(th, w, { model: modelLabel(), cwd: state.ctx?.cwd ?? ctx.cwd, version: VERSION }),
|
|
303
|
+
};
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
cleanupFooter = installFooter(ctx, state, (fn) => {
|
|
307
|
+
requestFooter = fn;
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
ctx.ui.setEditorComponent((t, editorTheme, keybindings) => {
|
|
311
|
+
tui = t;
|
|
312
|
+
editor = new PromptEditor(t, editorTheme, keybindings, {
|
|
313
|
+
state,
|
|
314
|
+
theme: () => theme ?? ctx.ui.theme,
|
|
315
|
+
modelParts,
|
|
316
|
+
placeholder: () => (state.home() ? `Ask anything… "${placeholder}"` : undefined),
|
|
317
|
+
blink: () => state.home(),
|
|
318
|
+
onModeCycle: () => {
|
|
319
|
+
if (state.ctx) modes.cycle(state.ctx);
|
|
320
|
+
},
|
|
321
|
+
onLeaderKey: runLeader,
|
|
322
|
+
onLeaderState: () => tui?.requestRender(),
|
|
323
|
+
onPalette: () => {
|
|
324
|
+
if (state.ctx) void openPalette(state.ctx);
|
|
325
|
+
},
|
|
326
|
+
onStatusChange: () => requestFooter?.(),
|
|
327
|
+
leaderTimeoutMs: LEADER_TIMEOUT_MS,
|
|
328
|
+
});
|
|
329
|
+
return editor;
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
theme = ctx.ui.theme;
|
|
333
|
+
cleanupMessages = installMessageStyle(state, () => theme ?? state.ctx?.ui.theme);
|
|
334
|
+
layout = installLayout(ctx, state, {
|
|
335
|
+
width: SIDEBAR_WIDTH,
|
|
336
|
+
minTerminalWidth: SIDEBAR_MIN_TERMINAL_WIDTH,
|
|
337
|
+
piVersion: VERSION,
|
|
338
|
+
home: () => !sessionStarted(),
|
|
339
|
+
tipIndex: Math.floor(Math.random() * TIPS.length),
|
|
340
|
+
});
|
|
341
|
+
state.sidebarShown = () => layout?.isShown() ?? false;
|
|
342
|
+
state.home = () => state.layout === "split" && !sessionStarted();
|
|
343
|
+
ctx.ui.setWorkingIndicator({ frames: ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"].map((f) => spinnerFrame(ctx.ui.theme, f)), intervalMs: 80 });
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
const uninstall = (ctx: ExtensionContext | undefined) => {
|
|
347
|
+
if (!active) return;
|
|
348
|
+
active = false;
|
|
349
|
+
layout?.dispose();
|
|
350
|
+
layout = undefined;
|
|
351
|
+
state.sidebarShown = () => false;
|
|
352
|
+
state.home = () => false;
|
|
353
|
+
cleanupFooter?.();
|
|
354
|
+
cleanupFooter = undefined;
|
|
355
|
+
cleanupMessages?.();
|
|
356
|
+
cleanupMessages = undefined;
|
|
357
|
+
setChatContainer(undefined);
|
|
358
|
+
requestFooter = undefined;
|
|
359
|
+
if (ctx) {
|
|
360
|
+
try {
|
|
361
|
+
ctx.ui.setHeader(undefined);
|
|
362
|
+
ctx.ui.setEditorComponent(undefined);
|
|
363
|
+
ctx.ui.setWorkingIndicator();
|
|
364
|
+
ctx.ui.setHiddenThinkingLabel();
|
|
365
|
+
} catch {
|
|
366
|
+
// stale ctx
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
editor?.dispose();
|
|
370
|
+
editor = undefined;
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
const stopWorkingTimer = () => {
|
|
374
|
+
if (workingTimer) clearInterval(workingTimer);
|
|
375
|
+
workingTimer = undefined;
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
pi.registerShortcut("ctrl+alt+b", {
|
|
379
|
+
description: "chassis: toggle sidebar",
|
|
380
|
+
handler: () => {
|
|
381
|
+
layout?.toggle();
|
|
382
|
+
render();
|
|
383
|
+
},
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
pi.on("session_start", async (_e, ctx) => {
|
|
387
|
+
state.ctx = ctx;
|
|
388
|
+
registry.ctx = ctx;
|
|
389
|
+
state.workingSince = undefined;
|
|
390
|
+
state.lastDurationMs = undefined;
|
|
391
|
+
if (ctx.mode !== "tui") return;
|
|
392
|
+
install(ctx);
|
|
393
|
+
modes.restore(ctx);
|
|
394
|
+
void refreshGit(ctx);
|
|
395
|
+
startGitPoll(ctx);
|
|
396
|
+
state.lastSession = undefined;
|
|
397
|
+
if (!sessionStarted()) void refreshLastSession(ctx);
|
|
398
|
+
// Resumed sessions render their history after session_start.
|
|
399
|
+
setTimeout(refreshChatContainer, 500);
|
|
400
|
+
render();
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
pi.on("session_shutdown", async (_e, ctx) => {
|
|
404
|
+
stopWorkingTimer();
|
|
405
|
+
stopGitPoll();
|
|
406
|
+
uninstall(ctx);
|
|
407
|
+
state.ctx = undefined;
|
|
408
|
+
registry.ctx = undefined;
|
|
409
|
+
registry.requestRender = () => {};
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
// The title goes out as an OSC sequence, and the folder name is chosen by whoever made the folder.
|
|
413
|
+
const sessionTitle = (ctx: ExtensionContext) => sanitize(plainTitle(ctx.cwd, safeName(ctx)));
|
|
414
|
+
const safeName = (ctx: ExtensionContext): string | undefined => {
|
|
415
|
+
try {
|
|
416
|
+
return sanitize(ctx.sessionManager.getSessionName()) || undefined;
|
|
417
|
+
} catch {
|
|
418
|
+
return undefined; // stale ctx after a session switch
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
/** Ring the bell and mark the title, for a run that has lasted long enough. */
|
|
422
|
+
const notifyUser = (ctx: ExtensionContext, reason: AlertReason, durationMs: number | undefined) => {
|
|
423
|
+
if (!active || ctx.mode !== "tui" || !shouldAlert(alertSettings, durationMs)) return;
|
|
424
|
+
try {
|
|
425
|
+
ringAlert({ write: (d) => process.stdout.write(d), setTitle: (t) => ctx.ui.setTitle(t) }, reason, sessionTitle(ctx));
|
|
426
|
+
titleMarked = true;
|
|
427
|
+
} catch {
|
|
428
|
+
// a closed terminal or stale ctx: nothing to alert
|
|
429
|
+
}
|
|
430
|
+
};
|
|
431
|
+
const clearTitleMark = (ctx: ExtensionContext) => {
|
|
432
|
+
if (!titleMarked) return;
|
|
433
|
+
titleMarked = false;
|
|
434
|
+
try {
|
|
435
|
+
ctx.ui.setTitle(sessionTitle(ctx));
|
|
436
|
+
} catch {
|
|
437
|
+
// stale ctx
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
// A prompt that opens while the agent is running (a tool asking you to confirm, say) is the agent
|
|
441
|
+
// waiting for you. One you opened yourself while idle is not, so it has no duration.
|
|
442
|
+
pi.on("ui_prompt_start", async (_e, ctx) => notifyUser(ctx, "waiting", state.workingSince ? Date.now() - state.workingSince : undefined));
|
|
443
|
+
pi.on("ui_prompt_end", async (_e, ctx) => clearTitleMark(ctx));
|
|
444
|
+
|
|
445
|
+
pi.on("agent_start", async (e, ctx) => {
|
|
446
|
+
clearTitleMark(ctx);
|
|
447
|
+
telemetry.handle(e);
|
|
448
|
+
state.workingSince = Date.now();
|
|
449
|
+
state.lastDurationMs = undefined;
|
|
450
|
+
stopWorkingTimer();
|
|
451
|
+
workingTimer = setInterval(() => requestFooter?.(), 1000);
|
|
452
|
+
workingTimer.unref?.();
|
|
453
|
+
});
|
|
454
|
+
pi.on("turn_start", async (e) => {
|
|
455
|
+
telemetry.handle(e);
|
|
456
|
+
});
|
|
457
|
+
pi.on("message_start", async (e) => {
|
|
458
|
+
telemetry.handle(e);
|
|
459
|
+
if (e.message?.role === "assistant" || e.message?.role === "user") peek.reset();
|
|
460
|
+
if (e.message?.role === "user") setTimeout(refreshChatContainer, 0);
|
|
461
|
+
});
|
|
462
|
+
pi.on("message_update", async (e, ctx) => {
|
|
463
|
+
telemetry.handle(e);
|
|
464
|
+
if (!active || ctx.isIdle() || e.message?.role !== "assistant") return;
|
|
465
|
+
peek.update(e.message);
|
|
466
|
+
});
|
|
467
|
+
pi.on("message_end", async (e) => {
|
|
468
|
+
telemetry.handle(e);
|
|
469
|
+
if (e.message?.role === "assistant") peek.end();
|
|
470
|
+
render();
|
|
471
|
+
});
|
|
472
|
+
pi.on("turn_end", async (e) => {
|
|
473
|
+
telemetry.handle(e);
|
|
474
|
+
});
|
|
475
|
+
pi.on("tool_execution_start", async () => refreshChatContainer());
|
|
476
|
+
pi.on("tool_execution_end", async () => render());
|
|
477
|
+
pi.on("agent_end", async (e, ctx) => {
|
|
478
|
+
lastOutcome = runOutcome(e.messages);
|
|
479
|
+
stopWorkingTimer();
|
|
480
|
+
if (state.workingSince) state.lastDurationMs = Date.now() - state.workingSince;
|
|
481
|
+
state.workingSince = undefined;
|
|
482
|
+
void refreshGit(ctx);
|
|
483
|
+
state.lastSession = undefined;
|
|
484
|
+
if (!sessionStarted()) void refreshLastSession(ctx);
|
|
485
|
+
render();
|
|
486
|
+
});
|
|
487
|
+
pi.on("agent_settled", async (e, ctx) => {
|
|
488
|
+
const t = telemetry.handle(e);
|
|
489
|
+
if (t && active && ctx.mode === "tui") ctx.ui.notify(formatTelemetry(t, ctx.ui.theme), "info");
|
|
490
|
+
peek.reset();
|
|
491
|
+
// After retries and compaction: the run is over for good. A user abort never rings.
|
|
492
|
+
if (lastOutcome !== "aborted") notifyUser(ctx, lastOutcome, state.lastDurationMs);
|
|
493
|
+
});
|
|
494
|
+
pi.on("model_select", async () => render());
|
|
495
|
+
pi.on("thinking_level_select", async () => render());
|
|
496
|
+
pi.on("session_tree", async (_e, ctx) => {
|
|
497
|
+
modes.restore(ctx);
|
|
498
|
+
render();
|
|
499
|
+
});
|
|
500
|
+
pi.on("session_compact", async () => render());
|
|
501
|
+
pi.on("session_info_changed", async () => render());
|
|
502
|
+
|
|
503
|
+
const resumeLastCommand: CommandSpec = {
|
|
504
|
+
description: "chassis: resume the most recent other session in this directory",
|
|
505
|
+
handler: async (_args, ctx) => {
|
|
506
|
+
if (!state.lastSession) await refreshLastSession(ctx);
|
|
507
|
+
const last = state.lastSession;
|
|
508
|
+
if (!last) {
|
|
509
|
+
ctx.ui.notify("No earlier session in this directory.", "info");
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
await ctx.switchSession(last.path);
|
|
513
|
+
},
|
|
514
|
+
};
|
|
515
|
+
pi.registerCommand("chassis-resume-last", resumeLastCommand);
|
|
516
|
+
|
|
517
|
+
pi.registerCommand("widgets", {
|
|
518
|
+
description: "chassis widgets: /widgets [list|disable ID|enable ID|refresh ID]",
|
|
519
|
+
getArgumentCompletions: (prefix) => {
|
|
520
|
+
const parts = prefix.split(/\s+/);
|
|
521
|
+
if (parts.length <= 1) {
|
|
522
|
+
const opts = ["list", "disable", "enable", "refresh"].filter((o) => o.startsWith(parts[0] ?? ""));
|
|
523
|
+
return opts.length ? opts.map((o) => ({ value: o, label: o })) : null;
|
|
524
|
+
}
|
|
525
|
+
const ids = widgetInfo().map((w) => w.id).filter((id) => id.startsWith(parts[1] ?? ""));
|
|
526
|
+
return ids.length ? ids.map((id) => ({ value: `${parts[0]} ${id}`, label: id })) : null;
|
|
527
|
+
},
|
|
528
|
+
handler: async (args, ctx) => {
|
|
529
|
+
const [sub = "list", id] = args.trim().split(/\s+/);
|
|
530
|
+
if (sub === "disable" || sub === "enable" || sub === "refresh") {
|
|
531
|
+
const ok = !id ? false : sub === "refresh" ? refreshWidget(id) : setWidgetDisabled(id, sub === "disable");
|
|
532
|
+
ctx.ui.notify(ok ? `widget ${id}: ${sub}d`.replace("refreshd", "refreshing") : `No widget ${id ? `"${id}"` : "id given"}. Try /widgets list.`, ok ? "info" : "warning");
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
ctx.ui.notify(formatWidgetList(widgetInfo()), "info");
|
|
536
|
+
},
|
|
537
|
+
});
|
|
538
|
+
|
|
539
|
+
const chassisCommand: CommandSpec = {
|
|
540
|
+
description: "chassis: /chassis [on|off|sidebar|status|alert [off|on|SECONDS]|icons [nerd|plain|auto]]",
|
|
541
|
+
getArgumentCompletions: (prefix) => {
|
|
542
|
+
const opts = ["on", "off", "sidebar", "status", "alert", "icons"].filter((o) => o.startsWith(prefix.trim()));
|
|
543
|
+
return opts.length ? opts.map((o) => ({ value: o, label: o })) : null;
|
|
544
|
+
},
|
|
545
|
+
handler: async (args, ctx) => {
|
|
546
|
+
const sub = args.trim().toLowerCase();
|
|
547
|
+
if (sub === "icons" || sub.startsWith("icons ")) {
|
|
548
|
+
const arg = sub.slice("icons".length).trim();
|
|
549
|
+
if (arg) {
|
|
550
|
+
if (arg !== "nerd" && arg !== "plain" && arg !== "auto") {
|
|
551
|
+
ctx.ui.notify("Usage: /chassis icons [nerd|plain|auto]", "warning");
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
const choice = arg === "auto" ? undefined : arg;
|
|
555
|
+
setIconSetting(choice);
|
|
556
|
+
saveIconSetting(choice);
|
|
557
|
+
invalidateWidgets();
|
|
558
|
+
refreshChatContainer();
|
|
559
|
+
render();
|
|
560
|
+
}
|
|
561
|
+
const src = iconSource();
|
|
562
|
+
const why = src === "env" ? " (set by PI_CHASSIS_ICONS, which wins over the saved setting)" : src === "setting" ? "" : src === "brand" ? " (the brand's default)" : " (the default)";
|
|
563
|
+
ctx.ui.notify(`icons: ${iconSet()}${why}. Nerd needs a Nerd Font in the terminal. /chassis icons nerd|plain|auto`, "info");
|
|
564
|
+
return;
|
|
565
|
+
}
|
|
566
|
+
if (sub === "alert" || sub.startsWith("alert ")) {
|
|
567
|
+
const arg = sub.slice("alert".length).trim();
|
|
568
|
+
if (arg) {
|
|
569
|
+
const change = parseAlertArg(arg);
|
|
570
|
+
if (!change) {
|
|
571
|
+
ctx.ui.notify("Usage: /chassis alert [off|on|SECONDS] (for example 45, 90s or 2m)", "warning");
|
|
572
|
+
return;
|
|
573
|
+
}
|
|
574
|
+
alertSettings = { ...alertSettings, ...change };
|
|
575
|
+
saveAlert(alertSettings);
|
|
576
|
+
}
|
|
577
|
+
ctx.ui.notify(describeAlert(alertSettings), "info");
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
if (sub === "off") {
|
|
581
|
+
enabled = false;
|
|
582
|
+
uninstall(ctx);
|
|
583
|
+
ctx.ui.notify("chassis off for this session. /chassis on to restore.", "info");
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
if (sub === "on") {
|
|
587
|
+
enabled = true;
|
|
588
|
+
install(ctx);
|
|
589
|
+
render();
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
592
|
+
if (sub === "sidebar") {
|
|
593
|
+
layout?.toggle();
|
|
594
|
+
render();
|
|
595
|
+
return;
|
|
596
|
+
}
|
|
597
|
+
const widgets = [...registry.widgets.keys()].join(", ") || "none";
|
|
598
|
+
ctx.ui.notify(
|
|
599
|
+
`chassis ${active ? "on" : "off"} · layout ${state.layout} · sidebar ${layout?.isShown() ? "shown" : "hidden"} · mode ${state.mode}\nwidgets: ${widgets}`,
|
|
600
|
+
"info",
|
|
601
|
+
);
|
|
602
|
+
},
|
|
603
|
+
};
|
|
604
|
+
pi.registerCommand("chassis", chassisCommand);
|
|
605
|
+
}
|