@copilotz/chat-adapter 0.4.0 → 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 +186 -104
- package/dist/index.js.map +1 -1
- package/package.json +3 -2
package/dist/index.js
CHANGED
|
@@ -337,6 +337,8 @@ async function runCopilotzStream(options) {
|
|
|
337
337
|
const threadName = mergedThreadMetadata.name ?? null;
|
|
338
338
|
const { name: _threadName, ...restThreadMetadata } = mergedThreadMetadata;
|
|
339
339
|
const resolvedParticipants = Array.isArray(participants) && participants.length > 0 ? participants : [selectedAgent || "assistant"];
|
|
340
|
+
const resolvedTarget = targetAgent?.trim() || null;
|
|
341
|
+
const toolCallSenderId = selectedAgent || resolvedParticipants[0] || "assistant";
|
|
340
342
|
const threadPayload = threadId || threadExternalId || threadName || Object.keys(restThreadMetadata).length > 0 ? {
|
|
341
343
|
id: threadId ?? null,
|
|
342
344
|
externalId: threadExternalId ?? null,
|
|
@@ -379,14 +381,13 @@ async function runCopilotzStream(options) {
|
|
|
379
381
|
if (parts.length === 1 && parts[0].type === "text") return parts[0].text;
|
|
380
382
|
return parts;
|
|
381
383
|
})();
|
|
382
|
-
const resolvedTarget = targetAgent?.trim() || null;
|
|
383
384
|
const payload = {
|
|
384
385
|
content: contentParts,
|
|
385
386
|
sender: {
|
|
386
387
|
type: normalizedToolCalls.length > 0 ? "agent" : "user",
|
|
387
|
-
externalId: user.externalId,
|
|
388
|
-
id: normalizedToolCalls.length > 0 ?
|
|
389
|
-
name: normalizedToolCalls.length > 0 ?
|
|
388
|
+
externalId: normalizedToolCalls.length > 0 ? toolCallSenderId : user.externalId,
|
|
389
|
+
id: normalizedToolCalls.length > 0 ? toolCallSenderId : void 0,
|
|
390
|
+
name: normalizedToolCalls.length > 0 ? toolCallSenderId : user.name ?? null,
|
|
390
391
|
metadata: Object.keys(senderMetadata).length > 0 ? senderMetadata : null
|
|
391
392
|
},
|
|
392
393
|
metadata: messageMetadata ?? null,
|
|
@@ -646,13 +647,6 @@ async function deleteThread(threadId, getRequestHeaders) {
|
|
|
646
647
|
}
|
|
647
648
|
return true;
|
|
648
649
|
}
|
|
649
|
-
var copilotzService = {
|
|
650
|
-
runCopilotzStream,
|
|
651
|
-
fetchThreads,
|
|
652
|
-
fetchThreadMessages,
|
|
653
|
-
updateThread,
|
|
654
|
-
deleteThread
|
|
655
|
-
};
|
|
656
650
|
|
|
657
651
|
// src/assetsService.ts
|
|
658
652
|
var rawBaseValue2 = import.meta.env?.VITE_API_URL;
|
|
@@ -816,19 +810,139 @@ function useUrlState(config = {}) {
|
|
|
816
810
|
};
|
|
817
811
|
}
|
|
818
812
|
|
|
819
|
-
// src/
|
|
820
|
-
var
|
|
821
|
-
var generateId = () => globalThis.crypto?.randomUUID?.() ?? `id-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;
|
|
822
|
-
var isAbortError = (error) => error instanceof DOMException && error.name === "AbortError" || typeof error === "object" && error !== null && "name" in error && error.name === "AbortError";
|
|
823
|
-
var getEventPayload = (event) => event?.payload ?? event;
|
|
824
|
-
var getEventSenderType = (payload) => payload?.senderType || payload?.sender?.type;
|
|
813
|
+
// src/activity.ts
|
|
814
|
+
var isToolCallActive = (toolCall) => toolCall.status === "pending" || toolCall.status === "running";
|
|
825
815
|
var hasVisibleAssistantOutput = (message) => {
|
|
826
816
|
if (message.role !== "assistant") return false;
|
|
827
817
|
if (typeof message.content === "string" && message.content.trim().length > 0) return true;
|
|
828
818
|
if (Array.isArray(message.attachments) && message.attachments.length > 0) return true;
|
|
829
|
-
if (Array.isArray(message.
|
|
819
|
+
if (Array.isArray(message._activityToolCalls) && message._activityToolCalls.length > 0) return true;
|
|
830
820
|
return false;
|
|
831
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;
|
|
832
946
|
var isInternalMessageMetadata = (metadata) => metadata?.visibility === "internal";
|
|
833
947
|
var normalizeAgentIdentity = (agent) => {
|
|
834
948
|
const senderAgentId = typeof agent?.id === "string" && agent.id.length > 0 ? agent.id : void 0;
|
|
@@ -883,24 +997,21 @@ var applyToolResultUpdateToMessages = (messages, update, assistantPatch) => {
|
|
|
883
997
|
const nextMessages = [...messages];
|
|
884
998
|
for (let i = nextMessages.length - 1; i >= 0; i--) {
|
|
885
999
|
const message = nextMessages[i];
|
|
886
|
-
if (message.role !== "assistant" || !Array.isArray(message.
|
|
1000
|
+
if (message.role !== "assistant" || !Array.isArray(message._activityToolCalls) || message._activityToolCalls.length === 0) {
|
|
887
1001
|
continue;
|
|
888
1002
|
}
|
|
889
|
-
const toolCallIndex = findMatchingToolCallIndex(message.
|
|
1003
|
+
const toolCallIndex = findMatchingToolCallIndex(message._activityToolCalls, update);
|
|
890
1004
|
if (toolCallIndex === -1) continue;
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
nextMessages[i] = {
|
|
900
|
-
...message,
|
|
901
|
-
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
|
+
}),
|
|
902
1013
|
...assistantPatch ?? {}
|
|
903
|
-
};
|
|
1014
|
+
});
|
|
904
1015
|
return { messages: nextMessages, matched: true };
|
|
905
1016
|
}
|
|
906
1017
|
return { messages, matched: false };
|
|
@@ -1061,7 +1172,7 @@ var convertServerMessage = (msg) => {
|
|
|
1061
1172
|
const reasoning = typeof msg.reasoning === "string" && msg.reasoning.length > 0 ? msg.reasoning : void 0;
|
|
1062
1173
|
const senderAgentId = msg.senderType === "agent" ? msg.senderId ?? void 0 : void 0;
|
|
1063
1174
|
const senderName = msg.senderType === "agent" ? typeof msg.senderName === "string" ? msg.senderName : msg.senderId ?? void 0 : void 0;
|
|
1064
|
-
return {
|
|
1175
|
+
return syncAssistantActivity({
|
|
1065
1176
|
id: msg.id,
|
|
1066
1177
|
role,
|
|
1067
1178
|
content,
|
|
@@ -1070,11 +1181,11 @@ var convertServerMessage = (msg) => {
|
|
|
1070
1181
|
isStreaming: false,
|
|
1071
1182
|
isComplete: true,
|
|
1072
1183
|
metadata,
|
|
1073
|
-
|
|
1074
|
-
...reasoning ? { reasoning } : {},
|
|
1184
|
+
_activityToolCalls: hasToolCalls ? mappedToolCalls : void 0,
|
|
1185
|
+
...reasoning ? { _activityReasoning: reasoning } : {},
|
|
1075
1186
|
...senderAgentId ? { senderAgentId } : {},
|
|
1076
1187
|
...senderName ? { senderName } : {}
|
|
1077
|
-
};
|
|
1188
|
+
});
|
|
1078
1189
|
};
|
|
1079
1190
|
function useCopilotz({
|
|
1080
1191
|
userId,
|
|
@@ -1191,14 +1302,14 @@ function useCopilotz({
|
|
|
1191
1302
|
for (let i = next.length - 1; i >= 0; i--) {
|
|
1192
1303
|
const m = next[i];
|
|
1193
1304
|
if (m.role === "assistant" && m.isStreaming && (!incomingAgentKey || messageAgentKey(m) === incomingAgentKey)) {
|
|
1194
|
-
next[i] = {
|
|
1305
|
+
next[i] = syncAssistantActivity({
|
|
1195
1306
|
...m,
|
|
1196
1307
|
content: payload.content,
|
|
1197
1308
|
isStreaming: false,
|
|
1198
1309
|
isComplete: true,
|
|
1199
1310
|
...agentSenderId ? { senderAgentId: agentSenderId } : {},
|
|
1200
1311
|
...agentSenderName ? { senderName: agentSenderName } : {}
|
|
1201
|
-
};
|
|
1312
|
+
});
|
|
1202
1313
|
return next;
|
|
1203
1314
|
}
|
|
1204
1315
|
}
|
|
@@ -1208,7 +1319,7 @@ function useCopilotz({
|
|
|
1208
1319
|
}
|
|
1209
1320
|
return [
|
|
1210
1321
|
...next,
|
|
1211
|
-
{
|
|
1322
|
+
syncAssistantActivity({
|
|
1212
1323
|
id: generateId(),
|
|
1213
1324
|
role: "assistant",
|
|
1214
1325
|
content: payload.content,
|
|
@@ -1218,7 +1329,7 @@ function useCopilotz({
|
|
|
1218
1329
|
metadata: liveMetadata,
|
|
1219
1330
|
...agentSenderId ? { senderAgentId: agentSenderId } : {},
|
|
1220
1331
|
...agentSenderName ? { senderName: agentSenderName } : {}
|
|
1221
|
-
}
|
|
1332
|
+
})
|
|
1222
1333
|
];
|
|
1223
1334
|
});
|
|
1224
1335
|
}, []);
|
|
@@ -1491,7 +1602,7 @@ function useCopilotz({
|
|
|
1491
1602
|
setMessages((prev) => {
|
|
1492
1603
|
const hasStreaming = prev.some((msg) => msg.isStreaming);
|
|
1493
1604
|
if (!hasStreaming) return prev;
|
|
1494
|
-
return prev.map((msg) => msg.isStreaming ?
|
|
1605
|
+
return prev.map((msg) => msg.isStreaming ? closeAssistantMessage(msg) : msg);
|
|
1495
1606
|
});
|
|
1496
1607
|
}, []);
|
|
1497
1608
|
const handleStreamAssetEvent = useCallback2((payload, assistantMessageId) => {
|
|
@@ -1509,12 +1620,12 @@ function useCopilotz({
|
|
|
1509
1620
|
dataUrl,
|
|
1510
1621
|
mimeType
|
|
1511
1622
|
};
|
|
1512
|
-
setMessages((prev) => prev.map((msg) => msg.id === assistantMessageId ? {
|
|
1623
|
+
setMessages((prev) => prev.map((msg) => msg.id === assistantMessageId ? syncAssistantActivity({
|
|
1513
1624
|
...msg,
|
|
1514
1625
|
attachments: [...msg.attachments || [], mediaAttachment],
|
|
1515
1626
|
isStreaming: false,
|
|
1516
1627
|
isComplete: true
|
|
1517
|
-
} : msg));
|
|
1628
|
+
}) : msg));
|
|
1518
1629
|
}, []);
|
|
1519
1630
|
const sendCopilotzMessage = useCallback2(async (params) => {
|
|
1520
1631
|
let currentAssistantId = generateId();
|
|
@@ -1525,38 +1636,22 @@ function useCopilotz({
|
|
|
1525
1636
|
if (partial && partial.length > 0) {
|
|
1526
1637
|
hasStreamProgress = true;
|
|
1527
1638
|
}
|
|
1528
|
-
const nextStreaming = true;
|
|
1529
|
-
const nextComplete = false;
|
|
1530
1639
|
const isReasoning = opts?.isReasoning ?? false;
|
|
1531
1640
|
const agentIdentity = normalizeAgentIdentity(opts?.agent ?? null);
|
|
1532
1641
|
const nextAgentKey = agentIdentity.senderAgentId ?? agentIdentity.senderName ?? null;
|
|
1533
1642
|
const applyUpdate = (msg) => {
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
isReasoningStreaming: true,
|
|
1540
|
-
isStreaming: nextStreaming,
|
|
1541
|
-
isComplete: nextComplete
|
|
1542
|
-
};
|
|
1543
|
-
}
|
|
1544
|
-
const reasoningPatch = msg.reasoning ? { isReasoningStreaming: false } : {};
|
|
1545
|
-
return {
|
|
1546
|
-
...msg,
|
|
1547
|
-
...agentIdentity,
|
|
1548
|
-
content: partial,
|
|
1549
|
-
...reasoningPatch,
|
|
1550
|
-
isStreaming: nextStreaming,
|
|
1551
|
-
isComplete: nextComplete
|
|
1552
|
-
};
|
|
1643
|
+
return updateAssistantMessageToken(msg, {
|
|
1644
|
+
partial,
|
|
1645
|
+
isReasoning,
|
|
1646
|
+
agentIdentity
|
|
1647
|
+
});
|
|
1553
1648
|
};
|
|
1554
1649
|
setMessages((prev) => {
|
|
1555
1650
|
const idx = prev.findIndex((m) => m.id === currentAssistantId);
|
|
1556
1651
|
if (idx >= 0 && prev[idx].role === "assistant" && prev[idx].isStreaming && (!nextAgentKey || messageAgentKey(prev[idx]) === nextAgentKey)) {
|
|
1557
1652
|
const msg = prev[idx];
|
|
1558
1653
|
const next = applyUpdate(msg);
|
|
1559
|
-
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) {
|
|
1560
1655
|
return prev;
|
|
1561
1656
|
}
|
|
1562
1657
|
const updated = [...prev];
|
|
@@ -1568,7 +1663,7 @@ function useCopilotz({
|
|
|
1568
1663
|
currentAssistantId = last.id;
|
|
1569
1664
|
pendingStartNewAssistantBubble = false;
|
|
1570
1665
|
const next = applyUpdate(last);
|
|
1571
|
-
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) {
|
|
1572
1667
|
return prev;
|
|
1573
1668
|
}
|
|
1574
1669
|
const updated = [...prev];
|
|
@@ -1585,8 +1680,8 @@ function useCopilotz({
|
|
|
1585
1680
|
role: "assistant",
|
|
1586
1681
|
content: "",
|
|
1587
1682
|
timestamp: nowTs(),
|
|
1588
|
-
isStreaming:
|
|
1589
|
-
isComplete:
|
|
1683
|
+
isStreaming: true,
|
|
1684
|
+
isComplete: false,
|
|
1590
1685
|
...agentIdentity
|
|
1591
1686
|
};
|
|
1592
1687
|
return [...prev, applyUpdate(base)];
|
|
@@ -1601,7 +1696,7 @@ function useCopilotz({
|
|
|
1601
1696
|
const msg = prev[idx];
|
|
1602
1697
|
if (!msg.isStreaming && msg.isComplete) return prev;
|
|
1603
1698
|
const updated = [...prev];
|
|
1604
|
-
updated[idx] =
|
|
1699
|
+
updated[idx] = closeAssistantMessage(msg);
|
|
1605
1700
|
return updated;
|
|
1606
1701
|
});
|
|
1607
1702
|
};
|
|
@@ -1633,14 +1728,8 @@ function useCopilotz({
|
|
|
1633
1728
|
})();
|
|
1634
1729
|
if (fallbackIdx < 0) return prev;
|
|
1635
1730
|
const message = prev[fallbackIdx];
|
|
1636
|
-
const nextMessage =
|
|
1637
|
-
|
|
1638
|
-
...typeof finalAnswer === "string" && finalAnswer.length > 0 ? { content: finalAnswer } : {},
|
|
1639
|
-
isStreaming: false,
|
|
1640
|
-
isComplete: true,
|
|
1641
|
-
...message.reasoning ? { isReasoningStreaming: false } : {}
|
|
1642
|
-
};
|
|
1643
|
-
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) {
|
|
1644
1733
|
return prev;
|
|
1645
1734
|
}
|
|
1646
1735
|
const updated = [...prev];
|
|
@@ -1776,19 +1865,15 @@ function useCopilotz({
|
|
|
1776
1865
|
setMessages(
|
|
1777
1866
|
(prev) => (() => {
|
|
1778
1867
|
const appendToolCall = (msg) => ({
|
|
1779
|
-
...msg,
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
startTime: Date.now(),
|
|
1789
|
-
...endTime !== void 0 ? { endTime } : {}
|
|
1790
|
-
}
|
|
1791
|
-
]
|
|
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
|
+
})
|
|
1792
1877
|
});
|
|
1793
1878
|
const currentIdx = prev.findIndex((message) => message.id === currentAssistantId && message.role === "assistant" && message.isStreaming);
|
|
1794
1879
|
if (currentIdx >= 0) {
|
|
@@ -1865,7 +1950,7 @@ function useCopilotz({
|
|
|
1865
1950
|
setMessages((prev) => {
|
|
1866
1951
|
const hasStreaming = prev.some((msg) => msg.isStreaming);
|
|
1867
1952
|
if (!hasStreaming) return prev;
|
|
1868
|
-
return prev.map((msg) => msg.isStreaming ?
|
|
1953
|
+
return prev.map((msg) => msg.isStreaming ? closeAssistantMessage(msg) : msg);
|
|
1869
1954
|
});
|
|
1870
1955
|
abortControllerRef.current = null;
|
|
1871
1956
|
}
|
|
@@ -1910,7 +1995,7 @@ function useCopilotz({
|
|
|
1910
1995
|
isComplete: false,
|
|
1911
1996
|
...targetAgentNameRef.current ? { senderName: targetAgentNameRef.current } : {}
|
|
1912
1997
|
};
|
|
1913
|
-
setMessages((prev) => [...prev, userMessage, assistantPlaceholder]);
|
|
1998
|
+
setMessages((prev) => [...prev, userMessage, syncAssistantActivity(assistantPlaceholder)]);
|
|
1914
1999
|
setSpecialState(null);
|
|
1915
2000
|
if (!threadsRef.current.some((t) => t.id === conversationKey)) {
|
|
1916
2001
|
const newThread = {
|
|
@@ -1949,7 +2034,7 @@ function useCopilotz({
|
|
|
1949
2034
|
return;
|
|
1950
2035
|
}
|
|
1951
2036
|
setMessages((prev) => {
|
|
1952
|
-
const finalized = prev.map((msg) => msg.isStreaming ?
|
|
2037
|
+
const finalized = prev.map((msg) => msg.isStreaming ? closeAssistantMessage(msg) : msg);
|
|
1953
2038
|
if (finalized.some(hasVisibleAssistantOutput)) {
|
|
1954
2039
|
return finalized;
|
|
1955
2040
|
}
|
|
@@ -1957,24 +2042,24 @@ function useCopilotz({
|
|
|
1957
2042
|
const message = finalized[i];
|
|
1958
2043
|
if (message.role !== "assistant") continue;
|
|
1959
2044
|
const updated = [...finalized];
|
|
1960
|
-
updated[i] = {
|
|
2045
|
+
updated[i] = syncAssistantActivity({
|
|
1961
2046
|
...message,
|
|
1962
2047
|
content: "Desculpe, ocorreu um erro ao gerar a resposta. Por favor, tente novamente.",
|
|
1963
2048
|
isStreaming: false,
|
|
1964
2049
|
isComplete: true
|
|
1965
|
-
};
|
|
2050
|
+
});
|
|
1966
2051
|
return updated;
|
|
1967
2052
|
}
|
|
1968
2053
|
return [
|
|
1969
2054
|
...finalized,
|
|
1970
|
-
{
|
|
2055
|
+
syncAssistantActivity({
|
|
1971
2056
|
id: generateId(),
|
|
1972
2057
|
role: "assistant",
|
|
1973
2058
|
content: "Desculpe, ocorreu um erro ao gerar a resposta. Por favor, tente novamente.",
|
|
1974
2059
|
timestamp: nowTs(),
|
|
1975
2060
|
isStreaming: false,
|
|
1976
2061
|
isComplete: true
|
|
1977
|
-
}
|
|
2062
|
+
})
|
|
1978
2063
|
];
|
|
1979
2064
|
});
|
|
1980
2065
|
}
|
|
@@ -2013,14 +2098,14 @@ function useCopilotz({
|
|
|
2013
2098
|
return;
|
|
2014
2099
|
}
|
|
2015
2100
|
setMessages([
|
|
2016
|
-
{
|
|
2101
|
+
syncAssistantActivity({
|
|
2017
2102
|
id: generateId(),
|
|
2018
2103
|
role: "assistant",
|
|
2019
2104
|
content: "N\xE3o foi poss\xEDvel iniciar a conversa. Tente novamente mais tarde.",
|
|
2020
2105
|
timestamp: nowTs(),
|
|
2021
2106
|
isStreaming: false,
|
|
2022
2107
|
isComplete: true
|
|
2023
|
-
}
|
|
2108
|
+
})
|
|
2024
2109
|
]);
|
|
2025
2110
|
}
|
|
2026
2111
|
}, [fetchAndSetThreadsState, loadThreadMessages, sendCopilotzMessage, bootstrap, defaultThreadName, getSpecialStateFromError]);
|
|
@@ -2076,7 +2161,7 @@ function useCopilotz({
|
|
|
2076
2161
|
}
|
|
2077
2162
|
}, [currentThreadId, threadMetadataMap]);
|
|
2078
2163
|
return {
|
|
2079
|
-
messages,
|
|
2164
|
+
messages: messages.map(toPublicChatMessage),
|
|
2080
2165
|
isMessagesLoading,
|
|
2081
2166
|
isLoadingOlderMessages,
|
|
2082
2167
|
messagePageInfo,
|
|
@@ -2288,17 +2373,14 @@ var CopilotzChat = ({
|
|
|
2288
2373
|
export {
|
|
2289
2374
|
CopilotzChat,
|
|
2290
2375
|
CopilotzRequestError,
|
|
2291
|
-
copilotzService,
|
|
2292
2376
|
deleteThread,
|
|
2293
2377
|
fetchThreadMessages,
|
|
2294
|
-
fetchThreadMessagesPage,
|
|
2295
2378
|
fetchThreads,
|
|
2296
2379
|
getAssetDataUrl,
|
|
2297
2380
|
resolveAssetsInMessages,
|
|
2298
2381
|
runCopilotzStream,
|
|
2299
2382
|
updateThread,
|
|
2300
|
-
useCopilotz
|
|
2301
|
-
useUrlState
|
|
2383
|
+
useCopilotz
|
|
2302
2384
|
};
|
|
2303
2385
|
/*! Bundled license information:
|
|
2304
2386
|
|