xonecas.io
53129
Pa

PAT / commit 9139076

feat: duck duck go instant answers query

xonecas2025-12-16 91390763603a55413ecf71e8bffcf36f185d0219 parent 147c7c8

7 files changed

AGENTS.md+21−2
@@ -155,7 +155,8 @@ Agents MUST use only these existing dependencies. Do NOT add new dependencies wi
155155 │ │ ├── db/
156156 │ │ │ ├── index.ts # Database connection and setup
157157 │ │ │ └── schema.ts # Drizzle ORM schema definitions
158-│ │ └── ollama.ts # Ollama API wrapper
158+│ │ ├── debug.ts # Debug logging utility (development only)
159+│ │ └── ollama.ts # Ollama API wrapper with tool calling support
159160 │ └── types.ts # Shared TypeScript type definitions
160161 ├── drizzle/ # Generated migration files
161162 │ ├── meta/ # Migration metadata
@@ -187,11 +188,29 @@ Agents MUST use only these existing dependencies. Do NOT add new dependencies wi
187188 ### Ollama Integration
188189
189190 - Default model: `gpt-oss`
190-- Function: `fetchOllamaResponse(messages: OllamaMessage[], model?: string)`
191+- Function: `fetchOllamaResponse(messages: OllamaMessageInput[], model?: string)`
191192 - Returns: Promise<OllamaResponse> with `{ content: string, generationTimeMs: number }`
192193 - Uses `ollama.chat()` API with full conversation history
194+- **Tool Calling Support**: Supports tool calling with automatic iteration to exhaust tool calls
195+ - Tools are executed and results are sent back to the model until a final response is generated
196+ - Maximum 10 iterations to prevent infinite loops
197+ - Currently supports: `query_duckduckgo` tool for DuckDuckGo Instant Answer API queries
193198 - Located in: `src/lib/ollama.ts`
194199
200+### Tool Integration
201+
202+- **DuckDuckGo Tool** (`query_duckduckgo`): Queries DuckDuckGo's Instant Answer API for quick information
203+ - Function: `queryDuckDuckGo(query: string): Promise<string>`
204+ - Returns formatted information including heading, abstract, answer, definition, and related topics
205+ - Handles cases where no instant answer is available (limited API coverage)
206+ - Located in: `src/lib/ollama.ts`
207+
208+### Debug Utility
209+
210+- **Debug Logging** (`src/lib/debug.ts`): Development-only debug logging utility
211+ - Function: `debug(...args: any[]): void`
212+ - Only logs when `NODE_ENV === "development"`
213+
195214 ### Database Integration (Drizzle ORM)
196215
197216 - **Database**: SQLite using better-sqlite3
TODO.md+1−0
@@ -3,6 +3,7 @@
33 ## BUGS:
44
55 - Improve websearch, currently not working at all. Use this as a template: https://github.com/meirm/ollama-tools/blob/main/sample_functions.py#L5
6+- Markdown typography, it could be much better.
67
78 ## New features:
89
app/api/chat/route.ts+2−2
@@ -1,5 +1,5 @@
11 import { NextResponse } from "next/server";
2-import { fetchOllamaResponse, type OllamaMessage } from "@/src/lib/ollama";
2+import { fetchOllamaResponse, type OllamaMessageInput } from "@/src/lib/ollama";
33 import { db } from "@/src/lib/db";
44 import { messages, threads } from "@/src/lib/db/schema";
55 import { eq, asc } from "drizzle-orm";
@@ -28,7 +28,7 @@ export async function POST(request: Request) {
2828 });
2929
3030 // 3. Build messages array for Ollama
31- const ollamaMessages: OllamaMessage[] = previousMessages.map((msg) => ({
31+ const ollamaMessages: OllamaMessageInput[] = previousMessages.map((msg) => ({
3232 role: msg.role as "user" | "assistant",
3333 content: msg.content,
3434 }));
app/page.tsx+4−2
@@ -95,8 +95,10 @@ export default function Home() {
9595 loadMessages(threadId);
9696 };
9797
98- const handleCreateNewThread = async () => {
99- await createNewThread();
98+ const handleCreateNewThread = () => {
99+ setCurrentThreadId(null);
100+ setMessages([]);
101+ prevMessagesLengthRef.current = 0;
100102 };
101103
102104 const sendMessage = async (message: string) => {
src/components/Sidebar.tsx+1−1
@@ -6,7 +6,7 @@ type SidebarProps = {
66 threads: Thread[];
77 currentThreadId: number | null;
88 themeMode: "device" | "dark" | "light";
9- onCreateNewThread: () => Promise<void>;
9+ onCreateNewThread: () => void;
1010 onThreadSelect: (threadId: number) => void;
1111 onThemeChange: (mode: "device" | "dark" | "light") => void;
1212 };
src/lib/debug.ts+12−0
@@ -0,0 +1,12 @@
1+/* personal-assistant-thing/src/lib/debug.ts */
2+
3+/**
4+ * Debug logging utility that only logs in development mode.
5+ * Accepts the same arguments as console.debug().
6+ */
7+export function debug(...args: any[]): void {
8+ if (process.env.NODE_ENV === "development") {
9+ console.debug(...args);
10+ }
11+}
12+
src/lib/ollama.ts+237−6
@@ -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 }