@ai-sdk/openai 4.0.63 → 4.0.65

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 CHANGED
@@ -1,5 +1,23 @@
1
1
  # @ai-sdk/openai
2
2
 
3
+ ## 4.0.65
4
+
5
+ ### Patch Changes
6
+
7
+ - 9942196: feat(openai): add batch cancellation and listing
8
+ - Updated dependencies [9942196]
9
+ - @ai-sdk/provider@4.0.13
10
+ - @ai-sdk/provider-utils@5.0.39
11
+
12
+ ## 4.0.64
13
+
14
+ ### Patch Changes
15
+
16
+ - ccb8952: fix(openai): return an AI SDK error for empty chat completion choices
17
+ - Updated dependencies [912fb01]
18
+ - @ai-sdk/provider@4.0.12
19
+ - @ai-sdk/provider-utils@5.0.38
20
+
3
21
  ## 4.0.63
4
22
 
5
23
  ### Patch Changes
package/dist/index.js CHANGED
@@ -8,6 +8,9 @@ import {
8
8
  } from "@ai-sdk/provider-utils";
9
9
 
10
10
  // src/chat/openai-chat-language-model.ts
11
+ import {
12
+ InvalidResponseDataError
13
+ } from "@ai-sdk/provider";
11
14
  import {
12
15
  StreamingToolCallTracker,
13
16
  combineHeaders,
@@ -1224,6 +1227,12 @@ var OpenAIChatLanguageModel = class _OpenAIChatLanguageModel {
1224
1227
  fetch: this.config.fetch
1225
1228
  });
1226
1229
  const choice = response.choices[0];
1230
+ if (choice == null) {
1231
+ throw new InvalidResponseDataError({
1232
+ data: rawResponse,
1233
+ message: "Response did not contain any choices."
1234
+ });
1235
+ }
1227
1236
  const content = [];
1228
1237
  const text = choice.message.content;
1229
1238
  if (text != null && text.length > 0) {
@@ -3553,7 +3562,7 @@ var openaiTools = {
3553
3562
  // src/openai-batch.ts
3554
3563
  import {
3555
3564
  InvalidArgumentError as InvalidArgumentError2,
3556
- InvalidResponseDataError
3565
+ InvalidResponseDataError as InvalidResponseDataError2
3557
3566
  } from "@ai-sdk/provider";
3558
3567
  import {
3559
3568
  combineHeaders as combineHeaders7,
@@ -9088,30 +9097,29 @@ var openaiBatchProviderOptionsSchema = lazySchema26(
9088
9097
  })
9089
9098
  )
9090
9099
  );
9100
+ var openaiBatchResponseZodSchema = () => z28.object({
9101
+ id: z28.string(),
9102
+ status: z28.string(),
9103
+ output_file_id: z28.string().nullish(),
9104
+ error_file_id: z28.string().nullish(),
9105
+ created_at: z28.number().nullish(),
9106
+ expires_at: z28.number().nullish(),
9107
+ request_counts: z28.object({
9108
+ total: z28.number().nullish(),
9109
+ completed: z28.number().nullish(),
9110
+ failed: z28.number().nullish()
9111
+ }).nullish(),
9112
+ errors: z28.object({
9113
+ data: z28.array(
9114
+ z28.object({
9115
+ code: z28.string().nullish(),
9116
+ message: z28.string().nullish()
9117
+ })
9118
+ ).nullish()
9119
+ }).nullish()
9120
+ });
9091
9121
  var openaiBatchResponseSchema = lazySchema26(
9092
- () => zodSchema26(
9093
- z28.object({
9094
- id: z28.string(),
9095
- status: z28.string(),
9096
- output_file_id: z28.string().nullish(),
9097
- error_file_id: z28.string().nullish(),
9098
- created_at: z28.number().nullish(),
9099
- expires_at: z28.number().nullish(),
9100
- request_counts: z28.object({
9101
- total: z28.number().nullish(),
9102
- completed: z28.number().nullish(),
9103
- failed: z28.number().nullish()
9104
- }).nullish(),
9105
- errors: z28.object({
9106
- data: z28.array(
9107
- z28.object({
9108
- code: z28.string().nullish(),
9109
- message: z28.string().nullish()
9110
- })
9111
- ).nullish()
9112
- }).nullish()
9113
- })
9114
- )
9122
+ () => zodSchema26(openaiBatchResponseZodSchema())
9115
9123
  );
9116
9124
  var openaiBatchResultLineSchema = lazySchema26(
9117
9125
  () => zodSchema26(
@@ -9129,6 +9137,15 @@ var openaiBatchResultLineSchema = lazySchema26(
9129
9137
  })
9130
9138
  )
9131
9139
  );
9140
+ var openaiBatchListResponseSchema = lazySchema26(
9141
+ () => zodSchema26(
9142
+ z28.object({
9143
+ data: z28.array(openaiBatchResponseZodSchema()),
9144
+ has_more: z28.boolean(),
9145
+ last_id: z28.string().nullish()
9146
+ })
9147
+ )
9148
+ );
9132
9149
  var OpenAIBatch = class {
9133
9150
  constructor(options) {
9134
9151
  this.options = options;
@@ -9264,6 +9281,51 @@ var OpenAIBatch = class {
9264
9281
  const batch = await this.retrieveBatch(options);
9265
9282
  return convertOpenAIBatchStatus(batch);
9266
9283
  }
9284
+ async doCancelBatch(options) {
9285
+ var _a2, _b;
9286
+ await postJsonToApi6({
9287
+ url: this.getUrl(
9288
+ `/batches/${encodeURIComponent(options.batchId)}/cancel`
9289
+ ),
9290
+ headers: combineHeaders7((_b = (_a2 = this.options.config).headers) == null ? void 0 : _b.call(_a2), options.headers),
9291
+ body: {},
9292
+ failedResponseHandler: openaiFailedResponseHandler,
9293
+ successfulResponseHandler: createJsonResponseHandler7(
9294
+ openaiBatchResponseSchema
9295
+ ),
9296
+ abortSignal: options.abortSignal,
9297
+ fetch: this.options.config.fetch
9298
+ });
9299
+ return {};
9300
+ }
9301
+ async doListBatches(options) {
9302
+ var _a2, _b;
9303
+ const url = new URL(this.getUrl("/batches"));
9304
+ if (options.limit != null) {
9305
+ url.searchParams.set("limit", String(options.limit));
9306
+ }
9307
+ if (options.cursor != null) {
9308
+ url.searchParams.set("after", options.cursor);
9309
+ }
9310
+ const { value: page } = await getFromApi2({
9311
+ url: url.toString(),
9312
+ headers: combineHeaders7((_b = (_a2 = this.options.config).headers) == null ? void 0 : _b.call(_a2), options.headers),
9313
+ failedResponseHandler: openaiFailedResponseHandler,
9314
+ successfulResponseHandler: createJsonResponseHandler7(
9315
+ openaiBatchListResponseSchema
9316
+ ),
9317
+ abortSignal: options.abortSignal,
9318
+ fetch: this.options.config.fetch,
9319
+ validateUrl: false
9320
+ });
9321
+ return {
9322
+ batches: page.data.map((batch) => ({
9323
+ batchId: batch.id,
9324
+ ...convertOpenAIBatchStatus(batch)
9325
+ })),
9326
+ ...page.has_more && page.last_id != null ? { nextCursor: page.last_id } : {}
9327
+ };
9328
+ }
9267
9329
  async doGetBatchResults(options) {
9268
9330
  const batch = await this.retrieveBatch(options);
9269
9331
  const batchStatus = convertOpenAIBatchStatus(batch);
@@ -9277,7 +9339,7 @@ var OpenAIBatch = class {
9277
9339
  (fileId) => fileId != null
9278
9340
  );
9279
9341
  if (batchStatus.status === "completed" && fileIds.length === 0) {
9280
- throw new InvalidResponseDataError({
9342
+ throw new InvalidResponseDataError2({
9281
9343
  data: batch,
9282
9344
  message: `OpenAI batch "${options.batchId}" completed without batch output.`
9283
9345
  });
@@ -11208,7 +11270,7 @@ var OpenAISkills = class {
11208
11270
  };
11209
11271
 
11210
11272
  // src/version.ts
11211
- var VERSION = true ? "4.0.63" : "0.0.0-test";
11273
+ var VERSION = true ? "4.0.65" : "0.0.0-test";
11212
11274
 
11213
11275
  // src/openai-provider.ts
11214
11276
  function createOpenAI(options = {}) {