@bismawy/pi-arnative 0.11.1 → 0.13.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.
@@ -86,6 +86,12 @@ export function setBudgetEnforce(on: boolean): void {
86
86
  saveChoice(BUDGET_ENFORCE_KEY, on ? "on" : "off");
87
87
  }
88
88
 
89
+ export function resetBudget(): void {
90
+ setBudgetDay(undefined);
91
+ setBudgetMonth(undefined);
92
+ setBudgetEnforce(false);
93
+ }
94
+
89
95
  /** The next cap choice after `current` (unset reads as 0); past the largest choice it wraps to off. */
90
96
  export function nextCap(current: number | undefined): number {
91
97
  const cur = current ?? 0;
@@ -188,7 +194,7 @@ export function budgetLevelNow(): BudgetLevel {
188
194
  const BUDGET_PICKER_TEXT: ListPickerText = {
189
195
  title: screenTitle("Budget"),
190
196
  description: "Warn-only caps on the UTC day and the rolling 30d — enforce on blocks the next turn.",
191
- legend: "[↑↓] Move [Space] Change [Esc] Back",
197
+ legend: "[↑↓] Move [Space] Change [R] Reset [Esc] Back",
192
198
  };
193
199
 
194
200
  const ENFORCE_ON_NOTE = "on — blocks the next turn over a cap";
@@ -275,7 +281,7 @@ export function budgetWindows(spend: BudgetSpend | null, caps: { day?: number; m
275
281
  class BudgetModalComponent implements Component {
276
282
  private readonly theme: { fg(c: string, t: string): string; bold?(t: string): string };
277
283
  private readonly rows: ListRow[];
278
- private readonly hooks: { change(index: number): void; done(back: boolean): void; requestRender(): void };
284
+ private readonly hooks: { change(index: number): void; reset(): void; done(back: boolean): void; requestRender(): void };
279
285
  private readonly text: ListPickerText;
280
286
  private readonly edge: (s: string) => string;
281
287
  private selected = 0;
@@ -283,7 +289,7 @@ class BudgetModalComponent implements Component {
283
289
  constructor(
284
290
  theme: { fg(c: string, t: string): string; bold?(t: string): string },
285
291
  rows: ListRow[],
286
- hooks: { change(index: number): void; done(back: boolean): void; requestRender(): void },
292
+ hooks: { change(index: number): void; reset(): void; done(back: boolean): void; requestRender(): void },
287
293
  text: ListPickerText,
288
294
  thinkingLevel?: string,
289
295
  ) {
@@ -324,6 +330,11 @@ class BudgetModalComponent implements Component {
324
330
  if (matchesKey(keyData, "space")) {
325
331
  this.hooks.change(this.selected);
326
332
  this.hooks.requestRender();
333
+ return;
334
+ }
335
+ if (keyData === "R" || matchesKey(keyData, "r")) {
336
+ this.hooks.reset();
337
+ this.hooks.requestRender();
327
338
  }
328
339
  }
329
340
 
@@ -346,7 +357,7 @@ class BudgetModalComponent implements Component {
346
357
  const capText = (w: BudgetWindow): string => (w.cap !== undefined && w.cap > 0 ? `${formatCost(w.total)} of ${formatCost(w.cap)}` : "");
347
358
  const partW = Math.max(0, ...windows.map((w) => visibleWidth(capText(w))));
348
359
  for (const w of windows) {
349
- const label = th.fg("text", w.label.padEnd(5));
360
+ const label = dim(w.label.padEnd(5));
350
361
  if (w.unknown) {
351
362
  body.push(`${label} · ${dim("unknown — no day buckets yet")}`);
352
363
  continue;
@@ -364,7 +375,7 @@ class BudgetModalComponent implements Component {
364
375
  // The estimated part of both windows, once: an estimate-only provider shows $0 billed
365
376
  // per window, so the total is the only place the guess is summed.
366
377
  const estimated = (spend?.estimatedToday ?? 0) + (spend?.estimatedMonth ?? 0);
367
- if (estimated > 0) body.push(`${dim(`Total · ${formatCost(estimated)} est.`)}`);
378
+ if (estimated > 0) body.push(`${dim("Total")} · ${th.fg("text", `${formatCost(estimated)} est.`)}`);
368
379
  body.push("");
369
380
 
370
381
  // Editable rows — the same rows the headless picker offers.
@@ -405,6 +416,11 @@ export async function openBudget(ctx: ExtensionCommandContext): Promise<boolean
405
416
  announce(ctx.ui, `Budget: ${rowText(budgetRows()[index]!)}.`);
406
417
  tui.requestRender();
407
418
  },
419
+ reset: () => {
420
+ resetBudget();
421
+ announce(ctx.ui, "Budget reset to defaults.");
422
+ tui.requestRender();
423
+ },
408
424
  done: (back) => done(back),
409
425
  requestRender: () => tui.requestRender(),
410
426
  },
@@ -589,7 +605,7 @@ if (isMain(import.meta.url)) {
589
605
  BUDGET_PICKER_TEXT.description === "Warn-only caps on the UTC day and the rolling 30d — enforce on blocks the next turn.",
590
606
  "the Budget screen description is pinned",
591
607
  );
592
- assert(BUDGET_PICKER_TEXT.legend === "[↑↓] Move [Space] Change [Esc] Back", "the Budget legend is pinned");
608
+ assert(BUDGET_PICKER_TEXT.legend === "[↑↓] Move [Space] Change [R] Reset [Esc] Back", "the Budget legend is pinned");
593
609
  assert(rowText(rows[0]!) === "Day cap · $5.00", "the headless row text is pinned: " + rowText(rows[0]!));
594
610
 
595
611
  // The dashboard: the two spend-vs-cap windows, pure and pinned.
@@ -603,10 +619,11 @@ if (isMain(import.meta.url)) {
603
619
  // The modal render: status bars above the editable rows, through the shared screen frame.
604
620
  const modalTheme = { fg: (_c: string, t: string) => t, bold: (t: string) => t };
605
621
  const changed: number[] = [];
622
+ let resets = 0;
606
623
  const modal = new BudgetModalComponent(
607
624
  modalTheme as never,
608
625
  budgetRows(),
609
- { change: (i) => void changed.push(i), done: () => {}, requestRender: () => {} },
626
+ { change: (i) => void changed.push(i), reset: () => void resets++, done: () => {}, requestRender: () => {} },
610
627
  BUDGET_PICKER_TEXT as never,
611
628
  );
612
629
  const rendered = modal.render(80).map((l) => l.replace(/\x1b\[[0-9;]*m/g, ""));
@@ -618,6 +635,9 @@ if (isMain(import.meta.url)) {
618
635
  modal.handleInput("\x1b[B"); // down
619
636
  modal.handleInput(" "); // space
620
637
  assert(changed.length === 1 && changed[0] === 1, "down+space changes the second row (30d cap), not the first");
638
+ modal.handleInput("R");
639
+ modal.handleInput("r");
640
+ assert(resets === 2, "R/r triggers budget reset");
621
641
  assert(modal.render(40).length > 0, "the modal renders on a narrow terminal");
622
642
 
623
643
  // Both windows capped: the bars share one column however long the spend-of-cap text is.
@@ -633,14 +653,18 @@ if (isMain(import.meta.url)) {
633
653
  // truncate the render.
634
654
  const calls: { slot: string; text: string }[] = [];
635
655
  const slotTheme = { fg: (c: string, t: string) => (calls.push({ slot: c, text: t }), t), bold: (t: string) => t };
636
- const slotModal = new BudgetModalComponent(slotTheme as never, budgetRows(), { change: () => {}, done: () => {}, requestRender: () => {} }, BUDGET_PICKER_TEXT as never);
656
+ const slotModal = new BudgetModalComponent(slotTheme as never, budgetRows(), { change: () => {}, reset: () => {}, done: () => {}, requestRender: () => {} }, BUDGET_PICKER_TEXT as never);
637
657
  slotModal.render(80); // today OVER (120% of $5), 30d OK (60% of $10)
658
+ assert(calls.find((c) => c.text === "Today")?.slot === "dim", "Today label uses dim");
659
+ assert(calls.find((c) => c.text === "30d ")?.slot === "dim", "30d label uses dim");
638
660
  assert(calls.find((c) => c.text === "OVER")?.slot === "error", "OVER paints with the theme's error slot");
639
661
  assert(calls.find((c) => c.text === "OK")?.slot === "accent", "OK paints with the theme's accent slot");
640
662
  setBudgetMonth(7); // 30d $6 of $7 = 85% → WARN
641
663
  slotModal.render(80);
642
664
  assert(calls.filter((c) => c.text === "WARN").pop()?.slot === "warning", "WARN paints with the theme's warning slot");
643
- setBudgetMonth(undefined);
665
+ setBudgetEnforce(true);
666
+ resetBudget();
667
+ assert(activeBudgetDay() === undefined && activeBudgetMonth() === undefined && activeBudgetEnforce() === false, "resetBudget clears caps and enforce");
644
668
 
645
669
  // An estimated-cost entry adds the `Total · $X est.` line under the windows.
646
670
  writeFileSync(
@@ -648,8 +672,11 @@ if (isMain(import.meta.url)) {
648
672
  JSON.stringify({ message: { role: "assistant", provider: "google", model: "m", timestamp: Date.now(), usage: { input: 1, output: 1, cost: { total: 2 } } } }) + "\n",
649
673
  );
650
674
  resetUsageCache();
651
- const estRendered = modal.render(80).map((l) => l.replace(/\x1b\[[0-9;]*m/g, ""));
675
+ calls.length = 0;
676
+ const estRendered = slotModal.render(80).map((l) => l.replace(/\x1b\[[0-9;]*m/g, ""));
652
677
  assert(estRendered.some((l) => l.includes("Total · $4.00 est.")), "the estimated total line renders: " + estRendered.filter((l) => l.includes("Total")).join(" | "));
678
+ assert(calls.find((c) => c.text === "Total")?.slot === "dim", "Total label uses dim");
679
+ assert(calls.find((c) => c.text === "$4.00 est.")?.slot === "text", "Total value beside label uses default text color");
653
680
  rmSync(join(fixture, "sessions", "p", "e.jsonl"));
654
681
  resetUsageCache();
655
682