@@ -1,21 +1,12 @@
11 /* personal-assistant-thing/src/lib/ollama/index.ts */
22 // Wrapper using the official Ollama npm package.
33
4-import ollama from "ollama";
4+import ollama, { type Message, type ToolCall } from "ollama";
55 import { debug } from "@/src/lib/debug";
66 import { duckDuckGoTool, queryDuckDuckGo } from "@/src/lib/ollama/duckduckgo";
77 import { queryWeather, weatherTool } from "@/src/lib/ollama/weather";
88 import { queryWebSearch, webSearchTool } from "@/src/lib/ollama/websearch";
9-import type {
10- OllamaMessage,
11- OllamaMessageInput,
12- OllamaChunk,
13- OllamaResponse,
14- MaxPromptLength,
15- ToolCall,
16- OllamaChatMessage,
17-} from "@/src/lib/ollama/types";
18-
9+import type { OllamaChunk, OllamaResponse, MaxPromptLength } from "@/src/lib/ollama/types";
1910
2011 /**
2112 * Executes a tool call and returns the result.
@@ -23,41 +14,25 @@ import type {
2314 * @param toolCall - The tool call from Ollama.
2415 * @returns The tool response with the execution result.
2516 */
26-async function executeToolCall(toolCall: ToolCall): Promise<OllamaMessage> {
17+async function executeToolCall(toolCall: ToolCall): Promise<Message> {
2718 const { name, arguments: args } = toolCall.function;
28- const parsedArgs = typeof args === "string" ? JSON.parse(args) : args;
19+ const { query } = args as { query: string };
2920
30- debug(`[Tool] Executing: ${name}`, parsedArgs);
21+ const tools = {
22+ query_web_search: queryWebSearch,
23+ query_weather: queryWeather,
24+ query_duckduckgo: queryDuckDuckGo,
25+ };
3126
3227 let result: string;
33- switch (name) {
34- case "query_duckduckgo":
35- if (!parsedArgs.query || typeof parsedArgs.query !== "string") {
36- result = `Error: Missing or invalid "query" parameter for query_duckduckgo`;
37- } else {
38- result = await queryDuckDuckGo(parsedArgs.query);
39- }
40- break;
41- case "query_weather":
42- if (!parsedArgs.location || typeof parsedArgs.location !== "string") {
43- result = `Error: Missing or invalid "location" parameter for query_weather`;
44- } else {
45- result = await queryWeather(parsedArgs.location);
46- }
47- break;
48- case "query_web_search":
49- if (!parsedArgs.query || typeof parsedArgs.query !== "string") {
50- result = `Error: Missing or invalid "query" parameter for query_web_search`;
51- } else {
52- result = await queryWebSearch(parsedArgs.query);
53- }
54- break;
55- default:
56- debug(`[Tool] Unknown tool: ${name}`);
57- result = `Error: Unknown tool "${name}"`;
28+ try {
29+ result = tools[name](query);
30+ debug(`[Tool] ${name} completed, result length: ${result.length} chars`);
31+ } catch (error) {
32+ debug("[Tool] call failed: " + error.message || "Unknown error")
33+ result = `Error executing tool call: ${error.message || "Unknown error"}`;
5834 }
5935
60- debug(`[Tool] ${name} completed, result length: ${result.length} chars`);
6136 return {
6237 role: "tool",
6338 tool_name: name,
@@ -73,36 +48,22 @@ async function executeToolCall(toolCall: ToolCall): Promise<OllamaMessage> {
7348 *
7449 * @param messages - Array of messages in the conversation history.
7550 * @param onChunk - Optional callback function to receive streaming chunks (content, thinking, toolCalls).
76- * @param model - The Ollama model to use. Defaults to 'gpt-oss'.
51+ * @param model - The Ollama model to use..
7752 * @param signal - Optional AbortSignal to cancel the request.
7853 * @param maxPromptLength - Optional maximum prompt length in tokens. Can be "none", 1024, or 4096.
7954 * @returns Object containing the response content and generation time in milliseconds.
8055 * @throws If the request fails.
8156 */
8257 export async function fetchOllamaResponse(
83- messages: OllamaMessageInput[],
58+ messages: Message[],
8459 onChunk?: (chunk: OllamaChunk) => void,
85- model = "gpt-oss",
60+ model = "",
8661 signal?: AbortSignal,
8762 maxPromptLength?: MaxPromptLength
8863 ): Promise<OllamaResponse> {
8964 const tools = [duckDuckGoTool, weatherTool, webSearchTool];
9065 let totalDuration = 0;
91- const currentMessages: OllamaMessage[] = messages.map((msg) => {
92- const message: OllamaMessage = {
93- role: msg.role,
94- content: msg.content,
95- };
96- // Include tool_calls if they exist (from database)
97- if (msg.toolCalls) {
98- try {
99- message.tool_calls = JSON.parse(msg.toolCalls);
100- } catch {
101- // If parsing fails, ignore tool_calls
102- }
103- }
104- return message;
105- });
66+ const currentMessages: Message[] = messages;
10667 const allToolCalls: ToolCall[] = [];
10768
10869 // Build options object with num_ctx if maxPromptLength is set
@@ -122,31 +83,12 @@ export async function fetchOllamaResponse(
12283 throw new Error("Request aborted");
12384 }
12485
125- // Ensure all messages have content as a string and tool_calls have proper format (required by ollama.chat)
126- const messagesForOllama: OllamaChatMessage[] = currentMessages.map((msg) => {
127- const { tool_calls, ...msgWithoutToolCalls } = msg;
128- const message: OllamaChatMessage = {
129- ...msgWithoutToolCalls,
130- content: msg.content ?? "",
131- };
132-
133- // Transform tool_calls to ensure arguments is always an object
134- if (tool_calls) {
135- message.tool_calls = tool_calls.map((tc) => ({
136- id: tc.id,
137- type: tc.type,
138- function: {
139- name: tc.function.name,
140- arguments:
141- typeof tc.function.arguments === "string"
142- ? JSON.parse(tc.function.arguments)
143- : tc.function.arguments,
144- },
145- }));
146- }
147-
148- return message;
149- });
86+ // Ensure all messages have content as a string (required by ollama.chat)
87+ // currentMessages is already Message[], just ensure content is not empty
88+ const messagesForOllama: Message[] = currentMessages.map((msg) => ({
89+ ...msg,
90+ content: msg.content ?? "",
91+ }));
15092
15193 const stream = await ollama.chat({
15294 model,
@@ -184,22 +126,23 @@ export async function fetchOllamaResponse(
184126 // Accumulate tool calls from chunks
185127 if (chunk.message?.tool_calls?.length) {
186128 for (const toolCall of chunk.message.tool_calls) {
187- // If tool call has an ID, check if we already have it
129+ // If tool call has an ID (present in streaming but not in type), check if we already have it
188130 const toolCallWithId = toolCall as ToolCall & { id?: string };
189131 if (toolCallWithId.id) {
190132 const existingIndex = toolCalls.findIndex(
191- (tc: ToolCall) => tc.id === toolCallWithId.id
133+ (tc: ToolCall & { id?: string }) =>
134+ (tc as ToolCall & { id?: string }).id === toolCallWithId.id
192135 );
193136 if (existingIndex >= 0) {
194- toolCalls[existingIndex] = toolCall as ToolCall;
137+ toolCalls[existingIndex] = toolCall;
195138 } else {
196- toolCalls.push(toolCall as ToolCall);
139+ toolCalls.push(toolCall);
197140 }
198141 } else {
199- toolCalls.push(toolCall as ToolCall);
142+ toolCalls.push(toolCall);
200143 }
201144 }
202- onChunk?.({ toolCalls: chunk.message.tool_calls as ToolCall[] });
145+ onChunk?.({ toolCalls: chunk.message.tool_calls });
203146 }
204147 }
205148
@@ -241,9 +184,9 @@ export async function fetchOllamaResponse(
241184 // Execute tool calls and add results to messages
242185 debug(
243186 `[Ollama] Tool calls detected:`,
244- toolCalls.map((tc: ToolCall) => ({
187+ toolCalls.map((tc: ToolCall & { id?: string }) => ({
245188 name: tc.function.name,
246- id: tc.id,
189+ id: (tc as ToolCall & { id?: string }).id,
247190 }))
248191 );
249192