@copilotz/chat-adapter 0.4.1 → 0.5.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/README.md +11 -26
- package/dist/index.d.ts +1 -82
- package/dist/index.js +181 -100
- package/dist/index.js.map +1 -1
- package/package.json +3 -2
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,139 @@ function useUrlState(config = {}) {
|
|
|
817
810
|
};
|
|
818
811
|
}
|
|
819
812
|
|
|
820
|
-
// src/
|
|
821
|
-
var
|
|
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.
|
|
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
|
+
isStreaming: true,
|
|
917
|
+
isComplete: false
|
|
918
|
+
});
|
|
919
|
+
};
|
|
920
|
+
var finalizeAssistantMessage = (message, finalAnswer) => {
|
|
921
|
+
if (message.role !== "assistant") return message;
|
|
922
|
+
return syncAssistantActivity({
|
|
923
|
+
...message,
|
|
924
|
+
...typeof finalAnswer === "string" && finalAnswer.length > 0 ? { content: finalAnswer } : {},
|
|
925
|
+
isStreaming: false,
|
|
926
|
+
isComplete: true,
|
|
927
|
+
_activityReasoningStreaming: false
|
|
928
|
+
});
|
|
929
|
+
};
|
|
930
|
+
var closeAssistantMessage = (message) => {
|
|
931
|
+
if (message.role !== "assistant") return message;
|
|
932
|
+
return syncAssistantActivity({
|
|
933
|
+
...message,
|
|
934
|
+
isStreaming: false,
|
|
935
|
+
isComplete: true,
|
|
936
|
+
_activityReasoningStreaming: false
|
|
937
|
+
});
|
|
938
|
+
};
|
|
939
|
+
|
|
940
|
+
// src/useCopilotzChat.ts
|
|
941
|
+
var nowTs = () => Date.now();
|
|
942
|
+
var generateId = () => globalThis.crypto?.randomUUID?.() ?? `id-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;
|
|
943
|
+
var isAbortError = (error) => error instanceof DOMException && error.name === "AbortError" || typeof error === "object" && error !== null && "name" in error && error.name === "AbortError";
|
|
944
|
+
var getEventPayload = (event) => event?.payload ?? event;
|
|
945
|
+
var getEventSenderType = (payload) => payload?.senderType || payload?.sender?.type;
|
|
833
946
|
var isInternalMessageMetadata = (metadata) => metadata?.visibility === "internal";
|
|
834
947
|
var normalizeAgentIdentity = (agent) => {
|
|
835
948
|
const senderAgentId = typeof agent?.id === "string" && agent.id.length > 0 ? agent.id : void 0;
|
|
@@ -884,24 +997,21 @@ var applyToolResultUpdateToMessages = (messages, update, assistantPatch) => {
|
|
|
884
997
|
const nextMessages = [...messages];
|
|
885
998
|
for (let i = nextMessages.length - 1; i >= 0; i--) {
|
|
886
999
|
const message = nextMessages[i];
|
|
887
|
-
if (message.role !== "assistant" || !Array.isArray(message.
|
|
1000
|
+
if (message.role !== "assistant" || !Array.isArray(message._activityToolCalls) || message._activityToolCalls.length === 0) {
|
|
888
1001
|
continue;
|
|
889
1002
|
}
|
|
890
|
-
const toolCallIndex = findMatchingToolCallIndex(message.
|
|
1003
|
+
const toolCallIndex = findMatchingToolCallIndex(message._activityToolCalls, update);
|
|
891
1004
|
if (toolCallIndex === -1) continue;
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
nextMessages[i] = {
|
|
901
|
-
...message,
|
|
902
|
-
toolCalls: updatedToolCalls,
|
|
1005
|
+
nextMessages[i] = syncAssistantActivity({
|
|
1006
|
+
...applyAssistantToolResult(message, {
|
|
1007
|
+
...update.id ? { id: update.id } : {},
|
|
1008
|
+
name: update.name ?? message._activityToolCalls[toolCallIndex].name,
|
|
1009
|
+
status: update.status,
|
|
1010
|
+
...update.result !== void 0 ? { result: update.result } : {},
|
|
1011
|
+
endTime: update.endTime
|
|
1012
|
+
}),
|
|
903
1013
|
...assistantPatch ?? {}
|
|
904
|
-
};
|
|
1014
|
+
});
|
|
905
1015
|
return { messages: nextMessages, matched: true };
|
|
906
1016
|
}
|
|
907
1017
|
return { messages, matched: false };
|
|
@@ -1062,7 +1172,7 @@ var convertServerMessage = (msg) => {
|
|
|
1062
1172
|
const reasoning = typeof msg.reasoning === "string" && msg.reasoning.length > 0 ? msg.reasoning : void 0;
|
|
1063
1173
|
const senderAgentId = msg.senderType === "agent" ? msg.senderId ?? void 0 : void 0;
|
|
1064
1174
|
const senderName = msg.senderType === "agent" ? typeof msg.senderName === "string" ? msg.senderName : msg.senderId ?? void 0 : void 0;
|
|
1065
|
-
return {
|
|
1175
|
+
return syncAssistantActivity({
|
|
1066
1176
|
id: msg.id,
|
|
1067
1177
|
role,
|
|
1068
1178
|
content,
|
|
@@ -1071,11 +1181,11 @@ var convertServerMessage = (msg) => {
|
|
|
1071
1181
|
isStreaming: false,
|
|
1072
1182
|
isComplete: true,
|
|
1073
1183
|
metadata,
|
|
1074
|
-
|
|
1075
|
-
...reasoning ? { reasoning } : {},
|
|
1184
|
+
_activityToolCalls: hasToolCalls ? mappedToolCalls : void 0,
|
|
1185
|
+
...reasoning ? { _activityReasoning: reasoning } : {},
|
|
1076
1186
|
...senderAgentId ? { senderAgentId } : {},
|
|
1077
1187
|
...senderName ? { senderName } : {}
|
|
1078
|
-
};
|
|
1188
|
+
});
|
|
1079
1189
|
};
|
|
1080
1190
|
function useCopilotz({
|
|
1081
1191
|
userId,
|
|
@@ -1192,14 +1302,14 @@ function useCopilotz({
|
|
|
1192
1302
|
for (let i = next.length - 1; i >= 0; i--) {
|
|
1193
1303
|
const m = next[i];
|
|
1194
1304
|
if (m.role === "assistant" && m.isStreaming && (!incomingAgentKey || messageAgentKey(m) === incomingAgentKey)) {
|
|
1195
|
-
next[i] = {
|
|
1305
|
+
next[i] = syncAssistantActivity({
|
|
1196
1306
|
...m,
|
|
1197
1307
|
content: payload.content,
|
|
1198
1308
|
isStreaming: false,
|
|
1199
1309
|
isComplete: true,
|
|
1200
1310
|
...agentSenderId ? { senderAgentId: agentSenderId } : {},
|
|
1201
1311
|
...agentSenderName ? { senderName: agentSenderName } : {}
|
|
1202
|
-
};
|
|
1312
|
+
});
|
|
1203
1313
|
return next;
|
|
1204
1314
|
}
|
|
1205
1315
|
}
|
|
@@ -1209,7 +1319,7 @@ function useCopilotz({
|
|
|
1209
1319
|
}
|
|
1210
1320
|
return [
|
|
1211
1321
|
...next,
|
|
1212
|
-
{
|
|
1322
|
+
syncAssistantActivity({
|
|
1213
1323
|
id: generateId(),
|
|
1214
1324
|
role: "assistant",
|
|
1215
1325
|
content: payload.content,
|
|
@@ -1219,7 +1329,7 @@ function useCopilotz({
|
|
|
1219
1329
|
metadata: liveMetadata,
|
|
1220
1330
|
...agentSenderId ? { senderAgentId: agentSenderId } : {},
|
|
1221
1331
|
...agentSenderName ? { senderName: agentSenderName } : {}
|
|
1222
|
-
}
|
|
1332
|
+
})
|
|
1223
1333
|
];
|
|
1224
1334
|
});
|
|
1225
1335
|
}, []);
|
|
@@ -1492,7 +1602,7 @@ function useCopilotz({
|
|
|
1492
1602
|
setMessages((prev) => {
|
|
1493
1603
|
const hasStreaming = prev.some((msg) => msg.isStreaming);
|
|
1494
1604
|
if (!hasStreaming) return prev;
|
|
1495
|
-
return prev.map((msg) => msg.isStreaming ?
|
|
1605
|
+
return prev.map((msg) => msg.isStreaming ? closeAssistantMessage(msg) : msg);
|
|
1496
1606
|
});
|
|
1497
1607
|
}, []);
|
|
1498
1608
|
const handleStreamAssetEvent = useCallback2((payload, assistantMessageId) => {
|
|
@@ -1510,12 +1620,12 @@ function useCopilotz({
|
|
|
1510
1620
|
dataUrl,
|
|
1511
1621
|
mimeType
|
|
1512
1622
|
};
|
|
1513
|
-
setMessages((prev) => prev.map((msg) => msg.id === assistantMessageId ? {
|
|
1623
|
+
setMessages((prev) => prev.map((msg) => msg.id === assistantMessageId ? syncAssistantActivity({
|
|
1514
1624
|
...msg,
|
|
1515
1625
|
attachments: [...msg.attachments || [], mediaAttachment],
|
|
1516
1626
|
isStreaming: false,
|
|
1517
1627
|
isComplete: true
|
|
1518
|
-
} : msg));
|
|
1628
|
+
}) : msg));
|
|
1519
1629
|
}, []);
|
|
1520
1630
|
const sendCopilotzMessage = useCallback2(async (params) => {
|
|
1521
1631
|
let currentAssistantId = generateId();
|
|
@@ -1526,38 +1636,22 @@ function useCopilotz({
|
|
|
1526
1636
|
if (partial && partial.length > 0) {
|
|
1527
1637
|
hasStreamProgress = true;
|
|
1528
1638
|
}
|
|
1529
|
-
const nextStreaming = true;
|
|
1530
|
-
const nextComplete = false;
|
|
1531
1639
|
const isReasoning = opts?.isReasoning ?? false;
|
|
1532
1640
|
const agentIdentity = normalizeAgentIdentity(opts?.agent ?? null);
|
|
1533
1641
|
const nextAgentKey = agentIdentity.senderAgentId ?? agentIdentity.senderName ?? null;
|
|
1534
1642
|
const applyUpdate = (msg) => {
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
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
|
-
};
|
|
1643
|
+
return updateAssistantMessageToken(msg, {
|
|
1644
|
+
partial,
|
|
1645
|
+
isReasoning,
|
|
1646
|
+
agentIdentity
|
|
1647
|
+
});
|
|
1554
1648
|
};
|
|
1555
1649
|
setMessages((prev) => {
|
|
1556
1650
|
const idx = prev.findIndex((m) => m.id === currentAssistantId);
|
|
1557
1651
|
if (idx >= 0 && prev[idx].role === "assistant" && prev[idx].isStreaming && (!nextAgentKey || messageAgentKey(prev[idx]) === nextAgentKey)) {
|
|
1558
1652
|
const msg = prev[idx];
|
|
1559
1653
|
const next = applyUpdate(msg);
|
|
1560
|
-
if (msg.content === next.content && msg.
|
|
1654
|
+
if (msg.content === next.content && msg._activityReasoning === next._activityReasoning && msg._activityReasoningStreaming === next._activityReasoningStreaming && msg.isStreaming === next.isStreaming && msg.isComplete === next.isComplete) {
|
|
1561
1655
|
return prev;
|
|
1562
1656
|
}
|
|
1563
1657
|
const updated = [...prev];
|
|
@@ -1569,7 +1663,7 @@ function useCopilotz({
|
|
|
1569
1663
|
currentAssistantId = last.id;
|
|
1570
1664
|
pendingStartNewAssistantBubble = false;
|
|
1571
1665
|
const next = applyUpdate(last);
|
|
1572
|
-
if (last.content === next.content && last.
|
|
1666
|
+
if (last.content === next.content && last._activityReasoning === next._activityReasoning && last._activityReasoningStreaming === next._activityReasoningStreaming && last.isStreaming === next.isStreaming && last.isComplete === next.isComplete) {
|
|
1573
1667
|
return prev;
|
|
1574
1668
|
}
|
|
1575
1669
|
const updated = [...prev];
|
|
@@ -1586,8 +1680,8 @@ function useCopilotz({
|
|
|
1586
1680
|
role: "assistant",
|
|
1587
1681
|
content: "",
|
|
1588
1682
|
timestamp: nowTs(),
|
|
1589
|
-
isStreaming:
|
|
1590
|
-
isComplete:
|
|
1683
|
+
isStreaming: true,
|
|
1684
|
+
isComplete: false,
|
|
1591
1685
|
...agentIdentity
|
|
1592
1686
|
};
|
|
1593
1687
|
return [...prev, applyUpdate(base)];
|
|
@@ -1602,7 +1696,7 @@ function useCopilotz({
|
|
|
1602
1696
|
const msg = prev[idx];
|
|
1603
1697
|
if (!msg.isStreaming && msg.isComplete) return prev;
|
|
1604
1698
|
const updated = [...prev];
|
|
1605
|
-
updated[idx] =
|
|
1699
|
+
updated[idx] = closeAssistantMessage(msg);
|
|
1606
1700
|
return updated;
|
|
1607
1701
|
});
|
|
1608
1702
|
};
|
|
@@ -1634,14 +1728,8 @@ function useCopilotz({
|
|
|
1634
1728
|
})();
|
|
1635
1729
|
if (fallbackIdx < 0) return prev;
|
|
1636
1730
|
const message = prev[fallbackIdx];
|
|
1637
|
-
const nextMessage =
|
|
1638
|
-
|
|
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) {
|
|
1731
|
+
const nextMessage = finalizeAssistantMessage(message, finalAnswer);
|
|
1732
|
+
if (message.content === nextMessage.content && message.isStreaming === nextMessage.isStreaming && message.isComplete === nextMessage.isComplete && message._activityReasoningStreaming === nextMessage._activityReasoningStreaming) {
|
|
1645
1733
|
return prev;
|
|
1646
1734
|
}
|
|
1647
1735
|
const updated = [...prev];
|
|
@@ -1777,19 +1865,15 @@ function useCopilotz({
|
|
|
1777
1865
|
setMessages(
|
|
1778
1866
|
(prev) => (() => {
|
|
1779
1867
|
const appendToolCall = (msg) => ({
|
|
1780
|
-
...msg,
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
startTime: Date.now(),
|
|
1790
|
-
...endTime !== void 0 ? { endTime } : {}
|
|
1791
|
-
}
|
|
1792
|
-
]
|
|
1868
|
+
...appendAssistantToolCall(msg, {
|
|
1869
|
+
id: callId,
|
|
1870
|
+
name: toolName,
|
|
1871
|
+
arguments: parsedToolCall.arguments,
|
|
1872
|
+
...initialResult !== void 0 ? { result: initialResult } : {},
|
|
1873
|
+
status: initialStatus,
|
|
1874
|
+
startTime: Date.now(),
|
|
1875
|
+
...endTime !== void 0 ? { endTime } : {}
|
|
1876
|
+
})
|
|
1793
1877
|
});
|
|
1794
1878
|
const currentIdx = prev.findIndex((message) => message.id === currentAssistantId && message.role === "assistant" && message.isStreaming);
|
|
1795
1879
|
if (currentIdx >= 0) {
|
|
@@ -1866,7 +1950,7 @@ function useCopilotz({
|
|
|
1866
1950
|
setMessages((prev) => {
|
|
1867
1951
|
const hasStreaming = prev.some((msg) => msg.isStreaming);
|
|
1868
1952
|
if (!hasStreaming) return prev;
|
|
1869
|
-
return prev.map((msg) => msg.isStreaming ?
|
|
1953
|
+
return prev.map((msg) => msg.isStreaming ? closeAssistantMessage(msg) : msg);
|
|
1870
1954
|
});
|
|
1871
1955
|
abortControllerRef.current = null;
|
|
1872
1956
|
}
|
|
@@ -1911,7 +1995,7 @@ function useCopilotz({
|
|
|
1911
1995
|
isComplete: false,
|
|
1912
1996
|
...targetAgentNameRef.current ? { senderName: targetAgentNameRef.current } : {}
|
|
1913
1997
|
};
|
|
1914
|
-
setMessages((prev) => [...prev, userMessage, assistantPlaceholder]);
|
|
1998
|
+
setMessages((prev) => [...prev, userMessage, syncAssistantActivity(assistantPlaceholder)]);
|
|
1915
1999
|
setSpecialState(null);
|
|
1916
2000
|
if (!threadsRef.current.some((t) => t.id === conversationKey)) {
|
|
1917
2001
|
const newThread = {
|
|
@@ -1950,7 +2034,7 @@ function useCopilotz({
|
|
|
1950
2034
|
return;
|
|
1951
2035
|
}
|
|
1952
2036
|
setMessages((prev) => {
|
|
1953
|
-
const finalized = prev.map((msg) => msg.isStreaming ?
|
|
2037
|
+
const finalized = prev.map((msg) => msg.isStreaming ? closeAssistantMessage(msg) : msg);
|
|
1954
2038
|
if (finalized.some(hasVisibleAssistantOutput)) {
|
|
1955
2039
|
return finalized;
|
|
1956
2040
|
}
|
|
@@ -1958,24 +2042,24 @@ function useCopilotz({
|
|
|
1958
2042
|
const message = finalized[i];
|
|
1959
2043
|
if (message.role !== "assistant") continue;
|
|
1960
2044
|
const updated = [...finalized];
|
|
1961
|
-
updated[i] = {
|
|
2045
|
+
updated[i] = syncAssistantActivity({
|
|
1962
2046
|
...message,
|
|
1963
2047
|
content: "Desculpe, ocorreu um erro ao gerar a resposta. Por favor, tente novamente.",
|
|
1964
2048
|
isStreaming: false,
|
|
1965
2049
|
isComplete: true
|
|
1966
|
-
};
|
|
2050
|
+
});
|
|
1967
2051
|
return updated;
|
|
1968
2052
|
}
|
|
1969
2053
|
return [
|
|
1970
2054
|
...finalized,
|
|
1971
|
-
{
|
|
2055
|
+
syncAssistantActivity({
|
|
1972
2056
|
id: generateId(),
|
|
1973
2057
|
role: "assistant",
|
|
1974
2058
|
content: "Desculpe, ocorreu um erro ao gerar a resposta. Por favor, tente novamente.",
|
|
1975
2059
|
timestamp: nowTs(),
|
|
1976
2060
|
isStreaming: false,
|
|
1977
2061
|
isComplete: true
|
|
1978
|
-
}
|
|
2062
|
+
})
|
|
1979
2063
|
];
|
|
1980
2064
|
});
|
|
1981
2065
|
}
|
|
@@ -2014,14 +2098,14 @@ function useCopilotz({
|
|
|
2014
2098
|
return;
|
|
2015
2099
|
}
|
|
2016
2100
|
setMessages([
|
|
2017
|
-
{
|
|
2101
|
+
syncAssistantActivity({
|
|
2018
2102
|
id: generateId(),
|
|
2019
2103
|
role: "assistant",
|
|
2020
2104
|
content: "N\xE3o foi poss\xEDvel iniciar a conversa. Tente novamente mais tarde.",
|
|
2021
2105
|
timestamp: nowTs(),
|
|
2022
2106
|
isStreaming: false,
|
|
2023
2107
|
isComplete: true
|
|
2024
|
-
}
|
|
2108
|
+
})
|
|
2025
2109
|
]);
|
|
2026
2110
|
}
|
|
2027
2111
|
}, [fetchAndSetThreadsState, loadThreadMessages, sendCopilotzMessage, bootstrap, defaultThreadName, getSpecialStateFromError]);
|
|
@@ -2077,7 +2161,7 @@ function useCopilotz({
|
|
|
2077
2161
|
}
|
|
2078
2162
|
}, [currentThreadId, threadMetadataMap]);
|
|
2079
2163
|
return {
|
|
2080
|
-
messages,
|
|
2164
|
+
messages: messages.map(toPublicChatMessage),
|
|
2081
2165
|
isMessagesLoading,
|
|
2082
2166
|
isLoadingOlderMessages,
|
|
2083
2167
|
messagePageInfo,
|
|
@@ -2289,17 +2373,14 @@ var CopilotzChat = ({
|
|
|
2289
2373
|
export {
|
|
2290
2374
|
CopilotzChat,
|
|
2291
2375
|
CopilotzRequestError,
|
|
2292
|
-
copilotzService,
|
|
2293
2376
|
deleteThread,
|
|
2294
2377
|
fetchThreadMessages,
|
|
2295
|
-
fetchThreadMessagesPage,
|
|
2296
2378
|
fetchThreads,
|
|
2297
2379
|
getAssetDataUrl,
|
|
2298
2380
|
resolveAssetsInMessages,
|
|
2299
2381
|
runCopilotzStream,
|
|
2300
2382
|
updateThread,
|
|
2301
|
-
useCopilotz
|
|
2302
|
-
useUrlState
|
|
2383
|
+
useCopilotz
|
|
2303
2384
|
};
|
|
2304
2385
|
/*! Bundled license information:
|
|
2305
2386
|
|