ai 5.0.0-beta.24 → 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.js CHANGED
@@ -3146,11 +3146,11 @@ function createStreamingUIMessageState({
3146
3146
  }
3147
3147
  function processUIMessageStream({
3148
3148
  stream,
3149
- onToolCall,
3150
3149
  messageMetadataSchema,
3151
3150
  dataPartSchemas,
3152
3151
  runUpdateMessageJob,
3153
3152
  onError,
3153
+ onToolCall,
3154
3154
  onData
3155
3155
  }) {
3156
3156
  return stream.pipeThrough(
@@ -3329,19 +3329,9 @@ function processUIMessageStream({
3329
3329
  });
3330
3330
  write();
3331
3331
  if (onToolCall && !chunk.providerExecuted) {
3332
- const result = await onToolCall({
3332
+ await onToolCall({
3333
3333
  toolCall: chunk
3334
3334
  });
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
3335
  }
3346
3336
  break;
3347
3337
  }
@@ -4066,7 +4056,7 @@ function createOutputTransformStream(output) {
4066
4056
  }) {
4067
4057
  controller.enqueue({
4068
4058
  part: {
4069
- type: "text",
4059
+ type: "text-delta",
4070
4060
  id: firstTextChunkId,
4071
4061
  text: textChunk
4072
4062
  },
@@ -4079,7 +4069,7 @@ function createOutputTransformStream(output) {
4079
4069
  if (chunk.type === "finish-step" && textChunk.length > 0) {
4080
4070
  publishTextChunk({ controller });
4081
4071
  }
4082
- if (chunk.type !== "text" && chunk.type !== "text-start" && chunk.type !== "text-end") {
4072
+ if (chunk.type !== "text-delta" && chunk.type !== "text-start" && chunk.type !== "text-end") {
4083
4073
  controller.enqueue({ part: chunk, partialOutput: void 0 });
4084
4074
  return;
4085
4075
  }
@@ -4164,7 +4154,7 @@ var DefaultStreamTextResult = class {
4164
4154
  var _a16, _b, _c;
4165
4155
  controller.enqueue(chunk);
4166
4156
  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") {
4157
+ 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
4158
  await (onChunk == null ? void 0 : onChunk({ chunk: part }));
4169
4159
  }
4170
4160
  if (part.type === "error") {
@@ -4178,7 +4168,7 @@ var DefaultStreamTextResult = class {
4178
4168
  };
4179
4169
  recordedContent.push(activeTextContent[part.id]);
4180
4170
  }
4181
- if (part.type === "text") {
4171
+ if (part.type === "text-delta") {
4182
4172
  const activeText = activeTextContent[part.id];
4183
4173
  if (activeText == null) {
4184
4174
  controller.enqueue({
@@ -4204,7 +4194,7 @@ var DefaultStreamTextResult = class {
4204
4194
  };
4205
4195
  recordedContent.push(activeReasoningContent[part.id]);
4206
4196
  }
4207
- if (part.type === "reasoning") {
4197
+ if (part.type === "reasoning-delta") {
4208
4198
  const activeReasoning = activeReasoningContent[part.id];
4209
4199
  if (activeReasoning == null) {
4210
4200
  controller.enqueue({
@@ -4570,7 +4560,7 @@ var DefaultStreamTextResult = class {
4570
4560
  case "text-delta": {
4571
4561
  if (chunk.delta.length > 0) {
4572
4562
  controller.enqueue({
4573
- type: "text",
4563
+ type: "text-delta",
4574
4564
  id: chunk.id,
4575
4565
  text: chunk.delta,
4576
4566
  providerMetadata: chunk.providerMetadata
@@ -4586,7 +4576,7 @@ var DefaultStreamTextResult = class {
4586
4576
  }
4587
4577
  case "reasoning-delta": {
4588
4578
  controller.enqueue({
4589
- type: "reasoning",
4579
+ type: "reasoning-delta",
4590
4580
  id: chunk.id,
4591
4581
  text: chunk.delta,
4592
4582
  providerMetadata: chunk.providerMetadata
@@ -4872,7 +4862,7 @@ var DefaultStreamTextResult = class {
4872
4862
  this.teeStream().pipeThrough(
4873
4863
  new TransformStream({
4874
4864
  transform({ part }, controller) {
4875
- if (part.type === "text") {
4865
+ if (part.type === "text-delta") {
4876
4866
  controller.enqueue(part.text);
4877
4867
  }
4878
4868
  }
@@ -4947,7 +4937,7 @@ var DefaultStreamTextResult = class {
4947
4937
  });
4948
4938
  break;
4949
4939
  }
4950
- case "text": {
4940
+ case "text-delta": {
4951
4941
  controller.enqueue({
4952
4942
  type: "text-delta",
4953
4943
  id: part.id,
@@ -4972,7 +4962,7 @@ var DefaultStreamTextResult = class {
4972
4962
  });
4973
4963
  break;
4974
4964
  }
4975
- case "reasoning": {
4965
+ case "reasoning-delta": {
4976
4966
  if (sendReasoning) {
4977
4967
  controller.enqueue({
4978
4968
  type: "reasoning-delta",
@@ -7193,23 +7183,23 @@ function smoothStream({
7193
7183
  let id = "";
7194
7184
  return new TransformStream({
7195
7185
  async transform(chunk, controller) {
7196
- if (chunk.type !== "text") {
7186
+ if (chunk.type !== "text-delta") {
7197
7187
  if (buffer.length > 0) {
7198
- controller.enqueue({ type: "text", text: buffer, id });
7188
+ controller.enqueue({ type: "text-delta", text: buffer, id });
7199
7189
  buffer = "";
7200
7190
  }
7201
7191
  controller.enqueue(chunk);
7202
7192
  return;
7203
7193
  }
7204
7194
  if (chunk.id !== id && buffer.length > 0) {
7205
- controller.enqueue({ type: "text", text: buffer, id });
7195
+ controller.enqueue({ type: "text-delta", text: buffer, id });
7206
7196
  buffer = "";
7207
7197
  }
7208
7198
  buffer += chunk.text;
7209
7199
  id = chunk.id;
7210
7200
  let match;
7211
7201
  while ((match = detectChunk(buffer)) != null) {
7212
- controller.enqueue({ type: "text", text: match, id });
7202
+ controller.enqueue({ type: "text-delta", text: match, id });
7213
7203
  buffer = buffer.slice(match.length);
7214
7204
  await delay2(delayInMs);
7215
7205
  }
@@ -8668,47 +8658,12 @@ var DefaultChatTransport = class extends HttpChatTransport {
8668
8658
  }
8669
8659
  };
8670
8660
 
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
8661
  // src/ui/chat.ts
8706
8662
  var AbstractChat = class {
8707
8663
  constructor({
8708
8664
  generateId: generateId3 = import_provider_utils27.generateId,
8709
8665
  id = generateId3(),
8710
8666
  transport = new DefaultChatTransport(),
8711
- maxSteps = 1,
8712
8667
  messageMetadataSchema,
8713
8668
  dataPartSchemas,
8714
8669
  state,
@@ -8725,8 +8680,16 @@ var AbstractChat = class {
8725
8680
  *
8726
8681
  * If a messageId is provided, the message will be replaced.
8727
8682
  */
8728
- this.sendMessage = async (message, options = {}) => {
8729
- var _a16, _b, _c;
8683
+ this.sendMessage = async (message, options) => {
8684
+ var _a16, _b, _c, _d;
8685
+ if (message == null) {
8686
+ await this.makeRequest({
8687
+ trigger: "submit-message",
8688
+ messageId: (_a16 = this.lastMessage) == null ? void 0 : _a16.id,
8689
+ ...options
8690
+ });
8691
+ return;
8692
+ }
8730
8693
  let uiMessage;
8731
8694
  if ("text" in message || "files" in message) {
8732
8695
  const fileParts = Array.isArray(message.files) ? message.files : await convertFileListToFileUIParts(message.files);
@@ -8755,19 +8718,19 @@ var AbstractChat = class {
8755
8718
  this.state.replaceMessage(messageIndex, {
8756
8719
  ...uiMessage,
8757
8720
  id: message.messageId,
8758
- role: (_a16 = uiMessage.role) != null ? _a16 : "user",
8721
+ role: (_b = uiMessage.role) != null ? _b : "user",
8759
8722
  metadata: message.metadata
8760
8723
  });
8761
8724
  } else {
8762
8725
  this.state.pushMessage({
8763
8726
  ...uiMessage,
8764
- id: (_b = uiMessage.id) != null ? _b : this.generateId(),
8765
- role: (_c = uiMessage.role) != null ? _c : "user",
8727
+ id: (_c = uiMessage.id) != null ? _c : this.generateId(),
8728
+ role: (_d = uiMessage.role) != null ? _d : "user",
8766
8729
  metadata: message.metadata
8767
8730
  });
8768
8731
  }
8769
8732
  await this.makeRequest({
8770
- trigger: "submit-user-message",
8733
+ trigger: "submit-message",
8771
8734
  messageId: message.messageId,
8772
8735
  ...options
8773
8736
  });
@@ -8790,7 +8753,7 @@ var AbstractChat = class {
8790
8753
  this.messages[messageIndex].role === "assistant" ? messageIndex : messageIndex + 1
8791
8754
  );
8792
8755
  await this.makeRequest({
8793
- trigger: "regenerate-assistant-message",
8756
+ trigger: "regenerate-message",
8794
8757
  messageId,
8795
8758
  ...options
8796
8759
  });
@@ -8802,27 +8765,38 @@ var AbstractChat = class {
8802
8765
  await this.makeRequest({ trigger: "resume-stream", ...options });
8803
8766
  };
8804
8767
  this.addToolResult = async ({
8768
+ tool: tool3,
8805
8769
  toolCallId,
8806
8770
  output
8807
8771
  }) => {
8808
8772
  this.jobExecutor.run(async () => {
8809
- updateToolOutput({
8810
- messages: this.state.messages,
8811
- toolCallId,
8812
- output
8773
+ const messages = this.state.messages;
8774
+ const lastMessage = messages[messages.length - 1];
8775
+ this.state.replaceMessage(messages.length - 1, {
8776
+ ...lastMessage,
8777
+ parts: lastMessage.parts.map(
8778
+ (part) => isToolUIPart(part) && part.toolCallId === toolCallId ? { ...part, state: "output-available", output } : part
8779
+ )
8813
8780
  });
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
- });
8781
+ if (this.activeResponse) {
8782
+ this.activeResponse.state.message.parts = this.activeResponse.state.message.parts.map(
8783
+ (part) => isToolUIPart(part) && part.toolCallId === toolCallId ? {
8784
+ ...part,
8785
+ state: "output-available",
8786
+ output,
8787
+ errorText: void 0
8788
+ } : part
8789
+ );
8823
8790
  }
8824
8791
  });
8825
8792
  };
8793
+ /**
8794
+ * Checks if the assistant message can be submitted, i.e. if it
8795
+ * has tool calls and all tool calls have results.
8796
+ *
8797
+ * @returns {boolean} True if the assistant message can be submitted, false otherwise.
8798
+ */
8799
+ this.canAssistantMessageBeSubmitted = () => isAssistantMessageWithCompletedToolCalls(this.lastMessage);
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;
@@ -8885,11 +8858,9 @@ var AbstractChat = class {
8885
8858
  body,
8886
8859
  messageId
8887
8860
  }) {
8888
- var _a16, _b;
8861
+ var _a16;
8889
8862
  this.setStatus({ status: "submitted", error: void 0 });
8890
- const messageCount = this.state.messages.length;
8891
8863
  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
8864
  try {
8894
8865
  const activeResponse = {
8895
8866
  state: createStreamingUIMessageState({
@@ -8960,7 +8931,7 @@ var AbstractChat = class {
8960
8931
  throw error;
8961
8932
  }
8962
8933
  });
8963
- (_b = this.onFinish) == null ? void 0 : _b.call(this, { message: activeResponse.state.message });
8934
+ (_a16 = this.onFinish) == null ? void 0 : _a16.call(this, { message: activeResponse.state.message });
8964
8935
  this.setStatus({ status: "ready" });
8965
8936
  } catch (err) {
8966
8937
  if (err.name === "AbortError") {
@@ -8974,36 +8945,20 @@ var AbstractChat = class {
8974
8945
  } finally {
8975
8946
  this.activeResponse = void 0;
8976
8947
  }
8977
- if (shouldResubmitMessages({
8978
- originalMaxToolInvocationStep: maxStep,
8979
- originalMessageCount: messageCount,
8980
- maxSteps: this.maxSteps,
8981
- messages: this.state.messages
8982
- })) {
8983
- await this.makeRequest({
8984
- metadata,
8985
- headers,
8986
- body,
8987
- // secondary requests are triggered by automatic tool execution
8988
- trigger: "submit-tool-result"
8989
- });
8990
- }
8991
8948
  }
8992
8949
  };
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;
8950
+ function isAssistantMessageWithCompletedToolCalls(message) {
8951
+ if (!message) {
8952
+ return false;
9004
8953
  }
9005
- toolPart.state = "output-available";
9006
- toolPart.output = output;
8954
+ if (message.role !== "assistant") {
8955
+ return false;
8956
+ }
8957
+ const lastStepStartIndex = message.parts.reduce((lastIndex, part, index) => {
8958
+ return part.type === "step-start" ? index : lastIndex;
8959
+ }, -1);
8960
+ const lastStepToolInvocations = message.parts.slice(lastStepStartIndex + 1).filter(isToolUIPart);
8961
+ return lastStepToolInvocations.length > 0 && lastStepToolInvocations.every((part) => part.state === "output-available");
9007
8962
  }
9008
8963
 
9009
8964
  // src/ui/convert-to-model-messages.ts
@@ -9301,10 +9256,13 @@ function createUIMessageStream({
9301
9256
  // src/ui-message-stream/read-ui-message-stream.ts
9302
9257
  function readUIMessageStream({
9303
9258
  message,
9304
- stream
9259
+ stream,
9260
+ onError,
9261
+ terminateOnError = false
9305
9262
  }) {
9306
9263
  var _a16;
9307
9264
  let controller;
9265
+ let hasErrored = false;
9308
9266
  const outputStream = new ReadableStream({
9309
9267
  start(controllerParam) {
9310
9268
  controller = controllerParam;
@@ -9314,6 +9272,13 @@ function readUIMessageStream({
9314
9272
  messageId: (_a16 = message == null ? void 0 : message.id) != null ? _a16 : "",
9315
9273
  lastMessage: message
9316
9274
  });
9275
+ const handleError = (error) => {
9276
+ onError == null ? void 0 : onError(error);
9277
+ if (!hasErrored && terminateOnError) {
9278
+ hasErrored = true;
9279
+ controller == null ? void 0 : controller.error(error);
9280
+ }
9281
+ };
9317
9282
  consumeStream({
9318
9283
  stream: processUIMessageStream({
9319
9284
  stream,
@@ -9325,12 +9290,13 @@ function readUIMessageStream({
9325
9290
  }
9326
9291
  });
9327
9292
  },
9328
- onError: (error) => {
9329
- throw error;
9330
- }
9331
- })
9293
+ onError: handleError
9294
+ }),
9295
+ onError: handleError
9332
9296
  }).finally(() => {
9333
- controller == null ? void 0 : controller.close();
9297
+ if (!hasErrored) {
9298
+ controller == null ? void 0 : controller.close();
9299
+ }
9334
9300
  });
9335
9301
  return createAsyncIterableStream(outputStream);
9336
9302
  }