ai 5.0.0-alpha.1 → 5.0.0-alpha.3

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.js CHANGED
@@ -47,6 +47,7 @@ __export(src_exports, {
47
47
  NoSuchToolError: () => NoSuchToolError,
48
48
  Output: () => output_exports,
49
49
  RetryError: () => RetryError,
50
+ TextStreamChatTransport: () => TextStreamChatTransport,
50
51
  ToolCallRepairError: () => ToolCallRepairError,
51
52
  ToolExecutionError: () => ToolExecutionError,
52
53
  TypeValidationError: () => import_provider16.TypeValidationError,
@@ -591,9 +592,8 @@ var uiMessageStreamPartSchema = import_zod.z.union([
591
592
  providerMetadata: import_zod.z.record(import_zod.z.any()).optional()
592
593
  }),
593
594
  import_zod.z.object({
594
- type: import_zod.z.literal("source"),
595
- sourceType: import_zod.z.literal("url"),
596
- id: import_zod.z.string(),
595
+ type: import_zod.z.literal("source-url"),
596
+ sourceId: import_zod.z.string(),
597
597
  url: import_zod.z.string(),
598
598
  title: import_zod.z.string().optional(),
599
599
  providerMetadata: import_zod.z.any().optional()
@@ -1147,16 +1147,13 @@ function processUIMessageStream({
1147
1147
  write();
1148
1148
  break;
1149
1149
  }
1150
- case "source": {
1150
+ case "source-url": {
1151
1151
  state.message.parts.push({
1152
- type: "source",
1153
- source: {
1154
- sourceType: "url",
1155
- id: part.id,
1156
- url: part.url,
1157
- title: part.title,
1158
- providerMetadata: part.providerMetadata
1159
- }
1152
+ type: "source-url",
1153
+ sourceId: part.sourceId,
1154
+ url: part.url,
1155
+ title: part.title,
1156
+ providerMetadata: part.providerMetadata
1160
1157
  });
1161
1158
  write();
1162
1159
  break;
@@ -1292,14 +1289,7 @@ function processUIMessageStream({
1292
1289
  (partArg) => part.type === partArg.type && part.id === partArg.id
1293
1290
  ) : void 0;
1294
1291
  if (existingPart != null) {
1295
- if (isObject(existingPart.data) && isObject(part.data)) {
1296
- existingPart.value = mergeObjects(
1297
- existingPart.data,
1298
- part.data
1299
- );
1300
- } else {
1301
- existingPart.data = part.data;
1302
- }
1292
+ existingPart.data = isObject(existingPart.data) && isObject(part.data) ? mergeObjects(existingPart.data, part.data) : part.data;
1303
1293
  } else {
1304
1294
  state.message.parts.push(part);
1305
1295
  }
@@ -1346,7 +1336,6 @@ var getOriginalFetch = () => fetch;
1346
1336
  async function fetchUIMessageStream({
1347
1337
  api,
1348
1338
  body,
1349
- streamProtocol = "ui-message",
1350
1339
  credentials,
1351
1340
  headers,
1352
1341
  abortController,
@@ -1380,9 +1369,7 @@ async function fetchUIMessageStream({
1380
1369
  if (!response.body) {
1381
1370
  throw new Error("The response body is empty.");
1382
1371
  }
1383
- return streamProtocol === "text" ? transformTextToUiMessageStream({
1384
- stream: response.body.pipeThrough(new TextDecoderStream())
1385
- }) : (0, import_provider_utils3.parseJsonEventStream)({
1372
+ return (0, import_provider_utils3.parseJsonEventStream)({
1386
1373
  stream: response.body,
1387
1374
  schema: uiMessageStreamPartSchema
1388
1375
  }).pipeThrough(
@@ -1396,6 +1383,46 @@ async function fetchUIMessageStream({
1396
1383
  })
1397
1384
  );
1398
1385
  }
1386
+ async function fetchTextStream({
1387
+ api,
1388
+ body,
1389
+ credentials,
1390
+ headers,
1391
+ abortController,
1392
+ fetch: fetch2 = getOriginalFetch(),
1393
+ requestType = "generate"
1394
+ }) {
1395
+ var _a17, _b, _c;
1396
+ const response = requestType === "resume" ? await fetch2(`${api}?chatId=${body.chatId}`, {
1397
+ method: "GET",
1398
+ headers: {
1399
+ "Content-Type": "application/json",
1400
+ ...headers
1401
+ },
1402
+ signal: (_a17 = abortController == null ? void 0 : abortController()) == null ? void 0 : _a17.signal,
1403
+ credentials
1404
+ }) : await fetch2(api, {
1405
+ method: "POST",
1406
+ body: JSON.stringify(body),
1407
+ headers: {
1408
+ "Content-Type": "application/json",
1409
+ ...headers
1410
+ },
1411
+ signal: (_b = abortController == null ? void 0 : abortController()) == null ? void 0 : _b.signal,
1412
+ credentials
1413
+ });
1414
+ if (!response.ok) {
1415
+ throw new Error(
1416
+ (_c = await response.text()) != null ? _c : "Failed to fetch the chat response."
1417
+ );
1418
+ }
1419
+ if (!response.body) {
1420
+ throw new Error("The response body is empty.");
1421
+ }
1422
+ return transformTextToUiMessageStream({
1423
+ stream: response.body.pipeThrough(new TextDecoderStream())
1424
+ });
1425
+ }
1399
1426
  async function consumeUIMessageStream({
1400
1427
  stream,
1401
1428
  onUpdate,
@@ -1446,10 +1473,17 @@ async function callChatApi({
1446
1473
  requestType = "generate",
1447
1474
  messageMetadataSchema
1448
1475
  }) {
1449
- const stream = await fetchUIMessageStream({
1476
+ const stream = streamProtocol === "text" ? await fetchTextStream({
1477
+ api,
1478
+ body,
1479
+ credentials,
1480
+ headers,
1481
+ abortController,
1482
+ fetch: fetch2,
1483
+ requestType
1484
+ }) : await fetchUIMessageStream({
1450
1485
  api,
1451
1486
  body,
1452
- streamProtocol,
1453
1487
  credentials,
1454
1488
  headers,
1455
1489
  abortController,
@@ -2001,7 +2035,6 @@ var DefaultChatTransport = class {
2001
2035
  credentials,
2002
2036
  headers,
2003
2037
  body,
2004
- streamProtocol,
2005
2038
  fetch: fetch2,
2006
2039
  prepareRequestBody
2007
2040
  }) {
@@ -2009,7 +2042,6 @@ var DefaultChatTransport = class {
2009
2042
  this.credentials = credentials;
2010
2043
  this.headers = headers;
2011
2044
  this.body = body;
2012
- this.streamProtocol = streamProtocol;
2013
2045
  this.fetch = fetch2;
2014
2046
  this.prepareRequestBody = prepareRequestBody;
2015
2047
  }
@@ -2039,7 +2071,55 @@ var DefaultChatTransport = class {
2039
2071
  ...this.body,
2040
2072
  ...body
2041
2073
  },
2042
- streamProtocol: this.streamProtocol,
2074
+ credentials: this.credentials,
2075
+ abortController: () => abortController,
2076
+ fetch: this.fetch,
2077
+ requestType
2078
+ });
2079
+ }
2080
+ };
2081
+ var TextStreamChatTransport = class {
2082
+ constructor({
2083
+ api,
2084
+ credentials,
2085
+ headers,
2086
+ body,
2087
+ fetch: fetch2,
2088
+ prepareRequestBody
2089
+ }) {
2090
+ this.api = api;
2091
+ this.credentials = credentials;
2092
+ this.headers = headers;
2093
+ this.body = body;
2094
+ this.fetch = fetch2;
2095
+ this.prepareRequestBody = prepareRequestBody;
2096
+ }
2097
+ submitMessages({
2098
+ chatId,
2099
+ messages,
2100
+ abortController,
2101
+ body,
2102
+ headers,
2103
+ requestType
2104
+ }) {
2105
+ var _a17, _b;
2106
+ return fetchTextStream({
2107
+ api: this.api,
2108
+ headers: {
2109
+ ...this.headers,
2110
+ ...headers
2111
+ },
2112
+ body: (_b = (_a17 = this.prepareRequestBody) == null ? void 0 : _a17.call(this, {
2113
+ chatId,
2114
+ messages,
2115
+ ...this.body,
2116
+ ...body
2117
+ })) != null ? _b : {
2118
+ chatId,
2119
+ messages,
2120
+ ...this.body,
2121
+ ...body
2122
+ },
2043
2123
  credentials: this.credentials,
2044
2124
  abortController: () => abortController,
2045
2125
  fetch: this.fetch,
@@ -2239,7 +2319,6 @@ var import_provider_utils6 = require("@ai-sdk/provider-utils");
2239
2319
  function defaultChatStore({
2240
2320
  api,
2241
2321
  fetch: fetch2,
2242
- streamProtocol = "ui-message",
2243
2322
  credentials,
2244
2323
  headers,
2245
2324
  body,
@@ -2254,7 +2333,6 @@ function defaultChatStore({
2254
2333
  transport: new DefaultChatTransport({
2255
2334
  api,
2256
2335
  fetch: fetch2,
2257
- streamProtocol,
2258
2336
  credentials,
2259
2337
  headers,
2260
2338
  body,
@@ -4752,11 +4830,11 @@ var DelayedPromise = class {
4752
4830
  this._resolve = void 0;
4753
4831
  this._reject = void 0;
4754
4832
  }
4755
- get value() {
4756
- if (this.promise) {
4757
- return this.promise;
4833
+ get promise() {
4834
+ if (this._promise) {
4835
+ return this._promise;
4758
4836
  }
4759
- this.promise = new Promise((resolve, reject) => {
4837
+ this._promise = new Promise((resolve, reject) => {
4760
4838
  if (this.status.type === "resolved") {
4761
4839
  resolve(this.status.value);
4762
4840
  } else if (this.status.type === "rejected") {
@@ -4765,19 +4843,19 @@ var DelayedPromise = class {
4765
4843
  this._resolve = resolve;
4766
4844
  this._reject = reject;
4767
4845
  });
4768
- return this.promise;
4846
+ return this._promise;
4769
4847
  }
4770
4848
  resolve(value) {
4771
4849
  var _a17;
4772
4850
  this.status = { type: "resolved", value };
4773
- if (this.promise) {
4851
+ if (this._promise) {
4774
4852
  (_a17 = this._resolve) == null ? void 0 : _a17.call(this, value);
4775
4853
  }
4776
4854
  }
4777
4855
  reject(error) {
4778
4856
  var _a17;
4779
4857
  this.status = { type: "rejected", error };
4780
- if (this.promise) {
4858
+ if (this._promise) {
4781
4859
  (_a17 = this._reject) == null ? void 0 : _a17.call(this, error);
4782
4860
  }
4783
4861
  }
@@ -4872,12 +4950,12 @@ var DefaultStreamObjectResult = class {
4872
4950
  currentDate,
4873
4951
  now: now2
4874
4952
  }) {
4875
- this.objectPromise = new DelayedPromise();
4876
- this.usagePromise = new DelayedPromise();
4877
- this.providerMetadataPromise = new DelayedPromise();
4878
- this.warningsPromise = new DelayedPromise();
4879
- this.requestPromise = new DelayedPromise();
4880
- this.responsePromise = new DelayedPromise();
4953
+ this._object = new DelayedPromise();
4954
+ this._usage = new DelayedPromise();
4955
+ this._providerMetadata = new DelayedPromise();
4956
+ this._warnings = new DelayedPromise();
4957
+ this._request = new DelayedPromise();
4958
+ this._response = new DelayedPromise();
4881
4959
  const { maxRetries, retry } = prepareRetries({
4882
4960
  maxRetries: maxRetriesArg
4883
4961
  });
@@ -4996,7 +5074,7 @@ var DefaultStreamObjectResult = class {
4996
5074
  })
4997
5075
  })
4998
5076
  );
4999
- self.requestPromise.resolve(request != null ? request : {});
5077
+ self._request.resolve(request != null ? request : {});
5000
5078
  let warnings;
5001
5079
  let usage = {
5002
5080
  inputTokens: void 0,
@@ -5089,9 +5167,9 @@ var DefaultStreamObjectResult = class {
5089
5167
  usage,
5090
5168
  response: fullResponse
5091
5169
  });
5092
- self.usagePromise.resolve(usage);
5093
- self.providerMetadataPromise.resolve(providerMetadata);
5094
- self.responsePromise.resolve({
5170
+ self._usage.resolve(usage);
5171
+ self._providerMetadata.resolve(providerMetadata);
5172
+ self._response.resolve({
5095
5173
  ...fullResponse,
5096
5174
  headers: response == null ? void 0 : response.headers
5097
5175
  });
@@ -5105,7 +5183,7 @@ var DefaultStreamObjectResult = class {
5105
5183
  );
5106
5184
  if (validationResult.success) {
5107
5185
  object2 = validationResult.value;
5108
- self.objectPromise.resolve(object2);
5186
+ self._object.resolve(object2);
5109
5187
  } else {
5110
5188
  error = new NoObjectGeneratedError({
5111
5189
  message: "No object generated: response did not match schema.",
@@ -5115,7 +5193,7 @@ var DefaultStreamObjectResult = class {
5115
5193
  usage,
5116
5194
  finishReason
5117
5195
  });
5118
- self.objectPromise.reject(error);
5196
+ self._object.reject(error);
5119
5197
  }
5120
5198
  break;
5121
5199
  }
@@ -5210,22 +5288,22 @@ var DefaultStreamObjectResult = class {
5210
5288
  this.outputStrategy = outputStrategy;
5211
5289
  }
5212
5290
  get object() {
5213
- return this.objectPromise.value;
5291
+ return this._object.promise;
5214
5292
  }
5215
5293
  get usage() {
5216
- return this.usagePromise.value;
5294
+ return this._usage.promise;
5217
5295
  }
5218
5296
  get providerMetadata() {
5219
- return this.providerMetadataPromise.value;
5297
+ return this._providerMetadata.promise;
5220
5298
  }
5221
5299
  get warnings() {
5222
- return this.warningsPromise.value;
5300
+ return this._warnings.promise;
5223
5301
  }
5224
5302
  get request() {
5225
- return this.requestPromise.value;
5303
+ return this._request.promise;
5226
5304
  }
5227
5305
  get response() {
5228
- return this.responsePromise.value;
5306
+ return this._response.promise;
5229
5307
  }
5230
5308
  get partialObjectStream() {
5231
5309
  return createAsyncIterableStream(
@@ -6473,7 +6551,7 @@ function streamText({
6473
6551
  maxRetries,
6474
6552
  abortSignal,
6475
6553
  headers,
6476
- maxSteps: maxSteps2 = 1,
6554
+ continueUntil = maxSteps(1),
6477
6555
  experimental_output: output,
6478
6556
  experimental_telemetry: telemetry,
6479
6557
  providerOptions,
@@ -6509,7 +6587,7 @@ function streamText({
6509
6587
  transforms: asArray(transform),
6510
6588
  activeTools,
6511
6589
  repairToolCall,
6512
- maxSteps: maxSteps2,
6590
+ continueUntil,
6513
6591
  output,
6514
6592
  providerOptions,
6515
6593
  onChunk,
@@ -6586,7 +6664,7 @@ var DefaultStreamTextResult = class {
6586
6664
  transforms,
6587
6665
  activeTools,
6588
6666
  repairToolCall,
6589
- maxSteps: maxSteps2,
6667
+ continueUntil,
6590
6668
  output,
6591
6669
  providerOptions,
6592
6670
  now: now2,
@@ -6597,18 +6675,12 @@ var DefaultStreamTextResult = class {
6597
6675
  onFinish,
6598
6676
  onStepFinish
6599
6677
  }) {
6600
- this.totalUsagePromise = new DelayedPromise();
6601
- this.finishReasonPromise = new DelayedPromise();
6602
- this.stepsPromise = new DelayedPromise();
6603
- if (maxSteps2 < 1) {
6604
- throw new InvalidArgumentError({
6605
- parameter: "maxSteps",
6606
- value: maxSteps2,
6607
- message: "maxSteps must be at least 1"
6608
- });
6609
- }
6678
+ this._totalUsage = new DelayedPromise();
6679
+ this._finishReason = new DelayedPromise();
6680
+ this._steps = new DelayedPromise();
6610
6681
  this.output = output;
6611
6682
  this.generateId = generateId3;
6683
+ let stepFinish;
6612
6684
  let activeReasoningPart = void 0;
6613
6685
  let recordedContent = [];
6614
6686
  const recordedResponseMessages = [];
@@ -6690,6 +6762,7 @@ var DefaultStreamTextResult = class {
6690
6762
  recordedContent = [];
6691
6763
  activeReasoningPart = void 0;
6692
6764
  recordedResponseMessages.push(...stepMessages);
6765
+ stepFinish.resolve();
6693
6766
  }
6694
6767
  if (part.type === "finish") {
6695
6768
  recordedTotalUsage = part.totalUsage;
@@ -6707,9 +6780,9 @@ var DefaultStreamTextResult = class {
6707
6780
  outputTokens: void 0,
6708
6781
  totalTokens: void 0
6709
6782
  };
6710
- self.finishReasonPromise.resolve(finishReason);
6711
- self.totalUsagePromise.resolve(totalUsage);
6712
- self.stepsPromise.resolve(recordedSteps);
6783
+ self._finishReason.resolve(finishReason);
6784
+ self._totalUsage.resolve(totalUsage);
6785
+ self._steps.resolve(recordedSteps);
6713
6786
  const finalStep = recordedSteps[recordedSteps.length - 1];
6714
6787
  await (onFinish == null ? void 0 : onFinish({
6715
6788
  finishReason,
@@ -6800,8 +6873,7 @@ var DefaultStreamTextResult = class {
6800
6873
  // specific settings that only make sense on the outer level:
6801
6874
  "ai.prompt": {
6802
6875
  input: () => JSON.stringify({ system, prompt, messages })
6803
- },
6804
- "ai.settings.maxSteps": maxSteps2
6876
+ }
6805
6877
  }
6806
6878
  }),
6807
6879
  tracer,
@@ -6813,6 +6885,7 @@ var DefaultStreamTextResult = class {
6813
6885
  responseMessages,
6814
6886
  usage
6815
6887
  }) {
6888
+ stepFinish = new DelayedPromise();
6816
6889
  const initialPrompt = await standardizePrompt({
6817
6890
  system,
6818
6891
  prompt,
@@ -6894,7 +6967,7 @@ var DefaultStreamTextResult = class {
6894
6967
  }
6895
6968
  })
6896
6969
  );
6897
- const transformedStream = runToolsTransformation({
6970
+ const streamWithToolResults = runToolsTransformation({
6898
6971
  tools,
6899
6972
  generatorStream: stream2,
6900
6973
  toolCallStreaming,
@@ -6933,7 +7006,7 @@ var DefaultStreamTextResult = class {
6933
7006
  stepText += chunk.text;
6934
7007
  }
6935
7008
  self.addStream(
6936
- transformedStream.pipeThrough(
7009
+ streamWithToolResults.pipeThrough(
6937
7010
  new TransformStream({
6938
7011
  async transform(chunk, controller) {
6939
7012
  var _a17, _b, _c, _d;
@@ -7088,9 +7161,9 @@ var DefaultStreamTextResult = class {
7088
7161
  }
7089
7162
  });
7090
7163
  const combinedUsage = addLanguageModelUsage(usage, stepUsage);
7091
- if (currentStep + 1 < maxSteps2 && // there are tool calls:
7092
- stepToolCalls.length > 0 && // all current tool calls have results:
7093
- stepToolResults.length === stepToolCalls.length) {
7164
+ await stepFinish.promise;
7165
+ if (stepToolCalls.length > 0 && // all current tool calls have results:
7166
+ stepToolResults.length === stepToolCalls.length && !await continueUntil({ steps: recordedSteps })) {
7094
7167
  responseMessages.push(
7095
7168
  ...toResponseMessages({
7096
7169
  content: stepContent,
@@ -7138,7 +7211,7 @@ var DefaultStreamTextResult = class {
7138
7211
  });
7139
7212
  }
7140
7213
  get steps() {
7141
- return this.stepsPromise.value;
7214
+ return this._steps.promise;
7142
7215
  }
7143
7216
  get finalStep() {
7144
7217
  return this.steps.then((steps) => steps[steps.length - 1]);
@@ -7183,10 +7256,10 @@ var DefaultStreamTextResult = class {
7183
7256
  return this.finalStep.then((step) => step.response);
7184
7257
  }
7185
7258
  get totalUsage() {
7186
- return this.totalUsagePromise.value;
7259
+ return this._totalUsage.promise;
7187
7260
  }
7188
7261
  get finishReason() {
7189
- return this.finishReasonPromise.value;
7262
+ return this._finishReason.promise;
7190
7263
  }
7191
7264
  /**
7192
7265
  Split out a new stream from the original stream.
@@ -7306,9 +7379,8 @@ var DefaultStreamTextResult = class {
7306
7379
  case "source": {
7307
7380
  if (sendSources) {
7308
7381
  controller.enqueue({
7309
- type: "source",
7310
- sourceType: part.sourceType,
7311
- id: part.id,
7382
+ type: "source-url",
7383
+ sourceId: part.id,
7312
7384
  url: part.url,
7313
7385
  title: part.title,
7314
7386
  providerMetadata: part.providerMetadata
@@ -8538,6 +8610,7 @@ var DefaultTranscriptionResult = class {
8538
8610
  NoSuchToolError,
8539
8611
  Output,
8540
8612
  RetryError,
8613
+ TextStreamChatTransport,
8541
8614
  ToolCallRepairError,
8542
8615
  ToolExecutionError,
8543
8616
  TypeValidationError,