@copilotz/chat-adapter 0.4.1 → 0.6.0

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
@@ -647,13 +647,6 @@ async function deleteThread(threadId, getRequestHeaders) {
647
647
  }
648
648
  return true;
649
649
  }
650
- var copilotzService = {
651
- runCopilotzStream,
652
- fetchThreads,
653
- fetchThreadMessages,
654
- updateThread,
655
- deleteThread
656
- };
657
650
 
658
651
  // src/assetsService.ts
659
652
  var rawBaseValue2 = import.meta.env?.VITE_API_URL;
@@ -817,19 +810,137 @@ function useUrlState(config = {}) {
817
810
  };
818
811
  }
819
812
 
820
- // src/useCopilotzChat.ts
821
- var nowTs = () => Date.now();
822
- var generateId = () => globalThis.crypto?.randomUUID?.() ?? `id-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;
823
- var isAbortError = (error) => error instanceof DOMException && error.name === "AbortError" || typeof error === "object" && error !== null && "name" in error && error.name === "AbortError";
824
- var getEventPayload = (event) => event?.payload ?? event;
825
- var getEventSenderType = (payload) => payload?.senderType || payload?.sender?.type;
813
+ // src/activity.ts
814
+ var isToolCallActive = (toolCall) => toolCall.status === "pending" || toolCall.status === "running";
826
815
  var hasVisibleAssistantOutput = (message) => {
827
816
  if (message.role !== "assistant") return false;
828
817
  if (typeof message.content === "string" && message.content.trim().length > 0) return true;
829
818
  if (Array.isArray(message.attachments) && message.attachments.length > 0) return true;
830
- if (Array.isArray(message.toolCalls) && message.toolCalls.length > 0) return true;
819
+ if (Array.isArray(message._activityToolCalls) && message._activityToolCalls.length > 0) return true;
831
820
  return false;
832
821
  };
822
+ var buildAssistantActivity = (message) => {
823
+ const toolCalls = Array.isArray(message._activityToolCalls) ? message._activityToolCalls : [];
824
+ const hasReasoning = typeof message._activityReasoning === "string" && message._activityReasoning.length > 0;
825
+ const hasToolCalls = toolCalls.length > 0;
826
+ const runningTools = toolCalls.filter(isToolCallActive);
827
+ const hasRunningTools = runningTools.length > 0;
828
+ const isStreaming = message.isStreaming === true;
829
+ const isReasoningStreaming = message._activityReasoningStreaming === true;
830
+ const hasContent = typeof message.content === "string" && message.content.trim().length > 0;
831
+ if (!hasReasoning && !hasToolCalls && !isStreaming && !isReasoningStreaming) {
832
+ return void 0;
833
+ }
834
+ const isActive = isStreaming || isReasoningStreaming || hasRunningTools;
835
+ const summary = hasRunningTools ? {
836
+ kind: "using_tools",
837
+ ...runningTools.length === 1 ? { toolName: runningTools[0].name } : {},
838
+ ...runningTools.length > 1 ? { toolCount: runningTools.length } : {}
839
+ } : isStreaming && hasToolCalls && !hasContent ? {
840
+ kind: "using_tools",
841
+ ...toolCalls.length === 1 ? { toolName: toolCalls[0].name } : {},
842
+ ...toolCalls.length > 1 ? { toolCount: toolCalls.length } : {}
843
+ } : isReasoningStreaming || !hasContent && hasReasoning ? { kind: "thinking" } : isStreaming && hasContent ? { kind: "preparing_answer" } : isStreaming ? { kind: "working" } : hasToolCalls ? {
844
+ kind: "using_tools",
845
+ ...toolCalls.length === 1 ? { toolName: toolCalls[0].name } : {},
846
+ ...toolCalls.length > 1 ? { toolCount: toolCalls.length } : {}
847
+ } : { kind: "thinking" };
848
+ return {
849
+ isActive,
850
+ ...isActive ? {} : { isComplete: true },
851
+ summary,
852
+ ...hasReasoning ? { reasoning: message._activityReasoning } : {},
853
+ ...hasToolCalls ? { toolCalls } : {}
854
+ };
855
+ };
856
+ var syncAssistantActivity = (message) => {
857
+ if (message.role !== "assistant") {
858
+ const { _activityReasoning, _activityReasoningStreaming, _activityToolCalls, ...rest } = message;
859
+ return rest;
860
+ }
861
+ return {
862
+ ...message,
863
+ activity: buildAssistantActivity(message)
864
+ };
865
+ };
866
+ var toPublicChatMessage = (message) => {
867
+ const { _activityReasoning, _activityReasoningStreaming, _activityToolCalls, ...rest } = syncAssistantActivity(message);
868
+ return rest;
869
+ };
870
+ var updateAssistantMessageToken = (message, params) => {
871
+ if (message.role !== "assistant") return message;
872
+ const next = params.isReasoning ? {
873
+ ...message,
874
+ ...params.agentIdentity,
875
+ _activityReasoning: params.partial,
876
+ _activityReasoningStreaming: true,
877
+ isStreaming: true,
878
+ isComplete: false
879
+ } : {
880
+ ...message,
881
+ ...params.agentIdentity,
882
+ content: params.partial,
883
+ _activityReasoningStreaming: false,
884
+ isStreaming: true,
885
+ isComplete: false
886
+ };
887
+ return syncAssistantActivity(next);
888
+ };
889
+ var appendAssistantToolCall = (message, toolCall) => {
890
+ if (message.role !== "assistant") return message;
891
+ return syncAssistantActivity({
892
+ ...message,
893
+ _activityToolCalls: [
894
+ ...Array.isArray(message._activityToolCalls) ? message._activityToolCalls : [],
895
+ toolCall
896
+ ],
897
+ isStreaming: true,
898
+ isComplete: false
899
+ });
900
+ };
901
+ var applyAssistantToolResult = (message, update) => {
902
+ if (message.role !== "assistant") return message;
903
+ const toolCalls = Array.isArray(message._activityToolCalls) ? message._activityToolCalls : [];
904
+ const nextToolCalls = toolCalls.map((toolCall) => {
905
+ const matchesById = update.id && toolCall.id === update.id;
906
+ const matchesByName = !update.id && toolCall.name === update.name;
907
+ if (!matchesById && !matchesByName) return toolCall;
908
+ return {
909
+ ...toolCall,
910
+ ...update
911
+ };
912
+ });
913
+ return syncAssistantActivity({
914
+ ...message,
915
+ _activityToolCalls: nextToolCalls
916
+ });
917
+ };
918
+ var finalizeAssistantMessage = (message, finalAnswer) => {
919
+ if (message.role !== "assistant") return message;
920
+ return syncAssistantActivity({
921
+ ...message,
922
+ ...typeof finalAnswer === "string" && finalAnswer.length > 0 ? { content: finalAnswer } : {},
923
+ isStreaming: false,
924
+ isComplete: true,
925
+ _activityReasoningStreaming: false
926
+ });
927
+ };
928
+ var closeAssistantMessage = (message) => {
929
+ if (message.role !== "assistant") return message;
930
+ return syncAssistantActivity({
931
+ ...message,
932
+ isStreaming: false,
933
+ isComplete: true,
934
+ _activityReasoningStreaming: false
935
+ });
936
+ };
937
+
938
+ // src/useCopilotzChat.ts
939
+ var nowTs = () => Date.now();
940
+ var generateId = () => globalThis.crypto?.randomUUID?.() ?? `id-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;
941
+ var isAbortError = (error) => error instanceof DOMException && error.name === "AbortError" || typeof error === "object" && error !== null && "name" in error && error.name === "AbortError";
942
+ var getEventPayload = (event) => event?.payload ?? event;
943
+ var getEventSenderType = (payload) => payload?.senderType || payload?.sender?.type;
833
944
  var isInternalMessageMetadata = (metadata) => metadata?.visibility === "internal";
834
945
  var normalizeAgentIdentity = (agent) => {
835
946
  const senderAgentId = typeof agent?.id === "string" && agent.id.length > 0 ? agent.id : void 0;
@@ -884,24 +995,21 @@ var applyToolResultUpdateToMessages = (messages, update, assistantPatch) => {
884
995
  const nextMessages = [...messages];
885
996
  for (let i = nextMessages.length - 1; i >= 0; i--) {
886
997
  const message = nextMessages[i];
887
- if (message.role !== "assistant" || !Array.isArray(message.toolCalls) || message.toolCalls.length === 0) {
998
+ if (message.role !== "assistant" || !Array.isArray(message._activityToolCalls) || message._activityToolCalls.length === 0) {
888
999
  continue;
889
1000
  }
890
- const toolCallIndex = findMatchingToolCallIndex(message.toolCalls, update);
1001
+ const toolCallIndex = findMatchingToolCallIndex(message._activityToolCalls, update);
891
1002
  if (toolCallIndex === -1) continue;
892
- const updatedToolCalls = [...message.toolCalls];
893
- const current = updatedToolCalls[toolCallIndex];
894
- updatedToolCalls[toolCallIndex] = {
895
- ...current,
896
- status: update.status,
897
- ...update.result !== void 0 ? { result: update.result } : {},
898
- endTime: update.endTime
899
- };
900
- nextMessages[i] = {
901
- ...message,
902
- toolCalls: updatedToolCalls,
1003
+ nextMessages[i] = syncAssistantActivity({
1004
+ ...applyAssistantToolResult(message, {
1005
+ ...update.id ? { id: update.id } : {},
1006
+ name: update.name ?? message._activityToolCalls[toolCallIndex].name,
1007
+ status: update.status,
1008
+ ...update.result !== void 0 ? { result: update.result } : {},
1009
+ endTime: update.endTime
1010
+ }),
903
1011
  ...assistantPatch ?? {}
904
- };
1012
+ });
905
1013
  return { messages: nextMessages, matched: true };
906
1014
  }
907
1015
  return { messages, matched: false };
@@ -1062,7 +1170,7 @@ var convertServerMessage = (msg) => {
1062
1170
  const reasoning = typeof msg.reasoning === "string" && msg.reasoning.length > 0 ? msg.reasoning : void 0;
1063
1171
  const senderAgentId = msg.senderType === "agent" ? msg.senderId ?? void 0 : void 0;
1064
1172
  const senderName = msg.senderType === "agent" ? typeof msg.senderName === "string" ? msg.senderName : msg.senderId ?? void 0 : void 0;
1065
- return {
1173
+ return syncAssistantActivity({
1066
1174
  id: msg.id,
1067
1175
  role,
1068
1176
  content,
@@ -1071,11 +1179,11 @@ var convertServerMessage = (msg) => {
1071
1179
  isStreaming: false,
1072
1180
  isComplete: true,
1073
1181
  metadata,
1074
- toolCalls: hasToolCalls ? mappedToolCalls : void 0,
1075
- ...reasoning ? { reasoning } : {},
1182
+ _activityToolCalls: hasToolCalls ? mappedToolCalls : void 0,
1183
+ ...reasoning ? { _activityReasoning: reasoning } : {},
1076
1184
  ...senderAgentId ? { senderAgentId } : {},
1077
1185
  ...senderName ? { senderName } : {}
1078
- };
1186
+ });
1079
1187
  };
1080
1188
  function useCopilotz({
1081
1189
  userId,
@@ -1192,14 +1300,14 @@ function useCopilotz({
1192
1300
  for (let i = next.length - 1; i >= 0; i--) {
1193
1301
  const m = next[i];
1194
1302
  if (m.role === "assistant" && m.isStreaming && (!incomingAgentKey || messageAgentKey(m) === incomingAgentKey)) {
1195
- next[i] = {
1303
+ next[i] = syncAssistantActivity({
1196
1304
  ...m,
1197
1305
  content: payload.content,
1198
1306
  isStreaming: false,
1199
1307
  isComplete: true,
1200
1308
  ...agentSenderId ? { senderAgentId: agentSenderId } : {},
1201
1309
  ...agentSenderName ? { senderName: agentSenderName } : {}
1202
- };
1310
+ });
1203
1311
  return next;
1204
1312
  }
1205
1313
  }
@@ -1209,7 +1317,7 @@ function useCopilotz({
1209
1317
  }
1210
1318
  return [
1211
1319
  ...next,
1212
- {
1320
+ syncAssistantActivity({
1213
1321
  id: generateId(),
1214
1322
  role: "assistant",
1215
1323
  content: payload.content,
@@ -1219,7 +1327,7 @@ function useCopilotz({
1219
1327
  metadata: liveMetadata,
1220
1328
  ...agentSenderId ? { senderAgentId: agentSenderId } : {},
1221
1329
  ...agentSenderName ? { senderName: agentSenderName } : {}
1222
- }
1330
+ })
1223
1331
  ];
1224
1332
  });
1225
1333
  }, []);
@@ -1492,7 +1600,7 @@ function useCopilotz({
1492
1600
  setMessages((prev) => {
1493
1601
  const hasStreaming = prev.some((msg) => msg.isStreaming);
1494
1602
  if (!hasStreaming) return prev;
1495
- return prev.map((msg) => msg.isStreaming ? { ...msg, isStreaming: false, isComplete: true } : msg);
1603
+ return prev.map((msg) => msg.isStreaming ? closeAssistantMessage(msg) : msg);
1496
1604
  });
1497
1605
  }, []);
1498
1606
  const handleStreamAssetEvent = useCallback2((payload, assistantMessageId) => {
@@ -1510,54 +1618,35 @@ function useCopilotz({
1510
1618
  dataUrl,
1511
1619
  mimeType
1512
1620
  };
1513
- setMessages((prev) => prev.map((msg) => msg.id === assistantMessageId ? {
1621
+ setMessages((prev) => prev.map((msg) => msg.id === assistantMessageId ? syncAssistantActivity({
1514
1622
  ...msg,
1515
- attachments: [...msg.attachments || [], mediaAttachment],
1516
- isStreaming: false,
1517
- isComplete: true
1518
- } : msg));
1623
+ attachments: [...msg.attachments || [], mediaAttachment]
1624
+ }) : msg));
1519
1625
  }, []);
1520
1626
  const sendCopilotzMessage = useCallback2(async (params) => {
1521
1627
  let currentAssistantId = generateId();
1522
1628
  params.onBeforeStart?.(currentAssistantId);
1523
1629
  let hasStreamProgress = false;
1524
- let pendingStartNewAssistantBubble = false;
1525
1630
  const updateStreamingMessage = (partial, opts) => {
1526
1631
  if (partial && partial.length > 0) {
1527
1632
  hasStreamProgress = true;
1528
1633
  }
1529
- const nextStreaming = true;
1530
- const nextComplete = false;
1531
1634
  const isReasoning = opts?.isReasoning ?? false;
1532
1635
  const agentIdentity = normalizeAgentIdentity(opts?.agent ?? null);
1533
1636
  const nextAgentKey = agentIdentity.senderAgentId ?? agentIdentity.senderName ?? null;
1534
1637
  const applyUpdate = (msg) => {
1535
- if (isReasoning) {
1536
- return {
1537
- ...msg,
1538
- ...agentIdentity,
1539
- reasoning: partial,
1540
- isReasoningStreaming: true,
1541
- isStreaming: nextStreaming,
1542
- isComplete: nextComplete
1543
- };
1544
- }
1545
- const reasoningPatch = msg.reasoning ? { isReasoningStreaming: false } : {};
1546
- return {
1547
- ...msg,
1548
- ...agentIdentity,
1549
- content: partial,
1550
- ...reasoningPatch,
1551
- isStreaming: nextStreaming,
1552
- isComplete: nextComplete
1553
- };
1638
+ return updateAssistantMessageToken(msg, {
1639
+ partial,
1640
+ isReasoning,
1641
+ agentIdentity
1642
+ });
1554
1643
  };
1555
1644
  setMessages((prev) => {
1556
1645
  const idx = prev.findIndex((m) => m.id === currentAssistantId);
1557
1646
  if (idx >= 0 && prev[idx].role === "assistant" && prev[idx].isStreaming && (!nextAgentKey || messageAgentKey(prev[idx]) === nextAgentKey)) {
1558
1647
  const msg = prev[idx];
1559
1648
  const next = applyUpdate(msg);
1560
- if (msg.content === next.content && msg.reasoning === next.reasoning && msg.isReasoningStreaming === next.isReasoningStreaming && msg.isStreaming === next.isStreaming && msg.isComplete === next.isComplete) {
1649
+ if (msg.content === next.content && msg._activityReasoning === next._activityReasoning && msg._activityReasoningStreaming === next._activityReasoningStreaming && msg.isStreaming === next.isStreaming && msg.isComplete === next.isComplete) {
1561
1650
  return prev;
1562
1651
  }
1563
1652
  const updated = [...prev];
@@ -1567,9 +1656,8 @@ function useCopilotz({
1567
1656
  const last = prev[prev.length - 1];
1568
1657
  if (last && last.role === "assistant" && last.isStreaming && (!nextAgentKey || messageAgentKey(last) === nextAgentKey)) {
1569
1658
  currentAssistantId = last.id;
1570
- pendingStartNewAssistantBubble = false;
1571
1659
  const next = applyUpdate(last);
1572
- if (last.content === next.content && last.reasoning === next.reasoning && last.isReasoningStreaming === next.isReasoningStreaming && last.isStreaming === next.isStreaming && last.isComplete === next.isComplete) {
1660
+ if (last.content === next.content && last._activityReasoning === next._activityReasoning && last._activityReasoningStreaming === next._activityReasoningStreaming && last.isStreaming === next.isStreaming && last.isComplete === next.isComplete) {
1573
1661
  return prev;
1574
1662
  }
1575
1663
  const updated = [...prev];
@@ -1577,17 +1665,16 @@ function useCopilotz({
1577
1665
  return updated;
1578
1666
  }
1579
1667
  const lastStreamingBelongsToDifferentAgent = Boolean(nextAgentKey) && last?.role === "assistant" && last.isStreaming && messageAgentKey(last) !== nextAgentKey;
1580
- if (pendingStartNewAssistantBubble || !prev.length || (prev[prev.length - 1].role !== "assistant" || !prev[prev.length - 1].isStreaming) || lastStreamingBelongsToDifferentAgent) {
1668
+ if (!prev.length || (prev[prev.length - 1].role !== "assistant" || !prev[prev.length - 1].isStreaming) || lastStreamingBelongsToDifferentAgent) {
1581
1669
  const newId = generateId();
1582
1670
  currentAssistantId = newId;
1583
- pendingStartNewAssistantBubble = false;
1584
1671
  const base = {
1585
1672
  id: newId,
1586
1673
  role: "assistant",
1587
1674
  content: "",
1588
1675
  timestamp: nowTs(),
1589
- isStreaming: nextStreaming,
1590
- isComplete: nextComplete,
1676
+ isStreaming: true,
1677
+ isComplete: false,
1591
1678
  ...agentIdentity
1592
1679
  };
1593
1680
  return [...prev, applyUpdate(base)];
@@ -1602,7 +1689,7 @@ function useCopilotz({
1602
1689
  const msg = prev[idx];
1603
1690
  if (!msg.isStreaming && msg.isComplete) return prev;
1604
1691
  const updated = [...prev];
1605
- updated[idx] = { ...msg, isStreaming: false, isComplete: true };
1692
+ updated[idx] = closeAssistantMessage(msg);
1606
1693
  return updated;
1607
1694
  });
1608
1695
  };
@@ -1634,14 +1721,8 @@ function useCopilotz({
1634
1721
  })();
1635
1722
  if (fallbackIdx < 0) return prev;
1636
1723
  const message = prev[fallbackIdx];
1637
- const nextMessage = {
1638
- ...message,
1639
- ...typeof finalAnswer === "string" && finalAnswer.length > 0 ? { content: finalAnswer } : {},
1640
- isStreaming: false,
1641
- isComplete: true,
1642
- ...message.reasoning ? { isReasoningStreaming: false } : {}
1643
- };
1644
- if (message.content === nextMessage.content && message.isStreaming === nextMessage.isStreaming && message.isComplete === nextMessage.isComplete && message.isReasoningStreaming === nextMessage.isReasoningStreaming) {
1724
+ const nextMessage = finalizeAssistantMessage(message, finalAnswer);
1725
+ if (message.content === nextMessage.content && message.isStreaming === nextMessage.isStreaming && message.isComplete === nextMessage.isComplete && message._activityReasoningStreaming === nextMessage._activityReasoningStreaming) {
1645
1726
  return prev;
1646
1727
  }
1647
1728
  const updated = [...prev];
@@ -1671,25 +1752,6 @@ function useCopilotz({
1671
1752
  }]
1672
1753
  };
1673
1754
  }
1674
- if (type === "ASSET_CREATED") {
1675
- const by = payload?.by || "";
1676
- if (by && by !== "tool") return null;
1677
- const mime = payload?.mime || "image/png";
1678
- const ref = payload?.ref || payload?.assetRef || "";
1679
- if (!ref) return null;
1680
- const kind = mime.startsWith("audio/") ? "audio" : mime.startsWith("video/") ? "video" : "image";
1681
- const msgLike = {
1682
- id: generateId(),
1683
- threadId: curThreadId ?? "",
1684
- senderType: "tool",
1685
- content: "",
1686
- metadata: {
1687
- attachments: [{ kind, assetRef: ref, mimeType: mime }]
1688
- }
1689
- };
1690
- const [resolved] = await resolveAssetsInMessages([msgLike]);
1691
- return resolved;
1692
- }
1693
1755
  return null;
1694
1756
  };
1695
1757
  const abortController = new AbortController();
@@ -1752,7 +1814,6 @@ function useCopilotz({
1752
1814
  if (type === "LLM_RESULT") {
1753
1815
  const finalAnswer = typeof payload?.answer === "string" ? payload.answer : void 0;
1754
1816
  finalizeActiveAssistantTurn(finalAnswer);
1755
- pendingStartNewAssistantBubble = true;
1756
1817
  return;
1757
1818
  }
1758
1819
  if (type === "MESSAGE" || type === "NEW_MESSAGE") {
@@ -1777,19 +1838,15 @@ function useCopilotz({
1777
1838
  setMessages(
1778
1839
  (prev) => (() => {
1779
1840
  const appendToolCall = (msg) => ({
1780
- ...msg,
1781
- toolCalls: [
1782
- ...Array.isArray(msg.toolCalls) ? msg.toolCalls : [],
1783
- {
1784
- id: callId,
1785
- name: toolName,
1786
- arguments: parsedToolCall.arguments,
1787
- ...initialResult !== void 0 ? { result: initialResult } : {},
1788
- status: initialStatus,
1789
- startTime: Date.now(),
1790
- ...endTime !== void 0 ? { endTime } : {}
1791
- }
1792
- ]
1841
+ ...appendAssistantToolCall(msg, {
1842
+ id: callId,
1843
+ name: toolName,
1844
+ arguments: parsedToolCall.arguments,
1845
+ ...initialResult !== void 0 ? { result: initialResult } : {},
1846
+ status: initialStatus,
1847
+ startTime: Date.now(),
1848
+ ...endTime !== void 0 ? { endTime } : {}
1849
+ })
1793
1850
  });
1794
1851
  const currentIdx = prev.findIndex((message) => message.id === currentAssistantId && message.role === "assistant" && message.isStreaming);
1795
1852
  if (currentIdx >= 0) {
@@ -1802,7 +1859,7 @@ function useCopilotz({
1802
1859
  return next;
1803
1860
  }
1804
1861
  const last = prev[prev.length - 1];
1805
- if (!pendingStartNewAssistantBubble && last?.role === "assistant" && last.isStreaming) {
1862
+ if (last?.role === "assistant" && last.isStreaming) {
1806
1863
  currentAssistantId = last.id;
1807
1864
  const next = [...prev];
1808
1865
  next[prev.length - 1] = appendToolCall({
@@ -1814,7 +1871,6 @@ function useCopilotz({
1814
1871
  }
1815
1872
  const newId = generateId();
1816
1873
  currentAssistantId = newId;
1817
- pendingStartNewAssistantBubble = false;
1818
1874
  return [
1819
1875
  ...prev,
1820
1876
  appendToolCall({
@@ -1829,7 +1885,6 @@ function useCopilotz({
1829
1885
  })()
1830
1886
  );
1831
1887
  hasStreamProgress = true;
1832
- pendingStartNewAssistantBubble = true;
1833
1888
  return;
1834
1889
  }
1835
1890
  const sm = await toServerMessageFromEvent(event);
@@ -1837,7 +1892,6 @@ function useCopilotz({
1837
1892
  const viewMsg = convertServerMessage(sm);
1838
1893
  finalizeCurrentAssistantBubble();
1839
1894
  setMessages((prev) => [...prev, viewMsg]);
1840
- pendingStartNewAssistantBubble = true;
1841
1895
  return;
1842
1896
  }
1843
1897
  handleStreamMessageEvent(event);
@@ -1849,14 +1903,7 @@ function useCopilotz({
1849
1903
  }
1850
1904
  await (async () => {
1851
1905
  if (!hasStreamProgress) return;
1852
- finalizeCurrentAssistantBubble();
1853
- const evt = { type: "ASSET_CREATED", payload };
1854
- const sm = await toServerMessageFromEvent(evt);
1855
- if (sm) {
1856
- const viewMsg = convertServerMessage(sm);
1857
- setMessages((prev) => [...prev, viewMsg]);
1858
- }
1859
- pendingStartNewAssistantBubble = true;
1906
+ handleStreamAssetEvent(payload, currentAssistantId);
1860
1907
  })();
1861
1908
  },
1862
1909
  signal: abortController.signal
@@ -1866,7 +1913,7 @@ function useCopilotz({
1866
1913
  setMessages((prev) => {
1867
1914
  const hasStreaming = prev.some((msg) => msg.isStreaming);
1868
1915
  if (!hasStreaming) return prev;
1869
- return prev.map((msg) => msg.isStreaming ? { ...msg, isStreaming: false, isComplete: true } : msg);
1916
+ return prev.map((msg) => msg.isStreaming ? closeAssistantMessage(msg) : msg);
1870
1917
  });
1871
1918
  abortControllerRef.current = null;
1872
1919
  }
@@ -1911,7 +1958,7 @@ function useCopilotz({
1911
1958
  isComplete: false,
1912
1959
  ...targetAgentNameRef.current ? { senderName: targetAgentNameRef.current } : {}
1913
1960
  };
1914
- setMessages((prev) => [...prev, userMessage, assistantPlaceholder]);
1961
+ setMessages((prev) => [...prev, userMessage, syncAssistantActivity(assistantPlaceholder)]);
1915
1962
  setSpecialState(null);
1916
1963
  if (!threadsRef.current.some((t) => t.id === conversationKey)) {
1917
1964
  const newThread = {
@@ -1950,7 +1997,7 @@ function useCopilotz({
1950
1997
  return;
1951
1998
  }
1952
1999
  setMessages((prev) => {
1953
- const finalized = prev.map((msg) => msg.isStreaming ? { ...msg, isStreaming: false, isComplete: true } : msg);
2000
+ const finalized = prev.map((msg) => msg.isStreaming ? closeAssistantMessage(msg) : msg);
1954
2001
  if (finalized.some(hasVisibleAssistantOutput)) {
1955
2002
  return finalized;
1956
2003
  }
@@ -1958,24 +2005,24 @@ function useCopilotz({
1958
2005
  const message = finalized[i];
1959
2006
  if (message.role !== "assistant") continue;
1960
2007
  const updated = [...finalized];
1961
- updated[i] = {
2008
+ updated[i] = syncAssistantActivity({
1962
2009
  ...message,
1963
2010
  content: "Desculpe, ocorreu um erro ao gerar a resposta. Por favor, tente novamente.",
1964
2011
  isStreaming: false,
1965
2012
  isComplete: true
1966
- };
2013
+ });
1967
2014
  return updated;
1968
2015
  }
1969
2016
  return [
1970
2017
  ...finalized,
1971
- {
2018
+ syncAssistantActivity({
1972
2019
  id: generateId(),
1973
2020
  role: "assistant",
1974
2021
  content: "Desculpe, ocorreu um erro ao gerar a resposta. Por favor, tente novamente.",
1975
2022
  timestamp: nowTs(),
1976
2023
  isStreaming: false,
1977
2024
  isComplete: true
1978
- }
2025
+ })
1979
2026
  ];
1980
2027
  });
1981
2028
  }
@@ -2014,14 +2061,14 @@ function useCopilotz({
2014
2061
  return;
2015
2062
  }
2016
2063
  setMessages([
2017
- {
2064
+ syncAssistantActivity({
2018
2065
  id: generateId(),
2019
2066
  role: "assistant",
2020
2067
  content: "N\xE3o foi poss\xEDvel iniciar a conversa. Tente novamente mais tarde.",
2021
2068
  timestamp: nowTs(),
2022
2069
  isStreaming: false,
2023
2070
  isComplete: true
2024
- }
2071
+ })
2025
2072
  ]);
2026
2073
  }
2027
2074
  }, [fetchAndSetThreadsState, loadThreadMessages, sendCopilotzMessage, bootstrap, defaultThreadName, getSpecialStateFromError]);
@@ -2077,7 +2124,7 @@ function useCopilotz({
2077
2124
  }
2078
2125
  }, [currentThreadId, threadMetadataMap]);
2079
2126
  return {
2080
- messages,
2127
+ messages: messages.map(toPublicChatMessage),
2081
2128
  isMessagesLoading,
2082
2129
  isLoadingOlderMessages,
2083
2130
  messagePageInfo,
@@ -2289,17 +2336,14 @@ var CopilotzChat = ({
2289
2336
  export {
2290
2337
  CopilotzChat,
2291
2338
  CopilotzRequestError,
2292
- copilotzService,
2293
2339
  deleteThread,
2294
2340
  fetchThreadMessages,
2295
- fetchThreadMessagesPage,
2296
2341
  fetchThreads,
2297
2342
  getAssetDataUrl,
2298
2343
  resolveAssetsInMessages,
2299
2344
  runCopilotzStream,
2300
2345
  updateThread,
2301
- useCopilotz,
2302
- useUrlState
2346
+ useCopilotz
2303
2347
  };
2304
2348
  /*! Bundled license information:
2305
2349