@vettvangur/ai-sdk 0.1.2 → 0.1.4
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 +10 -1
- package/dist/index.js +19 -1
- package/package.json +1 -1
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,12 +935,14 @@ 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
943
|
const element = root.ownerDocument.createElement("p");
|
|
928
944
|
element.dataset.aiChatTool = id;
|
|
945
|
+
element.className = classes.tool;
|
|
929
946
|
element.append(root.ownerDocument.createTextNode(labels.toolRunning));
|
|
930
947
|
activeAssistant?.append(element);
|
|
931
948
|
return element;
|
|
@@ -958,6 +975,7 @@ function bindChat(root, options = {}) {
|
|
|
958
975
|
if (content) {
|
|
959
976
|
const wrapper = root.ownerDocument.createElement("div");
|
|
960
977
|
wrapper.dataset.aiChatCard = "true";
|
|
978
|
+
wrapper.className = classes.card;
|
|
961
979
|
wrapper.append(content);
|
|
962
980
|
activeAssistant.append(wrapper);
|
|
963
981
|
options.onEmbedded?.(wrapper);
|
|
@@ -997,7 +1015,7 @@ function bindChat(root, options = {}) {
|
|
|
997
1015
|
}
|
|
998
1016
|
if (target?.closest("[data-ai-chat-reset]")) {
|
|
999
1017
|
session.reset();
|
|
1000
|
-
chatThread.replaceChildren();
|
|
1018
|
+
chatThread.replaceChildren(...Array.from(initialThread.childNodes, (node) => node.cloneNode(true)));
|
|
1001
1019
|
input.value = "";
|
|
1002
1020
|
return;
|
|
1003
1021
|
}
|