Spaces:
Sleeping
Sleeping
USAMA BHATTI
commited on
Commit
·
acbbba2
1
Parent(s):
5408bf6
FINAL DEPLOY READY: All Docker and CPU dependencies fixed.
Browse files- static/widget.js +62 -70
static/widget.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
(function() {
|
| 2 |
// ----------------------------------------------------
|
| 3 |
-
// 1. CONFIGURATION
|
| 4 |
// ----------------------------------------------------
|
| 5 |
const scriptTag = document.currentScript;
|
| 6 |
|
|
@@ -13,113 +13,106 @@
|
|
| 13 |
return;
|
| 14 |
}
|
| 15 |
|
| 16 |
-
// Modern way to generate unique ID (Fixing substr deprecated warning)
|
| 17 |
const CHAT_SESSION_ID = "omni_session_" + Math.random().toString(36).slice(2, 11);
|
| 18 |
|
| 19 |
// ----------------------------------------------------
|
| 20 |
-
// 2. STYLES
|
| 21 |
// ----------------------------------------------------
|
| 22 |
const style = document.createElement('style');
|
| 23 |
style.innerHTML = `
|
| 24 |
#omni-widget-container {
|
| 25 |
-
position: fixed; bottom: 20px; right: 20px; z-index:
|
| 26 |
-
transition: all 0.3s;
|
| 27 |
}
|
| 28 |
#omni-chat-btn {
|
| 29 |
background: ${THEME_COLOR}; color: white; border: none; padding: 15px; border-radius: 50%;
|
| 30 |
cursor: pointer; box-shadow: 0 4px 12px rgba(0,0,0,0.4); width: 60px; height: 60px; font-size: 24px;
|
| 31 |
-
display: flex; align-items: center; justify-content: center;
|
| 32 |
}
|
|
|
|
| 33 |
#omni-chat-window {
|
| 34 |
-
display: none; width: 350px; height: 500px; background: white; border-radius:
|
| 35 |
box-shadow: 0 10px 30px rgba(0,0,0,0.5); flex-direction: column; overflow: hidden;
|
| 36 |
-
margin-bottom: 15px;
|
| 37 |
}
|
| 38 |
#omni-header {
|
| 39 |
background: ${THEME_COLOR}; color: white; padding: 15px; font-weight: 600; display: flex;
|
| 40 |
-
justify-content: space-between; align-items: center;
|
| 41 |
}
|
| 42 |
-
#omni-messages { flex: 1; padding:
|
| 43 |
-
#omni-input-area { display: flex; border-top: 1px solid #ddd; }
|
| 44 |
#omni-input { flex: 1; padding: 12px; border: none; outline: none; font-size: 14px; }
|
| 45 |
-
#omni-send { background:
|
| 46 |
-
.omni-msg { margin: 8px 0; padding: 10px
|
| 47 |
-
.omni-msg.user { background: ${THEME_COLOR}; color: white;
|
| 48 |
-
.omni-msg.bot { background: #
|
| 49 |
-
|
| 50 |
-
@keyframes fadeIn { from { opacity: 0; transform: scale(0.9); } to { opacity: 1; transform: scale(1); } }
|
| 51 |
`;
|
| 52 |
document.head.appendChild(style);
|
| 53 |
|
| 54 |
// ----------------------------------------------------
|
| 55 |
-
// 3.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
// ----------------------------------------------------
|
| 57 |
const container = document.createElement('div');
|
| 58 |
container.id = 'omni-widget-container';
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
</div>
|
|
|
|
| 72 |
`;
|
| 73 |
|
| 74 |
-
const chatBtn = document.createElement('button');
|
| 75 |
-
chatBtn.id = 'omni-chat-btn';
|
| 76 |
-
chatBtn.innerHTML = '💬';
|
| 77 |
-
|
| 78 |
-
// onClick ko addEventListener se theek kiya
|
| 79 |
-
chatBtn.addEventListener('click', toggleOmniChat);
|
| 80 |
-
|
| 81 |
-
container.appendChild(chatWindow);
|
| 82 |
-
container.appendChild(chatBtn);
|
| 83 |
document.body.appendChild(container);
|
| 84 |
|
| 85 |
// ----------------------------------------------------
|
| 86 |
-
//
|
| 87 |
// ----------------------------------------------------
|
| 88 |
-
|
| 89 |
const inputField = document.getElementById('omni-input');
|
| 90 |
const sendButton = document.getElementById('omni-send');
|
| 91 |
-
|
| 92 |
-
window.toggleOmniChat = function() {
|
| 93 |
-
const win = document.getElementById('omni-chat-window');
|
| 94 |
-
const isVisible = win.style.display === 'flex';
|
| 95 |
-
win.style.display = isVisible ? 'none' : 'flex';
|
| 96 |
-
if (!isVisible) {
|
| 97 |
-
inputField.focus();
|
| 98 |
-
}
|
| 99 |
-
};
|
| 100 |
|
| 101 |
function addMessage(text, sender) {
|
| 102 |
-
const msgs = document.getElementById('omni-messages');
|
| 103 |
const div = document.createElement('div');
|
| 104 |
div.className = `omni-msg ${sender}`;
|
| 105 |
-
div.innerHTML = text.replace(/(https?:\/\/[^\s]+)/g, '<a href="$1" target="_blank" style="color:
|
| 106 |
-
|
| 107 |
-
|
| 108 |
}
|
| 109 |
|
| 110 |
async function sendMessage() {
|
| 111 |
-
const originalBtnText = sendButton.innerHTML;
|
| 112 |
const text = inputField.value.trim();
|
| 113 |
if (!text) return;
|
| 114 |
|
| 115 |
addMessage(text, 'user');
|
| 116 |
inputField.value = '';
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
try {
|
| 122 |
-
// Backend API Call
|
| 123 |
const response = await fetch(`${API_URL}/api/v1/chat`, {
|
| 124 |
method: 'POST',
|
| 125 |
headers: { 'Content-Type': 'application/json' },
|
|
@@ -130,24 +123,23 @@
|
|
| 130 |
})
|
| 131 |
});
|
| 132 |
const data = await response.json();
|
| 133 |
-
|
|
|
|
| 134 |
} catch (error) {
|
| 135 |
-
|
|
|
|
| 136 |
console.error("OmniAgent API Error:", error);
|
| 137 |
-
} finally {
|
| 138 |
-
inputField.disabled = false;
|
| 139 |
-
sendButton.innerHTML = originalBtnText;
|
| 140 |
-
sendButton.disabled = false;
|
| 141 |
-
inputField.focus();
|
| 142 |
}
|
| 143 |
}
|
| 144 |
|
| 145 |
-
// Modern Event Listeners (Fixing deprecated 'onkeypress')
|
| 146 |
sendButton.addEventListener('click', sendMessage);
|
| 147 |
inputField.addEventListener('keypress', (e) => {
|
| 148 |
-
if(e.key === 'Enter') {
|
| 149 |
-
sendMessage();
|
| 150 |
-
e.preventDefault(); // Enter key ka default action roko
|
| 151 |
-
}
|
| 152 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
})();
|
|
|
|
| 1 |
(function() {
|
| 2 |
// ----------------------------------------------------
|
| 3 |
+
// 1. CONFIGURATION
|
| 4 |
// ----------------------------------------------------
|
| 5 |
const scriptTag = document.currentScript;
|
| 6 |
|
|
|
|
| 13 |
return;
|
| 14 |
}
|
| 15 |
|
|
|
|
| 16 |
const CHAT_SESSION_ID = "omni_session_" + Math.random().toString(36).slice(2, 11);
|
| 17 |
|
| 18 |
// ----------------------------------------------------
|
| 19 |
+
// 2. STYLES
|
| 20 |
// ----------------------------------------------------
|
| 21 |
const style = document.createElement('style');
|
| 22 |
style.innerHTML = `
|
| 23 |
#omni-widget-container {
|
| 24 |
+
position: fixed; bottom: 20px; right: 20px; z-index: 999999; font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
|
|
|
|
| 25 |
}
|
| 26 |
#omni-chat-btn {
|
| 27 |
background: ${THEME_COLOR}; color: white; border: none; padding: 15px; border-radius: 50%;
|
| 28 |
cursor: pointer; box-shadow: 0 4px 12px rgba(0,0,0,0.4); width: 60px; height: 60px; font-size: 24px;
|
| 29 |
+
display: flex; align-items: center; justify-content: center; transition: transform 0.2s;
|
| 30 |
}
|
| 31 |
+
#omni-chat-btn:hover { transform: scale(1.1); }
|
| 32 |
#omni-chat-window {
|
| 33 |
+
display: none; width: 350px; height: 500px; background: white; border-radius: 12px;
|
| 34 |
box-shadow: 0 10px 30px rgba(0,0,0,0.5); flex-direction: column; overflow: hidden;
|
| 35 |
+
margin-bottom: 15px; border: 1px solid #ddd;
|
| 36 |
}
|
| 37 |
#omni-header {
|
| 38 |
background: ${THEME_COLOR}; color: white; padding: 15px; font-weight: 600; display: flex;
|
| 39 |
+
justify-content: space-between; align-items: center;
|
| 40 |
}
|
| 41 |
+
#omni-messages { flex: 1; padding: 15px; overflow-y: auto; background: #f9f9f9; display: flex; flex-direction: column; }
|
| 42 |
+
#omni-input-area { display: flex; border-top: 1px solid #ddd; background: white; }
|
| 43 |
#omni-input { flex: 1; padding: 12px; border: none; outline: none; font-size: 14px; }
|
| 44 |
+
#omni-send { background: transparent; border: none; color: ${THEME_COLOR}; font-weight: bold; cursor: pointer; padding: 0 15px; font-size: 20px; }
|
| 45 |
+
.omni-msg { margin: 8px 0; padding: 10px 14px; border-radius: 15px; max-width: 85%; font-size: 14px; word-wrap: break-word; }
|
| 46 |
+
.omni-msg.user { background: ${THEME_COLOR}; color: white; align-self: flex-end; border-bottom-right-radius: 2px; }
|
| 47 |
+
.omni-msg.bot { background: #eef2f7; color: #333; align-self: flex-start; border-bottom-left-radius: 2px; border: 1px solid #d1d9e6; }
|
|
|
|
|
|
|
| 48 |
`;
|
| 49 |
document.head.appendChild(style);
|
| 50 |
|
| 51 |
// ----------------------------------------------------
|
| 52 |
+
// 3. UI LOGIC (Pehle Define Karen)
|
| 53 |
+
// ----------------------------------------------------
|
| 54 |
+
window.toggleOmniChat = function() {
|
| 55 |
+
const win = document.getElementById('omni-chat-window');
|
| 56 |
+
if (!win) return;
|
| 57 |
+
const isVisible = win.style.display === 'flex';
|
| 58 |
+
win.style.display = isVisible ? 'none' : 'flex';
|
| 59 |
+
if (!isVisible) {
|
| 60 |
+
document.getElementById('omni-input').focus();
|
| 61 |
+
}
|
| 62 |
+
};
|
| 63 |
+
|
| 64 |
+
// ----------------------------------------------------
|
| 65 |
+
// 4. HTML STRUCTURE
|
| 66 |
// ----------------------------------------------------
|
| 67 |
const container = document.createElement('div');
|
| 68 |
container.id = 'omni-widget-container';
|
| 69 |
|
| 70 |
+
container.innerHTML = `
|
| 71 |
+
<div id="omni-chat-window">
|
| 72 |
+
<div id="omni-header">
|
| 73 |
+
<span>AI Support Agent</span>
|
| 74 |
+
<span style="cursor:pointer; font-size: 22px; line-height: 20px;" onclick="window.toggleOmniChat()">×</span>
|
| 75 |
+
</div>
|
| 76 |
+
<div id="omni-messages"></div>
|
| 77 |
+
<div id="omni-input-area">
|
| 78 |
+
<input type="text" id="omni-input" placeholder="Ask me anything..." autocomplete="off" />
|
| 79 |
+
<button id="omni-send">➤</button>
|
| 80 |
+
</div>
|
| 81 |
</div>
|
| 82 |
+
<button id="omni-chat-btn" onclick="window.toggleOmniChat()">💬</button>
|
| 83 |
`;
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
document.body.appendChild(container);
|
| 86 |
|
| 87 |
// ----------------------------------------------------
|
| 88 |
+
// 5. CHAT FUNCTIONALITY
|
| 89 |
// ----------------------------------------------------
|
|
|
|
| 90 |
const inputField = document.getElementById('omni-input');
|
| 91 |
const sendButton = document.getElementById('omni-send');
|
| 92 |
+
const messagesContainer = document.getElementById('omni-messages');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
function addMessage(text, sender) {
|
|
|
|
| 95 |
const div = document.createElement('div');
|
| 96 |
div.className = `omni-msg ${sender}`;
|
| 97 |
+
div.innerHTML = text.replace(/(https?:\/\/[^\s]+)/g, '<a href="$1" target="_blank" style="color:inherit; text-decoration:underline;">$1</a>');
|
| 98 |
+
messagesContainer.appendChild(div);
|
| 99 |
+
messagesContainer.scrollTop = messagesContainer.scrollHeight;
|
| 100 |
}
|
| 101 |
|
| 102 |
async function sendMessage() {
|
|
|
|
| 103 |
const text = inputField.value.trim();
|
| 104 |
if (!text) return;
|
| 105 |
|
| 106 |
addMessage(text, 'user');
|
| 107 |
inputField.value = '';
|
| 108 |
+
|
| 109 |
+
// Show Loading State
|
| 110 |
+
const loadingDiv = document.createElement('div');
|
| 111 |
+
loadingDiv.className = 'omni-msg bot';
|
| 112 |
+
loadingDiv.innerHTML = '...';
|
| 113 |
+
messagesContainer.appendChild(loadingDiv);
|
| 114 |
|
| 115 |
try {
|
|
|
|
| 116 |
const response = await fetch(`${API_URL}/api/v1/chat`, {
|
| 117 |
method: 'POST',
|
| 118 |
headers: { 'Content-Type': 'application/json' },
|
|
|
|
| 123 |
})
|
| 124 |
});
|
| 125 |
const data = await response.json();
|
| 126 |
+
messagesContainer.removeChild(loadingDiv);
|
| 127 |
+
addMessage(data.response || data.message || "No response received", 'bot');
|
| 128 |
} catch (error) {
|
| 129 |
+
messagesContainer.removeChild(loadingDiv);
|
| 130 |
+
addMessage("Connection error. Please try again.", 'bot');
|
| 131 |
console.error("OmniAgent API Error:", error);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
}
|
| 133 |
}
|
| 134 |
|
|
|
|
| 135 |
sendButton.addEventListener('click', sendMessage);
|
| 136 |
inputField.addEventListener('keypress', (e) => {
|
| 137 |
+
if(e.key === 'Enter') { sendMessage(); }
|
|
|
|
|
|
|
|
|
|
| 138 |
});
|
| 139 |
+
|
| 140 |
+
// Welcome Message
|
| 141 |
+
setTimeout(() => {
|
| 142 |
+
addMessage("Hello! How can I assist you with the SDD-RI Book today?", "bot");
|
| 143 |
+
}, 1000);
|
| 144 |
+
|
| 145 |
})();
|