@@ -2,8 +2,12 @@
22 // Wrapper using the official Ollama npm package.
33
44 import ollama from "ollama";
5+import { debug } from "./debug";
56
6-export interface OllamaMessage {
7+// Use types compatible with ollama package - use any to work around strict typing
8+type OllamaMessage = any;
9+
10+export interface OllamaMessageInput {
711 role: "user" | "assistant" | "system";
812 content: string;
913 }
@@ -13,8 +17,149 @@ export interface OllamaResponse {
1317 generationTimeMs: number;
1418 }
1519
20+/**
21+ * Queries DuckDuckGo's Instant Answer API for information about a given query.
22+ *
23+ * @param query - The search query to send to DuckDuckGo.
24+ * @returns A formatted string containing the instant answer information.
25+ */
26+export async function queryDuckDuckGo(query: string): Promise<string> {
27+ debug(`[DuckDuckGo] Querying: "${query}"`);
28+
29+ try {
30+ const url = "https://api.duckduckgo.com/";
31+ const params = new URLSearchParams({
32+ q: query,
33+ format: "json",
34+ no_html: "1",
35+ skip_disambig: "1",
36+ });
37+
38+ const response = await fetch(`${url}?${params.toString()}`);
39+ if (!response.ok) {
40+ debug(`[DuckDuckGo] Error: HTTP ${response.status}`);
41+ return `Error: Failed to fetch data from DuckDuckGo (${response.status})`;
42+ }
43+
44+ const data = await response.json();
45+ debug(`[DuckDuckGo] Raw API response:`, JSON.stringify(data, null, 2));
46+
47+ // Build a comprehensive response from available fields
48+ const parts: string[] = [];
49+
50+ if (data.Heading) {
51+ parts.push(`Heading: ${data.Heading}`);
52+ }
53+
54+ if (data.AbstractText) {
55+ parts.push(`Abstract: ${data.AbstractText}`);
56+ }
57+
58+ if (data.AbstractURL) {
59+ parts.push(`Source: ${data.AbstractURL}`);
60+ }
61+
62+ if (data.Answer) {
63+ parts.push(`Answer: ${data.Answer}`);
64+ }
65+
66+ if (data.Definition) {
67+ parts.push(`Definition: ${data.Definition}`);
68+ }
69+
70+ if (data.Type) {
71+ parts.push(`Type: ${data.Type}`);
72+ }
73+
74+ if (data.RelatedTopics && data.RelatedTopics.length > 0) {
75+ const topics = data.RelatedTopics.slice(0, 3)
76+ .map((topic: any) => topic.Text || topic.FirstURL)
77+ .filter(Boolean)
78+ .join(", ");
79+ if (topics) {
80+ parts.push(`Related Topics: ${topics}`);
81+ }
82+ }
83+
84+ if (parts.length === 0) {
85+ // DuckDuckGo Instant Answer API has limitations - it doesn't support all query types
86+ // (e.g., weather forecasts, real-time data). This is expected behavior.
87+ debug(`[DuckDuckGo] No instant answer data available for this query type`);
88+ return `No instant answer available for query: "${query}". Note: DuckDuckGo Instant Answer API has limited coverage and may not support weather forecasts, real-time data, or certain query types.`;
89+ }
90+
91+ return parts.join("\n\n");
92+ } catch (error) {
93+ console.error("DuckDuckGo query error:", error);
94+ return `Error querying DuckDuckGo: ${error instanceof Error ? error.message : "Unknown error"}`;
95+ }
96+}
97+
98+/**
99+ * Defines the DuckDuckGo search tool for Ollama.
100+ */
101+export const duckDuckGoTool = {
102+ type: "function" as const,
103+ function: {
104+ name: "query_duckduckgo",
105+ description: "Query DuckDuckGo's Instant Answer API to get quick information about a topic, person, place, or concept. Use this when you need current or factual information that might not be in your training data.",
106+ parameters: {
107+ type: "object",
108+ properties: {
109+ query: {
110+ type: "string",
111+ description: "The search query to send to DuckDuckGo",
112+ },
113+ },
114+ required: ["query"],
115+ },
116+ },
117+};
118+
119+/**
120+ * Executes a tool call and returns the result.
121+ *
122+ * @param toolCall - The tool call from Ollama.
123+ * @returns The tool response with the execution result.
124+ */
125+async function executeToolCall(toolCall: {
126+ id: string;
127+ type: "function";
128+ function: {
129+ name: string;
130+ arguments: string | { [key: string]: any };
131+ };
132+}): Promise<OllamaMessage> {
133+ const { name, arguments: args } = toolCall.function;
134+ const parsedArgs = typeof args === "string" ? JSON.parse(args) : args;
135+
136+ debug(`[Tool] Executing: ${name}`, parsedArgs);
137+
138+ switch (name) {
139+ case "query_duckduckgo":
140+ const result = await queryDuckDuckGo(parsedArgs.query);
141+ debug(`[Tool] ${name} completed, result length: ${result.length} chars`);
142+ return {
143+ tool_call_id: toolCall.id,
144+ role: "tool",
145+ name: "query_duckduckgo",
146+ content: result,
147+ };
148+ default:
149+ debug(`[Tool] Unknown tool: ${name}`);
150+ return {
151+ tool_call_id: toolCall.id,
152+ role: "tool",
153+ name,
154+ content: `Error: Unknown tool "${name}"`,
155+ };
156+ }
157+}
158+
16159 /**
17160 * Sends messages to the Ollama model using the chat API and returns the generated response.
161+ * Supports tool calling - if the model requests tools, they are executed and the results
162+ * are sent back to the model for a final response.
18163 *
19164 * @param messages - Array of messages in the conversation history.
20165 * @param model - The Ollama model to use. Defaults to 'gpt-oss'.
@@ -22,12 +167,98 @@ export interface OllamaResponse {
22167 * @throws If the request fails.
23168 */
24169 export async function fetchOllamaResponse(
25- messages: OllamaMessage[],
170+ messages: OllamaMessageInput[],
26171 model = "gpt-oss"
27172 ): Promise<OllamaResponse> {
28- const result = await ollama.chat({ model, messages });
29- // The official package returns an object with a `message.content` field and `total_duration` in nanoseconds.
30- const content = result.message.content;
31- const generationTimeMs = Math.round(result.total_duration / 1_000_000); // Convert nanoseconds to milliseconds
173+ const tools = [duckDuckGoTool];
174+ let totalDuration = 0;
175+ let currentMessages: OllamaMessage[] = messages.map((msg) => ({
176+ role: msg.role,
177+ content: msg.content,
178+ }));
179+ const maxIterations = 10; // Prevent infinite loops
180+ let iterations = 0;
181+
182+ debug(`[Ollama] Starting chat with model: ${model}, messages: ${messages.length}`);
183+
184+ // Exhaust tool calls: Keep iterating until the model stops requesting tools
185+ // and returns a final response. Each iteration executes any requested tools
186+ // and sends the results back to the model for processing.
187+ while (iterations < maxIterations) {
188+ iterations++;
189+ debug(`[Ollama] Iteration ${iterations}/${maxIterations}, sending to model...`);
190+
191+ const result = await ollama.chat({
192+ model,
193+ messages: currentMessages,
194+ tools,
195+ });
196+
197+ totalDuration += result.total_duration;
198+ const iterationDuration = Math.round(result.total_duration / 1_000_000);
199+
200+ debug(`[Ollama] Response received (${iterationDuration}ms), tool_calls: ${result.message.tool_calls?.length || 0}`);
201+
202+ // If the model made tool calls, execute them and continue the conversation
203+ if (result.message.tool_calls && result.message.tool_calls.length > 0) {
204+ debug(`[Ollama] Tool calls detected:`, result.message.tool_calls.map((tc: any) => ({
205+ name: tc.function.name,
206+ id: tc.id,
207+ })));
208+
209+ // Add the assistant's message with tool calls (use result.message.tool_calls directly)
210+ currentMessages.push({
211+ role: "assistant",
212+ content: result.message.content || "",
213+ tool_calls: result.message.tool_calls,
214+ });
215+
216+ // Execute all tool calls
217+ const toolResponses = await Promise.all(
218+ result.message.tool_calls.map((toolCall: any) =>
219+ executeToolCall({
220+ id: toolCall.id || toolCall.function.name,
221+ type: "function",
222+ function: {
223+ name: toolCall.function.name,
224+ arguments:
225+ typeof toolCall.function.arguments === "string"
226+ ? toolCall.function.arguments
227+ : toolCall.function.arguments,
228+ },
229+ })
230+ )
231+ );
232+
233+ debug(`[Ollama] Tool responses received, continuing conversation...`);
234+
235+ // Add tool responses to the conversation
236+ currentMessages.push(...toolResponses);
237+ continue;
238+ }
239+
240+ // No tool calls, return the final response
241+ const content = result.message.content;
242+ const generationTimeMs = Math.round(totalDuration / 1_000_000); // Convert nanoseconds to milliseconds
243+
244+ debug(`[Ollama] Final response (${generationTimeMs}ms total, ${iterations} iteration${iterations !== 1 ? "s" : ""}), content length: ${content.length} chars`);
245+
246+ return { content, generationTimeMs };
247+ }
248+
249+ // If we hit max iterations, return the last response
250+ debug(`[Ollama] Max iterations reached (${maxIterations}), getting final response...`);
251+
252+ const lastResult = await ollama.chat({
253+ model,
254+ messages: currentMessages,
255+ tools,
256+ });
257+ totalDuration += lastResult.total_duration;
258+ const content = lastResult.message.content;
259+ const generationTimeMs = Math.round(totalDuration / 1_000_000);
260+
261+ debug(`[Ollama] Final response after max iterations (${generationTimeMs}ms total, ${iterations} iterations), content length: ${content.length} chars`);
262+
32263 return { content, generationTimeMs };
33264 }