π€― LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
β¨ feat(server): add missing temporal placeholder generators for server-side runs (#17893)
Arvin Xuf7a7b84787b9
diff --git a/apps/server/src/modules/AgentRuntime/adapters/serverCallLlmContextBuilder.ts b/apps/server/src/modules/AgentRuntime/adapters/serverCallLlmContextBuilder.tsindex 728fc83aa1b..74ee7a83d8c 100644--- a/apps/server/src/modules/AgentRuntime/adapters/serverCallLlmContextBuilder.ts+++ b/apps/server/src/modules/AgentRuntime/adapters/serverCallLlmContextBuilder.ts@@ -479,6 +479,9 @@ export const buildServerCallLlmContext = async ({COMPOSIO_SERVICES_LIST: composioServicesListStr,CREDS_LIST: credsListStr,language: serverLanguage,+ // Only override the generator's 'en-US' locale fallback when the user info+ // fetch actually resolved a language β an empty string would render blank.+ ...(serverLanguage && { locale: serverLanguage }),memory_effort: memoryEffort,sandbox_enabled: sandboxEnabled,sandbox_uploaded_files: sandboxUploadedFiles,diff --git a/apps/server/src/modules/Mecha/ContextEngineering/__tests__/serverMessagesEngine.test.ts b/apps/server/src/modules/Mecha/ContextEngineering/__tests__/serverMessagesEngine.test.tsindex 264d346c217..90daf65f530 100644--- a/apps/server/src/modules/Mecha/ContextEngineering/__tests__/serverMessagesEngine.test.ts+++ b/apps/server/src/modules/Mecha/ContextEngineering/__tests__/serverMessagesEngine.test.ts@@ -150,6 +150,70 @@ describe('serverMessagesEngine', () => {expect(resolved[0].content).not.toContain('(not specified');});+ it('renders every temporal placeholder instead of leaking the literal', async () => {+ const messages = createBasicMessages();+ const systemRole =+ 'Date: {{date}} Day: {{day}} Weekday: {{weekday}} Hour: {{hour}} ' ++ 'Minute: {{minute}} Second: {{second}} Month: {{month}} Year: {{year}} ' ++ 'ISO: {{iso}} Timestamp: {{timestamp}} Locale: {{locale}}';++ const result = await serverMessagesEngine({+ messages,+ model: 'gpt-4',+ provider: 'openai',+ systemRole,+ userTimezone: 'Asia/Shanghai',+ });++ expect(result[0].content).not.toContain('{{');+ expect(result[0].content).toMatch(/ISO: \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z/);+ expect(result[0].content).toMatch(/Timestamp: \d{13}/);+ expect(result[0].content).toMatch(+ /Weekday: (Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)/,+ );+ expect(result[0].content).toContain('Locale: en-US');+ });++ it('lets additionalVariables override the locale fallback', async () => {+ const messages = createBasicMessages();++ const result = await serverMessagesEngine({+ additionalVariables: { locale: 'zh-CN' },+ messages,+ model: 'gpt-4',+ provider: 'openai',+ systemRole: 'Locale: {{locale}}',+ });++ expect(result[0].content).toContain('Locale: zh-CN');+ expect(result[0].content).not.toContain('en-US');+ });++ it('renders wall-clock components in the user timezone', async () => {+ const messages = createBasicMessages();++ // Kiritimati is UTC+14 β always a different hour (and often day) than UTC,+ // so a UTC-based rendering cannot accidentally pass.+ const result = await serverMessagesEngine({+ messages,+ model: 'gpt-4',+ provider: 'openai',+ systemRole: 'Hour: {{hour}} Day: {{day}}',+ userTimezone: 'Pacific/Kiritimati',+ });++ const expected = new Intl.DateTimeFormat('en-US', {+ day: '2-digit',+ hour: '2-digit',+ hourCycle: 'h23',+ timeZone: 'Pacific/Kiritimati',+ }).formatToParts(new Date());+ const partsMap = Object.fromEntries(expected.map((part) => [part.type, part.value]));++ expect(result[0].content).toContain(`Hour: ${partsMap.hour}`);+ expect(result[0].content).toContain(`Day: ${partsMap.day}`);+ });+it('should inject model knowledge cutoff when provided', async () => {const messages = createBasicMessages();diff --git a/apps/server/src/modules/Mecha/ContextEngineering/index.ts b/apps/server/src/modules/Mecha/ContextEngineering/index.tsindex 71ec93ef359..10718a40b2e 100644--- a/apps/server/src/modules/Mecha/ContextEngineering/index.ts+++ b/apps/server/src/modules/Mecha/ContextEngineering/index.ts@@ -15,12 +15,45 @@ const createServerVariableGenerators = (params: {}) => {const { model, provider, timezone } = params;const tz = timezone || 'UTC';+ // Wall-clock components in the user's timezone. The client generators read them+ // off a local Date (2-digit, 24h) β h23 keeps midnight as "00" instead of "24".+ const timeParts = (): Record<string, string> => {+ const parts = new Intl.DateTimeFormat('en-US', {+ day: '2-digit',+ hour: '2-digit',+ hourCycle: 'h23',+ minute: '2-digit',+ month: '2-digit',+ second: '2-digit',+ timeZone: tz,+ year: 'numeric',+ }).formatToParts(new Date());+ return Object.fromEntries(parts.map((part) => [part.type, part.value]));+ };return {- // Time-related variables (localized to user's timezone)+ // Time-related variables (localized to user's timezone), mirroring the client+ // VARIABLE_GENERATORS set so no temporal placeholder leaks as a literal in+ // server-side runs. Prefer the coarse ones ({{date}}, {{hour}}) in system+ // prompts: fine-grained values change every request and break prompt caching.date: () => new Date().toLocaleDateString('en-US', { dateStyle: 'full', timeZone: tz }),datetime: () => new Date().toLocaleString('en-US', { timeZone: tz }),+ day: () => timeParts().day,+ hour: () => timeParts().hour,+ iso: () => new Date().toISOString(),+ // The client resolves {{locale}} from the browser (Intl resolvedOptions). The+ // server has no request locale here β the user's configured response language+ // arrives via `additionalVariables` and overrides this through the spread+ // order below; without it, fall back to the same default as+ // UserModel.getInfoForAIGeneration instead of leaking the literal token.+ locale: () => 'en-US',+ minute: () => timeParts().minute,+ month: () => timeParts().month,+ second: () => timeParts().second,time: () => new Date().toLocaleTimeString('en-US', { timeStyle: 'medium', timeZone: tz }),+ timestamp: () => Date.now().toString(),timezone: () => tz,+ weekday: () => new Date().toLocaleDateString('en-US', { timeZone: tz, weekday: 'long' }),+ year: () => timeParts().year,// Model-related variablesmodel: () => model ?? '',provider: () => provider ?? '',