@butlerbot/sdk 0.0.8 → 0.0.10-alpha
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/config.d.ts +5 -0
- package/dist/config.js +3 -0
- package/dist/modules/conversation.d.ts +10 -1
- package/dist/modules/conversation.js +40 -31
- package/dist/types/usage/policy.d.ts +17 -5
- package/package.json +1 -1
package/dist/config.d.ts
CHANGED
package/dist/config.js
CHANGED
|
@@ -33,6 +33,8 @@ export type ConversationOptions = {
|
|
|
33
33
|
convoPath?: string;
|
|
34
34
|
/** The path to append to the server URL specifying the history API endpoint to use */
|
|
35
35
|
historyPath?: string;
|
|
36
|
+
/** The path to append to the server URL specifying the stream API endpoint to use */
|
|
37
|
+
streamPath?: string;
|
|
36
38
|
/** Whether to enable debug logs */
|
|
37
39
|
debug?: boolean;
|
|
38
40
|
/** The API version to use */
|
|
@@ -55,6 +57,8 @@ export declare class Conversation {
|
|
|
55
57
|
setConversationEndpoint(conversationEndpoint: string): this;
|
|
56
58
|
/** Sets the history endpoint where the quest is sent to, appended to server URL */
|
|
57
59
|
setHistoryEndpoint(historyEndpoint: string): this;
|
|
60
|
+
/** Sets the stream endpoint where the request is sent to, appended to server URL */
|
|
61
|
+
setStreamEndpoint(streamEndpoint: string): this;
|
|
58
62
|
/** Sets the conversation ID, has to be an existing conversation ID, undefined otherwise */
|
|
59
63
|
setConvoId(convoId: string | undefined): this;
|
|
60
64
|
/** Sets the AI model used for the next interaction, e.g Claude-Sonnet, GPT-4 */
|
|
@@ -76,6 +80,8 @@ export declare class Conversation {
|
|
|
76
80
|
getConversationEndpoint(): string;
|
|
77
81
|
/** Gets the current history endpoint */
|
|
78
82
|
getHistoryEndpoint(): string;
|
|
83
|
+
/** Gets the current stream endpoint */
|
|
84
|
+
getStreamEndpoint(): string;
|
|
79
85
|
/** Gets the current options object */
|
|
80
86
|
getModel(): string | undefined;
|
|
81
87
|
/** Gets the current location-specific instructions */
|
|
@@ -84,9 +90,12 @@ export declare class Conversation {
|
|
|
84
90
|
getPlatform(): string | undefined;
|
|
85
91
|
/** Gets the current personality configuration */
|
|
86
92
|
getPersonality(): string | undefined;
|
|
93
|
+
private handleSSE;
|
|
87
94
|
/** Fetches the conversation state from the server, including message history and metadata */
|
|
88
95
|
fetchState(): Promise<ConversationStateResponse>;
|
|
96
|
+
/** Fetches the conversation stream from the server */
|
|
97
|
+
fetchStream(cb: (chunk: RequestResponse) => any): void;
|
|
89
98
|
/** Sends a message into the conversation */
|
|
90
|
-
send(message: string, cb: (chunk: RequestResponse) => any, options?: DialogueRequestOptions):
|
|
99
|
+
send(message: string, cb: (chunk: RequestResponse) => any, options?: DialogueRequestOptions): void;
|
|
91
100
|
}
|
|
92
101
|
export {};
|
|
@@ -21,6 +21,7 @@ class Conversation {
|
|
|
21
21
|
this.endpoints = {
|
|
22
22
|
conversation: serverUrl + (config.convoPath || config.path || config_1.CONFIG.paths.conversation[config.chatApiV || DEFAULT_CONVO_API_V].base),
|
|
23
23
|
history: serverUrl + (config.historyPath || config_1.CONFIG.paths.history.chat.v1.base),
|
|
24
|
+
stream: serverUrl + (config.streamPath || config_1.CONFIG.paths.stream.v4.base),
|
|
24
25
|
};
|
|
25
26
|
this.apiKey = config.apiKey;
|
|
26
27
|
this.debug = config.debug || false;
|
|
@@ -44,6 +45,11 @@ class Conversation {
|
|
|
44
45
|
this.endpoints.history = historyEndpoint;
|
|
45
46
|
return this;
|
|
46
47
|
}
|
|
48
|
+
/** Sets the stream endpoint where the request is sent to, appended to server URL */
|
|
49
|
+
setStreamEndpoint(streamEndpoint) {
|
|
50
|
+
this.endpoints.stream = streamEndpoint;
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
47
53
|
/** Sets the conversation ID, has to be an existing conversation ID, undefined otherwise */
|
|
48
54
|
setConvoId(convoId) {
|
|
49
55
|
this.convoId = convoId;
|
|
@@ -89,6 +95,10 @@ class Conversation {
|
|
|
89
95
|
getHistoryEndpoint() {
|
|
90
96
|
return this.endpoints.history;
|
|
91
97
|
}
|
|
98
|
+
/** Gets the current stream endpoint */
|
|
99
|
+
getStreamEndpoint() {
|
|
100
|
+
return this.endpoints.stream;
|
|
101
|
+
}
|
|
92
102
|
/** Gets the current options object */
|
|
93
103
|
getModel() {
|
|
94
104
|
var _a;
|
|
@@ -109,6 +119,23 @@ class Conversation {
|
|
|
109
119
|
var _a;
|
|
110
120
|
return (_a = this.options) === null || _a === void 0 ? void 0 : _a.personality;
|
|
111
121
|
}
|
|
122
|
+
// SSE HANDLER
|
|
123
|
+
handleSSE(url, cb, options = {}) {
|
|
124
|
+
const sse = new eventsource_1.EventSource(url);
|
|
125
|
+
sse.addEventListener("message", (event) => {
|
|
126
|
+
const data = JSON.parse(event.data);
|
|
127
|
+
const convoId = data.success ? data.data.convoId : undefined;
|
|
128
|
+
if (convoId && options.onConvoId)
|
|
129
|
+
options.onConvoId(convoId);
|
|
130
|
+
cb(data);
|
|
131
|
+
if (data.data.quitStream)
|
|
132
|
+
sse.close();
|
|
133
|
+
});
|
|
134
|
+
sse.addEventListener("error", (event) => {
|
|
135
|
+
if (this.debug)
|
|
136
|
+
console.warn(`[Stream Error: ${url}]`, event);
|
|
137
|
+
});
|
|
138
|
+
}
|
|
112
139
|
// GETTERS
|
|
113
140
|
/** Fetches the conversation state from the server, including message history and metadata */
|
|
114
141
|
fetchState() {
|
|
@@ -125,40 +152,22 @@ class Conversation {
|
|
|
125
152
|
return data;
|
|
126
153
|
});
|
|
127
154
|
}
|
|
155
|
+
/** Fetches the conversation stream from the server */
|
|
156
|
+
fetchStream(cb) {
|
|
157
|
+
if (!this.convoId)
|
|
158
|
+
throw new Error("Conversation ID is not set");
|
|
159
|
+
const url = (0, url_formatter_1.formatURL)(this.endpoints.stream, { chatId: this.convoId }, { apiKey: this.apiKey, debug: this.debug });
|
|
160
|
+
return this.handleSSE(url, cb);
|
|
161
|
+
}
|
|
128
162
|
// LIFE CYCLE
|
|
129
163
|
/** Sends a message into the conversation */
|
|
130
164
|
send(message, cb, options) {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
const sse = new eventsource_1.EventSource(url);
|
|
138
|
-
sse.addEventListener("message", (event) => {
|
|
139
|
-
const data = JSON.parse(event.data);
|
|
140
|
-
const convoId = data.success ? data.data.convoId : undefined;
|
|
141
|
-
if (convoId)
|
|
142
|
-
this.convoId = convoId; // update convo id from response
|
|
143
|
-
cb(data);
|
|
144
|
-
if (data.data.quitStream)
|
|
145
|
-
sse.close();
|
|
146
|
-
});
|
|
147
|
-
sse.addEventListener("error", (event) => {
|
|
148
|
-
if (this.debug)
|
|
149
|
-
console.warn(`[Stream Error: ${url}]`, event);
|
|
150
|
-
cb({
|
|
151
|
-
success: false,
|
|
152
|
-
data: {
|
|
153
|
-
code: "STREAM_ERROR",
|
|
154
|
-
error: "Stream error",
|
|
155
|
-
message: event.message || "An error occurred while streaming",
|
|
156
|
-
quitStream: true
|
|
157
|
-
}
|
|
158
|
-
});
|
|
159
|
-
sse.close();
|
|
160
|
-
});
|
|
161
|
-
});
|
|
165
|
+
const payload = Object.assign(Object.assign({ message }, this.options), options // overwrite convos for this call
|
|
166
|
+
);
|
|
167
|
+
if (this.convoId)
|
|
168
|
+
payload.chatId = this.convoId;
|
|
169
|
+
const url = (0, url_formatter_1.formatURL)(this.endpoints.conversation, payload, { apiKey: this.apiKey, debug: this.debug });
|
|
170
|
+
return this.handleSSE(url, cb, { onConvoId: (convoId) => this.convoId = convoId });
|
|
162
171
|
}
|
|
163
172
|
}
|
|
164
173
|
exports.Conversation = Conversation;
|
|
@@ -20,6 +20,17 @@ export type UsagePolicy = {
|
|
|
20
20
|
rules: PolicyRule[];
|
|
21
21
|
maxConvoLength?: number;
|
|
22
22
|
};
|
|
23
|
+
export type UsagePolicyDataUsage = {
|
|
24
|
+
/** The current user's usage cost for this time period */
|
|
25
|
+
usage: number;
|
|
26
|
+
/** The user's limit for the time period */
|
|
27
|
+
usageLimit: number;
|
|
28
|
+
/** Explanation of limit for this time period */
|
|
29
|
+
limitBreakdown: {
|
|
30
|
+
description: string;
|
|
31
|
+
addition: number;
|
|
32
|
+
}[];
|
|
33
|
+
};
|
|
23
34
|
export type UsagePolicyData = {
|
|
24
35
|
limit: {
|
|
25
36
|
/** Current usage cost as a percentage of daily limit (0–1) */
|
|
@@ -36,10 +47,11 @@ export type UsagePolicyData = {
|
|
|
36
47
|
};
|
|
37
48
|
/** Usage policy information */
|
|
38
49
|
usagePolicy: UsagePolicy;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
50
|
+
/** Usage information */
|
|
51
|
+
usage: {
|
|
52
|
+
/** Daily usage data */
|
|
53
|
+
daily: UsagePolicyDataUsage;
|
|
54
|
+
/** Weekly usage data */
|
|
55
|
+
weekly: UsagePolicyDataUsage;
|
|
44
56
|
};
|
|
45
57
|
};
|