ai 5.0.0-beta.25 → 5.0.0-beta.26

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
@@ -3081,11 +3081,11 @@ function createStreamingUIMessageState({
3081
3081
  }
3082
3082
  function processUIMessageStream({
3083
3083
  stream,
3084
- onToolCall,
3085
3084
  messageMetadataSchema,
3086
3085
  dataPartSchemas,
3087
3086
  runUpdateMessageJob,
3088
3087
  onError,
3088
+ onToolCall,
3089
3089
  onData
3090
3090
  }) {
3091
3091
  return stream.pipeThrough(
@@ -3264,19 +3264,9 @@ function processUIMessageStream({
3264
3264
  });
3265
3265
  write();
3266
3266
  if (onToolCall && !chunk.providerExecuted) {
3267
- const result = await onToolCall({
3267
+ await onToolCall({
3268
3268
  toolCall: chunk
3269
3269
  });
3270
- if (result != null) {
3271
- updateToolInvocationPart({
3272
- toolCallId: chunk.toolCallId,
3273
- toolName: chunk.toolName,
3274
- state: "output-available",
3275
- input: chunk.input,
3276
- output: result
3277
- });
3278
- write();
3279
- }
3280
3270
  }
3281
3271
  break;
3282
3272
  }
@@ -4001,7 +3991,7 @@ function createOutputTransformStream(output) {
4001
3991
  }) {
4002
3992
  controller.enqueue({
4003
3993
  part: {
4004
- type: "text",
3994
+ type: "text-delta",
4005
3995
  id: firstTextChunkId,
4006
3996
  text: textChunk
4007
3997
  },
@@ -4014,7 +4004,7 @@ function createOutputTransformStream(output) {
4014
4004
  if (chunk.type === "finish-step" && textChunk.length > 0) {
4015
4005
  publishTextChunk({ controller });
4016
4006
  }
4017
- if (chunk.type !== "text" && chunk.type !== "text-start" && chunk.type !== "text-end") {
4007
+ if (chunk.type !== "text-delta" && chunk.type !== "text-start" && chunk.type !== "text-end") {
4018
4008
  controller.enqueue({ part: chunk, partialOutput: void 0 });
4019
4009
  return;
4020
4010
  }
@@ -4099,7 +4089,7 @@ var DefaultStreamTextResult = class {
4099
4089
  var _a16, _b, _c;
4100
4090
  controller.enqueue(chunk);
4101
4091
  const { part } = chunk;
4102
- 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") {
4092
+ 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") {
4103
4093
  await (onChunk == null ? void 0 : onChunk({ chunk: part }));
4104
4094
  }
4105
4095
  if (part.type === "error") {
@@ -4113,7 +4103,7 @@ var DefaultStreamTextResult = class {
4113
4103
  };
4114
4104
  recordedContent.push(activeTextContent[part.id]);
4115
4105
  }
4116
- if (part.type === "text") {
4106
+ if (part.type === "text-delta") {
4117
4107
  const activeText = activeTextContent[part.id];
4118
4108
  if (activeText == null) {
4119
4109
  controller.enqueue({
@@ -4139,7 +4129,7 @@ var DefaultStreamTextResult = class {
4139
4129
  };
4140
4130
  recordedContent.push(activeReasoningContent[part.id]);
4141
4131
  }
4142
- if (part.type === "reasoning") {
4132
+ if (part.type === "reasoning-delta") {
4143
4133
  const activeReasoning = activeReasoningContent[part.id];
4144
4134
  if (activeReasoning == null) {
4145
4135
  controller.enqueue({
@@ -4505,7 +4495,7 @@ var DefaultStreamTextResult = class {
4505
4495
  case "text-delta": {
4506
4496
  if (chunk.delta.length > 0) {
4507
4497
  controller.enqueue({
4508
- type: "text",
4498
+ type: "text-delta",
4509
4499
  id: chunk.id,
4510
4500
  text: chunk.delta,
4511
4501
  providerMetadata: chunk.providerMetadata
@@ -4521,7 +4511,7 @@ var DefaultStreamTextResult = class {
4521
4511
  }
4522
4512
  case "reasoning-delta": {
4523
4513
  controller.enqueue({
4524
- type: "reasoning",
4514
+ type: "reasoning-delta",
4525
4515
  id: chunk.id,
4526
4516
  text: chunk.delta,
4527
4517
  providerMetadata: chunk.providerMetadata
@@ -4807,7 +4797,7 @@ var DefaultStreamTextResult = class {
4807
4797
  this.teeStream().pipeThrough(
4808
4798
  new TransformStream({
4809
4799
  transform({ part }, controller) {
4810
- if (part.type === "text") {
4800
+ if (part.type === "text-delta") {
4811
4801
  controller.enqueue(part.text);
4812
4802
  }
4813
4803
  }
@@ -4882,7 +4872,7 @@ var DefaultStreamTextResult = class {
4882
4872
  });
4883
4873
  break;
4884
4874
  }
4885
- case "text": {
4875
+ case "text-delta": {
4886
4876
  controller.enqueue({
4887
4877
  type: "text-delta",
4888
4878
  id: part.id,
@@ -4907,7 +4897,7 @@ var DefaultStreamTextResult = class {
4907
4897
  });
4908
4898
  break;
4909
4899
  }
4910
- case "reasoning": {
4900
+ case "reasoning-delta": {
4911
4901
  if (sendReasoning) {
4912
4902
  controller.enqueue({
4913
4903
  type: "reasoning-delta",
@@ -7148,23 +7138,23 @@ function smoothStream({
7148
7138
  let id = "";
7149
7139
  return new TransformStream({
7150
7140
  async transform(chunk, controller) {
7151
- if (chunk.type !== "text") {
7141
+ if (chunk.type !== "text-delta") {
7152
7142
  if (buffer.length > 0) {
7153
- controller.enqueue({ type: "text", text: buffer, id });
7143
+ controller.enqueue({ type: "text-delta", text: buffer, id });
7154
7144
  buffer = "";
7155
7145
  }
7156
7146
  controller.enqueue(chunk);
7157
7147
  return;
7158
7148
  }
7159
7149
  if (chunk.id !== id && buffer.length > 0) {
7160
- controller.enqueue({ type: "text", text: buffer, id });
7150
+ controller.enqueue({ type: "text-delta", text: buffer, id });
7161
7151
  buffer = "";
7162
7152
  }
7163
7153
  buffer += chunk.text;
7164
7154
  id = chunk.id;
7165
7155
  let match;
7166
7156
  while ((match = detectChunk(buffer)) != null) {
7167
- controller.enqueue({ type: "text", text: match, id });
7157
+ controller.enqueue({ type: "text-delta", text: match, id });
7168
7158
  buffer = buffer.slice(match.length);
7169
7159
  await delay2(delayInMs);
7170
7160
  }
@@ -8631,47 +8621,12 @@ var DefaultChatTransport = class extends HttpChatTransport {
8631
8621
  }
8632
8622
  };
8633
8623
 
8634
- // src/ui/should-resubmit-messages.ts
8635
- function shouldResubmitMessages({
8636
- originalMaxToolInvocationStep,
8637
- originalMessageCount,
8638
- maxSteps,
8639
- messages
8640
- }) {
8641
- const lastMessage = messages[messages.length - 1];
8642
- const lastMessageStepStartCount = lastMessage.parts.filter(
8643
- (part) => part.type === "step-start"
8644
- ).length;
8645
- return (
8646
- // check if the feature is enabled:
8647
- maxSteps > 1 && // ensure there is a last message:
8648
- lastMessage != null && // ensure we actually have new steps (to prevent infinite loops in case of errors):
8649
- (messages.length > originalMessageCount || lastMessageStepStartCount !== originalMaxToolInvocationStep) && // check that next step is possible:
8650
- isAssistantMessageWithCompletedToolCalls(lastMessage) && // limit the number of automatic steps:
8651
- lastMessageStepStartCount < maxSteps
8652
- );
8653
- }
8654
- function isAssistantMessageWithCompletedToolCalls(message) {
8655
- if (!message) {
8656
- return false;
8657
- }
8658
- if (message.role !== "assistant") {
8659
- return false;
8660
- }
8661
- const lastStepStartIndex = message.parts.reduce((lastIndex, part, index) => {
8662
- return part.type === "step-start" ? index : lastIndex;
8663
- }, -1);
8664
- const lastStepToolInvocations = message.parts.slice(lastStepStartIndex + 1).filter(isToolUIPart);
8665
- return lastStepToolInvocations.length > 0 && lastStepToolInvocations.every((part) => part.state === "output-available");
8666
- }
8667
-
8668
8624
  // src/ui/chat.ts
8669
8625
  var AbstractChat = class {
8670
8626
  constructor({
8671
8627
  generateId: generateId3 = generateIdFunc,
8672
8628
  id = generateId3(),
8673
8629
  transport = new DefaultChatTransport(),
8674
- maxSteps = 1,
8675
8630
  messageMetadataSchema,
8676
8631
  dataPartSchemas,
8677
8632
  state,
@@ -8688,8 +8643,16 @@ var AbstractChat = class {
8688
8643
  *
8689
8644
  * If a messageId is provided, the message will be replaced.
8690
8645
  */
8691
- this.sendMessage = async (message, options = {}) => {
8692
- var _a16, _b, _c;
8646
+ this.sendMessage = async (message, options) => {
8647
+ var _a16, _b, _c, _d;
8648
+ if (message == null) {
8649
+ await this.makeRequest({
8650
+ trigger: "submit-message",
8651
+ messageId: (_a16 = this.lastMessage) == null ? void 0 : _a16.id,
8652
+ ...options
8653
+ });
8654
+ return;
8655
+ }
8693
8656
  let uiMessage;
8694
8657
  if ("text" in message || "files" in message) {
8695
8658
  const fileParts = Array.isArray(message.files) ? message.files : await convertFileListToFileUIParts(message.files);
@@ -8718,19 +8681,19 @@ var AbstractChat = class {
8718
8681
  this.state.replaceMessage(messageIndex, {
8719
8682
  ...uiMessage,
8720
8683
  id: message.messageId,
8721
- role: (_a16 = uiMessage.role) != null ? _a16 : "user",
8684
+ role: (_b = uiMessage.role) != null ? _b : "user",
8722
8685
  metadata: message.metadata
8723
8686
  });
8724
8687
  } else {
8725
8688
  this.state.pushMessage({
8726
8689
  ...uiMessage,
8727
- id: (_b = uiMessage.id) != null ? _b : this.generateId(),
8728
- role: (_c = uiMessage.role) != null ? _c : "user",
8690
+ id: (_c = uiMessage.id) != null ? _c : this.generateId(),
8691
+ role: (_d = uiMessage.role) != null ? _d : "user",
8729
8692
  metadata: message.metadata
8730
8693
  });
8731
8694
  }
8732
8695
  await this.makeRequest({
8733
- trigger: "submit-user-message",
8696
+ trigger: "submit-message",
8734
8697
  messageId: message.messageId,
8735
8698
  ...options
8736
8699
  });
@@ -8753,7 +8716,7 @@ var AbstractChat = class {
8753
8716
  this.messages[messageIndex].role === "assistant" ? messageIndex : messageIndex + 1
8754
8717
  );
8755
8718
  await this.makeRequest({
8756
- trigger: "regenerate-assistant-message",
8719
+ trigger: "regenerate-message",
8757
8720
  messageId,
8758
8721
  ...options
8759
8722
  });
@@ -8765,27 +8728,38 @@ var AbstractChat = class {
8765
8728
  await this.makeRequest({ trigger: "resume-stream", ...options });
8766
8729
  };
8767
8730
  this.addToolResult = async ({
8731
+ tool: tool3,
8768
8732
  toolCallId,
8769
8733
  output
8770
8734
  }) => {
8771
8735
  this.jobExecutor.run(async () => {
8772
- updateToolOutput({
8773
- messages: this.state.messages,
8774
- toolCallId,
8775
- output
8736
+ const messages = this.state.messages;
8737
+ const lastMessage = messages[messages.length - 1];
8738
+ this.state.replaceMessage(messages.length - 1, {
8739
+ ...lastMessage,
8740
+ parts: lastMessage.parts.map(
8741
+ (part) => isToolUIPart(part) && part.toolCallId === toolCallId ? { ...part, state: "output-available", output } : part
8742
+ )
8776
8743
  });
8777
- this.messages = this.state.messages;
8778
- if (this.status === "submitted" || this.status === "streaming") {
8779
- return;
8780
- }
8781
- const lastMessage = this.lastMessage;
8782
- if (isAssistantMessageWithCompletedToolCalls(lastMessage)) {
8783
- this.makeRequest({
8784
- trigger: "submit-tool-result"
8785
- });
8744
+ if (this.activeResponse) {
8745
+ this.activeResponse.state.message.parts = this.activeResponse.state.message.parts.map(
8746
+ (part) => isToolUIPart(part) && part.toolCallId === toolCallId ? {
8747
+ ...part,
8748
+ state: "output-available",
8749
+ output,
8750
+ errorText: void 0
8751
+ } : part
8752
+ );
8786
8753
  }
8787
8754
  });
8788
8755
  };
8756
+ /**
8757
+ * Checks if the assistant message can be submitted, i.e. if it
8758
+ * has tool calls and all tool calls have results.
8759
+ *
8760
+ * @returns {boolean} True if the assistant message can be submitted, false otherwise.
8761
+ */
8762
+ this.canAssistantMessageBeSubmitted = () => isAssistantMessageWithCompletedToolCalls(this.lastMessage);
8789
8763
  /**
8790
8764
  * Abort the current request immediately, keep the generated tokens if any.
8791
8765
  */
@@ -8798,7 +8772,6 @@ var AbstractChat = class {
8798
8772
  }
8799
8773
  };
8800
8774
  this.id = id;
8801
- this.maxSteps = maxSteps;
8802
8775
  this.transport = transport;
8803
8776
  this.generateId = generateId3;
8804
8777
  this.messageMetadataSchema = messageMetadataSchema;
@@ -8848,11 +8821,9 @@ var AbstractChat = class {
8848
8821
  body,
8849
8822
  messageId
8850
8823
  }) {
8851
- var _a16, _b;
8824
+ var _a16;
8852
8825
  this.setStatus({ status: "submitted", error: void 0 });
8853
- const messageCount = this.state.messages.length;
8854
8826
  const lastMessage = this.lastMessage;
8855
- const maxStep = (_a16 = lastMessage == null ? void 0 : lastMessage.parts.filter((part) => part.type === "step-start").length) != null ? _a16 : 0;
8856
8827
  try {
8857
8828
  const activeResponse = {
8858
8829
  state: createStreamingUIMessageState({
@@ -8923,7 +8894,7 @@ var AbstractChat = class {
8923
8894
  throw error;
8924
8895
  }
8925
8896
  });
8926
- (_b = this.onFinish) == null ? void 0 : _b.call(this, { message: activeResponse.state.message });
8897
+ (_a16 = this.onFinish) == null ? void 0 : _a16.call(this, { message: activeResponse.state.message });
8927
8898
  this.setStatus({ status: "ready" });
8928
8899
  } catch (err) {
8929
8900
  if (err.name === "AbortError") {
@@ -8937,36 +8908,20 @@ var AbstractChat = class {
8937
8908
  } finally {
8938
8909
  this.activeResponse = void 0;
8939
8910
  }
8940
- if (shouldResubmitMessages({
8941
- originalMaxToolInvocationStep: maxStep,
8942
- originalMessageCount: messageCount,
8943
- maxSteps: this.maxSteps,
8944
- messages: this.state.messages
8945
- })) {
8946
- await this.makeRequest({
8947
- metadata,
8948
- headers,
8949
- body,
8950
- // secondary requests are triggered by automatic tool execution
8951
- trigger: "submit-tool-result"
8952
- });
8953
- }
8954
8911
  }
8955
8912
  };
8956
- function updateToolOutput({
8957
- messages,
8958
- toolCallId,
8959
- output
8960
- }) {
8961
- const lastMessage = messages[messages.length - 1];
8962
- const toolPart = lastMessage.parts.find(
8963
- (part) => isToolUIPart(part) && part.toolCallId === toolCallId
8964
- );
8965
- if (toolPart == null) {
8966
- return;
8913
+ function isAssistantMessageWithCompletedToolCalls(message) {
8914
+ if (!message) {
8915
+ return false;
8967
8916
  }
8968
- toolPart.state = "output-available";
8969
- toolPart.output = output;
8917
+ if (message.role !== "assistant") {
8918
+ return false;
8919
+ }
8920
+ const lastStepStartIndex = message.parts.reduce((lastIndex, part, index) => {
8921
+ return part.type === "step-start" ? index : lastIndex;
8922
+ }, -1);
8923
+ const lastStepToolInvocations = message.parts.slice(lastStepStartIndex + 1).filter(isToolUIPart);
8924
+ return lastStepToolInvocations.length > 0 && lastStepToolInvocations.every((part) => part.state === "output-available");
8970
8925
  }
8971
8926
 
8972
8927
  // src/ui/convert-to-model-messages.ts
@@ -9267,10 +9222,13 @@ function createUIMessageStream({
9267
9222
  // src/ui-message-stream/read-ui-message-stream.ts
9268
9223
  function readUIMessageStream({
9269
9224
  message,
9270
- stream
9225
+ stream,
9226
+ onError,
9227
+ terminateOnError = false
9271
9228
  }) {
9272
9229
  var _a16;
9273
9230
  let controller;
9231
+ let hasErrored = false;
9274
9232
  const outputStream = new ReadableStream({
9275
9233
  start(controllerParam) {
9276
9234
  controller = controllerParam;
@@ -9280,6 +9238,13 @@ function readUIMessageStream({
9280
9238
  messageId: (_a16 = message == null ? void 0 : message.id) != null ? _a16 : "",
9281
9239
  lastMessage: message
9282
9240
  });
9241
+ const handleError = (error) => {
9242
+ onError == null ? void 0 : onError(error);
9243
+ if (!hasErrored && terminateOnError) {
9244
+ hasErrored = true;
9245
+ controller == null ? void 0 : controller.error(error);
9246
+ }
9247
+ };
9283
9248
  consumeStream({
9284
9249
  stream: processUIMessageStream({
9285
9250
  stream,
@@ -9291,12 +9256,13 @@ function readUIMessageStream({
9291
9256
  }
9292
9257
  });
9293
9258
  },
9294
- onError: (error) => {
9295
- throw error;
9296
- }
9297
- })
9259
+ onError: handleError
9260
+ }),
9261
+ onError: handleError
9298
9262
  }).finally(() => {
9299
- controller == null ? void 0 : controller.close();
9263
+ if (!hasErrored) {
9264
+ controller == null ? void 0 : controller.close();
9265
+ }
9300
9266
  });
9301
9267
  return createAsyncIterableStream(outputStream);
9302
9268
  }