@buildanagent/gateway 0.0.0-bootstrap.0 → 0.1.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +52 -0
- package/LICENSE-APACHE +202 -0
- package/LICENSE-MIT +21 -0
- package/README.md +298 -1
- 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 +20 -0
- package/dist/adapters/anthropic/failure.js +46 -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 +31 -0
- package/dist/adapters/anthropic/response.js +212 -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 +25 -0
- package/dist/adapters/openai-chat/failure.js +33 -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 +19 -0
- package/dist/adapters/openai-chat/usage.js +22 -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 +77 -0
- package/dist/adapters/wire.js +137 -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 +42 -0
- package/dist/cost.js +122 -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 +30 -0
- package/dist/retry.js +191 -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 +130 -0
- package/dist/types.js +6 -0
- package/dist/types.js.map +1 -0
- package/dist/usage.d.ts +8 -0
- package/dist/usage.js +20 -0
- package/dist/usage.js.map +1 -0
- package/package.json +53 -3
|
@@ -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;
|
package/dist/classify.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// Quota and billing errors are final even when the status is 429, because waiting does not refill a budget. Only
|
|
2
|
+
// explicit billing codes and phrases count: a rate-limit message that merely mentions "quota" stays retryable.
|
|
3
|
+
// Anthropic's `enforced_spend_limit_reached` is a 429 for a monthly tier threshold (Messages API reference, Errors,
|
|
4
|
+
// https://platform.claude.com/docs/en/api/messages/create, as of November 2026), so waiting does not clear it.
|
|
5
|
+
// OpenAI's billing 429s carry a code that names the cause, and "retrying billing, spend, or quota errors won't restore
|
|
6
|
+
// API access" (Error codes, https://developers.openai.com/api/docs/guides/error-codes, as of November 2026).
|
|
7
|
+
const QUOTA = /insufficient_quota|exceeded your current quota|credit balance is too low|billing_hard_limit|enforced_spend_limit_reached|credit_balance_exhausted|(organization|project)_spend_limit_exceeded|organization_usage_limit_exceeded/i;
|
|
8
|
+
// A per-minute "request too large" 429 is a rate limit, not a full context, so only context phrases count here.
|
|
9
|
+
const OVERFLOW = /context[ _-]?(length|window)|prompt is too long|maximum context/i;
|
|
10
|
+
// Other 5xx statuses, such as 501 and 505, are permanent.
|
|
11
|
+
const RETRYABLE_STATUSES = new Set([408, 409, 425, 429, 500, 502, 503, 504, 529]);
|
|
12
|
+
/**
|
|
13
|
+
* Returns the error kind for a failed request. A request that got no response is `retryable`, because it failed in
|
|
14
|
+
* transit; a response whose status the gateway doesn't know is `fatal`, so nothing is retried by accident.
|
|
15
|
+
*/
|
|
16
|
+
export function classifyError(failure) {
|
|
17
|
+
const text = `${failure.code ?? ""} ${failure.message ?? ""}`;
|
|
18
|
+
if (failure.status === 402 || QUOTA.test(text))
|
|
19
|
+
return "quota";
|
|
20
|
+
if (failure.status === 413 || OVERFLOW.test(text))
|
|
21
|
+
return "overflow";
|
|
22
|
+
// No status means the request failed in transit (a reset connection or DNS failure), which is safe to retry.
|
|
23
|
+
if (failure.status === undefined)
|
|
24
|
+
return "retryable";
|
|
25
|
+
if (RETRYABLE_STATUSES.has(failure.status))
|
|
26
|
+
return "retryable";
|
|
27
|
+
return "fatal";
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* True for the errors a `fetch` throws when a request gets no response, or when its body breaks off: undici names a
|
|
31
|
+
* failed request "fetch failed" and a body cut short "terminated". Any other throw is a bug or a guard, so the
|
|
32
|
+
* gateway never retries it.
|
|
33
|
+
*/
|
|
34
|
+
export function isTransportFailure(thrown) {
|
|
35
|
+
return thrown instanceof TypeError && /fetch|network|socket|connect|terminated/i.test(thrown.message);
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=classify.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"classify.js","sourceRoot":"","sources":["../src/classify.ts"],"names":[],"mappings":"AAiBA,iHAAiH;AACjH,+GAA+G;AAC/G,oHAAoH;AACpH,+GAA+G;AAC/G,uHAAuH;AACvH,6GAA6G;AAC7G,MAAM,KAAK,GACT,kOAAkO,CAAC;AACrO,gHAAgH;AAChH,MAAM,QAAQ,GAAG,kEAAkE,CAAC;AACpF,0DAA0D;AAC1D,MAAM,kBAAkB,GAAG,IAAI,GAAG,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;AAElF;;;GAGG;AACH,MAAM,UAAU,aAAa,CAAC,OAAsB;IAClD,MAAM,IAAI,GAAG,GAAG,OAAO,CAAC,IAAI,IAAI,EAAE,IAAI,OAAO,CAAC,OAAO,IAAI,EAAE,EAAE,CAAC;IAC9D,IAAI,OAAO,CAAC,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC;QAAE,OAAO,OAAO,CAAC;IAC/D,IAAI,OAAO,CAAC,MAAM,KAAK,GAAG,IAAI,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC;QAAE,OAAO,UAAU,CAAC;IACrE,6GAA6G;IAC7G,IAAI,OAAO,CAAC,MAAM,KAAK,SAAS;QAAE,OAAO,WAAW,CAAC;IACrD,IAAI,kBAAkB,CAAC,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC;QAAE,OAAO,WAAW,CAAC;IAC/D,OAAO,OAAO,CAAC;AACjB,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,MAAe;IAChD,OAAO,MAAM,YAAY,SAAS,IAAI,0CAA0C,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;AACxG,CAAC","sourcesContent":["/**\n * `classifyError()`: sorts a failed request into the four error kinds the gateway and the loop act on (SDK decision 4;\n * agent-SDK guide §2). Adapters call it with the HTTP status and the provider's error code or message.\n */\nimport type { AssistantMessage } from \"./types.js\";\n\n/** How a failed call is handled: retried, compacted, stopped for billing, or stopped. */\nexport type ErrorKind = NonNullable<AssistantMessage[\"error\"]>[\"kind\"];\n\n/** What an adapter knows about one failed request. A request that never got a response has no `status`. */\nexport interface FailedRequest {\n status?: number;\n /** The provider's error code or type, for example `\"insufficient_quota\"`. */\n code?: string;\n message?: string;\n}\n\n// Quota and billing errors are final even when the status is 429, because waiting does not refill a budget. Only\n// explicit billing codes and phrases count: a rate-limit message that merely mentions \"quota\" stays retryable.\n// Anthropic's `enforced_spend_limit_reached` is a 429 for a monthly tier threshold (Messages API reference, Errors,\n// https://platform.claude.com/docs/en/api/messages/create, as of November 2026), so waiting does not clear it.\n// OpenAI's billing 429s carry a code that names the cause, and \"retrying billing, spend, or quota errors won't restore\n// API access\" (Error codes, https://developers.openai.com/api/docs/guides/error-codes, as of November 2026).\nconst QUOTA =\n /insufficient_quota|exceeded your current quota|credit balance is too low|billing_hard_limit|enforced_spend_limit_reached|credit_balance_exhausted|(organization|project)_spend_limit_exceeded|organization_usage_limit_exceeded/i;\n// A per-minute \"request too large\" 429 is a rate limit, not a full context, so only context phrases count here.\nconst OVERFLOW = /context[ _-]?(length|window)|prompt is too long|maximum context/i;\n// Other 5xx statuses, such as 501 and 505, are permanent.\nconst RETRYABLE_STATUSES = new Set([408, 409, 425, 429, 500, 502, 503, 504, 529]);\n\n/**\n * Returns the error kind for a failed request. A request that got no response is `retryable`, because it failed in\n * transit; a response whose status the gateway doesn't know is `fatal`, so nothing is retried by accident.\n */\nexport function classifyError(failure: FailedRequest): ErrorKind {\n const text = `${failure.code ?? \"\"} ${failure.message ?? \"\"}`;\n if (failure.status === 402 || QUOTA.test(text)) return \"quota\";\n if (failure.status === 413 || OVERFLOW.test(text)) return \"overflow\";\n // No status means the request failed in transit (a reset connection or DNS failure), which is safe to retry.\n if (failure.status === undefined) return \"retryable\";\n if (RETRYABLE_STATUSES.has(failure.status)) return \"retryable\";\n return \"fatal\";\n}\n\n/**\n * True for the errors a `fetch` throws when a request gets no response, or when its body breaks off: undici names a\n * failed request \"fetch failed\" and a body cut short \"terminated\". Any other throw is a bug or a guard, so the\n * gateway never retries it.\n */\nexport function isTransportFailure(thrown: unknown): boolean {\n return thrown instanceof TypeError && /fetch|network|socket|connect|terminated/i.test(thrown.message);\n}\n"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** `complete()`: folds a model stream into its final message (SDK plan §3.2). */
|
|
2
|
+
import type { GatewayEvent } from "./runtime-types.js";
|
|
3
|
+
import type { AssistantMessage } from "./types.js";
|
|
4
|
+
/**
|
|
5
|
+
* Reads the stream to its terminal event and returns that event's message. An `error` terminal is returned, not
|
|
6
|
+
* thrown: its message carries `stopReason` `"error"` or `"aborted"`, the partial content and the usage so far. A stream
|
|
7
|
+
* that ends without a terminal event breaks the stream rule, so that case throws. `retry` events are skipped.
|
|
8
|
+
*/
|
|
9
|
+
export declare function complete(events: AsyncIterable<GatewayEvent>): Promise<AssistantMessage>;
|
package/dist/complete.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reads the stream to its terminal event and returns that event's message. An `error` terminal is returned, not
|
|
3
|
+
* thrown: its message carries `stopReason` `"error"` or `"aborted"`, the partial content and the usage so far. A stream
|
|
4
|
+
* that ends without a terminal event breaks the stream rule, so that case throws. `retry` events are skipped.
|
|
5
|
+
*/
|
|
6
|
+
export async function complete(events) {
|
|
7
|
+
for await (const event of events) {
|
|
8
|
+
if (event.type === "done" || event.type === "error")
|
|
9
|
+
return event.message;
|
|
10
|
+
}
|
|
11
|
+
throw new Error("the model stream ended without a done or error event");
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=complete.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"complete.js","sourceRoot":"","sources":["../src/complete.ts"],"names":[],"mappings":"AAIA;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,MAAmC;IAChE,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QACjC,IAAI,KAAK,CAAC,IAAI,KAAK,MAAM,IAAI,KAAK,CAAC,IAAI,KAAK,OAAO;YAAE,OAAO,KAAK,CAAC,OAAO,CAAC;IAC5E,CAAC;IACD,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAC;AAC1E,CAAC","sourcesContent":["/** `complete()`: folds a model stream into its final message (SDK plan §3.2). */\nimport type { GatewayEvent } from \"./runtime-types.js\";\nimport type { AssistantMessage } from \"./types.js\";\n\n/**\n * Reads the stream to its terminal event and returns that event's message. An `error` terminal is returned, not\n * thrown: its message carries `stopReason` `\"error\"` or `\"aborted\"`, the partial content and the usage so far. A stream\n * that ends without a terminal event breaks the stream rule, so that case throws. `retry` events are skipped.\n */\nexport async function complete(events: AsyncIterable<GatewayEvent>): Promise<AssistantMessage> {\n for await (const event of events) {\n if (event.type === \"done\" || event.type === \"error\") return event.message;\n }\n throw new Error(\"the model stream ended without a done or error event\");\n}\n"]}
|
package/dist/cost.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pricing usage against the catalog, and projecting a call's cost before it is sent (D3, D4, D9; ADR 0043).
|
|
3
|
+
*
|
|
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 has an unknown price, never a guessed zero.
|
|
6
|
+
*/
|
|
7
|
+
import type { CatalogEntry } from "./catalog.js";
|
|
8
|
+
import type { Model, ModelCallOptions } from "./runtime-types.js";
|
|
9
|
+
import type { AssistantMessage, Cost, ModelContext, Usage } from "./types.js";
|
|
10
|
+
/**
|
|
11
|
+
* 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, the result is `{ total: null, basis: "unknown" }`.
|
|
13
|
+
*/
|
|
14
|
+
export declare function priceUsage(usage: Usage, entry: CatalogEntry | undefined): Cost;
|
|
15
|
+
/** The message's cost: the provider's billed total when the adapter reported one, else `price(usage)`. */
|
|
16
|
+
export declare function foldCost(message: AssistantMessage, price: (usage: Usage) => Cost): Cost;
|
|
17
|
+
/** The token counts a projection prices: the input to send and the most output the call may produce. */
|
|
18
|
+
export interface ProjectedTokens {
|
|
19
|
+
readonly inputTokens: number;
|
|
20
|
+
readonly maxOutputTokens: number;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The most one call can cost: every input token at the highest input rate in the tier the input selects (the
|
|
24
|
+
* uncached rate or either cache-write rate, whichever costs most), plus `maxOutputTokens` at the output rate. Without
|
|
25
|
+
* an entry, the result is `{ total: null, basis: "unknown" }`.
|
|
26
|
+
*/
|
|
27
|
+
export declare function projectCost(tokens: ProjectedTokens, entry: CatalogEntry | undefined): Cost;
|
|
28
|
+
/**
|
|
29
|
+
* Estimates the input tokens of sending `context`. When an earlier assistant message reports its usage, the estimate
|
|
30
|
+
* is that request's total input and output plus a character-based estimate of the messages after it; otherwise every
|
|
31
|
+
* part of the context is estimated from its characters. A retried call counts only its last attempt, through
|
|
32
|
+
* `lastAttemptUsage`, because its summed `usage` would count the same prompt once per attempt. The reported usage
|
|
33
|
+
* stands in for the system prompt and tools, so the estimate assumes they are unchanged since that request: if you
|
|
34
|
+
* grow them, the growth is not counted. Otherwise the estimate errs high, so a budget check errs towards stopping.
|
|
35
|
+
*/
|
|
36
|
+
export declare function estimateInputTokens(context: ModelContext): number;
|
|
37
|
+
/**
|
|
38
|
+
* The most the next call can cost, priced as `projectCost()` prices it: over `estimateInputTokens(context)` and the
|
|
39
|
+
* call's `maxOutputTokens`, or the model's own output limit when the options leave it out. Priced with the model's catalog
|
|
40
|
+
* entry, so a model the catalog lacks projects `{ total: null, basis: "unknown" }`.
|
|
41
|
+
*/
|
|
42
|
+
export declare function projectCallCost(model: Model, context: ModelContext, options?: ModelCallOptions): Cost;
|
package/dist/cost.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { emptyUsage, totalInput } from "./usage.js";
|
|
2
|
+
const PER_MILLION = 1_000_000;
|
|
3
|
+
/** The cost of a model the catalog doesn't list. */
|
|
4
|
+
const UNKNOWN_COST = { total: null, basis: "unknown", currency: "USD" };
|
|
5
|
+
/** The first tier whose limit covers `inputTokens`; the catalog check guarantees the last tier has no limit. */
|
|
6
|
+
function tierFor(rates, inputTokens) {
|
|
7
|
+
return rates.find((tier) => tier.upToInputTokens === null || inputTokens <= tier.upToInputTokens);
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
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, the result is `{ total: null, basis: "unknown" }`.
|
|
12
|
+
*/
|
|
13
|
+
export function priceUsage(usage, entry) {
|
|
14
|
+
const tier = entry ? tierFor(entry.rates, totalInput(usage)) : undefined;
|
|
15
|
+
if (entry === undefined || tier === undefined)
|
|
16
|
+
return { ...UNKNOWN_COST };
|
|
17
|
+
const total = (usage.uncachedInput * tier.uncachedInput +
|
|
18
|
+
usage.cacheRead * tier.cacheRead +
|
|
19
|
+
usage.cacheWrite["5m"] * tier.cacheWrite["5m"] +
|
|
20
|
+
usage.cacheWrite["1h"] * tier.cacheWrite["1h"] +
|
|
21
|
+
usage.output * tier.output) /
|
|
22
|
+
PER_MILLION;
|
|
23
|
+
return { total, basis: "catalog", currency: "USD", catalogAsOf: entry.asOf };
|
|
24
|
+
}
|
|
25
|
+
/** The message's cost: the provider's billed total when the adapter reported one, else `price(usage)`. */
|
|
26
|
+
export function foldCost(message, price) {
|
|
27
|
+
if (message.cost.basis === "provider" && message.cost.total !== null)
|
|
28
|
+
return message.cost;
|
|
29
|
+
return price(message.usage);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The most one call can cost: every input token at the highest input rate in the tier the input selects (the
|
|
33
|
+
* uncached rate or either cache-write rate, whichever costs most), plus `maxOutputTokens` at the output rate. Without
|
|
34
|
+
* an entry, the result is `{ total: null, basis: "unknown" }`.
|
|
35
|
+
*/
|
|
36
|
+
export function projectCost(tokens, entry) {
|
|
37
|
+
return projectWith(tokens, (usage) => priceUsage(usage, entry));
|
|
38
|
+
}
|
|
39
|
+
// Every input token is put in one class at a time and the dearest result wins. The tier depends only on total input,
|
|
40
|
+
// which is the same in each candidate, so this prices the input at the tier's highest input rate without reading the
|
|
41
|
+
// rates themselves; `Model.price()` keeps its catalog entry private.
|
|
42
|
+
function projectWith(tokens, price) {
|
|
43
|
+
const base = { ...emptyUsage(), output: tokens.maxOutputTokens };
|
|
44
|
+
const n = tokens.inputTokens;
|
|
45
|
+
const candidates = [
|
|
46
|
+
price({ ...base, uncachedInput: n }),
|
|
47
|
+
price({ ...base, cacheWrite: { "5m": n, "1h": 0 } }),
|
|
48
|
+
price({ ...base, cacheWrite: { "5m": 0, "1h": n } }),
|
|
49
|
+
];
|
|
50
|
+
return candidates.reduce((most, cost) => ((cost.total ?? -1) > (most.total ?? -1) ? cost : most));
|
|
51
|
+
}
|
|
52
|
+
// Anthropic's models overview puts 1M tokens at roughly 2.5M Unicode characters on its current tokenizer, as of
|
|
53
|
+
// November 2026. Other tokenizers fit more characters in a token, so this rate errs towards a higher estimate.
|
|
54
|
+
const CHARS_PER_TOKEN = 2.5;
|
|
55
|
+
// The largest per-image cost a first provider documents: Anthropic's high-resolution limit of 4,784 visual tokens
|
|
56
|
+
// (vision docs, as of November 2026). Counting an image's base64 data instead would overstate it a hundredfold.
|
|
57
|
+
const TOKENS_PER_IMAGE = 4784;
|
|
58
|
+
function blockChars(block) {
|
|
59
|
+
switch (block.type) {
|
|
60
|
+
case "text":
|
|
61
|
+
case "reasoning":
|
|
62
|
+
return block.text.length;
|
|
63
|
+
case "tool_call":
|
|
64
|
+
return block.name.length + JSON.stringify(block.input ?? null).length;
|
|
65
|
+
case "image":
|
|
66
|
+
return 0;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
function messageEstimate(message) {
|
|
70
|
+
const blocks = message.content;
|
|
71
|
+
const chars = blocks.reduce((sum, block) => sum + blockChars(block), 0);
|
|
72
|
+
const images = blocks.filter((block) => block.type === "image").length;
|
|
73
|
+
return chars / CHARS_PER_TOKEN + images * TOKENS_PER_IMAGE;
|
|
74
|
+
}
|
|
75
|
+
function promptEstimate(context) {
|
|
76
|
+
const tools = context.tools.reduce((sum, tool) => sum + tool.name.length + tool.description.length + JSON.stringify(tool.inputSchema).length, 0);
|
|
77
|
+
return (context.system.length + tools) / CHARS_PER_TOKEN;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* One request's usage: a retried call's `usage` sums every attempt, so its last attempt's usage alone gives the size
|
|
81
|
+
* of the request that produced the message.
|
|
82
|
+
*/
|
|
83
|
+
function requestUsage(message) {
|
|
84
|
+
return message.lastAttemptUsage ?? message.usage;
|
|
85
|
+
}
|
|
86
|
+
/** The index of the last assistant message whose request reports any input, or -1 when there is none. */
|
|
87
|
+
function lastReportedCall(messages) {
|
|
88
|
+
return messages.findLastIndex((m) => m.role === "assistant" && totalInput(requestUsage(m)) > 0);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Estimates the input tokens of sending `context`. When an earlier assistant message reports its usage, the estimate
|
|
92
|
+
* is that request's total input and output plus a character-based estimate of the messages after it; otherwise every
|
|
93
|
+
* part of the context is estimated from its characters. A retried call counts only its last attempt, through
|
|
94
|
+
* `lastAttemptUsage`, because its summed `usage` would count the same prompt once per attempt. The reported usage
|
|
95
|
+
* stands in for the system prompt and tools, so the estimate assumes they are unchanged since that request: if you
|
|
96
|
+
* grow them, the growth is not counted. Otherwise the estimate errs high, so a budget check errs towards stopping.
|
|
97
|
+
*/
|
|
98
|
+
export function estimateInputTokens(context) {
|
|
99
|
+
const last = lastReportedCall(context.messages);
|
|
100
|
+
const previous = context.messages[last];
|
|
101
|
+
if (previous?.role !== "assistant") {
|
|
102
|
+
const messages = context.messages.reduce((sum, m) => sum + messageEstimate(m), 0);
|
|
103
|
+
return Math.ceil(promptEstimate(context) + messages);
|
|
104
|
+
}
|
|
105
|
+
// The reported output stands in for the assistant message's own content, which the next call sends as input.
|
|
106
|
+
const added = context.messages.slice(last + 1).reduce((sum, m) => sum + messageEstimate(m), 0);
|
|
107
|
+
const usage = requestUsage(previous);
|
|
108
|
+
return Math.ceil(totalInput(usage) + usage.output + added);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* The most the next call can cost, priced as `projectCost()` prices it: over `estimateInputTokens(context)` and the
|
|
112
|
+
* call's `maxOutputTokens`, or the model's own output limit when the options leave it out. Priced with the model's catalog
|
|
113
|
+
* entry, so a model the catalog lacks projects `{ total: null, basis: "unknown" }`.
|
|
114
|
+
*/
|
|
115
|
+
export function projectCallCost(model, context, options = {}) {
|
|
116
|
+
const tokens = {
|
|
117
|
+
inputTokens: estimateInputTokens(context),
|
|
118
|
+
maxOutputTokens: options.maxOutputTokens ?? model.caps.maxOutputTokens,
|
|
119
|
+
};
|
|
120
|
+
return projectWith(tokens, (usage) => model.price(usage));
|
|
121
|
+
}
|
|
122
|
+
//# sourceMappingURL=cost.js.map
|
package/dist/cost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C,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"]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { CreateModelOptions, Model } from "./runtime-types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a model from an adapter object and a model id. Every call through it is retried, wrapped in middleware
|
|
4
|
+
* and dumped the same way, whatever the adapter. Throws a `GatewayError` for bad options, including a malformed
|
|
5
|
+
* `catalog` (code `invalid_catalog`).
|
|
6
|
+
*/
|
|
7
|
+
export declare function createModel(options: CreateModelOptions): Model;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `createModel()`: binds an adapter to one model id and wraps it in the gateway's shared behaviour (SDK plan §3.2;
|
|
3
|
+
* SDK decisions 4, 13). The order, outside in: middleware, then retries, then the request dump, then the adapter.
|
|
4
|
+
*/
|
|
5
|
+
import { defaultCatalog, findCatalogEntry, parseCatalog } from "./catalog.js";
|
|
6
|
+
import { complete } from "./complete.js";
|
|
7
|
+
import { priceUsage } from "./cost.js";
|
|
8
|
+
import { GatewayError } from "./errors.js";
|
|
9
|
+
import { transformParams, wrapStream } from "./middleware.js";
|
|
10
|
+
import { startAttempt } from "./request-dump.js";
|
|
11
|
+
import { abortableSleep, withRetries } from "./retry.js";
|
|
12
|
+
const DEFAULT_RETRIES = 2;
|
|
13
|
+
const DEFAULT_INITIAL_DELAY_MS = 2000;
|
|
14
|
+
function assertAdapter(adapter) {
|
|
15
|
+
const candidate = adapter;
|
|
16
|
+
if (typeof candidate !== "object" || candidate === null || typeof candidate.stream !== "function") {
|
|
17
|
+
// A string here is usually a bare model id; the gateway never resolves one to a provider.
|
|
18
|
+
throw new GatewayError("invalid_options", "createModel() needs an adapter object, such as anthropic({ apiKey }).");
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function nonNegativeInteger(name, value, fallback) {
|
|
22
|
+
if (value === undefined)
|
|
23
|
+
return fallback;
|
|
24
|
+
if (!Number.isInteger(value) || value < 0) {
|
|
25
|
+
throw new GatewayError("invalid_options", `createModel(): ${name} must be a non-negative integer, got ${value}`);
|
|
26
|
+
}
|
|
27
|
+
return value;
|
|
28
|
+
}
|
|
29
|
+
/** The catalog entry's limits, with the adapter's defaults for anything the entry leaves out. */
|
|
30
|
+
function resolveCaps(adapter, id, entry) {
|
|
31
|
+
const defaults = adapter.defaultCaps;
|
|
32
|
+
if (entry) {
|
|
33
|
+
return {
|
|
34
|
+
contextWindow: entry.contextWindow,
|
|
35
|
+
maxOutputTokens: entry.maxOutputTokens,
|
|
36
|
+
// Without a record either way, assume no support: sending an image or reasoning option a model rejects fails.
|
|
37
|
+
images: entry.images ?? defaults?.images ?? false,
|
|
38
|
+
reasoning: entry.reasoning ?? defaults?.reasoning ?? false,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
if (defaults)
|
|
42
|
+
return { ...defaults };
|
|
43
|
+
throw new GatewayError("missing_caps", `createModel(): ${adapter.provider}/${id} is not in the catalog and the adapter has no default caps`);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Creates a model from an adapter object and a model id. Every call through it is retried, wrapped in middleware
|
|
47
|
+
* and dumped the same way, whatever the adapter. Throws a `GatewayError` for bad options, including a malformed
|
|
48
|
+
* `catalog` (code `invalid_catalog`).
|
|
49
|
+
*/
|
|
50
|
+
export function createModel(options) {
|
|
51
|
+
const { adapter, id } = options;
|
|
52
|
+
assertAdapter(adapter);
|
|
53
|
+
const ref = { api: adapter.api, provider: adapter.provider, id };
|
|
54
|
+
// A caller's catalog is checked like a JSON one: pricing relies on ascending tiers, an open last tier and
|
|
55
|
+
// finite, non-negative rates, and a typed object can still break them.
|
|
56
|
+
const catalog = options.catalog === undefined ? defaultCatalog : parseCatalog(options.catalog);
|
|
57
|
+
const entry = findCatalogEntry(catalog, adapter.provider, id);
|
|
58
|
+
const caps = resolveCaps(adapter, id, entry);
|
|
59
|
+
const price = (usage) => priceUsage(usage, entry);
|
|
60
|
+
const middleware = [...(options.middleware ?? [])];
|
|
61
|
+
const settings = {
|
|
62
|
+
retries: nonNegativeInteger("retries", options.retries, DEFAULT_RETRIES),
|
|
63
|
+
initialDelayMs: nonNegativeInteger("initialRetryDelayMs", options.initialRetryDelayMs, DEFAULT_INITIAL_DELAY_MS),
|
|
64
|
+
sleep: options.sleep ?? abortableSleep,
|
|
65
|
+
price,
|
|
66
|
+
};
|
|
67
|
+
const onRequest = options.onRequest?.bind(options);
|
|
68
|
+
async function* stream(context, streamOptions = {}) {
|
|
69
|
+
const params = await transformParams(middleware, { context, options: streamOptions });
|
|
70
|
+
const request = { adapter, ref, context: params.context, options: params.options, caps, onRequest };
|
|
71
|
+
yield* wrapStream(middleware, params, () => withRetries((n) => startAttempt(request, n), ref, params.options.signal, settings));
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
ref,
|
|
75
|
+
caps,
|
|
76
|
+
stream,
|
|
77
|
+
complete: (context, streamOptions) => complete(stream(context, streamOptions)),
|
|
78
|
+
price,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=create-model.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-model.js","sourceRoot":"","sources":["../src/create-model.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAqB,cAAc,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AACjG,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC9D,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,cAAc,EAAsB,WAAW,EAAE,MAAM,YAAY,CAAC;AAK7E,MAAM,eAAe,GAAG,CAAC,CAAC;AAC1B,MAAM,wBAAwB,GAAG,IAAI,CAAC;AAEtC,SAAS,aAAa,CAAC,OAAgB;IACrC,MAAM,SAAS,GAAG,OAAuC,CAAC;IAC1D,IAAI,OAAO,SAAS,KAAK,QAAQ,IAAI,SAAS,KAAK,IAAI,IAAI,OAAO,SAAS,CAAC,MAAM,KAAK,UAAU,EAAE,CAAC;QAClG,0FAA0F;QAC1F,MAAM,IAAI,YAAY,CAAC,iBAAiB,EAAE,uEAAuE,CAAC,CAAC;IACrH,CAAC;AACH,CAAC;AAED,SAAS,kBAAkB,CAAC,IAAY,EAAE,KAAyB,EAAE,QAAgB;IACnF,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO,QAAQ,CAAC;IACzC,IAAI,CAAC,MAAM,CA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C,OAAO,CAAC,MAAM,EAAE,QAAQ,CAAC,CACnF,CAAC;IACJ,CAAC;IAED,OAAO;QACL,GAAG;QACH,IAAI;QACJ,MAAM;QACN,QAAQ,EAAE,CAAC,OAAO,EAAE,aAAa,EAAE,EAAE,CAAC,QAAQ,CAAC,MAAM,CAAC,OAAO,EAAE,aAAa,CAAC,CAAC;QAC9E,KAAK;KACN,CAAC;AACJ,CAAC","sourcesContent":["/**\n * `createModel()`: binds an adapter to one model id and wraps it in the gateway's shared behaviour (SDK plan §3.2;\n * SDK decisions 4, 13). The order, outside in: middleware, then retries, then the request dump, then the adapter.\n */\nimport { type CatalogEntry, defaultCatalog, findCatalogEntry, parseCatalog } from \"./catalog.js\";\nimport { complete } from \"./complete.js\";\nimport { priceUsage } from \"./cost.js\";\nimport { GatewayError } from \"./errors.js\";\nimport { transformParams, wrapStream } from \"./middleware.js\";\nimport { startAttempt } from \"./request-dump.js\";\nimport { abortableSleep, type RetrySettings, withRetries } from \"./retry.js\";\nimport type { CreateModelOptions, GatewayEvent, Model, ModelCallOptions } from \"./runtime-types.js\";\nimport type { ModelAdapter } from \"./spec.js\";\nimport type { ModelCaps, ModelContext, Usage } from \"./types.js\";\n\nconst DEFAULT_RETRIES = 2;\nconst DEFAULT_INITIAL_DELAY_MS = 2000;\n\nfunction assertAdapter(adapter: unknown): asserts adapter is ModelAdapter {\n const candidate = adapter as Partial<ModelAdapter> | null;\n if (typeof candidate !== \"object\" || candidate === null || typeof candidate.stream !== \"function\") {\n // A string here is usually a bare model id; the gateway never resolves one to a provider.\n throw new GatewayError(\"invalid_options\", \"createModel() needs an adapter object, such as anthropic({ apiKey }).\");\n }\n}\n\nfunction nonNegativeInteger(name: string, value: number | undefined, fallback: number): number {\n if (value === undefined) return fallback;\n if (!Number.isInteger(value) || value < 0) {\n throw new GatewayError(\"invalid_options\", `createModel(): ${name} must be a non-negative integer, got ${value}`);\n }\n return value;\n}\n\n/** The catalog entry's limits, with the adapter's defaults for anything the entry leaves out. */\nfunction resolveCaps(adapter: ModelAdapter, id: string, entry: CatalogEntry | undefined): ModelCaps {\n const defaults = adapter.defaultCaps;\n if (entry) {\n return {\n contextWindow: entry.contextWindow,\n maxOutputTokens: entry.maxOutputTokens,\n // Without a record either way, assume no support: sending an image or reasoning option a model rejects fails.\n images: entry.images ?? defaults?.images ?? false,\n reasoning: entry.reasoning ?? defaults?.reasoning ?? false,\n };\n }\n if (defaults) return { ...defaults };\n throw new GatewayError(\n \"missing_caps\",\n `createModel(): ${adapter.provider}/${id} is not in the catalog and the adapter has no default caps`,\n );\n}\n\n/**\n * Creates a model from an adapter object and a model id. Every call through it is retried, wrapped in middleware\n * and dumped the same way, whatever the adapter. Throws a `GatewayError` for bad options, including a malformed\n * `catalog` (code `invalid_catalog`).\n */\nexport function createModel(options: CreateModelOptions): Model {\n const { adapter, id } = options;\n assertAdapter(adapter);\n const ref = { api: adapter.api, provider: adapter.provider, id };\n // A caller's catalog is checked like a JSON one: pricing relies on ascending tiers, an open last tier and\n // finite, non-negative rates, and a typed object can still break them.\n const catalog = options.catalog === undefined ? defaultCatalog : parseCatalog(options.catalog);\n const entry = findCatalogEntry(catalog, adapter.provider, id);\n const caps = resolveCaps(adapter, id, entry);\n const price = (usage: Usage) => priceUsage(usage, entry);\n const middleware = [...(options.middleware ?? [])];\n const settings: RetrySettings = {\n retries: nonNegativeInteger(\"retries\", options.retries, DEFAULT_RETRIES),\n initialDelayMs: nonNegativeInteger(\"initialRetryDelayMs\", options.initialRetryDelayMs, DEFAULT_INITIAL_DELAY_MS),\n sleep: options.sleep ?? abortableSleep,\n price,\n };\n const onRequest = options.onRequest?.bind(options);\n\n async function* stream(context: ModelContext, streamOptions: ModelCallOptions = {}): AsyncGenerator<GatewayEvent> {\n const params = await transformParams(middleware, { context, options: streamOptions });\n const request = { adapter, ref, context: params.context, options: params.options, caps, onRequest };\n yield* wrapStream(middleware, params, () =>\n withRetries((n) => startAttempt(request, n), ref, params.options.signal, settings),\n );\n }\n\n return {\n ref,\n caps,\n stream,\n complete: (context, streamOptions) => complete(stream(context, streamOptions)),\n price,\n };\n}\n"]}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `GatewayError`: the gateway's error for programmer errors, such as a missing key, invalid options, a malformed
|
|
3
|
+
* catalog, a tool name outside the canonical rule, or a request a mock transport's script did not expect.
|
|
4
|
+
*/
|
|
5
|
+
/** Why a `GatewayError` was thrown. */
|
|
6
|
+
export type GatewayErrorCode = "missing_api_key" | "missing_caps" | "invalid_options" | "invalid_catalog" | "invalid_tool_name" | "unexpected_request";
|
|
7
|
+
/**
|
|
8
|
+
* Thrown for a mistake in how the gateway is called. A failed model call is never thrown: it ends its stream with an
|
|
9
|
+
* `error` event instead.
|
|
10
|
+
*/
|
|
11
|
+
export declare class GatewayError extends Error {
|
|
12
|
+
readonly name = "GatewayError";
|
|
13
|
+
readonly code: GatewayErrorCode;
|
|
14
|
+
constructor(code: GatewayErrorCode, message: string, options?: ErrorOptions);
|
|
15
|
+
}
|
|
16
|
+
/** True for a `GatewayError`, including one from another copy of the package. */
|
|
17
|
+
export declare function isGatewayError(error: unknown): error is GatewayError;
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `GatewayError`: the gateway's error for programmer errors, such as a missing key, invalid options, a malformed
|
|
3
|
+
* catalog, a tool name outside the canonical rule, or a request a mock transport's script did not expect.
|
|
4
|
+
*/
|
|
5
|
+
// A registered symbol, so the guard still works when two copies of the package are bundled.
|
|
6
|
+
const GATEWAY_ERROR = Symbol.for("buildanagent.gateway.GatewayError");
|
|
7
|
+
/**
|
|
8
|
+
* Thrown for a mistake in how the gateway is called. A failed model call is never thrown: it ends its stream with an
|
|
9
|
+
* `error` event instead.
|
|
10
|
+
*/
|
|
11
|
+
export class GatewayError extends Error {
|
|
12
|
+
name = "GatewayError";
|
|
13
|
+
code;
|
|
14
|
+
constructor(code, message, options) {
|
|
15
|
+
super(message, options);
|
|
16
|
+
this.code = code;
|
|
17
|
+
Object.defineProperty(this, GATEWAY_ERROR, { value: true });
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** True for a `GatewayError`, including one from another copy of the package. */
|
|
21
|
+
export function isGatewayError(error) {
|
|
22
|
+
return typeof error === "object" && error !== null && error[GATEWAY_ERROR] === true;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAWH,4FAA4F;AAC5F,MAAM,aAAa,GAAG,MAAM,CAAC,GAAG,CAAC,mCAAmC,CAAC,CAAC;AAEtE;;;GAGG;AACH,MAAM,OAAO,YAAa,SAAQ,KAAK;IACnB,IAAI,GAAG,cAAc,CAAC;IAC/B,IAAI,CAAmB;IAEhC,YAAY,IAAsB,EAAE,OAAe,EAAE,OAAsB;QACzE,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,aAAa,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;IAC9D,CAAC;CACF;AAED,iFAAiF;AACjF,MAAM,UAAU,cAAc,CAAC,KAAc;IAC3C,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAK,KAAiC,CAAC,aAAa,CAAC,KAAK,IAAI,CAAC;AACnH,CAAC","sourcesContent":["/**\n * `GatewayError`: the gateway's error for programmer errors, such as a missing key, invalid options, a malformed\n * catalog, a tool name outside the canonical rule, or a request a mock transport's script did not expect.\n */\n\n/** Why a `GatewayError` was thrown. */\nexport type GatewayErrorCode =\n | \"missing_api_key\"\n | \"missing_caps\"\n | \"invalid_options\"\n | \"invalid_catalog\"\n | \"invalid_tool_name\"\n | \"unexpected_request\";\n\n// A registered symbol, so the guard still works when two copies of the package are bundled.\nconst GATEWAY_ERROR = Symbol.for(\"buildanagent.gateway.GatewayError\");\n\n/**\n * Thrown for a mistake in how the gateway is called. A failed model call is never thrown: it ends its stream with an\n * `error` event instead.\n */\nexport class GatewayError extends Error {\n override readonly name = \"GatewayError\";\n readonly code: GatewayErrorCode;\n\n constructor(code: GatewayErrorCode, message: string, options?: ErrorOptions) {\n super(message, options);\n this.code = code;\n Object.defineProperty(this, GATEWAY_ERROR, { value: true });\n }\n}\n\n/** True for a `GatewayError`, including one from another copy of the package. */\nexport function isGatewayError(error: unknown): error is GatewayError {\n return typeof error === \"object\" && error !== null && (error as Record<symbol, unknown>)[GATEWAY_ERROR] === true;\n}\n"]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@buildanagent/gateway`: the model gateway's transcript, model spec, stream rule, runtime, price catalog and
|
|
3
|
+
* helpers.
|
|
4
|
+
*/
|
|
5
|
+
export { type CatalogEntry, type CatalogSources, defaultCatalog, findCatalogEntry, type PriceCatalog, parseCatalog, type RateTier, staleCatalogEntries, } from "./catalog.js";
|
|
6
|
+
export { classifyError, type ErrorKind, type FailedRequest } from "./classify.js";
|
|
7
|
+
export { complete } from "./complete.js";
|
|
8
|
+
export { estimateInputTokens, type ProjectedTokens, priceUsage, projectCallCost, projectCost } from "./cost.js";
|
|
9
|
+
export { createModel } from "./create-model.js";
|
|
10
|
+
export { GatewayError, type GatewayErrorCode, isGatewayError } from "./errors.js";
|
|
11
|
+
export { assertCanonicalName, CANONICAL_TOOL_NAME, createToolNameMap, isCanonicalName, TOOL_NAME_RULES, type ToolNameMap, type ToolNameRule, } from "./names.js";
|
|
12
|
+
export { type ResolvedProviderConfig, resolveProviderConfig } from "./provider-config.js";
|
|
13
|
+
export { assertModelRequestAllowed, isMockTransport, ModelRequestsDisabledError, modelRequestsAreAllowed, } from "./request-policy.js";
|
|
14
|
+
export type { CreateModelOptions, DumpedOptions, GatewayEvent, MiddlewareParams, Model, ModelCallOptions, ModelMiddleware, ProviderConfig, RequestDump, Sleep, Transport, } from "./runtime-types.js";
|
|
15
|
+
export type { ModelAdapter, ModelEvent, StreamOptions } from "./spec.js";
|
|
16
|
+
export { assertStreamContract, StreamContractError } from "./stream-contract.js";
|
|
17
|
+
export { transformMessages } from "./transform-messages.js";
|
|
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";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@buildanagent/gateway`: the model gateway's transcript, model spec, stream rule, runtime, price catalog and
|
|
3
|
+
* helpers.
|
|
4
|
+
*/
|
|
5
|
+
export { defaultCatalog, findCatalogEntry, parseCatalog, staleCatalogEntries, } from "./catalog.js";
|
|
6
|
+
export { classifyError } from "./classify.js";
|
|
7
|
+
export { complete } from "./complete.js";
|
|
8
|
+
export { estimateInputTokens, priceUsage, projectCallCost, projectCost } from "./cost.js";
|
|
9
|
+
export { createModel } from "./create-model.js";
|
|
10
|
+
export { GatewayError, isGatewayError } from "./errors.js";
|
|
11
|
+
export { assertCanonicalName, CANONICAL_TOOL_NAME, createToolNameMap, isCanonicalName, TOOL_NAME_RULES, } from "./names.js";
|
|
12
|
+
export { resolveProviderConfig } from "./provider-config.js";
|
|
13
|
+
export { assertModelRequestAllowed, isMockTransport, ModelRequestsDisabledError, modelRequestsAreAllowed, } from "./request-policy.js";
|
|
14
|
+
export { assertStreamContract, StreamContractError } from "./stream-contract.js";
|
|
15
|
+
export { transformMessages } from "./transform-messages.js";
|
|
16
|
+
export { addUsage, emptyUsage, totalInput } from "./usage.js";
|
|
17
|
+
//# sourceMappingURL=index.js.map
|