xonecas.io
5414
Sa

sat / commit 885f3c7

feat: enhance chat functionality with conversation history and summarization

xonecas2026-01-14 885f3c7cfb01417197e826891b88aa4ea8ae2ffd parent 44ff317

9 files changed

drizzle/0001_tiresome_zaran.sql+1−0
@@ -0,0 +1 @@
1+ALTER TABLE "conversations" ADD COLUMN "summary" text;
drizzle/meta/0001_snapshot.json+136−0
@@ -0,0 +1,136 @@
1+{
2+ "id": "63d7c5d4-bd09-46bb-b9fd-13cd5a94f08f",
3+ "prevId": "c5145db9-57d5-4900-a75f-768038e4d932",
4+ "version": "7",
5+ "dialect": "postgresql",
6+ "tables": {
7+ "public.conversations": {
8+ "name": "conversations",
9+ "schema": "",
10+ "columns": {
11+ "id": {
12+ "name": "id",
13+ "type": "uuid",
14+ "primaryKey": true,
15+ "notNull": true,
16+ "default": "gen_random_uuid()"
17+ },
18+ "title": {
19+ "name": "title",
20+ "type": "text",
21+ "primaryKey": false,
22+ "notNull": false
23+ },
24+ "summary": {
25+ "name": "summary",
26+ "type": "text",
27+ "primaryKey": false,
28+ "notNull": false
29+ },
30+ "created_at": {
31+ "name": "created_at",
32+ "type": "timestamp",
33+ "primaryKey": false,
34+ "notNull": true,
35+ "default": "now()"
36+ },
37+ "updated_at": {
38+ "name": "updated_at",
39+ "type": "timestamp",
40+ "primaryKey": false,
41+ "notNull": true,
42+ "default": "now()"
43+ }
44+ },
45+ "indexes": {},
46+ "foreignKeys": {},
47+ "compositePrimaryKeys": {},
48+ "uniqueConstraints": {},
49+ "policies": {},
50+ "checkConstraints": {},
51+ "isRLSEnabled": false
52+ },
53+ "public.turns": {
54+ "name": "turns",
55+ "schema": "",
56+ "columns": {
57+ "id": {
58+ "name": "id",
59+ "type": "uuid",
60+ "primaryKey": true,
61+ "notNull": true,
62+ "default": "gen_random_uuid()"
63+ },
64+ "conversation_id": {
65+ "name": "conversation_id",
66+ "type": "uuid",
67+ "primaryKey": false,
68+ "notNull": true
69+ },
70+ "role": {
71+ "name": "role",
72+ "type": "text",
73+ "primaryKey": false,
74+ "notNull": true
75+ },
76+ "content": {
77+ "name": "content",
78+ "type": "text",
79+ "primaryKey": false,
80+ "notNull": true
81+ },
82+ "reasoning": {
83+ "name": "reasoning",
84+ "type": "text",
85+ "primaryKey": false,
86+ "notNull": false
87+ },
88+ "tool_calls": {
89+ "name": "tool_calls",
90+ "type": "jsonb",
91+ "primaryKey": false,
92+ "notNull": false
93+ },
94+ "created_at": {
95+ "name": "created_at",
96+ "type": "timestamp",
97+ "primaryKey": false,
98+ "notNull": true,
99+ "default": "now()"
100+ }
101+ },
102+ "indexes": {},
103+ "foreignKeys": {
104+ "turns_conversation_id_conversations_id_fk": {
105+ "name": "turns_conversation_id_conversations_id_fk",
106+ "tableFrom": "turns",
107+ "tableTo": "conversations",
108+ "columnsFrom": [
109+ "conversation_id"
110+ ],
111+ "columnsTo": [
112+ "id"
113+ ],
114+ "onDelete": "cascade",
115+ "onUpdate": "no action"
116+ }
117+ },
118+ "compositePrimaryKeys": {},
119+ "uniqueConstraints": {},
120+ "policies": {},
121+ "checkConstraints": {},
122+ "isRLSEnabled": false
123+ }
124+ },
125+ "enums": {},
126+ "schemas": {},
127+ "sequences": {},
128+ "roles": {},
129+ "policies": {},
130+ "views": {},
131+ "_meta": {
132+ "columns": {},
133+ "schemas": {},
134+ "tables": {}
135+ }
136+}
drizzle/meta/_journal.json+7−0
@@ -8,6 +8,13 @@
88 "when": 1768430745778,
99 "tag": "0000_handy_kang",
1010 "breakpoints": true
11+ },
12+ {
13+ "idx": 1,
14+ "version": "7",
15+ "when": 1768432178025,
16+ "tag": "0001_tiresome_zaran",
17+ "breakpoints": true
1118 }
1219 ]
1320 }
src/app/actions/conversations.ts+10−3
@@ -1,9 +1,9 @@
11 "use server";
22
3-import { db } from "@/db";
4-import { type NewTurn, conversations, turns } from "@/db/schema";
5-import { eq, desc } from "drizzle-orm";
3+import { desc, eq } from "drizzle-orm";
64 import { revalidatePath } from "next/cache";
5+import { db } from "@/db";
6+import { conversations, type NewTurn, turns } from "@/db/schema";
77
88 export async function getConversations() {
99 return db.query.conversations.findMany({
@@ -88,6 +88,13 @@ export async function deleteConversation(id: string) {
8888 revalidatePath("/");
8989 }
9090
91+export async function updateConversationSummary(id: string, summary: string) {
92+ await db
93+ .update(conversations)
94+ .set({ summary, updatedAt: new Date() })
95+ .where(eq(conversations.id, id));
96+}
97+
9198 // Type for the full conversation with turns
9299 export type ConversationWithTurns = NonNullable<
93100 Awaited<ReturnType<typeof getConversation>>
src/app/api/stream/route.ts+33−6
@@ -1,16 +1,43 @@
11 import { NextResponse } from "next/server";
2-import { chat, type StreamEvent } from "@/lib/chat";
2+import { type ChatHistory, chat, type StreamEvent } from "@/lib/chat";
3+import { summarizeIfNeeded } from "@/lib/summarize";
34
45 export async function POST(request: Request) {
5- const { userInput } = await request.json();
6+ const { userInput, conversationId } = await request.json();
67
78 const encoder = new TextEncoder();
89 const stream = new ReadableStream({
910 async start(controller) {
10- await chat(userInput, (event: StreamEvent) => {
11- // Use newline-delimited JSON for easier parsing
12- controller.enqueue(encoder.encode(`${JSON.stringify(event)}\n`));
13- });
11+ let history: ChatHistory | undefined;
12+
13+ if (conversationId) {
14+ try {
15+ const result = await summarizeIfNeeded(conversationId);
16+ history = result.history;
17+
18+ // Notify client if summarization occurred
19+ if (result.summarizedCount > 0) {
20+ controller.enqueue(
21+ encoder.encode(
22+ `${JSON.stringify({ type: "summarized", messageCount: result.summarizedCount })}\n`,
23+ ),
24+ );
25+ }
26+ } catch (error) {
27+ console.error("Failed to fetch conversation history:", error);
28+ // Continue without history if fetch fails
29+ }
30+ }
31+
32+ await chat(
33+ userInput,
34+ (event: StreamEvent) => {
35+ // Use newline-delimited JSON for easier parsing
36+ controller.enqueue(encoder.encode(`${JSON.stringify(event)}\n`));
37+ },
38+ history,
39+ conversationId,
40+ );
1441
1542 controller.close();
1643 },
src/components/chat.tsx+33−2
@@ -3,6 +3,7 @@
33 import {
44 Bot,
55 ChevronRight,
6+ Info,
67 Loader2,
78 Search,
89 SendHorizontal,
@@ -40,7 +41,7 @@ interface ToolCall {
4041
4142 interface Message {
4243 id: string;
43- role: "user" | "assistant";
44+ role: "user" | "assistant" | "system";
4445 content: string;
4546 reasoning?: string;
4647 toolCalls?: ToolCall[];
@@ -187,7 +188,10 @@ export function Chat({ conversationId }: ChatProps) {
187188 const response = await fetch("/api/stream", {
188189 method: "POST",
189190 headers: { "Content-Type": "application/json" },
190- body: JSON.stringify({ userInput: trimmedInput }),
191+ body: JSON.stringify({
192+ userInput: trimmedInput,
193+ conversationId: activeConversationId,
194+ }),
191195 });
192196
193197 if (!response.ok) {
@@ -244,6 +248,18 @@ export function Chat({ conversationId }: ChatProps) {
244248 }
245249 break;
246250 }
251+ case "summarized": {
252+ // Insert a system notification message
253+ setMessages((prev) => [
254+ ...prev,
255+ {
256+ id: crypto.randomUUID(),
257+ role: "system",
258+ content: `Older messages have been summarized to maintain context (${event.messageCount} messages compressed).`,
259+ },
260+ ]);
261+ break;
262+ }
247263 }
248264 } catch {
249265 // Ignore parse errors for partial chunks
@@ -421,6 +437,21 @@ export function Chat({ conversationId }: ChatProps) {
421437
422438 function MessageBubble({ message }: { message: Message }) {
423439 const isUser = message.role === "user";
440+ const isSystem = message.role === "system";
441+
442+ // System messages are rendered as centered info cards
443+ if (isSystem) {
444+ return (
445+ <div className="flex justify-center">
446+ <Card size="sm" className="bg-muted/50 border-dashed">
447+ <CardContent className="text-muted-foreground flex items-center gap-2 text-sm">
448+ <Info className="size-4" />
449+ {message.content}
450+ </CardContent>
451+ </Card>
452+ </div>
453+ );
454+ }
424455
425456 return (
426457 <div className={cn("flex gap-3", isUser && "flex-row-reverse")}>
src/db/schema.ts+1−0
@@ -5,6 +5,7 @@ import { jsonb, pgTable, text, timestamp, uuid } from "drizzle-orm/pg-core";
55 export const conversations = pgTable("conversations", {
66 id: uuid("id").primaryKey().defaultRandom(),
77 title: text("title"),
8+ summary: text("summary"), // Compressed history of older messages
89 createdAt: timestamp("created_at").defaultNow().notNull(),
910 updatedAt: timestamp("updated_at").defaultNow().notNull(),
1011 });
src/lib/chat.ts+146−24
@@ -1,10 +1,14 @@
11 import {
22 AIMessage,
33 AIMessageChunk,
4+ type BaseMessage,
5+ HumanMessage,
6+ SystemMessage as LangChainSystemMessage,
47 ToolMessage,
58 } from "@langchain/core/messages";
69 import { tool } from "@langchain/core/tools";
710 import { concat } from "@langchain/core/utils/stream";
11+import { MemorySaver } from "@langchain/langgraph";
812 import { ChatOllama } from "@langchain/ollama";
913 import { createAgent, SystemMessage } from "langchain";
1014 import { z } from "zod";
@@ -43,38 +47,156 @@ export type StreamEvent =
4347 name: string;
4448 args: Record<string, unknown>;
4549 }
46- | { type: "tool_result"; id: string; result: string };
50+ | { type: "tool_result"; id: string; result: string }
51+ | { type: "summarized"; messageCount: number };
52+
53+// Types for chat history
54+export interface ToolCallData {
55+ id: string;
56+ name: string;
57+ args: Record<string, unknown>;
58+ result?: string;
59+}
60+
61+export interface ChatHistoryTurn {
62+ role: "user" | "assistant";
63+ content: string;
64+ toolCalls?: ToolCallData[];
65+}
66+
67+export interface ChatHistory {
68+ summary?: string;
69+ turns: ChatHistoryTurn[];
70+}
71+
72+// Singleton checkpointer - persists in memory across requests
73+// Note: Lost on server restart, but we re-populate from PostgreSQL
74+const checkpointer = new MemorySaver();
75+
76+// Singleton agent instance
77+let agentInstance: ReturnType<typeof createAgent> | null = null;
78+
79+function getAgent() {
80+ if (!agentInstance) {
81+ const llm = new ChatOllama({
82+ model: "qwen3:8b",
83+ });
84+
85+ agentInstance = createAgent({
86+ model: llm,
87+ tools: [searchWeb],
88+ checkpointer,
89+ systemPrompt: new SystemMessage(
90+ "You are a helpful web search assistant. Always respond in English, regardless of the language used in search results or user queries." +
91+ " You have access to a tool that searches the web." +
92+ " Create optimized search queries from the user's input, use them with the search_web tool to get the best results." +
93+ " Review the results and provide a concise summary of the information found in English. Include sources and links if available.",
94+ ),
95+ });
96+ }
97+ return agentInstance;
98+}
99+
100+/**
101+ * Convert history turns to LangChain BaseMessage array
102+ */
103+function historyToMessages(history: ChatHistory): BaseMessage[] {
104+ const messages: BaseMessage[] = [];
105+
106+ // Prepend summary as system context if exists
107+ if (history.summary) {
108+ messages.push(
109+ new LangChainSystemMessage(
110+ `Previous conversation summary:\n${history.summary}`,
111+ ),
112+ );
113+ }
114+
115+ for (const turn of history.turns) {
116+ if (turn.role === "user") {
117+ messages.push(new HumanMessage(turn.content));
118+ } else {
119+ if (turn.toolCalls?.length) {
120+ messages.push(
121+ new AIMessage({
122+ content: turn.content,
123+ tool_calls: turn.toolCalls.map((tc) => ({
124+ id: tc.id,
125+ name: tc.name,
126+ args: tc.args,
127+ })),
128+ }),
129+ );
130+ // Add corresponding ToolMessages
131+ for (const tc of turn.toolCalls) {
132+ if (tc.result) {
133+ messages.push(
134+ new ToolMessage({
135+ tool_call_id: tc.id,
136+ content: tc.result,
137+ }),
138+ );
139+ }
140+ }
141+ } else {
142+ messages.push(new AIMessage(turn.content));
143+ }
144+ }
145+ }
146+
147+ return messages;
148+}
47149
48150 export async function chat(
49151 userInput: string,
50152 onEvent: (event: StreamEvent) => void,
153+ history?: ChatHistory,
154+ conversationId?: string,
51155 ) {
52- const llm = new ChatOllama({
53- model: "qwen3:8b",
54- });
55-
56- const agent = createAgent({
57- model: llm,
58- tools: [searchWeb],
59- systemPrompt: new SystemMessage(
60- "You are a helpful web search assistant. Always respond in English, regardless of the language used in search results or user queries." +
61- " You have access to a tool that searches the web." +
62- " Create optimized search queries from the user's input, use them with the search_web tool to get the best results." +
63- " Review the results and provide a concise summary of the information found in English. Include sources and links if available.",
64- ),
65- });
66-
67- // Stream both modes: "messages" for tokens, "updates" for tool calls/results
156+ const agent = getAgent();
157+ const threadId = conversationId ?? "default";
158+ const config = { configurable: { thread_id: threadId } };
159+
160+ // Check if this thread has existing state in the checkpointer
161+ // If not, we need to populate it from PostgreSQL history
162+ if (history && history.turns.length > 0) {
163+ try {
164+ const existingState = (await agent.getState(config)) as {
165+ values?: { messages?: unknown[] };
166+ } | null;
167+ const stateValues = existingState?.values;
168+
169+ // If no messages in checkpointer state, populate from DB history
170+ const hasMessages =
171+ stateValues?.messages &&
172+ Array.isArray(stateValues.messages) &&
173+ stateValues.messages.length > 0;
174+
175+ if (!hasMessages) {
176+ // Convert history to messages (excluding the current user message if it's already there)
177+ const lastTurn = history.turns.at(-1);
178+ const turnsToLoad =
179+ lastTurn?.role === "user" && lastTurn.content === userInput
180+ ? { ...history, turns: history.turns.slice(0, -1) }
181+ : history;
182+
183+ if (turnsToLoad.turns.length > 0) {
184+ const historyMessages = historyToMessages(turnsToLoad);
185+ // Use updateState to populate the checkpointer
186+ await agent.updateState(config, { messages: historyMessages });
187+ }
188+ }
189+ } catch (error) {
190+ console.error("[chat] Error checking/populating state:", error);
191+ }
192+ }
193+
194+ // Stream with just the new user message
68195 const stream = await agent.stream(
69196 {
70- messages: [
71- {
72- role: "user",
73- content: userInput,
74- },
75- ],
197+ messages: [{ role: "user", content: userInput }],
76198 },
77- { streamMode: ["messages", "updates"] },
199+ { ...config, streamMode: ["messages", "updates"] },
78200 );
79201
80202 // Track emitted tool calls to avoid duplicates
src/lib/summarize.ts+103−0
@@ -0,0 +1,103 @@
1+import { ChatOllama } from "@langchain/ollama";
2+import {
3+ getConversation,
4+ updateConversationSummary,
5+} from "@/app/actions/conversations";
6+import type { Turn } from "@/db/schema";
7+import type { ChatHistory, ChatHistoryTurn, ToolCallData } from "@/lib/chat";
8+
9+const TOKEN_THRESHOLD = 24000; // ~75% of qwen3:8b's 32k context
10+const MESSAGES_TO_KEEP = 10;
11+
12+export interface SummarizeResult {
13+ history: ChatHistory;
14+ summarizedCount: number; // Number of messages that were summarized (0 if none)
15+}
16+
17+/**
18+ * Fetches conversation history and summarizes older messages if needed.
19+ * Returns the history ready to be passed to the chat function.
20+ */
21+export async function summarizeIfNeeded(
22+ conversationId: string,
23+): Promise<SummarizeResult> {
24+ const conversation = await getConversation(conversationId);
25+ if (!conversation) {
26+ throw new Error("Conversation not found");
27+ }
28+
29+ const turns = conversation.turns;
30+ const estimatedTokens = estimateTokens(turns);
31+
32+ if (estimatedTokens > TOKEN_THRESHOLD && turns.length > MESSAGES_TO_KEEP) {
33+ const toSummarize = turns.slice(0, -MESSAGES_TO_KEEP);
34+ const toKeep = turns.slice(-MESSAGES_TO_KEEP);
35+
36+ const summaryText = await generateSummary(
37+ conversation.summary,
38+ toSummarize,
39+ );
40+
41+ await updateConversationSummary(conversationId, summaryText);
42+
43+ return {
44+ history: {
45+ summary: summaryText,
46+ turns: toKeep.map(turnToHistoryTurn),
47+ },
48+ summarizedCount: toSummarize.length,
49+ };
50+ }
51+
52+ return {
53+ history: {
54+ summary: conversation.summary ?? undefined,
55+ turns: turns.map(turnToHistoryTurn),
56+ },
57+ summarizedCount: 0,
58+ };
59+}
60+
61+/**
62+ * Rough token estimation: ~4 characters per token
63+ */
64+function estimateTokens(turns: Turn[]): number {
65+ return turns.reduce((sum, t) => sum + t.content.length / 4, 0);
66+}
67+
68+/**
69+ * Convert a DB turn to a ChatHistoryTurn for the chat function
70+ */
71+function turnToHistoryTurn(turn: Turn): ChatHistoryTurn {
72+ return {
73+ role: turn.role,
74+ content: turn.content,
75+ toolCalls: turn.toolCalls as ToolCallData[] | undefined,
76+ };
77+}
78+
79+/**
80+ * Generate a summary of older messages using the LLM
81+ */
82+async function generateSummary(
83+ existingSummary: string | null,
84+ turns: Turn[],
85+): Promise<string> {
86+ const llm = new ChatOllama({ model: "qwen3:8b" });
87+
88+ const conversationText = turns
89+ .map((t) => `${t.role}: ${t.content}`)
90+ .join("\n");
91+
92+ const prompt = `Summarize this conversation concisely, preserving key facts, decisions, and context. Focus on information that would be useful for continuing the conversation.
93+${existingSummary ? `\nPrevious summary to incorporate:\n${existingSummary}\n` : ""}
94+Conversation to summarize:
95+${conversationText}
96+
97+Summary:`;
98+
99+ const response = await llm.invoke(prompt);
100+ return typeof response.content === "string"
101+ ? response.content
102+ : JSON.stringify(response.content);
103+}