@butlerbot/sdk 0.0.13 → 0.0.15

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.
@@ -2,6 +2,7 @@ import { EventSource } from "eventsource";
2
2
  import { CONFIG } from "../config";
3
3
  import { RequestResponseV4 } from "../types/type_registry";
4
4
  import { ConversationStateResponse } from "../types/state/convo_state_response";
5
+ import { TurnProgressEntry } from "../types/response/v4/turn_registry_v4";
5
6
  export type DialogueRequestParams = {
6
7
  /** Unique identifier for the chat session */
7
8
  chatId?: string;
@@ -49,6 +50,7 @@ export declare class Conversation {
49
50
  apiKey: string;
50
51
  private debug;
51
52
  private options?;
53
+ private events;
52
54
  private endpoints;
53
55
  constructor(config: ConversationOptions);
54
56
  /**
@@ -97,6 +99,24 @@ export declare class Conversation {
97
99
  getPlatform(): string | undefined;
98
100
  /** Gets the current personality configuration */
99
101
  getPersonality(): string | undefined;
102
+ private hasEmitter;
103
+ private addEmitter;
104
+ private removeEmitter;
105
+ private addListener;
106
+ private removeListener;
107
+ private emit;
108
+ /**
109
+ * Fires when the conversation ID is set
110
+ * if convoId is already set when this is called, fires immediately
111
+ * */
112
+ onConvoId(cb: (convoId: string) => any): string;
113
+ /** Removes a convoId listener */
114
+ offConvoId(listenerId: string): void;
115
+ /**
116
+ * Fires once when the conversation ID is set, then removes the listener.
117
+ * If convoId is already set, fires immediately
118
+ */
119
+ onceConvoId(cb: (convoId: string) => any): `${string}-${string}-${string}-${string}-${string}` | undefined;
100
120
  private handleSSE;
101
121
  /** Fetches the conversation state from the server, including message history and metadata */
102
122
  fetchState(): Promise<ConversationStateResponse>;
@@ -106,7 +126,7 @@ export declare class Conversation {
106
126
  * Fetches the conversation progress from the server
107
127
  * Returns undefined if no active turn progress
108
128
  */
109
- fetchProgress(): Promise<RequestResponse[] | undefined>;
129
+ fetchProgress(): Promise<TurnProgressEntry[] | undefined>;
110
130
  /** Sends a message into the conversation */
111
131
  send(message: string, cb: (chunk: RequestResponse) => any, options?: DialogueRequestOptions): EventSource;
112
132
  }
@@ -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, { 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
+ });
202
265
  }
203
266
  }
204
267
  exports.Conversation = Conversation;
@@ -0,0 +1,7 @@
1
+ import { RequestResponseV4 } from "./dialogue_response_v4";
2
+ export type TurnProgressEntry = {
3
+ /** The ID of the buffered event */
4
+ eventId: string;
5
+ /** The actual event payload */
6
+ event: RequestResponseV4;
7
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@butlerbot/sdk",
3
- "version": "0.0.13",
3
+ "version": "0.0.15",
4
4
  "description": "The official ButlerBot SDK",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",