@ohgodtamit/pi-usage 0.1.0-alpha.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/CHANGELOG.md +8 -0
- package/LICENSE +22 -0
- package/README.md +403 -0
- package/THIRD_PARTY_NOTICES.md +54 -0
- package/dist/aggregate.d.ts +392 -0
- package/dist/aggregate.d.ts.map +1 -0
- package/dist/cache.d.ts +27 -0
- package/dist/cache.d.ts.map +1 -0
- package/dist/config.d.ts +38 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/format.d.ts +46 -0
- package/dist/format.d.ts.map +1 -0
- package/dist/freshness.d.ts +42 -0
- package/dist/freshness.d.ts.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/mascot.d.ts +35 -0
- package/dist/mascot.d.ts.map +1 -0
- package/dist/prices.d.ts +24 -0
- package/dist/prices.d.ts.map +1 -0
- package/dist/provider.d.ts +147 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/view.d.ts +207 -0
- package/dist/view.d.ts.map +1 -0
- package/dist/zai.d.ts +57 -0
- package/dist/zai.d.ts.map +1 -0
- package/package.json +60 -0
- package/src/aggregate.ts +1838 -0
- package/src/cache.ts +100 -0
- package/src/config.ts +93 -0
- package/src/format.ts +166 -0
- package/src/freshness.ts +55 -0
- package/src/index.ts +642 -0
- package/src/mascot.ts +227 -0
- package/src/prices.ts +63 -0
- package/src/provider.ts +704 -0
- package/src/view.ts +2585 -0
- package/src/zai.ts +101 -0
package/src/zai.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure ZAI (Zhipu / GLM coding plans) quota-limit classification.
|
|
3
|
+
*
|
|
4
|
+
* Extracted from provider.ts into its own module so it has ZERO pi / pi-ai
|
|
5
|
+
* runtime dependencies and is unit-testable without network or auth. The
|
|
6
|
+
* network layer (provider.ts) imports `classifyZaiLimits` from here.
|
|
7
|
+
*
|
|
8
|
+
* The return shape is structurally compatible with the `planQuota` field on
|
|
9
|
+
* provider.ts's `ProviderQuota` (a subset: ZAI never reports purchased
|
|
10
|
+
* credits, only the 5h/weekly windows + web searches), so it can be returned
|
|
11
|
+
* directly from `fetchZaiPlanQuota`.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** A single quota window in ZAI's /quota/limit response. */
|
|
15
|
+
export interface ZaiQuotaLimit {
|
|
16
|
+
type?: string;
|
|
17
|
+
unit?: number;
|
|
18
|
+
number?: number;
|
|
19
|
+
usage?: number;
|
|
20
|
+
currentValue?: number;
|
|
21
|
+
remaining?: number;
|
|
22
|
+
percentage?: number;
|
|
23
|
+
nextResetTime?: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Provider-native plan quota fragment produced from a ZAI limits array. */
|
|
27
|
+
export interface ZaiPlanQuota {
|
|
28
|
+
plan: string;
|
|
29
|
+
session5h?: { usedPct: number; resetMs: number };
|
|
30
|
+
weekly?: { usedPct: number; resetMs: number };
|
|
31
|
+
webSearches?: { used: number; limit: number; resetMs: number };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Classify a ZAI /quota/limit `limits[]` into the planQuota shape.
|
|
36
|
+
*
|
|
37
|
+
* PURE function (no network). Detection strategy (robust across ZAI plan
|
|
38
|
+
* tiers and regions):
|
|
39
|
+
* 1. Prefer the documented exact identifiers verified on the "max" plan:
|
|
40
|
+
* session 5h = (unit 3, number 5), weekly 7d = (unit 6, number 1).
|
|
41
|
+
* 2. Fall back to positional pairing: ZAI only ever reports these two
|
|
42
|
+
* token windows, so the window that is NOT the session is the weekly.
|
|
43
|
+
* This keeps weekly resolving when other plans/regions encode the window
|
|
44
|
+
* with different unit/number values — the production bug where only the
|
|
45
|
+
* 5h bar appeared for non-"max" accounts.
|
|
46
|
+
* 3. When neither exact code matches, assign by reset-window length: the
|
|
47
|
+
* shorter countdown maps to the 5h session, the longer to weekly.
|
|
48
|
+
*/
|
|
49
|
+
export function classifyZaiLimits(
|
|
50
|
+
limits: ZaiQuotaLimit[],
|
|
51
|
+
level: string,
|
|
52
|
+
): ZaiPlanQuota | undefined {
|
|
53
|
+
const tokLimits = limits.filter((l) => l.type === "TOKENS_LIMIT");
|
|
54
|
+
const web = limits.find((l) => l.type === "TIME_LIMIT");
|
|
55
|
+
|
|
56
|
+
// 1. Documented exact identifiers (verified on the "max" plan).
|
|
57
|
+
const byCode = (unit: number, num: number): ZaiQuotaLimit | undefined =>
|
|
58
|
+
tokLimits.find((l) => l.unit === unit && l.number === num);
|
|
59
|
+
let session = byCode(3, 5);
|
|
60
|
+
let weekly = byCode(6, 1);
|
|
61
|
+
|
|
62
|
+
// 2/3. Positional-pairing fallback for plans whose window codes differ.
|
|
63
|
+
if (tokLimits.length >= 2 && (!session || !weekly)) {
|
|
64
|
+
const known = new Set<ZaiQuotaLimit>();
|
|
65
|
+
if (session) known.add(session);
|
|
66
|
+
if (weekly) known.add(weekly);
|
|
67
|
+
const remaining = tokLimits.filter((l) => !known.has(l));
|
|
68
|
+
if (!session && !weekly) {
|
|
69
|
+
// Neither code matched: sooner reset = 5h session, later = weekly.
|
|
70
|
+
const sorted = [...remaining].sort((a, b) => (a.nextResetTime ?? 0) - (b.nextResetTime ?? 0));
|
|
71
|
+
session = sorted[0];
|
|
72
|
+
weekly = sorted[sorted.length - 1];
|
|
73
|
+
} else if (!weekly && remaining.length > 0) {
|
|
74
|
+
weekly = remaining[0];
|
|
75
|
+
} else if (!session && remaining.length > 0) {
|
|
76
|
+
session = remaining[0];
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (!session && !weekly && !web) return undefined;
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
plan: level ?? "",
|
|
84
|
+
session5h:
|
|
85
|
+
session?.percentage != null && session.nextResetTime != null
|
|
86
|
+
? { usedPct: session.percentage, resetMs: session.nextResetTime }
|
|
87
|
+
: undefined,
|
|
88
|
+
weekly:
|
|
89
|
+
weekly?.percentage != null && weekly.nextResetTime != null
|
|
90
|
+
? { usedPct: weekly.percentage, resetMs: weekly.nextResetTime }
|
|
91
|
+
: undefined,
|
|
92
|
+
webSearches:
|
|
93
|
+
web && web.usage != null && web.remaining != null
|
|
94
|
+
? {
|
|
95
|
+
used: web.currentValue ?? 0,
|
|
96
|
+
limit: web.usage,
|
|
97
|
+
resetMs: web.nextResetTime ?? 0,
|
|
98
|
+
}
|
|
99
|
+
: undefined,
|
|
100
|
+
};
|
|
101
|
+
}
|