@ftrdotdev/pi-tui 0.5.2 → 0.7.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.
@@ -3,7 +3,15 @@ import { Effect } from "effect";
3
3
  import type { Theme } from "@earendil-works/pi-coding-agent";
4
4
  import { DEFAULT_CONFIG, type TuiConfig } from "../config.ts";
5
5
  import { VimRouter } from "../vim/vim-router.ts";
6
- import { makeSettingsUi, type SettingsUiHandle } from "./settings-command.ts";
6
+ import {
7
+ hasTrackerExtension,
8
+ loadClassifierChoices,
9
+ makeSettingsUi,
10
+ type ClassifierChoice,
11
+ type ClassifierDescriptor,
12
+ type ClassifierSource,
13
+ type SettingsUiHandle,
14
+ } from "./settings-command.ts";
7
15
 
8
16
  // Pass-through theme: selections and the active tab are detected via
9
17
  // plain-text markers ("→ " prefix, "[Tab]" brackets) in the render output.
@@ -11,6 +19,7 @@ const theme = {
11
19
  fg: (_color: string, text: string) => text,
12
20
  bg: (_color: string, text: string) => text,
13
21
  bold: (text: string) => text,
22
+ style: (text: string) => text,
14
23
  } as unknown as Theme;
15
24
 
16
25
  const RENDER_WIDTH = 120;
@@ -28,11 +37,23 @@ interface UiSpy {
28
37
  closes: number;
29
38
  }
30
39
 
31
- const makeUi = (overrides: Partial<TuiConfig> = {}): UiSpy => {
40
+ const defaultChoices: ClassifierChoice[] = [
41
+ { value: null, label: "Automatic (prefer Clef Flash, then Jev)" },
42
+ { value: "typesafe/jev-latest", label: "Safe" },
43
+ { value: "cloudflare-workers-ai/@cf/cloudflare/clef-flash", label: "Fast" },
44
+ ];
45
+
46
+ const makeUi = (
47
+ overrides: Partial<TuiConfig> = {},
48
+ choices: readonly ClassifierChoice[] = defaultChoices,
49
+ hasTracker = true,
50
+ ): UiSpy => {
32
51
  const spy: UiSpy = { ui: undefined!, changes: [], closes: 0 };
33
52
  spy.ui = makeSettingsUi(
34
53
  theme,
35
54
  { ...structuredClone(DEFAULT_CONFIG), ...overrides },
55
+ choices,
56
+ hasTracker,
36
57
  (config) => {
37
58
  spy.changes.push(config);
38
59
  },
@@ -52,7 +73,7 @@ const selectedShows = (ui: SettingsUiHandle, label: string): boolean =>
52
73
  const activeTab = (ui: SettingsUiHandle): string | undefined => {
53
74
  const line = rendered(ui).find((l) => l.includes("["));
54
75
  if (!line) return undefined;
55
- for (const tab of ["General", "Icons", "Footer", "Telemetry"] as const) {
76
+ for (const tab of ["General", "Compaction", "Icons", "Footer", "Telemetry"] as const) {
56
77
  if (line.includes(`[${tab}]`)) return tab;
57
78
  }
58
79
  return undefined;
@@ -72,7 +93,10 @@ it("starts with the first item of the features tab selected", () => {
72
93
  it("renders the Vim mode item on the features tab showing Off by default", () => {
73
94
  const { ui } = makeUi();
74
95
  const lines = rendered(ui);
75
- assert.strictEqual(lines.some((line) => line.includes("Vim mode") && line.includes("Off")), true);
96
+ assert.strictEqual(
97
+ lines.some((line) => line.includes("Vim mode") && line.includes("Off")),
98
+ true,
99
+ );
76
100
  });
77
101
 
78
102
  it("Space toggles vim on and reports the change", () => {
@@ -82,7 +106,10 @@ it("Space toggles vim on and reports the change", () => {
82
106
  assert.strictEqual(changes.length, 1);
83
107
  assert.strictEqual(changes[0]!.vim, true);
84
108
  assert.strictEqual(changes[0]!.enabled, true); // other fields untouched
85
- assert.strictEqual(rendered(ui).some((line) => line.includes("Vim mode") && line.includes("On")), true);
109
+ assert.strictEqual(
110
+ rendered(ui).some((line) => line.includes("Vim mode") && line.includes("On")),
111
+ true,
112
+ );
86
113
  });
87
114
 
88
115
  it("j moves the selection down and wraps to the first item at the end", () => {
@@ -132,6 +159,8 @@ it("j moves the selection with vim off (settings gate is unconditional)", () =>
132
159
  it("l switches to the next tab and wraps from the last tab to the first", () => {
133
160
  const { ui } = makeUi();
134
161
  ui.handleInput("l");
162
+ assert.strictEqual(activeTab(ui), "Compaction");
163
+ ui.handleInput("l");
135
164
  assert.strictEqual(activeTab(ui), "Icons");
136
165
  ui.handleInput("l");
137
166
  assert.strictEqual(activeTab(ui), "Footer");
@@ -150,6 +179,8 @@ it("h switches to the previous tab and wraps from the first tab to the last", ()
150
179
  ui.handleInput("h");
151
180
  assert.strictEqual(activeTab(ui), "Icons");
152
181
  ui.handleInput("h");
182
+ assert.strictEqual(activeTab(ui), "Compaction");
183
+ ui.handleInput("h");
153
184
  assert.strictEqual(activeTab(ui), "General");
154
185
  });
155
186
 
@@ -157,7 +188,7 @@ it("remembers the selected item per tab when navigating with h/l", () => {
157
188
  const { ui } = makeUi();
158
189
  ui.handleInput("j"); // features: Vim mode
159
190
  ui.handleInput("j"); // features: Language
160
- ui.handleInput("l"); // → Icons
191
+ ui.handleInput("l"); // → Compaction
161
192
  ui.handleInput("h"); // → back to features
162
193
  assert.strictEqual(selectedShows(ui, "Language"), true);
163
194
  });
@@ -177,7 +208,7 @@ it("the arrow keys still move the selection", () => {
177
208
  it("Tab and Shift+Tab still switch tabs", () => {
178
209
  const { ui } = makeUi();
179
210
  ui.handleInput("\t");
180
- assert.strictEqual(activeTab(ui), "Icons");
211
+ assert.strictEqual(activeTab(ui), "Compaction");
181
212
  ui.handleInput("\x1b[Z");
182
213
  assert.strictEqual(activeTab(ui), "General");
183
214
  });
@@ -185,6 +216,7 @@ it("Tab and Shift+Tab still switch tabs", () => {
185
216
  it("j and k work on tabs with more items than fit the visible list", () => {
186
217
  const { ui } = makeUi();
187
218
  ui.handleInput("l");
219
+ ui.handleInput("l");
188
220
  ui.handleInput("l"); // → Footer (9 items)
189
221
  ui.handleInput("k"); // wrap to the bottom of the segments list
190
222
  assert.strictEqual(selectedShows(ui, "Extension status line"), true);
@@ -216,3 +248,315 @@ it("unrelated letters are ignored", () => {
216
248
  assert.strictEqual(closes, 0);
217
249
  assert.strictEqual(selectedShows(ui, "Enabled"), true);
218
250
  });
251
+
252
+ // ---------------------------------------------------------------------------
253
+ // Compaction tab: gated on the tracker extension, five rows
254
+ // ---------------------------------------------------------------------------
255
+
256
+ /** Switch from the General tab to the Compaction tab. */
257
+ const goToCompaction = (ui: SettingsUiHandle): void => {
258
+ ui.handleInput("\t");
259
+ };
260
+
261
+ type CompactionRow = "toggle" | "classifier" | "probability" | "answerConfidence" | "cap";
262
+ const ROW_OFFSET: Record<CompactionRow, number> = {
263
+ toggle: 0,
264
+ classifier: 1,
265
+ probability: 2,
266
+ answerConfidence: 3,
267
+ cap: 4,
268
+ };
269
+
270
+ /** Switch to the Compaction tab and move to the named row. */
271
+ const selectCompactionRow = (ui: SettingsUiHandle, row: CompactionRow): void => {
272
+ goToCompaction(ui);
273
+ for (let step = 0; step < ROW_OFFSET[row]; step += 1) ui.handleInput("j");
274
+ };
275
+
276
+ it("the Compaction tab is hidden when the tracker extension is not loaded", () => {
277
+ const { ui } = makeUi({}, defaultChoices, false);
278
+ assert.strictEqual(activeTab(ui), "General");
279
+ ui.handleInput("l");
280
+ assert.strictEqual(activeTab(ui), "Icons");
281
+ assert.strictEqual(
282
+ rendered(ui).some((line) => line.includes("Smart compaction")),
283
+ false,
284
+ );
285
+ });
286
+
287
+ it("the Compaction tab shows the toggle, classifier, two probability knobs, and cap", () => {
288
+ const { ui } = makeUi();
289
+ goToCompaction(ui);
290
+ const lines = rendered(ui);
291
+ for (const label of [
292
+ "Smart compaction",
293
+ "Compaction classifier",
294
+ "Needs-context probability",
295
+ "Answer confidence floor",
296
+ "Max candidates",
297
+ ]) {
298
+ assert.strictEqual(
299
+ lines.some((line) => line.includes(label)),
300
+ true,
301
+ );
302
+ }
303
+ });
304
+
305
+ it("pressing enter toggles smart compaction On -> Off -> On", () => {
306
+ const { ui, changes } = makeUi();
307
+ selectCompactionRow(ui, "toggle");
308
+ assert.strictEqual(selectedShows(ui, "Smart compaction"), true);
309
+
310
+ ui.handleInput("\r");
311
+ assert.strictEqual(changes.at(-1)?.smartCompaction.enabled, false);
312
+ ui.handleInput("\r");
313
+ assert.strictEqual(changes.at(-1)?.smartCompaction.enabled, true);
314
+ assert.deepStrictEqual(
315
+ changes.map((config) => config.smartCompaction.enabled),
316
+ [false, true],
317
+ );
318
+ });
319
+
320
+ it("cycling the classifier row advances to the next enumerated choice", () => {
321
+ const { ui, changes } = makeUi();
322
+ selectCompactionRow(ui, "classifier");
323
+
324
+ ui.handleInput("\r");
325
+
326
+ assert.strictEqual(changes.length, 1);
327
+ assert.strictEqual(changes[0]!.smartCompaction.classifier, "typesafe/jev-latest");
328
+ });
329
+
330
+ it("cycling past the last classifier choice returns to Automatic", () => {
331
+ const { ui, changes } = makeUi({
332
+ smartCompaction: {
333
+ ...DEFAULT_CONFIG.smartCompaction,
334
+ classifier: "cloudflare-workers-ai/@cf/cloudflare/clef-flash",
335
+ },
336
+ });
337
+ selectCompactionRow(ui, "classifier");
338
+
339
+ ui.handleInput("\r");
340
+
341
+ assert.strictEqual(changes.at(-1)?.smartCompaction.classifier, null);
342
+ });
343
+
344
+ it("the classifier row shows the label of the chosen model", () => {
345
+ const { ui } = makeUi({
346
+ smartCompaction: { ...DEFAULT_CONFIG.smartCompaction, classifier: "typesafe/jev-latest" },
347
+ });
348
+ selectCompactionRow(ui, "classifier");
349
+ const lines = rendered(ui);
350
+ assert.strictEqual(
351
+ lines.some((line) => line.includes("Compaction classifier") && line.includes("Safe")),
352
+ true,
353
+ );
354
+ });
355
+
356
+ it("the Compaction tab adds exactly the five smart-compaction rows", () => {
357
+ const { ui } = makeUi();
358
+ selectCompactionRow(ui, "cap");
359
+ assert.strictEqual(selectedShows(ui, "Max candidates"), true);
360
+ ui.handleInput("j"); // wraps to the first item, proving no extra row follows
361
+ assert.strictEqual(selectedShows(ui, "Smart compaction"), true);
362
+ });
363
+
364
+ it("the needs-context probability row shows the current value with two decimals", () => {
365
+ const { ui } = makeUi({
366
+ smartCompaction: { ...DEFAULT_CONFIG.smartCompaction, needsContextProbabilityThreshold: 0.7 },
367
+ });
368
+ goToCompaction(ui);
369
+ assert.strictEqual(
370
+ rendered(ui).some(
371
+ (line) => line.includes("Needs-context probability") && line.includes("0.70"),
372
+ ),
373
+ true,
374
+ );
375
+ });
376
+
377
+ it("Enter on the needs-context probability row steps the value up by 0.05", () => {
378
+ const { ui, changes } = makeUi();
379
+ selectCompactionRow(ui, "probability");
380
+ assert.strictEqual(selectedShows(ui, "Needs-context probability"), true);
381
+
382
+ ui.handleInput("\r");
383
+
384
+ assert.strictEqual(changes.length, 1);
385
+ assert.strictEqual(changes[0]!.smartCompaction.needsContextProbabilityThreshold, 0.55);
386
+ });
387
+
388
+ it("the +/- keys adjust the needs-context probability row and clamp inside the open interval", () => {
389
+ const { ui, changes } = makeUi({
390
+ smartCompaction: { ...DEFAULT_CONFIG.smartCompaction, needsContextProbabilityThreshold: 0.95 },
391
+ });
392
+ selectCompactionRow(ui, "probability");
393
+
394
+ ui.handleInput("+");
395
+ assert.strictEqual(changes.at(-1)?.smartCompaction.needsContextProbabilityThreshold, 0.95);
396
+ ui.handleInput("-");
397
+ assert.strictEqual(changes.at(-1)?.smartCompaction.needsContextProbabilityThreshold, 0.9);
398
+ });
399
+
400
+ it("the answer confidence floor row shows the current value with two decimals", () => {
401
+ const { ui } = makeUi({
402
+ smartCompaction: { ...DEFAULT_CONFIG.smartCompaction, minAnswerConfidence: 0.7 },
403
+ });
404
+ goToCompaction(ui);
405
+ assert.strictEqual(
406
+ rendered(ui).some((line) => line.includes("Answer confidence floor") && line.includes("0.70")),
407
+ true,
408
+ );
409
+ });
410
+
411
+ it("Enter on the answer confidence floor row steps the value up by 0.05", () => {
412
+ const { ui, changes } = makeUi();
413
+ selectCompactionRow(ui, "answerConfidence");
414
+ assert.strictEqual(selectedShows(ui, "Answer confidence floor"), true);
415
+
416
+ ui.handleInput("\r");
417
+
418
+ assert.strictEqual(changes.length, 1);
419
+ assert.strictEqual(changes[0]!.smartCompaction.minAnswerConfidence, 0.55);
420
+ });
421
+
422
+ it("the +/- keys adjust the answer confidence floor row and clamp inside the open interval", () => {
423
+ const { ui, changes } = makeUi({
424
+ smartCompaction: { ...DEFAULT_CONFIG.smartCompaction, minAnswerConfidence: 0.95 },
425
+ });
426
+ selectCompactionRow(ui, "answerConfidence");
427
+
428
+ ui.handleInput("+");
429
+ assert.strictEqual(changes.at(-1)?.smartCompaction.minAnswerConfidence, 0.95);
430
+ ui.handleInput("-");
431
+ assert.strictEqual(changes.at(-1)?.smartCompaction.minAnswerConfidence, 0.9);
432
+ });
433
+
434
+ it("the cap row shows the integer value", () => {
435
+ const { ui } = makeUi({
436
+ smartCompaction: { ...DEFAULT_CONFIG.smartCompaction, maxCandidates: 3 },
437
+ });
438
+ goToCompaction(ui);
439
+ assert.strictEqual(
440
+ rendered(ui).some((line) => line.includes("Max candidates") && line.includes("3")),
441
+ true,
442
+ );
443
+ });
444
+
445
+ it("Enter on the cap row steps the value up by one", () => {
446
+ const { ui, changes } = makeUi();
447
+ selectCompactionRow(ui, "cap");
448
+
449
+ ui.handleInput("\r");
450
+
451
+ assert.strictEqual(changes.length, 1);
452
+ assert.strictEqual(changes[0]!.smartCompaction.maxCandidates, 9);
453
+ });
454
+
455
+ it("the +/- keys adjust the cap row and clamp to the bounds", () => {
456
+ const { ui, changes } = makeUi({
457
+ smartCompaction: { ...DEFAULT_CONFIG.smartCompaction, maxCandidates: 20 },
458
+ });
459
+ selectCompactionRow(ui, "cap");
460
+
461
+ ui.handleInput("+");
462
+ assert.strictEqual(changes.at(-1)?.smartCompaction.maxCandidates, 20);
463
+ ui.handleInput("-");
464
+ assert.strictEqual(changes.at(-1)?.smartCompaction.maxCandidates, 19);
465
+ });
466
+
467
+ // ---------------------------------------------------------------------------
468
+ // hasTrackerExtension: the gate itself
469
+ // ---------------------------------------------------------------------------
470
+
471
+ const probe = (
472
+ tools: readonly string[],
473
+ commands: readonly { name: string; source: string }[],
474
+ ): {
475
+ getAllTools: () => readonly { name: string }[];
476
+ getCommands: () => readonly { name: string; source: string }[];
477
+ } => ({
478
+ getAllTools: () => tools.map((name) => ({ name })),
479
+ getCommands: () => commands,
480
+ });
481
+
482
+ it("hasTrackerExtension is true when the tracker tool is registered", () => {
483
+ assert.strictEqual(hasTrackerExtension(probe(["read", "tracker"], [])), true);
484
+ });
485
+
486
+ it("hasTrackerExtension is true when the tracker extension command is registered", () => {
487
+ assert.strictEqual(
488
+ hasTrackerExtension(probe([], [{ name: "tracker", source: "extension" }])),
489
+ true,
490
+ );
491
+ });
492
+
493
+ it("hasTrackerExtension ignores a non-extension command named tracker", () => {
494
+ assert.strictEqual(
495
+ hasTrackerExtension(probe([], [{ name: "tracker", source: "prompt" }])),
496
+ false,
497
+ );
498
+ });
499
+
500
+ it("hasTrackerExtension is false when the tracker is not loaded", () => {
501
+ assert.strictEqual(
502
+ hasTrackerExtension(probe(["read", "bash"], [{ name: "tui", source: "extension" }])),
503
+ false,
504
+ );
505
+ });
506
+
507
+ // ---------------------------------------------------------------------------
508
+ // loadClassifierChoices: credential-available models, else the catalog
509
+ // ---------------------------------------------------------------------------
510
+
511
+ const descriptor = (provider: string, id: string, name: string): ClassifierDescriptor => ({
512
+ provider,
513
+ id,
514
+ name,
515
+ });
516
+
517
+ const catalogModels: readonly ClassifierDescriptor[] = [
518
+ descriptor("cloudflare-workers-ai", "@cf/cloudflare/clef-flash", "Clef Flash"),
519
+ descriptor("typesafe", "jev-latest", "Jev"),
520
+ descriptor("other", "alpha", "Alpha"),
521
+ descriptor("other", "beta", "Beta"),
522
+ ];
523
+
524
+ const source = (available: readonly ClassifierDescriptor[]): ClassifierSource => ({
525
+ getAvailableOfType: async () => available,
526
+ getModelsOfType: () => catalogModels,
527
+ });
528
+
529
+ it("loadClassifierChoices prefers credential-available classifiers", async () => {
530
+ const choices = await loadClassifierChoices(source([catalogModels[0]!, catalogModels[1]!]));
531
+ assert.deepStrictEqual(
532
+ choices.map((choice) => choice.value),
533
+ [null, "cloudflare-workers-ai/@cf/cloudflare/clef-flash", "typesafe/jev-latest"],
534
+ );
535
+ assert.strictEqual(choices[0]!.label, "Automatic (prefer Clef Flash, then Jev)");
536
+ });
537
+
538
+ it("loadClassifierChoices falls back to the full catalog when none are available", async () => {
539
+ const choices = await loadClassifierChoices(source([]));
540
+ assert.strictEqual(choices.length, catalogModels.length + 1);
541
+ assert.deepStrictEqual(
542
+ choices.map((choice) => choice.value),
543
+ [
544
+ null,
545
+ "cloudflare-workers-ai/@cf/cloudflare/clef-flash",
546
+ "typesafe/jev-latest",
547
+ "other/alpha",
548
+ "other/beta",
549
+ ],
550
+ );
551
+ });
552
+
553
+ it("loadClassifierChoices survives a discovery failure", async () => {
554
+ const failing: ClassifierSource = {
555
+ getAvailableOfType: async () => {
556
+ throw new Error("no credentials");
557
+ },
558
+ getModelsOfType: () => catalogModels,
559
+ };
560
+ const choices = await loadClassifierChoices(failing);
561
+ assert.strictEqual(choices.length, catalogModels.length + 1);
562
+ });