xonecas.io
53129
Pa

PAT / commit 5e84abc

fix: small ui fixes

xonecas2025-12-15 5e84abc111b1a518ad7920843e73d27160129765 parent 7bb5408

6 files changed

TODO.md+5−0
@@ -1,5 +1,10 @@
11 # TODO:
22
3+## BUGS:
4+
5+- use the chat() function of the ollama library instead of generate, so we can send the history of messages.
6+- Improve websearch, currently not working at all. Use this as a template: https://github.com/meirm/ollama-tools/blob/main/sample_functions.py#L5
7+
38 ## Must haves:
49
510 - The agent's reply UI needs to be as wide as it can in the browser window, and handle markdown and code snippets correctly
app/api/chat/route.ts+9−7
@@ -1,6 +1,6 @@
1-import { NextResponse } from 'next/server';
2-import { fetchOllamaResponse } from '../../../src/lib/ollama';
3-import { fetchDuckDuckGo } from '../../../src/lib/duckduckgo';
1+import { NextResponse } from "next/server";
2+import { fetchOllamaResponse } from "../../../src/lib/ollama";
3+import { fetchDuckDuckGo } from "../../../src/lib/duckduckgo";
44
55 export async function POST(request: Request) {
66 const { message } = await request.json();
@@ -9,18 +9,20 @@ export async function POST(request: Request) {
99 // 1. Search DuckDuckGo
1010 const searchResults = await fetchDuckDuckGo(message);
1111
12+ console.debug("searchResults:", searchResults);
13+
1214 // 2. Build prompt
1315 const prompt = `${message}\n\nRelevant info from DuckDuckGo: ${JSON.stringify(
14- searchResults
16+ searchResults,
1517 )}\n\nAssistant:`;
1618
1719 const answer = await fetchOllamaResponse(prompt);
1820 return NextResponse.json({ answer });
1921 } catch (error) {
20- console.error('Chat API error', error);
22+ console.error("Chat API error", error);
2123 return NextResponse.json(
22- { answer: 'Sorry, something went wrong.' },
23- { status: 500 }
24+ { answer: "Sorry, something went wrong." },
25+ { status: 500 },
2426 );
2527 }
2628 }
app/page.tsx+27−0
@@ -7,12 +7,14 @@ export default function Home() {
77 );
88 const [input, setInput] = useState("");
99
10+ const [historyIndex, setHistoryIndex] = useState<number | null>(null);
1011 const sendMessage = async (e: React.FormEvent) => {
1112 e.preventDefault();
1213 if (!input.trim()) return;
1314
1415 const userMsg = { role: "user", content: input };
1516 setMessages((prev) => [...prev, userMsg]);
17+ setInput("");
1618
1719 const res = await fetch("/api/chat", {
1820 method: "POST",
@@ -26,6 +28,30 @@ export default function Home() {
2628 setMessages((prev) => [...prev, botMsg]);
2729
2830 setInput("");
31+ setHistoryIndex(null);
32+ };
33+ const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
34+ if (e.key === "ArrowUp") {
35+ e.preventDefault();
36+ const idx = historyIndex ?? messages.length;
37+ if (idx > 0) {
38+ const newIdx = idx - 1;
39+ setHistoryIndex(newIdx);
40+ setInput(messages[newIdx].content);
41+ }
42+ } else if (e.key === "ArrowDown") {
43+ e.preventDefault();
44+ if (historyIndex !== null) {
45+ const newIdx = historyIndex + 1;
46+ if (newIdx < messages.length) {
47+ setHistoryIndex(newIdx);
48+ setInput(messages[newIdx].content);
49+ } else {
50+ setHistoryIndex(null);
51+ setInput("");
52+ }
53+ }
54+ }
2955 };
3056
3157 return (
@@ -56,6 +82,7 @@ export default function Home() {
5682 type="text"
5783 value={input}
5884 onChange={(e) => setInput(e.target.value)}
85+ onKeyDown={handleKeyDown}
5986 placeholder="Type a message..."
6087 className="flex-1 border rounded px-3 py-2 focus:outline-none focus:ring-2 focus:ring-blue-500"
6188 />
package-lock.json+16−0
@@ -10,6 +10,7 @@
1010 "dependencies": {
1111 "autoprefixer": "^10.4.17",
1212 "next": "^16.0.10",
13+ "ollama": "^0.6.3",
1314 "postcss": "^8.4.30",
1415 "react": "18.2.0",
1516 "react-dom": "18.2.0",
@@ -4406,6 +4407,15 @@
44064407 "url": "https://github.com/sponsors/ljharb"
44074408 }
44084409 },
4410+ "node_modules/ollama": {
4411+ "version": "0.6.3",
4412+ "resolved": "https://registry.npmjs.org/ollama/-/ollama-0.6.3.tgz",
4413+ "integrity": "sha512-KEWEhIqE5wtfzEIZbDCLH51VFZ6Z3ZSa6sIOg/E/tBV8S51flyqBOXi+bRxlOYKDf8i327zG9eSTb8IJxvm3Zg==",
4414+ "license": "MIT",
4415+ "dependencies": {
4416+ "whatwg-fetch": "^3.6.20"
4417+ }
4418+ },
44094419 "node_modules/once": {
44104420 "version": "1.4.0",
44114421 "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
@@ -5897,6 +5907,12 @@
58975907 "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==",
58985908 "license": "MIT"
58995909 },
5910+ "node_modules/whatwg-fetch": {
5911+ "version": "3.6.20",
5912+ "resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-3.6.20.tgz",
5913+ "integrity": "sha512-EqhiFU6daOA8kpjOWTL0olhVOF3i7OrFzSYiGsEMB8GcXS+RrzauAERX65xMeNWVqxA6HXH2m69Z9LaKKdisfg==",
5914+ "license": "MIT"
5915+ },
59005916 "node_modules/which": {
59015917 "version": "2.0.2",
59025918 "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz",
package.json+2−1
@@ -15,7 +15,8 @@
1515 "react": "18.2.0",
1616 "react-dom": "18.2.0",
1717 "tailwindcss": "^3.4.0",
18- "typescript": "^5.4.2"
18+ "typescript": "^5.4.2",
19+ "ollama": "^0.6.3"
1920 },
2021 "devDependencies": {
2122 "@types/node": "^20.11.18",
src/lib/ollama.ts+10−32
@@ -1,45 +1,23 @@
1-// personal-assistant-thing/src/lib/ollama.ts
2-// Simple wrapper around Ollama's local server.
1+/* personal-assistant-thing/src/lib/ollama.ts */
2+// Wrapper using the official Ollama npm package.
33
4-export interface OllamaGenerateResponse {
5- model: string;
6- created_at: string;
7- response: string;
8- done: boolean;
9- // Other fields omitted for brevity.
10-}
4+import ollama from "ollama";
115
126 /**
13- * Sends a prompt to the local Ollama server and returns the generated response.
7+ * Sends a prompt to the Ollama model and returns the generated response.
148 *
159 * @param prompt - The text prompt to send to the model.
1610 * @param model - The Ollama model to use. Defaults to 'gpt-oss'.
1711 * @returns The text generated by the model.
18- * @throws If the request fails or the server returns an error status.
12+ * @throws If the request fails.
1913 */
2014 export async function fetchOllamaResponse(
2115 prompt: string,
2216 model = "gpt-oss",
2317 ): Promise<string> {
24- const url = "http://localhost:11434/api/generate";
25-
26- const payload = {
27- model,
28- prompt,
29- stream: false, // We want a single response, not a stream.
30- };
31-
32- const res = await fetch(url, {
33- method: "POST",
34- headers: { "Content-Type": "application/json" },
35- body: JSON.stringify(payload),
36- });
37-
38- if (!res.ok) {
39- const errorText = await res.text();
40- throw new Error(`Ollama API error (${res.status}): ${errorText}`);
41- }
42-
43- const data: OllamaGenerateResponse = await res.json();
44- return data.response;
18+ const result = await ollama.generate({ model, prompt });
19+ // The official package returns an object with a `response` field.
20+ // Adjust if the shape differs.
21+ // @ts-ignore
22+ return result.response;
4523 }