pi-mega-compact 0.7.8 → 0.8.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/README.md +11 -12
- package/dist/extensions/dashboard-server/html.js +1023 -0
- package/dist/extensions/dashboard-server/html.test.js +41 -0
- package/dist/extensions/dashboard-server/index-reader.js +133 -0
- package/dist/extensions/dashboard-server/server.js +530 -0
- package/dist/extensions/dashboard-server/server.test.js +120 -0
- package/dist/extensions/dashboard-server/snapshot.js +43 -0
- package/dist/extensions/dashboard-server/state.js +30 -0
- package/dist/extensions/dashboard-server/types.js +5 -0
- package/dist/extensions/dashboard-server-s32.test.js +181 -0
- package/dist/extensions/dashboard-server.js +7 -1315
- package/dist/extensions/mega-commands.js +162 -134
- package/dist/extensions/mega-compact.js +3 -0
- package/dist/extensions/mega-compact.test.js +90 -21
- package/dist/extensions/mega-conflict-cmds.js +5 -1
- package/dist/extensions/mega-dashboard-cmds.js +29 -22
- package/dist/extensions/mega-db-cmds.js +11 -2
- package/dist/extensions/mega-events/agent-handlers.js +222 -0
- package/dist/extensions/mega-events/compact-handlers.js +162 -0
- package/dist/extensions/mega-events/context-handler.js +249 -0
- package/dist/extensions/mega-events/register.js +21 -0
- package/dist/extensions/mega-events/session-handlers.js +142 -0
- package/dist/extensions/mega-events.js +15 -699
- package/dist/extensions/mega-game-cmds.js +106 -0
- package/dist/extensions/mega-game-cmds.test.js +113 -0
- package/dist/extensions/mega-pipeline/compact.js +324 -0
- package/dist/extensions/mega-pipeline/memory-review.js +38 -0
- package/dist/extensions/mega-pipeline/recall.js +147 -0
- package/dist/extensions/mega-pipeline.js +9 -480
- package/dist/extensions/mega-runtime/helpers.js +40 -0
- package/dist/extensions/mega-runtime/query.js +29 -0
- package/dist/extensions/mega-runtime/state.js +877 -0
- package/dist/extensions/mega-runtime/state.test.js +171 -0
- package/dist/extensions/mega-runtime/widget.js +270 -0
- package/dist/extensions/mega-runtime/widget.test.js +160 -0
- package/dist/extensions/mega-runtime.js +15 -947
- package/dist/src/config/themes.js +84 -0
- package/dist/src/config/themes.test.js +94 -0
- package/dist/src/game/scoring.js +105 -0
- package/dist/src/game/scoring.test.js +98 -0
- package/dist/src/store/sqlite/checkpoints.js +145 -0
- package/dist/src/store/sqlite/dedup-mirror.js +64 -0
- package/dist/src/store/sqlite/foundation.js +38 -0
- package/dist/src/store/sqlite/game-achievements.js +111 -0
- package/dist/src/store/sqlite/game-achievements.test.js +67 -0
- package/dist/src/store/sqlite/game-scores.js +105 -0
- package/dist/src/store/sqlite/game-scores.test.js +106 -0
- package/dist/src/store/sqlite/game-state.js +54 -0
- package/dist/src/store/sqlite/game-state.test.js +76 -0
- package/dist/src/store/sqlite/global-index.js +224 -0
- package/dist/src/store/sqlite/maintenance.js +235 -0
- package/dist/src/store/sqlite/memories.js +164 -0
- package/dist/src/store/sqlite/meta.js +82 -0
- package/dist/src/store/sqlite/model-snapshots.js +47 -0
- package/dist/src/store/sqlite/raptor.js +57 -0
- package/dist/src/store/sqlite/raw-transcript.js +134 -0
- package/dist/src/store/sqlite/schema.js +294 -0
- package/dist/src/store/sqlite/session-state.js +28 -0
- package/dist/src/store/sqlite/stats.js +66 -0
- package/dist/src/store/sqlite/utils.js +120 -0
- package/dist/src/store/sqlite.js +23 -1607
- package/extensions/dashboard-server/html.test.ts +50 -0
- package/extensions/dashboard-server/html.ts +1026 -0
- package/extensions/dashboard-server/index-reader.ts +130 -0
- package/extensions/dashboard-server/server.test.ts +131 -0
- package/extensions/dashboard-server/server.ts +505 -0
- package/extensions/dashboard-server/snapshot.ts +44 -0
- package/extensions/dashboard-server/state.ts +33 -0
- package/extensions/dashboard-server/types.ts +134 -0
- package/extensions/dashboard-server-s32.test.ts +195 -0
- package/extensions/dashboard-server.ts +7 -1431
- package/extensions/mega-commands.ts +33 -10
- package/extensions/mega-compact.test.ts +198 -43
- package/extensions/mega-compact.ts +3 -0
- package/extensions/mega-conflict-cmds.ts +6 -2
- package/extensions/mega-dashboard-cmds.ts +30 -23
- package/extensions/mega-db-cmds.ts +11 -3
- package/extensions/mega-events/agent-handlers.ts +262 -0
- package/extensions/mega-events/compact-handlers.ts +192 -0
- package/extensions/mega-events/context-handler.ts +290 -0
- package/extensions/mega-events/register.ts +37 -0
- package/extensions/mega-events/session-handlers.ts +165 -0
- package/extensions/mega-events.ts +15 -780
- package/extensions/mega-game-cmds.test.ts +137 -0
- package/extensions/mega-game-cmds.ts +122 -0
- package/extensions/mega-pipeline/compact.ts +366 -0
- package/extensions/mega-pipeline/memory-review.ts +46 -0
- package/extensions/mega-pipeline/recall.ts +165 -0
- package/extensions/mega-pipeline.ts +9 -537
- package/extensions/mega-runtime/helpers.ts +68 -0
- package/extensions/mega-runtime/query.ts +29 -0
- package/extensions/mega-runtime/state.test.ts +171 -0
- package/extensions/mega-runtime/state.ts +967 -0
- package/extensions/mega-runtime/widget.test.ts +185 -0
- package/extensions/mega-runtime/widget.ts +359 -0
- package/extensions/mega-runtime.ts +15 -1093
- package/package.json +4 -3
- package/src/config/themes.test.ts +116 -0
- package/src/config/themes.ts +124 -0
- package/src/game/scoring.test.ts +103 -0
- package/src/game/scoring.ts +158 -0
- package/src/store/sqlite/checkpoints.ts +204 -0
- package/src/store/sqlite/dedup-mirror.ts +114 -0
- package/src/store/sqlite/foundation.ts +63 -0
- package/src/store/sqlite/game-achievements.test.ts +80 -0
- package/src/store/sqlite/game-achievements.ts +147 -0
- package/src/store/sqlite/game-scores.test.ts +132 -0
- package/src/store/sqlite/game-scores.ts +168 -0
- package/src/store/sqlite/game-state.test.ts +89 -0
- package/src/store/sqlite/game-state.ts +87 -0
- package/src/store/sqlite/global-index.ts +305 -0
- package/src/store/sqlite/maintenance.ts +294 -0
- package/src/store/sqlite/memories.ts +217 -0
- package/src/store/sqlite/meta.ts +108 -0
- package/src/store/sqlite/model-snapshots.ts +83 -0
- package/src/store/sqlite/raptor.ts +107 -0
- package/src/store/sqlite/raw-transcript.ts +221 -0
- package/src/store/sqlite/schema.ts +305 -0
- package/src/store/sqlite/session-state.ts +38 -0
- package/src/store/sqlite/stats.ts +127 -0
- package/src/store/sqlite/utils.ts +125 -0
- package/src/store/sqlite.ts +23 -2204
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* widget.test.ts — S31 buildWidgetLines render snapshot matrix.
|
|
3
|
+
*
|
|
4
|
+
* buildWidgetLines is a free function (no pi runtime needed), so we drive it
|
|
5
|
+
* directly with a fake WidgetData across the full matrix:
|
|
6
|
+
* 6 themes × {full,minimal} × {gameMode on/off} × {cachePct<100, >=100}
|
|
7
|
+
* and assert the S31 invariants:
|
|
8
|
+
* (a) transparent theme → no '\x1b[48;' bg fill on any line
|
|
9
|
+
* (b) non-transparent themes → at least one line carries '\x1b[48;'
|
|
10
|
+
* (c) minimal mode → exactly one content line (between the two panel bars)
|
|
11
|
+
* (d) gameMode off → no 'LVL' + no 'MEGA CACHE'
|
|
12
|
+
* (e) megaCacheFlare + gameMode on + cachePct>=100 → 'MEGA CACHE' text present
|
|
13
|
+
* (f) every line visibleWidth <= the terminal width passed in
|
|
14
|
+
*
|
|
15
|
+
* Uses MEGACOMPACT_STATE_DIR + mkdtemp (G7). No pi runtime.
|
|
16
|
+
*/
|
|
17
|
+
import { describe, it, before, after } from "node:test";
|
|
18
|
+
import assert from "node:assert/strict";
|
|
19
|
+
import { tmpdir } from "node:os";
|
|
20
|
+
import { join } from "node:path";
|
|
21
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
22
|
+
import { visibleWidth } from "@earendil-works/pi-tui";
|
|
23
|
+
import { buildWidgetLines, type WidgetData } from "./widget.js";
|
|
24
|
+
import { THEME_IDS, DEFAULT_THEME, getTheme } from "../../src/config/themes.js";
|
|
25
|
+
|
|
26
|
+
/** A minimal but complete WidgetData (all required fields populated). The S31
|
|
27
|
+
* game-mode fields are overridden per-case. */
|
|
28
|
+
function baseWd(overrides: Partial<WidgetData> = {}): WidgetData {
|
|
29
|
+
return {
|
|
30
|
+
version: "0.0.0-test",
|
|
31
|
+
tierLabel: "low",
|
|
32
|
+
triggerLabel: "idle",
|
|
33
|
+
pctStr: "42%",
|
|
34
|
+
tokStr: "10k",
|
|
35
|
+
maxStr: "200k",
|
|
36
|
+
ctxPct: 0.42,
|
|
37
|
+
chk: 3,
|
|
38
|
+
agentStr: "",
|
|
39
|
+
turnStr: "",
|
|
40
|
+
dedupStr: "12%",
|
|
41
|
+
sessIn: 1000,
|
|
42
|
+
sessKept: 800,
|
|
43
|
+
sTxt: "20",
|
|
44
|
+
repoIn: 5000,
|
|
45
|
+
repoKept: 4000,
|
|
46
|
+
rTxt: "20",
|
|
47
|
+
repoChk: 9,
|
|
48
|
+
repoSess: 2,
|
|
49
|
+
modelStr: "test-model",
|
|
50
|
+
sinceCompact: null,
|
|
51
|
+
embedderName: "Trigram",
|
|
52
|
+
compStr: "1.2x",
|
|
53
|
+
driftStatus: "ok",
|
|
54
|
+
agentsActive: false,
|
|
55
|
+
fresh: false,
|
|
56
|
+
ticker: [],
|
|
57
|
+
lastWhy: undefined,
|
|
58
|
+
tierTrace: undefined,
|
|
59
|
+
pulsing: false,
|
|
60
|
+
...overrides,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const WIDTH = 120;
|
|
65
|
+
|
|
66
|
+
function contentLines(lines: string[]): string[] {
|
|
67
|
+
// Strip the top + bottom panel bars; what's left is the content body.
|
|
68
|
+
return lines.slice(1, lines.length - 1);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
describe("buildWidgetLines (S31)", () => {
|
|
72
|
+
let dir: string;
|
|
73
|
+
before(() => {
|
|
74
|
+
dir = mkdtempSync(join(tmpdir(), "mc-widget-"));
|
|
75
|
+
process.env.MEGACOMPACT_STATE_DIR = dir;
|
|
76
|
+
});
|
|
77
|
+
after(() => {
|
|
78
|
+
delete process.env.MEGACOMPACT_STATE_DIR;
|
|
79
|
+
rmSync(dir, { recursive: true, force: true });
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("null wd → warm-up panel (3 lines, all width-safe)", () => {
|
|
83
|
+
const lines = buildWidgetLines(null, WIDTH, 0);
|
|
84
|
+
assert.equal(lines.length, 3);
|
|
85
|
+
for (const l of lines) assert.ok(visibleWidth(l) <= WIDTH, "width safe");
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
for (const theme of THEME_IDS) {
|
|
89
|
+
for (const tuiMode of ["full", "minimal"] as const) {
|
|
90
|
+
for (const gameMode of [false, true] as const) {
|
|
91
|
+
for (const cachePct of [42, 150] as const) {
|
|
92
|
+
const flare = cachePct >= 100;
|
|
93
|
+
const label = `theme=${theme} tui=${tuiMode} game=${gameMode} cache=${cachePct}`;
|
|
94
|
+
it(label, () => {
|
|
95
|
+
const wd = baseWd({
|
|
96
|
+
theme,
|
|
97
|
+
tuiMode,
|
|
98
|
+
gameMode,
|
|
99
|
+
level: 1,
|
|
100
|
+
cachePct,
|
|
101
|
+
megaCacheFlare: flare,
|
|
102
|
+
});
|
|
103
|
+
const lines = buildWidgetLines(wd, WIDTH, 0);
|
|
104
|
+
const body = contentLines(lines);
|
|
105
|
+
const joined = lines.join("\n");
|
|
106
|
+
|
|
107
|
+
// (f) every line width <= terminal (truncateToWidth respected)
|
|
108
|
+
for (const l of lines) {
|
|
109
|
+
assert.ok(
|
|
110
|
+
visibleWidth(l) <= WIDTH,
|
|
111
|
+
`width safe: ${visibleWidth(l)} > ${WIDTH}`,
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// (a) transparent theme → no '\x1b[48;' bg fill anywhere
|
|
116
|
+
if (theme === DEFAULT_THEME) {
|
|
117
|
+
assert.ok(
|
|
118
|
+
!joined.includes("\x1b[48;"),
|
|
119
|
+
`transparent has no 48; bg fill: ${label}`,
|
|
120
|
+
);
|
|
121
|
+
} else {
|
|
122
|
+
// (b) non-transparent themes → their bg SGR appears on some line.
|
|
123
|
+
// (Themes may use 3-bit bg like \x1b[40m, not just 48;5; form.)
|
|
124
|
+
const bgParams = getTheme(theme)!.ansi.bg!;
|
|
125
|
+
const bgEsc = `\x1b[${bgParams}m`;
|
|
126
|
+
assert.ok(
|
|
127
|
+
joined.includes(bgEsc),
|
|
128
|
+
`themed has bg fill ${bgEsc}: ${label}`,
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// (c) minimal mode → exactly one content line
|
|
133
|
+
if (tuiMode === "minimal") {
|
|
134
|
+
assert.equal(body.length, 1, `minimal one line: ${label}`);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// (d) gameMode off → no LVL + no MEGA CACHE
|
|
138
|
+
if (!gameMode) {
|
|
139
|
+
assert.ok(!joined.includes("LVL"), `no LVL when off: ${label}`);
|
|
140
|
+
assert.ok(
|
|
141
|
+
!joined.includes("MEGA CACHE"),
|
|
142
|
+
`no MEGA CACHE when off: ${label}`,
|
|
143
|
+
);
|
|
144
|
+
} else {
|
|
145
|
+
// gameMode on → LVL shown (in full mode header, or minimal line)
|
|
146
|
+
assert.ok(joined.includes("LVL"), `LVL shown when on: ${label}`);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// (e) MEGA CACHE flare text appears only when flare + gameMode on
|
|
150
|
+
const expectMega = flare && gameMode;
|
|
151
|
+
const hasMega = joined.includes("MEGA CACHE");
|
|
152
|
+
assert.equal(
|
|
153
|
+
hasMega,
|
|
154
|
+
expectMega,
|
|
155
|
+
`MEGA CACHE flare: ${label} (got ${hasMega}, want ${expectMega})`,
|
|
156
|
+
);
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
describe("buildWidgetLines achievement flare (S35)", () => {
|
|
166
|
+
const achBase = (overrides: Partial<WidgetData> = {}): WidgetData => baseWd({
|
|
167
|
+
theme: DEFAULT_THEME, tuiMode: "full", gameMode: true, level: 1, cachePct: 42, ...overrides,
|
|
168
|
+
});
|
|
169
|
+
it("achievementFlare + titles -> renders the unlock toast line", () => {
|
|
170
|
+
const lines = buildWidgetLines(achBase({ achievementFlare: true, achievementFlareTitles: ["First Compact"] }), WIDTH, 0);
|
|
171
|
+
assert.ok(lines.some((l) => l.includes("Achievement unlocked: First Compact")), "toast line present");
|
|
172
|
+
});
|
|
173
|
+
it("achievementFlare off -> no toast line", () => {
|
|
174
|
+
const lines = buildWidgetLines(achBase({ achievementFlare: false, achievementFlareTitles: ["First Compact"] }), WIDTH, 0);
|
|
175
|
+
assert.ok(!lines.some((l) => l.includes("Achievement unlocked")), "no toast when flare off");
|
|
176
|
+
});
|
|
177
|
+
it("gameMode off -> no toast even if flare set", () => {
|
|
178
|
+
const lines = buildWidgetLines(achBase({ gameMode: false, achievementFlare: true, achievementFlareTitles: ["X"] }), WIDTH, 0);
|
|
179
|
+
assert.ok(!lines.some((l) => l.includes("Achievement unlocked")), "no toast when game off");
|
|
180
|
+
});
|
|
181
|
+
it("achievement toast is width-safe", () => {
|
|
182
|
+
const lines = buildWidgetLines(achBase({ achievementFlare: true, achievementFlareTitles: ["First Compact", "Turn Veteran"] }), 60, 0);
|
|
183
|
+
for (const l of lines) assert.ok(visibleWidth(l) <= 60, "width safe");
|
|
184
|
+
});
|
|
185
|
+
});
|
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* widget.ts — above-editor widget rendering helpers (free functions, consts,
|
|
3
|
+
* and interfaces) extracted from the original mega-runtime.ts monolith.
|
|
4
|
+
*
|
|
5
|
+
* These are pure rendering primitives with zero runtime-state dependencies;
|
|
6
|
+
* the `MegaRuntime` class (state.ts) imports them to paint the panel.
|
|
7
|
+
*/
|
|
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
|
+
|
|
19
|
+
// ── ANSI palette ───────────────────────────────────────────────────────────
|
|
20
|
+
// The pi TUI's Text component preserves ANSI escape codes (see wrapTextWithAnsi),
|
|
21
|
+
// so raw escapes render as colors. No chalk dependency needed — these are just
|
|
22
|
+
// strings. Exported because mega-pipeline.ts and mega-commands.ts import `C`
|
|
23
|
+
// from the mega-runtime barrel.
|
|
24
|
+
export const C = {
|
|
25
|
+
reset: "\x1b[0m",
|
|
26
|
+
dim: "\x1b[2m",
|
|
27
|
+
bold: "\x1b[1m",
|
|
28
|
+
amber: "\x1b[38;5;214m", // tier / ready
|
|
29
|
+
green: "\x1b[38;5;120m", // saved
|
|
30
|
+
cyan: "\x1b[38;5;51m", // used / live activity
|
|
31
|
+
teal: "\x1b[38;5;37m", // processing (compress/dedup)
|
|
32
|
+
magenta: "\x1b[38;5;201m", // dedup rate
|
|
33
|
+
blue: "\x1b[38;5;75m", // repo totals
|
|
34
|
+
gray: "\x1b[38;5;245m", // labels
|
|
35
|
+
red: "\x1b[38;5;203m", // pressure / overflow
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const PULSE = ["◐", "◓", "◑", "◒"];
|
|
39
|
+
|
|
40
|
+
// ── Full-width widget panel helpers ────────────────────────────────────────
|
|
41
|
+
// pi's above-editor widget renderer (a Container of Text lines) does NOT pass
|
|
42
|
+
// a terminal width to setWidget(), so lines render left-aligned by default. To
|
|
43
|
+
// make the widget read as a full-width status panel we pad each line to the
|
|
44
|
+
// real terminal width with a background fill. NOTE: C.reset is a FULL SGR
|
|
45
|
+
// reset, so we re-apply the panel bg after every reset to keep the background
|
|
46
|
+
// continuous under colored text (and under pi's own trailing reset).
|
|
47
|
+
/** Default panel background (dark slate). Used when no theme is threaded or
|
|
48
|
+
* the theme is transparent (no bg fill). Parametrized per-render via the
|
|
49
|
+
* `panelTheme` arg so game-mode themes can restyle the panel background.
|
|
50
|
+
* A transparent theme yields `""` so panelLine/panelBar still call
|
|
51
|
+
* truncateToWidth (the width guard holds — empty prefix adds zero cells). */
|
|
52
|
+
const DEFAULT_PANEL_BG = "\x1b[48;5;236m"; // dark slate panel background
|
|
53
|
+
|
|
54
|
+
/** Resolve the panel-background SGR prefix for a theme id. Transparent themes
|
|
55
|
+
* (bg=null) and unknown themes yield `""` (no bg fill) — the width guard in
|
|
56
|
+
* panelLine/panelBar still applies via truncateToWidth. */
|
|
57
|
+
function panelBgFor(theme: string | undefined): string {
|
|
58
|
+
if (!theme || theme === DEFAULT_THEME) return "";
|
|
59
|
+
const t = getTheme(theme);
|
|
60
|
+
const bg = t?.ansi.bg;
|
|
61
|
+
return bg ? `\x1b[${bg}m` : "";
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Resolve a theme ANSI accent/mega/fg SGR prefix (`\x1b[<params>m`) for the
|
|
65
|
+
* given role. Falls back to `""` (no SGR) for transparent/unknown themes so
|
|
66
|
+
* game-mode text still renders in the default fg without color noise. */
|
|
67
|
+
function themeAnsi(theme: string | undefined, role: "fg" | "accent" | "mega"): string {
|
|
68
|
+
const t = theme ? getTheme(theme) : undefined;
|
|
69
|
+
const params = t?.ansi[role];
|
|
70
|
+
return params ? `\x1b[${params}m` : "";
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Emit a full SGR reset, OR `""` if `sgr` is empty (transparent theme). Keeps
|
|
74
|
+
* the panel bg continuous by NOT clobbering it with a bare reset when the
|
|
75
|
+
* accent/mega prefix was a no-op. */
|
|
76
|
+
function sgrReset(sgr: string): string {
|
|
77
|
+
return sgr ? "\x1b[0m" : "";
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// (Visible-width measurement is delegated to pi-tui's `visibleWidth` — imported
|
|
81
|
+
// above — so our width math can never diverge from pi-tui's render-width check.)
|
|
82
|
+
|
|
83
|
+
/** Wrap a string (with ANSI codes) to fit within `maxWidth` visible chars.
|
|
84
|
+
* Splits at │ separators or whitespace when possible. */
|
|
85
|
+
function wrapLine(text: string, maxWidth: number, panelBg: string): string[] {
|
|
86
|
+
if (maxWidth <= 0) return [text];
|
|
87
|
+
const panelRst = "\x1b[0m" + panelBg;
|
|
88
|
+
const result: string[] = [];
|
|
89
|
+
let current = "";
|
|
90
|
+
let currentW = 0;
|
|
91
|
+
// Split at │ boundaries first
|
|
92
|
+
const segments = text.split("│");
|
|
93
|
+
for (let i = 0; i < segments.length; i++) {
|
|
94
|
+
const seg = (i > 0 ? "│" : "") + segments[i];
|
|
95
|
+
const segW = visibleWidth(panelBg + seg.replace(/\x1b\[0m/g, panelRst));
|
|
96
|
+
if (currentW + segW <= maxWidth || currentW === 0) {
|
|
97
|
+
current += seg;
|
|
98
|
+
currentW += segW;
|
|
99
|
+
} else {
|
|
100
|
+
result.push(current);
|
|
101
|
+
current = seg;
|
|
102
|
+
currentW = segW;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (current) result.push(current);
|
|
106
|
+
return result;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function panelLine(content: string, width: number, panelBg: string = DEFAULT_PANEL_BG): string {
|
|
110
|
+
if (width <= 0) return "";
|
|
111
|
+
const panelRst = "\x1b[0m" + panelBg;
|
|
112
|
+
// Apply the panel background; swap every inner full-reset for a reset that
|
|
113
|
+
// re-applies the bg so the fill stays continuous under colored text.
|
|
114
|
+
const withBg = panelBg + content.replace(/\x1b\[0m/g, panelRst);
|
|
115
|
+
// truncateToWidth(line, width, "", true) returns EXACTLY `width` visible cells
|
|
116
|
+
// (by pi-tui's measure), ANSI-preserved, space-padded. It hard-clips overflow
|
|
117
|
+
// — so even a segment wider than `width` (or a width-rule mismatch) can never
|
|
118
|
+
// produce a line that trips pi-tui's strict `visibleWidth(line) > width` check.
|
|
119
|
+
return truncateToWidth(withBg, width, "", true) + "\x1b[0m";
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** A full-width hairline bar (top/bottom border of the panel). */
|
|
123
|
+
function panelBar(width: number, ch = "─", panelBg: string = DEFAULT_PANEL_BG): string {
|
|
124
|
+
// `─` (U+2500) is narrow (1 cell) in both our measure and pi-tui's, so a
|
|
125
|
+
// `ch.repeat(width)` bar is exactly `width` cells and already passes the
|
|
126
|
+
// `> width` guard. truncateToWidth is belt-and-suspenders in case `ch` is
|
|
127
|
+
// ever swapped for a wide/fullwidth character.
|
|
128
|
+
return truncateToWidth(panelBg + ch.repeat(Math.max(0, width)), width, "", false) + "\x1b[0m";
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Token-count formatter: M at/above 1e6, k at/above 1e3, raw below.
|
|
132
|
+
* 5,472,700 → "5.5mil", 24,100 → "24.1k", 142 → "142". */
|
|
133
|
+
function fmtTokens(x: number): string {
|
|
134
|
+
return x >= 1_000_000
|
|
135
|
+
? `${(x / 1_000_000).toFixed(1)}mil`
|
|
136
|
+
: x >= 1000
|
|
137
|
+
? `${(x / 1000).toFixed(1)}k`
|
|
138
|
+
: `${Math.round(x)}`;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Retro gradient bar — `w` cells shaded by fill position (green→amber→red).
|
|
142
|
+
* Used for CONTEXT fill where low=green (room) and high=red (near the limit). */
|
|
143
|
+
function ramp(pct: number, w = 12): string {
|
|
144
|
+
const cells = ["▏", "▎", "▍", "▌", "▋", "▊", "▉", "█"];
|
|
145
|
+
const scaled = Math.max(0, Math.min(w, pct * w));
|
|
146
|
+
const full = Math.floor(scaled);
|
|
147
|
+
const frac = scaled - full;
|
|
148
|
+
const fracCell = frac > 0 ? cells[Math.round(frac * (cells.length - 1))] : "";
|
|
149
|
+
let out = "";
|
|
150
|
+
for (let i = 0; i < full; i++)
|
|
151
|
+
out += (i / w < 0.6 ? C.green : i / w < 0.85 ? C.amber : C.red) + "█";
|
|
152
|
+
if (fracCell)
|
|
153
|
+
out +=
|
|
154
|
+
(full / w < 0.6 ? C.green : full / w < 0.85 ? C.amber : C.red) + fracCell;
|
|
155
|
+
out +=
|
|
156
|
+
C.dim + "░".repeat(Math.max(0, w - full - (fracCell ? 1 : 0))) + C.reset;
|
|
157
|
+
return out;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Human "time since" string from a millisecond delta (or null → "never"). */
|
|
161
|
+
function sinceCompactStr(ms: number | null): string {
|
|
162
|
+
if (ms == null) return "never";
|
|
163
|
+
const s = Math.floor(ms / 1000);
|
|
164
|
+
if (s < 60) return `${s}s ago`;
|
|
165
|
+
const m = Math.floor(s / 60);
|
|
166
|
+
if (m < 60) return `${m}m ago`;
|
|
167
|
+
const h = Math.floor(m / 60);
|
|
168
|
+
if (h < 24) return `${h}h ago`;
|
|
169
|
+
return `${Math.floor(h / 24)}d ago`;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Ticker ring-buffer entry (recall/activity history for the widget footer). */
|
|
173
|
+
export interface TickerEntry {
|
|
174
|
+
text: string;
|
|
175
|
+
at: number;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Immutable snapshot of everything the above-editor widget needs to render.
|
|
179
|
+
* Computed once per `snapshot()` (event-driven) and read by `buildWidgetLines`
|
|
180
|
+
* on every TUI render frame, so frame rendering stays allocation-cheap and the
|
|
181
|
+
* panel auto-fits whatever width pi passes to the setWidget factory. */
|
|
182
|
+
export interface WidgetData {
|
|
183
|
+
version: string;
|
|
184
|
+
tierLabel: string;
|
|
185
|
+
triggerLabel: string;
|
|
186
|
+
pctStr: string;
|
|
187
|
+
tokStr: string;
|
|
188
|
+
maxStr: string;
|
|
189
|
+
ctxPct: number;
|
|
190
|
+
chk: number;
|
|
191
|
+
agentStr: string;
|
|
192
|
+
turnStr: string;
|
|
193
|
+
dedupStr: string;
|
|
194
|
+
sessIn: number;
|
|
195
|
+
sessKept: number;
|
|
196
|
+
sTxt: string;
|
|
197
|
+
repoIn: number;
|
|
198
|
+
repoKept: number;
|
|
199
|
+
rTxt: string;
|
|
200
|
+
repoChk: number;
|
|
201
|
+
repoSess: number;
|
|
202
|
+
modelStr: string;
|
|
203
|
+
sinceCompact: number | null;
|
|
204
|
+
embedderName: string;
|
|
205
|
+
compStr: string;
|
|
206
|
+
driftStatus: "ok" | "warn";
|
|
207
|
+
agentsActive: boolean;
|
|
208
|
+
fresh: boolean;
|
|
209
|
+
ticker: TickerEntry[];
|
|
210
|
+
lastWhy: string | undefined;
|
|
211
|
+
tierTrace: string | undefined;
|
|
212
|
+
pulsing: boolean;
|
|
213
|
+
// ── S31: game-mode theming + display modes + level + MEGA CACHE flare ──
|
|
214
|
+
/** Game mode on (shows level + MEGA CACHE flare; hides them when off). */
|
|
215
|
+
gameMode?: boolean;
|
|
216
|
+
/** Theme id (src/config/themes). Drives the panel bg + accent/mega ANSI. */
|
|
217
|
+
theme?: string;
|
|
218
|
+
/** TUI display mode: 'full' (default) = the full stats panel; 'minimal' = a
|
|
219
|
+
* one-line `LVL n | cache NN%` view flanked by panel bars. */
|
|
220
|
+
tuiMode?: "full" | "minimal";
|
|
221
|
+
/** Player level (game-mode). Stub = 1 until S33 wires the real scoring. */
|
|
222
|
+
level?: number;
|
|
223
|
+
/** Cache hit rate as a percent (0..100+, may exceed 100 → MEGA CACHE). */
|
|
224
|
+
cachePct?: number;
|
|
225
|
+
/** MEGA CACHE flare armed (fires at cachePct >= 100 + gameMode on). Adds the
|
|
226
|
+
* ANSI MEGA CACHE banner + the oopsie gag to the header. */
|
|
227
|
+
megaCacheFlare?: boolean;
|
|
228
|
+
/** The peak cache % that armed the flare (for the oopsie toast text). */
|
|
229
|
+
megaCacheFlarePct?: number;
|
|
230
|
+
levelUpFlare?: boolean;
|
|
231
|
+
/** S35: achievement-unlock flare -- renders a one-line toast for one cycle. */
|
|
232
|
+
achievementFlare?: boolean;
|
|
233
|
+
achievementFlareTitles?: string[];
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// ── buildWidgetLines ───────────────────────────────────────────────────────
|
|
237
|
+
// Kept as a free function (not a MegaRuntime method) so state.ts stays
|
|
238
|
+
// focused on state management. It reads the WidgetData snapshot + the live
|
|
239
|
+
// activeAgents counter (passed in) and returns the panel lines.
|
|
240
|
+
|
|
241
|
+
/** Build the full-width panel lines from the latest snapshot. Cheap: reads
|
|
242
|
+
* only the WidgetData + a couple of live counters; no DB/IO. */
|
|
243
|
+
export function buildWidgetLines(
|
|
244
|
+
wd: WidgetData | null,
|
|
245
|
+
width: number,
|
|
246
|
+
activeAgents: number,
|
|
247
|
+
): string[] {
|
|
248
|
+
// Resolve the panel background from the threaded theme (transparent → "",
|
|
249
|
+
// unknown → default dark slate). Computed once per render; threaded into
|
|
250
|
+
// every panelLine/panelBar/wrapLine so the bg stays continuous and the width
|
|
251
|
+
// guard (truncateToWidth) still holds for transparent themes.
|
|
252
|
+
const panelBg = wd?.theme ? panelBgFor(wd.theme) : DEFAULT_PANEL_BG;
|
|
253
|
+
if (!wd) {
|
|
254
|
+
return [
|
|
255
|
+
panelBar(width, "─", panelBg),
|
|
256
|
+
panelLine(" mega-compact: warming up…", width, panelBg),
|
|
257
|
+
panelBar(width, "─", panelBg),
|
|
258
|
+
];
|
|
259
|
+
}
|
|
260
|
+
// S31: minimal TUI mode — a single content line `LVL n | cache NN%` flanked
|
|
261
|
+
// by panel bars. Built through the same panelLine/panelBar helpers so the
|
|
262
|
+
// width guard + theme bg apply identically to the full panel. Level is shown
|
|
263
|
+
// only when game mode is on (otherwise just the cache %).
|
|
264
|
+
if (wd.tuiMode === "minimal") {
|
|
265
|
+
const lvl = wd.gameMode ? wd.level ?? 1 : undefined;
|
|
266
|
+
const cachePct = wd.cachePct ?? 0;
|
|
267
|
+
const cacheStr = `${Math.round(cachePct * 10) / 10}%`;
|
|
268
|
+
const accent = themeAnsi(wd.theme, "accent");
|
|
269
|
+
const mega = themeAnsi(wd.theme, "mega");
|
|
270
|
+
const megaFlare =
|
|
271
|
+
wd.gameMode && wd.megaCacheFlare && cachePct >= 100
|
|
272
|
+
? ` ${mega}MEGA CACHE${sgrReset(mega)}`
|
|
273
|
+
: "";
|
|
274
|
+
const body =
|
|
275
|
+
lvl != null
|
|
276
|
+
? `${accent}${wd.gameMode && wd.levelUpFlare ? "\x1b[5m" : ""}LVL ${lvl}${wd.gameMode && wd.levelUpFlare ? "\x1b[0m" : ""}${sgrReset(accent)} ${C.dim}|${C.reset} cache ${cacheStr}${megaFlare}`
|
|
277
|
+
: `cache ${cacheStr}${megaFlare}`;
|
|
278
|
+
return [
|
|
279
|
+
panelBar(width, "─", panelBg),
|
|
280
|
+
panelLine(` ${body}`, width, panelBg),
|
|
281
|
+
panelBar(width, "─", panelBg),
|
|
282
|
+
];
|
|
283
|
+
}
|
|
284
|
+
const pulse = wd.pulsing
|
|
285
|
+
? `${C.cyan}${PULSE[Math.floor(Date.now() / 250) % PULSE.length]}${C.reset} `
|
|
286
|
+
: "";
|
|
287
|
+
const sep = ` ${C.dim}│${C.reset} `;
|
|
288
|
+
// S31: game-mode header prefix — `LVL n` (accent) prepended to content[0],
|
|
289
|
+
// and a MEGA CACHE flare (mega ansi) + oopsie gag appended when armed. Both
|
|
290
|
+
// hidden when game mode is off (keeps the legacy panel byte-for-byte).
|
|
291
|
+
const lvlPrefix = wd.gameMode
|
|
292
|
+
? `${themeAnsi(wd.theme, "accent")}${wd.gameMode && wd.levelUpFlare ? "\x1b[5m" : ""}LVL ${wd.level ?? 1}${wd.gameMode && wd.levelUpFlare ? "\x1b[0m" : ""}${sgrReset(themeAnsi(wd.theme, "accent"))} `
|
|
293
|
+
: "";
|
|
294
|
+
const megaFlareSuffix =
|
|
295
|
+
wd.gameMode && wd.megaCacheFlare && (wd.cachePct ?? 0) >= 100
|
|
296
|
+
? `${sep}${themeAnsi(wd.theme, "mega")}MEGA CACHE! (oops, you cached so hard the dedup caught fire)${sgrReset(themeAnsi(wd.theme, "mega"))}`
|
|
297
|
+
: "";
|
|
298
|
+
// Build one long content line — let terminal wrap it naturally
|
|
299
|
+
const content = [
|
|
300
|
+
`${lvlPrefix}${C.amber}⚡ ${wd.tierLabel}${C.reset} v${C.bold}${wd.version}${C.reset} ${ramp(wd.ctxPct, 20)} ${C.bold}${wd.pctStr}${C.reset} ${wd.tokStr}/${wd.maxStr}${megaFlareSuffix}`,
|
|
301
|
+
wd.triggerLabel,
|
|
302
|
+
`${C.cyan}${wd.modelStr}${C.reset}`,
|
|
303
|
+
`${wd.chk} chk${wd.agentStr}${wd.turnStr}`,
|
|
304
|
+
`${C.magenta}dup ${wd.dedupStr}${C.reset}`,
|
|
305
|
+
`${C.gray}sess${C.reset} ${fmtTokens(wd.sessIn)}→${fmtTokens(wd.sessKept)} kept ${C.green}(${wd.sTxt}% freed)${C.reset}`,
|
|
306
|
+
`${C.gray}all-time${C.reset} ${fmtTokens(wd.repoIn)}→${fmtTokens(wd.repoKept)} kept ${C.blue}(${wd.rTxt}% freed)${C.reset}`,
|
|
307
|
+
`${wd.repoChk} chk/${wd.repoSess} sess`,
|
|
308
|
+
`${C.gray}mem${C.reset} ${wd.embedderName} · ${wd.chk} chunks · ${C.blue}comp ${wd.compStr}${C.reset}`,
|
|
309
|
+
`${C.gray}drift${C.reset} ${wd.driftStatus === "ok" ? C.green : C.amber}${wd.driftStatus}${C.reset}`,
|
|
310
|
+
`${C.gray}compact${C.reset} ${sinceCompactStr(wd.sinceCompact)}`,
|
|
311
|
+
].join(sep);
|
|
312
|
+
// Wrap to terminal width and pad each line
|
|
313
|
+
const wrapped = wrapLine(content, width - 2, panelBg); // 2-char indent
|
|
314
|
+
const lines: string[] = [
|
|
315
|
+
panelBar(width, "─", panelBg),
|
|
316
|
+
...wrapped.map((l) => panelLine(l, width, panelBg)),
|
|
317
|
+
];
|
|
318
|
+
// L4 — agents block (S27, count + status; per-agent tokens gated on P0)
|
|
319
|
+
if (wd.agentsActive) {
|
|
320
|
+
lines.push(
|
|
321
|
+
panelLine(
|
|
322
|
+
` ${C.cyan}🤖 ${activeAgents} active${wd.turnStr}${C.reset}`,
|
|
323
|
+
width,
|
|
324
|
+
panelBg,
|
|
325
|
+
),
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
// L5 — live ticker / activity (♻ deduped … why, or tier trace, or pulsing)
|
|
329
|
+
if (wd.tierTrace && wd.fresh) {
|
|
330
|
+
lines.push(panelLine(` ${pulse}${wd.tierTrace}`, width, panelBg));
|
|
331
|
+
} else if (wd.ticker.length > 0) {
|
|
332
|
+
const step = Math.floor(Date.now() / 250);
|
|
333
|
+
const idx = wd.ticker.length - 1 - (step % wd.ticker.length);
|
|
334
|
+
const head = wd.ticker[idx].text;
|
|
335
|
+
const why = wd.lastWhy ? ` ${C.gray}· ${wd.lastWhy}${C.reset}` : "";
|
|
336
|
+
const more =
|
|
337
|
+
wd.ticker.length > 1
|
|
338
|
+
? ` ${C.dim}(+${wd.ticker.length - 1} more)${C.reset}`
|
|
339
|
+
: "";
|
|
340
|
+
lines.push(
|
|
341
|
+
panelLine(
|
|
342
|
+
` ${wd.fresh ? C.teal : C.dim}${head}${why}${more}${C.reset}`,
|
|
343
|
+
width,
|
|
344
|
+
panelBg,
|
|
345
|
+
),
|
|
346
|
+
);
|
|
347
|
+
} else if (wd.pulsing) {
|
|
348
|
+
lines.push(panelLine(` ${pulse}${C.teal}compacting…${C.reset}`, width, panelBg));
|
|
349
|
+
}
|
|
350
|
+
// S35: achievement-unlock toast (one-line, accent) -- fires for one render cycle.
|
|
351
|
+
if (wd.gameMode && wd.achievementFlare && wd.achievementFlareTitles?.length) {
|
|
352
|
+
const accentSgr = themeAnsi(wd.theme, "accent");
|
|
353
|
+
const titlesStr = wd.achievementFlareTitles.join(", ");
|
|
354
|
+
lines.push(panelLine(` ${accentSgr}🏆 Achievement unlocked: ${titlesStr}${sgrReset(accentSgr)}`, width, panelBg));
|
|
355
|
+
}
|
|
356
|
+
// bottom border
|
|
357
|
+
lines.push(panelBar(width, "─", panelBg));
|
|
358
|
+
return lines;
|
|
359
|
+
}
|