@narumitw/pi-statusline 0.49.15 → 0.50.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/statusline.ts CHANGED
@@ -1,25 +1,22 @@
1
1
  import { homedir } from "node:os";
2
- import {
3
- type ExtensionAPI,
4
- type ExtensionContext,
5
- getAgentDir,
6
- } from "@earendil-works/pi-coding-agent";
2
+ import { type ExtensionAPI, type ExtensionContext, getAgentDir } from "@earendil-works/pi-coding-agent";
3
+ import { getCapabilities } from "@earendil-works/pi-tui";
7
4
  import { completeStatuslineArguments } from "./command-contract.js";
8
5
  import type { StatuslineCommandOptions } from "./commands.js";
9
6
  import {
10
- buildExtensionStatusIconAliases,
11
- type ExtensionStatusIconAliasMap,
12
- findDuplicateExtensions,
13
- readInstalledExtensionPackages,
7
+ buildExtensionStatusIconAliases,
8
+ type ExtensionStatusIconAliasMap,
9
+ findDuplicateExtensions,
10
+ readInstalledExtensionPackages,
14
11
  } from "./extension-status.js";
15
12
  import { type GitStatusSummary, gitStatusSummaryEqual, readGitStatus } from "./git-status.js";
16
13
  import { type RuntimeState, renderExtensionStatusline, renderStatusline } from "./render.js";
17
14
  import {
18
- consumeStatuslineSettingsNotice,
19
- type LoadedStatuslineSettings,
20
- loadStatuslineSettings,
21
- loadStatuslineSettingsForAgent,
22
- settingsFilePath,
15
+ consumeStatuslineSettingsNotice,
16
+ type LoadedStatuslineSettings,
17
+ loadStatuslineSettings,
18
+ loadStatuslineSettingsForAgent,
19
+ settingsFilePath,
23
20
  } from "./settings.js";
24
21
  import type { PalettePreset } from "./types.js";
25
22
 
@@ -32,345 +29,355 @@ type StatuslineCommands = typeof import("./commands.js");
32
29
  let statuslineCommandsPromise: Promise<StatuslineCommands> | undefined;
33
30
 
34
31
  function loadStatuslineCommands(): Promise<StatuslineCommands> {
35
- if (!statuslineCommandsPromise) {
36
- statuslineCommandsPromise = import("./commands.js").catch((error) => {
37
- statuslineCommandsPromise = undefined;
38
- throw error;
39
- });
40
- }
41
- return statuslineCommandsPromise;
32
+ if (!statuslineCommandsPromise) {
33
+ statuslineCommandsPromise = import("./commands.js").catch((error) => {
34
+ statuslineCommandsPromise = undefined;
35
+ throw error;
36
+ });
37
+ }
38
+ return statuslineCommandsPromise;
42
39
  }
43
40
 
44
41
  export default function statusline(pi: ExtensionAPI) {
45
- let loaded: LoadedStatuslineSettings | undefined;
46
- let previewPalettePreset: PalettePreset | undefined;
47
- let activeSessionManager: ExtensionContext["sessionManager"] | undefined;
48
- const runtime: RuntimeState = {
49
- turnCount: 0,
50
- activeTools: new Map(),
51
- isStreaming: false,
52
- thinkingLevel: "off",
53
- duplicateExtensions: [],
54
- extensionStatusIconAliases: EMPTY_EXTENSION_STATUS_ICON_ALIASES,
55
- };
56
-
57
- let sessionGeneration = 0;
58
- let menuController = new AbortController();
59
- let gitStatusRequestId = 0;
60
- let activeGitStatusTarget: { cwd: string; generation: number } | undefined;
61
- let gitStatusRefreshInFlight = false;
62
- let gitStatusAbortController: AbortController | undefined;
63
- let gitStatusDebounceTimer: ReturnType<typeof setTimeout> | undefined;
64
- let pendingGitStatusRefresh: { cwd: string; generation: number; requestId: number } | undefined;
65
-
66
- const refresh = () => runtime.requestRender?.();
67
- const ownsRuntime = (ctx: ExtensionContext) => ctx.sessionManager === activeSessionManager;
68
-
69
- const setGitStatus = (summary: GitStatusSummary | undefined) => {
70
- if (gitStatusSummaryEqual(runtime.gitStatus, summary)) return;
71
- runtime.gitStatus = summary;
72
- refresh();
73
- };
74
-
75
- const clearGitStatusDebounce = () => {
76
- if (!gitStatusDebounceTimer) return;
77
- clearTimeout(gitStatusDebounceTimer);
78
- gitStatusDebounceTimer = undefined;
79
- };
80
-
81
- const abortGitStatusRefresh = (reason: string) => {
82
- gitStatusAbortController?.abort(new DOMException(reason, "AbortError"));
83
- };
84
-
85
- const isActiveGitStatusTarget = (cwd: string, generation: number) =>
86
- activeGitStatusTarget?.cwd === cwd &&
87
- activeGitStatusTarget.generation === generation &&
88
- generation === sessionGeneration;
89
-
90
- const isCurrentGitStatusRequest = (cwd: string, generation: number, requestId: number) =>
91
- isActiveGitStatusTarget(cwd, generation) && requestId === gitStatusRequestId;
92
-
93
- const runGitStatusRefresh = (cwd: string, generation: number, requestId: number) => {
94
- if (!isCurrentGitStatusRequest(cwd, generation, requestId)) return;
95
- if (gitStatusRefreshInFlight) {
96
- pendingGitStatusRefresh = { cwd, generation, requestId };
97
- return;
98
- }
99
-
100
- gitStatusRefreshInFlight = true;
101
- const abortController = new AbortController();
102
- gitStatusAbortController = abortController;
103
- void (async () => {
104
- try {
105
- const summary = await readGitStatus(pi, cwd, abortController.signal);
106
- if (isCurrentGitStatusRequest(cwd, generation, requestId)) setGitStatus(summary);
107
- } catch {
108
- if (isCurrentGitStatusRequest(cwd, generation, requestId)) setGitStatus(undefined);
109
- } finally {
110
- gitStatusRefreshInFlight = false;
111
- if (gitStatusAbortController === abortController) gitStatusAbortController = undefined;
112
- const pending = pendingGitStatusRefresh;
113
- pendingGitStatusRefresh = undefined;
114
- if (pending) runGitStatusRefresh(pending.cwd, pending.generation, pending.requestId);
115
- }
116
- })();
117
- };
118
-
119
- const refreshGitStatus = (cwd: string, generation = sessionGeneration) => {
120
- if (!isActiveGitStatusTarget(cwd, generation)) return;
121
- runGitStatusRefresh(cwd, generation, ++gitStatusRequestId);
122
- };
123
-
124
- const scheduleGitStatusRefresh = (cwd: string, generation = sessionGeneration) => {
125
- if (!isActiveGitStatusTarget(cwd, generation)) return;
126
- const requestId = ++gitStatusRequestId;
127
- clearGitStatusDebounce();
128
- gitStatusDebounceTimer = setTimeout(() => {
129
- gitStatusDebounceTimer = undefined;
130
- runGitStatusRefresh(cwd, generation, requestId);
131
- }, GIT_STATUS_EVENT_DEBOUNCE_MS);
132
- };
133
-
134
- const scheduleGitStatusRefreshForContext = (ctx: ExtensionContext) => {
135
- if (!activeGitStatusTarget || activeGitStatusTarget.cwd !== ctx.cwd) return;
136
- scheduleGitStatusRefresh(activeGitStatusTarget.cwd, activeGitStatusTarget.generation);
137
- };
138
-
139
- const installFooter = (ctx: ExtensionContext) => {
140
- const generation = ++sessionGeneration;
141
- menuController.abort(new DOMException("Statusline session context replaced", "AbortError"));
142
- menuController = new AbortController();
143
- const cwd = ctx.cwd;
144
- activeSessionManager = ctx.sessionManager;
145
- runtime.homeDir = homedir();
146
- previewPalettePreset = undefined;
147
- abortGitStatusRefresh("Statusline session context replaced");
148
- clearGitStatusDebounce();
149
- activeGitStatusTarget = ctx.mode === "tui" ? { cwd, generation } : undefined;
150
- runtime.gitStatus = undefined;
151
- runtime.duplicateExtensions = [];
152
- runtime.extensionStatusIconAliases = EMPTY_EXTENSION_STATUS_ICON_ALIASES;
153
- ctx.ui.setStatus(STATUSLINE_KEY, undefined);
154
- if (!activeGitStatusTarget || !loaded) return;
155
- const installedPackages = readInstalledExtensionPackages(cwd);
156
- runtime.duplicateExtensions = findDuplicateExtensions(installedPackages);
157
- runtime.extensionStatusIconAliases = buildExtensionStatusIconAliases(installedPackages);
158
- ctx.ui.setFooter((tui, theme, footerData) => {
159
- runtime.requestRender = () => tui.requestRender();
160
-
161
- const refreshFooterGitStatus = () => refreshGitStatus(cwd, generation);
162
- const branchUnsubscribe = footerData.onBranchChange(() => {
163
- runtime.gitStatus = undefined;
164
- abortGitStatusRefresh("Statusline Git branch changed");
165
- clearGitStatusDebounce();
166
- refreshFooterGitStatus();
167
- tui.requestRender();
168
- });
169
- const clock = setInterval(() => {
170
- clearGitStatusDebounce();
171
- refreshFooterGitStatus();
172
- tui.requestRender();
173
- }, GIT_STATUS_REFRESH_INTERVAL_MS);
174
-
175
- return {
176
- dispose() {
177
- branchUnsubscribe();
178
- clearInterval(clock);
179
- if (isActiveGitStatusTarget(cwd, generation)) {
180
- activeGitStatusTarget = undefined;
181
- abortGitStatusRefresh("Statusline footer disposed");
182
- clearGitStatusDebounce();
183
- pendingGitStatusRefresh = undefined;
184
- runtime.gitStatus = undefined;
185
- runtime.duplicateExtensions = [];
186
- runtime.extensionStatusIconAliases = EMPTY_EXTENSION_STATUS_ICON_ALIASES;
187
- runtime.requestRender = undefined;
188
- }
189
- },
190
- invalidate() {},
191
- render(width: number): string[] {
192
- if (!loaded) return [];
193
- const config = previewPalettePreset
194
- ? { ...loaded.config, palettePreset: previewPalettePreset }
195
- : loaded.config;
196
- const mainLine = renderStatusline(width, ctx, footerData, theme, config, runtime);
197
- const lines = mainLine ? mainLine.split("\n") : [];
198
- lines.push(
199
- ...renderExtensionStatusline(width, footerData, theme, config, runtime, mainLine),
200
- );
201
- return lines;
202
- },
203
- };
204
- });
205
- refreshGitStatus(cwd, generation);
206
- };
207
-
208
- const agentDir = getAgentDir();
209
- const configPath = settingsFilePath(agentDir);
210
- const commandOptions: StatuslineCommandOptions = {
211
- settingsPath: configPath,
212
- getLoaded: () => loaded ?? loadStatuslineSettings(configPath),
213
- getMenuOwner: () => {
214
- const generation = sessionGeneration;
215
- return {
216
- signal: menuController.signal,
217
- isCurrent: () => generation === sessionGeneration && !menuController.signal.aborted,
218
- };
219
- },
220
- apply(next, ctx) {
221
- if (ctx.sessionManager !== activeSessionManager) return;
222
- previewPalettePreset = undefined;
223
- loaded = next;
224
- refresh();
225
- },
226
- preview(palettePreset, ctx) {
227
- if (ctx.sessionManager !== activeSessionManager) return;
228
- previewPalettePreset = palettePreset;
229
- refresh();
230
- },
231
- };
232
- pi.registerCommand("statusline", {
233
- description: "Open or inspect the statusline settings",
234
- getArgumentCompletions: completeStatuslineArguments,
235
- handler: async (args, ctx) => {
236
- const commands = await loadStatuslineCommands();
237
- if (ctx.sessionManager !== activeSessionManager) return;
238
- await commands.handleStatuslineCommand(args, ctx, commandOptions);
239
- },
240
- });
241
-
242
- pi.on("session_start", (_event, ctx) => {
243
- runtime.turnCount = 0;
244
- runtime.activeTools.clear();
245
- runtime.isStreaming = false;
246
- loaded = loadStatuslineSettingsForAgent(agentDir);
247
- const settingsNotice = consumeStatuslineSettingsNotice();
248
- if (settingsNotice) ctx.ui.notify(settingsNotice, "warning");
249
- if (loaded.diagnostics.length > 0) {
250
- ctx.ui.notify(formatSettingsDiagnostics(loaded), "warning");
251
- }
252
- runtime.thinkingLevel = pi.getThinkingLevel();
253
- installFooter(ctx);
254
- });
255
-
256
- pi.on("session_tree", (_event, ctx) => {
257
- installFooter(ctx);
258
- refresh();
259
- });
260
-
261
- pi.on("session_shutdown", (_event, ctx) => {
262
- if (!ownsRuntime(ctx)) return;
263
- sessionGeneration += 1;
264
- menuController.abort(new DOMException("Statusline session shut down", "AbortError"));
265
- activeSessionManager = undefined;
266
- previewPalettePreset = undefined;
267
- activeGitStatusTarget = undefined;
268
- abortGitStatusRefresh("Statusline session shut down");
269
- clearGitStatusDebounce();
270
- pendingGitStatusRefresh = undefined;
271
- runtime.gitStatus = undefined;
272
- runtime.activeTools.clear();
273
- runtime.isStreaming = false;
274
- runtime.duplicateExtensions = [];
275
- runtime.extensionStatusIconAliases = EMPTY_EXTENSION_STATUS_ICON_ALIASES;
276
- ctx.ui.setFooter(undefined);
277
- ctx.ui.setStatus(STATUSLINE_KEY, undefined);
278
- runtime.requestRender = undefined;
279
- });
280
-
281
- pi.on("model_select", () => refresh());
282
-
283
- pi.on("thinking_level_select", (event) => {
284
- runtime.thinkingLevel = event.level;
285
- refresh();
286
- });
287
-
288
- pi.on("agent_start", (_event, ctx) => {
289
- if (!ownsRuntime(ctx)) return;
290
- runtime.isStreaming = true;
291
- refresh();
292
- });
293
-
294
- pi.on("agent_end", (_event, ctx) => {
295
- if (!ownsRuntime(ctx)) return;
296
- runtime.activeTools.clear();
297
- scheduleGitStatusRefreshForContext(ctx);
298
- refresh();
299
- });
300
-
301
- pi.on("agent_settled", (_event, ctx) => {
302
- if (!ownsRuntime(ctx)) return;
303
- runtime.isStreaming = false;
304
- runtime.activeTools.clear();
305
- refresh();
306
- });
307
-
308
- pi.on("turn_start", (_event, ctx) => {
309
- if (!ownsRuntime(ctx)) return;
310
- runtime.turnCount += 1;
311
- runtime.isStreaming = true;
312
- refresh();
313
- });
314
-
315
- pi.on("turn_end", (_event, ctx) => {
316
- if (!ownsRuntime(ctx)) return;
317
- scheduleGitStatusRefreshForContext(ctx);
318
- refresh();
319
- });
320
-
321
- pi.on("tool_execution_start", (event, ctx) => {
322
- if (!ownsRuntime(ctx)) return;
323
- const currentCount = runtime.activeTools.get(event.toolName) ?? 0;
324
- runtime.activeTools.set(event.toolName, currentCount + 1);
325
- refresh();
326
- });
327
-
328
- pi.on("tool_execution_end", (event, ctx) => {
329
- if (!ownsRuntime(ctx)) return;
330
- const currentCount = runtime.activeTools.get(event.toolName) ?? 0;
331
- if (currentCount <= 1) runtime.activeTools.delete(event.toolName);
332
- else runtime.activeTools.set(event.toolName, currentCount - 1);
333
-
334
- scheduleGitStatusRefreshForContext(ctx);
335
- refresh();
336
- });
42
+ let loaded: LoadedStatuslineSettings | undefined;
43
+ let previewPalettePreset: PalettePreset | undefined;
44
+ let activeSessionManager: ExtensionContext["sessionManager"] | undefined;
45
+ const runtime: RuntimeState = {
46
+ turnCount: 0,
47
+ activeTools: new Map(),
48
+ isStreaming: false,
49
+ thinkingLevel: "off",
50
+ duplicateExtensions: [],
51
+ extensionStatusIconAliases: EMPTY_EXTENSION_STATUS_ICON_ALIASES,
52
+ };
53
+
54
+ let sessionGeneration = 0;
55
+ let menuController = new AbortController();
56
+ let gitStatusRequestId = 0;
57
+ let activeGitStatusTarget: { cwd: string; generation: number } | undefined;
58
+ let gitStatusRefreshInFlight = false;
59
+ let gitStatusAbortController: AbortController | undefined;
60
+ let gitStatusDebounceTimer: ReturnType<typeof setTimeout> | undefined;
61
+ let pendingGitStatusRefresh: { cwd: string; generation: number; requestId: number } | undefined;
62
+
63
+ const refresh = () => runtime.requestRender?.();
64
+ const ownsRuntime = (ctx: ExtensionContext) => ctx.sessionManager === activeSessionManager;
65
+
66
+ const setGitStatus = (summary: GitStatusSummary | undefined) => {
67
+ if (gitStatusSummaryEqual(runtime.gitStatus, summary)) return;
68
+ runtime.gitStatus = summary;
69
+ refresh();
70
+ };
71
+
72
+ const clearGitStatusDebounce = () => {
73
+ if (!gitStatusDebounceTimer) return;
74
+ clearTimeout(gitStatusDebounceTimer);
75
+ gitStatusDebounceTimer = undefined;
76
+ };
77
+
78
+ const abortGitStatusRefresh = (reason: string) => {
79
+ gitStatusAbortController?.abort(new DOMException(reason, "AbortError"));
80
+ };
81
+
82
+ const isActiveGitStatusTarget = (cwd: string, generation: number) =>
83
+ activeGitStatusTarget?.cwd === cwd &&
84
+ activeGitStatusTarget.generation === generation &&
85
+ generation === sessionGeneration;
86
+
87
+ const isCurrentGitStatusRequest = (cwd: string, generation: number, requestId: number) =>
88
+ isActiveGitStatusTarget(cwd, generation) && requestId === gitStatusRequestId;
89
+
90
+ const runGitStatusRefresh = (cwd: string, generation: number, requestId: number) => {
91
+ if (!isCurrentGitStatusRequest(cwd, generation, requestId)) return;
92
+ if (gitStatusRefreshInFlight) {
93
+ pendingGitStatusRefresh = { cwd, generation, requestId };
94
+ return;
95
+ }
96
+
97
+ gitStatusRefreshInFlight = true;
98
+ const abortController = new AbortController();
99
+ gitStatusAbortController = abortController;
100
+ void (async () => {
101
+ try {
102
+ const summary = await readGitStatus(pi, cwd, abortController.signal);
103
+ if (isCurrentGitStatusRequest(cwd, generation, requestId)) setGitStatus(summary);
104
+ } catch {
105
+ if (isCurrentGitStatusRequest(cwd, generation, requestId)) setGitStatus(undefined);
106
+ } finally {
107
+ gitStatusRefreshInFlight = false;
108
+ if (gitStatusAbortController === abortController) gitStatusAbortController = undefined;
109
+ const pending = pendingGitStatusRefresh;
110
+ pendingGitStatusRefresh = undefined;
111
+ if (pending) runGitStatusRefresh(pending.cwd, pending.generation, pending.requestId);
112
+ }
113
+ })();
114
+ };
115
+
116
+ const refreshGitStatus = (cwd: string, generation = sessionGeneration) => {
117
+ if (!isActiveGitStatusTarget(cwd, generation)) return;
118
+ runGitStatusRefresh(cwd, generation, ++gitStatusRequestId);
119
+ };
120
+
121
+ const scheduleGitStatusRefresh = (cwd: string, generation = sessionGeneration) => {
122
+ if (!isActiveGitStatusTarget(cwd, generation)) return;
123
+ const requestId = ++gitStatusRequestId;
124
+ clearGitStatusDebounce();
125
+ gitStatusDebounceTimer = setTimeout(() => {
126
+ gitStatusDebounceTimer = undefined;
127
+ runGitStatusRefresh(cwd, generation, requestId);
128
+ }, GIT_STATUS_EVENT_DEBOUNCE_MS);
129
+ };
130
+
131
+ const scheduleGitStatusRefreshForContext = (ctx: ExtensionContext) => {
132
+ if (!activeGitStatusTarget || activeGitStatusTarget.cwd !== ctx.cwd) return;
133
+ scheduleGitStatusRefresh(activeGitStatusTarget.cwd, activeGitStatusTarget.generation);
134
+ };
135
+
136
+ const installFooter = (ctx: ExtensionContext) => {
137
+ const generation = ++sessionGeneration;
138
+ menuController.abort(new DOMException("Statusline session context replaced", "AbortError"));
139
+ menuController = new AbortController();
140
+ const cwd = ctx.cwd;
141
+ activeSessionManager = ctx.sessionManager;
142
+ runtime.homeDir = homedir();
143
+ previewPalettePreset = undefined;
144
+ abortGitStatusRefresh("Statusline session context replaced");
145
+ clearGitStatusDebounce();
146
+ activeGitStatusTarget = ctx.mode === "tui" ? { cwd, generation } : undefined;
147
+ runtime.gitStatus = undefined;
148
+ runtime.duplicateExtensions = [];
149
+ runtime.extensionStatusIconAliases = EMPTY_EXTENSION_STATUS_ICON_ALIASES;
150
+ ctx.ui.setStatus(STATUSLINE_KEY, undefined);
151
+ if (!activeGitStatusTarget || !loaded) return;
152
+ const installedPackages = readInstalledExtensionPackages(cwd);
153
+ runtime.duplicateExtensions = findDuplicateExtensions(installedPackages);
154
+ runtime.extensionStatusIconAliases = buildExtensionStatusIconAliases(installedPackages);
155
+ ctx.ui.setFooter((tui, theme, footerData) => {
156
+ runtime.requestRender = () => tui.requestRender();
157
+
158
+ const refreshFooterGitStatus = () => refreshGitStatus(cwd, generation);
159
+ const branchUnsubscribe = footerData.onBranchChange(() => {
160
+ runtime.gitStatus = undefined;
161
+ abortGitStatusRefresh("Statusline Git branch changed");
162
+ clearGitStatusDebounce();
163
+ refreshFooterGitStatus();
164
+ tui.requestRender();
165
+ });
166
+ const clock = setInterval(() => {
167
+ clearGitStatusDebounce();
168
+ refreshFooterGitStatus();
169
+ tui.requestRender();
170
+ }, GIT_STATUS_REFRESH_INTERVAL_MS);
171
+
172
+ return {
173
+ dispose() {
174
+ branchUnsubscribe();
175
+ clearInterval(clock);
176
+ if (isActiveGitStatusTarget(cwd, generation)) {
177
+ activeGitStatusTarget = undefined;
178
+ abortGitStatusRefresh("Statusline footer disposed");
179
+ clearGitStatusDebounce();
180
+ pendingGitStatusRefresh = undefined;
181
+ runtime.gitStatus = undefined;
182
+ runtime.duplicateExtensions = [];
183
+ runtime.extensionStatusIconAliases = EMPTY_EXTENSION_STATUS_ICON_ALIASES;
184
+ runtime.requestRender = undefined;
185
+ }
186
+ },
187
+ invalidate() {},
188
+ render(width: number): string[] {
189
+ if (!loaded) return [];
190
+ const config = previewPalettePreset
191
+ ? { ...loaded.config, palettePreset: previewPalettePreset }
192
+ : loaded.config;
193
+ const trueColor = getCapabilities().trueColor;
194
+ const mainLine = renderStatusline(width, ctx, footerData, theme, config, runtime, trueColor);
195
+ const lines = mainLine ? mainLine.split("\n") : [];
196
+ lines.push(...renderExtensionStatusline(width, footerData, theme, config, runtime, mainLine, trueColor));
197
+ return lines;
198
+ },
199
+ };
200
+ });
201
+ refreshGitStatus(cwd, generation);
202
+ };
203
+
204
+ const agentDir = getAgentDir();
205
+ const configPath = settingsFilePath(agentDir);
206
+ const commandOptions: StatuslineCommandOptions = {
207
+ settingsPath: configPath,
208
+ getLoaded: () => loaded ?? loadStatuslineSettings(configPath),
209
+ getMenuOwner: () => {
210
+ const generation = sessionGeneration;
211
+ return {
212
+ signal: menuController.signal,
213
+ isCurrent: () => generation === sessionGeneration && !menuController.signal.aborted,
214
+ };
215
+ },
216
+ apply(next, ctx) {
217
+ if (ctx.sessionManager !== activeSessionManager) return;
218
+ previewPalettePreset = undefined;
219
+ loaded = next;
220
+ refresh();
221
+ },
222
+ preview(palettePreset, ctx) {
223
+ if (ctx.sessionManager !== activeSessionManager) return;
224
+ previewPalettePreset = palettePreset;
225
+ refresh();
226
+ },
227
+ };
228
+ pi.registerCommand("statusline", {
229
+ description: "Open or inspect the statusline settings",
230
+ getArgumentCompletions: completeStatuslineArguments,
231
+ handler: async (args, ctx) => {
232
+ const commands = await loadStatuslineCommands();
233
+ if (ctx.sessionManager !== activeSessionManager) return;
234
+ await commands.handleStatuslineCommand(args, ctx, commandOptions);
235
+ },
236
+ });
237
+
238
+ pi.on("session_start", (_event, ctx) => {
239
+ runtime.turnCount = 0;
240
+ runtime.activeTools.clear();
241
+ runtime.isStreaming = false;
242
+ runtime.uiPrompt = undefined;
243
+ loaded = loadStatuslineSettingsForAgent(agentDir);
244
+ const settingsNotice = consumeStatuslineSettingsNotice();
245
+ if (settingsNotice) ctx.ui.notify(settingsNotice, "warning");
246
+ if (loaded.diagnostics.length > 0) {
247
+ ctx.ui.notify(formatSettingsDiagnostics(loaded), "warning");
248
+ }
249
+ runtime.thinkingLevel = pi.getThinkingLevel();
250
+ installFooter(ctx);
251
+ });
252
+
253
+ pi.on("session_tree", (_event, ctx) => {
254
+ installFooter(ctx);
255
+ refresh();
256
+ });
257
+
258
+ pi.on("session_shutdown", (_event, ctx) => {
259
+ if (!ownsRuntime(ctx)) return;
260
+ sessionGeneration += 1;
261
+ menuController.abort(new DOMException("Statusline session shut down", "AbortError"));
262
+ activeSessionManager = undefined;
263
+ previewPalettePreset = undefined;
264
+ activeGitStatusTarget = undefined;
265
+ abortGitStatusRefresh("Statusline session shut down");
266
+ clearGitStatusDebounce();
267
+ pendingGitStatusRefresh = undefined;
268
+ runtime.gitStatus = undefined;
269
+ runtime.activeTools.clear();
270
+ runtime.isStreaming = false;
271
+ runtime.uiPrompt = undefined;
272
+ runtime.duplicateExtensions = [];
273
+ runtime.extensionStatusIconAliases = EMPTY_EXTENSION_STATUS_ICON_ALIASES;
274
+ ctx.ui.setFooter(undefined);
275
+ ctx.ui.setStatus(STATUSLINE_KEY, undefined);
276
+ runtime.requestRender = undefined;
277
+ });
278
+
279
+ pi.on("model_select", () => refresh());
280
+
281
+ pi.on("thinking_level_select", (event) => {
282
+ runtime.thinkingLevel = event.level;
283
+ refresh();
284
+ });
285
+
286
+ pi.on("agent_start", (_event, ctx) => {
287
+ if (!ownsRuntime(ctx)) return;
288
+ runtime.isStreaming = true;
289
+ refresh();
290
+ });
291
+
292
+ pi.on("agent_end", (_event, ctx) => {
293
+ if (!ownsRuntime(ctx)) return;
294
+ runtime.activeTools.clear();
295
+ scheduleGitStatusRefreshForContext(ctx);
296
+ refresh();
297
+ });
298
+
299
+ pi.on("agent_settled", (_event, ctx) => {
300
+ if (!ownsRuntime(ctx)) return;
301
+ runtime.isStreaming = false;
302
+ runtime.activeTools.clear();
303
+ refresh();
304
+ });
305
+
306
+ pi.on("ui_prompt_start", (event, ctx) => {
307
+ if (!ownsRuntime(ctx)) return;
308
+ runtime.uiPrompt = { kind: event.kind, title: event.title };
309
+ refresh();
310
+ });
311
+
312
+ pi.on("ui_prompt_end", (_event, ctx) => {
313
+ if (!ownsRuntime(ctx)) return;
314
+ runtime.uiPrompt = undefined;
315
+ refresh();
316
+ });
317
+
318
+ pi.on("turn_start", (_event, ctx) => {
319
+ if (!ownsRuntime(ctx)) return;
320
+ runtime.turnCount += 1;
321
+ runtime.isStreaming = true;
322
+ refresh();
323
+ });
324
+
325
+ pi.on("turn_end", (_event, ctx) => {
326
+ if (!ownsRuntime(ctx)) return;
327
+ scheduleGitStatusRefreshForContext(ctx);
328
+ refresh();
329
+ });
330
+
331
+ pi.on("tool_execution_start", (event, ctx) => {
332
+ if (!ownsRuntime(ctx)) return;
333
+ const currentCount = runtime.activeTools.get(event.toolName) ?? 0;
334
+ runtime.activeTools.set(event.toolName, currentCount + 1);
335
+ refresh();
336
+ });
337
+
338
+ pi.on("tool_execution_end", (event, ctx) => {
339
+ if (!ownsRuntime(ctx)) return;
340
+ const currentCount = runtime.activeTools.get(event.toolName) ?? 0;
341
+ if (currentCount <= 1) runtime.activeTools.delete(event.toolName);
342
+ else runtime.activeTools.set(event.toolName, currentCount - 1);
343
+
344
+ scheduleGitStatusRefreshForContext(ctx);
345
+ refresh();
346
+ });
337
347
  }
338
348
 
339
349
  function formatSettingsDiagnostics(loaded: LoadedStatuslineSettings): string {
340
- const details = loaded.diagnostics.slice(0, 5).map((item) => item.message);
341
- const remaining = loaded.diagnostics.length - details.length;
342
- return [
343
- `pi-statusline settings: ${details.join("; ")}`,
344
- ...(remaining > 0 ? [`+${remaining} more`] : []),
345
- ].join(" ");
350
+ const details = loaded.diagnostics.slice(0, 5).map((item) => item.message);
351
+ const remaining = loaded.diagnostics.length - details.length;
352
+ return [`pi-statusline settings: ${details.join("; ")}`, ...(remaining > 0 ? [`+${remaining} more`] : [])].join(" ");
346
353
  }
347
354
 
348
355
  export {
349
- buildExtensionStatusIconAliases,
350
- type ExtensionStatusIconAliasMap,
351
- extensionColor,
352
- formatExtensionStatus,
353
- npmPackageName,
354
- simplifyExtensionStatusText,
355
- splitExtensionStatusIcon,
356
- stripExtensionStatusPrefix,
357
- wrapExtensionStatusline,
356
+ buildExtensionStatusIconAliases,
357
+ type ExtensionStatusIconAliasMap,
358
+ extensionColor,
359
+ formatExtensionStatus,
360
+ npmPackageName,
361
+ simplifyExtensionStatusText,
362
+ splitExtensionStatusIcon,
363
+ stripExtensionStatusPrefix,
364
+ wrapExtensionStatusline,
358
365
  } from "./extension-status.js";
359
366
  export {
360
- formatGitBranchText,
361
- formatGitBranchValue,
362
- formatGitStatusSummary,
363
- type GitStatusSummary,
364
- parseGitRoot,
365
- parseGitStatusPorcelain,
366
- readGitStatus,
367
+ formatGitBranchText,
368
+ formatGitBranchValue,
369
+ formatGitStatusSummary,
370
+ type GitStatusSummary,
371
+ parseGitRoot,
372
+ parseGitStatusPorcelain,
373
+ readGitStatus,
367
374
  } from "./git-status.js";
368
375
  export {
369
- contextColor,
370
- formatCount,
371
- formatToolActivity,
372
- prContextFromStatuses,
373
- prLinkFromStatuses,
374
- shortenModel,
376
+ contextColor,
377
+ formatCount,
378
+ formatToolActivity,
379
+ prContextFromStatuses,
380
+ prLinkFromStatuses,
381
+ shortenModel,
375
382
  } from "./render.js";
376
383
  export { normalizeStatuslineSettings, readStatuslineSettings } from "./settings.js";