@chenlongapps/opencode-token-usage 0.4.28 → 0.5.1
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 +13 -9
- package/README.zh-CN.md +13 -9
- package/dist/aliases.d.ts +7 -5
- package/dist/aliases.js +32 -24
- package/dist/overrides.d.ts +7 -8
- package/dist/overrides.js +20 -49
- package/dist/performance.d.ts +3 -1
- package/dist/performance.js +158 -50
- package/dist/price-identity.d.ts +38 -0
- package/dist/price-identity.js +75 -0
- package/dist/prices.generated.d.ts +2 -3912
- package/dist/prices.generated.js +1401 -306
- package/dist/pricing.d.ts +3 -23
- package/dist/pricing.js +4 -97
- package/dist/tui.js +35 -25
- package/dist/usage.d.ts +5 -5
- package/dist/usage.js +34 -12
- package/docs/pricing.md +27 -26
- package/package.json +1 -1
package/dist/performance.js
CHANGED
|
@@ -6,20 +6,31 @@ export const LIVE_MIN_TOKENS = 20;
|
|
|
6
6
|
export const LIVE_MIN_BATCHES = 3;
|
|
7
7
|
export const LIVE_EWMA_ALPHA = 0.35;
|
|
8
8
|
export const LIVE_BYTES_PER_TOKEN = 4;
|
|
9
|
+
const SLOW_WINDOW_MS = 30_000;
|
|
10
|
+
const SLOW_MIN_SPAN_MS = 3_000;
|
|
11
|
+
const SLOW_MIN_BYTES = 12;
|
|
12
|
+
const SLOW_MIN_INTERVAL_MS = 500;
|
|
13
|
+
const SLOW_MIN_QUIET_MS = 6_000;
|
|
14
|
+
const SLOW_INTERVAL_FACTOR = 3;
|
|
15
|
+
const SLOW_RECENT_INTERVALS = 5;
|
|
16
|
+
const SLOW_MAX_BURST_SHARE = 0.75;
|
|
17
|
+
const FAST_MIN_BYTES = LIVE_MIN_TOKENS * LIVE_BYTES_PER_TOKEN;
|
|
18
|
+
const utf8 = new TextEncoder();
|
|
9
19
|
/** Quiet streams need a server liveness check, not a maximum tool-wait duration. */
|
|
10
20
|
export const ACTIVITY_CHECK_MS = 60_000;
|
|
11
21
|
/** Requires three merged batches; the first is a baseline, not newly generated bytes. */
|
|
12
|
-
export function estimatedLiveTps(samples) {
|
|
13
|
-
if (samples.length < LIVE_MIN_BATCHES)
|
|
22
|
+
export function estimatedLiveTps(samples, start = 0) {
|
|
23
|
+
if (samples.length - start < LIVE_MIN_BATCHES)
|
|
14
24
|
return undefined;
|
|
15
|
-
const first = samples[
|
|
25
|
+
const first = samples[start];
|
|
16
26
|
const last = samples[samples.length - 1];
|
|
17
27
|
const duration = last.time - first.time;
|
|
18
28
|
if (!Number.isFinite(duration) || duration < LIVE_MIN_SPAN_MS)
|
|
19
29
|
return undefined;
|
|
20
30
|
let bytes = 0;
|
|
21
31
|
let time = first.time;
|
|
22
|
-
for (
|
|
32
|
+
for (let index = start; index < samples.length; index++) {
|
|
33
|
+
const sample = samples[index];
|
|
23
34
|
if (!Number.isFinite(sample.time) || sample.time < time || !Number.isFinite(sample.bytes) || sample.bytes < 0)
|
|
24
35
|
return undefined;
|
|
25
36
|
if (sample.time > first.time)
|
|
@@ -30,6 +41,48 @@ export function estimatedLiveTps(samples) {
|
|
|
30
41
|
return undefined;
|
|
31
42
|
return (bytes / LIVE_BYTES_PER_TOKEN) / (duration / 1_000);
|
|
32
43
|
}
|
|
44
|
+
function estimateSlow(samples, since) {
|
|
45
|
+
const end = samples.length - 1;
|
|
46
|
+
if (end < 2)
|
|
47
|
+
return undefined;
|
|
48
|
+
const last = samples[end];
|
|
49
|
+
const recent = last.time - samples[end - 1].time;
|
|
50
|
+
const previous = samples[end - 1].time - samples[end - 2].time;
|
|
51
|
+
// One long pause between dense deliveries isn't evidence of a slow stream.
|
|
52
|
+
const min = Math.max(SLOW_MIN_INTERVAL_MS, Math.max(recent, previous) / SLOW_INTERVAL_FACTOR);
|
|
53
|
+
const max = Math.min(recent, previous) * SLOW_INTERVAL_FACTOR;
|
|
54
|
+
if (Math.min(recent, previous) < min)
|
|
55
|
+
return undefined;
|
|
56
|
+
const intervals = [];
|
|
57
|
+
let start = end;
|
|
58
|
+
let bytes = 0;
|
|
59
|
+
let largest = last;
|
|
60
|
+
while (start > 0) {
|
|
61
|
+
const batch = samples[start], baseline = samples[start - 1];
|
|
62
|
+
const gap = batch.time - baseline.time;
|
|
63
|
+
if (baseline.time < since || gap < min || gap > max)
|
|
64
|
+
break;
|
|
65
|
+
if (intervals.length < SLOW_RECENT_INTERVALS)
|
|
66
|
+
intervals.push(gap);
|
|
67
|
+
bytes += batch.bytes;
|
|
68
|
+
if (batch.bytes > largest.bytes)
|
|
69
|
+
largest = batch;
|
|
70
|
+
start--;
|
|
71
|
+
}
|
|
72
|
+
const baseline = samples[start];
|
|
73
|
+
const duration = last.time - baseline.time;
|
|
74
|
+
if (end - start + 1 < LIVE_MIN_BATCHES || duration < SLOW_MIN_SPAN_MS || bytes + baseline.bytes < SLOW_MIN_BYTES)
|
|
75
|
+
return undefined;
|
|
76
|
+
// Don't give one substantial buffered burst a long-lived slow estimate.
|
|
77
|
+
// Rebaseline at that delivery rather than capping a legitimate sampled rate.
|
|
78
|
+
if (largest.bytes >= FAST_MIN_BYTES && largest.bytes > bytes * SLOW_MAX_BURST_SHARE)
|
|
79
|
+
return { restartAt: largest.time };
|
|
80
|
+
// The lower median also avoids inflating the deadline with one long gap when
|
|
81
|
+
// there are only two intervals. The baseline counts only toward readiness.
|
|
82
|
+
intervals.sort((a, b) => a - b);
|
|
83
|
+
const quiet = Math.min(SLOW_WINDOW_MS, Math.max(SLOW_MIN_QUIET_MS, SLOW_INTERVAL_FACTOR * intervals[Math.floor((intervals.length - 1) / 2)]));
|
|
84
|
+
return { tps: (bytes / LIVE_BYTES_PER_TOKEN) / (duration / 1_000), duration, quiet };
|
|
85
|
+
}
|
|
33
86
|
/** Time-based EWMA: elapsed time determines the weight, never delta count. */
|
|
34
87
|
export function smoothLiveTps(previous, raw, elapsed = LIVE_MIN_SPAN_MS) {
|
|
35
88
|
if (previous === undefined || !Number.isFinite(previous))
|
|
@@ -192,10 +245,82 @@ export class PerformanceMonitor {
|
|
|
192
245
|
step.samples = [];
|
|
193
246
|
delete step.bucketStarted;
|
|
194
247
|
delete step.observedAt;
|
|
195
|
-
delete step.
|
|
196
|
-
delete step.
|
|
248
|
+
delete step.fastSince;
|
|
249
|
+
delete step.slowSince;
|
|
250
|
+
delete step.slowQuiet;
|
|
251
|
+
delete step.live;
|
|
197
252
|
delete step.smoothedAt;
|
|
198
253
|
}
|
|
254
|
+
breakDelivery(step, time) {
|
|
255
|
+
const wasLive = step.live !== undefined;
|
|
256
|
+
step.sampleBoundary = time;
|
|
257
|
+
this.resetLive(step);
|
|
258
|
+
if (wasLive)
|
|
259
|
+
this.publish(step.sessionID);
|
|
260
|
+
}
|
|
261
|
+
recordDelta(step, time, bytes) {
|
|
262
|
+
const now = this.monotonic();
|
|
263
|
+
const last = step.samples.at(-1);
|
|
264
|
+
const gap = last ? time - last.time : 0; // Server time, used only against server time.
|
|
265
|
+
if (gap >= (step.slowQuiet ?? SLOW_WINDOW_MS))
|
|
266
|
+
this.resetLive(step);
|
|
267
|
+
if (gap >= LIVE_WINDOW_MS || (step.observedAt !== undefined && now - step.observedAt >= LIVE_WINDOW_MS)) {
|
|
268
|
+
step.fastSince = time;
|
|
269
|
+
delete step.smoothedAt;
|
|
270
|
+
}
|
|
271
|
+
const batch = step.samples.at(-1);
|
|
272
|
+
if (batch && step.bucketStarted !== undefined && time - step.bucketStarted < LIVE_SAMPLE_MS) {
|
|
273
|
+
// Include a whole delivery burst in one batch, including the initial baseline.
|
|
274
|
+
batch.time = time;
|
|
275
|
+
batch.bytes += bytes;
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
step.bucketStarted = time;
|
|
279
|
+
step.samples.push({ time, bytes });
|
|
280
|
+
}
|
|
281
|
+
// Keep at most ~301 entries (100ms batches); once fast TPS is established,
|
|
282
|
+
// batches older than its window can't seed either estimate.
|
|
283
|
+
const oldest = Math.max(time - SLOW_WINDOW_MS, (step.slowSince ?? -Infinity) - LIVE_WINDOW_MS);
|
|
284
|
+
while (step.samples[0].time < oldest)
|
|
285
|
+
step.samples.shift();
|
|
286
|
+
step.observedAt = now;
|
|
287
|
+
// Fast readiness is response-wide and survives window resets.
|
|
288
|
+
if (step.bytes >= FAST_MIN_BYTES) {
|
|
289
|
+
const cutoff = Math.max(step.fastSince ?? -Infinity, time - LIVE_WINDOW_MS);
|
|
290
|
+
let start = step.samples.length - 1;
|
|
291
|
+
while (start > 0 && step.samples[start - 1].time >= cutoff)
|
|
292
|
+
start--;
|
|
293
|
+
const raw = estimatedLiveTps(step.samples, start);
|
|
294
|
+
if (raw !== undefined) {
|
|
295
|
+
const previous = step.smoothedAt === undefined ? undefined : step.live;
|
|
296
|
+
const elapsed = time - (step.smoothedAt ?? time);
|
|
297
|
+
if (previous && elapsed < LIVE_SAMPLE_MS)
|
|
298
|
+
previous.expiresAt = now + LIVE_WINDOW_MS;
|
|
299
|
+
else {
|
|
300
|
+
step.live = {
|
|
301
|
+
tps: smoothLiveTps(previous?.tps, raw, elapsed),
|
|
302
|
+
duration: time - step.samples[start].time,
|
|
303
|
+
expiresAt: now + LIVE_WINDOW_MS,
|
|
304
|
+
};
|
|
305
|
+
step.smoothedAt = time;
|
|
306
|
+
}
|
|
307
|
+
step.slowSince = time;
|
|
308
|
+
delete step.slowQuiet;
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
delete step.smoothedAt;
|
|
313
|
+
const slow = estimateSlow(step.samples, step.slowSince ?? -Infinity);
|
|
314
|
+
if (slow && slow.restartAt === undefined) {
|
|
315
|
+
step.slowQuiet = slow.quiet;
|
|
316
|
+
step.live = { tps: slow.tps, duration: slow.duration, expiresAt: now + slow.quiet };
|
|
317
|
+
}
|
|
318
|
+
else {
|
|
319
|
+
if (slow)
|
|
320
|
+
step.slowSince = slow.restartAt;
|
|
321
|
+
delete step.live;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
199
324
|
touch(step, created) {
|
|
200
325
|
step.revision++;
|
|
201
326
|
step.checkedAt = this.monotonic();
|
|
@@ -286,9 +411,14 @@ export class PerformanceMonitor {
|
|
|
286
411
|
this.active.delete(key);
|
|
287
412
|
changed.add(step.sessionID);
|
|
288
413
|
}
|
|
289
|
-
else
|
|
290
|
-
|
|
291
|
-
|
|
414
|
+
else {
|
|
415
|
+
if (step.live && step.live.expiresAt <= monotonic) {
|
|
416
|
+
delete step.live;
|
|
417
|
+
delete step.smoothedAt;
|
|
418
|
+
changed.add(step.sessionID);
|
|
419
|
+
}
|
|
420
|
+
if (step.observedAt !== undefined && monotonic - step.observedAt >= (step.slowQuiet ?? SLOW_WINDOW_MS))
|
|
421
|
+
this.resetLive(step);
|
|
292
422
|
}
|
|
293
423
|
}
|
|
294
424
|
for (const [key, sample] of this.observed) {
|
|
@@ -314,8 +444,10 @@ export class PerformanceMonitor {
|
|
|
314
444
|
next = Math.min(next, step.checkedAt + ACTIVITY_CHECK_MS - monotonic);
|
|
315
445
|
}
|
|
316
446
|
// Local monotonic age, never client epoch minus a remote event timestamp.
|
|
447
|
+
if (step.live)
|
|
448
|
+
next = Math.min(next, step.live.expiresAt - monotonic);
|
|
317
449
|
if (step.observedAt !== undefined)
|
|
318
|
-
next = Math.min(next, step.observedAt +
|
|
450
|
+
next = Math.min(next, step.observedAt + (step.slowQuiet ?? SLOW_WINDOW_MS) - monotonic);
|
|
319
451
|
}
|
|
320
452
|
for (const sample of this.observed.values()) {
|
|
321
453
|
if (retain && sample.expiresAt !== undefined)
|
|
@@ -370,8 +502,11 @@ export class PerformanceMonitor {
|
|
|
370
502
|
if (event.type === "session.execution.started"
|
|
371
503
|
|| (event.type === "session.status" && ["busy", "retry"].includes(String(data.status?.type)))) {
|
|
372
504
|
for (const step of this.active.values()) {
|
|
373
|
-
if (step.sessionID
|
|
374
|
-
|
|
505
|
+
if (step.sessionID !== sessionID || !finite(event.created) || event.created < step.latest)
|
|
506
|
+
continue;
|
|
507
|
+
this.touch(step, event.created);
|
|
508
|
+
if (event.type === "session.status" && data.status?.type === "retry")
|
|
509
|
+
this.breakDelivery(step, event.created);
|
|
375
510
|
}
|
|
376
511
|
this.scheduleCleanup();
|
|
377
512
|
return false;
|
|
@@ -442,12 +577,15 @@ export class PerformanceMonitor {
|
|
|
442
577
|
delete step.inactive;
|
|
443
578
|
delete step.reconciledAt;
|
|
444
579
|
delete step.streamed;
|
|
445
|
-
this.resetLive(step);
|
|
446
580
|
const sample = this.observed.get(key);
|
|
447
581
|
if (sample)
|
|
448
582
|
delete sample.expiresAt;
|
|
449
583
|
}
|
|
450
584
|
this.touch(step, event.created);
|
|
585
|
+
// Tool input can still be generated by the model. Tool execution markers
|
|
586
|
+
// and retries instead break delivery continuity without ending the step.
|
|
587
|
+
if (!event.type.startsWith("session.tool.input."))
|
|
588
|
+
this.breakDelivery(step, event.created);
|
|
451
589
|
this.scheduleCleanup();
|
|
452
590
|
return false;
|
|
453
591
|
}
|
|
@@ -494,36 +632,10 @@ export class PerformanceMonitor {
|
|
|
494
632
|
delete sample.expiresAt;
|
|
495
633
|
this.scheduleCleanup();
|
|
496
634
|
}
|
|
497
|
-
const sampleBytes =
|
|
635
|
+
const sampleBytes = utf8.encode(data.delta).byteLength;
|
|
498
636
|
step.bytes += sampleBytes;
|
|
499
|
-
if (step.
|
|
500
|
-
this.
|
|
501
|
-
const last = step.samples.at(-1);
|
|
502
|
-
if (last && step.bucketStarted !== undefined && event.created - step.bucketStarted < LIVE_SAMPLE_MS) {
|
|
503
|
-
// Include a whole delivery burst in one batch, including the initial baseline.
|
|
504
|
-
last.time = event.created;
|
|
505
|
-
last.bytes += sampleBytes;
|
|
506
|
-
}
|
|
507
|
-
else {
|
|
508
|
-
step.bucketStarted = event.created;
|
|
509
|
-
step.samples.push({ time: event.created, bytes: sampleBytes });
|
|
510
|
-
}
|
|
511
|
-
const cutoff = event.created - LIVE_WINDOW_MS;
|
|
512
|
-
while (step.samples.length > 0 && step.samples[0].time < cutoff)
|
|
513
|
-
step.samples.shift();
|
|
514
|
-
// The response-wide threshold includes the baseline and survives window resets.
|
|
515
|
-
const raw = step.bytes >= LIVE_MIN_TOKENS * LIVE_BYTES_PER_TOKEN ? estimatedLiveTps(step.samples) : undefined;
|
|
516
|
-
if (raw === undefined) {
|
|
517
|
-
delete step.liveTps;
|
|
518
|
-
delete step.liveDuration;
|
|
519
|
-
delete step.smoothedAt;
|
|
520
|
-
}
|
|
521
|
-
else if (step.smoothedAt === undefined || event.created - step.smoothedAt >= LIVE_SAMPLE_MS) {
|
|
522
|
-
step.liveTps = smoothLiveTps(step.liveTps, raw, event.created - (step.smoothedAt ?? event.created));
|
|
523
|
-
step.liveDuration = event.created - step.samples[0].time;
|
|
524
|
-
step.smoothedAt = event.created;
|
|
525
|
-
}
|
|
526
|
-
step.observedAt = this.monotonic();
|
|
637
|
+
if (step.sampleBoundary === undefined || event.created >= step.sampleBoundary)
|
|
638
|
+
this.recordDelta(step, event.created, sampleBytes);
|
|
527
639
|
step.last = Math.max(step.last ?? event.created, event.created);
|
|
528
640
|
if (step.first === undefined) {
|
|
529
641
|
step.first = event.created;
|
|
@@ -566,7 +678,7 @@ export class PerformanceMonitor {
|
|
|
566
678
|
const samples = [...this.observed.values()].filter(sample => sessions.has(sample.sessionID));
|
|
567
679
|
const result = performanceWithObservations(prepared, samples);
|
|
568
680
|
// Live throughput covers only observable deltas after the first one, over a
|
|
569
|
-
//
|
|
681
|
+
// bounded sliding window. TTFT keeps the started -> first wait separately, and
|
|
570
682
|
// completed provider usage still replaces this estimate via reconcile().
|
|
571
683
|
let weighted = 0;
|
|
572
684
|
let durationTotal = 0;
|
|
@@ -574,14 +686,10 @@ export class PerformanceMonitor {
|
|
|
574
686
|
for (const step of this.active.values()) {
|
|
575
687
|
if (!sessions.has(step.sessionID) || step.reconciledAt !== undefined)
|
|
576
688
|
continue;
|
|
577
|
-
if (step.
|
|
578
|
-
|| monotonic - step.observedAt >= LIVE_WINDOW_MS)
|
|
579
|
-
continue;
|
|
580
|
-
const duration = step.liveDuration;
|
|
581
|
-
if (!finite(duration) || duration < LIVE_MIN_SPAN_MS)
|
|
689
|
+
if (!step.live || step.live.expiresAt <= monotonic)
|
|
582
690
|
continue;
|
|
583
|
-
weighted += step.
|
|
584
|
-
durationTotal += duration;
|
|
691
|
+
weighted += step.live.tps * step.live.duration;
|
|
692
|
+
durationTotal += step.live.duration;
|
|
585
693
|
}
|
|
586
694
|
if (durationTotal > 0) {
|
|
587
695
|
result.tps = weighted / durationTotal;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { ModelRef, Price } from "./usage.js";
|
|
2
|
+
export interface SnapshotPrice {
|
|
3
|
+
lab: string;
|
|
4
|
+
id: string;
|
|
5
|
+
/** Omitted when the provider's ID equals the LAB's ID. */
|
|
6
|
+
sourceProvider?: string;
|
|
7
|
+
/** Unset for an official tariff (including reviewed overrides). */
|
|
8
|
+
reference?: true;
|
|
9
|
+
prices: readonly Price[];
|
|
10
|
+
/** Source URL for a reviewed override. Otherwise the catalog URL applies. */
|
|
11
|
+
source?: string;
|
|
12
|
+
/** Synthetic billing SKUs match known provider IDs only. */
|
|
13
|
+
scoped?: true;
|
|
14
|
+
zeroException?: string;
|
|
15
|
+
}
|
|
16
|
+
export interface PriceSnapshot {
|
|
17
|
+
verified: string;
|
|
18
|
+
source: string;
|
|
19
|
+
entries: readonly SnapshotPrice[];
|
|
20
|
+
/** LAB/SKU -> exact provider/SKU bindings; the empty key inhibits generic lookup. */
|
|
21
|
+
bindings: Readonly<Record<string, readonly string[]>>;
|
|
22
|
+
/** Paid LAB/SKU -> exact Free provider/SKUs with a verified counterpart. No rate copies. */
|
|
23
|
+
freeReferences: Readonly<Record<string, readonly string[]>>;
|
|
24
|
+
/** LAB IDs and bare IDs that must not fall through to a different LAB. */
|
|
25
|
+
labs: readonly string[];
|
|
26
|
+
ambiguous: readonly string[];
|
|
27
|
+
}
|
|
28
|
+
export declare const priceKey: (entry: Pick<SnapshotPrice, "lab" | "id">) => string;
|
|
29
|
+
export declare const sdkModes: Set<string>;
|
|
30
|
+
/** SDK cost is attached to a model ID, not to its selectable serving modes. */
|
|
31
|
+
export declare function billingMode(model: ModelRef): string | undefined;
|
|
32
|
+
/** Shared by the generator (to omit redundant bindings) and offline lookup.
|
|
33
|
+
* These spellings never establish a price candidate's identity. */
|
|
34
|
+
export declare function defaultIdentity(entries: readonly SnapshotPrice[], labs: readonly string[], ambiguous: readonly string[]): (providerID: string, raw: string) => string | undefined;
|
|
35
|
+
/** Exact identity resolution only. There is no runtime ranking of providers. */
|
|
36
|
+
export declare function snapshotLookup(snapshot: PriceSnapshot): (model: ModelRef) => SnapshotPrice | undefined;
|
|
37
|
+
/** The reference is separate from the Free SKU's own zero schedule. */
|
|
38
|
+
export declare function snapshotFreeReferenceLookup(snapshot: PriceSnapshot): (model: ModelRef) => SnapshotPrice | undefined;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { LAB_ALIASES, MODEL_ALIASES, PROVIDER_LABS } from "./aliases.js";
|
|
2
|
+
export const priceKey = (entry) => `${entry.lab}/${entry.id}`;
|
|
3
|
+
export const sdkModes = new Set(["fast", "flex", "priority", "pro", "ultrafast"]);
|
|
4
|
+
/** SDK cost is attached to a model ID, not to its selectable serving modes. */
|
|
5
|
+
export function billingMode(model) {
|
|
6
|
+
const mode = model.variant?.toLowerCase();
|
|
7
|
+
if (!mode || !sdkModes.has(mode))
|
|
8
|
+
return undefined;
|
|
9
|
+
const id = model.id.toLowerCase();
|
|
10
|
+
return !id.endsWith(`-${mode}`) && !id.endsWith(`:${mode}`) ? mode : undefined;
|
|
11
|
+
}
|
|
12
|
+
/** Shared by the generator (to omit redundant bindings) and offline lookup.
|
|
13
|
+
* These spellings never establish a price candidate's identity. */
|
|
14
|
+
export function defaultIdentity(entries, labs, ambiguous) {
|
|
15
|
+
const knownLabs = new Set(labs);
|
|
16
|
+
const blocked = new Set(ambiguous);
|
|
17
|
+
const keys = new Set(entries.filter(entry => !entry.scoped).map(priceKey));
|
|
18
|
+
const bare = new Map();
|
|
19
|
+
for (const entry of entries) {
|
|
20
|
+
const { id, scoped } = entry;
|
|
21
|
+
if (scoped)
|
|
22
|
+
continue;
|
|
23
|
+
bare.set(id, blocked.has(id) || bare.has(id) ? null : priceKey(entry));
|
|
24
|
+
}
|
|
25
|
+
const resolve = (key) => keys.has(key) ? key
|
|
26
|
+
: Object.hasOwn(MODEL_ALIASES, key) && keys.has(MODEL_ALIASES[key]) ? MODEL_ALIASES[key] : undefined;
|
|
27
|
+
return (providerID, raw) => {
|
|
28
|
+
const id = providerID === "google-vertex" ? raw.replace(/@default$/, "") : raw;
|
|
29
|
+
const lab = PROVIDER_LABS.get(providerID) ?? providerID;
|
|
30
|
+
const slash = id.indexOf("/");
|
|
31
|
+
if (slash >= 0) {
|
|
32
|
+
const slug = id.slice(0, slash);
|
|
33
|
+
const namespace = LAB_ALIASES.get(slug) ?? slug;
|
|
34
|
+
if (knownLabs.has(lab) && lab !== namespace)
|
|
35
|
+
return undefined;
|
|
36
|
+
return resolve(`${namespace}/${id.slice(slash + 1)}`);
|
|
37
|
+
}
|
|
38
|
+
if (providerID === "amazon-bedrock") {
|
|
39
|
+
const match = /^(?:(?:us|eu|apac|global)\.)?([a-z0-9-]+)\.(.+)$/.exec(id);
|
|
40
|
+
if (match)
|
|
41
|
+
return resolve(`${LAB_ALIASES.get(match[1]) ?? match[1]}/${match[2]}`);
|
|
42
|
+
}
|
|
43
|
+
if (knownLabs.has(lab))
|
|
44
|
+
return resolve(`${lab}/${id}`);
|
|
45
|
+
return bare.get(id) ?? undefined;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/** Exact identity resolution only. There is no runtime ranking of providers. */
|
|
49
|
+
export function snapshotLookup(snapshot) {
|
|
50
|
+
const index = new Map(snapshot.entries.map(entry => [priceKey(entry), entry]));
|
|
51
|
+
const bindings = new Map(Object.entries(snapshot.bindings).flatMap(([key, skus]) => skus.map(sku => [sku, key])));
|
|
52
|
+
const resolve = defaultIdentity(snapshot.entries, snapshot.labs, snapshot.ambiguous);
|
|
53
|
+
return (model) => {
|
|
54
|
+
const provider = model.providerID.toLowerCase();
|
|
55
|
+
const raw = model.id.toLowerCase();
|
|
56
|
+
const sku = `${provider}/${raw}`;
|
|
57
|
+
const mode = billingMode(model);
|
|
58
|
+
if (mode) {
|
|
59
|
+
const key = bindings.get(`${sku}@mode:${mode}`);
|
|
60
|
+
return key ? index.get(key) : undefined;
|
|
61
|
+
}
|
|
62
|
+
const key = bindings.get(sku) ?? resolve(provider, raw);
|
|
63
|
+
return key ? index.get(key) : undefined;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/** The reference is separate from the Free SKU's own zero schedule. */
|
|
67
|
+
export function snapshotFreeReferenceLookup(snapshot) {
|
|
68
|
+
const index = new Map(snapshot.entries.map(entry => [priceKey(entry), entry]));
|
|
69
|
+
const references = new Map(Object.entries(snapshot.freeReferences).flatMap(([key, skus]) => {
|
|
70
|
+
const rate = index.get(key);
|
|
71
|
+
return rate ? skus.map(sku => [sku, rate]) : [];
|
|
72
|
+
}));
|
|
73
|
+
return (model) => billingMode(model) ? undefined
|
|
74
|
+
: references.get(`${model.providerID.toLowerCase()}/${model.id.toLowerCase()}`);
|
|
75
|
+
}
|