xonecas.io
53129
Pa

PAT / commit a3e2f6c

chore: cleanup and small fixes

xonecas2025-12-16 a3e2f6c85a24273afb9076a9ce3755e6aebacb0b parent 351b753

9 files changed

AGENTS.md+19−23
@@ -1,6 +1,6 @@
11 # AGENTS.md
22
3-This is a personal assistant project, that renders a simple assistant chat layout as a web page, and provides access to an agentic model running locally via ollama. The project uses Next.js for it's typescript framework TailwindCSS for it's styling, and Ollama's typescript/javascript library itself for querying the model. This project is also responsible for augmenting the model's prompt with search results from DuckDuckGo.
3+This is a personal assistant project, that renders a simple assistant chat layout as a web page, and provides access to an agentic model running locally via ollama. The project uses Next.js for it's typescript framework TailwindCSS for it's styling, and Ollama's typescript/javascript library itself for querying the model.
44
55 ## Project Commands
66
@@ -73,7 +73,7 @@ Agents MUST use only these existing dependencies. Do NOT add new dependencies wi
7373 3. **Naming Conventions**:
7474 - Functions: `camelCase` (e.g., `fetchOllamaResponse`, `sendMessage`)
7575 - Components: `PascalCase` (e.g., `Home`, `RootLayout`)
76- - Interfaces: `PascalCase` (e.g., `DuckDuckGoResult`)
76+ - Interfaces: `PascalCase` (e.g., `OllamaMessage`, `OllamaResponse`)
7777 - Constants: `UPPER_SNAKE_CASE` or `camelCase` depending on scope
7878
7979 4. **TypeScript Types**:
@@ -137,7 +137,6 @@ Agents MUST use only these existing dependencies. Do NOT add new dependencies wi
137137 │ ├── db/
138138 │ │ ├── index.ts # Database connection and setup
139139 │ │ └── schema.ts # Drizzle ORM schema definitions
140-│ ├── duckduckgo.ts # DuckDuckGo API wrapper
141140 │ └── ollama.ts # Ollama API wrapper
142141 ├── drizzle/ # Generated migration files
143142 │ ├── meta/ # Migration metadata
@@ -155,29 +154,25 @@ Agents MUST use only these existing dependencies. Do NOT add new dependencies wi
155154 ### UI Style
156155
157156 - Always ensure support for both light and dark modes
158-- Use slate color palette for backgrounds and text (not gray or stone)
159-- Default text colors: `text-slate-800 dark:text-slate-200` (set on topmost container to cascade)
160-- Background colors: `bg-slate-200/300` for light mode, `bg-slate-900/950` for dark mode
157+- Use stone color palette for backgrounds and text (not gray or slate)
158+- Default text colors: `text-stone-800 dark:text-stone-200` (set on topmost container to cascade)
159+- Container backgrounds: `bg-stone-100` for light mode, `bg-stone-950` for dark mode
160+- Child component backgrounds: `bg-stone-200` for light mode, `bg-stone-900` for dark mode
161+- User message backgrounds: `bg-stone-300` for light mode, `bg-stone-800` for dark mode
162+- Assistant message backgrounds: `bg-stone-200` for light mode, `bg-stone-900` for dark mode
161163 - Don't use round corners
162164 - The UI should always fit to the available space
163-- Primary colors: emerald-500 for dark mode, emerald-900 for light mode
164-- Alternate colors: indigo at an appropriate shade for current mode
165+- Primary colors: green-500 for dark mode, green-900 for light mode
166+- margins and paddings shouldn't stack, use gaps when possible
165167
166168 ### Ollama Integration
167169
168170 - Default model: `gpt-oss`
169-- Function: `fetchOllamaResponse(prompt: string, model?: string)`
170-- Returns: Promise<string> with the model's response
171+- Function: `fetchOllamaResponse(messages: OllamaMessage[], model?: string)`
172+- Returns: Promise<OllamaResponse> with `{ content: string, generationTimeMs: number }`
173+- Uses `ollama.chat()` API with full conversation history
171174 - Located in: `src/lib/ollama.ts`
172175
173-### DuckDuckGo Integration
174-
175-- Endpoint: `https://api.duckduckgo.com/`
176-- Function: `fetchDuckDuckGo(query: string)`
177-- Returns: Promise<DuckDuckGoResult[]>
178-- Interface: `{ title: string; snippet: string; url: string }`
179-- Located in: `src/lib/duckduckgo.ts`
180-
181176 ### Database Integration (Drizzle ORM)
182177
183178 - **Database**: SQLite using better-sqlite3
@@ -196,11 +191,12 @@ Agents MUST use only these existing dependencies. Do NOT add new dependencies wi
196191
197192 ### Chat API Flow
198193
199-1. Client sends POST request to `/api/chat` with `{ message: string }`
200-2. API route fetches DuckDuckGo results for the message
201-3. API route builds prompt with message + search results
202-4. API route calls Ollama with the augmented prompt
203-5. API route returns `{ answer: string }`
194+1. Client sends POST request to `/api/chat` with `{ message: string, threadId: number }`
195+2. API route fetches previous messages from the thread
196+3. API route stores the user message
197+4. API route calls Ollama with the full conversation history
198+5. API route stores the assistant response with generation time
199+6. API route returns `{ answer: string }`
204200
205201 ## Development Workflow
206202
app/api/chat/route.ts+31−25
@@ -1,9 +1,8 @@
11 import { NextResponse } from "next/server";
2-import { fetchOllamaResponse } from "@/src/lib/ollama";
3-import { fetchDuckDuckGo } from "@/src/lib/duckduckgo";
2+import { fetchOllamaResponse, type OllamaMessage } from "@/src/lib/ollama";
43 import { db } from "@/src/lib/db";
54 import { messages, threads } from "@/src/lib/db/schema";
6-import { eq } from "drizzle-orm";
5+import { eq, asc } from "drizzle-orm";
76
87 export async function POST(request: Request) {
98 const { message, threadId } = await request.json();
@@ -13,36 +12,43 @@ export async function POST(request: Request) {
1312 }
1413
1514 try {
16- // 1. Store user message
17- const userMessage = await db
18- .insert(messages)
19- .values({
20- threadId: parseInt(threadId),
21- role: "user",
22- content: message,
23- createdAt: new Date(),
24- })
25- .returning();
26-
27- // 2. Search DuckDuckGo
28- const searchResults = await fetchDuckDuckGo(message);
29-
30- console.debug("searchResults:", searchResults);
31-
32- // 3. Build prompt
33- const prompt = `${message}\n\nRelevant info from DuckDuckGo: ${JSON.stringify(
34- searchResults
35- )}\n\nAssistant:`;
15+ // 1. Fetch previous messages from thread
16+ const previousMessages = await db
17+ .select()
18+ .from(messages)
19+ .where(eq(messages.threadId, parseInt(threadId)))
20+ .orderBy(asc(messages.createdAt));
21+
22+ // 2. Store user message
23+ await db.insert(messages).values({
24+ threadId: parseInt(threadId),
25+ role: "user",
26+ content: message,
27+ createdAt: new Date(),
28+ });
29+
30+ // 3. Build messages array for Ollama
31+ const ollamaMessages: OllamaMessage[] = previousMessages.map((msg) => ({
32+ role: msg.role as "user" | "assistant",
33+ content: msg.content,
34+ }));
35+
36+ // Add the new user message
37+ ollamaMessages.push({
38+ role: "user",
39+ content: message,
40+ });
3641
3742 // 4. Get assistant response
38- const answer = await fetchOllamaResponse(prompt);
43+ const { content: answer, generationTimeMs } = await fetchOllamaResponse(ollamaMessages);
3944
40- // 5. Store assistant message
45+ // 5. Store assistant message with generation time
4146 await db.insert(messages).values({
4247 threadId: parseInt(threadId),
4348 role: "assistant",
4449 content: answer,
4550 createdAt: new Date(),
51+ generationTimeMs,
4652 });
4753
4854 // 6. Update thread's updatedAt timestamp
app/page.tsx+22−2
@@ -19,6 +19,7 @@ type Message = {
1919 role: string;
2020 content: string;
2121 createdAt: Date;
22+ generationTimeMs: number | null;
2223 };
2324
2425 export default function Home() {
@@ -174,6 +175,7 @@ export default function Home() {
174175 role: "user",
175176 content: input,
176177 createdAt: new Date(),
178+ generationTimeMs: null,
177179 };
178180 setMessages((prev) => [...prev, userMsg]);
179181 const inputValue = input.trim();
@@ -195,6 +197,7 @@ export default function Home() {
195197 role: "assistant",
196198 content: data.answer || "",
197199 createdAt: new Date(),
200+ generationTimeMs: null,
198201 };
199202 setMessages((prev) => [...prev, botMsg]);
200203
@@ -240,6 +243,17 @@ export default function Home() {
240243 }
241244 };
242245
246+ const formatTimestamp = (date: Date | string): string => {
247+ const d = typeof date === "string" ? new Date(date) : date;
248+ return d.toLocaleString();
249+ };
250+
251+ const formatGenerationTime = (ms: number | null): string => {
252+ if (!ms) return "";
253+ if (ms < 1000) return `${ms}ms`;
254+ return `${(ms / 1000).toFixed(1)}s`;
255+ };
256+
243257 return (
244258 <div className="flex h-screen bg-stone-100 text-stone-800 dark:bg-stone-950 dark:text-stone-200">
245259 <div className="flex min-w-0 flex-1 flex-col">
@@ -267,7 +281,7 @@ Once you've created a thread, you can start chatting with me by typing a message
267281 className={`markdown-content p-1 text-stone-800 dark:text-stone-200 ${
268282 msg.role === "assistant"
269283 ? "bg-stone-200 dark:bg-stone-900"
270- : "bg-stone-300 dark:bg-stone-800"
284+ : "bg-stone-300 dark:bg-stone-800 text-right"
271285 }`}
272286 >
273287 {msg.role === "assistant" ? (
@@ -275,8 +289,14 @@ Once you've created a thread, you can start chatting with me by typing a message
275289 {msg.content}
276290 </ReactMarkdown>
277291 ) : (
278- <div className="text-right">{msg.content}</div>
292+ <div>{msg.content}</div>
279293 )}
294+ <div className="text-xs text-stone-600 dark:text-stone-400 mt-1">
295+ sent on: {formatTimestamp(msg.createdAt)}
296+ {msg.role === "assistant" && msg.generationTimeMs && (
297+ <span className="ml-2">• generated in {formatGenerationTime(msg.generationTimeMs)}</span>
298+ )}
299+ </div>
280300 </div>
281301 </div>
282302 ))
drizzle/0001_worthless_wolf_cub.sql+1−0
@@ -0,0 +1 @@
1+ALTER TABLE `messages` ADD `generation_time_ms` integer;
drizzle/meta/0001_snapshot.json+122−0
@@ -0,0 +1,122 @@
1+{
2+ "version": "6",
3+ "dialect": "sqlite",
4+ "id": "8ba81961-1ce9-4e1d-b4b2-5559d2f2de11",
5+ "prevId": "f2f09444-b0be-4fd4-bc43-aeb9a6479eab",
6+ "tables": {
7+ "messages": {
8+ "name": "messages",
9+ "columns": {
10+ "id": {
11+ "name": "id",
12+ "type": "integer",
13+ "primaryKey": true,
14+ "notNull": true,
15+ "autoincrement": true
16+ },
17+ "thread_id": {
18+ "name": "thread_id",
19+ "type": "integer",
20+ "primaryKey": false,
21+ "notNull": true,
22+ "autoincrement": false
23+ },
24+ "role": {
25+ "name": "role",
26+ "type": "text",
27+ "primaryKey": false,
28+ "notNull": true,
29+ "autoincrement": false
30+ },
31+ "content": {
32+ "name": "content",
33+ "type": "text",
34+ "primaryKey": false,
35+ "notNull": true,
36+ "autoincrement": false
37+ },
38+ "created_at": {
39+ "name": "created_at",
40+ "type": "integer",
41+ "primaryKey": false,
42+ "notNull": true,
43+ "autoincrement": false
44+ },
45+ "generation_time_ms": {
46+ "name": "generation_time_ms",
47+ "type": "integer",
48+ "primaryKey": false,
49+ "notNull": false,
50+ "autoincrement": false
51+ }
52+ },
53+ "indexes": {},
54+ "foreignKeys": {
55+ "messages_thread_id_threads_id_fk": {
56+ "name": "messages_thread_id_threads_id_fk",
57+ "tableFrom": "messages",
58+ "tableTo": "threads",
59+ "columnsFrom": [
60+ "thread_id"
61+ ],
62+ "columnsTo": [
63+ "id"
64+ ],
65+ "onDelete": "cascade",
66+ "onUpdate": "no action"
67+ }
68+ },
69+ "compositePrimaryKeys": {},
70+ "uniqueConstraints": {},
71+ "checkConstraints": {}
72+ },
73+ "threads": {
74+ "name": "threads",
75+ "columns": {
76+ "id": {
77+ "name": "id",
78+ "type": "integer",
79+ "primaryKey": true,
80+ "notNull": true,
81+ "autoincrement": true
82+ },
83+ "title": {
84+ "name": "title",
85+ "type": "text",
86+ "primaryKey": false,
87+ "notNull": false,
88+ "autoincrement": false
89+ },
90+ "created_at": {
91+ "name": "created_at",
92+ "type": "integer",
93+ "primaryKey": false,
94+ "notNull": true,
95+ "autoincrement": false
96+ },
97+ "updated_at": {
98+ "name": "updated_at",
99+ "type": "integer",
100+ "primaryKey": false,
101+ "notNull": true,
102+ "autoincrement": false
103+ }
104+ },
105+ "indexes": {},
106+ "foreignKeys": {},
107+ "compositePrimaryKeys": {},
108+ "uniqueConstraints": {},
109+ "checkConstraints": {}
110+ }
111+ },
112+ "views": {},
113+ "enums": {},
114+ "_meta": {
115+ "schemas": {},
116+ "tables": {},
117+ "columns": {}
118+ },
119+ "internal": {
120+ "indexes": {}
121+ }
122+}
drizzle/meta/_journal.json+7−0
@@ -8,6 +8,13 @@
88 "when": 1765904176875,
99 "tag": "0000_rich_betty_ross",
1010 "breakpoints": true
11+ },
12+ {
13+ "idx": 1,
14+ "version": "6",
15+ "when": 1765910805241,
16+ "tag": "0001_worthless_wolf_cub",
17+ "breakpoints": true
1118 }
1219 ]
1320 }
src/lib/db/schema.ts+1−0
@@ -23,6 +23,7 @@ export const messages = sqliteTable("messages", {
2323 createdAt: integer("created_at", { mode: "timestamp" })
2424 .notNull()
2525 .$defaultFn(() => new Date()),
26+ generationTimeMs: integer("generation_time_ms"), // Time taken to generate response (for assistant messages)
2627 });
2728
2829 // Define relations
src/lib/duckduckgo.ts+0−46
@@ -1,46 +0,0 @@
1-export interface DuckDuckGoResult {
2- title: string;
3- snippet: string;
4- url: string;
5-}
6-
7-const DDG_ENDPOINT = "https://api.duckduckgo.com/";
8-
9-export async function fetchDuckDuckGo(query: string): Promise<DuckDuckGoResult[]> {
10- const params = new URLSearchParams({
11- q: query,
12- format: "json",
13- no_html: "1",
14- skip_disambig: "1",
15- });
16-
17- const url = `${DDG_ENDPOINT}?${params.toString()}`;
18-
19- const res = await fetch(url, { headers: { Accept: "application/json" } });
20- if (!res.ok) {
21- throw new Error(`DuckDuckGo API error: ${res.status} ${res.statusText}`);
22- }
23-
24- const data = await res.json();
25-
26- const results: DuckDuckGoResult[] = [];
27-
28- // DuckDuckGo returns search results in the "RelatedTopics" array.
29- // Each item may contain a nested array; we flatten only the first level.
30- const topics = Array.isArray(data.RelatedTopics) ? data.RelatedTopics : [];
31- for (const item of topics) {
32- if (item.Text && item.FirstURL) {
33- // Use the first line of the text as a title
34- const title = item.Text.split("\n")[0];
35- // Limit snippet to 70 characters
36- const snippet = item.Text.length > 70 ? item.Text.slice(0, 70) + "…" : item.Text;
37- results.push({
38- title,
39- snippet,
40- url: item.FirstURL,
41- });
42- }
43- }
44-
45- return results;
46-}
src/lib/ollama.ts+22−9
@@ -3,18 +3,31 @@
33
44 import ollama from "ollama";
55
6+export interface OllamaMessage {
7+ role: "user" | "assistant" | "system";
8+ content: string;
9+}
10+
11+export interface OllamaResponse {
12+ content: string;
13+ generationTimeMs: number;
14+}
15+
616 /**
7- * Sends a prompt to the Ollama model and returns the generated response.
17+ * Sends messages to the Ollama model using the chat API and returns the generated response.
818 *
9- * @param prompt - The text prompt to send to the model.
19+ * @param messages - Array of messages in the conversation history.
1020 * @param model - The Ollama model to use. Defaults to 'gpt-oss'.
11- * @returns The text generated by the model.
21+ * @returns Object containing the response content and generation time in milliseconds.
1222 * @throws If the request fails.
1323 */
14-export async function fetchOllamaResponse(prompt: string, model = "gpt-oss"): Promise<string> {
15- const result = await ollama.generate({ model, prompt });
16- // The official package returns an object with a `response` field.
17- // Adjust if the shape differs.
18- // @ts-ignore
19- return result.response;
24+export async function fetchOllamaResponse(
25+ messages: OllamaMessage[],
26+ model = "gpt-oss"
27+): Promise<OllamaResponse> {
28+ const result = await ollama.chat({ model, messages });
29+ // The official package returns an object with a `message.content` field and `total_duration` in nanoseconds.
30+ const content = result.message.content;
31+ const generationTimeMs = Math.round(result.total_duration / 1_000_000); // Convert nanoseconds to milliseconds
32+ return { content, generationTimeMs };
2033 }