@narumitw/pi-statusline 0.50.0 → 0.50.2

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