@namzu/sdk 74.0.0 → 74.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/dist/config/runtime.d.ts +3 -3
  3. package/dist/index.d.ts +1 -0
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +1 -0
  6. package/dist/index.js.map +1 -1
  7. package/dist/manager/session/turn-recorder.d.ts +1 -0
  8. package/dist/manager/session/turn-recorder.d.ts.map +1 -1
  9. package/dist/manager/session/turn-recorder.js +31 -7
  10. package/dist/manager/session/turn-recorder.js.map +1 -1
  11. package/dist/pricing/catalogue.generated.d.ts.map +1 -1
  12. package/dist/pricing/catalogue.generated.js +176 -21
  13. package/dist/pricing/catalogue.generated.js.map +1 -1
  14. package/dist/public-runtime.d.ts +1 -0
  15. package/dist/public-runtime.d.ts.map +1 -1
  16. package/dist/public-runtime.js +1 -0
  17. package/dist/public-runtime.js.map +1 -1
  18. package/dist/runtime/query/guard.d.ts.map +1 -1
  19. package/dist/runtime/query/guard.js +12 -2
  20. package/dist/runtime/query/guard.js.map +1 -1
  21. package/dist/runtime/query/index.d.ts.map +1 -1
  22. package/dist/runtime/query/index.js +1 -0
  23. package/dist/runtime/query/index.js.map +1 -1
  24. package/dist/runtime/query/iteration/index.d.ts +3 -0
  25. package/dist/runtime/query/iteration/index.d.ts.map +1 -1
  26. package/dist/runtime/query/iteration/index.js +32 -11
  27. package/dist/runtime/query/iteration/index.js.map +1 -1
  28. package/dist/runtime/query/iteration/phases/advisory.js +2 -2
  29. package/dist/runtime/query/iteration/phases/advisory.js.map +1 -1
  30. package/dist/runtime/query/prepare-turn.d.ts.map +1 -1
  31. package/dist/runtime/query/prepare-turn.js +1 -1
  32. package/dist/runtime/query/prepare-turn.js.map +1 -1
  33. package/dist/sandbox/file-walk-program.js +1 -1
  34. package/dist/sandbox/ignore-matcher.d.ts +2 -0
  35. package/dist/sandbox/ignore-matcher.d.ts.map +1 -1
  36. package/dist/sandbox/ignore-matcher.js +8 -2
  37. package/dist/sandbox/ignore-matcher.js.map +1 -1
  38. package/dist/sandbox/ignore-source.d.ts.map +1 -1
  39. package/dist/sandbox/ignore-source.js +51 -7
  40. package/dist/sandbox/ignore-source.js.map +1 -1
  41. package/dist/sandbox/limit-error.d.ts +7 -0
  42. package/dist/sandbox/limit-error.d.ts.map +1 -0
  43. package/dist/sandbox/limit-error.js +13 -0
  44. package/dist/sandbox/limit-error.js.map +1 -0
  45. package/dist/sandbox/provider/local.d.ts +1 -0
  46. package/dist/sandbox/provider/local.d.ts.map +1 -1
  47. package/dist/sandbox/provider/local.js +50 -13
  48. package/dist/sandbox/provider/local.js.map +1 -1
  49. package/dist/store/budget/ledger.d.ts +15 -1
  50. package/dist/store/budget/ledger.d.ts.map +1 -1
  51. package/dist/store/budget/ledger.js +80 -1
  52. package/dist/store/budget/ledger.js.map +1 -1
  53. package/dist/store/evidence/source-text.d.ts +1 -1
  54. package/dist/tools/builtins/atomic-write-file.d.ts +2 -0
  55. package/dist/tools/builtins/atomic-write-file.d.ts.map +1 -1
  56. package/dist/tools/builtins/atomic-write-file.js +37 -8
  57. package/dist/tools/builtins/atomic-write-file.js.map +1 -1
  58. package/dist/tools/builtins/read-file.d.ts.map +1 -1
  59. package/dist/tools/builtins/read-file.js +20 -12
  60. package/dist/tools/builtins/read-file.js.map +1 -1
  61. package/dist/tools/defineTool.d.ts.map +1 -1
  62. package/dist/tools/defineTool.js +4 -3
  63. package/dist/tools/defineTool.js.map +1 -1
  64. package/dist/tools/tool-is-available.d.ts +4 -0
  65. package/dist/tools/tool-is-available.d.ts.map +1 -0
  66. package/dist/tools/tool-is-available.js +8 -0
  67. package/dist/tools/tool-is-available.js.map +1 -0
  68. package/dist/types/common/index.d.ts +6 -0
  69. package/dist/types/common/index.d.ts.map +1 -1
  70. package/dist/types/common/index.js +13 -0
  71. package/dist/types/common/index.js.map +1 -1
  72. package/dist/types/sandbox/index.d.ts +13 -2
  73. package/dist/types/sandbox/index.d.ts.map +1 -1
  74. package/dist/types/sandbox/index.js.map +1 -1
  75. package/dist/types/session/checkpoint.d.ts +4 -0
  76. package/dist/types/session/checkpoint.d.ts.map +1 -1
  77. package/dist/types/session/checkpoint.js +2 -0
  78. package/dist/types/session/checkpoint.js.map +1 -1
  79. package/dist/types/session/events.d.ts +1 -0
  80. package/dist/types/session/events.d.ts.map +1 -1
  81. package/dist/types/session/events.js.map +1 -1
  82. package/dist/types/session/records.d.ts +42 -6
  83. package/dist/types/session/records.d.ts.map +1 -1
  84. package/dist/types/session/records.js +1 -0
  85. package/dist/types/session/records.js.map +1 -1
  86. package/dist/utils/cost.d.ts +17 -0
  87. package/dist/utils/cost.d.ts.map +1 -1
  88. package/dist/utils/cost.js +78 -8
  89. package/dist/utils/cost.js.map +1 -1
  90. package/package.json +1 -1
  91. package/src/index.ts +2 -0
  92. package/src/manager/session/turn-recorder.ts +31 -7
  93. package/src/pricing/catalogue.generated.ts +176 -21
  94. package/src/pricing/rates.source.json +108 -33
  95. package/src/public-runtime.ts +1 -0
  96. package/src/runtime/query/guard.ts +13 -2
  97. package/src/runtime/query/index.ts +1 -0
  98. package/src/runtime/query/iteration/index.ts +32 -11
  99. package/src/runtime/query/iteration/phases/advisory.ts +2 -2
  100. package/src/runtime/query/prepare-turn.ts +2 -1
  101. package/src/sandbox/file-walk-program.ts +1 -1
  102. package/src/sandbox/ignore-matcher.ts +12 -3
  103. package/src/sandbox/ignore-source.ts +51 -7
  104. package/src/sandbox/limit-error.ts +10 -0
  105. package/src/sandbox/provider/local.ts +54 -23
  106. package/src/store/budget/ledger.ts +93 -2
  107. package/src/tools/builtins/atomic-write-file.ts +49 -7
  108. package/src/tools/builtins/read-file.ts +34 -12
  109. package/src/tools/defineTool.ts +4 -3
  110. package/src/tools/tool-is-available.ts +11 -0
  111. package/src/types/common/index.ts +24 -0
  112. package/src/types/sandbox/index.ts +12 -1
  113. package/src/types/session/checkpoint.ts +2 -0
  114. package/src/types/session/events.ts +1 -0
  115. package/src/types/session/records.ts +1 -0
  116. package/src/utils/cost.ts +101 -8
@@ -189,6 +189,7 @@ const tokenBudgetSummary = z
189
189
  limit: z.number().nonnegative(),
190
190
  ownTokens: count,
191
191
  treeTokens: count,
192
+ treeCost: CostInfoSchema.optional(),
192
193
  reservedTokens: count,
193
194
  remainingTokens: z.number().nullable(),
194
195
  inFlightRequests: count,
package/src/utils/cost.ts CHANGED
@@ -28,6 +28,13 @@ export interface CacheRates {
28
28
  * a quietly wrong total.
29
29
  */
30
30
  readonly writeCostPer1M?: number
31
+ /**
32
+ * What a write costs when the entry was asked to live for one hour
33
+ * (`TokenUsage.cacheWriteTtlTokens.oneHour`, or `cacheTtlMs` of one hour
34
+ * when the vendor gives no split). Absent means the vendor has no separate
35
+ * price and every write is charged at {@link CacheRates.writeCostPer1M}.
36
+ */
37
+ readonly writeCostPer1MOneHour?: number
31
38
  }
32
39
 
33
40
  export interface LongContextRates {
@@ -38,6 +45,10 @@ export interface LongContextRates {
38
45
  readonly cacheReadCostPer1M: number
39
46
  /** Falls back to the base cache-write rate when omitted. */
40
47
  readonly cacheWriteCostPer1M?: number
48
+ /** Falls back to the base one-hour cache-write rate when omitted. */
49
+ readonly cacheWriteCostPer1MOneHour?: number
50
+ /** Published fast-mode multiplier for this tier. */
51
+ readonly fastMultiplier?: number
41
52
  }
42
53
 
43
54
  export interface ModelPricing {
@@ -64,6 +75,12 @@ export interface ModelPricing {
64
75
  * writes. Absent means one rate card at every length.
65
76
  */
66
77
  longContext?: LongContextRates
78
+ /**
79
+ * The vendor's multiple of every rate on this card when the call ran with
80
+ * US-only inference (`TokenUsage.inferenceGeo === 'us'`). Absent means no
81
+ * published surcharge for this model.
82
+ */
83
+ usMultiplier?: number
67
84
  /**
68
85
  * What the vendor charges per 1,000 hosted web searches
69
86
  * (`TokenUsage.webSearchRequests`). Absent means no published search price:
@@ -113,7 +130,9 @@ function priceTokens(usage: TokenUsage, pricing: ModelPricing): Priced {
113
130
  return { cost: 0, cacheDiscount: 0, unpriced: usage.totalTokens }
114
131
  }
115
132
  if (usage.speed === 'fast') {
116
- const multiplier = pricing.fastMultiplier
133
+ const multiplier = usesLongContext(usage, pricing)
134
+ ? pricing.longContext?.fastMultiplier
135
+ : pricing.fastMultiplier
117
136
  if (multiplier === undefined) {
118
137
  return { cost: 0, cacheDiscount: 0, unpriced: usage.totalTokens }
119
138
  }
@@ -128,6 +147,7 @@ function priceTokens(usage: TokenUsage, pricing: ModelPricing): Priced {
128
147
  }
129
148
 
130
149
  function countPromptTokens(usage: TokenUsage, cache: CacheRates | undefined): number {
150
+ if (usage.cacheTokensInPrompt !== undefined) return usage.promptTokens
131
151
  return (
132
152
  usage.fullPromptTokens ??
133
153
  usage.promptTokens +
@@ -136,10 +156,39 @@ function countPromptTokens(usage: TokenUsage, cache: CacheRates | undefined): nu
136
156
  )
137
157
  }
138
158
 
159
+ /** Cache-write tokens split into the one-hour entry and the rest. */
160
+ function splitWrites(usage: TokenUsage): { oneHour: number; rest: number } {
161
+ const total = usage.cacheWriteTokens
162
+ const split = usage.cacheWriteTtlTokens
163
+ if (split !== undefined) {
164
+ const oneHour = Math.min(Math.max(split.oneHour ?? 0, 0), total)
165
+ return { oneHour, rest: total - oneHour }
166
+ }
167
+ return usage.cacheTtlMs === 3_600_000 ? { oneHour: total, rest: 0 } : { oneHour: 0, rest: total }
168
+ }
169
+
139
170
  function priceStandard(usage: TokenUsage, basePricing: ModelPricing): Priced {
171
+ const priced = priceStandardTokens(usage, basePricing)
172
+ const us = basePricing.usMultiplier
173
+ if (us === undefined || usage.inferenceGeo !== 'us') return priced
174
+ return {
175
+ cost: priced.cost * us,
176
+ cacheDiscount: priced.cacheDiscount * us,
177
+ unpriced: priced.unpriced,
178
+ }
179
+ }
180
+
181
+ function usesLongContext(usage: TokenUsage, pricing: ModelPricing): boolean {
182
+ return (
183
+ pricing.longContext !== undefined &&
184
+ countPromptTokens(usage, pricing.cache) > pricing.longContext.aboveInputTokens
185
+ )
186
+ }
187
+
188
+ function priceStandardTokens(usage: TokenUsage, basePricing: ModelPricing): Priced {
140
189
  const tier = basePricing.longContext
141
190
  const pricing: ModelPricing =
142
- tier && countPromptTokens(usage, basePricing.cache) > tier.aboveInputTokens
191
+ tier && usesLongContext(usage, basePricing)
143
192
  ? {
144
193
  ...basePricing,
145
194
  inputCostPer1M: tier.inputCostPer1M,
@@ -150,6 +199,8 @@ function priceStandard(usage: TokenUsage, basePricing: ModelPricing): Priced {
150
199
  ...basePricing.cache,
151
200
  readCostPer1M: tier.cacheReadCostPer1M,
152
201
  writeCostPer1M: tier.cacheWriteCostPer1M ?? basePricing.cache.writeCostPer1M,
202
+ writeCostPer1MOneHour:
203
+ tier.cacheWriteCostPer1MOneHour ?? basePricing.cache.writeCostPer1MOneHour,
153
204
  },
154
205
  }
155
206
  : {}),
@@ -171,18 +222,23 @@ function priceStandard(usage: TokenUsage, basePricing: ModelPricing): Priced {
171
222
  // Never negative: a driver that reports more cached tokens than prompt
172
223
  // tokens is contradicting itself, and clamping keeps that from turning into
173
224
  // a credit on the bill.
174
- const billableInput = cache.promptIncludesCacheReads
175
- ? Math.max(0, usage.promptTokens - usage.cachedTokens)
176
- : usage.promptTokens
225
+ const cacheInPrompt =
226
+ usage.cacheTokensInPrompt ?? (cache.promptIncludesCacheReads ? usage.cachedTokens : 0)
227
+ const billableInput = Math.max(0, usage.promptTokens - cacheInPrompt)
177
228
 
178
229
  const writesRated = cache.writeCostPer1M !== undefined
230
+ const writes = splitWrites(usage)
231
+ const writeCost = writesRated
232
+ ? perMillion(writes.rest, cache.writeCostPer1M as number) +
233
+ perMillion(writes.oneHour, cache.writeCostPer1MOneHour ?? (cache.writeCostPer1M as number))
234
+ : 0
179
235
 
180
236
  return {
181
237
  cost:
182
238
  perMillion(billableInput, pricing.inputCostPer1M) +
183
239
  perMillion(usage.completionTokens, pricing.outputCostPer1M) +
184
240
  perMillion(usage.cachedTokens, cache.readCostPer1M) +
185
- (writesRated ? perMillion(usage.cacheWriteTokens, cache.writeCostPer1M as number) : 0),
241
+ writeCost,
186
242
  // Reported, not subtracted. The saving is already inside `cost` — the
187
243
  // reads were charged at the read rate rather than the input rate — so
188
244
  // taking it off again would double-count it. This field was previously
@@ -194,6 +250,8 @@ function priceStandard(usage: TokenUsage, basePricing: ModelPricing): Priced {
194
250
  }
195
251
 
196
252
  export function calculateCost(usage: TokenUsage, pricing: ModelPricing): CostInfo {
253
+ if (usage.billingRoute === 'subscription')
254
+ return { ...ZERO_COST, includedTokens: usage.totalTokens }
197
255
  const priced = priceUsage(usage, pricing)
198
256
  return {
199
257
  inputCostPer1M: pricing.inputCostPer1M,
@@ -223,6 +281,7 @@ function isFresh(current: CostInfo): boolean {
223
281
  return (
224
282
  current.totalCost === 0 &&
225
283
  current.unpricedTokens === 0 &&
284
+ current.includedTokens === undefined &&
226
285
  current.inputCostPer1M === undefined &&
227
286
  current.outputCostPer1M === undefined
228
287
  )
@@ -264,11 +323,20 @@ export function accumulateCost(
264
323
  additionalUsage: TokenUsage,
265
324
  pricing: ModelPricing,
266
325
  ): CostInfo {
326
+ if (additionalUsage.billingRoute === 'subscription') {
327
+ return {
328
+ totalCost: current.totalCost,
329
+ cacheDiscount: current.cacheDiscount,
330
+ unpricedTokens: current.unpricedTokens,
331
+ includedTokens: (current.includedTokens ?? 0) + additionalUsage.totalTokens,
332
+ }
333
+ }
267
334
  const priced = priceUsage(additionalUsage, pricing)
268
335
  const rates = priced.unpriced > 0 ? {} : ratesFor(current, pricing)
269
336
  return {
270
337
  ...rates,
271
338
  totalCost: current.totalCost + priced.cost,
339
+ ...includedUsage(current),
272
340
  cacheDiscount: current.cacheDiscount + priced.cacheDiscount,
273
341
  unpricedTokens: current.unpricedTokens + priced.unpriced,
274
342
  }
@@ -290,6 +358,7 @@ export function accumulateUnpricedCost(current: CostInfo, additionalUsage: Token
290
358
  totalCost: current.totalCost,
291
359
  cacheDiscount: current.cacheDiscount,
292
360
  unpricedTokens: current.unpricedTokens + additionalUsage.totalTokens,
361
+ ...includedUsage(current),
293
362
  }
294
363
  }
295
364
 
@@ -305,11 +374,16 @@ export function accumulateUnpricedCost(current: CostInfo, additionalUsage: Token
305
374
  export function accumulateReportedCost(current: CostInfo, reportedCostUsd: number): CostInfo {
306
375
  return {
307
376
  totalCost: current.totalCost + reportedCostUsd,
377
+ ...includedUsage(current),
308
378
  cacheDiscount: current.cacheDiscount,
309
379
  unpricedTokens: current.unpricedTokens,
310
380
  }
311
381
  }
312
382
 
383
+ function includedUsage(cost: CostInfo): Pick<CostInfo, 'includedTokens'> {
384
+ return cost.includedTokens === undefined ? {} : { includedTokens: cost.includedTokens }
385
+ }
386
+
313
387
  /** Whether a usage frame carries a usable vendor-reported cost. */
314
388
  export function hasReportedCost(
315
389
  usage: TokenUsage,
@@ -317,10 +391,22 @@ export function hasReportedCost(
317
391
  return Number.isFinite(usage.reportedCostUsd) && (usage.reportedCostUsd as number) >= 0
318
392
  }
319
393
 
394
+ /**
395
+ * Round half up on the decimal digits, not on the binary float:
396
+ * `(0.995).toFixed(2)` is `'0.99'` because 0.995 is stored as 0.99499999...
397
+ * The tiny relative nudge brings a value that is a decimal tie in the vendor's
398
+ * rate card back onto it.
399
+ */
400
+ function roundDecimal(usd: number, digits: number): string {
401
+ const scale = 10 ** digits
402
+ return (Math.round(usd * scale * (1 + Number.EPSILON * 8)) / scale).toFixed(digits)
403
+ }
404
+
320
405
  export function formatCost(usd: number): string {
321
406
  if (usd === 0) return '$0.00'
322
- if (usd < 0.01) return `$${usd.toFixed(4)}`
323
- return `$${usd.toFixed(2)}`
407
+ if (usd > 0 && usd < 0.0001) return '<$0.0001'
408
+ if (usd < 0.01) return `$${roundDecimal(usd, 4)}`
409
+ return `$${roundDecimal(usd, 2)}`
324
410
  }
325
411
 
326
412
  /**
@@ -333,6 +419,13 @@ export function formatCost(usd: number): string {
333
419
  * to follow.
334
420
  */
335
421
  export function describeCost(cost: CostInfo): string {
422
+ if (cost.includedTokens !== undefined) {
423
+ const plan = `included in your plan (${cost.includedTokens.toLocaleString('en-US')} tokens)`
424
+ const { includedTokens: _included, ...metered } = cost
425
+ return cost.totalCost === 0 && cost.unpricedTokens === 0
426
+ ? plan
427
+ : `${describeCost(metered)} + ${plan}`
428
+ }
336
429
  if (cost.unpricedTokens === 0) return formatCost(cost.totalCost)
337
430
  if (cost.totalCost === 0) {
338
431
  return `unknown (${cost.unpricedTokens} tokens at no known rate)`