@bismawy/pi-arnative 0.16.1 → 0.18.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.
Files changed (61) hide show
  1. package/README.md +25 -16
  2. package/extensions/index.ts +41 -0
  3. package/lib/active-clock.ts +91 -0
  4. package/lib/ansi.ts +40 -78
  5. package/lib/box.ts +49 -68
  6. package/lib/chatbox.ts +371 -0
  7. package/lib/color.ts +3 -31
  8. package/lib/compat.ts +0 -60
  9. package/lib/edit-diff.ts +199 -0
  10. package/{extensions → lib/features}/arnative.ts +96 -423
  11. package/{extensions/autocompact.ts → lib/features/auto-compact.ts} +30 -327
  12. package/{extensions → lib/features}/budget.ts +17 -242
  13. package/lib/features/footer.ts +2232 -0
  14. package/{extensions/section-headers.ts → lib/features/headers.ts} +38 -49
  15. package/{extensions → lib/features}/model-heal.ts +5 -28
  16. package/lib/features/progress-group.ts +1557 -0
  17. package/{extensions → lib/features}/render-cache.ts +113 -161
  18. package/{extensions → lib/features}/timestamps.ts +18 -94
  19. package/{extensions → lib/features}/todos.ts +44 -158
  20. package/lib/features/tools.ts +1202 -0
  21. package/lib/features/ui-render-tweaks.ts +606 -0
  22. package/lib/features/usage.ts +786 -0
  23. package/lib/format.ts +22 -4
  24. package/lib/git-status.ts +164 -0
  25. package/lib/list-picker.ts +1 -127
  26. package/lib/model-heal.ts +70 -162
  27. package/lib/patch.ts +0 -63
  28. package/lib/preset-store.ts +17 -133
  29. package/lib/provider-store.ts +573 -0
  30. package/lib/provider.ts +52 -851
  31. package/lib/runtime.ts +209 -0
  32. package/lib/shortcuts.ts +8 -58
  33. package/lib/spinner.ts +88 -162
  34. package/lib/status-widget.ts +4 -30
  35. package/lib/todos.ts +20 -143
  36. package/lib/usage-pricing.ts +143 -0
  37. package/lib/usage-store.ts +44 -378
  38. package/package.json +5 -6
  39. package/themes/arnative-blossom.json +0 -0
  40. package/themes/arnative-crimson.json +0 -0
  41. package/themes/arnative-cyberpunk.json +0 -0
  42. package/themes/arnative-dracula.json +0 -0
  43. package/themes/arnative-earthy.json +0 -0
  44. package/themes/arnative-garden.json +0 -0
  45. package/themes/arnative-gruvbox.json +0 -0
  46. package/themes/arnative-lemonade.json +0 -0
  47. package/themes/arnative-magical.json +0 -0
  48. package/themes/arnative-matrix.json +0 -0
  49. package/themes/arnative-neutral.json +0 -0
  50. package/themes/arnative-nord.json +0 -0
  51. package/themes/arnative-zinc.json +0 -0
  52. package/themes/arnative.json +0 -0
  53. package/CHANGELOG.md +0 -683
  54. package/extensions/animations.ts +0 -242
  55. package/extensions/footer.ts +0 -2617
  56. package/extensions/tools.ts +0 -2231
  57. package/extensions/ui-render-tweaks.ts +0 -1005
  58. package/extensions/usage-export.ts +0 -283
  59. package/extensions/usage.ts +0 -1406
  60. package/lib/check.ts +0 -21
  61. package/themes/gen-themes.mjs +0 -715
@@ -1,242 +0,0 @@
1
- /**
2
- * Animations — the working-indicator preset, owned outside the footer because it is not a
3
- * footer concern: pi's loader spins in the boxed editor's top border and in the in-place
4
- * status row whether or not an arnative footer is installed.
5
- *
6
- * `/arnative` → **Animations** picks a preset; this module installs it, repaints the live
7
- * spinner on the spot, and hands the slot back to pi for `Pi (system)`. The frames, their
8
- * cadence and their painter live in `lib/spinner.ts`; the picker UI lives here.
9
- *
10
- * The Loader's colour hooks are not reachable through pi's public indicator options, so this
11
- * module reaches the live editor the footer publishes under `EDITOR_KEY`. That is the one
12
- * coupling between the two, and it is a published symbol, not an import.
13
- */
14
- import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
15
- import { choiceSetting, flagSetting } from "../lib/preset-store.ts";
16
- import {
17
- DEFAULT_SPINNER_PRESET,
18
- SPINNER_GRADIENT_KEY,
19
- SPINNER_PRESETS,
20
- SPINNER_PRESET_DEFS,
21
- SPINNER_PRESET_KEY,
22
- previewFrameAt,
23
- previewFrames,
24
- spinnerPainter,
25
- type SpinnerPreset,
26
- } from "../lib/spinner.ts";
27
- import { assert, isMain } from "../lib/check.ts";
28
- import { ROW_DIVIDER } from "../lib/format.ts";
29
-
30
- /** The arnative editor instance, published by `footer.ts` so the live spinner is reachable. */
31
- export const EDITOR_KEY = Symbol.for("pi-arnative.editor");
32
- const spinnerPresetSetting = choiceSetting(SPINNER_PRESET_KEY, SPINNER_PRESETS, DEFAULT_SPINNER_PRESET);
33
- const spinnerGradientSetting = flagSetting(SPINNER_GRADIENT_KEY);
34
- const SPINNER_PRESET_CONFIG_KEY = spinnerPresetSetting.slot;
35
- const SPINNER_GRADIENT_CONFIG_KEY = spinnerGradientSetting.slot;
36
-
37
- /** The `setWorkingMessage`/`setWorkingIndicator` surface of `ctx.ui`. */
38
- type WorkingUi = { setWorkingIndicator?: (options?: unknown) => void } | null | undefined;
39
-
40
- /** The live theme, for the accent presets. A gradient preset ignores it. */
41
- type ThemeLike = { fg?(color: string, text: string): string } | null;
42
-
43
- // Active UI and theme are kept on globalThis so choices apply across reloads and multi-instance loads.
44
- const UI_KEY = Symbol.for("pi-arnative.workingUi");
45
- const THEME_KEY = Symbol.for("pi-arnative.workingTheme");
46
-
47
- /** Record the session's `ctx.ui`, so a later preset pick can install through it. */
48
- function workingUi(ui: WorkingUi): void {
49
- (globalThis as Record<symbol, unknown>)[UI_KEY] = ui ?? undefined;
50
- }
51
-
52
- /** The `ctx.ui` recorded at `session_start`, or null before one ran. */
53
- function activeWorkingUi(): WorkingUi {
54
- return ((globalThis as Record<symbol, unknown>)[UI_KEY] as WorkingUi) ?? null;
55
- }
56
-
57
- /** Record the session's theme, so accent presets follow it instead of a stale colour. */
58
- function workingTheme(theme: ThemeLike): void {
59
- (globalThis as Record<symbol, unknown>)[THEME_KEY] = theme ?? undefined;
60
- }
61
-
62
- /** The theme recorded at `session_start`; a gradient preset ignores it. */
63
- function activeWorkingTheme(): ThemeLike {
64
- return ((globalThis as Record<symbol, unknown>)[THEME_KEY] as ThemeLike) ?? null;
65
- }
66
-
67
- export const activeSpinnerPreset = (): SpinnerPreset => spinnerPresetSetting.get();
68
-
69
- /** Is the `accent` → gradient override on? */
70
- export const activeSpinnerGradient = (): boolean => spinnerGradientSetting.get();
71
-
72
- /** Flip the gradient override, persist it, and repaint the live spinner at once. */
73
- export function setSpinnerGradient(on: boolean): void {
74
- spinnerGradientSetting.set(on);
75
- applySpinnerPreset(activeWorkingUi(), activeWorkingTheme());
76
- }
77
-
78
- /** Publish the choice for this process, write it to config, and install it on the running session. */
79
- export function setSpinnerPreset(preset: SpinnerPreset): void {
80
- spinnerPresetSetting.set(preset);
81
- applySpinnerPreset(activeWorkingUi(), activeWorkingTheme());
82
- }
83
-
84
- /** Updates custom painter on live workingStatusIndicator if available. */
85
- export function paintLiveIndicator(painter: (text: string) => string): void {
86
- const editor = (globalThis as Record<symbol, any>)[EDITOR_KEY];
87
- const indicator = editor?.workingStatusIndicator;
88
- if (!indicator) return;
89
- indicator.renderIndicatorVerbatim = false;
90
- indicator.spinnerColorFn = painter;
91
- indicator.updateDisplay?.();
92
- }
93
-
94
- /** Applies preset to working indicator or restores system default for 'Pi (system)'. */
95
- export function applySpinnerPreset(ui: WorkingUi, theme: ThemeLike): void {
96
- const preset = activeSpinnerPreset();
97
- const def = SPINNER_PRESET_DEFS[preset];
98
- const painter = spinnerPainter(preset, theme, activeSpinnerGradient());
99
- try {
100
- if (def.frames.length === 0) ui?.setWorkingIndicator?.();
101
- else ui?.setWorkingIndicator?.({ frames: [...def.frames], intervalMs: def.intervalMs });
102
- } catch {
103
- // older host without the API -> pi's own frames stay
104
- }
105
- paintLiveIndicator(painter);
106
- }
107
-
108
- /** Picker copy. The description states the one seam: a preset applies to the next spinner.
109
- * The legend is built per call because its `[g]` row states the current mode — a constant
110
- * would keep saying `off` after the user turned it on. */
111
- export function animationsPickerText(): { title: string; description: string; legend: string } {
112
- return {
113
- title: "Animations - Arnative\u2819",
114
- description: "The working spinner — applies on the next tool call.",
115
- legend: `[↑↓] Preview [g] Gradient ${activeSpinnerGradient() ? "on" : "off"} [Enter] Select [Esc] Back`,
116
- };
117
- }
118
-
119
- /** One row per preset: the name, then its note and the frames it actually cycles. The
120
- * frames belong in the note — a user picking an animation wants to see the glyphs, and this
121
- * is the screen where that is decided. */
122
- export function animationRows(): Array<{ label: string; note: string }> {
123
- return SPINNER_PRESETS.map((preset) => ({
124
- label: preset,
125
- note: `${SPINNER_PRESET_DEFS[preset].note} ${previewFrames(preset)}`,
126
- }));
127
- }
128
-
129
- /** The row as one flat string, for the headless fallback and the self-check. */
130
- export function animationRowText(preset: SpinnerPreset): string {
131
- return `${preset}${ROW_DIVIDER}${SPINNER_PRESET_DEFS[preset].note} ${previewFrames(preset)}`;
132
- }
133
-
134
- /** Repaint cadence for the picker's animated row. Fast enough to read as motion at every
135
- * preset's own interval (the slowest is 200ms, the fastest 80ms), cheap because the row only
136
- * redraws one line. */
137
- export const PREVIEW_TICK_MS = 80;
138
-
139
- /** Note for one row with the frame that is live *now*, padded to the widest frame so the
140
- * text after it does not shift as the glyph changes. */
141
- export function animationRowNote(preset: SpinnerPreset, now = Date.now()): string {
142
- const { text, width } = previewFrameAt(preset, now);
143
- const pad = " ".repeat(Math.max(0, width - [...text].length));
144
- return `${SPINNER_PRESET_DEFS[preset].note} ${text}${pad}`;
145
- }
146
-
147
- /** Register the extension: keep the installed preset in step with the session's theme. */
148
- export default function registerAnimations(pi: ExtensionAPI): void {
149
- pi.on("session_start", async (_event, ctx: ExtensionContext) => {
150
- // A preset is installed per session so it survives `/reload` and a theme switch
151
- // repaints the accent presets without re-selecting anything. Publish the surfaces first:
152
- // `/arnative` → Animations re-applies through them the moment a row is chosen.
153
- workingUi(ctx.ui);
154
- workingTheme((ctx.ui as unknown as { theme?: ThemeLike })?.theme ?? null);
155
- applySpinnerPreset(activeWorkingUi(), activeWorkingTheme());
156
- });
157
- }
158
-
159
- // Self-check: `node extensions/animations.ts`. Async because the cross-instance assertion
160
- // imports a second copy of this module.
161
- if (isMain(import.meta.url)) {
162
- await (async () => {
163
- // The rows describe exactly the presets, and every row carries its own note.
164
- const rows = animationRows();
165
- assert(rows.length === SPINNER_PRESETS.length, "one row per preset");
166
- assert(
167
- rows.every((r) => r.note.length > 0),
168
- "every row has a note",
169
- );
170
- assert(animationRowText("Diamond Glow").includes("\u2b25"), "the row text previews the frames");
171
-
172
- // The picker's animated note shows exactly one live frame, padded to the widest one so the
173
- // row does not shift. `animationRowNote` takes `now`, so the assertion does not race a clock.
174
- assert(animationRowNote("Diamond Glow", 0).endsWith("\u2b25"), "the live note shows frame 0");
175
- assert(animationRowNote("Diamond Glow", 200).endsWith("\u25c8"), "the live note advances with the clock");
176
- assert(animationRowNote("Pacman", 0).endsWith("\u15e7 + \u2212 \u00d7"), "a wide frame is shown whole");
177
- assert(
178
- animationRowNote("Pacman", 0).length === animationRowNote("Pacman", 600).length,
179
- "every frame pads to the same width, so the note never shifts",
180
- );
181
- assert(PREVIEW_TICK_MS <= 80, "the preview ticks at least as fast as the quickest preset");
182
-
183
- // Installing a preset goes through pi's indicator API; `Pi (system)` passes nothing.
184
- const calls: Array<Record<string, unknown> | undefined> = [];
185
- const ui = { setWorkingIndicator: (options?: Record<string, unknown>) => calls.push(options) };
186
- (globalThis as Record<symbol, unknown>)[SPINNER_PRESET_CONFIG_KEY] = "Diamond Glow";
187
- applySpinnerPreset(ui, null);
188
- assert(JSON.stringify(calls[0]?.frames) === JSON.stringify(SPINNER_PRESET_DEFS["Diamond Glow"].frames), "frames installed");
189
- assert(calls[0]?.intervalMs === 200, "interval installed");
190
- (globalThis as Record<symbol, unknown>)[SPINNER_PRESET_CONFIG_KEY] = "Pi (system)";
191
- applySpinnerPreset(ui, null);
192
- assert(calls.at(-1) === undefined, "Pi (system) passes no options -> pi restores its own spinner");
193
-
194
- // A stored value that names no preset falls back instead of breaking the load.
195
- (globalThis as Record<symbol, unknown>)[SPINNER_PRESET_CONFIG_KEY] = "Nope";
196
- assert(activeSpinnerPreset() === DEFAULT_SPINNER_PRESET, "an unknown stored preset falls back to the default");
197
- delete (globalThis as Record<symbol, unknown>)[SPINNER_PRESET_CONFIG_KEY];
198
-
199
- // The painter seam: without a published editor there is nothing to paint, and that must
200
- // be a no-op rather than a throw (the editor registers after the first session_start).
201
- delete (globalThis as Record<symbol, unknown>)[EDITOR_KEY];
202
- paintLiveIndicator((t) => t);
203
-
204
- // Picking a preset must apply it NOW, not on the next session — and it must work when the
205
- // picker calls a DIFFERENT module instance than the one that ran `session_start`. pi loads
206
- // every manifest entry through its own loader while `arnative.ts` imports this module too,
207
- // so there are two copies with separate module scope; only `globalThis` is shared. Keeping
208
- // the surfaces on globalThis is what this pins, and a same-instance test would not catch it
209
- // (the original bug passed such a test: the row moved, the spinner did not).
210
- const pickCalls: Array<Record<string, unknown> | undefined> = [];
211
- const pickUi = { setWorkingIndicator: (options?: Record<string, unknown>) => pickCalls.push(options) };
212
- const handlers: Array<(e: unknown, c: unknown) => unknown> = [];
213
- registerAnimations({ on: (_n: string, fn: (e: unknown, c: unknown) => unknown) => handlers.push(fn) } as never);
214
- // Instance A runs session_start and records the ui on globalThis.
215
- handlers[0]!({}, { ui: pickUi, theme: { fg: (_c: string, t: string) => t } });
216
- pickCalls.length = 0;
217
- // Instance B — a second copy of the module, like the one `arnative.ts` holds — does the pick.
218
- const instanceB = await import(`./animations.ts?second=${Date.now()}`);
219
- instanceB.setSpinnerPreset("Claudeon");
220
- assert(pickCalls.length === 1, "a second module instance installs through the shared ui");
221
- assert(pickCalls[0]?.frames?.[0] === "\u00b7", "the chosen preset's frames are installed, not the old ones");
222
- assert(pickCalls[0]?.intervalMs === 120, "and its cadence (120ms)");
223
-
224
- // Pi (system) still hands the slot back through the same path.
225
- pickCalls.length = 0;
226
- instanceB.setSpinnerPreset("Pi (system)");
227
- assert(pickCalls[0] === undefined, "choosing Pi (system) restores pi's own spinner at once");
228
-
229
- // With one, both the verbatim flag and the painter are set — this is what keeps the
230
- // colour on a custom frame.
231
- const indicator = { renderIndicatorVerbatim: true, spinnerColorFn: undefined as unknown, updateDisplay: () => {} };
232
- let painted = 0;
233
- const editor = { workingStatusIndicator: { ...indicator, updateDisplay: () => painted++ } };
234
- (globalThis as Record<symbol, any>)[EDITOR_KEY] = editor;
235
- paintLiveIndicator((t) => `<a>${t}`);
236
- assert(editor.workingStatusIndicator.renderIndicatorVerbatim === false, "verbatim cleared, else pi skips the painter");
237
- assert(painted === 1, "the live indicator repaints at once");
238
- delete (globalThis as Record<symbol, any>)[EDITOR_KEY];
239
-
240
- console.log("animations.ts self-check OK");
241
- })();
242
- }