@ai-sdk/open-responses 2.0.0-beta.9 → 2.0.0-canary.34

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