@vettvangur/ai-sdk 0.1.9 → 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 +46 -5
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -724,6 +724,11 @@ const DEFAULT_TIMEOUT_MS = 6e4;
|
|
|
724
724
|
function abortError() {
|
|
725
725
|
return /* @__PURE__ */ new Error("AI chat request was canceled");
|
|
726
726
|
}
|
|
727
|
+
function afterNextPaint() {
|
|
728
|
+
return new Promise((resolve) => {
|
|
729
|
+
requestAnimationFrame(() => requestAnimationFrame(() => resolve()));
|
|
730
|
+
});
|
|
731
|
+
}
|
|
727
732
|
function createChatSession(options) {
|
|
728
733
|
const history = [];
|
|
729
734
|
const context = {};
|
|
@@ -779,6 +784,7 @@ function createChatSession(options) {
|
|
|
779
784
|
for await (const event of readEventStream(response, requestController.signal)) {
|
|
780
785
|
options.onEvent?.(event);
|
|
781
786
|
for (const listener of listeners) listener(event);
|
|
787
|
+
if (event.name === "tool") await afterNextPaint();
|
|
782
788
|
if (event.name === "delta") answer += event.data.text;
|
|
783
789
|
else if (event.name === "tool_result" && event.data.reply) replies.push(event.data.reply);
|
|
784
790
|
else if (event.name === "done") provider = event.data.provider;
|
|
@@ -905,12 +911,15 @@ function bindChat(root, options = {}) {
|
|
|
905
911
|
...options.classes
|
|
906
912
|
};
|
|
907
913
|
const initialThread = chatThread.cloneNode(true);
|
|
908
|
-
|
|
914
|
+
let initialNodes = Array.from(chatThread.childNodes);
|
|
909
915
|
const endpoint = options.endpoint ?? root.dataset.aiChatEndpoint ?? form.action;
|
|
910
916
|
const header = options.antiforgeryHeader ?? root.dataset.aiChatAntiforgeryHeader;
|
|
911
917
|
const token = options.antiforgeryToken ?? root.dataset.aiChatAntiforgeryToken;
|
|
912
918
|
const headers = header && token ? { [header]: token } : void 0;
|
|
913
919
|
let activeAssistant = null;
|
|
920
|
+
let activeText = null;
|
|
921
|
+
let activeTextContent = "";
|
|
922
|
+
let pendingTool = null;
|
|
914
923
|
let destroyed = false;
|
|
915
924
|
const session = createChatSession({
|
|
916
925
|
endpoint,
|
|
@@ -932,6 +941,17 @@ function bindChat(root, options = {}) {
|
|
|
932
941
|
bubble.append(renderMarkdown(root.ownerDocument, text));
|
|
933
942
|
parent.append(bubble);
|
|
934
943
|
}
|
|
944
|
+
function appendStreamedMarkdown(text) {
|
|
945
|
+
if (!activeAssistant) return;
|
|
946
|
+
if (!activeText) {
|
|
947
|
+
activeText = root.ownerDocument.createElement("div");
|
|
948
|
+
activeText.dataset.aiChatText = "true";
|
|
949
|
+
activeText.className = classes.text;
|
|
950
|
+
activeAssistant.append(activeText);
|
|
951
|
+
}
|
|
952
|
+
activeTextContent += text;
|
|
953
|
+
activeText.replaceChildren(renderMarkdown(root.ownerDocument, activeTextContent));
|
|
954
|
+
}
|
|
935
955
|
function report(error) {
|
|
936
956
|
options.onError?.(error);
|
|
937
957
|
const message = root.ownerDocument.createElement("p");
|
|
@@ -955,6 +975,10 @@ function bindChat(root, options = {}) {
|
|
|
955
975
|
activeAssistant?.append(element);
|
|
956
976
|
return element;
|
|
957
977
|
}
|
|
978
|
+
function clearPendingTool() {
|
|
979
|
+
pendingTool?.remove();
|
|
980
|
+
pendingTool = null;
|
|
981
|
+
}
|
|
958
982
|
function finishTool(id, failed, interrupted = false) {
|
|
959
983
|
const element = activeAssistant?.querySelector(`[data-ai-chat-tool="${CSS.escape(id)}"]`);
|
|
960
984
|
if (element) {
|
|
@@ -969,6 +993,8 @@ function bindChat(root, options = {}) {
|
|
|
969
993
|
}
|
|
970
994
|
function renderResult(event) {
|
|
971
995
|
if (!activeAssistant) return;
|
|
996
|
+
activeText = null;
|
|
997
|
+
activeTextContent = "";
|
|
972
998
|
const context = {
|
|
973
999
|
document: root.ownerDocument,
|
|
974
1000
|
root,
|
|
@@ -998,10 +1024,17 @@ function bindChat(root, options = {}) {
|
|
|
998
1024
|
}
|
|
999
1025
|
const unsubscribe = session.subscribe((event) => {
|
|
1000
1026
|
if (destroyed) return;
|
|
1001
|
-
if (event.name === "delta" && activeAssistant)
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
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();
|
|
1005
1038
|
});
|
|
1006
1039
|
const submit = async (event) => {
|
|
1007
1040
|
event?.preventDefault();
|
|
@@ -1011,6 +1044,9 @@ function bindChat(root, options = {}) {
|
|
|
1011
1044
|
for (const node of initialNodes) node.remove();
|
|
1012
1045
|
appendMarkdown(appendMessage("user"), question);
|
|
1013
1046
|
activeAssistant = appendMessage("assistant");
|
|
1047
|
+
activeText = null;
|
|
1048
|
+
activeTextContent = "";
|
|
1049
|
+
pendingTool = tool("pending", "");
|
|
1014
1050
|
root.setAttribute("aria-busy", "true");
|
|
1015
1051
|
if (send) send.disabled = true;
|
|
1016
1052
|
const result = await session.send(question);
|
|
@@ -1032,7 +1068,12 @@ function bindChat(root, options = {}) {
|
|
|
1032
1068
|
if (target?.closest("[data-ai-chat-reset]")) {
|
|
1033
1069
|
session.reset();
|
|
1034
1070
|
chatThread.replaceChildren(...Array.from(initialThread.childNodes, (node) => node.cloneNode(true)));
|
|
1071
|
+
initialNodes = Array.from(chatThread.childNodes);
|
|
1035
1072
|
input.value = "";
|
|
1073
|
+
activeAssistant = null;
|
|
1074
|
+
activeText = null;
|
|
1075
|
+
activeTextContent = "";
|
|
1076
|
+
pendingTool = null;
|
|
1036
1077
|
return;
|
|
1037
1078
|
}
|
|
1038
1079
|
const actionElement = target?.closest("[data-ai-action]");
|