From f8faabd508ca2c08f014324cf5acc7c225afce98 Mon Sep 17 00:00:00 2001 From: Nicholai Date: Fri, 6 Feb 2026 18:08:16 -0700 Subject: [PATCH] feat(agent): localize date/time in system prompt (#45) Send the user's IANA timezone (Intl API) as x-timezone header from the chat transport. The system prompt now formats date and time in the user's local timezone instead of UTC. Co-authored-by: Nicholai --- src/app/api/agent/route.ts | 3 +++ src/hooks/use-compass-chat.ts | 6 +++++- src/lib/agent/system-prompt.ts | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/app/api/agent/route.ts b/src/app/api/agent/route.ts index 17fe4c7..df3ad05 100755 --- a/src/app/api/agent/route.ts +++ b/src/app/api/agent/route.ts @@ -37,6 +37,8 @@ export async function POST(req: Request): Promise { const currentPage = req.headers.get("x-current-page") ?? undefined + const timezone = + req.headers.get("x-timezone") ?? undefined const model = await getAgentModel() @@ -46,6 +48,7 @@ export async function POST(req: Request): Promise { userName: user.displayName ?? user.email, userRole: user.role, currentPage, + timezone, memories, pluginSections, mode: "full", diff --git a/src/hooks/use-compass-chat.ts b/src/hooks/use-compass-chat.ts index 4202664..25fe17d 100755 --- a/src/hooks/use-compass-chat.ts +++ b/src/hooks/use-compass-chat.ts @@ -33,7 +33,11 @@ export function useCompassChat(options?: UseCompassChatOptions) { const chatState = useChat({ transport: new DefaultChatTransport({ api: "/api/agent", - headers: { "x-current-page": pathname }, + headers: { + "x-current-page": pathname, + "x-timezone": + Intl.DateTimeFormat().resolvedOptions().timeZone, + }, }), onFinish: options?.onFinish, onError: (err) => { diff --git a/src/lib/agent/system-prompt.ts b/src/lib/agent/system-prompt.ts index aa88551..a560c9b 100755 --- a/src/lib/agent/system-prompt.ts +++ b/src/lib/agent/system-prompt.ts @@ -26,6 +26,7 @@ interface PromptContext { readonly userRole: string readonly currentPage?: string readonly memories?: string + readonly timezone?: string readonly pluginSections?: ReadonlyArray readonly mode?: PromptMode } @@ -216,11 +217,27 @@ function buildUserContext( state: DerivedState, ): ReadonlyArray { if (state.mode === "none") return [] + const tz = ctx.timezone ?? "UTC" + const now = new Date() + const date = now.toLocaleDateString("en-US", { + weekday: "long", + year: "numeric", + month: "long", + day: "numeric", + timeZone: tz, + }) + const time = now.toLocaleTimeString("en-US", { + hour: "2-digit", + minute: "2-digit", + timeZone: tz, + }) return [ "## User Context", `- Name: ${ctx.userName}`, `- Role: ${ctx.userRole}`, `- Current page: ${state.page}`, + `- Current date: ${date}`, + `- Current time: ${time} (${tz})`, ] }