@@ -5,13 +5,41 @@ import { executeToolCall, fetchPageTool, weatherTool, webSearchTool } from "@/sr
55 import { asc, eq } from "drizzle-orm";
66 import ollama, { type Tool, type ToolCall } from "ollama";
77
8-type MessageHistory = {
8+type MessageHistoryEntry = {
99 role: "user" | "assistant" | "system" | "tool";
1010 content: string;
1111 thinking?: string;
12-}[];
12+};
1313
14-async function agentLoop(model: string, messageHistory: MessageHistory) {
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+) {
1543 const tools: Tool[] = [weatherTool, fetchPageTool, webSearchTool];
1644 const newMessages: MessageHistory = [];
1745 let maxIterations = 6;
@@ -33,10 +61,12 @@ async function agentLoop(model: string, messageHistory: MessageHistory) {
3361
3462 for await (const chunk of stream) {
3563 if (chunk.message.thinking) {
36- thinking += chunk.message;
64+ thinking += chunk.message.thinking;
65+ enqueue({ role: "assistant", content: content, thinking: thinking });
3766 }
3867 if (chunk.message.content) {
3968 content += chunk.message.content;
69+ enqueue({ role: "assistant", content: content, thinking: thinking });
4070 }
4171 if (chunk.message.tool_calls?.length) {
4272 calls.push(...chunk.message.tool_calls);
@@ -50,8 +80,15 @@ async function agentLoop(model: string, messageHistory: MessageHistory) {
5080 for (const call of calls) {
5181 const toolResponse = await executeToolCall(call);
5282 newMessages.push({ role: "tool", content: `[${call.function.name}] ${toolResponse}` });
83+ enqueue({ role: "tool", content: `[${call.function.name}] ${toolResponse}` });
5384 }
5485 } else {
86+ enqueue({
87+ role: "assistant",
88+ content: content,
89+ thinking: thinking,
90+ done: true,
91+ });
5592 break;
5693 }
5794 }
@@ -59,7 +96,10 @@ async function agentLoop(model: string, messageHistory: MessageHistory) {
5996 return newMessages;
6097 }
6198
62-export async function generateResponse(threadId: number) {
99+export async function generateResponse(
100+ threadId: number,
101+ enqueue: (message: MessageHistoryEntry) => void
102+) {
63103 if (!threadId) {
64104 throw new Error("Thread ID is required");
65105 }
@@ -85,10 +125,10 @@ export async function generateResponse(threadId: number) {
85125 throw new Error("No messages found for this thread");
86126 }
87127
88- const messageHistory = await agentLoop(thread[0].model, messagesList);
128+ const messageHistory = await agentLoop(thread[0].model, messagesList, enqueue);
89129
90130 // Update the thread with the new message history:
91131 for (const message of messageHistory) {
92- await createMessage(message.content, message.role, threadId);
132+ await createMessage(message.content, message.role, threadId, message.thinking);
93133 }
94134 }