-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai.js
More file actions
230 lines (197 loc) · 7.19 KB
/
ai.js
File metadata and controls
230 lines (197 loc) · 7.19 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
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
// ai.js — MindMend AI with history + delete chat option
(function () {
let state = {
messages: [],
chats: [] // list of past chats
};
const isLocal =
location.hostname === "localhost" || location.hostname === "127.0.0.1";
const CHAT_URL = isLocal
? "http://localhost:54321/functions/v1/chat"
: "https://pgpxmtkzgifspotnjrpi.functions.supabase.co/chat";
// -------------------
// UI Helpers
// -------------------
function appendLog(html) {
const log = document.getElementById("aiLog");
const div = document.createElement("div");
div.innerHTML = html;
log.appendChild(div);
log.scrollTop = log.scrollHeight;
}
function clearLog() {
const log = document.getElementById("aiLog");
if (log) log.innerHTML = "";
}
function setSending(sending) {
const btn = document.getElementById("aiSend");
const input = document.getElementById("aiInput");
if (!btn || !input) return;
btn.disabled = !!sending;
input.disabled = !!sending;
}
function localFallback(userMessage) {
const genericResponses = [
"It sounds like you’re going through a lot. Try writing down what feels most important right now.",
"Remember you’re not alone—many students feel overwhelmed. Maybe take a short walk or breathe deeply.",
"Reaching out to a trusted friend, mentor, or counselor can help. You’re already taking a good step by reflecting here.",
];
const pick = genericResponses[Math.floor(Math.random() * genericResponses.length)];
return `Here’s a thought based on your message (“${userMessage}”): ${pick}`;
}
// -------------------
// Supabase call
// -------------------
async function callChat(message, allowJournal) {
const payload = {
messages: [...state.messages, { role: "user", content: message }],
use_journals: !!allowJournal
};
const { data: { session } } = await window.supabase.auth.getSession();
const res = await fetch(CHAT_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${session?.access_token ?? ""}`,
},
body: JSON.stringify(payload),
});
const json = await res.json().catch(() => ({}));
if (!res.ok) {
const msg = json?.message || `Request failed (${res.status})`;
throw new Error(msg);
}
return json?.content || "";
}
// -------------------
// Send + Log
// -------------------
async function onSend() {
const input = document.getElementById("aiInput");
const consent = document.getElementById("aiConsent");
const text = (input?.value || "").trim();
if (!text) return;
appendLog(`<p><strong>You:</strong> ${text}</p>`);
const typingId = `typing-${Date.now()}`;
appendLog(`<p id="${typingId}" class="muted">MindMend AI is thinking…</p>`);
setSending(true);
try {
const reply = await callChat(text, consent?.checked !== false);
state.messages.push({ role: "user", content: text });
state.messages.push({ role: "assistant", content: reply });
document.getElementById(typingId)?.remove();
appendLog(`<p><strong>MindMend AI:</strong> ${reply}</p>`);
input.value = "";
saveState();
} catch (err) {
document.getElementById(typingId)?.remove();
const fallback = localFallback(text);
appendLog(`<p class="muted">[AI unavailable: ${err.message}]</p>`);
appendLog(`<p><strong>MindMend AI (fallback):</strong> ${fallback}</p>`);
state.messages.push({ role: "user", content: text });
state.messages.push({ role: "assistant", content: fallback });
saveState();
} finally {
setSending(false);
}
}
// -------------------
// New Chat + History
// -------------------
function onNewChat() {
if (state.messages.length > 0) {
// Save current chat
state.chats.push([...state.messages]);
}
state.messages = [];
clearLog();
appendLog(`<p class="muted">Started a new chat. Previous conversations are in history.</p>`);
saveState();
renderChatHistory();
}
function renderChatHistory() {
const sidebar = document.getElementById("chatHistory");
if (!sidebar) return;
// Remove only summaries, not the New Chat button
const summaries = sidebar.querySelectorAll(".chat-summary");
summaries.forEach(s => s.remove());
state.chats.forEach((chat, idx) => {
const div = document.createElement("div");
div.className = "chat-summary";
const preview = chat.find(m => m.role === "user")?.content.slice(0, 30) || "(empty chat)";
// clickable chat title
const span = document.createElement("span");
span.textContent = `Chat ${idx + 1}: ${preview}...`;
span.style.cursor = "pointer";
span.addEventListener("click", () => loadChat(idx));
// delete button
const del = document.createElement("button");
del.textContent = "🗑️";
del.style.marginLeft = "8px";
del.style.cursor = "pointer";
del.style.border = "none";
del.style.background = "transparent";
del.addEventListener("click", (e) => {
e.stopPropagation(); // don’t trigger load
deleteChat(idx);
});
div.appendChild(span);
div.appendChild(del);
sidebar.appendChild(div);
});
}
function loadChat(index) {
clearLog();
state.messages = [...state.chats[index]];
state.messages.forEach(msg => {
if (msg.role === "user") appendLog(`<p><strong>You:</strong> ${msg.content}</p>`);
else appendLog(`<p><strong>MindMend AI:</strong> ${msg.content}</p>`);
});
}
function deleteChat(index) {
state.chats.splice(index, 1);
saveState();
renderChatHistory();
}
// -------------------
// Persistence (localStorage)
// -------------------
function saveState() {
localStorage.setItem("mindmend_ai_state", JSON.stringify(state));
}
function loadState() {
const raw = localStorage.getItem("mindmend_ai_state");
if (raw) {
try {
state = JSON.parse(raw);
} catch {
state = { messages: [], chats: [] };
}
}
}
// -------------------
// Init
// -------------------
function boot() {
const btn = document.getElementById("aiSend");
const input = document.getElementById("aiInput");
const newBtn = document.getElementById("newChat");
btn?.addEventListener("click", onSend);
input?.addEventListener("keydown", (e) => {
if (e.key === "Enter") onSend();
});
newBtn?.addEventListener("click", onNewChat);
renderChatHistory();
// Restore last open chat
if (state.messages.length > 0) {
clearLog();
state.messages.forEach(msg => {
if (msg.role === "user") appendLog(`<p><strong>You:</strong> ${msg.content}</p>`);
else appendLog(`<p><strong>MindMend AI:</strong> ${msg.content}</p>`);
});
}
}
loadState();
document.addEventListener("DOMContentLoaded", boot);
document.addEventListener("core:ready", boot); // keep if core.js dispatches it
})();