@namzu/sdk 74.0.0 → 75.0.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 (271) hide show
  1. package/CHANGELOG.md +65 -0
  2. package/dist/bridge/acp/server.d.ts +2 -0
  3. package/dist/bridge/acp/server.d.ts.map +1 -1
  4. package/dist/bridge/acp/server.js +9 -1
  5. package/dist/bridge/acp/server.js.map +1 -1
  6. package/dist/config/runtime.d.ts +3 -3
  7. package/dist/connector/mcp/adapter.d.ts +1 -1
  8. package/dist/connector/mcp/adapter.d.ts.map +1 -1
  9. package/dist/connector/mcp/adapter.js +3 -2
  10. package/dist/connector/mcp/adapter.js.map +1 -1
  11. package/dist/connector/mcp/client.d.ts +4 -25
  12. package/dist/connector/mcp/client.d.ts.map +1 -1
  13. package/dist/connector/mcp/client.js +32 -32
  14. package/dist/connector/mcp/client.js.map +1 -1
  15. package/dist/index.d.ts +1 -0
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +1 -0
  18. package/dist/index.js.map +1 -1
  19. package/dist/manager/session/turn-recorder.d.ts +1 -0
  20. package/dist/manager/session/turn-recorder.d.ts.map +1 -1
  21. package/dist/manager/session/turn-recorder.js +31 -7
  22. package/dist/manager/session/turn-recorder.js.map +1 -1
  23. package/dist/pricing/catalogue.generated.d.ts.map +1 -1
  24. package/dist/pricing/catalogue.generated.js +176 -21
  25. package/dist/pricing/catalogue.generated.js.map +1 -1
  26. package/dist/provider/errors.d.ts.map +1 -1
  27. package/dist/provider/errors.js +13 -18
  28. package/dist/provider/errors.js.map +1 -1
  29. package/dist/provider/stream-request-timeout.d.ts +9 -0
  30. package/dist/provider/stream-request-timeout.d.ts.map +1 -0
  31. package/dist/provider/stream-request-timeout.js +34 -0
  32. package/dist/provider/stream-request-timeout.js.map +1 -0
  33. package/dist/public-runtime.d.ts +2 -0
  34. package/dist/public-runtime.d.ts.map +1 -1
  35. package/dist/public-runtime.js +2 -0
  36. package/dist/public-runtime.js.map +1 -1
  37. package/dist/public-tools.d.ts +1 -1
  38. package/dist/public-tools.d.ts.map +1 -1
  39. package/dist/public-tools.js +1 -1
  40. package/dist/public-tools.js.map +1 -1
  41. package/dist/runtime/query/executor.d.ts +3 -0
  42. package/dist/runtime/query/executor.d.ts.map +1 -1
  43. package/dist/runtime/query/executor.js +15 -3
  44. package/dist/runtime/query/executor.js.map +1 -1
  45. package/dist/runtime/query/file-evidence-replay.d.ts +1 -0
  46. package/dist/runtime/query/file-evidence-replay.d.ts.map +1 -1
  47. package/dist/runtime/query/file-evidence-replay.js +64 -21
  48. package/dist/runtime/query/file-evidence-replay.js.map +1 -1
  49. package/dist/runtime/query/file-evidence-seed.d.ts +7 -0
  50. package/dist/runtime/query/file-evidence-seed.d.ts.map +1 -1
  51. package/dist/runtime/query/file-evidence-seed.js +12 -1
  52. package/dist/runtime/query/file-evidence-seed.js.map +1 -1
  53. package/dist/runtime/query/guard.d.ts.map +1 -1
  54. package/dist/runtime/query/guard.js +12 -2
  55. package/dist/runtime/query/guard.js.map +1 -1
  56. package/dist/runtime/query/index.d.ts +2 -0
  57. package/dist/runtime/query/index.d.ts.map +1 -1
  58. package/dist/runtime/query/index.js +6 -2
  59. package/dist/runtime/query/index.js.map +1 -1
  60. package/dist/runtime/query/iteration/index.d.ts +3 -0
  61. package/dist/runtime/query/iteration/index.d.ts.map +1 -1
  62. package/dist/runtime/query/iteration/index.js +32 -11
  63. package/dist/runtime/query/iteration/index.js.map +1 -1
  64. package/dist/runtime/query/iteration/phases/advisory.js +2 -2
  65. package/dist/runtime/query/iteration/phases/advisory.js.map +1 -1
  66. package/dist/runtime/query/path-escalation.d.ts.map +1 -1
  67. package/dist/runtime/query/path-escalation.js +4 -3
  68. package/dist/runtime/query/path-escalation.js.map +1 -1
  69. package/dist/runtime/query/prepare-turn.d.ts.map +1 -1
  70. package/dist/runtime/query/prepare-turn.js +1 -1
  71. package/dist/runtime/query/prepare-turn.js.map +1 -1
  72. package/dist/runtime/query/tooling.d.ts +2 -0
  73. package/dist/runtime/query/tooling.d.ts.map +1 -1
  74. package/dist/runtime/query/tooling.js +1 -0
  75. package/dist/runtime/query/tooling.js.map +1 -1
  76. package/dist/sandbox/file-walk-program.js +1 -1
  77. package/dist/sandbox/ignore-matcher.d.ts +2 -0
  78. package/dist/sandbox/ignore-matcher.d.ts.map +1 -1
  79. package/dist/sandbox/ignore-matcher.js +8 -2
  80. package/dist/sandbox/ignore-matcher.js.map +1 -1
  81. package/dist/sandbox/ignore-source.d.ts.map +1 -1
  82. package/dist/sandbox/ignore-source.js +51 -7
  83. package/dist/sandbox/ignore-source.js.map +1 -1
  84. package/dist/sandbox/limit-error.d.ts +7 -0
  85. package/dist/sandbox/limit-error.d.ts.map +1 -0
  86. package/dist/sandbox/limit-error.js +13 -0
  87. package/dist/sandbox/limit-error.js.map +1 -0
  88. package/dist/sandbox/provider/local.d.ts +1 -0
  89. package/dist/sandbox/provider/local.d.ts.map +1 -1
  90. package/dist/sandbox/provider/local.js +95 -31
  91. package/dist/sandbox/provider/local.js.map +1 -1
  92. package/dist/schedules/cron.js +1 -1
  93. package/dist/schedules/cron.js.map +1 -1
  94. package/dist/schedules/describe.js +1 -1
  95. package/dist/schedules/describe.js.map +1 -1
  96. package/dist/schedules/next-fire.d.ts +2 -2
  97. package/dist/schedules/next-fire.d.ts.map +1 -1
  98. package/dist/schedules/next-fire.js +7 -4
  99. package/dist/schedules/next-fire.js.map +1 -1
  100. package/dist/schedules/spec.d.ts.map +1 -1
  101. package/dist/schedules/spec.js +23 -7
  102. package/dist/schedules/spec.js.map +1 -1
  103. package/dist/schedules/types.d.ts +2 -0
  104. package/dist/schedules/types.d.ts.map +1 -1
  105. package/dist/store/budget/ledger.d.ts +15 -1
  106. package/dist/store/budget/ledger.d.ts.map +1 -1
  107. package/dist/store/budget/ledger.js +80 -1
  108. package/dist/store/budget/ledger.js.map +1 -1
  109. package/dist/store/evidence/source-text.d.ts +1 -1
  110. package/dist/store/session-index/sqlite.d.ts.map +1 -1
  111. package/dist/store/session-index/sqlite.js +7 -3
  112. package/dist/store/session-index/sqlite.js.map +1 -1
  113. package/dist/store/session-log/core.d.ts.map +1 -1
  114. package/dist/store/session-log/core.js +88 -24
  115. package/dist/store/session-log/core.js.map +1 -1
  116. package/dist/store/session-log/disk.js +1 -1
  117. package/dist/store/session-log/disk.js.map +1 -1
  118. package/dist/tools/builtins/atomic-write-file.d.ts +2 -0
  119. package/dist/tools/builtins/atomic-write-file.d.ts.map +1 -1
  120. package/dist/tools/builtins/atomic-write-file.js +37 -8
  121. package/dist/tools/builtins/atomic-write-file.js.map +1 -1
  122. package/dist/tools/builtins/code-analysis.d.ts.map +1 -1
  123. package/dist/tools/builtins/code-analysis.js +4 -1
  124. package/dist/tools/builtins/code-analysis.js.map +1 -1
  125. package/dist/tools/builtins/edit-apply.d.ts.map +1 -1
  126. package/dist/tools/builtins/edit-apply.js +24 -10
  127. package/dist/tools/builtins/edit-apply.js.map +1 -1
  128. package/dist/tools/builtins/edit.d.ts.map +1 -1
  129. package/dist/tools/builtins/edit.js +31 -15
  130. package/dist/tools/builtins/edit.js.map +1 -1
  131. package/dist/tools/builtins/file-text.d.ts +3 -0
  132. package/dist/tools/builtins/file-text.d.ts.map +1 -0
  133. package/dist/tools/builtins/file-text.js +14 -0
  134. package/dist/tools/builtins/file-text.js.map +1 -0
  135. package/dist/tools/builtins/glob.d.ts.map +1 -1
  136. package/dist/tools/builtins/glob.js +7 -2
  137. package/dist/tools/builtins/glob.js.map +1 -1
  138. package/dist/tools/builtins/grep.d.ts.map +1 -1
  139. package/dist/tools/builtins/grep.js +8 -5
  140. package/dist/tools/builtins/grep.js.map +1 -1
  141. package/dist/tools/builtins/ls.d.ts.map +1 -1
  142. package/dist/tools/builtins/ls.js +2 -1
  143. package/dist/tools/builtins/ls.js.map +1 -1
  144. package/dist/tools/builtins/notebook-edit.d.ts.map +1 -1
  145. package/dist/tools/builtins/notebook-edit.js +19 -10
  146. package/dist/tools/builtins/notebook-edit.js.map +1 -1
  147. package/dist/tools/builtins/read-file.d.ts.map +1 -1
  148. package/dist/tools/builtins/read-file.js +61 -29
  149. package/dist/tools/builtins/read-file.js.map +1 -1
  150. package/dist/tools/builtins/read-regular-file.d.ts +5 -0
  151. package/dist/tools/builtins/read-regular-file.d.ts.map +1 -0
  152. package/dist/tools/builtins/read-regular-file.js +23 -0
  153. package/dist/tools/builtins/read-regular-file.js.map +1 -0
  154. package/dist/tools/builtins/search-support.d.ts +2 -0
  155. package/dist/tools/builtins/search-support.d.ts.map +1 -1
  156. package/dist/tools/builtins/search-support.js +14 -0
  157. package/dist/tools/builtins/search-support.js.map +1 -1
  158. package/dist/tools/builtins/stale-before-review.d.ts.map +1 -1
  159. package/dist/tools/builtins/stale-before-review.js +8 -5
  160. package/dist/tools/builtins/stale-before-review.js.map +1 -1
  161. package/dist/tools/builtins/write-file.d.ts.map +1 -1
  162. package/dist/tools/builtins/write-file.js +17 -13
  163. package/dist/tools/builtins/write-file.js.map +1 -1
  164. package/dist/tools/defineTool.d.ts.map +1 -1
  165. package/dist/tools/defineTool.js +4 -3
  166. package/dist/tools/defineTool.js.map +1 -1
  167. package/dist/tools/paths.d.ts +9 -0
  168. package/dist/tools/paths.d.ts.map +1 -1
  169. package/dist/tools/paths.js +39 -9
  170. package/dist/tools/paths.js.map +1 -1
  171. package/dist/tools/posix-path.d.ts +2 -14
  172. package/dist/tools/posix-path.d.ts.map +1 -1
  173. package/dist/tools/posix-path.js +7 -0
  174. package/dist/tools/posix-path.js.map +1 -1
  175. package/dist/tools/tool-is-available.d.ts +4 -0
  176. package/dist/tools/tool-is-available.d.ts.map +1 -0
  177. package/dist/tools/tool-is-available.js +8 -0
  178. package/dist/tools/tool-is-available.js.map +1 -0
  179. package/dist/types/code-analysis/index.d.ts +1 -1
  180. package/dist/types/code-analysis/index.d.ts.map +1 -1
  181. package/dist/types/common/index.d.ts +6 -0
  182. package/dist/types/common/index.d.ts.map +1 -1
  183. package/dist/types/common/index.js +13 -0
  184. package/dist/types/common/index.js.map +1 -1
  185. package/dist/types/provider/errors.d.ts.map +1 -1
  186. package/dist/types/provider/errors.js +3 -5
  187. package/dist/types/provider/errors.js.map +1 -1
  188. package/dist/types/sandbox/index.d.ts +13 -2
  189. package/dist/types/sandbox/index.d.ts.map +1 -1
  190. package/dist/types/sandbox/index.js.map +1 -1
  191. package/dist/types/session/checkpoint.d.ts +4 -0
  192. package/dist/types/session/checkpoint.d.ts.map +1 -1
  193. package/dist/types/session/checkpoint.js +2 -0
  194. package/dist/types/session/checkpoint.js.map +1 -1
  195. package/dist/types/session/events.d.ts +1 -0
  196. package/dist/types/session/events.d.ts.map +1 -1
  197. package/dist/types/session/events.js.map +1 -1
  198. package/dist/types/session/records.d.ts +42 -6
  199. package/dist/types/session/records.d.ts.map +1 -1
  200. package/dist/types/session/records.js +1 -0
  201. package/dist/types/session/records.js.map +1 -1
  202. package/dist/types/tool/index.d.ts +2 -0
  203. package/dist/types/tool/index.d.ts.map +1 -1
  204. package/dist/types/tool/index.js.map +1 -1
  205. package/dist/utils/cost.d.ts +17 -0
  206. package/dist/utils/cost.d.ts.map +1 -1
  207. package/dist/utils/cost.js +78 -8
  208. package/dist/utils/cost.js.map +1 -1
  209. package/package.json +1 -1
  210. package/src/bridge/acp/server.ts +16 -7
  211. package/src/connector/mcp/adapter.ts +4 -3
  212. package/src/connector/mcp/client.ts +33 -32
  213. package/src/index.ts +2 -0
  214. package/src/manager/session/turn-recorder.ts +31 -7
  215. package/src/pricing/catalogue.generated.ts +176 -21
  216. package/src/pricing/rates.source.json +108 -33
  217. package/src/provider/errors.ts +12 -19
  218. package/src/provider/stream-request-timeout.ts +34 -0
  219. package/src/public-runtime.ts +2 -0
  220. package/src/public-tools.ts +1 -0
  221. package/src/runtime/query/executor.ts +29 -3
  222. package/src/runtime/query/file-evidence-replay.ts +73 -19
  223. package/src/runtime/query/file-evidence-seed.ts +16 -0
  224. package/src/runtime/query/guard.ts +13 -2
  225. package/src/runtime/query/index.ts +10 -1
  226. package/src/runtime/query/iteration/index.ts +32 -11
  227. package/src/runtime/query/iteration/phases/advisory.ts +2 -2
  228. package/src/runtime/query/path-escalation.ts +4 -3
  229. package/src/runtime/query/prepare-turn.ts +2 -1
  230. package/src/runtime/query/tooling.ts +3 -0
  231. package/src/sandbox/file-walk-program.ts +1 -1
  232. package/src/sandbox/ignore-matcher.ts +12 -3
  233. package/src/sandbox/ignore-source.ts +51 -7
  234. package/src/sandbox/limit-error.ts +10 -0
  235. package/src/sandbox/provider/local.ts +107 -48
  236. package/src/schedules/cron.ts +1 -1
  237. package/src/schedules/describe.ts +1 -1
  238. package/src/schedules/next-fire.ts +7 -4
  239. package/src/schedules/spec.ts +36 -7
  240. package/src/schedules/types.ts +2 -0
  241. package/src/store/budget/ledger.ts +93 -2
  242. package/src/store/session-index/sqlite.ts +8 -4
  243. package/src/store/session-log/core.ts +96 -25
  244. package/src/store/session-log/disk.ts +1 -1
  245. package/src/tools/builtins/atomic-write-file.ts +49 -7
  246. package/src/tools/builtins/code-analysis.ts +6 -1
  247. package/src/tools/builtins/edit-apply.ts +25 -10
  248. package/src/tools/builtins/edit.ts +31 -15
  249. package/src/tools/builtins/file-text.ts +14 -0
  250. package/src/tools/builtins/glob.ts +6 -1
  251. package/src/tools/builtins/grep.ts +7 -5
  252. package/src/tools/builtins/ls.ts +2 -1
  253. package/src/tools/builtins/notebook-edit.ts +18 -11
  254. package/src/tools/builtins/read-file.ts +71 -24
  255. package/src/tools/builtins/read-regular-file.ts +25 -0
  256. package/src/tools/builtins/search-support.ts +13 -0
  257. package/src/tools/builtins/stale-before-review.ts +8 -6
  258. package/src/tools/builtins/write-file.ts +18 -12
  259. package/src/tools/defineTool.ts +4 -3
  260. package/src/tools/paths.ts +49 -7
  261. package/src/tools/posix-path.ts +9 -0
  262. package/src/tools/tool-is-available.ts +11 -0
  263. package/src/types/code-analysis/index.ts +1 -1
  264. package/src/types/common/index.ts +24 -0
  265. package/src/types/provider/errors.ts +3 -5
  266. package/src/types/sandbox/index.ts +12 -1
  267. package/src/types/session/checkpoint.ts +2 -0
  268. package/src/types/session/events.ts +1 -0
  269. package/src/types/session/records.ts +1 -0
  270. package/src/types/tool/index.ts +2 -0
  271. package/src/utils/cost.ts +101 -8
@@ -497,6 +497,7 @@ const STRUCTURAL_CODES: Readonly<Record<string, ProviderErrorCode>> = {
497
497
  usage_limit_reached: 'usage_limit',
498
498
  usage_limit_exceeded: 'usage_limit',
499
499
  billing_error: 'usage_limit',
500
+ credits_required: 'usage_limit',
500
501
  overloaded_error: 'overloaded',
501
502
  content_filter: 'content_filter',
502
503
  content_policy_violation: 'content_filter',
@@ -507,14 +508,11 @@ const STRUCTURAL_CODES: Readonly<Record<string, ProviderErrorCode>> = {
507
508
  }
508
509
 
509
510
  /**
510
- * Is this exact provider code, or the provider's own complaint, a spent
511
- * allowance? The codes are the vendors' identifiers (`insufficient_quota`,
512
- * `usage_limit_reached`, `usage_limit_exceeded`, `billing_error`); the
513
- * sentences are the two vendors that word it only in prose: an exhausted
514
- * prepaid balance and an exhausted plan allowance.
511
+ * Provider wording for exhausted allowances and required credits.
515
512
  */
516
513
  const USAGE_LIMIT_DETAIL: readonly RegExp[] = [
517
514
  /credit balance is too low/i,
515
+ /credits are required/i,
518
516
  /exceeded your current quota/i,
519
517
  /usage limit (?:has been )?(?:reached|exceeded)/i,
520
518
  ]
@@ -128,6 +128,11 @@ export interface SandboxExecResult {
128
128
  // ---------------------------------------------------------------------------
129
129
 
130
130
  export interface SandboxExecOptions {
131
+ /**
132
+ * Maximum combined stdout and stderr bytes accepted by the host.
133
+ * The host rejects with `SandboxLimitError` when output exceeds it.
134
+ */
135
+ maxOutputBytes?: number
131
136
  readonly timeout?: number
132
137
  readonly env?: Record<string, string>
133
138
  readonly cwd?: string
@@ -364,6 +369,12 @@ export interface SandboxTcpConnectOptions {
364
369
  * file.
365
370
  */
366
371
  export interface SandboxReadFileOptions {
372
+ /**
373
+ * Maximum bytes in the requested file slice. A larger slice is refused
374
+ * with `SandboxLimitError`. A backend that cannot enforce this limit
375
+ * must refuse the operation.
376
+ */
377
+ readonly maxBytes?: number
367
378
  /** First byte to read. Defaults to 0. */
368
379
  readonly offset?: number
369
380
  /**
@@ -377,7 +388,7 @@ export interface SandboxReadFileOptions {
377
388
  * asked for 4 MiB, got 1 MiB and was told nothing would read the short
378
389
  * answer as the end of its range. The agent-backed backends cap it at
379
390
  * `NAMZU_AGENT_READ_FILE_RANGE_BYTES` (1 MiB by default), because one
380
- * range is one wire frame there; a provider reading from local disk has
391
+ * range without `maxBytes` is one wire frame there; a provider reading from local disk has
381
392
  * no such ceiling. A caller that wants more than a frame's worth in one
382
393
  * call iterates {@link Sandbox.readFileStream} instead, which is not
383
394
  * capped.
@@ -78,6 +78,7 @@ export const TokenUsageSchema = z
78
78
  totalTokens: count,
79
79
  cachedTokens: count,
80
80
  cacheWriteTokens: count,
81
+ cacheTokensInPrompt: count.optional(),
81
82
  reasoningTokens: count.optional(),
82
83
  cacheTtlMs: count.optional(),
83
84
  cacheWriteTtlTokens: z
@@ -94,6 +95,7 @@ export const CostInfoSchema = z
94
95
  totalCost: z.number().nonnegative(),
95
96
  cacheDiscount: z.number(),
96
97
  unpricedTokens: count,
98
+ includedTokens: count.optional(),
97
99
  })
98
100
  .strict()
99
101
 
@@ -987,6 +987,7 @@ type CoreSessionEvent =
987
987
  }
988
988
  | {
989
989
  type: 'token_usage_updated'
990
+ billingRoute?: TokenUsage['billingRoute']
990
991
  /** Aggregate tree spend, distinct from this turn's own usage. */
991
992
  budget?: SessionTokenBudgetSummary
992
993
  sessionId: SessionId
@@ -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,
@@ -692,6 +692,8 @@ export interface ToolContext {
692
692
  * directory; an absolute path inside any of these is accepted.
693
693
  */
694
694
  additionalDirectories?: readonly string[]
695
+ /** Host-owned absolute file paths available to read, write and edit, including in a sandbox. */
696
+ allowedFiles?: readonly string[]
695
697
  /**
696
698
  * Whether the session's read rules deny an absolute path. The search tools
697
699
  * (`grep`, `glob`) call it for every file they would list or open and skip
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)`