@butlerbot/sdk 0.0.13 → 0.0.14
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.
|
@@ -49,6 +49,7 @@ export declare class Conversation {
|
|
|
49
49
|
apiKey: string;
|
|
50
50
|
private debug;
|
|
51
51
|
private options?;
|
|
52
|
+
private events;
|
|
52
53
|
private endpoints;
|
|
53
54
|
constructor(config: ConversationOptions);
|
|
54
55
|
/**
|
|
@@ -97,6 +98,24 @@ export declare class Conversation {
|
|
|
97
98
|
getPlatform(): string | undefined;
|
|
98
99
|
/** Gets the current personality configuration */
|
|
99
100
|
getPersonality(): string | undefined;
|
|
101
|
+
private hasEmitter;
|
|
102
|
+
private addEmitter;
|
|
103
|
+
private removeEmitter;
|
|
104
|
+
private addListener;
|
|
105
|
+
private removeListener;
|
|
106
|
+
private emit;
|
|
107
|
+
/**
|
|
108
|
+
* Fires when the conversation ID is set
|
|
109
|
+
* if convoId is already set when this is called, fires immediately
|
|
110
|
+
* */
|
|
111
|
+
onConvoId(cb: (convoId: string) => any): string;
|
|
112
|
+
/** Removes a convoId listener */
|
|
113
|
+
offConvoId(listenerId: string): void;
|
|
114
|
+
/**
|
|
115
|
+
* Fires once when the conversation ID is set, then removes the listener.
|
|
116
|
+
* If convoId is already set, fires immediately
|
|
117
|
+
*/
|
|
118
|
+
onceConvoId(cb: (convoId: string) => any): `${string}-${string}-${string}-${string}-${string}` | undefined;
|
|
100
119
|
private handleSSE;
|
|
101
120
|
/** Fetches the conversation state from the server, including message history and metadata */
|
|
102
121
|
fetchState(): Promise<ConversationStateResponse>;
|
|
@@ -16,6 +16,7 @@ const url_formatter_1 = require("../util/url_formatter");
|
|
|
16
16
|
const DEFAULT_CONVO_API_V = "v4";
|
|
17
17
|
class Conversation {
|
|
18
18
|
constructor(config) {
|
|
19
|
+
this.events = new Map();
|
|
19
20
|
this.convoId = config.convoId;
|
|
20
21
|
const serverUrl = config.serverUrl || config_1.CONFIG.server;
|
|
21
22
|
this.endpoints = {
|
|
@@ -129,6 +130,63 @@ class Conversation {
|
|
|
129
130
|
var _a;
|
|
130
131
|
return (_a = this.options) === null || _a === void 0 ? void 0 : _a.personality;
|
|
131
132
|
}
|
|
133
|
+
// EVENT EMITTER
|
|
134
|
+
hasEmitter(event) {
|
|
135
|
+
return this.events.has(event);
|
|
136
|
+
}
|
|
137
|
+
addEmitter(event) {
|
|
138
|
+
if (!this.hasEmitter(event))
|
|
139
|
+
this.events.set(event, new Map());
|
|
140
|
+
}
|
|
141
|
+
removeEmitter(event) {
|
|
142
|
+
this.events.delete(event);
|
|
143
|
+
}
|
|
144
|
+
addListener(event, cb) {
|
|
145
|
+
if (!this.hasEmitter(event))
|
|
146
|
+
this.addEmitter(event);
|
|
147
|
+
const listeners = this.events.get(event);
|
|
148
|
+
const id = crypto.randomUUID();
|
|
149
|
+
listeners.set(id, cb);
|
|
150
|
+
return id;
|
|
151
|
+
}
|
|
152
|
+
removeListener(event, id) {
|
|
153
|
+
const listeners = this.events.get(event);
|
|
154
|
+
if (listeners)
|
|
155
|
+
listeners.delete(id);
|
|
156
|
+
}
|
|
157
|
+
emit(event, ...args) {
|
|
158
|
+
const listeners = this.events.get(event);
|
|
159
|
+
if (listeners)
|
|
160
|
+
listeners.forEach(listener => listener(...args));
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Fires when the conversation ID is set
|
|
164
|
+
* if convoId is already set when this is called, fires immediately
|
|
165
|
+
* */
|
|
166
|
+
onConvoId(cb) {
|
|
167
|
+
if (this.convoId)
|
|
168
|
+
cb(this.convoId);
|
|
169
|
+
return this.addListener("convoId", cb);
|
|
170
|
+
}
|
|
171
|
+
/** Removes a convoId listener */
|
|
172
|
+
offConvoId(listenerId) {
|
|
173
|
+
this.removeListener("convoId", listenerId);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Fires once when the conversation ID is set, then removes the listener.
|
|
177
|
+
* If convoId is already set, fires immediately
|
|
178
|
+
*/
|
|
179
|
+
onceConvoId(cb) {
|
|
180
|
+
if (this.convoId) {
|
|
181
|
+
cb(this.convoId);
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
const id = this.addListener("convoId", (convoId) => {
|
|
185
|
+
cb(convoId);
|
|
186
|
+
this.offConvoId(id);
|
|
187
|
+
});
|
|
188
|
+
return id;
|
|
189
|
+
}
|
|
132
190
|
// SSE HANDLER
|
|
133
191
|
handleSSE(url, cb, options = {}) {
|
|
134
192
|
const sse = new eventsource_1.EventSource(url);
|
|
@@ -198,7 +256,12 @@ class Conversation {
|
|
|
198
256
|
if (this.convoId)
|
|
199
257
|
payload.chatId = this.convoId;
|
|
200
258
|
const url = (0, url_formatter_1.formatURL)(this.endpoints.conversation, payload, { apiKey: this.apiKey, debug: this.debug });
|
|
201
|
-
return this.handleSSE(url, cb, {
|
|
259
|
+
return this.handleSSE(url, cb, {
|
|
260
|
+
onConvoId: (convoId) => {
|
|
261
|
+
this.convoId = convoId;
|
|
262
|
+
this.emit("convoId", convoId);
|
|
263
|
+
}
|
|
264
|
+
});
|
|
202
265
|
}
|
|
203
266
|
}
|
|
204
267
|
exports.Conversation = Conversation;
|