@underactive/pi-topping 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Eric Sison
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # pi-topping
2
+
3
+ We garnish our pies. It seemed rude not to extend Pi the same courtesy. This is a [Pi](https://github.com/earendil-works/pi-coding-agent) extension that changes the default "Working..." loader with a shimmering activity word, a scrolling token-activity meter, an elapsed timer, and a live output-token count.
4
+
5
+ ![Demo of pi-topping's shimmering activity word, scrolling activity meter, elapsed timer, and token count](https://raw.githubusercontent.com/underactive/pi-topping/main/demo.gif)
6
+
7
+ ## What it does
8
+
9
+ - Picks a random activity word (`Cerebrating…`, `Noodling…`, `Zigzagging…`, etc.) when you submit a prompt, and a new one each time a tool call starts.
10
+ - Sweeps a theme-aware shimmer across the word while it's showing.
11
+ - Shows an elapsed timer (`Xm YYs`) that counts up from when you submitted the prompt.
12
+ - Shows a live output-token estimate (`↓ N tokens`), reconciled against the real usage total once each assistant message finishes.
13
+ - Resets everything — word, timer, token count — on each new prompt, and restores Pi's normal loader between turns.
14
+
15
+ ## Activity meter
16
+
17
+ Next to Pi's spinner there's an eight-column scrolling meter that reflects how fast tokens are streaming in. It updates every 100ms and uses your theme's accent color, so it should blend in with whatever theme you're running.
18
+
19
+ ## Settings
20
+
21
+ Run `/topping-settings` (TUI only) to toggle things on or off:
22
+
23
+ ```
24
+ ╔═[ Pi Topping: Settings ]══════════════════════════╗
25
+ ╟─ Preview ─────────────────────────────────────────╢
26
+ ║ ║
27
+ ║ ⠋ Crafting ⣤⣤⣤⣤ (0m 03s · ↓ 84 tokens) ║
28
+ ║ ║
29
+ ╟─ Decorations ─────────────────────────────────────╢
30
+ ║ ▸ [■] Animated spinner ON ║
31
+ ║ [■] "Working..." text shimmer ON ║
32
+ ║ [■] Token activity monitor ON ║
33
+ ╟─ Features ────────────────────────────────────────╢
34
+ ║ [■] Substitute Pi's "Working..." message ON ║
35
+ ║ [■] Elapsed time since prompt ON ║
36
+ ║ [■] Show output tokens ON ║
37
+ ╟───────────────────────────────────────────────────╢
38
+ ║ ↑↓ move ␣ toggle ⏎ apply esc cancel ║
39
+ ╚═══════════════════════════════════════════════════╝
40
+ ```
41
+
42
+ **Decorations** — animated spinner, word shimmer, activity meter.
43
+
44
+ **Features** — whether to substitute the random word at all, the elapsed timer, and the token count.
45
+
46
+ The menu has a live preview so you can see changes as you make them. Everything is on by default, so nothing changes unless you open the menu. Use `↑↓` to move, `␣` to toggle, `⏎` to save, `esc` to cancel. Settings are saved to `~/.pi/agent/pi-topping/settings.json`.
47
+
48
+ The toggle-menu component itself lives in `menu.ts` and is reusable by other extensions if you want a similar settings UI.
49
+
50
+ ## Install
51
+
52
+ ```bash
53
+ pi install npm:@underactive/pi-topping
54
+ ```
55
+
56
+ Restart Pi (or run `/reload`) to pick it up.
57
+
58
+ ## Requirements
59
+
60
+ - [Pi](https://github.com/earendil-works/pi-coding-agent)
61
+ - Node.js ≥22.19.0
62
+
63
+ ## Development
64
+
65
+ ```bash
66
+ npm install
67
+ npm test
68
+ npm run typecheck
69
+ ```
70
+
71
+ Tests cover the word/shimmer rendering, activity meter behavior, timer resets, settings persistence, the `menu.ts` component, and the settings menu wiring end to end.
72
+
73
+ ## Limitations
74
+
75
+ - Token counts are a streaming word-count estimate, not an exact tokenizer count — they self-correct when each message finishes, so you'll see a small jump at that point.
76
+ - Elapsed time normally starts when you submit a prompt; for auto-continuations without that event, it starts slightly later instead.
77
+ - If another extension also customizes the working message, whichever one runs last wins.
78
+
79
+ ## Author
80
+
81
+ Eric Sison
@@ -0,0 +1,102 @@
1
+ import type { ThemeColor } from "@earendil-works/pi-coding-agent";
2
+
3
+ export const ActivityMeterLevel = {
4
+ IDLE: 0,
5
+ LOW: 1,
6
+ MEDIUM: 2,
7
+ HIGH: 3,
8
+ PEAK_1: 4,
9
+ PEAK_2: 5,
10
+ PEAK_3: 6,
11
+ FULL: 7,
12
+ } as const;
13
+
14
+ export type ActivityMeterLevel = (typeof ActivityMeterLevel)[keyof typeof ActivityMeterLevel];
15
+
16
+ const EMA_ALPHA = 0.4;
17
+ const BRAILLE: Record<ActivityMeterLevel, string> = {
18
+ [ActivityMeterLevel.IDLE]: "⢀",
19
+ [ActivityMeterLevel.LOW]: "⣀",
20
+ [ActivityMeterLevel.MEDIUM]: "⣠",
21
+ [ActivityMeterLevel.HIGH]: "⣤",
22
+ [ActivityMeterLevel.PEAK_1]: "⣴",
23
+ [ActivityMeterLevel.PEAK_2]: "⣶",
24
+ [ActivityMeterLevel.PEAK_3]: "⣾",
25
+ [ActivityMeterLevel.FULL]: "⣿",
26
+ };
27
+ const WIDTH = 8;
28
+ type CellColorizer = (level: ActivityMeterLevel, char: string) => string;
29
+
30
+ /** Convert an estimated output-token rate to a display level. */
31
+ export function rateToLevel(tokensPerSecond: number): ActivityMeterLevel {
32
+ const THRESHOLDS = [0, 5, 10, 15, 22, 30, 40];
33
+ for (let i = THRESHOLDS.length - 1; i >= 0; i--) {
34
+ if (tokensPerSecond > THRESHOLDS[i]) return (i + 1) as ActivityMeterLevel;
35
+ }
36
+ return 0;
37
+ }
38
+
39
+ /** EMA-smoothed rate tracker for a cumulative output-token estimate. */
40
+ export class TokRateTracker {
41
+ #lastTotal = 0;
42
+ #lastTime = 0;
43
+ #rate = 0;
44
+ #hasSample = false;
45
+
46
+ sample(totalTokens: number, now: number): number {
47
+ if (!this.#hasSample) {
48
+ this.#lastTotal = totalTokens;
49
+ this.#hasSample = true;
50
+ this.#lastTime = now;
51
+ return this.#rate;
52
+ }
53
+
54
+ const elapsedSeconds = (now - this.#lastTime) / 1_000;
55
+ if (elapsedSeconds <= 0) return this.#rate;
56
+
57
+ const instantRate = Math.max(0, (totalTokens - this.#lastTotal) / elapsedSeconds);
58
+ this.#rate = EMA_ALPHA * instantRate + (1 - EMA_ALPHA) * this.#rate;
59
+ this.#lastTotal = totalTokens;
60
+ this.#lastTime = now;
61
+ return this.#rate;
62
+ }
63
+
64
+ reset(): void {
65
+ this.#lastTotal = 0;
66
+ this.#lastTime = 0;
67
+ this.#rate = 0;
68
+ this.#hasSample = false;
69
+ }
70
+ }
71
+
72
+ /** Eight-column, left-to-right scrolling activity meter. */
73
+ export class ActivityMeter {
74
+ #levels: ActivityMeterLevel[] = Array<ActivityMeterLevel>(WIDTH).fill(0);
75
+
76
+ push(level: ActivityMeterLevel): void {
77
+ this.#levels.pop();
78
+ this.#levels.unshift(level);
79
+ }
80
+
81
+ render(colorize?: CellColorizer): string {
82
+ return this.#levels
83
+ .map((level) => {
84
+ const char = BRAILLE[level];
85
+ return colorize ? colorize(level, char) : char;
86
+ })
87
+ .join("");
88
+ }
89
+
90
+ reset(): void {
91
+ this.#levels.fill(0);
92
+ }
93
+
94
+ /** Colorize a meter cell using the default theme mapping (dim at IDLE, accent otherwise). */
95
+ static colorizeCell(
96
+ level: ActivityMeterLevel,
97
+ char: string,
98
+ theme: { fg: (style: ThemeColor, s: string) => string },
99
+ ): string {
100
+ return level === ActivityMeterLevel.IDLE ? theme.fg("dim", char) : theme.fg("accent", char);
101
+ }
102
+ }
package/format.ts ADDED
@@ -0,0 +1,71 @@
1
+ /** Format an output-token count for the working indicator. */
2
+ export function formatTokens(count: number): string {
3
+ if (count < 1000) return count.toString();
4
+ const UNITS = [
5
+ { threshold: 10_000, divisor: 1_000, decimals: 1, suffix: "k" },
6
+ { threshold: 1_000_000, divisor: 1_000, decimals: 0, suffix: "k" },
7
+ { threshold: 10_000_000, divisor: 1_000_000, decimals: 1, suffix: "M" },
8
+ ];
9
+ const unit = UNITS.find(u => count < u.threshold);
10
+ if (unit) {
11
+ const value = unit.decimals > 0
12
+ ? (count / unit.divisor).toFixed(unit.decimals)
13
+ : `${Math.round(count / unit.divisor)}`;
14
+ return `${value}${unit.suffix}`;
15
+ }
16
+ return `${Math.round(count / 1_000_000)}M`;
17
+ }
18
+
19
+ /** Format elapsed milliseconds as minutes and zero-padded seconds. */
20
+ export function formatElapsed(ms: number): string {
21
+ const totalSeconds = Math.max(0, Math.floor(ms / 1000));
22
+ const minutes = Math.floor(totalSeconds / 60);
23
+ const seconds = totalSeconds % 60;
24
+ return `${minutes}m ${seconds.toString().padStart(2, "0")}s`;
25
+ }
26
+
27
+ /**
28
+ * Incremental whitespace-boundary word counter.
29
+ *
30
+ * The in-word state is retained across streaming chunks so a word split across
31
+ * multiple deltas is counted exactly once.
32
+ */
33
+ export class StreamingWordCounter {
34
+ #inWordByStream = new Map<string, boolean>();
35
+
36
+ count(text: string, stream = "default"): number {
37
+ let inWord = this.#inWordByStream.get(stream) ?? false;
38
+ let count = 0;
39
+ for (let i = 0; i < text.length; i++) {
40
+ const code = text.charCodeAt(i);
41
+ if (isWhitespace(code)) {
42
+ inWord = false;
43
+ } else if (!inWord) {
44
+ count++;
45
+ inWord = true;
46
+ }
47
+ }
48
+ this.#inWordByStream.set(stream, inWord);
49
+ return count;
50
+ }
51
+
52
+ reset(): void {
53
+ this.#inWordByStream.clear();
54
+ }
55
+ }
56
+
57
+ function isWhitespace(code: number): boolean {
58
+ return (
59
+ code === 32 ||
60
+ (code >= 9 && code <= 13) ||
61
+ code === 160 ||
62
+ code === 0x1680 ||
63
+ (code >= 0x2000 && code <= 0x200a) ||
64
+ code === 0x2028 ||
65
+ code === 0x2029 ||
66
+ code === 0x202f ||
67
+ code === 0x205f ||
68
+ code === 0x3000 ||
69
+ code === 0xfeff
70
+ );
71
+ }