xonecas.io
53129
Pa

PAT / commit 8247fc9

feat: add system and tool message display with visibility controls

- Add settings to control visibility of system and tool messages
- Create SystemMessage and ToolMessage components with centered, greyed styling
- Update messages API to accept optional_roles parameter for filtering
- Stream tool messages in real-time alongside assistant responses
- Persist tool messages to database when tools are executed
- Return system message in thread creation response for immediate display
- Refactor useLocalStorage to use event system for cross-component sync
- Add useMessageDisplaySettings hook for managing display preferences
- Update MessageList to render all message types based on settings
- Hide delete button for system and tool messages

xonecas2025-12-19 8247fc9aab43c64a179aef0240444198c5e29363 parent dc7a429

14 files changed

app/api/threads/[id]/messages/route.ts+13−2
@@ -1,7 +1,7 @@
11 import { NextResponse } from "next/server";
22 import { db } from "@/src/lib/db";
33 import { messages } from "@/src/lib/db/schema";
4-import { eq, asc, and, notInArray } from "drizzle-orm";
4+import { eq, asc, and, inArray } from "drizzle-orm";
55 import { debug } from "@/src/lib/debug";
66
77 export async function GET(request: Request, { params }: { params: Promise<{ id: string }> }) {
@@ -13,10 +13,21 @@ export async function GET(request: Request, { params }: { params: Promise<{ id:
1313 return NextResponse.json({ error: "Invalid thread ID" }, { status: 400 });
1414 }
1515
16+ const url = new URL(request.url);
17+ const optionalRolesParam = url.searchParams.get("optional_roles");
18+
19+ const allowedRoles: string[] = ["user", "assistant"];
20+
21+ if (optionalRolesParam) {
22+ const optionalRoles = optionalRolesParam.split(",").map((r) => r.trim()).filter(Boolean);
23+ const validOptionalRoles = optionalRoles.filter((r) => ["system", "tool"].includes(r));
24+ allowedRoles.push(...validOptionalRoles);
25+ }
26+
1627 const messagesList = await db
1728 .select()
1829 .from(messages)
19- .where(and(eq(messages.threadId, threadId), notInArray(messages.role, ["system", "tool"])))
30+ .where(and(eq(messages.threadId, threadId), inArray(messages.role, allowedRoles)))
2031 .orderBy(asc(messages.createdAt));
2132
2233 return NextResponse.json({ messages: messagesList });
app/api/threads/route.ts+13−7
@@ -40,14 +40,20 @@ export async function POST(request: Request) {
4040 });
4141
4242 // Insert system message with guidelines
43- await db.insert(messages).values({
44- threadId,
45- role: "system",
46- content: systemPrompt,
47- createdAt: new Date(),
48- });
43+ const systemMessage = await db
44+ .insert(messages)
45+ .values({
46+ threadId,
47+ role: "system",
48+ content: systemPrompt,
49+ createdAt: new Date(),
50+ })
51+ .returning({ id: messages.id, content: messages.content, createdAt: messages.createdAt });
4952
50- return NextResponse.json({ thread: newThread[0] });
53+ return NextResponse.json({
54+ thread: newThread[0],
55+ systemMessage: systemMessage[0],
56+ });
5157 } catch (err) {
5258 debug(`[Threads API] Error creating thread:`, err instanceof Error ? err.message : "Unknown error");
5359 return NextResponse.json({ error: "Failed to create thread" }, { status: 500 });
src/components/MessageList/SystemMessage.tsx+24−0
@@ -0,0 +1,24 @@
1+"use client";
2+
3+import type { Message } from "@/src/lib/db/schema";
4+
5+type SystemMessageProps = {
6+ message: Message;
7+};
8+
9+export default function SystemMessage({ message }: SystemMessageProps) {
10+ const formatTimestamp = (date: Date | string): string => {
11+ const d = typeof date === "string" ? new Date(date) : date;
12+ return d.toLocaleString();
13+ };
14+
15+ return (
16+ <div className="mx-auto w-1/2 min-w-64 rounded-lg bg-neutral-100 p-2 text-sm text-neutral-500 dark:bg-neutral-900 dark:text-neutral-500">
17+ <div>{message.content}</div>
18+ <div className="mt-4 text-center text-xs text-neutral-500 dark:text-neutral-500">
19+ System message • {formatTimestamp(message.createdAt)}
20+ </div>
21+ </div>
22+ );
23+}
24+
src/components/MessageList/ToolMessage.tsx+24−0
@@ -0,0 +1,24 @@
1+"use client";
2+
3+import type { Message } from "@/src/lib/db/schema";
4+
5+type ToolMessageProps = {
6+ message: Message;
7+};
8+
9+export default function ToolMessage({ message }: ToolMessageProps) {
10+ const formatTimestamp = (date: Date | string): string => {
11+ const d = typeof date === "string" ? new Date(date) : date;
12+ return d.toLocaleString();
13+ };
14+
15+ return (
16+ <div className="mx-auto w-1/2 min-w-64 rounded-lg bg-neutral-100 p-2 text-sm text-neutral-500 dark:bg-neutral-900 dark:text-neutral-500">
17+ <div>{message.content}</div>
18+ <div className="mt-4 text-center text-xs text-neutral-500 dark:text-neutral-500">
19+ Tool message • {formatTimestamp(message.createdAt)}
20+ </div>
21+ </div>
22+ );
23+}
24+
src/components/MessageList/index.tsx+15−7
@@ -4,6 +4,8 @@ import { useRef, useEffect } from "react";
44 import type { Message } from "@/src/lib/db/schema";
55 import AssistantMessage from "./AssistantMessage";
66 import UserMessage from "./UserMessage";
7+import SystemMessage from "./SystemMessage";
8+import ToolMessage from "./ToolMessage";
79 import DeleteMessageButton from "./DeleteMessageButton";
810
911 type MessageListProps = {
@@ -45,15 +47,21 @@ export default function MessageList({ messages, threadId, onDeleteMessage }: Mes
4547 <div className="p-2">
4648 {msg.role === "assistant" ? (
4749 <AssistantMessage message={msg} />
48- ) : (
50+ ) : msg.role === "user" ? (
4951 <UserMessage message={msg} />
52+ ) : msg.role === "system" ? (
53+ <SystemMessage message={msg} />
54+ ) : msg.role === "tool" ? (
55+ <ToolMessage message={msg} />
56+ ) : null}
57+ {msg.role !== "system" && msg.role !== "tool" && (
58+ <DeleteMessageButton
59+ messageId={msg.id}
60+ threadId={threadId}
61+ onDeleteMessage={onDeleteMessage}
62+ align={msg.role === "user" ? "right" : "left"}
63+ />
5064 )}
51- <DeleteMessageButton
52- messageId={msg.id}
53- threadId={threadId}
54- onDeleteMessage={onDeleteMessage}
55- align={msg.role === "user" ? "right" : "left"}
56- />
5765 </div>
5866 </div>
5967 ))}
src/components/SettingsModal.tsx+40−0
@@ -1,6 +1,7 @@
11 "use client";
22
33 import Modal from "@/src/components/Modal";
4+import { useMessageDisplaySettings } from "@/src/hooks/useMessageDisplaySettings";
45
56 type SettingsModalProps = {
67 isOpen: boolean;
@@ -15,6 +16,8 @@ export default function SettingsModal({
1516 themeMode,
1617 onThemeChange,
1718 }: SettingsModalProps) {
19+ const [messageDisplaySettings, setMessageDisplaySettings] = useMessageDisplaySettings();
20+
1821 return (
1922 <Modal isOpen={isOpen} onClose={onClose}>
2023 <h2 className="mb-4 text-xl font-semibold text-neutral-800 dark:text-neutral-200">
@@ -33,6 +36,43 @@ export default function SettingsModal({
3336 <option value="light">Light</option>
3437 </select>
3538 </div>
39+ <div className="flex flex-col gap-2">
40+ <label className="text-neutral-600 dark:text-neutral-400">Message Display</label>
41+ <div className="flex flex-col gap-2">
42+ <label className="flex items-center gap-2">
43+ <input
44+ type="checkbox"
45+ checked={messageDisplaySettings.showSystemMessages}
46+ onChange={(e) =>
47+ setMessageDisplaySettings((prev) => ({
48+ ...prev,
49+ showSystemMessages: e.target.checked,
50+ }))
51+ }
52+ className="h-4 w-4"
53+ />
54+ <span className="text-sm text-neutral-800 dark:text-neutral-200">
55+ Show system messages
56+ </span>
57+ </label>
58+ <label className="flex items-center gap-2">
59+ <input
60+ type="checkbox"
61+ checked={messageDisplaySettings.showToolMessages}
62+ onChange={(e) =>
63+ setMessageDisplaySettings((prev) => ({
64+ ...prev,
65+ showToolMessages: e.target.checked,
66+ }))
67+ }
68+ className="h-4 w-4"
69+ />
70+ <span className="text-sm text-neutral-800 dark:text-neutral-200">
71+ Show tool messages
72+ </span>
73+ </label>
74+ </div>
75+ </div>
3676 </div>
3777 </Modal>
3878 );
src/hooks/useLocalStorage.ts+49−26
@@ -1,7 +1,7 @@
1-import { useState } from "react";
1+import { useState, useEffect, useCallback } from "react";
22
33 /**
4- * Custom hook for managing localStorage with React state
4+ * Custom hook for managing localStorage
55 * @param key - The localStorage key
66 * @param initialValue - The initial value if no value exists in localStorage
77 * @returns A tuple of [value, setValue] similar to useState
@@ -10,44 +10,67 @@ export function useLocalStorage<T>(
1010 key: string,
1111 initialValue: T
1212 ): [T, (value: T | ((prev: T) => T)) => void] {
13- // State to store our value
14- // Pass initial state function to useState so logic is only executed once
15- const [storedValue, setStoredValue] = useState<T>(() => {
13+ const getValue = useCallback((): T => {
1614 if (typeof window === "undefined") {
1715 return initialValue;
1816 }
1917 try {
20- // Get from local storage by key
2118 const item = window.localStorage.getItem(key);
2219 if (!item) {
2320 return initialValue;
2421 }
25- // Parse stored json or if none return initialValue
2622 return JSON.parse(item) as T;
2723 } catch {
28- // If error, clear invalid value and return initialValue
2924 window.localStorage.removeItem(key);
3025 return initialValue;
3126 }
32- });
27+ }, [key, initialValue]);
3328
34- // Return a wrapped version of useState's setter function that
35- // persists the new value to localStorage.
36- const setValue = (value: T | ((prev: T) => T)) => {
37- try {
38- // Allow value to be a function so we have same API as useState
39- const valueToStore = value instanceof Function ? value(storedValue) : value;
40- // Save state
41- setStoredValue(valueToStore);
42- // Save to local storage
43- if (typeof window !== "undefined") {
44- window.localStorage.setItem(key, JSON.stringify(valueToStore));
29+ const [value, setValueState] = useState<T>(getValue);
30+
31+ const setValue = useCallback(
32+ (newValue: T | ((prev: T) => T)) => {
33+ try {
34+ const valueToStore = newValue instanceof Function ? newValue(getValue()) : newValue;
35+ if (typeof window !== "undefined") {
36+ window.localStorage.setItem(key, JSON.stringify(valueToStore));
37+ // Dispatch custom event to notify other hooks using the same key
38+ window.dispatchEvent(new CustomEvent(`localStorage-${key}`, { detail: valueToStore }));
39+ }
40+ setValueState(valueToStore);
41+ } catch (error) {
42+ console.error(`Error setting localStorage key "${key}":`, error);
4543 }
46- } catch (error) {
47- // A more advanced implementation would handle the error case
48- console.error(`Error setting localStorage key "${key}":`, error);
49- }
50- };
44+ },
45+ [key, getValue]
46+ );
47+
48+ // Listen for changes to this localStorage key from other components
49+ useEffect(() => {
50+ const handleStorageChange = (e: Event) => {
51+ const customEvent = e as CustomEvent<T>;
52+ setValueState(customEvent.detail);
53+ };
54+
55+ const handleStorageEvent = (e: StorageEvent) => {
56+ if (e.key === key && e.newValue !== null) {
57+ try {
58+ setValueState(JSON.parse(e.newValue) as T);
59+ } catch {
60+ // Ignore parse errors
61+ }
62+ }
63+ };
64+
65+ const eventName = `localStorage-${key}`;
66+ window.addEventListener(eventName, handleStorageChange);
67+ window.addEventListener("storage", handleStorageEvent);
68+
69+ return () => {
70+ window.removeEventListener(eventName, handleStorageChange);
71+ window.removeEventListener("storage", handleStorageEvent);
72+ };
73+ }, [key]);
5174
52- return [storedValue, setValue];
75+ return [value, setValue];
5376 }
src/hooks/useMessageDisplaySettings.ts+23−0
@@ -0,0 +1,23 @@
1+import { useLocalStorage } from "./useLocalStorage";
2+
3+type MessageDisplaySettings = {
4+ showSystemMessages: boolean;
5+ showToolMessages: boolean;
6+};
7+
8+const DEFAULT_SETTINGS: MessageDisplaySettings = {
9+ showSystemMessages: false,
10+ showToolMessages: false,
11+};
12+
13+/**
14+ * Custom hook for managing message display settings in localStorage
15+ * @returns A tuple of [settings, setSettings] similar to useState
16+ */
17+export function useMessageDisplaySettings(): [
18+ MessageDisplaySettings,
19+ (value: MessageDisplaySettings | ((prev: MessageDisplaySettings) => MessageDisplaySettings)) => void
20+] {
21+ return useLocalStorage<MessageDisplaySettings>("messageDisplaySettings", DEFAULT_SETTINGS);
22+}
23+
src/hooks/useMessages.ts+102−18
@@ -1,6 +1,7 @@
11 import { useState, useEffect, useCallback, useRef } from "react";
22 import type { Message } from "@/src/lib/db/schema";
33 import { getParseErrorMessage, handleError } from "@/src/lib/errors";
4+import { useMessageDisplaySettings } from "./useMessageDisplaySettings";
45
56 /**
67 * Custom hook for managing messages within a thread.
@@ -30,23 +31,40 @@ export function useMessages(
3031 const [streamingMessageId, setStreamingMessageId] = useState<number | null>(null);
3132 const sendingToThreadIdRef = useRef<number | null>(null);
3233 const abortControllerRef = useRef<AbortController | null>(null);
34+ const [messageDisplaySettings] = useMessageDisplaySettings();
3335
34- const loadMessages = useCallback(async (id: number, onError?: (error: string) => void) => {
35- setIsLoadingMessages(true);
36- try {
37- const res = await fetch(`/api/threads/${id}/messages`);
38- if (!res.ok) {
39- throw new Error(`Failed to load messages: ${res.status}`);
36+ const loadMessages = useCallback(
37+ async (id: number, onError?: (error: string) => void) => {
38+ setIsLoadingMessages(true);
39+ try {
40+ const optionalRoles: string[] = [];
41+ if (messageDisplaySettings.showSystemMessages) {
42+ optionalRoles.push("system");
43+ }
44+ if (messageDisplaySettings.showToolMessages) {
45+ optionalRoles.push("tool");
46+ }
47+
48+ let url = `/api/threads/${id}/messages`;
49+ if (optionalRoles.length > 0) {
50+ url += `?optional_roles=${optionalRoles.join(",")}`;
51+ }
52+
53+ const res = await fetch(url);
54+ if (!res.ok) {
55+ throw new Error(`Failed to load messages: ${res.status}`);
56+ }
57+ const data = await res.json();
58+ const loadedMessages = data.messages || [];
59+ setMessages(loadedMessages);
60+ } catch (error) {
61+ handleError(error, onError);
62+ } finally {
63+ setIsLoadingMessages(false);
4064 }
41- const data = await res.json();
42- const loadedMessages = data.messages || [];
43- setMessages(loadedMessages);
44- } catch (error) {
45- handleError(error, onError);
46- } finally {
47- setIsLoadingMessages(false);
48- }
49- }, []);
65+ },
66+ [messageDisplaySettings.showSystemMessages, messageDisplaySettings.showToolMessages]
67+ );
5068
5169 const createUserMessage = (message: string, threadId: number): Message => ({
5270 id: Date.now(),
@@ -81,6 +99,30 @@ export function useMessages(
8199 toolCallCounts: metadata?.toolCallCounts ?? null,
82100 });
83101
102+ const createToolMessage = (id: number, threadId: number, content: string, createdAt?: string | Date): Message => ({
103+ id,
104+ threadId,
105+ role: "tool",
106+ content,
107+ createdAt: createdAt ? (typeof createdAt === "string" ? new Date(createdAt) : createdAt) : new Date(),
108+ model: null,
109+ maxPromptLength: null,
110+ generationTimeMs: null,
111+ toolCallCounts: null,
112+ });
113+
114+ const createSystemMessage = (id: number, threadId: number, content: string, createdAt?: string | Date): Message => ({
115+ id,
116+ threadId,
117+ role: "system",
118+ content,
119+ createdAt: createdAt ? (typeof createdAt === "string" ? new Date(createdAt) : createdAt) : new Date(),
120+ model: null,
121+ maxPromptLength: null,
122+ generationTimeMs: null,
123+ toolCallCounts: null,
124+ });
125+
84126 const updateAssistantMessage = (
85127 messageId: number,
86128 content: string,
@@ -139,10 +181,24 @@ export function useMessages(
139181 }
140182 }, [threadId, loadMessages, onError]);
141183
184+ // Reload messages when display settings change (if we have a thread selected)
185+ // Use JSON.stringify to create a stable dependency key from the settings object
186+ const settingsKey = JSON.stringify(messageDisplaySettings);
187+ const prevSettingsKeyRef = useRef<string>(settingsKey);
188+
189+ useEffect(() => {
190+ if (settingsKey !== prevSettingsKeyRef.current) {
191+ prevSettingsKeyRef.current = settingsKey;
192+ if (threadId !== null && sendingToThreadIdRef.current !== threadId) {
193+ loadMessages(threadId, onError);
194+ }
195+ }
196+ }, [settingsKey, threadId, loadMessages, onError]);
197+
142198 const sendMessage = async (
143199 message: string,
144200 currentThreadId: number | null,
145- onCreateThread: (title?: string, firstMessage?: string) => Promise<number | null>,
201+ onCreateThread: (title?: string, firstMessage?: string) => Promise<{ threadId: number; systemMessage?: { id: number; content: string; createdAt: string } } | null>,
146202 onThreadSelect: (id: number) => void,
147203 onThreadsReload: () => void,
148204 onError?: (error: string) => void
@@ -155,14 +211,26 @@ export function useMessages(
155211 if (!targetThreadId) {
156212 // Create a new thread if none exists
157213 // The onCreateThread callback should already have model and maxPromptLength
158- targetThreadId = await onCreateThread(message.substring(0, 100), message);
159- if (!targetThreadId) {
214+ const threadResult = await onCreateThread(message.substring(0, 100), message);
215+ if (!threadResult) {
160216 setIsLoading(false);
161217 if (onError) {
162218 onError("Failed to create thread");
163219 }
164220 return;
165221 }
222+ targetThreadId = threadResult.threadId;
223+
224+ // Add system message to state if it was returned and setting is enabled
225+ if (threadResult.systemMessage && messageDisplaySettings.showSystemMessages) {
226+ addSystemMessage(
227+ threadResult.systemMessage.id,
228+ targetThreadId,
229+ threadResult.systemMessage.content,
230+ threadResult.systemMessage.createdAt
231+ );
232+ }
233+
166234 // Mark that we're sending to this thread before selecting it
167235 sendingToThreadIdRef.current = targetThreadId;
168236 onThreadSelect(targetThreadId);
@@ -229,6 +297,10 @@ export function useMessages(
229297 } else if (messageAdded) {
230298 updateAssistantMessage(assistantMsgId, accumulatedContent);
231299 }
300+ } else if (data.type === "toolMessage") {
301+ // Add tool message to state in real-time
302+ const toolMsg = createToolMessage(data.id, data.threadId, data.content, data.createdAt);
303+ setMessages((prev) => [...prev, toolMsg]);
232304 } else if (data.type === "done") {
233305 // Final update with complete answer and metadata
234306 const finalContent = data.answer || accumulatedContent;
@@ -344,6 +416,17 @@ export function useMessages(
344416 }
345417 };
346418
419+ const addSystemMessage = (id: number, threadId: number, content: string, createdAt?: string | Date) => {
420+ const systemMsg = createSystemMessage(id, threadId, content, createdAt);
421+ setMessages((prev) => {
422+ // Check if message already exists to avoid duplicates
423+ if (prev.some((msg) => msg.id === id)) {
424+ return prev;
425+ }
426+ return [...prev, systemMsg];
427+ });
428+ };
429+
347430 return {
348431 messages,
349432 isLoading,
@@ -353,5 +436,6 @@ export function useMessages(
353436 clearMessages,
354437 stopGeneration,
355438 deleteMessage,
439+ addSystemMessage,
356440 };
357441 }
src/hooks/useThreads.ts+11−2
@@ -64,7 +64,7 @@ export function useThreads() {
6464 firstMessage?: string,
6565 userPrompt?: string,
6666 onError?: (error: string) => void
67- ): Promise<number | null> => {
67+ ): Promise<{ threadId: number; systemMessage?: { id: number; content: string; createdAt: string } } | null> => {
6868 try {
6969 const title = titleOverride || (firstMessage ? firstMessage.substring(0, 100) : "New Thread");
7070
@@ -87,7 +87,16 @@ export function useThreads() {
8787 // Reload threads to get the full list
8888 await loadThreads(8);
8989
90- return newThread.id;
90+ return {
91+ threadId: newThread.id,
92+ systemMessage: data.systemMessage
93+ ? {
94+ id: data.systemMessage.id,
95+ content: data.systemMessage.content,
96+ createdAt: data.systemMessage.createdAt,
97+ }
98+ : undefined,
99+ };
91100 } catch (error) {
92101 handleError(error, onError);
93102 return null;
src/lib/ollama/chat/assistant-response.ts+17−2
@@ -5,7 +5,7 @@ import { OllamaChat } from "./ollama-chat";
55 import type { MaxPromptLength, OllamaChunk } from "@/src/lib/ollama/types";
66 import { createStreamController } from "./stream-controller";
77 import { setupAbortHandler, isAbortedOrClosed } from "./abort-handler";
8-import { saveAssistantMessage, extractToolCounts } from "./message-persistence";
8+import { saveAssistantMessage, saveToolMessage, extractToolCounts } from "./message-persistence";
99
1010 export interface StreamAssistantResponseParams {
1111 ollamaMessages: Message[];
@@ -70,7 +70,22 @@ export function streamAssistantResponse({
7070 onChunk,
7171 threadModel ?? "",
7272 signal,
73- threadMaxPromptLength
73+ threadMaxPromptLength,
74+ async (content: string, toolName: string) => {
75+ const savedMessage = await saveToolMessage({
76+ threadId,
77+ content,
78+ toolName,
79+ });
80+ // Stream tool message to frontend
81+ safeEnqueue({
82+ type: "toolMessage",
83+ id: savedMessage.id,
84+ content: savedMessage.content,
85+ threadId,
86+ createdAt: savedMessage.createdAt.toISOString(),
87+ });
88+ }
7489 );
7590 generationTimeMs = result.generationTimeMs;
7691 } catch (error) {
src/lib/ollama/chat/message-persistence.ts+38−0
@@ -35,6 +35,44 @@ export interface SaveMessageParams {
3535 toolCallCounts: string | null;
3636 }
3737
38+export interface SaveToolMessageParams {
39+ threadId: number;
40+ content: string;
41+ toolName: string;
42+}
43+
44+/**
45+ * Saves a tool message to the database and updates the thread's updatedAt timestamp.
46+ *
47+ * @param params - Object containing tool message data
48+ * @param params.threadId - Thread ID to save message to
49+ * @param params.content - Tool response content
50+ * @param params.toolName - Name of the tool that was called
51+ * @returns Promise that resolves with the saved message ID and content
52+ */
53+export async function saveToolMessage(params: SaveToolMessageParams): Promise<{ id: number; content: string; createdAt: Date }> {
54+ const { threadId, content, toolName } = params;
55+
56+ const savedMessage = await db
57+ .insert(messages)
58+ .values({
59+ threadId,
60+ role: "tool",
61+ content: `[${toolName}]\n${content}`,
62+ createdAt: new Date(),
63+ })
64+ .returning({ id: messages.id, content: messages.content, createdAt: messages.createdAt });
65+
66+ // Update thread's updatedAt timestamp
67+ await db.update(threads).set({ updatedAt: new Date() }).where(eq(threads.id, threadId));
68+
69+ return {
70+ id: savedMessage[0].id,
71+ content: savedMessage[0].content,
72+ createdAt: savedMessage[0].createdAt,
73+ };
74+}
75+
3876 /**
3977 * Saves an assistant message to the database and updates the thread's updatedAt timestamp.
4078 * Also generates and updates the thread title from the message history.
src/lib/ollama/chat/ollama-chat.ts+14−3
@@ -22,6 +22,7 @@ import type { OllamaChunk, OllamaChatResponse, MaxPromptLength } from "@/src/lib
2222 * @param model - The Ollama model to use.
2323 * @param signal - Optional AbortSignal to cancel the request.
2424 * @param maxPromptLength - Optional maximum prompt length in tokens. Can be "none", 1024, or 4096.
25+ * @param onToolMessage - Optional callback function to save tool messages to the database.
2526 * @returns Object containing the response content and generation time in milliseconds.
2627 * @throws If the request fails.
2728 */
@@ -30,7 +31,8 @@ export async function OllamaChat(
3031 onChunk?: (chunk: OllamaChunk) => void,
3132 model = "",
3233 signal?: AbortSignal,
33- maxPromptLength?: MaxPromptLength
34+ maxPromptLength?: MaxPromptLength,
35+ onToolMessage?: (content: string, toolName: string) => Promise<void>
3436 ): Promise<OllamaChatResponse> {
3537 const tools = [duckDuckGoTool, weatherTool, webSearchTool, fetchPageTool];
3638 let totalDuration = 0;
@@ -181,15 +183,24 @@ export async function OllamaChat(
181183 try {
182184 const toolResponse = await executeToolCall(toolCall);
183185 currentMessages.push(toolResponse);
186+ // Save tool message to database if callback provided
187+ if (onToolMessage && toolResponse.role === "tool" && toolResponse.tool_name) {
188+ await onToolMessage(toolResponse.content, toolResponse.tool_name);
189+ }
184190 } catch (error) {
185191 const errorMessage = getErrorMessage(error);
186192 debug(`[Ollama] Tool execution failed: ${errorMessage}`);
187193 // Add error message as tool response so the model can handle it
188- currentMessages.push({
194+ const errorToolMessage: Message = {
189195 role: "tool",
190196 tool_name: toolCall.function.name,
191197 content: `Error: ${errorMessage}`,
192- });
198+ };
199+ currentMessages.push(errorToolMessage);
200+ // Save error tool message to database if callback provided
201+ if (onToolMessage) {
202+ await onToolMessage(errorToolMessage.content, toolCall.function.name);
203+ }
193204 }
194205 }
195206 }
src/tests/lib/ollama/chat/assistant-response.test.ts+3−1
@@ -24,6 +24,7 @@ vi.mock("@/src/lib/ollama/chat/abort-handler", () => ({
2424
2525 vi.mock("@/src/lib/ollama/chat/message-persistence", () => ({
2626 saveAssistantMessage: vi.fn().mockResolvedValue(undefined),
27+ saveToolMessage: vi.fn().mockResolvedValue({ id: 1, content: "[tool]\nresult", createdAt: new Date() }),
2728 extractToolCounts: vi.fn(),
2829 }));
2930
@@ -96,7 +97,8 @@ describe("streamAssistantResponse", () => {
9697 expect.any(Function),
9798 "test-model",
9899 signal,
99- 1024
100+ 1024,
101+ expect.any(Function)
100102 );
101103 expect(mockSaveAssistantMessage).toHaveBeenCalledWith({
102104 threadId: 1,