@ibti-tech/chatbot 0.2.0 → 0.5.7

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 CHANGED
@@ -1,36 +1,478 @@
1
- # IBTI Chatbot
1
+ # @ibti-tech/chatbot
2
2
 
3
- IBTI Chatbot is an assistent designed to provide intelligent responses and assist users effectively according to ibti available data from website and blog.
3
+ A modern and customizable React chatbot component developed by IBTI Soluções em TI. This package provides an intelligent conversational interface that can be easily integrated into any React application.
4
4
 
5
- ## How to use on your project
5
+ ## 📋 Features
6
6
 
7
- 1. Install using yarn or npm
7
+ - 🎨 **Customizable themes**: Support for light and dark themes
8
+ - 🌍 **Internationalization**: Support for Brazilian Portuguese (pt-BR) and English (en)
9
+ - 💬 **Modern interface**: Responsive and intuitive design
10
+ - ⚡ **Optimized performance**: Built with ESBuild for maximum performance
11
+ - 🔌 **Easy integration**: Simple React component to use
12
+ - 📱 **Responsive**: Works perfectly on desktop and mobile
8
13
 
9
- ```sh
10
- yarn add @ibti-tech/chatbot | npm install @ibti-tech/chatbot
14
+ ## 📦 Installation
15
+
16
+ Install the package using yarn or npm:
17
+
18
+ ```bash
19
+ # Using yarn
20
+ yarn add @ibti-tech/chatbot
21
+
22
+ # Using npm
23
+ npm install @ibti-tech/chatbot
24
+ ```
25
+
26
+ ## 🚀 Basic Usage
27
+
28
+ ### Import
29
+
30
+ ```jsx
31
+ import { ChatbotProvider, ChatbotBar } from '@ibti-tech/chatbot'
32
+ ```
33
+
34
+ ### Minimal Example
35
+
36
+ ```jsx
37
+ import React from 'react'
38
+ import { ChatbotProvider, ChatbotBar } from '@ibti-tech/chatbot'
39
+
40
+ function App() {
41
+ return (
42
+ <ChatbotProvider
43
+ locale="pt-BR"
44
+ apiURL="https://api.example.com"
45
+ theme="dark"
46
+ >
47
+ <ChatbotBar />
48
+ </ChatbotProvider>
49
+ )
50
+ }
51
+
52
+ export default App
53
+ ```
54
+
55
+ ## 📚 Available Components
56
+
57
+ ### `ChatbotProvider`
58
+
59
+ Context component that provides chatbot configuration and state to child components.
60
+
61
+ #### Props
62
+
63
+ | Prop | Type | Required | Description |
64
+ |------|------|----------|-------------|
65
+ | `locale` | `'pt-BR' \| 'en'` | ✅ | Chatbot interface language |
66
+ | `apiURL` | `string` | ✅ | Chatbot backend API URL |
67
+ | `theme` | `'light' \| 'dark'` | ✅ | Chatbot visual theme |
68
+ | `isOpen` | `boolean` | ❌ | Controls if the chatbot is initially open. Default: `false` |
69
+ | `texts` | `ChatbotTypes.Texts` | ❌ | Custom texts to override default translations. See [Custom Texts](#custom-texts) section |
70
+ | `children` | `ReactNode` | ❌ | Child components to be rendered |
71
+
72
+ ### `ChatbotBar`
73
+
74
+ Main component that displays the chatbot bar with toggle button and conversation interface.
75
+
76
+ #### Props
77
+
78
+ | Prop | Type | Required | Description |
79
+ |------|------|----------|-------------|
80
+ | `verticalPosition` | `'top' \| 'bottom'` | ❌ | Vertical position of the chatbot. Default: `'bottom'` |
81
+ | `horizontalPosition` | `'left' \| 'right'` | ❌ | Horizontal position of the chatbot. Default: `'right'` |
82
+
83
+ ### `ChatbotDevice`
84
+
85
+ Alternative component that can be used to display the chatbot on different devices or layouts.
86
+
87
+ #### Props
88
+
89
+ | Prop | Type | Required | Description |
90
+ |------|------|----------|-------------|
91
+ | `verticalPosition` | `'top' \| 'bottom'` | ❌ | Vertical position of the chatbot. Default: `'bottom'` |
92
+
93
+ ## 🎨 Themes
94
+
95
+ The chatbot supports two themes:
96
+
97
+ - **`light`**: Light theme with bright colors
98
+ - **`dark`**: Dark theme with dark colors
99
+
100
+ ```jsx
101
+ <ChatbotProvider theme="dark" ...>
102
+ <ChatbotBar />
103
+ </ChatbotProvider>
104
+ ```
105
+
106
+ ## 🌍 Localization
107
+
108
+ The chatbot supports the following languages:
109
+
110
+ - **`pt-BR`**: Brazilian Portuguese
111
+ - **`en`**: English
112
+
113
+ ```jsx
114
+ <ChatbotProvider locale="pt-BR" ...>
115
+ <ChatbotBar />
116
+ </ChatbotProvider>
11
117
  ```
12
118
 
13
- 2. Import Chatbot component into your react application and use it as a component
119
+ ### Custom Texts
120
+
121
+ You can customize all texts in the chatbot interface by passing a `texts` prop to `ChatbotProvider`. This allows you to override default translations for any locale.
14
122
 
15
123
  ```jsx
16
- import { Chatbot } from '@ibti-tech/chatbot'
124
+ <ChatbotProvider
125
+ locale="pt-BR"
126
+ apiURL="https://api.example.com"
127
+ theme="dark"
128
+ texts={{
129
+ 'pt-BR': {
130
+ CHATBOT_NAME: 'Meu Assistente',
131
+ INPUT_PLACEHOLDER: 'Digite sua mensagem...',
132
+ CHATBOT_BAR: {
133
+ OPEN: 'Abrir chat',
134
+ CLOSE: 'Fechar chat',
135
+ },
136
+ CHAT_FEEDBACK: {
137
+ TITLE: 'Avalie esta resposta',
138
+ DESCRIPTION: 'Sua opinião nos ajuda a melhorar',
139
+ MESSAGE_FIELD: 'Mensagem (opcional)',
140
+ SUBMIT_BUTTON: 'Enviar',
141
+ CLOSE_BUTTON: 'Fechar',
142
+ RATE: 'Avaliar',
143
+ },
144
+ SUGGESTED_QUESTIONS: {
145
+ TITLE: 'Perguntas sugeridas',
146
+ LOADING: 'Carregando...',
147
+ },
148
+ STATUS: {
149
+ ONLINE: 'Online',
150
+ LOADING: 'Carregando',
151
+ WRITING: 'Digitando...',
152
+ UNAVAILABLE: 'Indisponível',
153
+ INACTIVE: 'Inativo',
154
+ },
155
+ ERRORS: {
156
+ UNKNOWN: 'Erro desconhecido',
157
+ },
158
+ MESSAGE_ACTIONS: {
159
+ COPY: 'Copiar',
160
+ SHARE: 'Compartilhar',
161
+ RESEND: 'Reenviar',
162
+ },
163
+ USER_LABEL: 'Você',
164
+ WRITING_MESSAGE: 'Digitando...',
165
+ },
166
+ 'en': {
167
+ CHATBOT_NAME: 'My Assistant',
168
+ // ... other custom texts
169
+ },
170
+ }}
171
+ >
172
+ <ChatbotBar />
173
+ </ChatbotProvider>
17
174
  ```
18
175
 
19
- 3. Use the component
176
+ **Available text keys:**
177
+ - `CHATBOT_NAME`: Name displayed in the chatbot header
178
+ - `INPUT_PLACEHOLDER`: Placeholder text for the input field
179
+ - `CHATBOT_BAR.OPEN`: Text for opening the chatbot
180
+ - `CHATBOT_BAR.CLOSE`: Text for closing the chatbot
181
+ - `CHAT_FEEDBACK.*`: All feedback form texts
182
+ - `SUGGESTED_QUESTIONS.*`: Suggested questions section texts
183
+ - `STATUS.*`: Status indicator texts
184
+ - `ERRORS.*`: Error message texts
185
+ - `MESSAGE_ACTIONS.*`: Message action button texts
186
+ - `USER_LABEL`: Label for user messages
187
+ - `WRITING_MESSAGE`: Text shown when the bot is typing
188
+
189
+ ## 📝 Complete Example
20
190
 
21
191
  ```jsx
22
- // {...}
23
- const MyFunctionalComponent = () => {
192
+ import React from 'react'
193
+ import { ChatbotProvider, ChatbotBar } from '@ibti-tech/chatbot'
194
+
195
+ function MyApp() {
24
196
  return (
25
- <div>
197
+ <div className="app">
198
+ <h1>My Application</h1>
199
+
200
+ {/* Other components of your application */}
201
+
26
202
  <ChatbotProvider
27
203
  locale="pt-BR"
28
- apiURL="http://localhost:6061"
204
+ apiURL={process.env.REACT_APP_CHATBOT_API_URL || "http://localhost:6061"}
29
205
  theme="dark"
206
+ isOpen={false}
30
207
  >
31
- <ChatbotBar />
208
+ <ChatbotBar
209
+ verticalPosition="bottom"
210
+ horizontalPosition="right"
211
+ />
32
212
  </ChatbotProvider>
33
213
  </div>
34
214
  )
35
215
  }
216
+
217
+ export default MyApp
36
218
  ```
219
+
220
+ ## 🎛️ Advanced Configuration
221
+
222
+ ### Positioning the Chatbot
223
+
224
+ You can control the position of the chatbot using the `ChatbotBar` component props:
225
+
226
+ ```jsx
227
+ // Bottom right (default)
228
+ <ChatbotBar />
229
+
230
+ // Top right
231
+ <ChatbotBar verticalPosition="top" />
232
+
233
+ // Bottom left
234
+ <ChatbotBar horizontalPosition="left" />
235
+
236
+ // Top left
237
+ <ChatbotBar
238
+ verticalPosition="top"
239
+ horizontalPosition="left"
240
+ />
241
+ ```
242
+
243
+ ### Controlling Initial State
244
+
245
+ Use the `isOpen` prop to control whether the chatbot starts open or closed:
246
+
247
+ ```jsx
248
+ <ChatbotProvider
249
+ locale="pt-BR"
250
+ apiURL="https://api.example.com"
251
+ theme="dark"
252
+ isOpen={true} // Chatbot starts open
253
+ >
254
+ <ChatbotBar />
255
+ </ChatbotProvider>
256
+ ```
257
+
258
+ ## 🔧 Requirements
259
+
260
+ ### Peer Dependencies
261
+
262
+ This package requires the following dependencies that must be installed in your project:
263
+
264
+ - `react`: `^18.2.0`
265
+ - `react-dom`: `^18.2.0`
266
+ - `lodash`: `^4.17.21`
267
+ - `nookies`: `^2.5.2`
268
+
269
+ Make sure these dependencies are installed:
270
+
271
+ ```bash
272
+ yarn add react react-dom lodash nookies
273
+ # or
274
+ npm install react react-dom lodash nookies
275
+ ```
276
+
277
+ ## 📡 Backend API
278
+
279
+ The chatbot expects the backend API to be configured and accessible at the provided URL. The API must implement the following endpoints:
280
+
281
+ ### Required Endpoints
282
+
283
+ #### 1. `GET /chat/welcome?locale={locale}`
284
+
285
+ **Purpose**: Retrieves the welcome message and checks API health/availability.
286
+
287
+ **Query Parameters**:
288
+ - `locale` (required): The locale code (`'pt-BR'` or `'en'`)
289
+
290
+ **Response**:
291
+ ```json
292
+ {
293
+ "welcomeMessage": "Hello! How can I help you today?"
294
+ }
295
+ ```
296
+
297
+ **Status Codes**:
298
+ - `200 OK`: API is available and welcome message is returned
299
+ - `4xx/5xx`: API is considered unavailable
300
+
301
+ **Notes**:
302
+ - This endpoint is used for health checks (via `HEAD` or `GET` requests)
303
+ - The chatbot uses this to determine connection status
304
+ - Should return a welcome message appropriate for the specified locale
305
+
306
+ ---
307
+
308
+ #### 2. `POST /chat/completion?locale={locale}`
309
+
310
+ **Purpose**: Sends a chat message and receives a streaming response from the AI assistant.
311
+
312
+ **Query Parameters**:
313
+ - `locale` (required): The locale code (`'pt-BR'` or `'en'`)
314
+
315
+ **Request Body**:
316
+ ```json
317
+ {
318
+ "context": [
319
+ {
320
+ "role": "user",
321
+ "content": "What is artificial intelligence?"
322
+ },
323
+ {
324
+ "role": "assistant",
325
+ "content": "Artificial intelligence (AI) is..."
326
+ },
327
+ {
328
+ "role": "user",
329
+ "content": "Tell me more about machine learning"
330
+ }
331
+ ]
332
+ }
333
+ ```
334
+
335
+ **Request Body Schema**:
336
+ - `context` (required, array): Array of chat messages representing the conversation history
337
+ - Each item must have:
338
+ - `role` (required, string): Either `"user"` or `"assistant"`
339
+ - `content` (required, string): The message content
340
+ - The array must not be empty
341
+ - The last message should typically be from the `"user"` role
342
+
343
+ **Response**:
344
+ - **Content-Type**: `text/event-stream`
345
+ - **Format**: Server-Sent Events (SSE) stream
346
+ - **Body**: Plain text chunks that are concatenated to form the complete response
347
+
348
+ **Response Headers**:
349
+ ```
350
+ Content-Type: text/event-stream
351
+ Cache-Control: no-cache
352
+ Connection: keep-alive
353
+ ```
354
+
355
+ **Status Codes**:
356
+ - `200 OK`: Stream started successfully
357
+ - `429 Too Many Requests`: Rate limit exceeded (should send error message via stream before closing)
358
+ - `4xx/5xx`: Error occurred
359
+
360
+ **Streaming Behavior**:
361
+ - The response is sent as a continuous stream of text chunks
362
+ - Each chunk is immediately displayed to the user as it arrives
363
+ - The stream ends when the complete response is sent
364
+ - The chatbot accumulates all chunks to form the final message
365
+
366
+ **Error Handling**:
367
+ - For `429` errors, the API should send a friendly error message via the stream before closing:
368
+ ```json
369
+ {"error": "Sorry, too many requests were made. Please try again in a few moments. 😊"}
370
+ ```
371
+ - For other errors, the stream should be closed and an appropriate HTTP status code returned
372
+
373
+ **Example Flow**:
374
+ 1. Frontend sends POST request with conversation context
375
+ 2. Backend starts streaming response chunks
376
+ 3. Frontend receives chunks and displays them incrementally
377
+ 4. Stream completes when response is finished
378
+ 5. Frontend saves the complete message to conversation history
379
+
380
+ ---
381
+
382
+ #### 3. `POST /chat/feedback`
383
+
384
+ **Purpose**: Submits user feedback about a chatbot response (rating and optional message).
385
+
386
+ **Request Body**:
387
+ ```json
388
+ {
389
+ "rating_score": 5,
390
+ "chat_context": [
391
+ {
392
+ "role": "user",
393
+ "content": "What is AI?"
394
+ },
395
+ {
396
+ "role": "assistant",
397
+ "content": "Artificial intelligence is..."
398
+ }
399
+ ],
400
+ "locale": "pt-BR",
401
+ "message": "Very helpful response!"
402
+ }
403
+ ```
404
+
405
+ **Request Body Schema**:
406
+ - `rating_score` (required, number): Rating from 1 to 5 (inclusive)
407
+ - Valid values: `1`, `2`, `3`, `4`, `5`
408
+ - `chat_context` (required, array): The conversation context that was rated
409
+ - Same structure as in `/chat/completion` endpoint
410
+ - Must not be empty
411
+ - `locale` (required, string): The locale code (`'pt-BR'` or `'en'`)
412
+ - `message` (optional, string): Optional feedback message from the user
413
+
414
+ **Response**:
415
+ - **Status Code**: `200 OK` or `201 Created` (implementation dependent)
416
+ - **Body**: Typically empty or a success confirmation
417
+
418
+ **Status Codes**:
419
+ - `200 OK` / `201 Created`: Feedback submitted successfully
420
+ - `400 Bad Request`: Invalid request body (invalid rating, missing fields, etc.)
421
+ - `4xx/5xx`: Other errors
422
+
423
+ **Notes**:
424
+ - This endpoint is called when a user rates a chatbot response
425
+ - The feedback is typically stored for analytics and improvement purposes
426
+ - The endpoint should handle the request asynchronously if possible to avoid blocking the UI
427
+
428
+ ---
429
+
430
+ ### API Requirements Summary
431
+
432
+ | Endpoint | Method | Purpose | Streaming | Required |
433
+ |----------|--------|---------|-----------|----------|
434
+ | `/chat/welcome` | `GET` | Health check & welcome message | ❌ | ✅ |
435
+ | `/chat/completion` | `POST` | Send message & receive response | ✅ | ✅ |
436
+ | `/chat/feedback` | `POST` | Submit user feedback | ❌ | ✅ |
437
+
438
+ ### Implementation Notes
439
+
440
+ 1. **CORS**: The API must allow cross-origin requests from the frontend domain
441
+ 2. **Locale Support**: All endpoints should respect the `locale` parameter and return appropriate content
442
+ 3. **Error Handling**: Provide clear error messages in the appropriate locale
443
+ 4. **Streaming**: The `/chat/completion` endpoint must support Server-Sent Events (SSE) for real-time response streaming
444
+ 5. **Rate Limiting**: Consider implementing rate limiting and return `429` status when exceeded
445
+ 6. **Content-Type**:
446
+ - Use `application/json` for JSON endpoints
447
+ - Use `text/event-stream` for streaming endpoints
448
+
449
+ ### Example API Base URL
450
+
451
+ ```jsx
452
+ <ChatbotProvider
453
+ apiURL="https://api.example.com" // Base URL without trailing slash
454
+ // ...
455
+ />
456
+ ```
457
+
458
+ The chatbot will automatically append the endpoint paths (e.g., `/chat/welcome`, `/chat/completion`, `/chat/feedback`) to this base URL.
459
+
460
+ ## 🐛 Issues and Support
461
+
462
+ If you encounter any issues or have questions:
463
+
464
+ 1. Check the [complete documentation](https://github.com/ibti-solutions/ibti-chatbot)
465
+ 2. Open an [issue on GitHub](https://github.com/ibti-solutions/ibti-chatbot/issues)
466
+ 3. Contact the IBTI team
467
+
468
+ ## 📄 License
469
+
470
+ ISC
471
+
472
+ ## 👥 Author
473
+
474
+ IBTI Soluções em TI
475
+
476
+ ---
477
+
478
+ For more information about development and contribution, see the [DEV_NOTES.md](./DEV_NOTES.md) file.