xonecas.io
53129
Pa

PAT / commit dd4f14a

fix: missing data in agent reply broke the UI

Sean2025-12-18 dd4f14af83107c5f0f72cf6c571d054e4c94056a parent 6557443

5 files changed

app/api/chat/route.ts+8−8
@@ -50,6 +50,7 @@ export async function POST(request: Request) {
5050 const ollamaMessages: OllamaMessageInput[] = previousMessages.map((msg) => ({
5151 role: msg.role as "user" | "assistant" | "system",
5252 content: msg.content,
53+ toolCalls: msg.toolCalls || undefined,
5354 }));
5455
5556 // Add the new user message
@@ -80,7 +81,6 @@ export async function POST(request: Request) {
8081 };
8182
8283 try {
83-
8484 // Check if already aborted
8585 if (signal.aborted) {
8686 safeClose();
@@ -182,17 +182,17 @@ export async function POST(request: Request) {
182182 .update(threads)
183183 .set({ updatedAt: new Date() })
184184 .where(eq(threads.id, parseInt(threadId)));
185-
185+
186186 // Send done message with metadata for aborted generation
187187 if (!isControllerClosed) {
188188 try {
189189 controller.enqueue(
190190 encoder.encode(
191- `data: ${JSON.stringify({
192- type: "done",
191+ `data: ${JSON.stringify({
192+ type: "done",
193193 answer: accumulatedContent,
194194 model: threadModel,
195- maxPromptLength: threadMaxPromptLength
195+ maxPromptLength: threadMaxPromptLength,
196196 })}\n\n`
197197 )
198198 );
@@ -228,11 +228,11 @@ export async function POST(request: Request) {
228228 try {
229229 controller.enqueue(
230230 encoder.encode(
231- `data: ${JSON.stringify({
232- type: "done",
231+ `data: ${JSON.stringify({
232+ type: "done",
233233 answer: accumulatedContent,
234234 model: threadModel,
235- maxPromptLength: threadMaxPromptLength
235+ maxPromptLength: threadMaxPromptLength,
236236 })}\n\n`
237237 )
238238 );
src/components/SettingsModal.tsx+0−1
@@ -15,7 +15,6 @@ export default function SettingsModal({
1515 themeMode,
1616 onThemeChange,
1717 }: SettingsModalProps) {
18-
1918 return (
2019 <Modal isOpen={isOpen} onClose={onClose}>
2120 <h2 className="mb-4 text-xl font-semibold text-neutral-800 dark:text-neutral-200">
src/hooks/useMessages.ts+6−3
@@ -149,11 +149,14 @@ export function useMessages(threadId: number | null) {
149149 setMessages((prev) =>
150150 prev.map((msg) =>
151151 msg.id === assistantMsgId
152- ? {
153- ...msg,
152+ ? {
153+ ...msg,
154154 content: data.answer || accumulatedContent,
155155 model: data.model || msg.model,
156- maxPromptLength: data.maxPromptLength !== undefined ? data.maxPromptLength : msg.maxPromptLength
156+ maxPromptLength:
157+ data.maxPromptLength !== undefined
158+ ? data.maxPromptLength
159+ : msg.maxPromptLength,
157160 }
158161 : msg
159162 )
src/hooks/useModelSettings.ts+0−1
@@ -94,4 +94,3 @@ export function useModelSettings() {
9494 handleMaxPromptLengthChange,
9595 };
9696 }
97-
src/lib/ollama/index.ts+17−5
@@ -3,7 +3,7 @@
33
44 import ollama from "ollama";
55 import { debug } from "../debug";
6-import { queryDuckDuckGo, duckDuckGoTool } from "./duckduckgo";
6+import { duckDuckGoTool, queryDuckDuckGo } from "./duckduckgo";
77 import { queryWeather, weatherTool } from "./weather";
88 import { queryWebSearch, webSearchTool } from "./websearch";
99
@@ -13,6 +13,7 @@ type OllamaMessage = any;
1313 export interface OllamaMessageInput {
1414 role: "user" | "assistant" | "system";
1515 content: string;
16+ toolCalls?: string; // Optional JSON string of tool calls (from database)
1617 }
1718
1819 export interface OllamaResponse {
@@ -105,10 +106,21 @@ export async function fetchOllamaResponse(
105106 ): Promise<OllamaResponse> {
106107 const tools = [duckDuckGoTool, weatherTool, webSearchTool];
107108 let totalDuration = 0;
108- let currentMessages: OllamaMessage[] = messages.map((msg) => ({
109- role: msg.role,
110- content: msg.content,
111- }));
109+ let currentMessages: OllamaMessage[] = messages.map((msg) => {
110+ const message: any = {
111+ role: msg.role,
112+ content: msg.content,
113+ };
114+ // Include tool_calls if they exist (from database)
115+ if (msg.toolCalls) {
116+ try {
117+ message.tool_calls = JSON.parse(msg.toolCalls);
118+ } catch {
119+ // If parsing fails, ignore tool_calls
120+ }
121+ }
122+ return message;
123+ });
112124 const allToolCalls: any[] = [];
113125
114126 // Build options object with num_ctx if maxPromptLength is set