From 93e34d086f841c3c1c52f345da95b8992682e456 Mon Sep 17 00:00:00 2001 From: Agent Date: Sun, 24 May 2026 21:59:38 +0000 Subject: [PATCH] Augment system prompt with date and user location (default SF) --- .../Sybil/Sources/Sybil/SybilAPIClient.swift | 1 + server/src/llm/chat-tools.ts | 31 +++++++------ server/src/llm/message-content.ts | 25 ++++++++++- server/src/llm/multiplexer.ts | 5 ++- server/src/llm/streaming.ts | 5 ++- server/src/llm/types.ts | 1 + server/src/routes.ts | 43 ++++++++++++++++++- server/tests/message-content.test.ts | 26 +++++++++++ tui/src/api.ts | 1 + web/src/lib/api.ts | 2 + 10 files changed, 121 insertions(+), 19 deletions(-) create mode 100644 server/tests/message-content.test.ts diff --git a/ios/Packages/Sybil/Sources/Sybil/SybilAPIClient.swift b/ios/Packages/Sybil/Sources/Sybil/SybilAPIClient.swift index fdc4eb5..3a8135b 100644 --- a/ios/Packages/Sybil/Sources/Sybil/SybilAPIClient.swift +++ b/ios/Packages/Sybil/Sources/Sybil/SybilAPIClient.swift @@ -631,6 +631,7 @@ struct CompletionStreamRequest: Codable, Sendable { var provider: Provider var model: String var messages: [CompletionRequestMessage] + var userLocation: String? = nil } private struct ChatCreateBody: Encodable { diff --git a/server/src/llm/chat-tools.ts b/server/src/llm/chat-tools.ts index 0d32bbd..f58fd4b 100644 --- a/server/src/llm/chat-tools.ts +++ b/server/src/llm/chat-tools.ts @@ -9,7 +9,11 @@ import { z } from "zod"; import { env } from "../env.js"; import { exaClient } from "../search/exa.js"; import { searchSearxng } from "../search/searxng.js"; -import { buildOpenAIConversationMessage, buildOpenAIResponsesInputMessage } from "./message-content.js"; +import { + buildOpenAIConversationMessage, + buildOpenAIResponsesInputMessage, + buildSystemPromptAugmentationMessage, +} from "./message-content.js"; import type { ChatMessage } from "./types.js"; const MAX_TOOL_ROUNDS = env.CHAT_MAX_TOOL_ROUNDS; @@ -239,6 +243,7 @@ type ToolAwareCompletionParams = { client: OpenAI; model: string; messages: ChatMessage[]; + userLocation?: string; temperature?: number; maxTokens?: number; onToolEvent?: (event: ToolExecutionEvent) => void | Promise; @@ -379,20 +384,20 @@ function extractHtmlTitle(html: string) { ); } -function normalizeIncomingMessages(messages: ChatMessage[]) { +function normalizeIncomingMessages(messages: ChatMessage[], userLocation?: string) { const normalized = messages.map((message) => buildOpenAIConversationMessage(message)); - return [{ role: "system", content: CHAT_TOOL_SYSTEM_PROMPT }, ...normalized]; + return [{ role: "system", content: CHAT_TOOL_SYSTEM_PROMPT }, buildSystemPromptAugmentationMessage(userLocation), ...normalized]; } -function normalizePlainIncomingMessages(messages: ChatMessage[]) { - return messages.map((message) => buildOpenAIConversationMessage(message)); +function normalizePlainIncomingMessages(messages: ChatMessage[], userLocation?: string) { + return [buildSystemPromptAugmentationMessage(userLocation), ...messages.map((message) => buildOpenAIConversationMessage(message))]; } -function normalizeIncomingResponsesInput(messages: ChatMessage[]) { +function normalizeIncomingResponsesInput(messages: ChatMessage[], userLocation?: string) { const normalized = messages.map((message) => buildOpenAIResponsesInputMessage(message)); - return [{ role: "system", content: CHAT_TOOL_SYSTEM_PROMPT }, ...normalized]; + return [{ role: "system", content: CHAT_TOOL_SYSTEM_PROMPT }, buildSystemPromptAugmentationMessage(userLocation), ...normalized]; } async function runExaWebSearchTool(args: WebSearchArgs): Promise { @@ -957,7 +962,7 @@ async function executeToolCallAndBuildEvent( } export async function runToolAwareOpenAIChat(params: ToolAwareCompletionParams): Promise { - const input: any[] = normalizeIncomingResponsesInput(params.messages); + const input: any[] = normalizeIncomingResponsesInput(params.messages, params.userLocation); const rawResponses: unknown[] = []; const toolEvents: ToolExecutionEvent[] = []; const usageAcc: Required = { inputTokens: 0, outputTokens: 0, totalTokens: 0 }; @@ -1026,7 +1031,7 @@ export async function runToolAwareOpenAIChat(params: ToolAwareCompletionParams): } export async function runToolAwareChatCompletions(params: ToolAwareCompletionParams): Promise { - const conversation: any[] = normalizeIncomingMessages(params.messages); + const conversation: any[] = normalizeIncomingMessages(params.messages, params.userLocation); const rawResponses: unknown[] = []; const toolEvents: ToolExecutionEvent[] = []; const usageAcc: Required = { inputTokens: 0, outputTokens: 0, totalTokens: 0 }; @@ -1114,7 +1119,7 @@ export async function runToolAwareChatCompletions(params: ToolAwareCompletionPar export async function runPlainChatCompletions(params: ToolAwareCompletionParams): Promise { const completion = await params.client.chat.completions.create({ model: params.model, - messages: normalizePlainIncomingMessages(params.messages), + messages: normalizePlainIncomingMessages(params.messages, params.userLocation), temperature: params.temperature, max_tokens: params.maxTokens, } as any); @@ -1134,7 +1139,7 @@ export async function runPlainChatCompletions(params: ToolAwareCompletionParams) export async function* runToolAwareOpenAIChatStream( params: ToolAwareCompletionParams ): AsyncGenerator { - const input: any[] = normalizeIncomingResponsesInput(params.messages); + const input: any[] = normalizeIncomingResponsesInput(params.messages, params.userLocation); const rawResponses: unknown[] = []; const toolEvents: ToolExecutionEvent[] = []; const usageAcc: Required = { inputTokens: 0, outputTokens: 0, totalTokens: 0 }; @@ -1260,7 +1265,7 @@ export async function* runToolAwareOpenAIChatStream( export async function* runToolAwareChatCompletionsStream( params: ToolAwareCompletionParams ): AsyncGenerator { - const conversation: any[] = normalizeIncomingMessages(params.messages); + const conversation: any[] = normalizeIncomingMessages(params.messages, params.userLocation); const rawResponses: unknown[] = []; const toolEvents: ToolExecutionEvent[] = []; const usageAcc: Required = { inputTokens: 0, outputTokens: 0, totalTokens: 0 }; @@ -1403,7 +1408,7 @@ export async function* runPlainChatCompletionsStream( const stream = await params.client.chat.completions.create({ model: params.model, - messages: normalizePlainIncomingMessages(params.messages), + messages: normalizePlainIncomingMessages(params.messages, params.userLocation), temperature: params.temperature, max_tokens: params.maxTokens, stream: true, diff --git a/server/src/llm/message-content.ts b/server/src/llm/message-content.ts index 8b3434c..d399b29 100644 --- a/server/src/llm/message-content.ts +++ b/server/src/llm/message-content.ts @@ -1,5 +1,19 @@ import type { ChatAttachment, ChatImageAttachment, ChatMessage, ChatTextAttachment } from "./types.js"; +const DEFAULT_USER_LOCATION = "San Francisco, CA"; + +function currentDateString(now = new Date()) { + return now.toISOString().slice(0, 10); +} + +function resolveUserLocation(userLocation?: string) { + return userLocation?.trim() || process.env.SYBIL_USER_LOCATION?.trim() || DEFAULT_USER_LOCATION; +} + +export function buildSystemPromptAugmentation(userLocation?: string, now = new Date()) { + return `Current date: ${currentDateString(now)}.\nUser location: ${resolveUserLocation(userLocation)}.`; +} + function escapeAttribute(value: string) { return value.replace(/"/g, """); } @@ -198,11 +212,18 @@ export function buildOpenAIResponsesInputMessage(message: ChatMessage) { }; } +export function buildSystemPromptAugmentationMessage(userLocation?: string) { + return { + role: "system", + content: buildSystemPromptAugmentation(userLocation), + }; +} + const ANTHROPIC_NO_SERVER_TOOLS_PROMPT = "This Anthropic backend path does not have server-managed tool calls. Do not claim to run shell commands, Codex tasks, web searches, or fetch URLs. If the user asks for tool execution, explain that they should switch to OpenAI or xAI in this app for tool-enabled chat."; -export function getAnthropicSystemPrompt(messages: ChatMessage[]) { - return [ANTHROPIC_NO_SERVER_TOOLS_PROMPT, messages.find((message) => message.role === "system")?.content] +export function getAnthropicSystemPrompt(messages: ChatMessage[], userLocation?: string) { + return [ANTHROPIC_NO_SERVER_TOOLS_PROMPT, buildSystemPromptAugmentation(userLocation), messages.find((message) => message.role === "system")?.content] .filter(Boolean) .join("\n\n"); } diff --git a/server/src/llm/multiplexer.ts b/server/src/llm/multiplexer.ts index e2b39ed..f852f13 100644 --- a/server/src/llm/multiplexer.ts +++ b/server/src/llm/multiplexer.ts @@ -54,6 +54,7 @@ export async function runMultiplex(req: MultiplexRequest): Promise message.role !== "system").map((message) => buildAnthropicConversationMessage(message)); const r = await client.messages.create({ diff --git a/server/src/llm/streaming.ts b/server/src/llm/streaming.ts index cbafb19..32ea834 100644 --- a/server/src/llm/streaming.ts +++ b/server/src/llm/streaming.ts @@ -82,6 +82,7 @@ export async function* runMultiplexStream(req: MultiplexRequest): AsyncGenerator client, model: req.model, messages: req.messages, + userLocation: req.userLocation, temperature: req.temperature, maxTokens: req.maxTokens, logContext: { @@ -95,6 +96,7 @@ export async function* runMultiplexStream(req: MultiplexRequest): AsyncGenerator client, model: req.model, messages: req.messages, + userLocation: req.userLocation, temperature: req.temperature, maxTokens: req.maxTokens, logContext: { @@ -107,6 +109,7 @@ export async function* runMultiplexStream(req: MultiplexRequest): AsyncGenerator client, model: req.model, messages: req.messages, + userLocation: req.userLocation, temperature: req.temperature, maxTokens: req.maxTokens, logContext: { @@ -146,7 +149,7 @@ export async function* runMultiplexStream(req: MultiplexRequest): AsyncGenerator } else if (req.provider === "anthropic") { const client = anthropicClient(); - const system = getAnthropicSystemPrompt(req.messages); + const system = getAnthropicSystemPrompt(req.messages, req.userLocation); const msgs = req.messages.filter((message) => message.role !== "system").map((message) => buildAnthropicConversationMessage(message)); const stream = await client.messages.create({ diff --git a/server/src/llm/types.ts b/server/src/llm/types.ts index 618fdf4..427f053 100644 --- a/server/src/llm/types.ts +++ b/server/src/llm/types.ts @@ -36,6 +36,7 @@ export type MultiplexRequest = { provider: Provider; model: string; messages: ChatMessage[]; + userLocation?: string; temperature?: number; maxTokens?: number; }; diff --git a/server/src/routes.ts b/server/src/routes.ts index 4a58109..ddecbef 100644 --- a/server/src/routes.ts +++ b/server/src/routes.ts @@ -47,6 +47,43 @@ function isToolCallLogMessage(message: { role: string; metadata: unknown }) { return message.role === "tool" && isToolCallLogMetadata(message.metadata); } +function getHeaderString(req: FastifyRequest, name: string) { + const value = req.headers[name.toLowerCase()]; + if (Array.isArray(value)) return value.find((item) => item.trim()); + return typeof value === "string" && value.trim() ? value : undefined; +} + +function decodeHeaderPart(value: string | undefined) { + if (!value) return undefined; + const trimmed = value.trim(); + if (!trimmed) return undefined; + try { + return decodeURIComponent(trimmed); + } catch { + return trimmed; + } +} + +function inferRequestUserLocation(req: FastifyRequest) { + const explicit = decodeHeaderPart(getHeaderString(req, "x-user-location")); + if (explicit) return explicit; + + const vercelCity = decodeHeaderPart(getHeaderString(req, "x-vercel-ip-city")); + const vercelRegion = decodeHeaderPart(getHeaderString(req, "x-vercel-ip-country-region")); + const vercelCountry = decodeHeaderPart(getHeaderString(req, "x-vercel-ip-country")); + const vercelLocation = [vercelCity, vercelRegion, vercelCountry].filter(Boolean).join(", "); + if (vercelLocation) return vercelLocation; + + const cfCity = decodeHeaderPart(getHeaderString(req, "cf-ipcity")); + const cfRegion = decodeHeaderPart(getHeaderString(req, "cf-region")); + const cfCountry = decodeHeaderPart(getHeaderString(req, "cf-ipcountry")); + return [cfCity, cfRegion, cfCountry].filter(Boolean).join(", ") || undefined; +} + +function withRequestUserLocation(body: T, req: FastifyRequest): T { + return body.userLocation ? body : { ...body, userLocation: inferRequestUserLocation(req) }; +} + async function storeNonAssistantMessages(chatId: string, messages: IncomingChatMessage[]) { const incoming = messages.filter((m) => m.role !== "assistant"); if (!incoming.length) return; @@ -131,6 +168,7 @@ const CompletionStreamBody = z provider: ProviderSchema, model: z.string().min(1), messages: z.array(CompletionMessageSchema), + userLocation: z.string().trim().min(1).max(200).optional(), temperature: z.number().min(0).max(2).optional(), maxTokens: z.number().int().positive().optional(), }) @@ -1085,13 +1123,14 @@ export async function registerRoutes(app: FastifyInstance) { provider: ProviderSchema, model: z.string().min(1), messages: z.array(CompletionMessageSchema), + userLocation: z.string().trim().min(1).max(200).optional(), temperature: z.number().min(0).max(2).optional(), maxTokens: z.number().int().positive().optional(), }); const parsed = Body.safeParse(req.body); if (!parsed.success) return app.httpErrors.badRequest(parsed.error.message); - const body = parsed.data; + const body = withRequestUserLocation(parsed.data, req); // ensure chat exists if provided if (body.chatId) { @@ -1118,7 +1157,7 @@ export async function registerRoutes(app: FastifyInstance) { const parsed = CompletionStreamBody.safeParse(req.body); if (!parsed.success) return app.httpErrors.badRequest(parsed.error.message); - const body = parsed.data; + const body = withRequestUserLocation(parsed.data, req); // ensure chat exists if provided if (body.chatId) { diff --git a/server/tests/message-content.test.ts b/server/tests/message-content.test.ts new file mode 100644 index 0000000..3f38080 --- /dev/null +++ b/server/tests/message-content.test.ts @@ -0,0 +1,26 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { buildSystemPromptAugmentation, getAnthropicSystemPrompt } from "../src/llm/message-content.js"; + +test("system prompt augmentation includes date and default location", () => { + const prompt = buildSystemPromptAugmentation(undefined, new Date("2026-05-24T15:30:00Z")); + + assert.equal(prompt, "Current date: 2026-05-24.\nUser location: San Francisco, CA."); +}); + +test("system prompt augmentation uses provided user location", () => { + const prompt = buildSystemPromptAugmentation("New York, NY", new Date("2026-05-24T15:30:00Z")); + + assert.equal(prompt, "Current date: 2026-05-24.\nUser location: New York, NY."); +}); + +test("Anthropic system prompt includes runtime context with existing system messages", () => { + const prompt = getAnthropicSystemPrompt( + [{ role: "system", content: "Use concise answers." }], + "Los Angeles, CA" + ); + + assert.match(prompt, /Current date: \d{4}-\d{2}-\d{2}\./); + assert.match(prompt, /User location: Los Angeles, CA\./); + assert.match(prompt, /Use concise answers\./); +}); diff --git a/tui/src/api.ts b/tui/src/api.ts index 2a81506..be66200 100644 --- a/tui/src/api.ts +++ b/tui/src/api.ts @@ -100,6 +100,7 @@ export class SybilApiClient { provider: Provider; model: string; messages: CompletionRequestMessage[]; + userLocation?: string; }, handlers: CompletionStreamHandlers, options?: { signal?: AbortSignal } diff --git a/web/src/lib/api.ts b/web/src/lib/api.ts index 5a4b9f1..3a1b00d 100644 --- a/web/src/lib/api.ts +++ b/web/src/lib/api.ts @@ -569,6 +569,7 @@ export async function runCompletion(body: { provider: Provider; model: string; messages: CompletionRequestMessage[]; + userLocation?: string; }) { return api("/v1/chat-completions", { method: "POST", @@ -583,6 +584,7 @@ export async function runCompletionStream( provider: Provider; model: string; messages: CompletionRequestMessage[]; + userLocation?: string; }, handlers: CompletionStreamHandlers, options?: { signal?: AbortSignal }