@vettvangur/ai-sdk 0.1.12 → 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 +3 -0
- package/dist/index.js +31 -0
- package/package.json +1 -1
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
|
},
|
|
@@ -1098,6 +1123,12 @@ function bindChat(root, options = {}) {
|
|
|
1098
1123
|
input.addEventListener("keydown", keydown);
|
|
1099
1124
|
return {
|
|
1100
1125
|
session,
|
|
1126
|
+
appendAssistantReply(text) {
|
|
1127
|
+
const content = text.trim();
|
|
1128
|
+
if (!content) return;
|
|
1129
|
+
appendMarkdown(appendMessage("assistant"), content);
|
|
1130
|
+
session.addAssistantReply(content);
|
|
1131
|
+
},
|
|
1101
1132
|
destroy() {
|
|
1102
1133
|
if (destroyed) return;
|
|
1103
1134
|
destroyed = true;
|