fix: include current user message in AI context

Persist the user message before streaming so the model receives the
current question (previously only the system prompt was sent on new
chats, causing canned greetings). Also fixes user message being lost
when the AI call errors.
This commit is contained in:
2026-08-19 09:51:41 -04:00
parent ff9da4d324
commit 00a181134d
+4 -1
View File
@@ -23,6 +23,10 @@ router.post("/", async (req, res) => {
} }
const currentChatId = session.id; const currentChatId = session.id;
// Persist the user message BEFORE streaming so it is included in the AI
// context (the model must see the current question) and survives even if
// the AI call fails.
await addMessage(currentChatId, "user", text.trim());
const chatMessages = await getMessages(currentChatId); const chatMessages = await getMessages(currentChatId);
res.setHeader("Content-Type", "text/event-stream"); res.setHeader("Content-Type", "text/event-stream");
res.setHeader("Cache-Control", "no-cache"); res.setHeader("Cache-Control", "no-cache");
@@ -50,7 +54,6 @@ router.post("/", async (req, res) => {
); );
res.write(`data: ${JSON.stringify({ type: "done" })}\n\n`); res.write(`data: ${JSON.stringify({ type: "done" })}\n\n`);
await addMessage(currentChatId, "user", text.trim());
await addMessage(currentChatId, "assistant", assistantText); await addMessage(currentChatId, "assistant", assistantText);
} catch (err) { } catch (err) {
console.error("Chat error:", err.message); console.error("Chat error:", err.message);