@ai-sdk/open-responses 2.0.0-beta.21 → 2.0.0-beta.22

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