@@ -1,16 +1,63 @@
1-import { debug } from "@/src/lib/debug";
1+import { db } from "@/src/lib/db";
2+import { messages, threads } from "@/src/lib/db/schema";
23 import { createMessage } from "@/src/lib/messages";
3-import {
4- duckDuckGoTool,
5- executeToolCall,
6- fetchPageTool,
7- weatherTool,
8- webSearchTool,
9-} from "@/src/lib/tools";
4+import { executeToolCall, fetchPageTool, weatherTool, webSearchTool } from "@/src/lib/tools";
105 import { asc, eq } from "drizzle-orm";
11-import ollama, { type Tool } from "ollama";
12-import { db } from "./db";
13-import { messages, threads } from "./db/schema";
6+import ollama, { type Tool, type ToolCall } from "ollama";
7+
8+type MessageHistory = {
9+ role: "user" | "assistant" | "system" | "tool";
10+ content: string;
11+ thinking?: string;
12+}[];
13+
14+async function agentLoop(model: string, messageHistory: MessageHistory) {
15+ const tools: Tool[] = [weatherTool, fetchPageTool, webSearchTool];
16+ const newMessages: MessageHistory = [];
17+ let maxIterations = 6;
18+
19+ while (true) {
20+ maxIterations--;
21+
22+ const stream = await ollama.chat({
23+ model: model,
24+ messages: [...messageHistory, ...newMessages],
25+ tools: maxIterations > 0 ? tools : undefined,
26+ think: true,
27+ stream: true,
28+ });
29+
30+ let thinking = "";
31+ let content = "";
32+ const calls: ToolCall[] = [];
33+
34+ for await (const chunk of stream) {
35+ if (chunk.message.thinking) {
36+ thinking += chunk.message;
37+ }
38+ if (chunk.message.content) {
39+ content += chunk.message.content;
40+ }
41+ if (chunk.message.tool_calls?.length) {
42+ calls.push(...chunk.message.tool_calls);
43+ }
44+ }
45+
46+ if (content) {
47+ newMessages.push({ role: "assistant", content: content, thinking: thinking });
48+ }
49+ if (calls.length) {
50+ for (const call of calls) {
51+ const toolResponse = await executeToolCall(call);
52+ newMessages.push({ role: "tool", content: `[${call.function.name}] ${toolResponse}` });
53+ }
54+ } else {
55+ break;
56+ }
57+ }
58+
59+ return newMessages;
60+}
1461
1562 export async function generateResponse(threadId: number) {
1663 if (!threadId) {
@@ -24,63 +71,24 @@ export async function generateResponse(threadId: number) {
2471 }
2572
2673 // Get all of the messages for this thread:
27- const messagesList = await db
28- .select()
74+ const messagesList = (await db
75+ .select({
76+ role: messages.role,
77+ content: messages.content,
78+ thinking: messages.thinking,
79+ })
2980 .from(messages)
3081 .where(eq(messages.threadId, threadId))
31- .orderBy(asc(messages.createdAt));
82+ .orderBy(asc(messages.createdAt))) as MessageHistory;
3283
3384 if (messagesList.length === 0) {
3485 throw new Error("No messages found for this thread");
3586 }
3687
37- const tools: Tool[] = [duckDuckGoTool, weatherTool, fetchPageTool, webSearchTool];
38- const chatMessages = messagesList.map((message) => ({
39- role: message.role,
40- content: message.content,
41- }));
42- let iterations = 0;
43- let toolCallsExist = true;
44-
45- // Repeatedly generate model responses until no tool calls remain
46- while (toolCallsExist) {
47- iterations++;
88+ const messageHistory = await agentLoop(thread[0].model, messagesList);
4889
49- const response = await ollama.chat({
50- model: thread[0].model,
51- messages: chatMessages,
52- tools: iterations >= 10 ? undefined : tools, // Avoid looping forever
53- think: true,
54- });
55-
56- if (response.message.thinking) {
57- debug("Thinking:", response.message.thinking);
58- }
59-
60- if (response.message?.tool_calls && response.message.tool_calls.length > 0) {
61- for (const toolCall of response.message.tool_calls) {
62- try {
63- const toolResponse = await executeToolCall(toolCall);
64- await createMessage(`${toolCall.function.name}: ${toolResponse}`, "tool", threadId);
65- chatMessages.push({
66- role: "tool",
67- content: toolResponse,
68- });
69- } catch (error) {
70- const content = `${toolCall.function.name}: Error: ${error instanceof Error ? error.message : "Unknown error"}`;
71- await createMessage(content, "tool", threadId);
72- chatMessages.push({
73- role: "tool",
74- content: content,
75- });
76-
77- debug(content);
78- }
79- }
80- } else {
81- toolCallsExist = false;
82- // insert the final response into the thread:
83- await createMessage(response.message.content, "assistant", threadId);
84- }
90+ // Update the thread with the new message history:
91+ for (const message of messageHistory) {
92+ await createMessage(message.content, message.role, threadId);
8593 }
8694 }