@buni.ai/chatbot-core 1.0.0
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/README.md +133 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.esm.js +226 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.js +232 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +106 -0
- package/dist/widget.d.ts +51 -0
- package/package.json +45 -0
package/README.md
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# @buni.ai/chatbot-core
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/js/%40buni.ai%2Fchatbot-core)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
|
|
6
|
+
Core functionality for BuniAI Chat Widget adapters - provides framework-agnostic utilities and API for seamless chatbot integration.
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- 🚀 **Framework Agnostic** - Works with any JavaScript framework or vanilla JS
|
|
11
|
+
- 📦 **TypeScript Ready** - Full type definitions included
|
|
12
|
+
- 🎯 **Event-Driven** - Comprehensive event system for real-time updates
|
|
13
|
+
- 🔧 **Configurable** - Extensive customization options
|
|
14
|
+
- 📱 **Mobile Friendly** - Responsive design and touch support
|
|
15
|
+
- 🎨 **Themeable** - Multiple built-in themes and custom styling options
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @buni.ai/chatbot-core
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Quick Start
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { BuniChatWidget } from '@buni.ai/chatbot-core';
|
|
27
|
+
|
|
28
|
+
// Initialize the widget
|
|
29
|
+
const widget = new BuniChatWidget();
|
|
30
|
+
|
|
31
|
+
await widget.initialize({
|
|
32
|
+
token: 'your-embed-token',
|
|
33
|
+
config: {
|
|
34
|
+
theme: 'default',
|
|
35
|
+
position: 'bottom-right',
|
|
36
|
+
welcomeMessage: 'Hello! How can we help you today?'
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// Control the widget
|
|
41
|
+
widget.show();
|
|
42
|
+
widget.hide();
|
|
43
|
+
widget.toggle();
|
|
44
|
+
|
|
45
|
+
// Set customer data
|
|
46
|
+
widget.setCustomerData({
|
|
47
|
+
name: 'John Doe',
|
|
48
|
+
email: 'john@example.com',
|
|
49
|
+
userId: 'user123'
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// Listen for events
|
|
53
|
+
widget.on('new_message', (data) => {
|
|
54
|
+
console.log('New message:', data.message);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
widget.on('visibility_changed', (data) => {
|
|
58
|
+
console.log('Widget visibility:', data.visibility);
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## API Reference
|
|
63
|
+
|
|
64
|
+
### Configuration Options
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
interface BuniChatConfig {
|
|
68
|
+
theme?: 'default' | 'dark' | 'light' | 'corporate' | 'friendly' | 'minimal';
|
|
69
|
+
primaryColor?: string;
|
|
70
|
+
secondaryColor?: string;
|
|
71
|
+
companyName?: string;
|
|
72
|
+
customAvatar?: string;
|
|
73
|
+
welcomeMessage?: string;
|
|
74
|
+
position?: 'bottom-right' | 'bottom-left' | 'top-right' | 'top-left';
|
|
75
|
+
width?: number;
|
|
76
|
+
height?: number;
|
|
77
|
+
triggerText?: string;
|
|
78
|
+
autoOpen?: boolean;
|
|
79
|
+
showMinimize?: boolean;
|
|
80
|
+
showBranding?: boolean;
|
|
81
|
+
enableMobile?: boolean;
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Widget Methods
|
|
86
|
+
|
|
87
|
+
- `initialize(options)` - Initialize the widget with configuration
|
|
88
|
+
- `destroy()` - Clean up and remove the widget
|
|
89
|
+
- `show()` - Show the chat widget
|
|
90
|
+
- `hide()` - Hide the chat widget
|
|
91
|
+
- `toggle()` - Toggle widget visibility
|
|
92
|
+
- `minimize()` - Minimize the widget
|
|
93
|
+
- `maximize()` - Maximize the widget
|
|
94
|
+
- `setCustomerData(data)` - Set customer information
|
|
95
|
+
- `getCustomerData()` - Get current customer data
|
|
96
|
+
- `setSessionVariables(variables)` - Set session variables
|
|
97
|
+
- `sendMessage(message)` - Send a message programmatically
|
|
98
|
+
- `clearChat()` - Clear chat history
|
|
99
|
+
|
|
100
|
+
### Events
|
|
101
|
+
|
|
102
|
+
- `ready` - Widget is loaded and ready
|
|
103
|
+
- `visibility_changed` - Widget visibility changed
|
|
104
|
+
- `new_message` - New message received
|
|
105
|
+
- `minimized` - Widget was minimized
|
|
106
|
+
- `maximized` - Widget was maximized
|
|
107
|
+
- `error` - An error occurred
|
|
108
|
+
|
|
109
|
+
## TypeScript Support
|
|
110
|
+
|
|
111
|
+
This package includes comprehensive TypeScript definitions:
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
import {
|
|
115
|
+
BuniChatWidget,
|
|
116
|
+
BuniChatOptions,
|
|
117
|
+
BuniChatConfig,
|
|
118
|
+
CustomerData,
|
|
119
|
+
SessionVariables
|
|
120
|
+
} from '@buni.ai/chatbot-core';
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## Framework Adapters
|
|
124
|
+
|
|
125
|
+
For framework-specific implementations, use these packages:
|
|
126
|
+
|
|
127
|
+
- **React**: `@buni.ai/chatbot-react`
|
|
128
|
+
- **Vue**: `@buni.ai/chatbot-vue`
|
|
129
|
+
- **Angular**: `@buni.ai/chatbot-angular`
|
|
130
|
+
|
|
131
|
+
## License
|
|
132
|
+
|
|
133
|
+
MIT © BuniAI Team
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
// Core widget loader class
|
|
2
|
+
class BuniChatWidget {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.state = {
|
|
5
|
+
isOpen: false,
|
|
6
|
+
isLoaded: false,
|
|
7
|
+
isMinimized: false,
|
|
8
|
+
unreadCount: 0,
|
|
9
|
+
};
|
|
10
|
+
this.eventListeners = new Map();
|
|
11
|
+
this.widgetElement = null;
|
|
12
|
+
this.customerData = null;
|
|
13
|
+
this.sessionVariables = null;
|
|
14
|
+
}
|
|
15
|
+
async initialize(options) {
|
|
16
|
+
this.options = options;
|
|
17
|
+
try {
|
|
18
|
+
await this.loadWidget();
|
|
19
|
+
this.state.isLoaded = true;
|
|
20
|
+
this.emit("ready", { timestamp: Date.now() });
|
|
21
|
+
if (options.onReady) {
|
|
22
|
+
options.onReady({ timestamp: Date.now() });
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
const errorObj = error instanceof Error ? error : new Error(String(error));
|
|
27
|
+
this.emit("error", { error: errorObj, context: "initialization" });
|
|
28
|
+
if (options.onError) {
|
|
29
|
+
options.onError(errorObj);
|
|
30
|
+
}
|
|
31
|
+
throw errorObj;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
async loadWidget() {
|
|
35
|
+
return new Promise((resolve, reject) => {
|
|
36
|
+
// Check if widget is already loaded
|
|
37
|
+
if (window.BuniChat) {
|
|
38
|
+
resolve();
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
// Load the widget script dynamically
|
|
42
|
+
const script = document.createElement("script");
|
|
43
|
+
script.src = `${this.getBaseUrl()}/api/embed/widget?token=${encodeURIComponent(this.options.token)}`;
|
|
44
|
+
script.async = true;
|
|
45
|
+
script.onload = () => {
|
|
46
|
+
// Initialize the actual widget
|
|
47
|
+
this.initializeWidgetAPI();
|
|
48
|
+
resolve();
|
|
49
|
+
};
|
|
50
|
+
script.onerror = () => {
|
|
51
|
+
reject(new Error("Failed to load BuniAI widget script"));
|
|
52
|
+
};
|
|
53
|
+
document.head.appendChild(script);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
initializeWidgetAPI() {
|
|
57
|
+
// Set up communication with the loaded widget
|
|
58
|
+
if (window.BuniChat) {
|
|
59
|
+
// Configure the widget with our options
|
|
60
|
+
window.BuniChat.configure(this.options.config || {});
|
|
61
|
+
// Set up event forwarding from the widget to our event system
|
|
62
|
+
window.BuniChat.on("visibility_changed", (data) => {
|
|
63
|
+
this.state.isOpen = data.visibility === "visible";
|
|
64
|
+
this.emit("visibility_changed", data);
|
|
65
|
+
if (this.options.onVisibilityChanged) {
|
|
66
|
+
this.options.onVisibilityChanged(data);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
window.BuniChat.on("new_message", (data) => {
|
|
70
|
+
if (data.isFromBot) {
|
|
71
|
+
this.state.unreadCount++;
|
|
72
|
+
}
|
|
73
|
+
this.emit("new_message", data);
|
|
74
|
+
if (this.options.onNewMessage) {
|
|
75
|
+
this.options.onNewMessage(data);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
window.BuniChat.on("minimized", (data) => {
|
|
79
|
+
this.state.isMinimized = true;
|
|
80
|
+
this.emit("minimized", data);
|
|
81
|
+
});
|
|
82
|
+
window.BuniChat.on("maximized", (data) => {
|
|
83
|
+
this.state.isMinimized = false;
|
|
84
|
+
this.emit("maximized", data);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
getBaseUrl() {
|
|
89
|
+
// Return the base URL for the BuniAI platform
|
|
90
|
+
return globalThis.BUNI_API_URL || window.location.origin;
|
|
91
|
+
}
|
|
92
|
+
destroy() {
|
|
93
|
+
if (window.BuniChat) {
|
|
94
|
+
window.BuniChat.destroy();
|
|
95
|
+
}
|
|
96
|
+
if (this.widgetElement) {
|
|
97
|
+
this.widgetElement.remove();
|
|
98
|
+
this.widgetElement = null;
|
|
99
|
+
}
|
|
100
|
+
this.eventListeners.clear();
|
|
101
|
+
this.state.isLoaded = false;
|
|
102
|
+
this.customerData = null;
|
|
103
|
+
this.sessionVariables = null;
|
|
104
|
+
}
|
|
105
|
+
show() {
|
|
106
|
+
if (window.BuniChat) {
|
|
107
|
+
window.BuniChat.show();
|
|
108
|
+
}
|
|
109
|
+
this.state.isOpen = true;
|
|
110
|
+
this.state.unreadCount = 0;
|
|
111
|
+
this.emit("visibility_changed", { visibility: "visible" });
|
|
112
|
+
}
|
|
113
|
+
hide() {
|
|
114
|
+
if (window.BuniChat) {
|
|
115
|
+
window.BuniChat.hide();
|
|
116
|
+
}
|
|
117
|
+
this.state.isOpen = false;
|
|
118
|
+
this.emit("visibility_changed", { visibility: "hidden" });
|
|
119
|
+
}
|
|
120
|
+
toggle() {
|
|
121
|
+
this.state.isOpen ? this.hide() : this.show();
|
|
122
|
+
}
|
|
123
|
+
minimize() {
|
|
124
|
+
if (window.BuniChat) {
|
|
125
|
+
window.BuniChat.minimize();
|
|
126
|
+
}
|
|
127
|
+
this.state.isMinimized = true;
|
|
128
|
+
this.emit("minimized", { timestamp: Date.now() });
|
|
129
|
+
}
|
|
130
|
+
maximize() {
|
|
131
|
+
if (window.BuniChat) {
|
|
132
|
+
window.BuniChat.maximize();
|
|
133
|
+
}
|
|
134
|
+
this.state.isMinimized = false;
|
|
135
|
+
this.emit("maximized", { timestamp: Date.now() });
|
|
136
|
+
}
|
|
137
|
+
setCustomerData(data) {
|
|
138
|
+
this.customerData = { ...this.customerData, ...data };
|
|
139
|
+
if (window.BuniChat) {
|
|
140
|
+
window.BuniChat.setCustomerData(this.customerData);
|
|
141
|
+
}
|
|
142
|
+
this.emit("customer_data_updated", { data: this.customerData });
|
|
143
|
+
}
|
|
144
|
+
getCustomerData() {
|
|
145
|
+
return this.customerData ? { ...this.customerData } : null;
|
|
146
|
+
}
|
|
147
|
+
setSessionVariables(variables) {
|
|
148
|
+
this.sessionVariables = { ...this.sessionVariables, ...variables };
|
|
149
|
+
if (window.BuniChat) {
|
|
150
|
+
window.BuniChat.setSessionVariables(this.sessionVariables);
|
|
151
|
+
}
|
|
152
|
+
this.emit("session_updated", { variables: this.sessionVariables });
|
|
153
|
+
}
|
|
154
|
+
getSessionVariables() {
|
|
155
|
+
return this.sessionVariables ? { ...this.sessionVariables } : null;
|
|
156
|
+
}
|
|
157
|
+
sendMessage(message) {
|
|
158
|
+
if (window.BuniChat) {
|
|
159
|
+
window.BuniChat.sendMessage(message);
|
|
160
|
+
}
|
|
161
|
+
const messageData = {
|
|
162
|
+
message,
|
|
163
|
+
isFromBot: false,
|
|
164
|
+
timestamp: Date.now(),
|
|
165
|
+
messageId: `msg_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
|
|
166
|
+
};
|
|
167
|
+
this.emit("new_message", messageData);
|
|
168
|
+
}
|
|
169
|
+
clearChat() {
|
|
170
|
+
if (window.BuniChat) {
|
|
171
|
+
window.BuniChat.clearChat();
|
|
172
|
+
}
|
|
173
|
+
this.state.unreadCount = 0;
|
|
174
|
+
}
|
|
175
|
+
on(event, callback) {
|
|
176
|
+
if (!this.eventListeners.has(event)) {
|
|
177
|
+
this.eventListeners.set(event, []);
|
|
178
|
+
}
|
|
179
|
+
this.eventListeners.get(event).push(callback);
|
|
180
|
+
}
|
|
181
|
+
off(event, callback) {
|
|
182
|
+
const listeners = this.eventListeners.get(event);
|
|
183
|
+
if (!listeners)
|
|
184
|
+
return;
|
|
185
|
+
if (callback) {
|
|
186
|
+
const index = listeners.indexOf(callback);
|
|
187
|
+
if (index > -1) {
|
|
188
|
+
listeners.splice(index, 1);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
this.eventListeners.set(event, []);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
emit(event, data) {
|
|
196
|
+
const listeners = this.eventListeners.get(event);
|
|
197
|
+
if (listeners) {
|
|
198
|
+
listeners.forEach((callback) => {
|
|
199
|
+
try {
|
|
200
|
+
callback(data);
|
|
201
|
+
}
|
|
202
|
+
catch (error) {
|
|
203
|
+
console.error(`Error in event listener for ${event}:`, error);
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
getState() {
|
|
209
|
+
return { ...this.state };
|
|
210
|
+
}
|
|
211
|
+
isReady() {
|
|
212
|
+
return this.state.isLoaded;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
// Utility function - named export
|
|
216
|
+
function createBuniChatWidget(options) {
|
|
217
|
+
const widget = new BuniChatWidget();
|
|
218
|
+
widget.initialize(options);
|
|
219
|
+
return widget;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Main exports for @buni.ai/chatbot-core
|
|
223
|
+
// Export all types
|
|
224
|
+
|
|
225
|
+
export { BuniChatWidget, createBuniChatWidget, BuniChatWidget as default };
|
|
226
|
+
//# sourceMappingURL=index.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.esm.js","sources":["../src/widget.ts","../src/index.ts"],"sourcesContent":["import {\n BuniChatAPI,\n BuniChatOptions,\n BuniChatState,\n CustomerData,\n SessionVariables,\n} from \"./types\";\n\n// Core widget loader class\nexport class BuniChatWidget implements BuniChatAPI {\n private options!: BuniChatOptions;\n private state: BuniChatState;\n private eventListeners: Map<string, Function[]>;\n private widgetElement: HTMLElement | null;\n private customerData: CustomerData | null;\n private sessionVariables: SessionVariables | null;\n\n constructor() {\n this.state = {\n isOpen: false,\n isLoaded: false,\n isMinimized: false,\n unreadCount: 0,\n };\n this.eventListeners = new Map();\n this.widgetElement = null;\n this.customerData = null;\n this.sessionVariables = null;\n }\n\n async initialize(options: BuniChatOptions): Promise<void> {\n this.options = options;\n\n try {\n await this.loadWidget();\n this.state.isLoaded = true;\n this.emit(\"ready\", { timestamp: Date.now() });\n\n if (options.onReady) {\n options.onReady({ timestamp: Date.now() });\n }\n } catch (error) {\n const errorObj =\n error instanceof Error ? error : new Error(String(error));\n this.emit(\"error\", { error: errorObj, context: \"initialization\" });\n\n if (options.onError) {\n options.onError(errorObj);\n }\n throw errorObj;\n }\n }\n\n private async loadWidget(): Promise<void> {\n return new Promise((resolve, reject) => {\n // Check if widget is already loaded\n if (window.BuniChat) {\n resolve();\n return;\n }\n\n // Load the widget script dynamically\n const script = document.createElement(\"script\");\n script.src = `${this.getBaseUrl()}/api/embed/widget?token=${encodeURIComponent(this.options.token)}`;\n script.async = true;\n\n script.onload = () => {\n // Initialize the actual widget\n this.initializeWidgetAPI();\n resolve();\n };\n\n script.onerror = () => {\n reject(new Error(\"Failed to load BuniAI widget script\"));\n };\n\n document.head.appendChild(script);\n });\n }\n\n private initializeWidgetAPI(): void {\n // Set up communication with the loaded widget\n if (window.BuniChat) {\n // Configure the widget with our options\n window.BuniChat.configure(this.options.config || {});\n\n // Set up event forwarding from the widget to our event system\n window.BuniChat.on(\"visibility_changed\", (data: any) => {\n this.state.isOpen = data.visibility === \"visible\";\n this.emit(\"visibility_changed\", data);\n\n if (this.options.onVisibilityChanged) {\n this.options.onVisibilityChanged(data);\n }\n });\n\n window.BuniChat.on(\"new_message\", (data: any) => {\n if (data.isFromBot) {\n this.state.unreadCount++;\n }\n this.emit(\"new_message\", data);\n\n if (this.options.onNewMessage) {\n this.options.onNewMessage(data);\n }\n });\n\n window.BuniChat.on(\"minimized\", (data: any) => {\n this.state.isMinimized = true;\n this.emit(\"minimized\", data);\n });\n\n window.BuniChat.on(\"maximized\", (data: any) => {\n this.state.isMinimized = false;\n this.emit(\"maximized\", data);\n });\n }\n }\n\n private getBaseUrl(): string {\n // Return the base URL for the BuniAI platform\n return (globalThis as any).BUNI_API_URL || window.location.origin;\n }\n\n destroy(): void {\n if (window.BuniChat) {\n window.BuniChat.destroy();\n }\n\n if (this.widgetElement) {\n this.widgetElement.remove();\n this.widgetElement = null;\n }\n\n this.eventListeners.clear();\n this.state.isLoaded = false;\n this.customerData = null;\n this.sessionVariables = null;\n }\n\n show(): void {\n if (window.BuniChat) {\n window.BuniChat.show();\n }\n this.state.isOpen = true;\n this.state.unreadCount = 0;\n this.emit(\"visibility_changed\", { visibility: \"visible\" });\n }\n\n hide(): void {\n if (window.BuniChat) {\n window.BuniChat.hide();\n }\n this.state.isOpen = false;\n this.emit(\"visibility_changed\", { visibility: \"hidden\" });\n }\n\n toggle(): void {\n this.state.isOpen ? this.hide() : this.show();\n }\n\n minimize(): void {\n if (window.BuniChat) {\n window.BuniChat.minimize();\n }\n this.state.isMinimized = true;\n this.emit(\"minimized\", { timestamp: Date.now() });\n }\n\n maximize(): void {\n if (window.BuniChat) {\n window.BuniChat.maximize();\n }\n this.state.isMinimized = false;\n this.emit(\"maximized\", { timestamp: Date.now() });\n }\n\n setCustomerData(data: CustomerData): void {\n this.customerData = { ...this.customerData, ...data };\n\n if (window.BuniChat) {\n window.BuniChat.setCustomerData(this.customerData);\n }\n\n this.emit(\"customer_data_updated\", { data: this.customerData });\n }\n\n getCustomerData(): CustomerData | null {\n return this.customerData ? { ...this.customerData } : null;\n }\n\n setSessionVariables(variables: SessionVariables): void {\n this.sessionVariables = { ...this.sessionVariables, ...variables };\n\n if (window.BuniChat) {\n window.BuniChat.setSessionVariables(this.sessionVariables);\n }\n\n this.emit(\"session_updated\", { variables: this.sessionVariables });\n }\n\n getSessionVariables(): SessionVariables | null {\n return this.sessionVariables ? { ...this.sessionVariables } : null;\n }\n\n sendMessage(message: string): void {\n if (window.BuniChat) {\n window.BuniChat.sendMessage(message);\n }\n\n const messageData = {\n message,\n isFromBot: false,\n timestamp: Date.now(),\n messageId: `msg_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,\n };\n\n this.emit(\"new_message\", messageData);\n }\n\n clearChat(): void {\n if (window.BuniChat) {\n window.BuniChat.clearChat();\n }\n this.state.unreadCount = 0;\n }\n\n on(event: string, callback: Function): void {\n if (!this.eventListeners.has(event)) {\n this.eventListeners.set(event, []);\n }\n this.eventListeners.get(event)!.push(callback);\n }\n\n off(event: string, callback?: Function): void {\n const listeners = this.eventListeners.get(event);\n if (!listeners) return;\n\n if (callback) {\n const index = listeners.indexOf(callback);\n if (index > -1) {\n listeners.splice(index, 1);\n }\n } else {\n this.eventListeners.set(event, []);\n }\n }\n\n emit(event: string, data?: any): void {\n const listeners = this.eventListeners.get(event);\n if (listeners) {\n listeners.forEach((callback) => {\n try {\n callback(data);\n } catch (error) {\n console.error(`Error in event listener for ${event}:`, error);\n }\n });\n }\n }\n\n getState(): BuniChatState {\n return { ...this.state };\n }\n\n isReady(): boolean {\n return this.state.isLoaded;\n }\n}\n\n// Utility function - named export\nexport function createBuniChatWidget(options: BuniChatOptions): BuniChatWidget {\n const widget = new BuniChatWidget();\n widget.initialize(options);\n return widget;\n}\n\n// Default export - the main widget class\nexport default BuniChatWidget;\n\n// Global type declaration for the widget API\ndeclare global {\n interface Window {\n BuniChat?: {\n configure: (config: any) => void;\n show: () => void;\n hide: () => void;\n minimize: () => void;\n maximize: () => void;\n destroy: () => void;\n setCustomerData: (data: CustomerData) => void;\n setSessionVariables: (variables: SessionVariables) => void;\n sendMessage: (message: string) => void;\n clearChat: () => void;\n on: (event: string, callback: Function) => void;\n off: (event: string, callback?: Function) => void;\n };\n }\n}\n","// Main exports for @buni.ai/chatbot-core\n\n// Export all types\nexport * from \"./types\";\n\n// Export everything from widget\nexport * from \"./widget\";\n\n// Make BuniChatWidget the default export as well\nimport { BuniChatWidget } from \"./widget\";\nexport default BuniChatWidget;\n"],"names":[],"mappings":"AAQA;MACa,cAAc,CAAA;AAQzB,IAAA,WAAA,GAAA;QACE,IAAI,CAAC,KAAK,GAAG;AACX,YAAA,MAAM,EAAE,KAAK;AACb,YAAA,QAAQ,EAAE,KAAK;AACf,YAAA,WAAW,EAAE,KAAK;AAClB,YAAA,WAAW,EAAE,CAAC;SACf;AACD,QAAA,IAAI,CAAC,cAAc,GAAG,IAAI,GAAG,EAAE;AAC/B,QAAA,IAAI,CAAC,aAAa,GAAG,IAAI;AACzB,QAAA,IAAI,CAAC,YAAY,GAAG,IAAI;AACxB,QAAA,IAAI,CAAC,gBAAgB,GAAG,IAAI;IAC9B;IAEA,MAAM,UAAU,CAAC,OAAwB,EAAA;AACvC,QAA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4BAAA,EAA+B,KAAK,CAAA,CAAA,CAAG,EAAE,KAAK,CAAC;gBAC/D;AACF,YAAA,CAAC,CAAC;QACJ;IACF;IAEA,QAAQ,GAAA;AACN,QAAA,OAAO,EAAE,GAAG,IAAI,CAAC,KAAK,EAAE;IAC1B;IAEA,OAAO,GAAA;AACL,QAAA,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ;IAC5B;AACD;AAED;AACM,SAAU,oBAAoB,CAAC,OAAwB,EAAA;AAC3D,IAAA,MAAM,MAAM,GAAG,IAAI,cAAc,EAAE;AACnC,IAAA,MAAM,CAAC,UAAU,CAAC,OAAO,CAAC;AAC1B,IAAA,OAAO,MAAM;AACf;;ACnRA;AAEA;;;;"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
// Core widget loader class
|
|
6
|
+
class BuniChatWidget {
|
|
7
|
+
constructor() {
|
|
8
|
+
this.state = {
|
|
9
|
+
isOpen: false,
|
|
10
|
+
isLoaded: false,
|
|
11
|
+
isMinimized: false,
|
|
12
|
+
unreadCount: 0,
|
|
13
|
+
};
|
|
14
|
+
this.eventListeners = new Map();
|
|
15
|
+
this.widgetElement = null;
|
|
16
|
+
this.customerData = null;
|
|
17
|
+
this.sessionVariables = null;
|
|
18
|
+
}
|
|
19
|
+
async initialize(options) {
|
|
20
|
+
this.options = options;
|
|
21
|
+
try {
|
|
22
|
+
await this.loadWidget();
|
|
23
|
+
this.state.isLoaded = true;
|
|
24
|
+
this.emit("ready", { timestamp: Date.now() });
|
|
25
|
+
if (options.onReady) {
|
|
26
|
+
options.onReady({ timestamp: Date.now() });
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
const errorObj = error instanceof Error ? error : new Error(String(error));
|
|
31
|
+
this.emit("error", { error: errorObj, context: "initialization" });
|
|
32
|
+
if (options.onError) {
|
|
33
|
+
options.onError(errorObj);
|
|
34
|
+
}
|
|
35
|
+
throw errorObj;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
async loadWidget() {
|
|
39
|
+
return new Promise((resolve, reject) => {
|
|
40
|
+
// Check if widget is already loaded
|
|
41
|
+
if (window.BuniChat) {
|
|
42
|
+
resolve();
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
// Load the widget script dynamically
|
|
46
|
+
const script = document.createElement("script");
|
|
47
|
+
script.src = `${this.getBaseUrl()}/api/embed/widget?token=${encodeURIComponent(this.options.token)}`;
|
|
48
|
+
script.async = true;
|
|
49
|
+
script.onload = () => {
|
|
50
|
+
// Initialize the actual widget
|
|
51
|
+
this.initializeWidgetAPI();
|
|
52
|
+
resolve();
|
|
53
|
+
};
|
|
54
|
+
script.onerror = () => {
|
|
55
|
+
reject(new Error("Failed to load BuniAI widget script"));
|
|
56
|
+
};
|
|
57
|
+
document.head.appendChild(script);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
initializeWidgetAPI() {
|
|
61
|
+
// Set up communication with the loaded widget
|
|
62
|
+
if (window.BuniChat) {
|
|
63
|
+
// Configure the widget with our options
|
|
64
|
+
window.BuniChat.configure(this.options.config || {});
|
|
65
|
+
// Set up event forwarding from the widget to our event system
|
|
66
|
+
window.BuniChat.on("visibility_changed", (data) => {
|
|
67
|
+
this.state.isOpen = data.visibility === "visible";
|
|
68
|
+
this.emit("visibility_changed", data);
|
|
69
|
+
if (this.options.onVisibilityChanged) {
|
|
70
|
+
this.options.onVisibilityChanged(data);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
window.BuniChat.on("new_message", (data) => {
|
|
74
|
+
if (data.isFromBot) {
|
|
75
|
+
this.state.unreadCount++;
|
|
76
|
+
}
|
|
77
|
+
this.emit("new_message", data);
|
|
78
|
+
if (this.options.onNewMessage) {
|
|
79
|
+
this.options.onNewMessage(data);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
window.BuniChat.on("minimized", (data) => {
|
|
83
|
+
this.state.isMinimized = true;
|
|
84
|
+
this.emit("minimized", data);
|
|
85
|
+
});
|
|
86
|
+
window.BuniChat.on("maximized", (data) => {
|
|
87
|
+
this.state.isMinimized = false;
|
|
88
|
+
this.emit("maximized", data);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
getBaseUrl() {
|
|
93
|
+
// Return the base URL for the BuniAI platform
|
|
94
|
+
return globalThis.BUNI_API_URL || window.location.origin;
|
|
95
|
+
}
|
|
96
|
+
destroy() {
|
|
97
|
+
if (window.BuniChat) {
|
|
98
|
+
window.BuniChat.destroy();
|
|
99
|
+
}
|
|
100
|
+
if (this.widgetElement) {
|
|
101
|
+
this.widgetElement.remove();
|
|
102
|
+
this.widgetElement = null;
|
|
103
|
+
}
|
|
104
|
+
this.eventListeners.clear();
|
|
105
|
+
this.state.isLoaded = false;
|
|
106
|
+
this.customerData = null;
|
|
107
|
+
this.sessionVariables = null;
|
|
108
|
+
}
|
|
109
|
+
show() {
|
|
110
|
+
if (window.BuniChat) {
|
|
111
|
+
window.BuniChat.show();
|
|
112
|
+
}
|
|
113
|
+
this.state.isOpen = true;
|
|
114
|
+
this.state.unreadCount = 0;
|
|
115
|
+
this.emit("visibility_changed", { visibility: "visible" });
|
|
116
|
+
}
|
|
117
|
+
hide() {
|
|
118
|
+
if (window.BuniChat) {
|
|
119
|
+
window.BuniChat.hide();
|
|
120
|
+
}
|
|
121
|
+
this.state.isOpen = false;
|
|
122
|
+
this.emit("visibility_changed", { visibility: "hidden" });
|
|
123
|
+
}
|
|
124
|
+
toggle() {
|
|
125
|
+
this.state.isOpen ? this.hide() : this.show();
|
|
126
|
+
}
|
|
127
|
+
minimize() {
|
|
128
|
+
if (window.BuniChat) {
|
|
129
|
+
window.BuniChat.minimize();
|
|
130
|
+
}
|
|
131
|
+
this.state.isMinimized = true;
|
|
132
|
+
this.emit("minimized", { timestamp: Date.now() });
|
|
133
|
+
}
|
|
134
|
+
maximize() {
|
|
135
|
+
if (window.BuniChat) {
|
|
136
|
+
window.BuniChat.maximize();
|
|
137
|
+
}
|
|
138
|
+
this.state.isMinimized = false;
|
|
139
|
+
this.emit("maximized", { timestamp: Date.now() });
|
|
140
|
+
}
|
|
141
|
+
setCustomerData(data) {
|
|
142
|
+
this.customerData = { ...this.customerData, ...data };
|
|
143
|
+
if (window.BuniChat) {
|
|
144
|
+
window.BuniChat.setCustomerData(this.customerData);
|
|
145
|
+
}
|
|
146
|
+
this.emit("customer_data_updated", { data: this.customerData });
|
|
147
|
+
}
|
|
148
|
+
getCustomerData() {
|
|
149
|
+
return this.customerData ? { ...this.customerData } : null;
|
|
150
|
+
}
|
|
151
|
+
setSessionVariables(variables) {
|
|
152
|
+
this.sessionVariables = { ...this.sessionVariables, ...variables };
|
|
153
|
+
if (window.BuniChat) {
|
|
154
|
+
window.BuniChat.setSessionVariables(this.sessionVariables);
|
|
155
|
+
}
|
|
156
|
+
this.emit("session_updated", { variables: this.sessionVariables });
|
|
157
|
+
}
|
|
158
|
+
getSessionVariables() {
|
|
159
|
+
return this.sessionVariables ? { ...this.sessionVariables } : null;
|
|
160
|
+
}
|
|
161
|
+
sendMessage(message) {
|
|
162
|
+
if (window.BuniChat) {
|
|
163
|
+
window.BuniChat.sendMessage(message);
|
|
164
|
+
}
|
|
165
|
+
const messageData = {
|
|
166
|
+
message,
|
|
167
|
+
isFromBot: false,
|
|
168
|
+
timestamp: Date.now(),
|
|
169
|
+
messageId: `msg_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
|
|
170
|
+
};
|
|
171
|
+
this.emit("new_message", messageData);
|
|
172
|
+
}
|
|
173
|
+
clearChat() {
|
|
174
|
+
if (window.BuniChat) {
|
|
175
|
+
window.BuniChat.clearChat();
|
|
176
|
+
}
|
|
177
|
+
this.state.unreadCount = 0;
|
|
178
|
+
}
|
|
179
|
+
on(event, callback) {
|
|
180
|
+
if (!this.eventListeners.has(event)) {
|
|
181
|
+
this.eventListeners.set(event, []);
|
|
182
|
+
}
|
|
183
|
+
this.eventListeners.get(event).push(callback);
|
|
184
|
+
}
|
|
185
|
+
off(event, callback) {
|
|
186
|
+
const listeners = this.eventListeners.get(event);
|
|
187
|
+
if (!listeners)
|
|
188
|
+
return;
|
|
189
|
+
if (callback) {
|
|
190
|
+
const index = listeners.indexOf(callback);
|
|
191
|
+
if (index > -1) {
|
|
192
|
+
listeners.splice(index, 1);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
this.eventListeners.set(event, []);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
emit(event, data) {
|
|
200
|
+
const listeners = this.eventListeners.get(event);
|
|
201
|
+
if (listeners) {
|
|
202
|
+
listeners.forEach((callback) => {
|
|
203
|
+
try {
|
|
204
|
+
callback(data);
|
|
205
|
+
}
|
|
206
|
+
catch (error) {
|
|
207
|
+
console.error(`Error in event listener for ${event}:`, error);
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
getState() {
|
|
213
|
+
return { ...this.state };
|
|
214
|
+
}
|
|
215
|
+
isReady() {
|
|
216
|
+
return this.state.isLoaded;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
// Utility function - named export
|
|
220
|
+
function createBuniChatWidget(options) {
|
|
221
|
+
const widget = new BuniChatWidget();
|
|
222
|
+
widget.initialize(options);
|
|
223
|
+
return widget;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// Main exports for @buni.ai/chatbot-core
|
|
227
|
+
// Export all types
|
|
228
|
+
|
|
229
|
+
exports.BuniChatWidget = BuniChatWidget;
|
|
230
|
+
exports.createBuniChatWidget = createBuniChatWidget;
|
|
231
|
+
exports.default = BuniChatWidget;
|
|
232
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/widget.ts","../src/index.ts"],"sourcesContent":["import {\n BuniChatAPI,\n BuniChatOptions,\n BuniChatState,\n CustomerData,\n SessionVariables,\n} from \"./types\";\n\n// Core widget loader class\nexport class BuniChatWidget implements BuniChatAPI {\n private options!: BuniChatOptions;\n private state: BuniChatState;\n private eventListeners: Map<string, Function[]>;\n private widgetElement: HTMLElement | null;\n private customerData: CustomerData | null;\n private sessionVariables: SessionVariables | null;\n\n constructor() {\n this.state = {\n isOpen: false,\n isLoaded: false,\n isMinimized: false,\n unreadCount: 0,\n };\n this.eventListeners = new Map();\n this.widgetElement = null;\n this.customerData = null;\n this.sessionVariables = null;\n }\n\n async initialize(options: BuniChatOptions): Promise<void> {\n this.options = options;\n\n try {\n await this.loadWidget();\n this.state.isLoaded = true;\n this.emit(\"ready\", { timestamp: Date.now() });\n\n if (options.onReady) {\n options.onReady({ timestamp: Date.now() });\n }\n } catch (error) {\n const errorObj =\n error instanceof Error ? error : new Error(String(error));\n this.emit(\"error\", { error: errorObj, context: \"initialization\" });\n\n if (options.onError) {\n options.onError(errorObj);\n }\n throw errorObj;\n }\n }\n\n private async loadWidget(): Promise<void> {\n return new Promise((resolve, reject) => {\n // Check if widget is already loaded\n if (window.BuniChat) {\n resolve();\n return;\n }\n\n // Load the widget script dynamically\n const script = document.createElement(\"script\");\n script.src = `${this.getBaseUrl()}/api/embed/widget?token=${encodeURIComponent(this.options.token)}`;\n script.async = true;\n\n script.onload = () => {\n // Initialize the actual widget\n this.initializeWidgetAPI();\n resolve();\n };\n\n script.onerror = () => {\n reject(new Error(\"Failed to load BuniAI widget script\"));\n };\n\n document.head.appendChild(script);\n });\n }\n\n private initializeWidgetAPI(): void {\n // Set up communication with the loaded widget\n if (window.BuniChat) {\n // Configure the widget with our options\n window.BuniChat.configure(this.options.config || {});\n\n // Set up event forwarding from the widget to our event system\n window.BuniChat.on(\"visibility_changed\", (data: any) => {\n this.state.isOpen = data.visibility === \"visible\";\n this.emit(\"visibility_changed\", data);\n\n if (this.options.onVisibilityChanged) {\n this.options.onVisibilityChanged(data);\n }\n });\n\n window.BuniChat.on(\"new_message\", (data: any) => {\n if (data.isFromBot) {\n this.state.unreadCount++;\n }\n this.emit(\"new_message\", data);\n\n if (this.options.onNewMessage) {\n this.options.onNewMessage(data);\n }\n });\n\n window.BuniChat.on(\"minimized\", (data: any) => {\n this.state.isMinimized = true;\n this.emit(\"minimized\", data);\n });\n\n window.BuniChat.on(\"maximized\", (data: any) => {\n this.state.isMinimized = false;\n this.emit(\"maximized\", data);\n });\n }\n }\n\n private getBaseUrl(): string {\n // Return the base URL for the BuniAI platform\n return (globalThis as any).BUNI_API_URL || window.location.origin;\n }\n\n destroy(): void {\n if (window.BuniChat) {\n window.BuniChat.destroy();\n }\n\n if (this.widgetElement) {\n this.widgetElement.remove();\n this.widgetElement = null;\n }\n\n this.eventListeners.clear();\n this.state.isLoaded = false;\n this.customerData = null;\n this.sessionVariables = null;\n }\n\n show(): void {\n if (window.BuniChat) {\n window.BuniChat.show();\n }\n this.state.isOpen = true;\n this.state.unreadCount = 0;\n this.emit(\"visibility_changed\", { visibility: \"visible\" });\n }\n\n hide(): void {\n if (window.BuniChat) {\n window.BuniChat.hide();\n }\n this.state.isOpen = false;\n this.emit(\"visibility_changed\", { visibility: \"hidden\" });\n }\n\n toggle(): void {\n this.state.isOpen ? this.hide() : this.show();\n }\n\n minimize(): void {\n if (window.BuniChat) {\n window.BuniChat.minimize();\n }\n this.state.isMinimized = true;\n this.emit(\"minimized\", { timestamp: Date.now() });\n }\n\n maximize(): void {\n if (window.BuniChat) {\n window.BuniChat.maximize();\n }\n this.state.isMinimized = false;\n this.emit(\"maximized\", { timestamp: Date.now() });\n }\n\n setCustomerData(data: CustomerData): void {\n this.customerData = { ...this.customerData, ...data };\n\n if (window.BuniChat) {\n window.BuniChat.setCustomerData(this.customerData);\n }\n\n this.emit(\"customer_data_updated\", { data: this.customerData });\n }\n\n getCustomerData(): CustomerData | null {\n return this.customerData ? { ...this.customerData } : null;\n }\n\n setSessionVariables(variables: SessionVariables): void {\n this.sessionVariables = { ...this.sessionVariables, ...variables };\n\n if (window.BuniChat) {\n window.BuniChat.setSessionVariables(this.sessionVariables);\n }\n\n this.emit(\"session_updated\", { variables: this.sessionVariables });\n }\n\n getSessionVariables(): SessionVariables | null {\n return this.sessionVariables ? { ...this.sessionVariables } : null;\n }\n\n sendMessage(message: string): void {\n if (window.BuniChat) {\n window.BuniChat.sendMessage(message);\n }\n\n const messageData = {\n message,\n isFromBot: false,\n timestamp: Date.now(),\n messageId: `msg_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,\n };\n\n this.emit(\"new_message\", messageData);\n }\n\n clearChat(): void {\n if (window.BuniChat) {\n window.BuniChat.clearChat();\n }\n this.state.unreadCount = 0;\n }\n\n on(event: string, callback: Function): void {\n if (!this.eventListeners.has(event)) {\n this.eventListeners.set(event, []);\n }\n this.eventListeners.get(event)!.push(callback);\n }\n\n off(event: string, callback?: Function): void {\n const listeners = this.eventListeners.get(event);\n if (!listeners) return;\n\n if (callback) {\n const index = listeners.indexOf(callback);\n if (index > -1) {\n listeners.splice(index, 1);\n }\n } else {\n this.eventListeners.set(event, []);\n }\n }\n\n emit(event: string, data?: any): void {\n const listeners = this.eventListeners.get(event);\n if (listeners) {\n listeners.forEach((callback) => {\n try {\n callback(data);\n } catch (error) {\n console.error(`Error in event listener for ${event}:`, error);\n }\n });\n }\n }\n\n getState(): BuniChatState {\n return { ...this.state };\n }\n\n isReady(): boolean {\n return this.state.isLoaded;\n }\n}\n\n// Utility function - named export\nexport function createBuniChatWidget(options: BuniChatOptions): BuniChatWidget {\n const widget = new BuniChatWidget();\n widget.initialize(options);\n return widget;\n}\n\n// Default export - the main widget class\nexport default BuniChatWidget;\n\n// Global type declaration for the widget API\ndeclare global {\n interface Window {\n BuniChat?: {\n configure: (config: any) => void;\n show: () => void;\n hide: () => void;\n minimize: () => void;\n maximize: () => void;\n destroy: () => void;\n setCustomerData: (data: CustomerData) => void;\n setSessionVariables: (variables: SessionVariables) => void;\n sendMessage: (message: string) => void;\n clearChat: () => void;\n on: (event: string, callback: Function) => void;\n off: (event: string, callback?: Function) => void;\n };\n }\n}\n","// Main exports for @buni.ai/chatbot-core\n\n// Export all types\nexport * from \"./types\";\n\n// Export everything from widget\nexport * from \"./widget\";\n\n// Make BuniChatWidget the default export as well\nimport { BuniChatWidget } from \"./widget\";\nexport default BuniChatWidget;\n"],"names":[],"mappings":";;;;AAQA;MACa,cAAc,CAAA;AAQzB,IAAA,WAAA,GAAA;QACE,IAAI,CAAC,KAAK,GAAG;AACX,YAAA,MAAM,EAAE,KAAK;AACb,YAAA,QAAQ,EAAE,KAAK;AACf,YAAA,WAAW,EAAE,KAAK;AAClB,YAAA,WAAW,EAAE,CAAC;SACf;AACD,QAAA,IAAI,CAAC,cAAc,GAAG,IAAI,GAAG,EAAE;AAC/B,QAAA,IAAI,CAAC,aAAa,GAAG,IAAI;AACzB,QAAA,IAAI,CAAC,YAAY,GAAG,IAAI;AACxB,QAAA,IAAI,CAAC,gBAAgB,GAAG,IAAI;IAC9B;IAEA,MAAM,UAAU,CAAC,OAAwB,EAAA;AACvC,QAAA,IAAI,CAAC,OAAO,GAAG,OAAO;AAEtB,QAAA,IAAI;AACF,YAAA,MAAM,IAAI,CAAC,UAAU,EAAE;AACvB,YAAA,IAAI,CAAC,KAAK,CAAC,QAAQ,GAAG,IAAI;AAC1B,YAAA,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC;AAE7C,YAAA,IAAI,OAAO,CAAC,OAAO,EAAE;AACnB,gBAAA,OAAO,CAAC,OAAO,CAAC,EAAE,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC;YAC5C;QACF;QAAE,OAAO,KAAK,EAAE;YACd,MAAM,QAAQ,GACZ,KAAK,YAAY,KAAK,GAAG,KAAK,GAAG,IAAI,KAAK,CAAC,MAAM,CAA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export interface BuniChatConfig {
|
|
2
|
+
theme?: 'default' | 'dark' | 'light' | 'corporate' | 'friendly' | 'minimal';
|
|
3
|
+
primaryColor?: string;
|
|
4
|
+
secondaryColor?: string;
|
|
5
|
+
companyName?: string;
|
|
6
|
+
customAvatar?: string;
|
|
7
|
+
welcomeMessage?: string;
|
|
8
|
+
position?: 'bottom-right' | 'bottom-left' | 'top-right' | 'top-left';
|
|
9
|
+
width?: number;
|
|
10
|
+
height?: number;
|
|
11
|
+
triggerText?: string;
|
|
12
|
+
autoOpen?: boolean;
|
|
13
|
+
showMinimize?: boolean;
|
|
14
|
+
showBranding?: boolean;
|
|
15
|
+
enableMobile?: boolean;
|
|
16
|
+
}
|
|
17
|
+
export interface BuniChatOptions {
|
|
18
|
+
token: string;
|
|
19
|
+
config?: BuniChatConfig;
|
|
20
|
+
onReady?: (data: any) => void;
|
|
21
|
+
onVisibilityChanged?: (data: {
|
|
22
|
+
visibility: 'visible' | 'hidden';
|
|
23
|
+
}) => void;
|
|
24
|
+
onNewMessage?: (data: {
|
|
25
|
+
message: string;
|
|
26
|
+
isFromBot: boolean;
|
|
27
|
+
}) => void;
|
|
28
|
+
onError?: (error: Error) => void;
|
|
29
|
+
}
|
|
30
|
+
export interface BuniChatState {
|
|
31
|
+
isOpen: boolean;
|
|
32
|
+
isLoaded: boolean;
|
|
33
|
+
isMinimized: boolean;
|
|
34
|
+
unreadCount: number;
|
|
35
|
+
}
|
|
36
|
+
export interface CustomerData {
|
|
37
|
+
name?: string;
|
|
38
|
+
email?: string;
|
|
39
|
+
phone?: string;
|
|
40
|
+
userId?: string;
|
|
41
|
+
customAttributes?: Record<string, any>;
|
|
42
|
+
}
|
|
43
|
+
export interface SessionVariables {
|
|
44
|
+
pageUrl?: string;
|
|
45
|
+
pageTitle?: string;
|
|
46
|
+
userAgent?: string;
|
|
47
|
+
referrer?: string;
|
|
48
|
+
timestamp?: number;
|
|
49
|
+
customData?: Record<string, any>;
|
|
50
|
+
}
|
|
51
|
+
export interface BuniChatAPI {
|
|
52
|
+
initialize(options: BuniChatOptions): Promise<void>;
|
|
53
|
+
destroy(): void;
|
|
54
|
+
show(): void;
|
|
55
|
+
hide(): void;
|
|
56
|
+
toggle(): void;
|
|
57
|
+
minimize(): void;
|
|
58
|
+
maximize(): void;
|
|
59
|
+
setCustomerData(data: CustomerData): void;
|
|
60
|
+
getCustomerData(): CustomerData | null;
|
|
61
|
+
setSessionVariables(variables: SessionVariables): void;
|
|
62
|
+
getSessionVariables(): SessionVariables | null;
|
|
63
|
+
sendMessage(message: string): void;
|
|
64
|
+
clearChat(): void;
|
|
65
|
+
on(event: string, callback: Function): void;
|
|
66
|
+
off(event: string, callback?: Function): void;
|
|
67
|
+
emit(event: string, data?: any): void;
|
|
68
|
+
getState(): BuniChatState;
|
|
69
|
+
isReady(): boolean;
|
|
70
|
+
}
|
|
71
|
+
export interface BuniChatEvents {
|
|
72
|
+
ready: {
|
|
73
|
+
timestamp: number;
|
|
74
|
+
};
|
|
75
|
+
visibility_changed: {
|
|
76
|
+
visibility: 'visible' | 'hidden';
|
|
77
|
+
};
|
|
78
|
+
minimized: {
|
|
79
|
+
timestamp: number;
|
|
80
|
+
};
|
|
81
|
+
maximized: {
|
|
82
|
+
timestamp: number;
|
|
83
|
+
};
|
|
84
|
+
new_message: {
|
|
85
|
+
message: string;
|
|
86
|
+
isFromBot: boolean;
|
|
87
|
+
timestamp: number;
|
|
88
|
+
messageId: string;
|
|
89
|
+
};
|
|
90
|
+
typing_start: {
|
|
91
|
+
isBot: boolean;
|
|
92
|
+
};
|
|
93
|
+
typing_stop: {
|
|
94
|
+
isBot: boolean;
|
|
95
|
+
};
|
|
96
|
+
error: {
|
|
97
|
+
error: Error;
|
|
98
|
+
context?: string;
|
|
99
|
+
};
|
|
100
|
+
customer_data_updated: {
|
|
101
|
+
data: CustomerData;
|
|
102
|
+
};
|
|
103
|
+
session_updated: {
|
|
104
|
+
variables: SessionVariables;
|
|
105
|
+
};
|
|
106
|
+
}
|
package/dist/widget.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { BuniChatAPI, BuniChatOptions, BuniChatState, CustomerData, SessionVariables } from "./types";
|
|
2
|
+
export declare class BuniChatWidget implements BuniChatAPI {
|
|
3
|
+
private options;
|
|
4
|
+
private state;
|
|
5
|
+
private eventListeners;
|
|
6
|
+
private widgetElement;
|
|
7
|
+
private customerData;
|
|
8
|
+
private sessionVariables;
|
|
9
|
+
constructor();
|
|
10
|
+
initialize(options: BuniChatOptions): Promise<void>;
|
|
11
|
+
private loadWidget;
|
|
12
|
+
private initializeWidgetAPI;
|
|
13
|
+
private getBaseUrl;
|
|
14
|
+
destroy(): void;
|
|
15
|
+
show(): void;
|
|
16
|
+
hide(): void;
|
|
17
|
+
toggle(): void;
|
|
18
|
+
minimize(): void;
|
|
19
|
+
maximize(): void;
|
|
20
|
+
setCustomerData(data: CustomerData): void;
|
|
21
|
+
getCustomerData(): CustomerData | null;
|
|
22
|
+
setSessionVariables(variables: SessionVariables): void;
|
|
23
|
+
getSessionVariables(): SessionVariables | null;
|
|
24
|
+
sendMessage(message: string): void;
|
|
25
|
+
clearChat(): void;
|
|
26
|
+
on(event: string, callback: Function): void;
|
|
27
|
+
off(event: string, callback?: Function): void;
|
|
28
|
+
emit(event: string, data?: any): void;
|
|
29
|
+
getState(): BuniChatState;
|
|
30
|
+
isReady(): boolean;
|
|
31
|
+
}
|
|
32
|
+
export declare function createBuniChatWidget(options: BuniChatOptions): BuniChatWidget;
|
|
33
|
+
export default BuniChatWidget;
|
|
34
|
+
declare global {
|
|
35
|
+
interface Window {
|
|
36
|
+
BuniChat?: {
|
|
37
|
+
configure: (config: any) => void;
|
|
38
|
+
show: () => void;
|
|
39
|
+
hide: () => void;
|
|
40
|
+
minimize: () => void;
|
|
41
|
+
maximize: () => void;
|
|
42
|
+
destroy: () => void;
|
|
43
|
+
setCustomerData: (data: CustomerData) => void;
|
|
44
|
+
setSessionVariables: (variables: SessionVariables) => void;
|
|
45
|
+
sendMessage: (message: string) => void;
|
|
46
|
+
clearChat: () => void;
|
|
47
|
+
on: (event: string, callback: Function) => void;
|
|
48
|
+
off: (event: string, callback?: Function) => void;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@buni.ai/chatbot-core",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Core functionality for BuniAI chat widget adapters",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.cjs.js",
|
|
7
|
+
"module": "dist/index.esm.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./dist/index.esm.js",
|
|
12
|
+
"require": "./dist/index.cjs.js",
|
|
13
|
+
"types": "./dist/index.d.ts"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "rollup -c",
|
|
21
|
+
"dev": "rollup -c -w",
|
|
22
|
+
"test": "jest",
|
|
23
|
+
"lint": "eslint src --ext .ts",
|
|
24
|
+
"lint:fix": "eslint src --ext .ts --fix"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"chat",
|
|
28
|
+
"widget",
|
|
29
|
+
"buni",
|
|
30
|
+
"ai",
|
|
31
|
+
"customer-support",
|
|
32
|
+
"core"
|
|
33
|
+
],
|
|
34
|
+
"author": "BuniAI",
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@rollup/plugin-node-resolve": "^15.0.0",
|
|
38
|
+
"@rollup/plugin-commonjs": "^24.0.0",
|
|
39
|
+
"@rollup/plugin-typescript": "^11.0.0",
|
|
40
|
+
"rollup": "^3.20.0",
|
|
41
|
+
"rollup-plugin-dts": "^5.3.0",
|
|
42
|
+
"tslib": "^2.5.0",
|
|
43
|
+
"typescript": "^5.0.0"
|
|
44
|
+
}
|
|
45
|
+
}
|