@specode/pi-subscription-usage 1.0.2 → 1.1.1

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 CHANGED
@@ -85,10 +85,10 @@ Before redeeming a Codex reset credit, the extension:
85
85
 
86
86
  The extension publishes two status layers:
87
87
 
88
- - A plain `setStatus` string without provider names or icons, such as `5h 99% · 1w 85% · 1m 60%`.
88
+ - A plain `setStatus` string without provider names or icons, such as `5h 99% ↻2h13m · 1w 85% ↻3d4h · 1m 60%`. `↻` marks the countdown to each window's reset, shown only when the provider reports a future reset time. The footer refreshes every 5 minutes and after each agent turn, so the countdown can lag by up to about 5 minutes.
89
89
  - Structured window data through the `subscription-usage/status/v1` event.
90
90
 
91
- Windows are always ordered as `5h / 1w / 1m / other`. Other extensions can consume the structured event to provide their own icons, colors, and layout without parsing display text. Ready events include `displayMode`; each window includes `displayPercent`, `remainingPercent`, and `usedPercent`. Consumers should render `displayPercent` while using the explicit remaining/used fields for semantic decisions such as colors or alerts.
91
+ Windows are always ordered as `5h / 1w / 1m / other`. Other extensions can consume the structured event to provide their own icons, colors, and layout without parsing display text. Ready events include `displayMode`; each window includes `displayPercent`, `remainingPercent`, and `usedPercent`. Consumers should render `displayPercent` while using the explicit remaining/used fields for semantic decisions such as colors or alerts. When the provider reports a future reset time, a window also carries `resetCountdown` (for example `2h13m`), computed at publish time with the same format and refresh cadence as the footer text; consumers can show it as-is instead of formatting `resetsAt` themselves.
92
92
 
93
93
  ## Security boundaries
94
94
 
package/README.zh-CN.md CHANGED
@@ -85,10 +85,10 @@ Codex 结果会按以下额度域分组:
85
85
 
86
86
  本扩展提供两层状态输出:
87
87
 
88
- - 不含提供商名称或图标的普通 `setStatus` 文本,例如 `5h 99% · 1w 85% · 1m 60%`。
88
+ - 不含提供商名称或图标的普通 `setStatus` 文本,例如 `5h 99% ↻2h13m · 1w 85% ↻3d4h · 1m 60%`。`↻` 后是距离该窗口重置的倒计时,仅在提供商返回未来的重置时间时显示。底部状态每 5 分钟及每轮 agent 结束后刷新,因此倒计时最多可能滞后约 5 分钟。
89
89
  - 通过 `subscription-usage/status/v1` 事件发布的结构化窗口数据。
90
90
 
91
- 窗口始终按 `5h / 1w / 1m / other` 排序。其他扩展可以直接消费结构化事件,自定义图标、颜色和布局,而不必解析显示文本。就绪事件包含 `displayMode`,每个窗口包含 `displayPercent`、`remainingPercent` 和 `usedPercent`;消费者应展示 `displayPercent`,并在颜色或告警等语义判断中使用明确的剩余/已用字段。
91
+ 窗口始终按 `5h / 1w / 1m / other` 排序。其他扩展可以直接消费结构化事件,自定义图标、颜色和布局,而不必解析显示文本。就绪事件包含 `displayMode`,每个窗口包含 `displayPercent`、`remainingPercent` 和 `usedPercent`;消费者应展示 `displayPercent`,并在颜色或告警等语义判断中使用明确的剩余/已用字段。提供商返回未来的重置时间时,窗口还包含 `resetCountdown`(例如 `2h13m`),在发布事件时计算,格式和刷新频率与底部状态文本一致;消费者可直接展示,无需自行根据 `resetsAt` 格式化。
92
92
 
93
93
  ## 安全边界
94
94
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@specode/pi-subscription-usage",
3
- "version": "1.0.2",
3
+ "version": "1.1.1",
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/status.ts CHANGED
@@ -27,6 +27,8 @@ export interface UsageStatusWindow {
27
27
  displayPercent: number;
28
28
  windowMinutes?: number;
29
29
  resetsAt?: number;
30
+ /** Countdown to `resetsAt` at publish time, such as `2h13m`; absent once the reset has passed. */
31
+ resetCountdown?: string;
30
32
  }
31
33
 
32
34
  export type UsageStatusEvent =
@@ -51,6 +53,7 @@ export function buildUsageStatusEvent(
51
53
  report: UsageReport,
52
54
  model?: UsageModel,
53
55
  displayMode: UsageDisplayMode = DEFAULT_USAGE_DISPLAY_MODE,
56
+ nowMs: number = Date.now(),
54
57
  ): UsageStatusEvent {
55
58
  const buckets = bucketsForModel(report, model);
56
59
  const windows = sortUsageBuckets(buckets).flatMap((bucket) => {
@@ -64,6 +67,10 @@ export function buildUsageStatusEvent(
64
67
  ) {
65
68
  return [];
66
69
  }
70
+ const resetCountdown =
71
+ bucket.resetsAt === undefined
72
+ ? undefined
73
+ : formatResetCountdown(bucket.resetsAt * 1_000 - nowMs);
67
74
  return [
68
75
  {
69
76
  kind: usageWindowKind(bucket),
@@ -75,6 +82,7 @@ export function buildUsageStatusEvent(
75
82
  ? {}
76
83
  : { windowMinutes: bucket.windowMinutes }),
77
84
  ...(bucket.resetsAt === undefined ? {} : { resetsAt: bucket.resetsAt }),
85
+ ...(resetCountdown === undefined ? {} : { resetCountdown }),
78
86
  } satisfies UsageStatusWindow,
79
87
  ];
80
88
  });
@@ -93,16 +101,33 @@ export function formatUsageStatusline(
93
101
  report: UsageReport,
94
102
  model?: UsageModel,
95
103
  displayMode: UsageDisplayMode = DEFAULT_USAGE_DISPLAY_MODE,
104
+ nowMs: number = Date.now(),
96
105
  ): string | undefined {
97
- const event = buildUsageStatusEvent(report, model, displayMode);
106
+ const event = buildUsageStatusEvent(report, model, displayMode, nowMs);
98
107
  if (event.status !== "ready" || event.windows.length === 0) return undefined;
99
108
  return event.windows
100
- .map(
101
- (window) => `${window.label} ${formatUsagePercent(window.displayPercent)}%`,
102
- )
109
+ .map((window) => {
110
+ const percent = `${window.label} ${formatUsagePercent(window.displayPercent)}%`;
111
+ return window.resetCountdown
112
+ ? `${percent} ↻${window.resetCountdown}`
113
+ : percent;
114
+ })
103
115
  .join(" · ");
104
116
  }
105
117
 
118
+ /** Compact two-unit countdown such as `3d4h`, `2h13m`, or `<1m`; past resets yield nothing. */
119
+ export function formatResetCountdown(remainingMs: number): string | undefined {
120
+ if (!Number.isFinite(remainingMs) || remainingMs <= 0) return undefined;
121
+ const totalMinutes = Math.floor(remainingMs / 60_000);
122
+ if (totalMinutes < 1) return "<1m";
123
+ const days = Math.floor(totalMinutes / 1_440);
124
+ const hours = Math.floor((totalMinutes % 1_440) / 60);
125
+ const minutes = totalMinutes % 60;
126
+ if (days > 0) return hours > 0 ? `${days}d${hours}h` : `${days}d`;
127
+ if (hours > 0) return minutes > 0 ? `${hours}h${minutes}m` : `${hours}h`;
128
+ return `${minutes}m`;
129
+ }
130
+
106
131
  export function usageWindowLabel(bucket: UsageBucket): string {
107
132
  const kind = usageWindowKind(bucket);
108
133
  if (kind === "weekly") return "1w Window";
package/src/usage.ts CHANGED
@@ -153,10 +153,16 @@ export default function subscriptionUsage(pi: ExtensionAPI): void {
153
153
  }
154
154
  return;
155
155
  }
156
+ const nowMs = Date.now();
156
157
  emitUsageStatus(
157
- buildUsageStatusEvent(outcome.state.report, model, displayMode),
158
+ buildUsageStatusEvent(outcome.state.report, model, displayMode, nowMs),
159
+ );
160
+ const value = formatUsageStatusline(
161
+ outcome.state.report,
162
+ model,
163
+ displayMode,
164
+ nowMs,
158
165
  );
159
- const value = formatUsageStatusline(outcome.state.report, model, displayMode);
160
166
  if (!safeSetStatus(ctx, value)) return;
161
167
  if (schedule && sessionActive) scheduleStatusRefresh(ctx, model);
162
168
  }