@@ -1,6 +1,5 @@
11 import { NextResponse } from "next/server";
22 import {
3- fetchOllamaResponse,
43 type OllamaMessageInput,
54 type OllamaMessageRole,
65 type MaxPromptLength,
@@ -8,11 +7,16 @@ import {
87 import { db } from "@/src/lib/db";
98 import { messages, threads } from "@/src/lib/db/schema";
109 import { eq, asc } from "drizzle-orm";
10+import { streamAssistantResponse } from "@/src/lib/chat";
1111
1212 export async function POST(request: Request) {
1313 const { message, threadId: threadIdRaw } = await request.json();
1414 const signal = request.signal;
1515
16+ if (typeof message !== "string" || !message.trim()) {
17+ return NextResponse.json({ error: "message is required " }, { status: 400 });
18+ }
19+
1620 if (!threadIdRaw) {
1721 return NextResponse.json({ error: "threadId is required" }, { status: 400 });
1822 }
@@ -25,21 +29,15 @@ export async function POST(request: Request) {
2529 try {
2630 // 1. Fetch thread to get its model and maxPromptLength settings
2731 // These thread-specific settings override any global settings
28- const thread = await db
29- .select()
30- .from(threads)
31- .where(eq(threads.id, threadId))
32- .limit(1);
32+ const thread = await db.select().from(threads).where(eq(threads.id, threadId)).limit(1);
3333 if (thread.length === 0) {
3434 return NextResponse.json({ error: "Thread not found" }, { status: 404 });
3535 }
36- // Use thread-specific model (stored in thread table)
37- const threadModel = thread[0].model || "gpt-oss";
38- // Use thread-specific maxPromptLength (stored in thread table, can be null, 1024, or 4096)
39- // Type assertion ensures compatibility with fetchOllamaResponse which expects "none" | 1024 | 4096 | null
36+ // Use thread-specific settings (stored in thread table)
37+ const threadModel = thread[0].model;
4038 // Note: null from database means "none" (no limit), which fetchOllamaResponse handles correctly
4139 const threadMaxPromptLength: MaxPromptLength =
42- thread[0].maxPromptLength === null ? 'none' : thread[0].maxPromptLength as MaxPromptLength;
40+ thread[0].maxPromptLength === null ? null : (thread[0].maxPromptLength as MaxPromptLength);
4341
4442 // 2. Fetch previous messages from thread
4543 const previousMessages = await db
@@ -70,202 +68,12 @@ export async function POST(request: Request) {
7068 });
7169
7270 // 5. Stream assistant response
73- let accumulatedContent = "";
74- let accumulatedThinking = "";
75- const allToolCalls: any[] = [];
76-
77- const encoder = new TextEncoder();
78- const stream = new ReadableStream({
79- async start(controller) {
80- // Track if controller is closed to avoid double-close
81- let isControllerClosed = false;
82- const safeClose = () => {
83- if (!isControllerClosed) {
84- isControllerClosed = true;
85- try {
86- controller.close();
87- } catch (e) {
88- // Controller may already be closed, ignore
89- }
90- }
91- };
92-
93- try {
94- // Check if already aborted
95- if (signal.aborted) {
96- safeClose();
97- return;
98- }
99-
100- const onChunk = (chunk: { content?: string; thinking?: string; toolCalls?: any[] }) => {
101- // Check abort signal before each chunk
102- if (signal.aborted || isControllerClosed) {
103- return;
104- }
105-
106- try {
107- if (chunk.content) {
108- accumulatedContent += chunk.content;
109- controller.enqueue(
110- encoder.encode(
111- `data: ${JSON.stringify({ type: "content", content: chunk.content })}\n\n`
112- )
113- );
114- }
115- if (chunk.thinking) {
116- accumulatedThinking += chunk.thinking;
117- controller.enqueue(
118- encoder.encode(
119- `data: ${JSON.stringify({ type: "thinking", thinking: chunk.thinking })}\n\n`
120- )
121- );
122- }
123- if (chunk.toolCalls) {
124- allToolCalls.push(...chunk.toolCalls);
125- controller.enqueue(
126- encoder.encode(
127- `data: ${JSON.stringify({ type: "toolCalls", toolCalls: chunk.toolCalls })}\n\n`
128- )
129- );
130- }
131- } catch (e) {
132- // Controller may be closed, ignore
133- }
134- };
135-
136- // Set up abort handler
137- const abortHandler = () => {
138- if (!isControllerClosed) {
139- try {
140- controller.enqueue(
141- encoder.encode(
142- `data: ${JSON.stringify({ type: "error", error: "Generation stopped" })}\n\n`
143- )
144- );
145- } catch (e) {
146- // Controller may be closed, ignore
147- }
148- safeClose();
149- }
150- };
151- signal.addEventListener("abort", abortHandler);
152-
153- let generationTimeMs: number | undefined;
154- let toolCalls: any[] | undefined;
155-
156- try {
157- const result = await fetchOllamaResponse(
158- ollamaMessages,
159- onChunk,
160- threadModel,
161- signal,
162- threadMaxPromptLength
163- );
164- generationTimeMs = result.generationTimeMs;
165- toolCalls = result.toolCalls;
166- } catch (error) {
167- // If aborted, don't throw - just close
168- if (signal.aborted || (error instanceof Error && error.message === "Request aborted")) {
169- safeClose();
170- return;
171- }
172- throw error;
173- } finally {
174- signal.removeEventListener("abort", abortHandler);
175- }
176-
177- // Check if aborted before saving
178- if (signal.aborted) {
179- // Save partial content if any was generated
180- if (accumulatedContent) {
181- await db.insert(messages).values({
182- threadId: threadId,
183- role: "assistant",
184- content: accumulatedContent,
185- model: threadModel,
186- maxPromptLength: threadMaxPromptLength === "none" ? null : threadMaxPromptLength, // null means "none" (no limit), otherwise 1024 or 4096
187- createdAt: new Date(),
188- generationTimeMs: null,
189- toolCalls: allToolCalls.length > 0 ? JSON.stringify(allToolCalls) : null,
190- });
191- await db
192- .update(threads)
193- .set({ updatedAt: new Date() })
194- .where(eq(threads.id, threadId));
195-
196- // Send done message with metadata for aborted generation
197- if (!isControllerClosed) {
198- try {
199- controller.enqueue(
200- encoder.encode(
201- `data: ${JSON.stringify({
202- type: "done",
203- answer: accumulatedContent,
204- model: threadModel,
205- maxPromptLength: threadMaxPromptLength,
206- })}\n\n`
207- )
208- );
209- } catch (e) {
210- // Controller may be closed, ignore
211- }
212- }
213- }
214- safeClose();
215- return;
216- }
217-
218- // 6. Store assistant message with model, maxPromptLength, generation time and tool calls
219- await db.insert(messages).values({
220- threadId: threadId,
221- role: "assistant",
222- content: accumulatedContent,
223- model: threadModel,
224- maxPromptLength: threadMaxPromptLength === "none" ? null : threadMaxPromptLength, // null means "none" (no limit), otherwise 1024 or 4096
225- createdAt: new Date(),
226- generationTimeMs,
227- toolCalls: toolCalls ? JSON.stringify(toolCalls) : null,
228- });
229-
230- // 7. Update thread's updatedAt timestamp
231- await db
232- .update(threads)
233- .set({ updatedAt: new Date() })
234- .where(eq(threads.id, threadId));
235-
236- // Send final message with metadata
237- if (!isControllerClosed) {
238- try {
239- controller.enqueue(
240- encoder.encode(
241- `data: ${JSON.stringify({
242- type: "done",
243- answer: accumulatedContent,
244- model: threadModel,
245- maxPromptLength: threadMaxPromptLength,
246- })}\n\n`
247- )
248- );
249- } catch (e) {
250- // Controller may be closed, ignore
251- }
252- safeClose();
253- }
254- } catch (error) {
255- if (!isControllerClosed) {
256- try {
257- controller.enqueue(
258- encoder.encode(
259- `data: ${JSON.stringify({ type: "error", error: "Sorry, something went wrong." })}\n\n`
260- )
261- );
262- } catch (e) {
263- // Controller may be closed, ignore
264- }
265- safeClose();
266- }
267- }
268- },
71+ const stream = streamAssistantResponse({
72+ ollamaMessages,
73+ threadId,
74+ threadModel,
75+ threadMaxPromptLength,
76+ signal,
26977 });
27078
27179 return new Response(stream, {
@@ -275,7 +83,7 @@ export async function POST(request: Request) {
27583 Connection: "keep-alive",
27684 },
27785 });
278- } catch (error) {
86+ } catch {
27987 return NextResponse.json({ answer: "Sorry, something went wrong." }, { status: 500 });
28088 }
28189 }