ai 5.0.0-alpha.2 → 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;
@@ -1254,7 +1250,6 @@ var getOriginalFetch = () => fetch;
1254
1250
  async function fetchUIMessageStream({
1255
1251
  api,
1256
1252
  body,
1257
- streamProtocol = "ui-message",
1258
1253
  credentials,
1259
1254
  headers,
1260
1255
  abortController,
@@ -1288,9 +1283,7 @@ async function fetchUIMessageStream({
1288
1283
  if (!response.body) {
1289
1284
  throw new Error("The response body is empty.");
1290
1285
  }
1291
- return streamProtocol === "text" ? transformTextToUiMessageStream({
1292
- stream: response.body.pipeThrough(new TextDecoderStream())
1293
- }) : parseJsonEventStream({
1286
+ return parseJsonEventStream({
1294
1287
  stream: response.body,
1295
1288
  schema: uiMessageStreamPartSchema
1296
1289
  }).pipeThrough(
@@ -1304,6 +1297,46 @@ async function fetchUIMessageStream({
1304
1297
  })
1305
1298
  );
1306
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
+ }
1307
1340
  async function consumeUIMessageStream({
1308
1341
  stream,
1309
1342
  onUpdate,
@@ -1354,10 +1387,17 @@ async function callChatApi({
1354
1387
  requestType = "generate",
1355
1388
  messageMetadataSchema
1356
1389
  }) {
1357
- 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({
1358
1399
  api,
1359
1400
  body,
1360
- streamProtocol,
1361
1401
  credentials,
1362
1402
  headers,
1363
1403
  abortController,
@@ -1911,7 +1951,6 @@ var DefaultChatTransport = class {
1911
1951
  credentials,
1912
1952
  headers,
1913
1953
  body,
1914
- streamProtocol,
1915
1954
  fetch: fetch2,
1916
1955
  prepareRequestBody
1917
1956
  }) {
@@ -1919,7 +1958,6 @@ var DefaultChatTransport = class {
1919
1958
  this.credentials = credentials;
1920
1959
  this.headers = headers;
1921
1960
  this.body = body;
1922
- this.streamProtocol = streamProtocol;
1923
1961
  this.fetch = fetch2;
1924
1962
  this.prepareRequestBody = prepareRequestBody;
1925
1963
  }
@@ -1949,7 +1987,55 @@ var DefaultChatTransport = class {
1949
1987
  ...this.body,
1950
1988
  ...body
1951
1989
  },
1952
- 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
+ },
1953
2039
  credentials: this.credentials,
1954
2040
  abortController: () => abortController,
1955
2041
  fetch: this.fetch,
@@ -2151,7 +2237,6 @@ import {
2151
2237
  function defaultChatStore({
2152
2238
  api,
2153
2239
  fetch: fetch2,
2154
- streamProtocol = "ui-message",
2155
2240
  credentials,
2156
2241
  headers,
2157
2242
  body,
@@ -2166,7 +2251,6 @@ function defaultChatStore({
2166
2251
  transport: new DefaultChatTransport({
2167
2252
  api,
2168
2253
  fetch: fetch2,
2169
- streamProtocol,
2170
2254
  credentials,
2171
2255
  headers,
2172
2256
  body,
@@ -4684,11 +4768,11 @@ var DelayedPromise = class {
4684
4768
  this._resolve = void 0;
4685
4769
  this._reject = void 0;
4686
4770
  }
4687
- get value() {
4688
- if (this.promise) {
4689
- return this.promise;
4771
+ get promise() {
4772
+ if (this._promise) {
4773
+ return this._promise;
4690
4774
  }
4691
- this.promise = new Promise((resolve, reject) => {
4775
+ this._promise = new Promise((resolve, reject) => {
4692
4776
  if (this.status.type === "resolved") {
4693
4777
  resolve(this.status.value);
4694
4778
  } else if (this.status.type === "rejected") {
@@ -4697,19 +4781,19 @@ var DelayedPromise = class {
4697
4781
  this._resolve = resolve;
4698
4782
  this._reject = reject;
4699
4783
  });
4700
- return this.promise;
4784
+ return this._promise;
4701
4785
  }
4702
4786
  resolve(value) {
4703
4787
  var _a17;
4704
4788
  this.status = { type: "resolved", value };
4705
- if (this.promise) {
4789
+ if (this._promise) {
4706
4790
  (_a17 = this._resolve) == null ? void 0 : _a17.call(this, value);
4707
4791
  }
4708
4792
  }
4709
4793
  reject(error) {
4710
4794
  var _a17;
4711
4795
  this.status = { type: "rejected", error };
4712
- if (this.promise) {
4796
+ if (this._promise) {
4713
4797
  (_a17 = this._reject) == null ? void 0 : _a17.call(this, error);
4714
4798
  }
4715
4799
  }
@@ -4804,12 +4888,12 @@ var DefaultStreamObjectResult = class {
4804
4888
  currentDate,
4805
4889
  now: now2
4806
4890
  }) {
4807
- this.objectPromise = new DelayedPromise();
4808
- this.usagePromise = new DelayedPromise();
4809
- this.providerMetadataPromise = new DelayedPromise();
4810
- this.warningsPromise = new DelayedPromise();
4811
- this.requestPromise = new DelayedPromise();
4812
- 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();
4813
4897
  const { maxRetries, retry } = prepareRetries({
4814
4898
  maxRetries: maxRetriesArg
4815
4899
  });
@@ -4928,7 +5012,7 @@ var DefaultStreamObjectResult = class {
4928
5012
  })
4929
5013
  })
4930
5014
  );
4931
- self.requestPromise.resolve(request != null ? request : {});
5015
+ self._request.resolve(request != null ? request : {});
4932
5016
  let warnings;
4933
5017
  let usage = {
4934
5018
  inputTokens: void 0,
@@ -5021,9 +5105,9 @@ var DefaultStreamObjectResult = class {
5021
5105
  usage,
5022
5106
  response: fullResponse
5023
5107
  });
5024
- self.usagePromise.resolve(usage);
5025
- self.providerMetadataPromise.resolve(providerMetadata);
5026
- self.responsePromise.resolve({
5108
+ self._usage.resolve(usage);
5109
+ self._providerMetadata.resolve(providerMetadata);
5110
+ self._response.resolve({
5027
5111
  ...fullResponse,
5028
5112
  headers: response == null ? void 0 : response.headers
5029
5113
  });
@@ -5037,7 +5121,7 @@ var DefaultStreamObjectResult = class {
5037
5121
  );
5038
5122
  if (validationResult.success) {
5039
5123
  object2 = validationResult.value;
5040
- self.objectPromise.resolve(object2);
5124
+ self._object.resolve(object2);
5041
5125
  } else {
5042
5126
  error = new NoObjectGeneratedError({
5043
5127
  message: "No object generated: response did not match schema.",
@@ -5047,7 +5131,7 @@ var DefaultStreamObjectResult = class {
5047
5131
  usage,
5048
5132
  finishReason
5049
5133
  });
5050
- self.objectPromise.reject(error);
5134
+ self._object.reject(error);
5051
5135
  }
5052
5136
  break;
5053
5137
  }
@@ -5142,22 +5226,22 @@ var DefaultStreamObjectResult = class {
5142
5226
  this.outputStrategy = outputStrategy;
5143
5227
  }
5144
5228
  get object() {
5145
- return this.objectPromise.value;
5229
+ return this._object.promise;
5146
5230
  }
5147
5231
  get usage() {
5148
- return this.usagePromise.value;
5232
+ return this._usage.promise;
5149
5233
  }
5150
5234
  get providerMetadata() {
5151
- return this.providerMetadataPromise.value;
5235
+ return this._providerMetadata.promise;
5152
5236
  }
5153
5237
  get warnings() {
5154
- return this.warningsPromise.value;
5238
+ return this._warnings.promise;
5155
5239
  }
5156
5240
  get request() {
5157
- return this.requestPromise.value;
5241
+ return this._request.promise;
5158
5242
  }
5159
5243
  get response() {
5160
- return this.responsePromise.value;
5244
+ return this._response.promise;
5161
5245
  }
5162
5246
  get partialObjectStream() {
5163
5247
  return createAsyncIterableStream(
@@ -6413,7 +6497,7 @@ function streamText({
6413
6497
  maxRetries,
6414
6498
  abortSignal,
6415
6499
  headers,
6416
- maxSteps: maxSteps2 = 1,
6500
+ continueUntil = maxSteps(1),
6417
6501
  experimental_output: output,
6418
6502
  experimental_telemetry: telemetry,
6419
6503
  providerOptions,
@@ -6449,7 +6533,7 @@ function streamText({
6449
6533
  transforms: asArray(transform),
6450
6534
  activeTools,
6451
6535
  repairToolCall,
6452
- maxSteps: maxSteps2,
6536
+ continueUntil,
6453
6537
  output,
6454
6538
  providerOptions,
6455
6539
  onChunk,
@@ -6526,7 +6610,7 @@ var DefaultStreamTextResult = class {
6526
6610
  transforms,
6527
6611
  activeTools,
6528
6612
  repairToolCall,
6529
- maxSteps: maxSteps2,
6613
+ continueUntil,
6530
6614
  output,
6531
6615
  providerOptions,
6532
6616
  now: now2,
@@ -6537,18 +6621,12 @@ var DefaultStreamTextResult = class {
6537
6621
  onFinish,
6538
6622
  onStepFinish
6539
6623
  }) {
6540
- this.totalUsagePromise = new DelayedPromise();
6541
- this.finishReasonPromise = new DelayedPromise();
6542
- this.stepsPromise = new DelayedPromise();
6543
- if (maxSteps2 < 1) {
6544
- throw new InvalidArgumentError({
6545
- parameter: "maxSteps",
6546
- value: maxSteps2,
6547
- message: "maxSteps must be at least 1"
6548
- });
6549
- }
6624
+ this._totalUsage = new DelayedPromise();
6625
+ this._finishReason = new DelayedPromise();
6626
+ this._steps = new DelayedPromise();
6550
6627
  this.output = output;
6551
6628
  this.generateId = generateId3;
6629
+ let stepFinish;
6552
6630
  let activeReasoningPart = void 0;
6553
6631
  let recordedContent = [];
6554
6632
  const recordedResponseMessages = [];
@@ -6630,6 +6708,7 @@ var DefaultStreamTextResult = class {
6630
6708
  recordedContent = [];
6631
6709
  activeReasoningPart = void 0;
6632
6710
  recordedResponseMessages.push(...stepMessages);
6711
+ stepFinish.resolve();
6633
6712
  }
6634
6713
  if (part.type === "finish") {
6635
6714
  recordedTotalUsage = part.totalUsage;
@@ -6647,9 +6726,9 @@ var DefaultStreamTextResult = class {
6647
6726
  outputTokens: void 0,
6648
6727
  totalTokens: void 0
6649
6728
  };
6650
- self.finishReasonPromise.resolve(finishReason);
6651
- self.totalUsagePromise.resolve(totalUsage);
6652
- self.stepsPromise.resolve(recordedSteps);
6729
+ self._finishReason.resolve(finishReason);
6730
+ self._totalUsage.resolve(totalUsage);
6731
+ self._steps.resolve(recordedSteps);
6653
6732
  const finalStep = recordedSteps[recordedSteps.length - 1];
6654
6733
  await (onFinish == null ? void 0 : onFinish({
6655
6734
  finishReason,
@@ -6740,8 +6819,7 @@ var DefaultStreamTextResult = class {
6740
6819
  // specific settings that only make sense on the outer level:
6741
6820
  "ai.prompt": {
6742
6821
  input: () => JSON.stringify({ system, prompt, messages })
6743
- },
6744
- "ai.settings.maxSteps": maxSteps2
6822
+ }
6745
6823
  }
6746
6824
  }),
6747
6825
  tracer,
@@ -6753,6 +6831,7 @@ var DefaultStreamTextResult = class {
6753
6831
  responseMessages,
6754
6832
  usage
6755
6833
  }) {
6834
+ stepFinish = new DelayedPromise();
6756
6835
  const initialPrompt = await standardizePrompt({
6757
6836
  system,
6758
6837
  prompt,
@@ -6834,7 +6913,7 @@ var DefaultStreamTextResult = class {
6834
6913
  }
6835
6914
  })
6836
6915
  );
6837
- const transformedStream = runToolsTransformation({
6916
+ const streamWithToolResults = runToolsTransformation({
6838
6917
  tools,
6839
6918
  generatorStream: stream2,
6840
6919
  toolCallStreaming,
@@ -6873,7 +6952,7 @@ var DefaultStreamTextResult = class {
6873
6952
  stepText += chunk.text;
6874
6953
  }
6875
6954
  self.addStream(
6876
- transformedStream.pipeThrough(
6955
+ streamWithToolResults.pipeThrough(
6877
6956
  new TransformStream({
6878
6957
  async transform(chunk, controller) {
6879
6958
  var _a17, _b, _c, _d;
@@ -7028,9 +7107,9 @@ var DefaultStreamTextResult = class {
7028
7107
  }
7029
7108
  });
7030
7109
  const combinedUsage = addLanguageModelUsage(usage, stepUsage);
7031
- if (currentStep + 1 < maxSteps2 && // there are tool calls:
7032
- stepToolCalls.length > 0 && // all current tool calls have results:
7033
- 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 })) {
7034
7113
  responseMessages.push(
7035
7114
  ...toResponseMessages({
7036
7115
  content: stepContent,
@@ -7078,7 +7157,7 @@ var DefaultStreamTextResult = class {
7078
7157
  });
7079
7158
  }
7080
7159
  get steps() {
7081
- return this.stepsPromise.value;
7160
+ return this._steps.promise;
7082
7161
  }
7083
7162
  get finalStep() {
7084
7163
  return this.steps.then((steps) => steps[steps.length - 1]);
@@ -7123,10 +7202,10 @@ var DefaultStreamTextResult = class {
7123
7202
  return this.finalStep.then((step) => step.response);
7124
7203
  }
7125
7204
  get totalUsage() {
7126
- return this.totalUsagePromise.value;
7205
+ return this._totalUsage.promise;
7127
7206
  }
7128
7207
  get finishReason() {
7129
- return this.finishReasonPromise.value;
7208
+ return this._finishReason.promise;
7130
7209
  }
7131
7210
  /**
7132
7211
  Split out a new stream from the original stream.
@@ -7246,9 +7325,8 @@ var DefaultStreamTextResult = class {
7246
7325
  case "source": {
7247
7326
  if (sendSources) {
7248
7327
  controller.enqueue({
7249
- type: "source",
7250
- sourceType: part.sourceType,
7251
- id: part.id,
7328
+ type: "source-url",
7329
+ sourceId: part.id,
7252
7330
  url: part.url,
7253
7331
  title: part.title,
7254
7332
  providerMetadata: part.providerMetadata
@@ -8477,6 +8555,7 @@ export {
8477
8555
  NoSuchToolError,
8478
8556
  output_exports as Output,
8479
8557
  RetryError,
8558
+ TextStreamChatTransport,
8480
8559
  ToolCallRepairError,
8481
8560
  ToolExecutionError,
8482
8561
  TypeValidationError,