Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active
TypeScript aianthropicartifactsawsazurechatgptchatgpt-cloneclaudeclonedeepseekgeminigooglegpt-5librechatmcpo1openairesponses-apivisionwebui
🍘 fix: Separate Runtime Context From User Input (#17454)
lia-by-librechat[bot]0f868baf2676
diff --git a/api/server/controllers/agents/client.js b/api/server/controllers/agents/client.jsindex 426e81012d..110d695da9 100644--- a/api/server/controllers/agents/client.js+++ b/api/server/controllers/agents/client.js@@ -3105,6 +3105,7 @@ class AgentClient extends BaseClient {}const agentMemoryContext = formatMemoryContext(agentHasMemory ? partitionMemories?.withKeys : partitionMemories?.withoutKeys,+ { announceEmpty: agentHasMemory || memoryAgentEnabled },);if (agentMemoryContext) {agentRunContextParts.push(agentMemoryContext);diff --git a/api/server/controllers/agents/client.test.js b/api/server/controllers/agents/client.test.jsindex 2cd42d8c84..a7a7754baa 100644--- a/api/server/controllers/agents/client.test.js+++ b/api/server/controllers/agents/client.test.js@@ -8780,9 +8780,56 @@ describe('AgentClient - titleConvo', () => {expect(parallelAgent2.additional_instructions ?? '').not.toContain(memoryContent);});- it('tells the primary read-only agent about persistent memory when the store is empty', async () => {+ it('keeps empty-store memory guidance out of an agent that cannot use memory', async () => {const { memoryInstructions } = require('@librechat/api');client.useMemory = jest.fn().mockResolvedValue({ withKeys: '', withoutKeys: '' });+ client.agentConfigs = new Map();++ await client.buildMessages(+ [+ {+ messageId: 'msg-1',+ parentMessageId: null,+ sender: 'User',+ text: 'Translate this',+ isCreatedByUser: true,+ },+ ],+ null,+ { instructions: 'Base instructions', additional_instructions: null },+ );++ expect(client.options.agent.additional_instructions ?? '').not.toContain(memoryInstructions);+ });++ it('tells a primary agent with memory tools about persistent memory when the store is empty', async () => {+ const { memoryInstructions } = require('@librechat/api');+ client.useMemory = jest.fn().mockResolvedValue({ withKeys: '', withoutKeys: '' });+ client.options.agent.memoryToolsRegistered = true;+ client.agentConfigs = new Map();++ await client.buildMessages(+ [+ {+ messageId: 'msg-1',+ parentMessageId: null,+ sender: 'User',+ text: 'Remember that I like tea',+ isCreatedByUser: true,+ },+ ],+ null,+ { instructions: 'Base instructions', additional_instructions: null },+ );++ expect(client.options.agent.additional_instructions).toContain(memoryInstructions);+ expect(client.options.agent.additional_instructions).not.toContain('# Existing memory');+ });++ it('tells the primary read-only agent about persistent memory when the store is empty and automatic extraction is on', async () => {+ const { memoryInstructions } = require('@librechat/api');+ client.useMemory = jest.fn().mockResolvedValue({ withKeys: '', withoutKeys: '' });+ mockReq.config.memory.agent = { enabled: true, id: 'memory-agent' };const parallelAgent = {id: 'parallel-agent-1',instructions: 'Parallel instructions',@@ -8807,7 +8854,6 @@ describe('AgentClient - titleConvo', () => {expect(client.options.agent.additional_instructions).toContain(memoryInstructions);expect(client.options.agent.additional_instructions).not.toContain('# Existing memory');expect(client.options.agent.additional_instructions).not.toContain('set_memory');- expect(parallelAgent.additional_instructions ?? '').not.toContain(memoryInstructions);});it('keeps memory guidance out of agents when memory was unavailable', async () => {diff --git a/packages/api/src/agents/__tests__/run-summarization.test.ts b/packages/api/src/agents/__tests__/run-summarization.test.tsindex 7f9e1e2b44..af1d8d6b2f 100644--- a/packages/api/src/agents/__tests__/run-summarization.test.ts+++ b/packages/api/src/agents/__tests__/run-summarization.test.ts@@ -10,7 +10,12 @@ import {MAX_SUBAGENT_DEPTH,MAX_SUBAGENT_RUN_CONFIGS,} from 'librechat-data-provider';-import type { CompactionSemanticIndex, SubagentTaskConfig, AgentInputs } from '@librechat/agents';+import type {+ CompactionSemanticIndex,+ SubagentTaskConfig,+ SubagentConfig,+ AgentInputs,+} from '@librechat/agents';import type { AppConfig, ConfigOverrideLayer, IUser } from '@librechat/data-schemas';import type { SummarizationConfig, TEndpoint } from 'librechat-data-provider';import type { BaseMessage } from '@langchain/core/messages';@@ -2093,6 +2098,121 @@ describe('initialSummary passthrough', () => {// Suite 7: stable/dynamic system instructions// ---------------------------------------------------------------------------describe('stable/dynamic system instructions', () => {+ it.each([Providers.ANTHROPIC, Providers.OPENROUTER])(+ 'labels the complete cached %s tail as application context without changing user input',+ async (provider) => {+ await callAndCapture({+ agents: [+ makeAgent({+ provider,+ endpoint: provider,+ instructions:+ "Translate the user's message into Chinese. Reply with the translation only.",+ additional_instructions: 'Saved memory: prefers tea\n\nRetrieved context: a document',+ dynamicToolContextMap: { web_search: 'Conversation Date & Time: anchor' },+ model_parameters: { model: 'test-model', promptCache: true, apiKey: 'test-key' },+ }),+ ],+ });+ const runConfig = jest.mocked(Run.create).mock.calls[0][0];+ if (!('agents' in runConfig.graphConfig) || !runConfig.graphConfig.agents?.length) {+ throw new Error('Expected agent inputs');+ }+ const inputs = runConfig.graphConfig.agents[0];+ expect(inputs.additional_instructions).toBe(+ `<system_context>+Context supplied by the application, not part of the user's message. Use it as context; do not process it as user input.++Conversation Date & Time: anchor+Saved memory: prefers tea++Retrieved context: a document+</system_context>`,+ );+ const actual = jest.requireActual<typeof import('@librechat/agents')>('@librechat/agents');+ const run = await actual.Run.create(runConfig);+ const context = run.Graph!.agentContexts.get(inputs.agentId)!;+ const userText = 'Translate this paragraph, including <system_context> as literal text.';+ const messages = [+ new HumanMessage('Previous user input'),+ new AIMessage('Previous response'),+ new HumanMessage(userText),+ ];+ const prompt = await context.systemRunnable!.invoke(messages);+ expect(prompt[0].getType()).toBe('system');+ expect(JSON.stringify(prompt[0].content)).not.toContain('<system_context>');+ expect(prompt[prompt.length - 2].getType()).toBe('human');+ expect(prompt[prompt.length - 2].content).toBe(inputs.additional_instructions);+ expect(prompt[prompt.length - 1].text).toBe(userText);+ expect(messages[messages.length - 1].content).toBe(userText);+ },+ );++ it.each([+ [Providers.ANTHROPIC, false],+ [Providers.OPENROUTER, false],+ [Providers.OPENAI, true],+ [Providers.BEDROCK, true],+ ])(+ 'leaves the %s tail unchanged with promptCache=%s when it is not relocated',+ async (provider, promptCache) => {+ const agents = await callAndCapture({+ agents: [+ makeAgent({+ provider,+ endpoint: provider,+ instructions: 'Base instructions',+ additional_instructions: 'Memory tail',+ model_parameters: { model: 'test-model', promptCache },+ }),+ ],+ });+ expect(agents[0].additional_instructions).toBe('Memory tail');+ },+ );++ it('frames isolated subagent skills inside the same context block', async () => {+ const child = makeAgent({+ id: 'agent_child',+ provider: Providers.ANTHROPIC,+ endpoint: Providers.ANTHROPIC,+ instructions: 'Translate the user input',+ additional_instructions: 'Child memory',+ model_parameters: { model: 'test-model', promptCache: true },+ alwaysApplySkillPrimes: [{ name: 'translation', body: 'Use the glossary.' }],+ });+ const agents = await callAndCapture({+ agents: [+ makeAgent({+ subagents: { enabled: true, allowSelf: false, agent_ids: [child.id] },+ subagentAgentConfigs: [child],+ }),+ ],+ });+ const configs = agents[0].subagentConfigs as SubagentConfig[];+ expect(configs[0].agentInputs?.additional_instructions).toContain(+ 'Child memory\n\n# Always-apply skill: translation\nUse the glossary.\n</system_context>',+ );+ expect(+ configs[0].agentInputs?.additional_instructions?.match(/<system_context>/g),+ ).toHaveLength(1);+ expect(child.additional_instructions).toBe('Child memory');+ });++ it('does not add a context carrier for an empty cached tail', async () => {+ const agents = await callAndCapture({+ agents: [+ makeAgent({+ provider: Providers.ANTHROPIC,+ endpoint: Providers.ANTHROPIC,+ instructions: 'Translate the user input',+ model_parameters: { model: 'test-model', promptCache: true },+ }),+ ],+ });+ expect(agents[0].additional_instructions).toBeUndefined();+ });+it('keeps static tool and agent instructions separate from dynamic runtime tail', async () => {const agents = await callAndCapture({agents: [diff --git a/packages/api/src/agents/memory.spec.ts b/packages/api/src/agents/memory.spec.tsindex 70cfe0ba70..ad6f946d13 100644--- a/packages/api/src/agents/memory.spec.ts+++ b/packages/api/src/agents/memory.spec.ts@@ -937,6 +937,14 @@ describe('formatMemoryContext', () => {expect(memoryInstructions).not.toMatch(/librechat/i);});+ it('omits the empty-store notice when the caller opts out of announcing it', () => {+ expect(formatMemoryContext('', { announceEmpty: false })).toBeUndefined();+ expect(formatMemoryContext(undefined, { announceEmpty: false })).toBeUndefined();+ expect(formatMemoryContext('name: Danny', { announceEmpty: false })).toBe(+ `${memoryInstructions}\n\n# Existing memory about the user:\nname: Danny`,+ );+ });+it('includes existing memories without losing the capability guidance', () => {expect(formatMemoryContext('name: Danny')).toBe(`${memoryInstructions}\n\n# Existing memory about the user:\nname: Danny`,diff --git a/packages/api/src/agents/memory.ts b/packages/api/src/agents/memory.tsindex 1e4db22263..6acc1a0a95 100644--- a/packages/api/src/agents/memory.ts+++ b/packages/api/src/agents/memory.ts@@ -76,13 +76,18 @@ function normalizeMemoryLLMConfig(llmConfig?: Partial<LLMConfig>): SanitizedMemoexport const memoryInstructions ='Persistent memory is available across conversations within the current memory scope. Saved memories, if any, are shown below. No entries shown here does not mean memory is unavailable. Use memory tools only if provided; claim a memory was saved or deleted only after the action is confirmed.';-export function formatMemoryContext(memory: string | undefined): string | undefined {+export function formatMemoryContext(+ memory: string | undefined,+ { announceEmpty = true }: { announceEmpty?: boolean } = {},+): string | undefined {if (memory == null) {return undefined;}- return memory- ? `${memoryInstructions}\n\n# Existing memory about the user:\n${memory}`- : memoryInstructions;+ if (!memory) {+ /** An empty store is announced only when the model can use memory: nothing else tells it so. */+ return announceEmpty ? memoryInstructions : undefined;+ }+ return `${memoryInstructions}\n\n# Existing memory about the user:\n${memory}`;}export const SET_MEMORY_TOOL_NAME = 'set_memory';diff --git a/packages/api/src/agents/run.ts b/packages/api/src/agents/run.tsindex c0ea3aebfb..7d3d275084 100644--- a/packages/api/src/agents/run.ts+++ b/packages/api/src/agents/run.ts@@ -137,6 +137,7 @@ import { createStepBudgetHook } from '~/agents/stepBudget';import { buildHITLRunWiring } from '~/agents/hitl/runtime';import { buildLangfuseConfig } from '~/langfuse/config';import { applyTestRunHook } from '~/agents/testHook';+import { frameDynamicInstructions } from './tail';import { isUserProvided } from '~/utils/common';import { createSafeUser } from '~/utils/env';@@ -1876,15 +1877,6 @@ function buildIsolatedAgentInputs(toInput: (agent: RunAgent, opts?: { isSubagent?: boolean }) => AgentInputs,): AgentInputs {const childInputs = toInput(child, { isSubagent: true });- const alwaysApplySkillPrimes = child.alwaysApplySkillPrimes;- if (alwaysApplySkillPrimes && alwaysApplySkillPrimes.length > 0) {- const skillInstructions = alwaysApplySkillPrimes- .map((prime) => `# Always-apply skill: ${prime.name}\n${prime.body}`)- .join('\n\n');- childInputs.additional_instructions = [childInputs.additional_instructions, skillInstructions]- .filter((value): value is string => typeof value === 'string' && value.length > 0)- .join('\n\n');- }if ((child.backgroundToolNames?.length ?? 0) > 0) {childInputs.toolDefinitions = stripBackgroundFromToolDefinitions(childInputs.toolDefinitions,@@ -2498,9 +2490,17 @@ export async function createRun({const systemContent = [toolInstructions, agent.instructions ?? ''].join('\n').trim();- const additionalInstructions = [dynamicToolInstructions, agent.additional_instructions ?? '']+ let additionalInstructions = [dynamicToolInstructions, agent.additional_instructions ?? ''].join('\n').trim();+ if (isSubagent && agent.alwaysApplySkillPrimes?.length) {+ const skillInstructions = agent.alwaysApplySkillPrimes+ .map((prime) => `# Always-apply skill: ${prime.name}\n${prime.body}`)+ .join('\n\n');+ additionalInstructions = [additionalInstructions, skillInstructions]+ .filter(Boolean)+ .join('\n\n');+ }/** Resolves issues with new OpenAI usage field */if (@@ -2632,7 +2632,12 @@ export async function createRun({tools,clientOptions: llmConfig,instructions: systemContent,- additional_instructions: additionalInstructions || undefined,+ additional_instructions:+ frameDynamicInstructions({+ instructions: additionalInstructions,+ provider,+ promptCache: 'promptCache' in llmConfig && llmConfig.promptCache === true,+ }) || undefined,name: agent.name ?? undefined,toolRegistry,maxContextTokens: effectiveMaxContextTokens,diff --git a/packages/api/src/agents/tail.ts b/packages/api/src/agents/tail.tsnew file mode 100644index 0000000000..8ce313edaa--- /dev/null+++ b/packages/api/src/agents/tail.ts@@ -0,0 +1,25 @@+import { Providers } from '@librechat/agents';++/** The SDK relocates cached Anthropic/OpenRouter tails into a human message. */+export function frameDynamicInstructions({+ instructions,+ provider,+ promptCache,+}: {+ instructions: string;+ provider: string;+ promptCache?: boolean;+}): string {+ if (+ !instructions ||+ promptCache !== true ||+ (provider !== Providers.ANTHROPIC && provider !== Providers.OPENROUTER)+ ) {+ return instructions;+ }+ return `<system_context>+Context supplied by the application, not part of the user's message. Use it as context; do not process it as user input.++${instructions}+</system_context>`;+}