xonecas.io
53129
Pa

PAT / commit 70587bb

feat: title generation

xonecas2025-12-19 70587bbdbd035008f625c04d2acc186c7f73d4c5 parent 7eee0ac

12 files changed

TODO.md+2−3
@@ -2,11 +2,10 @@
22
33 ## BUGS:
44
5-- The message input and scrolling functions still feel jank
5+- so far so good?
66
77 ## New features:
88
9+- Sidebar improvements, more information about the thread quickly
910 - Text input, we should be able to press a button and record a prompt.
1011 - Text to voice, we should have a button to have the models replies be read with text to voice.
11-- Select the ollama model to use for the thread
12-- Add a context prompt, something to be sent seting the tone of the conversation
app/api/threads/[id]/route.ts+11−2
@@ -12,9 +12,14 @@ export async function PATCH(request: Request, { params }: { params: Promise<{ id
1212 }
1313
1414 const body = await request.json();
15- const { model, maxPromptLength } = body;
15+ const { model, maxPromptLength, title } = body;
1616
17- const updateData: { model?: string; maxPromptLength?: number | null; updatedAt?: Date } = {
17+ const updateData: {
18+ model?: string;
19+ maxPromptLength?: number | null;
20+ title?: string;
21+ updatedAt?: Date;
22+ } = {
1823 updatedAt: new Date(),
1924 };
2025
@@ -27,6 +32,10 @@ export async function PATCH(request: Request, { params }: { params: Promise<{ id
2732 maxPromptLength === "none" || maxPromptLength === null ? null : maxPromptLength;
2833 }
2934
35+ if (title !== undefined) {
36+ updateData.title = title;
37+ }
38+
3039 const updatedThread = await db
3140 .update(threads)
3241 .set(updateData)
app/page.tsx+4−1
@@ -37,7 +37,10 @@ export default function Home() {
3737 clearMessages,
3838 stopGeneration,
3939 deleteMessage: deleteMessageInternal,
40- } = useMessages(currentThreadId, setError);
40+ } = useMessages(currentThreadId, setError, async (threadId: number, title: string) => {
41+ await updateThread(threadId, { title }, setError);
42+ await loadThreads(8, setError);
43+ });
4144 const messageInputRef = useRef<MessageInputRef>(null);
4245
4346 const handleDeleteMessage = (messageId: number, threadId: number) => {
src/components/Sidebar.tsx+1−0
@@ -79,6 +79,7 @@ export default function Sidebar({
7979 onThreadSelect(thread.id);
8080 }}
8181 isSelected={isSelected}
82+ className="text-sm"
8283 >
8384 <span className="min-w-0 truncate">
8485 {thread.title || `Thread ${thread.id}`}
src/hooks/useMessages.ts+45−1
@@ -1,7 +1,11 @@
11 import { useState, useEffect, useCallback, useRef } from "react";
22 import type { Message } from "@/src/lib/db/schema";
33
4-export function useMessages(threadId: number | null, onError?: (error: string) => void) {
4+export function useMessages(
5+ threadId: number | null,
6+ onError?: (error: string) => void,
7+ onUpdateThreadTitle?: (threadId: number, title: string) => Promise<void>
8+) {
59 const [messages, setMessages] = useState<Message[]>([]);
610 const [isLoading, setIsLoading] = useState<boolean>(false);
711 const [isLoadingMessages, setIsLoadingMessages] = useState<boolean>(false);
@@ -207,6 +211,46 @@ export function useMessages(threadId: number | null, onError?: (error: string) =
207211 );
208212 }
209213 setStreamingMessageId(null);
214+
215+ // Generate title after first assistant response
216+ // Check if this is the first response (exactly 1 user message and 1 assistant message, excluding system messages)
217+ const previousUserMessages = messages.filter(
218+ (m) => m.role === "user" && m.threadId === targetThreadId
219+ );
220+ const previousAssistantMessages = messages.filter(
221+ (m) => m.role === "assistant" && m.threadId === targetThreadId
222+ );
223+
224+ // After this response, we'll have: previous user messages + this user message, and previous assistant messages + this assistant message
225+ const totalUserMessages = previousUserMessages.length + 1;
226+ const totalAssistantMessages = previousAssistantMessages.length + 1;
227+
228+ if (
229+ totalUserMessages === 1 &&
230+ totalAssistantMessages === 1 &&
231+ onUpdateThreadTitle &&
232+ data.model
233+ ) {
234+ // This is the first response, generate a title
235+ const threadModel = data.model;
236+
237+ if (threadModel) {
238+ try {
239+ const titleRes = await fetch(`/api/threads/${targetThreadId}/title`, {
240+ method: "POST",
241+ });
242+ if (titleRes.ok) {
243+ const titleData = await titleRes.json();
244+ if (titleData.title) {
245+ await onUpdateThreadTitle(targetThreadId, titleData.title);
246+ }
247+ }
248+ } catch {
249+ // Silently fail title generation - don't interrupt the flow
250+ }
251+ }
252+ }
253+
210254 shouldStop = true;
211255 break;
212256 } else if (data.type === "error") {
src/hooks/useThreads.ts+10−2
@@ -101,14 +101,22 @@ export function useThreads() {
101101
102102 const updateThread = async (
103103 threadId: number,
104- updates: { model?: string; maxPromptLength?: "none" | 1024 | 4096 | null },
104+ updates: {
105+ model?: string;
106+ maxPromptLength?: "none" | 1024 | 4096 | null;
107+ title?: string;
108+ },
105109 onError?: (error: string) => void
106110 ): Promise<void> => {
107111 try {
108112 const res = await fetch(`/api/threads/${threadId}`, {
109113 method: "PATCH",
110114 headers: { "Content-Type": "application/json" },
111- body: JSON.stringify(updates),
115+ body: JSON.stringify({
116+ ...updates,
117+ // Only send title if it's provided
118+ ...(updates.title !== undefined ? { title: updates.title } : {}),
119+ }),
112120 });
113121 if (!res.ok) {
114122 const errorData = await res.json().catch((parseError) => {
src/lib/ollama/chat/assistant-response.ts+22−34
@@ -84,50 +84,38 @@ export function streamAssistantResponse({
8484 cleanupAbortHandler();
8585 }
8686
87+ // Helper function to save message and send done event
88+ const saveAndSendDone = async (genTimeMs: number | null) => {
89+ await saveAssistantMessage({
90+ threadId,
91+ content: accumulatedContent,
92+ model: threadModel,
93+ maxPromptLength: threadMaxPromptLength,
94+ generationTimeMs: genTimeMs,
95+ toolCallCounts: extractToolCounts(allToolCalls),
96+ });
97+
98+ safeEnqueue({
99+ type: "done",
100+ answer: accumulatedContent,
101+ model: threadModel,
102+ maxPromptLength: threadMaxPromptLength,
103+ toolCallCounts: extractToolCounts(allToolCalls),
104+ });
105+ };
106+
87107 // Check if aborted before saving
88108 if (signal.aborted) {
89109 // Save partial content if any was generated
90110 if (accumulatedContent) {
91- await saveAssistantMessage({
92- threadId,
93- content: accumulatedContent,
94- model: threadModel,
95- maxPromptLength: threadMaxPromptLength,
96- generationTimeMs: null,
97- toolCallCounts: extractToolCounts(allToolCalls),
98- });
99-
100- // Send done message with metadata for aborted generation
101- safeEnqueue({
102- type: "done",
103- answer: accumulatedContent,
104- model: threadModel,
105- maxPromptLength: threadMaxPromptLength,
106- toolCallCounts: extractToolCounts(allToolCalls),
107- });
111+ await saveAndSendDone(null);
108112 }
109113 safeClose();
110114 return;
111115 }
112116
113117 // Store assistant message with model, maxPromptLength, generation time and tool calls
114- await saveAssistantMessage({
115- threadId,
116- content: accumulatedContent,
117- model: threadModel,
118- maxPromptLength: threadMaxPromptLength,
119- generationTimeMs: generationTimeMs ?? null,
120- toolCallCounts: extractToolCounts(allToolCalls),
121- });
122-
123- // Send final message with metadata
124- safeEnqueue({
125- type: "done",
126- answer: accumulatedContent,
127- model: threadModel,
128- maxPromptLength: threadMaxPromptLength,
129- toolCallCounts: extractToolCounts(allToolCalls),
130- });
118+ await saveAndSendDone(generationTimeMs ?? null);
131119 safeClose();
132120 } catch {
133121 safeEnqueue({ type: "error", error: "Sorry, something went wrong." });
src/lib/ollama/chat/message-persistence.ts+32−2
@@ -3,8 +3,9 @@
33 import { type ToolCall } from "ollama";
44 import { db } from "@/src/lib/db";
55 import { messages, threads } from "@/src/lib/db/schema";
6-import { eq } from "drizzle-orm";
6+import { eq, asc, inArray, and } from "drizzle-orm";
77 import type { MaxPromptLength } from "@/src/lib/ollama/types";
8+import { generateTitle } from "@/src/lib/ollama/title";
89
910 /**
1011 * Extracts tool name counts from an array of tool calls.
@@ -33,6 +34,7 @@ export interface SaveMessageParams {
3334
3435 /**
3536 * Saves an assistant message to the database and updates the thread's updatedAt timestamp.
37+ * Also generates and updates the thread title from the message history.
3638 */
3739 export async function saveAssistantMessage(params: SaveMessageParams): Promise<void> {
3840 const { threadId, content, model, maxPromptLength, generationTimeMs, toolCallCounts } = params;
@@ -48,5 +50,33 @@ export async function saveAssistantMessage(params: SaveMessageParams): Promise<v
4850 toolCallCounts,
4951 });
5052
51- await db.update(threads).set({ updatedAt: new Date() }).where(eq(threads.id, threadId));
53+ // Fetch thread to get the model for title generation
54+ const thread = await db.select().from(threads).where(eq(threads.id, threadId)).limit(1);
55+ const threadModel = thread[0]?.model;
56+
57+ // Always generate and update title if we have a model
58+ if (threadModel) {
59+ // Fetch user and assistant messages for the thread to generate title from message history
60+ const allMessages = await db
61+ .select({ content: messages.content })
62+ .from(messages)
63+ .where(and(eq(messages.threadId, threadId), inArray(messages.role, ["user", "assistant"])))
64+ .orderBy(asc(messages.createdAt));
65+
66+ const messageContents = allMessages.map((msg) => msg.content);
67+ const generatedTitle = await generateTitle(threadModel, messageContents);
68+
69+ if (generatedTitle) {
70+ await db
71+ .update(threads)
72+ .set({ title: generatedTitle, updatedAt: new Date() })
73+ .where(eq(threads.id, threadId));
74+ } else {
75+ // Still update updatedAt even if title generation failed
76+ await db.update(threads).set({ updatedAt: new Date() }).where(eq(threads.id, threadId));
77+ }
78+ } else {
79+ // Just update updatedAt if no model available
80+ await db.update(threads).set({ updatedAt: new Date() }).where(eq(threads.id, threadId));
81+ }
5282 }
src/lib/ollama/index.ts+1−0
@@ -4,4 +4,5 @@
44 export { OllamaChat, streamAssistantResponse } from "./chat";
55 export type { StreamAssistantResponseParams } from "./chat";
66 export { getFirstAvailableModel } from "./models";
7+export { generateTitle } from "./title";
78 export type { OllamaChunk, OllamaChatResponse, MaxPromptLength } from "./types";
src/lib/ollama/title.ts+74−0
@@ -0,0 +1,74 @@
1+/* personal-assistant-thing/src/lib/ollama/title.ts */
2+// Title generation utility for threads
3+
4+import ollama from "ollama";
5+import { debug } from "@/src/lib/debug";
6+
7+/**
8+ * Generates a concise title for a conversation based on message contents.
9+ * @param model - The Ollama model to use for generation.
10+ * @param messageContents - List of message content strings to generate a title from.
11+ * @returns The generated title, or null if generation fails.
12+ */
13+export async function generateTitle(
14+ model: string,
15+ messageContents: string[]
16+): Promise<string | null> {
17+ if (!model || !model.trim()) {
18+ debug("[Title] Model is not set");
19+ return null;
20+ }
21+
22+ if (!messageContents || messageContents.length === 0) {
23+ debug("[Title] Message contents list is empty");
24+ return null;
25+ }
26+
27+ // Use the first non-empty message content for title generation
28+ const firstMessage = messageContents.find((msg) => msg && msg.trim());
29+ if (!firstMessage || !firstMessage.trim()) {
30+ debug("[Title] No valid message content found");
31+ return null;
32+ }
33+
34+ try {
35+ // Generate the prompt internally
36+ const titlePrompt = `Generate a concise title (maximum 5 words) for a conversation. Return only plain text, no markdown formatting, no quotes, no asterisks, no special characters: "${firstMessage.substring(0, 200)}"`;
37+
38+ const response = await ollama.generate({
39+ model,
40+ prompt: titlePrompt,
41+ options: {
42+ num_predict: 20,
43+ temperature: 0.7,
44+ stop: ["\n", "*", "#", "`", "[", "]", "(", ")", "{", "}"],
45+ },
46+ });
47+
48+ if (!response.response) {
49+ debug("[Title] No response from Ollama");
50+ return null;
51+ }
52+
53+ // Clean up markdown characters and quotes
54+ const generatedTitle = response.response
55+ .trim()
56+ .replace(/^["']|["']$/g, "")
57+ .replace(/[*#`\[\](){}]/g, "")
58+ .replace(/^#+\s*/, "")
59+ .replace(/\*\*/g, "")
60+ .replace(/\*/g, "")
61+ .trim();
62+
63+ if (!generatedTitle) {
64+ debug("[Title] Generated title is empty after cleanup");
65+ return null;
66+ }
67+
68+ return generatedTitle;
69+ } catch (err) {
70+ debug(`[Title] Error:`, err instanceof Error ? err.message : "Unknown error");
71+ return null;
72+ }
73+}
74+
src/tests/lib/ollama/chat/ollama-chat.test.ts+29−19
@@ -89,7 +89,9 @@ describe("OllamaChat", () => {
8989 { message: { content: " there" }, total_duration: 2000000 },
9090 ];
9191
92- mockOllamaChatPackage.mockResolvedValue(createMockStream(mockChunks) as unknown as Awaited<ReturnType<typeof ollama.chat>>);
92+ mockOllamaChatPackage.mockResolvedValue(
93+ createMockStream(mockChunks) as unknown as Awaited<ReturnType<typeof ollama.chat>>
94+ );
9395
9496 const result = await OllamaChat(messages, undefined, "test-model");
9597
@@ -102,7 +104,9 @@ describe("OllamaChat", () => {
102104 const messages: Message[] = [{ role: "user", content: "Test" }];
103105 const mockChunks = [{ message: {}, total_duration: 0 }];
104106
105- mockOllamaChatPackage.mockResolvedValue(createMockStream(mockChunks) as unknown as Awaited<ReturnType<typeof ollama.chat>>);
107+ mockOllamaChatPackage.mockResolvedValue(
108+ createMockStream(mockChunks) as unknown as Awaited<ReturnType<typeof ollama.chat>>
109+ );
106110
107111 const result = await OllamaChat(messages, undefined, "test-model");
108112
@@ -114,13 +118,12 @@ describe("OllamaChat", () => {
114118 describe("Streaming chunks", () => {
115119 it("should call onChunk callback with content chunks", async () => {
116120 const messages: Message[] = [{ role: "user", content: "Hello" }];
117- const mockChunks = [
118- { message: { content: "Hello" } },
119- { message: { content: " there" } },
120- ];
121+ const mockChunks = [{ message: { content: "Hello" } }, { message: { content: " there" } }];
121122 const onChunk = vi.fn();
122123
123- mockOllamaChatPackage.mockResolvedValue(createMockStream(mockChunks) as unknown as Awaited<ReturnType<typeof ollama.chat>>);
124+ mockOllamaChatPackage.mockResolvedValue(
125+ createMockStream(mockChunks) as unknown as Awaited<ReturnType<typeof ollama.chat>>
126+ );
124127
125128 await OllamaChat(messages, onChunk, "test-model");
126129
@@ -141,9 +144,7 @@ describe("OllamaChat", () => {
141144 };
142145
143146 // First iteration: tool call
144- const firstChunks = [
145- { message: { tool_calls: [toolCall] }, total_duration: 1000000 },
146- ];
147+ const firstChunks = [{ message: { tool_calls: [toolCall] }, total_duration: 1000000 }];
147148
148149 // Second iteration: final response
149150 const secondChunks = [
@@ -151,8 +152,12 @@ describe("OllamaChat", () => {
151152 ];
152153
153154 mockOllamaChatPackage
154- .mockResolvedValueOnce(createMockStream(firstChunks) as unknown as Awaited<ReturnType<typeof ollama.chat>>)
155- .mockResolvedValueOnce(createMockStream(secondChunks) as unknown as Awaited<ReturnType<typeof ollama.chat>>);
155+ .mockResolvedValueOnce(
156+ createMockStream(firstChunks) as unknown as Awaited<ReturnType<typeof ollama.chat>>
157+ )
158+ .mockResolvedValueOnce(
159+ createMockStream(secondChunks) as unknown as Awaited<ReturnType<typeof ollama.chat>>
160+ );
156161
157162 mockExecuteToolCall.mockResolvedValue({
158163 role: "tool",
@@ -175,9 +180,9 @@ describe("OllamaChat", () => {
175180 const controller = new AbortController();
176181 controller.abort();
177182
178- await expect(OllamaChat(messages, undefined, "test-model", controller.signal)).rejects.toThrow(
179- "Request aborted"
180- );
183+ await expect(
184+ OllamaChat(messages, undefined, "test-model", controller.signal)
185+ ).rejects.toThrow("Request aborted");
181186
182187 expect(mockOllamaChatPackage).not.toHaveBeenCalled();
183188 });
@@ -190,7 +195,9 @@ describe("OllamaChat", () => {
190195 ]);
191196 const cleanup = vi.fn();
192197
193- mockOllamaChatPackage.mockResolvedValue(mockStream as unknown as Awaited<ReturnType<typeof ollama.chat>>);
198+ mockOllamaChatPackage.mockResolvedValue(
199+ mockStream as unknown as Awaited<ReturnType<typeof ollama.chat>>
200+ );
194201 mockSetupAbortHandler.mockReturnValue(cleanup);
195202
196203 await OllamaChat(messages, undefined, "test-model", signal);
@@ -205,7 +212,9 @@ describe("OllamaChat", () => {
205212 const messages: Message[] = [{ role: "user", content: "Test" }];
206213 const mockChunks = [{ message: { content: "Response" }, total_duration: 1000000 }];
207214
208- mockOllamaChatPackage.mockResolvedValue(createMockStream(mockChunks) as unknown as Awaited<ReturnType<typeof ollama.chat>>);
215+ mockOllamaChatPackage.mockResolvedValue(
216+ createMockStream(mockChunks) as unknown as Awaited<ReturnType<typeof ollama.chat>>
217+ );
209218
210219 await OllamaChat(messages, undefined, "test-model", undefined, 1024);
211220
@@ -220,7 +229,9 @@ describe("OllamaChat", () => {
220229 const messages: Message[] = [{ role: "user", content: "Test" }];
221230 const mockChunks = [{ message: { content: "Response" }, total_duration: 1000000 }];
222231
223- mockOllamaChatPackage.mockResolvedValue(createMockStream(mockChunks) as unknown as Awaited<ReturnType<typeof ollama.chat>>);
232+ mockOllamaChatPackage.mockResolvedValue(
233+ createMockStream(mockChunks) as unknown as Awaited<ReturnType<typeof ollama.chat>>
234+ );
224235
225236 await OllamaChat(messages, undefined, "test-model", undefined, "none");
226237
@@ -232,4 +243,3 @@ describe("OllamaChat", () => {
232243 });
233244 });
234245 });
235-
tsconfig.tsbuildinfo+1−1
@@ -1 +1 @@
1-{"fileNames":["./node_modules/typescript/lib/lib.es5.d.ts","./node_modules/typescript/lib/lib.es2015.d.ts","./node_modules/typescript/lib/lib.es2016.d.ts","./node_modules/typescript/lib/lib.es2017.d.ts","./node_modules/typescript/lib/lib.es2018.d.ts","./node_modules/typescript/lib/lib.es2019.d.ts","./node_modules/typescript/lib/lib.es2020.d.ts","./node_modules/typescript/lib/lib.es2021.d.ts","./node_modules/typescript/lib/lib.es2022.d.ts","./node_modules/typescript/lib/lib.es2023.d.ts","./node_m …
1+{"fileNames":["./node_modules/typescript/lib/lib.es5.d.ts","./node_modules/typescript/lib/lib.es2015.d.ts","./node_modules/typescript/lib/lib.es2016.d.ts","./node_modules/typescript/lib/lib.es2017.d.ts","./node_modules/typescript/lib/lib.es2018.d.ts","./node_modules/typescript/lib/lib.es2019.d.ts","./node_modules/typescript/lib/lib.es2020.d.ts","./node_modules/typescript/lib/lib.es2021.d.ts","./node_modules/typescript/lib/lib.es2022.d.ts","./node_modules/typescript/lib/lib.es2023.d.ts","./node_m …