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 =
|
const currentPage =
|
||||||
req.headers.get("x-current-page") ?? undefined
|
req.headers.get("x-current-page") ?? undefined
|
||||||
|
const timezone =
|
||||||
|
req.headers.get("x-timezone") ?? undefined
|
||||||
|
|
||||||
const model = await getAgentModel()
|
const model = await getAgentModel()
|
||||||
|
|
||||||
@ -46,6 +48,7 @@ export async function POST(req: Request): Promise<Response> {
|
|||||||
userName: user.displayName ?? user.email,
|
userName: user.displayName ?? user.email,
|
||||||
userRole: user.role,
|
userRole: user.role,
|
||||||
currentPage,
|
currentPage,
|
||||||
|
timezone,
|
||||||
memories,
|
memories,
|
||||||
pluginSections,
|
pluginSections,
|
||||||
mode: "full",
|
mode: "full",
|
||||||
|
|||||||
@ -33,7 +33,11 @@ export function useCompassChat(options?: UseCompassChatOptions) {
|
|||||||
const chatState = useChat({
|
const chatState = useChat({
|
||||||
transport: new DefaultChatTransport({
|
transport: new DefaultChatTransport({
|
||||||
api: "/api/agent",
|
api: "/api/agent",
|
||||||
headers: { "x-current-page": pathname },
|
headers: {
|
||||||
|
"x-current-page": pathname,
|
||||||
|
"x-timezone":
|
||||||
|
Intl.DateTimeFormat().resolvedOptions().timeZone,
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
onFinish: options?.onFinish,
|
onFinish: options?.onFinish,
|
||||||
onError: (err) => {
|
onError: (err) => {
|
||||||
|
|||||||
@ -26,6 +26,7 @@ interface PromptContext {
|
|||||||
readonly userRole: string
|
readonly userRole: string
|
||||||
readonly currentPage?: string
|
readonly currentPage?: string
|
||||||
readonly memories?: string
|
readonly memories?: string
|
||||||
|
readonly timezone?: string
|
||||||
readonly pluginSections?: ReadonlyArray<PromptSection>
|
readonly pluginSections?: ReadonlyArray<PromptSection>
|
||||||
readonly mode?: PromptMode
|
readonly mode?: PromptMode
|
||||||
}
|
}
|
||||||
@ -216,11 +217,27 @@ function buildUserContext(
|
|||||||
state: DerivedState,
|
state: DerivedState,
|
||||||
): ReadonlyArray<string> {
|
): ReadonlyArray<string> {
|
||||||
if (state.mode === "none") return []
|
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 [
|
return [
|
||||||
"## User Context",
|
"## User Context",
|
||||||
`- Name: ${ctx.userName}`,
|
`- Name: ${ctx.userName}`,
|
||||||
`- Role: ${ctx.userRole}`,
|
`- Role: ${ctx.userRole}`,
|
||||||
`- Current page: ${state.page}`,
|
`- Current page: ${state.page}`,
|
||||||
|
`- Current date: ${date}`,
|
||||||
|
`- Current time: ${time} (${tz})`,
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user