pi-mega-compact 0.8.23 → 0.8.24

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 (55) hide show
  1. package/dist/extensions/dashboard-server/api-contracts/endpoints.js +8 -0
  2. package/dist/extensions/dashboard-server/api-contracts/game-types.js +7 -0
  3. package/dist/extensions/mega-runtime/append-event.js +24 -0
  4. package/dist/extensions/mega-runtime/bind-repo.js +65 -0
  5. package/dist/extensions/mega-runtime/capture-model.js +87 -0
  6. package/dist/extensions/mega-runtime/dashboard-snapshot.js +118 -0
  7. package/dist/extensions/mega-runtime/effects.js +86 -0
  8. package/dist/extensions/mega-runtime/engine-view.js +11 -0
  9. package/dist/extensions/mega-runtime/game-state.js +116 -0
  10. package/dist/extensions/mega-runtime/get-state-dir.js +10 -0
  11. package/dist/extensions/mega-runtime/perf.js +49 -0
  12. package/dist/extensions/mega-runtime/pressure-getters.js +64 -0
  13. package/dist/extensions/mega-runtime/render-widget.js +17 -0
  14. package/dist/extensions/mega-runtime/reset-runtime.js +50 -0
  15. package/dist/extensions/mega-runtime/runtime-helpers.js +73 -0
  16. package/dist/extensions/mega-runtime/runtime-snapshot.js +204 -0
  17. package/dist/extensions/mega-runtime/runtime.js +352 -0
  18. package/dist/extensions/mega-runtime/snapshot.js +142 -0
  19. package/dist/extensions/mega-runtime/state.js +5 -1151
  20. package/dist/extensions/mega-runtime/status.js +11 -0
  21. package/dist/extensions/mega-runtime/widget-ansi.js +207 -0
  22. package/dist/extensions/mega-runtime/widget-types.js +8 -0
  23. package/dist/extensions/mega-runtime/widget.js +15 -204
  24. package/dist/extensions/openclaw-mega-compact.js +291 -0
  25. package/dist/src/minilm.js +92 -0
  26. package/dist/src/wordpiece.js +129 -0
  27. package/extensions/dashboard-client/dist/assets/index-D_WtU2TV.js.map +1 -1
  28. package/extensions/dashboard-server/api-contracts/endpoints.ts +30 -155
  29. package/extensions/dashboard-server/api-contracts/game-types.ts +172 -0
  30. package/extensions/mega-runtime/DECOMPOSITION.md +180 -0
  31. package/extensions/mega-runtime/README.md +38 -0
  32. package/extensions/mega-runtime/append-event.ts +40 -0
  33. package/extensions/mega-runtime/bind-repo.ts +81 -0
  34. package/extensions/mega-runtime/capture-model.ts +101 -0
  35. package/extensions/mega-runtime/dashboard-snapshot.ts +173 -0
  36. package/extensions/mega-runtime/effects.ts +129 -0
  37. package/extensions/mega-runtime/engine-view.ts +17 -0
  38. package/extensions/mega-runtime/game-state.ts +149 -0
  39. package/extensions/mega-runtime/get-state-dir.ts +19 -0
  40. package/extensions/mega-runtime/perf.ts +60 -0
  41. package/extensions/mega-runtime/pressure-getters.ts +96 -0
  42. package/extensions/mega-runtime/render-widget.ts +41 -0
  43. package/extensions/mega-runtime/reset-runtime.ts +80 -0
  44. package/extensions/mega-runtime/runtime-helpers.ts +119 -0
  45. package/extensions/mega-runtime/runtime-snapshot.ts +289 -0
  46. package/extensions/mega-runtime/runtime.ts +437 -0
  47. package/extensions/mega-runtime/snapshot.ts +230 -0
  48. package/extensions/mega-runtime/state.ts +5 -1268
  49. package/extensions/mega-runtime/status.ts +26 -0
  50. package/extensions/mega-runtime/widget-ansi.ts +217 -0
  51. package/extensions/mega-runtime/widget-types.ts +80 -0
  52. package/extensions/mega-runtime/widget.ts +34 -285
  53. package/package.json +1 -1
  54. package/dist/extensions/dashboard-client/src/hooks/useApi.js +0 -51
  55. package/dist/extensions/dashboard-client/src/hooks/useSSE.js +0 -63
@@ -0,0 +1,11 @@
1
+ /**
2
+ * status.ts — extracted `MegaRuntime.setStatus()`: the runtime status-key text
3
+ * mirrored to pi's status line and the dashboard. Same thin-delegate pattern
4
+ * as the other runtime.ts extractions.
5
+ */
6
+ import { STATUS_KEY } from "./helpers.js";
7
+ // ------------------------------------------------------------------ setStatus
8
+ export function setStatusImpl(self, ctx, text) {
9
+ self.statusKey = text;
10
+ ctx.ui.setStatus(STATUS_KEY, text);
11
+ }
@@ -0,0 +1,207 @@
1
+ /**
2
+ * widget-ansi.ts — the ANSI palette, theme resolution, panel layout helpers,
3
+ * and ambient border-effect helpers extracted from the original widget.ts.
4
+ *
5
+ * Pure rendering primitives with zero runtime-state dependencies. The render
6
+ * function (widget.ts) and the snapshot computation (snapshot.ts) import these;
7
+ * `C` is also re-exported via the widget barrel for mega-pipeline/mega-commands.
8
+ */
9
+ // pi-tui's OWN width measurers — the same functions pi-tui uses to enforce its
10
+ // render-width check ("visibleWidth(line) > width" → crash). Measuring with
11
+ // these guarantees we never disagree on a grapheme's width (e.g. RGI emoji
12
+ // like ⚡ which pi-tui counts as 2 but a naive regex counts as 1), and
13
+ // truncateToWidth both pads AND hard-clips to exactly `width` cells, so no
14
+ // off-by-one can trip the strict `> width` guard. (Fix for the
15
+ // `Rendered line N exceeds terminal width (W > W-1)` crash.)
16
+ import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
17
+ import { getTheme, DEFAULT_THEME } from "../../src/config/themes.js";
18
+ // ── ANSI palette ───────────────────────────────────────────────────────────
19
+ // The pi TUI's Text component preserves ANSI escape codes (see wrapTextWithAnsi),
20
+ // so raw escapes render as colors. No chalk dependency needed — these are just
21
+ // strings. Exported because mega-pipeline.ts and mega-commands.ts import `C`
22
+ // from the mega-runtime barrel.
23
+ export const C = {
24
+ reset: "\x1b[0m",
25
+ dim: "\x1b[2m",
26
+ bold: "\x1b[1m",
27
+ amber: "\x1b[38;5;214m", // tier / ready
28
+ green: "\x1b[38;5;120m", // saved
29
+ cyan: "\x1b[38;5;51m", // used / live activity
30
+ teal: "\x1b[38;5;37m", // processing (compress/dedup)
31
+ magenta: "\x1b[38;5;201m", // dedup rate
32
+ blue: "\x1b[38;5;75m", // repo totals
33
+ gray: "\x1b[38;5;245m", // labels
34
+ red: "\x1b[38;5;203m", // pressure / overflow
35
+ };
36
+ export const PULSE = ["◐", "◓", "◑", "◒"];
37
+ // ── Full-width widget panel helpers ────────────────────────────────────────
38
+ // pi's above-editor widget renderer (a Container of Text lines) does NOT pass
39
+ // a terminal width to setWidget(), so lines render left-aligned by default. To
40
+ // make the widget read as a full-width status panel we pad each line to the
41
+ // real terminal width with a background fill. NOTE: C.reset is a FULL SGR
42
+ // reset, so we re-apply the panel bg after every reset to keep the background
43
+ // continuous under colored text (and under pi's own trailing reset).
44
+ /** Default panel background (dark slate). Used when no theme is threaded or
45
+ * the theme is transparent (no bg fill). Parametrized per-render via the
46
+ * `panelTheme` arg so game-mode themes can restyle the panel background.
47
+ * A transparent theme yields `""` so panelLine/panelBar still call
48
+ * truncateToWidth (the width guard holds — empty prefix adds zero cells). */
49
+ export const DEFAULT_PANEL_BG = "\x1b[48;5;236m"; // dark slate panel background
50
+ /** Resolve the panel-background SGR prefix for a theme id. Transparent themes
51
+ * (bg=null) and unknown themes yield `""` (no bg fill) — the width guard in
52
+ * panelLine/panelBar still applies via truncateToWidth. */
53
+ export function panelBgFor(theme) {
54
+ if (!theme || theme === DEFAULT_THEME)
55
+ return "";
56
+ const t = getTheme(theme);
57
+ const bg = t?.ansi.bg;
58
+ return bg ? `\x1b[${bg}m` : "";
59
+ }
60
+ /** Resolve a theme ANSI accent/mega/fg SGR prefix (`\x1b[<params>m`) for the
61
+ * given role. Falls back to `""` (no SGR) for transparent/unknown themes so
62
+ * game-mode text still renders in the default fg without color noise. */
63
+ export function themeAnsi(theme, role) {
64
+ const t = theme ? getTheme(theme) : undefined;
65
+ const params = t?.ansi[role];
66
+ return params ? `\x1b[${params}m` : "";
67
+ }
68
+ /** Emit a full SGR reset, OR `""` if `sgr` is empty (transparent theme). Keeps
69
+ * the panel bg continuous by NOT clobbering it with a bare reset when the
70
+ * accent/mega prefix was a no-op. */
71
+ export function sgrReset(sgr) {
72
+ return sgr ? "\x1b[0m" : "";
73
+ }
74
+ // (Visible-width measurement is delegated to pi-tui's `visibleWidth` — imported
75
+ // above — so our width math can never diverge from pi-tui's render-width check.)
76
+ /** Wrap a string (with ANSI codes) to fit within `maxWidth` visible chars.
77
+ * Splits at │ separators or whitespace when possible. */
78
+ export function wrapLine(text, maxWidth, panelBg) {
79
+ if (maxWidth <= 0)
80
+ return [text];
81
+ const panelRst = "\x1b[0m" + panelBg;
82
+ const result = [];
83
+ let current = "";
84
+ let currentW = 0;
85
+ // Split at │ boundaries first
86
+ const segments = text.split("│");
87
+ for (let i = 0; i < segments.length; i++) {
88
+ const seg = (i > 0 ? "│" : "") + segments[i];
89
+ const segW = visibleWidth(panelBg + seg.replace(/\x1b\[0m/g, panelRst));
90
+ if (currentW + segW <= maxWidth || currentW === 0) {
91
+ current += seg;
92
+ currentW += segW;
93
+ }
94
+ else {
95
+ result.push(current);
96
+ current = seg;
97
+ currentW = segW;
98
+ }
99
+ }
100
+ if (current)
101
+ result.push(current);
102
+ return result;
103
+ }
104
+ export function panelLine(content, width, panelBg = DEFAULT_PANEL_BG) {
105
+ if (width <= 0)
106
+ return "";
107
+ const panelRst = "\x1b[0m" + panelBg;
108
+ // Apply the panel background; swap every inner full-reset for a reset that
109
+ // re-applies the bg so the fill stays continuous under colored text.
110
+ const withBg = panelBg + content.replace(/\x1b\[0m/g, panelRst);
111
+ // truncateToWidth(line, width, "", true) returns EXACTLY `width` visible cells
112
+ // (by pi-tui's measure), ANSI-preserved, space-padded. It hard-clips overflow
113
+ // — so even a segment wider than `width` (or a width-rule mismatch) can never
114
+ // produce a line that trips pi-tui's strict `visibleWidth(line) > width` check.
115
+ return truncateToWidth(withBg, width, "", true) + "\x1b[0m";
116
+ }
117
+ /** A full-width hairline bar (top/bottom border of the panel). */
118
+ export function panelBar(width, ch = "─", panelBg = DEFAULT_PANEL_BG) {
119
+ // `─` (U+2500) is narrow (1 cell) in both our measure and pi-tui's, so a
120
+ // `ch.repeat(width)` bar is exactly `width` cells and already passes the
121
+ // `> width` guard. truncateToWidth is belt-and-suspenders in case `ch` is
122
+ // ever swapped for a wide/fullwidth character.
123
+ return truncateToWidth(panelBg + ch.repeat(Math.max(0, width)), width, "", false) + "\x1b[0m";
124
+ }
125
+ // ── v0.8.3: ambient border-effect helpers ───────────────────────────────
126
+ // The panel borders animate when an `activeEffect` is armed (level-up,
127
+ // mega-cache overshoot, achievement unlock, compaction start). Two modes:
128
+ // • pulse — a sine ramp on a 256-color base (accent=51 / mega=214 / red=203):
129
+ // the base index is scaled by sin(π·t) so the border swells 0→peak→0 over
130
+ // the duration, then returns to '' (idle). 256-color indices are clamped
131
+ // to 0–255 defensively (the bases are all ≤214 so the clamp rarely bites).
132
+ // • flash — a 120ms hard on/off alternate using the base index at full.
133
+ // Returns '' when idle, expired, or elapsed<0 (clock skew) so non-effect
134
+ // renders are byte-identical to the pre-effect panel (S31 matrix stays green).
135
+ export const EFFECT_BASE = {
136
+ accent: 51,
137
+ mega: 214,
138
+ red: 203,
139
+ };
140
+ /** Resolve the per-frame border-fg SGR for an active effect. '' when idle or
141
+ * expired (the widget's real per-frame expiry enforcer — snapshot-level clear
142
+ * is just bookkeeping since snapshot is event-driven). */
143
+ export function effectBorderSgr(ae, now) {
144
+ if (!ae)
145
+ return "";
146
+ const elapsed = now - ae.startedAt;
147
+ if (elapsed < 0 || elapsed >= ae.durationMs)
148
+ return "";
149
+ const base = EFFECT_BASE[ae.role];
150
+ if (ae.type === "flash") {
151
+ // 120ms hard alternate: on (full base) / off (no SGR).
152
+ return Math.floor(elapsed / 120) % 2 === 0 ? `\x1b[38;5;${base}m` : "";
153
+ }
154
+ // pulse: sine ramp 0 → peak → 0 over the duration.
155
+ const t = elapsed / ae.durationMs;
156
+ const amp = Math.sin(Math.PI * t); // 0 at start/end, 1 at midpoint
157
+ const idx = Math.max(0, Math.min(255, Math.round(base * amp)));
158
+ return `\x1b[38;5;${idx}m`;
159
+ }
160
+ /** Prepend the effect border SGR to a panel bar line. The SGR is a pure-fg
161
+ * escape (zero visible width), so it never perturbs truncateToWidth's width
162
+ * math — the bar's own `\x1b[0m` tail resets both fg + bg. No-op when sgr=''. */
163
+ export function effectBar(bar, sgr) {
164
+ return sgr ? sgr + bar : bar;
165
+ }
166
+ /** Token-count formatter: M at/above 1e6, k at/above 1e3, raw below.
167
+ * 5,472,700 → "5.5mil", 24,100 → "24.1k", 142 → "142". */
168
+ export function fmtTokens(x) {
169
+ return x >= 1_000_000
170
+ ? `${(x / 1_000_000).toFixed(1)}mil`
171
+ : x >= 1000
172
+ ? `${(x / 1000).toFixed(1)}k`
173
+ : `${Math.round(x)}`;
174
+ }
175
+ /** Retro gradient bar — `w` cells shaded by fill position (green→amber→red).
176
+ * Used for CONTEXT fill where low=green (room) and high=red (near the limit). */
177
+ export function ramp(pct, w = 12) {
178
+ const cells = ["▏", "▎", "▍", "▌", "▋", "▊", "▉", "█"];
179
+ const scaled = Math.max(0, Math.min(w, pct * w));
180
+ const full = Math.floor(scaled);
181
+ const frac = scaled - full;
182
+ const fracCell = frac > 0 ? cells[Math.round(frac * (cells.length - 1))] : "";
183
+ let out = "";
184
+ for (let i = 0; i < full; i++)
185
+ out += (i / w < 0.6 ? C.green : i / w < 0.85 ? C.amber : C.red) + "█";
186
+ if (fracCell)
187
+ out +=
188
+ (full / w < 0.6 ? C.green : full / w < 0.85 ? C.amber : C.red) + fracCell;
189
+ out +=
190
+ C.dim + "░".repeat(Math.max(0, w - full - (fracCell ? 1 : 0))) + C.reset;
191
+ return out;
192
+ }
193
+ /** Human "time since" string from a millisecond delta (or null → "never"). */
194
+ export function sinceCompactStr(ms) {
195
+ if (ms == null)
196
+ return "never";
197
+ const s = Math.floor(ms / 1000);
198
+ if (s < 60)
199
+ return `${s}s ago`;
200
+ const m = Math.floor(s / 60);
201
+ if (m < 60)
202
+ return `${m}m ago`;
203
+ const h = Math.floor(m / 60);
204
+ if (h < 24)
205
+ return `${h}h ago`;
206
+ return `${Math.floor(h / 24)}d ago`;
207
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * widget-types.ts — the `TickerEntry` and `WidgetData` interfaces extracted from
3
+ * the original widget.ts so the ANSI helpers (widget-ansi.ts) and the render
4
+ * function (widget.ts) can import them without a cycle.
5
+ *
6
+ * Pure type definitions — zero runtime code, zero imports.
7
+ */
8
+ export {};
@@ -1,211 +1,22 @@
1
1
  /**
2
- * widget.ts — above-editor widget rendering helpers (free functions, consts,
3
- * and interfaces) extracted from the original mega-runtime.ts monolith.
2
+ * widget.ts — above-editor widget rendering. Now a thin module that owns the
3
+ * `buildWidgetLines` render function and re-exports the ANSI helpers
4
+ * (widget-ansi.ts) + the interfaces (widget-types.ts) so every existing
5
+ * `import { C, TickerEntry, WidgetData, buildWidgetLines } from "./widget.js"`
6
+ * continues to resolve with no consumer changes.
4
7
  *
5
- * These are pure rendering primitives with zero runtime-state dependencies;
6
- * the `MegaRuntime` class (state.ts) imports them to paint the panel.
8
+ * Originally a single ~422-line file; split for the Phase 2 decomposition
9
+ * (see DECOMPOSITION.md). Pure rendering primitives zero runtime-state
10
+ * dependencies; the `MegaRuntime` class (runtime.ts) imports them to paint the
11
+ * panel.
7
12
  */
8
- // pi-tui's OWN width measurers the same functions pi-tui uses to enforce its
9
- // render-width check ("visibleWidth(line) > width" → crash). Measuring with
10
- // these guarantees we never disagree on a grapheme's width (e.g. RGI emoji
11
- // like which pi-tui counts as 2 but a naive regex counts as 1), and
12
- // truncateToWidth both pads AND hard-clips to exactly `width` cells, so no
13
- // off-by-one can trip the strict `> width` guard. (Fix for the
14
- // `Rendered line N exceeds terminal width (W > W-1)` crash.)
15
- import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
16
- import { getTheme, DEFAULT_THEME } from "../../src/config/themes.js";
17
- // ── ANSI palette ───────────────────────────────────────────────────────────
18
- // The pi TUI's Text component preserves ANSI escape codes (see wrapTextWithAnsi),
19
- // so raw escapes render as colors. No chalk dependency needed — these are just
20
- // strings. Exported because mega-pipeline.ts and mega-commands.ts import `C`
21
- // from the mega-runtime barrel.
22
- export const C = {
23
- reset: "\x1b[0m",
24
- dim: "\x1b[2m",
25
- bold: "\x1b[1m",
26
- amber: "\x1b[38;5;214m", // tier / ready
27
- green: "\x1b[38;5;120m", // saved
28
- cyan: "\x1b[38;5;51m", // used / live activity
29
- teal: "\x1b[38;5;37m", // processing (compress/dedup)
30
- magenta: "\x1b[38;5;201m", // dedup rate
31
- blue: "\x1b[38;5;75m", // repo totals
32
- gray: "\x1b[38;5;245m", // labels
33
- red: "\x1b[38;5;203m", // pressure / overflow
34
- };
35
- const PULSE = ["◐", "◓", "◑", "◒"];
36
- // ── Full-width widget panel helpers ────────────────────────────────────────
37
- // pi's above-editor widget renderer (a Container of Text lines) does NOT pass
38
- // a terminal width to setWidget(), so lines render left-aligned by default. To
39
- // make the widget read as a full-width status panel we pad each line to the
40
- // real terminal width with a background fill. NOTE: C.reset is a FULL SGR
41
- // reset, so we re-apply the panel bg after every reset to keep the background
42
- // continuous under colored text (and under pi's own trailing reset).
43
- /** Default panel background (dark slate). Used when no theme is threaded or
44
- * the theme is transparent (no bg fill). Parametrized per-render via the
45
- * `panelTheme` arg so game-mode themes can restyle the panel background.
46
- * A transparent theme yields `""` so panelLine/panelBar still call
47
- * truncateToWidth (the width guard holds — empty prefix adds zero cells). */
48
- const DEFAULT_PANEL_BG = "\x1b[48;5;236m"; // dark slate panel background
49
- /** Resolve the panel-background SGR prefix for a theme id. Transparent themes
50
- * (bg=null) and unknown themes yield `""` (no bg fill) — the width guard in
51
- * panelLine/panelBar still applies via truncateToWidth. */
52
- function panelBgFor(theme) {
53
- if (!theme || theme === DEFAULT_THEME)
54
- return "";
55
- const t = getTheme(theme);
56
- const bg = t?.ansi.bg;
57
- return bg ? `\x1b[${bg}m` : "";
58
- }
59
- /** Resolve a theme ANSI accent/mega/fg SGR prefix (`\x1b[<params>m`) for the
60
- * given role. Falls back to `""` (no SGR) for transparent/unknown themes so
61
- * game-mode text still renders in the default fg without color noise. */
62
- function themeAnsi(theme, role) {
63
- const t = theme ? getTheme(theme) : undefined;
64
- const params = t?.ansi[role];
65
- return params ? `\x1b[${params}m` : "";
66
- }
67
- /** Emit a full SGR reset, OR `""` if `sgr` is empty (transparent theme). Keeps
68
- * the panel bg continuous by NOT clobbering it with a bare reset when the
69
- * accent/mega prefix was a no-op. */
70
- function sgrReset(sgr) {
71
- return sgr ? "\x1b[0m" : "";
72
- }
73
- // (Visible-width measurement is delegated to pi-tui's `visibleWidth` — imported
74
- // above — so our width math can never diverge from pi-tui's render-width check.)
75
- /** Wrap a string (with ANSI codes) to fit within `maxWidth` visible chars.
76
- * Splits at │ separators or whitespace when possible. */
77
- function wrapLine(text, maxWidth, panelBg) {
78
- if (maxWidth <= 0)
79
- return [text];
80
- const panelRst = "\x1b[0m" + panelBg;
81
- const result = [];
82
- let current = "";
83
- let currentW = 0;
84
- // Split at │ boundaries first
85
- const segments = text.split("│");
86
- for (let i = 0; i < segments.length; i++) {
87
- const seg = (i > 0 ? "│" : "") + segments[i];
88
- const segW = visibleWidth(panelBg + seg.replace(/\x1b\[0m/g, panelRst));
89
- if (currentW + segW <= maxWidth || currentW === 0) {
90
- current += seg;
91
- currentW += segW;
92
- }
93
- else {
94
- result.push(current);
95
- current = seg;
96
- currentW = segW;
97
- }
98
- }
99
- if (current)
100
- result.push(current);
101
- return result;
102
- }
103
- function panelLine(content, width, panelBg = DEFAULT_PANEL_BG) {
104
- if (width <= 0)
105
- return "";
106
- const panelRst = "\x1b[0m" + panelBg;
107
- // Apply the panel background; swap every inner full-reset for a reset that
108
- // re-applies the bg so the fill stays continuous under colored text.
109
- const withBg = panelBg + content.replace(/\x1b\[0m/g, panelRst);
110
- // truncateToWidth(line, width, "", true) returns EXACTLY `width` visible cells
111
- // (by pi-tui's measure), ANSI-preserved, space-padded. It hard-clips overflow
112
- // — so even a segment wider than `width` (or a width-rule mismatch) can never
113
- // produce a line that trips pi-tui's strict `visibleWidth(line) > width` check.
114
- return truncateToWidth(withBg, width, "", true) + "\x1b[0m";
115
- }
116
- /** A full-width hairline bar (top/bottom border of the panel). */
117
- function panelBar(width, ch = "─", panelBg = DEFAULT_PANEL_BG) {
118
- // `─` (U+2500) is narrow (1 cell) in both our measure and pi-tui's, so a
119
- // `ch.repeat(width)` bar is exactly `width` cells and already passes the
120
- // `> width` guard. truncateToWidth is belt-and-suspenders in case `ch` is
121
- // ever swapped for a wide/fullwidth character.
122
- return truncateToWidth(panelBg + ch.repeat(Math.max(0, width)), width, "", false) + "\x1b[0m";
123
- }
124
- // ── v0.8.3: ambient border-effect helpers ───────────────────────────────
125
- // The panel borders animate when an `activeEffect` is armed (level-up,
126
- // mega-cache overshoot, achievement unlock, compaction start). Two modes:
127
- // • pulse — a sine ramp on a 256-color base (accent=51 / mega=214 / red=203):
128
- // the base index is scaled by sin(π·t) so the border swells 0→peak→0 over
129
- // the duration, then returns to '' (idle). 256-color indices are clamped
130
- // to 0–255 defensively (the bases are all ≤214 so the clamp rarely bites).
131
- // • flash — a 120ms hard on/off alternate using the base index at full.
132
- // Returns '' when idle, expired, or elapsed<0 (clock skew) so non-effect
133
- // renders are byte-identical to the pre-effect panel (S31 matrix stays green).
134
- const EFFECT_BASE = {
135
- accent: 51,
136
- mega: 214,
137
- red: 203,
138
- };
139
- /** Resolve the per-frame border-fg SGR for an active effect. '' when idle or
140
- * expired (the widget's real per-frame expiry enforcer — snapshot-level clear
141
- * is just bookkeeping since snapshot is event-driven). */
142
- function effectBorderSgr(ae, now) {
143
- if (!ae)
144
- return "";
145
- const elapsed = now - ae.startedAt;
146
- if (elapsed < 0 || elapsed >= ae.durationMs)
147
- return "";
148
- const base = EFFECT_BASE[ae.role];
149
- if (ae.type === "flash") {
150
- // 120ms hard alternate: on (full base) / off (no SGR).
151
- return Math.floor(elapsed / 120) % 2 === 0 ? `\x1b[38;5;${base}m` : "";
152
- }
153
- // pulse: sine ramp 0 → peak → 0 over the duration.
154
- const t = elapsed / ae.durationMs;
155
- const amp = Math.sin(Math.PI * t); // 0 at start/end, 1 at midpoint
156
- const idx = Math.max(0, Math.min(255, Math.round(base * amp)));
157
- return `\x1b[38;5;${idx}m`;
158
- }
159
- /** Prepend the effect border SGR to a panel bar line. The SGR is a pure-fg
160
- * escape (zero visible width), so it never perturbs truncateToWidth's width
161
- * math — the bar's own `\x1b[0m` tail resets both fg + bg. No-op when sgr=''. */
162
- function effectBar(bar, sgr) {
163
- return sgr ? sgr + bar : bar;
164
- }
165
- /** Token-count formatter: M at/above 1e6, k at/above 1e3, raw below.
166
- * 5,472,700 → "5.5mil", 24,100 → "24.1k", 142 → "142". */
167
- function fmtTokens(x) {
168
- return x >= 1_000_000
169
- ? `${(x / 1_000_000).toFixed(1)}mil`
170
- : x >= 1000
171
- ? `${(x / 1000).toFixed(1)}k`
172
- : `${Math.round(x)}`;
173
- }
174
- /** Retro gradient bar — `w` cells shaded by fill position (green→amber→red).
175
- * Used for CONTEXT fill where low=green (room) and high=red (near the limit). */
176
- function ramp(pct, w = 12) {
177
- const cells = ["▏", "▎", "▍", "▌", "▋", "▊", "▉", "█"];
178
- const scaled = Math.max(0, Math.min(w, pct * w));
179
- const full = Math.floor(scaled);
180
- const frac = scaled - full;
181
- const fracCell = frac > 0 ? cells[Math.round(frac * (cells.length - 1))] : "";
182
- let out = "";
183
- for (let i = 0; i < full; i++)
184
- out += (i / w < 0.6 ? C.green : i / w < 0.85 ? C.amber : C.red) + "█";
185
- if (fracCell)
186
- out +=
187
- (full / w < 0.6 ? C.green : full / w < 0.85 ? C.amber : C.red) + fracCell;
188
- out +=
189
- C.dim + "░".repeat(Math.max(0, w - full - (fracCell ? 1 : 0))) + C.reset;
190
- return out;
191
- }
192
- /** Human "time since" string from a millisecond delta (or null → "never"). */
193
- function sinceCompactStr(ms) {
194
- if (ms == null)
195
- return "never";
196
- const s = Math.floor(ms / 1000);
197
- if (s < 60)
198
- return `${s}s ago`;
199
- const m = Math.floor(s / 60);
200
- if (m < 60)
201
- return `${m}m ago`;
202
- const h = Math.floor(m / 60);
203
- if (h < 24)
204
- return `${h}h ago`;
205
- return `${Math.floor(h / 24)}d ago`;
206
- }
13
+ // Re-export the ANSI palette + layout/effect helpers and the interfaces so the
14
+ // barrel (`extensions/mega-runtime.ts` does `export * from "./widget.js"`) and
15
+ // every direct consumer keep resolving the same named exports.
16
+ export { C, PULSE, DEFAULT_PANEL_BG, EFFECT_BASE, panelBgFor, themeAnsi, sgrReset, wrapLine, panelLine, panelBar, effectBorderSgr, effectBar, fmtTokens, ramp, sinceCompactStr, } from "./widget-ansi.js";
17
+ import { C, PULSE, DEFAULT_PANEL_BG, panelBgFor, themeAnsi, sgrReset, wrapLine, panelLine, panelBar, effectBorderSgr, effectBar, fmtTokens, ramp, sinceCompactStr } from "./widget-ansi.js";
207
18
  // ── buildWidgetLines ───────────────────────────────────────────────────────
208
- // Kept as a free function (not a MegaRuntime method) so state.ts stays
19
+ // Kept as a free function (not a MegaRuntime method) so runtime.ts stays
209
20
  // focused on state management. It reads the WidgetData snapshot + the live
210
21
  // activeAgents counter (passed in) and returns the panel lines.
211
22
  /** Build the full-width panel lines from the latest snapshot. Cheap: reads