-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstandalone-demo.js
More file actions
138 lines (119 loc) · 4.52 KB
/
Copy pathstandalone-demo.js
File metadata and controls
138 lines (119 loc) · 4.52 KB
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
/* global qaBotCore */
let botController;
// Initialize the bot with user-provided configuration
function initializeBot() {
const apiKey = document.getElementById('api-key-input').value.trim();
const qaEndpoint = document.getElementById('qa-endpoint-input').value.trim();
const ratingEndpoint = document.getElementById('rating-endpoint-input').value.trim();
const backendId = document.getElementById('backend-id-input').value.trim();
const turnstileSiteKey = document.getElementById('turnstile-site-key-input').value.trim();
const allowAnonAccess = document.getElementById('allow-anon-input').checked;
if (!apiKey) {
updateStatus('Please enter an API key', 'error');
return;
}
if (!qaEndpoint) {
updateStatus('Please enter a Q&A endpoint URL', 'error');
return;
}
// Destroy existing bot if present
if (botController) {
botController.destroy();
botController = null;
}
if (typeof qaBotCore !== 'undefined') {
try {
const config = {
target: document.getElementById('qa-bot-container'),
apiKey: apiKey,
qaEndpoint: qaEndpoint,
welcomeMessage: "Hello! How can I help you today?",
primaryColor: '#24292e',
secondaryColor: '#586069',
botName: 'Demo Assistant',
logo: 'https://github.com/github.png',
placeholder: "Type your message here...",
errorMessage: "Sorry, something went wrong",
tooltipText: "Need help? Click here to chat!",
embedded: false,
enabled: true,
defaultOpen: false,
allowAnonAccess: allowAnonAccess,
isLoggedIn: !allowAnonAccess,
onAnalyticsEvent: (event) => {
console.log('[analytics]', event.type, event);
},
};
if (ratingEndpoint) config.ratingEndpoint = ratingEndpoint;
if (backendId) config.backendId = backendId;
if (turnstileSiteKey) config.turnstileSiteKey = turnstileSiteKey;
botController = qaBotCore(config);
updateStatus('Bot initialized successfully', 'success');
console.log('QA Bot initialized successfully with API key');
// Enable control buttons
document.querySelectorAll('.bot-control').forEach(btn => btn.disabled = false);
} catch (error) {
updateStatus('Failed to initialize bot: ' + error.message, 'error');
console.error('Bot initialization failed:', error);
}
} else {
updateStatus('qaBotCore not loaded. Make sure the script is built and available.', 'error');
}
}
// Update status message
function updateStatus(message, type) {
const statusEl = document.getElementById('status-message');
statusEl.textContent = message;
statusEl.className = 'status ' + type;
statusEl.style.display = 'block';
}
// Initialize on page load
window.addEventListener('load', function() {
// No pre-populated values — fill in your own endpoints and keys.
// See the README for guidance on proxy routing, Turnstile keys, etc.
// Focus API key input
document.getElementById('api-key-input').focus();
// Handle Enter key in any input field
document.querySelectorAll('input').forEach(input => {
input.addEventListener('keydown', function(e) {
if (e.key === 'Enter') {
initializeBot();
}
});
});
});
// Utility functions for demo controls
function openChat() {
if (botController) {
botController.openChat();
} else {
updateStatus('Please initialize the bot first', 'error');
}
}
function closeChat() {
if (botController) {
botController.closeChat();
} else {
updateStatus('Please initialize the bot first', 'error');
}
}
function toggleChat() {
if (botController) {
botController.toggleChat();
} else {
updateStatus('Please initialize the bot first', 'error');
}
}
function sendTestMessage() {
if (botController) {
botController.addMessage('Hello! This is a test message from the demo.');
} else {
updateStatus('Please initialize the bot first', 'error');
}
}
// Expose functions to global scope for onclick handlers
window.initializeBot = initializeBot;
window.openChat = openChat;
window.closeChat = closeChat;
window.toggleChat = toggleChat;
window.sendTestMessage = sendTestMessage;