@vettvangur/ai-sdk 0.1.11 → 0.1.12
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 -5
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -911,7 +911,7 @@ function bindChat(root, options = {}) {
|
|
|
911
911
|
...options.classes
|
|
912
912
|
};
|
|
913
913
|
const initialThread = chatThread.cloneNode(true);
|
|
914
|
-
|
|
914
|
+
let initialNodes = Array.from(chatThread.childNodes);
|
|
915
915
|
const endpoint = options.endpoint ?? root.dataset.aiChatEndpoint ?? form.action;
|
|
916
916
|
const header = options.antiforgeryHeader ?? root.dataset.aiChatAntiforgeryHeader;
|
|
917
917
|
const token = options.antiforgeryToken ?? root.dataset.aiChatAntiforgeryToken;
|
|
@@ -919,6 +919,7 @@ function bindChat(root, options = {}) {
|
|
|
919
919
|
let activeAssistant = null;
|
|
920
920
|
let activeText = null;
|
|
921
921
|
let activeTextContent = "";
|
|
922
|
+
let pendingTool = null;
|
|
922
923
|
let destroyed = false;
|
|
923
924
|
const session = createChatSession({
|
|
924
925
|
endpoint,
|
|
@@ -974,6 +975,10 @@ function bindChat(root, options = {}) {
|
|
|
974
975
|
activeAssistant?.append(element);
|
|
975
976
|
return element;
|
|
976
977
|
}
|
|
978
|
+
function clearPendingTool() {
|
|
979
|
+
pendingTool?.remove();
|
|
980
|
+
pendingTool = null;
|
|
981
|
+
}
|
|
977
982
|
function finishTool(id, failed, interrupted = false) {
|
|
978
983
|
const element = activeAssistant?.querySelector(`[data-ai-chat-tool="${CSS.escape(id)}"]`);
|
|
979
984
|
if (element) {
|
|
@@ -1019,10 +1024,17 @@ function bindChat(root, options = {}) {
|
|
|
1019
1024
|
}
|
|
1020
1025
|
const unsubscribe = session.subscribe((event) => {
|
|
1021
1026
|
if (destroyed) return;
|
|
1022
|
-
if (event.name === "delta" && activeAssistant)
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
else if (event.name === "
|
|
1027
|
+
if (event.name === "delta" && activeAssistant) {
|
|
1028
|
+
clearPendingTool();
|
|
1029
|
+
appendStreamedMarkdown(event.data.text);
|
|
1030
|
+
} else if (event.name === "tool") {
|
|
1031
|
+
clearPendingTool();
|
|
1032
|
+
tool(event.data.id, event.data.name);
|
|
1033
|
+
} else if (event.name === "tool_result") {
|
|
1034
|
+
clearPendingTool();
|
|
1035
|
+
renderResult(event);
|
|
1036
|
+
} else if (event.name === "tool_done") finishTool(event.data.id, event.data.isError);
|
|
1037
|
+
else if (event.name === "done" || event.name === "error") clearPendingTool();
|
|
1026
1038
|
});
|
|
1027
1039
|
const submit = async (event) => {
|
|
1028
1040
|
event?.preventDefault();
|
|
@@ -1034,6 +1046,7 @@ function bindChat(root, options = {}) {
|
|
|
1034
1046
|
activeAssistant = appendMessage("assistant");
|
|
1035
1047
|
activeText = null;
|
|
1036
1048
|
activeTextContent = "";
|
|
1049
|
+
pendingTool = tool("pending", "");
|
|
1037
1050
|
root.setAttribute("aria-busy", "true");
|
|
1038
1051
|
if (send) send.disabled = true;
|
|
1039
1052
|
const result = await session.send(question);
|
|
@@ -1055,10 +1068,12 @@ function bindChat(root, options = {}) {
|
|
|
1055
1068
|
if (target?.closest("[data-ai-chat-reset]")) {
|
|
1056
1069
|
session.reset();
|
|
1057
1070
|
chatThread.replaceChildren(...Array.from(initialThread.childNodes, (node) => node.cloneNode(true)));
|
|
1071
|
+
initialNodes = Array.from(chatThread.childNodes);
|
|
1058
1072
|
input.value = "";
|
|
1059
1073
|
activeAssistant = null;
|
|
1060
1074
|
activeText = null;
|
|
1061
1075
|
activeTextContent = "";
|
|
1076
|
+
pendingTool = null;
|
|
1062
1077
|
return;
|
|
1063
1078
|
}
|
|
1064
1079
|
const actionElement = target?.closest("[data-ai-action]");
|