xonecas.io
53129
Pa

PAT / commit 50b5a93

feat: time and location in prompt

xonecas2025-12-19 50b5a935efce5ae6c8b5b6261a6819be50fa773b parent 70587bb

4 files changed

app/api/settings/route.ts+19−1
@@ -7,6 +7,8 @@ const SETTINGS_CACHE_KEY = "app_settings";
77 type Settings = {
88 maxPromptLength: "none" | 1024 | 4096;
99 selectedModel?: string;
10+ location?: string;
11+ currentTime?: string;
1012 };
1113
1214 export async function GET() {
@@ -31,7 +33,7 @@ export async function GET() {
3133
3234 export async function POST(request: Request) {
3335 try {
34- const { maxPromptLength, selectedModel } = await request.json();
36+ const { maxPromptLength, selectedModel, location, currentTime } = await request.json();
3537
3638 let settings: Partial<Settings> = {};
3739
@@ -51,6 +53,22 @@ export async function POST(request: Request) {
5153 settings.selectedModel = selectedModel;
5254 }
5355
56+ // Validate and set location if provided
57+ if (location !== undefined) {
58+ if (typeof location !== "string") {
59+ return NextResponse.json({ error: "Invalid location value" }, { status: 400 });
60+ }
61+ settings.location = location;
62+ }
63+
64+ // Validate and set currentTime if provided
65+ if (currentTime !== undefined) {
66+ if (typeof currentTime !== "string") {
67+ return NextResponse.json({ error: "Invalid currentTime value" }, { status: 400 });
68+ }
69+ settings.currentTime = currentTime;
70+ }
71+
5472 // Get existing settings and merge, prefering the ones in the request
5573 const existingSettings = await getCache<Settings>(SETTINGS_CACHE_KEY);
5674 if (existingSettings) {
app/api/threads/route.ts+33−5
@@ -2,6 +2,16 @@ import { NextResponse } from "next/server";
22 import { db } from "@/src/lib/db";
33 import { threads, messages } from "@/src/lib/db/schema";
44 import { desc, count } from "drizzle-orm";
5+import { getCache } from "@/src/lib/cache";
6+
7+const SETTINGS_CACHE_KEY = "app_settings";
8+
9+type Settings = {
10+ maxPromptLength: "none" | 1024 | 4096;
11+ selectedModel?: string;
12+ location?: string;
13+ currentTime?: string;
14+};
515
616 export async function POST(request: Request) {
717 try {
@@ -20,15 +30,33 @@ export async function POST(request: Request) {
2030
2131 const threadId = newThread[0].id;
2232
33+ // Get location and currentTime from settings
34+ const settings = await getCache<Settings>(SETTINGS_CACHE_KEY);
35+ const location = settings?.location;
36+ const currentTime = settings?.currentTime;
37+
38+ // Build system prompt with location and time if available
39+ let systemPrompt = `You are PAT, a helpful personal assistant. You must follow these guidelines:
40+- Only repeat tool calls in case of errors
41+- Use simple and concise language
42+- Reply with markdown whenever possible
43+- When asked for code or text return it in a markdown code block`;
44+
45+ if (location || currentTime) {
46+ systemPrompt += "\n\n";
47+ if (currentTime) {
48+ systemPrompt += `Current time: ${currentTime}\n`;
49+ }
50+ if (location) {
51+ systemPrompt += `Location: ${location}`;
52+ }
53+ }
54+
2355 // Insert system message with guidelines
2456 await db.insert(messages).values({
2557 threadId,
2658 role: "system",
27- content: `You are PAT, a helpful personal assistant. Please follow these guidelines:
28-- Only repeat tool calls in case of errors
29-- Use simple and concise language
30-- Reply with markdown whenever possible
31-- When asked for code or text return it in a markdown code block`,
59+ content: systemPrompt,
3260 createdAt: new Date(),
3361 });
3462
app/page.tsx+2−2
@@ -5,7 +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 { useModelSettings } from "@/src/hooks/useModelSettings";
8+import { useSettings } from "@/src/hooks/useSettings";
99 import { useThreads } from "@/src/hooks/useThreads";
1010 import { useMessages } from "@/src/hooks/useMessages";
1111 import { useThreadSelection } from "@/src/hooks/useThreadSelection";
@@ -15,7 +15,7 @@ export default function Home() {
1515 const [error, setError] = useState<string | null>(null);
1616 const { themeMode, handleThemeChange } = useTheme();
1717 const { selectedModel, handleModelChange, maxPromptLength, handleMaxPromptLengthChange } =
18- useModelSettings(setError);
18+ useSettings(setError);
1919 const {
2020 threads,
2121 totalThreadCount,
src/hooks/useSettings.ts+29−3
@@ -1,8 +1,10 @@
11 import { useState, useEffect } from "react";
22
3-export function useModelSettings(onError?: (error: string) => void) {
3+export function useSettings(onError?: (error: string) => void) {
44 const [selectedModel, setSelectedModel] = useState<string>("gpt-oss");
55 const [maxPromptLength, setMaxPromptLength] = useState<"none" | 1024 | 4096>("none");
6+ const [location, setLocation] = useState<string | undefined>(undefined);
7+ const [currentTime, setCurrentTime] = useState<string | undefined>(undefined);
68
79 // Load settings from API and validate model against available models
810 useEffect(() => {
@@ -20,8 +22,10 @@ export function useModelSettings(onError?: (error: string) => void) {
2022 }
2123 const settingsData = await settingsRes.json();
2224 const settings = settingsData.settings || {};
23- const savedModel = settings.selectedModel || "gpt-oss";
25+ const savedModel = settings.selectedModel || "";
2426 const savedMaxPromptLength = settings.maxPromptLength || "none";
27+ const savedLocation = settings.location;
28+ const savedCurrentTime = settings.currentTime;
2529
2630 // Get available models
2731 const modelsRes = await fetch("/api/models");
@@ -38,6 +42,25 @@ export function useModelSettings(onError?: (error: string) => void) {
3842
3943 // Set maxPromptLength
4044 setMaxPromptLength(savedMaxPromptLength);
45+ // Set location and currentTime
46+ setLocation(savedLocation);
47+ setCurrentTime(savedCurrentTime);
48+
49+ // Post current time in ISO format to settings on app start
50+ const currentTimeISO = new Date().toISOString();
51+ try {
52+ await fetch("/api/settings", {
53+ method: "POST",
54+ headers: { "Content-Type": "application/json" },
55+ body: JSON.stringify({ currentTime: currentTimeISO }),
56+ });
57+ setCurrentTime(currentTimeISO);
58+ } catch (error) {
59+ // Silently fail - don't block app startup if time update fails
60+ if (onError && error instanceof Error) {
61+ onError(error.message);
62+ }
63+ }
4164
4265 if (availableModels.length === 0) {
4366 // No models available, keep default
@@ -69,8 +92,10 @@ export function useModelSettings(onError?: (error: string) => void) {
6992 onError(error.message);
7093 }
7194 // On error, keep defaults
72- setSelectedModel("gpt-oss");
95+ setSelectedModel("");
7396 setMaxPromptLength("none");
97+ setLocation(undefined);
98+ setCurrentTime(undefined);
7499 }
75100 };
76101
@@ -132,3 +157,4 @@ export function useModelSettings(onError?: (error: string) => void) {
132157 handleMaxPromptLengthChange,
133158 };
134159 }
160+