UNPKG

brainkb-assistant

Version:

A configurable, standalone BrainKB Assistant that can be integrated into any website

286 lines (241 loc) • 6.67 kB
# BrainKB Assistant A configurable, standalone chat widget that can be integrated into any website. It supports REST APIs, WebSocket connections, and provides a rich set of features for knowledge base interactions. ## šŸš€ Quick Start ### Installation ```bash npm install brainkb-assistant ``` ### Basic Usage ```jsx import BrainKBAssistant from 'brainkb-assistant'; function App() { const config = { api: { endpoint: 'https://api.example.com/chat', type: 'rest' }, branding: { title: 'BrainKB Assistant' } }; return ( <div> <h1>My Website</h1> <BrainKBAssistant config={config} /> </div> ); } ``` ## ✨ Features - **šŸ”§ Configurable** - Easy to customize branding, features, and API endpoints - **🌐 Universal** - Works with React, Next.js, static HTML, and any web framework - **šŸ“± Responsive** - Adapts to different screen sizes and devices - **šŸŽÆ Context Aware** - Automatically detects current page content - **šŸ’¬ Rich Chat** - Markdown support, file uploads, quick actions - **šŸ“Š Enhanced Tables** - Professional table rendering with download functionality - **šŸ“„ Smart Downloads** - Multi-format data export (CSV, JSON, TSV) - **šŸ”— API Flexible** - Supports REST APIs and WebSocket connections - **šŸ” JWT Authentication** - Secure authentication with username/password - **šŸŽØ Customizable** - Branding, colors, positioning, and features - **šŸ“¦ Standalone** - No dependencies on your existing codebase - **šŸ”„ Dynamic Response Parsing** - Handles any API response structure - **✨ Professional UI** - Beautiful gradients, animations, and modern design ## šŸ“š Documentation - [Installation Guide](./INSTALLATION.md) - Complete setup instructions - [Setup Guide](./SETUP_GUIDE.md) - Detailed integration guide - [Examples](./examples/) - Integration examples for different platforms ## šŸ”§ Configuration ### Basic Configuration ```javascript const config = { branding: { title: 'BrainKB Assistant', subtitle: 'Knowledge Helper', primaryColor: 'from-purple-600 to-blue-600' }, api: { endpoint: 'https://api.example.com/chat', type: 'rest', // or 'websocket' streaming: true, // Enable streaming responses headers: { 'Authorization': 'Bearer your-token' } }, features: { enableQuickActions: true, enableFileUpload: true, enableContextDetection: true, enableMarkdown: true, enableMessageEditing: true, enableCodeRendering: true, enableTypingIndicator: true }, ui: { position: 'bottom-right', // bottom-right, bottom-left, top-right, top-left size: { width: '600px', height: '700px' }, theme: 'light', // light, dark, auto zIndex: 999999 }, quickActions: [ { id: 'help', label: 'ā“ Get Help', action: 'help', description: 'Get assistance' } ] }; ``` ### JWT Authentication Configuration ```javascript const config = { api: { endpoint: 'https://api.example.com/chat', type: 'rest', streaming: true, // JWT Authentication auth: { enabled: true, type: 'jwt', jwtEndpoint: 'https://api.example.com/auth/login', username: 'your-username', password: 'your-password', // Optional: Custom token field names tokenKey: 'access_token', // Default: 'access_token' refreshTokenKey: 'refresh_token', // Default: 'refresh_token' // Optional: Auto-refresh configuration autoRefresh: true, // Default: false refreshThreshold: 300, // Refresh 5 minutes before expiry }, }, // ... other configuration }; ``` ## šŸŽÆ Integration Examples ### React Application ```jsx import BrainKBAssistant from 'brainkb-assistant'; function App() { const config = { api: { endpoint: 'https://api.example.com/chat' }, branding: { title: 'My Assistant' } }; return ( <div> <h1>My React App</h1> <BrainKBAssistant config={config} /> </div> ); } ``` ### Next.js Application ```tsx // app/layout.tsx import BrainKBAssistant from 'brainkb-assistant'; export default function RootLayout({ children, }: { children: React.ReactNode }) { const config = { api: { endpoint: process.env.NEXT_PUBLIC_API_ENDPOINT } }; return ( <html lang="en"> <body> {children} <BrainKBAssistant config={config} /> </body> </html> ); } ``` ### Static HTML ```html <!DOCTYPE html> <html> <head> <script src="https://unpkg.com/brainkb-assistant@latest/dist/index.js"></script> </head> <body> <h1>My Website</h1> <script> const config = { api: { endpoint: 'https://api.example.com/chat' } }; BrainKBAssistant.init({ config }); </script> </body> </html> ``` ## šŸ”„ Dynamic Response Handling The assistant now includes intelligent response parsing that handles any API response structure: ```javascript // These response formats all work: { content: "Hello world" } { message: "Hello world" } { data: { text: "Hello world" } } "Hello world" ["Hello", "world"] { custom_field: "Hello world" } ``` ## šŸ› ļø Development ```bash # Clone the repository git clone https://github.com/brainkb/brainkb-assistant.git # Install dependencies npm install # Start development mode npm run dev # Build for production npm run build # Publish to npm npm publish ``` ## šŸ“¦ Package Structure ``` brainkb-assistant/ ā”œā”€ā”€ src/ │ ā”œā”€ā”€ components/ │ │ ā”œā”€ā”€ BrainKBAssistant.tsx │ │ └── BrainKBAssistantWrapper.tsx │ ā”œā”€ā”€ styles/ │ │ └── brainkb-assistant.css │ └── index.ts ā”œā”€ā”€ examples/ │ ā”œā”€ā”€ react-integration.jsx │ ā”œā”€ā”€ nextjs-integration.tsx │ ā”œā”€ā”€ static-html-integration.html │ └── nodejs-server.js ā”œā”€ā”€ dist/ │ ā”œā”€ā”€ index.js │ ā”œā”€ā”€ index.esm.js │ └── index.d.ts └── package.json ``` ## šŸ¤ Contributing 1. Fork the repository 2. Create a feature branch: `git checkout -b feature-name` 3. Make your changes 4. Run tests: `npm test` 5. Commit your changes: `git commit -am 'Add feature'` 6. Push to the branch: `git push origin feature-name` 7. Submit a pull request ## šŸ“„ License MIT License - see [LICENSE](./LICENSE) for details. ## šŸ†˜ Support - šŸ“– [Documentation](./SETUP_GUIDE.md) - šŸ’” [Examples](./examples/) - šŸ› [Issues](https://github.com/brainkb/brainkb-assistant/issues) - šŸ’¬ [Discussions](https://github.com/brainkb/brainkb-assistant/discussions)