@@ -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";