@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.
- package/CHANGELOG.md +65 -0
- package/dist/bridge/acp/server.d.ts +2 -0
- package/dist/bridge/acp/server.d.ts.map +1 -1
- package/dist/bridge/acp/server.js +9 -1
- package/dist/bridge/acp/server.js.map +1 -1
- package/dist/config/runtime.d.ts +3 -3
- package/dist/connector/mcp/adapter.d.ts +1 -1
- package/dist/connector/mcp/adapter.d.ts.map +1 -1
- package/dist/connector/mcp/adapter.js +3 -2
- package/dist/connector/mcp/adapter.js.map +1 -1
- package/dist/connector/mcp/client.d.ts +4 -25
- package/dist/connector/mcp/client.d.ts.map +1 -1
- package/dist/connector/mcp/client.js +32 -32
- package/dist/connector/mcp/client.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/manager/session/turn-recorder.d.ts +1 -0
- package/dist/manager/session/turn-recorder.d.ts.map +1 -1
- package/dist/manager/session/turn-recorder.js +31 -7
- package/dist/manager/session/turn-recorder.js.map +1 -1
- package/dist/pricing/catalogue.generated.d.ts.map +1 -1
- package/dist/pricing/catalogue.generated.js +176 -21
- package/dist/pricing/catalogue.generated.js.map +1 -1
- package/dist/provider/errors.d.ts.map +1 -1
- package/dist/provider/errors.js +13 -18
- package/dist/provider/errors.js.map +1 -1
- package/dist/provider/stream-request-timeout.d.ts +9 -0
- package/dist/provider/stream-request-timeout.d.ts.map +1 -0
- package/dist/provider/stream-request-timeout.js +34 -0
- package/dist/provider/stream-request-timeout.js.map +1 -0
- package/dist/public-runtime.d.ts +2 -0
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +2 -0
- package/dist/public-runtime.js.map +1 -1
- package/dist/public-tools.d.ts +1 -1
- package/dist/public-tools.d.ts.map +1 -1
- package/dist/public-tools.js +1 -1
- package/dist/public-tools.js.map +1 -1
- package/dist/runtime/query/executor.d.ts +3 -0
- package/dist/runtime/query/executor.d.ts.map +1 -1
- package/dist/runtime/query/executor.js +15 -3
- package/dist/runtime/query/executor.js.map +1 -1
- package/dist/runtime/query/file-evidence-replay.d.ts +1 -0
- package/dist/runtime/query/file-evidence-replay.d.ts.map +1 -1
- package/dist/runtime/query/file-evidence-replay.js +64 -21
- package/dist/runtime/query/file-evidence-replay.js.map +1 -1
- package/dist/runtime/query/file-evidence-seed.d.ts +7 -0
- package/dist/runtime/query/file-evidence-seed.d.ts.map +1 -1
- package/dist/runtime/query/file-evidence-seed.js +12 -1
- package/dist/runtime/query/file-evidence-seed.js.map +1 -1
- package/dist/runtime/query/guard.d.ts.map +1 -1
- package/dist/runtime/query/guard.js +12 -2
- package/dist/runtime/query/guard.js.map +1 -1
- package/dist/runtime/query/index.d.ts +2 -0
- package/dist/runtime/query/index.d.ts.map +1 -1
- package/dist/runtime/query/index.js +6 -2
- package/dist/runtime/query/index.js.map +1 -1
- package/dist/runtime/query/iteration/index.d.ts +3 -0
- package/dist/runtime/query/iteration/index.d.ts.map +1 -1
- package/dist/runtime/query/iteration/index.js +32 -11
- package/dist/runtime/query/iteration/index.js.map +1 -1
- package/dist/runtime/query/iteration/phases/advisory.js +2 -2
- package/dist/runtime/query/iteration/phases/advisory.js.map +1 -1
- package/dist/runtime/query/path-escalation.d.ts.map +1 -1
- package/dist/runtime/query/path-escalation.js +4 -3
- package/dist/runtime/query/path-escalation.js.map +1 -1
- package/dist/runtime/query/prepare-turn.d.ts.map +1 -1
- package/dist/runtime/query/prepare-turn.js +1 -1
- package/dist/runtime/query/prepare-turn.js.map +1 -1
- package/dist/runtime/query/tooling.d.ts +2 -0
- package/dist/runtime/query/tooling.d.ts.map +1 -1
- package/dist/runtime/query/tooling.js +1 -0
- package/dist/runtime/query/tooling.js.map +1 -1
- package/dist/sandbox/file-walk-program.js +1 -1
- package/dist/sandbox/ignore-matcher.d.ts +2 -0
- package/dist/sandbox/ignore-matcher.d.ts.map +1 -1
- package/dist/sandbox/ignore-matcher.js +8 -2
- package/dist/sandbox/ignore-matcher.js.map +1 -1
- package/dist/sandbox/ignore-source.d.ts.map +1 -1
- package/dist/sandbox/ignore-source.js +51 -7
- package/dist/sandbox/ignore-source.js.map +1 -1
- package/dist/sandbox/limit-error.d.ts +7 -0
- package/dist/sandbox/limit-error.d.ts.map +1 -0
- package/dist/sandbox/limit-error.js +13 -0
- package/dist/sandbox/limit-error.js.map +1 -0
- package/dist/sandbox/provider/local.d.ts +1 -0
- package/dist/sandbox/provider/local.d.ts.map +1 -1
- package/dist/sandbox/provider/local.js +95 -31
- package/dist/sandbox/provider/local.js.map +1 -1
- package/dist/schedules/cron.js +1 -1
- package/dist/schedules/cron.js.map +1 -1
- package/dist/schedules/describe.js +1 -1
- package/dist/schedules/describe.js.map +1 -1
- package/dist/schedules/next-fire.d.ts +2 -2
- package/dist/schedules/next-fire.d.ts.map +1 -1
- package/dist/schedules/next-fire.js +7 -4
- package/dist/schedules/next-fire.js.map +1 -1
- package/dist/schedules/spec.d.ts.map +1 -1
- package/dist/schedules/spec.js +23 -7
- package/dist/schedules/spec.js.map +1 -1
- package/dist/schedules/types.d.ts +2 -0
- package/dist/schedules/types.d.ts.map +1 -1
- package/dist/store/budget/ledger.d.ts +15 -1
- package/dist/store/budget/ledger.d.ts.map +1 -1
- package/dist/store/budget/ledger.js +80 -1
- package/dist/store/budget/ledger.js.map +1 -1
- package/dist/store/evidence/source-text.d.ts +1 -1
- package/dist/store/session-index/sqlite.d.ts.map +1 -1
- package/dist/store/session-index/sqlite.js +7 -3
- package/dist/store/session-index/sqlite.js.map +1 -1
- package/dist/store/session-log/core.d.ts.map +1 -1
- package/dist/store/session-log/core.js +88 -24
- package/dist/store/session-log/core.js.map +1 -1
- package/dist/store/session-log/disk.js +1 -1
- package/dist/store/session-log/disk.js.map +1 -1
- package/dist/tools/builtins/atomic-write-file.d.ts +2 -0
- package/dist/tools/builtins/atomic-write-file.d.ts.map +1 -1
- package/dist/tools/builtins/atomic-write-file.js +37 -8
- package/dist/tools/builtins/atomic-write-file.js.map +1 -1
- package/dist/tools/builtins/code-analysis.d.ts.map +1 -1
- package/dist/tools/builtins/code-analysis.js +4 -1
- package/dist/tools/builtins/code-analysis.js.map +1 -1
- package/dist/tools/builtins/edit-apply.d.ts.map +1 -1
- package/dist/tools/builtins/edit-apply.js +24 -10
- package/dist/tools/builtins/edit-apply.js.map +1 -1
- package/dist/tools/builtins/edit.d.ts.map +1 -1
- package/dist/tools/builtins/edit.js +31 -15
- package/dist/tools/builtins/edit.js.map +1 -1
- package/dist/tools/builtins/file-text.d.ts +3 -0
- package/dist/tools/builtins/file-text.d.ts.map +1 -0
- package/dist/tools/builtins/file-text.js +14 -0
- package/dist/tools/builtins/file-text.js.map +1 -0
- package/dist/tools/builtins/glob.d.ts.map +1 -1
- package/dist/tools/builtins/glob.js +7 -2
- package/dist/tools/builtins/glob.js.map +1 -1
- package/dist/tools/builtins/grep.d.ts.map +1 -1
- package/dist/tools/builtins/grep.js +8 -5
- package/dist/tools/builtins/grep.js.map +1 -1
- package/dist/tools/builtins/ls.d.ts.map +1 -1
- package/dist/tools/builtins/ls.js +2 -1
- package/dist/tools/builtins/ls.js.map +1 -1
- package/dist/tools/builtins/notebook-edit.d.ts.map +1 -1
- package/dist/tools/builtins/notebook-edit.js +19 -10
- package/dist/tools/builtins/notebook-edit.js.map +1 -1
- package/dist/tools/builtins/read-file.d.ts.map +1 -1
- package/dist/tools/builtins/read-file.js +61 -29
- package/dist/tools/builtins/read-file.js.map +1 -1
- package/dist/tools/builtins/read-regular-file.d.ts +5 -0
- package/dist/tools/builtins/read-regular-file.d.ts.map +1 -0
- package/dist/tools/builtins/read-regular-file.js +23 -0
- package/dist/tools/builtins/read-regular-file.js.map +1 -0
- package/dist/tools/builtins/search-support.d.ts +2 -0
- package/dist/tools/builtins/search-support.d.ts.map +1 -1
- package/dist/tools/builtins/search-support.js +14 -0
- package/dist/tools/builtins/search-support.js.map +1 -1
- package/dist/tools/builtins/stale-before-review.d.ts.map +1 -1
- package/dist/tools/builtins/stale-before-review.js +8 -5
- package/dist/tools/builtins/stale-before-review.js.map +1 -1
- package/dist/tools/builtins/write-file.d.ts.map +1 -1
- package/dist/tools/builtins/write-file.js +17 -13
- package/dist/tools/builtins/write-file.js.map +1 -1
- package/dist/tools/defineTool.d.ts.map +1 -1
- package/dist/tools/defineTool.js +4 -3
- package/dist/tools/defineTool.js.map +1 -1
- package/dist/tools/paths.d.ts +9 -0
- package/dist/tools/paths.d.ts.map +1 -1
- package/dist/tools/paths.js +39 -9
- package/dist/tools/paths.js.map +1 -1
- package/dist/tools/posix-path.d.ts +2 -14
- package/dist/tools/posix-path.d.ts.map +1 -1
- package/dist/tools/posix-path.js +7 -0
- package/dist/tools/posix-path.js.map +1 -1
- package/dist/tools/tool-is-available.d.ts +4 -0
- package/dist/tools/tool-is-available.d.ts.map +1 -0
- package/dist/tools/tool-is-available.js +8 -0
- package/dist/tools/tool-is-available.js.map +1 -0
- package/dist/types/code-analysis/index.d.ts +1 -1
- package/dist/types/code-analysis/index.d.ts.map +1 -1
- package/dist/types/common/index.d.ts +6 -0
- package/dist/types/common/index.d.ts.map +1 -1
- package/dist/types/common/index.js +13 -0
- package/dist/types/common/index.js.map +1 -1
- package/dist/types/provider/errors.d.ts.map +1 -1
- package/dist/types/provider/errors.js +3 -5
- package/dist/types/provider/errors.js.map +1 -1
- package/dist/types/sandbox/index.d.ts +13 -2
- package/dist/types/sandbox/index.d.ts.map +1 -1
- package/dist/types/sandbox/index.js.map +1 -1
- package/dist/types/session/checkpoint.d.ts +4 -0
- package/dist/types/session/checkpoint.d.ts.map +1 -1
- package/dist/types/session/checkpoint.js +2 -0
- package/dist/types/session/checkpoint.js.map +1 -1
- package/dist/types/session/events.d.ts +1 -0
- package/dist/types/session/events.d.ts.map +1 -1
- package/dist/types/session/events.js.map +1 -1
- package/dist/types/session/records.d.ts +42 -6
- package/dist/types/session/records.d.ts.map +1 -1
- package/dist/types/session/records.js +1 -0
- package/dist/types/session/records.js.map +1 -1
- package/dist/types/tool/index.d.ts +2 -0
- package/dist/types/tool/index.d.ts.map +1 -1
- package/dist/types/tool/index.js.map +1 -1
- package/dist/utils/cost.d.ts +17 -0
- package/dist/utils/cost.d.ts.map +1 -1
- package/dist/utils/cost.js +78 -8
- package/dist/utils/cost.js.map +1 -1
- package/package.json +1 -1
- package/src/bridge/acp/server.ts +16 -7
- package/src/connector/mcp/adapter.ts +4 -3
- package/src/connector/mcp/client.ts +33 -32
- package/src/index.ts +2 -0
- package/src/manager/session/turn-recorder.ts +31 -7
- package/src/pricing/catalogue.generated.ts +176 -21
- package/src/pricing/rates.source.json +108 -33
- package/src/provider/errors.ts +12 -19
- package/src/provider/stream-request-timeout.ts +34 -0
- package/src/public-runtime.ts +2 -0
- package/src/public-tools.ts +1 -0
- package/src/runtime/query/executor.ts +29 -3
- package/src/runtime/query/file-evidence-replay.ts +73 -19
- package/src/runtime/query/file-evidence-seed.ts +16 -0
- package/src/runtime/query/guard.ts +13 -2
- package/src/runtime/query/index.ts +10 -1
- package/src/runtime/query/iteration/index.ts +32 -11
- package/src/runtime/query/iteration/phases/advisory.ts +2 -2
- package/src/runtime/query/path-escalation.ts +4 -3
- package/src/runtime/query/prepare-turn.ts +2 -1
- package/src/runtime/query/tooling.ts +3 -0
- package/src/sandbox/file-walk-program.ts +1 -1
- package/src/sandbox/ignore-matcher.ts +12 -3
- package/src/sandbox/ignore-source.ts +51 -7
- package/src/sandbox/limit-error.ts +10 -0
- package/src/sandbox/provider/local.ts +107 -48
- package/src/schedules/cron.ts +1 -1
- package/src/schedules/describe.ts +1 -1
- package/src/schedules/next-fire.ts +7 -4
- package/src/schedules/spec.ts +36 -7
- package/src/schedules/types.ts +2 -0
- package/src/store/budget/ledger.ts +93 -2
- package/src/store/session-index/sqlite.ts +8 -4
- package/src/store/session-log/core.ts +96 -25
- package/src/store/session-log/disk.ts +1 -1
- package/src/tools/builtins/atomic-write-file.ts +49 -7
- package/src/tools/builtins/code-analysis.ts +6 -1
- package/src/tools/builtins/edit-apply.ts +25 -10
- package/src/tools/builtins/edit.ts +31 -15
- package/src/tools/builtins/file-text.ts +14 -0
- package/src/tools/builtins/glob.ts +6 -1
- package/src/tools/builtins/grep.ts +7 -5
- package/src/tools/builtins/ls.ts +2 -1
- package/src/tools/builtins/notebook-edit.ts +18 -11
- package/src/tools/builtins/read-file.ts +71 -24
- package/src/tools/builtins/read-regular-file.ts +25 -0
- package/src/tools/builtins/search-support.ts +13 -0
- package/src/tools/builtins/stale-before-review.ts +8 -6
- package/src/tools/builtins/write-file.ts +18 -12
- package/src/tools/defineTool.ts +4 -3
- package/src/tools/paths.ts +49 -7
- package/src/tools/posix-path.ts +9 -0
- package/src/tools/tool-is-available.ts +11 -0
- package/src/types/code-analysis/index.ts +1 -1
- package/src/types/common/index.ts +24 -0
- package/src/types/provider/errors.ts +3 -5
- package/src/types/sandbox/index.ts +12 -1
- package/src/types/session/checkpoint.ts +2 -0
- package/src/types/session/events.ts +1 -0
- package/src/types/session/records.ts +1 -0
- package/src/types/tool/index.ts +2 -0
- 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
|
-
*
|
|
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
|
|
package/src/types/tool/index.ts
CHANGED
|
@@ -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
|
|
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 &&
|
|
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
|
|
175
|
-
|
|
176
|
-
|
|
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
|
-
|
|
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.
|
|
323
|
-
return `$${usd
|
|
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)`
|