ai 5.0.0-beta.25 → 5.0.0-beta.27

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
@@ -94,6 +94,7 @@ __export(src_exports, {
94
94
  isDeepEqualData: () => isDeepEqualData,
95
95
  isToolUIPart: () => isToolUIPart,
96
96
  jsonSchema: () => import_provider_utils29.jsonSchema,
97
+ lastAssistantMessageIsCompleteWithToolCalls: () => lastAssistantMessageIsCompleteWithToolCalls,
97
98
  modelMessageSchema: () => modelMessageSchema,
98
99
  parsePartialJson: () => parsePartialJson,
99
100
  pipeTextStreamToResponse: () => pipeTextStreamToResponse,
@@ -3146,11 +3147,11 @@ function createStreamingUIMessageState({
3146
3147
  }
3147
3148
  function processUIMessageStream({
3148
3149
  stream,
3149
- onToolCall,
3150
3150
  messageMetadataSchema,
3151
3151
  dataPartSchemas,
3152
3152
  runUpdateMessageJob,
3153
3153
  onError,
3154
+ onToolCall,
3154
3155
  onData
3155
3156
  }) {
3156
3157
  return stream.pipeThrough(
@@ -3329,19 +3330,9 @@ function processUIMessageStream({
3329
3330
  });
3330
3331
  write();
3331
3332
  if (onToolCall && !chunk.providerExecuted) {
3332
- const result = await onToolCall({
3333
+ await onToolCall({
3333
3334
  toolCall: chunk
3334
3335
  });
3335
- if (result != null) {
3336
- updateToolInvocationPart({
3337
- toolCallId: chunk.toolCallId,
3338
- toolName: chunk.toolName,
3339
- state: "output-available",
3340
- input: chunk.input,
3341
- output: result
3342
- });
3343
- write();
3344
- }
3345
3336
  }
3346
3337
  break;
3347
3338
  }
@@ -4066,7 +4057,7 @@ function createOutputTransformStream(output) {
4066
4057
  }) {
4067
4058
  controller.enqueue({
4068
4059
  part: {
4069
- type: "text",
4060
+ type: "text-delta",
4070
4061
  id: firstTextChunkId,
4071
4062
  text: textChunk
4072
4063
  },
@@ -4079,7 +4070,7 @@ function createOutputTransformStream(output) {
4079
4070
  if (chunk.type === "finish-step" && textChunk.length > 0) {
4080
4071
  publishTextChunk({ controller });
4081
4072
  }
4082
- if (chunk.type !== "text" && chunk.type !== "text-start" && chunk.type !== "text-end") {
4073
+ if (chunk.type !== "text-delta" && chunk.type !== "text-start" && chunk.type !== "text-end") {
4083
4074
  controller.enqueue({ part: chunk, partialOutput: void 0 });
4084
4075
  return;
4085
4076
  }
@@ -4164,7 +4155,7 @@ var DefaultStreamTextResult = class {
4164
4155
  var _a16, _b, _c;
4165
4156
  controller.enqueue(chunk);
4166
4157
  const { part } = chunk;
4167
- if (part.type === "text" || part.type === "reasoning" || part.type === "source" || part.type === "tool-call" || part.type === "tool-result" || part.type === "tool-input-start" || part.type === "tool-input-delta" || part.type === "raw") {
4158
+ if (part.type === "text-delta" || part.type === "reasoning-delta" || part.type === "source" || part.type === "tool-call" || part.type === "tool-result" || part.type === "tool-input-start" || part.type === "tool-input-delta" || part.type === "raw") {
4168
4159
  await (onChunk == null ? void 0 : onChunk({ chunk: part }));
4169
4160
  }
4170
4161
  if (part.type === "error") {
@@ -4178,7 +4169,7 @@ var DefaultStreamTextResult = class {
4178
4169
  };
4179
4170
  recordedContent.push(activeTextContent[part.id]);
4180
4171
  }
4181
- if (part.type === "text") {
4172
+ if (part.type === "text-delta") {
4182
4173
  const activeText = activeTextContent[part.id];
4183
4174
  if (activeText == null) {
4184
4175
  controller.enqueue({
@@ -4204,7 +4195,7 @@ var DefaultStreamTextResult = class {
4204
4195
  };
4205
4196
  recordedContent.push(activeReasoningContent[part.id]);
4206
4197
  }
4207
- if (part.type === "reasoning") {
4198
+ if (part.type === "reasoning-delta") {
4208
4199
  const activeReasoning = activeReasoningContent[part.id];
4209
4200
  if (activeReasoning == null) {
4210
4201
  controller.enqueue({
@@ -4570,7 +4561,7 @@ var DefaultStreamTextResult = class {
4570
4561
  case "text-delta": {
4571
4562
  if (chunk.delta.length > 0) {
4572
4563
  controller.enqueue({
4573
- type: "text",
4564
+ type: "text-delta",
4574
4565
  id: chunk.id,
4575
4566
  text: chunk.delta,
4576
4567
  providerMetadata: chunk.providerMetadata
@@ -4586,7 +4577,7 @@ var DefaultStreamTextResult = class {
4586
4577
  }
4587
4578
  case "reasoning-delta": {
4588
4579
  controller.enqueue({
4589
- type: "reasoning",
4580
+ type: "reasoning-delta",
4590
4581
  id: chunk.id,
4591
4582
  text: chunk.delta,
4592
4583
  providerMetadata: chunk.providerMetadata
@@ -4872,7 +4863,7 @@ var DefaultStreamTextResult = class {
4872
4863
  this.teeStream().pipeThrough(
4873
4864
  new TransformStream({
4874
4865
  transform({ part }, controller) {
4875
- if (part.type === "text") {
4866
+ if (part.type === "text-delta") {
4876
4867
  controller.enqueue(part.text);
4877
4868
  }
4878
4869
  }
@@ -4947,7 +4938,7 @@ var DefaultStreamTextResult = class {
4947
4938
  });
4948
4939
  break;
4949
4940
  }
4950
- case "text": {
4941
+ case "text-delta": {
4951
4942
  controller.enqueue({
4952
4943
  type: "text-delta",
4953
4944
  id: part.id,
@@ -4972,7 +4963,7 @@ var DefaultStreamTextResult = class {
4972
4963
  });
4973
4964
  break;
4974
4965
  }
4975
- case "reasoning": {
4966
+ case "reasoning-delta": {
4976
4967
  if (sendReasoning) {
4977
4968
  controller.enqueue({
4978
4969
  type: "reasoning-delta",
@@ -7193,23 +7184,23 @@ function smoothStream({
7193
7184
  let id = "";
7194
7185
  return new TransformStream({
7195
7186
  async transform(chunk, controller) {
7196
- if (chunk.type !== "text") {
7187
+ if (chunk.type !== "text-delta") {
7197
7188
  if (buffer.length > 0) {
7198
- controller.enqueue({ type: "text", text: buffer, id });
7189
+ controller.enqueue({ type: "text-delta", text: buffer, id });
7199
7190
  buffer = "";
7200
7191
  }
7201
7192
  controller.enqueue(chunk);
7202
7193
  return;
7203
7194
  }
7204
7195
  if (chunk.id !== id && buffer.length > 0) {
7205
- controller.enqueue({ type: "text", text: buffer, id });
7196
+ controller.enqueue({ type: "text-delta", text: buffer, id });
7206
7197
  buffer = "";
7207
7198
  }
7208
7199
  buffer += chunk.text;
7209
7200
  id = chunk.id;
7210
7201
  let match;
7211
7202
  while ((match = detectChunk(buffer)) != null) {
7212
- controller.enqueue({ type: "text", text: match, id });
7203
+ controller.enqueue({ type: "text-delta", text: match, id });
7213
7204
  buffer = buffer.slice(match.length);
7214
7205
  await delay2(delayInMs);
7215
7206
  }
@@ -8668,54 +8659,20 @@ var DefaultChatTransport = class extends HttpChatTransport {
8668
8659
  }
8669
8660
  };
8670
8661
 
8671
- // src/ui/should-resubmit-messages.ts
8672
- function shouldResubmitMessages({
8673
- originalMaxToolInvocationStep,
8674
- originalMessageCount,
8675
- maxSteps,
8676
- messages
8677
- }) {
8678
- const lastMessage = messages[messages.length - 1];
8679
- const lastMessageStepStartCount = lastMessage.parts.filter(
8680
- (part) => part.type === "step-start"
8681
- ).length;
8682
- return (
8683
- // check if the feature is enabled:
8684
- maxSteps > 1 && // ensure there is a last message:
8685
- lastMessage != null && // ensure we actually have new steps (to prevent infinite loops in case of errors):
8686
- (messages.length > originalMessageCount || lastMessageStepStartCount !== originalMaxToolInvocationStep) && // check that next step is possible:
8687
- isAssistantMessageWithCompletedToolCalls(lastMessage) && // limit the number of automatic steps:
8688
- lastMessageStepStartCount < maxSteps
8689
- );
8690
- }
8691
- function isAssistantMessageWithCompletedToolCalls(message) {
8692
- if (!message) {
8693
- return false;
8694
- }
8695
- if (message.role !== "assistant") {
8696
- return false;
8697
- }
8698
- const lastStepStartIndex = message.parts.reduce((lastIndex, part, index) => {
8699
- return part.type === "step-start" ? index : lastIndex;
8700
- }, -1);
8701
- const lastStepToolInvocations = message.parts.slice(lastStepStartIndex + 1).filter(isToolUIPart);
8702
- return lastStepToolInvocations.length > 0 && lastStepToolInvocations.every((part) => part.state === "output-available");
8703
- }
8704
-
8705
8662
  // src/ui/chat.ts
8706
8663
  var AbstractChat = class {
8707
8664
  constructor({
8708
8665
  generateId: generateId3 = import_provider_utils27.generateId,
8709
8666
  id = generateId3(),
8710
8667
  transport = new DefaultChatTransport(),
8711
- maxSteps = 1,
8712
8668
  messageMetadataSchema,
8713
8669
  dataPartSchemas,
8714
8670
  state,
8715
8671
  onError,
8716
8672
  onToolCall,
8717
8673
  onFinish,
8718
- onData
8674
+ onData,
8675
+ sendAutomaticallyWhen
8719
8676
  }) {
8720
8677
  this.activeResponse = void 0;
8721
8678
  this.jobExecutor = new SerialJobExecutor();
@@ -8725,8 +8682,16 @@ var AbstractChat = class {
8725
8682
  *
8726
8683
  * If a messageId is provided, the message will be replaced.
8727
8684
  */
8728
- this.sendMessage = async (message, options = {}) => {
8729
- var _a16, _b, _c;
8685
+ this.sendMessage = async (message, options) => {
8686
+ var _a16, _b, _c, _d;
8687
+ if (message == null) {
8688
+ await this.makeRequest({
8689
+ trigger: "submit-message",
8690
+ messageId: (_a16 = this.lastMessage) == null ? void 0 : _a16.id,
8691
+ ...options
8692
+ });
8693
+ return;
8694
+ }
8730
8695
  let uiMessage;
8731
8696
  if ("text" in message || "files" in message) {
8732
8697
  const fileParts = Array.isArray(message.files) ? message.files : await convertFileListToFileUIParts(message.files);
@@ -8755,19 +8720,19 @@ var AbstractChat = class {
8755
8720
  this.state.replaceMessage(messageIndex, {
8756
8721
  ...uiMessage,
8757
8722
  id: message.messageId,
8758
- role: (_a16 = uiMessage.role) != null ? _a16 : "user",
8723
+ role: (_b = uiMessage.role) != null ? _b : "user",
8759
8724
  metadata: message.metadata
8760
8725
  });
8761
8726
  } else {
8762
8727
  this.state.pushMessage({
8763
8728
  ...uiMessage,
8764
- id: (_b = uiMessage.id) != null ? _b : this.generateId(),
8765
- role: (_c = uiMessage.role) != null ? _c : "user",
8729
+ id: (_c = uiMessage.id) != null ? _c : this.generateId(),
8730
+ role: (_d = uiMessage.role) != null ? _d : "user",
8766
8731
  metadata: message.metadata
8767
8732
  });
8768
8733
  }
8769
8734
  await this.makeRequest({
8770
- trigger: "submit-user-message",
8735
+ trigger: "submit-message",
8771
8736
  messageId: message.messageId,
8772
8737
  ...options
8773
8738
  });
@@ -8790,7 +8755,7 @@ var AbstractChat = class {
8790
8755
  this.messages[messageIndex].role === "assistant" ? messageIndex : messageIndex + 1
8791
8756
  );
8792
8757
  await this.makeRequest({
8793
- trigger: "regenerate-assistant-message",
8758
+ trigger: "regenerate-message",
8794
8759
  messageId,
8795
8760
  ...options
8796
8761
  });
@@ -8802,27 +8767,36 @@ var AbstractChat = class {
8802
8767
  await this.makeRequest({ trigger: "resume-stream", ...options });
8803
8768
  };
8804
8769
  this.addToolResult = async ({
8770
+ tool: tool3,
8805
8771
  toolCallId,
8806
8772
  output
8807
- }) => {
8808
- this.jobExecutor.run(async () => {
8809
- updateToolOutput({
8810
- messages: this.state.messages,
8811
- toolCallId,
8812
- output
8813
- });
8814
- this.messages = this.state.messages;
8815
- if (this.status === "submitted" || this.status === "streaming") {
8816
- return;
8817
- }
8818
- const lastMessage = this.lastMessage;
8819
- if (isAssistantMessageWithCompletedToolCalls(lastMessage)) {
8820
- this.makeRequest({
8821
- trigger: "submit-tool-result"
8822
- });
8823
- }
8773
+ }) => this.jobExecutor.run(async () => {
8774
+ var _a16, _b;
8775
+ const messages = this.state.messages;
8776
+ const lastMessage = messages[messages.length - 1];
8777
+ this.state.replaceMessage(messages.length - 1, {
8778
+ ...lastMessage,
8779
+ parts: lastMessage.parts.map(
8780
+ (part) => isToolUIPart(part) && part.toolCallId === toolCallId ? { ...part, state: "output-available", output } : part
8781
+ )
8824
8782
  });
8825
- };
8783
+ if (this.activeResponse) {
8784
+ this.activeResponse.state.message.parts = this.activeResponse.state.message.parts.map(
8785
+ (part) => isToolUIPart(part) && part.toolCallId === toolCallId ? {
8786
+ ...part,
8787
+ state: "output-available",
8788
+ output,
8789
+ errorText: void 0
8790
+ } : part
8791
+ );
8792
+ }
8793
+ if (this.status !== "streaming" && this.status !== "submitted" && ((_a16 = this.sendAutomaticallyWhen) == null ? void 0 : _a16.call(this, { messages }))) {
8794
+ await this.makeRequest({
8795
+ trigger: "submit-message",
8796
+ messageId: (_b = this.lastMessage) == null ? void 0 : _b.id
8797
+ });
8798
+ }
8799
+ });
8826
8800
  /**
8827
8801
  * Abort the current request immediately, keep the generated tokens if any.
8828
8802
  */
@@ -8835,7 +8809,6 @@ var AbstractChat = class {
8835
8809
  }
8836
8810
  };
8837
8811
  this.id = id;
8838
- this.maxSteps = maxSteps;
8839
8812
  this.transport = transport;
8840
8813
  this.generateId = generateId3;
8841
8814
  this.messageMetadataSchema = messageMetadataSchema;
@@ -8845,6 +8818,7 @@ var AbstractChat = class {
8845
8818
  this.onToolCall = onToolCall;
8846
8819
  this.onFinish = onFinish;
8847
8820
  this.onData = onData;
8821
+ this.sendAutomaticallyWhen = sendAutomaticallyWhen;
8848
8822
  }
8849
8823
  /**
8850
8824
  * Hook status:
@@ -8885,11 +8859,9 @@ var AbstractChat = class {
8885
8859
  body,
8886
8860
  messageId
8887
8861
  }) {
8888
- var _a16, _b;
8862
+ var _a16, _b, _c;
8889
8863
  this.setStatus({ status: "submitted", error: void 0 });
8890
- const messageCount = this.state.messages.length;
8891
8864
  const lastMessage = this.lastMessage;
8892
- const maxStep = (_a16 = lastMessage == null ? void 0 : lastMessage.parts.filter((part) => part.type === "step-start").length) != null ? _a16 : 0;
8893
8865
  try {
8894
8866
  const activeResponse = {
8895
8867
  state: createStreamingUIMessageState({
@@ -8960,7 +8932,7 @@ var AbstractChat = class {
8960
8932
  throw error;
8961
8933
  }
8962
8934
  });
8963
- (_b = this.onFinish) == null ? void 0 : _b.call(this, { message: activeResponse.state.message });
8935
+ (_a16 = this.onFinish) == null ? void 0 : _a16.call(this, { message: activeResponse.state.message });
8964
8936
  this.setStatus({ status: "ready" });
8965
8937
  } catch (err) {
8966
8938
  if (err.name === "AbortError") {
@@ -8974,37 +8946,17 @@ var AbstractChat = class {
8974
8946
  } finally {
8975
8947
  this.activeResponse = void 0;
8976
8948
  }
8977
- if (shouldResubmitMessages({
8978
- originalMaxToolInvocationStep: maxStep,
8979
- originalMessageCount: messageCount,
8980
- maxSteps: this.maxSteps,
8981
- messages: this.state.messages
8982
- })) {
8949
+ if ((_b = this.sendAutomaticallyWhen) == null ? void 0 : _b.call(this, { messages: this.state.messages })) {
8983
8950
  await this.makeRequest({
8951
+ trigger: "submit-message",
8952
+ messageId: (_c = this.lastMessage) == null ? void 0 : _c.id,
8984
8953
  metadata,
8985
8954
  headers,
8986
- body,
8987
- // secondary requests are triggered by automatic tool execution
8988
- trigger: "submit-tool-result"
8955
+ body
8989
8956
  });
8990
8957
  }
8991
8958
  }
8992
8959
  };
8993
- function updateToolOutput({
8994
- messages,
8995
- toolCallId,
8996
- output
8997
- }) {
8998
- const lastMessage = messages[messages.length - 1];
8999
- const toolPart = lastMessage.parts.find(
9000
- (part) => isToolUIPart(part) && part.toolCallId === toolCallId
9001
- );
9002
- if (toolPart == null) {
9003
- return;
9004
- }
9005
- toolPart.state = "output-available";
9006
- toolPart.output = output;
9007
- }
9008
8960
 
9009
8961
  // src/ui/convert-to-model-messages.ts
9010
8962
  function convertToModelMessages(messages, options) {
@@ -9177,6 +9129,24 @@ function convertToModelMessages(messages, options) {
9177
9129
  }
9178
9130
  var convertToCoreMessages = convertToModelMessages;
9179
9131
 
9132
+ // src/ui/last-assistant-message-is-complete-with-tool-calls.ts
9133
+ function lastAssistantMessageIsCompleteWithToolCalls({
9134
+ messages
9135
+ }) {
9136
+ const message = messages[messages.length - 1];
9137
+ if (!message) {
9138
+ return false;
9139
+ }
9140
+ if (message.role !== "assistant") {
9141
+ return false;
9142
+ }
9143
+ const lastStepStartIndex = message.parts.reduce((lastIndex, part, index) => {
9144
+ return part.type === "step-start" ? index : lastIndex;
9145
+ }, -1);
9146
+ const lastStepToolInvocations = message.parts.slice(lastStepStartIndex + 1).filter(isToolUIPart);
9147
+ return lastStepToolInvocations.length > 0 && lastStepToolInvocations.every((part) => part.state === "output-available");
9148
+ }
9149
+
9180
9150
  // src/ui/transform-text-to-ui-message-stream.ts
9181
9151
  function transformTextToUiMessageStream({
9182
9152
  stream
@@ -9301,10 +9271,13 @@ function createUIMessageStream({
9301
9271
  // src/ui-message-stream/read-ui-message-stream.ts
9302
9272
  function readUIMessageStream({
9303
9273
  message,
9304
- stream
9274
+ stream,
9275
+ onError,
9276
+ terminateOnError = false
9305
9277
  }) {
9306
9278
  var _a16;
9307
9279
  let controller;
9280
+ let hasErrored = false;
9308
9281
  const outputStream = new ReadableStream({
9309
9282
  start(controllerParam) {
9310
9283
  controller = controllerParam;
@@ -9314,6 +9287,13 @@ function readUIMessageStream({
9314
9287
  messageId: (_a16 = message == null ? void 0 : message.id) != null ? _a16 : "",
9315
9288
  lastMessage: message
9316
9289
  });
9290
+ const handleError = (error) => {
9291
+ onError == null ? void 0 : onError(error);
9292
+ if (!hasErrored && terminateOnError) {
9293
+ hasErrored = true;
9294
+ controller == null ? void 0 : controller.error(error);
9295
+ }
9296
+ };
9317
9297
  consumeStream({
9318
9298
  stream: processUIMessageStream({
9319
9299
  stream,
@@ -9325,12 +9305,13 @@ function readUIMessageStream({
9325
9305
  }
9326
9306
  });
9327
9307
  },
9328
- onError: (error) => {
9329
- throw error;
9330
- }
9331
- })
9308
+ onError: handleError
9309
+ }),
9310
+ onError: handleError
9332
9311
  }).finally(() => {
9333
- controller == null ? void 0 : controller.close();
9312
+ if (!hasErrored) {
9313
+ controller == null ? void 0 : controller.close();
9314
+ }
9334
9315
  });
9335
9316
  return createAsyncIterableStream(outputStream);
9336
9317
  }
@@ -9410,6 +9391,7 @@ function readUIMessageStream({
9410
9391
  isDeepEqualData,
9411
9392
  isToolUIPart,
9412
9393
  jsonSchema,
9394
+ lastAssistantMessageIsCompleteWithToolCalls,
9413
9395
  modelMessageSchema,
9414
9396
  parsePartialJson,
9415
9397
  pipeTextStreamToResponse,