@ai-sdk/open-responses 0.0.0-6b196531-20260710185421
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 +606 -0
- package/LICENSE +13 -0
- package/README.md +57 -0
- package/dist/index.d.ts +40 -0
- package/dist/index.js +729 -0
- package/dist/index.js.map +1 -0
- package/docs/06-open-responses.mdx +238 -0
- package/package.json +76 -0
- package/src/index.ts +11 -0
- package/src/open-responses-provider.ts +100 -0
- package/src/responses/convert-to-open-responses-input.ts +253 -0
- package/src/responses/map-open-responses-finish-reason.ts +21 -0
- package/src/responses/open-responses-api.ts +1238 -0
- package/src/responses/open-responses-config.ts +10 -0
- package/src/responses/open-responses-language-model-options.ts +22 -0
- package/src/responses/open-responses-language-model.ts +557 -0
- package/src/version.ts +6 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,729 @@
|
|
|
1
|
+
// src/version.ts
|
|
2
|
+
var VERSION = true ? "0.0.0-6b196531-20260710185421" : "0.0.0-test";
|
|
3
|
+
|
|
4
|
+
// src/open-responses-provider.ts
|
|
5
|
+
import {
|
|
6
|
+
NoSuchModelError
|
|
7
|
+
} from "@ai-sdk/provider";
|
|
8
|
+
import {
|
|
9
|
+
generateId,
|
|
10
|
+
withUserAgentSuffix
|
|
11
|
+
} from "@ai-sdk/provider-utils";
|
|
12
|
+
|
|
13
|
+
// src/responses/open-responses-language-model.ts
|
|
14
|
+
import {
|
|
15
|
+
combineHeaders,
|
|
16
|
+
createEventSourceResponseHandler,
|
|
17
|
+
createJsonErrorResponseHandler,
|
|
18
|
+
createJsonResponseHandler,
|
|
19
|
+
isCustomReasoning,
|
|
20
|
+
jsonSchema,
|
|
21
|
+
mapReasoningToProviderEffort,
|
|
22
|
+
parseProviderOptions,
|
|
23
|
+
postJsonToApi,
|
|
24
|
+
serializeModelOptions,
|
|
25
|
+
WORKFLOW_SERIALIZE,
|
|
26
|
+
WORKFLOW_DESERIALIZE
|
|
27
|
+
} from "@ai-sdk/provider-utils";
|
|
28
|
+
import { z as z3 } from "zod/v4";
|
|
29
|
+
|
|
30
|
+
// src/responses/convert-to-open-responses-input.ts
|
|
31
|
+
import {
|
|
32
|
+
UnsupportedFunctionalityError
|
|
33
|
+
} from "@ai-sdk/provider";
|
|
34
|
+
import {
|
|
35
|
+
convertToBase64,
|
|
36
|
+
getTopLevelMediaType,
|
|
37
|
+
resolveFullMediaType
|
|
38
|
+
} from "@ai-sdk/provider-utils";
|
|
39
|
+
async function convertToOpenResponsesInput({
|
|
40
|
+
prompt
|
|
41
|
+
}) {
|
|
42
|
+
var _a, _b, _c;
|
|
43
|
+
const input = [];
|
|
44
|
+
const warnings = [];
|
|
45
|
+
const systemMessages = [];
|
|
46
|
+
for (const { role, content } of prompt) {
|
|
47
|
+
switch (role) {
|
|
48
|
+
case "system": {
|
|
49
|
+
systemMessages.push(content);
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
case "user": {
|
|
53
|
+
const userContent = [];
|
|
54
|
+
for (const part of content) {
|
|
55
|
+
switch (part.type) {
|
|
56
|
+
case "text": {
|
|
57
|
+
userContent.push({ type: "input_text", text: part.text });
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
case "file": {
|
|
61
|
+
switch (part.data.type) {
|
|
62
|
+
case "reference": {
|
|
63
|
+
throw new UnsupportedFunctionalityError({
|
|
64
|
+
functionality: "file parts with provider references"
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
case "text": {
|
|
68
|
+
throw new UnsupportedFunctionalityError({
|
|
69
|
+
functionality: "text file parts"
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
case "url":
|
|
73
|
+
case "data": {
|
|
74
|
+
const topLevel = getTopLevelMediaType(part.mediaType);
|
|
75
|
+
if (topLevel === "image") {
|
|
76
|
+
userContent.push({
|
|
77
|
+
type: "input_image",
|
|
78
|
+
...part.data.type === "url" ? { image_url: part.data.url.toString() } : {
|
|
79
|
+
image_url: `data:${resolveFullMediaType({ part })};base64,${convertToBase64(part.data.data)}`
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
} else if (part.data.type === "url") {
|
|
83
|
+
userContent.push({
|
|
84
|
+
type: "input_file",
|
|
85
|
+
file_url: part.data.url.toString()
|
|
86
|
+
});
|
|
87
|
+
} else {
|
|
88
|
+
const fullMediaType = resolveFullMediaType({ part });
|
|
89
|
+
userContent.push({
|
|
90
|
+
type: "input_file",
|
|
91
|
+
filename: (_a = part.filename) != null ? _a : "data",
|
|
92
|
+
file_data: `data:${fullMediaType};base64,${convertToBase64(part.data.data)}`
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
input.push({ type: "message", role: "user", content: userContent });
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
case "assistant": {
|
|
106
|
+
const assistantContent = [];
|
|
107
|
+
const toolCalls = [];
|
|
108
|
+
for (const part of content) {
|
|
109
|
+
switch (part.type) {
|
|
110
|
+
case "text": {
|
|
111
|
+
assistantContent.push({ type: "output_text", text: part.text });
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
case "tool-call": {
|
|
115
|
+
const argumentsValue = typeof part.input === "string" ? part.input : JSON.stringify(part.input);
|
|
116
|
+
toolCalls.push({
|
|
117
|
+
type: "function_call",
|
|
118
|
+
call_id: part.toolCallId,
|
|
119
|
+
name: part.toolName,
|
|
120
|
+
arguments: argumentsValue
|
|
121
|
+
});
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (assistantContent.length > 0) {
|
|
127
|
+
input.push({
|
|
128
|
+
type: "message",
|
|
129
|
+
role: "assistant",
|
|
130
|
+
content: assistantContent
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
for (const toolCall of toolCalls) {
|
|
134
|
+
input.push(toolCall);
|
|
135
|
+
}
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
case "tool": {
|
|
139
|
+
for (const part of content) {
|
|
140
|
+
if (part.type === "tool-result") {
|
|
141
|
+
const output = part.output;
|
|
142
|
+
let contentValue;
|
|
143
|
+
switch (output.type) {
|
|
144
|
+
case "text":
|
|
145
|
+
case "error-text":
|
|
146
|
+
contentValue = output.value;
|
|
147
|
+
break;
|
|
148
|
+
case "execution-denied":
|
|
149
|
+
contentValue = (_b = output.reason) != null ? _b : "Tool call execution denied.";
|
|
150
|
+
break;
|
|
151
|
+
case "json":
|
|
152
|
+
case "error-json":
|
|
153
|
+
contentValue = JSON.stringify(output.value);
|
|
154
|
+
break;
|
|
155
|
+
case "content": {
|
|
156
|
+
const contentParts = [];
|
|
157
|
+
for (const item of output.value) {
|
|
158
|
+
switch (item.type) {
|
|
159
|
+
case "text": {
|
|
160
|
+
contentParts.push({
|
|
161
|
+
type: "input_text",
|
|
162
|
+
text: item.text
|
|
163
|
+
});
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
case "file": {
|
|
167
|
+
const topLevel = getTopLevelMediaType(item.mediaType);
|
|
168
|
+
if (item.data.type === "data") {
|
|
169
|
+
const fullMediaType = resolveFullMediaType({
|
|
170
|
+
part: item
|
|
171
|
+
});
|
|
172
|
+
if (topLevel === "image") {
|
|
173
|
+
contentParts.push({
|
|
174
|
+
type: "input_image",
|
|
175
|
+
image_url: `data:${fullMediaType};base64,${convertToBase64(item.data.data)}`
|
|
176
|
+
});
|
|
177
|
+
} else {
|
|
178
|
+
contentParts.push({
|
|
179
|
+
type: "input_file",
|
|
180
|
+
filename: (_c = item.filename) != null ? _c : "data",
|
|
181
|
+
file_data: `data:${fullMediaType};base64,${convertToBase64(item.data.data)}`
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
} else if (item.data.type === "url") {
|
|
185
|
+
if (topLevel === "image") {
|
|
186
|
+
contentParts.push({
|
|
187
|
+
type: "input_image",
|
|
188
|
+
image_url: item.data.url.toString()
|
|
189
|
+
});
|
|
190
|
+
} else {
|
|
191
|
+
contentParts.push({
|
|
192
|
+
type: "input_file",
|
|
193
|
+
file_url: item.data.url.toString()
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
} else {
|
|
197
|
+
warnings.push({
|
|
198
|
+
type: "other",
|
|
199
|
+
message: `unsupported tool content part type: ${item.type} with data type: ${item.data.type}`
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
default: {
|
|
205
|
+
warnings.push({
|
|
206
|
+
type: "other",
|
|
207
|
+
message: `unsupported tool content part type: ${item.type}`
|
|
208
|
+
});
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
contentValue = contentParts;
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
input.push({
|
|
218
|
+
type: "function_call_output",
|
|
219
|
+
call_id: part.toolCallId,
|
|
220
|
+
output: contentValue
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return {
|
|
229
|
+
input,
|
|
230
|
+
instructions: systemMessages.length > 0 ? systemMessages.join("\n") : void 0,
|
|
231
|
+
warnings
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// src/responses/open-responses-api.ts
|
|
236
|
+
import { lazySchema, zodSchema } from "@ai-sdk/provider-utils";
|
|
237
|
+
import { z } from "zod/v4";
|
|
238
|
+
var openResponsesErrorSchema = lazySchema(
|
|
239
|
+
() => zodSchema(
|
|
240
|
+
z.object({
|
|
241
|
+
error: z.object({
|
|
242
|
+
message: z.string(),
|
|
243
|
+
type: z.string(),
|
|
244
|
+
param: z.string(),
|
|
245
|
+
code: z.string()
|
|
246
|
+
})
|
|
247
|
+
})
|
|
248
|
+
)
|
|
249
|
+
);
|
|
250
|
+
|
|
251
|
+
// src/responses/map-open-responses-finish-reason.ts
|
|
252
|
+
function mapOpenResponsesFinishReason({
|
|
253
|
+
finishReason,
|
|
254
|
+
hasToolCalls
|
|
255
|
+
}) {
|
|
256
|
+
switch (finishReason) {
|
|
257
|
+
case void 0:
|
|
258
|
+
case null:
|
|
259
|
+
return hasToolCalls ? "tool-calls" : "stop";
|
|
260
|
+
case "max_output_tokens":
|
|
261
|
+
return "length";
|
|
262
|
+
case "content_filter":
|
|
263
|
+
return "content-filter";
|
|
264
|
+
default:
|
|
265
|
+
return hasToolCalls ? "tool-calls" : "other";
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// src/responses/open-responses-language-model-options.ts
|
|
270
|
+
import {
|
|
271
|
+
lazySchema as lazySchema2,
|
|
272
|
+
zodSchema as zodSchema2
|
|
273
|
+
} from "@ai-sdk/provider-utils";
|
|
274
|
+
import { z as z2 } from "zod/v4";
|
|
275
|
+
var openResponsesLanguageModelOptions = lazySchema2(
|
|
276
|
+
() => zodSchema2(
|
|
277
|
+
z2.object({
|
|
278
|
+
/**
|
|
279
|
+
* Controls reasoning summary output from the model.
|
|
280
|
+
* Valid values: 'concise', 'detailed', 'auto'.
|
|
281
|
+
*/
|
|
282
|
+
reasoningSummary: z2.enum(["concise", "detailed", "auto"]).nullish()
|
|
283
|
+
})
|
|
284
|
+
)
|
|
285
|
+
);
|
|
286
|
+
|
|
287
|
+
// src/responses/open-responses-language-model.ts
|
|
288
|
+
var OpenResponsesLanguageModel = class _OpenResponsesLanguageModel {
|
|
289
|
+
constructor(modelId, config) {
|
|
290
|
+
this.specificationVersion = "v4";
|
|
291
|
+
this.supportedUrls = {
|
|
292
|
+
"image/*": [/^https?:\/\/.*$/]
|
|
293
|
+
};
|
|
294
|
+
this.modelId = modelId;
|
|
295
|
+
this.config = config;
|
|
296
|
+
}
|
|
297
|
+
static [WORKFLOW_SERIALIZE](model) {
|
|
298
|
+
return serializeModelOptions({
|
|
299
|
+
modelId: model.modelId,
|
|
300
|
+
config: model.config
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
static [WORKFLOW_DESERIALIZE](options) {
|
|
304
|
+
return new _OpenResponsesLanguageModel(options.modelId, options.config);
|
|
305
|
+
}
|
|
306
|
+
get provider() {
|
|
307
|
+
return this.config.provider;
|
|
308
|
+
}
|
|
309
|
+
async getArgs({
|
|
310
|
+
maxOutputTokens,
|
|
311
|
+
temperature,
|
|
312
|
+
stopSequences,
|
|
313
|
+
topP,
|
|
314
|
+
topK,
|
|
315
|
+
presencePenalty,
|
|
316
|
+
frequencyPenalty,
|
|
317
|
+
seed,
|
|
318
|
+
reasoning,
|
|
319
|
+
prompt,
|
|
320
|
+
providerOptions,
|
|
321
|
+
tools,
|
|
322
|
+
toolChoice,
|
|
323
|
+
responseFormat
|
|
324
|
+
}) {
|
|
325
|
+
var _a;
|
|
326
|
+
const warnings = [];
|
|
327
|
+
if (stopSequences != null) {
|
|
328
|
+
warnings.push({ type: "unsupported", feature: "stopSequences" });
|
|
329
|
+
}
|
|
330
|
+
if (topK != null) {
|
|
331
|
+
warnings.push({ type: "unsupported", feature: "topK" });
|
|
332
|
+
}
|
|
333
|
+
if (seed != null) {
|
|
334
|
+
warnings.push({ type: "unsupported", feature: "seed" });
|
|
335
|
+
}
|
|
336
|
+
const {
|
|
337
|
+
input,
|
|
338
|
+
instructions,
|
|
339
|
+
warnings: inputWarnings
|
|
340
|
+
} = await convertToOpenResponsesInput({
|
|
341
|
+
prompt
|
|
342
|
+
});
|
|
343
|
+
warnings.push(...inputWarnings);
|
|
344
|
+
const functionTools = tools == null ? void 0 : tools.filter((tool) => tool.type === "function").map((tool) => ({
|
|
345
|
+
type: "function",
|
|
346
|
+
name: tool.name,
|
|
347
|
+
description: tool.description,
|
|
348
|
+
parameters: tool.inputSchema,
|
|
349
|
+
...tool.strict != null ? { strict: tool.strict } : {}
|
|
350
|
+
}));
|
|
351
|
+
const convertedToolChoice = toolChoice == null ? void 0 : toolChoice.type === "tool" ? { type: "function", name: toolChoice.toolName } : toolChoice.type;
|
|
352
|
+
const textFormat = (responseFormat == null ? void 0 : responseFormat.type) === "json" ? {
|
|
353
|
+
type: "json_schema",
|
|
354
|
+
...responseFormat.schema != null ? {
|
|
355
|
+
name: (_a = responseFormat.name) != null ? _a : "response",
|
|
356
|
+
description: responseFormat.description,
|
|
357
|
+
schema: responseFormat.schema,
|
|
358
|
+
strict: true
|
|
359
|
+
} : {}
|
|
360
|
+
} : void 0;
|
|
361
|
+
const openResponsesOptions = await parseProviderOptions({
|
|
362
|
+
provider: this.config.providerOptionsName,
|
|
363
|
+
providerOptions,
|
|
364
|
+
schema: openResponsesLanguageModelOptions
|
|
365
|
+
});
|
|
366
|
+
const resolvedReasoningEffort = isCustomReasoning(reasoning) ? reasoning === "none" ? "none" : mapReasoningToProviderEffort({
|
|
367
|
+
reasoning,
|
|
368
|
+
effortMap: {
|
|
369
|
+
minimal: "low",
|
|
370
|
+
low: "low",
|
|
371
|
+
medium: "medium",
|
|
372
|
+
high: "high",
|
|
373
|
+
xhigh: "xhigh"
|
|
374
|
+
},
|
|
375
|
+
warnings
|
|
376
|
+
}) : void 0;
|
|
377
|
+
return {
|
|
378
|
+
body: {
|
|
379
|
+
model: this.modelId,
|
|
380
|
+
input,
|
|
381
|
+
instructions,
|
|
382
|
+
max_output_tokens: maxOutputTokens,
|
|
383
|
+
temperature,
|
|
384
|
+
top_p: topP,
|
|
385
|
+
presence_penalty: presencePenalty,
|
|
386
|
+
frequency_penalty: frequencyPenalty,
|
|
387
|
+
reasoning: resolvedReasoningEffort != null || (openResponsesOptions == null ? void 0 : openResponsesOptions.reasoningSummary) != null ? {
|
|
388
|
+
...resolvedReasoningEffort != null && {
|
|
389
|
+
effort: resolvedReasoningEffort
|
|
390
|
+
},
|
|
391
|
+
...(openResponsesOptions == null ? void 0 : openResponsesOptions.reasoningSummary) != null && {
|
|
392
|
+
summary: openResponsesOptions.reasoningSummary
|
|
393
|
+
}
|
|
394
|
+
} : void 0,
|
|
395
|
+
tools: (functionTools == null ? void 0 : functionTools.length) ? functionTools : void 0,
|
|
396
|
+
tool_choice: convertedToolChoice,
|
|
397
|
+
...textFormat != null && { text: { format: textFormat } }
|
|
398
|
+
},
|
|
399
|
+
warnings
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
async doGenerate(options) {
|
|
403
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
404
|
+
const { body, warnings } = await this.getArgs(options);
|
|
405
|
+
const {
|
|
406
|
+
responseHeaders,
|
|
407
|
+
value: response,
|
|
408
|
+
rawValue: rawResponse
|
|
409
|
+
} = await postJsonToApi({
|
|
410
|
+
url: this.config.url,
|
|
411
|
+
headers: combineHeaders((_b = (_a = this.config).headers) == null ? void 0 : _b.call(_a), options.headers),
|
|
412
|
+
body,
|
|
413
|
+
failedResponseHandler: createJsonErrorResponseHandler({
|
|
414
|
+
errorSchema: openResponsesErrorSchema,
|
|
415
|
+
errorToMessage: (error) => error.error.message
|
|
416
|
+
}),
|
|
417
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
418
|
+
// do not validate the response body, only apply types to the response body
|
|
419
|
+
jsonSchema(() => {
|
|
420
|
+
throw new Error("json schema not implemented");
|
|
421
|
+
})
|
|
422
|
+
),
|
|
423
|
+
abortSignal: options.abortSignal,
|
|
424
|
+
fetch: this.config.fetch
|
|
425
|
+
});
|
|
426
|
+
const content = [];
|
|
427
|
+
let hasToolCalls = false;
|
|
428
|
+
for (const part of response.output) {
|
|
429
|
+
switch (part.type) {
|
|
430
|
+
// TODO AI SDK 7 adjust reasoning in the specification to better support the reasoning structure from open responses.
|
|
431
|
+
case "reasoning": {
|
|
432
|
+
for (const contentPart of (_c = part.content) != null ? _c : []) {
|
|
433
|
+
content.push({
|
|
434
|
+
type: "reasoning",
|
|
435
|
+
text: contentPart.text
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
break;
|
|
439
|
+
}
|
|
440
|
+
case "message": {
|
|
441
|
+
for (const contentPart of part.content) {
|
|
442
|
+
content.push({
|
|
443
|
+
type: "text",
|
|
444
|
+
text: contentPart.text
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
break;
|
|
448
|
+
}
|
|
449
|
+
case "function_call": {
|
|
450
|
+
hasToolCalls = true;
|
|
451
|
+
content.push({
|
|
452
|
+
type: "tool-call",
|
|
453
|
+
toolCallId: part.call_id,
|
|
454
|
+
toolName: part.name,
|
|
455
|
+
input: part.arguments
|
|
456
|
+
});
|
|
457
|
+
break;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
const usage = response.usage;
|
|
462
|
+
const inputTokens = usage == null ? void 0 : usage.input_tokens;
|
|
463
|
+
const cachedInputTokens = (_d = usage == null ? void 0 : usage.input_tokens_details) == null ? void 0 : _d.cached_tokens;
|
|
464
|
+
const outputTokens = usage == null ? void 0 : usage.output_tokens;
|
|
465
|
+
const reasoningTokens = (_e = usage == null ? void 0 : usage.output_tokens_details) == null ? void 0 : _e.reasoning_tokens;
|
|
466
|
+
return {
|
|
467
|
+
content,
|
|
468
|
+
finishReason: {
|
|
469
|
+
unified: mapOpenResponsesFinishReason({
|
|
470
|
+
finishReason: (_f = response.incomplete_details) == null ? void 0 : _f.reason,
|
|
471
|
+
hasToolCalls
|
|
472
|
+
}),
|
|
473
|
+
raw: (_h = (_g = response.incomplete_details) == null ? void 0 : _g.reason) != null ? _h : void 0
|
|
474
|
+
},
|
|
475
|
+
usage: {
|
|
476
|
+
inputTokens: {
|
|
477
|
+
total: inputTokens,
|
|
478
|
+
noCache: (inputTokens != null ? inputTokens : 0) - (cachedInputTokens != null ? cachedInputTokens : 0),
|
|
479
|
+
cacheRead: cachedInputTokens,
|
|
480
|
+
cacheWrite: void 0
|
|
481
|
+
},
|
|
482
|
+
outputTokens: {
|
|
483
|
+
total: outputTokens,
|
|
484
|
+
text: (outputTokens != null ? outputTokens : 0) - (reasoningTokens != null ? reasoningTokens : 0),
|
|
485
|
+
reasoning: reasoningTokens
|
|
486
|
+
},
|
|
487
|
+
raw: response.usage
|
|
488
|
+
},
|
|
489
|
+
request: { body },
|
|
490
|
+
response: {
|
|
491
|
+
id: response.id,
|
|
492
|
+
timestamp: new Date(response.created_at * 1e3),
|
|
493
|
+
modelId: response.model,
|
|
494
|
+
headers: responseHeaders,
|
|
495
|
+
body: rawResponse
|
|
496
|
+
},
|
|
497
|
+
providerMetadata: void 0,
|
|
498
|
+
warnings
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
async doStream(options) {
|
|
502
|
+
var _a, _b;
|
|
503
|
+
const { body, warnings } = await this.getArgs(options);
|
|
504
|
+
const { responseHeaders, value: response } = await postJsonToApi({
|
|
505
|
+
url: this.config.url,
|
|
506
|
+
headers: combineHeaders((_b = (_a = this.config).headers) == null ? void 0 : _b.call(_a), options.headers),
|
|
507
|
+
body: {
|
|
508
|
+
...body,
|
|
509
|
+
stream: true
|
|
510
|
+
},
|
|
511
|
+
failedResponseHandler: createJsonErrorResponseHandler({
|
|
512
|
+
errorSchema: openResponsesErrorSchema,
|
|
513
|
+
errorToMessage: (error) => error.error.message
|
|
514
|
+
}),
|
|
515
|
+
successfulResponseHandler: createEventSourceResponseHandler(z3.any()),
|
|
516
|
+
abortSignal: options.abortSignal,
|
|
517
|
+
fetch: this.config.fetch
|
|
518
|
+
});
|
|
519
|
+
const usage = {
|
|
520
|
+
inputTokens: {
|
|
521
|
+
total: void 0,
|
|
522
|
+
noCache: void 0,
|
|
523
|
+
cacheRead: void 0,
|
|
524
|
+
cacheWrite: void 0
|
|
525
|
+
},
|
|
526
|
+
outputTokens: {
|
|
527
|
+
total: void 0,
|
|
528
|
+
text: void 0,
|
|
529
|
+
reasoning: void 0
|
|
530
|
+
}
|
|
531
|
+
};
|
|
532
|
+
const updateUsage = (responseUsage) => {
|
|
533
|
+
var _a2, _b2;
|
|
534
|
+
if (!responseUsage) {
|
|
535
|
+
return;
|
|
536
|
+
}
|
|
537
|
+
const inputTokens = responseUsage.input_tokens;
|
|
538
|
+
const cachedInputTokens = (_a2 = responseUsage.input_tokens_details) == null ? void 0 : _a2.cached_tokens;
|
|
539
|
+
const outputTokens = responseUsage.output_tokens;
|
|
540
|
+
const reasoningTokens = (_b2 = responseUsage.output_tokens_details) == null ? void 0 : _b2.reasoning_tokens;
|
|
541
|
+
usage.inputTokens = {
|
|
542
|
+
total: inputTokens,
|
|
543
|
+
noCache: (inputTokens != null ? inputTokens : 0) - (cachedInputTokens != null ? cachedInputTokens : 0),
|
|
544
|
+
cacheRead: cachedInputTokens,
|
|
545
|
+
cacheWrite: void 0
|
|
546
|
+
};
|
|
547
|
+
usage.outputTokens = {
|
|
548
|
+
total: outputTokens,
|
|
549
|
+
text: (outputTokens != null ? outputTokens : 0) - (reasoningTokens != null ? reasoningTokens : 0),
|
|
550
|
+
reasoning: reasoningTokens
|
|
551
|
+
};
|
|
552
|
+
usage.raw = responseUsage;
|
|
553
|
+
};
|
|
554
|
+
let isActiveReasoning = false;
|
|
555
|
+
let hasToolCalls = false;
|
|
556
|
+
let finishReason = {
|
|
557
|
+
unified: "other",
|
|
558
|
+
raw: void 0
|
|
559
|
+
};
|
|
560
|
+
const toolCallsByItemId = /* @__PURE__ */ new Map();
|
|
561
|
+
return {
|
|
562
|
+
stream: response.pipeThrough(
|
|
563
|
+
new TransformStream({
|
|
564
|
+
start(controller) {
|
|
565
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
566
|
+
},
|
|
567
|
+
transform(parseResult, controller) {
|
|
568
|
+
var _a2, _b2, _c, _d, _e, _f, _g, _h;
|
|
569
|
+
if (options.includeRawChunks) {
|
|
570
|
+
controller.enqueue({
|
|
571
|
+
type: "raw",
|
|
572
|
+
rawValue: parseResult.rawValue
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
if (!parseResult.success) {
|
|
576
|
+
controller.enqueue({ type: "error", error: parseResult.error });
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
const chunk = parseResult.value;
|
|
580
|
+
if (chunk.type === "response.output_item.added" && chunk.item.type === "function_call") {
|
|
581
|
+
toolCallsByItemId.set(chunk.item.id, {
|
|
582
|
+
toolName: chunk.item.name,
|
|
583
|
+
toolCallId: chunk.item.call_id,
|
|
584
|
+
arguments: chunk.item.arguments
|
|
585
|
+
});
|
|
586
|
+
} else if (chunk.type === "response.function_call_arguments.delta") {
|
|
587
|
+
const functionCallChunk = chunk;
|
|
588
|
+
const toolCall = toolCallsByItemId.get(functionCallChunk.item_id);
|
|
589
|
+
if (toolCall == null) {
|
|
590
|
+
toolCallsByItemId.set(functionCallChunk.item_id, {
|
|
591
|
+
arguments: functionCallChunk.delta
|
|
592
|
+
});
|
|
593
|
+
return;
|
|
594
|
+
}
|
|
595
|
+
toolCall.arguments = ((_a2 = toolCall.arguments) != null ? _a2 : "") + functionCallChunk.delta;
|
|
596
|
+
} else if (chunk.type === "response.function_call_arguments.done") {
|
|
597
|
+
const functionCallChunk = chunk;
|
|
598
|
+
const toolCall = toolCallsByItemId.get(functionCallChunk.item_id);
|
|
599
|
+
if (toolCall == null) {
|
|
600
|
+
toolCallsByItemId.set(functionCallChunk.item_id, {
|
|
601
|
+
arguments: functionCallChunk.arguments
|
|
602
|
+
});
|
|
603
|
+
return;
|
|
604
|
+
}
|
|
605
|
+
toolCall.arguments = functionCallChunk.arguments;
|
|
606
|
+
} else if (chunk.type === "response.output_item.done" && chunk.item.type === "function_call") {
|
|
607
|
+
const toolCall = toolCallsByItemId.get(chunk.item.id);
|
|
608
|
+
const toolName = (_b2 = toolCall == null ? void 0 : toolCall.toolName) != null ? _b2 : chunk.item.name;
|
|
609
|
+
const toolCallId = (_c = toolCall == null ? void 0 : toolCall.toolCallId) != null ? _c : chunk.item.call_id;
|
|
610
|
+
const input = (_e = (_d = toolCall == null ? void 0 : toolCall.arguments) != null ? _d : chunk.item.arguments) != null ? _e : "";
|
|
611
|
+
controller.enqueue({
|
|
612
|
+
type: "tool-call",
|
|
613
|
+
toolCallId,
|
|
614
|
+
toolName,
|
|
615
|
+
input
|
|
616
|
+
});
|
|
617
|
+
hasToolCalls = true;
|
|
618
|
+
toolCallsByItemId.delete(chunk.item.id);
|
|
619
|
+
} else if (chunk.type === "response.output_item.added" && chunk.item.type === "reasoning") {
|
|
620
|
+
controller.enqueue({
|
|
621
|
+
type: "reasoning-start",
|
|
622
|
+
id: chunk.item.id
|
|
623
|
+
});
|
|
624
|
+
isActiveReasoning = true;
|
|
625
|
+
} else if (chunk.type === "response.reasoning_text.delta") {
|
|
626
|
+
const reasoningChunk = chunk;
|
|
627
|
+
controller.enqueue({
|
|
628
|
+
type: "reasoning-delta",
|
|
629
|
+
id: reasoningChunk.item_id,
|
|
630
|
+
delta: reasoningChunk.delta
|
|
631
|
+
});
|
|
632
|
+
} else if (chunk.type === "response.output_item.done" && chunk.item.type === "reasoning") {
|
|
633
|
+
controller.enqueue({ type: "reasoning-end", id: chunk.item.id });
|
|
634
|
+
isActiveReasoning = false;
|
|
635
|
+
} else if (chunk.type === "response.output_item.added" && chunk.item.type === "message") {
|
|
636
|
+
controller.enqueue({ type: "text-start", id: chunk.item.id });
|
|
637
|
+
} else if (chunk.type === "response.output_text.delta") {
|
|
638
|
+
controller.enqueue({
|
|
639
|
+
type: "text-delta",
|
|
640
|
+
id: chunk.item_id,
|
|
641
|
+
delta: chunk.delta
|
|
642
|
+
});
|
|
643
|
+
} else if (chunk.type === "response.output_item.done" && chunk.item.type === "message") {
|
|
644
|
+
controller.enqueue({ type: "text-end", id: chunk.item.id });
|
|
645
|
+
} else if (chunk.type === "response.completed" || chunk.type === "response.incomplete") {
|
|
646
|
+
const reason = (_f = chunk.response.incomplete_details) == null ? void 0 : _f.reason;
|
|
647
|
+
finishReason = {
|
|
648
|
+
unified: mapOpenResponsesFinishReason({
|
|
649
|
+
finishReason: reason,
|
|
650
|
+
hasToolCalls
|
|
651
|
+
}),
|
|
652
|
+
raw: reason != null ? reason : void 0
|
|
653
|
+
};
|
|
654
|
+
updateUsage(chunk.response.usage);
|
|
655
|
+
} else if (chunk.type === "response.failed") {
|
|
656
|
+
finishReason = {
|
|
657
|
+
unified: "error",
|
|
658
|
+
raw: (_h = (_g = chunk.response.error) == null ? void 0 : _g.code) != null ? _h : chunk.response.status
|
|
659
|
+
};
|
|
660
|
+
updateUsage(chunk.response.usage);
|
|
661
|
+
}
|
|
662
|
+
},
|
|
663
|
+
flush(controller) {
|
|
664
|
+
if (isActiveReasoning) {
|
|
665
|
+
controller.enqueue({ type: "reasoning-end", id: "reasoning-0" });
|
|
666
|
+
}
|
|
667
|
+
controller.enqueue({
|
|
668
|
+
type: "finish",
|
|
669
|
+
finishReason,
|
|
670
|
+
usage,
|
|
671
|
+
providerMetadata: void 0
|
|
672
|
+
});
|
|
673
|
+
}
|
|
674
|
+
})
|
|
675
|
+
),
|
|
676
|
+
request: { body },
|
|
677
|
+
response: { headers: responseHeaders }
|
|
678
|
+
};
|
|
679
|
+
}
|
|
680
|
+
};
|
|
681
|
+
|
|
682
|
+
// src/open-responses-provider.ts
|
|
683
|
+
function createOpenResponses(options) {
|
|
684
|
+
const providerName = options.name;
|
|
685
|
+
const getHeaders = () => withUserAgentSuffix(
|
|
686
|
+
{
|
|
687
|
+
...options.apiKey ? {
|
|
688
|
+
Authorization: `Bearer ${options.apiKey}`
|
|
689
|
+
} : {},
|
|
690
|
+
...options.headers
|
|
691
|
+
},
|
|
692
|
+
`ai-sdk/open-responses/${VERSION}`
|
|
693
|
+
);
|
|
694
|
+
const createResponsesModel = (modelId) => {
|
|
695
|
+
return new OpenResponsesLanguageModel(modelId, {
|
|
696
|
+
provider: `${providerName}.responses`,
|
|
697
|
+
providerOptionsName: providerName,
|
|
698
|
+
headers: getHeaders,
|
|
699
|
+
url: options.url,
|
|
700
|
+
fetch: options.fetch,
|
|
701
|
+
generateId: () => generateId()
|
|
702
|
+
});
|
|
703
|
+
};
|
|
704
|
+
const createLanguageModel = (modelId) => {
|
|
705
|
+
if (new.target) {
|
|
706
|
+
throw new Error(
|
|
707
|
+
"The OpenAI model function cannot be called with the new keyword."
|
|
708
|
+
);
|
|
709
|
+
}
|
|
710
|
+
return createResponsesModel(modelId);
|
|
711
|
+
};
|
|
712
|
+
const provider = function(modelId) {
|
|
713
|
+
return createLanguageModel(modelId);
|
|
714
|
+
};
|
|
715
|
+
provider.specificationVersion = "v4";
|
|
716
|
+
provider.languageModel = createLanguageModel;
|
|
717
|
+
provider.embeddingModel = (modelId) => {
|
|
718
|
+
throw new NoSuchModelError({ modelId, modelType: "embeddingModel" });
|
|
719
|
+
};
|
|
720
|
+
provider.imageModel = (modelId) => {
|
|
721
|
+
throw new NoSuchModelError({ modelId, modelType: "imageModel" });
|
|
722
|
+
};
|
|
723
|
+
return provider;
|
|
724
|
+
}
|
|
725
|
+
export {
|
|
726
|
+
VERSION,
|
|
727
|
+
createOpenResponses
|
|
728
|
+
};
|
|
729
|
+
//# sourceMappingURL=index.js.map
|