prelington commited on
Commit
9fd5a58
·
verified ·
1 Parent(s): 8ca1a6a

Create cognito-widget.js

Browse files
Files changed (1) hide show
  1. cognito-widget.js +116 -0
cognito-widget.js ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // cognito-widget.js
2
+ (function () {
3
+ if (window.CognitoWidgetInitialized) return;
4
+ window.CognitoWidgetInitialized = true;
5
+
6
+ const CONFIG = {
7
+ apiBase: window.COGNITO_API_BASE || "https://your-cognito-server.example.com",
8
+ apiKey: window.COGNITO_API_KEY || null, // optional if you protect endpoint with a key
9
+ placeholder: "Ask Cognito...",
10
+ title: "Cognito",
11
+ welcome: "Hi — I'm Cognito. How can I help?",
12
+ };
13
+
14
+ // minimal CSS
15
+ const css = `
16
+ .cognito-chat-btn { position: fixed; right: 20px; bottom: 20px; z-index: 100000; background: #0b5fff; color: #fff; border-radius: 28px; padding: 12px 16px; cursor: pointer; box-shadow: 0 6px 18px rgba(11,95,255,0.2); font-family: Arial, sans-serif; }
17
+ .cognito-panel { position: fixed; right: 20px; bottom: 80px; width: 360px; max-width: 90vw; z-index: 100000; border-radius: 12px; box-shadow: 0 10px 40px rgba(0,0,0,0.2); overflow: hidden; font-family: Arial, sans-serif; background: #fff; display: none; flex-direction: column; }
18
+ .cognito-header { padding: 12px; background: #0b5fff; color: #fff; font-weight: bold; display:flex; align-items:center; justify-content:space-between }
19
+ .cognito-body { padding: 12px; height: 320px; overflow:auto; background:#f7f7fb }
20
+ .cognito-input-row { display:flex; padding: 8px; border-top:1px solid #eee; background:#fff }
21
+ .cognito-input { flex:1; padding:8px 10px; border-radius:8px; border:1px solid #ddd; outline:none }
22
+ .cognito-send { margin-left:8px; padding:8px 12px; border-radius:8px; border:none; background:#0b5fff; color:#fff; cursor:pointer }
23
+ .cognito-msg { margin-bottom:8px; padding:8px 10px; border-radius:8px; max-width:85% }
24
+ .cognito-msg.user { background:#0b5fff; color:#fff; margin-left:auto }
25
+ .cognito-msg.bot { background:#fff; color:#111; border:1px solid #eee }
26
+ `;
27
+
28
+ const styleEl = document.createElement("style");
29
+ styleEl.innerHTML = css;
30
+ document.head.appendChild(styleEl);
31
+
32
+ const btn = document.createElement("button");
33
+ btn.className = "cognito-chat-btn";
34
+ btn.innerText = CONFIG.title;
35
+ document.body.appendChild(btn);
36
+
37
+ const panel = document.createElement("div");
38
+ panel.className = "cognito-panel";
39
+ panel.innerHTML = `
40
+ <div class="cognito-header">${CONFIG.title} <span style="font-size:12px;opacity:.9">AI assistant</span></div>
41
+ <div class="cognito-body">
42
+ <div class="cognito-msg bot">${CONFIG.welcome}</div>
43
+ </div>
44
+ <div class="cognito-input-row">
45
+ <input class="cognito-input" placeholder="${CONFIG.placeholder}" />
46
+ <button class="cognito-send">Send</button>
47
+ </div>
48
+ `;
49
+ document.body.appendChild(panel);
50
+
51
+ btn.addEventListener("click", () => {
52
+ panel.style.display = panel.style.display === "flex" ? "none" : "flex";
53
+ panel.style.flexDirection = "column";
54
+ });
55
+
56
+ const bodyEl = panel.querySelector(".cognito-body");
57
+ const inputEl = panel.querySelector(".cognito-input");
58
+ const sendBtn = panel.querySelector(".cognito-send");
59
+
60
+ function appendMessage(text, who="bot") {
61
+ const m = document.createElement("div");
62
+ m.className = "cognito-msg " + (who==="user" ? "user" : "bot");
63
+ m.innerText = text;
64
+ bodyEl.appendChild(m);
65
+ bodyEl.scrollTop = bodyEl.scrollHeight;
66
+ }
67
+
68
+ async function sendMessage(content) {
69
+ appendMessage(content, "user");
70
+ inputEl.value = "";
71
+ const payload = {
72
+ messages: [
73
+ {role: "user", content}
74
+ ]
75
+ };
76
+ const headers = {"Content-Type": "application/json"};
77
+ if (CONFIG.apiKey) headers["x-api-key"] = CONFIG.apiKey;
78
+ try {
79
+ const res = await fetch(CONFIG.apiBase + "/chat", {
80
+ method: "POST",
81
+ headers,
82
+ body: JSON.stringify(payload)
83
+ });
84
+ if (!res.ok) {
85
+ const txt = await res.text();
86
+ appendMessage("Error: " + res.status + " - " + txt, "bot");
87
+ return;
88
+ }
89
+ const data = await res.json();
90
+ appendMessage(data.reply || JSON.stringify(data), "bot");
91
+ } catch (err) {
92
+ appendMessage("Network error: " + err.message, "bot");
93
+ }
94
+ }
95
+
96
+ sendBtn.addEventListener("click", () => {
97
+ const v = inputEl.value.trim();
98
+ if (!v) return;
99
+ sendMessage(v);
100
+ });
101
+
102
+ inputEl.addEventListener("keydown", (e) => {
103
+ if (e.key === "Enter") {
104
+ e.preventDefault();
105
+ sendBtn.click();
106
+ }
107
+ });
108
+
109
+ // Expose small API to host page
110
+ window.CognitoWidget = {
111
+ open: () => { panel.style.display = "flex"; },
112
+ close: () => { panel.style.display = "none"; },
113
+ send: (text) => sendMessage(text),
114
+ appendSystem: (text) => appendMessage(text, "bot")
115
+ };
116
+ })();