@zenning/ai 5.0.42-patch → 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.mjs CHANGED
@@ -3736,8 +3736,11 @@ function processUIMessageStream({
3736
3736
  }
3737
3737
  case "start": {
3738
3738
  if (chunk.messageId != null) {
3739
- if (state.isFinalized && state.message.parts.length > 0) {
3740
- state.messageQueue.push(state.message);
3739
+ const isNewMessage = state.isFinalized && state.message.id !== chunk.messageId;
3740
+ if (isNewMessage) {
3741
+ if (state.message.parts.length > 0 || state.message.id) {
3742
+ state.messageQueue.push(structuredClone(state.message));
3743
+ }
3741
3744
  state.message = {
3742
3745
  id: chunk.messageId,
3743
3746
  metadata: chunk.messageMetadata,
@@ -3748,15 +3751,24 @@ function processUIMessageStream({
3748
3751
  state.activeReasoningParts = {};
3749
3752
  state.partialToolCalls = {};
3750
3753
  state.isFinalized = false;
3751
- } else {
3754
+ write();
3755
+ } else if (!state.message.id) {
3752
3756
  state.message.id = chunk.messageId;
3753
3757
  await updateMessageMetadata(chunk.messageMetadata);
3758
+ if (chunk.messageId != null || chunk.messageMetadata != null) {
3759
+ write();
3760
+ }
3761
+ } else {
3762
+ await updateMessageMetadata(chunk.messageMetadata);
3763
+ if (chunk.messageMetadata != null) {
3764
+ write();
3765
+ }
3754
3766
  }
3755
3767
  } else {
3756
3768
  await updateMessageMetadata(chunk.messageMetadata);
3757
- }
3758
- if (chunk.messageId != null || chunk.messageMetadata != null) {
3759
- write();
3769
+ if (chunk.messageMetadata != null) {
3770
+ write();
3771
+ }
3760
3772
  }
3761
3773
  break;
3762
3774
  }
@@ -9751,33 +9763,38 @@ var AbstractChat = class {
9751
9763
  this.setStatus({ status: "streaming" });
9752
9764
  if (activeResponse.state.messageQueue && activeResponse.state.messageQueue.length > 0) {
9753
9765
  for (const queuedMessage of activeResponse.state.messageQueue) {
9754
- const existingMessageIndex2 = this.state.messages.findIndex(
9766
+ const existingIndex = this.state.messages.findIndex(
9755
9767
  (msg) => msg.id === queuedMessage.id
9756
9768
  );
9757
- if (existingMessageIndex2 >= 0) {
9758
- this.state.replaceMessage(existingMessageIndex2, queuedMessage);
9769
+ if (existingIndex >= 0) {
9770
+ this.state.replaceMessage(existingIndex, queuedMessage);
9759
9771
  } else {
9760
9772
  this.state.pushMessage(queuedMessage);
9761
9773
  }
9762
9774
  }
9763
9775
  activeResponse.state.messageQueue = [];
9764
9776
  }
9765
- const replaceLastMessage = activeResponse.state.message.id === ((_a18 = this.lastMessage) == null ? void 0 : _a18.id);
9766
- const existingMessageIndex = this.state.messages.findIndex(
9777
+ if (!activeResponse.state.message.id) {
9778
+ return;
9779
+ }
9780
+ const currentMessageIndex = this.state.messages.findIndex(
9767
9781
  (msg) => msg.id === activeResponse.state.message.id
9768
9782
  );
9769
- if (existingMessageIndex >= 0) {
9783
+ if (currentMessageIndex >= 0) {
9770
9784
  this.state.replaceMessage(
9771
- existingMessageIndex,
9772
- activeResponse.state.message
9773
- );
9774
- } else if (replaceLastMessage) {
9775
- this.state.replaceMessage(
9776
- this.state.messages.length - 1,
9785
+ currentMessageIndex,
9777
9786
  activeResponse.state.message
9778
9787
  );
9779
9788
  } else {
9780
- this.state.pushMessage(activeResponse.state.message);
9789
+ const replaceLastMessage = activeResponse.state.message.id === ((_a18 = this.lastMessage) == null ? void 0 : _a18.id);
9790
+ if (replaceLastMessage && this.state.messages.length > 0) {
9791
+ this.state.replaceMessage(
9792
+ this.state.messages.length - 1,
9793
+ activeResponse.state.message
9794
+ );
9795
+ } else {
9796
+ this.state.pushMessage(activeResponse.state.message);
9797
+ }
9781
9798
  }
9782
9799
  }
9783
9800
  })
@@ -9812,6 +9829,16 @@ var AbstractChat = class {
9812
9829
  }
9813
9830
  activeResponse.state.messageQueue = [];
9814
9831
  }
9832
+ if (activeResponse.state.isFinalized && activeResponse.state.message.parts.length > 0) {
9833
+ const existingMessageIndex = this.state.messages.findIndex(
9834
+ (msg) => msg.id === activeResponse.state.message.id
9835
+ );
9836
+ if (existingMessageIndex >= 0) {
9837
+ this.state.replaceMessage(existingMessageIndex, activeResponse.state.message);
9838
+ } else {
9839
+ this.state.pushMessage(activeResponse.state.message);
9840
+ }
9841
+ }
9815
9842
  this.setStatus({ status: "ready" });
9816
9843
  } catch (err) {
9817
9844
  if (isAbort || err.name === "AbortError") {