@buildanagent/gateway 0.1.0-alpha.0 → 0.1.0-alpha.1
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 +11 -5
- package/dist/adapters/anthropic/failure.d.ts +8 -1
- package/dist/adapters/anthropic/failure.js +10 -3
- package/dist/adapters/anthropic/failure.js.map +1 -1
- package/dist/adapters/anthropic/response.d.ts +7 -0
- package/dist/adapters/anthropic/response.js +27 -2
- package/dist/adapters/anthropic/response.js.map +1 -1
- package/dist/adapters/openai-chat/failure.d.ts +8 -1
- package/dist/adapters/openai-chat/failure.js +10 -3
- package/dist/adapters/openai-chat/failure.js.map +1 -1
- package/dist/adapters/openai-chat/usage.d.ts +4 -1
- package/dist/adapters/openai-chat/usage.js +6 -3
- package/dist/adapters/openai-chat/usage.js.map +1 -1
- package/dist/adapters/wire.d.ts +22 -1
- package/dist/adapters/wire.js +54 -4
- package/dist/adapters/wire.js.map +1 -1
- package/dist/cost.d.ts +4 -2
- package/dist/cost.js +13 -3
- package/dist/cost.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/retry.d.ts +5 -2
- package/dist/retry.js +37 -9
- package/dist/retry.js.map +1 -1
- package/dist/types.d.ts +13 -0
- package/dist/types.js.map +1 -1
- package/dist/usage.d.ts +9 -1
- package/dist/usage.js +12 -1
- package/dist/usage.js.map +1 -1
- package/package.json +1 -1
package/dist/adapters/wire.js
CHANGED
|
@@ -2,12 +2,16 @@
|
|
|
2
2
|
* What every wire adapter shares: sending one HTTP request through the caller's transport, reading a server-sent
|
|
3
3
|
* event stream to its terminal, and turning a failed request, a broken stream or an abort into an `error` terminal.
|
|
4
4
|
* A failed model call never throws; only a guard's error or a `GatewayError` does.
|
|
5
|
+
*
|
|
6
|
+
* Usage: an attempt that ends without a usage report has `unreported` usage, so `createModel()` prices it as unknown.
|
|
7
|
+
* The one exception is an HTTP error response, which counts as zero tokens: the provider answered with an error
|
|
8
|
+
* instead of a reply, and its body reports no usage to read.
|
|
5
9
|
*/
|
|
6
10
|
import { classifyError, isTransportFailure } from "../classify.js";
|
|
7
11
|
import { isGatewayError } from "../errors.js";
|
|
8
12
|
import { assertModelRequestAllowed, ModelRequestsDisabledError } from "../request-policy.js";
|
|
9
13
|
import { readServerSentEvents } from "../sse.js";
|
|
10
|
-
import { emptyUsage } from "../usage.js";
|
|
14
|
+
import { emptyUsage, unreportedUsage } from "../usage.js";
|
|
11
15
|
/** True for a JSON object, not an array or `null`. */
|
|
12
16
|
export function isObject(value) {
|
|
13
17
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
@@ -31,7 +35,10 @@ export function parseToolInput(text, empty = {}) {
|
|
|
31
35
|
return text;
|
|
32
36
|
}
|
|
33
37
|
}
|
|
34
|
-
/**
|
|
38
|
+
/**
|
|
39
|
+
* An assistant message with an unknown cost, which `createModel()` prices from the catalog. Without `partial.usage`,
|
|
40
|
+
* the provider reported no usage, so the message's usage is `unreported`.
|
|
41
|
+
*/
|
|
35
42
|
export function terminalMessage(model, partial, stopReason, error) {
|
|
36
43
|
return {
|
|
37
44
|
role: "assistant",
|
|
@@ -39,7 +46,7 @@ export function terminalMessage(model, partial, stopReason, error) {
|
|
|
39
46
|
model,
|
|
40
47
|
stopReason,
|
|
41
48
|
...(error ? { error } : {}),
|
|
42
|
-
usage: partial.usage ??
|
|
49
|
+
usage: partial.usage ?? unreportedUsage(),
|
|
43
50
|
cost: { total: null, basis: "unknown", currency: "USD" },
|
|
44
51
|
};
|
|
45
52
|
}
|
|
@@ -50,6 +57,7 @@ export function abortedEvent(model, partial = {}) {
|
|
|
50
57
|
/**
|
|
51
58
|
* The `error` terminal for a failure. A failure with no HTTP status, such as an error event inside a 200 stream,
|
|
52
59
|
* classifies like the status its type stands for in `statusOfType`. The message starts with the error code or type.
|
|
60
|
+
* A failure with a status is an HTTP error response, so without `partial.usage` it reports zero tokens.
|
|
53
61
|
*/
|
|
54
62
|
export function failureTerminal(model, failure, statusOfType, partial = {}, code) {
|
|
55
63
|
const status = failure.status ?? (failure.type ? statusOfType[failure.type] : undefined);
|
|
@@ -67,14 +75,56 @@ export function failureTerminal(model, failure, statusOfType, partial = {}, code
|
|
|
67
75
|
message: label ? `${label}: ${failure.message}` : failure.message,
|
|
68
76
|
...(failure.status === undefined ? {} : { status: failure.status }),
|
|
69
77
|
...(code ? { code } : {}),
|
|
78
|
+
...(failure.retryAfterMs === undefined ? {} : { retryAfterMs: failure.retryAfterMs }),
|
|
70
79
|
};
|
|
71
|
-
|
|
80
|
+
const reply = failure.status === undefined || partial.usage ? partial : { ...partial, usage: emptyUsage() };
|
|
81
|
+
return { type: "error", message: terminalMessage(model, reply, "error", error) };
|
|
72
82
|
}
|
|
73
83
|
/** The `error` terminal for a throw: retryable only for a transport failure, and `fatal` for a bug or bad data. */
|
|
74
84
|
export function thrownTerminal(model, thrown, partial = {}) {
|
|
75
85
|
const message = thrown instanceof Error ? `${thrown.name}: ${thrown.message}` : String(thrown);
|
|
76
86
|
return failureTerminal(model, { message, ...(isTransportFailure(thrown) ? {} : { kind: "fatal" }) }, {}, partial);
|
|
77
87
|
}
|
|
88
|
+
// A delay in seconds, as `retry-after` sends it, or in milliseconds, as `retry-after-ms` does: a non-negative decimal.
|
|
89
|
+
const DELAY = /^\d+(\.\d+)?$/;
|
|
90
|
+
// The three HTTP-date forms of RFC 9110, section 5.6.7: IMF-fixdate, the obsolete RFC 850 form, and asctime, which
|
|
91
|
+
// has no zone and means GMT. `Date.parse` alone would also take text such as "-5" or "5, 10" as a date.
|
|
92
|
+
const 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$/;
|
|
93
|
+
const ASCTIME_DATE = /^[A-Z][a-z]{2} [A-Z][a-z]{2} [ \d]\d \d{2}:\d{2}:\d{2} \d{4}$/;
|
|
94
|
+
/** The time an HTTP-date names, in milliseconds since the epoch, or `NaN` for any other text. */
|
|
95
|
+
function httpDate(value) {
|
|
96
|
+
if (value === null)
|
|
97
|
+
return Number.NaN;
|
|
98
|
+
const text = value.trim();
|
|
99
|
+
if (GMT_DATE.test(text))
|
|
100
|
+
return Date.parse(text);
|
|
101
|
+
return ASCTIME_DATE.test(text) ? Date.parse(`${text} GMT`) : Number.NaN;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* How long a failed response asks the caller to wait before retrying, in milliseconds, or `undefined` when it gives
|
|
105
|
+
* no valid delay. `retry-after-ms` wins over `retry-after`, as in the official TypeScript SDKs (openai-node v7.49.0
|
|
106
|
+
* and anthropic-sdk-typescript v0.129.0, `src/client.ts`, as of November 2026). `retry-after` holds seconds, as
|
|
107
|
+
* Anthropic's and OpenAI's rate-limit pages say (https://platform.claude.com/docs/en/api/rate-limits and
|
|
108
|
+
* https://developers.openai.com/api/docs/guides/rate-limits, as of November 2026), or an HTTP date (RFC 9110,
|
|
109
|
+
* section 10.2.3). A date counts from the response's own `date` header, so the delay depends on no local clock, and
|
|
110
|
+
* a response without one gives no delay. Any other value, such as a negative number or the comma-joined values of a
|
|
111
|
+
* repeated header, gives no delay.
|
|
112
|
+
*/
|
|
113
|
+
export function retryAfterMs(headers) {
|
|
114
|
+
const millis = headers.get("retry-after-ms")?.trim();
|
|
115
|
+
if (millis !== undefined && DELAY.test(millis))
|
|
116
|
+
return Number(millis);
|
|
117
|
+
const value = headers.get("retry-after")?.trim();
|
|
118
|
+
if (value === undefined || value === "")
|
|
119
|
+
return undefined;
|
|
120
|
+
if (DELAY.test(value))
|
|
121
|
+
return Number(value) * 1000;
|
|
122
|
+
const until = httpDate(value);
|
|
123
|
+
const sent = httpDate(headers.get("date"));
|
|
124
|
+
if (Number.isNaN(until) || Number.isNaN(sent))
|
|
125
|
+
return undefined;
|
|
126
|
+
return Math.max(0, until - sent);
|
|
127
|
+
}
|
|
78
128
|
/** A failed response's body, parsed as JSON when it is JSON, and its text. */
|
|
79
129
|
export async function readErrorBody(response) {
|
|
80
130
|
const text = await response.text().catch(() => "");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wire.js","sourceRoot":"","sources":["../../src/adapters/wire.ts"],"names":[],"mappings":"AAAA;;;;GAIG;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,MAAM,aAAa,CAAC;AAuBzC,sDAAsD;AACtD,MAAM,UAAU,QAAQ,CAAC,KAAc;IACrC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC;AAED,8CAA8C;AAC9C,MAAM,UAAU,GAAG,CAAC,KAAc;IAChC,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;AAChD,CAAC;AAED,qDAAqD;AACrD,MAAM,UAAU,KAAK,CAAC,KAAc;IAClC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;AACtF,CAAC;AAED,uGAAuG;AACvG,MAAM,UAAU,cAAc,CAAC,IAAY,EAAE,KAAK,GAAY,EAAE;IAC9D,IAAI,IAAI,KAAK,EAAE;QAAE,OAAO,KAAK,CAAC;IAC9B,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI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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 */\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 } 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}\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/** An assistant message with an unknown cost, which `createModel()` prices from the catalog. */\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 ?? emptyUsage(),\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 */\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 };\n return { type: \"error\", message: terminalMessage(model, partial, \"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 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,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,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC;AAED,8CAA8C;AAC9C,MAAM,UAAU,GAAG,CAAC,KAAc;IAChC,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;AAChD,CAAC;AAED,qDAAqD;AACrD,MAAM,UAAU,KAAK,CAAC,KAAc;IAClC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAA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E,CAAC;YACvC,MAAM,KAAK,CAAC;YACZ,IAAI,KAAK,CAAC,IAAI,KAAK,MAAM,IAAI,KAAK,CAAC,IAAI,KAAK,OAAO;gBAAE,OAAO,IAAI,CAAC;QACnE,CAAC;IACH,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED,0GAA0G;AAC1G,MAAM,CAAC,KAAK,SAAS,CAAC,CAAC,eAAe,CACpC,QAAkB,EAClB,KAAe,EACf,MAAoB,EACpB,MAA+B;IAE/B,IAAI,QAAQ,CAAC,IAAI,KAAK,IAAI,EAAE,CAAC;QAC3B,MAAM,eAAe,CAAC,KAAK,EAAE,EAAE,OAAO,EAAE,0BAA0B,EAAE,EAAE,EAAE,CAAC,CAAC;QAC1E,OAAO;IACT,CAAC;IACD,IAAI,CAAC;QACH,IAAI,KAAK,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;YAAE,OAAO;IACjD,CAAC;IAAC,OAAO,MAAM,EAAE,CAAC;QAChB,8GAA8G;QAC9G,MAAM,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,YAAY,CAAC,KAAK,EAAE,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,OAAO,EAAE,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, 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"]}
|
package/dist/cost.d.ts
CHANGED
|
@@ -2,14 +2,16 @@
|
|
|
2
2
|
* Pricing usage against the catalog, and projecting a call's cost before it is sent (D3, D4, D9; ADR 0043).
|
|
3
3
|
*
|
|
4
4
|
* Separate token classes (D3) mean the fold multiplies each class by its own rate with no conversion. A provider's
|
|
5
|
-
* billed total wins over the catalog, and a model the catalog lacks
|
|
5
|
+
* billed total wins over the catalog, and a model the catalog lacks, or a response whose usage the provider didn't
|
|
6
|
+
* report, has an unknown price, never a guessed zero.
|
|
6
7
|
*/
|
|
7
8
|
import type { CatalogEntry } from "./catalog.js";
|
|
8
9
|
import type { Model, ModelCallOptions } from "./runtime-types.js";
|
|
9
10
|
import type { AssistantMessage, Cost, ModelContext, Usage } from "./types.js";
|
|
10
11
|
/**
|
|
11
12
|
* Prices one response's usage with a catalog entry. Each token class is multiplied by its rate in the tier that the
|
|
12
|
-
* response's total input selects. Without an entry,
|
|
13
|
+
* response's total input selects. Without an entry, or for usage marked `unreported`, the result is
|
|
14
|
+
* `{ total: null, basis: "unknown" }`.
|
|
13
15
|
*/
|
|
14
16
|
export declare function priceUsage(usage: Usage, entry: CatalogEntry | undefined): Cost;
|
|
15
17
|
/** The message's cost: the provider's billed total when the adapter reported one, else `price(usage)`. */
|
package/dist/cost.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { emptyUsage, totalInput } from "./usage.js";
|
|
2
2
|
const PER_MILLION = 1_000_000;
|
|
3
|
-
/** The cost of a model the catalog doesn't list. */
|
|
3
|
+
/** The cost of a model the catalog doesn't list, or of usage the provider didn't report. */
|
|
4
4
|
const UNKNOWN_COST = { total: null, basis: "unknown", currency: "USD" };
|
|
5
5
|
/** The first tier whose limit covers `inputTokens`; the catalog check guarantees the last tier has no limit. */
|
|
6
6
|
function tierFor(rates, inputTokens) {
|
|
@@ -8,9 +8,13 @@ function tierFor(rates, inputTokens) {
|
|
|
8
8
|
}
|
|
9
9
|
/**
|
|
10
10
|
* Prices one response's usage with a catalog entry. Each token class is multiplied by its rate in the tier that the
|
|
11
|
-
* response's total input selects. Without an entry,
|
|
11
|
+
* response's total input selects. Without an entry, or for usage marked `unreported`, the result is
|
|
12
|
+
* `{ total: null, basis: "unknown" }`.
|
|
12
13
|
*/
|
|
13
14
|
export function priceUsage(usage, entry) {
|
|
15
|
+
// Zero counts the provider never reported are not a price of zero, and a budget must not treat them as one.
|
|
16
|
+
if (usage.unreported)
|
|
17
|
+
return { ...UNKNOWN_COST };
|
|
14
18
|
const tier = entry ? tierFor(entry.rates, totalInput(usage)) : undefined;
|
|
15
19
|
if (entry === undefined || tier === undefined)
|
|
16
20
|
return { ...UNKNOWN_COST };
|
|
@@ -85,7 +89,13 @@ function requestUsage(message) {
|
|
|
85
89
|
}
|
|
86
90
|
/** The index of the last assistant message whose request reports any input, or -1 when there is none. */
|
|
87
91
|
function lastReportedCall(messages) {
|
|
88
|
-
return messages.findLastIndex((m) =>
|
|
92
|
+
return messages.findLastIndex((m) => {
|
|
93
|
+
if (m.role !== "assistant")
|
|
94
|
+
return false;
|
|
95
|
+
const usage = requestUsage(m);
|
|
96
|
+
// Unreported counts may miss the output, so the estimate falls back to characters, which errs high.
|
|
97
|
+
return !usage.unreported && totalInput(usage) > 0;
|
|
98
|
+
});
|
|
89
99
|
}
|
|
90
100
|
/**
|
|
91
101
|
* Estimates the input tokens of sending `context`. When an earlier assistant message reports its usage, the estimate
|
package/dist/cost.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cost.js","sourceRoot":"","sources":["../src/cost.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAEpD,MAAM,WAAW,GAAG,SAAS,CAAC;AAE9B,oDAAoD;AACpD,MAAM,YAAY,GAAS,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC;AAE9E,gHAAgH;AAChH,SAAS,OAAO,CAAC,KAA0B,EAAE,WAAmB;IAC9D,OAAO,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,eAAe,KAAK,IAAI,IAAI,WAAW,IAAI,IAAI,CAAC,eAAe,CAAC,CAAC;AACpG,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,UAAU,CAAC,KAAY,EAAE,KAA+B;IACtE,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,EAAE,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IACzE,IAAI,KAAK,KAAK,SAAS,IAAI,IAAI,KAAK,SAAS;QAAE,OAAO,EAAE,GAAG,YAAY,EAAE,CAAC;IAC1E,MAAM,KAAK,GACT,CAAC,KAAK,CAAC,aAAa,GAAG,IAAI,CAAC,aAAa;QACvC,KAAK,CAAC,SAAS,GAAG,IAAI,CAAC,SAAS;QAChC,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC;QAC9C,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,UAA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CAAC,GAAG,QAAQ,CAAC,CAAC;IACvD,CAAC;IACD,6GAA6G;IAC7G,MAAM,KAAK,GAAG,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,MAAM,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 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. */\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, the result is `{ total: null, basis: \"unknown\" }`.\n */\nexport function priceUsage(usage: Usage, entry: CatalogEntry | undefined): 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) => m.role === \"assistant\" && totalInput(requestUsage(m)) > 0);\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,SAAS,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC;AAE9E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C,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"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -16,4 +16,4 @@ export type { ModelAdapter, ModelEvent, StreamOptions } from "./spec.js";
|
|
|
16
16
|
export { assertStreamContract, StreamContractError } from "./stream-contract.js";
|
|
17
17
|
export { transformMessages } from "./transform-messages.js";
|
|
18
18
|
export type { AssistantMessage, ContentBlock, Cost, Message, ModelCaps, ModelContent, ModelContext, ModelErrorCode, ModelRef, OpaqueToken, Usage, WireTool, } from "./types.js";
|
|
19
|
-
export { addUsage, emptyUsage, totalInput } from "./usage.js";
|
|
19
|
+
export { addUsage, emptyUsage, totalInput, unreportedUsage } from "./usage.js";
|
package/dist/index.js
CHANGED
|
@@ -13,5 +13,5 @@ export { resolveProviderConfig } from "./provider-config.js";
|
|
|
13
13
|
export { assertModelRequestAllowed, isMockTransport, ModelRequestsDisabledError, modelRequestsAreAllowed, } from "./request-policy.js";
|
|
14
14
|
export { assertStreamContract, StreamContractError } from "./stream-contract.js";
|
|
15
15
|
export { transformMessages } from "./transform-messages.js";
|
|
16
|
-
export { addUsage, emptyUsage, totalInput } from "./usage.js";
|
|
16
|
+
export { addUsage, emptyUsage, totalInput, unreportedUsage } from "./usage.js";
|
|
17
17
|
//# sourceMappingURL=index.js.map
|
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,EAAE,mBAAmB,EAAwB,UAAU,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAChH,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,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 { estimateInputTokens, type ProjectedTokens, priceUsage, projectCallCost, projectCost } 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 } from \"./usage.js\";\n"]}
|
|
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,EAAE,mBAAmB,EAAwB,UAAU,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAChH,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 { estimateInputTokens, type ProjectedTokens, priceUsage, projectCallCost, projectCost } 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/retry.d.ts
CHANGED
|
@@ -10,8 +10,11 @@
|
|
|
10
10
|
* - A stream that throws is treated like one that ends with an `error` event, so the caller always gets a terminal.
|
|
11
11
|
* - A warning an earlier attempt already streamed, with the same code and message, is not streamed again, because a
|
|
12
12
|
* retry sends the same request and an adapter warns about it each time.
|
|
13
|
-
* -
|
|
14
|
-
*
|
|
13
|
+
* - A failure's `error.retryAfterMs`, which an adapter reads from the provider's retry-after header, sets the least
|
|
14
|
+
* wait: the gateway waits for the longer of the backoff and the hint. Each wait is capped at 60 s.
|
|
15
|
+
* - A hint longer than that cap ends the call with the failure, hint kept, and no `retry` event. OpenAI's rate-limit
|
|
16
|
+
* guide says to stop rather than retry sooner when a server delay exceeds the client's maximum
|
|
17
|
+
* (https://developers.openai.com/api/docs/guides/rate-limits, as of November 2026).
|
|
15
18
|
*/
|
|
16
19
|
import type { GatewayEvent, Sleep } from "./runtime-types.js";
|
|
17
20
|
import type { ModelEvent } from "./spec.js";
|
package/dist/retry.js
CHANGED
|
@@ -10,12 +10,15 @@
|
|
|
10
10
|
* - A stream that throws is treated like one that ends with an `error` event, so the caller always gets a terminal.
|
|
11
11
|
* - A warning an earlier attempt already streamed, with the same code and message, is not streamed again, because a
|
|
12
12
|
* retry sends the same request and an adapter warns about it each time.
|
|
13
|
-
* -
|
|
14
|
-
*
|
|
13
|
+
* - A failure's `error.retryAfterMs`, which an adapter reads from the provider's retry-after header, sets the least
|
|
14
|
+
* wait: the gateway waits for the longer of the backoff and the hint. Each wait is capped at 60 s.
|
|
15
|
+
* - A hint longer than that cap ends the call with the failure, hint kept, and no `retry` event. OpenAI's rate-limit
|
|
16
|
+
* guide says to stop rather than retry sooner when a server delay exceeds the client's maximum
|
|
17
|
+
* (https://developers.openai.com/api/docs/guides/rate-limits, as of November 2026).
|
|
15
18
|
*/
|
|
16
19
|
import { classifyError, isTransportFailure } from "./classify.js";
|
|
17
20
|
import { foldCost } from "./cost.js";
|
|
18
|
-
import { addUsage, emptyUsage } from "./usage.js";
|
|
21
|
+
import { addUsage, emptyUsage, unreportedUsage } from "./usage.js";
|
|
19
22
|
const BACKOFF_FACTOR = 2;
|
|
20
23
|
const MAX_RETRY_DELAY_MS = 60_000;
|
|
21
24
|
const UNKNOWN_COST = { total: null, basis: "unknown", currency: "USD" };
|
|
@@ -77,15 +80,16 @@ function withCarriedUsage(event, call) {
|
|
|
77
80
|
return { ...event, message: withCarried(event.message, call, foldCost(event.message, call.price)) };
|
|
78
81
|
}
|
|
79
82
|
/**
|
|
80
|
-
* The message for an adapter whose stream threw instead of ending with an `error` event.
|
|
81
|
-
*
|
|
83
|
+
* The message for an adapter whose stream threw instead of ending with an `error` event. Nothing reported its usage,
|
|
84
|
+
* so the usage is `unreported` and the cost unknown, not zero: the request may have reached the provider and been
|
|
85
|
+
* billed.
|
|
82
86
|
*/
|
|
83
87
|
function thrownFailure(thrown, call) {
|
|
84
88
|
const base = {
|
|
85
89
|
role: "assistant",
|
|
86
90
|
content: [],
|
|
87
91
|
model: call.ref,
|
|
88
|
-
usage:
|
|
92
|
+
usage: unreportedUsage(),
|
|
89
93
|
cost: { ...UNKNOWN_COST },
|
|
90
94
|
};
|
|
91
95
|
if (call.signal?.aborted)
|
|
@@ -130,7 +134,6 @@ async function* runAttempt(events, call, mayRetry) {
|
|
|
130
134
|
for (const key of output.warned)
|
|
131
135
|
call.warned.add(key);
|
|
132
136
|
}
|
|
133
|
-
// The failure's unknown cost is final: pricing its zero usage would claim a known zero.
|
|
134
137
|
if (mayRetry && !output.seen && failure.error?.kind === "retryable") {
|
|
135
138
|
return { retry: true, failure, cost: failure.cost };
|
|
136
139
|
}
|
|
@@ -151,6 +154,26 @@ function abortedTerminal(failure, call, cost, retried) {
|
|
|
151
154
|
const message = { ...rest, stopReason: "aborted", usage: call.carried, cost };
|
|
152
155
|
return { type: "error", message: retried ? { ...message, lastAttemptUsage: failure.usage } : message };
|
|
153
156
|
}
|
|
157
|
+
/**
|
|
158
|
+
* The terminal for a retryable failure whose hint asks for longer than the cap. Like `abortedTerminal`, it keeps every
|
|
159
|
+
* failed attempt's usage, but it keeps the error too, `retryAfterMs` included, so the caller can wait and call again.
|
|
160
|
+
*/
|
|
161
|
+
function deferredTerminal(failure, call, cost, retried) {
|
|
162
|
+
const message = { ...failure, usage: call.carried, cost };
|
|
163
|
+
return { type: "error", message: retried ? { ...message, lastAttemptUsage: failure.usage } : message };
|
|
164
|
+
}
|
|
165
|
+
/** A NaN hint from a hand-written adapter is no hint. */
|
|
166
|
+
function hintOf(failure) {
|
|
167
|
+
const hintMs = failure.error?.retryAfterMs;
|
|
168
|
+
return hintMs === undefined || Number.isNaN(hintMs) ? 0 : hintMs;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* The wait before retry `n`: the exponential backoff, or the provider's hint when it asks for longer. The cap keeps a
|
|
172
|
+
* long retry count from waiting hours, or overflowing setTimeout's 32-bit delay; a hint over the cap never gets here.
|
|
173
|
+
*/
|
|
174
|
+
function retryDelay(initialDelayMs, n, hintMs) {
|
|
175
|
+
return Math.min(Math.max(initialDelayMs * BACKOFF_FACTOR ** (n - 1), hintMs), MAX_RETRY_DELAY_MS);
|
|
176
|
+
}
|
|
154
177
|
/** Runs `attempt(1)`, then retries per `settings`. `attempt(n)` must start a fresh request each time it is called. */
|
|
155
178
|
export async function* withRetries(attempt, ref, signal, settings) {
|
|
156
179
|
const call = {
|
|
@@ -178,8 +201,13 @@ export async function* withRetries(attempt, ref, signal, settings) {
|
|
|
178
201
|
yield abortedTerminal(outcome.failure, call, carriedCost, n > 1);
|
|
179
202
|
return;
|
|
180
203
|
}
|
|
181
|
-
|
|
182
|
-
|
|
204
|
+
const hintMs = hintOf(outcome.failure);
|
|
205
|
+
// Waiting less than the provider asked would only fail again, so a hint over the cap ends the call.
|
|
206
|
+
if (hintMs > MAX_RETRY_DELAY_MS) {
|
|
207
|
+
yield deferredTerminal(outcome.failure, call, carriedCost, n > 1);
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
const delayMs = retryDelay(settings.initialDelayMs, n, hintMs);
|
|
183
211
|
yield { type: "retry", attempt: n, delayMs, error: outcome.failure.error?.message ?? "retryable error" };
|
|
184
212
|
await settings.sleep(delayMs, signal);
|
|
185
213
|
if (signal?.aborted) {
|
package/dist/retry.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retry.js","sourceRoot":"","sources":["../src/retry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAClE,OAAO,E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W,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;QAC5D,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAC/B,4GAA4G;QAC5G,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;YAClB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;YACpB,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;QAC1B,CAAC;QACD,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;QACD,iGAAiG;QACjG,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,cAAc,GAAG,cAAc,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,kBAAkB,CAAC,CAAC;QAClG,MAAM,EAAE,IAAI,EAAE,OAAO,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 * - Each wait is capped at 60 s. The provider's retry-after hint isn't used yet: adapters see the response headers,\n * and no event carries a hint to the gateway.\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 } 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. Its usage is zero, because\n * nothing reported any, but its cost is unknown, not zero: the request may have reached the provider and been 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: emptyUsage(),\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 // The failure's unknown cost is final: pricing its zero usage would claim a known zero.\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/** 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 // The cap keeps a long retry count from waiting hours, or overflowing setTimeout's 32-bit delay.\n const delayMs = Math.min(settings.initialDelayMs * BACKOFF_FACTOR ** (n - 1), MAX_RETRY_DELAY_MS);\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"]}
|
|
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,EAA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AAC","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 { 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 abortedTerminal(outcome.failure, call, carriedCost, n > 1);\n return;\n }\n }\n}\n"]}
|
package/dist/types.d.ts
CHANGED
|
@@ -61,6 +61,12 @@ export interface Usage {
|
|
|
61
61
|
reasoning: number;
|
|
62
62
|
/** The provider's usage object, unchanged. */
|
|
63
63
|
raw: unknown;
|
|
64
|
+
/**
|
|
65
|
+
* `true` when the provider did not report this response's usage, such as a stream that ended without its usage
|
|
66
|
+
* chunk. The counts are then incomplete, often all zero, and the gateway prices the response as unknown instead of
|
|
67
|
+
* as free. Left out, the counts are what the provider reported, zeros included.
|
|
68
|
+
*/
|
|
69
|
+
unreported?: true;
|
|
64
70
|
}
|
|
65
71
|
/** The price of one response. `total` is `null` when the price is unknown, never a guessed zero. */
|
|
66
72
|
export interface Cost {
|
|
@@ -85,6 +91,13 @@ export interface AssistantMessage {
|
|
|
85
91
|
status?: number;
|
|
86
92
|
/** A provider-neutral name for a failure the caller may want to handle on its own, when the adapter knows one. */
|
|
87
93
|
code?: ModelErrorCode;
|
|
94
|
+
/**
|
|
95
|
+
* How long the provider asked the caller to wait before retrying, in milliseconds, read from the error
|
|
96
|
+
* response's `retry-after-ms` or `retry-after` header. Left out when the response sent neither or sent a value
|
|
97
|
+
* that isn't a valid delay. `createModel()` waits at least this long before it retries. A hint over 60 seconds
|
|
98
|
+
* gets no retry: the call ends with this error, so the caller can wait and call again.
|
|
99
|
+
*/
|
|
100
|
+
retryAfterMs?: number;
|
|
88
101
|
};
|
|
89
102
|
/** The tokens of every attempt the call made, failed attempts included, because the provider bills them all. */
|
|
90
103
|
usage: Usage;
|
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/**\n * The provider-neutral transcript the gateway, the adapters, the test model and the loop share\n * (SDK plan §3.2; SDK decisions 2 and 3; D3, D4).\n */\n\n/** A provider-issued token that is valid only for the model that produced it, such as a reasoning signature. */\nexport interface OpaqueToken {\n api: string;\n provider: string;\n model: string;\n value: string;\n}\n\n/** One block of message content. */\nexport type ContentBlock =\n | { type: \"text\"; text: string }\n /** Valid only for the model that made it; `transformMessages()` drops or converts it for other models. */\n | { type: \"reasoning\"; text: string; signature?: OpaqueToken }\n | { type: \"image\"; mediaType: string; data: string }\n | { type: \"tool_call\"; callId: string; name: string; input: unknown };\n\n/** What a tool result may carry. */\nexport type ModelContent = Extract<ContentBlock, { type: \"text\" | \"image\" }>;\n\n/** Identifies one model behind one wire protocol and one vendor. */\nexport interface ModelRef {\n /** Wire protocol, for example `\"anthropic-messages\"`. */\n api: string;\n /** Vendor and price-catalog entry. */\n provider: string;\n id: string;\n}\n\n/**\n * Token counts for one model response. There is deliberately no field named `input`: providers disagree on\n * whether \"input\" includes cached tokens, so the uncached part says so in its name and `totalInput()` sums the parts.\n */\nexport interface Usage {\n uncachedInput: number;\n cacheRead: number;\n /**\n * Cache writes at the short and the long lifetime. The keys name Anthropic's 5-minute and 1-hour writes; the\n * OpenAI-compatible adapter counts its 30-minute writes under `\"5m\"`.\n */\n cacheWrite: { \"5m\": number; \"1h\": number };\n output: number;\n /** The part of `output` spent on reasoning. */\n reasoning: number;\n /** The provider's usage object, unchanged. */\n raw: unknown;\n}\n\n/** The price of one response. `total` is `null` when the price is unknown, never a guessed zero. */\nexport interface Cost {\n total: number | null;\n basis: \"provider\" | \"catalog\" | \"unknown\";\n currency: \"USD\";\n catalogAsOf?: string;\n}\n\n/**\n * A failure an adapter can name in provider-neutral terms. `\"thinking_prefix_mismatch\"`: the provider rejected a\n * reasoning block because the transcript before it changed after the block was written.\n */\nexport type ModelErrorCode = \"thinking_prefix_mismatch\";\n\nexport interface AssistantMessage {\n role: \"assistant\";\n content: ContentBlock[];\n model: ModelRef;\n stopReason: \"stop\" | \"length\" | \"tool_calls\" | \"error\" | \"aborted\";\n error?: {\n kind: \"retryable\" | \"overflow\" | \"quota\" | \"fatal\";\n message: string;\n status?: number;\n /** A provider-neutral name for a failure the caller may want to handle on its own, when the adapter knows one. */\n code?: ModelErrorCode;\n };\n /** The tokens of every attempt the call made, failed attempts included, because the provider bills them all. */\n usage: Usage;\n /**\n * The usage of the attempt that produced this message alone. `createModel()` sets it when a call was retried, so\n * `usage` sums several requests; it is absent when `usage` is one request's.\n */\n lastAttemptUsage?: Usage;\n cost: Cost;\n}\n\nexport type Message =\n | { role: \"user\"; content: ContentBlock[] }\n | AssistantMessage\n | { role: \"tool\"; callId: string; content: ModelContent[]; isError: boolean };\n\n/**\n * A tool as one model call sends it: a name, a description and a JSON Schema for the input. The SDK's\n * `defineTool()` lowers its Standard Schema or JSON Schema input to this shape.\n */\nexport interface WireTool {\n name: string;\n description: string;\n /** A JSON Schema object describing the tool's input. */\n inputSchema: Record<string, unknown>;\n}\n\n/** What one model call sends. */\nexport interface ModelContext {\n system: string;\n messages: Message[];\n tools: WireTool[];\n}\n\n/** The context window and output limit that context trimming and the budget need; filled from the price catalog. */\nexport interface ModelCaps {\n /** The most input tokens one request can send, which is below the full window when the provider caps input. */\n contextWindow: number;\n maxOutputTokens: number;\n images: boolean;\n reasoning: boolean;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/**\n * The provider-neutral transcript the gateway, the adapters, the test model and the loop share\n * (SDK plan §3.2; SDK decisions 2 and 3; D3, D4).\n */\n\n/** A provider-issued token that is valid only for the model that produced it, such as a reasoning signature. */\nexport interface OpaqueToken {\n api: string;\n provider: string;\n model: string;\n value: string;\n}\n\n/** One block of message content. */\nexport type ContentBlock =\n | { type: \"text\"; text: string }\n /** Valid only for the model that made it; `transformMessages()` drops or converts it for other models. */\n | { type: \"reasoning\"; text: string; signature?: OpaqueToken }\n | { type: \"image\"; mediaType: string; data: string }\n | { type: \"tool_call\"; callId: string; name: string; input: unknown };\n\n/** What a tool result may carry. */\nexport type ModelContent = Extract<ContentBlock, { type: \"text\" | \"image\" }>;\n\n/** Identifies one model behind one wire protocol and one vendor. */\nexport interface ModelRef {\n /** Wire protocol, for example `\"anthropic-messages\"`. */\n api: string;\n /** Vendor and price-catalog entry. */\n provider: string;\n id: string;\n}\n\n/**\n * Token counts for one model response. There is deliberately no field named `input`: providers disagree on\n * whether \"input\" includes cached tokens, so the uncached part says so in its name and `totalInput()` sums the parts.\n */\nexport interface Usage {\n uncachedInput: number;\n cacheRead: number;\n /**\n * Cache writes at the short and the long lifetime. The keys name Anthropic's 5-minute and 1-hour writes; the\n * OpenAI-compatible adapter counts its 30-minute writes under `\"5m\"`.\n */\n cacheWrite: { \"5m\": number; \"1h\": number };\n output: number;\n /** The part of `output` spent on reasoning. */\n reasoning: number;\n /** The provider's usage object, unchanged. */\n raw: unknown;\n /**\n * `true` when the provider did not report this response's usage, such as a stream that ended without its usage\n * chunk. The counts are then incomplete, often all zero, and the gateway prices the response as unknown instead of\n * as free. Left out, the counts are what the provider reported, zeros included.\n */\n unreported?: true;\n}\n\n/** The price of one response. `total` is `null` when the price is unknown, never a guessed zero. */\nexport interface Cost {\n total: number | null;\n basis: \"provider\" | \"catalog\" | \"unknown\";\n currency: \"USD\";\n catalogAsOf?: string;\n}\n\n/**\n * A failure an adapter can name in provider-neutral terms. `\"thinking_prefix_mismatch\"`: the provider rejected a\n * reasoning block because the transcript before it changed after the block was written.\n */\nexport type ModelErrorCode = \"thinking_prefix_mismatch\";\n\nexport interface AssistantMessage {\n role: \"assistant\";\n content: ContentBlock[];\n model: ModelRef;\n stopReason: \"stop\" | \"length\" | \"tool_calls\" | \"error\" | \"aborted\";\n error?: {\n kind: \"retryable\" | \"overflow\" | \"quota\" | \"fatal\";\n message: string;\n status?: number;\n /** A provider-neutral name for a failure the caller may want to handle on its own, when the adapter knows one. */\n code?: ModelErrorCode;\n /**\n * How long the provider asked the caller to wait before retrying, in milliseconds, read from the error\n * response's `retry-after-ms` or `retry-after` header. Left out when the response sent neither or sent a value\n * that isn't a valid delay. `createModel()` waits at least this long before it retries. A hint over 60 seconds\n * gets no retry: the call ends with this error, so the caller can wait and call again.\n */\n retryAfterMs?: number;\n };\n /** The tokens of every attempt the call made, failed attempts included, because the provider bills them all. */\n usage: Usage;\n /**\n * The usage of the attempt that produced this message alone. `createModel()` sets it when a call was retried, so\n * `usage` sums several requests; it is absent when `usage` is one request's.\n */\n lastAttemptUsage?: Usage;\n cost: Cost;\n}\n\nexport type Message =\n | { role: \"user\"; content: ContentBlock[] }\n | AssistantMessage\n | { role: \"tool\"; callId: string; content: ModelContent[]; isError: boolean };\n\n/**\n * A tool as one model call sends it: a name, a description and a JSON Schema for the input. The SDK's\n * `defineTool()` lowers its Standard Schema or JSON Schema input to this shape.\n */\nexport interface WireTool {\n name: string;\n description: string;\n /** A JSON Schema object describing the tool's input. */\n inputSchema: Record<string, unknown>;\n}\n\n/** What one model call sends. */\nexport interface ModelContext {\n system: string;\n messages: Message[];\n tools: WireTool[];\n}\n\n/** The context window and output limit that context trimming and the budget need; filled from the price catalog. */\nexport interface ModelCaps {\n /** The most input tokens one request can send, which is below the full window when the provider caps input. */\n contextWindow: number;\n maxOutputTokens: number;\n images: boolean;\n reasoning: boolean;\n}\n"]}
|