Modern AI Chatbot Widget SDK with Shadow DOM isolation and Dify.ai integration
npm install chat-snail-sdkA modern, customizable AI chatbot widget SDK with Shadow DOM isolation. Perfect for integrating AI-powered chat functionality into any website while maintaining complete style isolation and security.
- šØ Customizable Themes - Light/Dark mode with smooth transitions
- š Multi-language Support - Built-in support for 6 languages (EN, ZH, ES, FR, JA)
- š Shadow DOM Isolation - Complete style isolation from host page
- š± Responsive Design - Works seamlessly on desktop and mobile
- šÆ Draggable Interface - Move the chat window anywhere on screen
- š¾ Persistent Conversations - LocalStorage integration for chat history
- šø Image Upload Support - Send images with messages (PNG, JPG, WebP, GIF)
- ā” Lightweight - Less than 50KB minified
- š Security First - Built-in API key security warnings
- š¬ Smooth Animations - Professional transitions and effects
- āæ Accessibility - ARIA labels and keyboard navigation
``html`
`javascript`
const widget = new AIChatWidget({
apiProvider: 'dify',
apiKey: 'app-your-dify-api-key',
difyConfig: {
baseUrl: 'https://api.dify.ai',
streaming: true
}
}).init();
`bash`
npm install ai-chat-widget-sdk
`javascript
import AIChatWidget from 'ai-chat-widget-sdk';
const widget = new AIChatWidget({
theme: 'dark',
language: 'en',
apiEndpoint: 'https://your-api.com/chat'
}).init();
`
`javascript
const widget = new AIChatWidget({
// Appearance
position: 'bottom-right', // 'bottom-right', 'bottom-left', 'top-right', 'top-left'
theme: 'light', // 'light' or 'dark'
primaryColor: '#007bff', // Custom primary color
width: 380, // Chat window width
height: 600, // Chat window height
zIndex: 9999, // z-index for stacking
// Content
title: 'AI Assistant', // Chat window title
subtitle: 'Powered by AI', // Subtitle text
welcomeMessage: 'Hello!', // Initial bot message
placeholder: 'Type here...', // Input placeholder
buttonIcon: 'š¬', // Floating button icon
userAvatar: 'š¤', // User message avatar
botAvatar: 'š¤', // Bot message avatar
// Behavior
language: 'en', // 'en', 'zh', 'es', 'fr', 'ja'
enableDrag: true, // Allow dragging chat window
autoOpen: false, // Auto-open on load
autoOpenDelay: 3000, // Delay before auto-open (ms)
persistConversation: true, // Save chat history
maxMessageLength: 1000, // Max input length
// API Configuration
apiEndpoint: null, // Your chat API endpoint
apiKey: null, // API key (ā ļø Use server-side!)
apiProvider: 'custom', // 'custom' or 'dify'
difyConfig: { // Dify.ai specific config
baseUrl: 'https://api.dify.ai',
user: null, // User identifier
streaming: true // Enable streaming responses
}
// Advanced Features
enableFileUpload: false, // Enable file attachments
enableVoiceInput: false, // Enable voice input
animations: {
enabled: true,
duration: 300
},
sounds: {
enabled: false,
newMessage: null, // Sound file URL
sendMessage: null // Sound file URL
}
});
`
`javascript
// Core Methods
widget.init(); // Initialize the widget
widget.destroy(); // Remove widget from DOM
widget.toggle(); // Toggle open/close
widget.open(); // Open chat window
widget.close(); // Close chat window
// Messaging
widget.sendMessage('Hello!'); // Send a message programmatically
// Customization
widget.setTheme('dark'); // Change theme
widget.setLanguage('zh'); // Change language
// Events
widget.on('message', callback); // Listen for events
widget.off('message', callback); // Remove listener
`
`javascript
widget.on('init', () => {
console.log('Widget initialized');
});
widget.on('open', () => {
console.log('Chat opened');
});
widget.on('close', () => {
console.log('Chat closed');
});
widget.on('message', (data) => {
console.log('User message:', data);
// { type: 'user', content: 'Hello', timestamp: '...' }
});
widget.on('response', (data) => {
console.log('Bot response:', data);
// { type: 'bot', content: 'Hi there!', timestamp: '...' }
});
widget.on('error', (error) => {
console.error('Error occurred:', error);
});
widget.on('theme-change', (theme) => {
console.log('Theme changed to:', theme);
});
widget.on('language-change', (lang) => {
console.log('Language changed to:', lang);
});
`
Never expose API keys in frontend code! The SDK will warn you if it detects an API key without a server endpoint.
#### ā Bad (Insecure)
`javascript`
const widget = new AIChatWidget({
apiKey: 'sk-abc123...' // NEVER DO THIS!
});
#### ā
Good (Secure)
`javascript
// Frontend
const widget = new AIChatWidget({
apiEndpoint: 'https://your-server.com/api/chat'
// No API key here!
});
// Backend (Node.js example)
app.post('/api/chat', async (req, res) => {
const response = await openai.chat({
apiKey: process.env.OPENAI_API_KEY, // Secure!
message: req.body.message
});
res.json(response);
});
`
The widget uses Shadow DOM for complete style isolation. You can customize the appearance through:
1. Configuration options - Set colors, sizes, and positions
2. Theme presets - Light and dark themes included
3. Custom CSS variables - Override default styles
`javascript`
// Custom theme example
const widget = new AIChatWidget({
primaryColor: '#ff6b6b',
theme: 'dark',
width: 400,
height: 550
});
Built-in support for multiple languages:
- š¬š§ English (en)
- šØš³ Chinese (zh)
- šŖšø Spanish (es)
- š«š· French (fr)
- šÆšµ Japanese (ja)
`javascript
// Change language dynamically
widget.setLanguage('zh');
// Or set during initialization
const widget = new AIChatWidget({
language: 'es'
});
`
The widget is fully responsive and adapts to mobile screens:
- Auto-fullscreen on small devices
- Touch-friendly interface
- Optimized keyboard handling
- Gesture support for dragging
`bashClone the repository
git clone https://github.com/yourusername/ai-chat-widget-sdk.git
$3
`
ai-widget-sdk/
āāā src/
ā āāā index.js # Main entry point
ā āāā core/
ā ā āāā WidgetCore.js # Core widget logic
ā āāā components/
ā ā āāā ChatWindow.js # Chat window component
ā ā āāā FloatingButton.js # Floating button
ā ā āāā MessageList.js # Message list
ā ā āāā InputArea.js # Input area
ā āāā utils/
ā ā āāā EventEmitter.js # Event system
ā ā āāā StorageManager.js # LocalStorage handler
ā ā āāā DragManager.js # Drag functionality
ā ā āāā i18n.js # Translations
ā āāā styles/
ā āāā main.css # Global styles
āāā dist/
ā āāā widget.min.js # Production build
āāā examples/
ā āāā index.html # Demo page
āāā package.json
`š¤ Browser Support
- Chrome 60+
- Firefox 55+
- Safari 11+
- Edge 79+
- Mobile browsers (iOS Safari, Chrome Mobile)
š License
MIT License - feel free to use in personal and commercial projects.
š Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
1. Fork the repository
2. Create your feature branch (
git checkout -b feature/AmazingFeature)
3. Commit your changes (git commit -m 'Add some AmazingFeature')
4. Push to the branch (git push origin feature/AmazingFeature)
5. Open a Pull Requestš” Examples
$3
`html
My Website
Welcome to my site!
`$3
`jsx
import { useEffect, useRef } from 'react';
import AIChatWidget from 'ai-chat-widget-sdk';function App() {
const widgetRef = useRef(null);
useEffect(() => {
widgetRef.current = new AIChatWidget({
theme: 'dark',
language: 'en'
}).init();
return () => {
widgetRef.current?.destroy();
};
}, []);
return
Your React App;
}
`$3
`vue
Your Vue App
``- Documentation
- NPM Package
- GitHub Repository
- Live Demo
For issues and questions:
- Open an issue on GitHub
- Email: support@example.com
---
Made with ā¤ļø by Your Team