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.mjs CHANGED
@@ -504,9 +504,8 @@ var uiMessageStreamPartSchema = z.union([
504
504
  providerMetadata: z.record(z.any()).optional()
505
505
  }),
506
506
  z.object({
507
- type: z.literal("source"),
508
- sourceType: z.literal("url"),
509
- id: z.string(),
507
+ type: z.literal("source-url"),
508
+ sourceId: z.string(),
510
509
  url: z.string(),
511
510
  title: z.string().optional(),
512
511
  providerMetadata: z.any().optional()
@@ -1062,16 +1061,13 @@ function processUIMessageStream({
1062
1061
  write();
1063
1062
  break;
1064
1063
  }
1065
- case "source": {
1064
+ case "source-url": {
1066
1065
  state.message.parts.push({
1067
- type: "source",
1068
- source: {
1069
- sourceType: "url",
1070
- id: part.id,
1071
- url: part.url,
1072
- title: part.title,
1073
- providerMetadata: part.providerMetadata
1074
- }
1066
+ type: "source-url",
1067
+ sourceId: part.sourceId,
1068
+ url: part.url,
1069
+ title: part.title,
1070
+ providerMetadata: part.providerMetadata
1075
1071
  });
1076
1072
  write();
1077
1073
  break;
@@ -1207,14 +1203,7 @@ function processUIMessageStream({
1207
1203
  (partArg) => part.type === partArg.type && part.id === partArg.id
1208
1204
  ) : void 0;
1209
1205
  if (existingPart != null) {
1210
- if (isObject(existingPart.data) && isObject(part.data)) {
1211
- existingPart.value = mergeObjects(
1212
- existingPart.data,
1213
- part.data
1214
- );
1215
- } else {
1216
- existingPart.data = part.data;
1217
- }
1206
+ existingPart.data = isObject(existingPart.data) && isObject(part.data) ? mergeObjects(existingPart.data, part.data) : part.data;
1218
1207
  } else {
1219
1208
  state.message.parts.push(part);
1220
1209
  }
@@ -1261,7 +1250,6 @@ var getOriginalFetch = () => fetch;
1261
1250
  async function fetchUIMessageStream({
1262
1251
  api,
1263
1252
  body,
1264
- streamProtocol = "ui-message",
1265
1253
  credentials,
1266
1254
  headers,
1267
1255
  abortController,
@@ -1295,9 +1283,7 @@ async function fetchUIMessageStream({
1295
1283
  if (!response.body) {
1296
1284
  throw new Error("The response body is empty.");
1297
1285
  }
1298
- return streamProtocol === "text" ? transformTextToUiMessageStream({
1299
- stream: response.body.pipeThrough(new TextDecoderStream())
1300
- }) : parseJsonEventStream({
1286
+ return parseJsonEventStream({
1301
1287
  stream: response.body,
1302
1288
  schema: uiMessageStreamPartSchema
1303
1289
  }).pipeThrough(
@@ -1311,6 +1297,46 @@ async function fetchUIMessageStream({
1311
1297
  })
1312
1298
  );
1313
1299
  }
1300
+ async function fetchTextStream({
1301
+ api,
1302
+ body,
1303
+ credentials,
1304
+ headers,
1305
+ abortController,
1306
+ fetch: fetch2 = getOriginalFetch(),
1307
+ requestType = "generate"
1308
+ }) {
1309
+ var _a17, _b, _c;
1310
+ const response = requestType === "resume" ? await fetch2(`${api}?chatId=${body.chatId}`, {
1311
+ method: "GET",
1312
+ headers: {
1313
+ "Content-Type": "application/json",
1314
+ ...headers
1315
+ },
1316
+ signal: (_a17 = abortController == null ? void 0 : abortController()) == null ? void 0 : _a17.signal,
1317
+ credentials
1318
+ }) : await fetch2(api, {
1319
+ method: "POST",
1320
+ body: JSON.stringify(body),
1321
+ headers: {
1322
+ "Content-Type": "application/json",
1323
+ ...headers
1324
+ },
1325
+ signal: (_b = abortController == null ? void 0 : abortController()) == null ? void 0 : _b.signal,
1326
+ credentials
1327
+ });
1328
+ if (!response.ok) {
1329
+ throw new Error(
1330
+ (_c = await response.text()) != null ? _c : "Failed to fetch the chat response."
1331
+ );
1332
+ }
1333
+ if (!response.body) {
1334
+ throw new Error("The response body is empty.");
1335
+ }
1336
+ return transformTextToUiMessageStream({
1337
+ stream: response.body.pipeThrough(new TextDecoderStream())
1338
+ });
1339
+ }
1314
1340
  async function consumeUIMessageStream({
1315
1341
  stream,
1316
1342
  onUpdate,
@@ -1361,10 +1387,17 @@ async function callChatApi({
1361
1387
  requestType = "generate",
1362
1388
  messageMetadataSchema
1363
1389
  }) {
1364
- const stream = await fetchUIMessageStream({
1390
+ const stream = streamProtocol === "text" ? await fetchTextStream({
1391
+ api,
1392
+ body,
1393
+ credentials,
1394
+ headers,
1395
+ abortController,
1396
+ fetch: fetch2,
1397
+ requestType
1398
+ }) : await fetchUIMessageStream({
1365
1399
  api,
1366
1400
  body,
1367
- streamProtocol,
1368
1401
  credentials,
1369
1402
  headers,
1370
1403
  abortController,
@@ -1918,7 +1951,6 @@ var DefaultChatTransport = class {
1918
1951
  credentials,
1919
1952
  headers,
1920
1953
  body,
1921
- streamProtocol,
1922
1954
  fetch: fetch2,
1923
1955
  prepareRequestBody
1924
1956
  }) {
@@ -1926,7 +1958,6 @@ var DefaultChatTransport = class {
1926
1958
  this.credentials = credentials;
1927
1959
  this.headers = headers;
1928
1960
  this.body = body;
1929
- this.streamProtocol = streamProtocol;
1930
1961
  this.fetch = fetch2;
1931
1962
  this.prepareRequestBody = prepareRequestBody;
1932
1963
  }
@@ -1956,7 +1987,55 @@ var DefaultChatTransport = class {
1956
1987
  ...this.body,
1957
1988
  ...body
1958
1989
  },
1959
- streamProtocol: this.streamProtocol,
1990
+ credentials: this.credentials,
1991
+ abortController: () => abortController,
1992
+ fetch: this.fetch,
1993
+ requestType
1994
+ });
1995
+ }
1996
+ };
1997
+ var TextStreamChatTransport = class {
1998
+ constructor({
1999
+ api,
2000
+ credentials,
2001
+ headers,
2002
+ body,
2003
+ fetch: fetch2,
2004
+ prepareRequestBody
2005
+ }) {
2006
+ this.api = api;
2007
+ this.credentials = credentials;
2008
+ this.headers = headers;
2009
+ this.body = body;
2010
+ this.fetch = fetch2;
2011
+ this.prepareRequestBody = prepareRequestBody;
2012
+ }
2013
+ submitMessages({
2014
+ chatId,
2015
+ messages,
2016
+ abortController,
2017
+ body,
2018
+ headers,
2019
+ requestType
2020
+ }) {
2021
+ var _a17, _b;
2022
+ return fetchTextStream({
2023
+ api: this.api,
2024
+ headers: {
2025
+ ...this.headers,
2026
+ ...headers
2027
+ },
2028
+ body: (_b = (_a17 = this.prepareRequestBody) == null ? void 0 : _a17.call(this, {
2029
+ chatId,
2030
+ messages,
2031
+ ...this.body,
2032
+ ...body
2033
+ })) != null ? _b : {
2034
+ chatId,
2035
+ messages,
2036
+ ...this.body,
2037
+ ...body
2038
+ },
1960
2039
  credentials: this.credentials,
1961
2040
  abortController: () => abortController,
1962
2041
  fetch: this.fetch,
@@ -2158,7 +2237,6 @@ import {
2158
2237
  function defaultChatStore({
2159
2238
  api,
2160
2239
  fetch: fetch2,
2161
- streamProtocol = "ui-message",
2162
2240
  credentials,
2163
2241
  headers,
2164
2242
  body,
@@ -2173,7 +2251,6 @@ function defaultChatStore({
2173
2251
  transport: new DefaultChatTransport({
2174
2252
  api,
2175
2253
  fetch: fetch2,
2176
- streamProtocol,
2177
2254
  credentials,
2178
2255
  headers,
2179
2256
  body,
@@ -4691,11 +4768,11 @@ var DelayedPromise = class {
4691
4768
  this._resolve = void 0;
4692
4769
  this._reject = void 0;
4693
4770
  }
4694
- get value() {
4695
- if (this.promise) {
4696
- return this.promise;
4771
+ get promise() {
4772
+ if (this._promise) {
4773
+ return this._promise;
4697
4774
  }
4698
- this.promise = new Promise((resolve, reject) => {
4775
+ this._promise = new Promise((resolve, reject) => {
4699
4776
  if (this.status.type === "resolved") {
4700
4777
  resolve(this.status.value);
4701
4778
  } else if (this.status.type === "rejected") {
@@ -4704,19 +4781,19 @@ var DelayedPromise = class {
4704
4781
  this._resolve = resolve;
4705
4782
  this._reject = reject;
4706
4783
  });
4707
- return this.promise;
4784
+ return this._promise;
4708
4785
  }
4709
4786
  resolve(value) {
4710
4787
  var _a17;
4711
4788
  this.status = { type: "resolved", value };
4712
- if (this.promise) {
4789
+ if (this._promise) {
4713
4790
  (_a17 = this._resolve) == null ? void 0 : _a17.call(this, value);
4714
4791
  }
4715
4792
  }
4716
4793
  reject(error) {
4717
4794
  var _a17;
4718
4795
  this.status = { type: "rejected", error };
4719
- if (this.promise) {
4796
+ if (this._promise) {
4720
4797
  (_a17 = this._reject) == null ? void 0 : _a17.call(this, error);
4721
4798
  }
4722
4799
  }
@@ -4811,12 +4888,12 @@ var DefaultStreamObjectResult = class {
4811
4888
  currentDate,
4812
4889
  now: now2
4813
4890
  }) {
4814
- this.objectPromise = new DelayedPromise();
4815
- this.usagePromise = new DelayedPromise();
4816
- this.providerMetadataPromise = new DelayedPromise();
4817
- this.warningsPromise = new DelayedPromise();
4818
- this.requestPromise = new DelayedPromise();
4819
- this.responsePromise = new DelayedPromise();
4891
+ this._object = new DelayedPromise();
4892
+ this._usage = new DelayedPromise();
4893
+ this._providerMetadata = new DelayedPromise();
4894
+ this._warnings = new DelayedPromise();
4895
+ this._request = new DelayedPromise();
4896
+ this._response = new DelayedPromise();
4820
4897
  const { maxRetries, retry } = prepareRetries({
4821
4898
  maxRetries: maxRetriesArg
4822
4899
  });
@@ -4935,7 +5012,7 @@ var DefaultStreamObjectResult = class {
4935
5012
  })
4936
5013
  })
4937
5014
  );
4938
- self.requestPromise.resolve(request != null ? request : {});
5015
+ self._request.resolve(request != null ? request : {});
4939
5016
  let warnings;
4940
5017
  let usage = {
4941
5018
  inputTokens: void 0,
@@ -5028,9 +5105,9 @@ var DefaultStreamObjectResult = class {
5028
5105
  usage,
5029
5106
  response: fullResponse
5030
5107
  });
5031
- self.usagePromise.resolve(usage);
5032
- self.providerMetadataPromise.resolve(providerMetadata);
5033
- self.responsePromise.resolve({
5108
+ self._usage.resolve(usage);
5109
+ self._providerMetadata.resolve(providerMetadata);
5110
+ self._response.resolve({
5034
5111
  ...fullResponse,
5035
5112
  headers: response == null ? void 0 : response.headers
5036
5113
  });
@@ -5044,7 +5121,7 @@ var DefaultStreamObjectResult = class {
5044
5121
  );
5045
5122
  if (validationResult.success) {
5046
5123
  object2 = validationResult.value;
5047
- self.objectPromise.resolve(object2);
5124
+ self._object.resolve(object2);
5048
5125
  } else {
5049
5126
  error = new NoObjectGeneratedError({
5050
5127
  message: "No object generated: response did not match schema.",
@@ -5054,7 +5131,7 @@ var DefaultStreamObjectResult = class {
5054
5131
  usage,
5055
5132
  finishReason
5056
5133
  });
5057
- self.objectPromise.reject(error);
5134
+ self._object.reject(error);
5058
5135
  }
5059
5136
  break;
5060
5137
  }
@@ -5149,22 +5226,22 @@ var DefaultStreamObjectResult = class {
5149
5226
  this.outputStrategy = outputStrategy;
5150
5227
  }
5151
5228
  get object() {
5152
- return this.objectPromise.value;
5229
+ return this._object.promise;
5153
5230
  }
5154
5231
  get usage() {
5155
- return this.usagePromise.value;
5232
+ return this._usage.promise;
5156
5233
  }
5157
5234
  get providerMetadata() {
5158
- return this.providerMetadataPromise.value;
5235
+ return this._providerMetadata.promise;
5159
5236
  }
5160
5237
  get warnings() {
5161
- return this.warningsPromise.value;
5238
+ return this._warnings.promise;
5162
5239
  }
5163
5240
  get request() {
5164
- return this.requestPromise.value;
5241
+ return this._request.promise;
5165
5242
  }
5166
5243
  get response() {
5167
- return this.responsePromise.value;
5244
+ return this._response.promise;
5168
5245
  }
5169
5246
  get partialObjectStream() {
5170
5247
  return createAsyncIterableStream(
@@ -6420,7 +6497,7 @@ function streamText({
6420
6497
  maxRetries,
6421
6498
  abortSignal,
6422
6499
  headers,
6423
- maxSteps: maxSteps2 = 1,
6500
+ continueUntil = maxSteps(1),
6424
6501
  experimental_output: output,
6425
6502
  experimental_telemetry: telemetry,
6426
6503
  providerOptions,
@@ -6456,7 +6533,7 @@ function streamText({
6456
6533
  transforms: asArray(transform),
6457
6534
  activeTools,
6458
6535
  repairToolCall,
6459
- maxSteps: maxSteps2,
6536
+ continueUntil,
6460
6537
  output,
6461
6538
  providerOptions,
6462
6539
  onChunk,
@@ -6533,7 +6610,7 @@ var DefaultStreamTextResult = class {
6533
6610
  transforms,
6534
6611
  activeTools,
6535
6612
  repairToolCall,
6536
- maxSteps: maxSteps2,
6613
+ continueUntil,
6537
6614
  output,
6538
6615
  providerOptions,
6539
6616
  now: now2,
@@ -6544,18 +6621,12 @@ var DefaultStreamTextResult = class {
6544
6621
  onFinish,
6545
6622
  onStepFinish
6546
6623
  }) {
6547
- this.totalUsagePromise = new DelayedPromise();
6548
- this.finishReasonPromise = new DelayedPromise();
6549
- this.stepsPromise = new DelayedPromise();
6550
- if (maxSteps2 < 1) {
6551
- throw new InvalidArgumentError({
6552
- parameter: "maxSteps",
6553
- value: maxSteps2,
6554
- message: "maxSteps must be at least 1"
6555
- });
6556
- }
6624
+ this._totalUsage = new DelayedPromise();
6625
+ this._finishReason = new DelayedPromise();
6626
+ this._steps = new DelayedPromise();
6557
6627
  this.output = output;
6558
6628
  this.generateId = generateId3;
6629
+ let stepFinish;
6559
6630
  let activeReasoningPart = void 0;
6560
6631
  let recordedContent = [];
6561
6632
  const recordedResponseMessages = [];
@@ -6637,6 +6708,7 @@ var DefaultStreamTextResult = class {
6637
6708
  recordedContent = [];
6638
6709
  activeReasoningPart = void 0;
6639
6710
  recordedResponseMessages.push(...stepMessages);
6711
+ stepFinish.resolve();
6640
6712
  }
6641
6713
  if (part.type === "finish") {
6642
6714
  recordedTotalUsage = part.totalUsage;
@@ -6654,9 +6726,9 @@ var DefaultStreamTextResult = class {
6654
6726
  outputTokens: void 0,
6655
6727
  totalTokens: void 0
6656
6728
  };
6657
- self.finishReasonPromise.resolve(finishReason);
6658
- self.totalUsagePromise.resolve(totalUsage);
6659
- self.stepsPromise.resolve(recordedSteps);
6729
+ self._finishReason.resolve(finishReason);
6730
+ self._totalUsage.resolve(totalUsage);
6731
+ self._steps.resolve(recordedSteps);
6660
6732
  const finalStep = recordedSteps[recordedSteps.length - 1];
6661
6733
  await (onFinish == null ? void 0 : onFinish({
6662
6734
  finishReason,
@@ -6747,8 +6819,7 @@ var DefaultStreamTextResult = class {
6747
6819
  // specific settings that only make sense on the outer level:
6748
6820
  "ai.prompt": {
6749
6821
  input: () => JSON.stringify({ system, prompt, messages })
6750
- },
6751
- "ai.settings.maxSteps": maxSteps2
6822
+ }
6752
6823
  }
6753
6824
  }),
6754
6825
  tracer,
@@ -6760,6 +6831,7 @@ var DefaultStreamTextResult = class {
6760
6831
  responseMessages,
6761
6832
  usage
6762
6833
  }) {
6834
+ stepFinish = new DelayedPromise();
6763
6835
  const initialPrompt = await standardizePrompt({
6764
6836
  system,
6765
6837
  prompt,
@@ -6841,7 +6913,7 @@ var DefaultStreamTextResult = class {
6841
6913
  }
6842
6914
  })
6843
6915
  );
6844
- const transformedStream = runToolsTransformation({
6916
+ const streamWithToolResults = runToolsTransformation({
6845
6917
  tools,
6846
6918
  generatorStream: stream2,
6847
6919
  toolCallStreaming,
@@ -6880,7 +6952,7 @@ var DefaultStreamTextResult = class {
6880
6952
  stepText += chunk.text;
6881
6953
  }
6882
6954
  self.addStream(
6883
- transformedStream.pipeThrough(
6955
+ streamWithToolResults.pipeThrough(
6884
6956
  new TransformStream({
6885
6957
  async transform(chunk, controller) {
6886
6958
  var _a17, _b, _c, _d;
@@ -7035,9 +7107,9 @@ var DefaultStreamTextResult = class {
7035
7107
  }
7036
7108
  });
7037
7109
  const combinedUsage = addLanguageModelUsage(usage, stepUsage);
7038
- if (currentStep + 1 < maxSteps2 && // there are tool calls:
7039
- stepToolCalls.length > 0 && // all current tool calls have results:
7040
- stepToolResults.length === stepToolCalls.length) {
7110
+ await stepFinish.promise;
7111
+ if (stepToolCalls.length > 0 && // all current tool calls have results:
7112
+ stepToolResults.length === stepToolCalls.length && !await continueUntil({ steps: recordedSteps })) {
7041
7113
  responseMessages.push(
7042
7114
  ...toResponseMessages({
7043
7115
  content: stepContent,
@@ -7085,7 +7157,7 @@ var DefaultStreamTextResult = class {
7085
7157
  });
7086
7158
  }
7087
7159
  get steps() {
7088
- return this.stepsPromise.value;
7160
+ return this._steps.promise;
7089
7161
  }
7090
7162
  get finalStep() {
7091
7163
  return this.steps.then((steps) => steps[steps.length - 1]);
@@ -7130,10 +7202,10 @@ var DefaultStreamTextResult = class {
7130
7202
  return this.finalStep.then((step) => step.response);
7131
7203
  }
7132
7204
  get totalUsage() {
7133
- return this.totalUsagePromise.value;
7205
+ return this._totalUsage.promise;
7134
7206
  }
7135
7207
  get finishReason() {
7136
- return this.finishReasonPromise.value;
7208
+ return this._finishReason.promise;
7137
7209
  }
7138
7210
  /**
7139
7211
  Split out a new stream from the original stream.
@@ -7253,9 +7325,8 @@ var DefaultStreamTextResult = class {
7253
7325
  case "source": {
7254
7326
  if (sendSources) {
7255
7327
  controller.enqueue({
7256
- type: "source",
7257
- sourceType: part.sourceType,
7258
- id: part.id,
7328
+ type: "source-url",
7329
+ sourceId: part.id,
7259
7330
  url: part.url,
7260
7331
  title: part.title,
7261
7332
  providerMetadata: part.providerMetadata
@@ -8484,6 +8555,7 @@ export {
8484
8555
  NoSuchToolError,
8485
8556
  output_exports as Output,
8486
8557
  RetryError,
8558
+ TextStreamChatTransport,
8487
8559
  ToolCallRepairError,
8488
8560
  ToolExecutionError,
8489
8561
  TypeValidationError,