opencode-codebuddy-quota 0.1.0 → 0.1.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/config.default.json +41 -38
- package/package.json +2 -1
- package/rpc.ts +92 -92
- package/server.ts +132 -17
- package/src/gui/battery.tsx +81 -81
- package/src/gui/pace.ts +76 -76
- package/src/gui/quota.ts +198 -198
- package/src/gui/store.ts +181 -181
- package/src/gui.tsx +111 -111
- package/src/server/catalog.ts +264 -264
- package/src/server/config-writer.ts +621 -621
- package/src/server/config.ts +215 -195
- package/src/server/embedding-local.ts +166 -0
- package/src/server/embedding-mode.ts +14 -0
- package/src/server/gateway-owner.ts +243 -0
- package/src/server/gateway-setup.ts +51 -0
- package/src/server/logger.ts +26 -26
- package/src/server/model-cache.ts +136 -0
- package/src/server/model-info.ts +109 -109
- package/src/server/model-prewarm.ts +123 -0
- package/src/server/models-dev.ts +77 -77
- package/src/server/models-endpoint.ts +74 -0
- package/src/server/package-spec.ts +42 -42
- package/src/server/package-version.ts +51 -51
- package/src/server/pricing.ts +169 -169
- package/src/server/proxy.ts +335 -7
- package/src/server/stream-collect.ts +166 -0
- package/src/server/wire.ts +67 -67
package/src/gui/pace.ts
CHANGED
|
@@ -1,76 +1,76 @@
|
|
|
1
|
-
import type { Quota } from "./quota"
|
|
2
|
-
|
|
3
|
-
export const GREEN = "#4ade80"
|
|
4
|
-
export const AMBER = "#f5c246"
|
|
5
|
-
export const RED = "#f87171"
|
|
6
|
-
export const NEUTRAL = "#8b90a0"
|
|
7
|
-
|
|
8
|
-
/** Below this share of the cycle, elapsed-time progress is too jumpy to color by. */
|
|
9
|
-
const WARMUP_TIME_PCT = 3
|
|
10
|
-
|
|
11
|
-
type Rgb = [number, number, number]
|
|
12
|
-
|
|
13
|
-
const hex = (value: string): Rgb => [
|
|
14
|
-
parseInt(value.slice(1, 3), 16),
|
|
15
|
-
parseInt(value.slice(3, 5), 16),
|
|
16
|
-
parseInt(value.slice(5, 7), 16),
|
|
17
|
-
]
|
|
18
|
-
|
|
19
|
-
const mix = (from: string, to: string, t: number): Rgb => {
|
|
20
|
-
const a = hex(from)
|
|
21
|
-
const b = hex(to)
|
|
22
|
-
return [
|
|
23
|
-
Math.round(a[0] + (b[0] - a[0]) * t),
|
|
24
|
-
Math.round(a[1] + (b[1] - a[1]) * t),
|
|
25
|
-
Math.round(a[2] + (b[2] - a[2]) * t),
|
|
26
|
-
]
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const toCss = ([r, g, b]: Rgb) => `rgb(${r}, ${g}, ${b})`
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Color by pace, not by absolute remainder: `diff` is time progress minus spend
|
|
33
|
-
* progress. A gap of +5 or more is comfortable, 0 is the amber midline, -5 or
|
|
34
|
-
* more is an overrun, linear in between.
|
|
35
|
-
*/
|
|
36
|
-
export function paceColor(q: Quota): string {
|
|
37
|
-
if (q.diff === null || q.timePct === null) return NEUTRAL
|
|
38
|
-
if (q.timePct < WARMUP_TIME_PCT) return GREEN
|
|
39
|
-
const diff = q.diff
|
|
40
|
-
if (diff >= 5) return GREEN
|
|
41
|
-
if (diff <= -5) return RED
|
|
42
|
-
// The gap is negative here, so remap it onto 0..1 for the ramp.
|
|
43
|
-
return toCss(mix(AMBER, diff < 0 ? RED : GREEN, Math.abs(diff) / 5))
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export type PaceLabel = "normal" | "ahead" | "fast" | "over" | "unknown"
|
|
47
|
-
|
|
48
|
-
export function paceLabel(q: Quota): PaceLabel {
|
|
49
|
-
if (q.diff === null || q.timePct === null) return "unknown"
|
|
50
|
-
// Mirror paceColor's warm-up rule, or the dialog calls "超支" on a capsule that
|
|
51
|
-
// is deliberately green because the cycle just reset.
|
|
52
|
-
if (q.timePct < WARMUP_TIME_PCT) return "normal"
|
|
53
|
-
if (q.diff >= 5) return "normal"
|
|
54
|
-
if (q.diff >= 0) return "ahead"
|
|
55
|
-
if (q.diff > -5) return "fast"
|
|
56
|
-
return "over"
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export function formatCredits(value: number | null): string {
|
|
60
|
-
return value === null ? "—" : value.toFixed(2)
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export function formatPct(value: number | null): string {
|
|
64
|
-
return value === null ? "—" : `${value.toFixed(2)}%`
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* The gap between time and spend, in percentage points, with the sign kept.
|
|
69
|
-
* Positive means spending lags the clock (the good case); negative means the
|
|
70
|
-
* credits are going faster than the cycle.
|
|
71
|
-
*/
|
|
72
|
-
export function signedPp(value: number | null): string {
|
|
73
|
-
if (value === null || !Number.isFinite(value)) return "—"
|
|
74
|
-
if (value === 0) return "0.00 pp"
|
|
75
|
-
return `${value > 0 ? "+" : "-"}${Math.abs(value).toFixed(2)} pp`
|
|
76
|
-
}
|
|
1
|
+
import type { Quota } from "./quota"
|
|
2
|
+
|
|
3
|
+
export const GREEN = "#4ade80"
|
|
4
|
+
export const AMBER = "#f5c246"
|
|
5
|
+
export const RED = "#f87171"
|
|
6
|
+
export const NEUTRAL = "#8b90a0"
|
|
7
|
+
|
|
8
|
+
/** Below this share of the cycle, elapsed-time progress is too jumpy to color by. */
|
|
9
|
+
const WARMUP_TIME_PCT = 3
|
|
10
|
+
|
|
11
|
+
type Rgb = [number, number, number]
|
|
12
|
+
|
|
13
|
+
const hex = (value: string): Rgb => [
|
|
14
|
+
parseInt(value.slice(1, 3), 16),
|
|
15
|
+
parseInt(value.slice(3, 5), 16),
|
|
16
|
+
parseInt(value.slice(5, 7), 16),
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
const mix = (from: string, to: string, t: number): Rgb => {
|
|
20
|
+
const a = hex(from)
|
|
21
|
+
const b = hex(to)
|
|
22
|
+
return [
|
|
23
|
+
Math.round(a[0] + (b[0] - a[0]) * t),
|
|
24
|
+
Math.round(a[1] + (b[1] - a[1]) * t),
|
|
25
|
+
Math.round(a[2] + (b[2] - a[2]) * t),
|
|
26
|
+
]
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const toCss = ([r, g, b]: Rgb) => `rgb(${r}, ${g}, ${b})`
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Color by pace, not by absolute remainder: `diff` is time progress minus spend
|
|
33
|
+
* progress. A gap of +5 or more is comfortable, 0 is the amber midline, -5 or
|
|
34
|
+
* more is an overrun, linear in between.
|
|
35
|
+
*/
|
|
36
|
+
export function paceColor(q: Quota): string {
|
|
37
|
+
if (q.diff === null || q.timePct === null) return NEUTRAL
|
|
38
|
+
if (q.timePct < WARMUP_TIME_PCT) return GREEN
|
|
39
|
+
const diff = q.diff
|
|
40
|
+
if (diff >= 5) return GREEN
|
|
41
|
+
if (diff <= -5) return RED
|
|
42
|
+
// The gap is negative here, so remap it onto 0..1 for the ramp.
|
|
43
|
+
return toCss(mix(AMBER, diff < 0 ? RED : GREEN, Math.abs(diff) / 5))
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export type PaceLabel = "normal" | "ahead" | "fast" | "over" | "unknown"
|
|
47
|
+
|
|
48
|
+
export function paceLabel(q: Quota): PaceLabel {
|
|
49
|
+
if (q.diff === null || q.timePct === null) return "unknown"
|
|
50
|
+
// Mirror paceColor's warm-up rule, or the dialog calls "超支" on a capsule that
|
|
51
|
+
// is deliberately green because the cycle just reset.
|
|
52
|
+
if (q.timePct < WARMUP_TIME_PCT) return "normal"
|
|
53
|
+
if (q.diff >= 5) return "normal"
|
|
54
|
+
if (q.diff >= 0) return "ahead"
|
|
55
|
+
if (q.diff > -5) return "fast"
|
|
56
|
+
return "over"
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function formatCredits(value: number | null): string {
|
|
60
|
+
return value === null ? "—" : value.toFixed(2)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function formatPct(value: number | null): string {
|
|
64
|
+
return value === null ? "—" : `${value.toFixed(2)}%`
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The gap between time and spend, in percentage points, with the sign kept.
|
|
69
|
+
* Positive means spending lags the clock (the good case); negative means the
|
|
70
|
+
* credits are going faster than the cycle.
|
|
71
|
+
*/
|
|
72
|
+
export function signedPp(value: number | null): string {
|
|
73
|
+
if (value === null || !Number.isFinite(value)) return "—"
|
|
74
|
+
if (value === 0) return "0.00 pp"
|
|
75
|
+
return `${value > 0 ? "+" : "-"}${Math.abs(value).toFixed(2)} pp`
|
|
76
|
+
}
|
package/src/gui/quota.ts
CHANGED
|
@@ -1,198 +1,198 @@
|
|
|
1
|
-
import type { Site, SiteInfo } from "../shared"
|
|
2
|
-
|
|
3
|
-
export type QuotaStatus = "ok" | "cached" | "stale" | "error"
|
|
4
|
-
|
|
5
|
-
export interface Quota {
|
|
6
|
-
status: QuotaStatus
|
|
7
|
-
site: Site
|
|
8
|
-
sites: SiteInfo[]
|
|
9
|
-
remaining: number | null
|
|
10
|
-
used: number | null
|
|
11
|
-
total: number | null
|
|
12
|
-
usedPct: number | null
|
|
13
|
-
timePct: number | null
|
|
14
|
-
diff: number | null
|
|
15
|
-
cycleStart: string | null
|
|
16
|
-
cycleEnd: string | null
|
|
17
|
-
resetAt: string | null
|
|
18
|
-
fetchedAt: string | null
|
|
19
|
-
/** Age of the numbers in whole seconds, as the server counted it. */
|
|
20
|
-
ageSeconds: number | null
|
|
21
|
-
error: string | null
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface NormalizeContext {
|
|
25
|
-
now: number
|
|
26
|
-
fallback?: Quota | null
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const round2 = (n: number) => Math.round(n * 100) / 100
|
|
30
|
-
|
|
31
|
-
function num(value: unknown): number | null {
|
|
32
|
-
const n = typeof value === "string" ? Number(value) : value
|
|
33
|
-
return typeof n === "number" && Number.isFinite(n) ? n : null
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* "2026-10-28 00:00:00" is the upstream's local-time format. `new Date(str)` treats it
|
|
38
|
-
* as UTC in some engines, which would shift timePct by the UTC offset, so parse the
|
|
39
|
-
* parts by hand into local time.
|
|
40
|
-
*/
|
|
41
|
-
export function parseLocalTime(value: unknown): number | null {
|
|
42
|
-
if (typeof value !== "string") return null
|
|
43
|
-
const m = /^(\d{4})-(\d{2})-(\d{2})[ T](\d{2}):(\d{2}):(\d{2})/.exec(value.trim())
|
|
44
|
-
if (!m) return null
|
|
45
|
-
const [, y, mo, d, h, mi, s] = m.map(Number)
|
|
46
|
-
const t = new Date(y, mo - 1, d, h, mi, s).getTime()
|
|
47
|
-
return Number.isFinite(t) ? t : null
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const DEFAULT_SITES: SiteInfo[] = [
|
|
51
|
-
{ site: "cn", available: false, accounts: [], activeId: null },
|
|
52
|
-
{ site: "intl", available: false, accounts: [], activeId: null },
|
|
53
|
-
]
|
|
54
|
-
|
|
55
|
-
function empty(status: QuotaStatus, error: string | null): Quota {
|
|
56
|
-
return {
|
|
57
|
-
status,
|
|
58
|
-
site: "cn",
|
|
59
|
-
sites: DEFAULT_SITES,
|
|
60
|
-
remaining: null,
|
|
61
|
-
used: null,
|
|
62
|
-
total: null,
|
|
63
|
-
usedPct: null,
|
|
64
|
-
timePct: null,
|
|
65
|
-
diff: null,
|
|
66
|
-
cycleStart: null,
|
|
67
|
-
cycleEnd: null,
|
|
68
|
-
resetAt: null,
|
|
69
|
-
fetchedAt: null,
|
|
70
|
-
ageSeconds: null,
|
|
71
|
-
error,
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function timeProgress(
|
|
76
|
-
startMs: number | null,
|
|
77
|
-
endMs: number | null,
|
|
78
|
-
atMs: number | null,
|
|
79
|
-
): number | null {
|
|
80
|
-
if (startMs === null || endMs === null || atMs === null) return null
|
|
81
|
-
if (endMs <= startMs) return null
|
|
82
|
-
// A fetched time outside the cycle (clock skew, a stale cached reading carried
|
|
83
|
-
// across a reset) would otherwise render -87% or 110%, desyncing formatPct and
|
|
84
|
-
// paceColor from the already-clamped progress bar.
|
|
85
|
-
const pct = ((atMs - startMs) / (endMs - startMs)) * 100
|
|
86
|
-
return round2(Math.min(100, Math.max(0, pct)))
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export function normalizeQuota(payload: unknown, ctx: NormalizeContext): Quota {
|
|
90
|
-
/**
|
|
91
|
-
* `keepFallback` separates two different failures:
|
|
92
|
-
* - the server said `ok: false` — it is unavailable, and a previous reading
|
|
93
|
-
* must not be dressed up as merely stale, so the fallback is dropped;
|
|
94
|
-
* - the answer arrived but is unusable — the numbers are bad, not the line,
|
|
95
|
-
* so the last good reading still describes the quota.
|
|
96
|
-
*/
|
|
97
|
-
const unusable = (error: string, keepFallback: boolean): Quota =>
|
|
98
|
-
keepFallback && ctx.fallback ? { ...ctx.fallback, status: "stale", error } : empty("error", error)
|
|
99
|
-
|
|
100
|
-
if (typeof payload !== "object" || payload === null) {
|
|
101
|
-
return unusable(
|
|
102
|
-
typeof payload === "string"
|
|
103
|
-
? `unexpected response: ${payload.slice(0, 80)}`
|
|
104
|
-
: "empty response",
|
|
105
|
-
true,
|
|
106
|
-
)
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const data = payload as Record<string, unknown>
|
|
110
|
-
const error = typeof data.error === "string" ? data.error : null
|
|
111
|
-
|
|
112
|
-
if (data.ok !== true) return unusable(error ?? "quota service reported no reading", false)
|
|
113
|
-
|
|
114
|
-
const total = num(data.total)
|
|
115
|
-
const used = num(data.used)
|
|
116
|
-
const remaining = num(data.remaining)
|
|
117
|
-
if (total === null || total <= 0 || used === null || remaining === null) {
|
|
118
|
-
return unusable(error ?? "payload is missing a usable total/used/remaining", true)
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
const cycleStart = typeof data.cycleStart === "string" ? data.cycleStart : null
|
|
122
|
-
const cycleEnd = typeof data.cycleEnd === "string" ? data.cycleEnd : null
|
|
123
|
-
const resetAt = typeof data.resetAt === "string" ? data.resetAt : null
|
|
124
|
-
const fetchedAt = typeof data.fetchedAt === "string" ? data.fetchedAt : null
|
|
125
|
-
|
|
126
|
-
const usedPct = round2((used / total) * 100)
|
|
127
|
-
// Date.parse answers NaN for an unparseable string, and `NaN ?? x` is NaN, so
|
|
128
|
-
// guard with isFinite rather than relying on a nullish fallback. An unknown
|
|
129
|
-
// fetch time cannot anchor the time progress: falling back to "now" would make
|
|
130
|
-
// the pace drift with every render, so report no progress instead.
|
|
131
|
-
const parsedAt = fetchedAt ? Date.parse(fetchedAt) : NaN
|
|
132
|
-
const atMs = Number.isFinite(parsedAt) ? parsedAt : null
|
|
133
|
-
// The cycle ends when the next one starts, not on its last second, so progress
|
|
134
|
-
// reaches 100% exactly when the allowance resets. Using `cycleEnd` (23:59:59)
|
|
135
|
-
// would read as "period over" a second before the credits come back.
|
|
136
|
-
const timePct = timeProgress(
|
|
137
|
-
parseLocalTime(cycleStart),
|
|
138
|
-
parseLocalTime(resetAt) ?? parseLocalTime(cycleEnd),
|
|
139
|
-
atMs,
|
|
140
|
-
)
|
|
141
|
-
// Time progress minus spend progress: positive means the credits are being
|
|
142
|
-
// used more slowly than the cycle is passing, which is the healthy direction.
|
|
143
|
-
const diff =
|
|
144
|
-
usedPct !== null && timePct !== null && Number.isFinite(timePct) ? round2(timePct - usedPct) : null
|
|
145
|
-
|
|
146
|
-
const status: QuotaStatus =
|
|
147
|
-
data.stale === true ? "stale" : data.cached === true ? "cached" : "ok"
|
|
148
|
-
const ageSeconds = num(data.ageSeconds)
|
|
149
|
-
const site: Site = data.site === "intl" ? "intl" : "cn"
|
|
150
|
-
const sites: SiteInfo[] = Array.isArray(data.sites)
|
|
151
|
-
? (data.sites as SiteInfo[]).filter(
|
|
152
|
-
(s) => s && (s.site === "cn" || s.site === "intl") && Array.isArray(s.accounts),
|
|
153
|
-
)
|
|
154
|
-
: DEFAULT_SITES
|
|
155
|
-
|
|
156
|
-
return {
|
|
157
|
-
status,
|
|
158
|
-
site,
|
|
159
|
-
sites,
|
|
160
|
-
remaining,
|
|
161
|
-
used,
|
|
162
|
-
total,
|
|
163
|
-
usedPct,
|
|
164
|
-
timePct,
|
|
165
|
-
diff,
|
|
166
|
-
cycleStart,
|
|
167
|
-
cycleEnd,
|
|
168
|
-
resetAt,
|
|
169
|
-
fetchedAt,
|
|
170
|
-
ageSeconds: ageSeconds === null ? null : Math.round(ageSeconds),
|
|
171
|
-
error: error ?? null,
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/** The number inside the capsule: remaining share as a whole 0..100, no percent sign. */
|
|
176
|
-
export function remainingPercent(q: Quota): number | null {
|
|
177
|
-
if (q.total === null || q.total <= 0) return null
|
|
178
|
-
// Derive from used rather than trusting `remaining`: the upstream clamps remaining
|
|
179
|
-
// to >= 0, so an overrun would otherwise read as a healthy 89%.
|
|
180
|
-
const used = q.used
|
|
181
|
-
if (used === null) {
|
|
182
|
-
if (q.remaining === null) return null
|
|
183
|
-
return Math.min(100, Math.max(0, Math.round((q.remaining / q.total) * 100)))
|
|
184
|
-
}
|
|
185
|
-
const pct = Math.round(((q.total - used) / q.total) * 100)
|
|
186
|
-
return Math.min(100, Math.max(0, pct))
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* The remaining share at full precision, for every label that is not the capsule.
|
|
191
|
-
* The capsule rounds it to a whole number; the tooltip and the dialog must agree
|
|
192
|
-
* with each other, so both read this one function.
|
|
193
|
-
*/
|
|
194
|
-
export function remainingShare(q: Quota): number | null {
|
|
195
|
-
if (q.usedPct === null || q.used === null || q.total === null) return null
|
|
196
|
-
const pct = 100 - q.usedPct
|
|
197
|
-
return Math.max(0, Math.round(pct * 100) / 100)
|
|
198
|
-
}
|
|
1
|
+
import type { Site, SiteInfo } from "../shared"
|
|
2
|
+
|
|
3
|
+
export type QuotaStatus = "ok" | "cached" | "stale" | "error"
|
|
4
|
+
|
|
5
|
+
export interface Quota {
|
|
6
|
+
status: QuotaStatus
|
|
7
|
+
site: Site
|
|
8
|
+
sites: SiteInfo[]
|
|
9
|
+
remaining: number | null
|
|
10
|
+
used: number | null
|
|
11
|
+
total: number | null
|
|
12
|
+
usedPct: number | null
|
|
13
|
+
timePct: number | null
|
|
14
|
+
diff: number | null
|
|
15
|
+
cycleStart: string | null
|
|
16
|
+
cycleEnd: string | null
|
|
17
|
+
resetAt: string | null
|
|
18
|
+
fetchedAt: string | null
|
|
19
|
+
/** Age of the numbers in whole seconds, as the server counted it. */
|
|
20
|
+
ageSeconds: number | null
|
|
21
|
+
error: string | null
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface NormalizeContext {
|
|
25
|
+
now: number
|
|
26
|
+
fallback?: Quota | null
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const round2 = (n: number) => Math.round(n * 100) / 100
|
|
30
|
+
|
|
31
|
+
function num(value: unknown): number | null {
|
|
32
|
+
const n = typeof value === "string" ? Number(value) : value
|
|
33
|
+
return typeof n === "number" && Number.isFinite(n) ? n : null
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* "2026-10-28 00:00:00" is the upstream's local-time format. `new Date(str)` treats it
|
|
38
|
+
* as UTC in some engines, which would shift timePct by the UTC offset, so parse the
|
|
39
|
+
* parts by hand into local time.
|
|
40
|
+
*/
|
|
41
|
+
export function parseLocalTime(value: unknown): number | null {
|
|
42
|
+
if (typeof value !== "string") return null
|
|
43
|
+
const m = /^(\d{4})-(\d{2})-(\d{2})[ T](\d{2}):(\d{2}):(\d{2})/.exec(value.trim())
|
|
44
|
+
if (!m) return null
|
|
45
|
+
const [, y, mo, d, h, mi, s] = m.map(Number)
|
|
46
|
+
const t = new Date(y, mo - 1, d, h, mi, s).getTime()
|
|
47
|
+
return Number.isFinite(t) ? t : null
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const DEFAULT_SITES: SiteInfo[] = [
|
|
51
|
+
{ site: "cn", available: false, accounts: [], activeId: null },
|
|
52
|
+
{ site: "intl", available: false, accounts: [], activeId: null },
|
|
53
|
+
]
|
|
54
|
+
|
|
55
|
+
function empty(status: QuotaStatus, error: string | null): Quota {
|
|
56
|
+
return {
|
|
57
|
+
status,
|
|
58
|
+
site: "cn",
|
|
59
|
+
sites: DEFAULT_SITES,
|
|
60
|
+
remaining: null,
|
|
61
|
+
used: null,
|
|
62
|
+
total: null,
|
|
63
|
+
usedPct: null,
|
|
64
|
+
timePct: null,
|
|
65
|
+
diff: null,
|
|
66
|
+
cycleStart: null,
|
|
67
|
+
cycleEnd: null,
|
|
68
|
+
resetAt: null,
|
|
69
|
+
fetchedAt: null,
|
|
70
|
+
ageSeconds: null,
|
|
71
|
+
error,
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function timeProgress(
|
|
76
|
+
startMs: number | null,
|
|
77
|
+
endMs: number | null,
|
|
78
|
+
atMs: number | null,
|
|
79
|
+
): number | null {
|
|
80
|
+
if (startMs === null || endMs === null || atMs === null) return null
|
|
81
|
+
if (endMs <= startMs) return null
|
|
82
|
+
// A fetched time outside the cycle (clock skew, a stale cached reading carried
|
|
83
|
+
// across a reset) would otherwise render -87% or 110%, desyncing formatPct and
|
|
84
|
+
// paceColor from the already-clamped progress bar.
|
|
85
|
+
const pct = ((atMs - startMs) / (endMs - startMs)) * 100
|
|
86
|
+
return round2(Math.min(100, Math.max(0, pct)))
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function normalizeQuota(payload: unknown, ctx: NormalizeContext): Quota {
|
|
90
|
+
/**
|
|
91
|
+
* `keepFallback` separates two different failures:
|
|
92
|
+
* - the server said `ok: false` — it is unavailable, and a previous reading
|
|
93
|
+
* must not be dressed up as merely stale, so the fallback is dropped;
|
|
94
|
+
* - the answer arrived but is unusable — the numbers are bad, not the line,
|
|
95
|
+
* so the last good reading still describes the quota.
|
|
96
|
+
*/
|
|
97
|
+
const unusable = (error: string, keepFallback: boolean): Quota =>
|
|
98
|
+
keepFallback && ctx.fallback ? { ...ctx.fallback, status: "stale", error } : empty("error", error)
|
|
99
|
+
|
|
100
|
+
if (typeof payload !== "object" || payload === null) {
|
|
101
|
+
return unusable(
|
|
102
|
+
typeof payload === "string"
|
|
103
|
+
? `unexpected response: ${payload.slice(0, 80)}`
|
|
104
|
+
: "empty response",
|
|
105
|
+
true,
|
|
106
|
+
)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const data = payload as Record<string, unknown>
|
|
110
|
+
const error = typeof data.error === "string" ? data.error : null
|
|
111
|
+
|
|
112
|
+
if (data.ok !== true) return unusable(error ?? "quota service reported no reading", false)
|
|
113
|
+
|
|
114
|
+
const total = num(data.total)
|
|
115
|
+
const used = num(data.used)
|
|
116
|
+
const remaining = num(data.remaining)
|
|
117
|
+
if (total === null || total <= 0 || used === null || remaining === null) {
|
|
118
|
+
return unusable(error ?? "payload is missing a usable total/used/remaining", true)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const cycleStart = typeof data.cycleStart === "string" ? data.cycleStart : null
|
|
122
|
+
const cycleEnd = typeof data.cycleEnd === "string" ? data.cycleEnd : null
|
|
123
|
+
const resetAt = typeof data.resetAt === "string" ? data.resetAt : null
|
|
124
|
+
const fetchedAt = typeof data.fetchedAt === "string" ? data.fetchedAt : null
|
|
125
|
+
|
|
126
|
+
const usedPct = round2((used / total) * 100)
|
|
127
|
+
// Date.parse answers NaN for an unparseable string, and `NaN ?? x` is NaN, so
|
|
128
|
+
// guard with isFinite rather than relying on a nullish fallback. An unknown
|
|
129
|
+
// fetch time cannot anchor the time progress: falling back to "now" would make
|
|
130
|
+
// the pace drift with every render, so report no progress instead.
|
|
131
|
+
const parsedAt = fetchedAt ? Date.parse(fetchedAt) : NaN
|
|
132
|
+
const atMs = Number.isFinite(parsedAt) ? parsedAt : null
|
|
133
|
+
// The cycle ends when the next one starts, not on its last second, so progress
|
|
134
|
+
// reaches 100% exactly when the allowance resets. Using `cycleEnd` (23:59:59)
|
|
135
|
+
// would read as "period over" a second before the credits come back.
|
|
136
|
+
const timePct = timeProgress(
|
|
137
|
+
parseLocalTime(cycleStart),
|
|
138
|
+
parseLocalTime(resetAt) ?? parseLocalTime(cycleEnd),
|
|
139
|
+
atMs,
|
|
140
|
+
)
|
|
141
|
+
// Time progress minus spend progress: positive means the credits are being
|
|
142
|
+
// used more slowly than the cycle is passing, which is the healthy direction.
|
|
143
|
+
const diff =
|
|
144
|
+
usedPct !== null && timePct !== null && Number.isFinite(timePct) ? round2(timePct - usedPct) : null
|
|
145
|
+
|
|
146
|
+
const status: QuotaStatus =
|
|
147
|
+
data.stale === true ? "stale" : data.cached === true ? "cached" : "ok"
|
|
148
|
+
const ageSeconds = num(data.ageSeconds)
|
|
149
|
+
const site: Site = data.site === "intl" ? "intl" : "cn"
|
|
150
|
+
const sites: SiteInfo[] = Array.isArray(data.sites)
|
|
151
|
+
? (data.sites as SiteInfo[]).filter(
|
|
152
|
+
(s) => s && (s.site === "cn" || s.site === "intl") && Array.isArray(s.accounts),
|
|
153
|
+
)
|
|
154
|
+
: DEFAULT_SITES
|
|
155
|
+
|
|
156
|
+
return {
|
|
157
|
+
status,
|
|
158
|
+
site,
|
|
159
|
+
sites,
|
|
160
|
+
remaining,
|
|
161
|
+
used,
|
|
162
|
+
total,
|
|
163
|
+
usedPct,
|
|
164
|
+
timePct,
|
|
165
|
+
diff,
|
|
166
|
+
cycleStart,
|
|
167
|
+
cycleEnd,
|
|
168
|
+
resetAt,
|
|
169
|
+
fetchedAt,
|
|
170
|
+
ageSeconds: ageSeconds === null ? null : Math.round(ageSeconds),
|
|
171
|
+
error: error ?? null,
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** The number inside the capsule: remaining share as a whole 0..100, no percent sign. */
|
|
176
|
+
export function remainingPercent(q: Quota): number | null {
|
|
177
|
+
if (q.total === null || q.total <= 0) return null
|
|
178
|
+
// Derive from used rather than trusting `remaining`: the upstream clamps remaining
|
|
179
|
+
// to >= 0, so an overrun would otherwise read as a healthy 89%.
|
|
180
|
+
const used = q.used
|
|
181
|
+
if (used === null) {
|
|
182
|
+
if (q.remaining === null) return null
|
|
183
|
+
return Math.min(100, Math.max(0, Math.round((q.remaining / q.total) * 100)))
|
|
184
|
+
}
|
|
185
|
+
const pct = Math.round(((q.total - used) / q.total) * 100)
|
|
186
|
+
return Math.min(100, Math.max(0, pct))
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* The remaining share at full precision, for every label that is not the capsule.
|
|
191
|
+
* The capsule rounds it to a whole number; the tooltip and the dialog must agree
|
|
192
|
+
* with each other, so both read this one function.
|
|
193
|
+
*/
|
|
194
|
+
export function remainingShare(q: Quota): number | null {
|
|
195
|
+
if (q.usedPct === null || q.used === null || q.total === null) return null
|
|
196
|
+
const pct = 100 - q.usedPct
|
|
197
|
+
return Math.max(0, Math.round(pct * 100) / 100)
|
|
198
|
+
}
|