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.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,54 +8621,20 @@ 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,
8678
8633
  onError,
8679
8634
  onToolCall,
8680
8635
  onFinish,
8681
- onData
8636
+ onData,
8637
+ sendAutomaticallyWhen
8682
8638
  }) {
8683
8639
  this.activeResponse = void 0;
8684
8640
  this.jobExecutor = new SerialJobExecutor();
@@ -8688,8 +8644,16 @@ var AbstractChat = class {
8688
8644
  *
8689
8645
  * If a messageId is provided, the message will be replaced.
8690
8646
  */
8691
- this.sendMessage = async (message, options = {}) => {
8692
- var _a16, _b, _c;
8647
+ this.sendMessage = async (message, options) => {
8648
+ var _a16, _b, _c, _d;
8649
+ if (message == null) {
8650
+ await this.makeRequest({
8651
+ trigger: "submit-message",
8652
+ messageId: (_a16 = this.lastMessage) == null ? void 0 : _a16.id,
8653
+ ...options
8654
+ });
8655
+ return;
8656
+ }
8693
8657
  let uiMessage;
8694
8658
  if ("text" in message || "files" in message) {
8695
8659
  const fileParts = Array.isArray(message.files) ? message.files : await convertFileListToFileUIParts(message.files);
@@ -8718,19 +8682,19 @@ var AbstractChat = class {
8718
8682
  this.state.replaceMessage(messageIndex, {
8719
8683
  ...uiMessage,
8720
8684
  id: message.messageId,
8721
- role: (_a16 = uiMessage.role) != null ? _a16 : "user",
8685
+ role: (_b = uiMessage.role) != null ? _b : "user",
8722
8686
  metadata: message.metadata
8723
8687
  });
8724
8688
  } else {
8725
8689
  this.state.pushMessage({
8726
8690
  ...uiMessage,
8727
- id: (_b = uiMessage.id) != null ? _b : this.generateId(),
8728
- role: (_c = uiMessage.role) != null ? _c : "user",
8691
+ id: (_c = uiMessage.id) != null ? _c : this.generateId(),
8692
+ role: (_d = uiMessage.role) != null ? _d : "user",
8729
8693
  metadata: message.metadata
8730
8694
  });
8731
8695
  }
8732
8696
  await this.makeRequest({
8733
- trigger: "submit-user-message",
8697
+ trigger: "submit-message",
8734
8698
  messageId: message.messageId,
8735
8699
  ...options
8736
8700
  });
@@ -8753,7 +8717,7 @@ var AbstractChat = class {
8753
8717
  this.messages[messageIndex].role === "assistant" ? messageIndex : messageIndex + 1
8754
8718
  );
8755
8719
  await this.makeRequest({
8756
- trigger: "regenerate-assistant-message",
8720
+ trigger: "regenerate-message",
8757
8721
  messageId,
8758
8722
  ...options
8759
8723
  });
@@ -8765,27 +8729,36 @@ var AbstractChat = class {
8765
8729
  await this.makeRequest({ trigger: "resume-stream", ...options });
8766
8730
  };
8767
8731
  this.addToolResult = async ({
8732
+ tool: tool3,
8768
8733
  toolCallId,
8769
8734
  output
8770
- }) => {
8771
- this.jobExecutor.run(async () => {
8772
- updateToolOutput({
8773
- messages: this.state.messages,
8774
- toolCallId,
8775
- output
8776
- });
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
- });
8786
- }
8735
+ }) => this.jobExecutor.run(async () => {
8736
+ var _a16, _b;
8737
+ const messages = this.state.messages;
8738
+ const lastMessage = messages[messages.length - 1];
8739
+ this.state.replaceMessage(messages.length - 1, {
8740
+ ...lastMessage,
8741
+ parts: lastMessage.parts.map(
8742
+ (part) => isToolUIPart(part) && part.toolCallId === toolCallId ? { ...part, state: "output-available", output } : part
8743
+ )
8787
8744
  });
8788
- };
8745
+ if (this.activeResponse) {
8746
+ this.activeResponse.state.message.parts = this.activeResponse.state.message.parts.map(
8747
+ (part) => isToolUIPart(part) && part.toolCallId === toolCallId ? {
8748
+ ...part,
8749
+ state: "output-available",
8750
+ output,
8751
+ errorText: void 0
8752
+ } : part
8753
+ );
8754
+ }
8755
+ if (this.status !== "streaming" && this.status !== "submitted" && ((_a16 = this.sendAutomaticallyWhen) == null ? void 0 : _a16.call(this, { messages }))) {
8756
+ await this.makeRequest({
8757
+ trigger: "submit-message",
8758
+ messageId: (_b = this.lastMessage) == null ? void 0 : _b.id
8759
+ });
8760
+ }
8761
+ });
8789
8762
  /**
8790
8763
  * Abort the current request immediately, keep the generated tokens if any.
8791
8764
  */
@@ -8798,7 +8771,6 @@ var AbstractChat = class {
8798
8771
  }
8799
8772
  };
8800
8773
  this.id = id;
8801
- this.maxSteps = maxSteps;
8802
8774
  this.transport = transport;
8803
8775
  this.generateId = generateId3;
8804
8776
  this.messageMetadataSchema = messageMetadataSchema;
@@ -8808,6 +8780,7 @@ var AbstractChat = class {
8808
8780
  this.onToolCall = onToolCall;
8809
8781
  this.onFinish = onFinish;
8810
8782
  this.onData = onData;
8783
+ this.sendAutomaticallyWhen = sendAutomaticallyWhen;
8811
8784
  }
8812
8785
  /**
8813
8786
  * Hook status:
@@ -8848,11 +8821,9 @@ var AbstractChat = class {
8848
8821
  body,
8849
8822
  messageId
8850
8823
  }) {
8851
- var _a16, _b;
8824
+ var _a16, _b, _c;
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,37 +8908,17 @@ 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
- })) {
8911
+ if ((_b = this.sendAutomaticallyWhen) == null ? void 0 : _b.call(this, { messages: this.state.messages })) {
8946
8912
  await this.makeRequest({
8913
+ trigger: "submit-message",
8914
+ messageId: (_c = this.lastMessage) == null ? void 0 : _c.id,
8947
8915
  metadata,
8948
8916
  headers,
8949
- body,
8950
- // secondary requests are triggered by automatic tool execution
8951
- trigger: "submit-tool-result"
8917
+ body
8952
8918
  });
8953
8919
  }
8954
8920
  }
8955
8921
  };
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;
8967
- }
8968
- toolPart.state = "output-available";
8969
- toolPart.output = output;
8970
- }
8971
8922
 
8972
8923
  // src/ui/convert-to-model-messages.ts
8973
8924
  function convertToModelMessages(messages, options) {
@@ -9140,6 +9091,24 @@ function convertToModelMessages(messages, options) {
9140
9091
  }
9141
9092
  var convertToCoreMessages = convertToModelMessages;
9142
9093
 
9094
+ // src/ui/last-assistant-message-is-complete-with-tool-calls.ts
9095
+ function lastAssistantMessageIsCompleteWithToolCalls({
9096
+ messages
9097
+ }) {
9098
+ const message = messages[messages.length - 1];
9099
+ if (!message) {
9100
+ return false;
9101
+ }
9102
+ if (message.role !== "assistant") {
9103
+ return false;
9104
+ }
9105
+ const lastStepStartIndex = message.parts.reduce((lastIndex, part, index) => {
9106
+ return part.type === "step-start" ? index : lastIndex;
9107
+ }, -1);
9108
+ const lastStepToolInvocations = message.parts.slice(lastStepStartIndex + 1).filter(isToolUIPart);
9109
+ return lastStepToolInvocations.length > 0 && lastStepToolInvocations.every((part) => part.state === "output-available");
9110
+ }
9111
+
9143
9112
  // src/ui/transform-text-to-ui-message-stream.ts
9144
9113
  function transformTextToUiMessageStream({
9145
9114
  stream
@@ -9267,10 +9236,13 @@ function createUIMessageStream({
9267
9236
  // src/ui-message-stream/read-ui-message-stream.ts
9268
9237
  function readUIMessageStream({
9269
9238
  message,
9270
- stream
9239
+ stream,
9240
+ onError,
9241
+ terminateOnError = false
9271
9242
  }) {
9272
9243
  var _a16;
9273
9244
  let controller;
9245
+ let hasErrored = false;
9274
9246
  const outputStream = new ReadableStream({
9275
9247
  start(controllerParam) {
9276
9248
  controller = controllerParam;
@@ -9280,6 +9252,13 @@ function readUIMessageStream({
9280
9252
  messageId: (_a16 = message == null ? void 0 : message.id) != null ? _a16 : "",
9281
9253
  lastMessage: message
9282
9254
  });
9255
+ const handleError = (error) => {
9256
+ onError == null ? void 0 : onError(error);
9257
+ if (!hasErrored && terminateOnError) {
9258
+ hasErrored = true;
9259
+ controller == null ? void 0 : controller.error(error);
9260
+ }
9261
+ };
9283
9262
  consumeStream({
9284
9263
  stream: processUIMessageStream({
9285
9264
  stream,
@@ -9291,12 +9270,13 @@ function readUIMessageStream({
9291
9270
  }
9292
9271
  });
9293
9272
  },
9294
- onError: (error) => {
9295
- throw error;
9296
- }
9297
- })
9273
+ onError: handleError
9274
+ }),
9275
+ onError: handleError
9298
9276
  }).finally(() => {
9299
- controller == null ? void 0 : controller.close();
9277
+ if (!hasErrored) {
9278
+ controller == null ? void 0 : controller.close();
9279
+ }
9300
9280
  });
9301
9281
  return createAsyncIterableStream(outputStream);
9302
9282
  }
@@ -9375,6 +9355,7 @@ export {
9375
9355
  isDeepEqualData,
9376
9356
  isToolUIPart,
9377
9357
  jsonSchema2 as jsonSchema,
9358
+ lastAssistantMessageIsCompleteWithToolCalls,
9378
9359
  modelMessageSchema,
9379
9360
  parsePartialJson,
9380
9361
  pipeTextStreamToResponse,