@specode/pi-subscription-usage 1.0.0 → 1.0.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@specode/pi-subscription-usage",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Show subscription quota and Codex reset credits for Codex, OpenCode Go, Grok, and Kimi in Pi",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/display.ts CHANGED
@@ -45,6 +45,11 @@ export function usageAmount(
45
45
  : Math.max(0, bucket.limit - opposite);
46
46
  }
47
47
 
48
+ /** Integer presentation only; keep the original number for semantic decisions. */
49
+ export function formatUsagePercent(value: number): string {
50
+ return String(Math.round(clampPercent(value)));
51
+ }
52
+
48
53
  function clampPercent(value: number): number {
49
54
  return Math.min(100, Math.max(0, value));
50
55
  }
package/src/format.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  // Formatting model adapted from @narumitw/pi-usage@0.53.0 (MIT).
2
2
  import {
3
3
  DEFAULT_USAGE_DISPLAY_MODE,
4
+ formatUsagePercent,
4
5
  usageAmount,
5
6
  usagePercent,
6
7
  type UsageDisplayMode,
@@ -94,7 +95,7 @@ function formatBucket(
94
95
  if (selectedPercent !== undefined) {
95
96
  const filled = Math.round((selectedPercent / 100) * BAR_SEGMENTS);
96
97
  const bar = `${"█".repeat(filled)}${"░".repeat(BAR_SEGMENTS - filled)}`;
97
- const percent = `${String(Math.round(selectedPercent)).padStart(3)}%`;
98
+ const percent = `${formatUsagePercent(selectedPercent).padStart(3)}%`;
98
99
  if (
99
100
  bucket.unit !== "percent" &&
100
101
  bucket.limit !== undefined &&
@@ -68,9 +68,7 @@ function kimiPlanName(root: Record<string, unknown>): string | undefined {
68
68
  if (typeof membership?.level !== "string") return undefined;
69
69
  const level = sanitizeDisplayText(membership.level, MAX_PLAN_LEVEL_LENGTH);
70
70
  if (!level) return undefined;
71
- return Object.hasOwn(KIMI_PLAN_NAMES, level)
72
- ? KIMI_PLAN_NAMES[level]
73
- : level;
71
+ return Object.hasOwn(KIMI_PLAN_NAMES, level) ? KIMI_PLAN_NAMES[level] : level;
74
72
  }
75
73
 
76
74
  function parseQuota(
@@ -79,13 +77,25 @@ function parseQuota(
79
77
  const record = asObject(value);
80
78
  if (!record) return undefined;
81
79
  const limit = asFiniteNumber(record.limit);
82
- const used = asFiniteNumber(record.used);
83
- const remaining = asFiniteNumber(record.remaining);
80
+ let used = asFiniteNumber(record.used);
81
+ let remaining = asFiniteNumber(record.remaining);
82
+ if (limit === undefined || limit <= 0) return undefined;
83
+ // Missing fields can be derived; present-but-invalid data must not become zero.
84
+ if (
85
+ (record.used !== undefined && used === undefined) ||
86
+ (record.remaining !== undefined && remaining === undefined)
87
+ )
88
+ return undefined;
89
+ // Observed: used=limit=100 with remaining omitted. The cause is undocumented.
90
+ if (used === undefined && remaining !== undefined) {
91
+ used = Math.max(0, limit - remaining);
92
+ }
93
+ if (remaining === undefined && used !== undefined) {
94
+ remaining = Math.max(0, limit - used);
95
+ }
84
96
  if (
85
- limit === undefined ||
86
97
  used === undefined ||
87
98
  remaining === undefined ||
88
- limit <= 0 ||
89
99
  used < 0 ||
90
100
  remaining < 0
91
101
  ) {
@@ -133,6 +143,7 @@ function asObject(value: unknown): Record<string, unknown> | undefined {
133
143
 
134
144
  function asFiniteNumber(value: unknown): number | undefined {
135
145
  if (typeof value !== "number" && typeof value !== "string") return undefined;
146
+ if (typeof value === "string" && !value.trim()) return undefined;
136
147
  const parsed = Number(value);
137
148
  return Number.isFinite(parsed) ? parsed : undefined;
138
149
  }
package/src/status.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  DEFAULT_USAGE_DISPLAY_MODE,
3
+ formatUsagePercent,
3
4
  usagePercent,
4
5
  usagePercentRemaining,
5
6
  usagePercentUsed,
@@ -97,8 +98,7 @@ export function formatUsageStatusline(
97
98
  if (event.status !== "ready" || event.windows.length === 0) return undefined;
98
99
  return event.windows
99
100
  .map(
100
- (window) =>
101
- `${window.label} ${Math.round(window.displayPercent).toString()}%`,
101
+ (window) => `${window.label} ${formatUsagePercent(window.displayPercent)}%`,
102
102
  )
103
103
  .join(" · ");
104
104
  }