-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathchatbot.js
More file actions
197 lines (164 loc) · 6.4 KB
/
Copy pathchatbot.js
File metadata and controls
197 lines (164 loc) · 6.4 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
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
(() => {
const modal = document.getElementById('chatbot-modal');
if (!modal) return;
modal.innerHTML = `
<div id="chatbot-header">
<span class="text-on-surface font-label-mono text-sm tracking-wider font-bold">OMNIKON AI CORE v1.0</span>
<button id="chatbot-close" class="text-on-surface-variant hover:text-primary transition-colors text-sm">✖</button>
</div>
<div id="chatbot-messages"></div>
<div id="chatbot-input">
<input id="chatbot-text" type="text" placeholder="Ask about projects, contributors, or rules..." />
<button id="chatbot-send">SEND</button>
</div>`;
const toggleBtn = document.getElementById('chatbot-toggle');
const closeBtn = document.getElementById('chatbot-close');
const sendBtn = document.getElementById('chatbot-send');
const input = document.getElementById('chatbot-text');
const messages = document.getElementById('chatbot-messages');
let orgData = null;
let chatHistory = [];
let isLoaded = false;
let hasGreeted = false;
async function loadOrgData() {
try {
const res = await fetch('/github_summary.json');
if (res.ok) {
orgData = await res.json();
}
} catch (e) {
console.warn('Could not load github_summary.json for chatbot context', e);
}
}
function formatMarkdown(text) {
let escaped = text
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
escaped = escaped.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
escaped = escaped.replace(/`(.*?)`/g, '<code class="bg-[#1e1e1e] border border-[#333] px-1 py-0.5 font-mono text-xs text-primary">$1</code>');
escaped = escaped.replace(/\[(.*?)\]\((.*?)\)/g, '<a href="$2" target="_blank" class="text-primary hover:underline">$1</a>');
const lines = escaped.split('\n');
const formattedLines = lines.map(line => {
const trimmed = line.trim();
if (trimmed.startsWith('- ') || trimmed.startsWith('* ')) {
return `<li class="ml-4 list-disc text-on-surface-variant">${trimmed.substring(2)}</li>`;
}
return line;
});
return formattedLines.join('<br>');
}
function getSystemPrompt() {
let context = '';
if (orgData) {
const projects = orgData.repos
? orgData.repos.map(r => `- ${r.name}: ${r.description || 'No description'} (${r.stars} stars, URL: ${r.html_url})`).join('\n')
: '';
const members = orgData.members
? orgData.members.map(m => `- ${m.login} (${m.html_url})`).join('\n')
: '';
context = `Here is the current real-time data about the Omnikon organization:\n\nActive Projects:\n${projects}\n\nKey Members/Contributors:\n${members}`;
}
return `You are the official AI Assistant for the Omnikon Open Source Organization.
Your sole purpose is to help users learn about the Omnikon community, its active projects, repositories, code of conduct, guidelines, and contributors.
${context}
RULES:
1. ONLY answer questions related to Omnikon, its projects, and its community.
2. If a query is unrelated to Omnikon (e.g. general knowledge, unrelated coding, general questions), politely refuse to answer. Say: "I am the Omnikon assistant, and I can only answer questions related to the organization."
3. Keep answers technical, concise, clear, and direct.
4. Respond in Markdown format where appropriate.`;
}
const openModal = async () => {
modal.classList.add('open');
modal.classList.remove('hidden');
input.focus();
if (!isLoaded) {
await loadOrgData();
isLoaded = true;
}
if (!hasGreeted) {
appendMessage('Hello! I am the Omnikon AI Assistant. I can help you with questions about our active projects, guidelines, contributors, or how to get started in our community. What would you like to know?', 'bot');
hasGreeted = true;
}
};
const closeModal = () => {
modal.classList.remove('open');
setTimeout(() => modal.classList.add('hidden'), 300);
};
toggleBtn.addEventListener('click', openModal);
closeBtn.addEventListener('click', closeModal);
const appendMessage = (text, role) => {
const msg = document.createElement('div');
msg.className = `chatbot-msg ${role}`;
if (role === 'bot') {
msg.innerHTML = formatMarkdown(text);
} else {
msg.textContent = text;
}
messages.appendChild(msg);
messages.scrollTop = messages.scrollHeight;
};
const showTypingIndicator = () => {
const indicator = document.createElement('div');
indicator.className = 'chatbot-msg bot typing-indicator';
indicator.id = 'chatbot-typing';
indicator.innerHTML = `
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
`;
messages.appendChild(indicator);
messages.scrollTop = messages.scrollHeight;
};
const removeTypingIndicator = () => {
const indicator = document.getElementById('chatbot-typing');
if (indicator) {
indicator.remove();
}
};
const sendMessage = async () => {
const userText = input.value.trim();
if (!userText) return;
appendMessage(userText, 'user');
input.value = '';
showTypingIndicator();
const recentHistory = chatHistory.slice(-6).map(msg => ({
role: msg.role,
content: msg.content
}));
const payload = {
model: 'meta-llama/Llama-3.1-8B-Instruct:novita',
messages: [
{ role: 'system', content: getSystemPrompt() },
...recentHistory,
{ role: 'user', content: userText }
]
};
try {
const resp = await fetch('/api/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
});
removeTypingIndicator();
if (!resp.ok) throw new Error('AI service request failed');
const data = await resp.json();
const reply = data.choices?.[0]?.message?.content || 'No response';
appendMessage(reply, 'bot');
chatHistory.push({ role: 'user', content: userText });
chatHistory.push({ role: 'assistant', content: reply });
} catch (e) {
console.error(e);
removeTypingIndicator();
appendMessage('Unable to connect to the AI service. Please try again later.', 'bot');
}
};
sendBtn.addEventListener('click', sendMessage);
input.addEventListener('keydown', (e) => {
if (e.key === 'Enter') sendMessage();
});
})();