@specode/pi-subscription-usage 0.1.1 → 0.2.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 +18 -1
- package/README.zh-CN.md +18 -1
- package/package.json +1 -1
- package/src/config-core.ts +43 -0
- package/src/config.ts +48 -0
- package/src/display.ts +50 -0
- package/src/format.ts +38 -23
- package/src/status.ts +28 -15
- package/src/usage.ts +23 -4
package/README.md
CHANGED
|
@@ -55,6 +55,23 @@ Windows from different domains are never interleaved. Run `/usage` again wheneve
|
|
|
55
55
|
|
|
56
56
|
The reset menu appears only when Codex reports redeemable reset credits. Grok's current API exposes quota windows and natural reset times, but no verified manual-reset endpoint or reset-credit count, so the extension never invents a reset action.
|
|
57
57
|
|
|
58
|
+
## Configuration
|
|
59
|
+
|
|
60
|
+
Create `~/.pi/agent/subscription-usage.json` for a global setting, or `.pi/subscription-usage.json` in a trusted project to override it:
|
|
61
|
+
|
|
62
|
+
```json
|
|
63
|
+
{
|
|
64
|
+
"displayMode": "used"
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
`displayMode` accepts:
|
|
69
|
+
|
|
70
|
+
- `"remaining"` — show quota remaining (default, preserving the existing behavior).
|
|
71
|
+
- `"used"` — show quota consumed.
|
|
72
|
+
|
|
73
|
+
The setting applies to the footer status, `/usage` quota bars, and the structured status event. Run `/reload` after editing the file.
|
|
74
|
+
|
|
58
75
|
## Codex reset safety
|
|
59
76
|
|
|
60
77
|
Before redeeming a Codex reset credit, the extension:
|
|
@@ -71,7 +88,7 @@ The extension publishes two status layers:
|
|
|
71
88
|
- A plain `setStatus` string without provider names or icons, such as `5h 99% · 1w 85% · 1m 60%`.
|
|
72
89
|
- Structured window data through the `subscription-usage/status/v1` event.
|
|
73
90
|
|
|
74
|
-
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.
|
|
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.
|
|
75
92
|
|
|
76
93
|
## Security boundaries
|
|
77
94
|
|
package/README.zh-CN.md
CHANGED
|
@@ -55,6 +55,23 @@ Codex 结果会按以下额度域分组:
|
|
|
55
55
|
|
|
56
56
|
只有当 Codex 返回可兑换的重置次数时,才会显示重置菜单。Grok 当前 API 只公开额度窗口和自然重置时间,没有经过验证的手动重置端点或重置次数,因此本扩展不会虚构重置操作。
|
|
57
57
|
|
|
58
|
+
## 配置
|
|
59
|
+
|
|
60
|
+
可创建 `~/.pi/agent/subscription-usage.json` 作为全局配置,或在受信任项目中创建 `.pi/subscription-usage.json` 覆盖全局配置:
|
|
61
|
+
|
|
62
|
+
```json
|
|
63
|
+
{
|
|
64
|
+
"displayMode": "used"
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
`displayMode` 支持:
|
|
69
|
+
|
|
70
|
+
- `"remaining"`:显示剩余额度(默认值,保持当前行为)。
|
|
71
|
+
- `"used"`:显示已使用额度。
|
|
72
|
+
|
|
73
|
+
该配置同时作用于底部状态、`/usage` 额度条和结构化状态事件。修改配置文件后运行 `/reload`。
|
|
74
|
+
|
|
58
75
|
## Codex 重置安全措施
|
|
59
76
|
|
|
60
77
|
兑换 Codex 重置次数前,本扩展会:
|
|
@@ -71,7 +88,7 @@ Codex 结果会按以下额度域分组:
|
|
|
71
88
|
- 不含提供商名称或图标的普通 `setStatus` 文本,例如 `5h 99% · 1w 85% · 1m 60%`。
|
|
72
89
|
- 通过 `subscription-usage/status/v1` 事件发布的结构化窗口数据。
|
|
73
90
|
|
|
74
|
-
窗口始终按 `5h / 1w / 1m / other`
|
|
91
|
+
窗口始终按 `5h / 1w / 1m / other` 排序。其他扩展可以直接消费结构化事件,自定义图标、颜色和布局,而不必解析显示文本。就绪事件包含 `displayMode`,每个窗口包含 `displayPercent`、`remainingPercent` 和 `usedPercent`;消费者应展示 `displayPercent`,并在颜色或告警等语义判断中使用明确的剩余/已用字段。
|
|
75
92
|
|
|
76
93
|
## 安全边界
|
|
77
94
|
|
package/package.json
CHANGED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_USAGE_DISPLAY_MODE,
|
|
3
|
+
type UsageDisplayMode,
|
|
4
|
+
} from "./display.ts";
|
|
5
|
+
|
|
6
|
+
export interface UsageConfig {
|
|
7
|
+
displayMode: UsageDisplayMode;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface ParsedUsageConfig {
|
|
11
|
+
displayMode?: UsageDisplayMode;
|
|
12
|
+
warning?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const DEFAULT_USAGE_CONFIG: UsageConfig = {
|
|
16
|
+
displayMode: DEFAULT_USAGE_DISPLAY_MODE,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export function resolveUsageConfig(
|
|
20
|
+
globalConfig?: ParsedUsageConfig,
|
|
21
|
+
projectConfig?: ParsedUsageConfig,
|
|
22
|
+
): UsageConfig {
|
|
23
|
+
return {
|
|
24
|
+
displayMode:
|
|
25
|
+
projectConfig?.displayMode ??
|
|
26
|
+
globalConfig?.displayMode ??
|
|
27
|
+
DEFAULT_USAGE_CONFIG.displayMode,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function parseUsageConfig(value: unknown): ParsedUsageConfig {
|
|
32
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
33
|
+
return { warning: "configuration must be a JSON object" };
|
|
34
|
+
}
|
|
35
|
+
const displayMode = (value as Record<string, unknown>).displayMode;
|
|
36
|
+
if (displayMode === undefined) return {};
|
|
37
|
+
if (displayMode !== "remaining" && displayMode !== "used") {
|
|
38
|
+
return {
|
|
39
|
+
warning: 'displayMode must be either "remaining" or "used"',
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
return { displayMode };
|
|
43
|
+
}
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { CONFIG_DIR_NAME, getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
4
|
+
import {
|
|
5
|
+
parseUsageConfig,
|
|
6
|
+
resolveUsageConfig,
|
|
7
|
+
type UsageConfig,
|
|
8
|
+
} from "./config-core.ts";
|
|
9
|
+
|
|
10
|
+
const CONFIG_FILE_NAME = "subscription-usage.json";
|
|
11
|
+
|
|
12
|
+
export interface LoadedUsageConfig extends UsageConfig {
|
|
13
|
+
warnings: string[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function loadUsageConfig(
|
|
17
|
+
cwd: string,
|
|
18
|
+
includeProjectConfig: boolean,
|
|
19
|
+
): LoadedUsageConfig {
|
|
20
|
+
const warnings: string[] = [];
|
|
21
|
+
const globalConfig = readConfigFile(
|
|
22
|
+
join(getAgentDir(), CONFIG_FILE_NAME),
|
|
23
|
+
warnings,
|
|
24
|
+
);
|
|
25
|
+
const projectConfig = includeProjectConfig
|
|
26
|
+
? readConfigFile(join(cwd, CONFIG_DIR_NAME, CONFIG_FILE_NAME), warnings)
|
|
27
|
+
: undefined;
|
|
28
|
+
return {
|
|
29
|
+
...resolveUsageConfig(globalConfig, projectConfig),
|
|
30
|
+
warnings,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function readConfigFile(
|
|
35
|
+
path: string,
|
|
36
|
+
warnings: string[],
|
|
37
|
+
): ReturnType<typeof parseUsageConfig> | undefined {
|
|
38
|
+
if (!existsSync(path)) return undefined;
|
|
39
|
+
try {
|
|
40
|
+
const parsed = parseUsageConfig(JSON.parse(readFileSync(path, "utf8")));
|
|
41
|
+
if (parsed.warning) warnings.push(`${path}: ${parsed.warning}`);
|
|
42
|
+
return parsed;
|
|
43
|
+
} catch (error) {
|
|
44
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
45
|
+
warnings.push(`${path}: ${message}`);
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
}
|
package/src/display.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { UsageBucket } from "./types.ts";
|
|
2
|
+
|
|
3
|
+
export type UsageDisplayMode = "remaining" | "used";
|
|
4
|
+
|
|
5
|
+
export const DEFAULT_USAGE_DISPLAY_MODE: UsageDisplayMode = "remaining";
|
|
6
|
+
|
|
7
|
+
export function usagePercentRemaining(bucket: UsageBucket): number | undefined {
|
|
8
|
+
if (bucket.unit === "percent" && bucket.remaining !== undefined) {
|
|
9
|
+
return clampPercent(bucket.remaining);
|
|
10
|
+
}
|
|
11
|
+
if (!bucket.limit || bucket.remaining === undefined) return undefined;
|
|
12
|
+
return clampPercent((bucket.remaining / bucket.limit) * 100);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function usagePercentUsed(bucket: UsageBucket): number | undefined {
|
|
16
|
+
if (bucket.unit === "percent" && bucket.used !== undefined) {
|
|
17
|
+
return clampPercent(bucket.used);
|
|
18
|
+
}
|
|
19
|
+
if (bucket.limit && bucket.used !== undefined) {
|
|
20
|
+
return clampPercent((bucket.used / bucket.limit) * 100);
|
|
21
|
+
}
|
|
22
|
+
const remaining = usagePercentRemaining(bucket);
|
|
23
|
+
return remaining === undefined ? undefined : 100 - remaining;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function usagePercent(
|
|
27
|
+
bucket: UsageBucket,
|
|
28
|
+
mode: UsageDisplayMode,
|
|
29
|
+
): number | undefined {
|
|
30
|
+
return mode === "used"
|
|
31
|
+
? usagePercentUsed(bucket)
|
|
32
|
+
: usagePercentRemaining(bucket);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function usageAmount(
|
|
36
|
+
bucket: UsageBucket,
|
|
37
|
+
mode: UsageDisplayMode,
|
|
38
|
+
): number | undefined {
|
|
39
|
+
const direct = mode === "used" ? bucket.used : bucket.remaining;
|
|
40
|
+
if (direct !== undefined) return direct;
|
|
41
|
+
if (bucket.limit === undefined) return undefined;
|
|
42
|
+
const opposite = mode === "used" ? bucket.remaining : bucket.used;
|
|
43
|
+
return opposite === undefined
|
|
44
|
+
? undefined
|
|
45
|
+
: Math.max(0, bucket.limit - opposite);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function clampPercent(value: number): number {
|
|
49
|
+
return Math.min(100, Math.max(0, value));
|
|
50
|
+
}
|
package/src/format.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
// Formatting model adapted from @narumitw/pi-usage@0.53.0 (MIT).
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
DEFAULT_USAGE_DISPLAY_MODE,
|
|
4
|
+
usageAmount,
|
|
5
|
+
usagePercent,
|
|
6
|
+
type UsageDisplayMode,
|
|
7
|
+
} from "./display.ts";
|
|
8
|
+
import { sortUsageBuckets, usageWindowLabel } from "./status.ts";
|
|
7
9
|
import type {
|
|
8
10
|
ProviderUsageState,
|
|
9
11
|
UsageBucket,
|
|
@@ -25,8 +27,11 @@ interface PanelSection {
|
|
|
25
27
|
rows: PanelRow[];
|
|
26
28
|
}
|
|
27
29
|
|
|
28
|
-
export function formatUsageReport(
|
|
29
|
-
|
|
30
|
+
export function formatUsageReport(
|
|
31
|
+
report: UsageReport,
|
|
32
|
+
displayMode: UsageDisplayMode = DEFAULT_USAGE_DISPLAY_MODE,
|
|
33
|
+
): string {
|
|
34
|
+
const sections = bucketSections(report.buckets, displayMode);
|
|
30
35
|
const accountRows: PanelRow[] = report.metrics.map((metric) => ({
|
|
31
36
|
label: metricLabel(metric),
|
|
32
37
|
detail: formatMetric(metric),
|
|
@@ -59,47 +64,57 @@ export function formatUsageReport(report: UsageReport): string {
|
|
|
59
64
|
return lines.join("\n");
|
|
60
65
|
}
|
|
61
66
|
|
|
62
|
-
export function formatProviderState(
|
|
63
|
-
|
|
67
|
+
export function formatProviderState(
|
|
68
|
+
state: ProviderUsageState,
|
|
69
|
+
displayMode: UsageDisplayMode = DEFAULT_USAGE_DISPLAY_MODE,
|
|
70
|
+
): string {
|
|
71
|
+
if (state.status === "ready") {
|
|
72
|
+
return formatUsageReport(state.report, displayMode);
|
|
73
|
+
}
|
|
64
74
|
let status = "Query Failed";
|
|
65
75
|
if (state.status === "auth-unavailable") status = "Auth Unavailable";
|
|
66
76
|
else if (state.status === "unsupported") status = "Unsupported";
|
|
67
77
|
return `${state.providerName}\n ${status}: ${state.message}`;
|
|
68
78
|
}
|
|
69
79
|
|
|
70
|
-
function formatBucket(
|
|
71
|
-
|
|
80
|
+
function formatBucket(
|
|
81
|
+
bucket: UsageBucket,
|
|
82
|
+
displayMode: UsageDisplayMode,
|
|
83
|
+
): string {
|
|
84
|
+
const selectedPercent = usagePercent(bucket, displayMode);
|
|
85
|
+
const selectedAmount = usageAmount(bucket, displayMode);
|
|
86
|
+
const qualifier = displayMode === "used" ? "used" : "left";
|
|
72
87
|
const reset = bucket.resetsAt
|
|
73
88
|
? ` · resets ${formatReset(bucket.resetsAt)}`
|
|
74
89
|
: "";
|
|
75
|
-
if (
|
|
76
|
-
const filled = Math.round((
|
|
90
|
+
if (selectedPercent !== undefined) {
|
|
91
|
+
const filled = Math.round((selectedPercent / 100) * BAR_SEGMENTS);
|
|
77
92
|
const bar = `${"█".repeat(filled)}${"░".repeat(BAR_SEGMENTS - filled)}`;
|
|
78
|
-
const percent = `${String(Math.round(
|
|
93
|
+
const percent = `${String(Math.round(selectedPercent)).padStart(3)}%`;
|
|
79
94
|
if (
|
|
80
95
|
bucket.unit !== "percent" &&
|
|
81
96
|
bucket.limit !== undefined &&
|
|
82
97
|
bucket.limit !== 100 && // x/100 duplicates the percent exactly
|
|
83
|
-
|
|
98
|
+
selectedAmount !== undefined
|
|
84
99
|
) {
|
|
85
|
-
return `${bar} ${percent}
|
|
100
|
+
return `${bar} ${percent} ${qualifier} · ${formatNumber(selectedAmount)}/${formatNumber(bucket.limit)}${reset}`;
|
|
86
101
|
}
|
|
87
|
-
return `${bar} ${percent}
|
|
102
|
+
return `${bar} ${percent} ${qualifier}${reset}`;
|
|
88
103
|
}
|
|
89
104
|
const indent = " ".repeat(BAR_SEGMENTS + 2);
|
|
90
|
-
|
|
91
|
-
return `${indent}${formatValue(bucket.remaining, bucket.unit)} left${reset}`;
|
|
92
|
-
}
|
|
93
|
-
return `${indent}${formatValue(bucket.used ?? "n/a", bucket.unit)} used${reset}`;
|
|
105
|
+
return `${indent}${formatValue(selectedAmount ?? "n/a", bucket.unit)} ${qualifier}${reset}`;
|
|
94
106
|
}
|
|
95
107
|
|
|
96
|
-
function bucketSections(
|
|
108
|
+
function bucketSections(
|
|
109
|
+
buckets: readonly UsageBucket[],
|
|
110
|
+
displayMode: UsageDisplayMode,
|
|
111
|
+
): PanelSection[] {
|
|
97
112
|
if (!buckets.some((bucket) => bucket.groupId)) {
|
|
98
113
|
return [
|
|
99
114
|
{
|
|
100
115
|
rows: sortUsageBuckets(buckets).map((bucket) => ({
|
|
101
116
|
label: usageWindowLabel(bucket),
|
|
102
|
-
detail: formatBucket(bucket),
|
|
117
|
+
detail: formatBucket(bucket, displayMode),
|
|
103
118
|
})),
|
|
104
119
|
},
|
|
105
120
|
];
|
|
@@ -130,7 +145,7 @@ function bucketSections(buckets: readonly UsageBucket[]): PanelSection[] {
|
|
|
130
145
|
heading: group.heading,
|
|
131
146
|
rows: sortUsageBuckets(group.buckets).map((bucket) => ({
|
|
132
147
|
label: usageWindowLabel(bucket),
|
|
133
|
-
detail: formatBucket(bucket),
|
|
148
|
+
detail: formatBucket(bucket, displayMode),
|
|
134
149
|
})),
|
|
135
150
|
}));
|
|
136
151
|
}
|
package/src/status.ts
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_USAGE_DISPLAY_MODE,
|
|
3
|
+
usagePercent,
|
|
4
|
+
usagePercentRemaining,
|
|
5
|
+
usagePercentUsed,
|
|
6
|
+
type UsageDisplayMode,
|
|
7
|
+
} from "./display.ts";
|
|
1
8
|
import type { UsageBucket, UsageModel, UsageReport } from "./types.ts";
|
|
2
9
|
|
|
10
|
+
export { usagePercentRemaining, usagePercentUsed } from "./display.ts";
|
|
11
|
+
|
|
3
12
|
export const USAGE_STATUS_EVENT = "subscription-usage/status/v1";
|
|
4
13
|
|
|
5
14
|
export type UsageStatusWindowKind =
|
|
@@ -13,6 +22,8 @@ export interface UsageStatusWindow {
|
|
|
13
22
|
kind: UsageStatusWindowKind;
|
|
14
23
|
label: string;
|
|
15
24
|
remainingPercent: number;
|
|
25
|
+
usedPercent: number;
|
|
26
|
+
displayPercent: number;
|
|
16
27
|
windowMinutes?: number;
|
|
17
28
|
resetsAt?: number;
|
|
18
29
|
}
|
|
@@ -23,6 +34,7 @@ export type UsageStatusEvent =
|
|
|
23
34
|
status: "ready";
|
|
24
35
|
providerId: string;
|
|
25
36
|
capturedAt: number;
|
|
37
|
+
displayMode: UsageDisplayMode;
|
|
26
38
|
windows: UsageStatusWindow[];
|
|
27
39
|
}
|
|
28
40
|
| {
|
|
@@ -37,6 +49,7 @@ export function unavailableUsageStatusEvent(): UsageStatusEvent {
|
|
|
37
49
|
export function buildUsageStatusEvent(
|
|
38
50
|
report: UsageReport,
|
|
39
51
|
model?: UsageModel,
|
|
52
|
+
displayMode: UsageDisplayMode = DEFAULT_USAGE_DISPLAY_MODE,
|
|
40
53
|
): UsageStatusEvent {
|
|
41
54
|
const buckets =
|
|
42
55
|
report.providerId === "openai-codex"
|
|
@@ -44,12 +57,22 @@ export function buildUsageStatusEvent(
|
|
|
44
57
|
: report.buckets;
|
|
45
58
|
const windows = sortUsageBuckets(buckets).flatMap((bucket) => {
|
|
46
59
|
const remainingPercent = usagePercentRemaining(bucket);
|
|
47
|
-
|
|
60
|
+
const usedPercent = usagePercentUsed(bucket);
|
|
61
|
+
const displayPercent = usagePercent(bucket, displayMode);
|
|
62
|
+
if (
|
|
63
|
+
remainingPercent === undefined ||
|
|
64
|
+
usedPercent === undefined ||
|
|
65
|
+
displayPercent === undefined
|
|
66
|
+
) {
|
|
67
|
+
return [];
|
|
68
|
+
}
|
|
48
69
|
return [
|
|
49
70
|
{
|
|
50
71
|
kind: usageWindowKind(bucket),
|
|
51
72
|
label: compactUsageWindowLabel(bucket),
|
|
52
73
|
remainingPercent,
|
|
74
|
+
usedPercent,
|
|
75
|
+
displayPercent,
|
|
53
76
|
...(bucket.windowMinutes === undefined
|
|
54
77
|
? {}
|
|
55
78
|
: { windowMinutes: bucket.windowMinutes }),
|
|
@@ -62,6 +85,7 @@ export function buildUsageStatusEvent(
|
|
|
62
85
|
status: "ready",
|
|
63
86
|
providerId: report.providerId,
|
|
64
87
|
capturedAt: report.capturedAt,
|
|
88
|
+
displayMode,
|
|
65
89
|
windows,
|
|
66
90
|
};
|
|
67
91
|
}
|
|
@@ -70,13 +94,14 @@ export function buildUsageStatusEvent(
|
|
|
70
94
|
export function formatUsageStatusline(
|
|
71
95
|
report: UsageReport,
|
|
72
96
|
model?: UsageModel,
|
|
97
|
+
displayMode: UsageDisplayMode = DEFAULT_USAGE_DISPLAY_MODE,
|
|
73
98
|
): string | undefined {
|
|
74
|
-
const event = buildUsageStatusEvent(report, model);
|
|
99
|
+
const event = buildUsageStatusEvent(report, model, displayMode);
|
|
75
100
|
if (event.status !== "ready" || event.windows.length === 0) return undefined;
|
|
76
101
|
return event.windows
|
|
77
102
|
.map(
|
|
78
103
|
(window) =>
|
|
79
|
-
`${window.label} ${Math.round(window.
|
|
104
|
+
`${window.label} ${Math.round(window.displayPercent).toString()}%`,
|
|
80
105
|
)
|
|
81
106
|
.join(" · ");
|
|
82
107
|
}
|
|
@@ -142,14 +167,6 @@ export function sortUsageBuckets(
|
|
|
142
167
|
});
|
|
143
168
|
}
|
|
144
169
|
|
|
145
|
-
export function usagePercentRemaining(bucket: UsageBucket): number | undefined {
|
|
146
|
-
if (bucket.unit === "percent" && bucket.remaining !== undefined) {
|
|
147
|
-
return clampPercent(bucket.remaining);
|
|
148
|
-
}
|
|
149
|
-
if (!bucket.limit || bucket.remaining === undefined) return undefined;
|
|
150
|
-
return clampPercent((bucket.remaining / bucket.limit) * 100);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
170
|
function usageWindowRank(bucket: UsageBucket): number {
|
|
154
171
|
const kind = usageWindowKind(bucket);
|
|
155
172
|
if (kind === "hourly" || kind === "rolling") return 0;
|
|
@@ -207,7 +224,3 @@ function normalizeKey(value: string | undefined): string | undefined {
|
|
|
207
224
|
.replace(/^-+|-+$/gu, "") || undefined
|
|
208
225
|
);
|
|
209
226
|
}
|
|
210
|
-
|
|
211
|
-
function clampPercent(value: number): number {
|
|
212
|
-
return Math.min(100, Math.max(0, value));
|
|
213
|
-
}
|
package/src/usage.ts
CHANGED
|
@@ -4,6 +4,7 @@ import type {
|
|
|
4
4
|
ExtensionCommandContext,
|
|
5
5
|
ExtensionContext,
|
|
6
6
|
} from "@earendil-works/pi-coding-agent";
|
|
7
|
+
import { loadUsageConfig } from "./config.ts";
|
|
7
8
|
import {
|
|
8
9
|
awaitWithDeadline,
|
|
9
10
|
errorMessage,
|
|
@@ -11,6 +12,10 @@ import {
|
|
|
11
12
|
modelIdentity,
|
|
12
13
|
UsageCache,
|
|
13
14
|
} from "./core.ts";
|
|
15
|
+
import {
|
|
16
|
+
DEFAULT_USAGE_DISPLAY_MODE,
|
|
17
|
+
type UsageDisplayMode,
|
|
18
|
+
} from "./display.ts";
|
|
14
19
|
import {
|
|
15
20
|
CODEX_RESET_CONFIRMATION_OPTIONS,
|
|
16
21
|
codexResetCount,
|
|
@@ -65,6 +70,7 @@ export default function subscriptionUsage(pi: ExtensionAPI): void {
|
|
|
65
70
|
const failureBackoff = new Map<string, { until: number; message: string }>();
|
|
66
71
|
const activeControllers = new Set<AbortController>();
|
|
67
72
|
let sessionActive = false;
|
|
73
|
+
let displayMode: UsageDisplayMode = DEFAULT_USAGE_DISPLAY_MODE;
|
|
68
74
|
let statusGeneration = 0;
|
|
69
75
|
let statusController: AbortController | undefined;
|
|
70
76
|
let statusTimer: ReturnType<typeof setTimeout> | undefined;
|
|
@@ -145,8 +151,10 @@ export default function subscriptionUsage(pi: ExtensionAPI): void {
|
|
|
145
151
|
}
|
|
146
152
|
return;
|
|
147
153
|
}
|
|
148
|
-
emitUsageStatus(
|
|
149
|
-
|
|
154
|
+
emitUsageStatus(
|
|
155
|
+
buildUsageStatusEvent(outcome.state.report, model, displayMode),
|
|
156
|
+
);
|
|
157
|
+
const value = formatUsageStatusline(outcome.state.report, model, displayMode);
|
|
150
158
|
if (!safeSetStatus(ctx, value)) return;
|
|
151
159
|
if (schedule && sessionActive) scheduleStatusRefresh(ctx, model);
|
|
152
160
|
}
|
|
@@ -482,7 +490,10 @@ export default function subscriptionUsage(pi: ExtensionAPI): void {
|
|
|
482
490
|
);
|
|
483
491
|
return;
|
|
484
492
|
}
|
|
485
|
-
ctx.ui.notify(
|
|
493
|
+
ctx.ui.notify(
|
|
494
|
+
formatProviderState(current.outcome.state, displayMode),
|
|
495
|
+
"info",
|
|
496
|
+
);
|
|
486
497
|
if (current.model) {
|
|
487
498
|
publishStatus(ctx, current.outcome, current.model, sessionActive);
|
|
488
499
|
}
|
|
@@ -501,7 +512,10 @@ export default function subscriptionUsage(pi: ExtensionAPI): void {
|
|
|
501
512
|
if (!action) return;
|
|
502
513
|
const refreshed = await redeemCodexReset(ctx, current, controller);
|
|
503
514
|
if (refreshed?.outcome.state.status === "ready") {
|
|
504
|
-
ctx.ui.notify(
|
|
515
|
+
ctx.ui.notify(
|
|
516
|
+
formatProviderState(refreshed.outcome.state, displayMode),
|
|
517
|
+
"info",
|
|
518
|
+
);
|
|
505
519
|
}
|
|
506
520
|
} finally {
|
|
507
521
|
controller.abort();
|
|
@@ -527,6 +541,11 @@ export default function subscriptionUsage(pi: ExtensionAPI): void {
|
|
|
527
541
|
|
|
528
542
|
pi.on("session_start", (_event, ctx) => {
|
|
529
543
|
sessionActive = ctx.hasUI;
|
|
544
|
+
const config = loadUsageConfig(ctx.cwd, ctx.isProjectTrusted());
|
|
545
|
+
displayMode = config.displayMode;
|
|
546
|
+
for (const warning of config.warnings) {
|
|
547
|
+
ctx.ui.notify(`Usage config ignored: ${warning}`, "warning");
|
|
548
|
+
}
|
|
530
549
|
if (ctx.hasUI) startStatusRefresh(ctx, ctx.model, false);
|
|
531
550
|
});
|
|
532
551
|
pi.on("session_tree", (_event, ctx) => {
|