@@ -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