@ai-sdk/open-responses 2.0.56 → 2.0.58
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/CHANGELOG.md +18 -0
- package/dist/index.d.ts +241 -235
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1321 -1737
- package/dist/index.js.map +1 -1
- package/package.json +8 -8
package/dist/index.js
CHANGED
|
@@ -1,1787 +1,1371 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
var coreItemTypes = /* @__PURE__ */ new Set([
|
|
16
|
-
"function_call",
|
|
17
|
-
"function_call_output",
|
|
18
|
-
"item_reference",
|
|
19
|
-
"message",
|
|
20
|
-
"reasoning"
|
|
1
|
+
import { APICallError, NoSuchModelError, UnsupportedFunctionalityError } from "@ai-sdk/provider";
|
|
2
|
+
import { SerializationError, WORKFLOW_DESERIALIZE, WORKFLOW_SERIALIZE, combineHeaders, convertToBase64, createEventSourceResponseHandler, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderDefinedToolFactory, createProviderStreamError, generateId, getTopLevelMediaType, isCustomReasoning, jsonSchema, lazySchema, mapReasoningToProviderEffort, parseProviderOptions, postJsonToApi, resolveFullMediaType, serializeModelOptions, withUserAgentSuffix, zodSchema } from "@ai-sdk/provider-utils";
|
|
3
|
+
import { z } from "zod/v4";
|
|
4
|
+
//#region src/version.ts
|
|
5
|
+
const VERSION = "2.0.58";
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region src/open-responses-extension.ts
|
|
8
|
+
const coreToolTypes = /* @__PURE__ */ new Set(["allowed_tools", "function"]);
|
|
9
|
+
const coreItemTypes = /* @__PURE__ */ new Set([
|
|
10
|
+
"function_call",
|
|
11
|
+
"function_call_output",
|
|
12
|
+
"item_reference",
|
|
13
|
+
"message",
|
|
14
|
+
"reasoning"
|
|
21
15
|
]);
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
16
|
+
const coreEventTypes = /* @__PURE__ */ new Set([
|
|
17
|
+
"error",
|
|
18
|
+
"response.completed",
|
|
19
|
+
"response.content_part.added",
|
|
20
|
+
"response.content_part.done",
|
|
21
|
+
"response.created",
|
|
22
|
+
"response.failed",
|
|
23
|
+
"response.function_call_arguments.delta",
|
|
24
|
+
"response.function_call_arguments.done",
|
|
25
|
+
"response.in_progress",
|
|
26
|
+
"response.incomplete",
|
|
27
|
+
"response.output_item.added",
|
|
28
|
+
"response.output_item.done",
|
|
29
|
+
"response.output_text.delta",
|
|
30
|
+
"response.output_text.done",
|
|
31
|
+
"response.reasoning_summary_part.added",
|
|
32
|
+
"response.reasoning_summary_part.done",
|
|
33
|
+
"response.reasoning_summary_text.delta",
|
|
34
|
+
"response.reasoning_summary_text.done",
|
|
35
|
+
"response.reasoning_text.delta",
|
|
36
|
+
"response.refusal.delta",
|
|
37
|
+
"response.refusal.done"
|
|
44
38
|
]);
|
|
45
39
|
function createOpenResponsesExtensionRegistry(extensions) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
...extension.bareEventTypes ?? []
|
|
177
|
-
];
|
|
178
|
-
const hasEventTypes = extension.eventTypes != null || extension.bareEventTypes != null;
|
|
179
|
-
const hasEventDecoder = extension.decodeEvent != null;
|
|
180
|
-
if (hasEventTypes !== hasEventDecoder) {
|
|
181
|
-
throw new Error(
|
|
182
|
-
`Open Responses extension ${extension.id} must provide eventTypes or bareEventTypes together with decodeEvent.`
|
|
183
|
-
);
|
|
184
|
-
}
|
|
185
|
-
if (hasEventTypes && hasEventDecoder) {
|
|
186
|
-
const eventExtension = extension;
|
|
187
|
-
if (eventTypes.length === 0) {
|
|
188
|
-
throw new Error(
|
|
189
|
-
`Open Responses extension ${extension.id} must register at least one event type.`
|
|
190
|
-
);
|
|
191
|
-
}
|
|
192
|
-
for (const eventType of extension.eventTypes ?? []) {
|
|
193
|
-
assertNamespacedType({
|
|
194
|
-
extensionId: extension.id,
|
|
195
|
-
namespace,
|
|
196
|
-
type: eventType,
|
|
197
|
-
field: "eventTypes"
|
|
198
|
-
});
|
|
199
|
-
registerUnique({
|
|
200
|
-
map: registry.byEventType,
|
|
201
|
-
key: eventType,
|
|
202
|
-
extension: eventExtension,
|
|
203
|
-
field: "event type"
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
for (const eventType of extension.bareEventTypes ?? []) {
|
|
207
|
-
assertBareType({
|
|
208
|
-
coreTypes: coreEventTypes,
|
|
209
|
-
extensionId: extension.id,
|
|
210
|
-
type: eventType,
|
|
211
|
-
field: "bareEventTypes"
|
|
212
|
-
});
|
|
213
|
-
registerUnique({
|
|
214
|
-
map: registry.byEventType,
|
|
215
|
-
key: eventType,
|
|
216
|
-
extension: eventExtension,
|
|
217
|
-
field: "event type"
|
|
218
|
-
});
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
if (!hasToolEncoder && !hasItemDecoder && !hasEventDecoder) {
|
|
222
|
-
throw new Error(
|
|
223
|
-
`Open Responses extension ${extension.id} must register a tool, item, or event capability.`
|
|
224
|
-
);
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
return registry;
|
|
40
|
+
const registry = {
|
|
41
|
+
byEventType: /* @__PURE__ */ new Map(),
|
|
42
|
+
byExtensionId: /* @__PURE__ */ new Map(),
|
|
43
|
+
byItemType: /* @__PURE__ */ new Map(),
|
|
44
|
+
byProviderToolId: /* @__PURE__ */ new Map(),
|
|
45
|
+
byToolType: /* @__PURE__ */ new Map()
|
|
46
|
+
};
|
|
47
|
+
for (const extension of extensions ?? []) {
|
|
48
|
+
const namespaceSeparatorIndex = extension.id.indexOf(".");
|
|
49
|
+
if (namespaceSeparatorIndex <= 0) throw new Error(`Open Responses extension ID ${extension.id} must use <implementor>.<extension> format.`);
|
|
50
|
+
const namespace = extension.id.slice(0, namespaceSeparatorIndex);
|
|
51
|
+
registerUnique({
|
|
52
|
+
map: registry.byExtensionId,
|
|
53
|
+
key: extension.id,
|
|
54
|
+
extension,
|
|
55
|
+
field: "id"
|
|
56
|
+
});
|
|
57
|
+
if ((extension.bareToolType != null || extension.bareItemTypes != null || extension.bareEventTypes != null) !== (extension.allowBareTypes === true)) throw new Error(`Open Responses extension ${extension.id} must set allowBareTypes to true exactly when registering bare types.`);
|
|
58
|
+
if (extension.toolType != null && extension.bareToolType != null) throw new Error(`Open Responses extension ${extension.id} cannot provide toolType and bareToolType together.`);
|
|
59
|
+
const toolType = extension.toolType ?? extension.bareToolType;
|
|
60
|
+
const hasToolType = toolType != null;
|
|
61
|
+
const hasToolEncoder = extension.encodeTool != null;
|
|
62
|
+
if (hasToolType !== hasToolEncoder) {
|
|
63
|
+
const typeField = extension.bareToolType != null ? "bareToolType" : "toolType";
|
|
64
|
+
throw new Error(`Open Responses extension ${extension.id} must provide ${typeField} and encodeTool together.`);
|
|
65
|
+
}
|
|
66
|
+
if (extension.encodeToolChoice != null && !hasToolEncoder) throw new Error(`Open Responses extension ${extension.id} cannot provide encodeToolChoice without a tool type and encodeTool.`);
|
|
67
|
+
if (hasToolType && hasToolEncoder) {
|
|
68
|
+
const toolExtension = extension;
|
|
69
|
+
if (extension.toolType != null) assertNamespacedType({
|
|
70
|
+
extensionId: extension.id,
|
|
71
|
+
namespace,
|
|
72
|
+
type: extension.toolType,
|
|
73
|
+
field: "toolType"
|
|
74
|
+
});
|
|
75
|
+
else assertBareType({
|
|
76
|
+
coreTypes: coreToolTypes,
|
|
77
|
+
extensionId: extension.id,
|
|
78
|
+
type: extension.bareToolType,
|
|
79
|
+
field: "bareToolType"
|
|
80
|
+
});
|
|
81
|
+
registerUnique({
|
|
82
|
+
map: registry.byProviderToolId,
|
|
83
|
+
key: extension.id,
|
|
84
|
+
extension: toolExtension,
|
|
85
|
+
field: "provider-tool id"
|
|
86
|
+
});
|
|
87
|
+
registerUnique({
|
|
88
|
+
map: registry.byToolType,
|
|
89
|
+
key: toolType,
|
|
90
|
+
extension: toolExtension,
|
|
91
|
+
field: "tool type"
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
const itemTypes = [...extension.itemTypes ?? [], ...extension.bareItemTypes ?? []];
|
|
95
|
+
const hasItemTypes = extension.itemTypes != null || extension.bareItemTypes != null;
|
|
96
|
+
const hasItemDecoder = extension.decodeItem != null;
|
|
97
|
+
if (hasItemTypes !== hasItemDecoder) throw new Error(`Open Responses extension ${extension.id} must provide itemTypes or bareItemTypes together with decodeItem.`);
|
|
98
|
+
if (extension.encodeInputItem != null && !hasItemDecoder) throw new Error(`Open Responses extension ${extension.id} cannot provide encodeInputItem without itemTypes or bareItemTypes and decodeItem.`);
|
|
99
|
+
if (hasItemTypes && hasItemDecoder) {
|
|
100
|
+
const itemExtension = extension;
|
|
101
|
+
if (itemTypes.length === 0) throw new Error(`Open Responses extension ${extension.id} must register at least one item type.`);
|
|
102
|
+
for (const itemType of extension.itemTypes ?? []) {
|
|
103
|
+
assertNamespacedType({
|
|
104
|
+
extensionId: extension.id,
|
|
105
|
+
namespace,
|
|
106
|
+
type: itemType,
|
|
107
|
+
field: "itemTypes"
|
|
108
|
+
});
|
|
109
|
+
registerUnique({
|
|
110
|
+
map: registry.byItemType,
|
|
111
|
+
key: itemType,
|
|
112
|
+
extension: itemExtension,
|
|
113
|
+
field: "item type"
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
for (const itemType of extension.bareItemTypes ?? []) {
|
|
117
|
+
assertBareType({
|
|
118
|
+
coreTypes: coreItemTypes,
|
|
119
|
+
extensionId: extension.id,
|
|
120
|
+
type: itemType,
|
|
121
|
+
field: "bareItemTypes"
|
|
122
|
+
});
|
|
123
|
+
registerUnique({
|
|
124
|
+
map: registry.byItemType,
|
|
125
|
+
key: itemType,
|
|
126
|
+
extension: itemExtension,
|
|
127
|
+
field: "item type"
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
const eventTypes = [...extension.eventTypes ?? [], ...extension.bareEventTypes ?? []];
|
|
132
|
+
const hasEventTypes = extension.eventTypes != null || extension.bareEventTypes != null;
|
|
133
|
+
const hasEventDecoder = extension.decodeEvent != null;
|
|
134
|
+
if (hasEventTypes !== hasEventDecoder) throw new Error(`Open Responses extension ${extension.id} must provide eventTypes or bareEventTypes together with decodeEvent.`);
|
|
135
|
+
if (hasEventTypes && hasEventDecoder) {
|
|
136
|
+
const eventExtension = extension;
|
|
137
|
+
if (eventTypes.length === 0) throw new Error(`Open Responses extension ${extension.id} must register at least one event type.`);
|
|
138
|
+
for (const eventType of extension.eventTypes ?? []) {
|
|
139
|
+
assertNamespacedType({
|
|
140
|
+
extensionId: extension.id,
|
|
141
|
+
namespace,
|
|
142
|
+
type: eventType,
|
|
143
|
+
field: "eventTypes"
|
|
144
|
+
});
|
|
145
|
+
registerUnique({
|
|
146
|
+
map: registry.byEventType,
|
|
147
|
+
key: eventType,
|
|
148
|
+
extension: eventExtension,
|
|
149
|
+
field: "event type"
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
for (const eventType of extension.bareEventTypes ?? []) {
|
|
153
|
+
assertBareType({
|
|
154
|
+
coreTypes: coreEventTypes,
|
|
155
|
+
extensionId: extension.id,
|
|
156
|
+
type: eventType,
|
|
157
|
+
field: "bareEventTypes"
|
|
158
|
+
});
|
|
159
|
+
registerUnique({
|
|
160
|
+
map: registry.byEventType,
|
|
161
|
+
key: eventType,
|
|
162
|
+
extension: eventExtension,
|
|
163
|
+
field: "event type"
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
if (!hasToolEncoder && !hasItemDecoder && !hasEventDecoder) throw new Error(`Open Responses extension ${extension.id} must register a tool, item, or event capability.`);
|
|
168
|
+
}
|
|
169
|
+
return registry;
|
|
228
170
|
}
|
|
229
|
-
function assertNamespacedType({
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
type,
|
|
233
|
-
field
|
|
234
|
-
}) {
|
|
235
|
-
const separatorIndex = type.indexOf(":");
|
|
236
|
-
if (separatorIndex <= 0 || type.slice(0, separatorIndex) !== namespace) {
|
|
237
|
-
throw new Error(
|
|
238
|
-
`Open Responses extension ${extensionId} has invalid ${field} value ${type}. Extension wire types must use the ${namespace}: namespace.`
|
|
239
|
-
);
|
|
240
|
-
}
|
|
171
|
+
function assertNamespacedType({ extensionId, namespace, type, field }) {
|
|
172
|
+
const separatorIndex = type.indexOf(":");
|
|
173
|
+
if (separatorIndex <= 0 || type.slice(0, separatorIndex) !== namespace) throw new Error(`Open Responses extension ${extensionId} has invalid ${field} value ${type}. Extension wire types must use the ${namespace}: namespace.`);
|
|
241
174
|
}
|
|
242
|
-
function assertBareType({
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
type,
|
|
246
|
-
field
|
|
247
|
-
}) {
|
|
248
|
-
if (type.length === 0 || type.includes(":")) {
|
|
249
|
-
throw new Error(
|
|
250
|
-
`Open Responses extension ${extensionId} has invalid ${field} value ${type}. Bare extension wire types must be non-empty and must not contain a colon.`
|
|
251
|
-
);
|
|
252
|
-
}
|
|
253
|
-
if (coreTypes.has(type)) {
|
|
254
|
-
throw new Error(
|
|
255
|
-
`Open Responses extension ${extensionId} cannot register core ${field} value ${type}.`
|
|
256
|
-
);
|
|
257
|
-
}
|
|
175
|
+
function assertBareType({ coreTypes, extensionId, type, field }) {
|
|
176
|
+
if (type.length === 0 || type.includes(":")) throw new Error(`Open Responses extension ${extensionId} has invalid ${field} value ${type}. Bare extension wire types must be non-empty and must not contain a colon.`);
|
|
177
|
+
if (coreTypes.has(type)) throw new Error(`Open Responses extension ${extensionId} cannot register core ${field} value ${type}.`);
|
|
258
178
|
}
|
|
259
179
|
function getOpenResponsesExtensionItemTypes(extension) {
|
|
260
|
-
|
|
180
|
+
return [...extension.itemTypes ?? [], ...extension.bareItemTypes ?? []];
|
|
261
181
|
}
|
|
262
182
|
function getOpenResponsesExtensionToolType(extension) {
|
|
263
|
-
|
|
183
|
+
return extension.toolType ?? extension.bareToolType;
|
|
264
184
|
}
|
|
185
|
+
/**
|
|
186
|
+
* Keeps the internal API unions narrow enough to discriminate core wire types.
|
|
187
|
+
* Bare records only reach this boundary after registry validation.
|
|
188
|
+
*/
|
|
265
189
|
function asOpenResponsesExtensionRecord(value) {
|
|
266
|
-
|
|
190
|
+
return value;
|
|
267
191
|
}
|
|
268
|
-
function registerUnique({
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
field
|
|
273
|
-
}) {
|
|
274
|
-
const existing = map.get(key);
|
|
275
|
-
if (existing != null) {
|
|
276
|
-
throw new Error(
|
|
277
|
-
`Open Responses extension ${extension.id} cannot register ${field} ${String(key)} because it is already registered by ${existing.id}.`
|
|
278
|
-
);
|
|
279
|
-
}
|
|
280
|
-
map.set(key, extension);
|
|
192
|
+
function registerUnique({ map, key, extension, field }) {
|
|
193
|
+
const existing = map.get(key);
|
|
194
|
+
if (existing != null) throw new Error(`Open Responses extension ${extension.id} cannot register ${field} ${String(key)} because it is already registered by ${existing.id}.`);
|
|
195
|
+
map.set(key, extension);
|
|
281
196
|
}
|
|
282
197
|
function isOpenResponsesExtensionRecord(value) {
|
|
283
|
-
|
|
198
|
+
return isOpenResponsesJSONObject(value) && typeof value.type === "string";
|
|
284
199
|
}
|
|
285
200
|
function isOpenResponsesJSONObject(value) {
|
|
286
|
-
|
|
201
|
+
return value != null && typeof value === "object" && !Array.isArray(value) && Object.getPrototypeOf(value) === Object.prototype && Object.values(value).every(isOpenResponsesJSONValue);
|
|
287
202
|
}
|
|
288
203
|
function isOpenResponsesExtensionItem(value) {
|
|
289
|
-
|
|
204
|
+
return isOpenResponsesExtensionRecord(value) && typeof value.id === "string" && typeof value.status === "string";
|
|
290
205
|
}
|
|
291
206
|
function isOpenResponsesExtensionEvent(value) {
|
|
292
|
-
|
|
207
|
+
return isOpenResponsesExtensionRecord(value) && typeof value.sequence_number === "number";
|
|
293
208
|
}
|
|
294
209
|
function isOpenResponsesJSONValue(value) {
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
if (Array.isArray(value)) {
|
|
299
|
-
return value.every(isOpenResponsesJSONValue);
|
|
300
|
-
}
|
|
301
|
-
return isOpenResponsesJSONObject(value);
|
|
210
|
+
if (value == null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") return true;
|
|
211
|
+
if (Array.isArray(value)) return value.every(isOpenResponsesJSONValue);
|
|
212
|
+
return isOpenResponsesJSONObject(value);
|
|
302
213
|
}
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
inputSchema: customInputSchema
|
|
318
|
-
});
|
|
319
|
-
return {
|
|
320
|
-
/**
|
|
321
|
-
* Creates a caller-executed OpenAI-compatible custom tool. Custom tools accept
|
|
322
|
-
* a raw string, optionally constrained by a grammar. Requires endpoint support.
|
|
323
|
-
*/
|
|
324
|
-
customTool: (options) => customToolFactory(options)
|
|
325
|
-
};
|
|
214
|
+
//#endregion
|
|
215
|
+
//#region src/open-responses-tools.ts
|
|
216
|
+
const customInputSchema = lazySchema(() => zodSchema(z.string()));
|
|
217
|
+
function createOpenResponsesTools({ customToolId }) {
|
|
218
|
+
const customToolFactory = createProviderDefinedToolFactory({
|
|
219
|
+
id: customToolId,
|
|
220
|
+
inputSchema: customInputSchema
|
|
221
|
+
});
|
|
222
|
+
return {
|
|
223
|
+
/**
|
|
224
|
+
* Creates a caller-executed OpenAI-compatible custom tool. Custom tools accept
|
|
225
|
+
* a raw string, optionally constrained by a grammar. Requires endpoint support.
|
|
226
|
+
*/
|
|
227
|
+
customTool: (options) => customToolFactory(options) };
|
|
326
228
|
}
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
})
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
}
|
|
615
|
-
}
|
|
616
|
-
continue;
|
|
617
|
-
}
|
|
618
|
-
const providerTool = providerToolsByName.get(part.toolName);
|
|
619
|
-
const extension = providerTool == null ? void 0 : extensionRegistry?.byProviderToolId.get(providerTool.id);
|
|
620
|
-
if (providerTool != null && extension != null) {
|
|
621
|
-
const encoded = await encodeExtensionInputPart({
|
|
622
|
-
extensionRegistry,
|
|
623
|
-
part,
|
|
624
|
-
providerTool
|
|
625
|
-
});
|
|
626
|
-
if (encoded == null) {
|
|
627
|
-
warnings.push({
|
|
628
|
-
type: "unsupported",
|
|
629
|
-
feature: `provider-defined tool ${providerTool.id} tool-result history`
|
|
630
|
-
});
|
|
631
|
-
} else {
|
|
632
|
-
input.push(...encoded.map(asOpenResponsesExtensionRecord));
|
|
633
|
-
}
|
|
634
|
-
continue;
|
|
635
|
-
}
|
|
636
|
-
const output = part.output;
|
|
637
|
-
let contentValue;
|
|
638
|
-
switch (output.type) {
|
|
639
|
-
case "text":
|
|
640
|
-
case "error-text":
|
|
641
|
-
contentValue = output.value;
|
|
642
|
-
break;
|
|
643
|
-
case "execution-denied":
|
|
644
|
-
contentValue = output.reason ?? "Tool call execution denied.";
|
|
645
|
-
break;
|
|
646
|
-
case "json":
|
|
647
|
-
case "error-json":
|
|
648
|
-
contentValue = JSON.stringify(output.value);
|
|
649
|
-
break;
|
|
650
|
-
case "content": {
|
|
651
|
-
const contentParts = [];
|
|
652
|
-
for (const item of output.value) {
|
|
653
|
-
switch (item.type) {
|
|
654
|
-
case "text": {
|
|
655
|
-
contentParts.push({
|
|
656
|
-
type: "input_text",
|
|
657
|
-
text: item.text
|
|
658
|
-
});
|
|
659
|
-
break;
|
|
660
|
-
}
|
|
661
|
-
case "file": {
|
|
662
|
-
const topLevel = getTopLevelMediaType(item.mediaType);
|
|
663
|
-
if (item.data.type === "data") {
|
|
664
|
-
const fullMediaType = resolveFullMediaType({
|
|
665
|
-
part: item
|
|
666
|
-
});
|
|
667
|
-
if (topLevel === "image") {
|
|
668
|
-
contentParts.push({
|
|
669
|
-
type: "input_image",
|
|
670
|
-
image_url: `data:${fullMediaType};base64,${convertToBase64(item.data.data)}`,
|
|
671
|
-
detail: getImageDetail(item, providerOptionsName)
|
|
672
|
-
});
|
|
673
|
-
} else {
|
|
674
|
-
contentParts.push({
|
|
675
|
-
type: "input_file",
|
|
676
|
-
filename: item.filename ?? "data",
|
|
677
|
-
file_data: `data:${fullMediaType};base64,${convertToBase64(item.data.data)}`
|
|
678
|
-
});
|
|
679
|
-
}
|
|
680
|
-
} else if (item.data.type === "url") {
|
|
681
|
-
if (topLevel === "image") {
|
|
682
|
-
contentParts.push({
|
|
683
|
-
type: "input_image",
|
|
684
|
-
image_url: item.data.url.toString(),
|
|
685
|
-
detail: getImageDetail(item, providerOptionsName)
|
|
686
|
-
});
|
|
687
|
-
} else {
|
|
688
|
-
contentParts.push({
|
|
689
|
-
type: "input_file",
|
|
690
|
-
file_url: item.data.url.toString()
|
|
691
|
-
});
|
|
692
|
-
}
|
|
693
|
-
} else {
|
|
694
|
-
warnings.push({
|
|
695
|
-
type: "other",
|
|
696
|
-
message: `unsupported tool content part type: ${item.type} with data type: ${item.data.type}`
|
|
697
|
-
});
|
|
698
|
-
}
|
|
699
|
-
break;
|
|
700
|
-
}
|
|
701
|
-
default: {
|
|
702
|
-
warnings.push({
|
|
703
|
-
type: "other",
|
|
704
|
-
message: `unsupported tool content part type: ${item.type}`
|
|
705
|
-
});
|
|
706
|
-
break;
|
|
707
|
-
}
|
|
708
|
-
}
|
|
709
|
-
}
|
|
710
|
-
contentValue = contentParts;
|
|
711
|
-
break;
|
|
712
|
-
}
|
|
713
|
-
}
|
|
714
|
-
input.push({
|
|
715
|
-
type: customToolId != null && providerTool?.id === customToolId ? "custom_tool_call_output" : "function_call_output",
|
|
716
|
-
call_id: part.toolCallId,
|
|
717
|
-
output: contentValue
|
|
718
|
-
});
|
|
719
|
-
}
|
|
720
|
-
}
|
|
721
|
-
break;
|
|
722
|
-
}
|
|
723
|
-
}
|
|
724
|
-
}
|
|
725
|
-
return {
|
|
726
|
-
input,
|
|
727
|
-
instructions: systemMessages.length > 0 ? systemMessages.join("\n") : void 0,
|
|
728
|
-
warnings
|
|
729
|
-
};
|
|
229
|
+
//#endregion
|
|
230
|
+
//#region src/responses/convert-to-open-responses-input.ts
|
|
231
|
+
async function convertToOpenResponsesInput({ prompt, providerOptionsName = "open-responses", extensionRegistry, providerToolsByName = /* @__PURE__ */ new Map(), strictResponseInput = false, customToolId }) {
|
|
232
|
+
const input = [];
|
|
233
|
+
const warnings = [];
|
|
234
|
+
const systemMessages = [];
|
|
235
|
+
const replayedExtensionItems = /* @__PURE__ */ new Set();
|
|
236
|
+
for (const { role, content } of prompt) switch (role) {
|
|
237
|
+
case "system":
|
|
238
|
+
systemMessages.push(content);
|
|
239
|
+
break;
|
|
240
|
+
case "user": {
|
|
241
|
+
const userContent = [];
|
|
242
|
+
for (const part of content) switch (part.type) {
|
|
243
|
+
case "text":
|
|
244
|
+
userContent.push({
|
|
245
|
+
type: "input_text",
|
|
246
|
+
text: part.text
|
|
247
|
+
});
|
|
248
|
+
break;
|
|
249
|
+
case "file": switch (part.data.type) {
|
|
250
|
+
case "reference": throw new UnsupportedFunctionalityError({ functionality: "file parts with provider references" });
|
|
251
|
+
case "text": throw new UnsupportedFunctionalityError({ functionality: "text file parts" });
|
|
252
|
+
case "url":
|
|
253
|
+
case "data": if (getTopLevelMediaType(part.mediaType) === "image") userContent.push({
|
|
254
|
+
type: "input_image",
|
|
255
|
+
...part.data.type === "url" ? { image_url: part.data.url.toString() } : { image_url: `data:${resolveFullMediaType({ part })};base64,${convertToBase64(part.data.data)}` },
|
|
256
|
+
detail: getImageDetail(part, providerOptionsName)
|
|
257
|
+
});
|
|
258
|
+
else if (part.data.type === "url") userContent.push({
|
|
259
|
+
type: "input_file",
|
|
260
|
+
file_url: part.data.url.toString()
|
|
261
|
+
});
|
|
262
|
+
else {
|
|
263
|
+
const fullMediaType = resolveFullMediaType({ part });
|
|
264
|
+
userContent.push({
|
|
265
|
+
type: "input_file",
|
|
266
|
+
filename: part.filename ?? "data",
|
|
267
|
+
file_data: `data:${fullMediaType};base64,${convertToBase64(part.data.data)}`
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
input.push({
|
|
273
|
+
type: "message",
|
|
274
|
+
role: "user",
|
|
275
|
+
content: userContent
|
|
276
|
+
});
|
|
277
|
+
break;
|
|
278
|
+
}
|
|
279
|
+
case "assistant": {
|
|
280
|
+
let assistantContent = [];
|
|
281
|
+
let assistantMessageId;
|
|
282
|
+
const flushAssistantContent = () => {
|
|
283
|
+
if (assistantContent.length === 0) return;
|
|
284
|
+
if (strictResponseInput && assistantMessageId == null) input.push({
|
|
285
|
+
type: "message",
|
|
286
|
+
role: "assistant",
|
|
287
|
+
content: assistantContent.map((part) => part.type === "output_text" ? part.text : part.refusal).join("")
|
|
288
|
+
});
|
|
289
|
+
else if (strictResponseInput) input.push({
|
|
290
|
+
id: assistantMessageId,
|
|
291
|
+
type: "message",
|
|
292
|
+
status: "completed",
|
|
293
|
+
role: "assistant",
|
|
294
|
+
content: assistantContent.map((part) => part.type === "output_text" ? {
|
|
295
|
+
...part,
|
|
296
|
+
annotations: part.annotations ?? [],
|
|
297
|
+
logprobs: part.logprobs ?? []
|
|
298
|
+
} : part)
|
|
299
|
+
});
|
|
300
|
+
else input.push({
|
|
301
|
+
type: "message",
|
|
302
|
+
role: "assistant",
|
|
303
|
+
content: assistantContent,
|
|
304
|
+
...assistantMessageId != null && { id: assistantMessageId }
|
|
305
|
+
});
|
|
306
|
+
assistantContent = [];
|
|
307
|
+
assistantMessageId = void 0;
|
|
308
|
+
};
|
|
309
|
+
for (const part of content) {
|
|
310
|
+
const extensionReplay = getExtensionReplay({
|
|
311
|
+
part,
|
|
312
|
+
providerOptionsName,
|
|
313
|
+
extensionRegistry
|
|
314
|
+
});
|
|
315
|
+
if (extensionReplay != null) {
|
|
316
|
+
flushAssistantContent();
|
|
317
|
+
const replayItem = extensionReplay.item;
|
|
318
|
+
if (replayItem != null) {
|
|
319
|
+
const replayKey = `${replayItem.type}:${replayItem.id}`;
|
|
320
|
+
if (!replayedExtensionItems.has(replayKey)) {
|
|
321
|
+
input.push(asOpenResponsesExtensionRecord(replayItem));
|
|
322
|
+
replayedExtensionItems.add(replayKey);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
continue;
|
|
326
|
+
}
|
|
327
|
+
if (part.type === "tool-call" || part.type === "tool-result") {
|
|
328
|
+
const providerTool = providerToolsByName.get(part.toolName);
|
|
329
|
+
const extension = providerTool == null ? void 0 : extensionRegistry?.byProviderToolId.get(providerTool.id);
|
|
330
|
+
if (providerTool != null && extension != null) {
|
|
331
|
+
flushAssistantContent();
|
|
332
|
+
const encoded = await encodeExtensionInputPart({
|
|
333
|
+
extensionRegistry,
|
|
334
|
+
part,
|
|
335
|
+
providerTool
|
|
336
|
+
});
|
|
337
|
+
if (encoded == null) warnings.push({
|
|
338
|
+
type: "unsupported",
|
|
339
|
+
feature: `provider-defined tool ${providerTool.id} ${part.type} history`
|
|
340
|
+
});
|
|
341
|
+
else input.push(...encoded.map(asOpenResponsesExtensionRecord));
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
switch (part.type) {
|
|
346
|
+
case "reasoning": {
|
|
347
|
+
flushAssistantContent();
|
|
348
|
+
const providerData = getProviderData(part, providerOptionsName);
|
|
349
|
+
const itemId = typeof providerData?.itemId === "string" ? providerData.itemId : void 0;
|
|
350
|
+
const summary = parseReasoningSummary(providerData?.reasoningSummary);
|
|
351
|
+
const reasoningContent = parseReasoningContent(providerData?.reasoningContent);
|
|
352
|
+
const hasReasoningContent = providerData != null && "reasoningContent" in providerData;
|
|
353
|
+
const encryptedContent = typeof providerData?.reasoningEncryptedContent === "string" ? providerData.reasoningEncryptedContent : void 0;
|
|
354
|
+
const reasoningItem = {
|
|
355
|
+
type: "reasoning",
|
|
356
|
+
summary: summary ?? [],
|
|
357
|
+
...itemId != null && { id: itemId },
|
|
358
|
+
...reasoningContent != null ? { content: reasoningContent } : !hasReasoningContent && part.text.length > 0 ? { content: [{
|
|
359
|
+
type: "reasoning_text",
|
|
360
|
+
text: part.text
|
|
361
|
+
}] } : {},
|
|
362
|
+
...encryptedContent != null && { encrypted_content: encryptedContent }
|
|
363
|
+
};
|
|
364
|
+
const previousItem = input[input.length - 1];
|
|
365
|
+
if (reasoningItem.id != null && previousItem?.type === "reasoning" && previousItem.id === reasoningItem.id) {
|
|
366
|
+
if (reasoningItem.content != null) previousItem.content = [...previousItem.content ?? [], ...reasoningItem.content];
|
|
367
|
+
} else input.push(reasoningItem);
|
|
368
|
+
break;
|
|
369
|
+
}
|
|
370
|
+
case "text": {
|
|
371
|
+
const providerData = getProviderData(part, providerOptionsName);
|
|
372
|
+
const itemId = typeof providerData?.itemId === "string" ? providerData.itemId : void 0;
|
|
373
|
+
const annotations = parseOutputTextAnnotations(providerData?.annotations);
|
|
374
|
+
if (assistantContent.length > 0 && assistantMessageId !== itemId) flushAssistantContent();
|
|
375
|
+
assistantMessageId = itemId;
|
|
376
|
+
assistantContent.push({
|
|
377
|
+
type: "output_text",
|
|
378
|
+
text: part.text,
|
|
379
|
+
...annotations != null && { annotations }
|
|
380
|
+
});
|
|
381
|
+
break;
|
|
382
|
+
}
|
|
383
|
+
case "tool-call": {
|
|
384
|
+
flushAssistantContent();
|
|
385
|
+
const argumentsValue = typeof part.input === "string" ? part.input : JSON.stringify(part.input);
|
|
386
|
+
const providerData = getProviderData(part, providerOptionsName);
|
|
387
|
+
const itemId = typeof providerData?.itemId === "string" ? providerData.itemId : void 0;
|
|
388
|
+
const providerTool = providerToolsByName.get(part.toolName);
|
|
389
|
+
if (customToolId != null && providerTool?.id === customToolId) input.push({
|
|
390
|
+
type: "custom_tool_call",
|
|
391
|
+
...itemId != null && { id: itemId },
|
|
392
|
+
call_id: part.toolCallId,
|
|
393
|
+
name: part.toolName,
|
|
394
|
+
input: argumentsValue
|
|
395
|
+
});
|
|
396
|
+
else input.push({
|
|
397
|
+
type: "function_call",
|
|
398
|
+
...itemId != null && { id: itemId },
|
|
399
|
+
call_id: part.toolCallId,
|
|
400
|
+
name: part.toolName,
|
|
401
|
+
arguments: argumentsValue
|
|
402
|
+
});
|
|
403
|
+
break;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
flushAssistantContent();
|
|
408
|
+
break;
|
|
409
|
+
}
|
|
410
|
+
case "tool": for (const part of content) if (part.type === "tool-result") {
|
|
411
|
+
const extensionReplay = getExtensionReplay({
|
|
412
|
+
part,
|
|
413
|
+
providerOptionsName,
|
|
414
|
+
extensionRegistry
|
|
415
|
+
});
|
|
416
|
+
if (extensionReplay != null) {
|
|
417
|
+
const replayItem = extensionReplay.item;
|
|
418
|
+
if (replayItem != null) {
|
|
419
|
+
const replayKey = `${replayItem.type}:${replayItem.id}`;
|
|
420
|
+
if (!replayedExtensionItems.has(replayKey)) {
|
|
421
|
+
input.push(asOpenResponsesExtensionRecord(replayItem));
|
|
422
|
+
replayedExtensionItems.add(replayKey);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
const providerTool = providerToolsByName.get(part.toolName);
|
|
428
|
+
const extension = providerTool == null ? void 0 : extensionRegistry?.byProviderToolId.get(providerTool.id);
|
|
429
|
+
if (providerTool != null && extension != null) {
|
|
430
|
+
const encoded = await encodeExtensionInputPart({
|
|
431
|
+
extensionRegistry,
|
|
432
|
+
part,
|
|
433
|
+
providerTool
|
|
434
|
+
});
|
|
435
|
+
if (encoded == null) warnings.push({
|
|
436
|
+
type: "unsupported",
|
|
437
|
+
feature: `provider-defined tool ${providerTool.id} tool-result history`
|
|
438
|
+
});
|
|
439
|
+
else input.push(...encoded.map(asOpenResponsesExtensionRecord));
|
|
440
|
+
continue;
|
|
441
|
+
}
|
|
442
|
+
const output = part.output;
|
|
443
|
+
let contentValue;
|
|
444
|
+
switch (output.type) {
|
|
445
|
+
case "text":
|
|
446
|
+
case "error-text":
|
|
447
|
+
contentValue = output.value;
|
|
448
|
+
break;
|
|
449
|
+
case "execution-denied":
|
|
450
|
+
contentValue = output.reason ?? "Tool call execution denied.";
|
|
451
|
+
break;
|
|
452
|
+
case "json":
|
|
453
|
+
case "error-json":
|
|
454
|
+
contentValue = JSON.stringify(output.value);
|
|
455
|
+
break;
|
|
456
|
+
case "content": {
|
|
457
|
+
const contentParts = [];
|
|
458
|
+
for (const item of output.value) switch (item.type) {
|
|
459
|
+
case "text":
|
|
460
|
+
contentParts.push({
|
|
461
|
+
type: "input_text",
|
|
462
|
+
text: item.text
|
|
463
|
+
});
|
|
464
|
+
break;
|
|
465
|
+
case "file": {
|
|
466
|
+
const topLevel = getTopLevelMediaType(item.mediaType);
|
|
467
|
+
if (item.data.type === "data") {
|
|
468
|
+
const fullMediaType = resolveFullMediaType({ part: item });
|
|
469
|
+
if (topLevel === "image") contentParts.push({
|
|
470
|
+
type: "input_image",
|
|
471
|
+
image_url: `data:${fullMediaType};base64,${convertToBase64(item.data.data)}`,
|
|
472
|
+
detail: getImageDetail(item, providerOptionsName)
|
|
473
|
+
});
|
|
474
|
+
else contentParts.push({
|
|
475
|
+
type: "input_file",
|
|
476
|
+
filename: item.filename ?? "data",
|
|
477
|
+
file_data: `data:${fullMediaType};base64,${convertToBase64(item.data.data)}`
|
|
478
|
+
});
|
|
479
|
+
} else if (item.data.type === "url") {
|
|
480
|
+
if (topLevel === "image") contentParts.push({
|
|
481
|
+
type: "input_image",
|
|
482
|
+
image_url: item.data.url.toString(),
|
|
483
|
+
detail: getImageDetail(item, providerOptionsName)
|
|
484
|
+
});
|
|
485
|
+
else contentParts.push({
|
|
486
|
+
type: "input_file",
|
|
487
|
+
file_url: item.data.url.toString()
|
|
488
|
+
});
|
|
489
|
+
} else warnings.push({
|
|
490
|
+
type: "other",
|
|
491
|
+
message: `unsupported tool content part type: ${item.type} with data type: ${item.data.type}`
|
|
492
|
+
});
|
|
493
|
+
break;
|
|
494
|
+
}
|
|
495
|
+
default: warnings.push({
|
|
496
|
+
type: "other",
|
|
497
|
+
message: `unsupported tool content part type: ${item.type}`
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
contentValue = contentParts;
|
|
501
|
+
break;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
input.push({
|
|
505
|
+
type: customToolId != null && providerTool?.id === customToolId ? "custom_tool_call_output" : "function_call_output",
|
|
506
|
+
call_id: part.toolCallId,
|
|
507
|
+
output: contentValue
|
|
508
|
+
});
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
return {
|
|
512
|
+
input,
|
|
513
|
+
instructions: systemMessages.length > 0 ? systemMessages.join("\n") : void 0,
|
|
514
|
+
warnings
|
|
515
|
+
};
|
|
730
516
|
}
|
|
731
|
-
async function encodeExtensionInputPart({
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
const items = value == null ? void 0 : Array.isArray(value) ? value : [value];
|
|
748
|
-
if (items == null || items.length === 0 || !items.every(
|
|
749
|
-
(item) => isOpenResponsesExtensionItem(item) && itemTypes.includes(item.type)
|
|
750
|
-
)) {
|
|
751
|
-
return void 0;
|
|
752
|
-
}
|
|
753
|
-
return items;
|
|
754
|
-
} catch {
|
|
755
|
-
return void 0;
|
|
756
|
-
}
|
|
517
|
+
async function encodeExtensionInputPart({ extensionRegistry, part, providerTool }) {
|
|
518
|
+
const extension = extensionRegistry?.byProviderToolId.get(providerTool.id);
|
|
519
|
+
const encodeInputItem = extension?.encodeInputItem;
|
|
520
|
+
const itemTypes = extension == null ? [] : getOpenResponsesExtensionItemTypes(extension);
|
|
521
|
+
if (encodeInputItem == null || itemTypes.length === 0) return;
|
|
522
|
+
try {
|
|
523
|
+
const value = await encodeInputItem({
|
|
524
|
+
part,
|
|
525
|
+
tool: providerTool
|
|
526
|
+
});
|
|
527
|
+
const items = value == null ? void 0 : Array.isArray(value) ? value : [value];
|
|
528
|
+
if (items == null || items.length === 0 || !items.every((item) => isOpenResponsesExtensionItem(item) && itemTypes.includes(item.type))) return;
|
|
529
|
+
return items;
|
|
530
|
+
} catch {
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
757
533
|
}
|
|
758
|
-
function getExtensionReplay({
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
if (extensionData == null || typeof extensionData !== "object" || Array.isArray(extensionData)) {
|
|
768
|
-
return void 0;
|
|
769
|
-
}
|
|
770
|
-
const { id, item, itemId } = extensionData;
|
|
771
|
-
if (typeof id !== "string") {
|
|
772
|
-
return void 0;
|
|
773
|
-
}
|
|
774
|
-
const extension = extensionRegistry?.byExtensionId.get(
|
|
775
|
-
id
|
|
776
|
-
);
|
|
777
|
-
if (extension == null) {
|
|
778
|
-
return void 0;
|
|
779
|
-
}
|
|
780
|
-
if (isOpenResponsesExtensionItem(item) && getOpenResponsesExtensionItemTypes(extension).includes(item.type)) {
|
|
781
|
-
return { item };
|
|
782
|
-
}
|
|
783
|
-
return typeof itemId === "string" ? {} : void 0;
|
|
534
|
+
function getExtensionReplay({ part, providerOptionsName, extensionRegistry }) {
|
|
535
|
+
const extensionData = getProviderData(part, providerOptionsName)?.openResponsesExtension;
|
|
536
|
+
if (extensionData == null || typeof extensionData !== "object" || Array.isArray(extensionData)) return;
|
|
537
|
+
const { id, item, itemId } = extensionData;
|
|
538
|
+
if (typeof id !== "string") return;
|
|
539
|
+
const extension = extensionRegistry?.byExtensionId.get(id);
|
|
540
|
+
if (extension == null) return;
|
|
541
|
+
if (isOpenResponsesExtensionItem(item) && getOpenResponsesExtensionItemTypes(extension).includes(item.type)) return { item };
|
|
542
|
+
return typeof itemId === "string" ? {} : void 0;
|
|
784
543
|
}
|
|
785
544
|
function getProviderData(part, providerOptionsName) {
|
|
786
|
-
|
|
787
|
-
|
|
545
|
+
const providerData = part.providerOptions?.[providerOptionsName] ?? part.providerMetadata?.[providerOptionsName];
|
|
546
|
+
return providerData != null && typeof providerData === "object" && !Array.isArray(providerData) ? providerData : void 0;
|
|
788
547
|
}
|
|
789
548
|
function getImageDetail(part, providerOptionsName) {
|
|
790
|
-
|
|
791
|
-
|
|
549
|
+
const imageDetail = getProviderData(part, providerOptionsName)?.imageDetail;
|
|
550
|
+
return imageDetail === "low" || imageDetail === "high" || imageDetail === "auto" ? imageDetail : "auto";
|
|
792
551
|
}
|
|
793
552
|
function parseReasoningSummary(value) {
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
return value.map((part) => ({
|
|
800
|
-
type: "summary_text",
|
|
801
|
-
text: part.text
|
|
802
|
-
}));
|
|
553
|
+
if (!Array.isArray(value) || !value.every((part) => part != null && typeof part === "object" && part.type === "summary_text" && typeof part.text === "string")) return;
|
|
554
|
+
return value.map((part) => ({
|
|
555
|
+
type: "summary_text",
|
|
556
|
+
text: part.text
|
|
557
|
+
}));
|
|
803
558
|
}
|
|
804
559
|
function parseReasoningContent(value) {
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
return value.map((part) => ({
|
|
811
|
-
type: "reasoning_text",
|
|
812
|
-
text: part.text
|
|
813
|
-
}));
|
|
560
|
+
if (!Array.isArray(value) || !value.every((part) => part != null && typeof part === "object" && part.type === "reasoning_text" && typeof part.text === "string")) return;
|
|
561
|
+
return value.map((part) => ({
|
|
562
|
+
type: "reasoning_text",
|
|
563
|
+
text: part.text
|
|
564
|
+
}));
|
|
814
565
|
}
|
|
815
566
|
function parseOutputTextAnnotations(value) {
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
end_index: annotation.end_index,
|
|
825
|
-
url: annotation.url,
|
|
826
|
-
title: annotation.title
|
|
827
|
-
}));
|
|
567
|
+
if (!Array.isArray(value) || !value.every((annotation) => annotation != null && typeof annotation === "object" && annotation.type === "url_citation" && typeof annotation.start_index === "number" && typeof annotation.end_index === "number" && typeof annotation.url === "string" && typeof annotation.title === "string")) return;
|
|
568
|
+
return value.map((annotation) => ({
|
|
569
|
+
type: "url_citation",
|
|
570
|
+
start_index: annotation.start_index,
|
|
571
|
+
end_index: annotation.end_index,
|
|
572
|
+
url: annotation.url,
|
|
573
|
+
title: annotation.title
|
|
574
|
+
}));
|
|
828
575
|
}
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
function mapOpenResponsesFinishReason({
|
|
848
|
-
finishReason,
|
|
849
|
-
hasToolCalls
|
|
850
|
-
}) {
|
|
851
|
-
switch (finishReason) {
|
|
852
|
-
case void 0:
|
|
853
|
-
case null:
|
|
854
|
-
return hasToolCalls ? "tool-calls" : "stop";
|
|
855
|
-
case "max_output_tokens":
|
|
856
|
-
return "length";
|
|
857
|
-
case "content_filter":
|
|
858
|
-
return "content-filter";
|
|
859
|
-
default:
|
|
860
|
-
return hasToolCalls ? "tool-calls" : "other";
|
|
861
|
-
}
|
|
576
|
+
//#endregion
|
|
577
|
+
//#region src/responses/open-responses-api.ts
|
|
578
|
+
const openResponsesErrorSchema = lazySchema(() => zodSchema(z.object({ error: z.object({
|
|
579
|
+
message: z.string(),
|
|
580
|
+
type: z.string().nullish(),
|
|
581
|
+
param: z.string().nullish(),
|
|
582
|
+
code: z.string().nullish()
|
|
583
|
+
}) })));
|
|
584
|
+
//#endregion
|
|
585
|
+
//#region src/responses/map-open-responses-finish-reason.ts
|
|
586
|
+
function mapOpenResponsesFinishReason({ finishReason, hasToolCalls }) {
|
|
587
|
+
switch (finishReason) {
|
|
588
|
+
case void 0:
|
|
589
|
+
case null: return hasToolCalls ? "tool-calls" : "stop";
|
|
590
|
+
case "max_output_tokens": return "length";
|
|
591
|
+
case "content_filter": return "content-filter";
|
|
592
|
+
default: return hasToolCalls ? "tool-calls" : "other";
|
|
593
|
+
}
|
|
862
594
|
}
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
)
|
|
886
|
-
|
|
887
|
-
// src/responses/open-responses-language-model.ts
|
|
888
|
-
var defaultFailedResponseHandler = createJsonErrorResponseHandler({
|
|
889
|
-
errorSchema: openResponsesErrorSchema,
|
|
890
|
-
errorToMessage: (error) => error.error.message
|
|
595
|
+
//#endregion
|
|
596
|
+
//#region src/responses/open-responses-language-model-options.ts
|
|
597
|
+
const openResponsesLanguageModelOptions = lazySchema(() => zodSchema(z.object({
|
|
598
|
+
/**
|
|
599
|
+
* Provider-native reasoning effort. The value is passed through to the
|
|
600
|
+
* endpoint and takes precedence over the top-level `reasoning` setting.
|
|
601
|
+
*/
|
|
602
|
+
reasoningEffort: z.string().nullish(),
|
|
603
|
+
/**
|
|
604
|
+
* Controls reasoning summary output from the model.
|
|
605
|
+
* Valid values: 'concise', 'detailed', 'auto'.
|
|
606
|
+
*/
|
|
607
|
+
reasoningSummary: z.enum([
|
|
608
|
+
"concise",
|
|
609
|
+
"detailed",
|
|
610
|
+
"auto"
|
|
611
|
+
]).nullish()
|
|
612
|
+
})));
|
|
613
|
+
//#endregion
|
|
614
|
+
//#region src/responses/open-responses-language-model.ts
|
|
615
|
+
const defaultFailedResponseHandler = createJsonErrorResponseHandler({
|
|
616
|
+
errorSchema: openResponsesErrorSchema,
|
|
617
|
+
errorToMessage: (error) => error.error.message
|
|
891
618
|
});
|
|
892
|
-
var OpenResponsesLanguageModel = class
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
controller.enqueue({
|
|
1513
|
-
type: "reasoning-end",
|
|
1514
|
-
id: chunk.item.id,
|
|
1515
|
-
providerMetadata: createReasoningProviderMetadata({
|
|
1516
|
-
part: chunk.item,
|
|
1517
|
-
providerOptionsName
|
|
1518
|
-
})
|
|
1519
|
-
});
|
|
1520
|
-
if (activeReasoningId === chunk.item.id) {
|
|
1521
|
-
activeReasoningId = void 0;
|
|
1522
|
-
}
|
|
1523
|
-
} else if (chunk.type === "response.output_item.added" && chunk.item.type === "message") {
|
|
1524
|
-
controller.enqueue({ type: "text-start", id: chunk.item.id });
|
|
1525
|
-
} else if (chunk.type === "response.output_text.delta") {
|
|
1526
|
-
controller.enqueue({
|
|
1527
|
-
type: "text-delta",
|
|
1528
|
-
id: chunk.item_id,
|
|
1529
|
-
delta: chunk.delta
|
|
1530
|
-
});
|
|
1531
|
-
} else if (chunk.type === "response.output_item.done" && chunk.item.type === "message") {
|
|
1532
|
-
const annotations = chunk.item.content.flatMap(
|
|
1533
|
-
getOutputTextAnnotations
|
|
1534
|
-
);
|
|
1535
|
-
controller.enqueue({
|
|
1536
|
-
type: "text-end",
|
|
1537
|
-
id: chunk.item.id,
|
|
1538
|
-
providerMetadata: {
|
|
1539
|
-
[providerOptionsName]: {
|
|
1540
|
-
itemId: chunk.item.id,
|
|
1541
|
-
...annotations.length > 0 && { annotations }
|
|
1542
|
-
}
|
|
1543
|
-
}
|
|
1544
|
-
});
|
|
1545
|
-
} else if (chunk.type === "response.completed" || chunk.type === "response.incomplete") {
|
|
1546
|
-
const reason = chunk.response.incomplete_details?.reason;
|
|
1547
|
-
finishReason = {
|
|
1548
|
-
unified: mapOpenResponsesFinishReason({
|
|
1549
|
-
finishReason: reason,
|
|
1550
|
-
hasToolCalls
|
|
1551
|
-
}),
|
|
1552
|
-
raw: reason ?? void 0
|
|
1553
|
-
};
|
|
1554
|
-
updateUsage(chunk.response.usage);
|
|
1555
|
-
} else if (chunk.type === "response.failed") {
|
|
1556
|
-
finishReason = {
|
|
1557
|
-
unified: "error",
|
|
1558
|
-
raw: chunk.response.error?.code ?? chunk.response.status
|
|
1559
|
-
};
|
|
1560
|
-
updateUsage(chunk.response.usage);
|
|
1561
|
-
if (chunk.response.error != null) {
|
|
1562
|
-
controller.enqueue({
|
|
1563
|
-
type: "error",
|
|
1564
|
-
error: createOpenResponsesStreamError({
|
|
1565
|
-
type: chunk.type,
|
|
1566
|
-
error: chunk.response.error,
|
|
1567
|
-
data: chunk,
|
|
1568
|
-
getResponseErrorMetadata
|
|
1569
|
-
})
|
|
1570
|
-
});
|
|
1571
|
-
}
|
|
1572
|
-
} else if (chunk.type === "error") {
|
|
1573
|
-
finishReason = {
|
|
1574
|
-
unified: "error",
|
|
1575
|
-
raw: chunk.error.code
|
|
1576
|
-
};
|
|
1577
|
-
controller.enqueue({
|
|
1578
|
-
type: "error",
|
|
1579
|
-
error: createOpenResponsesStreamError({
|
|
1580
|
-
type: chunk.type,
|
|
1581
|
-
error: chunk.error,
|
|
1582
|
-
data: chunk,
|
|
1583
|
-
getResponseErrorMetadata
|
|
1584
|
-
})
|
|
1585
|
-
});
|
|
1586
|
-
}
|
|
1587
|
-
},
|
|
1588
|
-
flush(controller) {
|
|
1589
|
-
if (activeReasoningId != null) {
|
|
1590
|
-
controller.enqueue({
|
|
1591
|
-
type: "reasoning-end",
|
|
1592
|
-
id: activeReasoningId
|
|
1593
|
-
});
|
|
1594
|
-
}
|
|
1595
|
-
controller.enqueue({
|
|
1596
|
-
type: "finish",
|
|
1597
|
-
finishReason,
|
|
1598
|
-
usage,
|
|
1599
|
-
providerMetadata: void 0
|
|
1600
|
-
});
|
|
1601
|
-
}
|
|
1602
|
-
})
|
|
1603
|
-
),
|
|
1604
|
-
request: { body },
|
|
1605
|
-
response: { headers: responseHeaders }
|
|
1606
|
-
};
|
|
1607
|
-
}
|
|
619
|
+
var OpenResponsesLanguageModel = class OpenResponsesLanguageModel {
|
|
620
|
+
static [WORKFLOW_SERIALIZE](model) {
|
|
621
|
+
if (model.extensionRegistry.byExtensionId.size > 0) throw new SerializationError({ message: "Open Responses models with registered extensions cannot be serialized across workflow boundaries. Recreate the provider with its extension codecs inside the workflow step." });
|
|
622
|
+
return serializeModelOptions({
|
|
623
|
+
modelId: model.modelId,
|
|
624
|
+
config: model.config
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
static [WORKFLOW_DESERIALIZE](options) {
|
|
628
|
+
return new OpenResponsesLanguageModel(options.modelId, options.config);
|
|
629
|
+
}
|
|
630
|
+
constructor(modelId, config) {
|
|
631
|
+
this.specificationVersion = "v4";
|
|
632
|
+
this.supportedUrls = { "image/*": [/^https?:\/\/.*$/] };
|
|
633
|
+
this.modelId = modelId;
|
|
634
|
+
this.config = config;
|
|
635
|
+
this.extensionRegistry = config.extensionRegistry ?? createOpenResponsesExtensionRegistry();
|
|
636
|
+
}
|
|
637
|
+
get provider() {
|
|
638
|
+
return this.config.provider;
|
|
639
|
+
}
|
|
640
|
+
async getArgs({ maxOutputTokens, temperature, stopSequences, topP, topK, presencePenalty, frequencyPenalty, seed, reasoning, prompt, providerOptions, tools, toolChoice, responseFormat }) {
|
|
641
|
+
const warnings = [];
|
|
642
|
+
if (stopSequences != null) warnings.push({
|
|
643
|
+
type: "unsupported",
|
|
644
|
+
feature: "stopSequences"
|
|
645
|
+
});
|
|
646
|
+
if (topK != null) warnings.push({
|
|
647
|
+
type: "unsupported",
|
|
648
|
+
feature: "topK"
|
|
649
|
+
});
|
|
650
|
+
if (seed != null) warnings.push({
|
|
651
|
+
type: "unsupported",
|
|
652
|
+
feature: "seed"
|
|
653
|
+
});
|
|
654
|
+
const providerToolsByName = new Map((tools ?? []).filter((tool) => tool.type === "provider").map((tool) => [tool.name, tool]));
|
|
655
|
+
const { input, instructions, warnings: inputWarnings } = await convertToOpenResponsesInput({
|
|
656
|
+
prompt,
|
|
657
|
+
providerOptionsName: this.config.providerOptionsName,
|
|
658
|
+
extensionRegistry: this.extensionRegistry,
|
|
659
|
+
providerToolsByName,
|
|
660
|
+
strictResponseInput: this.config.strictResponseInput,
|
|
661
|
+
customToolId: this.config.customToolId
|
|
662
|
+
});
|
|
663
|
+
warnings.push(...inputWarnings);
|
|
664
|
+
const convertedTools = [];
|
|
665
|
+
const encodedProviderToolsByName = /* @__PURE__ */ new Map();
|
|
666
|
+
for (const tool of tools ?? []) if (tool.type === "provider") {
|
|
667
|
+
if (this.config.customToolId != null && tool.id === this.config.customToolId) {
|
|
668
|
+
const format = tool.args.format != null && typeof tool.args.format === "object" && !Array.isArray(tool.args.format) ? tool.args.format : void 0;
|
|
669
|
+
convertedTools.push({
|
|
670
|
+
type: "custom",
|
|
671
|
+
name: tool.name,
|
|
672
|
+
description: typeof tool.args.description === "string" ? tool.args.description : void 0,
|
|
673
|
+
format: format != null && "type" in format && format.type === "grammar" && "syntax" in format && (format.syntax === "regex" || format.syntax === "lark") && "definition" in format && typeof format.definition === "string" ? {
|
|
674
|
+
type: "grammar",
|
|
675
|
+
syntax: format.syntax,
|
|
676
|
+
definition: format.definition
|
|
677
|
+
} : format != null && "type" in format && format.type === "text" ? { type: "text" } : void 0
|
|
678
|
+
});
|
|
679
|
+
continue;
|
|
680
|
+
}
|
|
681
|
+
const extension = this.extensionRegistry.byProviderToolId.get(tool.id);
|
|
682
|
+
let encoded;
|
|
683
|
+
if (extension != null) {
|
|
684
|
+
const toolType = getOpenResponsesExtensionToolType(extension);
|
|
685
|
+
try {
|
|
686
|
+
const fields = await extension.encodeTool({
|
|
687
|
+
name: tool.name,
|
|
688
|
+
args: tool.args
|
|
689
|
+
});
|
|
690
|
+
if (isOpenResponsesJSONObject(fields)) encoded = {
|
|
691
|
+
...fields,
|
|
692
|
+
type: toolType
|
|
693
|
+
};
|
|
694
|
+
} catch {}
|
|
695
|
+
}
|
|
696
|
+
if (encoded == null) warnings.push({
|
|
697
|
+
type: "unsupported",
|
|
698
|
+
feature: `provider-defined tool ${tool.id}`
|
|
699
|
+
});
|
|
700
|
+
else if (extension != null) {
|
|
701
|
+
convertedTools.push(asOpenResponsesExtensionRecord(encoded));
|
|
702
|
+
encodedProviderToolsByName.set(tool.name, {
|
|
703
|
+
toolType: getOpenResponsesExtensionToolType(extension),
|
|
704
|
+
encodeToolChoice: extension.encodeToolChoice,
|
|
705
|
+
tool
|
|
706
|
+
});
|
|
707
|
+
}
|
|
708
|
+
} else convertedTools.push({
|
|
709
|
+
type: "function",
|
|
710
|
+
name: tool.name,
|
|
711
|
+
description: tool.description,
|
|
712
|
+
parameters: tool.inputSchema,
|
|
713
|
+
...tool.strict != null ? { strict: tool.strict } : {}
|
|
714
|
+
});
|
|
715
|
+
let convertedToolChoice;
|
|
716
|
+
if (toolChoice?.type === "tool") {
|
|
717
|
+
const registeredTool = encodedProviderToolsByName.get(toolChoice.toolName);
|
|
718
|
+
if (registeredTool == null) {
|
|
719
|
+
if (this.config.customToolId != null && providerToolsByName.get(toolChoice.toolName)?.id === this.config.customToolId) convertedToolChoice = {
|
|
720
|
+
type: "custom",
|
|
721
|
+
name: toolChoice.toolName
|
|
722
|
+
};
|
|
723
|
+
else if (!providerToolsByName.has(toolChoice.toolName)) convertedToolChoice = {
|
|
724
|
+
type: "function",
|
|
725
|
+
name: toolChoice.toolName
|
|
726
|
+
};
|
|
727
|
+
} else {
|
|
728
|
+
const { encodeToolChoice, tool, toolType } = registeredTool;
|
|
729
|
+
let fields = {};
|
|
730
|
+
try {
|
|
731
|
+
fields = await encodeToolChoice?.({
|
|
732
|
+
name: tool.name,
|
|
733
|
+
args: tool.args
|
|
734
|
+
});
|
|
735
|
+
} catch {
|
|
736
|
+
fields = void 0;
|
|
737
|
+
}
|
|
738
|
+
if (encodeToolChoice != null && !isOpenResponsesJSONObject(fields)) warnings.push({
|
|
739
|
+
type: "unsupported",
|
|
740
|
+
feature: `tool choice for provider-defined tool ${tool.id}`
|
|
741
|
+
});
|
|
742
|
+
else convertedToolChoice = asOpenResponsesExtensionRecord({
|
|
743
|
+
...isOpenResponsesJSONObject(fields) ? fields : {},
|
|
744
|
+
type: toolType
|
|
745
|
+
});
|
|
746
|
+
}
|
|
747
|
+
} else convertedToolChoice = toolChoice?.type;
|
|
748
|
+
const textFormat = responseFormat?.type === "json" && this.config.structuredOutputs !== false ? responseFormat.schema != null ? {
|
|
749
|
+
type: "json_schema",
|
|
750
|
+
name: responseFormat.name ?? "response",
|
|
751
|
+
description: responseFormat.description,
|
|
752
|
+
schema: responseFormat.schema,
|
|
753
|
+
strict: true
|
|
754
|
+
} : { type: "json_object" } : void 0;
|
|
755
|
+
if (responseFormat?.type === "json" && this.config.structuredOutputs === false) warnings.push({
|
|
756
|
+
type: "unsupported",
|
|
757
|
+
feature: "responseFormat"
|
|
758
|
+
});
|
|
759
|
+
const openResponsesOptions = await parseProviderOptions({
|
|
760
|
+
provider: this.config.providerOptionsName,
|
|
761
|
+
providerOptions,
|
|
762
|
+
schema: openResponsesLanguageModelOptions
|
|
763
|
+
});
|
|
764
|
+
const resolvedReasoningEffort = openResponsesOptions?.reasoningEffort ?? (isCustomReasoning(reasoning) ? reasoning === "none" ? "none" : mapReasoningToProviderEffort({
|
|
765
|
+
reasoning,
|
|
766
|
+
effortMap: {
|
|
767
|
+
minimal: "low",
|
|
768
|
+
low: "low",
|
|
769
|
+
medium: "medium",
|
|
770
|
+
high: "high",
|
|
771
|
+
xhigh: "xhigh"
|
|
772
|
+
},
|
|
773
|
+
warnings
|
|
774
|
+
}) : void 0);
|
|
775
|
+
return {
|
|
776
|
+
body: {
|
|
777
|
+
model: this.modelId,
|
|
778
|
+
input,
|
|
779
|
+
instructions,
|
|
780
|
+
max_output_tokens: maxOutputTokens,
|
|
781
|
+
temperature,
|
|
782
|
+
top_p: topP,
|
|
783
|
+
presence_penalty: presencePenalty,
|
|
784
|
+
frequency_penalty: frequencyPenalty,
|
|
785
|
+
reasoning: resolvedReasoningEffort != null || openResponsesOptions?.reasoningSummary != null ? {
|
|
786
|
+
...resolvedReasoningEffort != null && { effort: resolvedReasoningEffort },
|
|
787
|
+
...openResponsesOptions?.reasoningSummary != null && { summary: openResponsesOptions.reasoningSummary }
|
|
788
|
+
} : void 0,
|
|
789
|
+
tools: convertedTools.length ? convertedTools : void 0,
|
|
790
|
+
tool_choice: convertedToolChoice,
|
|
791
|
+
...textFormat != null && { text: { format: textFormat } }
|
|
792
|
+
},
|
|
793
|
+
warnings
|
|
794
|
+
};
|
|
795
|
+
}
|
|
796
|
+
async doGenerate(options) {
|
|
797
|
+
const { body, warnings } = await this.getArgs(options);
|
|
798
|
+
const { responseHeaders, value: response, rawValue: rawResponse } = await postJsonToApi({
|
|
799
|
+
url: this.config.url,
|
|
800
|
+
headers: combineHeaders(this.config.headers?.(), options.headers),
|
|
801
|
+
body,
|
|
802
|
+
failedResponseHandler: this.config.failedResponseHandler ?? defaultFailedResponseHandler,
|
|
803
|
+
successfulResponseHandler: createJsonResponseHandler(jsonSchema(() => {
|
|
804
|
+
throw new Error("json schema not implemented");
|
|
805
|
+
})),
|
|
806
|
+
abortSignal: options.abortSignal,
|
|
807
|
+
fetch: this.config.fetch
|
|
808
|
+
});
|
|
809
|
+
if (response.error) {
|
|
810
|
+
const errorMetadata = this.config.getResponseErrorMetadata?.(response.error);
|
|
811
|
+
throw new APICallError({
|
|
812
|
+
message: response.error.message,
|
|
813
|
+
url: this.config.url,
|
|
814
|
+
requestBodyValues: body,
|
|
815
|
+
statusCode: errorMetadata?.statusCode ?? 400,
|
|
816
|
+
isRetryable: errorMetadata?.isRetryable,
|
|
817
|
+
responseHeaders,
|
|
818
|
+
responseBody: rawResponse,
|
|
819
|
+
data: response.error
|
|
820
|
+
});
|
|
821
|
+
}
|
|
822
|
+
if (response.output == null) {
|
|
823
|
+
const detail = response.incomplete_details?.reason ?? response.status;
|
|
824
|
+
throw new APICallError({
|
|
825
|
+
message: detail ? `Responses API returned no output (${detail})` : "Responses API returned no output",
|
|
826
|
+
url: this.config.url,
|
|
827
|
+
requestBodyValues: body,
|
|
828
|
+
statusCode: 500,
|
|
829
|
+
responseHeaders,
|
|
830
|
+
responseBody: rawResponse,
|
|
831
|
+
isRetryable: false
|
|
832
|
+
});
|
|
833
|
+
}
|
|
834
|
+
const content = [];
|
|
835
|
+
let hasToolCalls = false;
|
|
836
|
+
for (const part of response.output) switch (part.type) {
|
|
837
|
+
case "reasoning":
|
|
838
|
+
if ((part.content?.length ?? 0) > 0) for (const contentPart of part.content) content.push({
|
|
839
|
+
type: "reasoning",
|
|
840
|
+
text: contentPart.text,
|
|
841
|
+
providerMetadata: createReasoningProviderMetadata({
|
|
842
|
+
part,
|
|
843
|
+
providerOptionsName: this.config.providerOptionsName,
|
|
844
|
+
reasoningContent: [contentPart]
|
|
845
|
+
})
|
|
846
|
+
});
|
|
847
|
+
else content.push({
|
|
848
|
+
type: "reasoning",
|
|
849
|
+
text: part.summary.map((summaryPart) => summaryPart.text).join(""),
|
|
850
|
+
providerMetadata: createReasoningProviderMetadata({
|
|
851
|
+
part,
|
|
852
|
+
providerOptionsName: this.config.providerOptionsName
|
|
853
|
+
})
|
|
854
|
+
});
|
|
855
|
+
break;
|
|
856
|
+
case "message":
|
|
857
|
+
for (const contentPart of part.content) {
|
|
858
|
+
const annotations = getOutputTextAnnotations(contentPart);
|
|
859
|
+
content.push({
|
|
860
|
+
type: "text",
|
|
861
|
+
text: contentPart.text,
|
|
862
|
+
providerMetadata: { [this.config.providerOptionsName]: {
|
|
863
|
+
itemId: part.id,
|
|
864
|
+
...annotations.length > 0 && { annotations }
|
|
865
|
+
} }
|
|
866
|
+
});
|
|
867
|
+
}
|
|
868
|
+
break;
|
|
869
|
+
case "function_call":
|
|
870
|
+
hasToolCalls = true;
|
|
871
|
+
content.push({
|
|
872
|
+
type: "tool-call",
|
|
873
|
+
toolCallId: part.call_id,
|
|
874
|
+
toolName: part.name,
|
|
875
|
+
input: part.arguments,
|
|
876
|
+
providerMetadata: { [this.config.providerOptionsName]: { itemId: part.id } }
|
|
877
|
+
});
|
|
878
|
+
break;
|
|
879
|
+
case "custom_tool_call":
|
|
880
|
+
hasToolCalls = true;
|
|
881
|
+
content.push({
|
|
882
|
+
type: "tool-call",
|
|
883
|
+
toolCallId: part.call_id,
|
|
884
|
+
toolName: part.name,
|
|
885
|
+
input: JSON.stringify(part.input),
|
|
886
|
+
providerMetadata: { [this.config.providerOptionsName]: { itemId: part.id } }
|
|
887
|
+
});
|
|
888
|
+
break;
|
|
889
|
+
default: {
|
|
890
|
+
if (!isOpenResponsesExtensionItem(part)) break;
|
|
891
|
+
const decoded = await decodeExtensionItem({
|
|
892
|
+
extensionRegistry: this.extensionRegistry,
|
|
893
|
+
item: part,
|
|
894
|
+
mode: "generate",
|
|
895
|
+
providerOptionsName: this.config.providerOptionsName
|
|
896
|
+
});
|
|
897
|
+
if (decoded != null) {
|
|
898
|
+
content.push(...decoded);
|
|
899
|
+
hasToolCalls ||= decoded.some((part) => part.type === "tool-call");
|
|
900
|
+
}
|
|
901
|
+
break;
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
const usage = response.usage;
|
|
905
|
+
const inputTokens = usage?.input_tokens;
|
|
906
|
+
const cachedInputTokens = usage?.input_tokens_details?.cached_tokens;
|
|
907
|
+
const cacheWriteTokens = usage?.input_tokens_details?.cache_write_tokens;
|
|
908
|
+
const outputTokens = usage?.output_tokens;
|
|
909
|
+
const reasoningTokens = usage?.output_tokens_details?.reasoning_tokens;
|
|
910
|
+
return {
|
|
911
|
+
content,
|
|
912
|
+
finishReason: {
|
|
913
|
+
unified: mapOpenResponsesFinishReason({
|
|
914
|
+
finishReason: response.incomplete_details?.reason,
|
|
915
|
+
hasToolCalls
|
|
916
|
+
}),
|
|
917
|
+
raw: response.incomplete_details?.reason ?? void 0
|
|
918
|
+
},
|
|
919
|
+
usage: {
|
|
920
|
+
inputTokens: {
|
|
921
|
+
total: inputTokens,
|
|
922
|
+
noCache: (inputTokens ?? 0) - (cachedInputTokens ?? 0) - (cacheWriteTokens ?? 0),
|
|
923
|
+
cacheRead: cachedInputTokens,
|
|
924
|
+
cacheWrite: cacheWriteTokens
|
|
925
|
+
},
|
|
926
|
+
outputTokens: {
|
|
927
|
+
total: outputTokens,
|
|
928
|
+
text: (outputTokens ?? 0) - (reasoningTokens ?? 0),
|
|
929
|
+
reasoning: reasoningTokens
|
|
930
|
+
},
|
|
931
|
+
raw: response.usage
|
|
932
|
+
},
|
|
933
|
+
request: { body },
|
|
934
|
+
response: {
|
|
935
|
+
id: response.id,
|
|
936
|
+
timestamp: /* @__PURE__ */ new Date(response.created_at * 1e3),
|
|
937
|
+
modelId: response.model,
|
|
938
|
+
headers: responseHeaders,
|
|
939
|
+
body: rawResponse
|
|
940
|
+
},
|
|
941
|
+
providerMetadata: void 0,
|
|
942
|
+
warnings
|
|
943
|
+
};
|
|
944
|
+
}
|
|
945
|
+
async doStream(options) {
|
|
946
|
+
const { body, warnings } = await this.getArgs(options);
|
|
947
|
+
const { responseHeaders, value: response } = await postJsonToApi({
|
|
948
|
+
url: this.config.url,
|
|
949
|
+
headers: combineHeaders(this.config.headers?.(), options.headers),
|
|
950
|
+
body: {
|
|
951
|
+
...body,
|
|
952
|
+
stream: true
|
|
953
|
+
},
|
|
954
|
+
failedResponseHandler: this.config.failedResponseHandler ?? defaultFailedResponseHandler,
|
|
955
|
+
successfulResponseHandler: createEventSourceResponseHandler(z.any()),
|
|
956
|
+
abortSignal: options.abortSignal,
|
|
957
|
+
fetch: this.config.fetch
|
|
958
|
+
});
|
|
959
|
+
const usage = {
|
|
960
|
+
inputTokens: {
|
|
961
|
+
total: void 0,
|
|
962
|
+
noCache: void 0,
|
|
963
|
+
cacheRead: void 0,
|
|
964
|
+
cacheWrite: void 0
|
|
965
|
+
},
|
|
966
|
+
outputTokens: {
|
|
967
|
+
total: void 0,
|
|
968
|
+
text: void 0,
|
|
969
|
+
reasoning: void 0
|
|
970
|
+
}
|
|
971
|
+
};
|
|
972
|
+
const updateUsage = (responseUsage) => {
|
|
973
|
+
if (!responseUsage) return;
|
|
974
|
+
const inputTokens = responseUsage.input_tokens;
|
|
975
|
+
const cachedInputTokens = responseUsage.input_tokens_details?.cached_tokens;
|
|
976
|
+
const cacheWriteTokens = responseUsage.input_tokens_details?.cache_write_tokens;
|
|
977
|
+
const outputTokens = responseUsage.output_tokens;
|
|
978
|
+
const reasoningTokens = responseUsage.output_tokens_details?.reasoning_tokens;
|
|
979
|
+
usage.inputTokens = {
|
|
980
|
+
total: inputTokens,
|
|
981
|
+
noCache: (inputTokens ?? 0) - (cachedInputTokens ?? 0) - (cacheWriteTokens ?? 0),
|
|
982
|
+
cacheRead: cachedInputTokens,
|
|
983
|
+
cacheWrite: cacheWriteTokens
|
|
984
|
+
};
|
|
985
|
+
usage.outputTokens = {
|
|
986
|
+
total: outputTokens,
|
|
987
|
+
text: (outputTokens ?? 0) - (reasoningTokens ?? 0),
|
|
988
|
+
reasoning: reasoningTokens
|
|
989
|
+
};
|
|
990
|
+
usage.raw = responseUsage;
|
|
991
|
+
};
|
|
992
|
+
let activeReasoningId;
|
|
993
|
+
let hasToolCalls = false;
|
|
994
|
+
let finishReason = {
|
|
995
|
+
unified: "other",
|
|
996
|
+
raw: void 0
|
|
997
|
+
};
|
|
998
|
+
const toolCallsByItemId = /* @__PURE__ */ new Map();
|
|
999
|
+
const customToolCallsByItemId = /* @__PURE__ */ new Map();
|
|
1000
|
+
const providerOptionsName = this.config.providerOptionsName;
|
|
1001
|
+
const extensionRegistry = this.extensionRegistry;
|
|
1002
|
+
const extensionStreamState = /* @__PURE__ */ new Map();
|
|
1003
|
+
const getResponseErrorMetadata = this.config.getResponseErrorMetadata;
|
|
1004
|
+
return {
|
|
1005
|
+
stream: response.pipeThrough(new TransformStream({
|
|
1006
|
+
start(controller) {
|
|
1007
|
+
controller.enqueue({
|
|
1008
|
+
type: "stream-start",
|
|
1009
|
+
warnings
|
|
1010
|
+
});
|
|
1011
|
+
},
|
|
1012
|
+
async transform(parseResult, controller) {
|
|
1013
|
+
if (options.includeRawChunks) controller.enqueue({
|
|
1014
|
+
type: "raw",
|
|
1015
|
+
rawValue: parseResult.rawValue
|
|
1016
|
+
});
|
|
1017
|
+
if (!parseResult.success) {
|
|
1018
|
+
controller.enqueue({
|
|
1019
|
+
type: "error",
|
|
1020
|
+
error: parseResult.error
|
|
1021
|
+
});
|
|
1022
|
+
return;
|
|
1023
|
+
}
|
|
1024
|
+
const chunk = parseResult.value;
|
|
1025
|
+
if (isOpenResponsesExtensionEvent(chunk)) {
|
|
1026
|
+
const extension = extensionRegistry.byEventType.get(chunk.type);
|
|
1027
|
+
if (extension?.decodeEvent != null) {
|
|
1028
|
+
try {
|
|
1029
|
+
const decoded = await extension.decodeEvent({
|
|
1030
|
+
event: chunk,
|
|
1031
|
+
state: extensionStreamState
|
|
1032
|
+
});
|
|
1033
|
+
for (const part of decoded ?? []) {
|
|
1034
|
+
controller.enqueue(part);
|
|
1035
|
+
hasToolCalls ||= part.type === "tool-call" || part.type === "tool-input-start";
|
|
1036
|
+
}
|
|
1037
|
+
} catch (error) {
|
|
1038
|
+
controller.enqueue({
|
|
1039
|
+
type: "error",
|
|
1040
|
+
error
|
|
1041
|
+
});
|
|
1042
|
+
}
|
|
1043
|
+
return;
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
if (chunk.type === "response.output_item.added" && chunk.item.type === "function_call") toolCallsByItemId.set(chunk.item.id, {
|
|
1047
|
+
toolName: chunk.item.name,
|
|
1048
|
+
toolCallId: chunk.item.call_id,
|
|
1049
|
+
arguments: chunk.item.arguments
|
|
1050
|
+
});
|
|
1051
|
+
else if (chunk.type === "response.function_call_arguments.delta") {
|
|
1052
|
+
const functionCallChunk = chunk;
|
|
1053
|
+
const toolCall = toolCallsByItemId.get(functionCallChunk.item_id);
|
|
1054
|
+
if (toolCall == null) {
|
|
1055
|
+
toolCallsByItemId.set(functionCallChunk.item_id, { arguments: functionCallChunk.delta });
|
|
1056
|
+
return;
|
|
1057
|
+
}
|
|
1058
|
+
toolCall.arguments = (toolCall.arguments ?? "") + functionCallChunk.delta;
|
|
1059
|
+
} else if (chunk.type === "response.function_call_arguments.done") {
|
|
1060
|
+
const functionCallChunk = chunk;
|
|
1061
|
+
const toolCall = toolCallsByItemId.get(functionCallChunk.item_id);
|
|
1062
|
+
if (toolCall == null) {
|
|
1063
|
+
toolCallsByItemId.set(functionCallChunk.item_id, { arguments: functionCallChunk.arguments });
|
|
1064
|
+
return;
|
|
1065
|
+
}
|
|
1066
|
+
toolCall.arguments = functionCallChunk.arguments;
|
|
1067
|
+
} else if (chunk.type === "response.output_item.done" && chunk.item.type === "function_call") {
|
|
1068
|
+
const toolCall = toolCallsByItemId.get(chunk.item.id);
|
|
1069
|
+
const toolName = toolCall?.toolName ?? chunk.item.name;
|
|
1070
|
+
const toolCallId = toolCall?.toolCallId ?? chunk.item.call_id;
|
|
1071
|
+
const input = chunk.item.arguments !== "" ? chunk.item.arguments : toolCall?.arguments ?? "";
|
|
1072
|
+
controller.enqueue({
|
|
1073
|
+
type: "tool-call",
|
|
1074
|
+
toolCallId,
|
|
1075
|
+
toolName,
|
|
1076
|
+
input,
|
|
1077
|
+
providerMetadata: { [providerOptionsName]: { itemId: chunk.item.id } }
|
|
1078
|
+
});
|
|
1079
|
+
hasToolCalls = true;
|
|
1080
|
+
toolCallsByItemId.delete(chunk.item.id);
|
|
1081
|
+
} else if (chunk.type === "response.output_item.added" && chunk.item.type === "custom_tool_call") {
|
|
1082
|
+
customToolCallsByItemId.set(chunk.item.id, {
|
|
1083
|
+
toolName: chunk.item.name,
|
|
1084
|
+
toolCallId: chunk.item.call_id,
|
|
1085
|
+
input: chunk.item.input
|
|
1086
|
+
});
|
|
1087
|
+
controller.enqueue({
|
|
1088
|
+
type: "tool-input-start",
|
|
1089
|
+
id: chunk.item.call_id,
|
|
1090
|
+
toolName: chunk.item.name
|
|
1091
|
+
});
|
|
1092
|
+
} else if (chunk.type === "response.custom_tool_call_input.delta") {
|
|
1093
|
+
const toolCall = customToolCallsByItemId.get(chunk.item_id);
|
|
1094
|
+
if (toolCall == null) customToolCallsByItemId.set(chunk.item_id, { input: chunk.delta });
|
|
1095
|
+
else toolCall.input = (toolCall.input ?? "") + chunk.delta;
|
|
1096
|
+
controller.enqueue({
|
|
1097
|
+
type: "tool-input-delta",
|
|
1098
|
+
id: customToolCallsByItemId.get(chunk.item_id)?.toolCallId ?? chunk.item_id,
|
|
1099
|
+
delta: chunk.delta
|
|
1100
|
+
});
|
|
1101
|
+
} else if (chunk.type === "response.custom_tool_call_input.done") {
|
|
1102
|
+
const toolCall = customToolCallsByItemId.get(chunk.item_id);
|
|
1103
|
+
if (toolCall == null) customToolCallsByItemId.set(chunk.item_id, { input: chunk.input });
|
|
1104
|
+
else toolCall.input = chunk.input;
|
|
1105
|
+
} else if (chunk.type === "response.output_item.done" && chunk.item.type === "custom_tool_call") {
|
|
1106
|
+
const toolCall = customToolCallsByItemId.get(chunk.item.id);
|
|
1107
|
+
const toolCallId = toolCall?.toolCallId ?? chunk.item.call_id;
|
|
1108
|
+
const input = chunk.item.input !== "" ? chunk.item.input : toolCall?.input ?? "";
|
|
1109
|
+
controller.enqueue({
|
|
1110
|
+
type: "tool-input-end",
|
|
1111
|
+
id: toolCallId
|
|
1112
|
+
});
|
|
1113
|
+
controller.enqueue({
|
|
1114
|
+
type: "tool-call",
|
|
1115
|
+
toolCallId,
|
|
1116
|
+
toolName: toolCall?.toolName ?? chunk.item.name,
|
|
1117
|
+
input: JSON.stringify(input),
|
|
1118
|
+
providerMetadata: { [providerOptionsName]: { itemId: chunk.item.id } }
|
|
1119
|
+
});
|
|
1120
|
+
hasToolCalls = true;
|
|
1121
|
+
customToolCallsByItemId.delete(chunk.item.id);
|
|
1122
|
+
} else if (chunk.type === "response.output_item.done" && isOpenResponsesExtensionItem(chunk.item) && extensionRegistry.byItemType.has(chunk.item.type)) try {
|
|
1123
|
+
const decoded = await decodeExtensionItem({
|
|
1124
|
+
extensionRegistry,
|
|
1125
|
+
item: chunk.item,
|
|
1126
|
+
mode: "stream",
|
|
1127
|
+
providerOptionsName
|
|
1128
|
+
});
|
|
1129
|
+
for (const part of decoded ?? []) {
|
|
1130
|
+
controller.enqueue(part);
|
|
1131
|
+
hasToolCalls ||= part.type === "tool-call";
|
|
1132
|
+
}
|
|
1133
|
+
} catch (error) {
|
|
1134
|
+
controller.enqueue({
|
|
1135
|
+
type: "error",
|
|
1136
|
+
error
|
|
1137
|
+
});
|
|
1138
|
+
}
|
|
1139
|
+
else if (chunk.type === "response.output_item.added" && chunk.item.type === "reasoning") {
|
|
1140
|
+
controller.enqueue({
|
|
1141
|
+
type: "reasoning-start",
|
|
1142
|
+
id: chunk.item.id
|
|
1143
|
+
});
|
|
1144
|
+
activeReasoningId = chunk.item.id;
|
|
1145
|
+
} else if (chunk.type === "response.reasoning_summary_text.delta" || chunk.type === "response.reasoning_text.delta") {
|
|
1146
|
+
const reasoningChunk = chunk;
|
|
1147
|
+
controller.enqueue({
|
|
1148
|
+
type: "reasoning-delta",
|
|
1149
|
+
id: reasoningChunk.item_id,
|
|
1150
|
+
delta: reasoningChunk.delta
|
|
1151
|
+
});
|
|
1152
|
+
} else if (chunk.type === "response.output_item.done" && chunk.item.type === "reasoning") {
|
|
1153
|
+
controller.enqueue({
|
|
1154
|
+
type: "reasoning-end",
|
|
1155
|
+
id: chunk.item.id,
|
|
1156
|
+
providerMetadata: createReasoningProviderMetadata({
|
|
1157
|
+
part: chunk.item,
|
|
1158
|
+
providerOptionsName
|
|
1159
|
+
})
|
|
1160
|
+
});
|
|
1161
|
+
if (activeReasoningId === chunk.item.id) activeReasoningId = void 0;
|
|
1162
|
+
} else if (chunk.type === "response.output_item.added" && chunk.item.type === "message") controller.enqueue({
|
|
1163
|
+
type: "text-start",
|
|
1164
|
+
id: chunk.item.id
|
|
1165
|
+
});
|
|
1166
|
+
else if (chunk.type === "response.output_text.delta") controller.enqueue({
|
|
1167
|
+
type: "text-delta",
|
|
1168
|
+
id: chunk.item_id,
|
|
1169
|
+
delta: chunk.delta
|
|
1170
|
+
});
|
|
1171
|
+
else if (chunk.type === "response.output_item.done" && chunk.item.type === "message") {
|
|
1172
|
+
const annotations = chunk.item.content.flatMap(getOutputTextAnnotations);
|
|
1173
|
+
controller.enqueue({
|
|
1174
|
+
type: "text-end",
|
|
1175
|
+
id: chunk.item.id,
|
|
1176
|
+
providerMetadata: { [providerOptionsName]: {
|
|
1177
|
+
itemId: chunk.item.id,
|
|
1178
|
+
...annotations.length > 0 && { annotations }
|
|
1179
|
+
} }
|
|
1180
|
+
});
|
|
1181
|
+
} else if (chunk.type === "response.completed" || chunk.type === "response.incomplete") {
|
|
1182
|
+
const reason = chunk.response.incomplete_details?.reason;
|
|
1183
|
+
finishReason = {
|
|
1184
|
+
unified: mapOpenResponsesFinishReason({
|
|
1185
|
+
finishReason: reason,
|
|
1186
|
+
hasToolCalls
|
|
1187
|
+
}),
|
|
1188
|
+
raw: reason ?? void 0
|
|
1189
|
+
};
|
|
1190
|
+
updateUsage(chunk.response.usage);
|
|
1191
|
+
} else if (chunk.type === "response.failed") {
|
|
1192
|
+
finishReason = {
|
|
1193
|
+
unified: "error",
|
|
1194
|
+
raw: chunk.response.error?.code ?? chunk.response.status
|
|
1195
|
+
};
|
|
1196
|
+
updateUsage(chunk.response.usage);
|
|
1197
|
+
if (chunk.response.error != null) controller.enqueue({
|
|
1198
|
+
type: "error",
|
|
1199
|
+
error: createOpenResponsesStreamError({
|
|
1200
|
+
type: chunk.type,
|
|
1201
|
+
error: chunk.response.error,
|
|
1202
|
+
data: chunk,
|
|
1203
|
+
getResponseErrorMetadata
|
|
1204
|
+
})
|
|
1205
|
+
});
|
|
1206
|
+
} else if (chunk.type === "error") {
|
|
1207
|
+
finishReason = {
|
|
1208
|
+
unified: "error",
|
|
1209
|
+
raw: chunk.error.code
|
|
1210
|
+
};
|
|
1211
|
+
controller.enqueue({
|
|
1212
|
+
type: "error",
|
|
1213
|
+
error: createOpenResponsesStreamError({
|
|
1214
|
+
type: chunk.type,
|
|
1215
|
+
error: chunk.error,
|
|
1216
|
+
data: chunk,
|
|
1217
|
+
getResponseErrorMetadata
|
|
1218
|
+
})
|
|
1219
|
+
});
|
|
1220
|
+
}
|
|
1221
|
+
},
|
|
1222
|
+
flush(controller) {
|
|
1223
|
+
if (activeReasoningId != null) controller.enqueue({
|
|
1224
|
+
type: "reasoning-end",
|
|
1225
|
+
id: activeReasoningId
|
|
1226
|
+
});
|
|
1227
|
+
controller.enqueue({
|
|
1228
|
+
type: "finish",
|
|
1229
|
+
finishReason,
|
|
1230
|
+
usage,
|
|
1231
|
+
providerMetadata: void 0
|
|
1232
|
+
});
|
|
1233
|
+
}
|
|
1234
|
+
})),
|
|
1235
|
+
request: { body },
|
|
1236
|
+
response: { headers: responseHeaders }
|
|
1237
|
+
};
|
|
1238
|
+
}
|
|
1608
1239
|
};
|
|
1609
|
-
function createOpenResponsesStreamError({
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
type,
|
|
1618
|
-
code: error.code,
|
|
1619
|
-
...getResponseErrorMetadata?.(error),
|
|
1620
|
-
data
|
|
1621
|
-
});
|
|
1240
|
+
function createOpenResponsesStreamError({ type, error, data, getResponseErrorMetadata }) {
|
|
1241
|
+
return createProviderStreamError({
|
|
1242
|
+
message: error.message,
|
|
1243
|
+
type,
|
|
1244
|
+
code: error.code,
|
|
1245
|
+
...getResponseErrorMetadata?.(error),
|
|
1246
|
+
data
|
|
1247
|
+
});
|
|
1622
1248
|
}
|
|
1623
|
-
function createReasoningProviderMetadata({
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
type: "reasoning_text",
|
|
1637
|
-
text: contentPart.text
|
|
1638
|
-
})),
|
|
1639
|
-
...part.encrypted_content != null && {
|
|
1640
|
-
reasoningEncryptedContent: part.encrypted_content
|
|
1641
|
-
}
|
|
1642
|
-
}
|
|
1643
|
-
};
|
|
1249
|
+
function createReasoningProviderMetadata({ part, providerOptionsName, reasoningContent = part.content }) {
|
|
1250
|
+
return { [providerOptionsName]: {
|
|
1251
|
+
itemId: part.id,
|
|
1252
|
+
reasoningSummary: part.summary.map((summaryPart) => ({
|
|
1253
|
+
type: "summary_text",
|
|
1254
|
+
text: summaryPart.text
|
|
1255
|
+
})),
|
|
1256
|
+
reasoningContent: reasoningContent == null ? null : reasoningContent.map((contentPart) => ({
|
|
1257
|
+
type: "reasoning_text",
|
|
1258
|
+
text: contentPart.text
|
|
1259
|
+
})),
|
|
1260
|
+
...part.encrypted_content != null && { reasoningEncryptedContent: part.encrypted_content }
|
|
1261
|
+
} };
|
|
1644
1262
|
}
|
|
1645
|
-
async function decodeExtensionItem({
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
providerOptionsName
|
|
1664
|
-
}),
|
|
1665
|
-
...decoded.map(
|
|
1666
|
-
(part) => addExtensionItemReferenceMetadata({
|
|
1667
|
-
extension,
|
|
1668
|
-
item,
|
|
1669
|
-
part,
|
|
1670
|
-
providerOptionsName
|
|
1671
|
-
})
|
|
1672
|
-
)
|
|
1673
|
-
];
|
|
1263
|
+
async function decodeExtensionItem({ extensionRegistry, item, mode, providerOptionsName }) {
|
|
1264
|
+
const extension = extensionRegistry.byItemType.get(item.type);
|
|
1265
|
+
if (extension == null) return;
|
|
1266
|
+
const decoded = await extension.decodeItem({
|
|
1267
|
+
item,
|
|
1268
|
+
mode
|
|
1269
|
+
});
|
|
1270
|
+
if (decoded == null) return;
|
|
1271
|
+
return [createExtensionReplayCarrier({
|
|
1272
|
+
extension,
|
|
1273
|
+
item,
|
|
1274
|
+
providerOptionsName
|
|
1275
|
+
}), ...decoded.map((part) => addExtensionItemReferenceMetadata({
|
|
1276
|
+
extension,
|
|
1277
|
+
item,
|
|
1278
|
+
part,
|
|
1279
|
+
providerOptionsName
|
|
1280
|
+
}))];
|
|
1674
1281
|
}
|
|
1675
|
-
function createExtensionReplayCarrier({
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
[providerOptionsName]: {
|
|
1685
|
-
openResponsesExtension: {
|
|
1686
|
-
id: extension.id,
|
|
1687
|
-
item
|
|
1688
|
-
}
|
|
1689
|
-
}
|
|
1690
|
-
}
|
|
1691
|
-
};
|
|
1282
|
+
function createExtensionReplayCarrier({ extension, item, providerOptionsName }) {
|
|
1283
|
+
return {
|
|
1284
|
+
type: "custom",
|
|
1285
|
+
kind: "open-responses.extension-replay",
|
|
1286
|
+
providerMetadata: { [providerOptionsName]: { openResponsesExtension: {
|
|
1287
|
+
id: extension.id,
|
|
1288
|
+
item
|
|
1289
|
+
} } }
|
|
1290
|
+
};
|
|
1692
1291
|
}
|
|
1693
|
-
function addExtensionItemReferenceMetadata({
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
itemId: item.id
|
|
1709
|
-
}
|
|
1710
|
-
}
|
|
1711
|
-
}
|
|
1712
|
-
};
|
|
1292
|
+
function addExtensionItemReferenceMetadata({ extension, item, part, providerOptionsName }) {
|
|
1293
|
+
const providerMetadata = part.providerMetadata ?? {};
|
|
1294
|
+
return {
|
|
1295
|
+
...part,
|
|
1296
|
+
providerMetadata: {
|
|
1297
|
+
...providerMetadata,
|
|
1298
|
+
[providerOptionsName]: {
|
|
1299
|
+
...providerMetadata[providerOptionsName],
|
|
1300
|
+
openResponsesExtension: {
|
|
1301
|
+
id: extension.id,
|
|
1302
|
+
itemId: item.id
|
|
1303
|
+
}
|
|
1304
|
+
}
|
|
1305
|
+
}
|
|
1306
|
+
};
|
|
1713
1307
|
}
|
|
1714
1308
|
function getOutputTextAnnotations(value) {
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
end_index: annotation.end_index,
|
|
1724
|
-
url: annotation.url,
|
|
1725
|
-
title: annotation.title
|
|
1726
|
-
}));
|
|
1309
|
+
if (value == null || typeof value !== "object" || !("annotations" in value) || !Array.isArray(value.annotations) || !value.annotations.every((annotation) => annotation != null && typeof annotation === "object" && annotation.type === "url_citation" && typeof annotation.start_index === "number" && typeof annotation.end_index === "number" && typeof annotation.url === "string" && typeof annotation.title === "string")) return [];
|
|
1310
|
+
return value.annotations.map((annotation) => ({
|
|
1311
|
+
type: "url_citation",
|
|
1312
|
+
start_index: annotation.start_index,
|
|
1313
|
+
end_index: annotation.end_index,
|
|
1314
|
+
url: annotation.url,
|
|
1315
|
+
title: annotation.title
|
|
1316
|
+
}));
|
|
1727
1317
|
}
|
|
1728
|
-
|
|
1729
|
-
|
|
1318
|
+
//#endregion
|
|
1319
|
+
//#region src/open-responses-provider.ts
|
|
1730
1320
|
function createOpenResponses(options) {
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
};
|
|
1778
|
-
provider.imageModel = (modelId) => {
|
|
1779
|
-
throw new NoSuchModelError({ modelId, modelType: "imageModel" });
|
|
1780
|
-
};
|
|
1781
|
-
return provider;
|
|
1321
|
+
const providerName = options.name;
|
|
1322
|
+
const customToolId = options.customToolId ?? "open-responses.custom";
|
|
1323
|
+
const extensionRegistry = createOpenResponsesExtensionRegistry(options.experimental_extensions);
|
|
1324
|
+
const getHeaders = () => withUserAgentSuffix({
|
|
1325
|
+
...options.apiKey ? { Authorization: `Bearer ${options.apiKey}` } : {},
|
|
1326
|
+
...typeof options.headers === "function" ? options.headers() : options.headers
|
|
1327
|
+
}, options.userAgentSuffix ?? `ai-sdk-open-responses/2.0.58`);
|
|
1328
|
+
const createResponsesModel = (modelId) => {
|
|
1329
|
+
return new OpenResponsesLanguageModel(modelId, {
|
|
1330
|
+
provider: `${providerName}.responses`,
|
|
1331
|
+
providerOptionsName: providerName,
|
|
1332
|
+
headers: getHeaders,
|
|
1333
|
+
url: options.url,
|
|
1334
|
+
fetch: options.fetch,
|
|
1335
|
+
failedResponseHandler: options.failedResponseHandler,
|
|
1336
|
+
getResponseErrorMetadata: options.getResponseErrorMetadata,
|
|
1337
|
+
generateId: () => generateId(),
|
|
1338
|
+
extensionRegistry,
|
|
1339
|
+
strictResponseInput: options.strictResponseInput,
|
|
1340
|
+
customToolId,
|
|
1341
|
+
structuredOutputs: options.structuredOutputs
|
|
1342
|
+
});
|
|
1343
|
+
};
|
|
1344
|
+
const createLanguageModel = (modelId) => {
|
|
1345
|
+
if (new.target) throw new Error("The OpenAI model function cannot be called with the new keyword.");
|
|
1346
|
+
return createResponsesModel(modelId);
|
|
1347
|
+
};
|
|
1348
|
+
const provider = function(modelId) {
|
|
1349
|
+
return createLanguageModel(modelId);
|
|
1350
|
+
};
|
|
1351
|
+
provider.specificationVersion = "v4";
|
|
1352
|
+
provider.languageModel = createLanguageModel;
|
|
1353
|
+
provider.tools = createOpenResponsesTools({ customToolId });
|
|
1354
|
+
provider.embeddingModel = (modelId) => {
|
|
1355
|
+
throw new NoSuchModelError({
|
|
1356
|
+
modelId,
|
|
1357
|
+
modelType: "embeddingModel"
|
|
1358
|
+
});
|
|
1359
|
+
};
|
|
1360
|
+
provider.imageModel = (modelId) => {
|
|
1361
|
+
throw new NoSuchModelError({
|
|
1362
|
+
modelId,
|
|
1363
|
+
modelType: "imageModel"
|
|
1364
|
+
});
|
|
1365
|
+
};
|
|
1366
|
+
return provider;
|
|
1782
1367
|
}
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
};
|
|
1368
|
+
//#endregion
|
|
1369
|
+
export { VERSION, createOpenResponses };
|
|
1370
|
+
|
|
1787
1371
|
//# sourceMappingURL=index.js.map
|