@@ -6,6 +6,7 @@ import { eq, asc } from "drizzle-orm";
66
77 export async function POST(request: Request) {
88 const { message, threadId } = await request.json();
9+ const signal = request.signal;
910
1011 if (!threadId) {
1112 return NextResponse.json({ error: "threadId is required" }, { status: 400 });
@@ -22,6 +23,7 @@ export async function POST(request: Request) {
2223 return NextResponse.json({ error: "Thread not found" }, { status: 404 });
2324 }
2425 const threadModel = thread[0].model || "gpt-oss";
26+ const threadMaxPromptLength = thread[0].maxPromptLength;
2527
2628 // 2. Fetch previous messages from thread
2729 const previousMessages = await db
@@ -59,38 +61,122 @@ export async function POST(request: Request) {
5961 const stream = new ReadableStream({
6062 async start(controller) {
6163 try {
64+ // Track if controller is closed to avoid double-close
65+ let isControllerClosed = false;
66+ const safeClose = () => {
67+ if (!isControllerClosed) {
68+ isControllerClosed = true;
69+ try {
70+ controller.close();
71+ } catch (e) {
72+ // Controller may already be closed, ignore
73+ }
74+ }
75+ };
76+
77+ // Check if already aborted
78+ if (signal.aborted) {
79+ safeClose();
80+ return;
81+ }
82+
6283 const onChunk = (chunk: { content?: string; thinking?: string; toolCalls?: any[] }) => {
63- if (chunk.content) {
64- accumulatedContent += chunk.content;
65- controller.enqueue(
66- encoder.encode(
67- `data: ${JSON.stringify({ type: "content", content: chunk.content })}\n\n`
68- )
69- );
84+ // Check abort signal before each chunk
85+ if (signal.aborted || isControllerClosed) {
86+ return;
7087 }
71- if (chunk.thinking) {
72- accumulatedThinking += chunk.thinking;
73- controller.enqueue(
74- encoder.encode(
75- `data: ${JSON.stringify({ type: "thinking", thinking: chunk.thinking })}\n\n`
76- )
77- );
88+
89+ try {
90+ if (chunk.content) {
91+ accumulatedContent += chunk.content;
92+ controller.enqueue(
93+ encoder.encode(
94+ `data: ${JSON.stringify({ type: "content", content: chunk.content })}\n\n`
95+ )
96+ );
97+ }
98+ if (chunk.thinking) {
99+ accumulatedThinking += chunk.thinking;
100+ controller.enqueue(
101+ encoder.encode(
102+ `data: ${JSON.stringify({ type: "thinking", thinking: chunk.thinking })}\n\n`
103+ )
104+ );
105+ }
106+ if (chunk.toolCalls) {
107+ allToolCalls.push(...chunk.toolCalls);
108+ controller.enqueue(
109+ encoder.encode(
110+ `data: ${JSON.stringify({ type: "toolCalls", toolCalls: chunk.toolCalls })}\n\n`
111+ )
112+ );
113+ }
114+ } catch (e) {
115+ // Controller may be closed, ignore
78116 }
79- if (chunk.toolCalls) {
80- allToolCalls.push(...chunk.toolCalls);
81- controller.enqueue(
82- encoder.encode(
83- `data: ${JSON.stringify({ type: "toolCalls", toolCalls: chunk.toolCalls })}\n\n`
84- )
85- );
117+ };
118+
119+ // Set up abort handler
120+ const abortHandler = () => {
121+ if (!isControllerClosed) {
122+ try {
123+ controller.enqueue(
124+ encoder.encode(
125+ `data: ${JSON.stringify({ type: "error", error: "Generation stopped" })}\n\n`
126+ )
127+ );
128+ } catch (e) {
129+ // Controller may be closed, ignore
130+ }
131+ safeClose();
86132 }
87133 };
134+ signal.addEventListener("abort", abortHandler);
135+
136+ let generationTimeMs: number | undefined;
137+ let toolCalls: any[] | undefined;
138+
139+ try {
140+ const result = await fetchOllamaResponse(
141+ ollamaMessages,
142+ onChunk,
143+ threadModel,
144+ signal,
145+ threadMaxPromptLength
146+ );
147+ generationTimeMs = result.generationTimeMs;
148+ toolCalls = result.toolCalls;
149+ } catch (error) {
150+ // If aborted, don't throw - just close
151+ if (signal.aborted || (error instanceof Error && error.message === "Request aborted")) {
152+ safeClose();
153+ return;
154+ }
155+ throw error;
156+ } finally {
157+ signal.removeEventListener("abort", abortHandler);
158+ }
88159
89- const { generationTimeMs, toolCalls } = await fetchOllamaResponse(
90- ollamaMessages,
91- onChunk,
92- threadModel
93- );
160+ // Check if aborted before saving
161+ if (signal.aborted) {
162+ // Save partial content if any was generated
163+ if (accumulatedContent) {
164+ await db.insert(messages).values({
165+ threadId: parseInt(threadId),
166+ role: "assistant",
167+ content: accumulatedContent,
168+ createdAt: new Date(),
169+ generationTimeMs: null,
170+ toolCalls: allToolCalls.length > 0 ? JSON.stringify(allToolCalls) : null,
171+ });
172+ await db
173+ .update(threads)
174+ .set({ updatedAt: new Date() })
175+ .where(eq(threads.id, parseInt(threadId)));
176+ }
177+ safeClose();
178+ return;
179+ }
94180
95181 // 6. Store assistant message with generation time and tool calls
96182 await db.insert(messages).values({
@@ -109,20 +195,32 @@ export async function POST(request: Request) {
109195 .where(eq(threads.id, parseInt(threadId)));
110196
111197 // Send final message
112- controller.enqueue(
113- encoder.encode(
114- `data: ${JSON.stringify({ type: "done", answer: accumulatedContent })}\n\n`
115- )
116- );
117- controller.close();
198+ if (!isControllerClosed) {
199+ try {
200+ controller.enqueue(
201+ encoder.encode(
202+ `data: ${JSON.stringify({ type: "done", answer: accumulatedContent })}\n\n`
203+ )
204+ );
205+ } catch (e) {
206+ // Controller may be closed, ignore
207+ }
208+ safeClose();
209+ }
118210 } catch (error) {
119211 console.error("Chat API error", error);
120- controller.enqueue(
121- encoder.encode(
122- `data: ${JSON.stringify({ type: "error", error: "Sorry, something went wrong." })}\n\n`
123- )
124- );
125- controller.close();
212+ if (!isControllerClosed) {
213+ try {
214+ controller.enqueue(
215+ encoder.encode(
216+ `data: ${JSON.stringify({ type: "error", error: "Sorry, something went wrong." })}\n\n`
217+ )
218+ );
219+ } catch (e) {
220+ // Controller may be closed, ignore
221+ }
222+ safeClose();
223+ }
126224 }
127225 },
128226 });