@@ -13,7 +13,8 @@ export async function POST(request: Request) {
1313 }
1414
1515 try {
16- // 1. Fetch thread to get its model
16+ // 1. Fetch thread to get its model and maxPromptLength settings
17+ // These thread-specific settings override any global settings
1718 const thread = await db
1819 .select()
1920 .from(threads)
@@ -22,8 +23,13 @@ export async function POST(request: Request) {
2223 if (thread.length === 0) {
2324 return NextResponse.json({ error: "Thread not found" }, { status: 404 });
2425 }
26+ // Use thread-specific model (stored in thread table)
2527 const threadModel = thread[0].model || "gpt-oss";
26- const threadMaxPromptLength = thread[0].maxPromptLength;
28+ // Use thread-specific maxPromptLength (stored in thread table, can be null, 1024, or 4096)
29+ // Type assertion ensures compatibility with fetchOllamaResponse which expects "none" | 1024 | 4096 | null
30+ // Note: null from database means "none" (no limit), which fetchOllamaResponse handles correctly
31+ const threadMaxPromptLength: "none" | 1024 | 4096 | null =
32+ thread[0].maxPromptLength === null ? null : (thread[0].maxPromptLength as 1024 | 4096);
2733
2834 // 2. Fetch previous messages from thread
2935 const previousMessages = await db
@@ -60,19 +66,20 @@ export async function POST(request: Request) {
6066 const encoder = new TextEncoder();
6167 const stream = new ReadableStream({
6268 async start(controller) {
63- 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- }
69+ // Track if controller is closed to avoid double-close
70+ let isControllerClosed = false;
71+ const safeClose = () => {
72+ if (!isControllerClosed) {
73+ isControllerClosed = true;
74+ try {
75+ controller.close();
76+ } catch (e) {
77+ // Controller may already be closed, ignore
7478 }
75- };
79+ }
80+ };
81+
82+ try {
7683
7784 // Check if already aborted
7885 if (signal.aborted) {
@@ -165,6 +172,8 @@ export async function POST(request: Request) {
165172 threadId: parseInt(threadId),
166173 role: "assistant",
167174 content: accumulatedContent,
175+ model: threadModel,
176+ maxPromptLength: threadMaxPromptLength, // null means "none" (no limit), otherwise 1024 or 4096
168177 createdAt: new Date(),
169178 generationTimeMs: null,
170179 toolCalls: allToolCalls.length > 0 ? JSON.stringify(allToolCalls) : null,
@@ -173,16 +182,36 @@ export async function POST(request: Request) {
173182 .update(threads)
174183 .set({ updatedAt: new Date() })
175184 .where(eq(threads.id, parseInt(threadId)));
185+
186+ // Send done message with metadata for aborted generation
187+ if (!isControllerClosed) {
188+ try {
189+ controller.enqueue(
190+ encoder.encode(
191+ `data: ${JSON.stringify({
192+ type: "done",
193+ answer: accumulatedContent,
194+ model: threadModel,
195+ maxPromptLength: threadMaxPromptLength
196+ })}\n\n`
197+ )
198+ );
199+ } catch (e) {
200+ // Controller may be closed, ignore
201+ }
202+ }
176203 }
177204 safeClose();
178205 return;
179206 }
180207
181- // 6. Store assistant message with generation time and tool calls
208+ // 6. Store assistant message with model, maxPromptLength, generation time and tool calls
182209 await db.insert(messages).values({
183210 threadId: parseInt(threadId),
184211 role: "assistant",
185212 content: accumulatedContent,
213+ model: threadModel,
214+ maxPromptLength: threadMaxPromptLength, // null means "none" (no limit), otherwise 1024 or 4096
186215 createdAt: new Date(),
187216 generationTimeMs,
188217 toolCalls: toolCalls ? JSON.stringify(toolCalls) : null,
@@ -194,12 +223,17 @@ export async function POST(request: Request) {
194223 .set({ updatedAt: new Date() })
195224 .where(eq(threads.id, parseInt(threadId)));
196225
197- // Send final message
226+ // Send final message with metadata
198227 if (!isControllerClosed) {
199228 try {
200229 controller.enqueue(
201230 encoder.encode(
202- `data: ${JSON.stringify({ type: "done", answer: accumulatedContent })}\n\n`
231+ `data: ${JSON.stringify({
232+ type: "done",
233+ answer: accumulatedContent,
234+ model: threadModel,
235+ maxPromptLength: threadMaxPromptLength
236+ })}\n\n`
203237 )
204238 );
205239 } catch (e) {