ai 5.0.0-beta.32 → 5.0.0-beta.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 CHANGED
@@ -98,45 +98,51 @@ var RetryError = class extends AISDKError3 {
98
98
  _a3 = symbol3;
99
99
 
100
100
  // src/util/retry-with-exponential-backoff.ts
101
- function getRetryDelay(error, exponentialBackoffDelay) {
101
+ function getRetryDelayInMs({
102
+ error,
103
+ exponentialBackoffDelay
104
+ }) {
102
105
  const headers = error.responseHeaders;
103
106
  if (!headers)
104
107
  return exponentialBackoffDelay;
105
- let timeoutMillis;
108
+ let ms;
106
109
  const retryAfterMs = headers["retry-after-ms"];
107
110
  if (retryAfterMs) {
108
111
  const timeoutMs = parseFloat(retryAfterMs);
109
112
  if (!Number.isNaN(timeoutMs)) {
110
- timeoutMillis = timeoutMs;
113
+ ms = timeoutMs;
111
114
  }
112
115
  }
113
116
  const retryAfter = headers["retry-after"];
114
- if (retryAfter && timeoutMillis === void 0) {
117
+ if (retryAfter && ms === void 0) {
115
118
  const timeoutSeconds = parseFloat(retryAfter);
116
119
  if (!Number.isNaN(timeoutSeconds)) {
117
- timeoutMillis = timeoutSeconds * 1e3;
120
+ ms = timeoutSeconds * 1e3;
118
121
  } else {
119
- timeoutMillis = Date.parse(retryAfter) - Date.now();
122
+ ms = Date.parse(retryAfter) - Date.now();
120
123
  }
121
124
  }
122
- if (timeoutMillis !== void 0 && 0 <= timeoutMillis && timeoutMillis < 60 * 1e3) {
123
- return timeoutMillis;
125
+ if (ms != null && !Number.isNaN(ms) && 0 <= ms && (ms < 60 * 1e3 || ms < exponentialBackoffDelay)) {
126
+ return ms;
124
127
  }
125
128
  return exponentialBackoffDelay;
126
129
  }
127
130
  var retryWithExponentialBackoffRespectingRetryHeaders = ({
128
131
  maxRetries = 2,
129
132
  initialDelayInMs = 2e3,
130
- backoffFactor = 2
133
+ backoffFactor = 2,
134
+ abortSignal
131
135
  } = {}) => async (f) => _retryWithExponentialBackoff(f, {
132
136
  maxRetries,
133
137
  delayInMs: initialDelayInMs,
134
- backoffFactor
138
+ backoffFactor,
139
+ abortSignal
135
140
  });
136
141
  async function _retryWithExponentialBackoff(f, {
137
142
  maxRetries,
138
143
  delayInMs,
139
- backoffFactor
144
+ backoffFactor,
145
+ abortSignal
140
146
  }, errors = []) {
141
147
  try {
142
148
  return await f();
@@ -158,11 +164,21 @@ async function _retryWithExponentialBackoff(f, {
158
164
  });
159
165
  }
160
166
  if (error instanceof Error && APICallError.isInstance(error) && error.isRetryable === true && tryNumber <= maxRetries) {
161
- const actualDelay = getRetryDelay(error, delayInMs);
162
- await delay(actualDelay);
167
+ await delay(
168
+ getRetryDelayInMs({
169
+ error,
170
+ exponentialBackoffDelay: delayInMs
171
+ }),
172
+ { abortSignal }
173
+ );
163
174
  return _retryWithExponentialBackoff(
164
175
  f,
165
- { maxRetries, delayInMs: backoffFactor * delayInMs, backoffFactor },
176
+ {
177
+ maxRetries,
178
+ delayInMs: backoffFactor * delayInMs,
179
+ backoffFactor,
180
+ abortSignal
181
+ },
166
182
  newErrors
167
183
  );
168
184
  }
@@ -179,7 +195,8 @@ async function _retryWithExponentialBackoff(f, {
179
195
 
180
196
  // src/util/prepare-retries.ts
181
197
  function prepareRetries({
182
- maxRetries
198
+ maxRetries,
199
+ abortSignal
183
200
  }) {
184
201
  if (maxRetries != null) {
185
202
  if (!Number.isInteger(maxRetries)) {
@@ -201,7 +218,8 @@ function prepareRetries({
201
218
  return {
202
219
  maxRetries: maxRetriesResult,
203
220
  retry: retryWithExponentialBackoffRespectingRetryHeaders({
204
- maxRetries: maxRetriesResult
221
+ maxRetries: maxRetriesResult,
222
+ abortSignal
205
223
  })
206
224
  };
207
225
  }
@@ -1823,12 +1841,15 @@ function createToolModelOutput({
1823
1841
  if (errorMode === "text") {
1824
1842
  return { type: "error-text", value: getErrorMessage4(output) };
1825
1843
  } else if (errorMode === "json") {
1826
- return { type: "error-json", value: output };
1844
+ return { type: "error-json", value: toJSONValue(output) };
1827
1845
  }
1828
1846
  if (tool3 == null ? void 0 : tool3.toModelOutput) {
1829
1847
  return tool3.toModelOutput(output);
1830
1848
  }
1831
- return typeof output === "string" ? { type: "text", value: output } : { type: "json", value: output };
1849
+ return typeof output === "string" ? { type: "text", value: output } : { type: "json", value: toJSONValue(output) };
1850
+ }
1851
+ function toJSONValue(value) {
1852
+ return value === void 0 ? null : value;
1832
1853
  }
1833
1854
 
1834
1855
  // src/generate-text/to-response-messages.ts
@@ -1954,7 +1975,10 @@ async function generateText({
1954
1975
  }) {
1955
1976
  const model = resolveLanguageModel(modelArg);
1956
1977
  const stopConditions = asArray(stopWhen);
1957
- const { maxRetries, retry } = prepareRetries({ maxRetries: maxRetriesArg });
1978
+ const { maxRetries, retry } = prepareRetries({
1979
+ maxRetries: maxRetriesArg,
1980
+ abortSignal
1981
+ });
1958
1982
  const callSettings = prepareCallSettings(settings);
1959
1983
  const baseTelemetryAttributes = getBaseTelemetryAttributes({
1960
1984
  model,
@@ -4445,7 +4469,8 @@ var DefaultStreamTextResult = class {
4445
4469
  }
4446
4470
  this.baseStream = stream.pipeThrough(createOutputTransformStream(output)).pipeThrough(eventProcessor);
4447
4471
  const { maxRetries, retry } = prepareRetries({
4448
- maxRetries: maxRetriesArg
4472
+ maxRetries: maxRetriesArg,
4473
+ abortSignal
4449
4474
  });
4450
4475
  const tracer = getTracer(telemetry);
4451
4476
  const callSettings = prepareCallSettings(settings);
@@ -5321,7 +5346,10 @@ async function embed({
5321
5346
  modelId: model.modelId
5322
5347
  });
5323
5348
  }
5324
- const { maxRetries, retry } = prepareRetries({ maxRetries: maxRetriesArg });
5349
+ const { maxRetries, retry } = prepareRetries({
5350
+ maxRetries: maxRetriesArg,
5351
+ abortSignal
5352
+ });
5325
5353
  const baseTelemetryAttributes = getBaseTelemetryAttributes({
5326
5354
  model,
5327
5355
  telemetry,
@@ -5341,7 +5369,7 @@ async function embed({
5341
5369
  }),
5342
5370
  tracer,
5343
5371
  fn: async (span) => {
5344
- const { embedding, usage, response } = await retry(
5372
+ const { embedding, usage, response, providerMetadata } = await retry(
5345
5373
  () => (
5346
5374
  // nested spans to align with the embedMany telemetry data:
5347
5375
  recordSpan({
@@ -5385,6 +5413,7 @@ async function embed({
5385
5413
  return {
5386
5414
  embedding: embedding2,
5387
5415
  usage: usage2,
5416
+ providerMetadata: modelResponse.providerMetadata,
5388
5417
  response: modelResponse.response
5389
5418
  };
5390
5419
  }
@@ -5404,6 +5433,7 @@ async function embed({
5404
5433
  value,
5405
5434
  embedding,
5406
5435
  usage,
5436
+ providerMetadata,
5407
5437
  response
5408
5438
  });
5409
5439
  }
@@ -5414,6 +5444,7 @@ var DefaultEmbedResult = class {
5414
5444
  this.value = options.value;
5415
5445
  this.embedding = options.embedding;
5416
5446
  this.usage = options.usage;
5447
+ this.providerMetadata = options.providerMetadata;
5417
5448
  this.response = options.response;
5418
5449
  }
5419
5450
  };
@@ -5448,7 +5479,10 @@ async function embedMany({
5448
5479
  modelId: model.modelId
5449
5480
  });
5450
5481
  }
5451
- const { maxRetries, retry } = prepareRetries({ maxRetries: maxRetriesArg });
5482
+ const { maxRetries, retry } = prepareRetries({
5483
+ maxRetries: maxRetriesArg,
5484
+ abortSignal
5485
+ });
5452
5486
  const baseTelemetryAttributes = getBaseTelemetryAttributes({
5453
5487
  model,
5454
5488
  telemetry,
@@ -5471,58 +5505,64 @@ async function embedMany({
5471
5505
  }),
5472
5506
  tracer,
5473
5507
  fn: async (span) => {
5508
+ var _a16;
5474
5509
  const [maxEmbeddingsPerCall, supportsParallelCalls] = await Promise.all([
5475
5510
  model.maxEmbeddingsPerCall,
5476
5511
  model.supportsParallelCalls
5477
5512
  ]);
5478
5513
  if (maxEmbeddingsPerCall == null || maxEmbeddingsPerCall === Infinity) {
5479
- const { embeddings: embeddings2, usage, response } = await retry(() => {
5480
- return recordSpan({
5481
- name: "ai.embedMany.doEmbed",
5482
- attributes: selectTelemetryAttributes({
5483
- telemetry,
5484
- attributes: {
5485
- ...assembleOperationName({
5486
- operationId: "ai.embedMany.doEmbed",
5487
- telemetry
5488
- }),
5489
- ...baseTelemetryAttributes,
5490
- // specific settings that only make sense on the outer level:
5491
- "ai.values": {
5492
- input: () => values.map((value) => JSON.stringify(value))
5514
+ const { embeddings: embeddings2, usage, response, providerMetadata: providerMetadata2 } = await retry(
5515
+ () => {
5516
+ return recordSpan({
5517
+ name: "ai.embedMany.doEmbed",
5518
+ attributes: selectTelemetryAttributes({
5519
+ telemetry,
5520
+ attributes: {
5521
+ ...assembleOperationName({
5522
+ operationId: "ai.embedMany.doEmbed",
5523
+ telemetry
5524
+ }),
5525
+ ...baseTelemetryAttributes,
5526
+ // specific settings that only make sense on the outer level:
5527
+ "ai.values": {
5528
+ input: () => values.map((value) => JSON.stringify(value))
5529
+ }
5493
5530
  }
5531
+ }),
5532
+ tracer,
5533
+ fn: async (doEmbedSpan) => {
5534
+ var _a17;
5535
+ const modelResponse = await model.doEmbed({
5536
+ values,
5537
+ abortSignal,
5538
+ headers,
5539
+ providerOptions
5540
+ });
5541
+ const embeddings3 = modelResponse.embeddings;
5542
+ const usage2 = (_a17 = modelResponse.usage) != null ? _a17 : { tokens: NaN };
5543
+ doEmbedSpan.setAttributes(
5544
+ selectTelemetryAttributes({
5545
+ telemetry,
5546
+ attributes: {
5547
+ "ai.embeddings": {
5548
+ output: () => embeddings3.map(
5549
+ (embedding) => JSON.stringify(embedding)
5550
+ )
5551
+ },
5552
+ "ai.usage.tokens": usage2.tokens
5553
+ }
5554
+ })
5555
+ );
5556
+ return {
5557
+ embeddings: embeddings3,
5558
+ usage: usage2,
5559
+ providerMetadata: modelResponse.providerMetadata,
5560
+ response: modelResponse.response
5561
+ };
5494
5562
  }
5495
- }),
5496
- tracer,
5497
- fn: async (doEmbedSpan) => {
5498
- var _a16;
5499
- const modelResponse = await model.doEmbed({
5500
- values,
5501
- abortSignal,
5502
- headers,
5503
- providerOptions
5504
- });
5505
- const embeddings3 = modelResponse.embeddings;
5506
- const usage2 = (_a16 = modelResponse.usage) != null ? _a16 : { tokens: NaN };
5507
- doEmbedSpan.setAttributes(
5508
- selectTelemetryAttributes({
5509
- telemetry,
5510
- attributes: {
5511
- "ai.embeddings": {
5512
- output: () => embeddings3.map((embedding) => JSON.stringify(embedding))
5513
- },
5514
- "ai.usage.tokens": usage2.tokens
5515
- }
5516
- })
5517
- );
5518
- return {
5519
- embeddings: embeddings3,
5520
- usage: usage2,
5521
- response: modelResponse.response
5522
- };
5523
- }
5524
- });
5525
- });
5563
+ });
5564
+ }
5565
+ );
5526
5566
  span.setAttributes(
5527
5567
  selectTelemetryAttributes({
5528
5568
  telemetry,
@@ -5538,6 +5578,7 @@ async function embedMany({
5538
5578
  values,
5539
5579
  embeddings: embeddings2,
5540
5580
  usage,
5581
+ providerMetadata: providerMetadata2,
5541
5582
  responses: [response]
5542
5583
  });
5543
5584
  }
@@ -5545,6 +5586,7 @@ async function embedMany({
5545
5586
  const embeddings = [];
5546
5587
  const responses = [];
5547
5588
  let tokens = 0;
5589
+ let providerMetadata;
5548
5590
  const parallelChunks = splitArray(
5549
5591
  valueChunks,
5550
5592
  supportsParallelCalls ? maxParallelCalls : 1
@@ -5571,7 +5613,7 @@ async function embedMany({
5571
5613
  }),
5572
5614
  tracer,
5573
5615
  fn: async (doEmbedSpan) => {
5574
- var _a16;
5616
+ var _a17;
5575
5617
  const modelResponse = await model.doEmbed({
5576
5618
  values: chunk,
5577
5619
  abortSignal,
@@ -5579,7 +5621,7 @@ async function embedMany({
5579
5621
  providerOptions
5580
5622
  });
5581
5623
  const embeddings2 = modelResponse.embeddings;
5582
- const usage = (_a16 = modelResponse.usage) != null ? _a16 : { tokens: NaN };
5624
+ const usage = (_a17 = modelResponse.usage) != null ? _a17 : { tokens: NaN };
5583
5625
  doEmbedSpan.setAttributes(
5584
5626
  selectTelemetryAttributes({
5585
5627
  telemetry,
@@ -5596,6 +5638,7 @@ async function embedMany({
5596
5638
  return {
5597
5639
  embeddings: embeddings2,
5598
5640
  usage,
5641
+ providerMetadata: modelResponse.providerMetadata,
5599
5642
  response: modelResponse.response
5600
5643
  };
5601
5644
  }
@@ -5607,6 +5650,20 @@ async function embedMany({
5607
5650
  embeddings.push(...result.embeddings);
5608
5651
  responses.push(result.response);
5609
5652
  tokens += result.usage.tokens;
5653
+ if (result.providerMetadata) {
5654
+ if (!providerMetadata) {
5655
+ providerMetadata = { ...result.providerMetadata };
5656
+ } else {
5657
+ for (const [providerName, metadata] of Object.entries(
5658
+ result.providerMetadata
5659
+ )) {
5660
+ providerMetadata[providerName] = {
5661
+ ...(_a16 = providerMetadata[providerName]) != null ? _a16 : {},
5662
+ ...metadata
5663
+ };
5664
+ }
5665
+ }
5666
+ }
5610
5667
  }
5611
5668
  }
5612
5669
  span.setAttributes(
@@ -5624,6 +5681,7 @@ async function embedMany({
5624
5681
  values,
5625
5682
  embeddings,
5626
5683
  usage: { tokens },
5684
+ providerMetadata,
5627
5685
  responses
5628
5686
  });
5629
5687
  }
@@ -5634,6 +5692,7 @@ var DefaultEmbedManyResult = class {
5634
5692
  this.values = options.values;
5635
5693
  this.embeddings = options.embeddings;
5636
5694
  this.usage = options.usage;
5695
+ this.providerMetadata = options.providerMetadata;
5637
5696
  this.responses = options.responses;
5638
5697
  }
5639
5698
  };
@@ -5660,7 +5719,10 @@ async function generateImage({
5660
5719
  modelId: model.modelId
5661
5720
  });
5662
5721
  }
5663
- const { retry } = prepareRetries({ maxRetries: maxRetriesArg });
5722
+ const { retry } = prepareRetries({
5723
+ maxRetries: maxRetriesArg,
5724
+ abortSignal
5725
+ });
5664
5726
  const maxImagesPerCallWithDefault = (_a16 = maxImagesPerCall != null ? maxImagesPerCall : await invokeModelMaxImagesPerCall(model)) != null ? _a16 : 1;
5665
5727
  const callCount = Math.ceil(n / maxImagesPerCallWithDefault);
5666
5728
  const callImageCounts = Array.from({ length: callCount }, (_, i) => {
@@ -5749,12 +5811,7 @@ async function invokeModelMaxImagesPerCall(model) {
5749
5811
 
5750
5812
  // src/generate-object/generate-object.ts
5751
5813
  import {
5752
- JSONParseError as JSONParseError2,
5753
- TypeValidationError as TypeValidationError3
5754
- } from "@ai-sdk/provider";
5755
- import {
5756
- createIdGenerator as createIdGenerator3,
5757
- safeParseJSON as safeParseJSON3
5814
+ createIdGenerator as createIdGenerator3
5758
5815
  } from "@ai-sdk/provider-utils";
5759
5816
 
5760
5817
  // src/generate-object/output-strategy.ts
@@ -6141,6 +6198,63 @@ function validateObjectGenerationInput({
6141
6198
  }
6142
6199
  }
6143
6200
 
6201
+ // src/generate-object/parse-and-validate-object-result.ts
6202
+ import { JSONParseError as JSONParseError2, TypeValidationError as TypeValidationError3 } from "@ai-sdk/provider";
6203
+ import { safeParseJSON as safeParseJSON3 } from "@ai-sdk/provider-utils";
6204
+ async function parseAndValidateObjectResult(result, outputStrategy, context) {
6205
+ const parseResult = await safeParseJSON3({ text: result });
6206
+ if (!parseResult.success) {
6207
+ throw new NoObjectGeneratedError({
6208
+ message: "No object generated: could not parse the response.",
6209
+ cause: parseResult.error,
6210
+ text: result,
6211
+ response: context.response,
6212
+ usage: context.usage,
6213
+ finishReason: context.finishReason
6214
+ });
6215
+ }
6216
+ const validationResult = await outputStrategy.validateFinalResult(
6217
+ parseResult.value,
6218
+ {
6219
+ text: result,
6220
+ response: context.response,
6221
+ usage: context.usage
6222
+ }
6223
+ );
6224
+ if (!validationResult.success) {
6225
+ throw new NoObjectGeneratedError({
6226
+ message: "No object generated: response did not match schema.",
6227
+ cause: validationResult.error,
6228
+ text: result,
6229
+ response: context.response,
6230
+ usage: context.usage,
6231
+ finishReason: context.finishReason
6232
+ });
6233
+ }
6234
+ return validationResult.value;
6235
+ }
6236
+ async function parseAndValidateObjectResultWithRepair(result, outputStrategy, repairText, context) {
6237
+ try {
6238
+ return await parseAndValidateObjectResult(result, outputStrategy, context);
6239
+ } catch (error) {
6240
+ if (repairText != null && NoObjectGeneratedError.isInstance(error) && (JSONParseError2.isInstance(error.cause) || TypeValidationError3.isInstance(error.cause))) {
6241
+ const repairedText = await repairText({
6242
+ text: result,
6243
+ error: error.cause
6244
+ });
6245
+ if (repairedText === null) {
6246
+ throw error;
6247
+ }
6248
+ return await parseAndValidateObjectResult(
6249
+ repairedText,
6250
+ outputStrategy,
6251
+ context
6252
+ );
6253
+ }
6254
+ throw error;
6255
+ }
6256
+ }
6257
+
6144
6258
  // src/generate-object/generate-object.ts
6145
6259
  var originalGenerateId3 = createIdGenerator3({ prefix: "aiobj", size: 24 });
6146
6260
  async function generateObject(options) {
@@ -6176,7 +6290,10 @@ async function generateObject(options) {
6176
6290
  schemaDescription,
6177
6291
  enumValues
6178
6292
  });
6179
- const { maxRetries, retry } = prepareRetries({ maxRetries: maxRetriesArg });
6293
+ const { maxRetries, retry } = prepareRetries({
6294
+ maxRetries: maxRetriesArg,
6295
+ abortSignal
6296
+ });
6180
6297
  const outputStrategy = getOutputStrategy({
6181
6298
  output,
6182
6299
  schema: inputSchema,
@@ -6322,55 +6439,16 @@ async function generateObject(options) {
6322
6439
  resultProviderMetadata = generateResult.providerMetadata;
6323
6440
  request = (_a16 = generateResult.request) != null ? _a16 : {};
6324
6441
  response = generateResult.responseData;
6325
- async function processResult(result2) {
6326
- const parseResult = await safeParseJSON3({ text: result2 });
6327
- if (!parseResult.success) {
6328
- throw new NoObjectGeneratedError({
6329
- message: "No object generated: could not parse the response.",
6330
- cause: parseResult.error,
6331
- text: result2,
6332
- response,
6333
- usage,
6334
- finishReason
6335
- });
6336
- }
6337
- const validationResult = await outputStrategy.validateFinalResult(
6338
- parseResult.value,
6339
- {
6340
- text: result2,
6341
- response,
6342
- usage
6343
- }
6344
- );
6345
- if (!validationResult.success) {
6346
- throw new NoObjectGeneratedError({
6347
- message: "No object generated: response did not match schema.",
6348
- cause: validationResult.error,
6349
- text: result2,
6350
- response,
6351
- usage,
6352
- finishReason
6353
- });
6354
- }
6355
- return validationResult.value;
6356
- }
6357
- let object2;
6358
- try {
6359
- object2 = await processResult(result);
6360
- } catch (error) {
6361
- if (repairText != null && NoObjectGeneratedError.isInstance(error) && (JSONParseError2.isInstance(error.cause) || TypeValidationError3.isInstance(error.cause))) {
6362
- const repairedText = await repairText({
6363
- text: result,
6364
- error: error.cause
6365
- });
6366
- if (repairedText === null) {
6367
- throw error;
6368
- }
6369
- object2 = await processResult(repairedText);
6370
- } else {
6371
- throw error;
6442
+ const object2 = await parseAndValidateObjectResultWithRepair(
6443
+ result,
6444
+ outputStrategy,
6445
+ repairText,
6446
+ {
6447
+ response,
6448
+ usage,
6449
+ finishReason
6372
6450
  }
6373
- }
6451
+ );
6374
6452
  span.setAttributes(
6375
6453
  selectTelemetryAttributes({
6376
6454
  telemetry,
@@ -6571,6 +6649,7 @@ function streamObject(options) {
6571
6649
  maxRetries,
6572
6650
  abortSignal,
6573
6651
  headers,
6652
+ experimental_repairText: repairText,
6574
6653
  experimental_telemetry: telemetry,
6575
6654
  providerOptions,
6576
6655
  onError = ({ error }) => {
@@ -6616,6 +6695,7 @@ function streamObject(options) {
6616
6695
  schemaName,
6617
6696
  schemaDescription,
6618
6697
  providerOptions,
6698
+ repairText,
6619
6699
  onError,
6620
6700
  onFinish,
6621
6701
  generateId: generateId3,
@@ -6638,6 +6718,7 @@ var DefaultStreamObjectResult = class {
6638
6718
  schemaName,
6639
6719
  schemaDescription,
6640
6720
  providerOptions,
6721
+ repairText,
6641
6722
  onError,
6642
6723
  onFinish,
6643
6724
  generateId: generateId3,
@@ -6653,7 +6734,8 @@ var DefaultStreamObjectResult = class {
6653
6734
  this._finishReason = new DelayedPromise();
6654
6735
  const model = resolveLanguageModel(modelArg);
6655
6736
  const { maxRetries, retry } = prepareRetries({
6656
- maxRetries: maxRetriesArg
6737
+ maxRetries: maxRetriesArg,
6738
+ abortSignal
6657
6739
  });
6658
6740
  const callSettings = prepareCallSettings(settings);
6659
6741
  const baseTelemetryAttributes = getBaseTelemetryAttributes({
@@ -6871,27 +6953,21 @@ var DefaultStreamObjectResult = class {
6871
6953
  headers: response == null ? void 0 : response.headers
6872
6954
  });
6873
6955
  self._finishReason.resolve(finishReason != null ? finishReason : "unknown");
6874
- const validationResult = await outputStrategy.validateFinalResult(
6875
- latestObjectJson,
6876
- {
6877
- text: accumulatedText,
6878
- response: fullResponse,
6879
- usage
6880
- }
6881
- );
6882
- if (validationResult.success) {
6883
- object2 = validationResult.value;
6956
+ try {
6957
+ object2 = await parseAndValidateObjectResultWithRepair(
6958
+ accumulatedText,
6959
+ outputStrategy,
6960
+ repairText,
6961
+ {
6962
+ response: fullResponse,
6963
+ usage,
6964
+ finishReason
6965
+ }
6966
+ );
6884
6967
  self._object.resolve(object2);
6885
- } else {
6886
- error = new NoObjectGeneratedError({
6887
- message: "No object generated: response did not match schema.",
6888
- cause: validationResult.error,
6889
- text: accumulatedText,
6890
- response: fullResponse,
6891
- usage,
6892
- finishReason
6893
- });
6894
- self._object.reject(error);
6968
+ } catch (e) {
6969
+ error = e;
6970
+ self._object.reject(e);
6895
6971
  }
6896
6972
  break;
6897
6973
  }
@@ -7134,7 +7210,10 @@ async function generateSpeech({
7134
7210
  modelId: model.modelId
7135
7211
  });
7136
7212
  }
7137
- const { retry } = prepareRetries({ maxRetries: maxRetriesArg });
7213
+ const { retry } = prepareRetries({
7214
+ maxRetries: maxRetriesArg,
7215
+ abortSignal
7216
+ });
7138
7217
  const result = await retry(
7139
7218
  () => model.doGenerate({
7140
7219
  text: text2,
@@ -7182,7 +7261,7 @@ __export(output_exports, {
7182
7261
  });
7183
7262
  import {
7184
7263
  asSchema as asSchema4,
7185
- safeParseJSON as safeParseJSON4,
7264
+ safeParseJSON as safeParseJSON5,
7186
7265
  safeValidateTypes as safeValidateTypes4
7187
7266
  } from "@ai-sdk/provider-utils";
7188
7267
  var text = () => ({
@@ -7224,7 +7303,7 @@ var object = ({
7224
7303
  }
7225
7304
  },
7226
7305
  async parseOutput({ text: text2 }, context) {
7227
- const parseResult = await safeParseJSON4({ text: text2 });
7306
+ const parseResult = await safeParseJSON5({ text: text2 });
7228
7307
  if (!parseResult.success) {
7229
7308
  throw new NoObjectGeneratedError({
7230
7309
  message: "No object generated: could not parse the response.",
@@ -8446,7 +8525,10 @@ async function transcribe({
8446
8525
  modelId: model.modelId
8447
8526
  });
8448
8527
  }
8449
- const { retry } = prepareRetries({ maxRetries: maxRetriesArg });
8528
+ const { retry } = prepareRetries({
8529
+ maxRetries: maxRetriesArg,
8530
+ abortSignal
8531
+ });
8450
8532
  const audioData = audio instanceof URL ? (await download({ url: audio })).data : convertDataContentToUint8Array(audio);
8451
8533
  const result = await retry(
8452
8534
  () => {
@@ -8894,6 +8976,15 @@ var AbstractChat = class {
8894
8976
  this.resumeStream = async (options = {}) => {
8895
8977
  await this.makeRequest({ trigger: "resume-stream", ...options });
8896
8978
  };
8979
+ /**
8980
+ * Clear the error state and set the status to ready if the chat is in an error state.
8981
+ */
8982
+ this.clearError = () => {
8983
+ if (this.status === "error") {
8984
+ this.state.error = void 0;
8985
+ this.setStatus({ status: "ready" });
8986
+ }
8987
+ };
8897
8988
  this.addToolResult = async ({
8898
8989
  tool: tool3,
8899
8990
  toolCallId,