Spaces:
Sleeping
Sleeping
File size: 9,483 Bytes
6980b1d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const database_1 = require("../db/database");
const router = express_1.default.Router();
// Get widget configuration
router.get('/config/:tenantId', async (req, res) => {
try {
const { tenantId } = req.params;
// Get tenant info
const tenant = await database_1.database.get('SELECT id, name, plan FROM tenants WHERE id = ?', [tenantId]);
if (!tenant) {
return res.status(404).json({ error: 'Tenant not found' });
}
// Get widget configuration
const config = await database_1.database.get('SELECT config FROM widget_configs WHERE tenant_id = ?', [tenantId]);
const defaultConfig = {
theme: {
primaryColor: '#6366f1',
backgroundColor: '#ffffff',
textColor: '#374151',
borderRadius: '12px'
},
position: {
bottom: '20px',
right: '20px'
},
size: 'medium',
welcome: {
title: `Hi! I'm ${tenant.name}'s AI assistant`,
message: 'How can I help you today?',
showAvatar: true
},
features: {
fileUpload: false,
typing: true,
ratings: true
}
};
const widgetConfig = config ?
{ ...defaultConfig, ...JSON.parse(config.config) } :
defaultConfig;
res.json({
tenantId,
config: widgetConfig
});
}
catch (error) {
console.error('Get widget config error:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
// Get widget script
router.get('/script/:tenantId', async (req, res) => {
try {
const { tenantId } = req.params;
// Verify tenant exists
const tenant = await database_1.database.get('SELECT id FROM tenants WHERE id = ?', [tenantId]);
if (!tenant) {
return res.status(404).json({ error: 'Tenant not found' });
}
const script = `
(function() {
// MCP Chat Widget
const TENANT_ID = '${tenantId}';
const API_URL = '${process.env.FRONTEND_URL || 'http://localhost:3001'}/api';
// Create widget container
const widget = document.createElement('div');
widget.id = 'mcp-chat-widget';
widget.style.cssText = \`
position: fixed;
bottom: 20px;
right: 20px;
width: 350px;
height: 500px;
background: white;
border-radius: 12px;
box-shadow: 0 10px 25px rgba(0,0,0,0.1);
z-index: 10000;
display: none;
flex-direction: column;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
border: 1px solid #e5e7eb;
\`;
// Create chat button
const button = document.createElement('div');
button.id = 'mcp-chat-button';
button.style.cssText = \`
position: fixed;
bottom: 20px;
right: 20px;
width: 60px;
height: 60px;
background: linear-gradient(135deg, #6366f1, #8b5cf6);
border-radius: 50%;
cursor: pointer;
z-index: 10001;
display: flex;
align-items: center;
justify-content: center;
box-shadow: 0 4px 12px rgba(99, 102, 241, 0.4);
transition: transform 0.2s ease;
\`;
button.innerHTML = \`
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="2">
<path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"></path>
</svg>
\`;
button.onmouseover = () => button.style.transform = 'scale(1.05)';
button.onmouseout = () => button.style.transform = 'scale(1)';
// Widget header
const header = document.createElement('div');
header.style.cssText = \`
padding: 16px;
background: linear-gradient(135deg, #6366f1, #8b5cf6);
color: white;
border-radius: 12px 12px 0 0;
display: flex;
justify-content: space-between;
align-items: center;
\`;
header.innerHTML = \`
<div>
<h3 style="margin: 0; font-size: 16px; font-weight: 600;">Chat Support</h3>
<p style="margin: 4px 0 0 0; font-size: 12px; opacity: 0.9;">We're here to help!</p>
</div>
<button id="mcp-close-btn" style="background: none; border: none; color: white; font-size: 20px; cursor: pointer; padding: 4px;">×</button>
\`;
// Messages container
const messages = document.createElement('div');
messages.id = 'mcp-messages';
messages.style.cssText = \`
flex: 1;
padding: 16px;
overflow-y: auto;
display: flex;
flex-direction: column;
gap: 12px;
\`;
// Input container
const inputContainer = document.createElement('div');
inputContainer.style.cssText = \`
padding: 16px;
border-top: 1px solid #e5e7eb;
display: flex;
gap: 8px;
\`;
const input = document.createElement('input');
input.type = 'text';
input.placeholder = 'Type your message...';
input.style.cssText = \`
flex: 1;
padding: 12px;
border: 1px solid #d1d5db;
border-radius: 8px;
outline: none;
font-size: 14px;
\`;
const sendBtn = document.createElement('button');
sendBtn.textContent = 'Send';
sendBtn.style.cssText = \`
padding: 12px 16px;
background: #6366f1;
color: white;
border: none;
border-radius: 8px;
cursor: pointer;
font-size: 14px;
font-weight: 500;
\`;
// Assemble widget
inputContainer.appendChild(input);
inputContainer.appendChild(sendBtn);
widget.appendChild(header);
widget.appendChild(messages);
widget.appendChild(inputContainer);
// Add to page
document.body.appendChild(button);
document.body.appendChild(widget);
// Session management
let sessionToken = null;
let isVisible = false;
// Toggle widget
function toggleWidget() {
isVisible = !isVisible;
widget.style.display = isVisible ? 'flex' : 'none';
button.style.display = isVisible ? 'none' : 'flex';
if (isVisible && !sessionToken) {
initializeSession();
}
}
// Initialize chat session
async function initializeSession() {
try {
const response = await fetch(\`\${API_URL}/chat/sessions\`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
tenantId: TENANT_ID,
domain: window.location.hostname,
userAgent: navigator.userAgent
})
});
const data = await response.json();
sessionToken = data.sessionToken;
// Add welcome message
addMessage('ai', 'Hello! How can I help you today?');
} catch (error) {
console.error('Failed to initialize session:', error);
addMessage('ai', 'Sorry, I\\'m having trouble connecting. Please try again later.');
}
}
// Send message
async function sendMessage(message) {
if (!sessionToken || !message.trim()) return;
addMessage('user', message);
input.value = '';
// Show typing indicator
const typingEl = addMessage('ai', 'Typing...', true);
try {
const response = await fetch(\`\${API_URL}/chat/messages\`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
sessionToken,
message,
tenantId: TENANT_ID
})
});
const data = await response.json();
// Remove typing indicator
typingEl.remove();
// Add AI response
addMessage('ai', data.response);
} catch (error) {
typingEl.remove();
addMessage('ai', 'Sorry, I encountered an error. Please try again.');
}
}
// Add message to chat
function addMessage(sender, text, isTyping = false) {
const messageEl = document.createElement('div');
messageEl.style.cssText = \`
max-width: 80%;
padding: 12px 16px;
border-radius: 18px;
font-size: 14px;
line-height: 1.4;
\${sender === 'user' ?
'background: #6366f1; color: white; align-self: flex-end; margin-left: auto;' :
'background: #f3f4f6; color: #374151; align-self: flex-start;'
}
\${isTyping ? 'opacity: 0.7; font-style: italic;' : ''}
\`;
messageEl.textContent = text;
messages.appendChild(messageEl);
messages.scrollTop = messages.scrollHeight;
return messageEl;
}
// Event listeners
button.onclick = toggleWidget;
header.querySelector('#mcp-close-btn').onclick = toggleWidget;
sendBtn.onclick = () => sendMessage(input.value);
input.onkeypress = (e) => {
if (e.key === 'Enter') sendMessage(input.value);
};
console.log('MCP Chat Widget loaded successfully');
})();
`;
res.setHeader('Content-Type', 'application/javascript');
res.send(script);
}
catch (error) {
console.error('Get widget script error:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
exports.default = router;
//# sourceMappingURL=widget.js.map |