@vettvangur/ai-sdk 0.1.7 → 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 +29 -4
  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,
@@ -978,12 +999,11 @@ function bindChat(root, options = {}) {
978
999
  };
979
1000
  let content = null;
980
1001
  try {
981
- const registered = registryEntries(options.registry)?.renderers.get(event.data.name);
982
- content = registered?.(context) ?? null;
983
1002
  const templateName = dataString(event.data.data, "template");
984
1003
  const surfaceName = dataString(event.data.data, "surface");
985
- if (!content && templateName) content = root.querySelector(`template[data-template="${CSS.escape(templateName)}"]`)?.content.cloneNode(true) ?? null;
1004
+ if (templateName) content = root.querySelector(`template[data-template="${CSS.escape(templateName)}"]`)?.content.cloneNode(true) ?? null;
986
1005
  if (!content && surfaceName) content = registryEntries(options.registry)?.surfaces.get(surfaceName)?.(context) ?? root.querySelector(`template[data-ai-surface="${CSS.escape(surfaceName)}"]`)?.content.cloneNode(true) ?? null;
1006
+ if (!content) content = registryEntries(options.registry)?.renderers.get(event.data.name)?.(context) ?? null;
987
1007
  content ??= renderAnswerCard(root.ownerDocument, event.data.data, labels);
988
1008
  } catch (caught) {
989
1009
  options.onError?.(caught instanceof Error ? caught : /* @__PURE__ */ new Error("AI chat extension failed"));
@@ -999,7 +1019,7 @@ function bindChat(root, options = {}) {
999
1019
  }
1000
1020
  const unsubscribe = session.subscribe((event) => {
1001
1021
  if (destroyed) return;
1002
- if (event.name === "delta" && activeAssistant) appendMarkdown(activeAssistant, event.data.text);
1022
+ if (event.name === "delta" && activeAssistant) appendStreamedMarkdown(event.data.text);
1003
1023
  else if (event.name === "tool") tool(event.data.id, event.data.name);
1004
1024
  else if (event.name === "tool_result") renderResult(event);
1005
1025
  else if (event.name === "tool_done") finishTool(event.data.id, event.data.isError);
@@ -1012,6 +1032,8 @@ function bindChat(root, options = {}) {
1012
1032
  for (const node of initialNodes) node.remove();
1013
1033
  appendMarkdown(appendMessage("user"), question);
1014
1034
  activeAssistant = appendMessage("assistant");
1035
+ activeText = null;
1036
+ activeTextContent = "";
1015
1037
  root.setAttribute("aria-busy", "true");
1016
1038
  if (send) send.disabled = true;
1017
1039
  const result = await session.send(question);
@@ -1034,6 +1056,9 @@ function bindChat(root, options = {}) {
1034
1056
  session.reset();
1035
1057
  chatThread.replaceChildren(...Array.from(initialThread.childNodes, (node) => node.cloneNode(true)));
1036
1058
  input.value = "";
1059
+ activeAssistant = null;
1060
+ activeText = null;
1061
+ activeTextContent = "";
1037
1062
  return;
1038
1063
  }
1039
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.7",
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",