xonecas.io
53129
Pa

PAT / commit 528d87c

chore: backend code cleanup

xonecas2025-12-18 528d87c776f3c8f8142e092a39f076d0298ffe2e parent a004f7c

10 files changed

app/api/chat/route.ts+5−5
@@ -1,9 +1,9 @@
11 import { NextResponse } from "next/server";
2-import {
3- type OllamaMessageInput,
4- type OllamaMessageRole,
5- type MaxPromptLength,
6-} from "@/src/lib/ollama";
2+import type {
3+ OllamaMessageInput,
4+ OllamaMessageRole,
5+ MaxPromptLength,
6+} from "@/src/lib/ollama/types";
77 import { db } from "@/src/lib/db";
88 import { messages, threads } from "@/src/lib/db/schema";
99 import { eq, asc } from "drizzle-orm";
app/api/models/route.ts+0−2
@@ -4,8 +4,6 @@ import ollama from "ollama";
44 export async function GET() {
55 try {
66 const response = await ollama.list();
7- // ollama.list() returns { models: ModelResponse[] }
8- // Each model has a 'name' property that is used as the model identifier
97 const models = (response.models || []).map((model) => ({
108 name: model.name,
119 model: model.name, // Use name as the model identifier
app/api/settings/route.ts+26−11
@@ -1,5 +1,6 @@
11 import { NextResponse } from "next/server";
22 import { getCache, setCache } from "@/src/lib/cache";
3+import { getFirstAvailableModel } from "@/src/lib/ollama/models";
34
45 const SETTINGS_CACHE_KEY = "app_settings";
56
@@ -11,9 +12,18 @@ type Settings = {
1112 export async function GET() {
1213 try {
1314 const settings = await getCache<Settings>(SETTINGS_CACHE_KEY);
14- return NextResponse.json({
15- settings: settings || { maxPromptLength: "none", selectedModel: "gpt-oss" },
16- });
15+ if (settings) {
16+ return NextResponse.json({ settings });
17+ }
18+
19+ // If no settings exist, get the first available model
20+ const firstModel = await getFirstAvailableModel();
21+ const defaultSettings: Settings = {
22+ maxPromptLength: "none",
23+ selectedModel: firstModel || undefined,
24+ };
25+
26+ return NextResponse.json({ settings: defaultSettings });
1727 } catch {
1828 return NextResponse.json({ error: "Failed to get settings" }, { status: 500 });
1929 }
@@ -23,7 +33,7 @@ export async function POST(request: Request) {
2333 try {
2434 const { maxPromptLength, selectedModel } = await request.json();
2535
26- const settings: Partial<Settings> = {};
36+ let settings: Partial<Settings> = {};
2737
2838 // Validate and set maxPromptLength if provided
2939 if (maxPromptLength !== undefined) {
@@ -41,16 +51,21 @@ export async function POST(request: Request) {
4151 settings.selectedModel = selectedModel;
4252 }
4353
44- // Get existing settings and merge
54+ // Get existing settings and merge, prefering the ones in the request
4555 const existingSettings = await getCache<Settings>(SETTINGS_CACHE_KEY);
46- const mergedSettings: Settings = {
47- maxPromptLength: settings.maxPromptLength ?? existingSettings?.maxPromptLength ?? "none",
48- selectedModel: settings.selectedModel ?? existingSettings?.selectedModel ?? "gpt-oss",
49- };
56+ if (existingSettings) {
57+ settings = {...existingSettings, ...settings}
58+ }
5059
51- await setCache(SETTINGS_CACHE_KEY, mergedSettings);
60+ if (!settings.selectedModel) {
61+ const firstModel = await getFirstAvailableModel();
62+ settings.selectedModel = firstModel
63+ }
64+
65+ // Update the cache with new settings.
66+ await setCache(SETTINGS_CACHE_KEY, settings);
5267
53- return NextResponse.json({ settings: mergedSettings });
68+ return NextResponse.json({ settings: settings });
5469 } catch {
5570 return NextResponse.json({ error: "Failed to set settings" }, { status: 500 });
5671 }
app/page.tsx+0−2
@@ -44,7 +44,6 @@ export default function Home() {
4444 deleteMessageInternal(messageId, threadId, setError);
4545 };
4646
47-
4847 // Auto-dismiss error after 3 seconds
4948 useEffect(() => {
5049 if (error) {
@@ -123,7 +122,6 @@ export default function Home() {
123122 }
124123 };
125124
126-
127125 return (
128126 <div className="flex h-screen bg-neutral-100 text-neutral-800 dark:bg-neutral-950 dark:text-neutral-200">
129127 <div className="relative flex min-w-0 flex-1 flex-col">
src/components/MessageList/index.tsx+1−5
@@ -12,11 +12,7 @@ type MessageListProps = {
1212 onDeleteMessage: (messageId: number, threadId: number) => void;
1313 };
1414
15-export default function MessageList({
16- messages,
17- threadId,
18- onDeleteMessage,
19-}: MessageListProps) {
15+export default function MessageList({ messages, threadId, onDeleteMessage }: MessageListProps) {
2016 const messageRefs = useRef<Map<number, HTMLDivElement>>(new Map());
2117 const prevMessagesLengthRef = useRef<number>(0);
2218
src/lib/chat.ts+11−7
@@ -1,10 +1,10 @@
11 /* personal-assistant-thing/src/lib/chat.ts */
2-import {
3- fetchOllamaResponse,
4- type OllamaMessageInput,
5- type MaxPromptLength,
6- type ToolCall,
7-} from "@/src/lib/ollama";
2+import { fetchOllamaResponse } from "@/src/lib/ollama";
3+import type {
4+ OllamaMessageInput,
5+ MaxPromptLength,
6+ ToolCall,
7+} from "@/src/lib/ollama/types";
88 import { db } from "@/src/lib/db";
99 import { messages, threads } from "@/src/lib/db/schema";
1010 import { eq } from "drizzle-orm";
@@ -63,7 +63,11 @@ export function streamAssistantResponse({
6363 return;
6464 }
6565
66- const onChunk = (chunk: { content?: string; thinking?: string; toolCalls?: ToolCall[] }) => {
66+ const onChunk = (chunk: {
67+ content?: string;
68+ thinking?: string;
69+ toolCalls?: ToolCall[];
70+ }) => {
6771 // Check abort signal before each chunk
6872 if (signal.aborted || isControllerClosed) {
6973 return;
src/lib/ollama/index.ts+36−17
@@ -2,10 +2,10 @@
22 // Wrapper using the official Ollama npm package.
33
44 import ollama from "ollama";
5-import { debug } from "../debug";
6-import { duckDuckGoTool, queryDuckDuckGo } from "./duckduckgo";
7-import { queryWeather, weatherTool } from "./weather";
8-import { queryWebSearch, webSearchTool } from "./websearch";
5+import { debug } from "@/src/lib/debug";
6+import { duckDuckGoTool, queryDuckDuckGo } from "@/src/lib/ollama/duckduckgo";
7+import { queryWeather, weatherTool } from "@/src/lib/ollama/weather";
8+import { queryWebSearch, webSearchTool } from "@/src/lib/ollama/websearch";
99 import type {
1010 OllamaMessage,
1111 OllamaMessageInput,
@@ -13,18 +13,9 @@ import type {
1313 OllamaResponse,
1414 MaxPromptLength,
1515 ToolCall,
16-} from "./types";
16+ OllamaChatMessage,
17+} from "@/src/lib/ollama/types";
1718
18-// Re-export types for convenience
19-export type {
20- OllamaMessage,
21- OllamaMessageRole,
22- OllamaMessageInput,
23- OllamaChunk,
24- OllamaResponse,
25- MaxPromptLength,
26- ToolCall,
27-} from "./types";
2819
2920 /**
3021 * Executes a tool call and returns the result.
@@ -131,9 +122,35 @@ export async function fetchOllamaResponse(
131122 throw new Error("Request aborted");
132123 }
133124
125+ // Ensure all messages have content as a string and tool_calls have proper format (required by ollama.chat)
126+ const messagesForOllama: OllamaChatMessage[] = currentMessages.map((msg) => {
127+ const { tool_calls, ...msgWithoutToolCalls } = msg;
128+ const message: OllamaChatMessage = {
129+ ...msgWithoutToolCalls,
130+ content: msg.content ?? "",
131+ };
132+
133+ // Transform tool_calls to ensure arguments is always an object
134+ if (tool_calls) {
135+ message.tool_calls = tool_calls.map((tc) => ({
136+ id: tc.id,
137+ type: tc.type,
138+ function: {
139+ name: tc.function.name,
140+ arguments:
141+ typeof tc.function.arguments === "string"
142+ ? JSON.parse(tc.function.arguments)
143+ : tc.function.arguments,
144+ },
145+ }));
146+ }
147+
148+ return message;
149+ });
150+
134151 const stream = await ollama.chat({
135152 model,
136- messages: currentMessages,
153+ messages: messagesForOllama,
137154 tools,
138155 stream: true,
139156 options: Object.keys(options).length > 0 ? options : undefined,
@@ -170,7 +187,9 @@ export async function fetchOllamaResponse(
170187 // If tool call has an ID, check if we already have it
171188 const toolCallWithId = toolCall as ToolCall & { id?: string };
172189 if (toolCallWithId.id) {
173- const existingIndex = toolCalls.findIndex((tc: ToolCall) => tc.id === toolCallWithId.id);
190+ const existingIndex = toolCalls.findIndex(
191+ (tc: ToolCall) => tc.id === toolCallWithId.id
192+ );
174193 if (existingIndex >= 0) {
175194 toolCalls[existingIndex] = toolCall as ToolCall;
176195 } else {
src/lib/ollama/models.ts+19−0
@@ -0,0 +1,19 @@
1+/* personal-assistant-thing/src/lib/ollama/models.ts */
2+// Utility functions for working with Ollama models
3+
4+import ollama from "ollama";
5+
6+/**
7+ * Gets the first available model from Ollama.
8+ * @returns The name of the first model, or null if no models are available.
9+ */
10+export async function getFirstAvailableModel(): Promise<string | null> {
11+ try {
12+ const response = await ollama.list();
13+ const models = response.models || [];
14+ return models.length > 0 ? models[0].name : null;
15+ } catch {
16+ return null;
17+ }
18+}
19+
src/lib/ollama/types.ts+19−0
@@ -20,6 +20,25 @@ export interface ToolCall {
2020 };
2121 }
2222
23+// Tool call format required by ollama.chat() - arguments must be an object
24+export interface OllamaChatToolCall {
25+ id?: string;
26+ type?: "function";
27+ function: {
28+ name: string;
29+ arguments: { [key: string]: unknown };
30+ };
31+}
32+
33+// Message format required by ollama.chat() - content is required and tool_calls use OllamaChatToolCall
34+export interface OllamaChatMessage {
35+ role: "user" | "assistant" | "system" | "tool";
36+ content: string;
37+ thinking?: string;
38+ tool_calls?: OllamaChatToolCall[];
39+ tool_name?: string;
40+}
41+
2342 export interface OllamaMessageInput {
2443 role: OllamaMessageRole;
2544 content: string;
tsconfig.tsbuildinfo+1−1
@@ -1 +1 @@
1-{"fileNames":["./node_modules/typescript/lib/lib.es5.d.ts","./node_modules/typescript/lib/lib.es2015.d.ts","./node_modules/typescript/lib/lib.es2016.d.ts","./node_modules/typescript/lib/lib.es2017.d.ts","./node_modules/typescript/lib/lib.es2018.d.ts","./node_modules/typescript/lib/lib.es2019.d.ts","./node_modules/typescript/lib/lib.es2020.d.ts","./node_modules/typescript/lib/lib.es2021.d.ts","./node_modules/typescript/lib/lib.es2022.d.ts","./node_modules/typescript/lib/lib.es2023.d.ts","./node_m …
1+{"fileNames":["./node_modules/typescript/lib/lib.es5.d.ts","./node_modules/typescript/lib/lib.es2015.d.ts","./node_modules/typescript/lib/lib.es2016.d.ts","./node_modules/typescript/lib/lib.es2017.d.ts","./node_modules/typescript/lib/lib.es2018.d.ts","./node_modules/typescript/lib/lib.es2019.d.ts","./node_modules/typescript/lib/lib.es2020.d.ts","./node_modules/typescript/lib/lib.es2021.d.ts","./node_modules/typescript/lib/lib.es2022.d.ts","./node_modules/typescript/lib/lib.es2023.d.ts","./node_m …