@laoyuehanni/dsh-token-usage 0.4.8 → 0.4.10
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 +11 -4
- package/README.zh.md +11 -4
- package/lib/aux-request.d.ts +4 -0
- package/lib/aux-request.d.ts.map +1 -1
- package/lib/aux-request.js +1 -0
- package/lib/aux-request.js.map +1 -1
- package/lib/client/DashboardCards.d.ts +62 -0
- package/lib/client/DashboardCards.d.ts.map +1 -0
- package/lib/client/DashboardCards.js +25 -0
- package/lib/client/DashboardCards.js.map +1 -0
- package/lib/client/OverheadRows.d.ts +30 -0
- package/lib/client/OverheadRows.d.ts.map +1 -0
- package/lib/client/OverheadRows.js +57 -0
- package/lib/client/OverheadRows.js.map +1 -0
- package/lib/client/OverviewBoard.d.ts +54 -0
- package/lib/client/OverviewBoard.d.ts.map +1 -0
- package/lib/client/OverviewBoard.js +35 -0
- package/lib/client/OverviewBoard.js.map +1 -0
- package/lib/client/PanellistMoreEntry.d.ts +29 -0
- package/lib/client/PanellistMoreEntry.d.ts.map +1 -0
- package/lib/client/PanellistMoreEntry.js +12 -0
- package/lib/client/PanellistMoreEntry.js.map +1 -0
- package/lib/client/PanellistPinEntry.d.ts +21 -0
- package/lib/client/PanellistPinEntry.d.ts.map +1 -0
- package/lib/client/PanellistPinEntry.js +24 -0
- package/lib/client/PanellistPinEntry.js.map +1 -0
- package/lib/client/QuotaButton.d.ts +1 -0
- package/lib/client/QuotaButton.d.ts.map +1 -1
- package/lib/client/QuotaButton.js +69 -17
- package/lib/client/QuotaButton.js.map +1 -1
- package/lib/client/RequestPool.d.ts +39 -0
- package/lib/client/RequestPool.d.ts.map +1 -0
- package/lib/client/RequestPool.js +164 -0
- package/lib/client/RequestPool.js.map +1 -0
- package/lib/client/StatCard.d.ts +58 -5
- package/lib/client/StatCard.d.ts.map +1 -1
- package/lib/client/StatCard.js +27 -8
- package/lib/client/StatCard.js.map +1 -1
- package/lib/client/TokenActivity.d.ts +21 -0
- package/lib/client/TokenActivity.d.ts.map +1 -0
- package/lib/client/TokenActivity.js +71 -0
- package/lib/client/TokenActivity.js.map +1 -0
- package/lib/client/TokenUsageCard.d.ts.map +1 -1
- package/lib/client/TokenUsageCard.js +1 -1
- package/lib/client/TokenUsageCard.js.map +1 -1
- package/lib/client/TokenUsageSection.d.ts +13 -34
- package/lib/client/TokenUsageSection.d.ts.map +1 -1
- package/lib/client/TokenUsageSection.js +81 -93
- package/lib/client/TokenUsageSection.js.map +1 -1
- package/lib/client/TrendChart.d.ts +2 -1
- package/lib/client/TrendChart.d.ts.map +1 -1
- package/lib/client/TrendChart.js +31 -10
- package/lib/client/TrendChart.js.map +1 -1
- package/lib/client/UsageChart.d.ts +20 -0
- package/lib/client/UsageChart.d.ts.map +1 -0
- package/lib/client/UsageChart.js +26 -0
- package/lib/client/UsageChart.js.map +1 -0
- package/lib/client/UsagePanelPage.d.ts +37 -0
- package/lib/client/UsagePanelPage.d.ts.map +1 -0
- package/lib/client/UsagePanelPage.js +16 -0
- package/lib/client/UsagePanelPage.js.map +1 -0
- package/lib/client/UsageRows.d.ts +102 -0
- package/lib/client/UsageRows.d.ts.map +1 -0
- package/lib/client/UsageRows.js +261 -0
- package/lib/client/UsageRows.js.map +1 -0
- package/lib/client/UsageView.d.ts.map +1 -1
- package/lib/client/UsageView.js +79 -5
- package/lib/client/UsageView.js.map +1 -1
- package/lib/client/activity-grid.d.ts +42 -0
- package/lib/client/activity-grid.d.ts.map +1 -0
- package/lib/client/activity-grid.js +86 -0
- package/lib/client/activity-grid.js.map +1 -0
- package/lib/client/async-resource.d.ts +8 -0
- package/lib/client/async-resource.d.ts.map +1 -1
- package/lib/client/async-resource.js +22 -4
- package/lib/client/async-resource.js.map +1 -1
- package/lib/client/card-form.d.ts +15 -0
- package/lib/client/card-form.d.ts.map +1 -1
- package/lib/client/card-form.js +27 -5
- package/lib/client/card-form.js.map +1 -1
- package/lib/client/cost-split.d.ts +26 -0
- package/lib/client/cost-split.d.ts.map +1 -0
- package/lib/client/cost-split.js +37 -0
- package/lib/client/cost-split.js.map +1 -0
- package/lib/client/format.d.ts +31 -1
- package/lib/client/format.d.ts.map +1 -1
- package/lib/client/format.js +48 -0
- package/lib/client/format.js.map +1 -1
- package/lib/client/index.d.ts.map +1 -1
- package/lib/client/index.js +102 -0
- package/lib/client/index.js.map +1 -1
- package/lib/client/locales.d.ts +93 -1
- package/lib/client/locales.d.ts.map +1 -1
- package/lib/client/locales.js +186 -2
- package/lib/client/locales.js.map +1 -1
- package/lib/client/project-rows.d.ts +4 -0
- package/lib/client/project-rows.d.ts.map +1 -0
- package/lib/client/project-rows.js +3 -0
- package/lib/client/project-rows.js.map +1 -0
- package/lib/client/quota-format.d.ts +6 -4
- package/lib/client/quota-format.d.ts.map +1 -1
- package/lib/client/quota-format.js +7 -5
- package/lib/client/quota-format.js.map +1 -1
- package/lib/client.js +3034 -1490
- package/lib/client.js.map +1 -1
- package/lib/index.d.ts +6 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +50 -7
- package/lib/index.js.map +1 -1
- package/lib/project-rows.d.ts +22 -0
- package/lib/project-rows.d.ts.map +1 -0
- package/lib/project-rows.js +82 -0
- package/lib/project-rows.js.map +1 -0
- package/lib/quota/adapters/magpie.d.ts +84 -0
- package/lib/quota/adapters/magpie.d.ts.map +1 -0
- package/lib/quota/adapters/magpie.js +290 -0
- package/lib/quota/adapters/magpie.js.map +1 -0
- package/lib/quota/provider-tracking.d.ts +15 -4
- package/lib/quota/provider-tracking.d.ts.map +1 -1
- package/lib/quota/provider-tracking.js +18 -7
- package/lib/quota/provider-tracking.js.map +1 -1
- package/lib/quota/quota-route.d.ts +3 -3
- package/lib/quota/quota-route.d.ts.map +1 -1
- package/lib/quota/quota-route.js +3 -3
- package/lib/quota/quota-route.js.map +1 -1
- package/lib/quota/quota-service.d.ts +14 -4
- package/lib/quota/quota-service.d.ts.map +1 -1
- package/lib/quota/quota-service.js +45 -15
- package/lib/quota/quota-service.js.map +1 -1
- package/lib/quota/registry.d.ts.map +1 -1
- package/lib/quota/registry.js +6 -0
- package/lib/quota/registry.js.map +1 -1
- package/lib/quota/types.d.ts +9 -0
- package/lib/quota/types.d.ts.map +1 -1
- package/lib/quota/types.js.map +1 -1
- package/lib/request-details.d.ts +104 -0
- package/lib/request-details.d.ts.map +1 -0
- package/lib/request-details.js +215 -0
- package/lib/request-details.js.map +1 -0
- package/lib/request-observe.d.ts +167 -0
- package/lib/request-observe.d.ts.map +1 -0
- package/lib/request-observe.js +468 -0
- package/lib/request-observe.js.map +1 -0
- package/lib/rollup.d.ts +18 -0
- package/lib/rollup.d.ts.map +1 -1
- package/lib/rollup.js +12 -0
- package/lib/rollup.js.map +1 -1
- package/lib/session-request-details.d.ts +48 -0
- package/lib/session-request-details.d.ts.map +1 -0
- package/lib/session-request-details.js +315 -0
- package/lib/session-request-details.js.map +1 -0
- package/lib/stats-route.d.ts +9 -1
- package/lib/stats-route.d.ts.map +1 -1
- package/lib/stats-route.js +38 -4
- package/lib/stats-route.js.map +1 -1
- package/lib/stats.d.ts +23 -8
- package/lib/stats.d.ts.map +1 -1
- package/lib/stats.js +119 -27
- package/lib/stats.js.map +1 -1
- package/lib/sync.d.ts.map +1 -1
- package/lib/sync.js +1 -0
- package/lib/sync.js.map +1 -1
- package/lib/usage-record.d.ts +61 -0
- package/lib/usage-record.d.ts.map +1 -1
- package/lib/usage-record.js +80 -0
- package/lib/usage-record.js.map +1 -1
- package/lib/wire.d.ts +172 -5
- package/lib/wire.d.ts.map +1 -1
- package/lib/wire.js +8 -0
- package/lib/wire.js.map +1 -1
- package/package.json +30 -26
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Magpie gateway adapter: the aggregated quota cards of every upstream
|
|
3
|
+
* account the local magpie gateway holds, read through its read-only
|
|
4
|
+
* `GET /v1/magpie/quotas?kept=1` report. Only the gateway's last read is
|
|
5
|
+
* consumed: polling, opening and retrying never ask vendors to refresh.
|
|
6
|
+
*
|
|
7
|
+
* Identity is PROBED, not assumed: a magpie route is a user-declared
|
|
8
|
+
* OpenAI-compatible profile with an arbitrary name and a loopback-ish
|
|
9
|
+
* base URL no host table can enumerate. The adapter claims the `magpie`
|
|
10
|
+
* route key directly; every other unknown route is resolved by
|
|
11
|
+
* {@link MagpieDetector} (the gateway's `GET /v1` hello answers
|
|
12
|
+
* `{"name":"magpie"}`), which the quota service consults when the
|
|
13
|
+
* registry finds nothing — see quota-service.ts.
|
|
14
|
+
*
|
|
15
|
+
* Model scoping: magpie's model ids are `provider/model`, so the model
|
|
16
|
+
* the asking session uses names the card(s) to show — the account(s) of
|
|
17
|
+
* that provider, several when the user signed in more than one. A
|
|
18
|
+
* routing group id (`group/…`, whose members the gateway does not list
|
|
19
|
+
* over REST) and a bare model id scope to NOTHING, and an unmatched
|
|
20
|
+
* prefix falls back to the whole fleet: a group may route anywhere, and
|
|
21
|
+
* the full card list is what the user arranged the gateway around.
|
|
22
|
+
*
|
|
23
|
+
* Window vocabulary: magpie reports open-named windows ("5 hours",
|
|
24
|
+
* "Weekly", "7 days · Opus", "Credits", "Allowance") with used shares in
|
|
25
|
+
* percent; the known period names map onto the wire's tiers, the rest
|
|
26
|
+
* ride as `other` windows carrying the vendor's own name. Balances are
|
|
27
|
+
* sign-prefixed money strings ("¥356.81", "$11.12 · ¥-0.05" when a
|
|
28
|
+
* vendor reports two currencies) — the first amount parses, its sign
|
|
29
|
+
* naming the currency.
|
|
30
|
+
*
|
|
31
|
+
* @module token-usage/quota/adapters/magpie
|
|
32
|
+
*/
|
|
33
|
+
import { fetchJson } from "../http.js";
|
|
34
|
+
import { QuotaQueryError } from "../types.js";
|
|
35
|
+
/** The route key that claims this adapter outright (no probe needed). */
|
|
36
|
+
export const ROUTE_MAGPIE = 'magpie';
|
|
37
|
+
/** The gateway's default listen address, used when the profile carries no
|
|
38
|
+
* base URL (the `magpie` route key with a bare profile). */
|
|
39
|
+
export const DEFAULT_MAGPIE_BASE = 'http://127.0.0.1:3425/v1';
|
|
40
|
+
/** Transport ceiling only; kept reads never wait for vendor fan-out. */
|
|
41
|
+
const MAGPIE_QUERY_TIMEOUT_MS = 15_000;
|
|
42
|
+
/**
|
|
43
|
+
* The gateway root a base URL points at: trailing slashes trimmed, and a
|
|
44
|
+
* bare origin (no `/v1` tail) kept as-is — magpie mounts its API at the
|
|
45
|
+
* root, so both `http://host:3425` and `http://host:3425/v1` are valid
|
|
46
|
+
* client base URLs and every endpoint path is appended verbatim.
|
|
47
|
+
*/
|
|
48
|
+
export function magpieBaseOf(baseUrl) {
|
|
49
|
+
const base = baseUrl !== undefined && baseUrl !== '' ? baseUrl : DEFAULT_MAGPIE_BASE;
|
|
50
|
+
return base.replace(/\/+$/u, '');
|
|
51
|
+
}
|
|
52
|
+
/** The quota report URL of one gateway base. */
|
|
53
|
+
export function magpieQuotaUrl(baseUrl) {
|
|
54
|
+
const base = magpieBaseOf(baseUrl);
|
|
55
|
+
return /\/v1$/u.test(base) ? `${base}/magpie/quotas?kept=1` : `${base}/v1/magpie/quotas?kept=1`;
|
|
56
|
+
}
|
|
57
|
+
/** Parse the `data[]` array out of the report envelope. */
|
|
58
|
+
function cardsOf(body) {
|
|
59
|
+
if (typeof body !== 'object' || body === null) {
|
|
60
|
+
throw new QuotaQueryError('parse', 'response is not an object');
|
|
61
|
+
}
|
|
62
|
+
const data = body.data;
|
|
63
|
+
if (!Array.isArray(data)) {
|
|
64
|
+
throw new QuotaQueryError('parse', 'missing "data" array');
|
|
65
|
+
}
|
|
66
|
+
const cards = [];
|
|
67
|
+
for (const raw of data) {
|
|
68
|
+
if (typeof raw !== 'object' || raw === null)
|
|
69
|
+
continue;
|
|
70
|
+
const record = raw;
|
|
71
|
+
if (typeof record.provider !== 'string' || record.provider === '')
|
|
72
|
+
continue;
|
|
73
|
+
cards.push({
|
|
74
|
+
provider: record.provider,
|
|
75
|
+
name: typeof record.name === 'string' ? record.name : record.provider,
|
|
76
|
+
kind: typeof record.kind === 'string' ? record.kind : '',
|
|
77
|
+
...(typeof record.plan === 'string' && record.plan !== '' ? { plan: record.plan } : {}),
|
|
78
|
+
...(typeof record.user === 'string' && record.user !== '' ? { user: record.user } : {}),
|
|
79
|
+
windows: Array.isArray(record.windows)
|
|
80
|
+
? record.windows.filter((w) => typeof w === 'object' && w !== null)
|
|
81
|
+
: [],
|
|
82
|
+
...(typeof record.balance === 'string' ? { balance: record.balance } : {}),
|
|
83
|
+
...(typeof record.error === 'string' && record.error !== '' ? { error: record.error } : {}),
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
return cards;
|
|
87
|
+
}
|
|
88
|
+
/** The wire tier of one magpie window name, or `other` (the vendor's own
|
|
89
|
+
* name rides along on the window). The gateway normalizes its vendors to
|
|
90
|
+
* a small set of period names — "5 hours", "Weekly", "7 days" (at times
|
|
91
|
+
* suffixed "· Opus" when a model draws its own weekly pool), "Monthly" /
|
|
92
|
+
* "Month" — everything else is a meter the tier vocabulary has no slot
|
|
93
|
+
* for and stays itself. */
|
|
94
|
+
function tierOfWindow(name) {
|
|
95
|
+
const lower = name?.toLowerCase() ?? '';
|
|
96
|
+
if (/^5\s*h(ou?rs?)?$/u.test(lower))
|
|
97
|
+
return 'five_hour';
|
|
98
|
+
if (/^(weekly|7\s*days?\b)/u.test(lower))
|
|
99
|
+
return 'weekly';
|
|
100
|
+
if (/^month(ly)?$/u.test(lower))
|
|
101
|
+
return 'monthly';
|
|
102
|
+
return 'other';
|
|
103
|
+
}
|
|
104
|
+
/** The first sign-prefixed money amount of a balance string and its
|
|
105
|
+
* currency ("¥356.81" → cny, "$11.12 · ¥-0.05" → usd 11.12), or
|
|
106
|
+
* undefined when no money parses. Sign-less figures are refused: the
|
|
107
|
+
* gateway also formats non-money counts ("12.3k credits") the same way,
|
|
108
|
+
* and only the sign says which is money. */
|
|
109
|
+
function balanceOf(text) {
|
|
110
|
+
if (text === undefined)
|
|
111
|
+
return undefined;
|
|
112
|
+
const match = text.match(/([¥$])\s*(-?[\d,]+(?:\.\d+)?)/u);
|
|
113
|
+
if (match === null)
|
|
114
|
+
return undefined;
|
|
115
|
+
const value = Number(match[2]?.replace(/,/gu, ''));
|
|
116
|
+
if (!Number.isFinite(value))
|
|
117
|
+
return undefined;
|
|
118
|
+
return { value, unit: match[1] === '$' ? 'usd' : 'cny' };
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* The provider id prefix of a model id (`zcode/glm-4.7` → `zcode`), or
|
|
122
|
+
* undefined when the id names no provider: a routing group
|
|
123
|
+
* (`group/…` — the members are the user's to arrange and are not listed
|
|
124
|
+
* over REST) and a bare id both scope to the whole fleet.
|
|
125
|
+
*/
|
|
126
|
+
function providerPrefixOf(model) {
|
|
127
|
+
if (model === undefined || model === '')
|
|
128
|
+
return undefined;
|
|
129
|
+
const slash = model.indexOf('/');
|
|
130
|
+
if (slash <= 0)
|
|
131
|
+
return undefined;
|
|
132
|
+
const prefix = model.slice(0, slash);
|
|
133
|
+
if (prefix.toLowerCase() === 'group')
|
|
134
|
+
return undefined;
|
|
135
|
+
return prefix;
|
|
136
|
+
}
|
|
137
|
+
/** One normalized window of a card's report windows. */
|
|
138
|
+
function windowOf(card, span) {
|
|
139
|
+
const used = typeof span.used === 'number' && Number.isFinite(span.used) && span.used >= 0
|
|
140
|
+
? Math.min(span.used, 100)
|
|
141
|
+
: undefined;
|
|
142
|
+
if (used === undefined)
|
|
143
|
+
return undefined;
|
|
144
|
+
const tier = tierOfWindow(span.name);
|
|
145
|
+
const resetAt = span.resetsAt !== undefined ? Date.parse(span.resetsAt) : Number.NaN;
|
|
146
|
+
return {
|
|
147
|
+
tier,
|
|
148
|
+
usedPercent: used,
|
|
149
|
+
...(Number.isFinite(resetAt) ? { resetAt } : {}),
|
|
150
|
+
...(tier === 'other' && span.name !== undefined ? { name: span.name } : {}),
|
|
151
|
+
...(card.cardLabel !== undefined ? { card: card.cardLabel } : {}),
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* The magpie gateway adapter. One query answers the cards the asking
|
|
156
|
+
* model draws on — see the module note for the scoping rules.
|
|
157
|
+
*/
|
|
158
|
+
export const magpieAdapter = {
|
|
159
|
+
id: 'magpie-quotas',
|
|
160
|
+
label: 'Magpie',
|
|
161
|
+
routes: [ROUTE_MAGPIE],
|
|
162
|
+
queryTimeoutMs: MAGPIE_QUERY_TIMEOUT_MS,
|
|
163
|
+
// The loopback gateway serves its quota report without any key; a key
|
|
164
|
+
// matters only for a shared gateway on another machine (where its
|
|
165
|
+
// absence surfaces as an auth error with a retry, not a hidden button).
|
|
166
|
+
usesApiKey: false,
|
|
167
|
+
matches(input) {
|
|
168
|
+
return input.provider === ROUTE_MAGPIE;
|
|
169
|
+
},
|
|
170
|
+
async query(ctx) {
|
|
171
|
+
const headers = {};
|
|
172
|
+
if (ctx.apiKey !== '')
|
|
173
|
+
headers.authorization = `Bearer ${ctx.apiKey}`;
|
|
174
|
+
const cards = cardsOf(await fetchJson(ctx, magpieQuotaUrl(ctx.baseUrl), headers));
|
|
175
|
+
if (cards.length === 0) {
|
|
176
|
+
throw new QuotaQueryError('parse', 'no quota cards in "data"');
|
|
177
|
+
}
|
|
178
|
+
// Failed reads are cards too (their windows empty, their error set):
|
|
179
|
+
// drop them while any healthy card remains, but an all-error report
|
|
180
|
+
// is the gateway saying the read failed — surface it, not an empty
|
|
181
|
+
// success.
|
|
182
|
+
const healthy = cards.filter(card => card.error === undefined);
|
|
183
|
+
if (healthy.length === 0) {
|
|
184
|
+
throw new QuotaQueryError('http', cards[0]?.error ?? 'every card failed to read');
|
|
185
|
+
}
|
|
186
|
+
// Scope to the asking model's provider when the prefix names a card
|
|
187
|
+
// the report actually holds; otherwise (group, bare id, provider
|
|
188
|
+
// without a card) the whole fleet answers.
|
|
189
|
+
const prefix = providerPrefixOf(ctx.model);
|
|
190
|
+
let scoped = healthy;
|
|
191
|
+
if (prefix !== undefined) {
|
|
192
|
+
const matches = healthy.filter(card => card.provider === prefix || card.provider.startsWith(`${prefix}/`));
|
|
193
|
+
if (matches.length > 0)
|
|
194
|
+
scoped = matches;
|
|
195
|
+
}
|
|
196
|
+
// A provider with several cards (two accounts of one vendor, several
|
|
197
|
+
// keys) needs each card distinguishable: the account name rides
|
|
198
|
+
// along, a blank one numbered by position.
|
|
199
|
+
const byProvider = new Map();
|
|
200
|
+
for (const card of scoped)
|
|
201
|
+
byProvider.set(card.provider, (byProvider.get(card.provider) ?? 0) + 1);
|
|
202
|
+
let unnamed = 0;
|
|
203
|
+
const windows = [];
|
|
204
|
+
for (const card of scoped) {
|
|
205
|
+
const multi = (byProvider.get(card.provider) ?? 0) > 1;
|
|
206
|
+
let label = card.plan !== undefined ? `${card.name} · ${card.plan}` : card.name;
|
|
207
|
+
if (multi) {
|
|
208
|
+
unnamed = card.user !== undefined ? unnamed : unnamed + 1;
|
|
209
|
+
label += ` · ${card.user ?? `#${String(unnamed)}`}`;
|
|
210
|
+
}
|
|
211
|
+
card.cardLabel = label;
|
|
212
|
+
if (card.kind === 'balance') {
|
|
213
|
+
const balance = balanceOf(card.balance);
|
|
214
|
+
if (balance === undefined)
|
|
215
|
+
continue;
|
|
216
|
+
windows.push({
|
|
217
|
+
tier: 'balance',
|
|
218
|
+
remainingValue: balance.value,
|
|
219
|
+
unit: balance.unit,
|
|
220
|
+
card: label,
|
|
221
|
+
});
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
for (const span of card.windows) {
|
|
225
|
+
const window = windowOf(card, span);
|
|
226
|
+
if (window !== undefined)
|
|
227
|
+
windows.push(window);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
if (windows.length === 0) {
|
|
231
|
+
throw new QuotaQueryError('parse', 'no windows or balances in the report cards');
|
|
232
|
+
}
|
|
233
|
+
return { windows };
|
|
234
|
+
},
|
|
235
|
+
};
|
|
236
|
+
/**
|
|
237
|
+
* The magpie gateway detector: decides whether a base URL points at a
|
|
238
|
+
* magpie, caching both verdicts so the unsupported-path poll the panel
|
|
239
|
+
* keeps running never hammers the probe. The gateway answers every
|
|
240
|
+
* OpenAI-compatible hello (`GET /` and `GET /v1`) with
|
|
241
|
+
* `{"name":"magpie","version":…}` — a handshake it maintains precisely
|
|
242
|
+
* so clients can feature-detect it.
|
|
243
|
+
*/
|
|
244
|
+
export class MagpieDetector {
|
|
245
|
+
#cache = new Map();
|
|
246
|
+
#now;
|
|
247
|
+
/** @param now - clock seam; tests freeze it. */
|
|
248
|
+
constructor(now = Date.now) {
|
|
249
|
+
this.#now = now;
|
|
250
|
+
}
|
|
251
|
+
/** How long a POSITIVE verdict stays authoritative (the gateway is not
|
|
252
|
+
* going to stop being one). */
|
|
253
|
+
static HIT_TTL_MS = 10 * 60_000;
|
|
254
|
+
/** How long a NEGATIVE verdict holds — short, so a gateway that comes
|
|
255
|
+
* up between polls is found on the next one. */
|
|
256
|
+
static MISS_TTL_MS = 2 * 60_000;
|
|
257
|
+
/**
|
|
258
|
+
* Whether the base URL answers magpie's hello.
|
|
259
|
+
* @param input - the base URL (undefined reads the default gateway),
|
|
260
|
+
* the transport, and the optional gateway key (a shared gateway on
|
|
261
|
+
* another machine requires it; the loopback one does not mind).
|
|
262
|
+
*/
|
|
263
|
+
async detect(input) {
|
|
264
|
+
const base = magpieBaseOf(input.baseUrl);
|
|
265
|
+
const cached = this.#cache.get(base);
|
|
266
|
+
const ttl = cached?.hit === true ? MagpieDetector.HIT_TTL_MS : MagpieDetector.MISS_TTL_MS;
|
|
267
|
+
if (cached !== undefined && this.#now() - cached.at < ttl)
|
|
268
|
+
return cached.hit;
|
|
269
|
+
const headers = {};
|
|
270
|
+
if (input.apiKey !== undefined && input.apiKey !== '')
|
|
271
|
+
headers.authorization = `Bearer ${input.apiKey}`;
|
|
272
|
+
let hit = false;
|
|
273
|
+
try {
|
|
274
|
+
const body = await fetchJson({
|
|
275
|
+
provider: ROUTE_MAGPIE,
|
|
276
|
+
apiKey: '',
|
|
277
|
+
fetchFn: input.fetchFn,
|
|
278
|
+
...(input.signal !== undefined ? { signal: input.signal } : {}),
|
|
279
|
+
}, base, headers);
|
|
280
|
+
hit = typeof body === 'object' && body !== null
|
|
281
|
+
&& body.name === 'magpie';
|
|
282
|
+
}
|
|
283
|
+
catch {
|
|
284
|
+
hit = false;
|
|
285
|
+
}
|
|
286
|
+
this.#cache.set(base, { at: this.#now(), hit });
|
|
287
|
+
return hit;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
//# sourceMappingURL=magpie.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"magpie.js","sourceRoot":"","sources":["../../../src/quota/adapters/magpie.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAGH,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAEtC,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAE7C,yEAAyE;AACzE,MAAM,CAAC,MAAM,YAAY,GAAG,QAAQ,CAAA;AACpC;4DAC4D;AAC5D,MAAM,CAAC,MAAM,mBAAmB,GAAG,0BAA0B,CAAA;AAE7D,wEAAwE;AACxE,MAAM,uBAAuB,GAAG,MAAM,CAAA;AAEtC;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAAC,OAA2B;IACtD,MAAM,IAAI,GAAG,OAAO,KAAK,SAAS,IAAI,OAAO,KAAK,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,mBAAmB,CAAA;IACpF,OAAO,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAA;AAClC,CAAC;AAED,gDAAgD;AAChD,MAAM,UAAU,cAAc,CAAC,OAA2B;IACxD,MAAM,IAAI,GAAG,YAAY,CAAC,OAAO,CAAC,CAAA;IAClC,OAAO,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,uBAAuB,CAAC,CAAC,CAAC,GAAG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CAAA;QAE5E,MAAM,OAAO,GAA2B,EAAE,CAAA;QAC1C,IAAI,KAAK,CAAC,MAAM,KAAK,SAAS,IAAI,KAAK,CAAC,MAAM,KAAK,EAAE;YAAE,OAAO,CAAC,aAAa,GAAG,UAAU,KAAK,CAAC,MAAM,EAAE,CAAA;QACvG,IAAI,GAAG,GAAG,KAAK,CAAA;QACf,IAAI,CAAC;YACH,MAAM,IAAI,GAAG,MAAM,SAAS,CAC1B;gBACE,QAAQ,EAAE,YAAY;gBACtB,MAAM,EAAE,EAAE;gBACV,OAAO,EAAE,KAAK,CAAC,OAAO;gBACtB,GAAG,CAAC,KAAK,CAAC,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;aAChE,EACD,IAAI,EACJ,OAAO,CACR,CAAA;YACD,GAAG,GAAG,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,IAAI;mBACzC,IAAgC,CAAC,IAAI,KAAK,QAAQ,CAAA;QAC1D,CAAC;QAAC,MAAM,CAAC;YACP,GAAG,GAAG,KAAK,CAAA;QACb,CAAC;QACD,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,EAAE,EAAE,IAAI,CAAC,IAAI,EAAE,EAAE,GAAG,EAAE,CAAC,CAAA;QAC/C,OAAO,GAAG,CAAA;IACZ,CAAC"}
|
|
@@ -29,13 +29,24 @@ export declare class ProviderTracker {
|
|
|
29
29
|
sightingOf(sessionId: string | undefined): ProviderSighting | undefined;
|
|
30
30
|
}
|
|
31
31
|
/**
|
|
32
|
-
* Resolve the provider
|
|
33
|
-
*
|
|
32
|
+
* Resolve the provider sighting a quota query should target: the
|
|
33
|
+
* tracker's latest (the active session's or the global last), else the
|
|
34
34
|
* host's default model selection (the `agent-default-model` settings
|
|
35
|
-
* section — what a brand-new, never-requesting session would use)
|
|
35
|
+
* section — what a brand-new, never-requesting session would use) as a
|
|
36
|
+
* model-less sighting.
|
|
36
37
|
* @param input - the tracker, the asking session, and a thunk reading the
|
|
37
38
|
* default selection's `provider` (undefined when no default is stored).
|
|
38
|
-
* @returns the
|
|
39
|
+
* @returns the sighting (its model scopes gateway adapters' cards), or
|
|
40
|
+
* undefined when nothing determinable exists.
|
|
41
|
+
*/
|
|
42
|
+
export declare function resolveCurrentSighting(input: {
|
|
43
|
+
tracker: ProviderTracker;
|
|
44
|
+
sessionId?: string;
|
|
45
|
+
defaultProvider?: () => string | undefined;
|
|
46
|
+
}): ProviderSighting | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* Resolve the provider ROUTE KEY alone — the key-only projection of
|
|
49
|
+
* {@link resolveCurrentSighting} for callers that do not scope by model.
|
|
39
50
|
*/
|
|
40
51
|
export declare function resolveCurrentProvider(input: {
|
|
41
52
|
tracker: ProviderTracker;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider-tracking.d.ts","sourceRoot":"","sources":["../../src/quota/provider-tracking.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,8CAA8C;AAC9C,MAAM,WAAW,gBAAgB;IAC/B,oEAAoE;IACpE,QAAQ,EAAE,MAAM,CAAA;IAChB,iCAAiC;IACjC,KAAK,EAAE,MAAM,CAAA;IACb,4CAA4C;IAC5C,EAAE,EAAE,MAAM,CAAA;CACX;AAED,sEAAsE;AACtE,qBAAa,eAAe;;IAI1B,mEAAmE;IACnE,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE,GAAE,MAAmB,GAAG,IAAI;IAO1F;;wDAEoD;IACpD,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS,GAAG,gBAAgB,GAAG,SAAS;CAOxE;AAED
|
|
1
|
+
{"version":3,"file":"provider-tracking.d.ts","sourceRoot":"","sources":["../../src/quota/provider-tracking.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,8CAA8C;AAC9C,MAAM,WAAW,gBAAgB;IAC/B,oEAAoE;IACpE,QAAQ,EAAE,MAAM,CAAA;IAChB,iCAAiC;IACjC,KAAK,EAAE,MAAM,CAAA;IACb,4CAA4C;IAC5C,EAAE,EAAE,MAAM,CAAA;CACX;AAED,sEAAsE;AACtE,qBAAa,eAAe;;IAI1B,mEAAmE;IACnE,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE,GAAE,MAAmB,GAAG,IAAI;IAO1F;;wDAEoD;IACpD,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS,GAAG,gBAAgB,GAAG,SAAS;CAOxE;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE;IAC5C,OAAO,EAAE,eAAe,CAAA;IACxB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,eAAe,CAAC,EAAE,MAAM,MAAM,GAAG,SAAS,CAAA;CAC3C,GAAG,gBAAgB,GAAG,SAAS,CAO/B;AAED;;;GAGG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE;IAC5C,OAAO,EAAE,eAAe,CAAA;IACxB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,eAAe,CAAC,EAAE,MAAM,MAAM,GAAG,SAAS,CAAA;CAC3C,GAAG,MAAM,GAAG,SAAS,CAErB"}
|
|
@@ -34,19 +34,30 @@ export class ProviderTracker {
|
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
/**
|
|
37
|
-
* Resolve the provider
|
|
38
|
-
*
|
|
37
|
+
* Resolve the provider sighting a quota query should target: the
|
|
38
|
+
* tracker's latest (the active session's or the global last), else the
|
|
39
39
|
* host's default model selection (the `agent-default-model` settings
|
|
40
|
-
* section — what a brand-new, never-requesting session would use)
|
|
40
|
+
* section — what a brand-new, never-requesting session would use) as a
|
|
41
|
+
* model-less sighting.
|
|
41
42
|
* @param input - the tracker, the asking session, and a thunk reading the
|
|
42
43
|
* default selection's `provider` (undefined when no default is stored).
|
|
43
|
-
* @returns the
|
|
44
|
+
* @returns the sighting (its model scopes gateway adapters' cards), or
|
|
45
|
+
* undefined when nothing determinable exists.
|
|
44
46
|
*/
|
|
45
|
-
export function
|
|
47
|
+
export function resolveCurrentSighting(input) {
|
|
46
48
|
const sighting = input.tracker.sightingOf(input.sessionId);
|
|
47
49
|
if (sighting !== undefined)
|
|
48
|
-
return sighting
|
|
50
|
+
return sighting;
|
|
49
51
|
const fallback = input.defaultProvider?.();
|
|
50
|
-
return fallback !== undefined && fallback !== ''
|
|
52
|
+
return fallback !== undefined && fallback !== ''
|
|
53
|
+
? { provider: fallback, model: '', at: 0 }
|
|
54
|
+
: undefined;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Resolve the provider ROUTE KEY alone — the key-only projection of
|
|
58
|
+
* {@link resolveCurrentSighting} for callers that do not scope by model.
|
|
59
|
+
*/
|
|
60
|
+
export function resolveCurrentProvider(input) {
|
|
61
|
+
return resolveCurrentSighting(input)?.provider;
|
|
51
62
|
}
|
|
52
63
|
//# sourceMappingURL=provider-tracking.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider-tracking.js","sourceRoot":"","sources":["../../src/quota/provider-tracking.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAYH,sEAAsE;AACtE,MAAM,OAAO,eAAe;IACjB,UAAU,GAAG,IAAI,GAAG,EAA4B,CAAA;IACzD,KAAK,CAA8B;IAEnC,mEAAmE;IACnE,OAAO,CAAC,SAAiB,EAAE,QAAgB,EAAE,KAAa,EAAE,KAAa,IAAI,CAAC,GAAG,EAAE;QACjF,IAAI,SAAS,KAAK,EAAE,IAAI,QAAQ,KAAK,EAAE;YAAE,OAAM;QAC/C,MAAM,QAAQ,GAAqB,EAAE,QAAQ,EAAE,KAAK,EAAE,EAAE,EAAE,CA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KAAK,SAAS;gBAAE,OAAO,MAAM,CAAA;QACzC,CAAC;QACD,OAAO,IAAI,CAAC,KAAK,CAAA;IACnB,CAAC;CACF;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,sBAAsB,CAAC,KAItC;IACC,MAAM,QAAQ,GAAG,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,KAAK,CAAC,SAAS,CAAC,CAAA;IAC1D,IAAI,QAAQ,KAAK,SAAS;QAAE,OAAO,QAAQ,CAAA;IAC3C,MAAM,QAAQ,GAAG,KAAK,CAAC,eAAe,EAAE,EAAE,CAAA;IAC1C,OAAO,QAAQ,KAAK,SAAS,IAAI,QAAQ,KAAK,EAAE;QAC9C,CAAC,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,KAAK,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE;QAC1C,CAAC,CAAC,SAAS,CAAA;AACf,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,sBAAsB,CAAC,KAItC;IACC,OAAO,sBAAsB,CAAC,KAAK,CAAC,EAAE,QAAQ,CAAA;AAChD,CAAC"}
|
|
@@ -14,9 +14,9 @@ export { QUOTA_PATH } from '../wire.ts';
|
|
|
14
14
|
* Build the quota route over the orchestration service.
|
|
15
15
|
* @param snapshot - resolves the payload for one asking session (the
|
|
16
16
|
* `session` query parameter, undefined when absent) and its model chip's
|
|
17
|
-
* current selection (
|
|
18
|
-
*
|
|
17
|
+
* current selection (`provider` + `model`, undefined when the browser
|
|
18
|
+
* could not read them).
|
|
19
19
|
* @returns the exact GET route answering the quota JSON.
|
|
20
20
|
*/
|
|
21
|
-
export declare function createQuotaRoute(snapshot: (sessionId: string | undefined, providerHint: string | undefined) => Promise<QuotaPayload>): WebRoute;
|
|
21
|
+
export declare function createQuotaRoute(snapshot: (sessionId: string | undefined, providerHint: string | undefined, modelHint: string | undefined) => Promise<QuotaPayload>): WebRoute;
|
|
22
22
|
//# sourceMappingURL=quota-route.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"quota-route.d.ts","sourceRoot":"","sources":["../../src/quota/quota-route.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iCAAiC,CAAA;AAC/D,OAAO,EAAc,KAAK,YAAY,EAAE,MAAM,YAAY,CAAA;AAG1D,uEAAuE;AACvE,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAEvC;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS,EAAE,YAAY,EAAE,MAAM,GAAG,SAAS,KAAK,OAAO,CAAC,YAAY,CAAC,GAAG,QAAQ,
|
|
1
|
+
{"version":3,"file":"quota-route.d.ts","sourceRoot":"","sources":["../../src/quota/quota-route.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iCAAiC,CAAA;AAC/D,OAAO,EAAc,KAAK,YAAY,EAAE,MAAM,YAAY,CAAA;AAG1D,uEAAuE;AACvE,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAEvC;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS,EAAE,YAAY,EAAE,MAAM,GAAG,SAAS,EAAE,SAAS,EAAE,MAAM,GAAG,SAAS,KAAK,OAAO,CAAC,YAAY,CAAC,GAAG,QAAQ,CAkC9K"}
|
package/lib/quota/quota-route.js
CHANGED
|
@@ -14,8 +14,8 @@ export { QUOTA_PATH } from "../wire.js";
|
|
|
14
14
|
* Build the quota route over the orchestration service.
|
|
15
15
|
* @param snapshot - resolves the payload for one asking session (the
|
|
16
16
|
* `session` query parameter, undefined when absent) and its model chip's
|
|
17
|
-
* current selection (
|
|
18
|
-
*
|
|
17
|
+
* current selection (`provider` + `model`, undefined when the browser
|
|
18
|
+
* could not read them).
|
|
19
19
|
* @returns the exact GET route answering the quota JSON.
|
|
20
20
|
*/
|
|
21
21
|
export function createQuotaRoute(snapshot) {
|
|
@@ -39,7 +39,7 @@ export function createQuotaRoute(snapshot) {
|
|
|
39
39
|
return value === null || value === '' ? undefined : value;
|
|
40
40
|
};
|
|
41
41
|
try {
|
|
42
|
-
const payload = await snapshot(pick('session'), pick('provider'));
|
|
42
|
+
const payload = await snapshot(pick('session'), pick('provider'), pick('model'));
|
|
43
43
|
res.writeHead(200, {
|
|
44
44
|
'content-type': 'application/json; charset=utf-8',
|
|
45
45
|
// The payload changes with the provider's own meters; never cache.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"quota-route.js","sourceRoot":"","sources":["../../src/quota/quota-route.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAE,UAAU,EAAqB,MAAM,YAAY,CAAA;AAC1D,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AAErD,uEAAuE;AACvE,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAEvC;;;;;;;GAOG;AACH,MAAM,UAAU,gBAAgB,CAAC,
|
|
1
|
+
{"version":3,"file":"quota-route.js","sourceRoot":"","sources":["../../src/quota/quota-route.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAE,UAAU,EAAqB,MAAM,YAAY,CAAA;AAC1D,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AAErD,uEAAuE;AACvE,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAEvC;;;;;;;GAOG;AACH,MAAM,UAAU,gBAAgB,CAAC,QAAmI;IAClK,OAAO;QACL,IAAI,EAAE,OAAO;QACb,IAAI,EAAE,UAAU;QAChB,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE;YAC1B,IAAI,GAAG,CAAC,MAAM,KAAK,KAA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EAAE,CAAC,CAAA;gBACzE,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAA;YAC5F,CAAC;QACH,CAAC;KACF,CAAA;AACH,CAAC"}
|
|
@@ -16,12 +16,17 @@
|
|
|
16
16
|
* @module token-usage/quota/quota-service
|
|
17
17
|
*/
|
|
18
18
|
import type { QuotaPayload } from '../wire.ts';
|
|
19
|
+
import { MagpieDetector } from './adapters/magpie.ts';
|
|
20
|
+
import type { ProviderSighting } from './provider-tracking.ts';
|
|
19
21
|
import { type ResolvedQuotaCredentials } from './credentials.ts';
|
|
20
22
|
import type { AccountBalanceReader } from './types.ts';
|
|
21
23
|
/** The seams the service runs over (index.ts wires the host services in). */
|
|
22
24
|
export interface QuotaServiceDeps {
|
|
23
|
-
/** Route key of the provider the asking session uses, if
|
|
24
|
-
|
|
25
|
+
/** Route key + model of the provider the asking session uses, if
|
|
26
|
+
* determinable (the request tracker's latest sighting, else the default
|
|
27
|
+
* selection). The model rides along for adapters that scope a whole
|
|
28
|
+
* gateway's report by it (magpie). */
|
|
29
|
+
resolveSighting: (sessionId: string | undefined) => ProviderSighting | undefined;
|
|
25
30
|
/** Credential chain seam; see credentials.ts. */
|
|
26
31
|
resolveCredentials: (provider: string) => Promise<ResolvedQuotaCredentials>;
|
|
27
32
|
/** Transport (global fetch in production, a stub in tests). */
|
|
@@ -34,6 +39,9 @@ export interface QuotaServiceDeps {
|
|
|
34
39
|
now?: () => number;
|
|
35
40
|
/** DeepSeek account wallets. Absent readers make that route `signed-out`. */
|
|
36
41
|
readAccountBalance?: AccountBalanceReader;
|
|
42
|
+
/** The magpie-gateway probe consulted when no registry adapter matches
|
|
43
|
+
* a route; tests stub it. Defaults to a plain detector instance. */
|
|
44
|
+
magpieDetector?: MagpieDetector;
|
|
37
45
|
}
|
|
38
46
|
/** The quota orchestration service. */
|
|
39
47
|
export declare class QuotaService {
|
|
@@ -50,8 +58,10 @@ export declare class QuotaService {
|
|
|
50
58
|
* @param providerHint - the provider the session's model chip currently
|
|
51
59
|
* selects (the host-reported NEXT selection), when the browser could read
|
|
52
60
|
* one. It wins over the request tracker: the chip is the user's live
|
|
53
|
-
* intent
|
|
61
|
+
* intent.
|
|
62
|
+
* @param modelHint - that chip selection's model, paired with the live
|
|
63
|
+
* provider. Without a provider hint, only the tracker's model is trusted.
|
|
54
64
|
*/
|
|
55
|
-
snapshot(sessionId?: string, providerHint?: string): Promise<QuotaPayload>;
|
|
65
|
+
snapshot(sessionId?: string, providerHint?: string, modelHint?: string): Promise<QuotaPayload>;
|
|
56
66
|
}
|
|
57
67
|
//# sourceMappingURL=quota-service.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"quota-service.d.ts","sourceRoot":"","sources":["../../src/quota/quota-service.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,KAAK,EAAgB,YAAY,EAAiB,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"quota-service.d.ts","sourceRoot":"","sources":["../../src/quota/quota-service.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,KAAK,EAAgB,YAAY,EAAiB,MAAM,YAAY,CAAA;AAE3E,OAAO,EAAiB,cAAc,EAAE,MAAM,sBAAsB,CAAA;AACpE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAG9D,OAAO,EAAsB,KAAK,wBAAwB,EAAE,MAAM,kBAAkB,CAAA;AACpF,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAA;AAUtD,6EAA6E;AAC7E,MAAM,WAAW,gBAAgB;IAC/B;;;0CAGsC;IACtC,eAAe,EAAE,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS,KAAK,gBAAgB,GAAG,SAAS,CAAA;IAChF,iDAAiD;IACjD,kBAAkB,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,wBAAwB,CAAC,CAAA;IAC3E,+DAA+D;IAC/D,OAAO,CAAC,EAAE,OAAO,KAAK,CAAA;IACtB,2DAA2D;IAC3D,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,qEAAqE;IACrE,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,mCAAmC;IACnC,GAAG,CAAC,EAAE,MAAM,MAAM,CAAA;IAClB,6EAA6E;IAC7E,kBAAkB,CAAC,EAAE,oBAAoB,CAAA;IACzC;wEACoE;IACpE,cAAc,CAAC,EAAE,cAAc,CAAA;CAChC;AA+CD,uCAAuC;AACvC,qBAAa,YAAY;;gBAMX,IAAI,EAAE,gBAAgB;IAKlC,8CAA8C;IAC9C,IAAI,WAAW,IAAI,MAAM,CAExB;IAED;;;;;;;;;;;;OAYG;IACG,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;CA6HrG"}
|
|
@@ -16,12 +16,16 @@
|
|
|
16
16
|
* @module token-usage/quota/quota-service
|
|
17
17
|
*/
|
|
18
18
|
import { resolveQuotaAdapter } from "./registry.js";
|
|
19
|
+
import { magpieAdapter, MagpieDetector } from "./adapters/magpie.js";
|
|
19
20
|
import { QuotaQueryError } from "./types.js";
|
|
20
21
|
import { withCatalogBaseUrl } from "./credentials.js";
|
|
21
22
|
/** Default cache lifetime of one provider's ok result. */
|
|
22
23
|
const DEFAULT_CACHE_TTL_MS = 45_000;
|
|
23
24
|
/** Per-query deadline: quota endpoints are small; anything slower is dead. */
|
|
24
25
|
const QUERY_TIMEOUT_MS = 10_000;
|
|
26
|
+
/** The gateway probe's own deadline — a hello that takes longer than this
|
|
27
|
+
* is a dead route, not a magpie. */
|
|
28
|
+
const PROBE_TIMEOUT_MS = 5_000;
|
|
25
29
|
/** Map any thrown adapter failure onto the wire's error vocabulary. */
|
|
26
30
|
function toQuotaFailure(provider, providerName, adapter, error, intervalSec, now) {
|
|
27
31
|
const normalized = error instanceof QuotaQueryError
|
|
@@ -54,8 +58,10 @@ export class QuotaService {
|
|
|
54
58
|
#deps;
|
|
55
59
|
#cache = new Map();
|
|
56
60
|
#inflight = new Map();
|
|
61
|
+
#magpieDetector;
|
|
57
62
|
constructor(deps) {
|
|
58
63
|
this.#deps = deps;
|
|
64
|
+
this.#magpieDetector = deps.magpieDetector ?? new MagpieDetector(deps.now ?? Date.now);
|
|
59
65
|
}
|
|
60
66
|
/** The poll cadence every payload carries. */
|
|
61
67
|
get intervalSec() {
|
|
@@ -70,20 +76,39 @@ export class QuotaService {
|
|
|
70
76
|
* @param providerHint - the provider the session's model chip currently
|
|
71
77
|
* selects (the host-reported NEXT selection), when the browser could read
|
|
72
78
|
* one. It wins over the request tracker: the chip is the user's live
|
|
73
|
-
* intent
|
|
79
|
+
* intent.
|
|
80
|
+
* @param modelHint - that chip selection's model, paired with the live
|
|
81
|
+
* provider. Without a provider hint, only the tracker's model is trusted.
|
|
74
82
|
*/
|
|
75
|
-
async snapshot(sessionId, providerHint) {
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
83
|
+
async snapshot(sessionId, providerHint, modelHint) {
|
|
84
|
+
const sighting = this.#deps.resolveSighting(sessionId);
|
|
85
|
+
const hintLive = providerHint !== undefined && providerHint !== '';
|
|
86
|
+
const provider = hintLive ? providerHint : sighting?.provider;
|
|
79
87
|
if (provider === undefined) {
|
|
80
88
|
return { status: 'no-provider', intervalSec: this.intervalSec };
|
|
81
89
|
}
|
|
90
|
+
const model = hintLive && modelHint !== undefined && modelHint !== ''
|
|
91
|
+
? modelHint
|
|
92
|
+
: sighting !== undefined && sighting.provider === provider ? sighting.model : undefined;
|
|
82
93
|
const credentials = withCatalogBaseUrl(provider, await this.#deps.resolveCredentials(provider));
|
|
83
|
-
|
|
94
|
+
let adapter = resolveQuotaAdapter({
|
|
84
95
|
provider,
|
|
85
96
|
...(credentials.baseUrl !== undefined ? { baseUrl: credentials.baseUrl } : {}),
|
|
86
97
|
});
|
|
98
|
+
if (adapter === undefined && credentials.baseUrl !== undefined) {
|
|
99
|
+
// A route no table knows, pointing at a base URL: ask the magpie
|
|
100
|
+
// hello — a user-declared gateway profile is exactly the shape the
|
|
101
|
+
// host table cannot enumerate. The detector caches both verdicts,
|
|
102
|
+
// so this stays off the hot path.
|
|
103
|
+
const hit = await this.#magpieDetector.detect({
|
|
104
|
+
baseUrl: credentials.baseUrl,
|
|
105
|
+
...(credentials.apiKey !== undefined ? { apiKey: credentials.apiKey } : {}),
|
|
106
|
+
fetchFn: this.#deps.fetchFn ?? globalThis.fetch,
|
|
107
|
+
signal: AbortSignal.timeout(PROBE_TIMEOUT_MS),
|
|
108
|
+
});
|
|
109
|
+
if (hit)
|
|
110
|
+
adapter = magpieAdapter;
|
|
111
|
+
}
|
|
87
112
|
if (adapter === undefined) {
|
|
88
113
|
return {
|
|
89
114
|
status: 'unsupported',
|
|
@@ -107,29 +132,34 @@ export class QuotaService {
|
|
|
107
132
|
},
|
|
108
133
|
};
|
|
109
134
|
}
|
|
110
|
-
|
|
135
|
+
// The cache key includes the model: a gateway adapter's windows are a
|
|
136
|
+
// function of both (the model scopes the cards), so two sessions on
|
|
137
|
+
// the same route but different models keep their own snapshots.
|
|
138
|
+
const cacheKey = `${provider}\u0000${model ?? ''}`;
|
|
139
|
+
const cached = this.#cached(cacheKey);
|
|
111
140
|
if (cached !== undefined)
|
|
112
141
|
return cached;
|
|
113
|
-
const running = this.#inflight.get(
|
|
142
|
+
const running = this.#inflight.get(cacheKey);
|
|
114
143
|
if (running !== undefined)
|
|
115
144
|
return running;
|
|
116
|
-
const query = this.#query(provider, providerName, credentials, adapter);
|
|
117
|
-
this.#inflight.set(
|
|
145
|
+
const query = this.#query(provider, model, providerName, credentials, adapter);
|
|
146
|
+
this.#inflight.set(cacheKey, query);
|
|
118
147
|
try {
|
|
119
148
|
return await query;
|
|
120
149
|
}
|
|
121
150
|
finally {
|
|
122
|
-
this.#inflight.delete(
|
|
151
|
+
this.#inflight.delete(cacheKey);
|
|
123
152
|
}
|
|
124
153
|
}
|
|
125
154
|
/** The fresh-or-cached ok result, or a live error, of one provider query. */
|
|
126
|
-
async #query(provider, providerName, credentials, adapter) {
|
|
155
|
+
async #query(provider, model, providerName, credentials, adapter) {
|
|
127
156
|
try {
|
|
128
157
|
const result = await adapter.query({
|
|
129
158
|
provider,
|
|
130
159
|
...(credentials.baseUrl !== undefined ? { baseUrl: credentials.baseUrl } : {}),
|
|
131
160
|
apiKey: credentials.apiKey ?? '',
|
|
132
|
-
|
|
161
|
+
...(model !== undefined && model !== '' ? { model } : {}),
|
|
162
|
+
signal: AbortSignal.timeout(adapter.queryTimeoutMs ?? QUERY_TIMEOUT_MS),
|
|
133
163
|
...(this.#deps.fetchFn !== undefined ? { fetchFn: this.#deps.fetchFn } : { fetchFn: globalThis.fetch }),
|
|
134
164
|
...(this.#deps.readAccountBalance !== undefined ? { readAccountBalance: this.#deps.readAccountBalance } : {}),
|
|
135
165
|
});
|
|
@@ -143,12 +173,12 @@ export class QuotaService {
|
|
|
143
173
|
...(result.planTier !== undefined ? { planTier: result.planTier } : {}),
|
|
144
174
|
windows: result.windows,
|
|
145
175
|
};
|
|
146
|
-
this.#cache.set(provider
|
|
176
|
+
this.#cache.set(`${provider}\u0000${model ?? ''}`, { at: this.#now(), payload });
|
|
147
177
|
return payload;
|
|
148
178
|
}
|
|
149
179
|
catch (error) {
|
|
150
180
|
// Errors do not cache: the panel's retry must actually re-query.
|
|
151
|
-
this.#cache.delete(provider);
|
|
181
|
+
this.#cache.delete(`${provider}\u0000${model ?? ''}`);
|
|
152
182
|
return toQuotaFailure(provider, providerName, adapter, error, this.intervalSec, this.#now());
|
|
153
183
|
}
|
|
154
184
|
}
|