@headwai/chat-bubble 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 ADDED
@@ -0,0 +1,526 @@
1
+ # Chat Bubble Svelte
2
+
3
+ The chat bubble is based on a Svelte integration for the [deep-chat](https://www.npmjs.com/package/deep-chat) component, providing an easy-to-use AI chat interface for your web applications.
4
+
5
+ ## Features
6
+
7
+ - 🚀 Easy integration
8
+ - 🔧 Configurable via environment variables
9
+ - 💬 Support for various AI providers (LocalChat, OpenAI and more)
10
+ - 📱 Responsive design
11
+ - 🎨 Customizable UI components
12
+ - 🔄 Real-time messaging with WebSocket support
13
+
14
+ ## Installation
15
+
16
+ ### Prerequisites
17
+
18
+ - Node.js (version 16 or higher)
19
+ - npm or yarn
20
+
21
+ ### Setup
22
+
23
+ 1. Clone this repository:
24
+ ```bash
25
+ git clone <your-repo-url>
26
+ cd chat-bubble
27
+ ```
28
+
29
+ 2. Install dependencies:
30
+ ```bash
31
+ npm install
32
+ ```
33
+
34
+ 3. Create a `.env` file in the root directory:
35
+ ```bash
36
+ cp .env.example .env
37
+ ```
38
+
39
+ 4. Configure your environment variables (see [Configuration](#configuration) section)
40
+
41
+ 5. Start the development server:
42
+ ```bash
43
+ npm run dev
44
+ ```
45
+
46
+ ## Configuration
47
+
48
+ The deep-chat component can be configured using environment variables. Create a `.env` file in your project root with the following variables:
49
+
50
+ ### Required Environment Variables
51
+
52
+ ```env
53
+ # API Configuration
54
+ VITE_CHAT_BUBBLE_API_URL=https://your-api-endpoint.com/api/chat/completions
55
+ VITE_CHAT_BUBBLE_API_KEY=your-api-key-here
56
+ VITE_CHAT_BUBBLE_MODEL_NAME=gpt-3.5-turbo
57
+
58
+ # Connection Type (websocket, openAI, etc.)
59
+ VITE_CHAT_BUBBLE_CONNECTION_TYPE=websocket
60
+ ```
61
+
62
+ ### Optional Environment Variables
63
+
64
+ ```env
65
+ # Additional Headers
66
+ VITE_CHAT_BUBBLE_CONTENT_TYPE=application/json
67
+
68
+ # Request Configuration
69
+ VITE_CHAT_BUBBLE_MAX_MESSAGES=0
70
+
71
+ # UI Configuration
72
+ VITE_CHAT_BUBBLE_PLACEHOLDER_TEXT=Welcome to the chat!
73
+ VITE_CHAT_BUBBLE_DEMO_MODE=false
74
+ ```
75
+
76
+ ## Usage
77
+
78
+ ### Basic Integration
79
+
80
+ To integrate the deep-chat component into your Svelte application:
81
+
82
+ 1. **Install the package** (if using as a standalone package):
83
+ ```bash
84
+ npm install deep-chat
85
+ ```
86
+
87
+ 2. **Import and use in your Svelte component**:
88
+
89
+ ```svelte
90
+ <script>
91
+ import { DeepChat } from "deep-chat";
92
+
93
+ // Configuration using environment variables
94
+ const connect = {
95
+ type: import.meta.env.VITE_CHAT_BUBBLE_CONNECTION_TYPE || "websocket",
96
+ url: import.meta.env.VITE_CHAT_BUBBLE_API_URL,
97
+ headers: {
98
+ Authorization: `Bearer ${import.meta.env.VITE_CHAT_BUBBLE_API_KEY}`,
99
+ "Content-Type": import.meta.env.VITE_CHAT_BUBBLE_CONTENT_TYPE || "application/json"
100
+ },
101
+ additionalBodyProps: {
102
+ model: import.meta.env.VITE_CHAT_BUBBLE_MODEL_NAME
103
+ }
104
+ };
105
+
106
+ const requestBodyLimits = {
107
+ maxMessages: parseInt(import.meta.env.VITE_CHAT_BUBBLE_MAX_MESSAGES) || 0
108
+ };
109
+
110
+ const textInputConfig = {
111
+ placeholder: {
112
+ text: import.meta.env.VITE_CHAT_BUBBLE_PLACEHOLDER_TEXT || "Type your message..."
113
+ }
114
+ };
115
+
116
+ // Message transformation for OpenAI compatibility
117
+ const requestInterceptor = (details) => {
118
+ if (details.body && details.body.messages) {
119
+ details.body.messages = details.body.messages.map(message => ({
120
+ role: message.role === "ai" ? "assistant" : message.role,
121
+ content: message.text || message.content
122
+ }));
123
+ }
124
+ return details;
125
+ };
126
+
127
+ const responseInterceptor = (response) => {
128
+ if (response.choices && response.choices[0] && response.choices[0].message) {
129
+ const message = response.choices[0].message;
130
+ return {
131
+ text: message.content,
132
+ role: message.role === "assistant" ? "ai" : message.role
133
+ };
134
+ }
135
+
136
+ if (response.text || response.html || response.files) {
137
+ return response;
138
+ }
139
+
140
+ if (typeof response === 'string') {
141
+ return { text: response };
142
+ }
143
+
144
+ return response;
145
+ };
146
+ </script>
147
+
148
+ <deep-chat
149
+ demo={import.meta.env.VITE_CHAT_BUBBLE_DEMO_MODE === 'true'}
150
+ textInput={textInputConfig}
151
+ connect={connect}
152
+ requestBodyLimits={requestBodyLimits}
153
+ requestInterceptor={requestInterceptor}
154
+ responseInterceptor={responseInterceptor}
155
+ />
156
+ ```
157
+
158
+ ### Including Chat Bubble in Customer Websites
159
+
160
+ #### Option 1: Build and Deploy (Complete Application)
161
+
162
+ This option bundles everything into a ready-to-use application including all interceptors and logic.
163
+
164
+ **What gets bundled:**
165
+ - ✅ Request/Response interceptors (OpenAI compatibility)
166
+ - ✅ Environment variable configuration
167
+ - ✅ All chat functionality
168
+ - ✅ UI components and styling
169
+
170
+ **Build and deployment steps:**
171
+ 1. **Configure environment variables** for production:
172
+ ```bash
173
+ # Production .env file
174
+ VITE_CHAT_BUBBLE_API_URL=https://api.customer-domain.com/chat
175
+ VITE_CHAT_BUBBLE_API_KEY=prod-api-key
176
+ VITE_CHAT_BUBBLE_MODEL_NAME=gpt-4
177
+ VITE_CHAT_BUBBLE_CONNECTION_TYPE=websocket
178
+ VITE_CHAT_BUBBLE_DEMO_MODE=false
179
+ ```
180
+
181
+ 2. **Build the widget**:
182
+ ```bash
183
+ npm run build:widget
184
+ ```
185
+
186
+ 3. **Deploy the `dist-widget` folder** to your web server or CDN.
187
+
188
+ **Result:** Customers get a complete chat application - no additional coding required!
189
+
190
+ #### Option 2: Embed Chat Bubble as Script (Plug-and-Play)
191
+
192
+ After building the widget, you can include the chat component in any website as a complete widget.
193
+
194
+ **Build steps:**
195
+ 1. **Build the widget**:
196
+ ```bash
197
+ npm run build:widget
198
+ ```
199
+
200
+ 2. **Publish to NPM** (for jsDelivr access):
201
+ ```bash
202
+ npm publish
203
+ ```
204
+
205
+ **Customer integration:**
206
+ ```html
207
+ <!DOCTYPE html>
208
+ <html>
209
+ <head>
210
+ <title>Customer Website</title>
211
+ <!-- Configure via global variables -->
212
+ <script>
213
+ // Runtime configuration override
214
+ window.DEEP_CHAT_CONFIG = {
215
+ apiUrl: 'https://customer-api.com/chat',
216
+ apiKey: 'customer-api-key',
217
+ modelName: 'gpt-4',
218
+ connectionType: 'websocket'
219
+ };
220
+ </script>
221
+ </head>
222
+ <body>
223
+ <!-- Your existing content -->
224
+
225
+ <!-- Chat Bubble Integration - Auto-initializes! -->
226
+ <div id="chat-bubble-container"></div>
227
+
228
+ <!-- Include from jsDelivr CDN -->
229
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@headwai/chat-bubble@latest/dist-widget/chat-bubble.css">
230
+ <script src="https://cdn.jsdelivr.net/npm/@headwai/chat-bubble@latest/dist-widget/chat-bubble.js"></script>
231
+ </body>
232
+ </html>
233
+ ```
234
+
235
+ **Alternative: Multiple chat bubbles with data attributes:**
236
+ ```html
237
+ <!-- Chat bubble with specific configuration -->
238
+ <div data-chat-bubble
239
+ data-chat-bubble-api-url="https://api1.com/chat"
240
+ data-chat-bubble-api-key="key1"></div>
241
+
242
+ <!-- Another chat bubble with different config -->
243
+ <div data-chat-bubble
244
+ data-chat-bubble-api-url="https://api2.com/chat"
245
+ data-chat-bubble-api-key="key2"></div>
246
+
247
+ <script src="https://cdn.jsdelivr.net/npm/@headwai/chat-bubble@latest/dist-widget/chat-bubble.js"></script>
248
+ ```
249
+
250
+ **What customers get:**
251
+ - ✅ Complete chat interface
252
+ - ✅ All interceptors included
253
+ - ✅ OpenAI API compatibility built-in
254
+ - ✅ Just needs API configuration
255
+ - ✅ Auto-initialization on page load
256
+
257
+ #### Option 3: NPM Package Integration (Developer Integration)
258
+
259
+ For developers who need full control and customization.
260
+
261
+ **Build and publish steps:**
262
+ 1. **Build the library**:
263
+ ```bash
264
+ npm run build:lib
265
+ ```
266
+
267
+ 2. **Publish to NPM**:
268
+ ```bash
269
+ npm publish
270
+ ```
271
+
272
+ **Customer installation:**
273
+ ```bash
274
+ # Install in customer's project
275
+ npm install @headwai/chat-bubble
276
+ ```
277
+
278
+ **For Svelte projects:**
279
+ ```svelte
280
+ <!-- Customer's Svelte component -->
281
+ <script>
282
+ import { ChatBubble } from '@headwai/chat-bubble';
283
+
284
+ // Customers MUST implement these interceptors themselves
285
+ const requestInterceptor = (details) => {
286
+ if (details.body && details.body.messages) {
287
+ details.body.messages = details.body.messages.map(message => ({
288
+ role: message.role === "ai" ? "assistant" : message.role,
289
+ content: message.text || message.content
290
+ }));
291
+ }
292
+ return details;
293
+ };
294
+
295
+ const responseInterceptor = (response) => {
296
+ if (response.choices && response.choices[0] && response.choices[0].message) {
297
+ const message = response.choices[0].message;
298
+ return {
299
+ text: message.content,
300
+ role: message.role === "assistant" ? "ai" : message.role
301
+ };
302
+ }
303
+ return response;
304
+ };
305
+
306
+ const connect = {
307
+ type: "websocket",
308
+ url: process.env.API_URL,
309
+ headers: {
310
+ Authorization: `Bearer ${process.env.API_KEY}`,
311
+ "Content-Type": "application/json"
312
+ },
313
+ additionalBodyProps: {
314
+ model: process.env.MODEL_NAME
315
+ }
316
+ };
317
+ </script>
318
+
319
+ <ChatBubble
320
+ connect={connect}
321
+ requestInterceptor={requestInterceptor}
322
+ responseInterceptor={responseInterceptor}
323
+ />
324
+ ```
325
+
326
+ **For vanilla JavaScript projects:**
327
+ ```html
328
+ <script type="module">
329
+ import { createChatBubble } from 'https://cdn.jsdelivr.net/npm/@headwai/chat-bubble@latest/dist/index.js';
330
+
331
+ // Initialize manually with custom configuration
332
+ const chatBubble = createChatBubble(
333
+ document.getElementById('chat-container'),
334
+ {
335
+ apiUrl: 'https://api.example.com/chat',
336
+ apiKey: 'your-api-key',
337
+ // ... other props
338
+ }
339
+ );
340
+ </script>
341
+ ```
342
+
343
+ **What customers need to implement:**
344
+ - ❌ Request/Response interceptors (manual setup required)
345
+ - ❌ Environment variable handling
346
+ - ❌ API configuration logic
347
+ - ✅ Deep chat component only
348
+
349
+ ## Deployment Options Comparison
350
+
351
+ | Feature | Built App (Option 1) | Script Embed (Option 2) | NPM Package (Option 3) |
352
+ |---------|----------------------|--------------------------|-------------------------|
353
+ | **Build Command** | `npm run build:widget` | `npm run build:widget` + publish | `npm run build:lib` + publish |
354
+ | **Interceptors Included** | ✅ Yes, automatically | ✅ Yes, automatically | ❌ No, manual setup required |
355
+ | **Environment Variables** | ✅ Built-in support | ✅ Runtime configuration | ❌ Customer implements |
356
+ | **OpenAI Compatibility** | ✅ Ready to use | ✅ Ready to use | ❌ Customer implements |
357
+ | **Setup Complexity** | 🟢 Minimal (just config) | � Minimal (script tag) | �🟡 Moderate (coding required) |
358
+ | **Customization** | 🟡 Limited to env vars | � Limited to config object | �🟢 Full control |
359
+ | **File Size** | 🟡 Larger (complete app) | � Larger (complete app) | �🟢 Smaller (just component) |
360
+ | **Use Case** | Self-hosted deployment | CDN/jsDelivr embedding | Developers, custom integration |
361
+ | **Auto-initialization** | ✅ Yes | ✅ Yes | ❌ Manual setup |
362
+
363
+ ### Recommended Approach
364
+
365
+ **For most customers:** Use **Option 2** (Script Embed via jsDelivr)
366
+ - ✅ No coding required
367
+ - ✅ All interceptors included
368
+ - ✅ Easy CDN access via jsDelivr
369
+ - ✅ Just configure via JavaScript object
370
+ - ✅ Ready-to-deploy solution
371
+ - ✅ Auto-initialization
372
+
373
+ **For self-hosted deployments:** Use **Option 1** (Built Application)
374
+ - ✅ Complete control over hosting
375
+ - ✅ No external CDN dependencies
376
+ - ✅ All interceptors included
377
+ - ✅ Environment variable configuration
378
+
379
+ **For developers who need customization:** Use **Option 3** (NPM Package)
380
+ - ✅ Full control over interceptors
381
+ - ✅ Custom API integrations
382
+ - ✅ Smaller bundle size
383
+ - ❌ More setup required
384
+
385
+ ## Publishing to NPM and jsDelivr Access
386
+
387
+ ### Publishing Steps
388
+
389
+ 1. **Build both library and widget**:
390
+ ```bash
391
+ npm run build
392
+ ```
393
+
394
+ 2. **Login to NPM** (if not already logged in):
395
+ ```bash
396
+ npm login
397
+ ```
398
+
399
+ 3. **Publish the package**:
400
+ ```bash
401
+ npm publish
402
+ ```
403
+
404
+ ### jsDelivr CDN Access
405
+
406
+ Once published to NPM, your package is automatically available on jsDelivr:
407
+
408
+ **For Widget/Script Embed (Options 1 & 2):**
409
+ ```html
410
+ <!-- Latest version -->
411
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@headwai/chat-bubble@latest/dist-widget/chat-bubble.css">
412
+ <script src="https://cdn.jsdelivr.net/npm/@headwai/chat-bubble@latest/dist-widget/chat-bubble.js"></script>
413
+
414
+ <!-- Specific version -->
415
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@headwai/chat-bubble@1.0.0/dist-widget/chat-bubble.css">
416
+ <script src="https://cdn.jsdelivr.net/npm/@headwai/chat-bubble@1.0.0/dist-widget/chat-bubble.js"></script>
417
+ ```
418
+
419
+ **For Library/Module Import (Option 3):**
420
+ ```html
421
+ <!-- ES Module -->
422
+ <script type="module">
423
+ import { ChatBubble, createChatBubble } from 'https://cdn.jsdelivr.net/npm/@headwai/chat-bubble@latest/dist/index.js';
424
+ </script>
425
+
426
+ <!-- UMD (Universal Module Definition) -->
427
+ <script src="https://cdn.jsdelivr.net/npm/@headwai/chat-bubble@latest/dist/index.umd.js"></script>
428
+ ```
429
+
430
+ ### File Structure After Build
431
+
432
+ ```
433
+ dist/ # NPM package (Option 3)
434
+ ├── index.js # ES module
435
+ ├── index.umd.js # UMD module
436
+ └── style.css # Component styles
437
+
438
+ dist-widget/ # Standalone widget (Options 1 & 2)
439
+ ├── widget.html # Demo page
440
+ ├── chat-bubble.js # Complete widget with auto-init
441
+ ├── chat-bubble.css # Widget styles
442
+ └── assets/ # Additional assets
443
+ ```
444
+
445
+ ## Environment Variables Reference
446
+
447
+ | Variable | Description | Default | Required |
448
+ |----------|-------------|---------|----------|
449
+ | `VITE_CHAT_BUBBLE_API_URL` | The API endpoint URL | - | ✅ |
450
+ | `VITE_CHAT_BUBBLE_API_KEY` | API authentication key | - | ✅ |
451
+ | `VITE_CHAT_BUBBLE_MODEL_NAME` | AI model name to use | `gpt-3.5-turbo` | ✅ |
452
+ | `VITE_CHAT_BUBBLE_CONNECTION_TYPE` | Connection type (websocket, openAI, etc.) | `websocket` | ✅ |
453
+ | `VITE_CHAT_BUBBLE_CONTENT_TYPE` | HTTP Content-Type header | `application/json` | ❌ |
454
+ | `VITE_CHAT_BUBBLE_MAX_MESSAGES` | Maximum messages in conversation history | `0` (unlimited) | ❌ |
455
+ | `VITE_CHAT_BUBBLE_PLACEHOLDER_TEXT` | Chat input placeholder text | `Type your message...` | ❌ |
456
+ | `VITE_CHAT_BUBBLE_DEMO_MODE` | Enable demo mode | `false` | ❌ |
457
+
458
+ ## API Compatibility
459
+
460
+ This component is designed to work with OpenAI-compatible APIs. The request and response interceptors handle the message format transformation between deep-chat and OpenAI formats.
461
+
462
+ ### Supported Message Flow
463
+
464
+ 1. **User Input** → Deep Chat format
465
+ 2. **Request Interceptor** → Transforms to OpenAI format
466
+ 3. **API Call** → Your configured endpoint
467
+ 4. **Response Interceptor** → Transforms back to Deep Chat format
468
+ 5. **Display** → Rendered in chat interface
469
+
470
+ ## Development
471
+
472
+ ### Scripts
473
+
474
+ - `npm run dev` - Start development server
475
+ - `npm run build` - Build both library and widget for production
476
+ - `npm run build:lib` - Build NPM package (Option 3)
477
+ - `npm run build:widget` - Build standalone widget (Options 1 & 2)
478
+ - `npm run build:app` - Build demo application
479
+ - `npm run preview` - Preview production build
480
+
481
+ ### Project Structure
482
+
483
+ ```
484
+ chat-bubble/
485
+ ├── src/
486
+ │ ├── App.svelte # Main chat component
487
+ │ ├── main.js # Development entry point
488
+ │ ├── lib.js # NPM package entry point
489
+ │ └── widget.js # Widget/standalone entry point
490
+ ├── dist/ # NPM package build output
491
+ ├── dist-widget/ # Widget build output
492
+ ├── package.json # Dependencies and scripts
493
+ ├── vite.config.js # Default Vite configuration
494
+ ├── vite.lib.config.js # Library build configuration
495
+ ├── vite.widget.config.js # Widget build configuration
496
+ ├── vite.app.config.js # App build configuration
497
+ ├── svelte.config.js # Svelte configuration
498
+ ├── index.html # Development HTML
499
+ ├── widget.html # Widget HTML template
500
+ └── README.md # This file
501
+ ```
502
+
503
+ ## Troubleshooting
504
+
505
+ ### Common Issues
506
+
507
+ 1. **CORS Errors**: Ensure your API endpoint allows requests from your domain
508
+ 2. **Authentication Failures**: Verify your API key is correct and has proper permissions
509
+ 3. **WebSocket Connection Issues**: Check if your API supports WebSocket connections
510
+ 4. **Environment Variables Not Loading**: Ensure variables are prefixed with `VITE_CHAT_BUBBLE_`
511
+
512
+ ### Debug Mode
513
+
514
+ Enable debug mode by setting:
515
+ ```env
516
+ VITE_CHAT_BUBBLE_DEMO_MODE=true
517
+ ```
518
+
519
+ This will enable demo features that can help test the component without a real API connection.
520
+
521
+ ## Support
522
+
523
+ For issues and questions:
524
+ - Create an issue in this repository
525
+ - Check the [deep-chat documentation](https://deepchat.dev/)
526
+ - Review the [API compatibility guide](https://deepchat.dev/docs/connect)