@tanstack/openai-base 0.9.9 → 0.9.10
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/dist/esm/adapters/chat-completions-text.js +844 -962
- package/dist/esm/adapters/chat-completions-text.js.map +1 -1
- package/dist/esm/adapters/chat-completions-tool-converter.js +54 -38
- package/dist/esm/adapters/chat-completions-tool-converter.js.map +1 -1
- package/dist/esm/adapters/responses-text.js +1171 -1319
- package/dist/esm/adapters/responses-text.js.map +1 -1
- package/dist/esm/adapters/responses-tool-converter.js +50 -34
- package/dist/esm/adapters/responses-tool-converter.js.map +1 -1
- package/dist/esm/index.js +4 -43
- package/dist/esm/tools/apply-patch-tool.js +20 -13
- package/dist/esm/tools/apply-patch-tool.js.map +1 -1
- package/dist/esm/tools/code-interpreter-tool.js +26 -18
- package/dist/esm/tools/code-interpreter-tool.js.map +1 -1
- package/dist/esm/tools/computer-use-tool.js +26 -19
- package/dist/esm/tools/computer-use-tool.js.map +1 -1
- package/dist/esm/tools/custom-tool.js +23 -21
- package/dist/esm/tools/custom-tool.js.map +1 -1
- package/dist/esm/tools/file-search-tool.js +30 -30
- package/dist/esm/tools/file-search-tool.js.map +1 -1
- package/dist/esm/tools/function-tool.js +44 -31
- package/dist/esm/tools/function-tool.js.map +1 -1
- package/dist/esm/tools/image-generation-tool.js +30 -23
- package/dist/esm/tools/image-generation-tool.js.map +1 -1
- package/dist/esm/tools/local-shell-tool.js +20 -13
- package/dist/esm/tools/local-shell-tool.js.map +1 -1
- package/dist/esm/tools/mcp-tool.js +31 -25
- package/dist/esm/tools/mcp-tool.js.map +1 -1
- package/dist/esm/tools/shell-tool.js +25 -21
- package/dist/esm/tools/shell-tool.js.map +1 -1
- package/dist/esm/tools/tool-converter.js +37 -46
- package/dist/esm/tools/tool-converter.js.map +1 -1
- package/dist/esm/tools/web-search-preview-tool.js +26 -15
- package/dist/esm/tools/web-search-preview-tool.js.map +1 -1
- package/dist/esm/tools/web-search-tool.js +27 -15
- package/dist/esm/tools/web-search-tool.js.map +1 -1
- package/dist/esm/usage.js +88 -76
- package/dist/esm/usage.js.map +1 -1
- package/dist/esm/utils/request-options.js +20 -9
- package/dist/esm/utils/request-options.js.map +1 -1
- package/dist/esm/utils/schema-converter.js +193 -146
- package/dist/esm/utils/schema-converter.js.map +1 -1
- package/package.json +7 -7
- package/src/adapters/responses-text.ts +5 -2
- package/dist/esm/index.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request-options.js","sources":["../../../src/utils/request-options.ts"],"sourcesContent":["/**\n * Extract `headers` and `signal` from a `Request | RequestInit` for the OpenAI\n * SDK's per-call `RequestOptions`. `Request` exposes `headers` as a `Headers`\n * instance (HeadersInit-compatible) while `RequestInit` exposes `HeadersInit`\n * directly — this helper accepts either shape so callers don't need to cast.\n *\n * Always returns an object (possibly empty) rather than `undefined` so test\n * assertions that match the second argument shape via `expect.anything()` /\n * `expect.objectContaining()` keep working when no request override was set.\n */\nexport function extractRequestOptions(\n request: Request | RequestInit | undefined,\n): { headers?: HeadersInit; signal?: AbortSignal | null } {\n if (!request) return {}\n // Conditional spread: under exactOptionalPropertyTypes the target's\n // `headers?: HeadersInit` and `signal?: AbortSignal | null` forbid an\n // explicit `undefined`. Omit the keys entirely when the source values\n // are absent so the OpenAI SDK sees `headers: undefined` as \"not set\"\n // rather than a present-but-undefined value.\n return {\n ...(request.headers !== undefined && { headers: request.headers }),\n ...(request.signal != null && { signal: request.signal }),\n }\n}\n"],"
|
|
1
|
+
{"version":3,"file":"request-options.js","names":[],"sources":["../../../src/utils/request-options.ts"],"sourcesContent":["/**\n * Extract `headers` and `signal` from a `Request | RequestInit` for the OpenAI\n * SDK's per-call `RequestOptions`. `Request` exposes `headers` as a `Headers`\n * instance (HeadersInit-compatible) while `RequestInit` exposes `HeadersInit`\n * directly — this helper accepts either shape so callers don't need to cast.\n *\n * Always returns an object (possibly empty) rather than `undefined` so test\n * assertions that match the second argument shape via `expect.anything()` /\n * `expect.objectContaining()` keep working when no request override was set.\n */\nexport function extractRequestOptions(\n request: Request | RequestInit | undefined,\n): { headers?: HeadersInit; signal?: AbortSignal | null } {\n if (!request) return {}\n // Conditional spread: under exactOptionalPropertyTypes the target's\n // `headers?: HeadersInit` and `signal?: AbortSignal | null` forbid an\n // explicit `undefined`. Omit the keys entirely when the source values\n // are absent so the OpenAI SDK sees `headers: undefined` as \"not set\"\n // rather than a present-but-undefined value.\n return {\n ...(request.headers !== undefined && { headers: request.headers }),\n ...(request.signal != null && { signal: request.signal }),\n }\n}\n"],"mappings":";;;;;;;;;;;AAUA,SAAgB,sBACd,SACwD;CACxD,IAAI,CAAC,SAAS,OAAO,CAAC;CAMtB,OAAO;EACL,GAAI,QAAQ,YAAY,KAAA,KAAa,EAAE,SAAS,QAAQ,QAAQ;EAChE,GAAI,QAAQ,UAAU,QAAQ,EAAE,QAAQ,QAAQ,OAAO;CACzD;AACF"}
|
|
@@ -1,165 +1,212 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
//#region src/utils/schema-converter.ts
|
|
2
|
+
/**
|
|
3
|
+
* String `format` values accepted by OpenAI's strict Structured Outputs subset.
|
|
4
|
+
* Any other format (e.g. "uri", "uri-reference", "regex") causes the API to
|
|
5
|
+
* reject the whole request with `400 ... '<format>' is not a valid format`.
|
|
6
|
+
* MCP servers and hand-written tools routinely declare such formats, so we strip
|
|
7
|
+
* the unsupported ones before sending. See:
|
|
8
|
+
* https://platform.openai.com/docs/guides/structured-outputs#supported-properties
|
|
9
|
+
*/
|
|
10
|
+
var SUPPORTED_STRING_FORMATS = /* @__PURE__ */ new Set([
|
|
11
|
+
"date-time",
|
|
12
|
+
"time",
|
|
13
|
+
"date",
|
|
14
|
+
"duration",
|
|
15
|
+
"email",
|
|
16
|
+
"hostname",
|
|
17
|
+
"ipv4",
|
|
18
|
+
"ipv6",
|
|
19
|
+
"uuid"
|
|
11
20
|
]);
|
|
21
|
+
/**
|
|
22
|
+
* Recursively drop JSON-Schema `format` keywords whose value isn't in OpenAI's
|
|
23
|
+
* strict-mode allowlist. Pure — returns a fresh tree and never mutates `node`,
|
|
24
|
+
* so the caller's original tool definition is left intact.
|
|
25
|
+
*
|
|
26
|
+
* A property *named* `format` always has a schema (object/boolean) value, never
|
|
27
|
+
* a bare string, so it is preserved and recursed into; only the `format`
|
|
28
|
+
* *keyword* (whose value is a string) is subject to removal.
|
|
29
|
+
*/
|
|
12
30
|
function stripUnsupportedFormats(node) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
return out;
|
|
31
|
+
if (Array.isArray(node)) return node.map(stripUnsupportedFormats);
|
|
32
|
+
if (node === null || typeof node !== "object") return node;
|
|
33
|
+
const out = {};
|
|
34
|
+
for (const [key, value] of Object.entries(node)) {
|
|
35
|
+
if (key === "format" && typeof value === "string" && !SUPPORTED_STRING_FORMATS.has(value)) continue;
|
|
36
|
+
out[key] = stripUnsupportedFormats(value);
|
|
37
|
+
}
|
|
38
|
+
return out;
|
|
23
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Transform a JSON schema to be compatible with OpenAI's structured output requirements.
|
|
42
|
+
* OpenAI requires:
|
|
43
|
+
* - All properties must be in the `required` array
|
|
44
|
+
* - Optional fields should have null added to their type union
|
|
45
|
+
* - additionalProperties must be false for objects
|
|
46
|
+
* - String `format` keywords must be from a fixed allowlist (others are stripped)
|
|
47
|
+
*
|
|
48
|
+
* @param schema - JSON schema to transform
|
|
49
|
+
* @param originalRequired - Original required array (to know which fields were optional)
|
|
50
|
+
* @returns Transformed schema compatible with OpenAI structured output
|
|
51
|
+
*/
|
|
24
52
|
function makeStructuredOutputCompatible(schema, originalRequired) {
|
|
25
|
-
|
|
53
|
+
return stripUnsupportedFormats(coerceStrictSchema(schema, originalRequired));
|
|
26
54
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
55
|
+
/**
|
|
56
|
+
* JSON-Schema keywords outside OpenAI's strict Structured Outputs subset. A
|
|
57
|
+
* schema using any of these can't be coerced into a strict-valid shape, and
|
|
58
|
+
* sending it with `strict: true` makes the API reject the ENTIRE request
|
|
59
|
+
* (e.g. `400 Invalid schema ... 'additionalProperties' is required to be ...`).
|
|
60
|
+
* Tools with such schemas are emitted with `strict: false` instead (see the
|
|
61
|
+
* tool converters) so they remain callable. MCP servers (e.g. Notion) routinely
|
|
62
|
+
* emit these.
|
|
63
|
+
*
|
|
64
|
+
* - `oneOf` / `allOf` / `not` — combinator keywords strict mode rejects
|
|
65
|
+
* - `$ref` / `$defs` / `definitions` — references and definition pools whose
|
|
66
|
+
* object subschemas escape the `additionalProperties: false` normalization
|
|
67
|
+
* strict mode requires
|
|
68
|
+
*/
|
|
69
|
+
var STRICT_UNSUPPORTED_KEYWORDS = [
|
|
70
|
+
"oneOf",
|
|
71
|
+
"allOf",
|
|
72
|
+
"not",
|
|
73
|
+
"$ref",
|
|
74
|
+
"$defs",
|
|
75
|
+
"definitions"
|
|
34
76
|
];
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
77
|
+
/**
|
|
78
|
+
* Keys that give a schema node a resolvable type under OpenAI's strict subset.
|
|
79
|
+
* A schema-position node carrying none of these is *typeless* (e.g. the empty
|
|
80
|
+
* `{}` that `z.any()` / `z.unknown()` emit). Strict mode requires every schema
|
|
81
|
+
* to declare a type, so a typeless node 400s the whole request — such tools
|
|
82
|
+
* must be sent with `strict: false` instead. (`oneOf`/`allOf`/`$ref` count as
|
|
83
|
+
* type indicators here even though they're independently strict-unsupported;
|
|
84
|
+
* the keyword check below already rejects them.)
|
|
85
|
+
*/
|
|
86
|
+
var TYPE_INDICATOR_KEYWORDS = [
|
|
87
|
+
"type",
|
|
88
|
+
"enum",
|
|
89
|
+
"const",
|
|
90
|
+
"anyOf",
|
|
91
|
+
"oneOf",
|
|
92
|
+
"allOf",
|
|
93
|
+
"$ref"
|
|
43
94
|
];
|
|
95
|
+
/**
|
|
96
|
+
* Returns `false` when `schema` cannot be made strict-compatible and must be
|
|
97
|
+
* sent with `strict: false`. Two ways that happens:
|
|
98
|
+
*
|
|
99
|
+
* 1. It uses a JSON-Schema keyword outside OpenAI's strict subset anywhere in
|
|
100
|
+
* the tree (`oneOf`/`allOf`/`not`/`$ref`/`$defs`).
|
|
101
|
+
* 2. It contains a *typeless* schema node — a property/items/anyOf entry with
|
|
102
|
+
* no `type` (nor `enum`/`const`/combinator), e.g. the `{}` that `z.any()`
|
|
103
|
+
* produces. Strict mode rejects typeless schemas.
|
|
104
|
+
* 3. It contains an open object schema. OpenAI strict mode requires objects to
|
|
105
|
+
* set `additionalProperties: false`, which would change the semantics of a
|
|
106
|
+
* free-form map rather than merely normalizing it.
|
|
107
|
+
*
|
|
108
|
+
* Conservative by design: for (1) keywords are matched as object keys, so a
|
|
109
|
+
* property literally named e.g. `oneOf` also trips it. That only costs that one
|
|
110
|
+
* tool its strict mode, which is strictly safer than a false "compatible"
|
|
111
|
+
* verdict that 400s the whole request.
|
|
112
|
+
*/
|
|
44
113
|
function isStrictModeCompatible(schema) {
|
|
45
|
-
|
|
114
|
+
return !containsStrictUnsupportedKeyword(schema) && !containsTypelessSchema(schema) && !containsOpenObject(schema);
|
|
46
115
|
}
|
|
116
|
+
/**
|
|
117
|
+
* Reports object schemas that cannot be closed without changing their input
|
|
118
|
+
* semantics. Objects with `properties` and no explicit
|
|
119
|
+
* `additionalProperties` are safe because `coerceStrictSchema` closes them.
|
|
120
|
+
*/
|
|
47
121
|
function containsOpenObject(node) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
const properties = schema["properties"];
|
|
60
|
-
const hasProperties = properties !== null && typeof properties === "object" && !Array.isArray(properties);
|
|
61
|
-
if (!hasProperties && schema["additionalProperties"] !== false) {
|
|
62
|
-
return true;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
return Object.values(schema).some(containsOpenObject);
|
|
122
|
+
if (Array.isArray(node)) return node.some(containsOpenObject);
|
|
123
|
+
if (node === null || typeof node !== "object") return false;
|
|
124
|
+
const schema = node;
|
|
125
|
+
const type = schema["type"];
|
|
126
|
+
if (type === "object" || Array.isArray(type) && type.includes("object")) {
|
|
127
|
+
if ("additionalProperties" in schema && schema["additionalProperties"] !== false) return true;
|
|
128
|
+
const properties = schema["properties"];
|
|
129
|
+
if (!(properties !== null && typeof properties === "object" && !Array.isArray(properties)) && schema["additionalProperties"] !== false) return true;
|
|
130
|
+
}
|
|
131
|
+
return Object.values(schema).some(containsOpenObject);
|
|
66
132
|
}
|
|
67
133
|
function containsStrictUnsupportedKeyword(node) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
return false;
|
|
134
|
+
if (Array.isArray(node)) return node.some(containsStrictUnsupportedKeyword);
|
|
135
|
+
if (node === null || typeof node !== "object") return false;
|
|
136
|
+
for (const [key, value] of Object.entries(node)) {
|
|
137
|
+
if (STRICT_UNSUPPORTED_KEYWORDS.includes(key)) return true;
|
|
138
|
+
if (containsStrictUnsupportedKeyword(value)) return true;
|
|
139
|
+
}
|
|
140
|
+
return false;
|
|
77
141
|
}
|
|
142
|
+
/** A schema-position node that declares no type and so 400s strict mode. */
|
|
78
143
|
function isTypelessSchema(node) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
return !TYPE_INDICATOR_KEYWORDS.some((key) => key in node);
|
|
144
|
+
if (node === null || typeof node !== "object" || Array.isArray(node)) return false;
|
|
145
|
+
return !TYPE_INDICATOR_KEYWORDS.some((key) => key in node);
|
|
83
146
|
}
|
|
147
|
+
/**
|
|
148
|
+
* Walks the genuine schema positions (property values, `items`, `anyOf`
|
|
149
|
+
* variants) and reports whether any is typeless. Unlike the keyword walk this
|
|
150
|
+
* must respect structure: an empty `{}` is only a problem at a schema position,
|
|
151
|
+
* not e.g. an empty `properties` map.
|
|
152
|
+
*/
|
|
84
153
|
function containsTypelessSchema(node) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
if (schema.items !== void 0) {
|
|
94
|
-
children.push(
|
|
95
|
-
...Array.isArray(schema.items) ? schema.items : [schema.items]
|
|
96
|
-
);
|
|
97
|
-
}
|
|
98
|
-
if (Array.isArray(schema.anyOf)) {
|
|
99
|
-
children.push(...schema.anyOf);
|
|
100
|
-
}
|
|
101
|
-
return children.some(
|
|
102
|
-
(child) => isTypelessSchema(child) || containsTypelessSchema(child)
|
|
103
|
-
);
|
|
154
|
+
if (node === null || typeof node !== "object" || Array.isArray(node)) return false;
|
|
155
|
+
const schema = node;
|
|
156
|
+
const children = [];
|
|
157
|
+
if (schema.properties && typeof schema.properties === "object") children.push(...Object.values(schema.properties));
|
|
158
|
+
if (schema.items !== void 0) children.push(...Array.isArray(schema.items) ? schema.items : [schema.items]);
|
|
159
|
+
if (Array.isArray(schema.anyOf)) children.push(...schema.anyOf);
|
|
160
|
+
return children.some((child) => isTypelessSchema(child) || containsTypelessSchema(child));
|
|
104
161
|
}
|
|
162
|
+
/**
|
|
163
|
+
* Strict-mode structural rewrite (required widening, nullability,
|
|
164
|
+
* additionalProperties). Kept private so the public entry point can apply the
|
|
165
|
+
* format-stripping pass exactly once over the fully-rewritten tree.
|
|
166
|
+
*/
|
|
105
167
|
function coerceStrictSchema(schema, originalRequired) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
result.items = coerceStrictSchema(result.items, result.items.required || []);
|
|
147
|
-
}
|
|
148
|
-
if (result.anyOf && Array.isArray(result.anyOf)) {
|
|
149
|
-
result.anyOf = result.anyOf.map(
|
|
150
|
-
(variant) => coerceStrictSchema(variant, variant.required || [])
|
|
151
|
-
);
|
|
152
|
-
}
|
|
153
|
-
if (result.oneOf) {
|
|
154
|
-
throw new Error(
|
|
155
|
-
"oneOf is not supported in OpenAI structured output schemas. Check the supported outputs here: https://platform.openai.com/docs/guides/structured-outputs#supported-types"
|
|
156
|
-
);
|
|
157
|
-
}
|
|
158
|
-
return result;
|
|
168
|
+
const result = { ...schema };
|
|
169
|
+
const required = originalRequired ?? (Array.isArray(result["required"]) ? result["required"] : []);
|
|
170
|
+
if (result.type === "object" && result.properties) {
|
|
171
|
+
const properties = { ...result.properties };
|
|
172
|
+
const allPropertyNames = Object.keys(properties);
|
|
173
|
+
for (const propName of allPropertyNames) {
|
|
174
|
+
let prop = properties[propName];
|
|
175
|
+
const wasOptional = !required.includes(propName);
|
|
176
|
+
if (prop.type === "object" && prop.properties) prop = coerceStrictSchema(prop, prop.required || []);
|
|
177
|
+
else if (prop.type === "array" && prop.items) prop = {
|
|
178
|
+
...prop,
|
|
179
|
+
items: coerceStrictSchema(prop.items, prop.items.required || [])
|
|
180
|
+
};
|
|
181
|
+
else if (prop.anyOf) prop = coerceStrictSchema(prop, prop.required || []);
|
|
182
|
+
else if (prop.oneOf) throw new Error("oneOf is not supported in OpenAI structured output schemas. Check the supported outputs here: https://platform.openai.com/docs/guides/structured-outputs#supported-types");
|
|
183
|
+
if (wasOptional) {
|
|
184
|
+
if (prop.anyOf) {
|
|
185
|
+
if (!prop.anyOf.some((v) => v.type === "null")) prop = {
|
|
186
|
+
...prop,
|
|
187
|
+
anyOf: [...prop.anyOf, { type: "null" }]
|
|
188
|
+
};
|
|
189
|
+
} else if (prop.type && !Array.isArray(prop.type)) prop = {
|
|
190
|
+
...prop,
|
|
191
|
+
type: [prop.type, "null"]
|
|
192
|
+
};
|
|
193
|
+
else if (Array.isArray(prop.type) && !prop.type.includes("null")) prop = {
|
|
194
|
+
...prop,
|
|
195
|
+
type: [...prop.type, "null"]
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
properties[propName] = prop;
|
|
199
|
+
}
|
|
200
|
+
result.properties = properties;
|
|
201
|
+
result.required = allPropertyNames;
|
|
202
|
+
result.additionalProperties = false;
|
|
203
|
+
}
|
|
204
|
+
if (result.type === "array" && result.items) result.items = coerceStrictSchema(result.items, result.items.required || []);
|
|
205
|
+
if (result.anyOf && Array.isArray(result.anyOf)) result.anyOf = result.anyOf.map((variant) => coerceStrictSchema(variant, variant.required || []));
|
|
206
|
+
if (result.oneOf) throw new Error("oneOf is not supported in OpenAI structured output schemas. Check the supported outputs here: https://platform.openai.com/docs/guides/structured-outputs#supported-types");
|
|
207
|
+
return result;
|
|
159
208
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
};
|
|
165
|
-
//# sourceMappingURL=schema-converter.js.map
|
|
209
|
+
//#endregion
|
|
210
|
+
export { isStrictModeCompatible, makeStructuredOutputCompatible, stripUnsupportedFormats };
|
|
211
|
+
|
|
212
|
+
//# sourceMappingURL=schema-converter.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema-converter.js","sources":["../../../src/utils/schema-converter.ts"],"sourcesContent":["/**\n * String `format` values accepted by OpenAI's strict Structured Outputs subset.\n * Any other format (e.g. \"uri\", \"uri-reference\", \"regex\") causes the API to\n * reject the whole request with `400 ... '<format>' is not a valid format`.\n * MCP servers and hand-written tools routinely declare such formats, so we strip\n * the unsupported ones before sending. See:\n * https://platform.openai.com/docs/guides/structured-outputs#supported-properties\n */\nconst SUPPORTED_STRING_FORMATS = new Set([\n 'date-time',\n 'time',\n 'date',\n 'duration',\n 'email',\n 'hostname',\n 'ipv4',\n 'ipv6',\n 'uuid',\n])\n\n/**\n * Recursively drop JSON-Schema `format` keywords whose value isn't in OpenAI's\n * strict-mode allowlist. Pure — returns a fresh tree and never mutates `node`,\n * so the caller's original tool definition is left intact.\n *\n * A property *named* `format` always has a schema (object/boolean) value, never\n * a bare string, so it is preserved and recursed into; only the `format`\n * *keyword* (whose value is a string) is subject to removal.\n */\nexport function stripUnsupportedFormats(node: any): any {\n if (Array.isArray(node)) return node.map(stripUnsupportedFormats)\n if (node === null || typeof node !== 'object') return node\n\n const out: Record<string, any> = {}\n for (const [key, value] of Object.entries(node)) {\n if (\n key === 'format' &&\n typeof value === 'string' &&\n !SUPPORTED_STRING_FORMATS.has(value)\n ) {\n continue\n }\n out[key] = stripUnsupportedFormats(value)\n }\n return out\n}\n\n/**\n * Transform a JSON schema to be compatible with OpenAI's structured output requirements.\n * OpenAI requires:\n * - All properties must be in the `required` array\n * - Optional fields should have null added to their type union\n * - additionalProperties must be false for objects\n * - String `format` keywords must be from a fixed allowlist (others are stripped)\n *\n * @param schema - JSON schema to transform\n * @param originalRequired - Original required array (to know which fields were optional)\n * @returns Transformed schema compatible with OpenAI structured output\n */\nexport function makeStructuredOutputCompatible(\n schema: Record<string, any>,\n originalRequired?: Array<string>,\n): Record<string, any> {\n return stripUnsupportedFormats(coerceStrictSchema(schema, originalRequired))\n}\n\n/**\n * JSON-Schema keywords outside OpenAI's strict Structured Outputs subset. A\n * schema using any of these can't be coerced into a strict-valid shape, and\n * sending it with `strict: true` makes the API reject the ENTIRE request\n * (e.g. `400 Invalid schema ... 'additionalProperties' is required to be ...`).\n * Tools with such schemas are emitted with `strict: false` instead (see the\n * tool converters) so they remain callable. MCP servers (e.g. Notion) routinely\n * emit these.\n *\n * - `oneOf` / `allOf` / `not` — combinator keywords strict mode rejects\n * - `$ref` / `$defs` / `definitions` — references and definition pools whose\n * object subschemas escape the `additionalProperties: false` normalization\n * strict mode requires\n */\nconst STRICT_UNSUPPORTED_KEYWORDS: ReadonlyArray<string> = [\n 'oneOf',\n 'allOf',\n 'not',\n '$ref',\n '$defs',\n 'definitions',\n]\n\n/**\n * Keys that give a schema node a resolvable type under OpenAI's strict subset.\n * A schema-position node carrying none of these is *typeless* (e.g. the empty\n * `{}` that `z.any()` / `z.unknown()` emit). Strict mode requires every schema\n * to declare a type, so a typeless node 400s the whole request — such tools\n * must be sent with `strict: false` instead. (`oneOf`/`allOf`/`$ref` count as\n * type indicators here even though they're independently strict-unsupported;\n * the keyword check below already rejects them.)\n */\nconst TYPE_INDICATOR_KEYWORDS: ReadonlyArray<string> = [\n 'type',\n 'enum',\n 'const',\n 'anyOf',\n 'oneOf',\n 'allOf',\n '$ref',\n]\n\n/**\n * Returns `false` when `schema` cannot be made strict-compatible and must be\n * sent with `strict: false`. Two ways that happens:\n *\n * 1. It uses a JSON-Schema keyword outside OpenAI's strict subset anywhere in\n * the tree (`oneOf`/`allOf`/`not`/`$ref`/`$defs`).\n * 2. It contains a *typeless* schema node — a property/items/anyOf entry with\n * no `type` (nor `enum`/`const`/combinator), e.g. the `{}` that `z.any()`\n * produces. Strict mode rejects typeless schemas.\n * 3. It contains an open object schema. OpenAI strict mode requires objects to\n * set `additionalProperties: false`, which would change the semantics of a\n * free-form map rather than merely normalizing it.\n *\n * Conservative by design: for (1) keywords are matched as object keys, so a\n * property literally named e.g. `oneOf` also trips it. That only costs that one\n * tool its strict mode, which is strictly safer than a false \"compatible\"\n * verdict that 400s the whole request.\n */\nexport function isStrictModeCompatible(schema: unknown): boolean {\n return (\n !containsStrictUnsupportedKeyword(schema) &&\n !containsTypelessSchema(schema) &&\n !containsOpenObject(schema)\n )\n}\n\n/**\n * Reports object schemas that cannot be closed without changing their input\n * semantics. Objects with `properties` and no explicit\n * `additionalProperties` are safe because `coerceStrictSchema` closes them.\n */\nfunction containsOpenObject(node: unknown): boolean {\n if (Array.isArray(node)) {\n return node.some(containsOpenObject)\n }\n if (node === null || typeof node !== 'object') return false\n\n const schema = node as Record<string, unknown>\n const type = schema['type']\n const isObjectSchema =\n type === 'object' || (Array.isArray(type) && type.includes('object'))\n\n if (isObjectSchema) {\n if (\n 'additionalProperties' in schema &&\n schema['additionalProperties'] !== false\n ) {\n return true\n }\n\n const properties = schema['properties']\n const hasProperties =\n properties !== null &&\n typeof properties === 'object' &&\n !Array.isArray(properties)\n if (!hasProperties && schema['additionalProperties'] !== false) {\n return true\n }\n }\n\n return Object.values(schema).some(containsOpenObject)\n}\n\nfunction containsStrictUnsupportedKeyword(node: unknown): boolean {\n if (Array.isArray(node)) {\n return node.some(containsStrictUnsupportedKeyword)\n }\n if (node === null || typeof node !== 'object') return false\n for (const [key, value] of Object.entries(node)) {\n if (STRICT_UNSUPPORTED_KEYWORDS.includes(key)) return true\n if (containsStrictUnsupportedKeyword(value)) return true\n }\n return false\n}\n\n/** A schema-position node that declares no type and so 400s strict mode. */\nfunction isTypelessSchema(node: unknown): boolean {\n if (node === null || typeof node !== 'object' || Array.isArray(node)) {\n // boolean schemas (`true`/`false`) and non-objects aren't typeless props.\n return false\n }\n return !TYPE_INDICATOR_KEYWORDS.some((key) => key in node)\n}\n\n/**\n * Walks the genuine schema positions (property values, `items`, `anyOf`\n * variants) and reports whether any is typeless. Unlike the keyword walk this\n * must respect structure: an empty `{}` is only a problem at a schema position,\n * not e.g. an empty `properties` map.\n */\nfunction containsTypelessSchema(node: unknown): boolean {\n if (node === null || typeof node !== 'object' || Array.isArray(node)) {\n return false\n }\n const schema = node as Record<string, any>\n\n const children: Array<unknown> = []\n if (schema.properties && typeof schema.properties === 'object') {\n children.push(...Object.values(schema.properties))\n }\n if (schema.items !== undefined) {\n children.push(\n ...(Array.isArray(schema.items) ? schema.items : [schema.items]),\n )\n }\n if (Array.isArray(schema.anyOf)) {\n children.push(...schema.anyOf)\n }\n\n return children.some(\n (child) => isTypelessSchema(child) || containsTypelessSchema(child),\n )\n}\n\n/**\n * Strict-mode structural rewrite (required widening, nullability,\n * additionalProperties). Kept private so the public entry point can apply the\n * format-stripping pass exactly once over the fully-rewritten tree.\n */\nfunction coerceStrictSchema(\n schema: Record<string, any>,\n originalRequired?: Array<string>,\n): Record<string, any> {\n const result = { ...schema }\n const required =\n originalRequired ??\n (Array.isArray(result['required']) ? result['required'] : [])\n\n if (result.type === 'object' && result.properties) {\n const properties = { ...result.properties }\n const allPropertyNames = Object.keys(properties)\n\n for (const propName of allPropertyNames) {\n let prop = properties[propName]\n const wasOptional = !required.includes(propName)\n\n // Step 1: Recurse into nested structures\n if (prop.type === 'object' && prop.properties) {\n prop = coerceStrictSchema(prop, prop.required || [])\n } else if (prop.type === 'array' && prop.items) {\n prop = {\n ...prop,\n items: coerceStrictSchema(prop.items, prop.items.required || []),\n }\n } else if (prop.anyOf) {\n prop = coerceStrictSchema(prop, prop.required || [])\n } else if (prop.oneOf) {\n throw new Error(\n 'oneOf is not supported in OpenAI structured output schemas. Check the supported outputs here: https://platform.openai.com/docs/guides/structured-outputs#supported-types',\n )\n }\n\n // Step 2: Apply null-widening for optional properties (after recursion)\n if (wasOptional) {\n if (prop.anyOf) {\n // For anyOf, add a null variant if not already present\n if (!prop.anyOf.some((v: any) => v.type === 'null')) {\n prop = { ...prop, anyOf: [...prop.anyOf, { type: 'null' }] }\n }\n } else if (prop.type && !Array.isArray(prop.type)) {\n prop = { ...prop, type: [prop.type, 'null'] }\n } else if (Array.isArray(prop.type) && !prop.type.includes('null')) {\n prop = { ...prop, type: [...prop.type, 'null'] }\n }\n }\n\n properties[propName] = prop\n }\n\n result.properties = properties\n result.required = allPropertyNames\n result.additionalProperties = false\n }\n\n if (result.type === 'array' && result.items) {\n result.items = coerceStrictSchema(result.items, result.items.required || [])\n }\n\n if (result.anyOf && Array.isArray(result.anyOf)) {\n result.anyOf = result.anyOf.map((variant) =>\n coerceStrictSchema(variant, variant.required || []),\n )\n }\n\n if (result.oneOf) {\n throw new Error(\n 'oneOf is not supported in OpenAI structured output schemas. Check the supported outputs here: https://platform.openai.com/docs/guides/structured-outputs#supported-types',\n )\n }\n\n return result\n}\n"],"names":[],"mappings":"AAQA,MAAM,+CAA+B,IAAI;AAAA,EACvC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAWM,SAAS,wBAAwB,MAAgB;AACtD,MAAI,MAAM,QAAQ,IAAI,EAAG,QAAO,KAAK,IAAI,uBAAuB;AAChE,MAAI,SAAS,QAAQ,OAAO,SAAS,SAAU,QAAO;AAEtD,QAAM,MAA2B,CAAA;AACjC,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,IAAI,GAAG;AAC/C,QACE,QAAQ,YACR,OAAO,UAAU,YACjB,CAAC,yBAAyB,IAAI,KAAK,GACnC;AACA;AAAA,IACF;AACA,QAAI,GAAG,IAAI,wBAAwB,KAAK;AAAA,EAC1C;AACA,SAAO;AACT;AAcO,SAAS,+BACd,QACA,kBACqB;AACrB,SAAO,wBAAwB,mBAAmB,QAAQ,gBAAgB,CAAC;AAC7E;AAgBA,MAAM,8BAAqD;AAAA,EACzD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAWA,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AA;AAAA,EAEtD;AAEA,MAAI,OAAO,OAAO;AAChB,UAAM,IAAI;AAAA,MACR;AAAA,IAAA;AAAA,EAEJ;AAEA,SAAO;AACT;"}
|
|
1
|
+
{"version":3,"file":"schema-converter.js","names":[],"sources":["../../../src/utils/schema-converter.ts"],"sourcesContent":["/**\n * String `format` values accepted by OpenAI's strict Structured Outputs subset.\n * Any other format (e.g. \"uri\", \"uri-reference\", \"regex\") causes the API to\n * reject the whole request with `400 ... '<format>' is not a valid format`.\n * MCP servers and hand-written tools routinely declare such formats, so we strip\n * the unsupported ones before sending. See:\n * https://platform.openai.com/docs/guides/structured-outputs#supported-properties\n */\nconst SUPPORTED_STRING_FORMATS = new Set([\n 'date-time',\n 'time',\n 'date',\n 'duration',\n 'email',\n 'hostname',\n 'ipv4',\n 'ipv6',\n 'uuid',\n])\n\n/**\n * Recursively drop JSON-Schema `format` keywords whose value isn't in OpenAI's\n * strict-mode allowlist. Pure — returns a fresh tree and never mutates `node`,\n * so the caller's original tool definition is left intact.\n *\n * A property *named* `format` always has a schema (object/boolean) value, never\n * a bare string, so it is preserved and recursed into; only the `format`\n * *keyword* (whose value is a string) is subject to removal.\n */\nexport function stripUnsupportedFormats(node: any): any {\n if (Array.isArray(node)) return node.map(stripUnsupportedFormats)\n if (node === null || typeof node !== 'object') return node\n\n const out: Record<string, any> = {}\n for (const [key, value] of Object.entries(node)) {\n if (\n key === 'format' &&\n typeof value === 'string' &&\n !SUPPORTED_STRING_FORMATS.has(value)\n ) {\n continue\n }\n out[key] = stripUnsupportedFormats(value)\n }\n return out\n}\n\n/**\n * Transform a JSON schema to be compatible with OpenAI's structured output requirements.\n * OpenAI requires:\n * - All properties must be in the `required` array\n * - Optional fields should have null added to their type union\n * - additionalProperties must be false for objects\n * - String `format` keywords must be from a fixed allowlist (others are stripped)\n *\n * @param schema - JSON schema to transform\n * @param originalRequired - Original required array (to know which fields were optional)\n * @returns Transformed schema compatible with OpenAI structured output\n */\nexport function makeStructuredOutputCompatible(\n schema: Record<string, any>,\n originalRequired?: Array<string>,\n): Record<string, any> {\n return stripUnsupportedFormats(coerceStrictSchema(schema, originalRequired))\n}\n\n/**\n * JSON-Schema keywords outside OpenAI's strict Structured Outputs subset. A\n * schema using any of these can't be coerced into a strict-valid shape, and\n * sending it with `strict: true` makes the API reject the ENTIRE request\n * (e.g. `400 Invalid schema ... 'additionalProperties' is required to be ...`).\n * Tools with such schemas are emitted with `strict: false` instead (see the\n * tool converters) so they remain callable. MCP servers (e.g. Notion) routinely\n * emit these.\n *\n * - `oneOf` / `allOf` / `not` — combinator keywords strict mode rejects\n * - `$ref` / `$defs` / `definitions` — references and definition pools whose\n * object subschemas escape the `additionalProperties: false` normalization\n * strict mode requires\n */\nconst STRICT_UNSUPPORTED_KEYWORDS: ReadonlyArray<string> = [\n 'oneOf',\n 'allOf',\n 'not',\n '$ref',\n '$defs',\n 'definitions',\n]\n\n/**\n * Keys that give a schema node a resolvable type under OpenAI's strict subset.\n * A schema-position node carrying none of these is *typeless* (e.g. the empty\n * `{}` that `z.any()` / `z.unknown()` emit). Strict mode requires every schema\n * to declare a type, so a typeless node 400s the whole request — such tools\n * must be sent with `strict: false` instead. (`oneOf`/`allOf`/`$ref` count as\n * type indicators here even though they're independently strict-unsupported;\n * the keyword check below already rejects them.)\n */\nconst TYPE_INDICATOR_KEYWORDS: ReadonlyArray<string> = [\n 'type',\n 'enum',\n 'const',\n 'anyOf',\n 'oneOf',\n 'allOf',\n '$ref',\n]\n\n/**\n * Returns `false` when `schema` cannot be made strict-compatible and must be\n * sent with `strict: false`. Two ways that happens:\n *\n * 1. It uses a JSON-Schema keyword outside OpenAI's strict subset anywhere in\n * the tree (`oneOf`/`allOf`/`not`/`$ref`/`$defs`).\n * 2. It contains a *typeless* schema node — a property/items/anyOf entry with\n * no `type` (nor `enum`/`const`/combinator), e.g. the `{}` that `z.any()`\n * produces. Strict mode rejects typeless schemas.\n * 3. It contains an open object schema. OpenAI strict mode requires objects to\n * set `additionalProperties: false`, which would change the semantics of a\n * free-form map rather than merely normalizing it.\n *\n * Conservative by design: for (1) keywords are matched as object keys, so a\n * property literally named e.g. `oneOf` also trips it. That only costs that one\n * tool its strict mode, which is strictly safer than a false \"compatible\"\n * verdict that 400s the whole request.\n */\nexport function isStrictModeCompatible(schema: unknown): boolean {\n return (\n !containsStrictUnsupportedKeyword(schema) &&\n !containsTypelessSchema(schema) &&\n !containsOpenObject(schema)\n )\n}\n\n/**\n * Reports object schemas that cannot be closed without changing their input\n * semantics. Objects with `properties` and no explicit\n * `additionalProperties` are safe because `coerceStrictSchema` closes them.\n */\nfunction containsOpenObject(node: unknown): boolean {\n if (Array.isArray(node)) {\n return node.some(containsOpenObject)\n }\n if (node === null || typeof node !== 'object') return false\n\n const schema = node as Record<string, unknown>\n const type = schema['type']\n const isObjectSchema =\n type === 'object' || (Array.isArray(type) && type.includes('object'))\n\n if (isObjectSchema) {\n if (\n 'additionalProperties' in schema &&\n schema['additionalProperties'] !== false\n ) {\n return true\n }\n\n const properties = schema['properties']\n const hasProperties =\n properties !== null &&\n typeof properties === 'object' &&\n !Array.isArray(properties)\n if (!hasProperties && schema['additionalProperties'] !== false) {\n return true\n }\n }\n\n return Object.values(schema).some(containsOpenObject)\n}\n\nfunction containsStrictUnsupportedKeyword(node: unknown): boolean {\n if (Array.isArray(node)) {\n return node.some(containsStrictUnsupportedKeyword)\n }\n if (node === null || typeof node !== 'object') return false\n for (const [key, value] of Object.entries(node)) {\n if (STRICT_UNSUPPORTED_KEYWORDS.includes(key)) return true\n if (containsStrictUnsupportedKeyword(value)) return true\n }\n return false\n}\n\n/** A schema-position node that declares no type and so 400s strict mode. */\nfunction isTypelessSchema(node: unknown): boolean {\n if (node === null || typeof node !== 'object' || Array.isArray(node)) {\n // boolean schemas (`true`/`false`) and non-objects aren't typeless props.\n return false\n }\n return !TYPE_INDICATOR_KEYWORDS.some((key) => key in node)\n}\n\n/**\n * Walks the genuine schema positions (property values, `items`, `anyOf`\n * variants) and reports whether any is typeless. Unlike the keyword walk this\n * must respect structure: an empty `{}` is only a problem at a schema position,\n * not e.g. an empty `properties` map.\n */\nfunction containsTypelessSchema(node: unknown): boolean {\n if (node === null || typeof node !== 'object' || Array.isArray(node)) {\n return false\n }\n const schema = node as Record<string, any>\n\n const children: Array<unknown> = []\n if (schema.properties && typeof schema.properties === 'object') {\n children.push(...Object.values(schema.properties))\n }\n if (schema.items !== undefined) {\n children.push(\n ...(Array.isArray(schema.items) ? schema.items : [schema.items]),\n )\n }\n if (Array.isArray(schema.anyOf)) {\n children.push(...schema.anyOf)\n }\n\n return children.some(\n (child) => isTypelessSchema(child) || containsTypelessSchema(child),\n )\n}\n\n/**\n * Strict-mode structural rewrite (required widening, nullability,\n * additionalProperties). Kept private so the public entry point can apply the\n * format-stripping pass exactly once over the fully-rewritten tree.\n */\nfunction coerceStrictSchema(\n schema: Record<string, any>,\n originalRequired?: Array<string>,\n): Record<string, any> {\n const result = { ...schema }\n const required =\n originalRequired ??\n (Array.isArray(result['required']) ? result['required'] : [])\n\n if (result.type === 'object' && result.properties) {\n const properties = { ...result.properties }\n const allPropertyNames = Object.keys(properties)\n\n for (const propName of allPropertyNames) {\n let prop = properties[propName]\n const wasOptional = !required.includes(propName)\n\n // Step 1: Recurse into nested structures\n if (prop.type === 'object' && prop.properties) {\n prop = coerceStrictSchema(prop, prop.required || [])\n } else if (prop.type === 'array' && prop.items) {\n prop = {\n ...prop,\n items: coerceStrictSchema(prop.items, prop.items.required || []),\n }\n } else if (prop.anyOf) {\n prop = coerceStrictSchema(prop, prop.required || [])\n } else if (prop.oneOf) {\n throw new Error(\n 'oneOf is not supported in OpenAI structured output schemas. Check the supported outputs here: https://platform.openai.com/docs/guides/structured-outputs#supported-types',\n )\n }\n\n // Step 2: Apply null-widening for optional properties (after recursion)\n if (wasOptional) {\n if (prop.anyOf) {\n // For anyOf, add a null variant if not already present\n if (!prop.anyOf.some((v: any) => v.type === 'null')) {\n prop = { ...prop, anyOf: [...prop.anyOf, { type: 'null' }] }\n }\n } else if (prop.type && !Array.isArray(prop.type)) {\n prop = { ...prop, type: [prop.type, 'null'] }\n } else if (Array.isArray(prop.type) && !prop.type.includes('null')) {\n prop = { ...prop, type: [...prop.type, 'null'] }\n }\n }\n\n properties[propName] = prop\n }\n\n result.properties = properties\n result.required = allPropertyNames\n result.additionalProperties = false\n }\n\n if (result.type === 'array' && result.items) {\n result.items = coerceStrictSchema(result.items, result.items.required || [])\n }\n\n if (result.anyOf && Array.isArray(result.anyOf)) {\n result.anyOf = result.anyOf.map((variant) =>\n coerceStrictSchema(variant, variant.required || []),\n )\n }\n\n if (result.oneOf) {\n throw new Error(\n 'oneOf is not supported in OpenAI structured output schemas. Check the supported outputs here: https://platform.openai.com/docs/guides/structured-outputs#supported-types',\n )\n }\n\n return result\n}\n"],"mappings":";;;;;;;;;AAQA,IAAM,2CAA2B,IAAI,IAAI;CACvC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF,CAAC;;;;;;;;;;AAWD,SAAgB,wBAAwB,MAAgB;CACtD,IAAI,MAAM,QAAQ,IAAI,GAAG,OAAO,KAAK,IAAI,uBAAuB;CAChE,IAAI,SAAS,QAAQ,OAAO,SAAS,UAAU,OAAO;CAEtD,MAAM,MAA2B,CAAC;CAClC,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,IAAI,GAAG;EAC/C,IACE,QAAQ,YACR,OAAO,UAAU,YACjB,CAAC,yBAAyB,IAAI,KAAK,GAEnC;EAEF,IAAI,OAAO,wBAAwB,KAAK;CAC1C;CACA,OAAO;AACT;;;;;;;;;;;;;AAcA,SAAgB,+BACd,QACA,kBACqB;CACrB,OAAO,wBAAwB,mBAAmB,QAAQ,gBAAgB,CAAC;AAC7E;;;;;;;;;;;;;;;AAgBA,IAAM,8BAAqD;CACzD;CACA;CACA;CACA;CACA;CACA;AACF;;;;;;;;;;AAWA,IAAM,0BAAiD;CACrD;CACA;CACA;CACA;CACA;CACA;CACA;AACF;;;;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O,CAAC,GAAG,KAAK,OAAO,EAAE,MAAM,OAAO,CAAC;KAAE;IAAA,OAExD,IAAI,KAAK,QAAQ,CAAC,MAAM,QAAQ,KAAK,IAAI,GAC9C,OAAO;KAAE,GAAG;KAAM,MAAM,CAAC,KAAK,MAAM,MAAM;IAAE;SACvC,IAAI,MAAM,QAAQ,KAAK,IAAI,KAAK,CAAC,KAAK,KAAK,SAAS,MAAM,GAC/D,OAAO;KAAE,GAAG;KAAM,MAAM,CAAC,GAAG,KAAK,MAAM,MAAM;IAAE;GAAA;GAInD,WAAW,YAAY;EACzB;EAEA,OAAO,aAAa;EACpB,OAAO,WAAW;EAClB,OAAO,uBAAuB;CAChC;CAEA,IAAI,OAAO,SAAS,WAAW,OAAO,OACpC,OAAO,QAAQ,mBAAmB,OAAO,OAAO,OAAO,MAAM,YAAY,CAAC,CAAC;CAG7E,IAAI,OAAO,SAAS,MAAM,QAAQ,OAAO,KAAK,GAC5C,OAAO,QAAQ,OAAO,MAAM,KAAK,YAC/B,mBAAmB,SAAS,QAAQ,YAAY,CAAC,CAAC,CACpD;CAGF,IAAI,OAAO,OACT,MAAM,IAAI,MACR,0KACF;CAGF,OAAO;AACT"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/openai-base",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.10",
|
|
4
4
|
"description": "Shared OpenAI SDK base adapters for TanStack AI providers using Chat Completions and Responses APIs.",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -44,23 +44,23 @@
|
|
|
44
44
|
],
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"openai": "^6.41.0",
|
|
47
|
-
"@tanstack/ai-utils": "0.
|
|
47
|
+
"@tanstack/ai-utils": "0.4.0"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
|
-
"@tanstack/ai": "^0.
|
|
50
|
+
"@tanstack/ai": "^0.43.0"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@vitest/coverage-v8": "4.0.14",
|
|
54
|
-
"vite": "^
|
|
54
|
+
"vite": "^8.1.4",
|
|
55
55
|
"zod": "^4.2.0",
|
|
56
|
-
"@tanstack/ai": "0.
|
|
56
|
+
"@tanstack/ai": "0.43.0"
|
|
57
57
|
},
|
|
58
58
|
"scripts": {
|
|
59
59
|
"build": "vite build",
|
|
60
60
|
"clean": "premove ./build ./dist",
|
|
61
|
-
"lint:fix": "
|
|
61
|
+
"lint:fix": "oxlint src --type-aware --fix",
|
|
62
62
|
"test:build": "publint --strict",
|
|
63
|
-
"test:
|
|
63
|
+
"test:oxlint": "oxlint src --type-aware",
|
|
64
64
|
"test:lib": "vitest run",
|
|
65
65
|
"test:lib:dev": "pnpm test:lib --watch",
|
|
66
66
|
"test:types": "tsc"
|
|
@@ -85,9 +85,12 @@ export abstract class OpenAIBaseResponsesTextAdapter<
|
|
|
85
85
|
}
|
|
86
86
|
>()
|
|
87
87
|
|
|
88
|
-
// AG-UI lifecycle tracking
|
|
88
|
+
// AG-UI lifecycle tracking. Honor a caller-supplied `runId` (as `threadId`
|
|
89
|
+
// already does) so the emitted RUN_STARTED matches the id the caller keys
|
|
90
|
+
// durability by — e.g. a summarize run threading the client's runId through
|
|
91
|
+
// for mid-run reload resumability. Falls back to a generated id.
|
|
89
92
|
const aguiState = {
|
|
90
|
-
runId: generateId(this.name),
|
|
93
|
+
runId: options.runId ?? generateId(this.name),
|
|
91
94
|
threadId: options.threadId ?? generateId(this.name),
|
|
92
95
|
messageId: generateId(this.name),
|
|
93
96
|
hasEmittedRunStarted: false,
|
package/dist/esm/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;"}
|