53129
Pa
PAT / files
3.8 KB
1import { db } from "@/src/lib/db"; 2import { messages, threads } from "@/src/lib/db/schema"; 3import { createMessage } from "@/src/lib/messages"; 4import { executeToolCall, fetchPageTool, webSearchTool } from "@/src/lib/tools"; 5import { asc, eq } from "drizzle-orm"; 6import ollama, { type Tool, type ToolCall } from "ollama"; 7 8type MessageHistoryEntry = { 9 role: "user" | "assistant" | "system" | "tool"; 10 content: string; 11 thinking?: string; 12}; 13 14type MessageHistory = MessageHistoryEntry[]; 15 16export 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 38async function agentLoop( 39 model: string, 40 messageHistory: MessageHistory, 41 enqueue: (message: MessageHistoryEntry & { done?: boolean }) => void 42) { 43 const tools: Tool[] = [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 const toolContent = toolResponse ? JSON.stringify(toolResponse) : "null"; 83 newMessages.push({ 84 role: "tool", 85 content: `[${call.function.name}] ${toolContent}`, 86 }); 87 enqueue({ role: "tool", content: `[${call.function.name}] ${toolContent}` }); 88 } 89 } else { 90 enqueue({ 91 role: "assistant", 92 content: content, 93 thinking: thinking, 94 done: true, 95 }); 96 break; 97 } 98 } 99 100 return newMessages; 101} 102 103export async function generateResponse( 104 threadId: number, 105 enqueue: (message: MessageHistoryEntry) => void 106) { 107 if (!threadId) { 108 throw new Error("Thread ID is required"); 109 } 110 111 // Get the thread: 112 const thread = await db.select().from(threads).where(eq(threads.id, threadId)).limit(1); 113 if (thread.length === 0) { 114 throw new Error("Thread not found"); 115 } 116 117 // Get all of the messages for this thread: 118 const messagesList = (await db 119 .select({ 120 role: messages.role, 121 content: messages.content, 122 thinking: messages.thinking, 123 }) 124 .from(messages) 125 .where(eq(messages.threadId, threadId)) 126 .orderBy(asc(messages.createdAt))) as MessageHistory; 127 128 if (messagesList.length === 0) { 129 throw new Error("No messages found for this thread"); 130 } 131 132 const messageHistory = await agentLoop(thread[0].model, messagesList, enqueue); 133 134 // Update the thread with the new message history: 135 for (const message of messageHistory) { 136 await createMessage(message.content, message.role, threadId, message.thinking); 137 } 138}