@buildanagent/gateway 0.0.0-stage → 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/LICENSE +52 -0
- package/LICENSE-APACHE +202 -0
- package/LICENSE-MIT +21 -0
- package/README.md +305 -2
- package/dist/adapters/anthropic/adapter.d.ts +25 -0
- package/dist/adapters/anthropic/adapter.js +183 -0
- package/dist/adapters/anthropic/adapter.js.map +1 -0
- package/dist/adapters/anthropic/failure.d.ts +27 -0
- package/dist/adapters/anthropic/failure.js +53 -0
- package/dist/adapters/anthropic/failure.js.map +1 -0
- package/dist/adapters/anthropic/index.d.ts +3 -0
- package/dist/adapters/anthropic/index.js +3 -0
- package/dist/adapters/anthropic/index.js.map +1 -0
- package/dist/adapters/anthropic/request.d.ts +53 -0
- package/dist/adapters/anthropic/request.js +250 -0
- package/dist/adapters/anthropic/request.js.map +1 -0
- package/dist/adapters/anthropic/response.d.ts +38 -0
- package/dist/adapters/anthropic/response.js +237 -0
- package/dist/adapters/anthropic/response.js.map +1 -0
- package/dist/adapters/anthropic/usage.d.ts +26 -0
- package/dist/adapters/anthropic/usage.js +39 -0
- package/dist/adapters/anthropic/usage.js.map +1 -0
- package/dist/adapters/openai-chat/adapter.d.ts +18 -0
- package/dist/adapters/openai-chat/adapter.js +86 -0
- package/dist/adapters/openai-chat/adapter.js.map +1 -0
- package/dist/adapters/openai-chat/compat.d.ts +58 -0
- package/dist/adapters/openai-chat/compat.js +19 -0
- package/dist/adapters/openai-chat/compat.js.map +1 -0
- package/dist/adapters/openai-chat/failure.d.ts +32 -0
- package/dist/adapters/openai-chat/failure.js +40 -0
- package/dist/adapters/openai-chat/failure.js.map +1 -0
- package/dist/adapters/openai-chat/index.d.ts +4 -0
- package/dist/adapters/openai-chat/index.js +3 -0
- package/dist/adapters/openai-chat/index.js.map +1 -0
- package/dist/adapters/openai-chat/request.d.ts +47 -0
- package/dist/adapters/openai-chat/request.js +227 -0
- package/dist/adapters/openai-chat/request.js.map +1 -0
- package/dist/adapters/openai-chat/response.d.ts +22 -0
- package/dist/adapters/openai-chat/response.js +203 -0
- package/dist/adapters/openai-chat/response.js.map +1 -0
- package/dist/adapters/openai-chat/usage.d.ts +22 -0
- package/dist/adapters/openai-chat/usage.js +25 -0
- package/dist/adapters/openai-chat/usage.js.map +1 -0
- package/dist/adapters/strict-schema.d.ts +67 -0
- package/dist/adapters/strict-schema.js +269 -0
- package/dist/adapters/strict-schema.js.map +1 -0
- package/dist/adapters/wire.d.ts +98 -0
- package/dist/adapters/wire.js +187 -0
- package/dist/adapters/wire.js.map +1 -0
- package/dist/catalog.d.ts +65 -0
- package/dist/catalog.js +145 -0
- package/dist/catalog.js.map +1 -0
- package/dist/catalog.json +120 -0
- package/dist/classify.d.ts +25 -0
- package/dist/classify.js +37 -0
- package/dist/classify.js.map +1 -0
- package/dist/complete.d.ts +9 -0
- package/dist/complete.js +13 -0
- package/dist/complete.js.map +1 -0
- package/dist/cost.d.ts +44 -0
- package/dist/cost.js +132 -0
- package/dist/cost.js.map +1 -0
- package/dist/create-model.d.ts +7 -0
- package/dist/create-model.js +81 -0
- package/dist/create-model.js.map +1 -0
- package/dist/errors.d.ts +17 -0
- package/dist/errors.js +24 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/middleware.d.ts +11 -0
- package/dist/middleware.js +22 -0
- package/dist/middleware.js.map +1 -0
- package/dist/names.d.ts +72 -0
- package/dist/names.js +175 -0
- package/dist/names.js.map +1 -0
- package/dist/provider-config.d.ts +15 -0
- package/dist/provider-config.js +23 -0
- package/dist/provider-config.js.map +1 -0
- package/dist/request-dump.d.ts +28 -0
- package/dist/request-dump.js +52 -0
- package/dist/request-dump.js.map +1 -0
- package/dist/request-policy.d.ts +18 -0
- package/dist/request-policy.js +27 -0
- package/dist/request-policy.js.map +1 -0
- package/dist/retry.d.ts +33 -0
- package/dist/retry.js +219 -0
- package/dist/retry.js.map +1 -0
- package/dist/runtime-types.d.ts +112 -0
- package/dist/runtime-types.js +2 -0
- package/dist/runtime-types.js.map +1 -0
- package/dist/spec.d.ts +67 -0
- package/dist/spec.js +2 -0
- package/dist/spec.js.map +1 -0
- package/dist/sse.d.ts +15 -0
- package/dist/sse.js +73 -0
- package/dist/sse.js.map +1 -0
- package/dist/stream-contract.d.ts +23 -0
- package/dist/stream-contract.js +101 -0
- package/dist/stream-contract.js.map +1 -0
- package/dist/testing/guards.d.ts +16 -0
- package/dist/testing/guards.js +48 -0
- package/dist/testing/guards.js.map +1 -0
- package/dist/testing/index.d.ts +12 -0
- package/dist/testing/index.js +13 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/mock-transport.d.ts +49 -0
- package/dist/testing/mock-transport.js +101 -0
- package/dist/testing/mock-transport.js.map +1 -0
- package/dist/testing/random.d.ts +3 -0
- package/dist/testing/random.js +13 -0
- package/dist/testing/random.js.map +1 -0
- package/dist/testing/setup.d.ts +1 -0
- package/dist/testing/setup.js +12 -0
- package/dist/testing/setup.js.map +1 -0
- package/dist/transform-messages.d.ts +32 -0
- package/dist/transform-messages.js +291 -0
- package/dist/transform-messages.js.map +1 -0
- package/dist/types.d.ts +143 -0
- package/dist/types.js +6 -0
- package/dist/types.js.map +1 -0
- package/dist/usage.d.ts +16 -0
- package/dist/usage.js +31 -0
- package/dist/usage.js.map +1 -0
- package/package.json +50 -3
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What every wire adapter shares: sending one HTTP request through the caller's transport, reading a server-sent
|
|
3
|
+
* event stream to its terminal, and turning a failed request, a broken stream or an abort into an `error` terminal.
|
|
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.
|
|
9
|
+
*/
|
|
10
|
+
import { classifyError, isTransportFailure } from "../classify.js";
|
|
11
|
+
import { isGatewayError } from "../errors.js";
|
|
12
|
+
import { assertModelRequestAllowed, ModelRequestsDisabledError } from "../request-policy.js";
|
|
13
|
+
import { readServerSentEvents } from "../sse.js";
|
|
14
|
+
import { emptyUsage, unreportedUsage } from "../usage.js";
|
|
15
|
+
/** True for a JSON object, not an array or `null`. */
|
|
16
|
+
export function isObject(value) {
|
|
17
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
18
|
+
}
|
|
19
|
+
/** The value when it is a string, or `""`. */
|
|
20
|
+
export function str(value) {
|
|
21
|
+
return typeof value === "string" ? value : "";
|
|
22
|
+
}
|
|
23
|
+
/** A token count: a finite positive number, or 0. */
|
|
24
|
+
export function count(value) {
|
|
25
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : 0;
|
|
26
|
+
}
|
|
27
|
+
/** A tool call's input: the parsed JSON, or the partial JSON text when the output limit cut it off. */
|
|
28
|
+
export function parseToolInput(text, empty = {}) {
|
|
29
|
+
if (text === "")
|
|
30
|
+
return empty;
|
|
31
|
+
try {
|
|
32
|
+
return JSON.parse(text);
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
return text;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
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
|
+
*/
|
|
42
|
+
export function terminalMessage(model, partial, stopReason, error) {
|
|
43
|
+
return {
|
|
44
|
+
role: "assistant",
|
|
45
|
+
content: partial.content ?? [],
|
|
46
|
+
model,
|
|
47
|
+
stopReason,
|
|
48
|
+
...(error ? { error } : {}),
|
|
49
|
+
usage: partial.usage ?? unreportedUsage(),
|
|
50
|
+
cost: { total: null, basis: "unknown", currency: "USD" },
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/** The `error` terminal of an aborted call, which carries no `error`. */
|
|
54
|
+
export function abortedEvent(model, partial = {}) {
|
|
55
|
+
return { type: "error", message: terminalMessage(model, partial, "aborted") };
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The `error` terminal for a failure. A failure with no HTTP status, such as an error event inside a 200 stream,
|
|
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.
|
|
61
|
+
*/
|
|
62
|
+
export function failureTerminal(model, failure, statusOfType, partial = {}, code) {
|
|
63
|
+
const status = failure.status ?? (failure.type ? statusOfType[failure.type] : undefined);
|
|
64
|
+
const label = failure.errorCode ?? failure.type;
|
|
65
|
+
// Both go to the classifier: a billing error's code names its cause while its type says `insufficient_quota`.
|
|
66
|
+
const codes = [failure.errorCode, failure.type].filter((part) => part !== undefined && part !== "").join(" ");
|
|
67
|
+
const kind = failure.kind ??
|
|
68
|
+
classifyError({
|
|
69
|
+
...(status === undefined ? {} : { status }),
|
|
70
|
+
...(codes ? { code: codes } : {}),
|
|
71
|
+
message: failure.message,
|
|
72
|
+
});
|
|
73
|
+
const error = {
|
|
74
|
+
kind,
|
|
75
|
+
message: label ? `${label}: ${failure.message}` : failure.message,
|
|
76
|
+
...(failure.status === undefined ? {} : { status: failure.status }),
|
|
77
|
+
...(code ? { code } : {}),
|
|
78
|
+
...(failure.retryAfterMs === undefined ? {} : { retryAfterMs: failure.retryAfterMs }),
|
|
79
|
+
};
|
|
80
|
+
const reply = failure.status === undefined || partial.usage ? partial : { ...partial, usage: emptyUsage() };
|
|
81
|
+
return { type: "error", message: terminalMessage(model, reply, "error", error) };
|
|
82
|
+
}
|
|
83
|
+
/** The `error` terminal for a throw: retryable only for a transport failure, and `fatal` for a bug or bad data. */
|
|
84
|
+
export function thrownTerminal(model, thrown, partial = {}) {
|
|
85
|
+
const message = thrown instanceof Error ? `${thrown.name}: ${thrown.message}` : String(thrown);
|
|
86
|
+
return failureTerminal(model, { message, ...(isTransportFailure(thrown) ? {} : { kind: "fatal" }) }, {}, partial);
|
|
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
|
+
}
|
|
128
|
+
/** A failed response's body, parsed as JSON when it is JSON, and its text. */
|
|
129
|
+
export async function readErrorBody(response) {
|
|
130
|
+
const text = await response.text().catch(() => "");
|
|
131
|
+
try {
|
|
132
|
+
return { body: JSON.parse(text), text };
|
|
133
|
+
}
|
|
134
|
+
catch {
|
|
135
|
+
return { body: undefined, text };
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/** The body `onPayload` asks for, or the adapter's own when it returns `undefined`. */
|
|
139
|
+
export function payloadOf(body, options) {
|
|
140
|
+
const replaced = options.onPayload ? options.onPayload(body) : undefined;
|
|
141
|
+
return replaced === undefined ? body : replaced;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Sends one HTTP request. A transport that throws becomes an `error` terminal unless the throw is a guard's; only a
|
|
145
|
+
* transport failure, by the rule the retries use, may be retried, and any other throw is `fatal`.
|
|
146
|
+
*/
|
|
147
|
+
export async function* sendRequest(transport, request, model, signal) {
|
|
148
|
+
assertModelRequestAllowed(transport);
|
|
149
|
+
try {
|
|
150
|
+
return await transport(request);
|
|
151
|
+
}
|
|
152
|
+
catch (thrown) {
|
|
153
|
+
if (isGatewayError(thrown) || thrown instanceof ModelRequestsDisabledError)
|
|
154
|
+
throw thrown;
|
|
155
|
+
yield signal?.aborted ? abortedEvent(model) : thrownTerminal(model, thrown);
|
|
156
|
+
return undefined;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/** Feeds each server-sent event to the reader; returns true once it has yielded the terminal. */
|
|
160
|
+
async function* pump(body, reader) {
|
|
161
|
+
for await (const sse of readServerSentEvents(body)) {
|
|
162
|
+
for (const event of reader.handle(sse)) {
|
|
163
|
+
yield event;
|
|
164
|
+
if (event.type === "done" || event.type === "error")
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
/** Reads a 200 stream to its terminal. A stream that breaks or ends early ends in an `error` terminal. */
|
|
171
|
+
export async function* readEventStream(response, model, reader, signal) {
|
|
172
|
+
if (response.body === null) {
|
|
173
|
+
yield failureTerminal(model, { message: "the response has no body" }, {});
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
try {
|
|
177
|
+
if (yield* pump(response.body, reader))
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
catch (thrown) {
|
|
181
|
+
// A broken connection may work on a retry; malformed event data, a SyntaxError, will not parse on one either.
|
|
182
|
+
yield signal?.aborted ? abortedEvent(model, reader.partial()) : thrownTerminal(model, thrown, reader.partial());
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
yield* reader.ended();
|
|
186
|
+
}
|
|
187
|
+
//# sourceMappingURL=wire.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wire.js","sourceRoot":"","sources":["../../src/adapters/wire.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,aAAa,EAAkB,kBAAkB,EAAE,MAAM,gBAAgB,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,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"]}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The USD price per million tokens of each token class, for requests up to `upToInputTokens` input tokens.
|
|
3
|
+
* `upToInputTokens` counts every input class, cache reads and cache writes included, and is `null` for the last tier.
|
|
4
|
+
*/
|
|
5
|
+
export interface RateTier {
|
|
6
|
+
readonly upToInputTokens: number | null;
|
|
7
|
+
readonly uncachedInput: number;
|
|
8
|
+
readonly cacheRead: number;
|
|
9
|
+
/**
|
|
10
|
+
* The short and the long cache-write rate. The keys name Anthropic's 5-minute and 1-hour writes; an OpenAI entry
|
|
11
|
+
* stores its 30-minute rate under `"5m"`, the key its adapter counts every Chat Completions write under.
|
|
12
|
+
*/
|
|
13
|
+
readonly cacheWrite: {
|
|
14
|
+
readonly "5m": number;
|
|
15
|
+
readonly "1h": number;
|
|
16
|
+
};
|
|
17
|
+
/** Reasoning tokens are part of output and are billed at this rate. */
|
|
18
|
+
readonly output: number;
|
|
19
|
+
}
|
|
20
|
+
/** Where each part of a catalog entry was copied from: the provider's own pricing and model pages. */
|
|
21
|
+
export interface CatalogSources {
|
|
22
|
+
readonly rates: string;
|
|
23
|
+
readonly contextWindow: string;
|
|
24
|
+
readonly maxOutputTokens: string;
|
|
25
|
+
}
|
|
26
|
+
/** One model in the price catalog: its rates, its limits, and the date and pages they were copied from. */
|
|
27
|
+
export interface CatalogEntry {
|
|
28
|
+
readonly provider: string;
|
|
29
|
+
readonly id: string;
|
|
30
|
+
/** The rate tiers, in ascending order of `upToInputTokens`. A model without long-context pricing has one tier. */
|
|
31
|
+
readonly rates: readonly RateTier[];
|
|
32
|
+
/**
|
|
33
|
+
* The most input tokens one request can send: the context window, or the provider's lower input limit when its
|
|
34
|
+
* model page sets one inside the window.
|
|
35
|
+
*/
|
|
36
|
+
readonly contextWindow: number;
|
|
37
|
+
readonly maxOutputTokens: number;
|
|
38
|
+
/** Whether the model takes image input. Left out, the adapter's default applies. */
|
|
39
|
+
readonly images?: boolean;
|
|
40
|
+
/** Whether the model reasons. Left out, the adapter's default applies. */
|
|
41
|
+
readonly reasoning?: boolean;
|
|
42
|
+
/** The date the rates and limits were copied, as `YYYY-MM-DD`. */
|
|
43
|
+
readonly asOf: string;
|
|
44
|
+
readonly sources: CatalogSources;
|
|
45
|
+
/** How a number was derived when the sources don't state it directly. */
|
|
46
|
+
readonly note?: string;
|
|
47
|
+
}
|
|
48
|
+
/** A list of catalog entries. `createModel()` reads a model's caps and prices from it. */
|
|
49
|
+
export interface PriceCatalog {
|
|
50
|
+
readonly models: readonly CatalogEntry[];
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Checks a catalog read from JSON and returns it typed. Throws a `GatewayError` with code `invalid_catalog` that
|
|
54
|
+
* names the first bad field, such as a negative rate, a missing source URL or two entries for one model.
|
|
55
|
+
*/
|
|
56
|
+
export declare function parseCatalog(value: unknown): PriceCatalog;
|
|
57
|
+
/** The catalog that ships with the gateway. `createModel()` uses it unless you pass your own. */
|
|
58
|
+
export declare const defaultCatalog: PriceCatalog;
|
|
59
|
+
/** The entry for one model, or `undefined` when the catalog doesn't list it. */
|
|
60
|
+
export declare function findCatalogEntry(catalog: PriceCatalog, provider: string, id: string): CatalogEntry | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* The entries whose `asOf` date is more than `maxAgeDays` days before `now`. Prices change often, so check a catalog
|
|
63
|
+
* you maintain with this before each release.
|
|
64
|
+
*/
|
|
65
|
+
export declare function staleCatalogEntries(catalog: PriceCatalog, now: Date, maxAgeDays?: number): CatalogEntry[];
|
package/dist/catalog.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The price catalog: per-model rates, limits, the date they were copied and where from (D4). `catalog.json` ships
|
|
3
|
+
* with the package; a caller may pass its own catalog instead. Nothing here fetches prices at run time, so cost never
|
|
4
|
+
* depends on the network.
|
|
5
|
+
*/
|
|
6
|
+
import shipped from "./catalog.json" with { type: "json" };
|
|
7
|
+
import { GatewayError } from "./errors.js";
|
|
8
|
+
function fail(where, problem) {
|
|
9
|
+
throw new GatewayError("invalid_catalog", `price catalog: ${where} ${problem}`);
|
|
10
|
+
}
|
|
11
|
+
function objectAt(value, where) {
|
|
12
|
+
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
13
|
+
fail(where, "must be an object");
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
function rate(value, where) {
|
|
17
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value < 0)
|
|
18
|
+
fail(where, "must be a non-negative number");
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
function tokenCount(value, where) {
|
|
22
|
+
if (!Number.isInteger(value) || value <= 0)
|
|
23
|
+
fail(where, "must be a positive integer");
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
function text(value, where) {
|
|
27
|
+
if (typeof value !== "string" || value === "")
|
|
28
|
+
fail(where, "must be a non-empty string");
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
31
|
+
function url(value, where) {
|
|
32
|
+
const s = text(value, where);
|
|
33
|
+
if (!/^https:\/\/\S+$/.test(s))
|
|
34
|
+
fail(where, "must be an https URL");
|
|
35
|
+
return s;
|
|
36
|
+
}
|
|
37
|
+
function date(value, where) {
|
|
38
|
+
const s = text(value, where);
|
|
39
|
+
// A calendar check, so "2026-02-30" fails as well as "Nov 2026". `Date.parse` is NaN for a month or day out of
|
|
40
|
+
// range, such as "2026-13-01", where `toISOString()` would throw a RangeError.
|
|
41
|
+
const time = /^\d{4}-\d{2}-\d{2}$/.test(s) ? Date.parse(`${s}T00:00:00Z`) : Number.NaN;
|
|
42
|
+
if (Number.isNaN(time) || new Date(time).toISOString().slice(0, 10) !== s) {
|
|
43
|
+
fail(where, "must be a date in the form YYYY-MM-DD");
|
|
44
|
+
}
|
|
45
|
+
return s;
|
|
46
|
+
}
|
|
47
|
+
function optionalFlag(value, where) {
|
|
48
|
+
if (value !== undefined && typeof value !== "boolean")
|
|
49
|
+
fail(where, "must be true or false");
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
function optionalText(value, where) {
|
|
53
|
+
return value === undefined ? undefined : text(value, where);
|
|
54
|
+
}
|
|
55
|
+
function parseTier(value, where, last) {
|
|
56
|
+
const tier = objectAt(value, where);
|
|
57
|
+
const cacheWrite = objectAt(tier.cacheWrite, `${where}.cacheWrite`);
|
|
58
|
+
const limit = tier.upToInputTokens;
|
|
59
|
+
if (last && limit !== null)
|
|
60
|
+
fail(`${where}.upToInputTokens`, "must be null on the last tier");
|
|
61
|
+
return {
|
|
62
|
+
upToInputTokens: last ? null : tokenCount(limit, `${where}.upToInputTokens`),
|
|
63
|
+
uncachedInput: rate(tier.uncachedInput, `${where}.uncachedInput`),
|
|
64
|
+
cacheRead: rate(tier.cacheRead, `${where}.cacheRead`),
|
|
65
|
+
cacheWrite: {
|
|
66
|
+
"5m": rate(cacheWrite["5m"], `${where}.cacheWrite.5m`),
|
|
67
|
+
"1h": rate(cacheWrite["1h"], `${where}.cacheWrite.1h`),
|
|
68
|
+
},
|
|
69
|
+
output: rate(tier.output, `${where}.output`),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function parseTiers(value, where) {
|
|
73
|
+
if (!Array.isArray(value) || value.length === 0)
|
|
74
|
+
fail(where, "must be a non-empty array");
|
|
75
|
+
const tiers = value.map((tier, i) => parseTier(tier, `${where}[${i}]`, i === value.length - 1));
|
|
76
|
+
let previous = 0;
|
|
77
|
+
for (const tier of tiers) {
|
|
78
|
+
if (tier.upToInputTokens === null)
|
|
79
|
+
break;
|
|
80
|
+
if (tier.upToInputTokens <= previous)
|
|
81
|
+
fail(where, "must be in ascending order of upToInputTokens");
|
|
82
|
+
previous = tier.upToInputTokens;
|
|
83
|
+
}
|
|
84
|
+
return tiers;
|
|
85
|
+
}
|
|
86
|
+
function parseSources(value, where) {
|
|
87
|
+
const sources = objectAt(value, where);
|
|
88
|
+
return {
|
|
89
|
+
rates: url(sources.rates, `${where}.rates`),
|
|
90
|
+
contextWindow: url(sources.contextWindow, `${where}.contextWindow`),
|
|
91
|
+
maxOutputTokens: url(sources.maxOutputTokens, `${where}.maxOutputTokens`),
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
function parseEntry(value, where) {
|
|
95
|
+
const entry = objectAt(value, where);
|
|
96
|
+
const images = optionalFlag(entry.images, `${where}.images`);
|
|
97
|
+
const reasoning = optionalFlag(entry.reasoning, `${where}.reasoning`);
|
|
98
|
+
const note = optionalText(entry.note, `${where}.note`);
|
|
99
|
+
return {
|
|
100
|
+
provider: text(entry.provider, `${where}.provider`),
|
|
101
|
+
id: text(entry.id, `${where}.id`),
|
|
102
|
+
rates: parseTiers(entry.rates, `${where}.rates`),
|
|
103
|
+
contextWindow: tokenCount(entry.contextWindow, `${where}.contextWindow`),
|
|
104
|
+
maxOutputTokens: tokenCount(entry.maxOutputTokens, `${where}.maxOutputTokens`),
|
|
105
|
+
...(images === undefined ? {} : { images }),
|
|
106
|
+
...(reasoning === undefined ? {} : { reasoning }),
|
|
107
|
+
asOf: date(entry.asOf, `${where}.asOf`),
|
|
108
|
+
sources: parseSources(entry.sources, `${where}.sources`),
|
|
109
|
+
...(note === undefined ? {} : { note }),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Checks a catalog read from JSON and returns it typed. Throws a `GatewayError` with code `invalid_catalog` that
|
|
114
|
+
* names the first bad field, such as a negative rate, a missing source URL or two entries for one model.
|
|
115
|
+
*/
|
|
116
|
+
export function parseCatalog(value) {
|
|
117
|
+
const catalog = objectAt(value, "the catalog");
|
|
118
|
+
if (!Array.isArray(catalog.models))
|
|
119
|
+
fail("models", "must be an array");
|
|
120
|
+
const models = catalog.models.map((entry, i) => parseEntry(entry, `models[${i}]`));
|
|
121
|
+
const seen = new Set();
|
|
122
|
+
for (const { provider, id } of models) {
|
|
123
|
+
// A plain `provider/id` join would collide, because both may contain "/".
|
|
124
|
+
const key = JSON.stringify([provider, id]);
|
|
125
|
+
if (seen.has(key))
|
|
126
|
+
fail("models", `lists ${provider}/${id} twice`);
|
|
127
|
+
seen.add(key);
|
|
128
|
+
}
|
|
129
|
+
return { models };
|
|
130
|
+
}
|
|
131
|
+
/** The catalog that ships with the gateway. `createModel()` uses it unless you pass your own. */
|
|
132
|
+
export const defaultCatalog = parseCatalog(shipped);
|
|
133
|
+
/** The entry for one model, or `undefined` when the catalog doesn't list it. */
|
|
134
|
+
export function findCatalogEntry(catalog, provider, id) {
|
|
135
|
+
return catalog.models.find((m) => m.provider === provider && m.id === id);
|
|
136
|
+
}
|
|
137
|
+
const DAY_MS = 86_400_000;
|
|
138
|
+
/**
|
|
139
|
+
* The entries whose `asOf` date is more than `maxAgeDays` days before `now`. Prices change often, so check a catalog
|
|
140
|
+
* you maintain with this before each release.
|
|
141
|
+
*/
|
|
142
|
+
export function staleCatalogEntries(catalog, now, maxAgeDays = 90) {
|
|
143
|
+
return catalog.models.filter((entry) => now.getTime() - Date.parse(`${entry.asOf}T00:00:00Z`) > maxAgeDays * DAY_MS);
|
|
144
|
+
}
|
|
145
|
+
//# sourceMappingURL=catalog.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"catalog.js","sourceRoot":"","sources":["../src/catalog.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,OAAO,MAAM,gBAAgB,CAAC,OAAO,IAAI,EAAE,MAAM,EAAE,CAAC;AAC3D,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAwD3C,SAAS,IAAI,CAAC,KAAa,EAAE,OAAe;IAC1C,MAAM,IAAI,YAAY,CAAC,iBAAiB,EAAE,kBAAkB,KAAK,IAAI,OAAO,EAAE,CAAC,CAAC;AAClF,CAAC;AAED,SAAS,QAAQ,CAAC,KAAc,EAAE,KAAa;IAC7C,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,IAAI,CAAC,KAAK,EAAE,mBAAmB,CAAC,CAAC;I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AU,gBAAgB,CAAC,OAAqB,EAAE,QAAgB,EAAE,EAAU;IAClF,OAAO,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,KAAK,QAAQ,IAAI,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;AAC5E,CAAC;AAED,MAAM,MAAM,GAAG,UAAU,CAAC;AAE1B;;;GAGG;AACH,MAAM,UAAU,mBAAmB,CAAC,OAAqB,EAAE,GAAS,EAAE,UAAU,GAAG,EAAE;IACnF,OAAO,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,GAAG,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,IAAI,YAAY,CAAC,GAAG,UAAU,GAAG,MAAM,CAAC,CAAC;AACvH,CAAC","sourcesContent":["/**\n * The price catalog: per-model rates, limits, the date they were copied and where from (D4). `catalog.json` ships\n * with the package; a caller may pass its own catalog instead. Nothing here fetches prices at run time, so cost never\n * depends on the network.\n */\nimport shipped from \"./catalog.json\" with { type: \"json\" };\nimport { GatewayError } from \"./errors.js\";\n\n/**\n * The USD price per million tokens of each token class, for requests up to `upToInputTokens` input tokens.\n * `upToInputTokens` counts every input class, cache reads and cache writes included, and is `null` for the last tier.\n */\nexport interface RateTier {\n readonly upToInputTokens: number | null;\n readonly uncachedInput: number;\n readonly cacheRead: number;\n /**\n * The short and the long cache-write rate. The keys name Anthropic's 5-minute and 1-hour writes; an OpenAI entry\n * stores its 30-minute rate under `\"5m\"`, the key its adapter counts every Chat Completions write under.\n */\n readonly cacheWrite: { readonly \"5m\": number; readonly \"1h\": number };\n /** Reasoning tokens are part of output and are billed at this rate. */\n readonly output: number;\n}\n\n/** Where each part of a catalog entry was copied from: the provider's own pricing and model pages. */\nexport interface CatalogSources {\n readonly rates: string;\n readonly contextWindow: string;\n readonly maxOutputTokens: string;\n}\n\n/** One model in the price catalog: its rates, its limits, and the date and pages they were copied from. */\nexport interface CatalogEntry {\n readonly provider: string;\n readonly id: string;\n /** The rate tiers, in ascending order of `upToInputTokens`. A model without long-context pricing has one tier. */\n readonly rates: readonly RateTier[];\n /**\n * The most input tokens one request can send: the context window, or the provider's lower input limit when its\n * model page sets one inside the window.\n */\n readonly contextWindow: number;\n readonly maxOutputTokens: number;\n /** Whether the model takes image input. Left out, the adapter's default applies. */\n readonly images?: boolean;\n /** Whether the model reasons. Left out, the adapter's default applies. */\n readonly reasoning?: boolean;\n /** The date the rates and limits were copied, as `YYYY-MM-DD`. */\n readonly asOf: string;\n readonly sources: CatalogSources;\n /** How a number was derived when the sources don't state it directly. */\n readonly note?: string;\n}\n\n/** A list of catalog entries. `createModel()` reads a model's caps and prices from it. */\nexport interface PriceCatalog {\n readonly models: readonly CatalogEntry[];\n}\n\ntype Json = Record<string, unknown>;\n\nfunction fail(where: string, problem: string): never {\n throw new GatewayError(\"invalid_catalog\", `price catalog: ${where} ${problem}`);\n}\n\nfunction objectAt(value: unknown, where: string): Json {\n if (typeof value !== \"object\" || value === null || Array.isArray(value)) fail(where, \"must be an object\");\n return value as Json;\n}\n\nfunction rate(value: unknown, where: string): number {\n if (typeof value !== \"number\" || !Number.isFinite(value) || value < 0) fail(where, \"must be a non-negative number\");\n return value;\n}\n\nfunction tokenCount(value: unknown, where: string): number {\n if (!Number.isInteger(value) || (value as number) <= 0) fail(where, \"must be a positive integer\");\n return value as number;\n}\n\nfunction text(value: unknown, where: string): string {\n if (typeof value !== \"string\" || value === \"\") fail(where, \"must be a non-empty string\");\n return value;\n}\n\nfunction url(value: unknown, where: string): string {\n const s = text(value, where);\n if (!/^https:\\/\\/\\S+$/.test(s)) fail(where, \"must be an https URL\");\n return s;\n}\n\nfunction date(value: unknown, where: string): string {\n const s = text(value, where);\n // A calendar check, so \"2026-02-30\" fails as well as \"Nov 2026\". `Date.parse` is NaN for a month or day out of\n // range, such as \"2026-13-01\", where `toISOString()` would throw a RangeError.\n const time = /^\\d{4}-\\d{2}-\\d{2}$/.test(s) ? Date.parse(`${s}T00:00:00Z`) : Number.NaN;\n if (Number.isNaN(time) || new Date(time).toISOString().slice(0, 10) !== s) {\n fail(where, \"must be a date in the form YYYY-MM-DD\");\n }\n return s;\n}\n\nfunction optionalFlag(value: unknown, where: string): boolean | undefined {\n if (value !== undefined && typeof value !== \"boolean\") fail(where, \"must be true or false\");\n return value;\n}\n\nfunction optionalText(value: unknown, where: string): string | undefined {\n return value === undefined ? undefined : text(value, where);\n}\n\nfunction parseTier(value: unknown, where: string, last: boolean): RateTier {\n const tier = objectAt(value, where);\n const cacheWrite = objectAt(tier.cacheWrite, `${where}.cacheWrite`);\n const limit = tier.upToInputTokens;\n if (last && limit !== null) fail(`${where}.upToInputTokens`, \"must be null on the last tier\");\n return {\n upToInputTokens: last ? null : tokenCount(limit, `${where}.upToInputTokens`),\n uncachedInput: rate(tier.uncachedInput, `${where}.uncachedInput`),\n cacheRead: rate(tier.cacheRead, `${where}.cacheRead`),\n cacheWrite: {\n \"5m\": rate(cacheWrite[\"5m\"], `${where}.cacheWrite.5m`),\n \"1h\": rate(cacheWrite[\"1h\"], `${where}.cacheWrite.1h`),\n },\n output: rate(tier.output, `${where}.output`),\n };\n}\n\nfunction parseTiers(value: unknown, where: string): RateTier[] {\n if (!Array.isArray(value) || value.length === 0) fail(where, \"must be a non-empty array\");\n const tiers = value.map((tier, i) => parseTier(tier, `${where}[${i}]`, i === value.length - 1));\n let previous = 0;\n for (const tier of tiers) {\n if (tier.upToInputTokens === null) break;\n if (tier.upToInputTokens <= previous) fail(where, \"must be in ascending order of upToInputTokens\");\n previous = tier.upToInputTokens;\n }\n return tiers;\n}\n\nfunction parseSources(value: unknown, where: string): CatalogSources {\n const sources = objectAt(value, where);\n return {\n rates: url(sources.rates, `${where}.rates`),\n contextWindow: url(sources.contextWindow, `${where}.contextWindow`),\n maxOutputTokens: url(sources.maxOutputTokens, `${where}.maxOutputTokens`),\n };\n}\n\nfunction parseEntry(value: unknown, where: string): CatalogEntry {\n const entry = objectAt(value, where);\n const images = optionalFlag(entry.images, `${where}.images`);\n const reasoning = optionalFlag(entry.reasoning, `${where}.reasoning`);\n const note = optionalText(entry.note, `${where}.note`);\n return {\n provider: text(entry.provider, `${where}.provider`),\n id: text(entry.id, `${where}.id`),\n rates: parseTiers(entry.rates, `${where}.rates`),\n contextWindow: tokenCount(entry.contextWindow, `${where}.contextWindow`),\n maxOutputTokens: tokenCount(entry.maxOutputTokens, `${where}.maxOutputTokens`),\n ...(images === undefined ? {} : { images }),\n ...(reasoning === undefined ? {} : { reasoning }),\n asOf: date(entry.asOf, `${where}.asOf`),\n sources: parseSources(entry.sources, `${where}.sources`),\n ...(note === undefined ? {} : { note }),\n };\n}\n\n/**\n * Checks a catalog read from JSON and returns it typed. Throws a `GatewayError` with code `invalid_catalog` that\n * names the first bad field, such as a negative rate, a missing source URL or two entries for one model.\n */\nexport function parseCatalog(value: unknown): PriceCatalog {\n const catalog = objectAt(value, \"the catalog\");\n if (!Array.isArray(catalog.models)) fail(\"models\", \"must be an array\");\n const models = catalog.models.map((entry, i) => parseEntry(entry, `models[${i}]`));\n const seen = new Set<string>();\n for (const { provider, id } of models) {\n // A plain `provider/id` join would collide, because both may contain \"/\".\n const key = JSON.stringify([provider, id]);\n if (seen.has(key)) fail(\"models\", `lists ${provider}/${id} twice`);\n seen.add(key);\n }\n return { models };\n}\n\n/** The catalog that ships with the gateway. `createModel()` uses it unless you pass your own. */\nexport const defaultCatalog: PriceCatalog = parseCatalog(shipped);\n\n/** The entry for one model, or `undefined` when the catalog doesn't list it. */\nexport function findCatalogEntry(catalog: PriceCatalog, provider: string, id: string): CatalogEntry | undefined {\n return catalog.models.find((m) => m.provider === provider && m.id === id);\n}\n\nconst DAY_MS = 86_400_000;\n\n/**\n * The entries whose `asOf` date is more than `maxAgeDays` days before `now`. Prices change often, so check a catalog\n * you maintain with this before each release.\n */\nexport function staleCatalogEntries(catalog: PriceCatalog, now: Date, maxAgeDays = 90): CatalogEntry[] {\n return catalog.models.filter((entry) => now.getTime() - Date.parse(`${entry.asOf}T00:00:00Z`) > maxAgeDays * DAY_MS);\n}\n"]}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
{
|
|
2
|
+
"models": [
|
|
3
|
+
{
|
|
4
|
+
"provider": "anthropic",
|
|
5
|
+
"id": "claude-opus-5-5",
|
|
6
|
+
"rates": [
|
|
7
|
+
{
|
|
8
|
+
"upToInputTokens": null,
|
|
9
|
+
"uncachedInput": 4,
|
|
10
|
+
"cacheRead": 0.2,
|
|
11
|
+
"cacheWrite": {
|
|
12
|
+
"5m": 5,
|
|
13
|
+
"1h": 8
|
|
14
|
+
},
|
|
15
|
+
"output": 20
|
|
16
|
+
}
|
|
17
|
+
],
|
|
18
|
+
"contextWindow": 1000000,
|
|
19
|
+
"maxOutputTokens": 128000,
|
|
20
|
+
"images": true,
|
|
21
|
+
"reasoning": true,
|
|
22
|
+
"asOf": "2026-11-19",
|
|
23
|
+
"sources": {
|
|
24
|
+
"rates": "https://platform.claude.com/docs/en/about-claude/pricing",
|
|
25
|
+
"contextWindow": "https://platform.claude.com/docs/en/about-claude/models/overview",
|
|
26
|
+
"maxOutputTokens": "https://platform.claude.com/docs/en/about-claude/models/overview"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
"provider": "anthropic",
|
|
31
|
+
"id": "claude-sonnet-5-5",
|
|
32
|
+
"rates": [
|
|
33
|
+
{
|
|
34
|
+
"upToInputTokens": null,
|
|
35
|
+
"uncachedInput": 2,
|
|
36
|
+
"cacheRead": 0.1,
|
|
37
|
+
"cacheWrite": {
|
|
38
|
+
"5m": 2.5,
|
|
39
|
+
"1h": 4
|
|
40
|
+
},
|
|
41
|
+
"output": 10
|
|
42
|
+
}
|
|
43
|
+
],
|
|
44
|
+
"contextWindow": 1000000,
|
|
45
|
+
"maxOutputTokens": 128000,
|
|
46
|
+
"images": true,
|
|
47
|
+
"reasoning": true,
|
|
48
|
+
"asOf": "2026-11-19",
|
|
49
|
+
"sources": {
|
|
50
|
+
"rates": "https://platform.claude.com/docs/en/about-claude/pricing",
|
|
51
|
+
"contextWindow": "https://platform.claude.com/docs/en/about-claude/models/overview",
|
|
52
|
+
"maxOutputTokens": "https://platform.claude.com/docs/en/about-claude/models/overview"
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
"provider": "anthropic",
|
|
57
|
+
"id": "claude-haiku-5-5",
|
|
58
|
+
"rates": [
|
|
59
|
+
{
|
|
60
|
+
"upToInputTokens": 100000,
|
|
61
|
+
"uncachedInput": 0.1,
|
|
62
|
+
"cacheRead": 0.01,
|
|
63
|
+
"cacheWrite": {
|
|
64
|
+
"5m": 0.125,
|
|
65
|
+
"1h": 0.2
|
|
66
|
+
},
|
|
67
|
+
"output": 0.5
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
"upToInputTokens": null,
|
|
71
|
+
"uncachedInput": 0.5,
|
|
72
|
+
"cacheRead": 0.05,
|
|
73
|
+
"cacheWrite": {
|
|
74
|
+
"5m": 0.625,
|
|
75
|
+
"1h": 1
|
|
76
|
+
},
|
|
77
|
+
"output": 2.5
|
|
78
|
+
}
|
|
79
|
+
],
|
|
80
|
+
"contextWindow": 1000000,
|
|
81
|
+
"maxOutputTokens": 128000,
|
|
82
|
+
"images": true,
|
|
83
|
+
"reasoning": true,
|
|
84
|
+
"asOf": "2026-11-19",
|
|
85
|
+
"sources": {
|
|
86
|
+
"rates": "https://platform.claude.com/docs/en/about-claude/pricing",
|
|
87
|
+
"contextWindow": "https://platform.claude.com/docs/en/about-claude/models/overview",
|
|
88
|
+
"maxOutputTokens": "https://platform.claude.com/docs/en/about-claude/models/overview"
|
|
89
|
+
},
|
|
90
|
+
"note": "The pricing page's long context section prices a prompt over 100,000 input tokens, cache reads and writes included, at the higher tier."
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
"provider": "openai",
|
|
94
|
+
"id": "gpt-5.4-mini",
|
|
95
|
+
"rates": [
|
|
96
|
+
{
|
|
97
|
+
"upToInputTokens": null,
|
|
98
|
+
"uncachedInput": 0.75,
|
|
99
|
+
"cacheRead": 0.075,
|
|
100
|
+
"cacheWrite": {
|
|
101
|
+
"5m": 0.75,
|
|
102
|
+
"1h": 0.75
|
|
103
|
+
},
|
|
104
|
+
"output": 4.5
|
|
105
|
+
}
|
|
106
|
+
],
|
|
107
|
+
"contextWindow": 272000,
|
|
108
|
+
"maxOutputTokens": 128000,
|
|
109
|
+
"images": true,
|
|
110
|
+
"reasoning": true,
|
|
111
|
+
"asOf": "2026-11-19",
|
|
112
|
+
"sources": {
|
|
113
|
+
"rates": "https://developers.openai.com/api/docs/pricing",
|
|
114
|
+
"contextWindow": "https://developers.openai.com/api/docs/models/gpt-5.4-mini",
|
|
115
|
+
"maxOutputTokens": "https://developers.openai.com/api/docs/models/gpt-5.4-mini"
|
|
116
|
+
},
|
|
117
|
+
"note": "The cache-write rates are the uncached input rate: the prompt caching guide (https://developers.openai.com/api/docs/guides/prompt-caching) lists no additional cache-write charge for models before GPT-5.6. contextWindow records the model page's 272,000-token input limit, not its 400,000-token context window, because a request can send no more input than that."
|
|
118
|
+
}
|
|
119
|
+
]
|
|
120
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `classifyError()`: sorts a failed request into the four error kinds the gateway and the loop act on (SDK decision 4;
|
|
3
|
+
* agent-SDK guide §2). Adapters call it with the HTTP status and the provider's error code or message.
|
|
4
|
+
*/
|
|
5
|
+
import type { AssistantMessage } from "./types.js";
|
|
6
|
+
/** How a failed call is handled: retried, compacted, stopped for billing, or stopped. */
|
|
7
|
+
export type ErrorKind = NonNullable<AssistantMessage["error"]>["kind"];
|
|
8
|
+
/** What an adapter knows about one failed request. A request that never got a response has no `status`. */
|
|
9
|
+
export interface FailedRequest {
|
|
10
|
+
status?: number;
|
|
11
|
+
/** The provider's error code or type, for example `"insufficient_quota"`. */
|
|
12
|
+
code?: string;
|
|
13
|
+
message?: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Returns the error kind for a failed request. A request that got no response is `retryable`, because it failed in
|
|
17
|
+
* transit; a response whose status the gateway doesn't know is `fatal`, so nothing is retried by accident.
|
|
18
|
+
*/
|
|
19
|
+
export declare function classifyError(failure: FailedRequest): ErrorKind;
|
|
20
|
+
/**
|
|
21
|
+
* True for the errors a `fetch` throws when a request gets no response, or when its body breaks off: undici names a
|
|
22
|
+
* failed request "fetch failed" and a body cut short "terminated". Any other throw is a bug or a guard, so the
|
|
23
|
+
* gateway never retries it.
|
|
24
|
+
*/
|
|
25
|
+
export declare function isTransportFailure(thrown: unknown): boolean;
|