pi-usage-limits 0.1.3 → 0.1.4

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.
Files changed (2) hide show
  1. package/index.ts +18 -10
  2. package/package.json +1 -1
package/index.ts CHANGED
@@ -5,17 +5,25 @@ import type {
5
5
  } from "@earendil-works/pi-coding-agent";
6
6
  import { getUsage } from "./usage";
7
7
 
8
- // pi awaits handler promises, so awaiting here blocks the redraw.
9
- function refresh(_event: ExtensionEvent, ctx: ExtensionContext) {
10
- if (!ctx.model) return;
11
- getUsage(ctx.model, ctx.modelRegistry)
12
- .then((usage) =>
13
- ctx.ui.setStatus("model-usage", ctx.ui.theme.fg("dim", usage)),
14
- )
15
- .catch((e) => ctx.ui.setStatus("model-usage", ctx.ui.theme.fg("dim", e)));
16
- }
17
-
18
8
  export default function (pi: ExtensionAPI) {
9
+ let generation = 0;
10
+
11
+ // pi awaits handler promises, so awaiting here blocks the redraw.
12
+ function refresh(_event: ExtensionEvent, ctx: ExtensionContext) {
13
+ const current = ++generation;
14
+ if (ctx.mode !== "tui" || !ctx.model) return;
15
+
16
+ const setStatus = (text: string) => {
17
+ // Shutdown or a newer refresh invalidates this captured context.
18
+ if (current !== generation) return;
19
+ ctx.ui.setStatus("model-usage", ctx.ui.theme.fg("dim", text));
20
+ };
21
+ getUsage(ctx.model, ctx.modelRegistry).then(setStatus, setStatus);
22
+ }
23
+
24
+ pi.on("session_shutdown", () => {
25
+ generation++;
26
+ });
19
27
  pi.on("session_start", refresh);
20
28
  pi.on("model_select", refresh);
21
29
  pi.on("agent_settled", refresh);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-usage-limits",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "pi-package",