@0xkahi/pi-qol 1.1.2 → 1.1.3
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 +1 -1
- package/src/extensions/custom-footer/footer-component.ts +9 -1
- package/src/extensions/custom-footer/token-stats.ts +20 -11
- package/src/libs/subscription-usage/strategy/anthropic-oauth-usage.strategy.ts +10 -2
- package/src/libs/subscription-usage/strategy/openai-codex-usage.strategy.ts +7 -1
- package/src/libs/subscription-usage/subscription-usage-api.util.ts +2 -0
package/package.json
CHANGED
|
@@ -111,7 +111,7 @@ export class CustomFooterComponent implements Component {
|
|
|
111
111
|
|
|
112
112
|
private renderStatsLine(width: number, config: CustomFooterConfig): string {
|
|
113
113
|
const model = this.deps.ctx.model;
|
|
114
|
-
const usingSubscription = model ? this.
|
|
114
|
+
const usingSubscription = model ? this.isUsingSubscription(model) : false;
|
|
115
115
|
const contextUsage = this.deps.ctx.getContextUsage();
|
|
116
116
|
const contextWindow = contextUsage?.contextWindow ?? model?.contextWindow ?? 0;
|
|
117
117
|
const subscriptionUsageSegment = this.renderSubscriptionUsageSegment(config, usingSubscription);
|
|
@@ -158,6 +158,14 @@ export class CustomFooterComponent implements Component {
|
|
|
158
158
|
return `${statsLeft}${' '.repeat(Math.max(0, width - statsLeftWidth - truncatedRightWidth))}${truncatedRight}`;
|
|
159
159
|
}
|
|
160
160
|
|
|
161
|
+
private isUsingSubscription(model: NonNullable<CustomFooterComponentDeps['ctx']['model']>): boolean {
|
|
162
|
+
if (model.provider === 'kimi-coding') return true;
|
|
163
|
+
|
|
164
|
+
return (
|
|
165
|
+
this.deps.ctx.modelRegistry.isUsingOAuth(model) && this.deps.ctx.modelRegistry.getProvider(model.provider)?.auth?.oauth?.isSubscription === true
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
|
|
161
169
|
private buildModelNameSegment(config: CustomFooterConfig): string {
|
|
162
170
|
const model = this.deps.ctx.model;
|
|
163
171
|
const modelName = model?.id ?? 'no-model';
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { dye } from '@0xkahi/cli-dye';
|
|
2
|
+
import type { Usage } from '@earendil-works/pi-ai';
|
|
2
3
|
import type { ContextUsage, SessionEntry } from '@earendil-works/pi-coding-agent';
|
|
3
4
|
import { clampPercent, renderProgressBar } from './progress-bar';
|
|
4
5
|
import type { CustomFooterColors, CustomFooterDisplay, CustomFooterIcons, FooterTheme, SupportedProvider, UsageTotals } from './types';
|
|
@@ -40,22 +41,30 @@ export function calculateUsageTotals(entries: readonly SessionEntry[]): UsageTot
|
|
|
40
41
|
};
|
|
41
42
|
|
|
42
43
|
for (const entry of entries) {
|
|
43
|
-
if (entry.type
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
44
|
+
if (entry.type === 'message' && entry.message.role === 'assistant') {
|
|
45
|
+
const usage = entry.message.usage;
|
|
46
|
+
addUsageToTotals(totals, usage);
|
|
47
|
+
|
|
48
|
+
const latestPromptTokens = usage.input + usage.cacheRead + usage.cacheWrite;
|
|
49
|
+
totals.latestCacheHitRate = latestPromptTokens > 0 ? (usage.cacheRead / latestPromptTokens) * 100 : undefined;
|
|
50
|
+
} else if (entry.type === 'message' && entry.message.role === 'toolResult' && entry.message.usage) {
|
|
51
|
+
addUsageToTotals(totals, entry.message.usage);
|
|
52
|
+
} else if ((entry.type === 'usage' || entry.type === 'branch_summary' || entry.type === 'compaction') && entry.usage) {
|
|
53
|
+
addUsageToTotals(totals, entry.usage);
|
|
54
|
+
}
|
|
54
55
|
}
|
|
55
56
|
|
|
56
57
|
return totals;
|
|
57
58
|
}
|
|
58
59
|
|
|
60
|
+
function addUsageToTotals(totals: UsageTotals, usage: Usage): void {
|
|
61
|
+
totals.totalInput += usage.input;
|
|
62
|
+
totals.totalOutput += usage.output;
|
|
63
|
+
totals.totalCacheRead += usage.cacheRead;
|
|
64
|
+
totals.totalCacheWrite += usage.cacheWrite;
|
|
65
|
+
totals.totalCost += usage.cost.total;
|
|
66
|
+
}
|
|
67
|
+
|
|
59
68
|
export function buildSubscriptionUsageSegment({
|
|
60
69
|
colors,
|
|
61
70
|
icons,
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { RawDataParser } from '../../../utils/raw-data-parser.util';
|
|
2
|
-
import
|
|
2
|
+
import {
|
|
3
|
+
type ProviderAuth,
|
|
4
|
+
type RateWindow,
|
|
5
|
+
SUBSCRIPTION_USAGE_FETCH_TIMEOUT_MS,
|
|
6
|
+
type SubscriptionUsageStrategy,
|
|
7
|
+
} from '../subscription-usage-api.util';
|
|
3
8
|
|
|
4
9
|
export class AnthropicOauthUsageStrategy implements SubscriptionUsageStrategy {
|
|
5
10
|
readonly provider = 'anthropic';
|
|
@@ -7,6 +12,8 @@ export class AnthropicOauthUsageStrategy implements SubscriptionUsageStrategy {
|
|
|
7
12
|
|
|
8
13
|
async fetchUsage(auth: ProviderAuth) {
|
|
9
14
|
const response = await fetch(this.request(auth));
|
|
15
|
+
if (!response.ok) return;
|
|
16
|
+
|
|
10
17
|
const data = RawDataParser.asRecord(await response.json());
|
|
11
18
|
if (!data) return;
|
|
12
19
|
|
|
@@ -26,12 +33,13 @@ export class AnthropicOauthUsageStrategy implements SubscriptionUsageStrategy {
|
|
|
26
33
|
resetAt: resetDate && Number.isFinite(resetDate.getTime()) ? resetDate : undefined,
|
|
27
34
|
});
|
|
28
35
|
}
|
|
29
|
-
return windows;
|
|
36
|
+
return windows.length > 0 ? windows : undefined;
|
|
30
37
|
}
|
|
31
38
|
|
|
32
39
|
private request(opts: ProviderAuth) {
|
|
33
40
|
return new Request('https://api.anthropic.com/api/oauth/usage', {
|
|
34
41
|
method: 'GET',
|
|
42
|
+
signal: AbortSignal.timeout(SUBSCRIPTION_USAGE_FETCH_TIMEOUT_MS),
|
|
35
43
|
headers: {
|
|
36
44
|
Authorization: `Bearer ${opts.token}`,
|
|
37
45
|
'anthropic-beta': 'oauth-2025-04-20',
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { RawDataParser } from '../../../utils/raw-data-parser.util';
|
|
2
|
-
import
|
|
2
|
+
import {
|
|
3
|
+
type ProviderAuth,
|
|
4
|
+
type RateWindow,
|
|
5
|
+
SUBSCRIPTION_USAGE_FETCH_TIMEOUT_MS,
|
|
6
|
+
type SubscriptionUsageStrategy,
|
|
7
|
+
} from '../subscription-usage-api.util';
|
|
3
8
|
|
|
4
9
|
const PRIMARY_WINDOW_FALLBACK_SECONDS = 10800;
|
|
5
10
|
const SECONDARY_WINDOW_FALLBACK_SECONDS = 86400;
|
|
@@ -65,6 +70,7 @@ export class OpenAiCodexUsageStrategy implements SubscriptionUsageStrategy {
|
|
|
65
70
|
private request(opts: ProviderAuth) {
|
|
66
71
|
return new Request('https://chatgpt.com/backend-api/wham/usage', {
|
|
67
72
|
method: 'GET',
|
|
73
|
+
signal: AbortSignal.timeout(SUBSCRIPTION_USAGE_FETCH_TIMEOUT_MS),
|
|
68
74
|
headers: {
|
|
69
75
|
Authorization: `Bearer ${opts.token}`,
|
|
70
76
|
...(opts?.accountId ? { 'ChatGPT-Account-Id': opts.accountId } : {}),
|
|
@@ -2,6 +2,8 @@ import { readFileSync } from 'node:fs';
|
|
|
2
2
|
import { PathUtil } from '../../utils/path.util';
|
|
3
3
|
import { RawDataParser } from '../../utils/raw-data-parser.util';
|
|
4
4
|
|
|
5
|
+
export const SUBSCRIPTION_USAGE_FETCH_TIMEOUT_MS = 10_000;
|
|
6
|
+
|
|
5
7
|
export type ProviderAuth = {
|
|
6
8
|
token: string;
|
|
7
9
|
accountId?: string;
|