@vettvangur/ai-sdk 0.1.2 → 0.1.5

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
@@ -151,10 +151,19 @@ type AiChatBindings = {
151
151
  antiforgeryToken?: string;
152
152
  timeoutMs?: number;
153
153
  labels?: Partial<AiChatLabels>;
154
+ classes?: Partial<AiChatClasses>;
154
155
  registry?: AiChatRegistry;
155
156
  onEmbedded?: (element: HTMLElement) => void;
156
157
  onError?: (error: Error) => void;
157
158
  };
159
+ type AiChatClasses = {
160
+ messageUser: string;
161
+ messageAssistant: string;
162
+ text: string;
163
+ tool: string;
164
+ card: string;
165
+ error: string;
166
+ };
158
167
  type AiChatBinding = {
159
168
  session: AiChatSession;
160
169
  destroy(): void;
@@ -179,4 +188,4 @@ declare function createAiChatRegistry(): AiChatRegistry;
179
188
  //#region src/stream.d.ts
180
189
  declare function readEventStream(response: Response, signal: AbortSignal): AsyncGenerator<AiChatStreamEvent>;
181
190
  //#endregion
182
- export { AI_CHAT_PROTOCOL_VERSION, type AiAnswerCard, type AiAnswerCardFact, type AiAnswerCardLabels, type AiAnswerCardLink, type AiAnswerCardSection, type AiAnswerCardTable, type AiChatAction, type AiChatBinding, type AiChatBindings, type AiChatContext, type AiChatExtensionContext, type AiChatMessage, type AiChatRegistry, type AiChatRenderer, type AiChatRequest, type AiChatSession, type AiChatSessionOptions, type AiChatSessionResult, type AiChatSessionStatus, type AiChatStreamEvent, type AiChatSurface, bindChat, createAiChatRegistry, createChatSession, parseStreamEvent, readEventStream, renderAnswerCard };
191
+ export { AI_CHAT_PROTOCOL_VERSION, type AiAnswerCard, type AiAnswerCardFact, type AiAnswerCardLabels, type AiAnswerCardLink, type AiAnswerCardSection, type AiAnswerCardTable, type AiChatAction, type AiChatBinding, type AiChatBindings, type AiChatClasses, type AiChatContext, type AiChatExtensionContext, type AiChatMessage, type AiChatRegistry, type AiChatRenderer, type AiChatRequest, type AiChatSession, type AiChatSessionOptions, type AiChatSessionResult, type AiChatSessionStatus, type AiChatStreamEvent, type AiChatSurface, bindChat, createAiChatRegistry, createChatSession, parseStreamEvent, readEventStream, renderAnswerCard };
package/dist/index.js CHANGED
@@ -876,6 +876,14 @@ const defaultLabels = {
876
876
  table: "Published table",
877
877
  tableScroll: "Scroll sideways to see all columns"
878
878
  };
879
+ const defaultClasses = {
880
+ messageUser: "",
881
+ messageAssistant: "",
882
+ text: "",
883
+ tool: "",
884
+ card: "",
885
+ error: ""
886
+ };
879
887
  function dataString(data, name) {
880
888
  if (typeof data !== "object" || data === null || Array.isArray(data)) return null;
881
889
  const value = data[name];
@@ -892,6 +900,11 @@ function bindChat(root, options = {}) {
892
900
  ...defaultLabels,
893
901
  ...options.labels
894
902
  };
903
+ const classes = {
904
+ ...defaultClasses,
905
+ ...options.classes
906
+ };
907
+ const initialThread = chatThread.cloneNode(true);
895
908
  const endpoint = options.endpoint ?? root.dataset.aiChatEndpoint ?? form.action;
896
909
  const header = options.antiforgeryHeader ?? root.dataset.aiChatAntiforgeryHeader;
897
910
  const token = options.antiforgeryToken ?? root.dataset.aiChatAntiforgeryToken;
@@ -907,12 +920,14 @@ function bindChat(root, options = {}) {
907
920
  function appendMessage(role) {
908
921
  const message = root.ownerDocument.createElement("div");
909
922
  message.dataset.aiChatMessage = role;
923
+ message.className = role === "user" ? classes.messageUser : classes.messageAssistant;
910
924
  chatThread.append(message);
911
925
  return message;
912
926
  }
913
927
  function appendMarkdown(parent, text) {
914
928
  const bubble = root.ownerDocument.createElement("div");
915
929
  bubble.dataset.aiChatText = "true";
930
+ bubble.className = classes.text;
916
931
  bubble.append(renderMarkdown(root.ownerDocument, text));
917
932
  parent.append(bubble);
918
933
  }
@@ -920,19 +935,36 @@ function bindChat(root, options = {}) {
920
935
  options.onError?.(error);
921
936
  const message = root.ownerDocument.createElement("p");
922
937
  message.setAttribute("role", "alert");
938
+ message.className = classes.error;
923
939
  message.append(error.name === "TimeoutError" ? labels.timeout : labels.error);
924
940
  chatThread.append(message);
925
941
  }
926
942
  function tool(id, _name) {
927
- const element = root.ownerDocument.createElement("p");
943
+ const element = root.ownerDocument.createElement("div");
928
944
  element.dataset.aiChatTool = id;
929
- element.append(root.ownerDocument.createTextNode(labels.toolRunning));
945
+ element.className = classes.tool;
946
+ const dot = root.ownerDocument.createElement("span");
947
+ dot.className = "ai-chat__tool-dot";
948
+ dot.setAttribute("aria-hidden", "true");
949
+ const label = root.ownerDocument.createElement("span");
950
+ label.append(root.ownerDocument.createTextNode(labels.toolRunning));
951
+ const status = root.ownerDocument.createElement("span");
952
+ status.className = "ai-chat__tool-status sr-only";
953
+ element.append(dot, label, status);
930
954
  activeAssistant?.append(element);
931
955
  return element;
932
956
  }
933
957
  function finishTool(id, failed, interrupted = false) {
934
958
  const element = activeAssistant?.querySelector(`[data-ai-chat-tool="${CSS.escape(id)}"]`);
935
- if (element) element.textContent = interrupted ? labels.toolInterrupted : failed ? labels.toolFailed : labels.toolComplete;
959
+ if (element) {
960
+ element.classList.add("ai-chat__tool--done");
961
+ element.classList.toggle("ai-chat__tool--failed", failed || interrupted);
962
+ const status = element.querySelector(".ai-chat__tool-status");
963
+ if (status) {
964
+ status.textContent = interrupted ? labels.toolInterrupted : failed ? labels.toolFailed : labels.toolComplete;
965
+ status.classList.remove("sr-only");
966
+ }
967
+ }
936
968
  }
937
969
  function renderResult(event) {
938
970
  if (!activeAssistant) return;
@@ -958,6 +990,7 @@ function bindChat(root, options = {}) {
958
990
  if (content) {
959
991
  const wrapper = root.ownerDocument.createElement("div");
960
992
  wrapper.dataset.aiChatCard = "true";
993
+ wrapper.className = classes.card;
961
994
  wrapper.append(content);
962
995
  activeAssistant.append(wrapper);
963
996
  options.onEmbedded?.(wrapper);
@@ -975,6 +1008,7 @@ function bindChat(root, options = {}) {
975
1008
  const question = input.value.trim();
976
1009
  if (!question || session.status !== "idle") return;
977
1010
  input.value = "";
1011
+ chatThread.replaceChildren();
978
1012
  appendMarkdown(appendMessage("user"), question);
979
1013
  activeAssistant = appendMessage("assistant");
980
1014
  root.setAttribute("aria-busy", "true");
@@ -997,7 +1031,7 @@ function bindChat(root, options = {}) {
997
1031
  }
998
1032
  if (target?.closest("[data-ai-chat-reset]")) {
999
1033
  session.reset();
1000
- chatThread.replaceChildren();
1034
+ chatThread.replaceChildren(...Array.from(initialThread.childNodes, (node) => node.cloneNode(true)));
1001
1035
  input.value = "";
1002
1036
  return;
1003
1037
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vettvangur/ai-sdk",
3
- "version": "0.1.2",
3
+ "version": "0.1.5",
4
4
  "description": "Framework-agnostic AI chat client for server-rendered shells",
5
5
  "type": "module",
6
6
  "access": "public",