xonecas.io
53129
Pa

PAT / commit 2d06027

fix: a collection of too many fixes to list

xonecas2025-12-18 2d06027dbea485469705cd9bd3922fc8306c5647 parent 2ee1cbc

23 files changed

This diff is too big to show in full. The first 1000 lines are below, and every file is listed with its counts.

app/api/chat/route.ts+136−38
@@ -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 });
app/api/settings/route.ts+39−0
@@ -0,0 +1,39 @@
1+import { NextResponse } from "next/server";
2+import { getCache, setCache } from "@/src/lib/cache";
3+
4+const SETTINGS_CACHE_KEY = "app_settings";
5+
6+type Settings = {
7+ maxPromptLength: "none" | 1024 | 4096;
8+};
9+
10+export async function GET() {
11+ try {
12+ const settings = await getCache<Settings>(SETTINGS_CACHE_KEY);
13+ return NextResponse.json({
14+ settings: settings || { maxPromptLength: "none" },
15+ });
16+ } catch (error) {
17+ console.error("Get settings error", error);
18+ return NextResponse.json({ error: "Failed to get settings" }, { status: 500 });
19+ }
20+}
21+
22+export async function POST(request: Request) {
23+ try {
24+ const { maxPromptLength } = await request.json();
25+
26+ // Validate maxPromptLength
27+ if (maxPromptLength !== "none" && maxPromptLength !== 1024 && maxPromptLength !== 4096) {
28+ return NextResponse.json({ error: "Invalid maxPromptLength value" }, { status: 400 });
29+ }
30+
31+ const settings: Settings = { maxPromptLength };
32+ await setCache(SETTINGS_CACHE_KEY, settings);
33+
34+ return NextResponse.json({ settings });
35+ } catch (error) {
36+ console.error("Set settings error", error);
37+ return NextResponse.json({ error: "Failed to set settings" }, { status: 500 });
38+ }
39+}
app/api/threads/route.ts+3−1
@@ -5,12 +5,14 @@ import { desc, count } from "drizzle-orm";
55
66 export async function POST(request: Request) {
77 try {
8- const { title, model } = await request.json();
8+ const { title, model, maxPromptLength } = await request.json();
99 const newThread = await db
1010 .insert(threads)
1111 .values({
1212 title: title || "New Thread",
1313 model: model || "gpt-oss",
14+ maxPromptLength:
15+ maxPromptLength === "none" || maxPromptLength === null ? null : maxPromptLength,
1416 createdAt: new Date(),
1517 updatedAt: new Date(),
1618 })
app/page.tsx+27−4
@@ -6,6 +6,7 @@ import MessageList from "@/src/components/MessageList";
66 import NoThreadSelected from "@/src/components/NoThreadSelected";
77 import { useTheme } from "@/src/hooks/useTheme";
88 import { useModel } from "@/src/hooks/useModel";
9+import { useMaxPromptLength } from "@/src/hooks/useMaxPromptLength";
910 import { useThreads } from "@/src/hooks/useThreads";
1011 import { useMessages } from "@/src/hooks/useMessages";
1112 import { useThreadSelection } from "@/src/hooks/useThreadSelection";
@@ -14,12 +15,20 @@ import "./highlight-theme.css";
1415 export default function Home() {
1516 const { themeMode, handleThemeChange } = useTheme();
1617 const { selectedModel, handleModelChange } = useModel();
18+ const { maxPromptLength, handleMaxPromptLengthChange } = useMaxPromptLength();
1719 const { threads, totalThreadCount, hasMoreThreads, loadThreads, loadMoreThreads, createThread } =
1820 useThreads();
1921 const { currentThreadId, selectThread, deselectThread, messagesContainerRef } =
2022 useThreadSelection();
21- const { messages, isLoading, isLoadingMessages, streamingMessageId, sendMessage, clearMessages } =
22- useMessages(currentThreadId);
23+ const {
24+ messages,
25+ isLoading,
26+ isLoadingMessages,
27+ streamingMessageId,
28+ sendMessage,
29+ clearMessages,
30+ stopGeneration,
31+ } = useMessages(currentThreadId);
2332 const messageInputRef = useRef<MessageInputRef>(null);
2433
2534 const handleThreadSelect = (threadId: number) => {
@@ -36,7 +45,15 @@ export default function Home() {
3645 };
3746
3847 const handleSendMessage = async (message: string) => {
39- await sendMessage(message, currentThreadId, createThread, selectThread, loadThreads, selectedModel);
48+ await sendMessage(
49+ message,
50+ currentThreadId,
51+ createThread,
52+ selectThread,
53+ loadThreads,
54+ selectedModel,
55+ maxPromptLength
56+ );
4057 };
4158
4259 // Get the model name for the current thread, or use selected model if no thread
@@ -50,7 +67,9 @@ export default function Home() {
5067 <div className="relative flex min-w-0 flex-1 flex-col">
5168 <div
5269 ref={messagesContainerRef}
53- className="flex-1 overflow-y-auto overflow-x-hidden bg-neutral-100 p-4 pb-80 text-neutral-900 dark:bg-neutral-900 dark:text-neutral-100"
70+ className={`flex-1 overflow-y-auto overflow-x-hidden bg-neutral-100 p-4 text-neutral-900 dark:bg-neutral-900 dark:text-neutral-100 ${
71+ isLoading && streamingMessageId !== null ? "pb-40" : "pb-32"
72+ }`}
5473 >
5574 <div
5675 className={`mx-auto flex min-h-full max-w-5xl flex-col gap-8 ${currentThreadId === null ? "justify-center" : "justify-end"}`}
@@ -69,6 +88,8 @@ export default function Home() {
6988 messages={messages}
7089 onSubmit={handleSendMessage}
7190 modelName={currentModelName}
91+ isStreaming={isLoading && streamingMessageId !== null}
92+ onStop={stopGeneration}
7293 />
7394 </div>
7495 <Sidebar
@@ -80,6 +101,8 @@ export default function Home() {
80101 onThemeChange={handleThemeChange}
81102 selectedModel={selectedModel}
82103 onModelChange={handleModelChange}
104+ maxPromptLength={maxPromptLength}
105+ onMaxPromptLengthChange={handleMaxPromptLengthChange}
83106 onLoadMore={loadMoreThreads}
84107 hasMoreThreads={hasMoreThreads}
85108 />
drizzle/0002_white_echo.sql+1−0
@@ -0,0 +1 @@
1+ALTER TABLE "threads" ADD COLUMN "max_prompt_length" integer;
drizzle/meta/0002_snapshot.json+141−0
@@ -0,0 +1,141 @@
1+{
2+ "id": "b62bd3e8-878e-4048-b9d6-eec4a4e68c3a",
3+ "prevId": "610bf0a7-5793-4b66-ad2e-7e4fece27494",
4+ "version": "7",
5+ "dialect": "postgresql",
6+ "tables": {
7+ "public.messages": {
8+ "name": "messages",
9+ "schema": "",
10+ "columns": {
11+ "id": {
12+ "name": "id",
13+ "type": "serial",
14+ "primaryKey": true,
15+ "notNull": true
16+ },
17+ "thread_id": {
18+ "name": "thread_id",
19+ "type": "integer",
20+ "primaryKey": false,
21+ "notNull": true
22+ },
23+ "role": {
24+ "name": "role",
25+ "type": "text",
26+ "primaryKey": false,
27+ "notNull": true
28+ },
29+ "content": {
30+ "name": "content",
31+ "type": "text",
32+ "primaryKey": false,
33+ "notNull": true
34+ },
35+ "created_at": {
36+ "name": "created_at",
37+ "type": "timestamp with time zone",
38+ "primaryKey": false,
39+ "notNull": true,
40+ "default": "now()"
41+ },
42+ "generation_time_ms": {
43+ "name": "generation_time_ms",
44+ "type": "integer",
45+ "primaryKey": false,
46+ "notNull": false
47+ },
48+ "tool_calls": {
49+ "name": "tool_calls",
50+ "type": "text",
51+ "primaryKey": false,
52+ "notNull": false
53+ }
54+ },
55+ "indexes": {},
56+ "foreignKeys": {
57+ "messages_thread_id_threads_id_fk": {
58+ "name": "messages_thread_id_threads_id_fk",
59+ "tableFrom": "messages",
60+ "tableTo": "threads",
61+ "columnsFrom": [
62+ "thread_id"
63+ ],
64+ "columnsTo": [
65+ "id"
66+ ],
67+ "onDelete": "cascade",
68+ "onUpdate": "no action"
69+ }
70+ },
71+ "compositePrimaryKeys": {},
72+ "uniqueConstraints": {},
73+ "policies": {},
74+ "checkConstraints": {},
75+ "isRLSEnabled": false
76+ },
77+ "public.threads": {
78+ "name": "threads",
79+ "schema": "",
80+ "columns": {
81+ "id": {
82+ "name": "id",
83+ "type": "serial",
84+ "primaryKey": true,
85+ "notNull": true
86+ },
87+ "title": {
88+ "name": "title",
89+ "type": "text",
90+ "primaryKey": false,
91+ "notNull": false
92+ },
93+ "model": {
94+ "name": "model",
95+ "type": "text",
96+ "primaryKey": false,
97+ "notNull": true,
98+ "default": "'gpt-oss'"
99+ },
100+ "max_prompt_length": {
101+ "name": "max_prompt_length",
102+ "type": "integer",
103+ "primaryKey": false,
104+ "notNull": false
105+ },
106+ "created_at": {
107+ "name": "created_at",
108+ "type": "timestamp with time zone",
109+ "primaryKey": false,
110+ "notNull": true,
111+ "default": "now()"
112+ },
113+ "updated_at": {
114+ "name": "updated_at",
115+ "type": "timestamp with time zone",
116+ "primaryKey": false,
117+ "notNull": true,
118+ "default": "now()"
119+ }
120+ },
121+ "indexes": {},
122+ "foreignKeys": {},
123+ "compositePrimaryKeys": {},
124+ "uniqueConstraints": {},
125+ "policies": {},
126+ "checkConstraints": {},
127+ "isRLSEnabled": false
128+ }
129+ },
130+ "enums": {},
131+ "schemas": {},
132+ "sequences": {},
133+ "roles": {},
134+ "policies": {},
135+ "views": {},
136+ "_meta": {
137+ "columns": {},
138+ "schemas": {},
139+ "tables": {}
140+ }
141+}
drizzle/meta/_journal.json+7−0
@@ -15,6 +15,13 @@
1515 "when": 1766007076801,
1616 "tag": "0001_lethal_madripoor",
1717 "breakpoints": true
18+ },
19+ {
20+ "idx": 2,
21+ "version": "7",
22+ "when": 1766015192850,
23+ "tag": "0002_white_echo",
24+ "breakpoints": true
1825 }
1926 ]
2027 }
src/components/MessageInput.tsx+52−29
@@ -2,7 +2,8 @@
22
33 import { useState, useRef, useImperativeHandle, forwardRef, useEffect } from "react";
44 import PaperPlaneIcon from "@/src/components/icons/PaperPlaneIcon";
5-import BasicButton from "@/src/components/buttons/BasicButton";
5+import PrimaryButton from "@/src/components/buttons/PrimaryButton";
6+import ThinkingNotification from "@/src/components/ThinkingNotification";
67 import type { Message } from "@/src/lib/db/schema";
78
89 type MessageInputProps = {
@@ -11,6 +12,8 @@ type MessageInputProps = {
1112 messages: Message[];
1213 onSubmit: (message: string) => void;
1314 modelName?: string;
15+ isStreaming?: boolean;
16+ onStop?: () => void;
1417 };
1518
1619 export type MessageInputRef = {
@@ -18,10 +21,22 @@ export type MessageInputRef = {
1821 };
1922
2023 const MessageInput = forwardRef<MessageInputRef, MessageInputProps>(
21- ({ isLoading, isLoadingMessages, messages, onSubmit, modelName = "gpt-oss" }, ref) => {
24+ (
25+ {
26+ isLoading,
27+ isLoadingMessages,
28+ messages,
29+ onSubmit,
30+ modelName = "gpt-oss",
31+ isStreaming = false,
32+ onStop,
33+ },
34+ ref
35+ ) => {
2236 const [input, setInput] = useState("");
2337 const [historyIndex, setHistoryIndex] = useState<number | null>(null);
2438 const textareaRef = useRef<HTMLTextAreaElement>(null);
39+ const draftInputRef = useRef<string>("");
2540
2641 useImperativeHandle(ref, () => ({
2742 focus: () => {
@@ -80,18 +95,22 @@ const MessageInput = forwardRef<MessageInputRef, MessageInputProps>(
8095 // Only cycle through user messages, not assistant messages
8196 const userMessages = messages.filter((m) => m.role === "user");
8297
83- if (e.key === "Enter" && e.shiftKey) {
98+ if (e.key === "Enter" && !e.shiftKey) {
8499 e.preventDefault();
85100 handleSubmit();
86- } else if (e.key === "ArrowUp") {
101+ } else if (e.key === "ArrowUp" && (e.ctrlKey || e.metaKey)) {
87102 e.preventDefault();
103+ // Save current input as draft if we're starting to cycle from the current state
104+ if (historyIndex === null) {
105+ draftInputRef.current = input;
106+ }
88107 const idx = historyIndex ?? userMessages.length;
89108 if (idx > 0) {
90109 const newIdx = idx - 1;
91110 setHistoryIndex(newIdx);
92111 setInput(userMessages[newIdx].content);
93112 }
94- } else if (e.key === "ArrowDown") {
113+ } else if (e.key === "ArrowDown" && (e.ctrlKey || e.metaKey)) {
95114 e.preventDefault();
96115 if (historyIndex !== null) {
97116 const newIdx = historyIndex + 1;
@@ -99,8 +118,9 @@ const MessageInput = forwardRef<MessageInputRef, MessageInputProps>(
99118 setHistoryIndex(newIdx);
100119 setInput(userMessages[newIdx].content);
101120 } else {
121+ // Restore draft input when cycling back to current state
102122 setHistoryIndex(null);
103- setInput("");
123+ setInput(draftInputRef.current);
104124 }
105125 }
106126 }
@@ -108,29 +128,32 @@ const MessageInput = forwardRef<MessageInputRef, MessageInputProps>(
108128
109129 return (
110130 <div className="pointer-events-none absolute bottom-0 left-0 right-0 z-10 flex justify-center p-1">
111- <form
112- onSubmit={handleSubmit}
113- className="pointer-events-auto relative m-4 w-full max-w-5xl rounded-lg bg-neutral-300 shadow-lg dark:bg-neutral-700"
114- >
115- <textarea
116- ref={textareaRef}
117- value={input}
118- onChange={(e) => setInput(e.target.value)}
119- onKeyDown={handleKeyDown}
120- placeholder={isAnyLoading ? "Loading..." : "Type a message..."}
121- disabled={isAnyLoading}
122- rows={3}
123- className="w-full resize-none overflow-y-auto bg-transparent p-4 pr-12 placeholder:italic placeholder:text-neutral-600 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50 dark:placeholder:text-neutral-400"
124- />
125- <div className="absolute bottom-2 right-2 flex flex-col items-end gap-1">
126- <div className="text-xs text-neutral-600 dark:text-neutral-400">{modelName}</div>
127- <BasicButton color="neutral" type="submit" disabled={isAnyLoading}>
128- <PaperPlaneIcon
129- className={`h-8 w-10 p-1.5 ${isAnyLoading ? "animate-spin-and-color-cycle" : ""}`}
130- />
131- </BasicButton>
132- </div>
133- </form>
131+ <div className="pointer-events-auto m-4 w-full max-w-5xl">
132+ {isStreaming && onStop && <ThinkingNotification onStop={onStop} />}
133+ <form
134+ onSubmit={handleSubmit}
135+ className="relative rounded-lg bg-neutral-300 shadow-lg dark:bg-neutral-700"
136+ >
137+ <textarea
138+ ref={textareaRef}
139+ value={input}
140+ onChange={(e) => setInput(e.target.value)}
141+ onKeyDown={handleKeyDown}
142+ placeholder={isAnyLoading ? "" : "Type a message..."}
143+ disabled={isAnyLoading}
144+ rows={3}
145+ className="w-full resize-none overflow-y-auto bg-transparent p-4 pr-12 placeholder:italic placeholder:text-neutral-600 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50 dark:placeholder:text-neutral-400"
146+ />
147+ <div className="absolute bottom-2 right-2 flex flex-col items-end gap-1">
148+ <div className="text-xs text-neutral-600 dark:text-neutral-400">{modelName}</div>
149+ <PrimaryButton color="neutral" type="submit" disabled={isAnyLoading}>
150+ <PaperPlaneIcon
151+ className={`h-8 w-10 p-1.5 ${isAnyLoading ? "animate-spin-and-color-cycle" : ""}`}
152+ />
153+ </PrimaryButton>
154+ </div>
155+ </form>
156+ </div>
134157 </div>
135158 );
136159 }
src/components/MessageList/AssistantMessage.tsx+0−5
@@ -56,11 +56,6 @@ export default function AssistantMessage({ message, isStreaming = false }: Assis
5656 {message.content}
5757 </ReactMarkdown>
5858 )}
59- {isStreaming && (
60- <p className={`text-sm text-neutral-600 dark:text-neutral-400 ${hasContent ? "mt-2" : ""}`}>
61- <span className="animate-color-cycle">thinking</span>
62- </p>
63- )}
6459 <div className="mt-4 text-xs text-neutral-600 dark:text-neutral-400">
6560 generated on {formatTimestamp(message.createdAt)}
6661 {message.generationTimeMs && (
src/components/SettingsModal.tsx+23−3
@@ -10,6 +10,8 @@ type SettingsModalProps = {
1010 onThemeChange: (mode: "device" | "dark" | "light") => void;
1111 selectedModel: string;
1212 onModelChange: (model: string) => void;
13+ maxPromptLength: "none" | 1024 | 4096;
14+ onMaxPromptLengthChange: (value: "none" | 1024 | 4096) => void;
1315 };
1416
1517 export default function SettingsModal({
@@ -19,6 +21,8 @@ export default function SettingsModal({
1921 onThemeChange,
2022 selectedModel,
2123 onModelChange,
24+ maxPromptLength,
25+ onMaxPromptLengthChange,
2226 }: SettingsModalProps) {
2327 const [models, setModels] = useState<Array<{ name: string; model: string }>>([]);
2428 const [isLoadingModels, setIsLoadingModels] = useState(false);
@@ -31,20 +35,20 @@ export default function SettingsModal({
3135 .then((data) => {
3236 const availableModels = data.models || [];
3337 setModels(availableModels);
34-
38+
3539 // Validate selected model against available models
3640 if (availableModels.length > 0) {
3741 const modelExists = availableModels.some(
3842 (m: { name: string; model: string }) => m.model === selectedModel
3943 );
40-
44+
4145 if (!modelExists) {
4246 // Selected model doesn't exist, fall back to first available
4347 const fallbackModel = availableModels[0].model;
4448 onModelChange(fallbackModel);
4549 }
4650 }
47-
51+
4852 setIsLoadingModels(false);
4953 })
5054 .catch((error) => {
@@ -93,6 +97,22 @@ export default function SettingsModal({
9397 )}
9498 </select>
9599 </div>
100+ <div className="flex flex-col gap-2">
101+ <label className="text-neutral-600 dark:text-neutral-400">Max Prompt Length</label>
102+ <select
103+ value={maxPromptLength}
104+ onChange={(e) =>
105+ onMaxPromptLengthChange(
106+ e.target.value === "none" ? "none" : (parseInt(e.target.value, 10) as 1024 | 4096)
107+ )
108+ }
109+ className="bg-neutral-100 px-3 py-1 text-neutral-800 focus:outline-none dark:bg-neutral-900 dark:text-neutral-200"
110+ >
111+ <option value="none">None</option>
112+ <option value="1024">1024</option>
113+ <option value="4096">4096</option>
114+ </select>
115+ </div>
96116 </div>
97117 </Modal>
98118 );
src/components/Sidebar.tsx+15−9
@@ -6,7 +6,7 @@ import PlusIcon from "@/src/components/icons/PlusIcon";
66 import ChevronDownIcon from "@/src/components/icons/ChevronDownIcon";
77 import BowtieIcon from "@/src/components/icons/BowtieIcon";
88 import GearIcon from "@/src/components/icons/GearIcon";
9-import SidebarButton from "@/src/components/SidebarButton";
9+import SecondaryButton from "@/src/components/buttons/SecondaryButton";
1010 import SettingsModal from "@/src/components/SettingsModal";
1111
1212 type SidebarProps = {
@@ -18,6 +18,8 @@ type SidebarProps = {
1818 onThemeChange: (mode: "device" | "dark" | "light") => void;
1919 selectedModel: string;
2020 onModelChange: (model: string) => void;
21+ maxPromptLength: "none" | 1024 | 4096;
22+ onMaxPromptLengthChange: (value: "none" | 1024 | 4096) => void;
2123 onLoadMore: () => void;
2224 hasMoreThreads: boolean;
2325 };
@@ -31,6 +33,8 @@ export default function Sidebar({
3133 onThemeChange,
3234 selectedModel,
3335 onModelChange,
36+ maxPromptLength,
37+ onMaxPromptLengthChange,
3438 onLoadMore,
3539 hasMoreThreads,
3640 }: SidebarProps) {
@@ -44,7 +48,7 @@ export default function Sidebar({
4448 <BowtieIcon className="h-12 w-12" />
4549 </div>
4650 <div className="flex flex-col gap-2">
47- <SidebarButton
51+ <SecondaryButton
4852 href="#"
4953 onClick={(e) => {
5054 e.preventDefault();
@@ -53,8 +57,8 @@ export default function Sidebar({
5357 >
5458 <PlusIcon className="h-4 w-4" />
5559 New Thread
56- </SidebarButton>
57- <SidebarButton
60+ </SecondaryButton>
61+ <SecondaryButton
5862 href="#"
5963 onClick={(e) => {
6064 e.preventDefault();
@@ -63,7 +67,7 @@ export default function Sidebar({
6367 >
6468 <GearIcon className="h-4 w-4" />
6569 Settings
66- </SidebarButton>
70+ </SecondaryButton>
6771 <label className="text-neutral-500">Threads</label>
6872 <div className="flex flex-col gap-2">
6973 <div className="flex flex-col">
@@ -75,7 +79,7 @@ export default function Sidebar({
7579 threads.map((thread) => {
7680 const isSelected = currentThreadId === thread.id;
7781 return (
78- <SidebarButton
82+ <SecondaryButton
7983 key={thread.id}
8084 href={`#thread-${thread.id}`}
8185 onClick={(e) => {
@@ -87,14 +91,14 @@ export default function Sidebar({
8791 <span className="min-w-0 truncate">
8892 {thread.title || `Thread ${thread.id}`}
8993 </span>
90- </SidebarButton>
94+ </SecondaryButton>
9195 );
9296 })
9397 )}
9498 </div>
9599 </div>
96100 {hasMoreThreads && (
97- <SidebarButton
101+ <SecondaryButton
98102 href="#"
99103 className="text-xs"
100104 onClick={(e) => {
@@ -104,7 +108,7 @@ export default function Sidebar({
104108 >
105109 <ChevronDownIcon className="mt-0.5 h-3 w-3" />
106110 Load More
107- </SidebarButton>
111+ </SecondaryButton>
108112 )}
109113 </div>
110114 </div>
@@ -116,6 +120,8 @@ export default function Sidebar({
116120 onThemeChange={onThemeChange}
117121 selectedModel={selectedModel}
118122 onModelChange={onModelChange}
123+ maxPromptLength={maxPromptLength}
124+ onMaxPromptLengthChange={onMaxPromptLengthChange}
119125 />
120126 </>
121127 );
src/components/ThinkingNotification.tsx+29−0
@@ -0,0 +1,29 @@
1+"use client";
2+
3+import SecondaryButton from "@/src/components/buttons/SecondaryButton";
4+import StopIcon from "@/src/components/icons/StopIcon";
5+
6+type ThinkingNotificationProps = {
7+ onStop: () => void;
8+};
9+
10+export default function ThinkingNotification({ onStop }: ThinkingNotificationProps) {
11+ return (
12+ <div className="pointer-events-auto mb-2 flex w-fit items-center gap-2 rounded-lg bg-neutral-300/80 px-3 py-1 dark:bg-neutral-800/80">
13+ <span className="text-xs text-neutral-600 dark:text-neutral-400">
14+ <span className="animate-color-cycle">thinking</span>
15+ </span>
16+ <SecondaryButton
17+ href="#"
18+ className="text-xs"
19+ onClick={(e) => {
20+ e.preventDefault();
21+ onStop();
22+ }}
23+ >
24+ <StopIcon className="h-4 w-4" />
25+ Stop
26+ </SecondaryButton>
27+ </div>
28+ );
29+}
src/components/buttons/PrimaryButton.tsx+4−4
@@ -2,20 +2,20 @@
22
33 import type { ButtonHTMLAttributes, ReactNode } from "react";
44
5-type BasicButtonProps = ButtonHTMLAttributes<HTMLButtonElement> & {
5+type PrimaryButtonProps = ButtonHTMLAttributes<HTMLButtonElement> & {
66 children: ReactNode;
77 color?: string;
88 };
99
10-// BasicButton: A button with animated border effects and customizable color.
10+// PrimaryButton: A button with animated border effects and customizable color.
1111 // Features a bottom border that moves to top on active state, with rounded corners.
1212 // Color prop allows customization of the button's color scheme.
13-export default function BasicButton({
13+export default function PrimaryButton({
1414 children,
1515 color = "rose",
1616 className = "",
1717 ...props
18-}: BasicButtonProps) {
18+}: PrimaryButtonProps) {
1919 // Map color to Tailwind classes (using full class names for JIT compilation)
2020 const getColorClasses = (colorName: string) => {
2121 const colorMap: Record<string, string> = {
src/components/buttons/SecondaryButton.tsx+4−4
@@ -3,20 +3,20 @@
33 import type { AnchorHTMLAttributes, ReactNode } from "react";
44 import { Children, isValidElement } from "react";
55
6-type AnchorProps = AnchorHTMLAttributes<HTMLAnchorElement> & {
6+type SecondaryButtonProps = AnchorHTMLAttributes<HTMLAnchorElement> & {
77 children: ReactNode;
88 isSelected?: boolean;
99 };
1010
11-// SidebarButton: A reusable button component with neutral hover and selection states.
11+// SecondaryButton: A reusable button component with neutral hover and selection states.
1212 // When selected (isSelected=true), displays a neutral-800 background (dark) or neutral-200 (light).
1313 // On hover, displays a neutral-700 background (dark) or neutral-300 (light).
14-export default function SidebarButton({
14+export default function SecondaryButton({
1515 children,
1616 isSelected = false,
1717 className = "",
1818 ...props
19-}: AnchorProps) {
19+}: SecondaryButtonProps) {
2020 // Process children to wrap text nodes in a truncating span
2121 const processedChildren = Children.map(children, (child) => {
2222 if (typeof child === "string" || typeof child === "number") {
src/components/icons/StopIcon.tsx+12−0
@@ -0,0 +1,12 @@
1+export default function StopIcon({ className }: { className?: string }) {
2+ return (
3+ <svg
4+ xmlns="http://www.w3.org/2000/svg"
5+ viewBox="0 0 24 24"
6+ fill="currentColor"
7+ className={className}
8+ >
9+ <rect x="6" y="6" width="12" height="12" rx="1" />
10+ </svg>
11+ );
12+}
src/hooks/useMaxPromptLength.ts+36−0
@@ -0,0 +1,36 @@
1+import { useState, useEffect } from "react";
2+
3+export function useMaxPromptLength() {
4+ const [maxPromptLength, setMaxPromptLength] = useState<"none" | 1024 | 4096>("none");
5+
6+ useEffect(() => {
7+ const loadSettings = async () => {
8+ try {
9+ const res = await fetch("/api/settings");
10+ const data = await res.json();
11+ const settings = data.settings || { maxPromptLength: "none" };
12+ setMaxPromptLength(settings.maxPromptLength || "none");
13+ } catch (error) {
14+ console.error("Failed to load settings", error);
15+ setMaxPromptLength("none");
16+ }
17+ };
18+
19+ loadSettings();
20+ }, []);
21+
22+ const handleMaxPromptLengthChange = async (value: "none" | 1024 | 4096) => {
23+ setMaxPromptLength(value);
24+ try {
25+ await fetch("/api/settings", {
26+ method: "POST",
27+ headers: { "Content-Type": "application/json" },
28+ body: JSON.stringify({ maxPromptLength: value }),
29+ });
30+ } catch (error) {
31+ console.error("Failed to save settings", error);
32+ }
33+ };
34+
35+ return { maxPromptLength, handleMaxPromptLengthChange };
36+}
src/hooks/useMessages.ts+45−6
@@ -7,6 +7,7 @@ export function useMessages(threadId: number | null) {
77 const [isLoadingMessages, setIsLoadingMessages] = useState<boolean>(false);
88 const [streamingMessageId, setStreamingMessageId] = useState<number | null>(null);
99 const sendingToThreadIdRef = useRef<number | null>(null);
10+ const abortControllerRef = useRef<AbortController | null>(null);
1011
1112 const loadMessages = useCallback(async (id: number) => {
1213 setIsLoadingMessages(true);
@@ -37,10 +38,16 @@ export function useMessages(threadId: number | null) {
3738 const sendMessage = async (
3839 message: string,
3940 currentThreadId: number | null,
40- onCreateThread: (title?: string, firstMessage?: string, model?: string) => Promise<number | null>,
41+ onCreateThread: (
42+ title?: string,
43+ firstMessage?: string,
44+ model?: string,
45+ maxPromptLength?: "none" | 1024 | 4096
46+ ) => Promise<number | null>,
4147 onThreadSelect: (id: number) => void,
4248 onThreadsReload: () => void,
43- selectedModel: string = "gpt-oss"
49+ selectedModel: string = "gpt-oss",
50+ maxPromptLength?: "none" | 1024 | 4096
4451 ) => {
4552 if (!message.trim() || isLoading) return;
4653
@@ -50,7 +57,12 @@ export function useMessages(threadId: number | null) {
5057 if (!targetThreadId) {
5158 // Create a new thread if none exists
5259 // Use the validated selectedModel instead of reading from localStorage
53- targetThreadId = await onCreateThread(message.substring(0, 100), message, selectedModel);
60+ targetThreadId = await onCreateThread(
61+ message.substring(0, 100),
62+ message,
63+ selectedModel,
64+ maxPromptLength
65+ );
5466 if (!targetThreadId) {
5567 setIsLoading(false);
5668 return;
@@ -87,11 +99,15 @@ export function useMessages(threadId: number | null) {
8799 setMessages((prev) => [...prev, botMsg]);
88100 setStreamingMessageId(assistantMsgId);
89101
102+ // Create AbortController for this request
103+ abortControllerRef.current = new AbortController();
104+
90105 try {
91106 const res = await fetch("/api/chat", {
92107 method: "POST",
93108 headers: { "Content-Type": "application/json" },
94109 body: JSON.stringify({ message: message.trim(), threadId: targetThreadId }),
110+ signal: abortControllerRef.current.signal,
95111 });
96112
Cut short: this commit's diff is over 1000 lines.
src/hooks/useModel.ts+2−2
Cut short: this commit's diff is over 1000 lines.
src/hooks/useThreads.ts+21−2
Cut short: this commit's diff is over 1000 lines.
src/lib/db/schema.ts+1−0
Cut short: this commit's diff is over 1000 lines.
src/lib/ollama/duckduckgo.ts+0−1
Cut short: this commit's diff is over 1000 lines.
src/lib/ollama/index.ts+29−2
Cut short: this commit's diff is over 1000 lines.
src/lib/ollama/weather.ts+0−2
Cut short: this commit's diff is over 1000 lines.