@@ -0,0 +1,190 @@
1+/* personal-assistant-thing/src/lib/chat.ts */
2+import { fetchOllamaResponse } from "@/src/lib/ollama";
3+import type { MaxPromptLength } from "@/src/lib/ollama/types";
4+import type { Message, ToolCall } from "ollama";
5+import { db } from "@/src/lib/db";
6+import { messages, threads } from "@/src/lib/db/schema";
7+import { eq } from "drizzle-orm";
8+
9+/**
10+ * Extracts tool name counts from an array of tool calls.
11+ * Returns a JSON string of the format: {"tool_name": count, ...}
12+ */
13+function extractToolCounts(toolCalls: ToolCall[]): string | null {
14+ if (toolCalls.length === 0) return null;
15+
16+ const toolCounts: Record<string, number> = {};
17+ for (const toolCall of toolCalls) {
18+ const toolName = toolCall.function.name;
19+ toolCounts[toolName] = (toolCounts[toolName] || 0) + 1;
20+ }
21+
22+ return JSON.stringify(toolCounts);
23+}
24+
25+export interface StreamAssistantResponseParams {
26+ ollamaMessages: Message[];
27+ threadId: number;
28+ threadModel: string | null;
29+ threadMaxPromptLength: MaxPromptLength;
30+ signal: AbortSignal;
31+}
32+
33+/**
34+ * Streams assistant response and handles database persistence
35+ */
36+export function streamAssistantResponse({
37+ ollamaMessages,
38+ threadId,
39+ threadModel,
40+ threadMaxPromptLength,
41+ signal,
42+}: StreamAssistantResponseParams): ReadableStream<Uint8Array> {
43+ let accumulatedContent = "";
44+ const allToolCalls: ToolCall[] = [];
45+
46+ const encoder = new TextEncoder();
47+ const stream = new ReadableStream({
48+ async start(controller) {
49+ // Track if controller is closed to avoid double-close
50+ let isControllerClosed = false;
51+ const safeClose = () => {
52+ if (!isControllerClosed) {
53+ isControllerClosed = true;
54+ try {
55+ controller.close();
56+ } catch {
57+ // Controller may already be closed, ignore
58+ }
59+ }
60+ };
61+
62+ const safeEnqueue = (data: unknown) => {
63+ if (!isControllerClosed) {
64+ try {
65+ controller.enqueue(encoder.encode(`data: ${JSON.stringify(data)}\n\n`));
66+ } catch {
67+ // Controller may be closed, ignore
68+ }
69+ }
70+ };
71+
72+ try {
73+ // Check if already aborted
74+ if (signal.aborted) {
75+ safeClose();
76+ return;
77+ }
78+
79+ const onChunk = (chunk: {
80+ content?: string;
81+ thinking?: string;
82+ toolCalls?: ToolCall[];
83+ }) => {
84+ // Check abort signal before each chunk
85+ if (signal.aborted || isControllerClosed) {
86+ return;
87+ }
88+
89+ if (chunk.content) {
90+ accumulatedContent += chunk.content;
91+ safeEnqueue({ type: "content", content: chunk.content });
92+ }
93+
94+ if (chunk.toolCalls) {
95+ allToolCalls.push(...chunk.toolCalls);
96+ safeEnqueue({ type: "toolCalls", toolCalls: chunk.toolCalls });
97+ }
98+ };
99+
100+ // Set up abort handler
101+ const abortHandler = () => {
102+ safeEnqueue({ type: "error", error: "Generation stopped" });
103+ safeClose();
104+ };
105+ signal.addEventListener("abort", abortHandler);
106+
107+ let generationTimeMs: number | undefined;
108+ let toolCalls: ToolCall[] | undefined;
109+
110+ try {
111+ const result = await fetchOllamaResponse(
112+ ollamaMessages,
113+ onChunk,
114+ threadModel,
115+ signal,
116+ threadMaxPromptLength
117+ );
118+ generationTimeMs = result.generationTimeMs;
119+ toolCalls = result.toolCalls;
120+ } catch (error) {
121+ // If aborted, don't throw - just close
122+ if (signal.aborted || (error instanceof Error && error.message === "Request aborted")) {
123+ safeClose();
124+ return;
125+ }
126+ throw error;
127+ } finally {
128+ signal.removeEventListener("abort", abortHandler);
129+ }
130+
131+ // Check if aborted before saving
132+ if (signal.aborted) {
133+ // Save partial content if any was generated
134+ if (accumulatedContent) {
135+ await db.insert(messages).values({
136+ threadId: threadId,
137+ role: "assistant",
138+ content: accumulatedContent,
139+ model: threadModel,
140+ maxPromptLength: threadMaxPromptLength === "none" ? null : threadMaxPromptLength, // null means "none" (no limit), otherwise 1024 or 4096
141+ createdAt: new Date(),
142+ generationTimeMs: null,
143+ toolCallCounts: extractToolCounts(allToolCalls),
144+ });
145+ await db.update(threads).set({ updatedAt: new Date() }).where(eq(threads.id, threadId));
146+
147+ // Send done message with metadata for aborted generation
148+ safeEnqueue({
149+ type: "done",
150+ answer: accumulatedContent,
151+ model: threadModel,
152+ maxPromptLength: threadMaxPromptLength,
153+ });
154+ }
155+ safeClose();
156+ return;
157+ }
158+
159+ // Store assistant message with model, maxPromptLength, generation time and tool calls
160+ await db.insert(messages).values({
161+ threadId: threadId,
162+ role: "assistant",
163+ content: accumulatedContent,
164+ model: threadModel,
165+ maxPromptLength: threadMaxPromptLength === "none" ? null : threadMaxPromptLength, // null means "none" (no limit), otherwise 1024 or 4096
166+ createdAt: new Date(),
167+ generationTimeMs,
168+ toolCallCounts: toolCalls ? extractToolCounts(toolCalls) : null,
169+ });
170+
171+ // Update thread's updatedAt timestamp
172+ await db.update(threads).set({ updatedAt: new Date() }).where(eq(threads.id, threadId));
173+
174+ // Send final message with metadata
175+ safeEnqueue({
176+ type: "done",
177+ answer: accumulatedContent,
178+ model: threadModel,
179+ maxPromptLength: threadMaxPromptLength,
180+ });
181+ safeClose();
182+ } catch {
183+ safeEnqueue({ type: "error", error: "Sorry, something went wrong." });
184+ safeClose();
185+ }
186+ },
187+ });
188+
189+ return stream;
190+}