@zenning/ai 5.0.42 → 5.0.43

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
@@ -3805,8 +3805,11 @@ function processUIMessageStream({
3805
3805
  }
3806
3806
  case "start": {
3807
3807
  if (chunk.messageId != null) {
3808
- if (state.isFinalized && state.message.parts.length > 0) {
3809
- state.messageQueue.push(state.message);
3808
+ const isNewMessage = state.isFinalized && state.message.id !== chunk.messageId;
3809
+ if (isNewMessage) {
3810
+ if (state.message.parts.length > 0 || state.message.id) {
3811
+ state.messageQueue.push(structuredClone(state.message));
3812
+ }
3810
3813
  state.message = {
3811
3814
  id: chunk.messageId,
3812
3815
  metadata: chunk.messageMetadata,
@@ -3817,15 +3820,24 @@ function processUIMessageStream({
3817
3820
  state.activeReasoningParts = {};
3818
3821
  state.partialToolCalls = {};
3819
3822
  state.isFinalized = false;
3820
- } else {
3823
+ write();
3824
+ } else if (!state.message.id) {
3821
3825
  state.message.id = chunk.messageId;
3822
3826
  await updateMessageMetadata(chunk.messageMetadata);
3827
+ if (chunk.messageId != null || chunk.messageMetadata != null) {
3828
+ write();
3829
+ }
3830
+ } else {
3831
+ await updateMessageMetadata(chunk.messageMetadata);
3832
+ if (chunk.messageMetadata != null) {
3833
+ write();
3834
+ }
3823
3835
  }
3824
3836
  } else {
3825
3837
  await updateMessageMetadata(chunk.messageMetadata);
3826
- }
3827
- if (chunk.messageId != null || chunk.messageMetadata != null) {
3828
- write();
3838
+ if (chunk.messageMetadata != null) {
3839
+ write();
3840
+ }
3829
3841
  }
3830
3842
  break;
3831
3843
  }
@@ -9790,33 +9802,38 @@ var AbstractChat = class {
9790
9802
  this.setStatus({ status: "streaming" });
9791
9803
  if (activeResponse.state.messageQueue && activeResponse.state.messageQueue.length > 0) {
9792
9804
  for (const queuedMessage of activeResponse.state.messageQueue) {
9793
- const existingMessageIndex2 = this.state.messages.findIndex(
9805
+ const existingIndex = this.state.messages.findIndex(
9794
9806
  (msg) => msg.id === queuedMessage.id
9795
9807
  );
9796
- if (existingMessageIndex2 >= 0) {
9797
- this.state.replaceMessage(existingMessageIndex2, queuedMessage);
9808
+ if (existingIndex >= 0) {
9809
+ this.state.replaceMessage(existingIndex, queuedMessage);
9798
9810
  } else {
9799
9811
  this.state.pushMessage(queuedMessage);
9800
9812
  }
9801
9813
  }
9802
9814
  activeResponse.state.messageQueue = [];
9803
9815
  }
9804
- const replaceLastMessage = activeResponse.state.message.id === ((_a18 = this.lastMessage) == null ? void 0 : _a18.id);
9805
- const existingMessageIndex = this.state.messages.findIndex(
9816
+ if (!activeResponse.state.message.id) {
9817
+ return;
9818
+ }
9819
+ const currentMessageIndex = this.state.messages.findIndex(
9806
9820
  (msg) => msg.id === activeResponse.state.message.id
9807
9821
  );
9808
- if (existingMessageIndex >= 0) {
9822
+ if (currentMessageIndex >= 0) {
9809
9823
  this.state.replaceMessage(
9810
- existingMessageIndex,
9811
- activeResponse.state.message
9812
- );
9813
- } else if (replaceLastMessage) {
9814
- this.state.replaceMessage(
9815
- this.state.messages.length - 1,
9824
+ currentMessageIndex,
9816
9825
  activeResponse.state.message
9817
9826
  );
9818
9827
  } else {
9819
- this.state.pushMessage(activeResponse.state.message);
9828
+ const replaceLastMessage = activeResponse.state.message.id === ((_a18 = this.lastMessage) == null ? void 0 : _a18.id);
9829
+ if (replaceLastMessage && this.state.messages.length > 0) {
9830
+ this.state.replaceMessage(
9831
+ this.state.messages.length - 1,
9832
+ activeResponse.state.message
9833
+ );
9834
+ } else {
9835
+ this.state.pushMessage(activeResponse.state.message);
9836
+ }
9820
9837
  }
9821
9838
  }
9822
9839
  })
@@ -9851,6 +9868,16 @@ var AbstractChat = class {
9851
9868
  }
9852
9869
  activeResponse.state.messageQueue = [];
9853
9870
  }
9871
+ if (activeResponse.state.isFinalized && activeResponse.state.message.parts.length > 0) {
9872
+ const existingMessageIndex = this.state.messages.findIndex(
9873
+ (msg) => msg.id === activeResponse.state.message.id
9874
+ );
9875
+ if (existingMessageIndex >= 0) {
9876
+ this.state.replaceMessage(existingMessageIndex, activeResponse.state.message);
9877
+ } else {
9878
+ this.state.pushMessage(activeResponse.state.message);
9879
+ }
9880
+ }
9854
9881
  this.setStatus({ status: "ready" });
9855
9882
  } catch (err) {
9856
9883
  if (isAbort || err.name === "AbortError") {