@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,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lowers a tool's JSON Schema to the subset a provider's strict tool mode accepts, and undoes the one change the
|
|
3
|
+
* model can see: the `null` that a nullable dialect adds to an optional property. Each adapter passes its own
|
|
4
|
+
* `StrictDialect`; the rules both first providers share are these:
|
|
5
|
+
*
|
|
6
|
+
* - every object needs `additionalProperties: false`, and an object with any other `additionalProperties` cannot be
|
|
7
|
+
* strict. An object that declares neither `properties` nor `additionalProperties` accepts any keys, and adding
|
|
8
|
+
* `additionalProperties: false` would let the model send only `{}`, so it cannot be strict either;
|
|
9
|
+
* - `not` is unsupported, and `oneOf` must be written as `anyOf`;
|
|
10
|
+
* - numeric constraints, string length constraints and array constraints beyond `minItems` of 0 or 1 are dropped,
|
|
11
|
+
* unless the dialect keeps them. Anthropic rejects them (Structured outputs, "JSON Schema limitations",
|
|
12
|
+
* https://platform.claude.com/docs/en/build-with-claude/structured-outputs, as of November 2026).
|
|
13
|
+
*
|
|
14
|
+
* OpenAI's strict mode requires every property (Structured model outputs, "Supported schemas",
|
|
15
|
+
* https://developers.openai.com/api/docs/guides/structured-outputs#supported-schemas, as of November 2026). It
|
|
16
|
+
* accepts the numeric constraints and `minItems`/`maxItems`, rejects `allOf`, `if`/`then`/`else` and the
|
|
17
|
+
* `dependent*` keywords, and limits a schema's size, for example to 5,000 properties and 10 levels of nesting. Its
|
|
18
|
+
* dialect therefore writes an optional property as a required one that also accepts `null`. The removed constraints
|
|
19
|
+
* are not lost: the SDK validates every call against the original schema, after `stripAddedNulls()`.
|
|
20
|
+
*/
|
|
21
|
+
type Json = Record<string, unknown>;
|
|
22
|
+
/** How one provider's strict mode differs from the shared rules above. */
|
|
23
|
+
export interface StrictDialect {
|
|
24
|
+
/** `"keep"` leaves optional properties optional; `"nullable"` requires them and lets them be `null`. */
|
|
25
|
+
readonly optional: "keep" | "nullable";
|
|
26
|
+
/** The string formats the provider accepts; any other `format` is dropped. */
|
|
27
|
+
readonly formats: ReadonlySet<string>;
|
|
28
|
+
/** True when the provider accepts only an object schema, with `type: "object"`, at the root. */
|
|
29
|
+
readonly objectRoot: boolean;
|
|
30
|
+
/** Keywords the provider enforces, which lowering keeps although the shared rules drop them. */
|
|
31
|
+
readonly keeps?: ReadonlySet<string>;
|
|
32
|
+
/** Keywords the provider rejects; a schema that uses one is sent without strict. */
|
|
33
|
+
readonly rejects?: ReadonlySet<string>;
|
|
34
|
+
/** Limits on one schema; a schema over any of them is sent without strict. */
|
|
35
|
+
readonly limits?: StrictLimits;
|
|
36
|
+
}
|
|
37
|
+
/** The size limits of one strict schema. */
|
|
38
|
+
export interface StrictLimits {
|
|
39
|
+
readonly properties: number;
|
|
40
|
+
/** Levels of object nesting. */
|
|
41
|
+
readonly depth: number;
|
|
42
|
+
readonly enumValues: number;
|
|
43
|
+
/** Characters in all property names, definition names, and string enum and `const` values. */
|
|
44
|
+
readonly stringLength: number;
|
|
45
|
+
/** The most characters one string enum may hold once it has more than `values` values. */
|
|
46
|
+
readonly largeEnum: {
|
|
47
|
+
readonly values: number;
|
|
48
|
+
readonly length: number;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/** A schema lowered to strict form, with the counts the request-wide complexity limits are checked against. */
|
|
52
|
+
export interface StrictSchema {
|
|
53
|
+
readonly schema: Json;
|
|
54
|
+
/** Properties left out of `required`, across the whole schema. Always 0 for a nullable dialect. */
|
|
55
|
+
readonly optional: number;
|
|
56
|
+
/** Subschemas that use `anyOf` or a type array, the null unions a nullable dialect adds included. */
|
|
57
|
+
readonly unions: number;
|
|
58
|
+
}
|
|
59
|
+
/** The schema in strict form with its counts, or `undefined` when strict mode cannot express it. */
|
|
60
|
+
export declare function lowerToStrict(schema: Json, dialect: StrictDialect): StrictSchema | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* Removes from a tool call's input each `null` that nullable lowering added: a `null` in a property that the original
|
|
63
|
+
* schema leaves optional and does not let be `null`. It follows `properties`, `items`, local `$ref`s and, for an
|
|
64
|
+
* object or array inside an `anyOf` or `oneOf`, the first branch whose properties or items fit the value.
|
|
65
|
+
*/
|
|
66
|
+
export declare function stripAddedNulls(input: unknown, original: Json): unknown;
|
|
67
|
+
export {};
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lowers a tool's JSON Schema to the subset a provider's strict tool mode accepts, and undoes the one change the
|
|
3
|
+
* model can see: the `null` that a nullable dialect adds to an optional property. Each adapter passes its own
|
|
4
|
+
* `StrictDialect`; the rules both first providers share are these:
|
|
5
|
+
*
|
|
6
|
+
* - every object needs `additionalProperties: false`, and an object with any other `additionalProperties` cannot be
|
|
7
|
+
* strict. An object that declares neither `properties` nor `additionalProperties` accepts any keys, and adding
|
|
8
|
+
* `additionalProperties: false` would let the model send only `{}`, so it cannot be strict either;
|
|
9
|
+
* - `not` is unsupported, and `oneOf` must be written as `anyOf`;
|
|
10
|
+
* - numeric constraints, string length constraints and array constraints beyond `minItems` of 0 or 1 are dropped,
|
|
11
|
+
* unless the dialect keeps them. Anthropic rejects them (Structured outputs, "JSON Schema limitations",
|
|
12
|
+
* https://platform.claude.com/docs/en/build-with-claude/structured-outputs, as of November 2026).
|
|
13
|
+
*
|
|
14
|
+
* OpenAI's strict mode requires every property (Structured model outputs, "Supported schemas",
|
|
15
|
+
* https://developers.openai.com/api/docs/guides/structured-outputs#supported-schemas, as of November 2026). It
|
|
16
|
+
* accepts the numeric constraints and `minItems`/`maxItems`, rejects `allOf`, `if`/`then`/`else` and the
|
|
17
|
+
* `dependent*` keywords, and limits a schema's size, for example to 5,000 properties and 10 levels of nesting. Its
|
|
18
|
+
* dialect therefore writes an optional property as a required one that also accepts `null`. The removed constraints
|
|
19
|
+
* are not lost: the SDK validates every call against the original schema, after `stripAddedNulls()`.
|
|
20
|
+
*/
|
|
21
|
+
const UNSUPPORTED_KEYWORDS = new Set([
|
|
22
|
+
"minimum",
|
|
23
|
+
"maximum",
|
|
24
|
+
"exclusiveMinimum",
|
|
25
|
+
"exclusiveMaximum",
|
|
26
|
+
"multipleOf",
|
|
27
|
+
"minLength",
|
|
28
|
+
"maxLength",
|
|
29
|
+
"maxItems",
|
|
30
|
+
"uniqueItems",
|
|
31
|
+
"minProperties",
|
|
32
|
+
"maxProperties",
|
|
33
|
+
"patternProperties",
|
|
34
|
+
"propertyNames",
|
|
35
|
+
]);
|
|
36
|
+
// Lookaround, named groups and backreferences fall outside the documented regex subset.
|
|
37
|
+
const UNSUPPORTED_PATTERN = /\(\?|\\[1-9k]/;
|
|
38
|
+
const SCHEMA_LISTS = ["anyOf", "allOf", "oneOf", "prefixItems"];
|
|
39
|
+
const SCHEMA_MAPS = ["properties", "$defs", "definitions"];
|
|
40
|
+
function isObject(value) {
|
|
41
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
42
|
+
}
|
|
43
|
+
/** True for a key strict mode rejects, so lowering removes it. */
|
|
44
|
+
function dropsKey(key, value, dialect) {
|
|
45
|
+
if (dialect.keeps?.has(key))
|
|
46
|
+
return false;
|
|
47
|
+
if (UNSUPPORTED_KEYWORDS.has(key))
|
|
48
|
+
return true;
|
|
49
|
+
if (key === "minItems")
|
|
50
|
+
return typeof value !== "number" || value > 1;
|
|
51
|
+
if (key === "format")
|
|
52
|
+
return typeof value !== "string" || !dialect.formats.has(value);
|
|
53
|
+
return key === "pattern" && (typeof value !== "string" || UNSUPPORTED_PATTERN.test(value));
|
|
54
|
+
}
|
|
55
|
+
function lowerChildren(key, value, walk) {
|
|
56
|
+
if (SCHEMA_LISTS.includes(key) && Array.isArray(value))
|
|
57
|
+
return value.map((item) => lowerNode(item, walk));
|
|
58
|
+
if (SCHEMA_MAPS.includes(key) && isObject(value)) {
|
|
59
|
+
return Object.fromEntries(Object.entries(value).map(([name, item]) => [name, lowerNode(item, walk)]));
|
|
60
|
+
}
|
|
61
|
+
if (key === "items" && isObject(value))
|
|
62
|
+
return lowerNode(value, walk);
|
|
63
|
+
return value;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* True when the schema already accepts `null`. A schema that constrains nothing, such as `{}`, accepts it; a `$ref`,
|
|
67
|
+
* an `allOf`, or an object or array schema written without `type` counts as not accepting it, so a nullable dialect
|
|
68
|
+
* gives it an explicit `null` branch.
|
|
69
|
+
*/
|
|
70
|
+
function allowsNull(schema) {
|
|
71
|
+
if (!isObject(schema))
|
|
72
|
+
return true;
|
|
73
|
+
if (schema.type === "null" || (Array.isArray(schema.type) && schema.type.includes("null")))
|
|
74
|
+
return true;
|
|
75
|
+
if (Array.isArray(schema.enum))
|
|
76
|
+
return schema.enum.includes(null);
|
|
77
|
+
if ("const" in schema)
|
|
78
|
+
return schema.const === null;
|
|
79
|
+
const branches = Array.isArray(schema.anyOf) ? schema.anyOf : Array.isArray(schema.oneOf) ? schema.oneOf : undefined;
|
|
80
|
+
if (branches)
|
|
81
|
+
return branches.some(allowsNull);
|
|
82
|
+
return schema.type === undefined && !["$ref", "allOf", "properties", "items"].some((key) => key in schema);
|
|
83
|
+
}
|
|
84
|
+
/** The schema made to accept `null` as well, in the simplest form strict mode reads. */
|
|
85
|
+
function withNull(schema, walk) {
|
|
86
|
+
if (allowsNull(schema))
|
|
87
|
+
return schema;
|
|
88
|
+
walk.unions += 1;
|
|
89
|
+
if (typeof schema.type === "string" && !Array.isArray(schema.enum) && !("const" in schema)) {
|
|
90
|
+
return { ...schema, type: [schema.type, "null"] };
|
|
91
|
+
}
|
|
92
|
+
if (Array.isArray(schema.anyOf))
|
|
93
|
+
return { ...schema, anyOf: [...schema.anyOf, { type: "null" }] };
|
|
94
|
+
return { anyOf: [schema, { type: "null" }] };
|
|
95
|
+
}
|
|
96
|
+
/** Marks every property required, and lets each one that was optional be `null`. */
|
|
97
|
+
function requireAll(lowered, original, walk) {
|
|
98
|
+
if (!isObject(lowered.properties))
|
|
99
|
+
return;
|
|
100
|
+
const required = new Set(Array.isArray(original.required) ? original.required : []);
|
|
101
|
+
const properties = lowered.properties;
|
|
102
|
+
for (const [name, property] of Object.entries(properties)) {
|
|
103
|
+
if (!required.has(name) && isObject(property))
|
|
104
|
+
properties[name] = withNull(property, walk);
|
|
105
|
+
}
|
|
106
|
+
lowered.required = Object.keys(properties);
|
|
107
|
+
}
|
|
108
|
+
function countOptional(node, walk) {
|
|
109
|
+
const properties = isObject(node.properties) ? Object.keys(node.properties) : [];
|
|
110
|
+
const required = new Set(Array.isArray(node.required) ? node.required : []);
|
|
111
|
+
walk.optional += properties.filter((name) => !required.has(name)).length;
|
|
112
|
+
}
|
|
113
|
+
/** Why strict mode cannot express this node itself, or `undefined` when it can. */
|
|
114
|
+
function strictFailure(node, isObjectSchema, dialect) {
|
|
115
|
+
if ("not" in node)
|
|
116
|
+
return "uses not";
|
|
117
|
+
const rejected = Object.keys(node).find((key) => dialect.rejects?.has(key));
|
|
118
|
+
if (rejected !== undefined)
|
|
119
|
+
return `uses ${rejected}`;
|
|
120
|
+
if (!isObjectSchema)
|
|
121
|
+
return undefined;
|
|
122
|
+
if ("additionalProperties" in node) {
|
|
123
|
+
return node.additionalProperties === false ? undefined : "allows additional properties";
|
|
124
|
+
}
|
|
125
|
+
return isObject(node.properties) ? undefined : "declares no properties";
|
|
126
|
+
}
|
|
127
|
+
// A nullable object, `type: ["object", "null"]`, is an object too and needs the same treatment.
|
|
128
|
+
function isObjectNode(node) {
|
|
129
|
+
return (node.type === "object" || (Array.isArray(node.type) && node.type.includes("object")) || isObject(node.properties));
|
|
130
|
+
}
|
|
131
|
+
/** Counts the node toward `StrictDialect.limits`; an object nests one level deeper until its children are lowered. */
|
|
132
|
+
function measure(node, isObjectSchema, walk) {
|
|
133
|
+
if (isObjectSchema)
|
|
134
|
+
walk.depth += 1;
|
|
135
|
+
walk.maxDepth = Math.max(walk.maxDepth, walk.depth);
|
|
136
|
+
const names = SCHEMA_MAPS.flatMap((key) => (isObject(node[key]) ? Object.keys(node[key]) : []));
|
|
137
|
+
if (isObject(node.properties))
|
|
138
|
+
walk.properties += Object.keys(node.properties).length;
|
|
139
|
+
const strings = [...(Array.isArray(node.enum) ? node.enum : []), node.const].filter((v) => typeof v === "string");
|
|
140
|
+
walk.stringLength += [...names, ...strings].reduce((sum, text) => sum + text.length, 0);
|
|
141
|
+
if (!Array.isArray(node.enum))
|
|
142
|
+
return;
|
|
143
|
+
walk.enumValues += node.enum.length;
|
|
144
|
+
const enumStrings = node.enum.filter((value) => typeof value === "string");
|
|
145
|
+
const large = walk.dialect.limits?.largeEnum.values ?? Number.POSITIVE_INFINITY;
|
|
146
|
+
if (enumStrings.length > large) {
|
|
147
|
+
const length = enumStrings.reduce((sum, text) => sum + text.length, 0);
|
|
148
|
+
walk.largeEnumLength = Math.max(walk.largeEnumLength, length);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
function overLimits(walk, limits) {
|
|
152
|
+
if (limits === undefined)
|
|
153
|
+
return false;
|
|
154
|
+
return (walk.properties > limits.properties ||
|
|
155
|
+
walk.maxDepth > limits.depth ||
|
|
156
|
+
walk.enumValues > limits.enumValues ||
|
|
157
|
+
walk.stringLength > limits.stringLength ||
|
|
158
|
+
walk.largeEnumLength > limits.largeEnum.length);
|
|
159
|
+
}
|
|
160
|
+
function lowerNode(node, walk) {
|
|
161
|
+
if (!isObject(node) || walk.failure !== undefined)
|
|
162
|
+
return node;
|
|
163
|
+
const isObjectSchema = isObjectNode(node);
|
|
164
|
+
const failure = strictFailure(node, isObjectSchema, walk.dialect);
|
|
165
|
+
if (failure !== undefined) {
|
|
166
|
+
walk.failure = failure;
|
|
167
|
+
return node;
|
|
168
|
+
}
|
|
169
|
+
measure(node, isObjectSchema, walk);
|
|
170
|
+
const lowered = {};
|
|
171
|
+
for (const [key, value] of Object.entries(node)) {
|
|
172
|
+
if (dropsKey(key, value, walk.dialect))
|
|
173
|
+
continue;
|
|
174
|
+
lowered[key === "oneOf" ? "anyOf" : key] = lowerChildren(key, value, walk);
|
|
175
|
+
}
|
|
176
|
+
if (isObjectSchema)
|
|
177
|
+
walk.depth -= 1;
|
|
178
|
+
if (isObjectSchema) {
|
|
179
|
+
lowered.additionalProperties = false;
|
|
180
|
+
if (walk.dialect.optional === "nullable")
|
|
181
|
+
requireAll(lowered, node, walk);
|
|
182
|
+
else
|
|
183
|
+
countOptional(node, walk);
|
|
184
|
+
}
|
|
185
|
+
if (Array.isArray(node.type) || Array.isArray(lowered.anyOf))
|
|
186
|
+
walk.unions += 1;
|
|
187
|
+
return lowered;
|
|
188
|
+
}
|
|
189
|
+
/** The schema in strict form with its counts, or `undefined` when strict mode cannot express it. */
|
|
190
|
+
export function lowerToStrict(schema, dialect) {
|
|
191
|
+
if (dialect.objectRoot && schema.type !== "object")
|
|
192
|
+
return undefined;
|
|
193
|
+
const walk = {
|
|
194
|
+
dialect,
|
|
195
|
+
optional: 0,
|
|
196
|
+
unions: 0,
|
|
197
|
+
properties: 0,
|
|
198
|
+
depth: 0,
|
|
199
|
+
maxDepth: 0,
|
|
200
|
+
enumValues: 0,
|
|
201
|
+
stringLength: 0,
|
|
202
|
+
largeEnumLength: 0,
|
|
203
|
+
};
|
|
204
|
+
const lowered = lowerNode(schema, walk);
|
|
205
|
+
if (walk.failure !== undefined || overLimits(walk, dialect.limits))
|
|
206
|
+
return undefined;
|
|
207
|
+
return { schema: lowered, optional: walk.optional, unions: walk.unions };
|
|
208
|
+
}
|
|
209
|
+
// Stripping the added nulls ------------------------------------------------------------------------------------------
|
|
210
|
+
/** Follows a local `#/$defs/…` or `#/definitions/…` reference; any other schema comes back as it is. */
|
|
211
|
+
function resolve(schema, root) {
|
|
212
|
+
if (!isObject(schema) || typeof schema.$ref !== "string")
|
|
213
|
+
return schema;
|
|
214
|
+
const match = /^#\/(\$defs|definitions)\/([^/]+)$/.exec(schema.$ref);
|
|
215
|
+
if (match === null)
|
|
216
|
+
return schema;
|
|
217
|
+
const defs = root[match[1] ?? ""];
|
|
218
|
+
return isObject(defs) ? defs[decodeURIComponent(match[2] ?? "")] : schema;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* The first `anyOf`/`oneOf` branch that can describe the value: for an array, one with `items`; for an object, one
|
|
222
|
+
* whose `properties` name every key the value has. The branch the model chose is the one its value fits.
|
|
223
|
+
*/
|
|
224
|
+
function matchingBranch(value, schema, root) {
|
|
225
|
+
const branches = Array.isArray(schema.anyOf) ? schema.anyOf : Array.isArray(schema.oneOf) ? schema.oneOf : [];
|
|
226
|
+
return branches.find((branch) => {
|
|
227
|
+
const resolved = resolve(branch, root);
|
|
228
|
+
if (!isObject(resolved))
|
|
229
|
+
return false;
|
|
230
|
+
if (Array.isArray(value))
|
|
231
|
+
return isObject(resolved.items);
|
|
232
|
+
const properties = resolved.properties;
|
|
233
|
+
return isObject(value) && isObject(properties) && Object.keys(value).every((key) => key in properties);
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
function strip(value, node, root) {
|
|
237
|
+
const schema = resolve(node, root);
|
|
238
|
+
if (!isObject(schema))
|
|
239
|
+
return value;
|
|
240
|
+
if (!isObject(schema.properties) && !isObject(schema.items) && value !== null && typeof value === "object") {
|
|
241
|
+
const branch = matchingBranch(value, schema, root);
|
|
242
|
+
return branch === undefined ? value : strip(value, branch, root);
|
|
243
|
+
}
|
|
244
|
+
if (Array.isArray(value))
|
|
245
|
+
return isObject(schema.items) ? value.map((item) => strip(item, schema.items, root)) : value;
|
|
246
|
+
return isObject(value) && isObject(schema.properties) ? stripObject(value, schema.properties, schema, root) : value;
|
|
247
|
+
}
|
|
248
|
+
function stripObject(value, properties, schema, root) {
|
|
249
|
+
const required = new Set(Array.isArray(schema.required) ? schema.required : []);
|
|
250
|
+
const out = {};
|
|
251
|
+
for (const [name, item] of Object.entries(value)) {
|
|
252
|
+
const property = properties[name];
|
|
253
|
+
// A null the original schema does not allow here is one lowering added, and leaving the field out is what the
|
|
254
|
+
// model meant by it.
|
|
255
|
+
if (item === null && property !== undefined && !required.has(name) && !allowsNull(resolve(property, root)))
|
|
256
|
+
continue;
|
|
257
|
+
out[name] = property === undefined ? item : strip(item, property, root);
|
|
258
|
+
}
|
|
259
|
+
return out;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Removes from a tool call's input each `null` that nullable lowering added: a `null` in a property that the original
|
|
263
|
+
* schema leaves optional and does not let be `null`. It follows `properties`, `items`, local `$ref`s and, for an
|
|
264
|
+
* object or array inside an `anyOf` or `oneOf`, the first branch whose properties or items fit the value.
|
|
265
|
+
*/
|
|
266
|
+
export function stripAddedNulls(input, original) {
|
|
267
|
+
return strip(input, original, original);
|
|
268
|
+
}
|
|
269
|
+
//# sourceMappingURL=strict-schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"strict-schema.js","sourceRoot":"","sources":["../../src/adapters/strict-schema.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAyCH,MAAM,oBAAoB,GAAG,IAAI,GAAG,CAAC;IACnC,SAAS;IACT,SAAS;IACT,kBAAkB;IAClB,kBAAkB;IAClB,YAAY;IACZ,WAAW;IACX,WAAW;IACX,UAAU;IACV,aAAa;IACb,eAAe;IACf,eAAe;IACf,mBAAmB;IACnB,eAAe;CAChB,CAAC,CAAC;AACH,wFAAwF;AACxF,MAAM,mBAAmB,GAAG,eAAe,CAAC;AAC5C,MAAM,YAAY,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,aAAa,CAAC,CAAC;AAChE,MAAM,WAAW,GAAG,CAAC,YAAY,EAAE,OAAO,EAAE,aAAa,CAAC,CAAC;AAkB3D,SAAS,QAAQ,CAAC,KAAc;IAC9B,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC;AAED,kEAAkE;AAClE,SAAS,QAAQ,CAAC,GAAW,EAAE,KAAc,EAAE,OAAsB;IACnE,IAAI,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,GAAG,CAAC;QAAE,OAAO,KAAK,CAAC;IAC1C,IAAI,oBAAoB,CAAC,GAAG,CAAC,GAAG,CAAC;QAAE,OAAO,IAAI,CAAC;IAC/C,IAAI,GAAG,KAAK,UAAU;QAAE,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,GAAG,CAAC,CAAC;IACtE,IAAI,GAAG,KAAK,QAAQ;QAAE,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CA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AAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAC;IAC1E,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAAC,KAAc,EAAE,QAAc;IAC5D,OAAO,KAAK,CAAC,KAAK,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC;AAC1C,CAAC","sourcesContent":["/**\n * Lowers a tool's JSON Schema to the subset a provider's strict tool mode accepts, and undoes the one change the\n * model can see: the `null` that a nullable dialect adds to an optional property. Each adapter passes its own\n * `StrictDialect`; the rules both first providers share are these:\n *\n * - every object needs `additionalProperties: false`, and an object with any other `additionalProperties` cannot be\n * strict. An object that declares neither `properties` nor `additionalProperties` accepts any keys, and adding\n * `additionalProperties: false` would let the model send only `{}`, so it cannot be strict either;\n * - `not` is unsupported, and `oneOf` must be written as `anyOf`;\n * - numeric constraints, string length constraints and array constraints beyond `minItems` of 0 or 1 are dropped,\n * unless the dialect keeps them. Anthropic rejects them (Structured outputs, \"JSON Schema limitations\",\n * https://platform.claude.com/docs/en/build-with-claude/structured-outputs, as of November 2026).\n *\n * OpenAI's strict mode requires every property (Structured model outputs, \"Supported schemas\",\n * https://developers.openai.com/api/docs/guides/structured-outputs#supported-schemas, as of November 2026). It\n * accepts the numeric constraints and `minItems`/`maxItems`, rejects `allOf`, `if`/`then`/`else` and the\n * `dependent*` keywords, and limits a schema's size, for example to 5,000 properties and 10 levels of nesting. Its\n * dialect therefore writes an optional property as a required one that also accepts `null`. The removed constraints\n * are not lost: the SDK validates every call against the original schema, after `stripAddedNulls()`.\n */\n\ntype Json = Record<string, unknown>;\n\n/** How one provider's strict mode differs from the shared rules above. */\nexport interface StrictDialect {\n /** `\"keep\"` leaves optional properties optional; `\"nullable\"` requires them and lets them be `null`. */\n readonly optional: \"keep\" | \"nullable\";\n /** The string formats the provider accepts; any other `format` is dropped. */\n readonly formats: ReadonlySet<string>;\n /** True when the provider accepts only an object schema, with `type: \"object\"`, at the root. */\n readonly objectRoot: boolean;\n /** Keywords the provider enforces, which lowering keeps although the shared rules drop them. */\n readonly keeps?: ReadonlySet<string>;\n /** Keywords the provider rejects; a schema that uses one is sent without strict. */\n readonly rejects?: ReadonlySet<string>;\n /** Limits on one schema; a schema over any of them is sent without strict. */\n readonly limits?: StrictLimits;\n}\n\n/** The size limits of one strict schema. */\nexport interface StrictLimits {\n readonly properties: number;\n /** Levels of object nesting. */\n readonly depth: number;\n readonly enumValues: number;\n /** Characters in all property names, definition names, and string enum and `const` values. */\n readonly stringLength: number;\n /** The most characters one string enum may hold once it has more than `values` values. */\n readonly largeEnum: { readonly values: number; readonly length: number };\n}\n\n/** A schema lowered to strict form, with the counts the request-wide complexity limits are checked against. */\nexport interface StrictSchema {\n readonly schema: Json;\n /** Properties left out of `required`, across the whole schema. Always 0 for a nullable dialect. */\n readonly optional: number;\n /** Subschemas that use `anyOf` or a type array, the null unions a nullable dialect adds included. */\n readonly unions: number;\n}\n\nconst UNSUPPORTED_KEYWORDS = new Set([\n \"minimum\",\n \"maximum\",\n \"exclusiveMinimum\",\n \"exclusiveMaximum\",\n \"multipleOf\",\n \"minLength\",\n \"maxLength\",\n \"maxItems\",\n \"uniqueItems\",\n \"minProperties\",\n \"maxProperties\",\n \"patternProperties\",\n \"propertyNames\",\n]);\n// Lookaround, named groups and backreferences fall outside the documented regex subset.\nconst UNSUPPORTED_PATTERN = /\\(\\?|\\\\[1-9k]/;\nconst SCHEMA_LISTS = [\"anyOf\", \"allOf\", \"oneOf\", \"prefixItems\"];\nconst SCHEMA_MAPS = [\"properties\", \"$defs\", \"definitions\"];\n\ninterface Walk {\n readonly dialect: StrictDialect;\n optional: number;\n unions: number;\n /** Properties, object nesting (deepest so far and current), enum values and strings, for `StrictDialect.limits`. */\n properties: number;\n depth: number;\n maxDepth: number;\n enumValues: number;\n stringLength: number;\n /** The character count of the longest string enum with more than `limits.largeEnum.values` values. */\n largeEnumLength: number;\n /** Set when the schema uses a feature strict mode rejects; the tool is then sent without `strict`. */\n failure?: string;\n}\n\nfunction isObject(value: unknown): value is Json {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\n/** True for a key strict mode rejects, so lowering removes it. */\nfunction dropsKey(key: string, value: unknown, dialect: StrictDialect): boolean {\n if (dialect.keeps?.has(key)) return false;\n if (UNSUPPORTED_KEYWORDS.has(key)) return true;\n if (key === \"minItems\") return typeof value !== \"number\" || value > 1;\n if (key === \"format\") return typeof value !== \"string\" || !dialect.formats.has(value);\n return key === \"pattern\" && (typeof value !== \"string\" || UNSUPPORTED_PATTERN.test(value));\n}\n\nfunction lowerChildren(key: string, value: unknown, walk: Walk): unknown {\n if (SCHEMA_LISTS.includes(key) && Array.isArray(value)) return value.map((item) => lowerNode(item, walk));\n if (SCHEMA_MAPS.includes(key) && isObject(value)) {\n return Object.fromEntries(Object.entries(value).map(([name, item]) => [name, lowerNode(item, walk)]));\n }\n if (key === \"items\" && isObject(value)) return lowerNode(value, walk);\n return value;\n}\n\n/**\n * True when the schema already accepts `null`. A schema that constrains nothing, such as `{}`, accepts it; a `$ref`,\n * an `allOf`, or an object or array schema written without `type` counts as not accepting it, so a nullable dialect\n * gives it an explicit `null` branch.\n */\nfunction allowsNull(schema: unknown): boolean {\n if (!isObject(schema)) return true;\n if (schema.type === \"null\" || (Array.isArray(schema.type) && schema.type.includes(\"null\"))) return true;\n if (Array.isArray(schema.enum)) return schema.enum.includes(null);\n if (\"const\" in schema) return schema.const === null;\n const branches = Array.isArray(schema.anyOf) ? schema.anyOf : Array.isArray(schema.oneOf) ? schema.oneOf : undefined;\n if (branches) return branches.some(allowsNull);\n return schema.type === undefined && ![\"$ref\", \"allOf\", \"properties\", \"items\"].some((key) => key in schema);\n}\n\n/** The schema made to accept `null` as well, in the simplest form strict mode reads. */\nfunction withNull(schema: Json, walk: Walk): Json {\n if (allowsNull(schema)) return schema;\n walk.unions += 1;\n if (typeof schema.type === \"string\" && !Array.isArray(schema.enum) && !(\"const\" in schema)) {\n return { ...schema, type: [schema.type, \"null\"] };\n }\n if (Array.isArray(schema.anyOf)) return { ...schema, anyOf: [...schema.anyOf, { type: \"null\" }] };\n return { anyOf: [schema, { type: \"null\" }] };\n}\n\n/** Marks every property required, and lets each one that was optional be `null`. */\nfunction requireAll(lowered: Json, original: Json, walk: Walk): void {\n if (!isObject(lowered.properties)) return;\n const required = new Set(Array.isArray(original.required) ? original.required : []);\n const properties = lowered.properties;\n for (const [name, property] of Object.entries(properties)) {\n if (!required.has(name) && isObject(property)) properties[name] = withNull(property, walk);\n }\n lowered.required = Object.keys(properties);\n}\n\nfunction countOptional(node: Json, walk: Walk): void {\n const properties = isObject(node.properties) ? Object.keys(node.properties) : [];\n const required = new Set(Array.isArray(node.required) ? node.required : []);\n walk.optional += properties.filter((name) => !required.has(name)).length;\n}\n\n/** Why strict mode cannot express this node itself, or `undefined` when it can. */\nfunction strictFailure(node: Json, isObjectSchema: boolean, dialect: StrictDialect): string | undefined {\n if (\"not\" in node) return \"uses not\";\n const rejected = Object.keys(node).find((key) => dialect.rejects?.has(key));\n if (rejected !== undefined) return `uses ${rejected}`;\n if (!isObjectSchema) return undefined;\n if (\"additionalProperties\" in node) {\n return node.additionalProperties === false ? undefined : \"allows additional properties\";\n }\n return isObject(node.properties) ? undefined : \"declares no properties\";\n}\n\n// A nullable object, `type: [\"object\", \"null\"]`, is an object too and needs the same treatment.\nfunction isObjectNode(node: Json): boolean {\n return (\n node.type === \"object\" || (Array.isArray(node.type) && node.type.includes(\"object\")) || isObject(node.properties)\n );\n}\n\n/** Counts the node toward `StrictDialect.limits`; an object nests one level deeper until its children are lowered. */\nfunction measure(node: Json, isObjectSchema: boolean, walk: Walk): void {\n if (isObjectSchema) walk.depth += 1;\n walk.maxDepth = Math.max(walk.maxDepth, walk.depth);\n const names = SCHEMA_MAPS.flatMap((key) => (isObject(node[key]) ? Object.keys(node[key]) : []));\n if (isObject(node.properties)) walk.properties += Object.keys(node.properties).length;\n const strings = [...(Array.isArray(node.enum) ? node.enum : []), node.const].filter((v) => typeof v === \"string\");\n walk.stringLength += [...names, ...strings].reduce((sum, text) => sum + text.length, 0);\n if (!Array.isArray(node.enum)) return;\n walk.enumValues += node.enum.length;\n const enumStrings = node.enum.filter((value) => typeof value === \"string\");\n const large = walk.dialect.limits?.largeEnum.values ?? Number.POSITIVE_INFINITY;\n if (enumStrings.length > large) {\n const length = enumStrings.reduce((sum, text) => sum + text.length, 0);\n walk.largeEnumLength = Math.max(walk.largeEnumLength, length);\n }\n}\n\nfunction overLimits(walk: Walk, limits: StrictLimits | undefined): boolean {\n if (limits === undefined) return false;\n return (\n walk.properties > limits.properties ||\n walk.maxDepth > limits.depth ||\n walk.enumValues > limits.enumValues ||\n walk.stringLength > limits.stringLength ||\n walk.largeEnumLength > limits.largeEnum.length\n );\n}\n\nfunction lowerNode(node: unknown, walk: Walk): unknown {\n if (!isObject(node) || walk.failure !== undefined) return node;\n const isObjectSchema = isObjectNode(node);\n const failure = strictFailure(node, isObjectSchema, walk.dialect);\n if (failure !== undefined) {\n walk.failure = failure;\n return node;\n }\n measure(node, isObjectSchema, walk);\n const lowered: Json = {};\n for (const [key, value] of Object.entries(node)) {\n if (dropsKey(key, value, walk.dialect)) continue;\n lowered[key === \"oneOf\" ? \"anyOf\" : key] = lowerChildren(key, value, walk);\n }\n if (isObjectSchema) walk.depth -= 1;\n if (isObjectSchema) {\n lowered.additionalProperties = false;\n if (walk.dialect.optional === \"nullable\") requireAll(lowered, node, walk);\n else countOptional(node, walk);\n }\n if (Array.isArray(node.type) || Array.isArray(lowered.anyOf)) walk.unions += 1;\n return lowered;\n}\n\n/** The schema in strict form with its counts, or `undefined` when strict mode cannot express it. */\nexport function lowerToStrict(schema: Json, dialect: StrictDialect): StrictSchema | undefined {\n if (dialect.objectRoot && schema.type !== \"object\") return undefined;\n const walk: Walk = {\n dialect,\n optional: 0,\n unions: 0,\n properties: 0,\n depth: 0,\n maxDepth: 0,\n enumValues: 0,\n stringLength: 0,\n largeEnumLength: 0,\n };\n const lowered = lowerNode(schema, walk) as Json;\n if (walk.failure !== undefined || overLimits(walk, dialect.limits)) return undefined;\n return { schema: lowered, optional: walk.optional, unions: walk.unions };\n}\n\n// Stripping the added nulls ------------------------------------------------------------------------------------------\n\n/** Follows a local `#/$defs/…` or `#/definitions/…` reference; any other schema comes back as it is. */\nfunction resolve(schema: unknown, root: Json): unknown {\n if (!isObject(schema) || typeof schema.$ref !== \"string\") return schema;\n const match = /^#\\/(\\$defs|definitions)\\/([^/]+)$/.exec(schema.$ref);\n if (match === null) return schema;\n const defs = root[match[1] ?? \"\"];\n return isObject(defs) ? defs[decodeURIComponent(match[2] ?? \"\")] : schema;\n}\n\n/**\n * The first `anyOf`/`oneOf` branch that can describe the value: for an array, one with `items`; for an object, one\n * whose `properties` name every key the value has. The branch the model chose is the one its value fits.\n */\nfunction matchingBranch(value: unknown, schema: Json, root: Json): unknown {\n const branches = Array.isArray(schema.anyOf) ? schema.anyOf : Array.isArray(schema.oneOf) ? schema.oneOf : [];\n return branches.find((branch) => {\n const resolved = resolve(branch, root);\n if (!isObject(resolved)) return false;\n if (Array.isArray(value)) return isObject(resolved.items);\n const properties = resolved.properties;\n return isObject(value) && isObject(properties) && Object.keys(value).every((key) => key in properties);\n });\n}\n\nfunction strip(value: unknown, node: unknown, root: Json): unknown {\n const schema = resolve(node, root);\n if (!isObject(schema)) return value;\n if (!isObject(schema.properties) && !isObject(schema.items) && value !== null && typeof value === \"object\") {\n const branch = matchingBranch(value, schema, root);\n return branch === undefined ? value : strip(value, branch, root);\n }\n if (Array.isArray(value))\n return isObject(schema.items) ? value.map((item) => strip(item, schema.items, root)) : value;\n return isObject(value) && isObject(schema.properties) ? stripObject(value, schema.properties, schema, root) : value;\n}\n\nfunction stripObject(value: Json, properties: Json, schema: Json, root: Json): Json {\n const required = new Set(Array.isArray(schema.required) ? schema.required : []);\n const out: Json = {};\n for (const [name, item] of Object.entries(value)) {\n const property = properties[name];\n // A null the original schema does not allow here is one lowering added, and leaving the field out is what the\n // model meant by it.\n if (item === null && property !== undefined && !required.has(name) && !allowsNull(resolve(property, root)))\n continue;\n out[name] = property === undefined ? item : strip(item, property, root);\n }\n return out;\n}\n\n/**\n * Removes from a tool call's input each `null` that nullable lowering added: a `null` in a property that the original\n * schema leaves optional and does not let be `null`. It follows `properties`, `items`, local `$ref`s and, for an\n * object or array inside an `anyOf` or `oneOf`, the first branch whose properties or items fit the value.\n */\nexport function stripAddedNulls(input: unknown, original: Json): unknown {\n return strip(input, original, original);\n}\n"]}
|
|
@@ -0,0 +1,98 @@
|
|
|
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 { type ErrorKind } from "../classify.js";
|
|
11
|
+
import type { Transport } from "../runtime-types.js";
|
|
12
|
+
import type { ModelEvent, StreamOptions } from "../spec.js";
|
|
13
|
+
import { type ServerSentEvent } from "../sse.js";
|
|
14
|
+
import type { AssistantMessage, ContentBlock, ModelErrorCode, ModelRef, Usage } from "../types.js";
|
|
15
|
+
/** A JSON object as the adapters read it off the wire. */
|
|
16
|
+
export type Json = Record<string, unknown>;
|
|
17
|
+
/** The content and usage an attempt reached before it ended. */
|
|
18
|
+
export interface PartialReply {
|
|
19
|
+
readonly content?: ContentBlock[];
|
|
20
|
+
readonly usage?: Usage;
|
|
21
|
+
}
|
|
22
|
+
/** What one failure says about itself. */
|
|
23
|
+
export interface WireFailure {
|
|
24
|
+
readonly status?: number;
|
|
25
|
+
/** The provider's error type, such as `"invalid_request_error"`. */
|
|
26
|
+
readonly type?: string;
|
|
27
|
+
/** The provider's finer error code, when the error has one, such as `"insufficient_quota"`. */
|
|
28
|
+
readonly errorCode?: string;
|
|
29
|
+
readonly message: string;
|
|
30
|
+
/** Sets the kind outright, for a failure that no status describes, such as a refusal inside a 200 stream. */
|
|
31
|
+
readonly kind?: ErrorKind;
|
|
32
|
+
/** How long the response's `retry-after-ms` or `retry-after` header asked the caller to wait, in milliseconds. */
|
|
33
|
+
readonly retryAfterMs?: number;
|
|
34
|
+
}
|
|
35
|
+
/** True for a JSON object, not an array or `null`. */
|
|
36
|
+
export declare function isObject(value: unknown): value is Json;
|
|
37
|
+
/** The value when it is a string, or `""`. */
|
|
38
|
+
export declare function str(value: unknown): string;
|
|
39
|
+
/** A token count: a finite positive number, or 0. */
|
|
40
|
+
export declare function count(value: unknown): number;
|
|
41
|
+
/** A tool call's input: the parsed JSON, or the partial JSON text when the output limit cut it off. */
|
|
42
|
+
export declare function parseToolInput(text: string, empty?: unknown): unknown;
|
|
43
|
+
/**
|
|
44
|
+
* An assistant message with an unknown cost, which `createModel()` prices from the catalog. Without `partial.usage`,
|
|
45
|
+
* the provider reported no usage, so the message's usage is `unreported`.
|
|
46
|
+
*/
|
|
47
|
+
export declare function terminalMessage(model: ModelRef, partial: PartialReply, stopReason: AssistantMessage["stopReason"], error?: NonNullable<AssistantMessage["error"]>): AssistantMessage;
|
|
48
|
+
/** The `error` terminal of an aborted call, which carries no `error`. */
|
|
49
|
+
export declare function abortedEvent(model: ModelRef, partial?: PartialReply): Extract<ModelEvent, {
|
|
50
|
+
type: "error";
|
|
51
|
+
}>;
|
|
52
|
+
/**
|
|
53
|
+
* The `error` terminal for a failure. A failure with no HTTP status, such as an error event inside a 200 stream,
|
|
54
|
+
* classifies like the status its type stands for in `statusOfType`. The message starts with the error code or type.
|
|
55
|
+
* A failure with a status is an HTTP error response, so without `partial.usage` it reports zero tokens.
|
|
56
|
+
*/
|
|
57
|
+
export declare function failureTerminal(model: ModelRef, failure: WireFailure, statusOfType: Readonly<Record<string, number>>, partial?: PartialReply, code?: ModelErrorCode): Extract<ModelEvent, {
|
|
58
|
+
type: "error";
|
|
59
|
+
}>;
|
|
60
|
+
/** The `error` terminal for a throw: retryable only for a transport failure, and `fatal` for a bug or bad data. */
|
|
61
|
+
export declare function thrownTerminal(model: ModelRef, thrown: unknown, partial?: PartialReply): {
|
|
62
|
+
type: "error";
|
|
63
|
+
message: AssistantMessage;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* How long a failed response asks the caller to wait before retrying, in milliseconds, or `undefined` when it gives
|
|
67
|
+
* no valid delay. `retry-after-ms` wins over `retry-after`, as in the official TypeScript SDKs (openai-node v7.49.0
|
|
68
|
+
* and anthropic-sdk-typescript v0.129.0, `src/client.ts`, as of November 2026). `retry-after` holds seconds, as
|
|
69
|
+
* Anthropic's and OpenAI's rate-limit pages say (https://platform.claude.com/docs/en/api/rate-limits and
|
|
70
|
+
* https://developers.openai.com/api/docs/guides/rate-limits, as of November 2026), or an HTTP date (RFC 9110,
|
|
71
|
+
* section 10.2.3). A date counts from the response's own `date` header, so the delay depends on no local clock, and
|
|
72
|
+
* a response without one gives no delay. Any other value, such as a negative number or the comma-joined values of a
|
|
73
|
+
* repeated header, gives no delay.
|
|
74
|
+
*/
|
|
75
|
+
export declare function retryAfterMs(headers: Headers): number | undefined;
|
|
76
|
+
/** A failed response's body, parsed as JSON when it is JSON, and its text. */
|
|
77
|
+
export declare function readErrorBody(response: Response): Promise<{
|
|
78
|
+
body: unknown;
|
|
79
|
+
text: string;
|
|
80
|
+
}>;
|
|
81
|
+
/** The body `onPayload` asks for, or the adapter's own when it returns `undefined`. */
|
|
82
|
+
export declare function payloadOf(body: unknown, options: StreamOptions): unknown;
|
|
83
|
+
/**
|
|
84
|
+
* Sends one HTTP request. A transport that throws becomes an `error` terminal unless the throw is a guard's; only a
|
|
85
|
+
* transport failure, by the rule the retries use, may be retried, and any other throw is `fatal`.
|
|
86
|
+
*/
|
|
87
|
+
export declare function sendRequest(transport: Transport, request: Request, model: ModelRef, signal: AbortSignal | undefined): AsyncGenerator<ModelEvent, Response | undefined>;
|
|
88
|
+
/** How one protocol reads its event stream. */
|
|
89
|
+
export interface StreamReader {
|
|
90
|
+
/** The events one server-sent event produces. The stream is over once one of them is the terminal. */
|
|
91
|
+
handle(event: ServerSentEvent): ModelEvent[];
|
|
92
|
+
/** The content and usage so far, for an `error` terminal. */
|
|
93
|
+
partial(): PartialReply;
|
|
94
|
+
/** The events for a stream that closed before its terminal, ending in a terminal. */
|
|
95
|
+
ended(): ModelEvent[];
|
|
96
|
+
}
|
|
97
|
+
/** Reads a 200 stream to its terminal. A stream that breaks or ends early ends in an `error` terminal. */
|
|
98
|
+
export declare function readEventStream(response: Response, model: ModelRef, reader: StreamReader, signal: AbortSignal | undefined): AsyncGenerator<ModelEvent>;
|