@trazum/core 2.7.1 → 2.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/dist/anthropic-usage.d.ts +58 -0
  2. package/dist/anthropic-usage.d.ts.map +1 -1
  3. package/dist/anthropic-usage.js +91 -0
  4. package/dist/anthropic-usage.js.map +1 -1
  5. package/dist/billing.d.ts.map +1 -1
  6. package/dist/billing.js +6 -2
  7. package/dist/billing.js.map +1 -1
  8. package/dist/claude-code.d.ts +2 -0
  9. package/dist/claude-code.d.ts.map +1 -1
  10. package/dist/claude-code.js +9 -0
  11. package/dist/claude-code.js.map +1 -1
  12. package/dist/connector.d.ts +3 -1
  13. package/dist/connector.d.ts.map +1 -1
  14. package/dist/connector.js +64 -5
  15. package/dist/connector.js.map +1 -1
  16. package/dist/gateway.d.ts +18 -0
  17. package/dist/gateway.d.ts.map +1 -1
  18. package/dist/gateway.js +20 -1
  19. package/dist/gateway.js.map +1 -1
  20. package/dist/index.d.ts +1 -1
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +1 -1
  23. package/dist/index.js.map +1 -1
  24. package/dist/openai-usage.d.ts +38 -6
  25. package/dist/openai-usage.d.ts.map +1 -1
  26. package/dist/openai-usage.js +44 -8
  27. package/dist/openai-usage.js.map +1 -1
  28. package/dist/pricing-overlay.d.ts +1 -1
  29. package/dist/pricing-overlay.d.ts.map +1 -1
  30. package/dist/pricing-overlay.js +21 -0
  31. package/dist/pricing-overlay.js.map +1 -1
  32. package/dist/pricing.d.ts +37 -1
  33. package/dist/pricing.d.ts.map +1 -1
  34. package/dist/pricing.js +528 -9
  35. package/dist/pricing.js.map +1 -1
  36. package/dist/store.d.ts +4 -0
  37. package/dist/store.d.ts.map +1 -1
  38. package/dist/store.js +22 -1
  39. package/dist/store.js.map +1 -1
  40. package/dist/types.d.ts +54 -0
  41. package/dist/types.d.ts.map +1 -1
  42. package/dist/usage.d.ts +16 -0
  43. package/dist/usage.d.ts.map +1 -1
  44. package/dist/usage.js +17 -0
  45. package/dist/usage.js.map +1 -1
  46. package/package.json +1 -1
  47. package/src/anthropic-usage.ts +134 -1
  48. package/src/billing.ts +6 -2
  49. package/src/claude-code.ts +11 -0
  50. package/src/connector.ts +69 -6
  51. package/src/gateway.ts +35 -2
  52. package/src/index.ts +2 -0
  53. package/src/openai-usage.ts +72 -10
  54. package/src/pricing-overlay.ts +21 -0
  55. package/src/pricing.ts +552 -10
  56. package/src/store.ts +24 -3
  57. package/src/types.ts +57 -0
  58. package/src/usage.ts +33 -0
package/src/store.ts CHANGED
@@ -17,6 +17,10 @@
17
17
  * the later pull wins, because a window pulled again is at worst as complete
18
18
  * as it was. That makes overlapping pulls idempotent, which is what lets a
19
19
  * scheduled job run every hour over a rolling day without inventing money.
20
+ * And because one pull carries every grouping of its window at once, the
21
+ * newest pull of a window and model replaces all older records of it, even
22
+ * ones grouped differently: a finer grouping restates a day, it does not add
23
+ * one beside it.
20
24
  *
21
25
  * **Deduplication that cannot lie.** Two records the store cannot tell apart —
22
26
  * a source that served no window, or no model — are kept as *two* and reported
@@ -130,10 +134,27 @@ export function resolveStore(records: readonly StoreRecord[]): ResolvedStore {
130
134
  }
131
135
  }
132
136
 
137
+ /*
138
+ * One pull is the whole statement of its window and model: every grouping
139
+ * the provider served for that window arrives in it together. So the newest
140
+ * pull of a window replaces every older record of that window and model,
141
+ * whatever grouping the older one carried. Without this, a store written
142
+ * before `connect` asked for the tier, speed and geography groupings (2.7.3)
143
+ * would keep its old summed row beside the new split ones, and count the
144
+ * same day twice.
145
+ */
146
+ const slotOf = (record: StoreRecord): string =>
147
+ [record.provider, record.fromMs, record.toMs, record.model].join('\n');
148
+ const newest = new Map<string, number>();
149
+ for (const record of byIdentity.values()) {
150
+ const slot = slotOf(record);
151
+ newest.set(slot, Math.max(newest.get(slot) ?? Number.NEGATIVE_INFINITY, record.pulledAtMs));
152
+ }
153
+
133
154
  return {
134
- records: [...byIdentity.values()].sort(
135
- (a, b) => a.fromMs - b.fromMs || a.model.localeCompare(b.model),
136
- ),
155
+ records: [...byIdentity.values()]
156
+ .filter((record) => record.pulledAtMs === newest.get(slotOf(record)))
157
+ .sort((a, b) => a.fromMs - b.fromMs || a.model.localeCompare(b.model)),
137
158
  possiblyDouble,
138
159
  unknownVersion,
139
160
  };
package/src/types.ts CHANGED
@@ -222,6 +222,25 @@ export interface PricingTier {
222
222
  outputPerMTok: number;
223
223
  }
224
224
 
225
+ /**
226
+ * A provider's service tier other than its standard one, as a response names it
227
+ * after normalising: OpenAI's `service_tier` of `"flex"`, `"fast"` or
228
+ * `"priority"` (the same tier, under its name before 2026-07-30), and
229
+ * `"ultrafast"`. See `ModelPricing.serviceTiers`.
230
+ */
231
+ export type ServiceTier = 'flex' | 'fast' | 'ultrafast';
232
+
233
+ /** A geography a provider prices inference in differently. Anthropic's `inference_geo: "us"`. */
234
+ export type InferenceGeo = 'us';
235
+
236
+ /** The rates one service tier charges, with its own conditional bands. */
237
+ export interface ServiceTierRates {
238
+ inputPerMTok: number;
239
+ outputPerMTok: number;
240
+ /** The tier's own long-prompt band, when its table publishes one. */
241
+ tiers?: PricingTier[];
242
+ }
243
+
225
244
  /** One published price this model carried until an instant. See `ModelPricing.history`. */
226
245
  export interface PricePeriod {
227
246
  /** ISO 8601 instant, exclusive: a call at exactly this instant is priced by what came after. */
@@ -357,6 +376,44 @@ export interface ModelPricing {
357
376
  * earliest period stands for it, and the row's note says so.
358
377
  */
359
378
  history?: PricePeriod[];
379
+ /**
380
+ * What the same tokens cost at another of the provider's service tiers.
381
+ *
382
+ * OpenAI sells three besides its standard one, chosen per request with
383
+ * `service_tier` and named again in the response: Flex at about half the
384
+ * price, Fast (called Priority until 2026-07-30) at up to twice, and
385
+ * Ultrafast on two models at six times. Each is a separate table on the
386
+ * pricing page, with its own long-prompt band, and the rates are not one
387
+ * multiple of the standard ones (GPT-4o's Fast is 1.7x, GPT-5.2's 2x), so
388
+ * each is written out per model as the page prints it.
389
+ *
390
+ * Kept apart from `tiers` on purpose. A tier is a condition on the call that
391
+ * may be undecided, so it is never below the base, and Flex is. A service
392
+ * tier is decided by the response that names it, and replaces the base and
393
+ * its tiers whole: Flex on a long prompt is Flex's own long band, never a
394
+ * composite of two tables. The cache and batch ratios stay the row's, which
395
+ * is what the page's own figures show (Flex's cached input is a tenth of
396
+ * Flex's input wherever the standard one is a tenth).
397
+ *
398
+ * A response naming a tier this row has no sheet for is priced at the
399
+ * standard rate: the page publishes nothing else for it.
400
+ */
401
+ serviceTiers?: Partial<Record<ServiceTier, ServiceTierRates>>;
402
+ /**
403
+ * What running the call in one geography multiplies every rate by.
404
+ *
405
+ * Anthropic bills US-only inference (`inference_geo: "us"`) at 1.1x "on
406
+ * all token pricing categories, including input tokens, output tokens,
407
+ * cache writes, and cache reads" for Claude 4.6 and later, and says the
408
+ * multiplier stacks with fast mode, the Batch API discount and caching.
409
+ * Global routing, the default, is the row's own price, and earlier models
410
+ * reject the parameter. The response names the geography that ran in
411
+ * `usage.inference_geo`, so a call is decided by its own log line.
412
+ *
413
+ * A published multiplier, applied as the page states it, never one derived
414
+ * here: a row without the field prices every geography at its own rate.
415
+ */
416
+ inferenceGeoMultipliers?: Partial<Record<InferenceGeo, number>>;
360
417
  /**
361
418
  * Other ids that bill at exactly this rate.
362
419
  *
package/src/usage.ts CHANGED
@@ -185,6 +185,22 @@ export interface UsageRecord {
185
185
  * opt-in that every response names.
186
186
  */
187
187
  speed?: string;
188
+ /**
189
+ * The service tier the provider says answered, when the log kept it:
190
+ * OpenAI's top-level `service_tier`, `"flex"`, `"priority"` or `"fast"`,
191
+ * `"ultrafast"`, or `"default"`. Flex bills about half the standard rate
192
+ * and Fast up to twice, from tables of their own (`serviceTiers` in the
193
+ * catalogue). Absent, or `"default"`, is the standard rate.
194
+ */
195
+ serviceTier?: string;
196
+ /**
197
+ * The geography the provider says the call ran in, when the log kept it:
198
+ * Anthropic's `usage.inference_geo`, `"us"`, `"global"` or
199
+ * `"not_available"`. US-only inference bills 1.1x on Claude 4.6 and later
200
+ * (`inferenceGeoMultipliers` in the catalogue). Absent, global and
201
+ * not-available are the row's own price.
202
+ */
203
+ inferenceGeo?: string;
188
204
  /**
189
205
  * Set when an OpenAI-shaped line split its cache writes by lifetime and the
190
206
  * parts did not add up to the total. The lifetime is then unknown even on a
@@ -966,6 +982,23 @@ export function parseUsageLine(line: string): UsageRecord | null {
966
982
  * field is priced by it. Absent stays absent, which prices as standard.
967
983
  */
968
984
  ...(typeof usage.speed === 'string' && usage.speed !== '' ? { speed: usage.speed } : {}),
985
+ /*
986
+ * OpenAI's `service_tier`, which a Chat Completions or Responses object
987
+ * carries at the top level beside `usage`, and names the tier that
988
+ * actually ran: a Fast request downgraded under load says "default" and
989
+ * is billed at the standard rate. Priced from that tier's own table.
990
+ */
991
+ ...(typeof record.service_tier === 'string' && record.service_tier !== ''
992
+ ? { serviceTier: record.service_tier }
993
+ : {}),
994
+ /*
995
+ * Anthropic's `usage.inference_geo`, which every Messages response and
996
+ * every Claude Code log line carries: "us" bills 1.1x on Claude 4.6 and
997
+ * later, "global" and "not_available" do not.
998
+ */
999
+ ...(typeof usage.inference_geo === 'string' && usage.inference_geo !== ''
1000
+ ? { inferenceGeo: usage.inference_geo }
1001
+ : {}),
969
1002
  ...(openAiSplitGiven && !openAiSplitAgrees ? { writeSplitContradicted: true as const } : {}),
970
1003
  };
971
1004
  }