@buildanagent/gateway 0.1.0-alpha.2 → 0.1.0-alpha.4
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 +3 -3
- package/dist/adapters/wire.js +2 -2
- package/dist/adapters/wire.js.map +1 -1
- package/dist/cost.d.ts +5 -0
- package/dist/cost.js +14 -0
- package/dist/cost.js.map +1 -1
- package/dist/errors.d.ts +8 -4
- package/dist/errors.js +6 -2
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/request-policy.d.ts +7 -1
- package/dist/request-policy.js +15 -1
- package/dist/request-policy.js.map +1 -1
- package/dist/retry.js +10 -18
- package/dist/retry.js.map +1 -1
- package/dist/stream-contract.d.ts +6 -1
- package/dist/stream-contract.js +18 -2
- package/dist/stream-contract.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -51,7 +51,7 @@ A provider adapter implements one method, `stream()`. The stream it returns foll
|
|
|
51
51
|
|
|
52
52
|
A `warning` event may come anywhere between `start` and the terminal. It reports something the call survived, such as an option the provider would reject, with a snake_case `code` and a `message`. A warning is not output, so the gateway still retries a call that has only warned. A retry does not repeat a warning that an earlier attempt streamed with the same code and message.
|
|
53
53
|
|
|
54
|
-
This check reads a stream and throws a `StreamContractError` at the first broken rule:
|
|
54
|
+
This check reads a stream and throws a `StreamContractError` at the first broken rule. The error is a `GatewayError` with code `stream_contract`, and its `position` says which event broke the rule:
|
|
55
55
|
|
|
56
56
|
```ts
|
|
57
57
|
import { assertStreamContract } from "@buildanagent/gateway/testing";
|
|
@@ -80,7 +80,7 @@ The gateway prices each response from the price catalog, so every `AssistantMess
|
|
|
80
80
|
- `"catalog"`: the gateway multiplied each token class by its catalog rate, and `catalogAsOf` gives the date those rates were copied.
|
|
81
81
|
- `"unknown"`: the catalog doesn't list the model, or the provider never reported the usage, so `total` is `null`. Usage goes unreported when a request fails before any response arrives, when a stream throws or breaks off before its usage arrives, and when a reply ends without the usage chunk a server should send. A failed request's usage is unreported because the request may have reached the provider before the connection broke. The gateway never guesses a zero, because a budget that counts an unknown price as free never stops. An HTTP error response is the one failure that counts as zero tokens, because the provider sent an error instead of a reply.
|
|
82
82
|
|
|
83
|
-
A retried call's cost sums its attempts, and the sum takes the weakest basis among them. It is `"unknown"` if any attempt's cost is unknown, and `"catalog"`, with `catalogAsOf`, if any attempt was priced from the catalog. It is `"provider"` only when the provider billed every attempt.
|
|
83
|
+
A retried call's cost sums its attempts, and the sum takes the weakest basis among them. `addCost(a, b)` adds two costs by the same rule, such as the costs of every call in a run. It is `"unknown"` if any attempt's cost is unknown, and `"catalog"`, with `catalogAsOf`, if any attempt was priced from the catalog. It is `"provider"` only when the provider billed every attempt.
|
|
84
84
|
|
|
85
85
|
Some models charge more for long prompts. Their entries list several rate tiers, and each request pays the tier its total input selects, cache reads and writes included. The gateway prices each attempt of a call on its own before it adds them up, so a retried call pays each request's own tier. `model.price(usage)` applies the same rates to one request's usage.
|
|
86
86
|
|
|
@@ -293,7 +293,7 @@ A `ModelMiddleware` can rewrite the request with `transformParams` and watch or
|
|
|
293
293
|
|
|
294
294
|
## Tests run offline
|
|
295
295
|
|
|
296
|
-
Start each test file with `import "@buildanagent/gateway/testing/setup"`. The setup blocks every request to a host other than your own machine, stops the run if a provider API key is set, and turns real model requests off. Your tests then never reach a real provider by accident.
|
|
296
|
+
Start each test file with `import "@buildanagent/gateway/testing/setup"`. The setup blocks every request to a host other than your own machine, stops the run if a provider API key is set, and turns real model requests off. Your tests then never reach a real provider by accident. An adapter that tries anyway throws a `ModelRequestsDisabledError`, a `GatewayError` with code `model_requests_disabled`, before it sends anything.
|
|
297
297
|
|
|
298
298
|
To test an adapter's wire format, pass `mockTransport(api, script)` as its `transport`. The mock answers the n-th request with the n-th scripted response, splits each body into chunks of `chunkSize` bytes or at `splitAt` offsets, and records every request in `requests`. A request past the end of the script throws a `GatewayError` with code `unexpected_request`. `serverSentEvents(events)` writes a list of events as an Anthropic stream body, and `chatCompletionEvents(chunks)` writes Chat Completions chunks followed by `data: [DONE]`:
|
|
299
299
|
|
package/dist/adapters/wire.js
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
import { classifyError, isTransportFailure } from "../classify.js";
|
|
11
11
|
import { isGatewayError } from "../errors.js";
|
|
12
|
-
import { assertModelRequestAllowed
|
|
12
|
+
import { assertModelRequestAllowed } from "../request-policy.js";
|
|
13
13
|
import { readServerSentEvents } from "../sse.js";
|
|
14
14
|
import { emptyUsage, unreportedUsage } from "../usage.js";
|
|
15
15
|
/** True for a JSON object, not an array or `null`. */
|
|
@@ -150,7 +150,7 @@ export async function* sendRequest(transport, request, model, signal) {
|
|
|
150
150
|
return await transport(request);
|
|
151
151
|
}
|
|
152
152
|
catch (thrown) {
|
|
153
|
-
if (isGatewayError(thrown)
|
|
153
|
+
if (isGatewayError(thrown))
|
|
154
154
|
throw thrown;
|
|
155
155
|
yield signal?.aborted ? abortedEvent(model) : thrownTerminal(model, thrown);
|
|
156
156
|
return undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wire.js","sourceRoot":"","sources":["../../src/adapters/wire.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,aAAa,EAAkB,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACnF,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,yBAAyB,EAAE,0BAA0B,EAAE,MAAM,sBAAsB,CAAC;AAG7F,OAAO,EAAE,oBAAoB,EAAwB,MAAM,WAAW,CAAC;AAEvE,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAyB1D,sDAAsD;AACtD,MAAM,UAAU,QAAQ,CAAC,KAAc;IACrC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAA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C,MAAM,CAAC,KAAK,EAAE,CAAC;AACxB,CAAC","sourcesContent":["/**\n * What every wire adapter shares: sending one HTTP request through the caller's transport, reading a server-sent\n * event stream to its terminal, and turning a failed request, a broken stream or an abort into an `error` terminal.\n * A failed model call never throws; only a guard's error or a `GatewayError` does.\n *\n * Usage: an attempt that ends without a usage report has `unreported` usage, so `createModel()` prices it as unknown.\n * The one exception is an HTTP error response, which counts as zero tokens: the provider answered with an error\n * instead of a reply, and its body reports no usage to read.\n */\nimport { classifyError, type ErrorKind, isTransportFailure } from \"../classify.js\";\nimport { isGatewayError } from \"../errors.js\";\nimport { assertModelRequestAllowed, ModelRequestsDisabledError } from \"../request-policy.js\";\nimport type { Transport } from \"../runtime-types.js\";\nimport type { ModelEvent, StreamOptions } from \"../spec.js\";\nimport { readServerSentEvents, type ServerSentEvent } from \"../sse.js\";\nimport type { AssistantMessage, ContentBlock, ModelErrorCode, ModelRef, Usage } from \"../types.js\";\nimport { emptyUsage, unreportedUsage } from \"../usage.js\";\n\n/** A JSON object as the adapters read it off the wire. */\nexport type Json = Record<string, unknown>;\n\n/** The content and usage an attempt reached before it ended. */\nexport interface PartialReply {\n readonly content?: ContentBlock[];\n readonly usage?: Usage;\n}\n\n/** What one failure says about itself. */\nexport interface WireFailure {\n readonly status?: number;\n /** The provider's error type, such as `\"invalid_request_error\"`. */\n readonly type?: string;\n /** The provider's finer error code, when the error has one, such as `\"insufficient_quota\"`. */\n readonly errorCode?: string;\n readonly message: string;\n /** Sets the kind outright, for a failure that no status describes, such as a refusal inside a 200 stream. */\n readonly kind?: ErrorKind;\n /** How long the response's `retry-after-ms` or `retry-after` header asked the caller to wait, in milliseconds. */\n readonly retryAfterMs?: number;\n}\n\n/** True for a JSON object, not an array or `null`. */\nexport function isObject(value: unknown): value is Json {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\n/** The value when it is a string, or `\"\"`. */\nexport function str(value: unknown): string {\n return typeof value === \"string\" ? value : \"\";\n}\n\n/** A token count: a finite positive number, or 0. */\nexport function count(value: unknown): number {\n return typeof value === \"number\" && Number.isFinite(value) && value > 0 ? value : 0;\n}\n\n/** A tool call's input: the parsed JSON, or the partial JSON text when the output limit cut it off. */\nexport function parseToolInput(text: string, empty: unknown = {}): unknown {\n if (text === \"\") return empty;\n try {\n return JSON.parse(text);\n } catch {\n return text;\n }\n}\n\n/**\n * An assistant message with an unknown cost, which `createModel()` prices from the catalog. Without `partial.usage`,\n * the provider reported no usage, so the message's usage is `unreported`.\n */\nexport function terminalMessage(\n model: ModelRef,\n partial: PartialReply,\n stopReason: AssistantMessage[\"stopReason\"],\n error?: NonNullable<AssistantMessage[\"error\"]>,\n): AssistantMessage {\n return {\n role: \"assistant\",\n content: partial.content ?? [],\n model,\n stopReason,\n ...(error ? { error } : {}),\n usage: partial.usage ?? unreportedUsage(),\n cost: { total: null, basis: \"unknown\", currency: \"USD\" },\n };\n}\n\n/** The `error` terminal of an aborted call, which carries no `error`. */\nexport function abortedEvent(model: ModelRef, partial: PartialReply = {}): Extract<ModelEvent, { type: \"error\" }> {\n return { type: \"error\", message: terminalMessage(model, partial, \"aborted\") };\n}\n\n/**\n * The `error` terminal for a failure. A failure with no HTTP status, such as an error event inside a 200 stream,\n * classifies like the status its type stands for in `statusOfType`. The message starts with the error code or type.\n * A failure with a status is an HTTP error response, so without `partial.usage` it reports zero tokens.\n */\nexport function failureTerminal(\n model: ModelRef,\n failure: WireFailure,\n statusOfType: Readonly<Record<string, number>>,\n partial: PartialReply = {},\n code?: ModelErrorCode,\n): Extract<ModelEvent, { type: \"error\" }> {\n const status = failure.status ?? (failure.type ? statusOfType[failure.type] : undefined);\n const label = failure.errorCode ?? failure.type;\n // Both go to the classifier: a billing error's code names its cause while its type says `insufficient_quota`.\n const codes = [failure.errorCode, failure.type].filter((part) => part !== undefined && part !== \"\").join(\" \");\n const kind =\n failure.kind ??\n classifyError({\n ...(status === undefined ? {} : { status }),\n ...(codes ? { code: codes } : {}),\n message: failure.message,\n });\n const error: NonNullable<AssistantMessage[\"error\"]> = {\n kind,\n message: label ? `${label}: ${failure.message}` : failure.message,\n ...(failure.status === undefined ? {} : { status: failure.status }),\n ...(code ? { code } : {}),\n ...(failure.retryAfterMs === undefined ? {} : { retryAfterMs: failure.retryAfterMs }),\n };\n const reply = failure.status === undefined || partial.usage ? partial : { ...partial, usage: emptyUsage() };\n return { type: \"error\", message: terminalMessage(model, reply, \"error\", error) };\n}\n\n/** The `error` terminal for a throw: retryable only for a transport failure, and `fatal` for a bug or bad data. */\nexport function thrownTerminal(model: ModelRef, thrown: unknown, partial: PartialReply = {}) {\n const message = thrown instanceof Error ? `${thrown.name}: ${thrown.message}` : String(thrown);\n return failureTerminal(model, { message, ...(isTransportFailure(thrown) ? {} : { kind: \"fatal\" }) }, {}, partial);\n}\n\n// A delay in seconds, as `retry-after` sends it, or in milliseconds, as `retry-after-ms` does: a non-negative decimal.\nconst DELAY = /^\\d+(\\.\\d+)?$/;\n\n// The three HTTP-date forms of RFC 9110, section 5.6.7: IMF-fixdate, the obsolete RFC 850 form, and asctime, which\n// has no zone and means GMT. `Date.parse` alone would also take text such as \"-5\" or \"5, 10\" as a date.\nconst GMT_DATE = /^[A-Z][a-z]{2,8}, (\\d{2} [A-Z][a-z]{2} \\d{4}|\\d{2}-[A-Z][a-z]{2}-\\d{2}) \\d{2}:\\d{2}:\\d{2} GMT$/;\nconst ASCTIME_DATE = /^[A-Z][a-z]{2} [A-Z][a-z]{2} [ \\d]\\d \\d{2}:\\d{2}:\\d{2} \\d{4}$/;\n\n/** The time an HTTP-date names, in milliseconds since the epoch, or `NaN` for any other text. */\nfunction httpDate(value: string | null): number {\n if (value === null) return Number.NaN;\n const text = value.trim();\n if (GMT_DATE.test(text)) return Date.parse(text);\n return ASCTIME_DATE.test(text) ? Date.parse(`${text} GMT`) : Number.NaN;\n}\n\n/**\n * How long a failed response asks the caller to wait before retrying, in milliseconds, or `undefined` when it gives\n * no valid delay. `retry-after-ms` wins over `retry-after`, as in the official TypeScript SDKs (openai-node v7.49.0\n * and anthropic-sdk-typescript v0.129.0, `src/client.ts`, as of November 2026). `retry-after` holds seconds, as\n * Anthropic's and OpenAI's rate-limit pages say (https://platform.claude.com/docs/en/api/rate-limits and\n * https://developers.openai.com/api/docs/guides/rate-limits, as of November 2026), or an HTTP date (RFC 9110,\n * section 10.2.3). A date counts from the response's own `date` header, so the delay depends on no local clock, and\n * a response without one gives no delay. Any other value, such as a negative number or the comma-joined values of a\n * repeated header, gives no delay.\n */\nexport function retryAfterMs(headers: Headers): number | undefined {\n const millis = headers.get(\"retry-after-ms\")?.trim();\n if (millis !== undefined && DELAY.test(millis)) return Number(millis);\n const value = headers.get(\"retry-after\")?.trim();\n if (value === undefined || value === \"\") return undefined;\n if (DELAY.test(value)) return Number(value) * 1000;\n const until = httpDate(value);\n const sent = httpDate(headers.get(\"date\"));\n if (Number.isNaN(until) || Number.isNaN(sent)) return undefined;\n return Math.max(0, until - sent);\n}\n\n/** A failed response's body, parsed as JSON when it is JSON, and its text. */\nexport async function readErrorBody(response: Response): Promise<{ body: unknown; text: string }> {\n const text = await response.text().catch(() => \"\");\n try {\n return { body: JSON.parse(text), text };\n } catch {\n return { body: undefined, text };\n }\n}\n\n/** The body `onPayload` asks for, or the adapter's own when it returns `undefined`. */\nexport function payloadOf(body: unknown, options: StreamOptions): unknown {\n const replaced = options.onPayload ? options.onPayload(body) : undefined;\n return replaced === undefined ? body : replaced;\n}\n\n/**\n * Sends one HTTP request. A transport that throws becomes an `error` terminal unless the throw is a guard's; only a\n * transport failure, by the rule the retries use, may be retried, and any other throw is `fatal`.\n */\nexport async function* sendRequest(\n transport: Transport,\n request: Request,\n model: ModelRef,\n signal: AbortSignal | undefined,\n): AsyncGenerator<ModelEvent, Response | undefined> {\n assertModelRequestAllowed(transport);\n try {\n return await transport(request);\n } catch (thrown) {\n if (isGatewayError(thrown) || thrown instanceof ModelRequestsDisabledError) throw thrown;\n yield signal?.aborted ? abortedEvent(model) : thrownTerminal(model, thrown);\n return undefined;\n }\n}\n\n/** How one protocol reads its event stream. */\nexport interface StreamReader {\n /** The events one server-sent event produces. The stream is over once one of them is the terminal. */\n handle(event: ServerSentEvent): ModelEvent[];\n /** The content and usage so far, for an `error` terminal. */\n partial(): PartialReply;\n /** The events for a stream that closed before its terminal, ending in a terminal. */\n ended(): ModelEvent[];\n}\n\n/** Feeds each server-sent event to the reader; returns true once it has yielded the terminal. */\nasync function* pump(body: ReadableStream<Uint8Array>, reader: StreamReader): AsyncGenerator<ModelEvent, boolean> {\n for await (const sse of readServerSentEvents(body)) {\n for (const event of reader.handle(sse)) {\n yield event;\n if (event.type === \"done\" || event.type === \"error\") return true;\n }\n }\n return false;\n}\n\n/** Reads a 200 stream to its terminal. A stream that breaks or ends early ends in an `error` terminal. */\nexport async function* readEventStream(\n response: Response,\n model: ModelRef,\n reader: StreamReader,\n signal: AbortSignal | undefined,\n): AsyncGenerator<ModelEvent> {\n if (response.body === null) {\n yield failureTerminal(model, { message: \"the response has no body\" }, {});\n return;\n }\n try {\n if (yield* pump(response.body, reader)) return;\n } catch (thrown) {\n // A broken connection may work on a retry; malformed event data, a SyntaxError, will not parse on one either.\n yield signal?.aborted ? abortedEvent(model, reader.partial()) : thrownTerminal(model, thrown, reader.partial());\n return;\n }\n yield* reader.ended();\n}\n"]}
|
|
1
|
+
{"version":3,"file":"wire.js","sourceRoot":"","sources":["../../src/adapters/wire.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,aAAa,EAAkB,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACnF,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,yBAAyB,EAAE,MAAM,sBAAsB,CAAC;AAGjE,OAAO,EAAE,oBAAoB,EAAwB,MAAM,WAAW,CAAC;AAEvE,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAyB1D,sDAAsD;AACtD,MAAM,UAAU,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E,CAAC,CAAC;QAChH,OAAO;IACT,CAAC;IACD,KAAK,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;AACxB,CAAC","sourcesContent":["/**\n * What every wire adapter shares: sending one HTTP request through the caller's transport, reading a server-sent\n * event stream to its terminal, and turning a failed request, a broken stream or an abort into an `error` terminal.\n * A failed model call never throws; only a guard's error or a `GatewayError` does.\n *\n * Usage: an attempt that ends without a usage report has `unreported` usage, so `createModel()` prices it as unknown.\n * The one exception is an HTTP error response, which counts as zero tokens: the provider answered with an error\n * instead of a reply, and its body reports no usage to read.\n */\nimport { classifyError, type ErrorKind, isTransportFailure } from \"../classify.js\";\nimport { isGatewayError } from \"../errors.js\";\nimport { assertModelRequestAllowed } from \"../request-policy.js\";\nimport type { Transport } from \"../runtime-types.js\";\nimport type { ModelEvent, StreamOptions } from \"../spec.js\";\nimport { readServerSentEvents, type ServerSentEvent } from \"../sse.js\";\nimport type { AssistantMessage, ContentBlock, ModelErrorCode, ModelRef, Usage } from \"../types.js\";\nimport { emptyUsage, unreportedUsage } from \"../usage.js\";\n\n/** A JSON object as the adapters read it off the wire. */\nexport type Json = Record<string, unknown>;\n\n/** The content and usage an attempt reached before it ended. */\nexport interface PartialReply {\n readonly content?: ContentBlock[];\n readonly usage?: Usage;\n}\n\n/** What one failure says about itself. */\nexport interface WireFailure {\n readonly status?: number;\n /** The provider's error type, such as `\"invalid_request_error\"`. */\n readonly type?: string;\n /** The provider's finer error code, when the error has one, such as `\"insufficient_quota\"`. */\n readonly errorCode?: string;\n readonly message: string;\n /** Sets the kind outright, for a failure that no status describes, such as a refusal inside a 200 stream. */\n readonly kind?: ErrorKind;\n /** How long the response's `retry-after-ms` or `retry-after` header asked the caller to wait, in milliseconds. */\n readonly retryAfterMs?: number;\n}\n\n/** True for a JSON object, not an array or `null`. */\nexport function isObject(value: unknown): value is Json {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\n/** The value when it is a string, or `\"\"`. */\nexport function str(value: unknown): string {\n return typeof value === \"string\" ? value : \"\";\n}\n\n/** A token count: a finite positive number, or 0. */\nexport function count(value: unknown): number {\n return typeof value === \"number\" && Number.isFinite(value) && value > 0 ? value : 0;\n}\n\n/** A tool call's input: the parsed JSON, or the partial JSON text when the output limit cut it off. */\nexport function parseToolInput(text: string, empty: unknown = {}): unknown {\n if (text === \"\") return empty;\n try {\n return JSON.parse(text);\n } catch {\n return text;\n }\n}\n\n/**\n * An assistant message with an unknown cost, which `createModel()` prices from the catalog. Without `partial.usage`,\n * the provider reported no usage, so the message's usage is `unreported`.\n */\nexport function terminalMessage(\n model: ModelRef,\n partial: PartialReply,\n stopReason: AssistantMessage[\"stopReason\"],\n error?: NonNullable<AssistantMessage[\"error\"]>,\n): AssistantMessage {\n return {\n role: \"assistant\",\n content: partial.content ?? [],\n model,\n stopReason,\n ...(error ? { error } : {}),\n usage: partial.usage ?? unreportedUsage(),\n cost: { total: null, basis: \"unknown\", currency: \"USD\" },\n };\n}\n\n/** The `error` terminal of an aborted call, which carries no `error`. */\nexport function abortedEvent(model: ModelRef, partial: PartialReply = {}): Extract<ModelEvent, { type: \"error\" }> {\n return { type: \"error\", message: terminalMessage(model, partial, \"aborted\") };\n}\n\n/**\n * The `error` terminal for a failure. A failure with no HTTP status, such as an error event inside a 200 stream,\n * classifies like the status its type stands for in `statusOfType`. The message starts with the error code or type.\n * A failure with a status is an HTTP error response, so without `partial.usage` it reports zero tokens.\n */\nexport function failureTerminal(\n model: ModelRef,\n failure: WireFailure,\n statusOfType: Readonly<Record<string, number>>,\n partial: PartialReply = {},\n code?: ModelErrorCode,\n): Extract<ModelEvent, { type: \"error\" }> {\n const status = failure.status ?? (failure.type ? statusOfType[failure.type] : undefined);\n const label = failure.errorCode ?? failure.type;\n // Both go to the classifier: a billing error's code names its cause while its type says `insufficient_quota`.\n const codes = [failure.errorCode, failure.type].filter((part) => part !== undefined && part !== \"\").join(\" \");\n const kind =\n failure.kind ??\n classifyError({\n ...(status === undefined ? {} : { status }),\n ...(codes ? { code: codes } : {}),\n message: failure.message,\n });\n const error: NonNullable<AssistantMessage[\"error\"]> = {\n kind,\n message: label ? `${label}: ${failure.message}` : failure.message,\n ...(failure.status === undefined ? {} : { status: failure.status }),\n ...(code ? { code } : {}),\n ...(failure.retryAfterMs === undefined ? {} : { retryAfterMs: failure.retryAfterMs }),\n };\n const reply = failure.status === undefined || partial.usage ? partial : { ...partial, usage: emptyUsage() };\n return { type: \"error\", message: terminalMessage(model, reply, \"error\", error) };\n}\n\n/** The `error` terminal for a throw: retryable only for a transport failure, and `fatal` for a bug or bad data. */\nexport function thrownTerminal(model: ModelRef, thrown: unknown, partial: PartialReply = {}) {\n const message = thrown instanceof Error ? `${thrown.name}: ${thrown.message}` : String(thrown);\n return failureTerminal(model, { message, ...(isTransportFailure(thrown) ? {} : { kind: \"fatal\" }) }, {}, partial);\n}\n\n// A delay in seconds, as `retry-after` sends it, or in milliseconds, as `retry-after-ms` does: a non-negative decimal.\nconst DELAY = /^\\d+(\\.\\d+)?$/;\n\n// The three HTTP-date forms of RFC 9110, section 5.6.7: IMF-fixdate, the obsolete RFC 850 form, and asctime, which\n// has no zone and means GMT. `Date.parse` alone would also take text such as \"-5\" or \"5, 10\" as a date.\nconst GMT_DATE = /^[A-Z][a-z]{2,8}, (\\d{2} [A-Z][a-z]{2} \\d{4}|\\d{2}-[A-Z][a-z]{2}-\\d{2}) \\d{2}:\\d{2}:\\d{2} GMT$/;\nconst ASCTIME_DATE = /^[A-Z][a-z]{2} [A-Z][a-z]{2} [ \\d]\\d \\d{2}:\\d{2}:\\d{2} \\d{4}$/;\n\n/** The time an HTTP-date names, in milliseconds since the epoch, or `NaN` for any other text. */\nfunction httpDate(value: string | null): number {\n if (value === null) return Number.NaN;\n const text = value.trim();\n if (GMT_DATE.test(text)) return Date.parse(text);\n return ASCTIME_DATE.test(text) ? Date.parse(`${text} GMT`) : Number.NaN;\n}\n\n/**\n * How long a failed response asks the caller to wait before retrying, in milliseconds, or `undefined` when it gives\n * no valid delay. `retry-after-ms` wins over `retry-after`, as in the official TypeScript SDKs (openai-node v7.49.0\n * and anthropic-sdk-typescript v0.129.0, `src/client.ts`, as of November 2026). `retry-after` holds seconds, as\n * Anthropic's and OpenAI's rate-limit pages say (https://platform.claude.com/docs/en/api/rate-limits and\n * https://developers.openai.com/api/docs/guides/rate-limits, as of November 2026), or an HTTP date (RFC 9110,\n * section 10.2.3). A date counts from the response's own `date` header, so the delay depends on no local clock, and\n * a response without one gives no delay. Any other value, such as a negative number or the comma-joined values of a\n * repeated header, gives no delay.\n */\nexport function retryAfterMs(headers: Headers): number | undefined {\n const millis = headers.get(\"retry-after-ms\")?.trim();\n if (millis !== undefined && DELAY.test(millis)) return Number(millis);\n const value = headers.get(\"retry-after\")?.trim();\n if (value === undefined || value === \"\") return undefined;\n if (DELAY.test(value)) return Number(value) * 1000;\n const until = httpDate(value);\n const sent = httpDate(headers.get(\"date\"));\n if (Number.isNaN(until) || Number.isNaN(sent)) return undefined;\n return Math.max(0, until - sent);\n}\n\n/** A failed response's body, parsed as JSON when it is JSON, and its text. */\nexport async function readErrorBody(response: Response): Promise<{ body: unknown; text: string }> {\n const text = await response.text().catch(() => \"\");\n try {\n return { body: JSON.parse(text), text };\n } catch {\n return { body: undefined, text };\n }\n}\n\n/** The body `onPayload` asks for, or the adapter's own when it returns `undefined`. */\nexport function payloadOf(body: unknown, options: StreamOptions): unknown {\n const replaced = options.onPayload ? options.onPayload(body) : undefined;\n return replaced === undefined ? body : replaced;\n}\n\n/**\n * Sends one HTTP request. A transport that throws becomes an `error` terminal unless the throw is a guard's; only a\n * transport failure, by the rule the retries use, may be retried, and any other throw is `fatal`.\n */\nexport async function* sendRequest(\n transport: Transport,\n request: Request,\n model: ModelRef,\n signal: AbortSignal | undefined,\n): AsyncGenerator<ModelEvent, Response | undefined> {\n assertModelRequestAllowed(transport);\n try {\n return await transport(request);\n } catch (thrown) {\n if (isGatewayError(thrown)) throw thrown;\n yield signal?.aborted ? abortedEvent(model) : thrownTerminal(model, thrown);\n return undefined;\n }\n}\n\n/** How one protocol reads its event stream. */\nexport interface StreamReader {\n /** The events one server-sent event produces. The stream is over once one of them is the terminal. */\n handle(event: ServerSentEvent): ModelEvent[];\n /** The content and usage so far, for an `error` terminal. */\n partial(): PartialReply;\n /** The events for a stream that closed before its terminal, ending in a terminal. */\n ended(): ModelEvent[];\n}\n\n/** Feeds each server-sent event to the reader; returns true once it has yielded the terminal. */\nasync function* pump(body: ReadableStream<Uint8Array>, reader: StreamReader): AsyncGenerator<ModelEvent, boolean> {\n for await (const sse of readServerSentEvents(body)) {\n for (const event of reader.handle(sse)) {\n yield event;\n if (event.type === \"done\" || event.type === \"error\") return true;\n }\n }\n return false;\n}\n\n/** Reads a 200 stream to its terminal. A stream that breaks or ends early ends in an `error` terminal. */\nexport async function* readEventStream(\n response: Response,\n model: ModelRef,\n reader: StreamReader,\n signal: AbortSignal | undefined,\n): AsyncGenerator<ModelEvent> {\n if (response.body === null) {\n yield failureTerminal(model, { message: \"the response has no body\" }, {});\n return;\n }\n try {\n if (yield* pump(response.body, reader)) return;\n } catch (thrown) {\n // A broken connection may work on a retry; malformed event data, a SyntaxError, will not parse on one either.\n yield signal?.aborted ? abortedEvent(model, reader.partial()) : thrownTerminal(model, thrown, reader.partial());\n return;\n }\n yield* reader.ended();\n}\n"]}
|
package/dist/cost.d.ts
CHANGED
|
@@ -14,6 +14,11 @@ import type { AssistantMessage, Cost, ModelContext, Usage } from "./types.js";
|
|
|
14
14
|
* `{ total: null, basis: "unknown" }`.
|
|
15
15
|
*/
|
|
16
16
|
export declare function priceUsage(usage: Usage, entry: CatalogEntry | undefined): Cost;
|
|
17
|
+
/**
|
|
18
|
+
* Adds two costs, such as the costs of two model calls. The sum is unknown if either cost is, a catalog estimate if
|
|
19
|
+
* either is one, and a provider bill only when both are. A catalog sum keeps the later cost's `catalogAsOf`.
|
|
20
|
+
*/
|
|
21
|
+
export declare function addCost(a: Cost, b: Cost): Cost;
|
|
17
22
|
/** The message's cost: the provider's billed total when the adapter reported one, else `price(usage)`. */
|
|
18
23
|
export declare function foldCost(message: AssistantMessage, price: (usage: Usage) => Cost): Cost;
|
|
19
24
|
/** The token counts a projection prices: the input to send and the most output the call may produce. */
|
package/dist/cost.js
CHANGED
|
@@ -26,6 +26,20 @@ export function priceUsage(usage, entry) {
|
|
|
26
26
|
PER_MILLION;
|
|
27
27
|
return { total, basis: "catalog", currency: "USD", catalogAsOf: entry.asOf };
|
|
28
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* Adds two costs, such as the costs of two model calls. The sum is unknown if either cost is, a catalog estimate if
|
|
31
|
+
* either is one, and a provider bill only when both are. A catalog sum keeps the later cost's `catalogAsOf`.
|
|
32
|
+
*/
|
|
33
|
+
export function addCost(a, b) {
|
|
34
|
+
if (a.total === null || b.total === null || a.basis === "unknown" || b.basis === "unknown") {
|
|
35
|
+
return { ...UNKNOWN_COST };
|
|
36
|
+
}
|
|
37
|
+
const total = a.total + b.total;
|
|
38
|
+
if (a.basis === "provider" && b.basis === "provider")
|
|
39
|
+
return { total, basis: "provider", currency: "USD" };
|
|
40
|
+
const catalogAsOf = b.catalogAsOf ?? a.catalogAsOf;
|
|
41
|
+
return { total, basis: "catalog", currency: "USD", ...(catalogAsOf === undefined ? {} : { catalogAsOf }) };
|
|
42
|
+
}
|
|
29
43
|
/** The message's cost: the provider's billed total when the adapter reported one, else `price(usage)`. */
|
|
30
44
|
export function foldCost(message, price) {
|
|
31
45
|
if (message.cost.basis === "provider" && message.cost.total !== null)
|
package/dist/cost.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cost.js","sourceRoot":"","sources":["../src/cost.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAEpD,MAAM,WAAW,GAAG,SAAS,CAAC;AAE9B,4FAA4F;AAC5F,MAAM,YAAY,GAAS,EAAE,KAAK,EAAE,IA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,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC,GAAG,GAAG,eAAe,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IAC/F,MAAM,KAAK,GAAG,YAAY,CAAC,QAAQ,CAAC,CAAC;IACrC,OAAO,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,MAAM,GAAG,KAAK,CAAC,CAAC;AAC7D,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAAC,KAAY,EAAE,OAAqB,EAAE,OAAO,GAAqB,EAAE;IACjG,MAAM,MAAM,GAAG;QACb,WAAW,EAAE,mBAAmB,CAAC,OAAO,CAAC;QACzC,eAAe,EAAE,OAAO,CAAC,eAAe,IAAI,KAAK,CAAC,IAAI,CAAC,eAAe;KACvE,CAAC;IACF,OAAO,WAAW,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC;AAC5D,CAAC","sourcesContent":["/**\n * Pricing usage against the catalog, and projecting a call's cost before it is sent (D3, D4, D9; ADR 0043).\n *\n * Separate token classes (D3) mean the fold multiplies each class by its own rate with no conversion. A provider's\n * billed total wins over the catalog, and a model the catalog lacks, or a response whose usage the provider didn't\n * report, has an unknown price, never a guessed zero.\n */\nimport type { CatalogEntry, RateTier } from \"./catalog.js\";\nimport type { Model, ModelCallOptions } from \"./runtime-types.js\";\nimport type { AssistantMessage, ContentBlock, Cost, Message, ModelContext, Usage } from \"./types.js\";\nimport { emptyUsage, totalInput } from \"./usage.js\";\n\nconst PER_MILLION = 1_000_000;\n\n/** The cost of a model the catalog doesn't list, or of usage the provider didn't report. */\nconst UNKNOWN_COST: Cost = { total: null, basis: \"unknown\", currency: \"USD\" };\n\n/** The first tier whose limit covers `inputTokens`; the catalog check guarantees the last tier has no limit. */\nfunction tierFor(rates: readonly RateTier[], inputTokens: number): RateTier | undefined {\n return rates.find((tier) => tier.upToInputTokens === null || inputTokens <= tier.upToInputTokens);\n}\n\n/**\n * Prices one response's usage with a catalog entry. Each token class is multiplied by its rate in the tier that the\n * response's total input selects. Without an entry, or for usage marked `unreported`, the result is\n * `{ total: null, basis: \"unknown\" }`.\n */\nexport function priceUsage(usage: Usage, entry: CatalogEntry | undefined): Cost {\n // Zero counts the provider never reported are not a price of zero, and a budget must not treat them as one.\n if (usage.unreported) return { ...UNKNOWN_COST };\n const tier = entry ? tierFor(entry.rates, totalInput(usage)) : undefined;\n if (entry === undefined || tier === undefined) return { ...UNKNOWN_COST };\n const total =\n (usage.uncachedInput * tier.uncachedInput +\n usage.cacheRead * tier.cacheRead +\n usage.cacheWrite[\"5m\"] * tier.cacheWrite[\"5m\"] +\n usage.cacheWrite[\"1h\"] * tier.cacheWrite[\"1h\"] +\n usage.output * tier.output) /\n PER_MILLION;\n return { total, basis: \"catalog\", currency: \"USD\", catalogAsOf: entry.asOf };\n}\n\n/** The message's cost: the provider's billed total when the adapter reported one, else `price(usage)`. */\nexport function foldCost(message: AssistantMessage, price: (usage: Usage) => Cost): Cost {\n if (message.cost.basis === \"provider\" && message.cost.total !== null) return message.cost;\n return price(message.usage);\n}\n\n/** The token counts a projection prices: the input to send and the most output the call may produce. */\nexport interface ProjectedTokens {\n readonly inputTokens: number;\n readonly maxOutputTokens: number;\n}\n\n/**\n * The most one call can cost: every input token at the highest input rate in the tier the input selects (the\n * uncached rate or either cache-write rate, whichever costs most), plus `maxOutputTokens` at the output rate. Without\n * an entry, the result is `{ total: null, basis: \"unknown\" }`.\n */\nexport function projectCost(tokens: ProjectedTokens, entry: CatalogEntry | undefined): Cost {\n return projectWith(tokens, (usage) => priceUsage(usage, entry));\n}\n\n// Every input token is put in one class at a time and the dearest result wins. The tier depends only on total input,\n// which is the same in each candidate, so this prices the input at the tier's highest input rate without reading the\n// rates themselves; `Model.price()` keeps its catalog entry private.\nfunction projectWith(tokens: ProjectedTokens, price: (usage: Usage) => Cost): Cost {\n const base = { ...emptyUsage(), output: tokens.maxOutputTokens };\n const n = tokens.inputTokens;\n const candidates = [\n price({ ...base, uncachedInput: n }),\n price({ ...base, cacheWrite: { \"5m\": n, \"1h\": 0 } }),\n price({ ...base, cacheWrite: { \"5m\": 0, \"1h\": n } }),\n ];\n return candidates.reduce((most, cost) => ((cost.total ?? -1) > (most.total ?? -1) ? cost : most));\n}\n\n// Anthropic's models overview puts 1M tokens at roughly 2.5M Unicode characters on its current tokenizer, as of\n// November 2026. Other tokenizers fit more characters in a token, so this rate errs towards a higher estimate.\nconst CHARS_PER_TOKEN = 2.5;\n// The largest per-image cost a first provider documents: Anthropic's high-resolution limit of 4,784 visual tokens\n// (vision docs, as of November 2026). Counting an image's base64 data instead would overstate it a hundredfold.\nconst TOKENS_PER_IMAGE = 4784;\n\nfunction blockChars(block: ContentBlock): number {\n switch (block.type) {\n case \"text\":\n case \"reasoning\":\n return block.text.length;\n case \"tool_call\":\n return block.name.length + JSON.stringify(block.input ?? null).length;\n case \"image\":\n return 0;\n }\n}\n\nfunction messageEstimate(message: Message): number {\n const blocks: readonly ContentBlock[] = message.content;\n const chars = blocks.reduce((sum, block) => sum + blockChars(block), 0);\n const images = blocks.filter((block) => block.type === \"image\").length;\n return chars / CHARS_PER_TOKEN + images * TOKENS_PER_IMAGE;\n}\n\nfunction promptEstimate(context: ModelContext): number {\n const tools = context.tools.reduce(\n (sum, tool) => sum + tool.name.length + tool.description.length + JSON.stringify(tool.inputSchema).length,\n 0,\n );\n return (context.system.length + tools) / CHARS_PER_TOKEN;\n}\n\n/**\n * One request's usage: a retried call's `usage` sums every attempt, so its last attempt's usage alone gives the size\n * of the request that produced the message.\n */\nfunction requestUsage(message: AssistantMessage): Usage {\n return message.lastAttemptUsage ?? message.usage;\n}\n\n/** The index of the last assistant message whose request reports any input, or -1 when there is none. */\nfunction lastReportedCall(messages: readonly Message[]): number {\n return messages.findLastIndex((m) => {\n if (m.role !== \"assistant\") return false;\n const usage = requestUsage(m);\n // Unreported counts may miss the output, so the estimate falls back to characters, which errs high.\n return !usage.unreported && totalInput(usage) > 0;\n });\n}\n\n/**\n * Estimates the input tokens of sending `context`. When an earlier assistant message reports its usage, the estimate\n * is that request's total input and output plus a character-based estimate of the messages after it; otherwise every\n * part of the context is estimated from its characters. A retried call counts only its last attempt, through\n * `lastAttemptUsage`, because its summed `usage` would count the same prompt once per attempt. The reported usage\n * stands in for the system prompt and tools, so the estimate assumes they are unchanged since that request: if you\n * grow them, the growth is not counted. Otherwise the estimate errs high, so a budget check errs towards stopping.\n */\nexport function estimateInputTokens(context: ModelContext): number {\n const last = lastReportedCall(context.messages);\n const previous = context.messages[last];\n if (previous?.role !== \"assistant\") {\n const messages = context.messages.reduce((sum, m) => sum + messageEstimate(m), 0);\n return Math.ceil(promptEstimate(context) + messages);\n }\n // The reported output stands in for the assistant message's own content, which the next call sends as input.\n const added = context.messages.slice(last + 1).reduce((sum, m) => sum + messageEstimate(m), 0);\n const usage = requestUsage(previous);\n return Math.ceil(totalInput(usage) + usage.output + added);\n}\n\n/**\n * The most the next call can cost, priced as `projectCost()` prices it: over `estimateInputTokens(context)` and the\n * call's `maxOutputTokens`, or the model's own output limit when the options leave it out. Priced with the model's catalog\n * entry, so a model the catalog lacks projects `{ total: null, basis: \"unknown\" }`.\n */\nexport function projectCallCost(model: Model, context: ModelContext, options: ModelCallOptions = {}): Cost {\n const tokens = {\n inputTokens: estimateInputTokens(context),\n maxOutputTokens: options.maxOutputTokens ?? model.caps.maxOutputTokens,\n };\n return projectWith(tokens, (usage) => model.price(usage));\n}\n"]}
|
|
1
|
+
{"version":3,"file":"cost.js","sourceRoot":"","sources":["../src/cost.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAEpD,MAAM,WAAW,GAAG,SAAS,CAAC;AAE9B,4FAA4F;AAC5F,MAAM,YAAY,GAAS,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,S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CAAC,eAAe,IAAI,KAAK,CAAC,IAAI,CAAC,eAAe;KACvE,CAAC;IACF,OAAO,WAAW,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC;AAC5D,CAAC","sourcesContent":["/**\n * Pricing usage against the catalog, and projecting a call's cost before it is sent (D3, D4, D9; ADR 0043).\n *\n * Separate token classes (D3) mean the fold multiplies each class by its own rate with no conversion. A provider's\n * billed total wins over the catalog, and a model the catalog lacks, or a response whose usage the provider didn't\n * report, has an unknown price, never a guessed zero.\n */\nimport type { CatalogEntry, RateTier } from \"./catalog.js\";\nimport type { Model, ModelCallOptions } from \"./runtime-types.js\";\nimport type { AssistantMessage, ContentBlock, Cost, Message, ModelContext, Usage } from \"./types.js\";\nimport { emptyUsage, totalInput } from \"./usage.js\";\n\nconst PER_MILLION = 1_000_000;\n\n/** The cost of a model the catalog doesn't list, or of usage the provider didn't report. */\nconst UNKNOWN_COST: Cost = { total: null, basis: \"unknown\", currency: \"USD\" };\n\n/** The first tier whose limit covers `inputTokens`; the catalog check guarantees the last tier has no limit. */\nfunction tierFor(rates: readonly RateTier[], inputTokens: number): RateTier | undefined {\n return rates.find((tier) => tier.upToInputTokens === null || inputTokens <= tier.upToInputTokens);\n}\n\n/**\n * Prices one response's usage with a catalog entry. Each token class is multiplied by its rate in the tier that the\n * response's total input selects. Without an entry, or for usage marked `unreported`, the result is\n * `{ total: null, basis: \"unknown\" }`.\n */\nexport function priceUsage(usage: Usage, entry: CatalogEntry | undefined): Cost {\n // Zero counts the provider never reported are not a price of zero, and a budget must not treat them as one.\n if (usage.unreported) return { ...UNKNOWN_COST };\n const tier = entry ? tierFor(entry.rates, totalInput(usage)) : undefined;\n if (entry === undefined || tier === undefined) return { ...UNKNOWN_COST };\n const total =\n (usage.uncachedInput * tier.uncachedInput +\n usage.cacheRead * tier.cacheRead +\n usage.cacheWrite[\"5m\"] * tier.cacheWrite[\"5m\"] +\n usage.cacheWrite[\"1h\"] * tier.cacheWrite[\"1h\"] +\n usage.output * tier.output) /\n PER_MILLION;\n return { total, basis: \"catalog\", currency: \"USD\", catalogAsOf: entry.asOf };\n}\n\n/**\n * Adds two costs, such as the costs of two model calls. The sum is unknown if either cost is, a catalog estimate if\n * either is one, and a provider bill only when both are. A catalog sum keeps the later cost's `catalogAsOf`.\n */\nexport function addCost(a: Cost, b: Cost): Cost {\n if (a.total === null || b.total === null || a.basis === \"unknown\" || b.basis === \"unknown\") {\n return { ...UNKNOWN_COST };\n }\n const total = a.total + b.total;\n if (a.basis === \"provider\" && b.basis === \"provider\") return { total, basis: \"provider\", currency: \"USD\" };\n const catalogAsOf = b.catalogAsOf ?? a.catalogAsOf;\n return { total, basis: \"catalog\", currency: \"USD\", ...(catalogAsOf === undefined ? {} : { catalogAsOf }) };\n}\n\n/** The message's cost: the provider's billed total when the adapter reported one, else `price(usage)`. */\nexport function foldCost(message: AssistantMessage, price: (usage: Usage) => Cost): Cost {\n if (message.cost.basis === \"provider\" && message.cost.total !== null) return message.cost;\n return price(message.usage);\n}\n\n/** The token counts a projection prices: the input to send and the most output the call may produce. */\nexport interface ProjectedTokens {\n readonly inputTokens: number;\n readonly maxOutputTokens: number;\n}\n\n/**\n * The most one call can cost: every input token at the highest input rate in the tier the input selects (the\n * uncached rate or either cache-write rate, whichever costs most), plus `maxOutputTokens` at the output rate. Without\n * an entry, the result is `{ total: null, basis: \"unknown\" }`.\n */\nexport function projectCost(tokens: ProjectedTokens, entry: CatalogEntry | undefined): Cost {\n return projectWith(tokens, (usage) => priceUsage(usage, entry));\n}\n\n// Every input token is put in one class at a time and the dearest result wins. The tier depends only on total input,\n// which is the same in each candidate, so this prices the input at the tier's highest input rate without reading the\n// rates themselves; `Model.price()` keeps its catalog entry private.\nfunction projectWith(tokens: ProjectedTokens, price: (usage: Usage) => Cost): Cost {\n const base = { ...emptyUsage(), output: tokens.maxOutputTokens };\n const n = tokens.inputTokens;\n const candidates = [\n price({ ...base, uncachedInput: n }),\n price({ ...base, cacheWrite: { \"5m\": n, \"1h\": 0 } }),\n price({ ...base, cacheWrite: { \"5m\": 0, \"1h\": n } }),\n ];\n return candidates.reduce((most, cost) => ((cost.total ?? -1) > (most.total ?? -1) ? cost : most));\n}\n\n// Anthropic's models overview puts 1M tokens at roughly 2.5M Unicode characters on its current tokenizer, as of\n// November 2026. Other tokenizers fit more characters in a token, so this rate errs towards a higher estimate.\nconst CHARS_PER_TOKEN = 2.5;\n// The largest per-image cost a first provider documents: Anthropic's high-resolution limit of 4,784 visual tokens\n// (vision docs, as of November 2026). Counting an image's base64 data instead would overstate it a hundredfold.\nconst TOKENS_PER_IMAGE = 4784;\n\nfunction blockChars(block: ContentBlock): number {\n switch (block.type) {\n case \"text\":\n case \"reasoning\":\n return block.text.length;\n case \"tool_call\":\n return block.name.length + JSON.stringify(block.input ?? null).length;\n case \"image\":\n return 0;\n }\n}\n\nfunction messageEstimate(message: Message): number {\n const blocks: readonly ContentBlock[] = message.content;\n const chars = blocks.reduce((sum, block) => sum + blockChars(block), 0);\n const images = blocks.filter((block) => block.type === \"image\").length;\n return chars / CHARS_PER_TOKEN + images * TOKENS_PER_IMAGE;\n}\n\nfunction promptEstimate(context: ModelContext): number {\n const tools = context.tools.reduce(\n (sum, tool) => sum + tool.name.length + tool.description.length + JSON.stringify(tool.inputSchema).length,\n 0,\n );\n return (context.system.length + tools) / CHARS_PER_TOKEN;\n}\n\n/**\n * One request's usage: a retried call's `usage` sums every attempt, so its last attempt's usage alone gives the size\n * of the request that produced the message.\n */\nfunction requestUsage(message: AssistantMessage): Usage {\n return message.lastAttemptUsage ?? message.usage;\n}\n\n/** The index of the last assistant message whose request reports any input, or -1 when there is none. */\nfunction lastReportedCall(messages: readonly Message[]): number {\n return messages.findLastIndex((m) => {\n if (m.role !== \"assistant\") return false;\n const usage = requestUsage(m);\n // Unreported counts may miss the output, so the estimate falls back to characters, which errs high.\n return !usage.unreported && totalInput(usage) > 0;\n });\n}\n\n/**\n * Estimates the input tokens of sending `context`. When an earlier assistant message reports its usage, the estimate\n * is that request's total input and output plus a character-based estimate of the messages after it; otherwise every\n * part of the context is estimated from its characters. A retried call counts only its last attempt, through\n * `lastAttemptUsage`, because its summed `usage` would count the same prompt once per attempt. The reported usage\n * stands in for the system prompt and tools, so the estimate assumes they are unchanged since that request: if you\n * grow them, the growth is not counted. Otherwise the estimate errs high, so a budget check errs towards stopping.\n */\nexport function estimateInputTokens(context: ModelContext): number {\n const last = lastReportedCall(context.messages);\n const previous = context.messages[last];\n if (previous?.role !== \"assistant\") {\n const messages = context.messages.reduce((sum, m) => sum + messageEstimate(m), 0);\n return Math.ceil(promptEstimate(context) + messages);\n }\n // The reported output stands in for the assistant message's own content, which the next call sends as input.\n const added = context.messages.slice(last + 1).reduce((sum, m) => sum + messageEstimate(m), 0);\n const usage = requestUsage(previous);\n return Math.ceil(totalInput(usage) + usage.output + added);\n}\n\n/**\n * The most the next call can cost, priced as `projectCost()` prices it: over `estimateInputTokens(context)` and the\n * call's `maxOutputTokens`, or the model's own output limit when the options leave it out. Priced with the model's catalog\n * entry, so a model the catalog lacks projects `{ total: null, basis: \"unknown\" }`.\n */\nexport function projectCallCost(model: Model, context: ModelContext, options: ModelCallOptions = {}): Cost {\n const tokens = {\n inputTokens: estimateInputTokens(context),\n maxOutputTokens: options.maxOutputTokens ?? model.caps.maxOutputTokens,\n };\n return projectWith(tokens, (usage) => model.price(usage));\n}\n"]}
|
package/dist/errors.d.ts
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `GatewayError`: the gateway's error for programmer errors, such as a missing key, invalid options, a malformed
|
|
3
|
-
* catalog, a tool name outside the canonical rule, or a request a mock transport's script did not expect.
|
|
3
|
+
* catalog, a tool name outside the canonical rule, or a request a mock transport's script did not expect. Two
|
|
4
|
+
* subclasses keep their own names for the cases callers catch by class: `StreamContractError` and
|
|
5
|
+
* `ModelRequestsDisabledError`.
|
|
4
6
|
*/
|
|
5
7
|
/** Why a `GatewayError` was thrown. */
|
|
6
|
-
export type GatewayErrorCode = "missing_api_key" | "missing_caps" | "invalid_options" | "invalid_catalog" | "invalid_tool_name" | "unexpected_request";
|
|
8
|
+
export type GatewayErrorCode = "missing_api_key" | "missing_caps" | "invalid_options" | "invalid_catalog" | "invalid_tool_name" | "unexpected_request" | "stream_contract" | "model_requests_disabled";
|
|
7
9
|
/**
|
|
8
10
|
* Thrown for a mistake in how the gateway is called. A failed model call is never thrown: it ends its stream with an
|
|
9
|
-
* `error` event instead.
|
|
11
|
+
* `error` event instead. `stream_contract`: a stream broke the stream rule (`StreamContractError`).
|
|
12
|
+
* `model_requests_disabled`: a real request was attempted while `allowModelRequests(false)` is in force
|
|
13
|
+
* (`ModelRequestsDisabledError`).
|
|
10
14
|
*/
|
|
11
15
|
export declare class GatewayError extends Error {
|
|
12
|
-
readonly name
|
|
16
|
+
readonly name: string;
|
|
13
17
|
readonly code: GatewayErrorCode;
|
|
14
18
|
constructor(code: GatewayErrorCode, message: string, options?: ErrorOptions);
|
|
15
19
|
}
|
package/dist/errors.js
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `GatewayError`: the gateway's error for programmer errors, such as a missing key, invalid options, a malformed
|
|
3
|
-
* catalog, a tool name outside the canonical rule, or a request a mock transport's script did not expect.
|
|
3
|
+
* catalog, a tool name outside the canonical rule, or a request a mock transport's script did not expect. Two
|
|
4
|
+
* subclasses keep their own names for the cases callers catch by class: `StreamContractError` and
|
|
5
|
+
* `ModelRequestsDisabledError`.
|
|
4
6
|
*/
|
|
5
7
|
// A registered symbol, so the guard still works when two copies of the package are bundled.
|
|
6
8
|
const GATEWAY_ERROR = Symbol.for("buildanagent.gateway.GatewayError");
|
|
7
9
|
/**
|
|
8
10
|
* Thrown for a mistake in how the gateway is called. A failed model call is never thrown: it ends its stream with an
|
|
9
|
-
* `error` event instead.
|
|
11
|
+
* `error` event instead. `stream_contract`: a stream broke the stream rule (`StreamContractError`).
|
|
12
|
+
* `model_requests_disabled`: a real request was attempted while `allowModelRequests(false)` is in force
|
|
13
|
+
* (`ModelRequestsDisabledError`).
|
|
10
14
|
*/
|
|
11
15
|
export class GatewayError extends Error {
|
|
12
16
|
name = "GatewayError";
|
package/dist/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAaH,4FAA4F;AAC5F,MAAM,aAAa,GAAG,MAAM,CAAC,GAAG,CAAC,mCAAmC,CAAC,CAAC;AAEtE;;;;;GAKG;AACH,MAAM,OAAO,YAAa,SAAQ,KAAK;IACnB,IAAI,GAAW,cAAc,CAAC;IACvC,IAAI,CAAmB;IAEhC,YAAY,IAAsB,EAAE,OAAe,EAAE,OAAsB;QACzE,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,aAAa,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;IAC9D,CAAC;CACF;AAED,iFAAiF;AACjF,MAAM,UAAU,cAAc,CAAC,KAAc;IAC3C,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAK,KAAiC,CAAC,aAAa,CAAC,KAAK,IAAI,CAAC;AACnH,CAAC","sourcesContent":["/**\n * `GatewayError`: the gateway's error for programmer errors, such as a missing key, invalid options, a malformed\n * catalog, a tool name outside the canonical rule, or a request a mock transport's script did not expect. Two\n * subclasses keep their own names for the cases callers catch by class: `StreamContractError` and\n * `ModelRequestsDisabledError`.\n */\n\n/** Why a `GatewayError` was thrown. */\nexport type GatewayErrorCode =\n | \"missing_api_key\"\n | \"missing_caps\"\n | \"invalid_options\"\n | \"invalid_catalog\"\n | \"invalid_tool_name\"\n | \"unexpected_request\"\n | \"stream_contract\"\n | \"model_requests_disabled\";\n\n// A registered symbol, so the guard still works when two copies of the package are bundled.\nconst GATEWAY_ERROR = Symbol.for(\"buildanagent.gateway.GatewayError\");\n\n/**\n * Thrown for a mistake in how the gateway is called. A failed model call is never thrown: it ends its stream with an\n * `error` event instead. `stream_contract`: a stream broke the stream rule (`StreamContractError`).\n * `model_requests_disabled`: a real request was attempted while `allowModelRequests(false)` is in force\n * (`ModelRequestsDisabledError`).\n */\nexport class GatewayError extends Error {\n override readonly name: string = \"GatewayError\";\n readonly code: GatewayErrorCode;\n\n constructor(code: GatewayErrorCode, message: string, options?: ErrorOptions) {\n super(message, options);\n this.code = code;\n Object.defineProperty(this, GATEWAY_ERROR, { value: true });\n }\n}\n\n/** True for a `GatewayError`, including one from another copy of the package. */\nexport function isGatewayError(error: unknown): error is GatewayError {\n return typeof error === \"object\" && error !== null && (error as Record<symbol, unknown>)[GATEWAY_ERROR] === true;\n}\n"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
export { type CatalogEntry, type CatalogSources, defaultCatalog, findCatalogEntry, type PriceCatalog, parseCatalog, type RateTier, staleCatalogEntries, } from "./catalog.js";
|
|
6
6
|
export { classifyError, type ErrorKind, type FailedRequest } from "./classify.js";
|
|
7
7
|
export { complete } from "./complete.js";
|
|
8
|
-
export { estimateInputTokens, type ProjectedTokens, priceUsage, projectCallCost, projectCost } from "./cost.js";
|
|
8
|
+
export { addCost, estimateInputTokens, type ProjectedTokens, priceUsage, projectCallCost, projectCost, } from "./cost.js";
|
|
9
9
|
export { createModel } from "./create-model.js";
|
|
10
10
|
export { GatewayError, type GatewayErrorCode, isGatewayError } from "./errors.js";
|
|
11
11
|
export { assertCanonicalName, CANONICAL_TOOL_NAME, createToolNameMap, isCanonicalName, TOOL_NAME_RULES, type ToolNameMap, type ToolNameRule, } from "./names.js";
|
package/dist/index.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
export { defaultCatalog, findCatalogEntry, parseCatalog, staleCatalogEntries, } from "./catalog.js";
|
|
6
6
|
export { classifyError } from "./classify.js";
|
|
7
7
|
export { complete } from "./complete.js";
|
|
8
|
-
export { estimateInputTokens, priceUsage, projectCallCost, projectCost } from "./cost.js";
|
|
8
|
+
export { addCost, estimateInputTokens, priceUsage, projectCallCost, projectCost, } from "./cost.js";
|
|
9
9
|
export { createModel } from "./create-model.js";
|
|
10
10
|
export { GatewayError, isGatewayError } from "./errors.js";
|
|
11
11
|
export { assertCanonicalName, CANONICAL_TOOL_NAME, createToolNameMap, isCanonicalName, TOOL_NAME_RULES, } from "./names.js";
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAGL,cAAc,EACd,gBAAgB,EAEhB,YAAY,EAEZ,mBAAmB,GACpB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,aAAa,EAAsC,MAAM,eAAe,CAAC;AAClF,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAGL,cAAc,EACd,gBAAgB,EAEhB,YAAY,EAEZ,mBAAmB,GACpB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,aAAa,EAAsC,MAAM,eAAe,CAAC;AAClF,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EACL,OAAO,EACP,mBAAmB,EAEnB,UAAU,EACV,eAAe,EACf,WAAW,GACZ,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAyB,cAAc,EAAE,MAAM,aAAa,CAAC;AAClF,OAAO,EACL,mBAAmB,EACnB,mBAAmB,EACnB,iBAAiB,EACjB,eAAe,EACf,eAAe,GAGhB,MAAM,YAAY,CAAC;AACpB,OAAO,EAA+B,qBAAqB,EAAE,MAAM,sBAAsB,CAAC;AAC1F,OAAO,EACL,yBAAyB,EACzB,eAAe,EACf,0BAA0B,EAC1B,uBAAuB,GACxB,MAAM,qBAAqB,CAAC;AAe7B,OAAO,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AACjF,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAe5D,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC","sourcesContent":["/**\n * `@buildanagent/gateway`: the model gateway's transcript, model spec, stream rule, runtime, price catalog and\n * helpers.\n */\nexport {\n type CatalogEntry,\n type CatalogSources,\n defaultCatalog,\n findCatalogEntry,\n type PriceCatalog,\n parseCatalog,\n type RateTier,\n staleCatalogEntries,\n} from \"./catalog.js\";\nexport { classifyError, type ErrorKind, type FailedRequest } from \"./classify.js\";\nexport { complete } from \"./complete.js\";\nexport {\n addCost,\n estimateInputTokens,\n type ProjectedTokens,\n priceUsage,\n projectCallCost,\n projectCost,\n} from \"./cost.js\";\nexport { createModel } from \"./create-model.js\";\nexport { GatewayError, type GatewayErrorCode, isGatewayError } from \"./errors.js\";\nexport {\n assertCanonicalName,\n CANONICAL_TOOL_NAME,\n createToolNameMap,\n isCanonicalName,\n TOOL_NAME_RULES,\n type ToolNameMap,\n type ToolNameRule,\n} from \"./names.js\";\nexport { type ResolvedProviderConfig, resolveProviderConfig } from \"./provider-config.js\";\nexport {\n assertModelRequestAllowed,\n isMockTransport,\n ModelRequestsDisabledError,\n modelRequestsAreAllowed,\n} from \"./request-policy.js\";\nexport type {\n CreateModelOptions,\n DumpedOptions,\n GatewayEvent,\n MiddlewareParams,\n Model,\n ModelCallOptions,\n ModelMiddleware,\n ProviderConfig,\n RequestDump,\n Sleep,\n Transport,\n} from \"./runtime-types.js\";\nexport type { ModelAdapter, ModelEvent, StreamOptions } from \"./spec.js\";\nexport { assertStreamContract, StreamContractError } from \"./stream-contract.js\";\nexport { transformMessages } from \"./transform-messages.js\";\nexport type {\n AssistantMessage,\n ContentBlock,\n Cost,\n Message,\n ModelCaps,\n ModelContent,\n ModelContext,\n ModelErrorCode,\n ModelRef,\n OpaqueToken,\n Usage,\n WireTool,\n} from \"./types.js\";\nexport { addUsage, emptyUsage, totalInput, unreportedUsage } from \"./usage.js\";\n"]}
|
package/dist/request-policy.d.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* Every adapter calls `assertModelRequestAllowed(transport)` before it sends a request. When tests have turned real
|
|
5
5
|
* requests off, any transport not marked as a mock throws before anything leaves the process.
|
|
6
6
|
*/
|
|
7
|
+
import { GatewayError } from "./errors.js";
|
|
7
8
|
import type { Transport } from "./runtime-types.js";
|
|
8
9
|
/** Turns real model requests on or off for the whole process. Tests turn them off in the shared setup. */
|
|
9
10
|
export declare function setModelRequestsAllowed(allowed: boolean): void;
|
|
@@ -11,8 +12,13 @@ export declare function modelRequestsAreAllowed(): boolean;
|
|
|
11
12
|
/** Marks a transport as an in-process mock, which `allowModelRequests(false)` still lets through. */
|
|
12
13
|
export declare function markMockTransport<T extends Transport>(transport: T): T;
|
|
13
14
|
export declare function isMockTransport(transport: Transport | undefined): boolean;
|
|
14
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Thrown before a request is sent while real model requests are off and the transport is not a mock. It is a
|
|
17
|
+
* `GatewayError` with code `model_requests_disabled`, so `isGatewayError()` recognises it too.
|
|
18
|
+
*/
|
|
19
|
+
export declare class ModelRequestsDisabledError extends GatewayError {
|
|
15
20
|
readonly name = "ModelRequestsDisabledError";
|
|
21
|
+
constructor(message: string);
|
|
16
22
|
}
|
|
17
23
|
/** Throws unless real requests are allowed or the transport is a mock. Adapters call it before sending. */
|
|
18
24
|
export declare function assertModelRequestAllowed(transport: Transport | undefined): void;
|
package/dist/request-policy.js
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The process-wide switch behind `allowModelRequests()` (SDK decision 14; SDK plan §5.4).
|
|
3
|
+
*
|
|
4
|
+
* Every adapter calls `assertModelRequestAllowed(transport)` before it sends a request. When tests have turned real
|
|
5
|
+
* requests off, any transport not marked as a mock throws before anything leaves the process.
|
|
6
|
+
*/
|
|
7
|
+
import { GatewayError } from "./errors.js";
|
|
1
8
|
const MOCK_TRANSPORT = Symbol.for("buildanagent.gateway.mockTransport");
|
|
2
9
|
let modelRequestsAllowed = true;
|
|
3
10
|
/** Turns real model requests on or off for the whole process. Tests turn them off in the shared setup. */
|
|
@@ -15,8 +22,15 @@ export function markMockTransport(transport) {
|
|
|
15
22
|
export function isMockTransport(transport) {
|
|
16
23
|
return transport !== undefined && transport[MOCK_TRANSPORT] === true;
|
|
17
24
|
}
|
|
18
|
-
|
|
25
|
+
/**
|
|
26
|
+
* Thrown before a request is sent while real model requests are off and the transport is not a mock. It is a
|
|
27
|
+
* `GatewayError` with code `model_requests_disabled`, so `isGatewayError()` recognises it too.
|
|
28
|
+
*/
|
|
29
|
+
export class ModelRequestsDisabledError extends GatewayError {
|
|
19
30
|
name = "ModelRequestsDisabledError";
|
|
31
|
+
constructor(message) {
|
|
32
|
+
super("model_requests_disabled", message);
|
|
33
|
+
}
|
|
20
34
|
}
|
|
21
35
|
/** Throws unless real requests are allowed or the transport is a mock. Adapters call it before sending. */
|
|
22
36
|
export function assertModelRequestAllowed(transport) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request-policy.js","sourceRoot":"","sources":["../src/request-policy.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"request-policy.js","sourceRoot":"","sources":["../src/request-policy.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAG3C,MAAM,cAAc,GAAG,MAAM,CAAC,GAAG,CAAC,oCAAoC,CAAC,CAAC;AAExE,IAAI,oBAAoB,GAAG,IAAI,CAAC;AAEhC,0GAA0G;AAC1G,MAAM,UAAU,uBAAuB,CAAC,OAAgB;IACtD,oBAAoB,GAAG,OAAO,CAAC;AACjC,CAAC;AAED,MAAM,UAAU,uBAAuB;IACrC,OAAO,oBAAoB,CAAC;AAC9B,CAAC;AAED,qGAAqG;AACrG,MAAM,UAAU,iBAAiB,CAAsB,SAAY;IACjE,MAAM,CAAC,cAAc,CAAC,SAAS,EAAE,cAAc,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;IAClE,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,MAAM,UAAU,eAAe,CAAC,SAAgC;IAC9D,OAAO,SAAS,KAAK,SAAS,IAAK,SAAgD,CAAC,cAAc,CAAC,KAAK,IAAI,CAAC;AAC/G,CAAC;AAED;;;GAGG;AACH,MAAM,OAAO,0BAA2B,SAAQ,YAAY;IACxC,IAAI,GAAG,4BAA4B,CAAC;IAEtD,YAAY,OAAe;QACzB,KAAK,CAAC,yBAAyB,EAAE,OAAO,CAAC,CAAC;IAC5C,CAAC;CACF;AAED,2GAA2G;AAC3G,MAAM,UAAU,yBAAyB,CAAC,SAAgC;IACxE,IAAI,oBAAoB,IAAI,eAAe,CAAC,SAAS,CAAC;QAAE,OAAO;IAC/D,MAAM,IAAI,0BAA0B,CAClC,0GAA0G,CAC3G,CAAC;AACJ,CAAC","sourcesContent":["/**\n * The process-wide switch behind `allowModelRequests()` (SDK decision 14; SDK plan §5.4).\n *\n * Every adapter calls `assertModelRequestAllowed(transport)` before it sends a request. When tests have turned real\n * requests off, any transport not marked as a mock throws before anything leaves the process.\n */\nimport { GatewayError } from \"./errors.js\";\nimport type { Transport } from \"./runtime-types.js\";\n\nconst MOCK_TRANSPORT = Symbol.for(\"buildanagent.gateway.mockTransport\");\n\nlet modelRequestsAllowed = true;\n\n/** Turns real model requests on or off for the whole process. Tests turn them off in the shared setup. */\nexport function setModelRequestsAllowed(allowed: boolean): void {\n modelRequestsAllowed = allowed;\n}\n\nexport function modelRequestsAreAllowed(): boolean {\n return modelRequestsAllowed;\n}\n\n/** Marks a transport as an in-process mock, which `allowModelRequests(false)` still lets through. */\nexport function markMockTransport<T extends Transport>(transport: T): T {\n Object.defineProperty(transport, MOCK_TRANSPORT, { value: true });\n return transport;\n}\n\nexport function isMockTransport(transport: Transport | undefined): boolean {\n return transport !== undefined && (transport as unknown as Record<symbol, unknown>)[MOCK_TRANSPORT] === true;\n}\n\n/**\n * Thrown before a request is sent while real model requests are off and the transport is not a mock. It is a\n * `GatewayError` with code `model_requests_disabled`, so `isGatewayError()` recognises it too.\n */\nexport class ModelRequestsDisabledError extends GatewayError {\n override readonly name = \"ModelRequestsDisabledError\";\n\n constructor(message: string) {\n super(\"model_requests_disabled\", message);\n }\n}\n\n/** Throws unless real requests are allowed or the transport is a mock. Adapters call it before sending. */\nexport function assertModelRequestAllowed(transport: Transport | undefined): void {\n if (modelRequestsAllowed || isMockTransport(transport)) return;\n throw new ModelRequestsDisabledError(\n \"Model requests are disabled (allowModelRequests(false)); use a mock transport or ScriptedModel in tests.\",\n );\n}\n"]}
|
package/dist/retry.js
CHANGED
|
@@ -17,26 +17,14 @@
|
|
|
17
17
|
* (https://developers.openai.com/api/docs/guides/rate-limits, as of November 2026).
|
|
18
18
|
*/
|
|
19
19
|
import { classifyError, isTransportFailure } from "./classify.js";
|
|
20
|
-
import { foldCost } from "./cost.js";
|
|
20
|
+
import { addCost, foldCost } from "./cost.js";
|
|
21
21
|
import { addUsage, emptyUsage, unreportedUsage } from "./usage.js";
|
|
22
22
|
const BACKOFF_FACTOR = 2;
|
|
23
23
|
const MAX_RETRY_DELAY_MS = 60_000;
|
|
24
24
|
const UNKNOWN_COST = { total: null, basis: "unknown", currency: "USD" };
|
|
25
|
-
/**
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
*/
|
|
29
|
-
function addCost(a, b) {
|
|
30
|
-
if (a === undefined)
|
|
31
|
-
return b;
|
|
32
|
-
if (a.total === null || b.total === null || a.basis === "unknown" || b.basis === "unknown") {
|
|
33
|
-
return { ...UNKNOWN_COST };
|
|
34
|
-
}
|
|
35
|
-
const total = a.total + b.total;
|
|
36
|
-
if (a.basis === "provider" && b.basis === "provider")
|
|
37
|
-
return { total, basis: "provider", currency: "USD" };
|
|
38
|
-
const catalogAsOf = b.catalogAsOf ?? a.catalogAsOf;
|
|
39
|
-
return { total, basis: "catalog", currency: "USD", ...(catalogAsOf === undefined ? {} : { catalogAsOf }) };
|
|
25
|
+
/** Adds a carried cost to an attempt's; `undefined` means no attempt is carried yet. */
|
|
26
|
+
function addCarriedCost(a, b) {
|
|
27
|
+
return a === undefined ? b : addCost(a, b);
|
|
40
28
|
}
|
|
41
29
|
/** The default `Sleep`: a timer that resolves early, without rejecting, when the signal aborts. */
|
|
42
30
|
export const abortableSleep = (ms, signal) => new Promise((resolve) => {
|
|
@@ -69,7 +57,11 @@ function isBlockEvent(event) {
|
|
|
69
57
|
}
|
|
70
58
|
/** The final message: this attempt's usage and `cost`, with the failed attempts' usage and cost added. */
|
|
71
59
|
function withCarried(message, call, cost) {
|
|
72
|
-
const summed = {
|
|
60
|
+
const summed = {
|
|
61
|
+
...message,
|
|
62
|
+
usage: addUsage(call.carried, message.usage),
|
|
63
|
+
cost: addCarriedCost(call.carriedCost, cost),
|
|
64
|
+
};
|
|
73
65
|
// The projection needs one request's size, which a sum over several attempts overstates.
|
|
74
66
|
return call.carriedCost === undefined ? summed : { ...summed, lastAttemptUsage: message.usage };
|
|
75
67
|
}
|
|
@@ -190,7 +182,7 @@ export async function* withRetries(attempt, ref, signal, settings) {
|
|
|
190
182
|
if (!outcome.retry)
|
|
191
183
|
return;
|
|
192
184
|
call.carried = addUsage(call.carried, outcome.failure.usage);
|
|
193
|
-
const carriedCost =
|
|
185
|
+
const carriedCost = addCarriedCost(call.carriedCost, outcome.cost);
|
|
194
186
|
call.carriedCost = carriedCost;
|
|
195
187
|
// The stream rule puts `start` first, so a failure that arrived without one still gets it before the retry.
|
|
196
188
|
if (!call.started) {
|
package/dist/retry.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retry.js","sourceRoot":"","sources":["../src/retry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAClE,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAIrC,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAWnE,MAAM,cAAc,GAAG,CAAC,CAAC;AACzB,MAAM,kBAAkB,GAAG,MAAM,CAAC;AAClC,MAAM,YAAY,GAAS,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC;AAE9E;;;GAGG;AACH,SAAS,OAAO,CAAC,CAAmB,EAAE,CAAO;IAC3C,IAAI,CAAC,KAAK,SAAS;QAAE,OAAO,CAAC,CAAC;IAC9B,IAAI,CAAC,CAAC,KAAK,KAAK,IAAI,IAAI,CAAC,CAAC,KAAK,KAAK,IAAI,IAAI,CAAC,CAAC,KAAK,KAAK,SAAS,IAAI,CAAC,CAAC,KAAK,KAAK,SAAS,EAAE,CAAC;QAC3F,OAAO,EAAE,GAAG,YAAY,EAAE,CAAC;IAC7B,CAAC;IACD,MAAM,KAAK,GAAG,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC;IAChC,IAAI,CAAC,CAAC,KAAK,KAAK,UAAU,IAAI,CAAC,CAAC,KAAK,KAAK,UAAU;QAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,KAAK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n *\n * - Only an `error` terminal of kind `retryable` that arrives before any content is retried. Once a block event has\n * been streamed, a retry would replay output the caller has already seen, so the failure is final.\n * - The backoff is exponential (2 s, then 4 s by default, as Vercel's AI SDK) and ends early when the signal aborts.\n * - The caller sees one `start`, then one `retry` event per retry, then the last attempt's events. The final message\n * counts the usage and cost of every attempt, because the provider bills failed attempts too. Each attempt is\n * priced on its own before the sum, because a tiered rate depends on that request's input alone (D4).\n * - A stream that throws is treated like one that ends with an `error` event, so the caller always gets a terminal.\n * - A warning an earlier attempt already streamed, with the same code and message, is not streamed again, because a\n * retry sends the same request and an adapter warns about it each time.\n * - A failure's `error.retryAfterMs`, which an adapter reads from the provider's retry-after header, sets the least\n * wait: the gateway waits for the longer of the backoff and the hint. Each wait is capped at 60 s.\n * - A hint longer than that cap ends the call with the failure, hint kept, and no `retry` event. OpenAI's rate-limit\n * guide says to stop rather than retry sooner when a server delay exceeds the client's maximum\n * (https://developers.openai.com/api/docs/guides/rate-limits, as of November 2026).\n */\n\nimport { classifyError, isTransportFailure } from \"./classify.js\";\nimport { foldCost } from \"./cost.js\";\nimport type { GatewayEvent, Sleep } from \"./runtime-types.js\";\nimport type { ModelEvent } from \"./spec.js\";\nimport type { AssistantMessage, Cost, ModelRef, Usage } from \"./types.js\";\nimport { addUsage, emptyUsage, unreportedUsage } from \"./usage.js\";\n\n/** How many times to retry, how long to wait, and how. */\nexport interface RetrySettings {\n readonly retries: number;\n readonly initialDelayMs: number;\n readonly sleep: Sleep;\n /** Prices one attempt's usage; a provider-billed total skips it. */\n readonly price: (usage: Usage) => Cost;\n}\n\nconst BACKOFF_FACTOR = 2;\nconst MAX_RETRY_DELAY_MS = 60_000;\nconst UNKNOWN_COST: Cost = { total: null, basis: \"unknown\", currency: \"USD\" };\n\n/**\n * Adds a carried cost to an attempt's. The sum is unknown if either part is, a catalog estimate if either part is one,\n * and a provider bill only when both parts are. `undefined` means no attempt is carried yet.\n */\nfunction addCost(a: Cost | undefined, b: Cost): Cost {\n if (a === undefined) return b;\n if (a.total === null || b.total === null || a.basis === \"unknown\" || b.basis === \"unknown\") {\n return { ...UNKNOWN_COST };\n }\n const total = a.total + b.total;\n if (a.basis === \"provider\" && b.basis === \"provider\") return { total, basis: \"provider\", currency: \"USD\" };\n const catalogAsOf = b.catalogAsOf ?? a.catalogAsOf;\n return { total, basis: \"catalog\", currency: \"USD\", ...(catalogAsOf === undefined ? {} : { catalogAsOf }) };\n}\n\n/** The default `Sleep`: a timer that resolves early, without rejecting, when the signal aborts. */\nexport const abortableSleep: Sleep = (ms, signal) =>\n new Promise((resolve) => {\n if (signal?.aborted) {\n resolve();\n return;\n }\n const onAbort = (): void => {\n clearTimeout(timer);\n resolve();\n };\n const timer = setTimeout(() => {\n signal?.removeEventListener(\"abort\", onAbort);\n resolve();\n }, ms);\n signal?.addEventListener(\"abort\", onAbort, { once: true });\n });\n\n/**\n * How one attempt ended: it ran to its end, or it failed before any output and may be retried. A retryable failure\n * carries its own cost, already priced.\n */\ntype AttemptOutcome =\n | { readonly retry: false }\n | { readonly retry: true; readonly failure: AssistantMessage; readonly cost: Cost };\n\n/** One call's state across its attempts. */\ninterface CallState {\n readonly ref: ModelRef;\n readonly signal: AbortSignal | undefined;\n readonly price: (usage: Usage) => Cost;\n started: boolean;\n /** The usage of the failed attempts so far; the final message adds it, because the provider billed them too. */\n carried: Usage;\n /** The cost of the failed attempts so far, added the same way; `undefined` until an attempt has failed. */\n carriedCost: Cost | undefined;\n /** The code and message of each warning the earlier attempts streamed. */\n readonly warned: Set<string>;\n}\n\n/** One attempt's progress: whether content has reached the caller, and the warnings it streamed. */\ninterface AttemptProgress {\n seen: boolean;\n readonly warned: Set<string>;\n}\n\n/** True for a warning an earlier attempt streamed; any other warning is recorded as this attempt's. */\nfunction isRepeatedWarning(event: ModelEvent, call: CallState, output: AttemptProgress): boolean {\n if (event.type !== \"warning\") return false;\n const key = JSON.stringify([event.code, event.message]);\n if (call.warned.has(key)) return true;\n output.warned.add(key);\n return false;\n}\n\nfunction isBlockEvent(event: ModelEvent): boolean {\n return event.type !== \"start\" && event.type !== \"warning\" && event.type !== \"done\" && event.type !== \"error\";\n}\n\n/** The final message: this attempt's usage and `cost`, with the failed attempts' usage and cost added. */\nfunction withCarried(message: AssistantMessage, call: CallState, cost: Cost): AssistantMessage {\n const summed = { ...message, usage: addUsage(call.carried, message.usage), cost: addCost(call.carriedCost, cost) };\n // The projection needs one request's size, which a sum over several attempts overstates.\n return call.carriedCost === undefined ? summed : { ...summed, lastAttemptUsage: message.usage };\n}\n\n/** A terminal event with the failed attempts added; any other event unchanged. The adapter's usage is priced here. */\nfunction withCarriedUsage(event: GatewayEvent, call: CallState): GatewayEvent {\n if (event.type !== \"done\" && event.type !== \"error\") return event;\n return { ...event, message: withCarried(event.message, call, foldCost(event.message, call.price)) };\n}\n\n/**\n * The message for an adapter whose stream threw instead of ending with an `error` event. Nothing reported its usage,\n * so the usage is `unreported` and the cost unknown, not zero: the request may have reached the provider and been\n * billed.\n */\nfunction thrownFailure(thrown: unknown, call: CallState): AssistantMessage {\n const base = {\n role: \"assistant\" as const,\n content: [],\n model: call.ref,\n usage: unreportedUsage(),\n cost: { ...UNKNOWN_COST },\n };\n if (call.signal?.aborted) return { ...base, stopReason: \"aborted\" };\n const name = thrown instanceof Error ? thrown.name : \"Error\";\n const message = `${name}: ${thrown instanceof Error ? thrown.message : String(thrown)}`;\n // Only a failed fetch is a transport failure worth retrying; any other throw is a bug or a guard, so it is final.\n const kind = isTransportFailure(thrown) ? classifyError({ message }) : \"fatal\";\n return { ...base, stopReason: \"error\", error: { kind, message } };\n}\n\n/** Forwards an attempt's events until it ends or fails retryably before any output. */\nasync function* forwardEvents(\n events: AsyncIterable<ModelEvent>,\n call: CallState,\n mayRetry: boolean,\n output: AttemptProgress,\n): AsyncGenerator<GatewayEvent, AttemptOutcome> {\n for await (const event of events) {\n if (event.type === \"start\") {\n if (call.started) continue;\n call.started = true;\n }\n if (isRepeatedWarning(event, call, output)) continue;\n if (event.type === \"error\" && mayRetry && !output.seen && event.message.error?.kind === \"retryable\") {\n return { retry: true, failure: event.message, cost: foldCost(event.message, call.price) };\n }\n if (isBlockEvent(event)) output.seen = true;\n yield withCarriedUsage(event, call);\n }\n return { retry: false };\n}\n\n/** Runs one attempt, keeping only the first attempt's `start`, and holds back a retryable failure. */\nasync function* runAttempt(\n events: AsyncIterable<ModelEvent>,\n call: CallState,\n mayRetry: boolean,\n): AsyncGenerator<GatewayEvent, AttemptOutcome> {\n const output: AttemptProgress = { seen: false, warned: new Set() };\n let failure: AssistantMessage;\n try {\n return yield* forwardEvents(events, call, mayRetry, output);\n } catch (thrown) {\n // A thrown stream is a failure like any other: it ends in an `error` terminal and may be retried.\n failure = thrownFailure(thrown, call);\n } finally {\n for (const key of output.warned) call.warned.add(key);\n }\n if (mayRetry && !output.seen && failure.error?.kind === \"retryable\") {\n return { retry: true, failure, cost: failure.cost };\n }\n if (!call.started) {\n call.started = true;\n yield { type: \"start\" };\n }\n yield { type: \"error\", message: withCarried(failure, call, failure.cost) };\n return { retry: false };\n}\n\n/**\n * The terminal for a call aborted between attempts. It keeps every failed attempt's usage and drops the error.\n * `call.carried` already includes `failure`, so `lastAttemptUsage` is set only when `retried` says earlier attempts\n * were carried too, as `withCarried` does.\n */\nfunction abortedTerminal(failure: AssistantMessage, call: CallState, cost: Cost, retried: boolean): GatewayEvent {\n const { error: _error, ...rest } = failure;\n const message = { ...rest, stopReason: \"aborted\" as const, usage: call.carried, cost };\n return { type: \"error\", message: retried ? { ...message, lastAttemptUsage: failure.usage } : message };\n}\n\n/**\n * The terminal for a retryable failure whose hint asks for longer than the cap. Like `abortedTerminal`, it keeps every\n * failed attempt's usage, but it keeps the error too, `retryAfterMs` included, so the caller can wait and call again.\n */\nfunction deferredTerminal(failure: AssistantMessage, call: CallState, cost: Cost, retried: boolean): GatewayEvent {\n const message = { ...failure, usage: call.carried, cost };\n return { type: \"error\", message: retried ? { ...message, lastAttemptUsage: failure.usage } : message };\n}\n\n/** A NaN hint from a hand-written adapter is no hint. */\nfunction hintOf(failure: AssistantMessage): number {\n const hintMs = failure.error?.retryAfterMs;\n return hintMs === undefined || Number.isNaN(hintMs) ? 0 : hintMs;\n}\n\n/**\n * The wait before retry `n`: the exponential backoff, or the provider's hint when it asks for longer. The cap keeps a\n * long retry count from waiting hours, or overflowing setTimeout's 32-bit delay; a hint over the cap never gets here.\n */\nfunction retryDelay(initialDelayMs: number, n: number, hintMs: number): number {\n return Math.min(Math.max(initialDelayMs * BACKOFF_FACTOR ** (n - 1), hintMs), MAX_RETRY_DELAY_MS);\n}\n\n/** Runs `attempt(1)`, then retries per `settings`. `attempt(n)` must start a fresh request each time it is called. */\nexport async function* withRetries(\n attempt: (n: number) => AsyncIterable<ModelEvent>,\n ref: ModelRef,\n signal: AbortSignal | undefined,\n settings: RetrySettings,\n): AsyncGenerator<GatewayEvent> {\n const call: CallState = {\n ref,\n signal,\n price: settings.price,\n started: false,\n carried: emptyUsage(),\n carriedCost: undefined,\n warned: new Set(),\n };\n for (let n = 1; ; n += 1) {\n const outcome = yield* runAttempt(attempt(n), call, n <= settings.retries);\n if (!outcome.retry) return;\n call.carried = addUsage(call.carried, outcome.failure.usage);\n const carriedCost = addCost(call.carriedCost, outcome.cost);\n call.carriedCost = carriedCost;\n // The stream rule puts `start` first, so a failure that arrived without one still gets it before the retry.\n if (!call.started) {\n call.started = true;\n yield { type: \"start\" };\n }\n if (signal?.aborted) {\n yield abortedTerminal(outcome.failure, call, carriedCost, n > 1);\n return;\n }\n const hintMs = hintOf(outcome.failure);\n // Waiting less than the provider asked would only fail again, so a hint over the cap ends the call.\n if (hintMs > MAX_RETRY_DELAY_MS) {\n yield deferredTerminal(outcome.failure, call, carriedCost, n > 1);\n return;\n }\n const delayMs = retryDelay(settings.initialDelayMs, n, hintMs);\n yield { type: \"retry\", attempt: n, delayMs, error: outcome.failure.error?.message ?? \"retryable error\" };\n await settings.sleep(delayMs, signal);\n if (signal?.aborted) {\n yield abortedT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AAO,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,IAAI,iBAAiB,EAAE,CAAC;QACzG,MAAM,QAAQ,CAAC,KAAK,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QACtC,IAAI,MAAM,EAAE,OAAO,EAAE,CAAC;YACpB,MAAM,eAAe,CAAC,OAAO,CAAC,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;YACjE,OAAO;QACT,CAAC;IACH,CAAC;AACH,CAAC","sourcesContent":["/**\n * HTTP retries before any output (SDK decision 4; agent-SDK guide §2).\n *\n * - Only an `error` terminal of kind `retryable` that arrives before any content is retried. Once a block event has\n * been streamed, a retry would replay output the caller has already seen, so the failure is final.\n * - The backoff is exponential (2 s, then 4 s by default, as Vercel's AI SDK) and ends early when the signal aborts.\n * - The caller sees one `start`, then one `retry` event per retry, then the last attempt's events. The final message\n * counts the usage and cost of every attempt, because the provider bills failed attempts too. Each attempt is\n * priced on its own before the sum, because a tiered rate depends on that request's input alone (D4).\n * - A stream that throws is treated like one that ends with an `error` event, so the caller always gets a terminal.\n * - A warning an earlier attempt already streamed, with the same code and message, is not streamed again, because a\n * retry sends the same request and an adapter warns about it each time.\n * - A failure's `error.retryAfterMs`, which an adapter reads from the provider's retry-after header, sets the least\n * wait: the gateway waits for the longer of the backoff and the hint. Each wait is capped at 60 s.\n * - A hint longer than that cap ends the call with the failure, hint kept, and no `retry` event. OpenAI's rate-limit\n * guide says to stop rather than retry sooner when a server delay exceeds the client's maximum\n * (https://developers.openai.com/api/docs/guides/rate-limits, as of November 2026).\n */\n\nimport { classifyError, isTransportFailure } from \"./classify.js\";\nimport { addCost, foldCost } from \"./cost.js\";\nimport type { GatewayEvent, Sleep } from \"./runtime-types.js\";\nimport type { ModelEvent } from \"./spec.js\";\nimport type { AssistantMessage, Cost, ModelRef, Usage } from \"./types.js\";\nimport { addUsage, emptyUsage, unreportedUsage } from \"./usage.js\";\n\n/** How many times to retry, how long to wait, and how. */\nexport interface RetrySettings {\n readonly retries: number;\n readonly initialDelayMs: number;\n readonly sleep: Sleep;\n /** Prices one attempt's usage; a provider-billed total skips it. */\n readonly price: (usage: Usage) => Cost;\n}\n\nconst BACKOFF_FACTOR = 2;\nconst MAX_RETRY_DELAY_MS = 60_000;\nconst UNKNOWN_COST: Cost = { total: null, basis: \"unknown\", currency: \"USD\" };\n\n/** Adds a carried cost to an attempt's; `undefined` means no attempt is carried yet. */\nfunction addCarriedCost(a: Cost | undefined, b: Cost): Cost {\n return a === undefined ? b : addCost(a, b);\n}\n\n/** The default `Sleep`: a timer that resolves early, without rejecting, when the signal aborts. */\nexport const abortableSleep: Sleep = (ms, signal) =>\n new Promise((resolve) => {\n if (signal?.aborted) {\n resolve();\n return;\n }\n const onAbort = (): void => {\n clearTimeout(timer);\n resolve();\n };\n const timer = setTimeout(() => {\n signal?.removeEventListener(\"abort\", onAbort);\n resolve();\n }, ms);\n signal?.addEventListener(\"abort\", onAbort, { once: true });\n });\n\n/**\n * How one attempt ended: it ran to its end, or it failed before any output and may be retried. A retryable failure\n * carries its own cost, already priced.\n */\ntype AttemptOutcome =\n | { readonly retry: false }\n | { readonly retry: true; readonly failure: AssistantMessage; readonly cost: Cost };\n\n/** One call's state across its attempts. */\ninterface CallState {\n readonly ref: ModelRef;\n readonly signal: AbortSignal | undefined;\n readonly price: (usage: Usage) => Cost;\n started: boolean;\n /** The usage of the failed attempts so far; the final message adds it, because the provider billed them too. */\n carried: Usage;\n /** The cost of the failed attempts so far, added the same way; `undefined` until an attempt has failed. */\n carriedCost: Cost | undefined;\n /** The code and message of each warning the earlier attempts streamed. */\n readonly warned: Set<string>;\n}\n\n/** One attempt's progress: whether content has reached the caller, and the warnings it streamed. */\ninterface AttemptProgress {\n seen: boolean;\n readonly warned: Set<string>;\n}\n\n/** True for a warning an earlier attempt streamed; any other warning is recorded as this attempt's. */\nfunction isRepeatedWarning(event: ModelEvent, call: CallState, output: AttemptProgress): boolean {\n if (event.type !== \"warning\") return false;\n const key = JSON.stringify([event.code, event.message]);\n if (call.warned.has(key)) return true;\n output.warned.add(key);\n return false;\n}\n\nfunction isBlockEvent(event: ModelEvent): boolean {\n return event.type !== \"start\" && event.type !== \"warning\" && event.type !== \"done\" && event.type !== \"error\";\n}\n\n/** The final message: this attempt's usage and `cost`, with the failed attempts' usage and cost added. */\nfunction withCarried(message: AssistantMessage, call: CallState, cost: Cost): AssistantMessage {\n const summed = {\n ...message,\n usage: addUsage(call.carried, message.usage),\n cost: addCarriedCost(call.carriedCost, cost),\n };\n // The projection needs one request's size, which a sum over several attempts overstates.\n return call.carriedCost === undefined ? summed : { ...summed, lastAttemptUsage: message.usage };\n}\n\n/** A terminal event with the failed attempts added; any other event unchanged. The adapter's usage is priced here. */\nfunction withCarriedUsage(event: GatewayEvent, call: CallState): GatewayEvent {\n if (event.type !== \"done\" && event.type !== \"error\") return event;\n return { ...event, message: withCarried(event.message, call, foldCost(event.message, call.price)) };\n}\n\n/**\n * The message for an adapter whose stream threw instead of ending with an `error` event. Nothing reported its usage,\n * so the usage is `unreported` and the cost unknown, not zero: the request may have reached the provider and been\n * billed.\n */\nfunction thrownFailure(thrown: unknown, call: CallState): AssistantMessage {\n const base = {\n role: \"assistant\" as const,\n content: [],\n model: call.ref,\n usage: unreportedUsage(),\n cost: { ...UNKNOWN_COST },\n };\n if (call.signal?.aborted) return { ...base, stopReason: \"aborted\" };\n const name = thrown instanceof Error ? thrown.name : \"Error\";\n const message = `${name}: ${thrown instanceof Error ? thrown.message : String(thrown)}`;\n // Only a failed fetch is a transport failure worth retrying; any other throw is a bug or a guard, so it is final.\n const kind = isTransportFailure(thrown) ? classifyError({ message }) : \"fatal\";\n return { ...base, stopReason: \"error\", error: { kind, message } };\n}\n\n/** Forwards an attempt's events until it ends or fails retryably before any output. */\nasync function* forwardEvents(\n events: AsyncIterable<ModelEvent>,\n call: CallState,\n mayRetry: boolean,\n output: AttemptProgress,\n): AsyncGenerator<GatewayEvent, AttemptOutcome> {\n for await (const event of events) {\n if (event.type === \"start\") {\n if (call.started) continue;\n call.started = true;\n }\n if (isRepeatedWarning(event, call, output)) continue;\n if (event.type === \"error\" && mayRetry && !output.seen && event.message.error?.kind === \"retryable\") {\n return { retry: true, failure: event.message, cost: foldCost(event.message, call.price) };\n }\n if (isBlockEvent(event)) output.seen = true;\n yield withCarriedUsage(event, call);\n }\n return { retry: false };\n}\n\n/** Runs one attempt, keeping only the first attempt's `start`, and holds back a retryable failure. */\nasync function* runAttempt(\n events: AsyncIterable<ModelEvent>,\n call: CallState,\n mayRetry: boolean,\n): AsyncGenerator<GatewayEvent, AttemptOutcome> {\n const output: AttemptProgress = { seen: false, warned: new Set() };\n let failure: AssistantMessage;\n try {\n return yield* forwardEvents(events, call, mayRetry, output);\n } catch (thrown) {\n // A thrown stream is a failure like any other: it ends in an `error` terminal and may be retried.\n failure = thrownFailure(thrown, call);\n } finally {\n for (const key of output.warned) call.warned.add(key);\n }\n if (mayRetry && !output.seen && failure.error?.kind === \"retryable\") {\n return { retry: true, failure, cost: failure.cost };\n }\n if (!call.started) {\n call.started = true;\n yield { type: \"start\" };\n }\n yield { type: \"error\", message: withCarried(failure, call, failure.cost) };\n return { retry: false };\n}\n\n/**\n * The terminal for a call aborted between attempts. It keeps every failed attempt's usage and drops the error.\n * `call.carried` already includes `failure`, so `lastAttemptUsage` is set only when `retried` says earlier attempts\n * were carried too, as `withCarried` does.\n */\nfunction abortedTerminal(failure: AssistantMessage, call: CallState, cost: Cost, retried: boolean): GatewayEvent {\n const { error: _error, ...rest } = failure;\n const message = { ...rest, stopReason: \"aborted\" as const, usage: call.carried, cost };\n return { type: \"error\", message: retried ? { ...message, lastAttemptUsage: failure.usage } : message };\n}\n\n/**\n * The terminal for a retryable failure whose hint asks for longer than the cap. Like `abortedTerminal`, it keeps every\n * failed attempt's usage, but it keeps the error too, `retryAfterMs` included, so the caller can wait and call again.\n */\nfunction deferredTerminal(failure: AssistantMessage, call: CallState, cost: Cost, retried: boolean): GatewayEvent {\n const message = { ...failure, usage: call.carried, cost };\n return { type: \"error\", message: retried ? { ...message, lastAttemptUsage: failure.usage } : message };\n}\n\n/** A NaN hint from a hand-written adapter is no hint. */\nfunction hintOf(failure: AssistantMessage): number {\n const hintMs = failure.error?.retryAfterMs;\n return hintMs === undefined || Number.isNaN(hintMs) ? 0 : hintMs;\n}\n\n/**\n * The wait before retry `n`: the exponential backoff, or the provider's hint when it asks for longer. The cap keeps a\n * long retry count from waiting hours, or overflowing setTimeout's 32-bit delay; a hint over the cap never gets here.\n */\nfunction retryDelay(initialDelayMs: number, n: number, hintMs: number): number {\n return Math.min(Math.max(initialDelayMs * BACKOFF_FACTOR ** (n - 1), hintMs), MAX_RETRY_DELAY_MS);\n}\n\n/** Runs `attempt(1)`, then retries per `settings`. `attempt(n)` must start a fresh request each time it is called. */\nexport async function* withRetries(\n attempt: (n: number) => AsyncIterable<ModelEvent>,\n ref: ModelRef,\n signal: AbortSignal | undefined,\n settings: RetrySettings,\n): AsyncGenerator<GatewayEvent> {\n const call: CallState = {\n ref,\n signal,\n price: settings.price,\n started: false,\n carried: emptyUsage(),\n carriedCost: undefined,\n warned: new Set(),\n };\n for (let n = 1; ; n += 1) {\n const outcome = yield* runAttempt(attempt(n), call, n <= settings.retries);\n if (!outcome.retry) return;\n call.carried = addUsage(call.carried, outcome.failure.usage);\n const carriedCost = addCarriedCost(call.carriedCost, outcome.cost);\n call.carriedCost = carriedCost;\n // The stream rule puts `start` first, so a failure that arrived without one still gets it before the retry.\n if (!call.started) {\n call.started = true;\n yield { type: \"start\" };\n }\n if (signal?.aborted) {\n yield abortedTerminal(outcome.failure, call, carriedCost, n > 1);\n return;\n }\n const hintMs = hintOf(outcome.failure);\n // Waiting less than the provider asked would only fail again, so a hint over the cap ends the call.\n if (hintMs > MAX_RETRY_DELAY_MS) {\n yield deferredTerminal(outcome.failure, call, carriedCost, n > 1);\n return;\n }\n const delayMs = retryDelay(settings.initialDelayMs, n, hintMs);\n yield { type: \"retry\", attempt: n, delayMs, error: outcome.failure.error?.message ?? \"retryable error\" };\n await settings.sleep(delayMs, signal);\n if (signal?.aborted) {\n yield abortedTerminal(outcome.failure, call, carriedCost, n > 1);\n return;\n }\n }\n}\n"]}
|
|
@@ -9,8 +9,13 @@
|
|
|
9
9
|
* 5. A gateway `retry` event comes after `start` and before any block event, because output is never retried.
|
|
10
10
|
* 6. A `warning` may come anywhere between `start` and the terminal event. It is not output, so a retry may follow it.
|
|
11
11
|
*/
|
|
12
|
+
import { GatewayError } from "./errors.js";
|
|
12
13
|
import type { GatewayEvent } from "./runtime-types.js";
|
|
13
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Thrown by `assertStreamContract()` at the first event that breaks the stream rule. It is a `GatewayError` with code
|
|
16
|
+
* `stream_contract`, so `isGatewayError()` recognises it too.
|
|
17
|
+
*/
|
|
18
|
+
export declare class StreamContractError extends GatewayError {
|
|
14
19
|
readonly name = "StreamContractError";
|
|
15
20
|
/** Position of the offending event in the stream, or the stream length when the stream ended early. */
|
|
16
21
|
readonly position: number;
|
package/dist/stream-contract.js
CHANGED
|
@@ -1,9 +1,25 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* `assertStreamContract()`: checks the stream rule every adapter and test model must follow (SDK plan §3.2).
|
|
3
|
+
*
|
|
4
|
+
* 1. `start` comes first, once.
|
|
5
|
+
* 2. Each content index goes `*_start` → `*_delta`* → `*_end`, with one kind (text, reasoning or tool input) per index.
|
|
6
|
+
* 3. Exactly one terminal event, `done` or `error`, ends the stream, and nothing follows it.
|
|
7
|
+
* 4. An `error` terminal's message has `stopReason` `"error"` or `"aborted"`; a `done` terminal's has neither, and
|
|
8
|
+
* every block it started has ended.
|
|
9
|
+
* 5. A gateway `retry` event comes after `start` and before any block event, because output is never retried.
|
|
10
|
+
* 6. A `warning` may come anywhere between `start` and the terminal event. It is not output, so a retry may follow it.
|
|
11
|
+
*/
|
|
12
|
+
import { GatewayError } from "./errors.js";
|
|
13
|
+
/**
|
|
14
|
+
* Thrown by `assertStreamContract()` at the first event that breaks the stream rule. It is a `GatewayError` with code
|
|
15
|
+
* `stream_contract`, so `isGatewayError()` recognises it too.
|
|
16
|
+
*/
|
|
17
|
+
export class StreamContractError extends GatewayError {
|
|
2
18
|
name = "StreamContractError";
|
|
3
19
|
/** Position of the offending event in the stream, or the stream length when the stream ended early. */
|
|
4
20
|
position;
|
|
5
21
|
constructor(message, position) {
|
|
6
|
-
super(message);
|
|
22
|
+
super("stream_contract", message);
|
|
7
23
|
this.position = position;
|
|
8
24
|
}
|
|
9
25
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream-contract.js","sourceRoot":"","sources":["../src/stream-contract.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"stream-contract.js","sourceRoot":"","sources":["../src/stream-contract.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAI3C;;;GAGG;AACH,MAAM,OAAO,mBAAoB,SAAQ,YAAY;IACjC,IAAI,GAAG,qBAAqB,CAAC;IAC/C,uGAAuG;IAC9F,QAAQ,CAAS;IAE1B,YAAY,OAAe,EAAE,QAAgB;QAC3C,KAAK,CAAC,iBAAiB,EAAE,OAAO,CAAC,CAAC;QAClC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;CACF;AAKD,SAAS,UAAU,CAAC,IAA0B;IAC5C,MAAM,KAAK,GAAG,iDAAiD,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC3E,IAAI,CAAC,KAAK;QAAE,OAAO,SAAS,CAAC;IAC7B,OAAO,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC,CAAc,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAe,EAAE,CAAC;AACxE,CAAC;AAED,+EAA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AC;IACvG,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC","sourcesContent":["/**\n * `assertStreamContract()`: checks the stream rule every adapter and test model must follow (SDK plan §3.2).\n *\n * 1. `start` comes first, once.\n * 2. Each content index goes `*_start` → `*_delta`* → `*_end`, with one kind (text, reasoning or tool input) per index.\n * 3. Exactly one terminal event, `done` or `error`, ends the stream, and nothing follows it.\n * 4. An `error` terminal's message has `stopReason` `\"error\"` or `\"aborted\"`; a `done` terminal's has neither, and\n * every block it started has ended.\n * 5. A gateway `retry` event comes after `start` and before any block event, because output is never retried.\n * 6. A `warning` may come anywhere between `start` and the terminal event. It is not output, so a retry may follow it.\n */\nimport { GatewayError } from \"./errors.js\";\nimport type { GatewayEvent } from \"./runtime-types.js\";\nimport type { ModelEvent } from \"./spec.js\";\n\n/**\n * Thrown by `assertStreamContract()` at the first event that breaks the stream rule. It is a `GatewayError` with code\n * `stream_contract`, so `isGatewayError()` recognises it too.\n */\nexport class StreamContractError extends GatewayError {\n override readonly name = \"StreamContractError\";\n /** Position of the offending event in the stream, or the stream length when the stream ended early. */\n readonly position: number;\n\n constructor(message: string, position: number) {\n super(\"stream_contract\", message);\n this.position = position;\n }\n}\n\ntype BlockKind = \"text\" | \"reasoning\" | \"tool_input\";\ntype BlockPhase = \"start\" | \"delta\" | \"end\";\n\nfunction blockPhase(type: GatewayEvent[\"type\"]): { kind: BlockKind; phase: BlockPhase } | undefined {\n const match = /^(text|reasoning|tool_input)_(start|delta|end)$/.exec(type);\n if (!match) return undefined;\n return { kind: match[1] as BlockKind, phase: match[2] as BlockPhase };\n}\n\n/** Tracks one stream's state and checks each event against the stream rule. */\nclass StreamChecker {\n readonly seen: GatewayEvent[] = [];\n readonly #open = new Map<number, BlockKind>();\n readonly #closed = new Set<number>();\n #terminal = false;\n #output = false;\n\n get ended(): boolean {\n return this.#terminal;\n }\n\n check(event: GatewayEvent): void {\n const position = this.seen.length;\n this.seen.push(event);\n const fail = (message: string): never => {\n throw new StreamContractError(`${message} (event ${position}: ${event.type})`, position);\n };\n this.#checkOrder(event, position, fail);\n if (event.type === \"done\" || event.type === \"error\") {\n this.#checkTerminal(event, fail);\n return;\n }\n if (event.type === \"retry\" && this.#output) fail(\"a retry arrived after output was streamed\");\n const block = blockPhase(event.type);\n if (!block) return;\n this.#output = true;\n this.#checkBlock((event as { index: number }).index, block.kind, block.phase, fail);\n }\n\n #checkOrder(event: GatewayEvent, position: number, fail: (message: string) => never): void {\n if (this.#terminal) fail(\"an event followed the terminal event\");\n if (position === 0 && event.type !== \"start\") fail(\"the stream must begin with start\");\n if (position > 0 && event.type === \"start\") fail(\"start appeared twice\");\n }\n\n #checkTerminal(event: Extract<ModelEvent, { type: \"done\" | \"error\" }>, fail: (message: string) => never): void {\n this.#terminal = true;\n const { stopReason } = event.message;\n const failed = stopReason === \"error\" || stopReason === \"aborted\";\n if (event.type === \"error\" && !failed)\n fail(`an error terminal needs stopReason error or aborted, got ${stopReason}`);\n if (event.type === \"done\" && failed) fail(`a done terminal cannot have stopReason ${stopReason}`);\n if (event.type === \"done\" && this.#open.size > 0) {\n fail(`done arrived with open blocks at index ${[...this.#open.keys()].join(\", \")}`);\n }\n }\n\n #checkBlock(index: number, kind: BlockKind, phase: BlockPhase, fail: (message: string) => never): void {\n if (phase === \"start\") {\n if (this.#open.has(index) || this.#closed.has(index)) fail(`index ${index} started twice`);\n this.#open.set(index, kind);\n return;\n }\n if (this.#closed.has(index)) fail(`a ${kind}_${phase} arrived after index ${index} ended`);\n const openKind = this.#open.get(index);\n if (openKind === undefined) fail(`a ${kind}_${phase} arrived before index ${index} started`);\n if (openKind !== kind) fail(`index ${index} started as ${openKind} but got ${kind}_${phase}`);\n if (phase === \"end\") {\n this.#open.delete(index);\n this.#closed.add(index);\n }\n }\n}\n\n/**\n * Consumes the stream, throws `StreamContractError` at the first violation, and returns every event it read. It\n * checks an adapter's stream and a `Model.stream()` with its `retry` events alike.\n */\nexport async function assertStreamContract<E extends GatewayEvent>(events: AsyncIterable<E>): Promise<E[]> {\n const checker = new StreamChecker();\n const seen: E[] = [];\n for await (const event of events) {\n checker.check(event);\n seen.push(event);\n }\n if (!checker.ended) {\n throw new StreamContractError(\"the stream ended without a done or error event\", checker.seen.length);\n }\n return seen;\n}\n"]}
|
package/package.json
CHANGED