@vettvangur/ai-sdk 0.1.11 → 0.1.13

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.d.ts CHANGED
@@ -31,6 +31,8 @@ type AiChatSession = {
31
31
  cancel(): void;
32
32
  reset(): void;
33
33
  setContext(name: string, value: string): void;
34
+ setContextMessage(name: string, content: string): void;
35
+ addAssistantReply(content: string): void;
34
36
  clearContext(name: string): void;
35
37
  subscribe(listener: (event: AiChatStreamEvent) => void): () => void;
36
38
  dispose(): void;
@@ -166,6 +168,7 @@ type AiChatClasses = {
166
168
  };
167
169
  type AiChatBinding = {
168
170
  session: AiChatSession;
171
+ appendAssistantReply(text: string): void;
169
172
  destroy(): void;
170
173
  };
171
174
  //#endregion
package/dist/index.js CHANGED
@@ -732,6 +732,7 @@ function afterNextPaint() {
732
732
  function createChatSession(options) {
733
733
  const history = [];
734
734
  const context = {};
735
+ const contextMessages = /* @__PURE__ */ new Map();
735
736
  const listeners = /* @__PURE__ */ new Set();
736
737
  let status = "idle";
737
738
  let controller = null;
@@ -821,11 +822,35 @@ function createChatSession(options) {
821
822
  session.cancel();
822
823
  history.splice(0);
823
824
  for (const key of Object.keys(context)) delete context[key];
825
+ contextMessages.clear();
824
826
  },
825
827
  setContext(name, value) {
826
828
  if (!name.trim()) throw new Error("AI chat context names cannot be empty");
827
829
  context[name] = value;
828
830
  },
831
+ setContextMessage(name, content) {
832
+ const value = content.trim();
833
+ if (!name.trim() || !value) throw new Error("AI chat context messages cannot be empty");
834
+ const existing = contextMessages.get(name);
835
+ if (existing) {
836
+ existing.content = value;
837
+ return;
838
+ }
839
+ const message = {
840
+ role: "user",
841
+ content: value
842
+ };
843
+ contextMessages.set(name, message);
844
+ history.push(message);
845
+ },
846
+ addAssistantReply(content) {
847
+ const value = content.trim();
848
+ if (!value) throw new Error("AI chat assistant replies cannot be empty");
849
+ history.push({
850
+ role: "assistant",
851
+ content: value
852
+ });
853
+ },
829
854
  clearContext(name) {
830
855
  delete context[name];
831
856
  },
@@ -911,7 +936,7 @@ function bindChat(root, options = {}) {
911
936
  ...options.classes
912
937
  };
913
938
  const initialThread = chatThread.cloneNode(true);
914
- const initialNodes = Array.from(chatThread.childNodes);
939
+ let initialNodes = Array.from(chatThread.childNodes);
915
940
  const endpoint = options.endpoint ?? root.dataset.aiChatEndpoint ?? form.action;
916
941
  const header = options.antiforgeryHeader ?? root.dataset.aiChatAntiforgeryHeader;
917
942
  const token = options.antiforgeryToken ?? root.dataset.aiChatAntiforgeryToken;
@@ -919,6 +944,7 @@ function bindChat(root, options = {}) {
919
944
  let activeAssistant = null;
920
945
  let activeText = null;
921
946
  let activeTextContent = "";
947
+ let pendingTool = null;
922
948
  let destroyed = false;
923
949
  const session = createChatSession({
924
950
  endpoint,
@@ -974,6 +1000,10 @@ function bindChat(root, options = {}) {
974
1000
  activeAssistant?.append(element);
975
1001
  return element;
976
1002
  }
1003
+ function clearPendingTool() {
1004
+ pendingTool?.remove();
1005
+ pendingTool = null;
1006
+ }
977
1007
  function finishTool(id, failed, interrupted = false) {
978
1008
  const element = activeAssistant?.querySelector(`[data-ai-chat-tool="${CSS.escape(id)}"]`);
979
1009
  if (element) {
@@ -1019,10 +1049,17 @@ function bindChat(root, options = {}) {
1019
1049
  }
1020
1050
  const unsubscribe = session.subscribe((event) => {
1021
1051
  if (destroyed) return;
1022
- if (event.name === "delta" && activeAssistant) appendStreamedMarkdown(event.data.text);
1023
- else if (event.name === "tool") tool(event.data.id, event.data.name);
1024
- else if (event.name === "tool_result") renderResult(event);
1025
- else if (event.name === "tool_done") finishTool(event.data.id, event.data.isError);
1052
+ if (event.name === "delta" && activeAssistant) {
1053
+ clearPendingTool();
1054
+ appendStreamedMarkdown(event.data.text);
1055
+ } else if (event.name === "tool") {
1056
+ clearPendingTool();
1057
+ tool(event.data.id, event.data.name);
1058
+ } else if (event.name === "tool_result") {
1059
+ clearPendingTool();
1060
+ renderResult(event);
1061
+ } else if (event.name === "tool_done") finishTool(event.data.id, event.data.isError);
1062
+ else if (event.name === "done" || event.name === "error") clearPendingTool();
1026
1063
  });
1027
1064
  const submit = async (event) => {
1028
1065
  event?.preventDefault();
@@ -1034,6 +1071,7 @@ function bindChat(root, options = {}) {
1034
1071
  activeAssistant = appendMessage("assistant");
1035
1072
  activeText = null;
1036
1073
  activeTextContent = "";
1074
+ pendingTool = tool("pending", "");
1037
1075
  root.setAttribute("aria-busy", "true");
1038
1076
  if (send) send.disabled = true;
1039
1077
  const result = await session.send(question);
@@ -1055,10 +1093,12 @@ function bindChat(root, options = {}) {
1055
1093
  if (target?.closest("[data-ai-chat-reset]")) {
1056
1094
  session.reset();
1057
1095
  chatThread.replaceChildren(...Array.from(initialThread.childNodes, (node) => node.cloneNode(true)));
1096
+ initialNodes = Array.from(chatThread.childNodes);
1058
1097
  input.value = "";
1059
1098
  activeAssistant = null;
1060
1099
  activeText = null;
1061
1100
  activeTextContent = "";
1101
+ pendingTool = null;
1062
1102
  return;
1063
1103
  }
1064
1104
  const actionElement = target?.closest("[data-ai-action]");
@@ -1083,6 +1123,12 @@ function bindChat(root, options = {}) {
1083
1123
  input.addEventListener("keydown", keydown);
1084
1124
  return {
1085
1125
  session,
1126
+ appendAssistantReply(text) {
1127
+ const content = text.trim();
1128
+ if (!content) return;
1129
+ appendMarkdown(appendMessage("assistant"), content);
1130
+ session.addAssistantReply(content);
1131
+ },
1086
1132
  destroy() {
1087
1133
  if (destroyed) return;
1088
1134
  destroyed = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vettvangur/ai-sdk",
3
- "version": "0.1.11",
3
+ "version": "0.1.13",
4
4
  "description": "Framework-agnostic AI chat client for server-rendered shells",
5
5
  "type": "module",
6
6
  "access": "public",