@@ -1,18 +1,105 @@
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";
147
15-export async function generateResponse(threadId: number) {
8+type MessageHistoryEntry = {
9+ role: "user" | "assistant" | "system" | "tool";
10+ content: string;
11+ thinking?: string;
12+};
13+
14+type MessageHistory = MessageHistoryEntry[];
15+
16+export function streamResponse(
17+ onChunk: (enqueue: (message: MessageHistoryEntry & { done?: boolean }) => void) => void
18+) {
19+ const encoder = new TextEncoder();
20+
21+ const stream = new ReadableStream({
22+ start(controller) {
23+ const enqueue = (message: MessageHistoryEntry & { done?: boolean }) => {
24+ const encoded = encoder.encode("data: " + JSON.stringify(message) + "\n\n");
25+ controller.enqueue(encoded);
26+ if (message.done) {
27+ controller.close();
28+ }
29+ };
30+
31+ onChunk(enqueue);
32+ },
33+ });
34+
35+ return stream;
36+}
37+
38+async function agentLoop(
39+ model: string,
40+ messageHistory: MessageHistory,
41+ enqueue: (message: MessageHistoryEntry & { done?: boolean }) => void
42+) {
43+ const tools: Tool[] = [weatherTool, fetchPageTool, webSearchTool];
44+ const newMessages: MessageHistory = [];
45+ let maxIterations = 6;
46+
47+ while (true) {
48+ maxIterations--;
49+
50+ const stream = await ollama.chat({
51+ model: model,
52+ messages: [...messageHistory, ...newMessages],
53+ tools: maxIterations > 0 ? tools : undefined,
54+ think: true,
55+ stream: true,
56+ });
57+
58+ let thinking = "";
59+ let content = "";
60+ const calls: ToolCall[] = [];
61+
62+ for await (const chunk of stream) {
63+ if (chunk.message.thinking) {
64+ thinking += chunk.message.thinking;
65+ enqueue({ role: "assistant", content: content, thinking: thinking });
66+ }
67+ if (chunk.message.content) {
68+ content += chunk.message.content;
69+ enqueue({ role: "assistant", content: content, thinking: thinking });
70+ }
71+ if (chunk.message.tool_calls?.length) {
72+ calls.push(...chunk.message.tool_calls);
73+ }
74+ }
75+
76+ if (content) {
77+ newMessages.push({ role: "assistant", content: content, thinking: thinking });
78+ }
79+ if (calls.length) {
80+ for (const call of calls) {
81+ const toolResponse = await executeToolCall(call);
82+ newMessages.push({ role: "tool", content: `[${call.function.name}] ${toolResponse}` });
83+ enqueue({ role: "tool", content: `[${call.function.name}] ${toolResponse}` });
84+ }
85+ } else {
86+ enqueue({
87+ role: "assistant",
88+ content: content,
89+ thinking: thinking,
90+ done: true,
91+ });
92+ break;
93+ }
94+ }
95+
96+ return newMessages;
97+}
98+
99+export async function generateResponse(
100+ threadId: number,
101+ enqueue: (message: MessageHistoryEntry) => void
102+) {
16103 if (!threadId) {
17104 throw new Error("Thread ID is required");
18105 }
@@ -24,63 +111,24 @@ export async function generateResponse(threadId: number) {
24111 }
25112
26113 // Get all of the messages for this thread:
27- const messagesList = await db
28- .select()
114+ const messagesList = (await db
115+ .select({
116+ role: messages.role,
117+ content: messages.content,
118+ thinking: messages.thinking,
119+ })
29120 .from(messages)
30121 .where(eq(messages.threadId, threadId))
31- .orderBy(asc(messages.createdAt));
122+ .orderBy(asc(messages.createdAt))) as MessageHistory;
32123
33124 if (messagesList.length === 0) {
34125 throw new Error("No messages found for this thread");
35126 }
36127
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++;
48-
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- }
128+ const messageHistory = await agentLoop(thread[0].model, messagesList, enqueue);
59129
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- }
130+ // Update the thread with the new message history:
131+ for (const message of messageHistory) {
132+ await createMessage(message.content, message.role, threadId, message.thinking);
85133 }
86134 }