Persist per-message context for prompt caching

Move volatile current time and middle-pane data out of the system prompt and into a hidden userMessageContext stored on each user message. Reconstruct the LLM-facing message from this persisted context so older conversation turns remain stable across later requests while UI-facing content stays unchanged.

Keep finite branch instructions such as voice, search, code mode, agent notes, and workdir behavior in the system prompt so each conversation can still benefit from reusable prompt-cache prefixes.
This commit is contained in:
Ramnique Singh 2026-05-28 23:00:19 +05:30
parent c213274723
commit 129d91dc8d
2 changed files with 129 additions and 31 deletions

View file

@ -50,9 +50,29 @@ export const UserContentPart = z.union([UserTextPart, UserAttachmentPart]);
// Named type for user message content — used everywhere instead of repeating the union
export const UserMessageContent = z.union([z.string(), z.array(UserContentPart)]);
export const UserMessageContext = z.object({
currentDateTime: z.string().optional(),
middlePane: z.discriminatedUnion("kind", [
z.object({
kind: z.literal("empty"),
}),
z.object({
kind: z.literal("note"),
path: z.string(),
content: z.string(),
}),
z.object({
kind: z.literal("browser"),
url: z.string(),
title: z.string(),
}),
]).optional(),
});
export const UserMessage = z.object({
role: z.literal("user"),
content: UserMessageContent,
userMessageContext: UserMessageContext.optional(),
providerOptions: ProviderOptions.optional(),
});
@ -86,4 +106,4 @@ export const Message = z.discriminatedUnion("role", [
UserMessage,
]);
export const MessageList = z.array(Message);
export const MessageList = z.array(Message);