xonecas.io
53129
Pa

PAT / commit 7eee0ac

tests: adds more tests to libs

xonecas2025-12-19 7eee0ac0d7f0cd2872c0512c8e99a1ddcd8d2612 parent 99a77ba

22 files changed

This diff is too big to show in full. The first 1000 lines are below, and every file is listed with its counts.

AGENTS.md+2−2
@@ -41,9 +41,9 @@ Add tests when prompted only.
4141 - Use named exports for utility functions
4242
4343 3. **Naming Conventions**:
44- - Functions: `camelCase` (e.g., `fetchOllamaResponse`, `sendMessage`)
44+ - Functions: `camelCase` (e.g., `sendMessage`) or `PascalCase` for main API functions (e.g., `OllamaChat`)
4545 - Components: `PascalCase` (e.g., `Home`, `RootLayout`)
46- - Interfaces: `PascalCase` (e.g., `OllamaMessage`, `OllamaResponse`)
46+ - Interfaces: `PascalCase` (e.g., `OllamaMessage`, `OllamaChatResponse`)
4747 - Constants: `UPPER_SNAKE_CASE` or `camelCase` depending on scope
4848
4949 4. **TypeScript Types**:
app/api/chat/route.ts+1−1
@@ -33,7 +33,7 @@ export async function POST(request: Request) {
3333 }
3434 // Use thread-specific settings (stored in thread table)
3535 const threadModel = thread[0].model;
36- // Note: null from database means "none" (no limit), which fetchOllamaResponse handles correctly
36+ // Note: null from database means "none" (no limit), which OllamaChat handles correctly
3737 const threadMaxPromptLength: MaxPromptLength =
3838 thread[0].maxPromptLength === null ? null : (thread[0].maxPromptLength as MaxPromptLength);
3939
src/hooks/useMessages.ts+6−2
@@ -179,7 +179,8 @@ export function useMessages(threadId: number | null, onError?: (error: string) =
179179 content: data.answer || accumulatedContent,
180180 createdAt: new Date(),
181181 model: data.model || null,
182- maxPromptLength: data.maxPromptLength !== undefined ? data.maxPromptLength : null,
182+ maxPromptLength:
183+ data.maxPromptLength !== undefined ? data.maxPromptLength : null,
183184 generationTimeMs: null,
184185 toolCallCounts: data.toolCallCounts !== undefined ? data.toolCallCounts : null,
185186 };
@@ -196,7 +197,10 @@ export function useMessages(threadId: number | null, onError?: (error: string) =
196197 data.maxPromptLength !== undefined
197198 ? data.maxPromptLength
198199 : msg.maxPromptLength,
199- toolCallCounts: data.toolCallCounts !== undefined ? data.toolCallCounts : msg.toolCallCounts,
200+ toolCallCounts:
201+ data.toolCallCounts !== undefined
202+ ? data.toolCallCounts
203+ : msg.toolCallCounts,
200204 }
201205 : msg
202206 )
src/lib/ollama/chat/abort-handler.ts+23−0
@@ -0,0 +1,23 @@
1+/* personal-assistant-thing/src/lib/ollama/chat/abort-handler.ts */
2+
3+/**
4+ * Sets up an abort handler for the given signal.
5+ * When the signal is aborted, the handler will call the provided callback.
6+ *
7+ * @param signal - The AbortSignal to listen to
8+ * @param onAbort - Callback to execute when abort is triggered
9+ * @returns A cleanup function to remove the event listener
10+ */
11+export function setupAbortHandler(signal: AbortSignal, onAbort: () => void): () => void {
12+ signal.addEventListener("abort", onAbort);
13+ return () => {
14+ signal.removeEventListener("abort", onAbort);
15+ };
16+}
17+
18+/**
19+ * Checks if the signal is aborted or if the controller is closed.
20+ */
21+export function isAbortedOrClosed(signal: AbortSignal, isClosed: () => boolean): boolean {
22+ return signal.aborted || isClosed();
23+}
src/lib/ollama/chat/assistant-response.ts+24−74
@@ -1,26 +1,11 @@
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";
1+/* personal-assistant-thing/src/lib/ollama/chat/assistant-response.ts */
82
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-}
3+import { type Message, type ToolCall } from "ollama";
4+import { OllamaChat } from "./ollama-chat";
5+import type { MaxPromptLength, OllamaChunk } from "@/src/lib/ollama/types";
6+import { createStreamController } from "./stream-controller";
7+import { setupAbortHandler, isAbortedOrClosed } from "./abort-handler";
8+import { saveAssistantMessage, extractToolCounts } from "./message-persistence";
249
2510 export interface StreamAssistantResponseParams {
2611 ollamaMessages: Message[];
@@ -43,31 +28,9 @@ export function streamAssistantResponse({
4328 let accumulatedContent = "";
4429 const allToolCalls: ToolCall[] = [];
4530
46- const encoder = new TextEncoder();
4731 const stream = new ReadableStream({
4832 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- };
33+ const { safeClose, safeEnqueue, isClosed } = createStreamController(controller);
7134
7235 try {
7336 // Check if already aborted
@@ -76,13 +39,9 @@ export function streamAssistantResponse({
7639 return;
7740 }
7841
79- const onChunk = (chunk: {
80- content?: string;
81- thinking?: string;
82- toolCalls?: ToolCall[];
83- }) => {
42+ const onChunk = (chunk: OllamaChunk) => {
8443 // Check abort signal before each chunk
85- if (signal.aborted || isControllerClosed) {
44+ if (isAbortedOrClosed(signal, isClosed)) {
8645 return;
8746 }
8847
@@ -91,23 +50,22 @@ export function streamAssistantResponse({
9150 safeEnqueue({ type: "content", content: chunk.content });
9251 }
9352
94- if (chunk.toolCalls) {
95- allToolCalls.push(...chunk.toolCalls);
96- safeEnqueue({ type: "toolCalls", toolCalls: chunk.toolCalls });
53+ if (chunk.tool_calls) {
54+ allToolCalls.push(...chunk.tool_calls);
55+ safeEnqueue({ type: "toolCalls", toolCalls: chunk.tool_calls });
9756 }
9857 };
9958
10059 // Set up abort handler
101- const abortHandler = () => {
60+ const cleanupAbortHandler = setupAbortHandler(signal, () => {
10261 safeEnqueue({ type: "error", error: "Generation stopped" });
10362 safeClose();
104- };
105- signal.addEventListener("abort", abortHandler);
63+ });
10664
10765 let generationTimeMs: number | undefined;
10866
10967 try {
110- const result = await fetchOllamaResponse(
68+ const result = await OllamaChat(
11169 ollamaMessages,
11270 onChunk,
11371 threadModel,
@@ -123,24 +81,21 @@ export function streamAssistantResponse({
12381 }
12482 throw error;
12583 } finally {
126- signal.removeEventListener("abort", abortHandler);
84+ cleanupAbortHandler();
12785 }
12886
12987 // Check if aborted before saving
13088 if (signal.aborted) {
13189 // Save partial content if any was generated
13290 if (accumulatedContent) {
133- await db.insert(messages).values({
134- threadId: threadId,
135- role: "assistant",
91+ await saveAssistantMessage({
92+ threadId,
13693 content: accumulatedContent,
13794 model: threadModel,
138- maxPromptLength: threadMaxPromptLength === "none" ? null : threadMaxPromptLength, // null means "none" (no limit), otherwise 1024 or 4096
139- createdAt: new Date(),
95+ maxPromptLength: threadMaxPromptLength,
14096 generationTimeMs: null,
14197 toolCallCounts: extractToolCounts(allToolCalls),
14298 });
143- await db.update(threads).set({ updatedAt: new Date() }).where(eq(threads.id, threadId));
14499
145100 // Send done message with metadata for aborted generation
146101 safeEnqueue({
@@ -156,20 +111,15 @@ export function streamAssistantResponse({
156111 }
157112
158113 // Store assistant message with model, maxPromptLength, generation time and tool calls
159- await db.insert(messages).values({
160- threadId: threadId,
161- role: "assistant",
114+ await saveAssistantMessage({
115+ threadId,
162116 content: accumulatedContent,
163117 model: threadModel,
164- maxPromptLength: threadMaxPromptLength === "none" ? null : threadMaxPromptLength, // null means "none" (no limit), otherwise 1024 or 4096
165- createdAt: new Date(),
166- generationTimeMs,
118+ maxPromptLength: threadMaxPromptLength,
119+ generationTimeMs: generationTimeMs ?? null,
167120 toolCallCounts: extractToolCounts(allToolCalls),
168121 });
169122
170- // Update thread's updatedAt timestamp
171- await db.update(threads).set({ updatedAt: new Date() }).where(eq(threads.id, threadId));
172-
173123 // Send final message with metadata
174124 safeEnqueue({
175125 type: "done",
src/lib/ollama/chat/index.ts+6−0
@@ -0,0 +1,6 @@
1+/* personal-assistant-thing/src/lib/ollama/chat/index.ts */
2+// Barrel export file for chat functions
3+
4+export { OllamaChat } from "./ollama-chat";
5+export { streamAssistantResponse } from "./assistant-response";
6+export type { StreamAssistantResponseParams } from "./assistant-response";
src/lib/ollama/chat/message-persistence.ts+52−0
@@ -0,0 +1,52 @@
1+/* personal-assistant-thing/src/lib/ollama/chat/message-persistence.ts */
2+
3+import { type ToolCall } from "ollama";
4+import { db } from "@/src/lib/db";
5+import { messages, threads } from "@/src/lib/db/schema";
6+import { eq } from "drizzle-orm";
7+import type { MaxPromptLength } from "@/src/lib/ollama/types";
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+export 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 SaveMessageParams {
26+ threadId: number;
27+ content: string;
28+ model: string | null;
29+ maxPromptLength: MaxPromptLength;
30+ generationTimeMs: number | null;
31+ toolCallCounts: string | null;
32+}
33+
34+/**
35+ * Saves an assistant message to the database and updates the thread's updatedAt timestamp.
36+ */
37+export async function saveAssistantMessage(params: SaveMessageParams): Promise<void> {
38+ const { threadId, content, model, maxPromptLength, generationTimeMs, toolCallCounts } = params;
39+
40+ await db.insert(messages).values({
41+ threadId,
42+ role: "assistant",
43+ content,
44+ model,
45+ maxPromptLength: maxPromptLength === "none" ? null : maxPromptLength,
46+ createdAt: new Date(),
47+ generationTimeMs,
48+ toolCallCounts,
49+ });
50+
51+ await db.update(threads).set({ updatedAt: new Date() }).where(eq(threads.id, threadId));
52+}
src/lib/ollama/chat/ollama-chat.ts+195−0
@@ -0,0 +1,195 @@
1+/* personal-assistant-thing/src/lib/ollama/chat/ollama-chat.ts */
2+
3+import ollama, { type Message, type ToolCall } from "ollama";
4+import { debug } from "@/src/lib/debug";
5+import { duckDuckGoTool } from "@/src/lib/ollama/tools/duckduckgo";
6+import { fetchPageTool } from "@/src/lib/ollama/tools/fetchpage";
7+import { weatherTool } from "@/src/lib/ollama/tools/weather";
8+import { webSearchTool } from "@/src/lib/ollama/tools/websearch";
9+import { executeToolCall } from "@/src/lib/ollama/tools/tool-runner";
10+import { setupAbortHandler } from "@/src/lib/ollama/chat/abort-handler";
11+import type { OllamaChunk, OllamaChatResponse, MaxPromptLength } from "@/src/lib/ollama/types";
12+
13+/**
14+ * Sends messages to the Ollama model using the chat API and returns the generated response.
15+ * Supports tool calling with an agent loop - if the model requests tools, they are executed
16+ * and the results are sent back to the model until a final response is generated.
17+ * Uses streaming to process responses incrementally and can stream chunks to the UI.
18+ *
19+ * @param messages - Array of messages in the conversation history.
20+ * @param onChunk - Optional callback function to receive streaming chunks (content, thinking, toolCalls).
21+ * @param model - The Ollama model to use..
22+ * @param signal - Optional AbortSignal to cancel the request.
23+ * @param maxPromptLength - Optional maximum prompt length in tokens. Can be "none", 1024, or 4096.
24+ * @returns Object containing the response content and generation time in milliseconds.
25+ * @throws If the request fails.
26+ */
27+export async function OllamaChat(
28+ messages: Message[],
29+ onChunk?: (chunk: OllamaChunk) => void,
30+ model = "",
31+ signal?: AbortSignal,
32+ maxPromptLength?: MaxPromptLength
33+): Promise<OllamaChatResponse> {
34+ const tools = [duckDuckGoTool, weatherTool, webSearchTool, fetchPageTool];
35+ let totalDuration = 0;
36+ const currentMessages: Message[] = messages;
37+ const allToolCalls: ToolCall[] = [];
38+
39+ // Build options object with num_ctx if maxPromptLength is set
40+ const options: { num_ctx?: number } = {};
41+ if (maxPromptLength && maxPromptLength !== "none") {
42+ options.num_ctx = maxPromptLength;
43+ }
44+
45+ debug(
46+ `[Ollama] Starting chat with model: ${model}, messages: ${messages.length}, maxPromptLength: ${maxPromptLength || "none"}`
47+ );
48+
49+ // Agent loop: Keep iterating until the model stops requesting tools
50+ while (true) {
51+ // Check if aborted before starting new iteration
52+ if (signal?.aborted) {
53+ throw new Error("Request aborted");
54+ }
55+
56+ // Ensure all messages have content as a string (required by ollama.chat)
57+ // currentMessages is already Message[], just ensure content is not empty
58+ const messagesForOllama: Message[] = currentMessages.map((msg) => ({
59+ ...msg,
60+ content: msg.content ?? "",
61+ }));
62+
63+ const stream = await ollama.chat({
64+ model,
65+ messages: messagesForOllama,
66+ tools,
67+ stream: true,
68+ options: Object.keys(options).length > 0 ? options : undefined,
69+ });
70+
71+ // Set up abort handler to automatically abort the stream when signal is aborted
72+ const cleanupAbortHandler = signal
73+ ? setupAbortHandler(signal, () => {
74+ stream.abort();
75+ })
76+ : undefined;
77+
78+ let content = "";
79+ let thinking = "";
80+ const toolCalls: ToolCall[] = [];
81+ let iterationDuration = 0;
82+
83+ try {
84+ // Accumulate partial fields from streaming chunks
85+ // The stream will automatically abort when signal is aborted (via handler above)
86+ for await (const chunk of stream) {
87+ if (chunk.total_duration) {
88+ iterationDuration = chunk.total_duration;
89+ }
90+
91+ if (chunk.message?.thinking) {
92+ thinking += chunk.message.thinking;
93+ onChunk?.({ thinking: chunk.message.thinking });
94+ }
95+
96+ if (chunk.message?.content) {
97+ content += chunk.message.content;
98+ onChunk?.({ content: chunk.message.content });
99+ }
100+
101+ // Accumulate tool calls from chunks
102+ if (chunk.message?.tool_calls?.length) {
103+ for (const toolCall of chunk.message.tool_calls) {
104+ // If tool call has an ID (present in streaming but not in type), check if we already have it
105+ const toolCallWithId = toolCall as ToolCall & { id?: string };
106+ if (toolCallWithId.id) {
107+ const existingIndex = toolCalls.findIndex(
108+ (tc: ToolCall & { id?: string }) =>
109+ (tc as ToolCall & { id?: string }).id === toolCallWithId.id
110+ );
111+ if (existingIndex >= 0) {
112+ toolCalls[existingIndex] = toolCall;
113+ } else {
114+ toolCalls.push(toolCall);
115+ }
116+ } else {
117+ toolCalls.push(toolCall);
118+ }
119+ }
120+ onChunk?.({ tool_calls: chunk.message.tool_calls });
121+ }
122+ }
123+ } catch (error) {
124+ // If aborted, throw abort error
125+ if (signal?.aborted || (error instanceof Error && error.message.includes("abort"))) {
126+ throw new Error("Request aborted");
127+ }
128+ throw error;
129+ } finally {
130+ if (cleanupAbortHandler) {
131+ cleanupAbortHandler();
132+ }
133+ }
134+
135+ // Check if aborted after streaming completes
136+ if (signal?.aborted) {
137+ throw new Error("Request aborted");
138+ }
139+
140+ totalDuration += iterationDuration;
141+ const iterationDurationMs = Math.round(iterationDuration / 1_000_000);
142+
143+ debug(`[Ollama] Response (${iterationDurationMs}ms), tool_calls: ${toolCalls.length}`);
144+
145+ // Append accumulated fields to messages
146+ if (thinking || content || toolCalls.length) {
147+ currentMessages.push({
148+ role: "assistant",
149+ thinking: thinking || undefined,
150+ content: content || "",
151+ tool_calls: toolCalls.length > 0 ? toolCalls : undefined,
152+ });
153+ }
154+
155+ // If no tool calls, break the loop and return the final response
156+ if (!toolCalls.length) {
157+ const generationTimeMs = Math.round(totalDuration / 1_000_000);
158+
159+ debug(`[Ollama] Final response (${generationTimeMs}ms, ${content.length} chars)`);
160+
161+ return {
162+ content,
163+ generationTimeMs,
164+ toolCalls: allToolCalls.length > 0 ? allToolCalls : undefined,
165+ };
166+ }
167+
168+ // Execute tool calls and add results to messages
169+ debug(
170+ `[Ollama] Tool calls detected:`,
171+ toolCalls.map((tc: ToolCall & { id?: string }) => ({
172+ name: tc.function.name,
173+ id: (tc as ToolCall & { id?: string }).id,
174+ }))
175+ );
176+
177+ allToolCalls.push(...toolCalls);
178+
179+ for (const toolCall of toolCalls) {
180+ try {
181+ const toolResponse = await executeToolCall(toolCall);
182+ currentMessages.push(toolResponse);
183+ } catch (error) {
184+ const errorMessage = error instanceof Error ? error.message : "Unknown error";
185+ debug(`[Ollama] Tool execution failed: ${errorMessage}`);
186+ // Add error message as tool response so the model can handle it
187+ currentMessages.push({
188+ role: "tool",
189+ tool_name: toolCall.function.name,
190+ content: `Error: ${errorMessage}`,
191+ });
192+ }
193+ }
194+ }
195+}
src/lib/ollama/chat/stream-controller.ts+35−0
@@ -0,0 +1,35 @@
1+/* personal-assistant-thing/src/lib/ollama/chat/stream-controller.ts */
2+
3+/**
4+ * Creates safe close and enqueue functions for a ReadableStream controller.
5+ * These functions prevent double-close errors and handle closed controller states gracefully.
6+ */
7+export function createStreamController(controller: ReadableStreamDefaultController<Uint8Array>) {
8+ let isControllerClosed = false;
9+
10+ const safeClose = () => {
11+ if (!isControllerClosed) {
12+ isControllerClosed = true;
13+ try {
14+ controller.close();
15+ } catch {
16+ // Controller may already be closed, ignore
17+ }
18+ }
19+ };
20+
21+ const encoder = new TextEncoder();
22+ const safeEnqueue = (data: unknown) => {
23+ if (!isControllerClosed) {
24+ try {
25+ controller.enqueue(encoder.encode(`data: ${JSON.stringify(data)}\n\n`));
26+ } catch {
27+ // Controller may be closed, ignore
28+ }
29+ }
30+ };
31+
32+ const isClosed = () => isControllerClosed;
33+
34+ return { safeClose, safeEnqueue, isClosed };
35+}
src/lib/ollama/index.ts+5−221
@@ -1,223 +1,7 @@
11 /* personal-assistant-thing/src/lib/ollama/index.ts */
2-// Wrapper using the official Ollama npm package.
2+// Barrel export file for Ollama integration
33
4-import ollama, { type Message, type ToolCall } from "ollama";
5-import { debug } from "@/src/lib/debug";
6-import { duckDuckGoTool, queryDuckDuckGo } from "@/src/lib/ollama/duckduckgo";
7-import { fetchPage, fetchPageTool } from "@/src/lib/ollama/fetchpage";
8-import { queryWeather, weatherTool } from "@/src/lib/ollama/weather";
9-import { queryWebSearch, webSearchTool } from "@/src/lib/ollama/websearch";
10-import type { OllamaChunk, OllamaResponse, MaxPromptLength } from "@/src/lib/ollama/types";
11-
12-/**
13- * Executes a tool call and returns the result.
14- *
15- * @param toolCall - The tool call from Ollama.
16- * @returns The tool response with the execution result.
17- */
18-async function executeToolCall(toolCall: ToolCall): Promise<Message> {
19- const { name, arguments: args } = toolCall.function;
20- const { query, url, timezone, forecastDays } = args as {
21- query?: string;
22- url?: string;
23- timezone?: string;
24- forecastDays?: number;
25- };
26-
27- const tools = {
28- query_web_search: queryWebSearch,
29- query_weather: queryWeather,
30- query_duckduckgo: queryDuckDuckGo,
31- fetch_page: fetchPage,
32- };
33-
34- let result: string;
35- try {
36- if (name === "query_weather") {
37- result = await queryWeather(query!, timezone, forecastDays);
38- } else if (name === "fetch_page") {
39- result = await fetchPage(url!);
40- } else {
41- result = await tools[name](query!);
42- }
43- debug(`[Tool] ${name} completed (${result.length} chars)`);
44- } catch (error) {
45- const errorMessage = error instanceof Error ? error.message : "Unknown error";
46- debug(`[Tool] ${name} failed: ${errorMessage}`);
47- result = `Error: ${errorMessage}`;
48- }
49-
50- return {
51- role: "tool",
52- tool_name: name,
53- content: result,
54- };
55-}
56-
57-/**
58- * Sends messages to the Ollama model using the chat API and returns the generated response.
59- * Supports tool calling with an agent loop - if the model requests tools, they are executed
60- * and the results are sent back to the model until a final response is generated.
61- * Uses streaming to process responses incrementally and can stream chunks to the UI.
62- *
63- * @param messages - Array of messages in the conversation history.
64- * @param onChunk - Optional callback function to receive streaming chunks (content, thinking, toolCalls).
65- * @param model - The Ollama model to use..
66- * @param signal - Optional AbortSignal to cancel the request.
67- * @param maxPromptLength - Optional maximum prompt length in tokens. Can be "none", 1024, or 4096.
68- * @returns Object containing the response content and generation time in milliseconds.
69- * @throws If the request fails.
70- */
71-export async function fetchOllamaResponse(
72- messages: Message[],
73- onChunk?: (chunk: OllamaChunk) => void,
74- model = "",
75- signal?: AbortSignal,
76- maxPromptLength?: MaxPromptLength
77-): Promise<OllamaResponse> {
78- const tools = [duckDuckGoTool, weatherTool, webSearchTool, fetchPageTool];
79- let totalDuration = 0;
80- const currentMessages: Message[] = messages;
81- const allToolCalls: ToolCall[] = [];
82-
83- // Build options object with num_ctx if maxPromptLength is set
84- const options: { num_ctx?: number } = {};
85- if (maxPromptLength && maxPromptLength !== "none") {
86- options.num_ctx = maxPromptLength;
87- }
88-
89- debug(
90- `[Ollama] Starting chat with model: ${model}, messages: ${messages.length}, maxPromptLength: ${maxPromptLength || "none"}`
91- );
92-
93- // Agent loop: Keep iterating until the model stops requesting tools
94- while (true) {
95- // Check if aborted before starting new iteration
96- if (signal?.aborted) {
97- throw new Error("Request aborted");
98- }
99-
100- // Ensure all messages have content as a string (required by ollama.chat)
101- // currentMessages is already Message[], just ensure content is not empty
102- const messagesForOllama: Message[] = currentMessages.map((msg) => ({
103- ...msg,
104- content: msg.content ?? "",
105- }));
106-
107- const stream = await ollama.chat({
108- model,
109- messages: messagesForOllama,
110- tools,
111- stream: true,
112- options: Object.keys(options).length > 0 ? options : undefined,
113- });
114-
115- let content = "";
116- let thinking = "";
117- const toolCalls: ToolCall[] = [];
118- let iterationDuration = 0;
119-
120- // Accumulate partial fields from streaming chunks
121- for await (const chunk of stream) {
122- // Check abort signal during streaming
123- if (signal?.aborted) {
124- throw new Error("Request aborted");
125- }
126- if (chunk.total_duration) {
127- iterationDuration = chunk.total_duration;
128- }
129-
130- if (chunk.message?.thinking) {
131- thinking += chunk.message.thinking;
132- onChunk?.({ thinking: chunk.message.thinking });
133- }
134-
135- if (chunk.message?.content) {
136- content += chunk.message.content;
137- onChunk?.({ content: chunk.message.content });
138- }
139-
140- // Accumulate tool calls from chunks
141- if (chunk.message?.tool_calls?.length) {
142- for (const toolCall of chunk.message.tool_calls) {
143- // If tool call has an ID (present in streaming but not in type), check if we already have it
144- const toolCallWithId = toolCall as ToolCall & { id?: string };
145- if (toolCallWithId.id) {
146- const existingIndex = toolCalls.findIndex(
147- (tc: ToolCall & { id?: string }) =>
148- (tc as ToolCall & { id?: string }).id === toolCallWithId.id
149- );
150- if (existingIndex >= 0) {
151- toolCalls[existingIndex] = toolCall;
152- } else {
153- toolCalls.push(toolCall);
154- }
155- } else {
156- toolCalls.push(toolCall);
157- }
158- }
159- onChunk?.({ toolCalls: chunk.message.tool_calls });
160- }
161- }
162-
163- totalDuration += iterationDuration;
164- const iterationDurationMs = Math.round(iterationDuration / 1_000_000);
165-
166- debug(`[Ollama] Response (${iterationDurationMs}ms), tool_calls: ${toolCalls.length}`);
167-
168- // Append accumulated fields to messages
169- if (thinking || content || toolCalls.length) {
170- currentMessages.push({
171- role: "assistant",
172- thinking: thinking || undefined,
173- content: content || "",
174- tool_calls: toolCalls.length > 0 ? toolCalls : undefined,
175- });
176- }
177-
178- // Check if aborted before processing tool calls
179- if (signal?.aborted) {
180- throw new Error("Request aborted");
181- }
182-
183- // If no tool calls, break the loop and return the final response
184- if (!toolCalls.length) {
185- const generationTimeMs = Math.round(totalDuration / 1_000_000);
186-
187- debug(`[Ollama] Final response (${generationTimeMs}ms, ${content.length} chars)`);
188-
189- return {
190- content,
191- generationTimeMs,
192- toolCalls: allToolCalls.length > 0 ? allToolCalls : undefined,
193- };
194- }
195-
196- // Execute tool calls and add results to messages
197- debug(
198- `[Ollama] Tool calls detected:`,
199- toolCalls.map((tc: ToolCall & { id?: string }) => ({
200- name: tc.function.name,
201- id: (tc as ToolCall & { id?: string }).id,
202- }))
203- );
204-
205- allToolCalls.push(...toolCalls);
206-
207- for (const toolCall of toolCalls) {
208- try {
209- const toolResponse = await executeToolCall(toolCall);
210- currentMessages.push(toolResponse);
211- } catch (error) {
212- const errorMessage = error instanceof Error ? error.message : "Unknown error";
213- debug(`[Ollama] Tool execution failed: ${errorMessage}`);
214- // Add error message as tool response so the model can handle it
215- currentMessages.push({
216- role: "tool",
217- tool_name: toolCall.function.name,
218- content: `Error: ${errorMessage}`,
219- });
220- }
221- }
222- }
223-}
4+export { OllamaChat, streamAssistantResponse } from "./chat";
5+export type { StreamAssistantResponseParams } from "./chat";
6+export { getFirstAvailableModel } from "./models";
7+export type { OllamaChunk, OllamaChatResponse, MaxPromptLength } from "./types";
src/lib/ollama/tools/duckduckgo.ts+3−3
@@ -1,7 +1,7 @@
1-/* personal-assistant-thing/src/lib/ollama/duckduckgo.ts */
1+/* personal-assistant-thing/src/lib/ollama/tools/duckduckgo.ts */
22
3-import { getCache, setCache } from "../cache";
4-import { createRateLimiter } from "../ratelimit";
3+import { getCache, setCache } from "../../cache";
4+import { createRateLimiter } from "../../ratelimit";
55
66 const CACHE_TTL_MS = 6 * 60 * 60 * 1000; // 6 hours
77 const duckDuckGoRateLimiter = createRateLimiter({
src/lib/ollama/tools/fetchpage.ts+3−3
@@ -1,7 +1,7 @@
1-/* personal-assistant-thing/src/lib/ollama/fetchpage.ts */
1+/* personal-assistant-thing/src/lib/ollama/tools/fetchpage.ts */
22
3-import { getCache, setCache } from "../cache";
4-import { createRateLimiter } from "../ratelimit";
3+import { getCache, setCache } from "../../cache";
4+import { createRateLimiter } from "../../ratelimit";
55
66 const CACHE_TTL_MS = 6 * 60 * 60 * 1000; // 6 hours
77
src/lib/ollama/tools/index.ts+6−0
@@ -0,0 +1,6 @@
1+/* personal-assistant-thing/src/lib/ollama/tools/index.ts */
2+
3+export { queryDuckDuckGo } from "./duckduckgo";
4+export { fetchPage } from "./fetchpage";
5+export { queryWeather } from "./weather";
6+export { queryWebSearch } from "./websearch";
src/lib/ollama/tools/tool-runner.ts+50−0
@@ -0,0 +1,50 @@
1+/* personal-assistant-thing/src/lib/ollama/tools/tool-runner.ts */
2+
3+import { type Message, type ToolCall } from "ollama";
4+import { debug } from "@/src/lib/debug";
5+import { queryDuckDuckGo, fetchPage, queryWeather, queryWebSearch } from "@/src/lib/ollama/tools";
6+
7+/**
8+ * Executes a tool call and returns the result.
9+ *
10+ * @param toolCall - The tool call from Ollama.
11+ * @returns The tool response with the execution result.
12+ */
13+export async function executeToolCall(toolCall: ToolCall): Promise<Message> {
14+ const { name, arguments: args } = toolCall.function;
15+ const { query, url, timezone, forecastDays } = args as {
16+ query?: string;
17+ url?: string;
18+ timezone?: string;
19+ forecastDays?: number;
20+ };
21+
22+ const tools = {
23+ query_web_search: queryWebSearch,
24+ query_weather: queryWeather,
25+ query_duckduckgo: queryDuckDuckGo,
26+ fetch_page: fetchPage,
27+ };
28+
29+ let result: string;
30+ try {
31+ if (name === "query_weather") {
32+ result = await queryWeather(query!, timezone, forecastDays);
33+ } else if (name === "fetch_page") {
34+ result = await fetchPage(url!);
35+ } else {
36+ result = await tools[name](query!);
37+ }
38+ debug(`[Tool] ${name} completed (${result.length} chars)`);
39+ } catch (error) {
40+ const errorMessage = error instanceof Error ? error.message : "Unknown error";
41+ debug(`[Tool] ${name} failed: ${errorMessage}`);
42+ result = `Error: ${errorMessage}`;
43+ }
44+
45+ return {
46+ role: "tool",
47+ tool_name: name,
48+ content: result,
49+ };
50+}
src/lib/ollama/tools/weather.ts+3−3
@@ -1,7 +1,7 @@
1-/* personal-assistant-thing/src/lib/ollama/weather.ts */
1+/* personal-assistant-thing/src/lib/ollama/tools/weather.ts */
22
3-import { getCache, setCache } from "../cache";
4-import { createRateLimiter } from "../ratelimit";
3+import { getCache, setCache } from "../../cache";
4+import { createRateLimiter } from "../../ratelimit";
55
66 const CACHE_TTL_MS = 6 * 60 * 60 * 1000; // 6 hours
77
src/lib/ollama/tools/websearch.ts+3−3
@@ -1,8 +1,8 @@
1-/* personal-assistant-thing/src/lib/ollama/websearch.ts */
1+/* personal-assistant-thing/src/lib/ollama/tools/websearch.ts */
22
33 import { google } from "googleapis";
4-import { getCache, setCache } from "../cache";
5-import { createRateLimiter } from "../ratelimit";
4+import { getCache, setCache } from "../../cache";
5+import { createRateLimiter } from "../../ratelimit";
66
77 const CACHE_TTL_MS = 6 * 60 * 60 * 1000; // 6 hours
88
src/lib/ollama/types.ts+3−3
@@ -4,9 +4,9 @@
44
55 import type { Message, ToolCall } from "ollama";
66
7-// Response type from fetchOllamaResponse (includes generation time)
7+// Response type from OllamaChat (includes generation time)
88 // Based on Message content with additional metadata
9-export type OllamaResponse = Pick<Message, "content"> & {
9+export type OllamaChatResponse = Pick<Message, "content"> & {
1010 generationTimeMs: number;
1111 toolCalls?: ToolCall[]; // Array of tool calls made during the conversation
1212 };
@@ -14,7 +14,7 @@ export type OllamaResponse = Pick<Message, "content"> & {
1414 // Streaming chunk type for onChunk callback
1515 // Based on Message fields for streaming updates (all optional)
1616 export type OllamaChunk = Partial<Pick<Message, "content" | "thinking">> & {
17- toolCalls?: ToolCall[];
17+ tool_calls?: ToolCall[];
1818 };
1919
2020 // Project-specific type for max prompt length setting
src/tests/lib/ollama/chat/assistant-response.test.ts+258−0
@@ -0,0 +1,258 @@
1+/* personal-assistant-thing/src/tests/lib/ollama/chat/assistant-response.test.ts */
2+
3+import { describe, it, expect, beforeEach, vi } from "vitest";
4+import type { Message, ToolCall } from "ollama";
5+import { streamAssistantResponse } from "@/src/lib/ollama/chat/assistant-response";
6+import { OllamaChat } from "@/src/lib/ollama/chat/ollama-chat";
7+import { createStreamController } from "@/src/lib/ollama/chat/stream-controller";
8+import { setupAbortHandler, isAbortedOrClosed } from "@/src/lib/ollama/chat/abort-handler";
9+import { saveAssistantMessage, extractToolCounts } from "@/src/lib/ollama/chat/message-persistence";
10+
11+// Mock dependencies
12+vi.mock("@/src/lib/ollama/chat/ollama-chat", () => ({
13+ OllamaChat: vi.fn(),
14+}));
15+
16+vi.mock("@/src/lib/ollama/chat/stream-controller", () => ({
17+ createStreamController: vi.fn(),
18+}));
19+
20+vi.mock("@/src/lib/ollama/chat/abort-handler", () => ({
21+ setupAbortHandler: vi.fn(),
22+ isAbortedOrClosed: vi.fn(),
23+}));
24+
25+vi.mock("@/src/lib/ollama/chat/message-persistence", () => ({
26+ saveAssistantMessage: vi.fn().mockResolvedValue(undefined),
27+ extractToolCounts: vi.fn(),
28+}));
29+
30+describe("streamAssistantResponse", () => {
31+ const mockOllamaChat = vi.mocked(OllamaChat);
32+ const mockCreateStreamController = vi.mocked(createStreamController);
33+ const mockSetupAbortHandler = vi.mocked(setupAbortHandler);
34+ const mockIsAbortedOrClosed = vi.mocked(isAbortedOrClosed);
35+ const mockSaveAssistantMessage = vi.mocked(saveAssistantMessage);
36+ const mockExtractToolCounts = vi.mocked(extractToolCounts);
37+
38+ const mockSafeEnqueue = vi.fn();
39+ const mockSafeClose = vi.fn();
40+ const mockIsClosed = vi.fn().mockReturnValue(false);
41+ const mockCleanup = vi.fn();
42+
43+ beforeEach(() => {
44+ vi.clearAllMocks();
45+ mockCreateStreamController.mockReturnValue({
46+ safeEnqueue: mockSafeEnqueue,
47+ safeClose: mockSafeClose,
48+ isClosed: mockIsClosed,
49+ });
50+ mockSetupAbortHandler.mockReturnValue(mockCleanup);
51+ mockIsAbortedOrClosed.mockReturnValue(false);
52+ mockExtractToolCounts.mockReturnValue(null);
53+ });
54+
55+ it("should stream content chunks and save message", async () => {
56+ const messages: Message[] = [{ role: "user", content: "Hello" }];
57+ const signal = new AbortController().signal;
58+
59+ mockOllamaChat.mockImplementation(async (_, onChunk) => {
60+ if (onChunk) {
61+ onChunk({ content: "Hello" });
62+ onChunk({ content: " there" });
63+ }
64+ return {
65+ content: "Hello there",
66+ generationTimeMs: 100,
67+ };
68+ });
69+
70+ const stream = streamAssistantResponse({
71+ ollamaMessages: messages,
72+ threadId: 1,
73+ threadModel: "test-model",
74+ threadMaxPromptLength: 1024,
75+ signal,
76+ });
77+
78+ // Trigger stream start by reading
79+ const reader = stream.getReader();
80+ const readPromise = reader.read();
81+
82+ // Wait for stream to process
83+ await new Promise((resolve) => setTimeout(resolve, 10));
84+
85+ // Cancel reader to allow stream to complete
86+ reader.cancel();
87+
88+ try {
89+ await readPromise;
90+ } catch {
91+ // Ignore cancel errors
92+ }
93+
94+ expect(mockOllamaChat).toHaveBeenCalledWith(
95+ messages,
96+ expect.any(Function),
97+ "test-model",
98+ signal,
99+ 1024
100+ );
101+ expect(mockSaveAssistantMessage).toHaveBeenCalledWith({
102+ threadId: 1,
103+ content: "Hello there",
104+ model: "test-model",
105+ maxPromptLength: 1024,
106+ generationTimeMs: 100,
107+ toolCallCounts: null,
108+ });
109+ expect(mockSafeEnqueue).toHaveBeenCalledWith({
110+ type: "done",
111+ answer: "Hello there",
112+ model: "test-model",
113+ maxPromptLength: 1024,
114+ toolCallCounts: null,
115+ });
116+ expect(mockSafeClose).toHaveBeenCalled();
117+ });
118+
119+ it("should handle tool calls", async () => {
120+ const messages: Message[] = [{ role: "user", content: "Search for test" }];
Cut short: this commit's diff is over 1000 lines.
src/tests/lib/ollama/chat/ollama-chat.test.ts+235−0
Cut short: this commit's diff is over 1000 lines.
src/tests/lib/ratelimit.test.ts+3−3
Cut short: this commit's diff is over 1000 lines.
tsconfig.tsbuildinfo+1−1
Cut short: this commit's diff is over 1000 lines.
vitest.config.ts+1−0
Cut short: this commit's diff is over 1000 lines.