xonecas.io
53129
Pa

PAT / commit 343b043

feat: model selection setting

xonecas2025-12-17 343b043baea65cd58af3fad722f61df549381cad parent 3242dfe

18 files changed

app/api/chat/route.ts+19−7
@@ -12,14 +12,25 @@ export async function POST(request: Request) {
1212 }
1313
1414 try {
15- // 1. Fetch previous messages from thread
15+ // 1. Fetch thread to get its model
16+ const thread = await db
17+ .select()
18+ .from(threads)
19+ .where(eq(threads.id, parseInt(threadId)))
20+ .limit(1);
21+ if (thread.length === 0) {
22+ return NextResponse.json({ error: "Thread not found" }, { status: 404 });
23+ }
24+ const threadModel = thread[0].model || "gpt-oss";
25+
26+ // 2. Fetch previous messages from thread
1627 const previousMessages = await db
1728 .select()
1829 .from(messages)
1930 .where(eq(messages.threadId, parseInt(threadId)))
2031 .orderBy(asc(messages.createdAt));
2132
22- // 2. Store user message
33+ // 3. Store user message
2334 await db.insert(messages).values({
2435 threadId: parseInt(threadId),
2536 role: "user",
@@ -27,7 +38,7 @@ export async function POST(request: Request) {
2738 createdAt: new Date(),
2839 });
2940
30- // 3. Build messages array for Ollama
41+ // 4. Build messages array for Ollama
3142 const ollamaMessages: OllamaMessageInput[] = previousMessages.map((msg) => ({
3243 role: msg.role as "user" | "assistant" | "system",
3344 content: msg.content,
@@ -39,7 +50,7 @@ export async function POST(request: Request) {
3950 content: message,
4051 });
4152
42- // 4. Stream assistant response
53+ // 5. Stream assistant response
4354 let accumulatedContent = "";
4455 let accumulatedThinking = "";
4556 const allToolCalls: any[] = [];
@@ -77,10 +88,11 @@ export async function POST(request: Request) {
7788
7889 const { generationTimeMs, toolCalls } = await fetchOllamaResponse(
7990 ollamaMessages,
80- onChunk
91+ onChunk,
92+ threadModel
8193 );
8294
83- // 5. Store assistant message with generation time and tool calls
95+ // 6. Store assistant message with generation time and tool calls
8496 await db.insert(messages).values({
8597 threadId: parseInt(threadId),
8698 role: "assistant",
@@ -90,7 +102,7 @@ export async function POST(request: Request) {
90102 toolCalls: toolCalls ? JSON.stringify(toolCalls) : null,
91103 });
92104
93- // 6. Update thread's updatedAt timestamp
105+ // 7. Update thread's updatedAt timestamp
94106 await db
95107 .update(threads)
96108 .set({ updatedAt: new Date() })
app/api/models/route.ts+40−0
@@ -0,0 +1,40 @@
1+import { NextResponse } from "next/server";
2+import ollama from "ollama";
3+
4+/**
5+ * Model response from Ollama API
6+ * Based on OpenAPI spec: /api/tags returns { models: ModelSummary[] }
7+ */
8+interface ModelSummary {
9+ name: string;
10+ modified_at?: string;
11+ size?: number;
12+ digest?: string;
13+ details?: {
14+ format?: string;
15+ family?: string;
16+ families?: string[];
17+ parameter_size?: string;
18+ quantization_level?: string;
19+ };
20+}
21+
22+interface ListResponse {
23+ models: ModelSummary[];
24+}
25+
26+export async function GET() {
27+ try {
28+ const response = (await ollama.list()) as ListResponse;
29+ // ollama.list() returns { models: ModelSummary[] }
30+ // Each model has a 'name' property that is used as the model identifier
31+ const models = (response.models || []).map((model: ModelSummary) => ({
32+ name: model.name,
33+ model: model.name, // Use name as the model identifier
34+ }));
35+ return NextResponse.json({ models });
36+ } catch (error) {
37+ console.error("Failed to fetch models", error);
38+ return NextResponse.json({ error: "Failed to fetch models" }, { status: 500 });
39+ }
40+}
app/api/threads/route.ts+2−1
@@ -5,11 +5,12 @@ import { desc, count } from "drizzle-orm";
55
66 export async function POST(request: Request) {
77 try {
8- const { title } = await request.json();
8+ const { title, model } = await request.json();
99 const newThread = await db
1010 .insert(threads)
1111 .values({
1212 title: title || "New Thread",
13+ model: model || "gpt-oss",
1314 createdAt: new Date(),
1415 updatedAt: new Date(),
1516 })
app/page.tsx+11−0
@@ -5,6 +5,7 @@ import MessageInput, { type MessageInputRef } from "@/src/components/MessageInpu
55 import MessageList from "@/src/components/MessageList";
66 import NoThreadSelected from "@/src/components/NoThreadSelected";
77 import { useTheme } from "@/src/hooks/useTheme";
8+import { useModel } from "@/src/hooks/useModel";
89 import { useThreads } from "@/src/hooks/useThreads";
910 import { useMessages } from "@/src/hooks/useMessages";
1011 import { useThreadSelection } from "@/src/hooks/useThreadSelection";
@@ -12,6 +13,7 @@ import "./highlight-theme.css";
1213
1314 export default function Home() {
1415 const { themeMode, handleThemeChange } = useTheme();
16+ const { selectedModel, handleModelChange } = useModel();
1517 const { threads, totalThreadCount, hasMoreThreads, loadThreads, loadMoreThreads, createThread } =
1618 useThreads();
1719 const { currentThreadId, selectThread, deselectThread, messagesContainerRef } =
@@ -37,6 +39,12 @@ export default function Home() {
3739 await sendMessage(message, currentThreadId, createThread, selectThread, loadThreads);
3840 };
3941
42+ // Get the model name for the current thread, or use selected model if no thread
43+ const currentModelName =
44+ currentThreadId !== null
45+ ? threads.find((t) => t.id === currentThreadId)?.model || selectedModel || "gpt-oss"
46+ : selectedModel || "gpt-oss";
47+
4048 return (
4149 <div className="flex h-screen bg-neutral-100 text-neutral-800 dark:bg-neutral-950 dark:text-neutral-200">
4250 <div className="relative flex min-w-0 flex-1 flex-col">
@@ -60,6 +68,7 @@ export default function Home() {
6068 isLoadingMessages={isLoadingMessages}
6169 messages={messages}
6270 onSubmit={handleSendMessage}
71+ modelName={currentModelName}
6372 />
6473 </div>
6574 <Sidebar
@@ -69,6 +78,8 @@ export default function Home() {
6978 onCreateNewThread={handleCreateNewThread}
7079 onThreadSelect={handleThreadSelect}
7180 onThemeChange={handleThemeChange}
81+ selectedModel={selectedModel}
82+ onModelChange={handleModelChange}
7283 onLoadMore={loadMoreThreads}
7384 hasMoreThreads={hasMoreThreads}
7485 />
drizzle/0001_lethal_madripoor.sql+1−0
@@ -0,0 +1 @@
1+ALTER TABLE "threads" ADD COLUMN "model" text DEFAULT 'gpt-oss' NOT NULL;
drizzle/meta/0001_snapshot.json+135−0
@@ -0,0 +1,135 @@
1+{
2+ "id": "610bf0a7-5793-4b66-ad2e-7e4fece27494",
3+ "prevId": "34bac836-2b5d-4ea5-aeaf-a7e999466aa5",
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+ "created_at": {
101+ "name": "created_at",
102+ "type": "timestamp with time zone",
103+ "primaryKey": false,
104+ "notNull": true,
105+ "default": "now()"
106+ },
107+ "updated_at": {
108+ "name": "updated_at",
109+ "type": "timestamp with time zone",
110+ "primaryKey": false,
111+ "notNull": true,
112+ "default": "now()"
113+ }
114+ },
115+ "indexes": {},
116+ "foreignKeys": {},
117+ "compositePrimaryKeys": {},
118+ "uniqueConstraints": {},
119+ "policies": {},
120+ "checkConstraints": {},
121+ "isRLSEnabled": false
122+ }
123+ },
124+ "enums": {},
125+ "schemas": {},
126+ "sequences": {},
127+ "roles": {},
128+ "policies": {},
129+ "views": {},
130+ "_meta": {
131+ "columns": {},
132+ "schemas": {},
133+ "tables": {}
134+ }
135+}
drizzle/meta/_journal.json+7−0
@@ -8,6 +8,13 @@
88 "when": 1766005179126,
99 "tag": "0000_burly_pixie",
1010 "breakpoints": true
11+ },
12+ {
13+ "idx": 1,
14+ "version": "7",
15+ "when": 1766007076801,
16+ "tag": "0001_lethal_madripoor",
17+ "breakpoints": true
1118 }
1219 ]
1320 }
src/components/MessageInput.tsx+4−2
@@ -10,6 +10,7 @@ type MessageInputProps = {
1010 isLoadingMessages: boolean;
1111 messages: Message[];
1212 onSubmit: (message: string) => void;
13+ modelName?: string;
1314 };
1415
1516 export type MessageInputRef = {
@@ -17,7 +18,7 @@ export type MessageInputRef = {
1718 };
1819
1920 const MessageInput = forwardRef<MessageInputRef, MessageInputProps>(
20- ({ isLoading, isLoadingMessages, messages, onSubmit }, ref) => {
21+ ({ isLoading, isLoadingMessages, messages, onSubmit, modelName = "gpt-oss" }, ref) => {
2122 const [input, setInput] = useState("");
2223 const [historyIndex, setHistoryIndex] = useState<number | null>(null);
2324 const textareaRef = useRef<HTMLTextAreaElement>(null);
@@ -121,7 +122,8 @@ const MessageInput = forwardRef<MessageInputRef, MessageInputProps>(
121122 rows={3}
122123 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"
123124 />
124- <div className="absolute bottom-2 right-2">
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>
125127 <BasicButton color="neutral" type="submit" disabled={isAnyLoading}>
126128 <PaperPlaneIcon
127129 className={`h-8 w-10 p-1.5 ${isAnyLoading ? "animate-spin-and-color-cycle" : ""}`}
src/components/SettingsModal.tsx+58−11
@@ -1,5 +1,6 @@
11 "use client";
22
3+import { useState, useEffect } from "react";
34 import Modal from "@/src/components/Modal";
45
56 type SettingsModalProps = {
@@ -7,6 +8,8 @@ type SettingsModalProps = {
78 onClose: () => void;
89 themeMode: "device" | "dark" | "light";
910 onThemeChange: (mode: "device" | "dark" | "light") => void;
11+ selectedModel: string;
12+ onModelChange: (model: string) => void;
1013 };
1114
1215 export default function SettingsModal({
@@ -14,23 +17,67 @@ export default function SettingsModal({
1417 onClose,
1518 themeMode,
1619 onThemeChange,
20+ selectedModel,
21+ onModelChange,
1722 }: SettingsModalProps) {
23+ const [models, setModels] = useState<Array<{ name: string; model: string }>>([]);
24+ const [isLoadingModels, setIsLoadingModels] = useState(false);
25+
26+ useEffect(() => {
27+ if (isOpen) {
28+ setIsLoadingModels(true);
29+ fetch("/api/models")
30+ .then((res) => res.json())
31+ .then((data) => {
32+ setModels(data.models || []);
33+ setIsLoadingModels(false);
34+ })
35+ .catch((error) => {
36+ console.error("Failed to load models", error);
37+ setIsLoadingModels(false);
38+ });
39+ }
40+ }, [isOpen]);
41+
1842 return (
1943 <Modal isOpen={isOpen} onClose={onClose}>
2044 <h2 className="mb-4 text-xl font-semibold text-neutral-800 dark:text-neutral-200">
2145 Settings
2246 </h2>
23- <div className="flex flex-col gap-2">
24- <label className="text-neutral-600 dark:text-neutral-400">Theme</label>
25- <select
26- value={themeMode}
27- onChange={(e) => onThemeChange(e.target.value as "device" | "dark" | "light")}
28- className="bg-neutral-100 px-3 py-1 text-neutral-800 focus:outline-none dark:bg-neutral-900 dark:text-neutral-200"
29- >
30- <option value="device">Device</option>
31- <option value="dark">Dark</option>
32- <option value="light">Light</option>
33- </select>
47+ <div className="flex flex-col gap-4">
48+ <div className="flex flex-col gap-2">
49+ <label className="text-neutral-600 dark:text-neutral-400">Theme</label>
50+ <select
51+ value={themeMode}
52+ onChange={(e) => onThemeChange(e.target.value as "device" | "dark" | "light")}
53+ className="bg-neutral-100 px-3 py-1 text-neutral-800 focus:outline-none dark:bg-neutral-900 dark:text-neutral-200"
54+ >
55+ <option value="device">Device</option>
56+ <option value="dark">Dark</option>
57+ <option value="light">Light</option>
58+ </select>
59+ </div>
60+ <div className="flex flex-col gap-2">
61+ <label className="text-neutral-600 dark:text-neutral-400">Model</label>
62+ <select
63+ value={selectedModel}
64+ onChange={(e) => onModelChange(e.target.value)}
65+ disabled={isLoadingModels}
66+ className="bg-neutral-100 px-3 py-1 text-neutral-800 focus:outline-none disabled:opacity-50 dark:bg-neutral-900 dark:text-neutral-200"
67+ >
68+ {isLoadingModels ? (
69+ <option>Loading models...</option>
70+ ) : models.length === 0 ? (
71+ <option>No models available</option>
72+ ) : (
73+ models.map((model) => (
74+ <option key={model.name} value={model.model}>
75+ {model.name}
76+ </option>
77+ ))
78+ )}
79+ </select>
80+ </div>
3481 </div>
3582 </Modal>
3683 );
src/components/Sidebar.tsx+6−0
@@ -16,6 +16,8 @@ type SidebarProps = {
1616 onCreateNewThread: () => void;
1717 onThreadSelect: (threadId: number) => void;
1818 onThemeChange: (mode: "device" | "dark" | "light") => void;
19+ selectedModel: string;
20+ onModelChange: (model: string) => void;
1921 onLoadMore: () => void;
2022 hasMoreThreads: boolean;
2123 };
@@ -27,6 +29,8 @@ export default function Sidebar({
2729 onCreateNewThread,
2830 onThreadSelect,
2931 onThemeChange,
32+ selectedModel,
33+ onModelChange,
3034 onLoadMore,
3135 hasMoreThreads,
3236 }: SidebarProps) {
@@ -110,6 +114,8 @@ export default function Sidebar({
110114 onClose={() => setIsSettingsOpen(false)}
111115 themeMode={themeMode}
112116 onThemeChange={onThemeChange}
117+ selectedModel={selectedModel}
118+ onModelChange={onModelChange}
113119 />
114120 </>
115121 );
src/hooks/useMessages.ts+2−1
@@ -48,7 +48,8 @@ export function useMessages(threadId: number | null) {
4848
4949 if (!targetThreadId) {
5050 // Create a new thread if none exists
51- targetThreadId = await onCreateThread(message.substring(0, 100), message);
51+ const model = localStorage.getItem("selectedModel") || "gpt-oss";
52+ targetThreadId = await onCreateThread(message.substring(0, 100), message, model);
5253 if (!targetThreadId) {
5354 setIsLoading(false);
5455 return;
src/hooks/useModel.ts+19−0
@@ -0,0 +1,19 @@
1+import { useState, useEffect } from "react";
2+
3+export function useModel() {
4+ const [selectedModel, setSelectedModel] = useState<string>("gpt-oss");
5+
6+ useEffect(() => {
7+ const savedModel = localStorage.getItem("selectedModel");
8+ if (savedModel) {
9+ setSelectedModel(savedModel);
10+ }
11+ }, []);
12+
13+ const handleModelChange = (model: string) => {
14+ setSelectedModel(model);
15+ localStorage.setItem("selectedModel", model);
16+ };
17+
18+ return { selectedModel, handleModelChange };
19+}
src/hooks/useThreads.ts+4−2
@@ -38,14 +38,16 @@ export function useThreads() {
3838
3939 const createThread = async (
4040 titleOverride?: string,
41- firstMessage?: string
41+ firstMessage?: string,
42+ model?: string
4243 ): Promise<number | null> => {
4344 try {
4445 const title = titleOverride || (firstMessage ? firstMessage.substring(0, 100) : "New Thread");
46+ const modelToUse = model || localStorage.getItem("selectedModel") || "gpt-oss";
4547 const res = await fetch("/api/threads", {
4648 method: "POST",
4749 headers: { "Content-Type": "application/json" },
48- body: JSON.stringify({ title }),
50+ body: JSON.stringify({ title, model: modelToUse }),
4951 });
5052 const data = await res.json();
5153 const newThread = data.thread;
src/lib/db/schema.ts+1−0
@@ -5,6 +5,7 @@ import { relations } from "drizzle-orm";
55 export const threads = pgTable("threads", {
66 id: serial("id").primaryKey(),
77 title: text("title"),
8+ model: text("model").notNull().default("gpt-oss"),
89 createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
910 updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(),
1011 });
src/lib/ollama/duckduckgo.ts+4−0
@@ -24,6 +24,10 @@ const duckDuckGoRateLimiter = createRateLimiter({
2424 * @returns A formatted string containing the instant answer information.
2525 */
2626 export async function queryDuckDuckGo(query: string): Promise<string> {
27+ if (!query || typeof query !== "string") {
28+ return `Error: Invalid query parameter. Expected a non-empty string, got: ${typeof query}`;
29+ }
30+
2731 // Check cache first
2832 const cacheKey = `duckduckgo:${query.toLowerCase().trim()}`;
2933 const cached = await getCache<string>(cacheKey);
src/lib/ollama/index.ts+15−3
@@ -49,13 +49,25 @@ async function executeToolCall(toolCall: {
4949 let result: string;
5050 switch (name) {
5151 case "query_duckduckgo":
52- result = await queryDuckDuckGo(parsedArgs.query);
52+ if (!parsedArgs.query || typeof parsedArgs.query !== "string") {
53+ result = `Error: Missing or invalid "query" parameter for query_duckduckgo`;
54+ } else {
55+ result = await queryDuckDuckGo(parsedArgs.query);
56+ }
5357 break;
5458 case "query_weather":
55- result = await queryWeather(parsedArgs.location);
59+ if (!parsedArgs.location || typeof parsedArgs.location !== "string") {
60+ result = `Error: Missing or invalid "location" parameter for query_weather`;
61+ } else {
62+ result = await queryWeather(parsedArgs.location);
63+ }
5664 break;
5765 case "query_web_search":
58- result = await queryWebSearch(parsedArgs.query);
66+ if (!parsedArgs.query || typeof parsedArgs.query !== "string") {
67+ result = `Error: Missing or invalid "query" parameter for query_web_search`;
68+ } else {
69+ result = await queryWebSearch(parsedArgs.query);
70+ }
5971 break;
6072 default:
6173 debug(`[Tool] Unknown tool: ${name}`);
src/lib/ollama/weather.ts+4−0
@@ -63,6 +63,10 @@ function getWeatherDescription(code: number): string {
6363 * @returns A formatted string containing weather information.
6464 */
6565 export async function queryWeather(location: string): Promise<string> {
66+ if (!location || typeof location !== "string") {
67+ return `Error: Invalid location parameter. Expected a non-empty string, got: ${typeof location}`;
68+ }
69+
6670 // Check cache first
6771 const cacheKey = `weather:${location.toLowerCase().trim()}`;
6872 const cached = await getCache<string>(cacheKey);
src/lib/ollama/websearch.ts+4−0
@@ -26,6 +26,10 @@ const webSearchRateLimiter = createRateLimiter({
2626 * @returns A formatted string containing search results.
2727 */
2828 export async function queryWebSearch(query: string): Promise<string> {
29+ if (!query || typeof query !== "string") {
30+ return `Error: Invalid query parameter. Expected a non-empty string, got: ${typeof query}`;
31+ }
32+
2933 // Check cache first
3034 const cacheKey = `websearch:${query.toLowerCase().trim()}`;
3135 const cached = await getCache<string>(cacheKey);