@vettvangur/ai-sdk 0.1.9 → 0.1.11

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.
Files changed (2) hide show
  1. package/dist/index.js +27 -1
  2. 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;
@@ -911,6 +917,8 @@ function bindChat(root, options = {}) {
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 = "";
914
922
  let destroyed = false;
915
923
  const session = createChatSession({
916
924
  endpoint,
@@ -932,6 +940,17 @@ function bindChat(root, options = {}) {
932
940
  bubble.append(renderMarkdown(root.ownerDocument, text));
933
941
  parent.append(bubble);
934
942
  }
943
+ function appendStreamedMarkdown(text) {
944
+ if (!activeAssistant) return;
945
+ if (!activeText) {
946
+ activeText = root.ownerDocument.createElement("div");
947
+ activeText.dataset.aiChatText = "true";
948
+ activeText.className = classes.text;
949
+ activeAssistant.append(activeText);
950
+ }
951
+ activeTextContent += text;
952
+ activeText.replaceChildren(renderMarkdown(root.ownerDocument, activeTextContent));
953
+ }
935
954
  function report(error) {
936
955
  options.onError?.(error);
937
956
  const message = root.ownerDocument.createElement("p");
@@ -969,6 +988,8 @@ function bindChat(root, options = {}) {
969
988
  }
970
989
  function renderResult(event) {
971
990
  if (!activeAssistant) return;
991
+ activeText = null;
992
+ activeTextContent = "";
972
993
  const context = {
973
994
  document: root.ownerDocument,
974
995
  root,
@@ -998,7 +1019,7 @@ function bindChat(root, options = {}) {
998
1019
  }
999
1020
  const unsubscribe = session.subscribe((event) => {
1000
1021
  if (destroyed) return;
1001
- if (event.name === "delta" && activeAssistant) appendMarkdown(activeAssistant, event.data.text);
1022
+ if (event.name === "delta" && activeAssistant) appendStreamedMarkdown(event.data.text);
1002
1023
  else if (event.name === "tool") tool(event.data.id, event.data.name);
1003
1024
  else if (event.name === "tool_result") renderResult(event);
1004
1025
  else if (event.name === "tool_done") finishTool(event.data.id, event.data.isError);
@@ -1011,6 +1032,8 @@ function bindChat(root, options = {}) {
1011
1032
  for (const node of initialNodes) node.remove();
1012
1033
  appendMarkdown(appendMessage("user"), question);
1013
1034
  activeAssistant = appendMessage("assistant");
1035
+ activeText = null;
1036
+ activeTextContent = "";
1014
1037
  root.setAttribute("aria-busy", "true");
1015
1038
  if (send) send.disabled = true;
1016
1039
  const result = await session.send(question);
@@ -1033,6 +1056,9 @@ function bindChat(root, options = {}) {
1033
1056
  session.reset();
1034
1057
  chatThread.replaceChildren(...Array.from(initialThread.childNodes, (node) => node.cloneNode(true)));
1035
1058
  input.value = "";
1059
+ activeAssistant = null;
1060
+ activeText = null;
1061
+ activeTextContent = "";
1036
1062
  return;
1037
1063
  }
1038
1064
  const actionElement = target?.closest("[data-ai-action]");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vettvangur/ai-sdk",
3
- "version": "0.1.9",
3
+ "version": "0.1.11",
4
4
  "description": "Framework-agnostic AI chat client for server-rendered shells",
5
5
  "type": "module",
6
6
  "access": "public",