@butlerbot/sdk 0.0.12 → 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,13 +98,34 @@ 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>;
103
122
  /** Fetches the conversation progress stream from the server */
104
123
  fetchProgressStream(cb: (chunk: RequestResponse) => any): EventSource;
105
- /** Fetches the conversation progress from the server */
106
- fetchProgress(): Promise<RequestResponse[]>;
124
+ /**
125
+ * Fetches the conversation progress from the server
126
+ * Returns undefined if no active turn progress
127
+ */
128
+ fetchProgress(): Promise<RequestResponse[] | undefined>;
107
129
  /** Sends a message into the conversation */
108
130
  send(message: string, cb: (chunk: RequestResponse) => any, options?: DialogueRequestOptions): EventSource;
109
131
  }
@@ -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);
@@ -170,7 +228,10 @@ class Conversation {
170
228
  const url = (0, url_formatter_1.formatURL)(this.endpoints.progressStream, { chatId: this.convoId }, { apiKey: this.apiKey, debug: this.debug });
171
229
  return this.handleSSE(url, cb);
172
230
  }
173
- /** Fetches the conversation progress from the server */
231
+ /**
232
+ * Fetches the conversation progress from the server
233
+ * Returns undefined if no active turn progress
234
+ */
174
235
  fetchProgress() {
175
236
  return __awaiter(this, void 0, void 0, function* () {
176
237
  if (!this.convoId)
@@ -182,7 +243,7 @@ class Conversation {
182
243
  throw new Error(`Failed to fetch conversation progress: ${response.status} ${response.statusText} - ${errorText}`);
183
244
  }
184
245
  if (response.status === 204)
185
- return []; // No content, return empty array
246
+ return undefined; // No content
186
247
  const data = yield response.json();
187
248
  return data.events;
188
249
  });
@@ -195,7 +256,12 @@ class Conversation {
195
256
  if (this.convoId)
196
257
  payload.chatId = this.convoId;
197
258
  const url = (0, url_formatter_1.formatURL)(this.endpoints.conversation, payload, { apiKey: this.apiKey, debug: this.debug });
198
- return this.handleSSE(url, cb, { onConvoId: (convoId) => this.convoId = convoId });
259
+ return this.handleSSE(url, cb, {
260
+ onConvoId: (convoId) => {
261
+ this.convoId = convoId;
262
+ this.emit("convoId", convoId);
263
+ }
264
+ });
199
265
  }
200
266
  }
201
267
  exports.Conversation = Conversation;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@butlerbot/sdk",
3
- "version": "0.0.12",
3
+ "version": "0.0.14",
4
4
  "description": "The official ButlerBot SDK",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",