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 <nicholaivogelfilms@gmail.com>
This commit is contained in:
parent
3e5b351b19
commit
f8faabd508
@ -37,6 +37,8 @@ export async function POST(req: Request): Promise<Response> {
|
||||
|
||||
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<Response> {
|
||||
userName: user.displayName ?? user.email,
|
||||
userRole: user.role,
|
||||
currentPage,
|
||||
timezone,
|
||||
memories,
|
||||
pluginSections,
|
||||
mode: "full",
|
||||
|
||||
@ -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) => {
|
||||
|
||||
@ -26,6 +26,7 @@ interface PromptContext {
|
||||
readonly userRole: string
|
||||
readonly currentPage?: string
|
||||
readonly memories?: string
|
||||
readonly timezone?: string
|
||||
readonly pluginSections?: ReadonlyArray<PromptSection>
|
||||
readonly mode?: PromptMode
|
||||
}
|
||||
@ -216,11 +217,27 @@ function buildUserContext(
|
||||
state: DerivedState,
|
||||
): ReadonlyArray<string> {
|
||||
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})`,
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user