@cloudflare/ai-chat 0.3.0 → 0.3.1
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 +20 -1
- package/dist/index.js.map +1 -1
- package/dist/react.js +132 -7
- package/dist/react.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -807,8 +807,9 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
807
807
|
_persistOrphanedStream(streamId) {
|
|
808
808
|
const chunks = this._resumableStream.getStreamChunks(streamId);
|
|
809
809
|
if (!chunks.length) return;
|
|
810
|
+
const fallbackId = `assistant_${Date.now()}_${Math.random().toString(36).slice(2, 11)}`;
|
|
810
811
|
const message = {
|
|
811
|
-
id:
|
|
812
|
+
id: fallbackId,
|
|
812
813
|
role: "assistant",
|
|
813
814
|
parts: []
|
|
814
815
|
};
|
|
@@ -822,7 +823,21 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
822
823
|
applyChunkToParts(message.parts, data);
|
|
823
824
|
} catch {}
|
|
824
825
|
if (message.parts.length > 0) {
|
|
826
|
+
if (message.id === fallbackId) {
|
|
827
|
+
for (let i = this.messages.length - 1; i >= 0; i--) if (this.messages[i].role === "assistant") {
|
|
828
|
+
message.id = this.messages[i].id;
|
|
829
|
+
break;
|
|
830
|
+
}
|
|
831
|
+
}
|
|
825
832
|
const existingIdx = this.messages.findIndex((m) => m.id === message.id);
|
|
833
|
+
if (existingIdx >= 0) {
|
|
834
|
+
const existing = this.messages[existingIdx];
|
|
835
|
+
message.parts = [...existing.parts, ...message.parts];
|
|
836
|
+
if (existing.metadata) message.metadata = message.metadata ? {
|
|
837
|
+
...existing.metadata,
|
|
838
|
+
...message.metadata
|
|
839
|
+
} : existing.metadata;
|
|
840
|
+
}
|
|
826
841
|
const updatedMessages = existingIdx >= 0 ? this.messages.map((m, i) => i === existingIdx ? message : m) : [...this.messages, message];
|
|
827
842
|
this.persistMessages(updatedMessages);
|
|
828
843
|
}
|
|
@@ -2051,6 +2066,10 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
2051
2066
|
break;
|
|
2052
2067
|
}
|
|
2053
2068
|
let eventToSend = data;
|
|
2069
|
+
if (continuation && data.type === "start" && "messageId" in data) {
|
|
2070
|
+
const { messageId: _, ...rest } = data;
|
|
2071
|
+
eventToSend = rest;
|
|
2072
|
+
}
|
|
2054
2073
|
if (data.type === "finish" && "finishReason" in data) {
|
|
2055
2074
|
const { finishReason, ...rest } = data;
|
|
2056
2075
|
eventToSend = {
|