Skip to content

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

🧠 feat: User Memories for Conversational Context (#7760)

Danny Avila29ef91b4dd53
diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md
index 09444a1b44..207aa17e66 100644
--- a/.github/CONTRIBUTING.md
+++ b/.github/CONTRIBUTING.md
@@ -30,8 +30,8 @@ Project maintainers have the right and responsibility to remove, edit, or reject
2. Install typescript globally: `npm i -g typescript`.
3. Run `npm ci` to install dependencies.
4. Build the data provider: `npm run build:data-provider`.
-5. Build MCP: `npm run build:mcp`.
-6. Build data schemas: `npm run build:data-schemas`.
+5. Build data schemas: `npm run build:data-schemas`.
+6. Build API methods: `npm run build:api`.
7. Setup and run unit tests:
- Copy `.env.test`: `cp api/test/.env.test.example api/test/.env.test`.
- Run backend unit tests: `npm run test:api`.
diff --git a/.github/workflows/backend-review.yml b/.github/workflows/backend-review.yml
index b7bccecae8..0c593eac18 100644
--- a/.github/workflows/backend-review.yml
+++ b/.github/workflows/backend-review.yml
@@ -7,6 +7,7 @@ on:
- release/*
paths:
- 'api/**'
+ - 'packages/api/**'
jobs:
tests_Backend:
name: Run Backend unit tests
@@ -36,12 +37,12 @@ jobs:
- name: Install Data Provider Package
run: npm run build:data-provider
- - name: Install MCP Package
- run: npm run build:mcp
-
- name: Install Data Schemas Package
run: npm run build:data-schemas
+ - name: Install API Package
+ run: npm run build:api
+
- name: Create empty auth.json file
run: |
mkdir -p api/data
@@ -66,5 +67,5 @@ jobs:
- name: Run librechat-data-provider unit tests
run: cd packages/data-provider && npm run test:ci
- - name: Run librechat-mcp unit tests
- run: cd packages/mcp && npm run test:ci
\ No newline at end of file
+ - name: Run librechat-api unit tests
+ run: cd packages/api && npm run test:ci
\ No newline at end of file
diff --git a/Dockerfile.multi b/Dockerfile.multi
index 991f805bec..26c5705376 100644
--- a/Dockerfile.multi
+++ b/Dockerfile.multi
@@ -14,7 +14,7 @@ RUN npm config set fetch-retry-maxtimeout 600000 && \
npm config set fetch-retry-mintimeout 15000
COPY package*.json ./
COPY packages/data-provider/package*.json ./packages/data-provider/
-COPY packages/mcp/package*.json ./packages/mcp/
+COPY packages/api/package*.json ./packages/api/
COPY packages/data-schemas/package*.json ./packages/data-schemas/
COPY client/package*.json ./client/
COPY api/package*.json ./api/
@@ -24,26 +24,27 @@ FROM base-min AS base
WORKDIR /app
RUN npm ci
-# Build data-provider
+# Build `data-provider` package
FROM base AS data-provider-build
WORKDIR /app/packages/data-provider
COPY packages/data-provider ./
RUN npm run build
-# Build mcp package
-FROM base AS mcp-build
-WORKDIR /app/packages/mcp
-COPY packages/mcp ./
-COPY --from=data-provider-build /app/packages/data-provider/dist /app/packages/data-provider/dist
-RUN npm run build
-
-# Build data-schemas
+# Build `data-schemas` package
FROM base AS data-schemas-build
WORKDIR /app/packages/data-schemas
COPY packages/data-schemas ./
COPY --from=data-provider-build /app/packages/data-provider/dist /app/packages/data-provider/dist
RUN npm run build
+# Build `api` package
+FROM base AS api-build
+WORKDIR /app/packages/api
+COPY packages/api ./
+COPY --from=data-provider-build /app/packages/data-provider/dist /app/packages/data-provider/dist
+COPY --from=data-schemas-build /app/packages/data-schemas/dist /app/packages/data-schemas/dist
+RUN npm run build
+
# Client build
FROM base AS client-build
WORKDIR /app/client
@@ -63,8 +64,8 @@ RUN npm ci --omit=dev
COPY api ./api
COPY config ./config
COPY --from=data-provider-build /app/packages/data-provider/dist ./packages/data-provider/dist
-COPY --from=mcp-build /app/packages/mcp/dist ./packages/mcp/dist
COPY --from=data-schemas-build /app/packages/data-schemas/dist ./packages/data-schemas/dist
+COPY --from=api-build /app/packages/api/dist ./packages/api/dist
COPY --from=client-build /app/client/dist ./client/dist
WORKDIR /app/api
EXPOSE 3080
diff --git a/api/app/clients/AnthropicClient.js b/api/app/clients/AnthropicClient.js
index 0da331ced5..037f1e7c46 100644
--- a/api/app/clients/AnthropicClient.js
+++ b/api/app/clients/AnthropicClient.js
@@ -10,6 +10,7 @@ const {
validateVisionModel,
} = require('librechat-data-provider');
const { SplitStreamHandler: _Handler } = require('@librechat/agents');
+const { Tokenizer, createFetch, createStreamEventHandlers } = require('@librechat/api');
const {
truncateText,
formatMessage,
@@ -26,8 +27,6 @@ const {
const { getModelMaxTokens, getModelMaxOutputTokens, matchModelName } = require('~/utils');
const { spendTokens, spendStructuredTokens } = require('~/models/spendTokens');
const { encodeAndFormat } = require('~/server/services/Files/images/encode');
-const { createFetch, createStreamEventHandlers } = require('./generators');
-const Tokenizer = require('~/server/services/Tokenizer');
const { sleep } = require('~/server/utils');
const BaseClient = require('./BaseClient');
const { logger } = require('~/config');
diff --git a/api/app/clients/ChatGPTClient.js b/api/app/clients/ChatGPTClient.js
index 36a3f4936a..555028dc3f 100644
--- a/api/app/clients/ChatGPTClient.js
+++ b/api/app/clients/ChatGPTClient.js
@@ -2,6 +2,7 @@ const { Keyv } = require('keyv');
const crypto = require('crypto');
const { CohereClient } = require('cohere-ai');
const { fetchEventSource } = require('@waylaidwanderer/fetch-event-source');
+const { constructAzureURL, genAzureChatCompletion } = require('@librechat/api');
const { encoding_for_model: encodingForModel, get_encoding: getEncoding } = require('tiktoken');
const {
ImageDetail,
@@ -10,9 +11,9 @@ const {
CohereConstants,
mapModelToAzureConfig,
} = require('librechat-data-provider');
-const { extractBaseURL, constructAzureURL, genAzureChatCompletion } = require('~/utils');
const { createContextHandlers } = require('./prompts');
const { createCoherePayload } = require('./llm');
+const { extractBaseURL } = require('~/utils');
const BaseClient = require('./BaseClient');
const { logger } = require('~/config');
diff --git a/api/app/clients/GoogleClient.js b/api/app/clients/GoogleClient.js
index 4151e6663a..2ded0657c7 100644
--- a/api/app/clients/GoogleClient.js
+++ b/api/app/clients/GoogleClient.js
@@ -1,4 +1,5 @@
const { google } = require('googleapis');
+const { Tokenizer } = require('@librechat/api');
const { concat } = require('@langchain/core/utils/stream');
const { ChatVertexAI } = require('@langchain/google-vertexai');
const { ChatGoogleGenerativeAI } = require('@langchain/google-genai');
@@ -19,7 +20,6 @@ const {
} = require('librechat-data-provider');
const { getSafetySettings } = require('~/server/services/Endpoints/google/llm');
const { encodeAndFormat } = require('~/server/services/Files/images');
-const Tokenizer = require('~/server/services/Tokenizer');
const { spendTokens } = require('~/models/spendTokens');
const { getModelMaxTokens } = require('~/utils');
const { sleep } = require('~/server/utils');
diff --git a/api/app/clients/OpenAIClient.js b/api/app/clients/OpenAIClient.js
index 280db89284..f3a7e67c12 100644
--- a/api/app/clients/OpenAIClient.js
+++ b/api/app/clients/OpenAIClient.js
@@ -1,6 +1,14 @@
const { OllamaClient } = require('./OllamaClient');
const { HttpsProxyAgent } = require('https-proxy-agent');
const { SplitStreamHandler, CustomOpenAIClient: OpenAI } = require('@librechat/agents');
+const {
+ isEnabled,
+ Tokenizer,
+ createFetch,
+ constructAzureURL,
+ genAzureChatCompletion,
+ createStreamEventHandlers,
+} = require('@librechat/api');
const {
Constants,
ImageDetail,
@@ -16,13 +24,6 @@ const {
validateVisionModel,
mapModelToAzureConfig,
} = require('librechat-data-provider');
-const {
- extractBaseURL,
- constructAzureURL,
- getModelMaxTokens,
- genAzureChatCompletion,
- getModelMaxOutputTokens,
-} = require('~/utils');
const {
truncateText,
formatMessage,
@@ -30,10 +31,9 @@ const {
titleInstruction,
createContextHandlers,
} = require('./prompts');
+const { extractBaseURL, getModelMaxTokens, getModelMaxOutputTokens } = require('~/utils');
const { encodeAndFormat } = require('~/server/services/Files/images/encode');
-const { createFetch, createStreamEventHandlers } = require('./generators');
-const { addSpaceIfNeeded, isEnabled, sleep } = require('~/server/utils');
-const Tokenizer = require('~/server/services/Tokenizer');
+const { addSpaceIfNeeded, sleep } = require('~/server/utils');
const { spendTokens } = require('~/models/spendTokens');
const { handleOpenAIErrors } = require('./tools/util');
const { createLLM, RunManager } = require('./llm');
diff --git a/api/app/clients/generators.js b/api/app/clients/generators.js
deleted file mode 100644
index 9814cac7a5..0000000000
--- a/api/app/clients/generators.js
+++ /dev/null
@@ -1,71 +0,0 @@
-const fetch = require('node-fetch');
-const { GraphEvents } = require('@librechat/agents');
-const { logger, sendEvent } = require('~/config');
-const { sleep } = require('~/server/utils');
-
-/**
- * Makes a function to make HTTP request and logs the process.
- * @param {Object} params
- * @param {boolean} [params.directEndpoint] - Whether to use a direct endpoint.
- * @param {string} [params.reverseProxyUrl] - The reverse proxy URL to use for the request.
- * @returns {Promise<Response>} - A promise that resolves to the response of the fetch request.
- */
-function createFetch({ directEndpoint = false, reverseProxyUrl = '' }) {
- /**
- * Makes an HTTP request and logs the process.
- * @param {RequestInfo} url - The URL to make the request to. Can be a string or a Request object.
- * @param {RequestInit} [init] - Optional init options for the request.
- * @returns {Promise<Response>} - A promise that resolves to the response of the fetch request.
- */
- return async (_url, init) => {
- let url = _url;
- if (directEndpoint) {
- url = reverseProxyUrl;
- }
- logger.debug(`Making request to ${url}`);
- if (typeof Bun !== 'undefined') {
- return await fetch(url, init);
- }
- return await fetch(url, init);
- };
-}
-
-// Add this at the module level outside the class
-/**
- * Creates event handlers for stream events that don't capture client references
- * @param {Object} res - The response object to send events to
- * @returns {Object} Object containing handler functions
- */
-function createStreamEventHandlers(res) {
- return {
- [GraphEvents.ON_RUN_STEP]: (event) => {
- if (res) {
- sendEvent(res, event);
- }
- },
- [GraphEvents.ON_MESSAGE_DELTA]: (event) => {
- if (res) {
- sendEvent(res, event);
- }
- },
- [GraphEvents.ON_REASONING_DELTA]: (event) => {
- if (res) {
- sendEvent(res, event);
- }
- },
- };
-}
-
-function createHandleLLMNewToken(streamRate) {
- return async () => {
- if (streamRate) {
- await sleep(streamRate);
- }
- };
-}
-
-module.exports = {
- createFetch,
- createHandleLLMNewToken,
- createStreamEventHandlers,
-};
diff --git a/api/app/clients/llm/createLLM.js b/api/app/clients/llm/createLLM.js
index c8d6666bce..846c4d8e9c 100644
--- a/api/app/clients/llm/createLLM.js
+++ b/api/app/clients/llm/createLLM.js
@@ -1,6 +1,5 @@
const { ChatOpenAI } = require('@langchain/openai');
-const { sanitizeModelName, constructAzureURL } = require('~/utils');
-const { isEnabled } = require('~/server/utils');
+const { isEnabled, sanitizeModelName, constructAzureURL } = require('@librechat/api');
/**
* Creates a new instance of a language model (LLM) for chat interactions.
diff --git a/api/app/clients/specs/BaseClient.test.js b/api/app/clients/specs/BaseClient.test.js
index 0ba77db6fa..6d44915804 100644
--- a/api/app/clients/specs/BaseClient.test.js
+++ b/api/app/clients/specs/BaseClient.test.js
@@ -33,7 +33,9 @@ jest.mock('~/models', () => ({
const { getConvo, saveConvo } = require('~/models');
jest.mock('@librechat/agents', () => {
+ const { Providers } = jest.requireActual('@librechat/agents');
return {
+ Providers,
ChatOpenAI: jest.fn().mockImplementation(() => {
return {};
}),
diff --git a/api/app/clients/tools/dynamic/OpenAPIPlugin.js b/api/app/clients/tools/dynamic/OpenAPIPlugin.js
deleted file mode 100644
index acc3a64d32..0000000000
--- a/api/app/clients/tools/dynamic/OpenAPIPlugin.js
+++ /dev/null
@@ -1,184 +0,0 @@
-require('dotenv').config();
-const fs = require('fs');
-const { z } = require('zod');
-const path = require('path');
-const yaml = require('js-yaml');
-const { createOpenAPIChain } = require('langchain/chains');
-const { DynamicStructuredTool } = require('@langchain/core/tools');
-const { ChatPromptTemplate, HumanMessagePromptTemplate } = require('@langchain/core/prompts');
-const { logger } = require('~/config');
-
-function addLinePrefix(text, prefix = '// ') {
- return text
- .split('\n')
- .map((line) => prefix + line)
- .join('\n');
-}
-
-function createPrompt(name, functions) {
- const prefix = `// The ${name} tool has the following functions. Determine the desired or most optimal function for the user's query:`;
- const functionDescriptions = functions
- .map((func) => `// - ${func.name}: ${func.description}`)
- .join('\n');
- return `${prefix}\n${functionDescriptions}
-// You are an expert manager and scrum master. You must provide a detailed intent to better execute the function.
-// Always format as such: {{"func": "function_name", "intent": "intent and expected result"}}`;
-}
-
-const AuthBearer = z
- .object({
- type: z.string().includes('service_http'),
- authorization_type: z.string().includes('bearer'),
- verification_tokens: z.object({
- openai: z.string(),
- }),
- })
- .catch(() => false);
-
-const AuthDefinition = z
- .object({
- type: z.string(),
- authorization_type: z.string(),
- verification_tokens: z.object({
- openai: z.string(),
- }),
- })
- .catch(() => false);
-
-async function readSpecFile(filePath) {
- try {
- const fileContents = await fs.promises.readFile(filePath, 'utf8');
- if (path.extname(filePath) === '.json') {
- return JSON.parse(fileContents);
- }
- return yaml.load(fileContents);
- } catch (e) {
- logger.error('[readSpecFile] error', e);
- return false;
- }
-}
-
-async function getSpec(url) {
- const RegularUrl = z
- .string()
- .url()
- .catch(() => false);
-
- if (RegularUrl.parse(url) && path.extname(url) === '.json') {
- const response = await fetch(url);
- return await response.json();
- }
-
- const ValidSpecPath = z
- .string()
- .url()
- .catch(async () => {
- const spec = path.join(__dirname, '..', '.well-known', 'openapi', url);
- if (!fs.existsSync(spec)) {
- return false;
- }
-
- return await readSpecFile(spec);
- });
-
- return ValidSpecPath.parse(url);
-}
-
-async function createOpenAPIPlugin({ data, llm, user, message, memory, signal }) {
- let spec;
- try {
- spec = await getSpec(data.api.url);
- } catch (error) {
- logger.error('[createOpenAPIPlugin] getSpec error', error);
- return null;
- }
-
- if (!spec) {
- logger.warn('[createOpenAPIPlugin] No spec found');
- return null;
- }
-
- const headers = {};
- const { auth, name_for_model, description_for_model, description_for_human } = data;
- if (auth && AuthDefinition.parse(auth)) {
- logger.debug('[createOpenAPIPlugin] auth detected', auth);
- const { openai } = auth.verification_tokens;
- if (AuthBearer.parse(auth)) {
- headers.authorization = `Bearer ${openai}`;
- logger.debug('[createOpenAPIPlugin] added auth bearer', headers);
- }
- }
-
- const chainOptions = { llm };
-
- if (data.headers && data.headers['librechat_user_id']) {
- logger.debug('[createOpenAPIPlugin] id detected', headers);
- headers[data.headers['librechat_user_id']] = user;
- }
-
- if (Object.keys(headers).length > 0) {
- logger.debug('[createOpenAPIPlugin] headers detected', headers);
- chainOptions.headers = headers;
- }
-
- if (data.params) {
- logger.debug('[createOpenAPIPlugin] params detected', data.params);
- chainOptions.params = data.params;
- }
-
- let history = '';
- if (memory) {
- logger.debug('[createOpenAPIPlugin] openAPI chain: memory detected', memory);
- const { history: chat_history } = await memory.loadMemoryVariables({});
- history = chat_history?.length > 0 ? `\n\n## Chat History:\n${chat_history}\n` : '';
- }
-
- chainOptions.prompt = ChatPromptTemplate.fromMessages([
- HumanMessagePromptTemplate.fromTemplate(
- `# Use the provided API's to respond to this query:\n\n{query}\n\n## Instructions:\n${addLinePrefix(
- description_for_model,
- )}${history}`,
- ),
- ]);
-
- const chain = await createOpenAPIChain(spec, chainOptions);
-
- const { functions } = chain.chains[0].lc_kwargs.llmKwargs;
-
- return new DynamicStructuredTool({
- name: name_for_model,
- description_for_model: `${addLinePrefix(description_for_human)}${createPrompt(
- name_for_model,
- functions,
- )}`,
- description: `${description_for_human}`,
- schema: z.object({
- func: z
- .string()
- .describe(
- `The function to invoke. The functions available are: ${functions
- .map((func) => func.name)
- .join(', ')}`,
- ),
- intent: z
- .string()
- .describe('Describe your intent with the function and your expected result'),
- }),
- func: async ({ func = '', intent = '' }) => {
- const filteredFunctions = functions.filter((f) => f.name === func);
- chain.chains[0].lc_kwargs.llmKwargs.functions = filteredFunctions;
- const query = `${message}${func?.length > 0 ? `\n// Intent: ${intent}` : ''}`;
- const result = await chain.call({
- query,
- signal,
- });
- return result.response;
- },
- });
-}
-
-module.exports = {
- getSpec,
- readSpecFile,
- createOpenAPIPlugin,
-};
diff --git a/api/app/clients/tools/dynamic/OpenAPIPlugin.spec.js b/api/app/clients/tools/dynamic/OpenAPIPlugin.spec.js
deleted file mode 100644
index 83bc5e9397..0000000000
--- a/api/app/clients/tools/dynamic/OpenAPIPlugin.spec.js
+++ /dev/null
@@ -1,72 +0,0 @@
-const fs = require('fs');
-const { createOpenAPIPlugin, getSpec, readSpecFile } = require('./OpenAPIPlugin');
-
-global.fetch = jest.fn().mockImplementationOnce(() => {
- return new Promise((resolve) => {
- resolve({
- ok: true,
- json: () => Promise.resolve({ key: 'value' }),
- });
- });
-});
-jest.mock('fs', () => ({
- promises: {
- readFile: jest.fn(),
- },
- existsSync: jest.fn(),
-}));
-
-describe('readSpecFile', () => {
- it('reads JSON file correctly', async () => {
- fs.promises.readFile.mockResolvedValue(JSON.stringify({ test: 'value' }));
- const result = await readSpecFile('test.json');
- expect(result).toEqual({ test: 'value' });
- });
-
- it('reads YAML file correctly', async () => {
- fs.promises.readFile.mockResolvedValue('test: value');
- const result = await readSpecFile('test.yaml');
- expect(result).toEqual({ test: 'value' });
- });
-
- it('handles error correctly', async () => {
- fs.promises.readFile.mockRejectedValue(new Error('test error'));
- const result = await readSpecFile('test.json');
- expect(result).toBe(false);
- });
-});
-
-describe('getSpec', () => {
- it('fetches spec from url correctly', async () => {
- const parsedJson = await getSpec('https://www.instacart.com/.well-known/ai-plugin.json');
- const isObject = typeof parsedJson === 'object';
- expect(isObject).toEqual(true);
- });
-
- it('reads spec from file correctly', async () => {
- fs.existsSync.mockReturnValue(true);
- fs.promises.readFile.mockResolvedValue(JSON.stringify({ test: 'value' }));
- const result = await getSpec('test.json');
- expect(result).toEqual({ test: 'value' });
- });
-
- it('returns false when file does not exist', async () => {
- fs.existsSync.mockReturnValue(false);
- const result = await getSpec('test.json');
- expect(result).toBe(false);
- });
-});
-
-describe('createOpenAPIPlugin', () => {
- it('returns null when getSpec throws an error', async () => {
- const result = await createOpenAPIPlugin({ data: { api: { url: 'invalid' } } });
- expect(result).toBe(null);
- });
-
- it('returns null when no spec is found', async () => {
- const result = await createOpenAPIPlugin({});
- expect(result).toBe(null);
- });
-
- // Add more tests here for different scenarios
-});
diff --git a/api/app/clients/tools/structured/DALLE3.js b/api/app/clients/tools/structured/DALLE3.js
index fc0f1851f6..7c2a56fe71 100644
--- a/api/app/clients/tools/structured/DALLE3.js
+++ b/api/app/clients/tools/structured/DALLE3.js
@@ -8,10 +8,10 @@ const { HttpsProxyAgent } = require('https-proxy-agent');
const { FileContext, ContentTypes } = require('librechat-data-provider');
const { getImageBasename } = require('~/server/services/Files/images');
const extractBaseURL = require('~/utils/extractBaseURL');
-const { logger } = require('~/config');
+const logger = require('~/config/winston');
const displayMessage =
- 'DALL-E displayed an image. All generated images are already plainly visible, so don\'t repeat the descriptions in detail. Do not list download links as they are available in the UI already. The user may download the images by clicking on them, but do not mention anything about downloading to the user.';
+ "DALL-E displayed an image. All generated images are already plainly visible, so don't repeat the descriptions in detail. Do not list download links as they are available in the UI already. The user may download the images by clicking on them, but do not mention anything about downloading to the user.";
class DALLE3 extends Tool {
constructor(fields = {}) {
super();
diff --git a/api/app/clients/tools/structured/specs/DALLE3.spec.js b/api/app/clients/tools/structured/specs/DALLE3.spec.js
index 1b28de2faf..2def575fb3 100644
--- a/api/app/clients/tools/structured/specs/DALLE3.spec.js
+++ b/api/app/clients/tools/structured/specs/DALLE3.spec.js
@@ -1,10 +1,29 @@
const OpenAI = require('openai');
const DALLE3 = require('../DALLE3');
-
-const { logger } = require('~/config');
+const logger = require('~/config/winston');
jest.mock('openai');
+jest.mock('@librechat/data-schemas', () => {
+ return {
+ logger: {
+ info: jest.fn(),
+ warn: jest.fn(),
+ debug: jest.fn(),
+ error: jest.fn(),
+ },
+ };
+});
+
+jest.mock('tiktoken', () => {
+ return {
+ encoding_for_model: jest.fn().mockReturnValue({
+ encode: jest.fn(),
+ decode: jest.fn(),
+ }),
+ };
+});
+
const processFileURL = jest.fn();
jest.mock('~/server/services/Files/images', () => ({
@@ -37,6 +56,11 @@ jest.mock('fs', () => {
return {
existsSync: jest.fn(),
mkdirSync: jest.fn(),
+ promises: {
+ writeFile: jest.fn(),
+ readFile: jest.fn(),
+ unlink: jest.fn(),
+ },
};
});
diff --git a/api/app/clients/tools/util/fileSearch.js b/api/app/clients/tools/util/fileSearch.js
index 54da483362..19d3a79edb 100644
--- a/api/app/clients/tools/util/fileSearch.js
+++ b/api/app/clients/tools/util/fileSearch.js
@@ -135,7 +135,7 @@ const createFileSearchTool = async ({ req, files, entity_id }) => {
query: z
.string()
.describe(
- 'A natural language query to search for relevant information in the files. Be specific and use keywords related to the information you\'re looking for. The query will be used for semantic similarity matching against the file contents.',
+ "A natural language query to search for relevant information in the files. Be specific and use keywords related to the information you're looking for. The query will be used for semantic similarity matching against the file contents.",
),
}),
},
diff --git a/api/config/index.js b/api/config/index.js
index e238f700be..8f24581be2 100644
--- a/api/config/index.js
+++ b/api/config/index.js
@@ -1,7 +1,7 @@
const axios = require('axios');
const { EventSource } = require('eventsource');
-const { Time, CacheKeys } = require('librechat-data-provider');
-const { MCPManager, FlowStateManager } = require('librechat-mcp');
+const { Time } = require('librechat-data-provider');
+const { MCPManager, FlowStateManager } = require('@librechat/api');
const logger = require('./winston');
global.EventSource = EventSource;
diff --git a/api/package.json b/api/package.json
index 6646c3a493..22be4f18e9 100644
--- a/api/package.json
+++ b/api/package.json
@@ -49,6 +49,7 @@
"@langchain/google-vertexai": "^0.2.9",
"@langchain/textsplitters": "^0.1.0",
"@librechat/agents": "^2.4.38",
+ "@librechat/api": "*",
"@librechat/data-schemas": "*",
"@node-saml/passport-saml": "^5.0.0",
"@waylaidwanderer/fetch-event-source": "^3.0.1",
@@ -81,7 +82,6 @@
"keyv-file": "^5.1.2",
"klona": "^2.0.6",
"librechat-data-provider": "*",
- "librechat-mcp": "*",
"lodash": "^4.17.21",
"meilisearch": "^0.38.0",
"memorystore": "^1.6.7",
@@ -90,6 +90,7 @@
"mongoose": "^8.12.1",
"multer": "^2.0.0",
"nanoid": "^3.3.7",
+ "node-fetch": "^2.7.0",
"nodemailer": "^6.9.15",
"ollama": "^0.5.0",
"openai": "^4.96.2",
@@ -110,7 +111,7 @@
"traverse": "^0.6.7",
"ua-parser-js": "^1.0.36",
"winston": "^3.11.0",
- "winston-daily-rotate-file": "^4.7.1",
+ "winston-daily-rotate-file": "^5.0.0",
"youtube-transcript": "^1.2.1",
"zod": "^3.22.4"
},
diff --git a/api/server/cleanup.js b/api/server/cleanup.js
index 5bf336eed5..de7450cea0 100644
--- a/api/server/cleanup.js
+++ b/api/server/cleanup.js
@@ -220,6 +220,9 @@ function disposeClient(client) {
if (client.maxResponseTokens) {
client.maxResponseTokens = null;
}
+ if (client.processMemory) {
+ client.processMemory = null;
+ }
if (client.run) {
// Break circular references in run
if (client.run.Graph) {
diff --git a/api/server/controllers/agents/callbacks.js b/api/server/controllers/agents/callbacks.js
index cedfc6bd62..60e68b5f2d 100644
--- a/api/server/controllers/agents/callbacks.js
+++ b/api/server/controllers/agents/callbacks.js
@@ -1,4 +1,6 @@
const { nanoid } = require('nanoid');
+const { sendEvent } = require('@librechat/api');
+const { logger } = require('@librechat/data-schemas');
const { Tools, StepTypes, FileContext } = require('librechat-data-provider');
const {
EnvVar,
@@ -12,7 +14,6 @@ const {
const { processCodeOutput } = require('~/server/services/Files/Code/process');
const { loadAuthValues } = require('~/server/services/Tools/credentials');
const { saveBase64Image } = require('~/server/services/Files/process');
-const { logger, sendEvent } = require('~/config');
class ModelEndHandler {
/**
@@ -240,9 +241,7 @@ function createToolEndCallback({ req, res, artifactPromises }) {
if (output.artifact[Tools.web_search]) {
artifactPromises.push(
(async () => {
- const name = `${output.name}_${output.tool_call_id}_${nanoid()}`;
const attachment = {
- name,
type: Tools.web_search,
messageId: metadata.run_id,
toolCallId: output.tool_call_id,
diff --git a/api/server/controllers/agents/client.js b/api/server/controllers/agents/client.js
index 9631fe3801..7c8a375a9f 100644
--- a/api/server/controllers/agents/client.js
+++ b/api/server/controllers/agents/client.js
@@ -1,13 +1,12 @@
-// const { HttpsProxyAgent } = require('https-proxy-agent');
-// const {
-// Constants,
-// ImageDetail,
-// EModelEndpoint,
-// resolveHeaders,
-// validateVisionModel,
-// mapModelToAzureConfig,
-// } = require('librechat-data-provider');
require('events').EventEmitter.defaultMaxListeners = 100;
+const { logger } = require('@librechat/data-schemas');
+const {
+ sendEvent,
+ createRun,
+ Tokenizer,
+ memoryInstructions,
+ createMemoryProcessor,
+} = require('@librechat/api');
const {
Callback,
GraphEvents,
@@ -19,26 +18,30 @@ const {
} = require('@librechat/agents');
const {
Constants,
+ Permissions,
VisionModes,
ContentTypes,
EModelEndpoint,
KnownEndpoints,
+ PermissionTypes,
isAgentsEndpoint,
AgentCapabilities,
bedrockInputSchema,
removeNullishValues,
} = require('librechat-data-provider');
+const { DynamicStructuredTool } = require('@langchain/core/tools');
+const { getBufferString, HumanMessage } = require('@langchain/core/messages');
const { getCustomEndpointConfig, checkCapability } = require('~/server/services/Config');
const { addCacheControl, createContextHandlers } = require('~/app/clients/prompts');
+const { initializeAgent } = require('~/server/services/Endpoints/agents/agent');
const { spendTokens, spendStructuredTokens } = require('~/models/spendTokens');
-const { getBufferString, HumanMessage } = require('@langchain/core/messages');
-const { DynamicStructuredTool } = require('@langchain/core/tools');
+const { setMemory, deleteMemory, getFormattedMemories } = require('~/models');
const { encodeAndFormat } = require('~/server/services/Files/images/encode');
const initOpenAI = require('~/server/services/Endpoints/openAI/initialize');
-const Tokenizer = require('~/server/services/Tokenizer');
+const { checkAccess } = require('~/server/middleware/roles/access');
const BaseClient = require('~/app/clients/BaseClient');
-const { logger, sendEvent, getMCPManager } = require('~/config');
-const { createRun } = require('./run');
+const { loadAgent } = require('~/models/Agent');
+const { getMCPManager } = require('~/config');
/**
* @param {ServerRequest} req
@@ -58,12 +61,8 @@ const legacyContentEndpoints = new Set([KnownEndpoints.groq, KnownEndpoints.deep
const noSystemModelRegex = [/\b(o1-preview|o1-mini|amazon\.titan-text)\b/gi];
-// const { processMemory, memoryInstructions } = require('~/server/services/Endpoints/agents/memory');
-// const { getFormattedMemories } = require('~/models/Memory');
-// const { getCurrentDateTime } = require('~/utils');
-
function createTokenCounter(encoding) {
- return (message) => {
+ return function (message) {
const countTokens = (text) => Tokenizer.getTokenCount(text, encoding);
return getTokenCountForMessage(message, countTokens);
};
@@ -124,6 +123,8 @@ class AgentClient extends BaseClient {
this.usage;
/** @type {Record<string, number>} */
this.indexTokenCountMap = {};
+ /** @type {(messages: BaseMessage[]) => Promise<void>} */
+ this.processMemory;
}
/**
@@ -138,55 +139,10 @@ class AgentClient extends BaseClient {
}
/**
- *
- * Checks if the model is a vision model based on request attachments and sets the appropriate options:
- * - Sets `this.modelOptions.model` to `gpt-4-vision-preview` if the request is a vision request.
- * - Sets `this.isVisionModel` to `true` if vision request.
- * - Deletes `this.modelOptions.stop` if vision request.
+ * `AgentClient` is not opinionated about vision requests, so we don't do anything here
* @param {MongoFile[]} attachments
*/
- checkVisionRequest(attachments) {
- // if (!attachments) {
- // return;
- // }
- // const availableModels = this.options.modelsConfig?.[this.options.endpoint];
- // if (!availableModels) {
- // return;
- // }
- // let visionRequestDetected = false;
- // for (const file of attachments) {
- // if (file?.type?.includes('image')) {
- // visionRequestDetected = true;
- // break;
- // }
- // }
- // if (!visionRequestDetected) {
- // return;
- // }
- // this.isVisionModel = validateVisionModel({ model: this.modelOptions.model, availableModels });
- // if (this.isVisionModel) {
- // delete this.modelOptions.stop;
- // return;
- // }
- // for (const model of availableModels) {
- // if (!validateVisionModel({ model, availableModels })) {
- // continue;
- // }
- // this.modelOptions.model = model;
- // this.isVisionModel = true;
- // delete this.modelOptions.stop;
- // return;
- // }
- // if (!availableModels.includes(this.defaultVisionModel)) {
- // return;
- // }
- // if (!validateVisionModel({ model: this.defaultVisionModel, availableModels })) {
- // return;
- // }
- // this.modelOptions.model = this.defaultVisionModel;
- // this.isVisionModel = true;
- // delete this.modelOptions.stop;
- }
+ checkVisionRequest() {}
getSaveOptions() {
// TODO:
@@ -270,24 +226,6 @@ class AgentClient extends BaseClient {
.filter(Boolean)
.join('\n')
.trim();
- // this.systemMessage = getCurrentDateTime();
- // const { withKeys, withoutKeys } = await getFormattedMemories({
- // userId: this.options.req.user.id,
- // });
- // processMemory({
- // userId: this.options.req.user.id,
- // message: this.options.req.body.text,
- // parentMessageId,
- // memory: withKeys,
- // thread_id: this.conversationId,
- // }).catch((error) => {
- // logger.error('Memory Agent failed to process memory', error);
- // });
-
- // this.systemMessage += '\n\n' + memoryInstructions;
- // if (withoutKeys) {
- // this.systemMessage += `\n\n# Existing memory about the user:\n${withoutKeys}`;
- // }
if (this.options.attachments) {
const attachments = await this.options.attachments;
@@ -431,9 +369,150 @@ class AgentClient extends BaseClient {
opts.getReqData({ promptTokens });
}
+ const withoutKeys = await this.useMemory();
+ if (withoutKeys) {
+ systemContent += `${memoryInstructions}\n\n# Existing memory about the user:\n${withoutKeys}`;
+ }
+
+ if (systemContent) {
+ this.options.agent.instructions = systemContent;
+ }
+
return result;
}
+ /**
+ * @returns {Promise<string | undefined>}
+ */
+ async useMemory() {
+ const user = this.options.req.user;
+ if (user.personalization?.memories === false) {
+ return;
+ }
+ const hasAccess = await checkAccess(user, PermissionTypes.MEMORIES, [Permissions.USE]);
+
+ if (!hasAccess) {
+ logger.debug(
+ `[api/server/controllers/agents/client.js #useMemory] User ${user.id} does not have USE permission for memories`,
+ );
+ return;
+ }
+ /** @type {TCustomConfig['memory']} */
+ const memoryConfig = this.options.req?.app?.locals?.memory;
+ if (!memoryConfig || memoryConfig.disabled === true) {
+ return;
+ }
+
+ /** @type {Agent} */
+ let prelimAgent;
+ const allowedProviders = new Set(
+ this.options.req?.app?.locals?.[EModelEndpoint.agents]?.allowedProviders,
+ );
+ try {
+ if (memoryConfig.agent?.id != null && memoryConfig.agent.id !== this.options.agent.id) {
+ prelimAgent = await loadAgent({
+ req: this.options.req,
+ agent_id: memoryConfig.agent.id,
+ endpoint: EModelEndpoint.agents,
+ });
+ } else if (
+ memoryConfig.agent?.id == null &&
+ memoryConfig.agent?.model != null &&
+ memoryConfig.agent?.provider != null
+ ) {
+ prelimAgent = { id: Constants.EPHEMERAL_AGENT_ID, ...memoryConfig.agent };
+ }
+ } catch (error) {
+ logger.error(
+ '[api/server/controllers/agents/client.js #useMemory] Error loading agent for memory',
+ error,
+ );
+ }
+
+ const agent = await initializeAgent({
+ req: this.options.req,
+ res: this.options.res,
+ agent: prelimAgent,
+ allowedProviders,
+ });
+
+ if (!agent) {
+ logger.warn(
+ '[api/server/controllers/agents/client.js #useMemory] No agent found for memory',
+ memoryConfig,
+ );
+ return;
+ }
+
+ const llmConfig = Object.assign(
+ {
+ provider: agent.provider,
+ model: agent.model,
+ },
+ agent.model_parameters,
+ );
+
+ /** @type {import('@librechat/api').MemoryConfig} */
+ const config = {
+ validKeys: memoryConfig.validKeys,
+ instructions: agent.instructions,
+ llmConfig,
+ tokenLimit: memoryConfig.tokenLimit,
+ };
+
+ const userId = this.options.req.user.id + '';
+ const messageId = this.responseMessageId + '';
+ const conversationId = this.conversationId + '';
+ const [withoutKeys, processMemory] = await createMemoryProcessor({
+ userId,
+ config,
+ messageId,
+ conversationId,
+ memoryMethods: {
+ setMemory,
+ deleteMemory,
+ getFormattedMemories,
+ },
+ res: this.options.res,
+ });
+
+ this.processMemory = processMemory;
+ return withoutKeys;
+ }
+
+ /**
+ * @param {BaseMessage[]} messages
+ * @returns {Promise<void | (TAttachment | null)[]>}
+ */
+ async runMemory(messages) {
+ try {
+ if (this.processMemory == null) {
+ return;
+ }
+ /** @type {TCustomConfig['memory']} */
+ const memoryConfig = this.options.req?.app?.locals?.memory;
+ const messageWindowSize = memoryConfig?.messageWindowSize ?? 5;
+
+ let messagesToProcess = [...messages];
+ if (messages.length > messageWindowSize) {
+ for (let i = messages.length - messageWindowSize; i >= 0; i--) {
+ const potentialWindow = messages.slice(i, i + messageWindowSize);
+ if (potentialWindow[0]?.role === 'user') {
+ messagesToProcess = [...potentialWindow];
+ break;
+ }
+ }
+
+ if (messagesToProcess.length === messages.length) {
+ messagesToProcess = [...messages.slice(-messageWindowSize)];
+ }
+ }
+ return await this.processMemory(messagesToProcess);
+ } catch (error) {
+ logger.error('Memory Agent failed to process memory', error);
+ }
+ }
+
/** @type {sendCompletion} */
async sendCompletion(payload, opts = {}) {
await this.chatCompletion({
@@ -576,100 +655,13 @@ class AgentClient extends BaseClient {
let config;
/** @type {ReturnType<createRun>} */
let run;
+ /** @type {Promise<(TAttachment | null)[] | undefined>} */
+ let memoryPromise;
try {
if (!abortController) {
abortController = new AbortController();
}
- // if (this.options.headers) {
- // opts.defaultHeaders = { ...opts.defaultHeaders, ...this.options.headers };
- // }
-
- // if (this.options.proxy) {
- // opts.httpAgent = new HttpsProxyAgent(this.options.proxy);
- // }
-
- // if (this.isVisionModel) {
- // modelOptions.max_tokens = 4000;
- // }
-
- // /** @type {TAzureConfig | undefined} */
- // const azureConfig = this.options?.req?.app?.locals?.[EModelEndpoint.azureOpenAI];
-
- // if (
- // (this.azure && this.isVisionModel && azureConfig) ||
- // (azureConfig && this.isVisionModel && this.options.endpoint === EModelEndpoint.azureOpenAI)
- // ) {
- // const { modelGroupMap, groupMap } = azureConfig;
- // const {
- // azureOptions,
- // baseURL,
- // headers = {},
- // serverless,
- // } = mapModelToAzureConfig({
- // modelName: modelOptions.model,
- // modelGroupMap,
- // groupMap,
- // });
- // opts.defaultHeaders = resolveHeaders(headers);
- // this.langchainProxy = extractBaseURL(baseURL);
- // this.apiKey = azureOptions.azureOpenAIApiKey;
-
- // const groupName = modelGroupMap[modelOptions.model].group;
- // this.options.addParams = azureConfig.groupMap[groupName].addParams;
- // this.options.dropParams = azureConfig.groupMap[groupName].dropParams;
- // // Note: `forcePrompt` not re-assigned as only chat models are vision models
-
- // this.azure = !serverless && azureOptions;
- // this.azureEndpoint =
- // !serverless && genAzureChatCompletion(this.azure, modelOptions.model, this);
- // }
-
- // if (this.azure || this.options.azure) {
- // /* Azure Bug, extremely short default `max_tokens` response */
- // if (!modelOptions.max_tokens && modelOptions.model === 'gpt-4-vision-preview') {
- // modelOptions.max_tokens = 4000;
- // }
-
- // /* Azure does not accept `model` in the body, so we need to remove it. */
- // delete modelOptions.model;
-
- // opts.baseURL = this.langchainProxy
- // ? constructAzureURL({
- // baseURL: this.langchainProxy,
- // azureOptions: this.azure,
- // })
- // : this.azureEndpoint.split(/(?<!\/)\/(chat|completion)\//)[0];
-
- // opts.defaultQuery = { 'api-version': this.azure.azureOpenAIApiVersion };
- // opts.defaultHeaders = { ...opts.defaultHeaders, 'api-key': this.apiKey };
- // }
-
- // if (process.env.OPENAI_ORGANIZATION) {
- // opts.organization = process.env.OPENAI_ORGANIZATION;
- // }
-
- // if (this.options.addParams && typeof this.options.addParams === 'object') {
- // modelOptions = {
- // ...modelOptions,
- // ...this.options.addParams,
- // };
- // logger.debug('[api/server/controllers/agents/client.js #chatCompletion] added params', {
- // addParams: this.options.addParams,
- // modelOptions,
- // });
- // }
-
- // if (this.options.dropParams && Array.isArray(this.options.dropParams)) {
- // this.options.dropParams.forEach((param) => {
- // delete modelOptions[param];
- // });
- // logger.debug('[api/server/controllers/agents/client.js #chatCompletion] dropped params', {
- // dropParams: this.options.dropParams,
- // modelOptions,
- // });
- // }
-
/** @type {TCustomConfig['endpoints']['agents']} */
const agentsEConfig = this.options.req.app.locals[EModelEndpoint.agents];
@@ -766,6 +758,10 @@ class AgentClient extends BaseClient {
messages = addCacheControl(messages);
}
+ if (i === 0) {
+ memoryPromise = this.runMemory(messages);
+ }
+
run = await createRun({
agent,
req: this.options.req,
@@ -801,10 +797,9 @@ class AgentClient extends BaseClient {
run.Graph.contentData = contentData;
}
- const encoding = this.getEncoding();
await run.processStream({ messages }, config, {
keepContent: i !== 0,
- tokenCounter: createTokenCounter(encoding),
+ tokenCounter: createTokenCounter(this.getEncoding()),
indexTokenCountMap: currentIndexCountMap,
maxContextTokens: agent.maxContextTokens,
callbacks: {
@@ -919,6 +914,12 @@ class AgentClient extends BaseClient {
});
try {
+ if (memoryPromise) {
+ const attachments = await memoryPromise;
+ if (attachments && attachments.length > 0) {
+ this.artifactPromises.push(...attachments);
+ }
+ }
await this.recordCollectedUsage({ context: 'message' });
} catch (err) {
logger.error(
@@ -927,6 +928,12 @@ class AgentClient extends BaseClient {
);
}
} catch (err) {
+ if (memoryPromise) {
+ const attachments = await memoryPromise;
+ if (attachments && attachments.length > 0) {
+ this.artifactPromises.push(...attachments);
+ }
+ }
logger.error(
'[api/server/controllers/agents/client.js #sendCompletion] Operation aborted',
err,
diff --git a/api/server/controllers/agents/run.js b/api/server/controllers/agents/run.js
deleted file mode 100644
index 2452e66233..0000000000
--- a/api/server/controllers/agents/run.js
+++ /dev/null
@@ -1,94 +0,0 @@
-const { Run, Providers } = require('@librechat/agents');
-const { providerEndpointMap, KnownEndpoints } = require('librechat-data-provider');
-
-/**
- * @typedef {import('@librechat/agents').t} t
- * @typedef {import('@librechat/agents').StandardGraphConfig} StandardGraphConfig
- * @typedef {import('@librechat/agents').StreamEventData} StreamEventData
- * @typedef {import('@librechat/agents').EventHandler} EventHandler
- * @typedef {import('@librechat/agents').GraphEvents} GraphEvents
- * @typedef {import('@librechat/agents').LLMConfig} LLMConfig
- * @typedef {import('@librechat/agents').IState} IState
- */
-
-const customProviders = new Set([
- Providers.XAI,
- Providers.OLLAMA,
- Providers.DEEPSEEK,
- Providers.OPENROUTER,
-]);
-
-/**
- * Creates a new Run instance with custom handlers and configuration.
- *
- * @param {Object} options - The options for creating the Run instance.
- * @param {ServerRequest} [options.req] - The server request.
- * @param {string | undefined} [options.runId] - Optional run ID; otherwise, a new run ID will be generated.
- * @param {Agent} options.agent - The agent for this run.
- * @param {AbortSignal} options.signal - The signal for this run.
- * @param {Record<GraphEvents, EventHandler> | undefined} [options.customHandlers] - Custom event handlers.
- * @param {boolean} [options.streaming=true] - Whether to use streaming.
- * @param {boolean} [options.streamUsage=true] - Whether to stream usage information.
- * @returns {Promise<Run<IState>>} A promise that resolves to a new Run instance.
- */
-async function createRun({
- runId,
- agent,
- signal,
- customHandlers,
- streaming = true,
- streamUsage = true,
-}) {
- const provider = providerEndpointMap[agent.provider] ?? agent.provider;
- /** @type {LLMConfig} */
- const llmConfig = Object.assign(
- {
- provider,
- streaming,
- streamUsage,
- },
- agent.model_parameters,
- );
-
- /** Resolves issues with new OpenAI usage field */
- if (
- customProviders.has(agent.provider) ||
- (agent.provider === Providers.OPENAI && agent.endpoint !== agent.provider)
- ) {
- llmConfig.streamUsage = false;
- llmConfig.usage = true;
- }
-
- /** @type {'reasoning_content' | 'reasoning'} */
- let reasoningKey;
- if (
- llmConfig.configuration?.baseURL?.includes(KnownEndpoints.openrouter) ||
- (agent.endpoint && agent.endpoint.toLowerCase().includes(KnownEndpoints.openrouter))
- ) {
- reasoningKey = 'reasoning';
- }
-
- /** @type {StandardGraphConfig} */
- const graphConfig = {
- signal,
- llmConfig,
- reasoningKey,
- tools: agent.tools,
- instructions: agent.instructions,
- additional_instructions: agent.additional_instructions,
- // toolEnd: agent.end_after_tools,
- };
-
- // TEMPORARY FOR TESTING
- if (agent.provider === Providers.ANTHROPIC || agent.provider === Providers.BEDROCK) {
- graphConfig.streamBuffer = 2000;
- }
-
- return Run.create({
- runId,
- graphConfig,
- customHandlers,
- });
-}
-
-module.exports = { createRun };
diff --git a/api/server/index.js b/api/server/index.js
index ed770f7703..a04c339b0f 100644
--- a/api/server/index.js
+++ b/api/server/index.js
@@ -117,7 +117,7 @@ const startServer = async () => {
app.use('/api/agents', routes.agents);
app.use('/api/banner', routes.banner);
app.use('/api/bedrock', routes.bedrock);
-
+ app.use('/api/memories', routes.memories);
app.use('/api/tags', routes.tags);
app.use((req, res) => {
diff --git a/api/server/middleware/roles/generateCheckAccess.js b/api/server/middleware/roles/access.js
similarity index 100%
rename from api/server/middleware/roles/generateCheckAccess.js
rename to api/server/middleware/roles/access.js
diff --git a/api/server/middleware/roles/checkAdmin.js b/api/server/middleware/roles/admin.js
similarity index 100%
rename from api/server/middleware/roles/checkAdmin.js
rename to api/server/middleware/roles/admin.js
diff --git a/api/server/middleware/roles/index.js b/api/server/middleware/roles/index.js
index a9fc5b2a08..ebc0043f2f 100644
--- a/api/server/middleware/roles/index.js
+++ b/api/server/middleware/roles/index.js
@@ -1,5 +1,5 @@
-const checkAdmin = require('./checkAdmin');
-const { checkAccess, generateCheckAccess } = require('./generateCheckAccess');
+const checkAdmin = require('./admin');
+const { checkAccess, generateCheckAccess } = require('./access');
module.exports = {
checkAdmin,
diff --git a/api/server/routes/files/multer.js b/api/server/routes/files/multer.js
index f23ecd2823..257c309fa2 100644
--- a/api/server/routes/files/multer.js
+++ b/api/server/routes/files/multer.js
@@ -2,8 +2,8 @@ const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const multer = require('multer');
+const { sanitizeFilename } = require('@librechat/api');
const { fileConfig: defaultFileConfig, mergeFileConfig } = require('librechat-data-provider');
-const { sanitizeFilename } = require('~/server/utils/handleText');
const { getCustomConfig } = require('~/server/services/Config');
const storage = multer.diskStorage({
diff --git a/api/server/routes/index.js b/api/server/routes/index.js
index 449759383d..06e39d3671 100644
--- a/api/server/routes/index.js
+++ b/api/server/routes/index.js
@@ -4,6 +4,7 @@ const tokenizer = require('./tokenizer');
const endpoints = require('./endpoints');
const staticRoute = require('./static');
const messages = require('./messages');
+const memories = require('./memories');
const presets = require('./presets');
const prompts = require('./prompts');
const balance = require('./balance');
@@ -51,6 +52,7 @@ module.exports = {
presets,
balance,
messages,
+ memories,
endpoints,
tokenizer,
assistants,
diff --git a/api/server/routes/memories.js b/api/server/routes/memories.js
new file mode 100644
index 0000000000..86065fecaa
--- /dev/null
+++ b/api/server/routes/memories.js
@@ -0,0 +1,231 @@
+const express = require('express');
+const { Tokenizer } = require('@librechat/api');
+const { PermissionTypes, Permissions } = require('librechat-data-provider');
+const {
+ getAllUserMemories,
+ toggleUserMemories,
+ createMemory,
+ setMemory,
+ deleteMemory,
+} = require('~/models');
+const { requireJwtAuth, generateCheckAccess } = require('~/server/middleware');
+
+const router = express.Router();
+
+const checkMemoryRead = generateCheckAccess(PermissionTypes.MEMORIES, [
+ Permissions.USE,
+ Permissions.READ,
+]);
+const checkMemoryCreate = generateCheckAccess(PermissionTypes.MEMORIES, [
+ Permissions.USE,
+ Permissions.CREATE,
+]);
+const checkMemoryUpdate = generateCheckAccess(PermissionTypes.MEMORIES, [
+ Permissions.USE,
+ Permissions.UPDATE,
+]);
+const checkMemoryDelete = generateCheckAccess(PermissionTypes.MEMORIES, [
+ Permissions.USE,
+ Permissions.UPDATE,
+]);
+const checkMemoryOptOut = generateCheckAccess(PermissionTypes.MEMORIES, [
+ Permissions.USE,
+ Permissions.OPT_OUT,
+]);
+
+router.use(requireJwtAuth);
+
+/**
+ * GET /memories
+ * Returns all memories for the authenticated user, sorted by updated_at (newest first).
+ * Also includes memory usage percentage based on token limit.
+ */
+router.get('/', checkMemoryRead, async (req, res) => {
+ try {
+ const memories = await getAllUserMemories(req.user.id);
+
+ const sortedMemories = memories.sort(
+ (a, b) => new Date(b.updated_at).getTime() - new Date(a.updated_at).getTime(),
+ );
+
+ const totalTokens = memories.reduce((sum, memory) => {
+ return sum + (memory.tokenCount || 0);
+ }, 0);
+
+ const memoryConfig = req.app.locals?.memory;
+ const tokenLimit = memoryConfig?.tokenLimit;
+
+ let usagePercentage = null;
+ if (tokenLimit && tokenLimit > 0) {
+ usagePercentage = Math.min(100, Math.round((totalTokens / tokenLimit) * 100));
+ }
+
+ res.json({
+ memories: sortedMemories,
+ totalTokens,
+ tokenLimit: tokenLimit || null,
+ usagePercentage,
+ });
+ } catch (error) {
+ res.status(500).json({ error: error.message });
+ }
+});
+
+/**
+ * POST /memories
+ * Creates a new memory entry for the authenticated user.
+ * Body: { key: string, value: string }
+ * Returns 201 and { created: true, memory: <createdDoc> } when successful.
+ */
+router.post('/', checkMemoryCreate, async (req, res) => {
+ const { key, value } = req.body;
+
+ if (typeof key !== 'string' || key.trim() === '') {
+ return res.status(400).json({ error: 'Key is required and must be a non-empty string.' });
+ }
+
+ if (typeof value !== 'string' || value.trim() === '') {
+ return res.status(400).json({ error: 'Value is required and must be a non-empty string.' });
+ }
+
+ try {
+ const tokenCount = Tokenizer.getTokenCount(value, 'o200k_base');
+
+ const memories = await getAllUserMemories(req.user.id);
+
+ // Check token limit
+ const memoryConfig = req.app.locals?.memory;
+ const tokenLimit = memoryConfig?.tokenLimit;
+
+ if (tokenLimit) {
+ const currentTotalTokens = memories.reduce(
+ (sum, memory) => sum + (memory.tokenCount || 0),
+ 0,
+ );
+ if (currentTotalTokens + tokenCount > tokenLimit) {
+ return res.status(400).json({
+ error: `Adding this memory would exceed the token limit of ${tokenLimit}. Current usage: ${currentTotalTokens} tokens.`,
+ });
+ }
+ }
+
+ const result = await createMemory({
+ userId: req.user.id,
+ key: key.trim(),
+ value: value.trim(),
+ tokenCount,
+ });
+
+ if (!result.ok) {
+ return res.status(500).json({ error: 'Failed to create memory.' });
+ }
+
+ const updatedMemories = await getAllUserMemories(req.user.id);
+ const newMemory = updatedMemories.find((m) => m.key === key.trim());
+
+ res.status(201).json({ created: true, memory: newMemory });
+ } catch (error) {
+ if (error.message && error.message.includes('already exists')) {
+ return res.status(409).json({ error: 'Memory with this key already exists.' });
+ }
+ res.status(500).json({ error: error.message });
+ }
+});
+
+/**
+ * PATCH /memories/preferences
+ * Updates the user's memory preferences (e.g., enabling/disabling memories).
+ * Body: { memories: boolean }
+ * Returns 200 and { updated: true, preferences: { memories: boolean } } when successful.
+ */
+router.patch('/preferences', checkMemoryOptOut, async (req, res) => {
+ const { memories } = req.body;
+
+ if (typeof memories !== 'boolean') {
+ return res.status(400).json({ error: 'memories must be a boolean value.' });
+ }
+
+ try {
+ const updatedUser = await toggleUserMemories(req.user.id, memories);
+
+ if (!updatedUser) {
+ return res.status(404).json({ error: 'User not found.' });
+ }
+
+ res.json({
+ updated: true,
+ preferences: {
+ memories: updatedUser.personalization?.memories ?? true,
+ },
+ });
+ } catch (error) {
+ res.status(500).json({ error: error.message });
+ }
+});
+
+/**
+ * PATCH /memories/:key
+ * Updates the value of an existing memory entry for the authenticated user.
+ * Body: { value: string }
+ * Returns 200 and { updated: true, memory: <updatedDoc> } when successful.
+ */
+router.patch('/:key', checkMemoryUpdate, async (req, res) => {
+ const { key } = req.params;
+ const { value } = req.body || {};
+
+ if (typeof value !== 'string' || value.trim() === '') {
+ return res.status(400).json({ error: 'Value is required and must be a non-empty string.' });
+ }
+
+ try {
+ const tokenCount = Tokenizer.getTokenCount(value, 'o200k_base');
+
+ const memories = await getAllUserMemories(req.user.id);
+ const existingMemory = memories.find((m) => m.key === key);
+
+ if (!existingMemory) {
+ return res.status(404).json({ error: 'Memory not found.' });
+ }
+
+ const result = await setMemory({
+ userId: req.user.id,
+ key,
+ value,
+ tokenCount,
+ });
+
+ if (!result.ok) {
+ return res.status(500).json({ error: 'Failed to update memory.' });
+ }
+
+ const updatedMemories = await getAllUserMemories(req.user.id);
+ const updatedMemory = updatedMemories.find((m) => m.key === key);
+
+ res.json({ updated: true, memory: updatedMemory });
+ } catch (error) {
+ res.status(500).json({ error: error.message });
+ }
+});
+
+/**
+ * DELETE /memories/:key
+ * Deletes a memory entry for the authenticated user.
+ * Returns 200 and { deleted: true } when successful.
+ */
+router.delete('/:key', checkMemoryDelete, async (req, res) => {
+ const { key } = req.params;
+
+ try {
+ const result = await deleteMemory({ userId: req.user.id, key });
+
+ if (!result.ok) {
+ return res.status(404).json({ error: 'Memory not found.' });
+ }
+
+ res.json({ deleted: true });
+ } catch (error) {
+ res.status(500).json({ error: error.message });
+ }
+});
+
+module.exports = router;
diff --git a/api/server/routes/roles.js b/api/server/routes/roles.js
index 17768c7de6..aefbfcec0c 100644
--- a/api/server/routes/roles.js
+++ b/api/server/routes/roles.js
@@ -1,6 +1,7 @@
const express = require('express');
const {
promptPermissionsSchema,
+ memoryPermissionsSchema,
agentPermissionsSchema,
PermissionTypes,
roleDefaults,
@@ -118,4 +119,43 @@ router.put('/:roleName/agents', checkAdmin, async (req, res) => {
}
});
+/**
+ * PUT /api/roles/:roleName/memories
+ * Update memory permissions for a specific role
+ */
+router.put('/:roleName/memories', checkAdmin, async (req, res) => {
+ const { roleName: _r } = req.params;
+ // TODO: TEMP, use a better parsing for roleName
+ const roleName = _r.toUpperCase();
+ /** @type {TRole['permissions']['MEMORIES']} */
+ const updates = req.body;
+
+ try {
+ const parsedUpdates = memoryPermissionsSchema.partial().parse(updates);
+
+ const role = await getRoleByName(roleName);
+ if (!role) {
+ return res.status(404).send({ message: 'Role not found' });
+ }
+
+ const currentPermissions =
+ role.permissions?.[PermissionTypes.MEMORIES] || role[PermissionTypes.MEMORIES] || {};
+
+ const mergedUpdates = {
+ permissions: {
+ ...role.permissions,
+ [PermissionTypes.MEMORIES]: {
+ ...currentPermissions,
+ ...parsedUpdates,
+ },
+ },
+ };
+
+ const updatedRole = await updateRoleByName(roleName, mergedUpdates);
+ res.status(200).send(updatedRole);
+ } catch (error) {
+ return res.status(400).send({ message: 'Invalid memory permissions.', error: error.errors });
+ }
+});
+
module.exports = router;
diff --git a/api/server/services/ActionService.js b/api/server/services/ActionService.js
index a35c74ad74..ed9b0c447d 100644
--- a/api/server/services/ActionService.js
+++ b/api/server/services/ActionService.js
@@ -1,6 +1,8 @@
const jwt = require('jsonwebtoken');
const { nanoid } = require('nanoid');
+const { sendEvent } = require('@librechat/api');
const { tool } = require('@langchain/core/tools');
+const { logger } = require('@librechat/data-schemas');
const { GraphEvents, sleep } = require('@librechat/agents');
const {
Time,
@@ -13,10 +15,10 @@ const {
actionDomainSeparator,
} = require('librechat-data-provider');
const { refreshAccessToken } = require('~/server/services/TokenService');
-const { logger, getFlowStateManager, sendEvent } = require('~/config');
const { encryptV2, decryptV2 } = require('~/server/utils/crypto');
const { getActions, deleteActions } = require('~/models/Action');
const { deleteAssistant } = require('~/models/Assistant');
+const { getFlowStateManager } = require('~/config');
const { logAxiosError } = require('~/utils');
const { getLogStores } = require('~/cache');
const { findToken } = require('~/models');
diff --git a/api/server/services/AppService.js b/api/server/services/AppService.js
index 4bb8c51d00..dc686d6294 100644
--- a/api/server/services/AppService.js
+++ b/api/server/services/AppService.js
@@ -3,6 +3,7 @@ const {
loadOCRConfig,
processMCPEnv,
EModelEndpoint,
+ loadMemoryConfig,
getConfigDefaults,
loadWebSearchConfig,
} = require('librechat-data-provider');
@@ -44,6 +45,7 @@ const AppService = async (app) => {
const ocr = loadOCRConfig(config.ocr);
const webSearch = loadWebSearchConfig(config.webSearch);
checkWebSearchConfig(webSearch);
+ const memory = loadMemoryConfig(config.memory);
const filteredTools = config.filteredTools;
const includedTools = config.includedTools;
const fileStrategy = config.fileStrategy ?? configDefaults.fileStrategy;
@@ -88,6 +90,7 @@ const AppService = async (app) => {
const defaultLocals = {
ocr,
paths,
+ memory,
webSearch,
fileStrategy,
socialLogins,
diff --git a/api/server/services/Endpoints/agents/agent.js b/api/server/services/Endpoints/agents/agent.js
new file mode 100644
index 0000000000..13a42140db
--- /dev/null
+++ b/api/server/services/Endpoints/agents/agent.js
@@ -0,0 +1,196 @@
+const { Providers } = require('@librechat/agents');
+const { primeResources, optionalChainWithEmptyCheck } = require('@librechat/api');
+const {
+ ErrorTypes,
+ EModelEndpoint,
+ EToolResources,
+ replaceSpecialVars,
+ providerEndpointMap,
+} = require('librechat-data-provider');
+const initAnthropic = require('~/server/services/Endpoints/anthropic/initialize');
+const getBedrockOptions = require('~/server/services/Endpoints/bedrock/options');
+const initOpenAI = require('~/server/services/Endpoints/openAI/initialize');
+const initCustom = require('~/server/services/Endpoints/custom/initialize');
+const initGoogle = require('~/server/services/Endpoints/google/initialize');
+const generateArtifactsPrompt = require('~/app/clients/prompts/artifacts');
+const { getCustomEndpointConfig } = require('~/server/services/Config');
+const { processFiles } = require('~/server/services/Files/process');
+const { getConvoFiles } = require('~/models/Conversation');
+const { getToolFilesByIds } = require('~/models/File');
+const { getModelMaxTokens } = require('~/utils');
+const { getFiles } = require('~/models/File');
+
+const providerConfigMap = {
+ [Providers.XAI]: initCustom,
+ [Providers.OLLAMA]: initCustom,
+ [Providers.DEEPSEEK]: initCustom,
+ [Providers.OPENROUTER]: initCustom,
+ [EModelEndpoint.openAI]: initOpenAI,
+ [EModelEndpoint.google]: initGoogle,
+ [EModelEndpoint.azureOpenAI]: initOpenAI,
+ [EModelEndpoint.anthropic]: initAnthropic,
+ [EModelEndpoint.bedrock]: getBedrockOptions,
+};
+
+/**
+ * @param {object} params
+ * @param {ServerRequest} params.req
+ * @param {ServerResponse} params.res
+ * @param {Agent} params.agent
+ * @param {string | null} [params.conversationId]
+ * @param {Array<IMongoFile>} [params.requestFiles]
+ * @param {typeof import('~/server/services/ToolService').loadAgentTools | undefined} [params.loadTools]
+ * @param {TEndpointOption} [params.endpointOption]
+ * @param {Set<string>} [params.allowedProviders]
+ * @param {boolean} [params.isInitialAgent]
+ * @returns {Promise<Agent & { tools: StructuredTool[], attachments: Array<MongoFile>, toolContextMap: Record<string, unknown>, maxContextTokens: number }>}
+ */
+const initializeAgent = async ({
+ req,
+ res,
+ agent,
+ loadTools,
+ requestFiles,
+ conversationId,
+ endpointOption,
+ allowedProviders,
+ isInitialAgent = false,
+}) => {
+ if (allowedProviders.size > 0 && !allowedProviders.has(agent.provider)) {
+ throw new Error(
+ `{ "type": "${ErrorTypes.INVALID_AGENT_PROVIDER}", "info": "${agent.provider}" }`,
+ );
+ }
+ let currentFiles;
+
+ if (
+ isInitialAgent &&
+ conversationId != null &&
+ (agent.model_parameters?.resendFiles ?? true) === true
+ ) {
+ const fileIds = (await getConvoFiles(conversationId)) ?? [];
+ /** @type {Set<EToolResources>} */
+ const toolResourceSet = new Set();
+ for (const tool of agent.tools) {
+ if (EToolResources[tool]) {
+ toolResourceSet.add(EToolResources[tool]);
+ }
+ }
+ const toolFiles = await getToolFilesByIds(fileIds, toolResourceSet);
+ if (requestFiles.length || toolFiles.length) {
+ currentFiles = await processFiles(requestFiles.concat(toolFiles));
+ }
+ } else if (isInitialAgent && requestFiles.length) {
+ currentFiles = await processFiles(requestFiles);
+ }
+
+ const { attachments, tool_resources } = await primeResources({
+ req,
+ getFiles,
+ attachments: currentFiles,
+ tool_resources: agent.tool_resources,
+ requestFileSet: new Set(requestFiles?.map((file) => file.file_id)),
+ });
+
+ const provider = agent.provider;
+ const { tools, toolContextMap } =
+ (await loadTools?.({
+ req,
+ res,
+ provider,
+ agentId: agent.id,
+ tools: agent.tools,
+ model: agent.model,
+ tool_resources,
+ })) ?? {};
+
+ agent.endpoint = provider;
+ let getOptions = providerConfigMap[provider];
+ if (!getOptions && providerConfigMap[provider.toLowerCase()] != null) {
+ agent.provider = provider.toLowerCase();
+ getOptions = providerConfigMap[agent.provider];
+ } else if (!getOptions) {
+ const customEndpointConfig = await getCustomEndpointConfig(provider);
+ if (!customEndpointConfig) {
+ throw new Error(`Provider ${provider} not supported`);
+ }
+ getOptions = initCustom;
+ agent.provider = Providers.OPENAI;
+ }
+ const model_parameters = Object.assign(
+ {},
+ agent.model_parameters ?? { model: agent.model },
+ isInitialAgent === true ? endpointOption?.model_parameters : {},
+ );
+ const _endpointOption =
+ isInitialAgent === true
+ ? Object.assign({}, endpointOption, { model_parameters })
+ : { model_parameters };
+
+ const options = await getOptions({
+ req,
+ res,
+ optionsOnly: true,
+ overrideEndpoint: provider,
+ overrideModel: agent.model,
+ endpointOption: _endpointOption,
+ });
+
+ if (
+ agent.endpoint === EModelEndpoint.azureOpenAI &&
+ options.llmConfig?.azureOpenAIApiInstanceName == null
+ ) {
+ agent.provider = Providers.OPENAI;
+ }
+
+ if (options.provider != null) {
+ agent.provider = options.provider;
+ }
+
+ /** @type {import('@librechat/agents').ClientOptions} */
+ agent.model_parameters = Object.assign(model_parameters, options.llmConfig);
+ if (options.configOptions) {
+ agent.model_parameters.configuration = options.configOptions;
+ }
+
+ if (!agent.model_parameters.model) {
+ agent.model_parameters.model = agent.model;
+ }
+
+ if (agent.instructions && agent.instructions !== '') {
+ agent.instructions = replaceSpecialVars({
+ text: agent.instructions,
+ user: req.user,
+ });
+ }
+
+ if (typeof agent.artifacts === 'string' && agent.artifacts !== '') {
+ agent.additional_instructions = generateArtifactsPrompt({
+ endpoint: agent.provider,
+ artifacts: agent.artifacts,
+ });
+ }
+
+ const tokensModel =
+ agent.provider === EModelEndpoint.azureOpenAI ? agent.model : agent.model_parameters.model;
+ const maxTokens = optionalChainWithEmptyCheck(
+ agent.model_parameters.maxOutputTokens,
+ agent.model_parameters.maxTokens,
+ 0,
+ );
+ const maxContextTokens = optionalChainWithEmptyCheck(
+ agent.model_parameters.maxContextTokens,
+ agent.max_context_tokens,
+ getModelMaxTokens(tokensModel, providerEndpointMap[provider]),
+ 4096,
+ );
+ return {
+ ...agent,
+ tools,
+ attachments,
+ toolContextMap,
+ maxContextTokens: (maxContextTokens - maxTokens) * 0.9,
+ };
+};
+
+module.exports = { initializeAgent };
diff --git a/api/server/services/Endpoints/agents/initialize.js b/api/server/services/Endpoints/agents/initialize.js
index c9e363e815..e3154fe13a 100644
--- a/api/server/services/Endpoints/agents/initialize.js
+++ b/api/server/services/Endpoints/agents/initialize.js
@@ -1,294 +1,41 @@
-const { createContentAggregator, Providers } = require('@librechat/agents');
-const {
- Constants,
- ErrorTypes,
- EModelEndpoint,
- EToolResources,
- getResponseSender,
- AgentCapabilities,
- replaceSpecialVars,
- providerEndpointMap,
-} = require('librechat-data-provider');
+const { logger } = require('@librechat/data-schemas');
+const { createContentAggregator } = require('@librechat/agents');
+const { Constants, EModelEndpoint, getResponseSender } = require('librechat-data-provider');
const {
getDefaultHandlers,
createToolEndCallback,
} = require('~/server/controllers/agents/callbacks');
-const initAnthropic = require('~/server/services/Endpoints/anthropic/initialize');
-const getBedrockOptions = require('~/server/services/Endpoints/bedrock/options');
-const initOpenAI = require('~/server/services/Endpoints/openAI/initialize');
-const initCustom = require('~/server/services/Endpoints/custom/initialize');
-const initGoogle = require('~/server/services/Endpoints/google/initialize');
-const generateArtifactsPrompt = require('~/app/clients/prompts/artifacts');
-const { getCustomEndpointConfig } = require('~/server/services/Config');
-const { processFiles } = require('~/server/services/Files/process');
+const { initializeAgent } = require('~/server/services/Endpoints/agents/agent');
const { loadAgentTools } = require('~/server/services/ToolService');
const AgentClient = require('~/server/controllers/agents/client');
-const { getConvoFiles } = require('~/models/Conversation');
-const { getToolFilesByIds } = require('~/models/File');
-const { getModelMaxTokens } = require('~/utils');
const { getAgent } = require('~/models/Agent');
-const { getFiles } = require('~/models/File');
-const { logger } = require('~/config');
-
-const providerConfigMap = {
- [Providers.XAI]: initCustom,
- [Providers.OLLAMA]: initCustom,
- [Providers.DEEPSEEK]: initCustom,
- [Providers.OPENROUTER]: initCustom,
- [EModelEndpoint.openAI]: initOpenAI,
- [EModelEndpoint.google]: initGoogle,
- [EModelEndpoint.azureOpenAI]: initOpenAI,
- [EModelEndpoint.anthropic]: initAnthropic,
- [EModelEndpoint.bedrock]: getBedrockOptions,
-};
-
-/**
- * @param {Object} params
- * @param {ServerRequest} params.req
- * @param {Promise<Array<MongoFile | null>> | undefined} [params.attachments]
- * @param {Set<string>} params.requestFileSet
- * @param {AgentToolResources | undefined} [params.tool_resources]
- * @returns {Promise<{ attachments: Array<MongoFile | undefined> | undefined, tool_resources: AgentToolResources | undefined }>}
- */
-const primeResources = async ({
- req,
- attachments: _attachments,
- tool_resources: _tool_resources,
- requestFileSet,
-}) => {
- try {
- /** @type {Array<MongoFile | undefined> | undefined} */
- let attachments;
- const tool_resources = _tool_resources ?? {};
- const isOCREnabled = (req.app.locals?.[EModelEndpoint.agents]?.capabilities ?? []).includes(
- AgentCapabilities.ocr,
- );
- if (tool_resources[EToolResources.ocr]?.file_ids && isOCREnabled) {
- const context = await getFiles(
- {
- file_id: { $in: tool_resources.ocr.file_ids },
- },
- {},
- {},
- );
- attachments = (attachments ?? []).concat(context);
- }
- if (!_attachments) {
- return { attachments, tool_resources };
- }
- /** @type {Array<MongoFile | undefined> | undefined} */
- const files = await _attachments;
- if (!attachments) {
- /** @type {Array<MongoFile | undefined>} */
- attachments = [];
- }
-
- for (const file of files) {
- if (!file) {
- continue;
- }
- if (file.metadata?.fileIdentifier) {
- const execute_code = tool_resources[EToolResources.execute_code] ?? {};
- if (!execute_code.files) {
- tool_resources[EToolResources.execute_code] = { ...execute_code, files: [] };
- }
- tool_resources[EToolResources.execute_code].files.push(file);
- } else if (file.embedded === true) {
- const file_search = tool_resources[EToolResources.file_search] ?? {};
- if (!file_search.files) {
- tool_resources[EToolResources.file_search] = { ...file_search, files: [] };
- }
- tool_resources[EToolResources.file_search].files.push(file);
- } else if (
- requestFileSet.has(file.file_id) &&
- file.type.startsWith('image') &&
- file.height &&
- file.width
- ) {
- const image_edit = tool_resources[EToolResources.image_edit] ?? {};
- if (!image_edit.files) {
- tool_resources[EToolResources.image_edit] = { ...image_edit, files: [] };
- }
- tool_resources[EToolResources.image_edit].files.push(file);
- }
-
- attachments.push(file);
- }
- return { attachments, tool_resources };
- } catch (error) {
- logger.error('Error priming resources', error);
- return { attachments: _attachments, tool_resources: _tool_resources };
- }
-};
-
-/**
- * @param {...string | number} values
- * @returns {string | number | undefined}
- */
-function optionalChainWithEmptyCheck(...values) {
- for (const value of values) {
- if (value !== undefined && value !== null && value !== '') {
- return value;
- }
- }
- return values[values.length - 1];
-}
-
-/**
- * @param {object} params
- * @param {ServerRequest} params.req
- * @param {ServerResponse} params.res
- * @param {Agent} params.agent
- * @param {Set<string>} [params.allowedProviders]
- * @param {object} [params.endpointOption]
- * @param {boolean} [params.isInitialAgent]
- * @returns {Promise<Agent>}
- */
-const initializeAgentOptions = async ({
- req,
- res,
- agent,
- endpointOption,
- allowedProviders,
- isInitialAgent = false,
-}) => {
- if (allowedProviders.size > 0 && !allowedProviders.has(agent.provider)) {
- throw new Error(
- `{ "type": "${ErrorTypes.INVALID_AGENT_PROVIDER}", "info": "${agent.provider}" }`,
- );
- }
- let currentFiles;
- /** @type {Array<MongoFile>} */
- const requestFiles = req.body.files ?? [];
- if (
- isInitialAgent &&
- req.body.conversationId != null &&
- (agent.model_parameters?.resendFiles ?? true) === true
- ) {
- const fileIds = (await getConvoFiles(req.body.conversationId)) ?? [];
- /** @type {Set<EToolResources>} */
- const toolResourceSet = new Set();
- for (const tool of agent.tools) {
- if (EToolResources[tool]) {
- toolResourceSet.add(EToolResources[tool]);
- }
- }
- const toolFiles = await getToolFilesByIds(fileIds, toolResourceSet);
- if (requestFiles.length || toolFiles.length) {
- currentFiles = await processFiles(requestFiles.concat(toolFiles));
- }
- } else if (isInitialAgent && requestFiles.length) {
- currentFiles = await processFiles(requestFiles);
- }
-
- const { attachments, tool_resources } = await primeResources({
- req,
- attachments: currentFiles,
- tool_resources: agent.tool_resources,
- requestFileSet: new Set(requestFiles.map((file) => file.file_id)),
- });
-
- const provider = agent.provider;
- const { tools, toolContextMap } = await loadAgentTools({
- req,
- res,
- agent: {
- id: agent.id,
- tools: agent.tools,
- provider,
- model: agent.model,
- },
- tool_resources,
- });
- agent.endpoint = provider;
- let getOptions = providerConfigMap[provider];
- if (!getOptions && providerConfigMap[provider.toLowerCase()] != null) {
- agent.provider = provider.toLowerCase();
- getOptions = providerConfigMap[agent.provider];
- } else if (!getOptions) {
- const customEndpointConfig = await getCustomEndpointConfig(provider);
- if (!customEndpointConfig) {
- throw new Error(`Provider ${provider} not supported`);
+function createToolLoader() {
+ /**
+ * @param {object} params
+ * @param {ServerRequest} params.req
+ * @param {ServerResponse} params.res
+ * @param {string} params.agentId
+ * @param {string[]} params.tools
+ * @param {string} params.provider
+ * @param {string} params.model
+ * @param {AgentToolResources} params.tool_resources
+ * @returns {Promise<{ tools: StructuredTool[], toolContextMap: Record<string, unknown> } | undefined>}
+ */
+ return async function loadTools({ req, res, agentId, tools, provider, model, tool_resources }) {
+ const agent = { id: agentId, tools, provider, model };
+ try {
+ return await loadAgentTools({
+ req,
+ res,
+ agent,
+ tool_resources,
+ });
+ } catch (error) {
+ logger.error('Error loading tools for agent ' + agentId, error);
}
- getOptions = initCustom;
- agent.provider = Providers.OPENAI;
- }
- const model_parameters = Object.assign(
- {},
- agent.model_parameters ?? { model: agent.model },
- isInitialAgent === true ? endpointOption?.model_parameters : {},
- );
- const _endpointOption =
- isInitialAgent === true
- ? Object.assign({}, endpointOption, { model_parameters })
- : { model_parameters };
-
- const options = await getOptions({
- req,
- res,
- optionsOnly: true,
- overrideEndpoint: provider,
- overrideModel: agent.model,
- endpointOption: _endpointOption,
- });
-
- if (
- agent.endpoint === EModelEndpoint.azureOpenAI &&
- options.llmConfig?.azureOpenAIApiInstanceName == null
- ) {
- agent.provider = Providers.OPENAI;
- }
-
- if (options.provider != null) {
- agent.provider = options.provider;
- }
-
- /** @type {import('@librechat/agents').ClientOptions} */
- agent.model_parameters = Object.assign(model_parameters, options.llmConfig);
- if (options.configOptions) {
- agent.model_parameters.configuration = options.configOptions;
- }
-
- if (!agent.model_parameters.model) {
- agent.model_parameters.model = agent.model;
- }
-
- if (agent.instructions && agent.instructions !== '') {
- agent.instructions = replaceSpecialVars({
- text: agent.instructions,
- user: req.user,
- });
- }
-
- if (typeof agent.artifacts === 'string' && agent.artifacts !== '') {
- agent.additional_instructions = generateArtifactsPrompt({
- endpoint: agent.provider,
- artifacts: agent.artifacts,
- });
- }
-
- const tokensModel =
- agent.provider === EModelEndpoint.azureOpenAI ? agent.model : agent.model_parameters.model;
- const maxTokens = optionalChainWithEmptyCheck(
- agent.model_parameters.maxOutputTokens,
- agent.model_parameters.maxTokens,
- 0,
- );
- const maxContextTokens = optionalChainWithEmptyCheck(
- agent.model_parameters.maxContextTokens,
- agent.max_context_tokens,
- getModelMaxTokens(tokensModel, providerEndpointMap[provider]),
- 4096,
- );
- return {
- ...agent,
- tools,
- attachments,
- toolContextMap,
- maxContextTokens: (maxContextTokens - maxTokens) * 0.9,
};
-};
+}
const initializeClient = async ({ req, res, endpointOption }) => {
if (!endpointOption) {
@@ -313,7 +60,6 @@ const initializeClient = async ({ req, res, endpointOption }) => {
throw new Error('No agent promise provided');
}
- // Initialize primary agent
const primaryAgent = await endpointOption.agent;
if (!primaryAgent) {
throw new Error('Agent not found');
@@ -323,10 +69,18 @@ const initializeClient = async ({ req, res, endpointOption }) => {
/** @type {Set<string>} */
const allowedProviders = new Set(req?.app?.locals?.[EModelEndpoint.agents]?.allowedProviders);
- // Handle primary agent
- const primaryConfig = await initializeAgentOptions({
+ const loadTools = createToolLoader();
+ /** @type {Array<MongoFile>} */
+ const requestFiles = req.body.files ?? [];
+ /** @type {string} */
+ const conversationId = req.body.conversationId;
+
+ const primaryConfig = await initializeAgent({
req,
res,
+ loadTools,
+ requestFiles,
+ conversationId,
agent: primaryAgent,
endpointOption,
allowedProviders,
@@ -340,10 +94,13 @@ const initializeClient = async ({ req, res, endpointOption }) => {
if (!agent) {
throw new Error(`Agent ${agentId} not found`);
}
- const config = await initializeAgentOptions({
+ const config = await initializeAgent({
req,
res,
agent,
+ loadTools,
+ requestFiles,
+ conversationId,
endpointOption,
allowedProviders,
});
diff --git a/api/server/services/Endpoints/azureAssistants/initialize.js b/api/server/services/Endpoints/azureAssistants/initialize.js
index fc8024af07..88acef23e5 100644
--- a/api/server/services/Endpoints/azureAssistants/initialize.js
+++ b/api/server/services/Endpoints/azureAssistants/initialize.js
@@ -1,5 +1,6 @@
const OpenAI = require('openai');
const { HttpsProxyAgent } = require('https-proxy-agent');
+const { constructAzureURL, isUserProvided } = require('@librechat/api');
const {
ErrorTypes,
EModelEndpoint,
@@ -12,8 +13,6 @@ const {
checkUserKeyExpiry,
} = require('~/server/services/UserService');
const OpenAIClient = require('~/app/clients/OpenAIClient');
-const { isUserProvided } = require('~/server/utils');
-const { constructAzureURL } = require('~/utils');
class Files {
constructor(client) {
diff --git a/api/server/services/Endpoints/bedrock/options.js b/api/server/services/Endpoints/bedrock/options.js
index da332060e9..fc5536abbf 100644
--- a/api/server/services/Endpoints/bedrock/options.js
+++ b/api/server/services/Endpoints/bedrock/options.js
@@ -1,4 +1,5 @@
const { HttpsProxyAgent } = require('https-proxy-agent');
+const { createHandleLLMNewToken } = require('@librechat/api');
const {
AuthType,
Constants,
@@ -8,7 +9,6 @@ const {
removeNullishValues,
} = require('librechat-data-provider');
const { getUserKey, checkUserKeyExpiry } = require('~/server/services/UserService');
-const { createHandleLLMNewToken } = require('~/app/clients/generators');
const getOptions = async ({ req, overrideModel, endpointOption }) => {
const {
diff --git a/api/server/services/Endpoints/custom/initialize.js b/api/server/services/Endpoints/custom/initialize.js
index 39def8d0d5..754abef5a8 100644
--- a/api/server/services/Endpoints/custom/initialize.js
+++ b/api/server/services/Endpoints/custom/initialize.js
@@ -6,10 +6,9 @@ const {
extractEnvVariable,
} = require('librechat-data-provider');
const { Providers } = require('@librechat/agents');
+const { getOpenAIConfig, createHandleLLMNewToken } = require('@librechat/api');
const { getUserKeyValues, checkUserKeyExpiry } = require('~/server/services/UserService');
-const { getLLMConfig } = require('~/server/services/Endpoints/openAI/llm');
const { getCustomEndpointConfig } = require('~/server/services/Config');
-const { createHandleLLMNewToken } = require('~/app/clients/generators');
const { fetchModels } = require('~/server/services/ModelService');
const OpenAIClient = require('~/app/clients/OpenAIClient');
const { isUserProvided } = require('~/server/utils');
@@ -144,7 +143,7 @@ const initializeClient = async ({ req, res, endpointOption, optionsOnly, overrid
clientOptions,
);
clientOptions.modelOptions.user = req.user.id;
- const options = getLLMConfig(apiKey, clientOptions, endpoint);
+ const options = getOpenAIConfig(apiKey, clientOptions, endpoint);
if (!customOptions.streamRate) {
return options;
}
diff --git a/api/server/services/Endpoints/gptPlugins/initialize.js b/api/server/services/Endpoints/gptPlugins/initialize.js
index 7bfb43f004..d2af6c757e 100644
--- a/api/server/services/Endpoints/gptPlugins/initialize.js
+++ b/api/server/services/Endpoints/gptPlugins/initialize.js
@@ -1,11 +1,10 @@
const {
EModelEndpoint,
- mapModelToAzureConfig,
resolveHeaders,
+ mapModelToAzureConfig,
} = require('librechat-data-provider');
+const { isEnabled, isUserProvided, getAzureCredentials } = require('@librechat/api');
const { getUserKeyValues, checkUserKeyExpiry } = require('~/server/services/UserService');
-const { isEnabled, isUserProvided } = require('~/server/utils');
-const { getAzureCredentials } = require('~/utils');
const { PluginsClient } = require('~/app');
const initializeClient = async ({ req, res, endpointOption }) => {
diff --git a/api/server/services/Endpoints/gptPlugins/initialize.spec.js b/api/server/services/Endpoints/gptPlugins/initialize.spec.js
index 02199c9397..f9cb2750a4 100644
--- a/api/server/services/Endpoints/gptPlugins/initialize.spec.js
+++ b/api/server/services/Endpoints/gptPlugins/initialize.spec.js
@@ -114,11 +114,11 @@ describe('gptPlugins/initializeClient', () => {
test('should initialize PluginsClient with Azure credentials when PLUGINS_USE_AZURE is true', async () => {
process.env.AZURE_API_KEY = 'test-azure-api-key';
(process.env.AZURE_OPENAI_API_INSTANCE_NAME = 'some-value'),
- (process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME = 'some-value'),
- (process.env.AZURE_OPENAI_API_VERSION = 'some-value'),
- (process.env.AZURE_OPENAI_API_COMPLETIONS_DEPLOYMENT_NAME = 'some-value'),
- (process.env.AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME = 'some-value'),
- (process.env.PLUGINS_USE_AZURE = 'true');
+ (process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME = 'some-value'),
+ (process.env.AZURE_OPENAI_API_VERSION = 'some-value'),
+ (process.env.AZURE_OPENAI_API_COMPLETIONS_DEPLOYMENT_NAME = 'some-value'),
+ (process.env.AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME = 'some-value'),
+ (process.env.PLUGINS_USE_AZURE = 'true');
process.env.DEBUG_PLUGINS = 'false';
process.env.OPENAI_SUMMARIZE = 'false';
diff --git a/api/server/services/Endpoints/openAI/initialize.js b/api/server/services/Endpoints/openAI/initialize.js
index 714ed5a1e6..bc0907b3de 100644
--- a/api/server/services/Endpoints/openAI/initialize.js
+++ b/api/server/services/Endpoints/openAI/initialize.js
@@ -4,12 +4,15 @@ const {
resolveHeaders,
mapModelToAzureConfig,
} = require('librechat-data-provider');
+const {
+ isEnabled,
+ isUserProvided,
+ getOpenAIConfig,
+ getAzureCredentials,
+ createHandleLLMNewToken,
+} = require('@librechat/api');
const { getUserKeyValues, checkUserKeyExpiry } = require('~/server/services/UserService');
-const { getLLMConfig } = require('~/server/services/Endpoints/openAI/llm');
-const { createHandleLLMNewToken } = require('~/app/clients/generators');
-const { isEnabled, isUserProvided } = require('~/server/utils');
const OpenAIClient = require('~/app/clients/OpenAIClient');
-const { getAzureCredentials } = require('~/utils');
const initializeClient = async ({
req,
@@ -140,7 +143,7 @@ const initializeClient = async ({
modelOptions.model = modelName;
clientOptions = Object.assign({ modelOptions }, clientOptions);
clientOptions.modelOptions.user = req.user.id;
- const options = getLLMConfig(apiKey, clientOptions);
+ const options = getOpenAIConfig(apiKey, clientOptions);
const streamRate = clientOptions.streamRate;
if (!streamRate) {
return options;
diff --git a/api/server/services/Endpoints/openAI/llm.js b/api/server/services/Endpoints/openAI/llm.js
deleted file mode 100644
index c1fd090b28..0000000000
--- a/api/server/services/Endpoints/openAI/llm.js
+++ /dev/null
@@ -1,170 +0,0 @@
-const { HttpsProxyAgent } = require('https-proxy-agent');
-const { KnownEndpoints } = require('librechat-data-provider');
-const { sanitizeModelName, constructAzureURL } = require('~/utils');
-const { isEnabled } = require('~/server/utils');
-
-/**
- * Generates configuration options for creating a language model (LLM) instance.
- * @param {string} apiKey - The API key for authentication.
- * @param {Object} options - Additional options for configuring the LLM.
- * @param {Object} [options.modelOptions] - Model-specific options.
- * @param {string} [options.modelOptions.model] - The name of the model to use.
- * @param {string} [options.modelOptions.user] - The user ID
- * @param {number} [options.modelOptions.temperature] - Controls randomness in output generation (0-2).
- * @param {number} [options.modelOptions.top_p] - Controls diversity via nucleus sampling (0-1).
- * @param {number} [options.modelOptions.frequency_penalty] - Reduces repetition of token sequences (-2 to 2).
- * @param {number} [options.modelOptions.presence_penalty] - Encourages discussing new topics (-2 to 2).
- * @param {number} [options.modelOptions.max_tokens] - The maximum number of tokens to generate.
- * @param {string[]} [options.modelOptions.stop] - Sequences where the API will stop generating further tokens.
- * @param {string} [options.reverseProxyUrl] - URL for a reverse proxy, if used.
- * @param {boolean} [options.useOpenRouter] - Flag to use OpenRouter API.
- * @param {Object} [options.headers] - Additional headers for API requests.
- * @param {string} [options.proxy] - Proxy server URL.
- * @param {Object} [options.azure] - Azure-specific configurations.
- * @param {boolean} [options.streaming] - Whether to use streaming mode.
- * @param {Object} [options.addParams] - Additional parameters to add to the model options.
- * @param {string[]} [options.dropParams] - Parameters to remove from the model options.
- * @param {string|null} [endpoint=null] - The endpoint name
- * @returns {Object} Configuration options for creating an LLM instance.
- */
-function getLLMConfig(apiKey, options = {}, endpoint = null) {
- let {
- modelOptions = {},
- reverseProxyUrl,
- defaultQuery,
- headers,
- proxy,
- azure,
- streaming = true,
- addParams,
- dropParams,
- } = options;
-
- /** @type {OpenAIClientOptions} */
- let llmConfig = {
- streaming,
- };
-
- Object.assign(llmConfig, modelOptions);
-
- if (addParams && typeof addParams === 'object') {
- Object.assign(llmConfig, addParams);
- }
- /** Note: OpenAI Web Search models do not support any known parameters besdies `max_tokens` */
- if (modelOptions.model && /gpt-4o.*search/.test(modelOptions.model)) {
- const searchExcludeParams = [
- 'frequency_penalty',
- 'presence_penalty',
- 'temperature',
- 'top_p',
- 'top_k',
- 'stop',
- 'logit_bias',
- 'seed',
- 'response_format',
- 'n',
- 'logprobs',
- 'user',
- ];
-
- dropParams = dropParams || [];
- dropParams = [...new Set([...dropParams, ...searchExcludeParams])];
- }
-
- if (dropParams && Array.isArray(dropParams)) {
- dropParams.forEach((param) => {
- if (llmConfig[param]) {
- llmConfig[param] = undefined;
- }
- });
- }
-
- let useOpenRouter;
- /** @type {OpenAIClientOptions['configuration']} */
- const configOptions = {};
- if (
- (reverseProxyUrl && reverseProxyUrl.includes(KnownEndpoints.openrouter)) ||
- (endpoint && endpoint.toLowerCase().includes(KnownEndpoints.openrouter))
- ) {
- useOpenRouter = true;
- llmConfig.include_reasoning = true;
- configOptions.baseURL = reverseProxyUrl;
- configOptions.defaultHeaders = Object.assign(
- {
- 'HTTP-Referer': 'https://librechat.ai',
- 'X-Title': 'LibreChat',
- },
- headers,
- );
- } else if (reverseProxyUrl) {
- configOptions.baseURL = reverseProxyUrl;
- if (headers) {
- configOptions.defaultHeaders = headers;
- }
- }
-
- if (defaultQuery) {
- configOptions.defaultQuery = defaultQuery;
- }
-
- if (proxy) {
- const proxyAgent = new HttpsProxyAgent(proxy);
- Object.assign(configOptions, {
- httpAgent: proxyAgent,
- httpsAgent: proxyAgent,
- });
- }
-
- if (azure) {
- const useModelName = isEnabled(process.env.AZURE_USE_MODEL_AS_DEPLOYMENT_NAME);
- azure.azureOpenAIApiDeploymentName = useModelName
- ? sanitizeModelName(llmConfig.model)
- : azure.azureOpenAIApiDeploymentName;
-
- if (process.env.AZURE_OPENAI_DEFAULT_MODEL) {
- llmConfig.model = process.env.AZURE_OPENAI_DEFAULT_MODEL;
- }
-
- if (configOptions.baseURL) {
- const azureURL = constructAzureURL({
- baseURL: configOptions.baseURL,
- azureOptions: azure,
- });
- azure.azureOpenAIBasePath = azureURL.split(`/${azure.azureOpenAIApiDeploymentName}`)[0];
- }
-
- Object.assign(llmConfig, azure);
- llmConfig.model = llmConfig.azureOpenAIApiDeploymentName;
- } else {
- llmConfig.apiKey = apiKey;
- // Object.assign(llmConfig, {
- // configuration: { apiKey },
- // });
- }
-
- if (process.env.OPENAI_ORGANIZATION && this.azure) {
- llmConfig.organization = process.env.OPENAI_ORGANIZATION;
- }
-
- if (useOpenRouter && llmConfig.reasoning_effort != null) {
- llmConfig.reasoning = {
- effort: llmConfig.reasoning_effort,
- };
- delete llmConfig.reasoning_effort;
- }
-
- if (llmConfig?.['max_tokens'] != null) {
- /** @type {number} */
- llmConfig.maxTokens = llmConfig['max_tokens'];
- delete llmConfig['max_tokens'];
- }
-
- return {
- /** @type {OpenAIClientOptions} */
- llmConfig,
- /** @type {OpenAIClientOptions['configuration']} */
- configOptions,
- };
-}
-
-module.exports = { getLLMConfig };
diff --git a/api/server/services/Files/Audio/STTService.js b/api/server/services/Files/Audio/STTService.js
index d6c8cc4146..49a800336b 100644
--- a/api/server/services/Files/Audio/STTService.js
+++ b/api/server/services/Files/Audio/STTService.js
@@ -2,9 +2,9 @@ const axios = require('axios');
const fs = require('fs').promises;
const FormData = require('form-data');
const { Readable } = require('stream');
+const { genAzureEndpoint } = require('@librechat/api');
const { extractEnvVariable, STTProviders } = require('librechat-data-provider');
const { getCustomConfig } = require('~/server/services/Config');
-const { genAzureEndpoint } = require('~/utils');
const { logger } = require('~/config');
/**
diff --git a/api/server/services/Files/Audio/TTSService.js b/api/server/services/Files/Audio/TTSService.js
index cd718fdfc1..34d8202156 100644
--- a/api/server/services/Files/Audio/TTSService.js
+++ b/api/server/services/Files/Audio/TTSService.js
@@ -1,8 +1,8 @@
const axios = require('axios');
+const { genAzureEndpoint } = require('@librechat/api');
const { extractEnvVariable, TTSProviders } = require('librechat-data-provider');
const { getRandomVoiceId, createChunkProcessor, splitTextIntoChunks } = require('./streamAudio');
const { getCustomConfig } = require('~/server/services/Config');
-const { genAzureEndpoint } = require('~/utils');
const { logger } = require('~/config');
/**
diff --git a/api/server/services/MCP.js b/api/server/services/MCP.js
index b9baef462e..dc3d3f0037 100644
--- a/api/server/services/MCP.js
+++ b/api/server/services/MCP.js
@@ -1,6 +1,6 @@
const { z } = require('zod');
const { tool } = require('@langchain/core/tools');
-const { normalizeServerName } = require('librechat-mcp');
+const { normalizeServerName } = require('@librechat/api');
const { Constants: AgentConstants, Providers } = require('@librechat/agents');
const {
Constants,
diff --git a/api/server/services/start/interface.js b/api/server/services/start/interface.js
index 7578c036b2..c98fdb60bc 100644
--- a/api/server/services/start/interface.js
+++ b/api/server/services/start/interface.js
@@ -2,6 +2,7 @@ const {
SystemRoles,
Permissions,
PermissionTypes,
+ isMemoryEnabled,
removeNullishValues,
} = require('librechat-data-provider');
const { updateAccessPermissions } = require('~/models/Role');
@@ -20,6 +21,14 @@ async function loadDefaultInterface(config, configDefaults, roleName = SystemRol
const hasModelSpecs = config?.modelSpecs?.list?.length > 0;
const includesAddedEndpoints = config?.modelSpecs?.addedEndpoints?.length > 0;
+ const memoryConfig = config?.memory;
+ const memoryEnabled = isMemoryEnabled(memoryConfig);
+ /** Only disable memories if memory config is present but disabled/invalid */
+ const shouldDisableMemories = memoryConfig && !memoryEnabled;
+ /** Check if personalization is enabled (defaults to true if memory is configured and enabled) */
+ const isPersonalizationEnabled =
+ memoryConfig && memoryEnabled && memoryConfig.personalize !== false;
+
/** @type {TCustomConfig['interface']} */
const loadedInterface = removeNullishValues({
endpointsMenu:
@@ -33,6 +42,7 @@ async function loadDefaultInterface(config, configDefaults, roleName = SystemRol
privacyPolicy: interfaceConfig?.privacyPolicy ?? defaults.privacyPolicy,
termsOfService: interfaceConfig?.termsOfService ?? defaults.termsOfService,
bookmarks: interfaceConfig?.bookmarks ?? defaults.bookmarks,
+ memories: shouldDisableMemories ? false : (interfaceConfig?.memories ?? defaults.memories),
prompts: interfaceConfig?.prompts ?? defaults.prompts,
multiConvo: interfaceConfig?.multiConvo ?? defaults.multiConvo,
agents: interfaceConfig?.agents ?? defaults.agents,
@@ -45,6 +55,10 @@ async function loadDefaultInterface(config, configDefaults, roleName = SystemRol
await updateAccessPermissions(roleName, {
[PermissionTypes.PROMPTS]: { [Permissions.USE]: loadedInterface.prompts },
[PermissionTypes.BOOKMARKS]: { [Permissions.USE]: loadedInterface.bookmarks },
+ [PermissionTypes.MEMORIES]: {
+ [Permissions.USE]: loadedInterface.memories,
+ [Permissions.OPT_OUT]: isPersonalizationEnabled,
+ },
[PermissionTypes.MULTI_CONVO]: { [Permissions.USE]: loadedInterface.multiConvo },
[PermissionTypes.AGENTS]: { [Permissions.USE]: loadedInterface.agents },
[PermissionTypes.TEMPORARY_CHAT]: { [Permissions.USE]: loadedInterface.temporaryChat },
@@ -54,6 +68,10 @@ async function loadDefaultInterface(config, configDefaults, roleName = SystemRol
await updateAccessPermissions(SystemRoles.ADMIN, {
[PermissionTypes.PROMPTS]: { [Permissions.USE]: loadedInterface.prompts },
[PermissionTypes.BOOKMARKS]: { [Permissions.USE]: loadedInterface.bookmarks },
+ [PermissionTypes.MEMORIES]: {
+ [Permissions.USE]: loadedInterface.memories,
+ [Permissions.OPT_OUT]: isPersonalizationEnabled,
+ },
[PermissionTypes.MULTI_CONVO]: { [Permissions.USE]: loadedInterface.multiConvo },
[PermissionTypes.AGENTS]: { [Permissions.USE]: loadedInterface.agents },
[PermissionTypes.TEMPORARY_CHAT]: { [Permissions.USE]: loadedInterface.temporaryChat },
diff --git a/api/server/services/start/interface.spec.js b/api/server/services/start/interface.spec.js
index d0dcfaf55f..1a05c9cf12 100644
--- a/api/server/services/start/interface.spec.js
+++ b/api/server/services/start/interface.spec.js
@@ -12,6 +12,7 @@ describe('loadDefaultInterface', () => {
interface: {
prompts: true,
bookmarks: true,
+ memories: true,
multiConvo: true,
agents: true,
temporaryChat: true,
@@ -26,6 +27,7 @@ describe('loadDefaultInterface', () => {
expect(updateAccessPermissions).toHaveBeenCalledWith(SystemRoles.USER, {
[PermissionTypes.PROMPTS]: { [Permissions.USE]: true },
[PermissionTypes.BOOKMARKS]: { [Permissions.USE]: true },
+ [PermissionTypes.MEMORIES]: { [Permissions.USE]: true },
[PermissionTypes.MULTI_CONVO]: { [Permissions.USE]: true },
[PermissionTypes.AGENTS]: { [Permissions.USE]: true },
[PermissionTypes.TEMPORARY_CHAT]: { [Permissions.USE]: true },
@@ -39,6 +41,7 @@ describe('loadDefaultInterface', () => {
interface: {
prompts: false,
bookmarks: false,
+ memories: false,
multiConvo: false,
agents: false,
temporaryChat: false,
@@ -53,6 +56,7 @@ describe('loadDefaultInterface', () => {
expect(updateAccessPermissions).toHaveBeenCalledWith(SystemRoles.USER, {
[PermissionTypes.PROMPTS]: { [Permissions.USE]: false },
[PermissionTypes.BOOKMARKS]: { [Permissions.USE]: false },
+ [PermissionTypes.MEMORIES]: { [Permissions.USE]: false },
[PermissionTypes.MULTI_CONVO]: { [Permissions.USE]: false },
[PermissionTypes.AGENTS]: { [Permissions.USE]: false },
[PermissionTypes.TEMPORARY_CHAT]: { [Permissions.USE]: false },
@@ -70,6 +74,7 @@ describe('loadDefaultInterface', () => {
expect(updateAccessPermissions).toHaveBeenCalledWith(SystemRoles.USER, {
[PermissionTypes.PROMPTS]: { [Permissions.USE]: undefined },
[PermissionTypes.BOOKMARKS]: { [Permissions.USE]: undefined },
+ [PermissionTypes.MEMORIES]: { [Permissions.USE]: undefined },
[PermissionTypes.MULTI_CONVO]: { [Permissions.USE]: undefined },
[PermissionTypes.AGENTS]: { [Permissions.USE]: undefined },
[PermissionTypes.TEMPORARY_CHAT]: { [Permissions.USE]: undefined },
@@ -83,6 +88,7 @@ describe('loadDefaultInterface', () => {
interface: {
prompts: undefined,
bookmarks: undefined,
+ memories: undefined,
multiConvo: undefined,
agents: undefined,
temporaryChat: undefined,
@@ -97,6 +103,7 @@ describe('loadDefaultInterface', () => {
expect(updateAccessPermissions).toHaveBeenCalledWith(SystemRoles.USER, {
[PermissionTypes.PROMPTS]: { [Permissions.USE]: undefined },
[PermissionTypes.BOOKMARKS]: { [Permissions.USE]: undefined },
+ [PermissionTypes.MEMORIES]: { [Permissions.USE]: undefined },
[PermissionTypes.MULTI_CONVO]: { [Permissions.USE]: undefined },
[PermissionTypes.AGENTS]: { [Permissions.USE]: undefined },
[PermissionTypes.TEMPORARY_CHAT]: { [Permissions.USE]: undefined },
@@ -110,6 +117,7 @@ describe('loadDefaultInterface', () => {
interface: {
prompts: true,
bookmarks: false,
+ memories: true,
multiConvo: undefined,
agents: true,
temporaryChat: undefined,
@@ -124,6 +132,7 @@ describe('loadDefaultInterface', () => {
expect(updateAccessPermissions).toHaveBeenCalledWith(SystemRoles.USER, {
[PermissionTypes.PROMPTS]: { [Permissions.USE]: true },
[PermissionTypes.BOOKMARKS]: { [Permissions.USE]: false },
+ [PermissionTypes.MEMORIES]: { [Permissions.USE]: true },
[PermissionTypes.MULTI_CONVO]: { [Permissions.USE]: undefined },
[PermissionTypes.AGENTS]: { [Permissions.USE]: true },
[PermissionTypes.TEMPORARY_CHAT]: { [Permissions.USE]: undefined },
@@ -138,6 +147,7 @@ describe('loadDefaultInterface', () => {
interface: {
prompts: true,
bookmarks: true,
+ memories: true,
multiConvo: true,
agents: true,
temporaryChat: true,
@@ -151,6 +161,7 @@ describe('loadDefaultInterface', () => {
expect(updateAccessPermissions).toHaveBeenCalledWith(SystemRoles.USER, {
[PermissionTypes.PROMPTS]: { [Permissions.USE]: true },
[PermissionTypes.BOOKMARKS]: { [Permissions.USE]: true },
+ [PermissionTypes.MEMORIES]: { [Permissions.USE]: true },
[PermissionTypes.MULTI_CONVO]: { [Permissions.USE]: true },
[PermissionTypes.AGENTS]: { [Permissions.USE]: true },
[PermissionTypes.TEMPORARY_CHAT]: { [Permissions.USE]: true },
@@ -168,6 +179,7 @@ describe('loadDefaultInterface', () => {
expect(updateAccessPermissions).toHaveBeenCalledWith(SystemRoles.USER, {
[PermissionTypes.PROMPTS]: { [Permissions.USE]: undefined },
[PermissionTypes.BOOKMARKS]: { [Permissions.USE]: undefined },
+ [PermissionTypes.MEMORIES]: { [Permissions.USE]: undefined },
[PermissionTypes.MULTI_CONVO]: { [Permissions.USE]: true },
[PermissionTypes.AGENTS]: { [Permissions.USE]: undefined },
[PermissionTypes.TEMPORARY_CHAT]: { [Permissions.USE]: undefined },
@@ -185,6 +197,7 @@ describe('loadDefaultInterface', () => {
expect(updateAccessPermissions).toHaveBeenCalledWith(SystemRoles.USER, {
[PermissionTypes.PROMPTS]: { [Permissions.USE]: undefined },
[PermissionTypes.BOOKMARKS]: { [Permissions.USE]: undefined },
+ [PermissionTypes.MEMORIES]: { [Permissions.USE]: undefined },
[PermissionTypes.MULTI_CONVO]: { [Permissions.USE]: false },
[PermissionTypes.AGENTS]: { [Permissions.USE]: undefined },
[PermissionTypes.TEMPORARY_CHAT]: { [Permissions.USE]: undefined },
@@ -202,6 +215,7 @@ describe('loadDefaultInterface', () => {
expect(updateAccessPermissions).toHaveBeenCalledWith(SystemRoles.USER, {
[PermissionTypes.PROMPTS]: { [Permissions.USE]: undefined },
[PermissionTypes.BOOKMARKS]: { [Permissions.USE]: undefined },
+ [PermissionTypes.MEMORIES]: { [Permissions.USE]: undefined },
[PermissionTypes.MULTI_CONVO]: { [Permissions.USE]: undefined },
[PermissionTypes.AGENTS]: { [Permissions.USE]: undefined },
[PermissionTypes.TEMPORARY_CHAT]: { [Permissions.USE]: undefined },
@@ -215,6 +229,7 @@ describe('loadDefaultInterface', () => {
interface: {
prompts: true,
bookmarks: false,
+ memories: true,
multiConvo: true,
agents: false,
temporaryChat: true,
@@ -228,6 +243,7 @@ describe('loadDefaultInterface', () => {
expect(updateAccessPermissions).toHaveBeenCalledWith(SystemRoles.USER, {
[PermissionTypes.PROMPTS]: { [Permissions.USE]: true },
[PermissionTypes.BOOKMARKS]: { [Permissions.USE]: false },
+ [PermissionTypes.MEMORIES]: { [Permissions.USE]: true },
[PermissionTypes.MULTI_CONVO]: { [Permissions.USE]: true },
[PermissionTypes.AGENTS]: { [Permissions.USE]: false },
[PermissionTypes.TEMPORARY_CHAT]: { [Permissions.USE]: true },
@@ -242,6 +258,7 @@ describe('loadDefaultInterface', () => {
interface: {
prompts: true,
bookmarks: true,
+ memories: false,
multiConvo: false,
agents: undefined,
temporaryChat: undefined,
@@ -255,6 +272,7 @@ describe('loadDefaultInterface', () => {
expect(updateAccessPermissions).toHaveBeenCalledWith(SystemRoles.USER, {
[PermissionTypes.PROMPTS]: { [Permissions.USE]: true },
[PermissionTypes.BOOKMARKS]: { [Permissions.USE]: true },
+ [PermissionTypes.MEMORIES]: { [Permissions.USE]: false },
[PermissionTypes.MULTI_CONVO]: { [Permissions.USE]: false },
[PermissionTypes.AGENTS]: { [Permissions.USE]: undefined },
[PermissionTypes.TEMPORARY_CHAT]: { [Permissions.USE]: undefined },
@@ -268,6 +286,7 @@ describe('loadDefaultInterface', () => {
interface: {
prompts: true,
bookmarks: false,
+ memories: true,
multiConvo: true,
agents: false,
temporaryChat: true,
@@ -281,6 +300,7 @@ describe('loadDefaultInterface', () => {
expect(updateAccessPermissions).toHaveBeenCalledWith(SystemRoles.USER, {
[PermissionTypes.PROMPTS]: { [Permissions.USE]: true },
[PermissionTypes.BOOKMARKS]: { [Permissions.USE]: false },
+ [PermissionTypes.MEMORIES]: { [Permissions.USE]: true },
[PermissionTypes.MULTI_CONVO]: { [Permissions.USE]: true },
[PermissionTypes.AGENTS]: { [Permissions.USE]: false },
[PermissionTypes.TEMPORARY_CHAT]: { [Permissions.USE]: true },
diff --git a/api/server/utils/handleText.js b/api/server/utils/handleText.js
index 86c17f1dda..680da5da44 100644
--- a/api/server/utils/handleText.js
+++ b/api/server/utils/handleText.js
@@ -1,5 +1,3 @@
-const path = require('path');
-const crypto = require('crypto');
const {
Capabilities,
EModelEndpoint,
@@ -218,38 +216,6 @@ function normalizeEndpointName(name = '') {
return name.toLowerCase() === Providers.OLLAMA ? Providers.OLLAMA : name;
}
-/**
- * Sanitize a filename by removing any directory components, replacing non-alphanumeric characters
- * @param {string} inputName
- * @returns {string}
- */
-function sanitizeFilename(inputName) {
- // Remove any directory components
- let name = path.basename(inputName);
-
- // Replace any non-alphanumeric characters except for '.' and '-'
- name = name.replace(/[^a-zA-Z0-9.-]/g, '_');
-
- // Ensure the name doesn't start with a dot (hidden file in Unix-like systems)
- if (name.startsWith('.') || name === '') {
- name = '_' + name;
- }
-
- // Limit the length of the filename
- const MAX_LENGTH = 255;
- if (name.length > MAX_LENGTH) {
- const ext = path.extname(name);
- const nameWithoutExt = path.basename(name, ext);
- name =
- nameWithoutExt.slice(0, MAX_LENGTH - ext.length - 7) +
- '-' +
- crypto.randomBytes(3).toString('hex') +
- ext;
- }
-
- return name;
-}
-
module.exports = {
isEnabled,
handleText,
@@ -260,6 +226,5 @@ module.exports = {
generateConfig,
addSpaceIfNeeded,
createOnProgress,
- sanitizeFilename,
normalizeEndpointName,
};
diff --git a/api/server/utils/handleText.spec.js b/api/server/utils/handleText.spec.js
deleted file mode 100644
index 2cd6c51f91..0000000000
--- a/api/server/utils/handleText.spec.js
+++ /dev/null
@@ -1,103 +0,0 @@
-const { isEnabled, sanitizeFilename } = require('./handleText');
-
-describe('isEnabled', () => {
- test('should return true when input is "true"', () => {
- expect(isEnabled('true')).toBe(true);
- });
-
- test('should return true when input is "TRUE"', () => {
- expect(isEnabled('TRUE')).toBe(true);
- });
-
- test('should return true when input is true', () => {
- expect(isEnabled(true)).toBe(true);
- });
-
- test('should return false when input is "false"', () => {
- expect(isEnabled('false')).toBe(false);
- });
-
- test('should return false when input is false', () => {
- expect(isEnabled(false)).toBe(false);
- });
-
- test('should return false when input is null', () => {
- expect(isEnabled(null)).toBe(false);
- });
-
- test('should return false when input is undefined', () => {
- expect(isEnabled()).toBe(false);
- });
-
- test('should return false when input is an empty string', () => {
- expect(isEnabled('')).toBe(false);
- });
-
- test('should return false when input is a whitespace string', () => {
- expect(isEnabled(' ')).toBe(false);
- });
-
- test('should return false when input is a number', () => {
- expect(isEnabled(123)).toBe(false);
- });
-
- test('should return false when input is an object', () => {
- expect(isEnabled({})).toBe(false);
- });
-
- test('should return false when input is an array', () => {
- expect(isEnabled([])).toBe(false);
- });
-});
-
-jest.mock('crypto', () => {
- const actualModule = jest.requireActual('crypto');
- return {
- ...actualModule,
- randomBytes: jest.fn().mockReturnValue(Buffer.from('abc123', 'hex')),
- };
-});
-
-describe('sanitizeFilename', () => {
- test('removes directory components (1/2)', () => {
- expect(sanitizeFilename('/path/to/file.txt')).toBe('file.txt');
- });
-
- test('removes directory components (2/2)', () => {
- expect(sanitizeFilename('../../../../file.txt')).toBe('file.txt');
- });
-
- test('replaces non-alphanumeric characters', () => {
- expect(sanitizeFilename('file name@#$.txt')).toBe('file_name___.txt');
- });
-
- test('preserves dots and hyphens', () => {
- expect(sanitizeFilename('file-name.with.dots.txt')).toBe('file-name.with.dots.txt');
- });
-
- test('prepends underscore to filenames starting with a dot', () => {
- expect(sanitizeFilename('.hiddenfile')).toBe('_.hiddenfile');
- });
-
- test('truncates long filenames', () => {
- const longName = 'a'.repeat(300) + '.txt';
- const result = sanitizeFilename(longName);
- expect(result.length).toBe(255);
- expect(result).toMatch(/^a+-abc123\.txt$/);
- });
-
- test('handles filenames with no extension', () => {
- const longName = 'a'.repeat(300);
- const result = sanitizeFilename(longName);
- expect(result.length).toBe(255);
- expect(result).toMatch(/^a+-abc123$/);
- });
-
- test('handles empty input', () => {
- expect(sanitizeFilename('')).toBe('_');
- });
-
- test('handles input with only special characters', () => {
- expect(sanitizeFilename('@#$%^&*')).toBe('_______');
- });
-});
diff --git a/api/typedefs.js b/api/typedefs.js
index 8da5b34809..5bc7ebf664 100644
--- a/api/typedefs.js
+++ b/api/typedefs.js
@@ -1073,7 +1073,7 @@
/**
* @exports MCPServers
- * @typedef {import('librechat-mcp').MCPServers} MCPServers
+ * @typedef {import('@librechat/api').MCPServers} MCPServers
* @memberof typedefs
*/
@@ -1085,31 +1085,31 @@
/**
* @exports MCPManager
- * @typedef {import('librechat-mcp').MCPManager} MCPManager
+ * @typedef {import('@librechat/api').MCPManager} MCPManager
* @memberof typedefs
*/
/**
* @exports FlowStateManager
- * @typedef {import('librechat-mcp').FlowStateManager} FlowStateManager
+ * @typedef {import('@librechat/api').FlowStateManager} FlowStateManager
* @memberof typedefs
*/
/**
* @exports LCAvailableTools
- * @typedef {import('librechat-mcp').LCAvailableTools} LCAvailableTools
+ * @typedef {import('@librechat/api').LCAvailableTools} LCAvailableTools
* @memberof typedefs
*/
/**
* @exports LCTool
- * @typedef {import('librechat-mcp').LCTool} LCTool
+ * @typedef {import('@librechat/api').LCTool} LCTool
* @memberof typedefs
*/
/**
* @exports FormattedContent
- * @typedef {import('librechat-mcp').FormattedContent} FormattedContent
+ * @typedef {import('@librechat/api').FormattedContent} FormattedContent
* @memberof typedefs
*/
@@ -1232,7 +1232,7 @@
* @typedef {Object} AgentClientOptions
* @property {Agent} agent - The agent configuration object
* @property {string} endpoint - The endpoint identifier for the agent
- * @property {Object} req - The request object
+ * @property {ServerRequest} req - The request object
* @property {string} [name] - The username
* @property {string} [modelLabel] - The label for the model being used
* @property {number} [maxContextTokens] - Maximum number of tokens allowed in context
diff --git a/api/utils/index.js b/api/utils/index.js
index 62d61586bf..6a304efd6d 100644
--- a/api/utils/index.js
+++ b/api/utils/index.js
@@ -1,7 +1,6 @@
const loadYaml = require('./loadYaml');
const axiosHelpers = require('./axios');
const tokenHelpers = require('./tokens');
-const azureUtils = require('./azureUtils');
const deriveBaseURL = require('./deriveBaseURL');
const extractBaseURL = require('./extractBaseURL');
const findMessageContent = require('./findMessageContent');
@@ -10,7 +9,6 @@ module.exports = {
loadYaml,
deriveBaseURL,
extractBaseURL,
- ...azureUtils,
...axiosHelpers,
...tokenHelpers,
findMessageContent,
diff --git a/client/src/components/Chat/Messages/Content/ContentParts.tsx b/client/src/components/Chat/Messages/Content/ContentParts.tsx
index 8c1ff2ebb5..0a1b4616a0 100644
--- a/client/src/components/Chat/Messages/Content/ContentParts.tsx
+++ b/client/src/components/Chat/Messages/Content/ContentParts.tsx
@@ -9,6 +9,7 @@ import type {
} from 'librechat-data-provider';
import { ThinkingButton } from '~/components/Artifacts/Thinking';
import { MessageContext, SearchContext } from '~/Providers';
+import MemoryArtifacts from './MemoryArtifacts';
import Sources from '~/components/Web/Sources';
import useLocalize from '~/hooks/useLocalize';
import { mapAttachments } from '~/utils/map';
@@ -72,6 +73,7 @@ const ContentParts = memo(
return hasThinkPart && allThinkPartsHaveContent;
}, [content]);
+
if (!content) {
return null;
}
@@ -103,6 +105,7 @@ const ContentParts = memo(
return (
<>
<SearchContext.Provider value={{ searchResults }}>
+ <MemoryArtifacts attachments={attachments} />
<Sources />
{hasReasoningParts && (
<div className="mb-5">
diff --git a/client/src/components/Chat/Messages/Content/MemoryArtifacts.tsx b/client/src/components/Chat/Messages/Content/MemoryArtifacts.tsx
new file mode 100644
index 0000000000..7af4e9fcdd
--- /dev/null
+++ b/client/src/components/Chat/Messages/Content/MemoryArtifacts.tsx
@@ -0,0 +1,143 @@
+import { Tools } from 'librechat-data-provider';
+import { useState, useRef, useMemo, useLayoutEffect, useEffect } from 'react';
+import type { MemoryArtifact, TAttachment } from 'librechat-data-provider';
+import MemoryInfo from './MemoryInfo';
+import { useLocalize } from '~/hooks';
+import { cn } from '~/utils';
+
+export default function MemoryArtifacts({ attachments }: { attachments?: TAttachment[] }) {
+ const localize = useLocalize();
+ const [showInfo, setShowInfo] = useState(false);
+ const contentRef = useRef<HTMLDivElement>(null);
+ const [contentHeight, setContentHeight] = useState<number | undefined>(0);
+ const [isAnimating, setIsAnimating] = useState(false);
+ const prevShowInfoRef = useRef<boolean>(showInfo);
+
+ const memoryArtifacts = useMemo(() => {
+ const result: MemoryArtifact[] = [];
+ for (const attachment of attachments ?? []) {
+ if (attachment?.[Tools.memory] != null) {
+ result.push(attachment[Tools.memory]);
+ }
+ }
+ return result;
+ }, [attachments]);
+
+ useLayoutEffect(() => {
+ if (showInfo !== prevShowInfoRef.current) {
+ prevShowInfoRef.current = showInfo;
+ setIsAnimating(true);
+
+ if (showInfo && contentRef.current) {
+ requestAnimationFrame(() => {
+ if (contentRef.current) {
+ const height = contentRef.current.scrollHeight;
+ setContentHeight(height + 4);
+ }
+ });
+ } else {
+ setContentHeight(0);
+ }
+
+ const timer = setTimeout(() => {
+ setIsAnimating(false);
+ }, 400);
+
+ return () => clearTimeout(timer);
+ }
+ }, [showInfo]);
+
+ useEffect(() => {
+ if (!contentRef.current) {
+ return;
+ }
+ const resizeObserver = new ResizeObserver((entries) => {
+ if (showInfo && !isAnimating) {
+ for (const entry of entries) {
+ if (entry.target === contentRef.current) {
+ setContentHeight(entry.contentRect.height + 4);
+ }
+ }
+ }
+ });
+ resizeObserver.observe(contentRef.current);
+ return () => {
+ resizeObserver.disconnect();
+ };
+ }, [showInfo, isAnimating]);
+
+ if (!memoryArtifacts || memoryArtifacts.length === 0) {
+ return null;
+ }
+
+ return (
+ <>
+ <div className="flex items-center">
+ <div className="inline-block">
+ <button
+ className="outline-hidden my-1 flex items-center gap-1 text-sm font-semibold text-text-secondary-alt transition-colors hover:text-text-primary"
+ type="button"
+ onClick={() => setShowInfo((prev) => !prev)}
+ aria-expanded={showInfo}
+ aria-label={localize('com_ui_memory_updated')}
+ >
+ <svg
+ width="18"
+ height="18"
+ viewBox="0 0 18 18"
+ fill="none"
+ xmlns="http://www.w3.org/2000/svg"
+ className="mb-[-1px]"
+ >
+ <path
+ d="M6 3C4.89543 3 4 3.89543 4 5V13C4 14.1046 4.89543 15 6 15L6 3Z"
+ fill="currentColor"
+ />
+ <path
+ d="M7 3V15H8.18037L8.4899 13.4523C8.54798 13.1619 8.69071 12.8952 8.90012 12.6858L12.2931 9.29289C12.7644 8.82153 13.3822 8.58583 14 8.58578V3.5C14 3.22386 13.7761 3 13.5 3H7Z"
+ fill="currentColor"
+ />
+ <path
+ d="M11.3512 15.5297L9.73505 15.8529C9.38519 15.9229 9.07673 15.6144 9.14671 15.2646L9.46993 13.6484C9.48929 13.5517 9.53687 13.4628 9.60667 13.393L12.9996 10C13.5519 9.44771 14.4473 9.44771 14.9996 10C15.5519 10.5523 15.5519 11.4477 14.9996 12L11.6067 15.393C11.5369 15.4628 11.448 15.5103 11.3512 15.5297Z"
+ fill="currentColor"
+ />
+ </svg>
+ {localize('com_ui_memory_updated')}
+ </button>
+ </div>
+ </div>
+ <div
+ className="relative"
+ style={{
+ height: showInfo ? contentHeight : 0,
+ overflow: 'hidden',
+ transition:
+ 'height 0.4s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.4s cubic-bezier(0.16, 1, 0.3, 1)',
+ opacity: showInfo ? 1 : 0,
+ transformOrigin: 'top',
+ willChange: 'height, opacity',
+ perspective: '1000px',
+ backfaceVisibility: 'hidden',
+ WebkitFontSmoothing: 'subpixel-antialiased',
+ }}
+ >
+ <div
+ className={cn(
+ 'overflow-hidden rounded-xl border border-border-light bg-surface-primary-alt shadow-md',
+ showInfo && 'shadow-lg',
+ )}
+ style={{
+ transform: showInfo ? 'translateY(0) scale(1)' : 'translateY(-8px) scale(0.98)',
+ opacity: showInfo ? 1 : 0,
+ transition:
+ 'transform 0.4s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.4s cubic-bezier(0.16, 1, 0.3, 1)',
+ }}
+ >
+ <div ref={contentRef}>
+ {showInfo && <MemoryInfo key="memory-info" memoryArtifacts={memoryArtifacts} />}
+ </div>
+ </div>
+ </div>
+ </>
+ );
+}
diff --git a/client/src/components/Chat/Messages/Content/MemoryInfo.tsx b/client/src/components/Chat/Messages/Content/MemoryInfo.tsx
new file mode 100644
index 0000000000..574c2e8f5f
--- /dev/null
+++ b/client/src/components/Chat/Messages/Content/MemoryInfo.tsx
@@ -0,0 +1,61 @@
+import type { MemoryArtifact } from 'librechat-data-provider';
+import { useLocalize } from '~/hooks';
+
+export default function MemoryInfo({ memoryArtifacts }: { memoryArtifacts: MemoryArtifact[] }) {
+ const localize = useLocalize();
+ if (memoryArtifacts.length === 0) {
+ return null;
+ }
+
+ // Group artifacts by type
+ const updatedMemories = memoryArtifacts.filter((artifact) => artifact.type === 'update');
+ const deletedMemories = memoryArtifacts.filter((artifact) => artifact.type === 'delete');
+
+ if (updatedMemories.length === 0 && deletedMemories.length === 0) {
+ return null;
+ }
+
+ return (
+ <div className="space-y-4 p-4">
+ {updatedMemories.length > 0 && (
+ <div>
+ <h4 className="mb-2 text-sm font-semibold text-text-primary">
+ {localize('com_ui_memory_updated_items')}
+ </h4>
+ <div className="space-y-2">
+ {updatedMemories.map((artifact, index) => (
+ <div key={`update-${index}`} className="rounded-lg p-3">
+ <div className="mb-1 text-xs font-medium uppercase tracking-wide text-text-secondary">
+ {artifact.key}
+ </div>
+ <div className="whitespace-pre-wrap text-sm text-text-primary">
+ {artifact.value}
+ </div>
+ </div>
+ ))}
+ </div>
+ </div>
+ )}
+
+ {deletedMemories.length > 0 && (
+ <div>
+ <h4 className="mb-2 text-sm font-semibold text-text-primary">
+ {localize('com_ui_memory_deleted_items')}
+ </h4>
+ <div className="space-y-2">
+ {deletedMemories.map((artifact, index) => (
+ <div key={`delete-${index}`} className="rounded-lg p-3 opacity-60">
+ <div className="mb-1 text-xs font-medium uppercase tracking-wide text-text-secondary">
+ {artifact.key}
+ </div>
+ <div className="text-sm italic text-text-secondary">
+ {localize('com_ui_memory_deleted')}
+ </div>
+ </div>
+ ))}
+ </div>
+ </div>
+ )}
+ </div>
+ );
+}
diff --git a/client/src/components/Nav/Settings.tsx b/client/src/components/Nav/Settings.tsx
index 7bc1a8e75f..23d7a456b9 100644
--- a/client/src/components/Nav/Settings.tsx
+++ b/client/src/components/Nav/Settings.tsx
@@ -5,9 +5,27 @@ import { SettingsTabValues } from 'librechat-data-provider';
import { useGetStartupConfig } from '~/data-provider';
import type { TDialogProps } from '~/common';
import { Dialog, DialogPanel, DialogTitle, Transition, TransitionChild } from '@headlessui/react';
-import { GearIcon, DataIcon, SpeechIcon, UserIcon, ExperimentIcon } from '~/components/svg';
-import { General, Chat, Speech, Beta, Commands, Data, Account, Balance } from './SettingsTabs';
+import {
+ GearIcon,
+ DataIcon,
+ SpeechIcon,
+ UserIcon,
+ ExperimentIcon,
+ PersonalizationIcon,
+} from '~/components/svg';
+import {
+ General,
+ Chat,
+ Speech,
+ Beta,
+ Commands,
+ Data,
+ Account,
+ Balance,
+ Personalization,
+} from './SettingsTabs';
import { useMediaQuery, useLocalize, TranslationKeys } from '~/hooks';
+import usePersonalizationAccess from '~/hooks/usePersonalizationAccess';
import { cn } from '~/utils';
export default function Settings({ open, onOpenChange }: TDialogProps) {
@@ -16,6 +34,7 @@ export default function Settings({ open, onOpenChange }: TDialogProps) {
const localize = useLocalize();
const [activeTab, setActiveTab] = useState(SettingsTabValues.GENERAL);
const tabRefs = useRef({});
+ const { hasAnyPersonalizationFeature, hasMemoryOptOut } = usePersonalizationAccess();
const handleKeyDown = (event: React.KeyboardEvent) => {
const tabs: SettingsTabValues[] = [
@@ -24,6 +43,7 @@ export default function Settings({ open, onOpenChange }: TDialogProps) {
SettingsTabValues.BETA,
SettingsTabValues.COMMANDS,
SettingsTabValues.SPEECH,
+ ...(hasAnyPersonalizationFeature ? [SettingsTabValues.PERSONALIZATION] : []),
SettingsTabValues.DATA,
...(startupConfig?.balance?.enabled ? [SettingsTabValues.BALANCE] : []),
SettingsTabValues.ACCOUNT,
@@ -80,6 +100,15 @@ export default function Settings({ open, onOpenChange }: TDialogProps) {
icon: <SpeechIcon className="icon-sm" />,
label: 'com_nav_setting_speech',
},
+ ...(hasAnyPersonalizationFeature
+ ? [
+ {
+ value: SettingsTabValues.PERSONALIZATION,
+ icon: <PersonalizationIcon />,
+ label: 'com_nav_setting_personalization' as TranslationKeys,
+ },
+ ]
+ : []),
{
value: SettingsTabValues.DATA,
icon: <DataIcon />,
@@ -87,11 +116,11 @@ export default function Settings({ open, onOpenChange }: TDialogProps) {
},
...(startupConfig?.balance?.enabled
? [
- {
- value: SettingsTabValues.BALANCE,
+ {
+ value: SettingsTabValues.BALANCE,
icon: <DollarSign size={18} />,
- label: 'com_nav_setting_balance' as TranslationKeys,
- },
+ label: 'com_nav_setting_balance' as TranslationKeys,
+ },
]
: ([] as { value: SettingsTabValues; icon: React.JSX.Element; label: TranslationKeys }[])),
{
@@ -213,6 +242,14 @@ export default function Settings({ open, onOpenChange }: TDialogProps) {
<Tabs.Content value={SettingsTabValues.SPEECH}>
<Speech />
</Tabs.Content>
+ {hasAnyPersonalizationFeature && (
+ <Tabs.Content value={SettingsTabValues.PERSONALIZATION}>
+ <Personalization
+ hasMemoryOptOut={hasMemoryOptOut}
+ hasAnyPersonalizationFeature={hasAnyPersonalizationFeature}
+ />
+ </Tabs.Content>
+ )}
<Tabs.Content value={SettingsTabValues.DATA}>
<Data />
</Tabs.Content>
diff --git a/client/src/components/Nav/SettingsTabs/Personalization.tsx b/client/src/components/Nav/SettingsTabs/Personalization.tsx
new file mode 100644
index 0000000000..929646a440
--- /dev/null
+++ b/client/src/components/Nav/SettingsTabs/Personalization.tsx
@@ -0,0 +1,87 @@
+import { useState, useEffect } from 'react';
+import { useGetUserQuery, useUpdateMemoryPreferencesMutation } from '~/data-provider';
+import { useToastContext } from '~/Providers';
+import { Switch } from '~/components/ui';
+import { useLocalize } from '~/hooks';
+
+interface PersonalizationProps {
+ hasMemoryOptOut: boolean;
+ hasAnyPersonalizationFeature: boolean;
+}
+
+export default function Personalization({
+ hasMemoryOptOut,
+ hasAnyPersonalizationFeature,
+}: PersonalizationProps) {
+ const localize = useLocalize();
+ const { showToast } = useToastContext();
+ const { data: user } = useGetUserQuery();
+ const [referenceSavedMemories, setReferenceSavedMemories] = useState(true);
+
+ const updateMemoryPreferencesMutation = useUpdateMemoryPreferencesMutation({
+ onSuccess: () => {
+ showToast({
+ message: localize('com_ui_preferences_updated'),
+ status: 'success',
+ });
+ },
+ onError: () => {
+ showToast({
+ message: localize('com_ui_error_updating_preferences'),
+ status: 'error',
+ });
+ // Revert the toggle on error
+ setReferenceSavedMemories((prev) => !prev);
+ },
+ });
+
+ // Initialize state from user data
+ useEffect(() => {
+ if (user?.personalization?.memories !== undefined) {
+ setReferenceSavedMemories(user.personalization.memories);
+ }
+ }, [user?.personalization?.memories]);
+
+ const handleMemoryToggle = (checked: boolean) => {
+ setReferenceSavedMemories(checked);
+ updateMemoryPreferencesMutation.mutate({ memories: checked });
+ };
+
+ if (!hasAnyPersonalizationFeature) {
+ return (
+ <div className="flex flex-col gap-3 text-sm text-text-primary">
+ <div className="text-text-secondary">{localize('com_ui_no_personalization_available')}</div>
+ </div>
+ );
+ }
+
+ return (
+ <div className="flex flex-col gap-3 text-sm text-text-primary">
+ {/* Memory Settings Section */}
+ {hasMemoryOptOut && (
+ <>
+ <div className="border-b border-border-medium pb-3">
+ <div className="text-base font-semibold">{localize('com_ui_memory')}</div>
+ </div>
+
+ <div className="flex items-center justify-between">
+ <div>
+ <div className="flex items-center gap-2">
+ {localize('com_ui_reference_saved_memories')}
+ </div>
+ <div className="mt-1 text-xs text-text-secondary">
+ {localize('com_ui_reference_saved_memories_description')}
+ </div>
+ </div>
+ <Switch
+ checked={referenceSavedMemories}
+ onCheckedChange={handleMemoryToggle}
+ disabled={updateMemoryPreferencesMutation.isLoading}
+ aria-label={localize('com_ui_reference_saved_memories')}
+ />
+ </div>
+ </>
+ )}
+ </div>
+ );
+}
diff --git a/client/src/components/Nav/SettingsTabs/index.ts b/client/src/components/Nav/SettingsTabs/index.ts
index 380d9a7a6d..b3398431f5 100644
--- a/client/src/components/Nav/SettingsTabs/index.ts
+++ b/client/src/components/Nav/SettingsTabs/index.ts
@@ -7,3 +7,4 @@ export { RevokeKeysButton } from './Data/RevokeKeysButton';
export { default as Account } from './Account/Account';
export { default as Balance } from './Balance/Balance';
export { default as Speech } from './Speech/Speech';
+export { default as Personalization } from './Personalization';
diff --git a/client/src/components/Prompts/Groups/FilterPrompts.tsx b/client/src/components/Prompts/Groups/FilterPrompts.tsx
index 0d0ab9a2fc..19d2cde1f9 100644
--- a/client/src/components/Prompts/Groups/FilterPrompts.tsx
+++ b/client/src/components/Prompts/Groups/FilterPrompts.tsx
@@ -44,11 +44,11 @@ export default function FilterPrompts({
const categoryOptions = categories
? [...categories]
: [
- {
- value: SystemCategories.NO_CATEGORY,
- label: localize('com_ui_no_category'),
- },
- ];
+ {
+ value: SystemCategories.NO_CATEGORY,
+ label: localize('com_ui_no_category'),
+ },
+ ];
return [...baseOptions, ...categoryOptions];
}, [categories, localize]);
diff --git a/client/src/components/Prompts/Groups/GroupSidePanel.tsx b/client/src/components/Prompts/Groups/GroupSidePanel.tsx
index 5cfd77ec2a..e359d13ea0 100644
--- a/client/src/components/Prompts/Groups/GroupSidePanel.tsx
+++ b/client/src/components/Prompts/Groups/GroupSidePanel.tsx
@@ -1,6 +1,7 @@
import { useMemo } from 'react';
import { useLocation } from 'react-router-dom';
import PanelNavigation from '~/components/Prompts/Groups/PanelNavigation';
+import ManagePrompts from '~/components/Prompts/ManagePrompts';
import { useMediaQuery, usePromptGroupsNav } from '~/hooks';
import List from '~/components/Prompts/Groups/List';
import { cn } from '~/utils';
@@ -38,14 +39,17 @@ export default function GroupSidePanel({
<div className="flex-grow overflow-y-auto">
<List groups={promptGroups} isChatRoute={isChatRoute} isLoading={!!groupsQuery.isLoading} />
</div>
- <PanelNavigation
- nextPage={nextPage}
- prevPage={prevPage}
- isFetching={isFetching}
- hasNextPage={hasNextPage}
- isChatRoute={isChatRoute}
- hasPreviousPage={hasPreviousPage}
- />
+ <div className="flex items-center justify-between">
+ {isChatRoute && <ManagePrompts className="select-none" />}
+ <PanelNavigation
+ nextPage={nextPage}
+ prevPage={prevPage}
+ isFetching={isFetching}
+ hasNextPage={hasNextPage}
+ isChatRoute={isChatRoute}
+ hasPreviousPage={hasPreviousPage}
+ />
+ </div>
</div>
);
}
diff --git a/client/src/components/Prompts/Groups/PanelNavigation.tsx b/client/src/components/Prompts/Groups/PanelNavigation.tsx
index 76395e842d..5600415d7e 100644
--- a/client/src/components/Prompts/Groups/PanelNavigation.tsx
+++ b/client/src/components/Prompts/Groups/PanelNavigation.tsx
@@ -19,11 +19,13 @@ function PanelNavigation({
}) {
const localize = useLocalize();
return (
- <div className="my-1 flex justify-between">
- <div className="mb-2 flex gap-2">
- {!isChatRoute && <ThemeSelector returnThemeOnly={true} />}
- </div>
- <div className="mb-2 flex gap-2">
+ <>
+ <div className="flex gap-2">{!isChatRoute && <ThemeSelector returnThemeOnly={true} />}</div>
+ <div
+ className="flex items-center justify-between gap-2"
+ role="navigation"
+ aria-label="Pagination"
+ >
<Button variant="outline" size="sm" onClick={() => prevPage()} disabled={!hasPreviousPage}>
{localize('com_ui_prev')}
</Button>
@@ -36,7 +38,7 @@ function PanelNavigation({
{localize('com_ui_next')}
</Button>
</div>
- </div>
+ </>
);
}
diff --git a/client/src/components/Prompts/PromptsAccordion.tsx b/client/src/components/Prompts/PromptsAccordion.tsx
index 444646c5ee..6bc84005c4 100644
--- a/client/src/components/Prompts/PromptsAccordion.tsx
+++ b/client/src/components/Prompts/PromptsAccordion.tsx
@@ -1,19 +1,17 @@
import PromptSidePanel from '~/components/Prompts/Groups/GroupSidePanel';
import AutoSendPrompt from '~/components/Prompts/Groups/AutoSendPrompt';
import FilterPrompts from '~/components/Prompts/Groups/FilterPrompts';
-import ManagePrompts from '~/components/Prompts/ManagePrompts';
import { usePromptGroupsNav } from '~/hooks';
export default function PromptsAccordion() {
const groupsNav = usePromptGroupsNav();
return (
<div className="flex h-full w-full flex-col">
- <PromptSidePanel className="lg:w-full xl:w-full" {...groupsNav}>
- <div className="flex w-full flex-row items-center justify-between pt-2">
- <ManagePrompts className="select-none" />
+ <PromptSidePanel className="mt-2 space-y-2 lg:w-full xl:w-full" {...groupsNav}>
+ <FilterPrompts setName={groupsNav.setName} className="items-center justify-center" />
+ <div className="flex w-full flex-row items-center justify-end">
<AutoSendPrompt className="text-xs dark:text-white" />
</div>
- <FilterPrompts setName={groupsNav.setName} className="items-center justify-center" />
</PromptSidePanel>
</div>
);
diff --git a/client/src/components/SidePanel/Bookmarks/BookmarkTable.tsx b/client/src/components/SidePanel/Bookmarks/BookmarkTable.tsx
index 12de8b452d..0506b3b0dd 100644
--- a/client/src/components/SidePanel/Bookmarks/BookmarkTable.tsx
+++ b/client/src/components/SidePanel/Bookmarks/BookmarkTable.tsx
@@ -80,13 +80,13 @@ const BookmarkTable = () => {
<TableHeader>
<TableRow className="border-b border-border-light">
<TableHead className="w-[70%] bg-surface-secondary py-3 text-left text-sm font-medium text-text-secondary">
- <div className="px-4">{localize('com_ui_bookmarks_title')}</div>
+ <div>{localize('com_ui_bookmarks_title')}</div>
</TableHead>
<TableHead className="w-[30%] bg-surface-secondary py-3 text-left text-sm font-medium text-text-secondary">
- <div className="px-4">{localize('com_ui_bookmarks_count')}</div>
+ <div>{localize('com_ui_bookmarks_count')}</div>
</TableHead>
<TableHead className="w-[40%] bg-surface-secondary py-3 text-left text-sm font-medium text-text-secondary">
- <div className="px-4">{localize('com_assistants_actions')}</div>
+ <div>{localize('com_assistants_actions')}</div>
</TableHead>
</TableRow>
</TableHeader>
diff --git a/client/src/components/SidePanel/Memories/AdminSettings.tsx b/client/src/components/SidePanel/Memories/AdminSettings.tsx
new file mode 100644
index 0000000000..fcb347228d
--- /dev/null
+++ b/client/src/components/SidePanel/Memories/AdminSettings.tsx
@@ -0,0 +1,212 @@
+import * as Ariakit from '@ariakit/react';
+import { useMemo, useEffect, useState } from 'react';
+import { ShieldEllipsis } from 'lucide-react';
+import { useForm, Controller } from 'react-hook-form';
+import { Permissions, SystemRoles, roleDefaults, PermissionTypes } from 'librechat-data-provider';
+import type { Control, UseFormSetValue, UseFormGetValues } from 'react-hook-form';
+import { OGDialog, OGDialogTitle, OGDialogContent, OGDialogTrigger } from '~/components/ui';
+import { useUpdateMemoryPermissionsMutation } from '~/data-provider';
+import { Button, Switch, DropdownPopup } from '~/components/ui';
+import { useLocalize, useAuthContext } from '~/hooks';
+import { useToastContext } from '~/Providers';
+
+type FormValues = Record<Permissions, boolean>;
+
+type LabelControllerProps = {
+ label: string;
+ memoryPerm: Permissions;
+ control: Control<FormValues, unknown, FormValues>;
+ setValue: UseFormSetValue<FormValues>;
+ getValues: UseFormGetValues<FormValues>;
+};
+
+const LabelController: React.FC<LabelControllerProps> = ({ control, memoryPerm, label }) => (
+ <div className="mb-4 flex items-center justify-between gap-2">
+ {label}
+ <Controller
+ name={memoryPerm}
+ control={control}
+ render={({ field }) => (
+ <Switch
+ {...field}
+ checked={field.value}
+ onCheckedChange={field.onChange}
+ value={field.value.toString()}
+ />
+ )}
+ />
+ </div>
+);
+
+const AdminSettings = () => {
+ const localize = useLocalize();
+ const { user, roles } = useAuthContext();
+ const { showToast } = useToastContext();
+ const { mutate, isLoading } = useUpdateMemoryPermissionsMutation({
+ onSuccess: () => {
+ showToast({ status: 'success', message: localize('com_ui_saved') });
+ },
+ onError: () => {
+ showToast({ status: 'error', message: localize('com_ui_error_save_admin_settings') });
+ },
+ });
+
+ const [isRoleMenuOpen, setIsRoleMenuOpen] = useState(false);
+ const [selectedRole, setSelectedRole] = useState<SystemRoles>(SystemRoles.USER);
+
+ const defaultValues = useMemo(() => {
+ if (roles?.[selectedRole]?.permissions) {
+ return roles?.[selectedRole]?.permissions?.[PermissionTypes.MEMORIES];
+ }
+ return roleDefaults[selectedRole].permissions[PermissionTypes.MEMORIES];
+ }, [roles, selectedRole]);
+
+ const {
+ reset,
+ control,
+ setValue,
+ getValues,
+ handleSubmit,
+ formState: { isSubmitting },
+ } = useForm<FormValues>({
+ mode: 'onChange',
+ defaultValues,
+ });
+
+ useEffect(() => {
+ if (roles?.[selectedRole]?.permissions?.[PermissionTypes.MEMORIES]) {
+ reset(roles?.[selectedRole]?.permissions?.[PermissionTypes.MEMORIES]);
+ } else {
+ reset(roleDefaults[selectedRole].permissions[PermissionTypes.MEMORIES]);
+ }
+ }, [roles, selectedRole, reset]);
+
+ if (user?.role !== SystemRoles.ADMIN) {
+ return null;
+ }
+
+ const labelControllerData = [
+ {
+ memoryPerm: Permissions.USE,
+ label: localize('com_ui_memories_allow_use'),
+ },
+ {
+ memoryPerm: Permissions.CREATE,
+ label: localize('com_ui_memories_allow_create'),
+ },
+ {
+ memoryPerm: Permissions.UPDATE,
+ label: localize('com_ui_memories_allow_update'),
+ },
+ {
+ memoryPerm: Permissions.READ,
+ label: localize('com_ui_memories_allow_read'),
+ },
+ {
+ memoryPerm: Permissions.OPT_OUT,
+ label: localize('com_ui_memories_allow_opt_out'),
+ },
+ ];
+
+ const onSubmit = (data: FormValues) => {
+ mutate({ roleName: selectedRole, updates: data });
+ };
+
+ const roleDropdownItems = [
+ {
+ label: SystemRoles.USER,
+ onClick: () => {
+ setSelectedRole(SystemRoles.USER);
+ },
+ },
+ {
+ label: SystemRoles.ADMIN,
+ onClick: () => {
+ setSelectedRole(SystemRoles.ADMIN);
+ },
+ },
+ ];
+
+ return (
+ <OGDialog>
+ <OGDialogTrigger asChild>
+ <Button
+ size={'sm'}
+ variant={'outline'}
+ className="btn btn-neutral border-token-border-light relative h-9 w-full gap-1 rounded-lg font-medium"
+ >
+ <ShieldEllipsis className="cursor-pointer" aria-hidden="true" />
+ {localize('com_ui_admin_settings')}
+ </Button>
+ </OGDialogTrigger>
+ <OGDialogContent className="w-1/4 border-border-light bg-surface-primary text-text-primary">
+ <OGDialogTitle>{`${localize('com_ui_admin_settings')} - ${localize(
+ 'com_ui_memories',
+ )}`}</OGDialogTitle>
+ <div className="p-2">
+ {/* Role selection dropdown */}
+ <div className="flex items-center gap-2">
+ <span className="font-medium">{localize('com_ui_role_select')}:</span>
+ <DropdownPopup
+ unmountOnHide={true}
+ menuId="memory-role-dropdown"
+ isOpen={isRoleMenuOpen}
+ setIsOpen={setIsRoleMenuOpen}
+ trigger={
+ <Ariakit.MenuButton className="inline-flex w-1/4 items-center justify-center rounded-lg border border-border-light bg-transparent px-2 py-1 text-text-primary transition-all ease-in-out hover:bg-surface-tertiary">
+ {selectedRole}
+ </Ariakit.MenuButton>
+ }
+ items={roleDropdownItems}
+ itemClassName="items-center justify-center"
+ sameWidth={true}
+ />
+ </div>
+ {/* Permissions form */}
+ <form onSubmit={handleSubmit(onSubmit)}>
+ <div className="py-5">
+ {labelControllerData.map(({ memoryPerm, label }) => (
+ <div key={memoryPerm}>
+ <LabelController
+ control={control}
+ memoryPerm={memoryPerm}
+ label={label}
+ getValues={getValues}
+ setValue={setValue}
+ />
+ {selectedRole === SystemRoles.ADMIN && memoryPerm === Permissions.USE && (
+ <>
+ <div className="mb-2 max-w-full whitespace-normal break-words text-sm text-red-600">
+ <span>{localize('com_ui_admin_access_warning')}</span>
+ {'\n'}
+ <a
+ href="https://www.librechat.ai/docs/configuration/librechat_yaml/object_structure/interface"
+ target="_blank"
+ rel="noreferrer"
+ className="text-blue-500 underline"
+ >
+ {localize('com_ui_more_info')}
+ </a>
+ </div>
+ </>
+ )}
+ </div>
+ ))}
+ </div>
+ <div className="flex justify-end">
+ <button
+ type="submit"
+ disabled={isSubmitting || isLoading}
+ className="btn rounded bg-green-500 font-bold text-white transition-all hover:bg-green-600"
+ >
+ {localize('com_ui_save')}
+ </button>
+ </div>
+ </form>
+ </div>
+ </OGDialogContent>
+ </OGDialog>
+ );
+};
+
+export default AdminSettings;
diff --git a/client/src/components/SidePanel/Memories/MemoryCreateDialog.tsx b/client/src/components/SidePanel/Memories/MemoryCreateDialog.tsx
new file mode 100644
index 0000000000..1670ba6f60
--- /dev/null
+++ b/client/src/components/SidePanel/Memories/MemoryCreateDialog.tsx
@@ -0,0 +1,147 @@
+import React, { useState } from 'react';
+import { PermissionTypes, Permissions } from 'librechat-data-provider';
+import { OGDialog, OGDialogTemplate, Button, Label, Input } from '~/components/ui';
+import { useCreateMemoryMutation } from '~/data-provider';
+import { useLocalize, useHasAccess } from '~/hooks';
+import { useToastContext } from '~/Providers';
+import { Spinner } from '~/components/svg';
+
+interface MemoryCreateDialogProps {
+ open: boolean;
+ onOpenChange: (open: boolean) => void;
+ children: React.ReactNode;
+ triggerRef?: React.MutableRefObject<HTMLButtonElement | null>;
+}
+
+export default function MemoryCreateDialog({
+ open,
+ onOpenChange,
+ children,
+ triggerRef,
+}: MemoryCreateDialogProps) {
+ const localize = useLocalize();
+ const { showToast } = useToastContext();
+
+ const hasCreateAccess = useHasAccess({
+ permissionType: PermissionTypes.MEMORIES,
+ permission: Permissions.CREATE,
+ });
+
+ const { mutate: createMemory, isLoading } = useCreateMemoryMutation({
+ onSuccess: () => {
+ showToast({
+ message: localize('com_ui_memory_created'),
+ status: 'success',
+ });
+ onOpenChange(false);
+ setKey('');
+ setValue('');
+ setTimeout(() => {
+ triggerRef?.current?.focus();
+ }, 0);
+ },
+ onError: (error: Error) => {
+ let errorMessage = localize('com_ui_error');
+
+ if (error && typeof error === 'object' && 'response' in error) {
+ const axiosError = error as any;
+ if (axiosError.response?.data?.error) {
+ errorMessage = axiosError.response.data.error;
+
+ // Check for duplicate key error
+ if (axiosError.response?.status === 409 || errorMessage.includes('already exists')) {
+ errorMessage = localize('com_ui_memory_key_exists');
+ }
+ }
+ } else if (error.message) {
+ errorMessage = error.message;
+ }
+
+ showToast({
+ message: errorMessage,
+ status: 'error',
+ });
+ },
+ });
+
+ const [key, setKey] = useState('');
+ const [value, setValue] = useState('');
+
+ const handleSave = () => {
+ if (!hasCreateAccess) {
+ return;
+ }
+
+ if (!key.trim() || !value.trim()) {
+ showToast({
+ message: localize('com_ui_field_required'),
+ status: 'error',
+ });
+ return;
+ }
+
+ createMemory({
+ key: key.trim(),
+ value: value.trim(),
+ });
+ };
+
+ const handleKeyPress = (e: React.KeyboardEvent) => {
+ if (e.key === 'Enter' && e.ctrlKey && hasCreateAccess) {
+ handleSave();
+ }
+ };
+
+ return (
+ <OGDialog open={open} onOpenChange={onOpenChange} triggerRef={triggerRef}>
+ {children}
+ <OGDialogTemplate
+ title={localize('com_ui_create_memory')}
+ showCloseButton={false}
+ className="w-11/12 md:max-w-lg"
+ main={
+ <div className="space-y-4">
+ <div className="space-y-2">
+ <Label htmlFor="memory-key" className="text-sm font-medium">
+ {localize('com_ui_key')}
+ </Label>
+ <Input
+ id="memory-key"
+ value={key}
+ onChange={(e) => setKey(e.target.value)}
+ onKeyDown={handleKeyPress}
+ placeholder={localize('com_ui_enter_key')}
+ className="w-full"
+ />
+ </div>
+ <div className="space-y-2">
+ <Label htmlFor="memory-value" className="text-sm font-medium">
+ {localize('com_ui_value')}
+ </Label>
+ <textarea
+ id="memory-value"
+ value={value}
+ onChange={(e) => setValue(e.target.value)}
+ onKeyDown={handleKeyPress}
+ placeholder={localize('com_ui_enter_value')}
+ className="flex min-h-[80px] w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50"
+ rows={3}
+ />
+ </div>
+ </div>
+ }
+ buttons={
+ <Button
+ type="button"
+ variant="submit"
+ onClick={handleSave}
+ disabled={isLoading || !key.trim() || !value.trim()}
+ className="text-white"
+ >
+ {isLoading ? <Spinner className="size-4" /> : localize('com_ui_create')}
+ </Button>
+ }
+ />
+ </OGDialog>
+ );
+}
diff --git a/client/src/components/SidePanel/Memories/MemoryEditDialog.tsx b/client/src/components/SidePanel/Memories/MemoryEditDialog.tsx
new file mode 100644
index 0000000000..60eedb02fa
--- /dev/null
+++ b/client/src/components/SidePanel/Memories/MemoryEditDialog.tsx
@@ -0,0 +1,179 @@
+import React, { useState, useEffect } from 'react';
+import { PermissionTypes, Permissions } from 'librechat-data-provider';
+import type { TUserMemory } from 'librechat-data-provider';
+import { OGDialog, OGDialogTemplate, Button, Label, Input } from '~/components/ui';
+import { useUpdateMemoryMutation, useMemoriesQuery } from '~/data-provider';
+import { useLocalize, useHasAccess } from '~/hooks';
+import { useToastContext } from '~/Providers';
+import { Spinner } from '~/components/svg';
+
+interface MemoryEditDialogProps {
+ memory: TUserMemory | null;
+ open: boolean;
+ onOpenChange: (open: boolean) => void;
+ children: React.ReactNode;
+ triggerRef?: React.MutableRefObject<HTMLButtonElement | null>;
+}
+
+export default function MemoryEditDialog({
+ memory,
+ open,
+ onOpenChange,
+ children,
+ triggerRef,
+}: MemoryEditDialogProps) {
+ const localize = useLocalize();
+ const { showToast } = useToastContext();
+ const { data: memData } = useMemoriesQuery();
+
+ const hasUpdateAccess = useHasAccess({
+ permissionType: PermissionTypes.MEMORIES,
+ permission: Permissions.UPDATE,
+ });
+
+ const { mutate: updateMemory, isLoading } = useUpdateMemoryMutation({
+ onMutate: () => {
+ onOpenChange(false);
+ setTimeout(() => {
+ triggerRef?.current?.focus();
+ }, 0);
+ },
+ onSuccess: () => {
+ showToast({
+ message: localize('com_ui_saved'),
+ status: 'success',
+ });
+ },
+ onError: () => {
+ showToast({
+ message: localize('com_ui_error'),
+ status: 'error',
+ });
+ },
+ });
+
+ const [key, setKey] = useState('');
+ const [value, setValue] = useState('');
+ const [originalKey, setOriginalKey] = useState('');
+
+ useEffect(() => {
+ if (memory) {
+ setKey(memory.key);
+ setValue(memory.value);
+ setOriginalKey(memory.key);
+ }
+ }, [memory]);
+
+ const handleSave = () => {
+ if (!hasUpdateAccess || !memory) {
+ return;
+ }
+
+ if (!key.trim() || !value.trim()) {
+ showToast({
+ message: localize('com_ui_field_required'),
+ status: 'error',
+ });
+ return;
+ }
+
+ updateMemory({
+ key: key.trim(),
+ value: value.trim(),
+ ...(originalKey !== key.trim() && { originalKey }),
+ });
+ };
+
+ const handleKeyPress = (e: React.KeyboardEvent) => {
+ if (e.key === 'Enter' && e.ctrlKey && hasUpdateAccess) {
+ handleSave();
+ }
+ };
+
+ return (
+ <OGDialog open={open} onOpenChange={onOpenChange} triggerRef={triggerRef}>
+ {children}
+ <OGDialogTemplate
+ title={hasUpdateAccess ? localize('com_ui_edit_memory') : localize('com_ui_view_memory')}
+ showCloseButton={false}
+ className="w-11/12 md:max-w-lg"
+ main={
+ <div className="space-y-4">
+ {memory && (
+ <div className="space-y-2">
+ <div className="flex items-center justify-between text-xs text-text-secondary">
+ <div>
+ {localize('com_ui_date')}:{' '}
+ {new Date(memory.updated_at).toLocaleDateString(undefined, {
+ month: 'short',
+ day: 'numeric',
+ year: 'numeric',
+ hour: '2-digit',
+ minute: '2-digit',
+ })}
+ </div>
+ {/* Token Information */}
+ {memory.tokenCount !== undefined && (
+ <div>
+ {memory.tokenCount.toLocaleString()}
+ {memData?.tokenLimit && ` / ${memData.tokenLimit.toLocaleString()}`}{' '}
+ {localize('com_ui_tokens')}
+ </div>
+ )}
+ </div>
+ {/* Overall Memory Usage */}
+ {memData?.tokenLimit && memData?.usagePercentage !== null && (
+ <div className="text-xs text-text-secondary">
+ {localize('com_ui_usage')}: {memData.usagePercentage}%{' '}
+ </div>
+ )}
+ </div>
+ )}
+ <div className="space-y-2">
+ <Label htmlFor="memory-key" className="text-sm font-medium">
+ {localize('com_ui_key')}
+ </Label>
+ <Input
+ id="memory-key"
+ value={key}
+ onChange={(e) => hasUpdateAccess && setKey(e.target.value)}
+ onKeyDown={handleKeyPress}
+ placeholder={localize('com_ui_enter_key')}
+ className="w-full"
+ disabled={!hasUpdateAccess}
+ />
+ </div>
+ <div className="space-y-2">
+ <Label htmlFor="memory-value" className="text-sm font-medium">
+ {localize('com_ui_value')}
+ </Label>
+ <textarea
+ id="memory-value"
+ value={value}
+ onChange={(e) => hasUpdateAccess && setValue(e.target.value)}
+ onKeyDown={handleKeyPress}
+ placeholder={localize('com_ui_enter_value')}
+ className="flex min-h-[80px] w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50"
+ rows={3}
+ disabled={!hasUpdateAccess}
+ />
+ </div>
+ </div>
+ }
+ buttons={
+ hasUpdateAccess ? (
+ <Button
+ type="button"
+ variant="submit"
+ onClick={handleSave}
+ disabled={isLoading || !key.trim() || !value.trim()}
+ className="text-white"
+ >
+ {isLoading ? <Spinner className="size-4" /> : localize('com_ui_save')}
+ </Button>
+ ) : null
+ }
+ />
+ </OGDialog>
+ );
+}
diff --git a/client/src/components/SidePanel/Memories/MemoryViewer.tsx b/client/src/components/SidePanel/Memories/MemoryViewer.tsx
new file mode 100644
index 0000000000..66f3fee8e6
--- /dev/null
+++ b/client/src/components/SidePanel/Memories/MemoryViewer.tsx
@@ -0,0 +1,428 @@
+/* Memories */
+import { useMemo, useState, useRef, useEffect } from 'react';
+import { Plus } from 'lucide-react';
+import { matchSorter } from 'match-sorter';
+import { SystemRoles, PermissionTypes, Permissions } from 'librechat-data-provider';
+import type { TUserMemory } from 'librechat-data-provider';
+import {
+ Table,
+ Input,
+ Label,
+ Button,
+ Switch,
+ TableRow,
+ OGDialog,
+ TableHead,
+ TableBody,
+ TableCell,
+ TableHeader,
+ TooltipAnchor,
+ OGDialogTrigger,
+} from '~/components/ui';
+import {
+ useGetUserQuery,
+ useMemoriesQuery,
+ useDeleteMemoryMutation,
+ useUpdateMemoryPreferencesMutation,
+} from '~/data-provider';
+import { useLocalize, useAuthContext, useHasAccess } from '~/hooks';
+import OGDialogTemplate from '~/components/ui/OGDialogTemplate';
+import { EditIcon, TrashIcon } from '~/components/svg';
+import MemoryCreateDialog from './MemoryCreateDialog';
+import MemoryEditDialog from './MemoryEditDialog';
+import Spinner from '~/components/svg/Spinner';
+import { useToastContext } from '~/Providers';
+import AdminSettings from './AdminSettings';
+
+export default function MemoryViewer() {
+ const localize = useLocalize();
+ const { user } = useAuthContext();
+ const { data: userData } = useGetUserQuery();
+ const { data: memData, isLoading } = useMemoriesQuery();
+ const { mutate: deleteMemory } = useDeleteMemoryMutation();
+ const { showToast } = useToastContext();
+ const [pageIndex, setPageIndex] = useState(0);
+ const [searchQuery, setSearchQuery] = useState('');
+ const pageSize = 10;
+ const [createDialogOpen, setCreateDialogOpen] = useState(false);
+ const [deletingKey, setDeletingKey] = useState<string | null>(null);
+ const [referenceSavedMemories, setReferenceSavedMemories] = useState(true);
+
+ const updateMemoryPreferencesMutation = useUpdateMemoryPreferencesMutation({
+ onSuccess: () => {
+ showToast({
+ message: localize('com_ui_preferences_updated'),
+ status: 'success',
+ });
+ },
+ onError: () => {
+ showToast({
+ message: localize('com_ui_error_updating_preferences'),
+ status: 'error',
+ });
+ setReferenceSavedMemories((prev) => !prev);
+ },
+ });
+
+ useEffect(() => {
+ if (userData?.personalization?.memories !== undefined) {
+ setReferenceSavedMemories(userData.personalization.memories);
+ }
+ }, [userData?.personalization?.memories]);
+
+ const handleMemoryToggle = (checked: boolean) => {
+ setReferenceSavedMemories(checked);
+ updateMemoryPreferencesMutation.mutate({ memories: checked });
+ };
+
+ const hasReadAccess = useHasAccess({
+ permissionType: PermissionTypes.MEMORIES,
+ permission: Permissions.READ,
+ });
+
+ const hasUpdateAccess = useHasAccess({
+ permissionType: PermissionTypes.MEMORIES,
+ permission: Permissions.UPDATE,
+ });
+
+ const hasCreateAccess = useHasAccess({
+ permissionType: PermissionTypes.MEMORIES,
+ permission: Permissions.CREATE,
+ });
+
+ const hasOptOutAccess = useHasAccess({
+ permissionType: PermissionTypes.MEMORIES,
+ permission: Permissions.OPT_OUT,
+ });
+
+ const memories: TUserMemory[] = useMemo(() => memData?.memories ?? [], [memData]);
+
+ const filteredMemories = useMemo(() => {
+ return matchSorter(memories, searchQuery, {
+ keys: ['key', 'value'],
+ });
+ }, [memories, searchQuery]);
+
+ const currentRows = useMemo(() => {
+ return filteredMemories.slice(pageIndex * pageSize, (pageIndex + 1) * pageSize);
+ }, [filteredMemories, pageIndex]);
+
+ const getProgressBarColor = (percentage: number): string => {
+ if (percentage > 90) {
+ return 'stroke-red-500';
+ }
+ if (percentage > 75) {
+ return 'stroke-yellow-500';
+ }
+ return 'stroke-green-500';
+ };
+
+ const EditMemoryButton = ({ memory }: { memory: TUserMemory }) => {
+ const [open, setOpen] = useState(false);
+ const triggerRef = useRef<HTMLDivElement>(null);
+
+ const handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
+ if (event.key === 'Enter' || event.key === ' ') {
+ event.preventDefault();
+ setOpen(!open);
+ }
+ };
+
+ // Only show edit button if user has UPDATE permission
+ if (!hasUpdateAccess) {
+ return null;
+ }
+
+ return (
+ <MemoryEditDialog
+ open={open}
+ memory={memory}
+ onOpenChange={setOpen}
+ triggerRef={triggerRef as React.MutableRefObject<HTMLButtonElement | null>}
+ >
+ <OGDialogTrigger asChild>
+ <TooltipAnchor
+ ref={triggerRef}
+ role="button"
+ aria-label={localize('com_ui_edit')}
+ description={localize('com_ui_edit')}
+ tabIndex={0}
+ onClick={() => setOpen(!open)}
+ className="flex size-7 items-center justify-center rounded-lg transition-colors duration-200 hover:bg-surface-hover"
+ onKeyDown={handleKeyDown}
+ >
+ <EditIcon />
+ </TooltipAnchor>
+ </OGDialogTrigger>
+ </MemoryEditDialog>
+ );
+ };
+
+ const DeleteMemoryButton = ({ memory }: { memory: TUserMemory }) => {
+ const [open, setOpen] = useState(false);
+
+ const handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
+ if (event.key === 'Enter' || event.key === ' ') {
+ event.preventDefault();
+ event.stopPropagation();
+ setOpen(!open);
+ }
+ };
+
+ if (!hasUpdateAccess) {
+ return null;
+ }
+
+ const confirmDelete = async () => {
+ setDeletingKey(memory.key);
+ deleteMemory(memory.key, {
+ onSuccess: () => {
+ showToast({
+ message: localize('com_ui_deleted'),
+ status: 'success',
+ });
+ setOpen(false);
+ },
+ onError: () =>
+ showToast({
+ message: localize('com_ui_error'),
+ status: 'error',
+ }),
+ onSettled: () => setDeletingKey(null),
+ });
+ };
+
+ return (
+ <OGDialog open={open} onOpenChange={setOpen}>
+ <OGDialogTrigger asChild>
+ <TooltipAnchor
+ role="button"
+ aria-label={localize('com_ui_delete')}
+ description={localize('com_ui_delete')}
+ className="flex size-7 items-center justify-center rounded-lg transition-colors duration-200 hover:bg-surface-hover"
+ tabIndex={0}
+ onClick={() => setOpen(!open)}
+ onKeyDown={handleKeyDown}
+ >
+ {deletingKey === memory.key ? (
+ <Spinner className="size-4 animate-spin" />
+ ) : (
+ <TrashIcon className="size-4" />
+ )}
+ </TooltipAnchor>
+ </OGDialogTrigger>
+ <OGDialogTemplate
+ showCloseButton={false}
+ title={localize('com_ui_delete_memory')}
+ className="w-11/12 max-w-lg"
+ main={
+ <Label className="text-left text-sm font-medium">
+ {localize('com_ui_delete_confirm')} &quot;{memory.key}&quot;?
+ </Label>
+ }
+ selection={{
+ selectHandler: confirmDelete,
+ selectClasses:
+ 'bg-red-700 dark:bg-red-600 hover:bg-red-800 dark:hover:bg-red-800 text-white',
+ selectText: localize('com_ui_delete'),
+ }}
+ />
+ </OGDialog>
+ );
+ };
+
+ if (isLoading) {
+ return (
+ <div className="flex h-full w-full items-center justify-center p-4">
+ <Spinner />
+ </div>
+ );
+ }
+
+ if (!hasReadAccess) {
+ return (
+ <div className="flex h-full w-full items-center justify-center p-4">
+ <div className="text-center">
+ <p className="text-sm text-text-secondary">{localize('com_ui_no_read_access')}</p>
+ </div>
+ </div>
+ );
+ }
+
+ return (
+ <div className="flex h-full w-full flex-col overflow-hidden">
+ <div role="region" aria-label={localize('com_ui_memories')} className="mt-2 space-y-2">
+ <div className="flex items-center gap-4">
+ <Input
+ placeholder={localize('com_ui_memories_filter')}
+ value={searchQuery}
+ onChange={(e) => setSearchQuery(e.target.value)}
+ aria-label={localize('com_ui_memories_filter')}
+ />
+ </div>
+ {/* Memory Usage and Toggle Display */}
+ {(memData?.tokenLimit || hasOptOutAccess) && (
+ <div className="flex items-center justify-between rounded-lg">
+ {/* Usage Display */}
+ {memData?.tokenLimit && (
+ <div className="flex items-center gap-2">
+ <div className="relative size-10">
+ <svg className="size-10 -rotate-90 transform">
+ <circle
+ cx="20"
+ cy="20"
+ r="16"
+ stroke="currentColor"
+ strokeWidth="3"
+ fill="none"
+ className="text-gray-200 dark:text-gray-700"
+ />
+ <circle
+ cx="20"
+ cy="20"
+ r="16"
+ strokeWidth="3"
+ fill="none"
+ strokeDasharray={`${2 * Math.PI * 16}`}
+ strokeDashoffset={`${2 * Math.PI * 16 * (1 - (memData.usagePercentage ?? 0) / 100)}`}
+ className={`transition-all ${getProgressBarColor(memData.usagePercentage ?? 0)}`}
+ strokeLinecap="round"
+ />
+ </svg>
+ <div className="absolute inset-0 flex items-center justify-center">
+ <span className="text-xs font-medium">{memData.usagePercentage}%</span>
+ </div>
+ </div>
+ <div className="text-sm text-text-secondary">{localize('com_ui_usage')}</div>
+ </div>
+ )}
+
+ {/* Memory Toggle */}
+ {hasOptOutAccess && (
+ <div className="flex items-center gap-2 text-xs">
+ <span>{localize('com_ui_use_memory')}</span>
+ <Switch
+ checked={referenceSavedMemories}
+ onCheckedChange={handleMemoryToggle}
+ aria-label={localize('com_ui_reference_saved_memories')}
+ disabled={updateMemoryPreferencesMutation.isLoading}
+ />
+ </div>
+ )}
+ </div>
+ )}
+ {/* Create Memory Button */}
+ {hasCreateAccess && (
+ <div className="flex w-full justify-end">
+ <MemoryCreateDialog open={createDialogOpen} onOpenChange={setCreateDialogOpen}>
+ <OGDialogTrigger asChild>
+ <Button variant="outline" className="w-full bg-transparent">
+ <Plus className="size-4" aria-hidden />
+ {localize('com_ui_create_memory')}
+ </Button>
+ </OGDialogTrigger>
+ </MemoryCreateDialog>
+ </div>
+ )}
+ <div className="rounded-lg border border-border-light bg-transparent shadow-sm transition-colors">
+ <Table className="w-full table-fixed">
+ <TableHeader>
+ <TableRow className="border-b border-border-light hover:bg-surface-secondary">
+ <TableHead
+ className={`${
+ hasUpdateAccess ? 'w-[75%]' : 'w-[100%]'
+ } bg-surface-secondary py-3 text-left text-sm font-medium text-text-secondary`}
+ >
+ <div>{localize('com_ui_memory')}</div>
+ </TableHead>
+ {hasUpdateAccess && (
+ <TableHead className="w-[25%] bg-surface-secondary py-3 text-center text-sm font-medium text-text-secondary">
+ <div>{localize('com_assistants_actions')}</div>
+ </TableHead>
+ )}
+ </TableRow>
+ </TableHeader>
+ <TableBody>
+ {currentRows.length ? (
+ currentRows.map((memory: TUserMemory, idx: number) => (
+ <TableRow
+ key={idx}
+ className="border-b border-border-light hover:bg-surface-secondary"
+ >
+ <TableCell className={`${hasUpdateAccess ? 'w-[75%]' : 'w-[100%]'} px-4 py-4`}>
+ <div
+ className="overflow-hidden text-ellipsis whitespace-nowrap text-sm text-text-primary"
+ title={memory.value}
+ >
+ {memory.value}
+ </div>
+ </TableCell>
+ {hasUpdateAccess && (
+ <TableCell className="w-[25%] px-4 py-4">
+ <div className="flex justify-center gap-2">
+ <EditMemoryButton memory={memory} />
+ <DeleteMemoryButton memory={memory} />
+ </div>
+ </TableCell>
+ )}
+ </TableRow>
+ ))
+ ) : (
+ <TableRow>
+ <TableCell
+ colSpan={hasUpdateAccess ? 2 : 1}
+ className="h-24 text-center text-sm text-text-secondary"
+ >
+ {localize('com_ui_no_data')}
+ </TableCell>
+ </TableRow>
+ )}
+ </TableBody>
+ </Table>
+ </div>
+
+ {/* Pagination controls */}
+ {filteredMemories.length > pageSize && (
+ <div
+ className="flex items-center justify-end gap-2"
+ role="navigation"
+ aria-label="Pagination"
+ >
+ <Button
+ variant="outline"
+ size="sm"
+ onClick={() => setPageIndex((prev) => Math.max(prev - 1, 0))}
+ disabled={pageIndex === 0}
+ aria-label={localize('com_ui_prev')}
+ >
+ {localize('com_ui_prev')}
+ </Button>
+ <div className="text-sm" aria-live="polite">
+ {`${pageIndex + 1} / ${Math.ceil(filteredMemories.length / pageSize)}`}
+ </div>
+ <Button
+ variant="outline"
+ size="sm"
+ onClick={() =>
+ setPageIndex((prev) =>
+ (prev + 1) * pageSize < filteredMemories.length ? prev + 1 : prev,
+ )
+ }
+ disabled={(pageIndex + 1) * pageSize >= filteredMemories.length}
+ aria-label={localize('com_ui_next')}
+ >
+ {localize('com_ui_next')}
+ </Button>
+ </div>
+ )}
+
+ {/* Admin Settings */}
+ {user?.role === SystemRoles.ADMIN && (
+ <div className="mt-4">
+ <AdminSettings />
+ </div>
+ )}
+ </div>
+ </div>
+ );
+}
diff --git a/client/src/components/SidePanel/Memories/index.ts b/client/src/components/SidePanel/Memories/index.ts
new file mode 100644
index 0000000000..2169cae7e3
--- /dev/null
+++ b/client/src/components/SidePanel/Memories/index.ts
@@ -0,0 +1,2 @@
+export { default as MemoryViewer } from './MemoryViewer';
+export { default as MemoryEditDialog } from './MemoryEditDialog';
diff --git a/client/src/components/svg/PersonalizationIcon.tsx b/client/src/components/svg/PersonalizationIcon.tsx
new file mode 100644
index 0000000000..e67e84b221
--- /dev/null
+++ b/client/src/components/svg/PersonalizationIcon.tsx
@@ -0,0 +1,19 @@
+export default function PersonalizationIcon({ className = '' }: { className?: string }) {
+ return (
+ <svg
+ width="24"
+ height="24"
+ viewBox="0 0 24 24"
+ fill="none"
+ xmlns="http://www.w3.org/2000/svg"
+ className={`icon-sm ${className}`}
+ >
+ <path
+ fillRule="evenodd"
+ clipRule="evenodd"
+ d="M12 4C10.3431 4 9 5.34315 9 7C9 8.65685 10.3431 10 12 10C13.6569 10 15 8.65685 15 7C15 5.34315 13.6569 4 12 4ZM7 7C7 4.23858 9.23858 2 12 2C14.7614 2 17 4.23858 17 7C17 9.76142 14.7614 12 12 12C9.23858 12 7 9.76142 7 7ZM19.0277 15.6255C18.6859 15.5646 18.1941 15.6534 17.682 16.1829C17.4936 16.3777 17.2342 16.4877 16.9632 16.4877C16.6922 16.4877 16.4328 16.3777 16.2444 16.1829C15.7322 15.6534 15.2405 15.5646 14.8987 15.6255C14.5381 15.6897 14.2179 15.9384 14.0623 16.3275C13.8048 16.9713 13.9014 18.662 16.9632 20.4617C20.0249 18.662 20.1216 16.9713 19.864 16.3275C19.7084 15.9384 19.3882 15.6897 19.0277 15.6255ZM21.721 15.5847C22.5748 17.7191 21.2654 20.429 17.437 22.4892C17.1412 22.6484 16.7852 22.6484 16.4893 22.4892C12.6609 20.4291 11.3516 17.7191 12.2053 15.5847C12.6117 14.5689 13.4917 13.8446 14.5481 13.6565C15.3567 13.5125 16.2032 13.6915 16.9632 14.1924C17.7232 13.6915 18.5697 13.5125 19.3783 13.6565C20.4347 13.8446 21.3147 14.5689 21.721 15.5847ZM9.92597 14.2049C10.1345 14.7163 9.889 15.2999 9.3776 15.5084C7.06131 16.453 5.5 18.5813 5.5 20.9999C5.5 21.5522 5.05228 21.9999 4.5 21.9999C3.94772 21.9999 3.5 21.5522 3.5 20.9999C3.5 17.6777 5.641 14.8723 8.6224 13.6565C9.1338 13.448 9.71743 13.6935 9.92597 14.2049Z"
+ fill="currentColor"
+ />
+ </svg>
+ );
+}
diff --git a/client/src/components/svg/index.ts b/client/src/components/svg/index.ts
index 10068a1d47..73aad266d8 100644
--- a/client/src/components/svg/index.ts
+++ b/client/src/components/svg/index.ts
@@ -61,3 +61,4 @@ export { default as BedrockIcon } from './BedrockIcon';
export { default as ThumbUpIcon } from './ThumbUpIcon';
export { default as ThumbDownIcon } from './ThumbDownIcon';
export { default as XAIcon } from './XAIcon';
+export { default as PersonalizationIcon } from './PersonalizationIcon';
diff --git a/client/src/components/ui/OriginalDialog.tsx b/client/src/components/ui/OriginalDialog.tsx
index 28a8177817..4e1ff3bd48 100644
--- a/client/src/components/ui/OriginalDialog.tsx
+++ b/client/src/components/ui/OriginalDialog.tsx
@@ -4,7 +4,7 @@ import { X } from 'lucide-react';
import { cn } from '~/utils';
interface OGDialogProps extends DialogPrimitive.DialogProps {
- triggerRef?: React.RefObject<HTMLButtonElement | HTMLInputElement>;
+ triggerRef?: React.RefObject<HTMLButtonElement | HTMLInputElement | null>;
}
const Dialog = React.forwardRef<HTMLDivElement, OGDialogProps>(
diff --git a/client/src/components/ui/Table.tsx b/client/src/components/ui/Table.tsx
index cf7a23389f..6dac6d54ff 100644
--- a/client/src/components/ui/Table.tsx
+++ b/client/src/components/ui/Table.tsx
@@ -32,7 +32,7 @@ const TableFooter = React.forwardRef<
>(({ className, ...props }, ref) => (
<tfoot
ref={ref}
- className={cn('bg-muted/50 border-t font-medium [&>tr]:last:border-b-0', className)}
+ className={cn('border-t bg-muted/50 font-medium [&>tr]:last:border-b-0', className)}
{...props}
/>
));
@@ -43,7 +43,7 @@ const TableRow = React.forwardRef<HTMLTableRowElement, React.HTMLAttributes<HTML
<tr
ref={ref}
className={cn(
- 'hover:bg-muted/50 data-[state=selected]:bg-muted border-b border-border-light transition-colors',
+ 'border-b border-border-light transition-colors hover:bg-muted/50 data-[state=selected]:bg-muted',
className,
)}
{...props}
@@ -59,7 +59,7 @@ const TableHead = React.forwardRef<
<th
ref={ref}
className={cn(
- 'text-muted-foreground h-12 px-4 text-left align-middle font-medium [&:has([role=checkbox])]:pr-0',
+ 'h-12 px-4 text-left align-middle font-medium text-muted-foreground [&:has([role=checkbox])]:pr-0',
className,
)}
{...props}
@@ -83,7 +83,7 @@ const TableCaption = React.forwardRef<
HTMLTableCaptionElement,
React.HTMLAttributes<HTMLTableCaptionElement>
>(({ className, ...props }, ref) => (
- <caption ref={ref} className={cn('text-muted-foreground mt-4 text-sm', className)} {...props} />
+ <caption ref={ref} className={cn('mt-4 text-sm text-muted-foreground', className)} {...props} />
));
TableCaption.displayName = 'TableCaption';
diff --git a/client/src/data-provider/Memories/index.ts b/client/src/data-provider/Memories/index.ts
new file mode 100644
index 0000000000..27669d3c14
--- /dev/null
+++ b/client/src/data-provider/Memories/index.ts
@@ -0,0 +1,2 @@
+/* Memories */
+export * from './queries';
diff --git a/client/src/data-provider/Memories/queries.ts b/client/src/data-provider/Memories/queries.ts
new file mode 100644
index 0000000000..53c2025228
--- /dev/null
+++ b/client/src/data-provider/Memories/queries.ts
@@ -0,0 +1,116 @@
+/* Memories */
+import { QueryKeys, MutationKeys, dataService } from 'librechat-data-provider';
+import { useQuery, useQueryClient, useMutation } from '@tanstack/react-query';
+import type {
+ UseQueryOptions,
+ UseMutationOptions,
+ QueryObserverResult,
+} from '@tanstack/react-query';
+import type { TUserMemory, MemoriesResponse } from 'librechat-data-provider';
+
+export const useMemoriesQuery = (
+ config?: UseQueryOptions<MemoriesResponse>,
+): QueryObserverResult<MemoriesResponse> => {
+ return useQuery<MemoriesResponse>([QueryKeys.memories], () => dataService.getMemories(), {
+ refetchOnWindowFocus: false,
+ refetchOnReconnect: false,
+ refetchOnMount: false,
+ ...config,
+ });
+};
+
+export const useDeleteMemoryMutation = () => {
+ const queryClient = useQueryClient();
+ return useMutation((key: string) => dataService.deleteMemory(key), {
+ onSuccess: () => {
+ queryClient.invalidateQueries([QueryKeys.memories]);
+ },
+ });
+};
+
+export type UpdateMemoryParams = { key: string; value: string; originalKey?: string };
+export const useUpdateMemoryMutation = (
+ options?: UseMutationOptions<TUserMemory, Error, UpdateMemoryParams>,
+) => {
+ const queryClient = useQueryClient();
+ return useMutation(
+ ({ key, value, originalKey }: UpdateMemoryParams) =>
+ dataService.updateMemory(key, value, originalKey),
+ {
+ ...options,
+ onSuccess: (...params) => {
+ queryClient.invalidateQueries([QueryKeys.memories]);
+ options?.onSuccess?.(...params);
+ },
+ },
+ );
+};
+
+export type UpdateMemoryPreferencesParams = { memories: boolean };
+export type UpdateMemoryPreferencesResponse = {
+ updated: boolean;
+ preferences: { memories: boolean };
+};
+
+export const useUpdateMemoryPreferencesMutation = (
+ options?: UseMutationOptions<
+ UpdateMemoryPreferencesResponse,
+ Error,
+ UpdateMemoryPreferencesParams
+ >,
+) => {
+ const queryClient = useQueryClient();
+ return useMutation<UpdateMemoryPreferencesResponse, Error, UpdateMemoryPreferencesParams>(
+ [MutationKeys.updateMemoryPreferences],
+ (preferences: UpdateMemoryPreferencesParams) =>
+ dataService.updateMemoryPreferences(preferences),
+ {
+ ...options,
+ onSuccess: (...params) => {
+ queryClient.invalidateQueries([QueryKeys.user]);
+ options?.onSuccess?.(...params);
+ },
+ },
+ );
+};
+
+export type CreateMemoryParams = { key: string; value: string };
+export type CreateMemoryResponse = { created: boolean; memory: TUserMemory };
+
+export const useCreateMemoryMutation = (
+ options?: UseMutationOptions<CreateMemoryResponse, Error, CreateMemoryParams>,
+) => {
+ const queryClient = useQueryClient();
+ return useMutation<CreateMemoryResponse, Error, CreateMemoryParams>(
+ ({ key, value }: CreateMemoryParams) => dataService.createMemory({ key, value }),
+ {
+ ...options,
+ onSuccess: (data, variables, context) => {
+ queryClient.setQueryData<MemoriesResponse>([QueryKeys.memories], (oldData) => {
+ if (!oldData) return oldData;
+
+ const newMemories = [...oldData.memories, data.memory];
+ const totalTokens = newMemories.reduce(
+ (sum, memory) => sum + (memory.tokenCount || 0),
+ 0,
+ );
+ const tokenLimit = oldData.tokenLimit;
+ let usagePercentage = oldData.usagePercentage;
+
+ if (tokenLimit && tokenLimit > 0) {
+ usagePercentage = Math.min(100, Math.round((totalTokens / tokenLimit) * 100));
+ }
+
+ return {
+ ...oldData,
+ memories: newMemories,
+ totalTokens,
+ usagePercentage,
+ };
+ });
+
+ options?.onSuccess?.(data, variables, context);
+ },
+ },
+ );
+};
diff --git a/client/src/data-provider/__tests__/memories.test.ts b/client/src/data-provider/__tests__/memories.test.ts
new file mode 100644
index 0000000000..553ef227bd
--- /dev/null
+++ b/client/src/data-provider/__tests__/memories.test.ts
@@ -0,0 +1,19 @@
+import { dataService as _dataService } from 'librechat-data-provider';
+import axios from 'axios';
+
+jest.mock('axios');
+
+const mockedAxios = axios as jest.Mocked<typeof axios>;
+
+describe('getMemories', () => {
+ it('should fetch memories from /api/memories', async () => {
+ const mockData = [{ key: 'foo', value: 'bar', updated_at: '2024-05-01T00:00:00Z' }];
+
+ mockedAxios.get.mockResolvedValueOnce({ data: mockData } as any);
+
+ const result = await (_dataService as any).getMemories();
+
+ expect(mockedAxios.get).toHaveBeenCalledWith('/api/memories', expect.any(Object));
+ expect(result).toEqual(mockData);
+ });
+});
diff --git a/client/src/data-provider/index.ts b/client/src/data-provider/index.ts
index 0c476fd11c..1b1ceed19f 100644
--- a/client/src/data-provider/index.ts
+++ b/client/src/data-provider/index.ts
@@ -2,6 +2,8 @@ export * from './Auth';
export * from './Agents';
export * from './Endpoints';
export * from './Files';
+/* Memories */
+export * from './Memories';
export * from './Messages';
export * from './Misc';
export * from './Tools';
diff --git a/client/src/data-provider/roles.ts b/client/src/data-provider/roles.ts
index d038440d43..cdabc4c20c 100644
--- a/client/src/data-provider/roles.ts
+++ b/client/src/data-provider/roles.ts
@@ -1,10 +1,15 @@
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
+import {
+ QueryKeys,
+ dataService,
+ promptPermissionsSchema,
+ memoryPermissionsSchema,
+} from 'librechat-data-provider';
import type {
+ UseQueryOptions,
UseMutationResult,
QueryObserverResult,
- UseQueryOptions,
} from '@tanstack/react-query';
-import { QueryKeys, dataService, promptPermissionsSchema } from 'librechat-data-provider';
import type * as t from 'librechat-data-provider';
export const useGetRole = (
@@ -91,3 +96,39 @@ export const useUpdateAgentPermissionsMutation = (
},
);
};
+
+export const useUpdateMemoryPermissionsMutation = (
+ options?: t.UpdateMemoryPermOptions,
+): UseMutationResult<
+ t.UpdatePermResponse,
+ t.TError | undefined,
+ t.UpdateMemoryPermVars,
+ unknown
+> => {
+ const queryClient = useQueryClient();
+ const { onMutate, onSuccess, onError } = options ?? {};
+ return useMutation(
+ (variables) => {
+ memoryPermissionsSchema.partial().parse(variables.updates);
+ return dataService.updateMemoryPermissions(variables);
+ },
+ {
+ onSuccess: (data, variables, context) => {
+ queryClient.invalidateQueries([QueryKeys.roles, variables.roleName]);
+ if (onSuccess) {
+ onSuccess(data, variables, context);
+ }
+ },
+ onError: (...args) => {
+ const error = args[0];
+ if (error != null) {
+ console.error('Failed to update memory permissions:', error);
+ }
+ if (onError) {
+ onError(...args);
+ }
+ },
+ onMutate,
+ },
+ );
+};
diff --git a/client/src/hooks/Nav/useSideNavLinks.ts b/client/src/hooks/Nav/useSideNavLinks.ts
index c5516b9477..822bde3ace 100644
--- a/client/src/hooks/Nav/useSideNavLinks.ts
+++ b/client/src/hooks/Nav/useSideNavLinks.ts
@@ -1,5 +1,5 @@
import { useMemo } from 'react';
-import { MessageSquareQuote, ArrowRightToLine, Settings2, Bookmark } from 'lucide-react';
+import { MessageSquareQuote, ArrowRightToLine, Settings2, Database, Bookmark } from 'lucide-react';
import {
isAssistantsEndpoint,
isAgentsEndpoint,
@@ -12,6 +12,7 @@ import type { TInterfaceConfig, TEndpointsConfig } from 'librechat-data-provider
import type { NavLink } from '~/common';
import AgentPanelSwitch from '~/components/SidePanel/Agents/AgentPanelSwitch';
import BookmarkPanel from '~/components/SidePanel/Bookmarks/BookmarkPanel';
+import MemoryViewer from '~/components/SidePanel/Memories/MemoryViewer';
import PanelSwitch from '~/components/SidePanel/Builder/PanelSwitch';
import PromptsAccordion from '~/components/Prompts/PromptsAccordion';
import Parameters from '~/components/SidePanel/Parameters/Panel';
@@ -42,6 +43,14 @@ export default function useSideNavLinks({
permissionType: PermissionTypes.BOOKMARKS,
permission: Permissions.USE,
});
+ const hasAccessToMemories = useHasAccess({
+ permissionType: PermissionTypes.MEMORIES,
+ permission: Permissions.USE,
+ });
+ const hasAccessToReadMemories = useHasAccess({
+ permissionType: PermissionTypes.MEMORIES,
+ permission: Permissions.READ,
+ });
const hasAccessToAgents = useHasAccess({
permissionType: PermissionTypes.AGENTS,
permission: Permissions.USE,
@@ -97,6 +106,16 @@ export default function useSideNavLinks({
});
}
+ if (hasAccessToMemories && hasAccessToReadMemories) {
+ links.push({
+ title: 'com_ui_memories',
+ label: '',
+ icon: Database,
+ id: 'memories',
+ Component: MemoryViewer,
+ });
+ }
+
if (
interfaceConfig.parameters === true &&
isParamEndpoint(endpoint ?? '', endpointType ?? '') === true &&
@@ -147,6 +166,8 @@ export default function useSideNavLinks({
endpoint,
hasAccessToAgents,
hasAccessToPrompts,
+ hasAccessToMemories,
+ hasAccessToReadMemories,
hasAccessToBookmarks,
hasAccessToCreateAgents,
hidePanel,
diff --git a/client/src/hooks/SSE/useAttachmentHandler.ts b/client/src/hooks/SSE/useAttachmentHandler.ts
index cd511defd2..d9711718b1 100644
--- a/client/src/hooks/SSE/useAttachmentHandler.ts
+++ b/client/src/hooks/SSE/useAttachmentHandler.ts
@@ -1,7 +1,8 @@
import { useSetRecoilState } from 'recoil';
-import { QueryKeys } from 'librechat-data-provider';
import type { QueryClient } from '@tanstack/react-query';
-import type { TAttachment, EventSubmission } from 'librechat-data-provider';
+import { QueryKeys, Tools } from 'librechat-data-provider';
+import type { TAttachment, EventSubmission, MemoriesResponse } from 'librechat-data-provider';
+import { handleMemoryArtifact } from '~/utils/memory';
import store from '~/store';
export default function useAttachmentHandler(queryClient?: QueryClient) {
@@ -16,6 +17,18 @@ export default function useAttachmentHandler(queryClient?: QueryClient) {
});
}
+ if (queryClient && data.type === Tools.memory && data[Tools.memory]) {
+ const memoryArtifact = data[Tools.memory];
+
+ queryClient.setQueryData([QueryKeys.memories], (oldData: MemoriesResponse | undefined) => {
+ if (!oldData) {
+ return oldData;
+ }
+
+ return handleMemoryArtifact({ memoryArtifact, currentData: oldData }) || oldData;
+ });
+ }
+
setAttachmentsMap((prevMap) => {
const messageAttachments =
(prevMap as Record<string, TAttachment[] | undefined>)[messageId] || [];
diff --git a/client/src/hooks/usePersonalizationAccess.ts b/client/src/hooks/usePersonalizationAccess.ts
new file mode 100644
index 0000000000..9a419f4a63
--- /dev/null
+++ b/client/src/hooks/usePersonalizationAccess.ts
@@ -0,0 +1,16 @@
+import { PermissionTypes, Permissions } from 'librechat-data-provider';
+import useHasAccess from './Roles/useHasAccess';
+
+export default function usePersonalizationAccess() {
+ const hasMemoryOptOut = useHasAccess({
+ permissionType: PermissionTypes.MEMORIES,
+ permission: Permissions.OPT_OUT,
+ });
+
+ const hasAnyPersonalizationFeature = hasMemoryOptOut;
+
+ return {
+ hasMemoryOptOut,
+ hasAnyPersonalizationFeature,
+ };
+}
diff --git a/client/src/locales/en/translation.json b/client/src/locales/en/translation.json
index 284117ffe4..b235330c20 100644
--- a/client/src/locales/en/translation.json
+++ b/client/src/locales/en/translation.json
@@ -446,6 +446,7 @@
"com_nav_setting_data": "Data controls",
"com_nav_setting_general": "General",
"com_nav_setting_speech": "Speech",
+ "com_nav_setting_personalization": "Personalization",
"com_nav_settings": "Settings",
"com_nav_shared_links": "Shared links",
"com_nav_show_code": "Always show code when using code interpreter",
@@ -659,10 +660,12 @@
"com_ui_delete_confirm": "This will delete",
"com_ui_delete_confirm_prompt_version_var": "This will delete the selected version for \"{{0}}.\" If no other versions exist, the prompt will be deleted.",
"com_ui_delete_conversation": "Delete chat?",
+ "com_ui_delete_memory": "Delete Memory",
"com_ui_delete_prompt": "Delete Prompt?",
"com_ui_delete_shared_link": "Delete shared link?",
"com_ui_delete_tool": "Delete Tool",
"com_ui_delete_tool_confirm": "Are you sure you want to delete this tool?",
+ "com_ui_deleted": "Deleted",
"com_ui_descending": "Desc",
"com_ui_description": "Description",
"com_ui_description_placeholder": "Optional: Enter a description to display for the prompt",
@@ -770,6 +773,7 @@
"com_ui_include_shadcnui_agent": "Include shadcn/ui instructions",
"com_ui_input": "Input",
"com_ui_instructions": "Instructions",
+ "com_ui_key": "Key",
"com_ui_late_night": "Happy late night",
"com_ui_latest_footer": "Every AI for Everyone.",
"com_ui_latest_production_version": "Latest production version",
@@ -783,6 +787,17 @@
"com_ui_manage": "Manage",
"com_ui_max_tags": "Maximum number allowed is {{0}}, using latest values.",
"com_ui_mcp_servers": "MCP Servers",
+ "com_ui_memories": "Memories",
+ "com_ui_memories_filter": "Filter memories...",
+ "com_ui_memories_allow_use": "Allow using Memories",
+ "com_ui_memories_allow_create": "Allow creating Memories",
+ "com_ui_memories_allow_update": "Allow updating Memories",
+ "com_ui_memories_allow_read": "Allow reading Memories",
+ "com_ui_memories_allow_opt_out": "Allow users to opt out of Memories",
+ "com_ui_memory": "Memory",
+ "com_ui_usage": "Usage",
+ "com_ui_current": "Current",
+ "com_ui_tokens": "tokens",
"com_ui_mention": "Mention an endpoint, assistant, or preset to quickly switch to it",
"com_ui_min_tags": "Cannot remove more values, a minimum of {{0}} are required.",
"com_ui_misc": "Misc.",
@@ -800,7 +815,7 @@
"com_ui_no_bookmarks": "it seems like you have no bookmarks yet. Click on a chat and add a new one",
"com_ui_no_category": "No category",
"com_ui_no_changes": "No changes to update",
- "com_ui_no_data": "something needs to go here. was empty",
+ "com_ui_no_data": "No data available",
"com_ui_no_terms_content": "No terms and conditions content to display",
"com_ui_no_valid_items": "something needs to go here. was empty",
"com_ui_none": "None",
@@ -944,6 +959,8 @@
"com_ui_version_var": "Version {{0}}",
"com_ui_versions": "Versions",
"com_ui_view_source": "View source chat",
+ "com_ui_view_memory": "View Memory",
+ "com_ui_no_read_access": "You don't have permission to view memories",
"com_ui_web_search": "Web Search",
"com_ui_web_search_api_subtitle": "Search the web for up-to-date information",
"com_ui_web_search_cohere_key": "Enter Cohere API Key",
@@ -970,5 +987,23 @@
"com_ui_yes": "Yes",
"com_ui_zoom": "Zoom",
"com_user_message": "You",
- "com_warning_resubmit_unsupported": "Resubmitting the AI message is not supported for this endpoint."
+ "com_warning_resubmit_unsupported": "Resubmitting the AI message is not supported for this endpoint.",
+ "com_ui_value": "Value",
+ "com_ui_edit_memory": "Edit Memory",
+ "com_ui_enter_key": "Enter key",
+ "com_ui_enter_value": "Enter value",
+ "com_ui_memory_updated": "Updated saved memory",
+ "com_ui_memory_updated_items": "Updated Memories",
+ "com_ui_memory_deleted_items": "Deleted Memories",
+ "com_ui_memory_deleted": "Memory deleted",
+ "com_ui_reference_saved_memories": "Reference saved memories",
+ "com_ui_reference_saved_memories_description": "Allow the assistant to reference and use your saved memories when responding",
+ "com_ui_no_personalization_available": "No personalization options are currently available",
+ "com_ui_preferences_updated": "Preferences updated successfully",
+ "com_ui_error_updating_preferences": "Error updating preferences",
+ "com_ui_use_memory": "Use memory",
+ "com_ui_create_memory": "Create Memory",
+ "com_ui_memory_created": "Memory created successfully",
+ "com_ui_memory_key_exists": "A memory with this key already exists. Please use a different key."
}
+
diff --git a/client/src/utils/memory.ts b/client/src/utils/memory.ts
new file mode 100644
index 0000000000..49b383e9e4
--- /dev/null
+++ b/client/src/utils/memory.ts
@@ -0,0 +1,90 @@
+import type { MemoriesResponse, TUserMemory, MemoryArtifact } from 'librechat-data-provider';
+
+type HandleMemoryArtifactParams = {
+ memoryArtifact: MemoryArtifact;
+ currentData: MemoriesResponse;
+};
+
+/**
+ * Pure function to handle memory artifact updates
+ * @param params - Object containing memoryArtifact and currentData
+ * @returns Updated MemoriesResponse or undefined if no update needed
+ */
+export function handleMemoryArtifact({
+ memoryArtifact,
+ currentData,
+}: HandleMemoryArtifactParams): MemoriesResponse | undefined {
+ const { type, key, value, tokenCount = 0 } = memoryArtifact;
+
+ if (type === 'update' && !value) {
+ return undefined;
+ }
+
+ const memories = currentData.memories;
+ const existingIndex = memories.findIndex((m) => m.key === key);
+
+ if (type === 'delete') {
+ if (existingIndex === -1) {
+ return undefined;
+ }
+
+ const deletedMemory = memories[existingIndex];
+ const newMemories = [...memories];
+ newMemories.splice(existingIndex, 1);
+
+ const totalTokens = currentData.totalTokens - (deletedMemory.tokenCount || 0);
+ const usagePercentage = currentData.tokenLimit
+ ? Math.min(100, Math.round((totalTokens / currentData.tokenLimit) * 100))
+ : null;
+
+ return {
+ ...currentData,
+ memories: newMemories,
+ totalTokens,
+ usagePercentage,
+ };
+ }
+
+ if (type === 'update') {
+ const timestamp = new Date().toISOString();
+ let totalTokens = currentData.totalTokens;
+ let newMemories: TUserMemory[];
+
+ if (existingIndex >= 0) {
+ const oldTokenCount = memories[existingIndex].tokenCount || 0;
+ totalTokens = totalTokens - oldTokenCount + tokenCount;
+
+ newMemories = [...memories];
+ newMemories[existingIndex] = {
+ key,
+ value: value!,
+ tokenCount,
+ updated_at: timestamp,
+ };
+ } else {
+ totalTokens = totalTokens + tokenCount;
+ newMemories = [
+ ...memories,
+ {
+ key,
+ value: value!,
+ tokenCount,
+ updated_at: timestamp,
+ },
+ ];
+ }
+
+ const usagePercentage = currentData.tokenLimit
+ ? Math.min(100, Math.round((totalTokens / currentData.tokenLimit) * 100))
+ : null;
+
+ return {
+ ...currentData,
+ memories: newMemories,
+ totalTokens,
+ usagePercentage,
+ };
+ }
+
+ return undefined;
+}
diff --git a/config/packages.js b/config/packages.js
index 2ca187d5b9..e2a3b06b06 100644
--- a/config/packages.js
+++ b/config/packages.js
@@ -1,3 +1,5 @@
+// eslint-disable-next-line @typescript-eslint/ban-ts-comment
+// @ts-nocheck
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
@@ -10,7 +12,7 @@ const directories = [
rootDir,
path.resolve(rootDir, 'packages', 'data-provider'),
path.resolve(rootDir, 'packages', 'data-schemas'),
- path.resolve(rootDir, 'packages', 'mcp'),
+ path.resolve(rootDir, 'packages', 'api'),
path.resolve(rootDir, 'client'),
path.resolve(rootDir, 'api'),
];
diff --git a/config/update.js b/config/update.js
index 55867fedd6..e0dba111e5 100644
--- a/config/update.js
+++ b/config/update.js
@@ -1,3 +1,5 @@
+// eslint-disable-next-line @typescript-eslint/ban-ts-comment
+// @ts-nocheck
const path = require('path');
const { execSync } = require('child_process');
const { askQuestion, isDockerRunning, deleteNodeModules, silentExit } = require('./helpers');
@@ -17,7 +19,7 @@ const directories = [
rootDir,
path.resolve(rootDir, 'packages', 'data-provider'),
path.resolve(rootDir, 'packages', 'data-schemas'),
- path.resolve(rootDir, 'packages', 'mcp'),
+ path.resolve(rootDir, 'packages', 'api'),
path.resolve(rootDir, 'client'),
path.resolve(rootDir, 'api'),
];
@@ -128,7 +130,7 @@ async function validateDockerRunning() {
console.green('Your LibreChat app is now up to date! Start the app with the following command:');
console.purple(startCommand);
console.orange(
- 'Note: it\'s also recommended to clear your browser cookies and localStorage for LibreChat to assure a fully clean installation.',
+ "Note: it's also recommended to clear your browser cookies and localStorage for LibreChat to assure a fully clean installation.",
);
- console.orange('Also: Don\'t worry, your data is safe :)');
+ console.orange("Also: Don't worry, your data is safe :)");
})();
diff --git a/eslint.config.mjs b/eslint.config.mjs
index 03f0a81b5e..b602303047 100644
--- a/eslint.config.mjs
+++ b/eslint.config.mjs
@@ -31,9 +31,9 @@ export default [
'client/public/**/*',
'client/coverage/**/*',
'e2e/playwright-report/**/*',
- 'packages/mcp/types/**/*',
- 'packages/mcp/dist/**/*',
- 'packages/mcp/test_bundle/**/*',
+ 'packages/api/types/**/*',
+ 'packages/api/dist/**/*',
+ 'packages/api/test_bundle/**/*',
'api/demo/**/*',
'packages/data-provider/types/**/*',
'packages/data-provider/dist/**/*',
@@ -317,7 +317,7 @@ export default [
files: ['./api/demo/**/*.ts'],
},
{
- files: ['./packages/mcp/**/*.ts'],
+ files: ['./packages/api/**/*.ts'],
},
{
files: ['./config/translations/**/*.ts'],
@@ -351,12 +351,12 @@ export default [
},
},
{
- files: ['./packages/mcp/specs/**/*.ts'],
+ files: ['./packages/api/specs/**/*.ts'],
languageOptions: {
ecmaVersion: 5,
sourceType: 'script',
parserOptions: {
- project: './packages/mcp/tsconfig.spec.json',
+ project: './packages/api/tsconfig.spec.json',
},
},
},
diff --git a/librechat.example.yaml b/librechat.example.yaml
index 38cb26eb4d..fbe2612722 100644
--- a/librechat.example.yaml
+++ b/librechat.example.yaml
@@ -299,3 +299,25 @@ endpoints:
# px: 1024
# # See the Custom Configuration Guide for more information on Assistants Config:
# # https://www.librechat.ai/docs/configuration/librechat_yaml/object_structure/assistants_endpoint
+
+# Memory configuration for user memories
+# memory:
+# # (optional) Disable memory functionality
+# disabled: false
+# # (optional) Restrict memory keys to specific values to limit memory storage and improve consistency
+# validKeys: ["preferences", "work_info", "personal_info", "skills", "interests", "context"]
+# # (optional) Maximum token limit for memory storage (not yet implemented for token counting)
+# tokenLimit: 10000
+# # (optional) Enable personalization features (defaults to true if memory is configured)
+# # When false, users will not see the Personalization tab in settings
+# personalize: true
+# # Memory agent configuration - either use an existing agent by ID or define inline
+# agent:
+# # Option 1: Use existing agent by ID
+# id: "your-memory-agent-id"
+# # Option 2: Define agent inline
+# # provider: "openai"
+# # model: "gpt-4o-mini"
+# # instructions: "You are a memory management assistant. Store and manage user information accurately."
+# # model_parameters:
+# # temperature: 0.1
diff --git a/package-lock.json b/package-lock.json
index f53d7389ee..35a5a388ea 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -65,6 +65,7 @@
"@langchain/google-vertexai": "^0.2.9",
"@langchain/textsplitters": "^0.1.0",
"@librechat/agents": "^2.4.38",
+ "@librechat/api": "*",
"@librechat/data-schemas": "*",
"@node-saml/passport-saml": "^5.0.0",
"@waylaidwanderer/fetch-event-source": "^3.0.1",
@@ -97,7 +98,6 @@
"keyv-file": "^5.1.2",
"klona": "^2.0.6",
"librechat-data-provider": "*",
- "librechat-mcp": "*",
"lodash": "^4.17.21",
"meilisearch": "^0.38.0",
"memorystore": "^1.6.7",
@@ -106,6 +106,7 @@
"mongoose": "^8.12.1",
"multer": "^2.0.0",
"nanoid": "^3.3.7",
+ "node-fetch": "^2.7.0",
"nodemailer": "^6.9.15",
"ollama": "^0.5.0",
"openai": "^4.96.2",
@@ -126,7 +127,7 @@
"traverse": "^0.6.7",
"ua-parser-js": "^1.0.36",
"winston": "^3.11.0",
- "winston-daily-rotate-file": "^4.7.1",
+ "winston-daily-rotate-file": "^5.0.0",
"youtube-transcript": "^1.2.1",
"zod": "^3.22.4"
},
@@ -2372,23 +2373,13 @@
"node": ">= 10.16.0"
}
},
- "api/node_modules/node-fetch": {
- "version": "2.6.7",
- "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz",
- "integrity": "sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==",
- "dependencies": {
- "whatwg-url": "^5.0.0"
- },
+ "api/node_modules/object-hash": {
+ "version": "3.0.0",
+ "resolved": "https://registry.npmjs.org/object-hash/-/object-hash-3.0.0.tgz",
+ "integrity": "sha512-RSn9F68PjH9HqtltsSnqYC1XXoWe9Bju5+213R98cNGttag9q9yAOTzdbsqvIa7aNm5WffBZFpWYr2aWrklWAw==",
+ "license": "MIT",
"engines": {
- "node": "4.x || >=6.0.0"
- },
- "peerDependencies": {
- "encoding": "^0.1.0"
- },
- "peerDependenciesMeta": {
- "encoding": {
- "optional": true
- }
+ "node": ">= 6"
}
},
"api/node_modules/openid-client": {
@@ -2442,11 +2433,6 @@
"@img/sharp-win32-x64": "0.33.5"
}
},
- "api/node_modules/tr46": {
- "version": "0.0.3",
- "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
- "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw=="
- },
"api/node_modules/uuid": {
"version": "10.0.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-10.0.0.tgz",
@@ -2459,18 +2445,22 @@
"uuid": "dist/bin/uuid"
}
},
- "api/node_modules/webidl-conversions": {
- "version": "3.0.1",
- "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",
- "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ=="
- },
- "api/node_modules/whatwg-url": {
+ "api/node_modules/winston-daily-rotate-file": {
"version": "5.0.0",
- "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz",
- "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==",
+ "resolved": "https://registry.npmjs.org/winston-daily-rotate-file/-/winston-daily-rotate-file-5.0.0.tgz",
+ "integrity": "sha512-JDjiXXkM5qvwY06733vf09I2wnMXpZEhxEVOSPenZMii+g7pcDcTBt2MRugnoi8BwVSuCT2jfRXBUy+n1Zz/Yw==",
+ "license": "MIT",
"dependencies": {
- "tr46": "~0.0.3",
- "webidl-conversions": "^3.0.0"
+ "file-stream-rotator": "^0.6.1",
+ "object-hash": "^3.0.0",
+ "triple-beam": "^1.4.1",
+ "winston-transport": "^4.7.0"
+ },
+ "engines": {
+ "node": ">=8"
+ },
+ "peerDependencies": {
+ "winston": "^3"
}
},
"client": {
@@ -19850,6 +19840,10 @@
"uuid": "dist/bin/uuid"
}
},
+ "node_modules/@librechat/api": {
+ "resolved": "packages/api",
+ "link": true
+ },
"node_modules/@librechat/backend": {
"resolved": "api",
"link": true
@@ -20107,6 +20101,7 @@
"resolved": "https://registry.npmjs.org/@modelcontextprotocol/sdk/-/sdk-1.11.2.tgz",
"integrity": "sha512-H9vwztj5OAqHg9GockCQC06k1natgcxWQSRpQcPJf6i5+MWBzfKkRtxGbjQf0X2ihii0ffLZCRGbYV2f2bjNCQ==",
"license": "MIT",
+ "peer": true,
"dependencies": {
"content-type": "^1.0.5",
"cors": "^2.8.5",
@@ -20128,6 +20123,7 @@
"resolved": "https://registry.npmjs.org/accepts/-/accepts-2.0.0.tgz",
"integrity": "sha512-5cvg6CtKwfgdmVqY1WIiXKc3Q1bkRqGLi+2W/6ao+6Y7gu/RCwRuAhGEzh5B4KlszSuTLgZYuqFqo5bImjNKng==",
"license": "MIT",
+ "peer": true,
"dependencies": {
"mime-types": "^3.0.0",
"negotiator": "^1.0.0"
@@ -20141,6 +20137,7 @@
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-2.2.0.tgz",
"integrity": "sha512-02qvAaxv8tp7fBa/mw1ga98OGm+eCbqzJOKoRt70sLmfEEi+jyBYVTDGfCL/k06/4EMk/z01gCe7HoCH/f2LTg==",
"license": "MIT",
+ "peer": true,
"dependencies": {
"bytes": "^3.1.2",
"content-type": "^1.0.5",
@@ -20161,6 +20158,7 @@
"resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-1.0.0.tgz",
"integrity": "sha512-Au9nRL8VNUut/XSzbQA38+M78dzP4D+eqg3gfJHMIHHYa3bg067xj1KxMUWj+VULbiZMowKngFFbKczUrNJ1mg==",
"license": "MIT",
+ "peer": true,
"dependencies": {
"safe-buffer": "5.2.1"
},
@@ -20173,6 +20171,7 @@
"resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.2.2.tgz",
"integrity": "sha512-D76uU73ulSXrD1UXF4KE2TMxVVwhsnCgfAyTg9k8P6KGZjlXKrOLe4dJQKI3Bxi5wjesZoFXJWElNWBjPZMbhg==",
"license": "MIT",
+ "peer": true,
"engines": {
"node": ">=6.6.0"
}
@@ -20182,6 +20181,7 @@
"resolved": "https://registry.npmjs.org/express/-/express-5.1.0.tgz",
"integrity": "sha512-DT9ck5YIRU+8GYzzU5kT3eHGA5iL+1Zd0EutOmTE9Dtk+Tvuzd23VBU+ec7HPNSTxXYO55gPV/hq4pSBJDjFpA==",
"license": "MIT",
+ "peer": true,
"dependencies": {
"accepts": "^2.0.0",
"body-parser": "^2.2.0",
@@ -20224,6 +20224,7 @@
"resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-2.1.0.tgz",
"integrity": "sha512-/t88Ty3d5JWQbWYgaOGCCYfXRwV1+be02WqYYlL6h0lEiUAMPM8o8qKGO01YIkOHzka2up08wvgYD0mDiI+q3Q==",
"license": "MIT",
+ "peer": true,
"dependencies": {
"debug": "^4.4.0",
"encodeurl": "^2.0.0",
@@ -20241,6 +20242,7 @@
"resolved": "https://registry.npmjs.org/fresh/-/fresh-2.0.0.tgz",
"integrity": "sha512-Rx/WycZ60HOaqLKAi6cHRKKI7zxWbJ31MhntmtwMoaTeF7XFH9hhBp8vITaMidfljRQ6eYWCKkaTK+ykVJHP2A==",
"license": "MIT",
+ "peer": true,
"engines": {
"node": ">= 0.8"
}
@@ -20250,6 +20252,7 @@
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz",
"integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==",
"license": "MIT",
+ "peer": true,
"dependencies": {
"safer-buffer": ">= 2.1.2 < 3.0.0"
},
@@ -20262,6 +20265,7 @@
"resolved": "https://registry.npmjs.org/media-typer/-/media-typer-1.1.0.tgz",
"integrity": "sha512-aisnrDP4GNe06UcKFnV5bfMNPBUw4jsLGaWwWfnH3v02GnBuXX2MCVn5RbrWo0j3pczUilYblq7fQ7Nw2t5XKw==",
"license": "MIT",
+ "peer": true,
"engines": {
"node": ">= 0.8"
}
@@ -20271,6 +20275,7 @@
"resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-2.0.0.tgz",
"integrity": "sha512-Snk314V5ayFLhp3fkUREub6WtjBfPdCPY1Ln8/8munuLuiYhsABgBVWsozAG+MWMbVEvcdcpbi9R7ww22l9Q3g==",
"license": "MIT",
+ "peer": true,
"engines": {
"node": ">=18"
},
@@ -20283,6 +20288,7 @@
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.54.0.tgz",
"integrity": "sha512-aU5EJuIN2WDemCcAp2vFBfp/m4EAhWJnUNSSw0ixs7/kXbd6Pg64EmwJkNdFhB8aWt1sH2CTXrLxo/iAGV3oPQ==",
"license": "MIT",
+ "peer": true,
"engines": {
"node": ">= 0.6"
}
@@ -20292,6 +20298,7 @@
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-3.0.1.tgz",
"integrity": "sha512-xRc4oEhT6eaBpU1XF7AjpOFD+xQmXNB5OVKwp4tqCuBpHLS/ZbBDrc07mYTDqVMg6PfxUjjNp85O6Cd2Z/5HWA==",
"license": "MIT",
+ "peer": true,
"dependencies": {
"mime-db": "^1.54.0"
},
@@ -20304,6 +20311,7 @@
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-1.0.0.tgz",
"integrity": "sha512-8Ofs/AUQh8MaEcrlq5xOX0CQ9ypTF5dl78mjlMNfOK08fzpgTHQRQPBxcPlEtIw0yRpws+Zo/3r+5WRby7u3Gg==",
"license": "MIT",
+ "peer": true,
"engines": {
"node": ">= 0.6"
}
@@ -20313,6 +20321,7 @@
"resolved": "https://registry.npmjs.org/qs/-/qs-6.14.0.tgz",
"integrity": "sha512-YWWTjgABSKcvs/nWBi9PycY/JiPJqOD4JA6o9Sej2AtvSGarXxKC3OQSk4pAarbdQlKAh5D4FCQkJNkW+GAn3w==",
"license": "BSD-3-Clause",
+ "peer": true,
"dependencies": {
"side-channel": "^1.1.0"
},
@@ -20328,6 +20337,7 @@
"resolved": "https://registry.npmjs.org/raw-body/-/raw-body-3.0.0.tgz",
"integrity": "sha512-RmkhL8CAyCRPXCE28MMH0z2PNWQBNk2Q09ZdxM9IOOXwxwZbN+qbWaatPkdkWIKL2ZVDImrN/pK5HTRz2PcS4g==",
"license": "MIT",
+ "peer": true,
"dependencies": {
"bytes": "3.1.2",
"http-errors": "2.0.0",
@@ -20343,6 +20353,7 @@
"resolved": "https://registry.npmjs.org/send/-/send-1.2.0.tgz",
"integrity": "sha512-uaW0WwXKpL9blXE2o0bRhoL2EGXIrZxQ2ZQ4mgcfoBxdFmQold+qWsD2jLrfZ0trjKL6vOw0j//eAwcALFjKSw==",
"license": "MIT",
+ "peer": true,
"dependencies": {
"debug": "^4.3.5",
"encodeurl": "^2.0.0",
@@ -20365,6 +20376,7 @@
"resolved": "https://registry.npmjs.org/serve-static/-/serve-static-2.2.0.tgz",
"integrity": "sha512-61g9pCh0Vnh7IutZjtLGGpTA355+OPn2TyDv/6ivP2h/AdAVX9azsoxmg2/M6nZeQZNYBEwIcsne1mJd9oQItQ==",
"license": "MIT",
+ "peer": true,
"dependencies": {
"encodeurl": "^2.0.0",
"escape-html": "^1.0.3",
@@ -20380,6 +20392,7 @@
"resolved": "https://registry.npmjs.org/type-is/-/type-is-2.0.1.tgz",
"integrity": "sha512-OZs6gsjF4vMp32qrCbiVSkrFmXtG/AZhY3t0iAMrMBiAZyV9oALtXO8hsrHbMXF9x6L3grlFuwW2oAz7cav+Gw==",
"license": "MIT",
+ "peer": true,
"dependencies": {
"content-type": "^1.0.5",
"media-typer": "^1.1.0",
@@ -25236,6 +25249,16 @@
"@types/node": "*"
}
},
+ "node_modules/@types/bun": {
+ "version": "1.2.15",
+ "resolved": "https://registry.npmjs.org/@types/bun/-/bun-1.2.15.tgz",
+ "integrity": "sha512-U1ljPdBEphF0nw1MIk0hI7kPg7dFdPyM7EenHsp6W5loNHl7zqy6JQf/RKCgnUn2KDzUpkBwHPnEJEjII594bA==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "bun-types": "1.2.15"
+ }
+ },
"node_modules/@types/connect": {
"version": "3.4.38",
"resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.38.tgz",
@@ -27280,6 +27303,16 @@
"integrity": "sha512-HpGFw18DgFWlncDfjTa2rcQ4W88O1mC8e8yZ2AvQY5KDaktSTwo+KRf6nHK6FRI5FyRyb/5T6+TSxfP7QyGsmQ==",
"dev": true
},
+ "node_modules/bun-types": {
+ "version": "1.2.15",
+ "resolved": "https://registry.npmjs.org/bun-types/-/bun-types-1.2.15.tgz",
+ "integrity": "sha512-NarRIaS+iOaQU1JPfyKhZm4AsUOrwUOqRNHY0XxI8GI8jYxiLXLcdjYMG9UKS+fwWasc1uw1htV9AX24dD+p4w==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "@types/node": "*"
+ }
+ },
"node_modules/bundle-name": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/bundle-name/-/bundle-name-4.1.0.tgz",
@@ -28881,6 +28914,7 @@
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/diff/-/diff-7.0.0.tgz",
"integrity": "sha512-PJWHUb1RFevKCwaFA9RlG5tCd+FO5iRh9A8HEtkmBH2Li03iJriB6m6JIN4rGz3K3JLawI7/veA1xzRKP6ISBw==",
+ "peer": true,
"engines": {
"node": ">=0.3.1"
}
@@ -30431,6 +30465,7 @@
"version": "7.5.0",
"resolved": "https://registry.npmjs.org/express-rate-limit/-/express-rate-limit-7.5.0.tgz",
"integrity": "sha512-eB5zbQh5h+VenMPM3fh+nw1YExi5nMr6HUCR62ELSP11huvxm/Uir1H1QEyTkk5QX6A58pX6NmaTMceKZ0Eodg==",
+ "peer": true,
"engines": {
"node": ">= 16"
},
@@ -33004,7 +33039,8 @@
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/is-promise/-/is-promise-4.0.0.tgz",
"integrity": "sha512-hvpoI6korhJMnej285dSg6nu1+e6uxs7zG3BYAm5byqDsgJNWwxzM6z6iZiAgQR4TJ30JmBTOwqZUw3WlyH3AQ==",
- "license": "MIT"
+ "license": "MIT",
+ "peer": true
},
"node_modules/is-reference": {
"version": "1.2.1",
@@ -34656,10 +34692,6 @@
"resolved": "packages/data-provider",
"link": true
},
- "node_modules/librechat-mcp": {
- "resolved": "packages/mcp",
- "link": true
- },
"node_modules/lilconfig": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/lilconfig/-/lilconfig-2.1.0.tgz",
@@ -37152,6 +37184,7 @@
"version": "2.7.0",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz",
"integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==",
+ "license": "MIT",
"dependencies": {
"whatwg-url": "^5.0.0"
},
@@ -37170,17 +37203,20 @@
"node_modules/node-fetch/node_modules/tr46": {
"version": "0.0.3",
"resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
- "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw=="
+ "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==",
+ "license": "MIT"
},
"node_modules/node-fetch/node_modules/webidl-conversions": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",
- "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ=="
+ "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==",
+ "license": "BSD-2-Clause"
},
"node_modules/node-fetch/node_modules/whatwg-url": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz",
"integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==",
+ "license": "MIT",
"dependencies": {
"tr46": "~0.0.3",
"webidl-conversions": "^3.0.0"
@@ -37453,14 +37489,6 @@
"node": ">=0.10.0"
}
},
- "node_modules/object-hash": {
- "version": "2.2.0",
- "resolved": "https://registry.npmjs.org/object-hash/-/object-hash-2.2.0.tgz",
- "integrity": "sha512-gScRMn0bS5fH+IuwyIFgnh9zBdo4DV+6GhygmWM9HyNJSgS0hScp1f5vjtm7oIIOiT9trXrShAkLFSc2IqKNgw==",
- "engines": {
- "node": ">= 6"
- }
- },
"node_modules/object-inspect": {
"version": "1.13.4",
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.4.tgz",
@@ -38173,6 +38201,7 @@
"resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-8.2.0.tgz",
"integrity": "sha512-TdrF7fW9Rphjq4RjrW0Kp2AW0Ahwu9sRGTkS6bvDi0SCwZlEZYmcfDbEsTz8RVk0EHIS/Vd1bv3JhG+1xZuAyQ==",
"license": "MIT",
+ "peer": true,
"engines": {
"node": ">=16"
}
@@ -38267,6 +38296,7 @@
"resolved": "https://registry.npmjs.org/pkce-challenge/-/pkce-challenge-5.0.0.tgz",
"integrity": "sha512-ueGLflrrnvwB3xuo/uGob5pd5FN7l0MsLf0Z87o/UQmRtwjvfylfc9MurIxRAWywCYTgrvpXBcqjV4OfCYGCIQ==",
"license": "MIT",
+ "peer": true,
"engines": {
"node": ">=16.20.0"
}
@@ -41716,6 +41746,7 @@
"resolved": "https://registry.npmjs.org/router/-/router-2.2.0.tgz",
"integrity": "sha512-nLTrUKm2UyiL7rlhapu/Zl45FwNgkZGaCpZbIHajDYgwlJCOzLSk+cIPAnsEqV955GjILJnKbdQC1nVPz+gAYQ==",
"license": "MIT",
+ "peer": true,
"dependencies": {
"debug": "^4.4.0",
"depd": "^2.0.0",
@@ -45120,23 +45151,6 @@
"node": ">= 12.0.0"
}
},
- "node_modules/winston-daily-rotate-file": {
- "version": "4.7.1",
- "resolved": "https://registry.npmjs.org/winston-daily-rotate-file/-/winston-daily-rotate-file-4.7.1.tgz",
- "integrity": "sha512-7LGPiYGBPNyGHLn9z33i96zx/bd71pjBn9tqQzO3I4Tayv94WPmBNwKC7CO1wPHdP9uvu+Md/1nr6VSH9h0iaA==",
- "dependencies": {
- "file-stream-rotator": "^0.6.1",
- "object-hash": "^2.0.1",
- "triple-beam": "^1.3.0",
- "winston-transport": "^4.4.0"
- },
- "engines": {
- "node": ">=8"
- },
- "peerDependencies": {
- "winston": "^3"
- }
- },
"node_modules/winston-transport": {
"version": "4.7.0",
"resolved": "https://registry.npmjs.org/winston-transport/-/winston-transport-4.7.0.tgz",
@@ -46004,6 +46018,126 @@
"url": "https://github.com/sponsors/wooorm"
}
},
+ "packages/api": {
+ "name": "@librechat/api",
+ "version": "1.2.2",
+ "license": "ISC",
+ "devDependencies": {
+ "@babel/preset-env": "^7.21.5",
+ "@babel/preset-react": "^7.18.6",
+ "@babel/preset-typescript": "^7.21.0",
+ "@rollup/plugin-alias": "^5.1.0",
+ "@rollup/plugin-commonjs": "^25.0.2",
+ "@rollup/plugin-json": "^6.1.0",
+ "@rollup/plugin-node-resolve": "^15.1.0",
+ "@rollup/plugin-replace": "^5.0.5",
+ "@rollup/plugin-terser": "^0.4.4",
+ "@rollup/plugin-typescript": "^12.1.2",
+ "@types/bun": "^1.2.15",
+ "@types/diff": "^6.0.0",
+ "@types/express": "^5.0.0",
+ "@types/jest": "^29.5.2",
+ "@types/node": "^20.3.0",
+ "@types/react": "^18.2.18",
+ "@types/winston": "^2.4.4",
+ "jest": "^29.5.0",
+ "jest-junit": "^16.0.0",
+ "librechat-data-provider": "*",
+ "rimraf": "^5.0.1",
+ "rollup": "^4.22.4",
+ "rollup-plugin-generate-package-json": "^3.2.0",
+ "rollup-plugin-peer-deps-external": "^2.2.4",
+ "ts-node": "^10.9.2",
+ "typescript": "^5.0.4"
+ },
+ "peerDependencies": {
+ "@librechat/agents": "^2.4.37",
+ "@librechat/data-schemas": "*",
+ "@modelcontextprotocol/sdk": "^1.11.2",
+ "diff": "^7.0.0",
+ "eventsource": "^3.0.2",
+ "express": "^4.21.2",
+ "keyv": "^5.3.2",
+ "librechat-data-provider": "*",
+ "node-fetch": "2.7.0",
+ "tiktoken": "^1.0.15",
+ "zod": "^3.22.4"
+ }
+ },
+ "packages/api/node_modules/brace-expansion": {
+ "version": "2.0.1",
+ "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz",
+ "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==",
+ "dev": true,
+ "dependencies": {
+ "balanced-match": "^1.0.0"
+ }
+ },
+ "packages/api/node_modules/glob": {
+ "version": "10.4.5",
+ "resolved": "https://registry.npmjs.org/glob/-/glob-10.4.5.tgz",
+ "integrity": "sha512-7Bv8RF0k6xjo7d4A/PxYLbUCfb6c+Vpd2/mB2yRDlew7Jb5hEXiCD9ibfO7wpk8i4sevK6DFny9h7EYbM3/sHg==",
+ "dev": true,
+ "dependencies": {
+ "foreground-child": "^3.1.0",
+ "jackspeak": "^3.1.2",
+ "minimatch": "^9.0.4",
+ "minipass": "^7.1.2",
+ "package-json-from-dist": "^1.0.0",
+ "path-scurry": "^1.11.1"
+ },
+ "bin": {
+ "glob": "dist/esm/bin.mjs"
+ },
+ "funding": {
+ "url": "https://github.com/sponsors/isaacs"
+ }
+ },
+ "packages/api/node_modules/jackspeak": {
+ "version": "3.4.3",
+ "resolved": "https://registry.npmjs.org/jackspeak/-/jackspeak-3.4.3.tgz",
+ "integrity": "sha512-OGlZQpz2yfahA/Rd1Y8Cd9SIEsqvXkLVoSw/cgwhnhFMDbsQFeZYoJJ7bIZBS9BcamUW96asq/npPWugM+RQBw==",
+ "dev": true,
+ "dependencies": {
+ "@isaacs/cliui": "^8.0.2"
+ },
+ "funding": {
+ "url": "https://github.com/sponsors/isaacs"
+ },
+ "optionalDependencies": {
+ "@pkgjs/parseargs": "^0.11.0"
+ }
+ },
+ "packages/api/node_modules/minimatch": {
+ "version": "9.0.5",
+ "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-9.0.5.tgz",
+ "integrity": "sha512-G6T0ZX48xgozx7587koeX9Ys2NYy6Gmv//P89sEte9V9whIapMNF4idKxnW2QtCcLiTWlb/wfCabAtAFWhhBow==",
+ "dev": true,
+ "dependencies": {
+ "brace-expansion": "^2.0.1"
+ },
+ "engines": {
+ "node": ">=16 || 14 >=14.17"
+ },
+ "funding": {
+ "url": "https://github.com/sponsors/isaacs"
+ }
+ },
+ "packages/api/node_modules/rimraf": {
+ "version": "5.0.10",
+ "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-5.0.10.tgz",
+ "integrity": "sha512-l0OE8wL34P4nJH/H2ffoaniAokM2qSmrtXHmlpvYr5AVVX8msAyW0l8NVJFDxlSK4u3Uh/f41cQheDVdnYijwQ==",
+ "dev": true,
+ "dependencies": {
+ "glob": "^10.3.7"
+ },
+ "bin": {
+ "rimraf": "dist/esm/bin.mjs"
+ },
+ "funding": {
+ "url": "https://github.com/sponsors/isaacs"
+ }
+ },
"packages/auth": {
"name": "@librechat/auth",
"version": "0.0.1",
@@ -46046,7 +46180,7 @@
},
"packages/data-provider": {
"name": "librechat-data-provider",
- "version": "0.7.86",
+ "version": "0.7.87",
"license": "ISC",
"dependencies": {
"axios": "^1.8.2",
@@ -46481,121 +46615,6 @@
"engines": {
"node": ">= 12.0.0"
}
- },
- "packages/mcp": {
- "name": "librechat-mcp",
- "version": "1.2.2",
- "license": "ISC",
- "dependencies": {
- "@modelcontextprotocol/sdk": "^1.11.2",
- "diff": "^7.0.0",
- "eventsource": "^3.0.2",
- "express": "^4.21.2"
- },
- "devDependencies": {
- "@babel/preset-env": "^7.21.5",
- "@babel/preset-react": "^7.18.6",
- "@babel/preset-typescript": "^7.21.0",
- "@rollup/plugin-alias": "^5.1.0",
- "@rollup/plugin-commonjs": "^25.0.2",
- "@rollup/plugin-json": "^6.1.0",
- "@rollup/plugin-node-resolve": "^15.1.0",
- "@rollup/plugin-replace": "^5.0.5",
- "@rollup/plugin-terser": "^0.4.4",
- "@rollup/plugin-typescript": "^12.1.2",
- "@types/diff": "^6.0.0",
- "@types/express": "^5.0.0",
- "@types/jest": "^29.5.2",
- "@types/node": "^20.3.0",
- "@types/react": "^18.2.18",
- "@types/winston": "^2.4.4",
- "jest": "^29.5.0",
- "jest-junit": "^16.0.0",
- "librechat-data-provider": "*",
- "rimraf": "^5.0.1",
- "rollup": "^4.22.4",
- "rollup-plugin-generate-package-json": "^3.2.0",
- "rollup-plugin-peer-deps-external": "^2.2.4",
- "ts-node": "^10.9.2",
- "typescript": "^5.0.4"
- },
- "peerDependencies": {
- "keyv": "^5.3.2"
- }
- },
- "packages/mcp/node_modules/brace-expansion": {
- "version": "2.0.1",
- "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz",
- "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==",
- "dev": true,
- "dependencies": {
- "balanced-match": "^1.0.0"
- }
- },
- "packages/mcp/node_modules/glob": {
- "version": "10.4.5",
- "resolved": "https://registry.npmjs.org/glob/-/glob-10.4.5.tgz",
- "integrity": "sha512-7Bv8RF0k6xjo7d4A/PxYLbUCfb6c+Vpd2/mB2yRDlew7Jb5hEXiCD9ibfO7wpk8i4sevK6DFny9h7EYbM3/sHg==",
- "dev": true,
- "dependencies": {
- "foreground-child": "^3.1.0",
- "jackspeak": "^3.1.2",
- "minimatch": "^9.0.4",
- "minipass": "^7.1.2",
- "package-json-from-dist": "^1.0.0",
- "path-scurry": "^1.11.1"
- },
- "bin": {
- "glob": "dist/esm/bin.mjs"
- },
- "funding": {
- "url": "https://github.com/sponsors/isaacs"
- }
- },
- "packages/mcp/node_modules/jackspeak": {
- "version": "3.4.3",
- "resolved": "https://registry.npmjs.org/jackspeak/-/jackspeak-3.4.3.tgz",
- "integrity": "sha512-OGlZQpz2yfahA/Rd1Y8Cd9SIEsqvXkLVoSw/cgwhnhFMDbsQFeZYoJJ7bIZBS9BcamUW96asq/npPWugM+RQBw==",
- "dev": true,
- "dependencies": {
- "@isaacs/cliui": "^8.0.2"
- },
- "funding": {
- "url": "https://github.com/sponsors/isaacs"
- },
- "optionalDependencies": {
- "@pkgjs/parseargs": "^0.11.0"
- }
- },
- "packages/mcp/node_modules/minimatch": {
- "version": "9.0.5",
- "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-9.0.5.tgz",
- "integrity": "sha512-G6T0ZX48xgozx7587koeX9Ys2NYy6Gmv//P89sEte9V9whIapMNF4idKxnW2QtCcLiTWlb/wfCabAtAFWhhBow==",
- "dev": true,
- "dependencies": {
- "brace-expansion": "^2.0.1"
- },
- "engines": {
- "node": ">=16 || 14 >=14.17"
- },
- "funding": {
- "url": "https://github.com/sponsors/isaacs"
- }
- },
- "packages/mcp/node_modules/rimraf": {
- "version": "5.0.10",
- "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-5.0.10.tgz",
- "integrity": "sha512-l0OE8wL34P4nJH/H2ffoaniAokM2qSmrtXHmlpvYr5AVVX8msAyW0l8NVJFDxlSK4u3Uh/f41cQheDVdnYijwQ==",
- "dev": true,
- "dependencies": {
- "glob": "^10.3.7"
- },
- "bin": {
- "rimraf": "dist/esm/bin.mjs"
- },
- "funding": {
- "url": "https://github.com/sponsors/isaacs"
- }
}
}
}
diff --git a/package.json b/package.json
index e24c5b43cc..cb33ac9164 100644
--- a/package.json
+++ b/package.json
@@ -38,9 +38,9 @@
"backend:dev": "cross-env NODE_ENV=development npx nodemon api/server/index.js",
"backend:stop": "node config/stop-backend.js",
"build:data-provider": "cd packages/data-provider && npm run build",
- "build:mcp": "cd packages/mcp && npm run build",
+ "build:api": "cd packages/api && npm run build",
"build:data-schemas": "cd packages/data-schemas && npm run build",
- "frontend": "npm run build:data-provider && npm run build:mcp && npm run build:data-schemas && cd client && npm run build",
+ "frontend": "npm run build:data-provider && npm run build:data-schemas && npm run build:api && cd client && npm run build",
"frontend:ci": "npm run build:data-provider && cd client && npm run build:ci",
"frontend:dev": "cd client && npm run dev",
"e2e": "playwright test --config=e2e/playwright.config.local.ts",
@@ -62,7 +62,7 @@
"b:api-inspect": "NODE_ENV=production bun --inspect run api/server/index.js",
"b:api:dev": "NODE_ENV=production bun run --watch api/server/index.js",
"b:data": "cd packages/data-provider && bun run b:build",
- "b:mcp": "cd packages/mcp && bun run b:build",
+ "b:mcp": "cd packages/api && bun run b:build",
"b:data-schemas": "cd packages/data-schemas && bun run b:build",
"b:client": "bun --bun run b:data && bun --bun run b:mcp && bun --bun run b:data-schemas && cd client && bun --bun run b:build",
"b:client:dev": "cd client && bun run b:dev",
diff --git a/packages/mcp/.gitignore b/packages/api/.gitignore
similarity index 100%
rename from packages/mcp/.gitignore
rename to packages/api/.gitignore
diff --git a/packages/mcp/babel.config.cjs b/packages/api/babel.config.cjs
similarity index 100%
rename from packages/mcp/babel.config.cjs
rename to packages/api/babel.config.cjs
diff --git a/packages/mcp/jest.config.mjs b/packages/api/jest.config.mjs
similarity index 100%
rename from packages/mcp/jest.config.mjs
rename to packages/api/jest.config.mjs
diff --git a/packages/mcp/package.json b/packages/api/package.json
similarity index 90%
rename from packages/mcp/package.json
rename to packages/api/package.json
index 5bfb680e4f..6b2462684e 100644
--- a/packages/mcp/package.json
+++ b/packages/api/package.json
@@ -1,5 +1,5 @@
{
- "name": "librechat-mcp",
+ "name": "@librechat/api",
"version": "1.2.2",
"type": "commonjs",
"description": "MCP services for LibreChat",
@@ -47,6 +47,7 @@
"@rollup/plugin-replace": "^5.0.5",
"@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-typescript": "^12.1.2",
+ "@types/bun": "^1.2.15",
"@types/diff": "^6.0.0",
"@types/express": "^5.0.0",
"@types/jest": "^29.5.2",
@@ -66,13 +67,17 @@
"publishConfig": {
"registry": "https://registry.npmjs.org/"
},
- "dependencies": {
+ "peerDependencies": {
+ "@librechat/agents": "^2.4.37",
+ "@librechat/data-schemas": "*",
+ "librechat-data-provider": "*",
"@modelcontextprotocol/sdk": "^1.11.2",
"diff": "^7.0.0",
"eventsource": "^3.0.2",
- "express": "^4.21.2"
- },
- "peerDependencies": {
- "keyv": "^5.3.2"
+ "express": "^4.21.2",
+ "node-fetch": "2.7.0",
+ "keyv": "^5.3.2",
+ "zod": "^3.22.4",
+ "tiktoken": "^1.0.15"
}
}
diff --git a/packages/mcp/rollup.config.js b/packages/api/rollup.config.js
similarity index 100%
rename from packages/mcp/rollup.config.js
rename to packages/api/rollup.config.js
diff --git a/packages/api/src/agents/index.ts b/packages/api/src/agents/index.ts
new file mode 100644
index 0000000000..ed003606ed
--- /dev/null
+++ b/packages/api/src/agents/index.ts
@@ -0,0 +1,3 @@
+export * from './memory';
+export * from './resources';
+export * from './run';
diff --git a/packages/api/src/agents/memory.ts b/packages/api/src/agents/memory.ts
new file mode 100644
index 0000000000..c9e4d14063
--- /dev/null
+++ b/packages/api/src/agents/memory.ts
@@ -0,0 +1,468 @@
+/** Memories */
+import { z } from 'zod';
+import { tool } from '@langchain/core/tools';
+import { Tools } from 'librechat-data-provider';
+import { logger } from '@librechat/data-schemas';
+import { Run, Providers, GraphEvents } from '@librechat/agents';
+import type {
+ StreamEventData,
+ ToolEndCallback,
+ EventHandler,
+ ToolEndData,
+ LLMConfig,
+} from '@librechat/agents';
+import type { TAttachment, MemoryArtifact } from 'librechat-data-provider';
+import type { ObjectId, MemoryMethods } from '@librechat/data-schemas';
+import type { BaseMessage } from '@langchain/core/messages';
+import type { Response as ServerResponse } from 'express';
+import { Tokenizer } from '~/utils';
+
+type RequiredMemoryMethods = Pick<
+ MemoryMethods,
+ 'setMemory' | 'deleteMemory' | 'getFormattedMemories'
+>;
+
+type ToolEndMetadata = Record<string, unknown> & {
+ run_id?: string;
+ thread_id?: string;
+};
+
+export interface MemoryConfig {
+ validKeys?: string[];
+ instructions?: string;
+ llmConfig?: Partial<LLMConfig>;
+ tokenLimit?: number;
+}
+
+export const memoryInstructions =
+ 'The system automatically stores important user information and can update or delete memories based on user requests, enabling dynamic memory management.';
+
+const getDefaultInstructions = (
+ validKeys?: string[],
+ tokenLimit?: number,
+) => `Use the \`set_memory\` tool to save important information about the user, but ONLY when the user has explicitly provided this information. If there is nothing to note about the user specifically, END THE TURN IMMEDIATELY.
+
+ The \`delete_memory\` tool should only be used in two scenarios:
+ 1. When the user explicitly asks to forget or remove specific information
+ 2. When updating existing memories, use the \`set_memory\` tool instead of deleting and re-adding the memory.
+
+ ${
+ validKeys && validKeys.length > 0
+ ? `CRITICAL INSTRUCTION: Only the following keys are valid for storing memories:
+ ${validKeys.map((key) => `- ${key}`).join('\n ')}`
+ : 'You can use any appropriate key to store memories about the user.'
+ }
+
+ ${
+ tokenLimit
+ ? `⚠️ TOKEN LIMIT: Each memory value must not exceed ${tokenLimit} tokens. Be concise and store only essential information.`
+ : ''
+ }
+
+ ⚠️ WARNING ⚠️
+ DO NOT STORE ANY INFORMATION UNLESS THE USER HAS EXPLICITLY PROVIDED IT.
+ ONLY store information the user has EXPLICITLY shared.
+ NEVER guess or assume user information.
+ ALL memory values must be factual statements about THIS specific user.
+ If nothing needs to be stored, DO NOT CALL any memory tools.
+ If you're unsure whether to store something, DO NOT store it.
+ If nothing needs to be stored, END THE TURN IMMEDIATELY.`;
+
+/**
+ * Creates a memory tool instance with user context
+ */
+const createMemoryTool = ({
+ userId,
+ setMemory,
+ validKeys,
+ tokenLimit,
+ totalTokens = 0,
+}: {
+ userId: string | ObjectId;
+ setMemory: MemoryMethods['setMemory'];
+ validKeys?: string[];
+ tokenLimit?: number;
+ totalTokens?: number;
+}) => {
+ return tool(
+ async ({ key, value }) => {
+ try {
+ if (validKeys && validKeys.length > 0 && !validKeys.includes(key)) {
+ logger.warn(
+ `Memory Agent failed to set memory: Invalid key "${key}". Must be one of: ${validKeys.join(
+ ', ',
+ )}`,
+ );
+ return `Invalid key "${key}". Must be one of: ${validKeys.join(', ')}`;
+ }
+
+ const tokenCount = Tokenizer.getTokenCount(value, 'o200k_base');
+
+ if (tokenLimit && tokenCount > tokenLimit) {
+ logger.warn(
+ `Memory Agent failed to set memory: Value exceeds token limit. Value has ${tokenCount} tokens, but limit is ${tokenLimit}`,
+ );
+ return `Memory value too large: ${tokenCount} tokens exceeds limit of ${tokenLimit}`;
+ }
+
+ if (tokenLimit && totalTokens + tokenCount > tokenLimit) {
+ const remainingCapacity = tokenLimit - totalTokens;
+ logger.warn(
+ `Memory Agent failed to set memory: Would exceed total token limit. Current usage: ${totalTokens}, new memory: ${tokenCount} tokens, limit: ${tokenLimit}`,
+ );
+ return `Cannot add memory: would exceed token limit. Current usage: ${totalTokens}/${tokenLimit} tokens. This memory requires ${tokenCount} tokens, but only ${remainingCapacity} tokens available.`;
+ }
+
+ const artifact: Record<Tools.memory, MemoryArtifact> = {
+ [Tools.memory]: {
+ key,
+ value,
+ tokenCount,
+ type: 'update',
+ },
+ };
+
+ const result = await setMemory({ userId, key, value, tokenCount });
+ if (result.ok) {
+ logger.debug(`Memory set for key "${key}" (${tokenCount} tokens) for user "${userId}"`);
+ return [`Memory set for key "${key}" (${tokenCount} tokens)`, artifact];
+ }
+ logger.warn(`Failed to set memory for key "${key}" for user "${userId}"`);
+ return [`Failed to set memory for key "${key}"`, undefined];
+ } catch (error) {
+ logger.error('Memory Agent failed to set memory', error);
+ return [`Error setting memory for key "${key}"`, undefined];
+ }
+ },
+ {
+ name: 'set_memory',
+ description: 'Saves important information about the user into memory.',
+ responseFormat: 'content_and_artifact',
+ schema: z.object({
+ key: z
+ .string()
+ .describe(
+ validKeys && validKeys.length > 0
+ ? `The key of the memory value. Must be one of: ${validKeys.join(', ')}`
+ : 'The key identifier for this memory',
+ ),
+ value: z
+ .string()
+ .describe(
+ 'Value MUST be a complete sentence that fully describes relevant user information.',
+ ),
+ }),
+ },
+ );
+};
+
+/**
+ * Creates a delete memory tool instance with user context
+ */
+const createDeleteMemoryTool = ({
+ userId,
+ deleteMemory,
+ validKeys,
+}: {
+ userId: string | ObjectId;
+ deleteMemory: MemoryMethods['deleteMemory'];
+ validKeys?: string[];
+}) => {
+ return tool(
+ async ({ key }) => {
+ try {
+ if (validKeys && validKeys.length > 0 && !validKeys.includes(key)) {
+ logger.warn(
+ `Memory Agent failed to delete memory: Invalid key "${key}". Must be one of: ${validKeys.join(
+ ', ',
+ )}`,
+ );
+ return `Invalid key "${key}". Must be one of: ${validKeys.join(', ')}`;
+ }
+
+ const artifact: Record<Tools.memory, MemoryArtifact> = {
+ [Tools.memory]: {
+ key,
+ type: 'delete',
+ },
+ };
+
+ const result = await deleteMemory({ userId, key });
+ if (result.ok) {
+ logger.debug(`Memory deleted for key "${key}" for user "${userId}"`);
+ return [`Memory deleted for key "${key}"`, artifact];
+ }
+ logger.warn(`Failed to delete memory for key "${key}" for user "${userId}"`);
+ return [`Failed to delete memory for key "${key}"`, undefined];
+ } catch (error) {
+ logger.error('Memory Agent failed to delete memory', error);
+ return [`Error deleting memory for key "${key}"`, undefined];
+ }
+ },
+ {
+ name: 'delete_memory',
+ description:
+ 'Deletes specific memory data about the user using the provided key. For updating existing memories, use the `set_memory` tool instead',
+ responseFormat: 'content_and_artifact',
+ schema: z.object({
+ key: z
+ .string()
+ .describe(
+ validKeys && validKeys.length > 0
+ ? `The key of the memory to delete. Must be one of: ${validKeys.join(', ')}`
+ : 'The key identifier of the memory to delete',
+ ),
+ }),
+ },
+ );
+};
+export class BasicToolEndHandler implements EventHandler {
+ private callback?: ToolEndCallback;
+ constructor(callback?: ToolEndCallback) {
+ this.callback = callback;
+ }
+ handle(
+ event: string,
+ data: StreamEventData | undefined,
+ metadata?: Record<string, unknown>,
+ ): void {
+ if (!metadata) {
+ console.warn(`Graph or metadata not found in ${event} event`);
+ return;
+ }
+ const toolEndData = data as ToolEndData | undefined;
+ if (!toolEndData?.output) {
+ console.warn('No output found in tool_end event');
+ return;
+ }
+ this.callback?.(toolEndData, metadata);
+ }
+}
+
+export async function processMemory({
+ res,
+ userId,
+ setMemory,
+ deleteMemory,
+ messages,
+ memory,
+ messageId,
+ conversationId,
+ validKeys,
+ instructions,
+ llmConfig,
+ tokenLimit,
+ totalTokens = 0,
+}: {
+ res: ServerResponse;
+ setMemory: MemoryMethods['setMemory'];
+ deleteMemory: MemoryMethods['deleteMemory'];
+ userId: string | ObjectId;
+ memory: string;
+ messageId: string;
+ conversationId: string;
+ messages: BaseMessage[];
+ validKeys?: string[];
+ instructions: string;
+ tokenLimit?: number;
+ totalTokens?: number;
+ llmConfig?: Partial<LLMConfig>;
+}): Promise<(TAttachment | null)[] | undefined> {
+ try {
+ const memoryTool = createMemoryTool({ userId, tokenLimit, setMemory, validKeys, totalTokens });
+ const deleteMemoryTool = createDeleteMemoryTool({
+ userId,
+ validKeys,
+ deleteMemory,
+ });
+
+ const currentMemoryTokens = totalTokens;
+
+ let memoryStatus = `# Existing memory:\n${memory ?? 'No existing memories'}`;
+
+ if (tokenLimit) {
+ const remainingTokens = tokenLimit - currentMemoryTokens;
+ memoryStatus = `# Memory Status:
+Current memory usage: ${currentMemoryTokens} tokens
+Token limit: ${tokenLimit} tokens
+Remaining capacity: ${remainingTokens} tokens
+
+# Existing memory:
+${memory ?? 'No existing memories'}`;
+ }
+
+ const defaultLLMConfig: LLMConfig = {
+ provider: Providers.OPENAI,
+ model: 'gpt-4.1-mini',
+ temperature: 0.4,
+ streaming: false,
+ disableStreaming: true,
+ };
+
+ const finalLLMConfig = {
+ ...defaultLLMConfig,
+ ...llmConfig,
+ /**
+ * Ensure streaming is always disabled for memory processing
+ */
+ streaming: false,
+ disableStreaming: true,
+ };
+
+ const artifactPromises: Promise<TAttachment | null>[] = [];
+ const memoryCallback = createMemoryCallback({ res, artifactPromises });
+ const customHandlers = {
+ [GraphEvents.TOOL_END]: new BasicToolEndHandler(memoryCallback),
+ };
+
+ const run = await Run.create({
+ runId: messageId,
+ graphConfig: {
+ type: 'standard',
+ llmConfig: finalLLMConfig,
+ tools: [memoryTool, deleteMemoryTool],
+ instructions,
+ additional_instructions: memoryStatus,
+ toolEnd: true,
+ },
+ customHandlers,
+ returnContent: true,
+ });
+
+ const config = {
+ configurable: {
+ provider: llmConfig?.provider,
+ thread_id: `memory-run-${conversationId}`,
+ },
+ streamMode: 'values',
+ version: 'v2',
+ } as const;
+
+ const inputs = {
+ messages,
+ };
+ const content = await run.processStream(inputs, config);
+ if (content) {
+ logger.debug('Memory Agent processed memory successfully', content);
+ } else {
+ logger.warn('Memory Agent processed memory but returned no content');
+ }
+ return await Promise.all(artifactPromises);
+ } catch (error) {
+ logger.error('Memory Agent failed to process memory', error);
+ }
+}
+
+export async function createMemoryProcessor({
+ res,
+ userId,
+ messageId,
+ memoryMethods,
+ conversationId,
+ config = {},
+}: {
+ res: ServerResponse;
+ messageId: string;
+ conversationId: string;
+ userId: string | ObjectId;
+ memoryMethods: RequiredMemoryMethods;
+ config?: MemoryConfig;
+}): Promise<[string, (messages: BaseMessage[]) => Promise<(TAttachment | null)[] | undefined>]> {
+ const { validKeys, instructions, llmConfig, tokenLimit } = config;
+ const finalInstructions = instructions || getDefaultInstructions(validKeys, tokenLimit);
+
+ const { withKeys, withoutKeys, totalTokens } = await memoryMethods.getFormattedMemories({
+ userId,
+ });
+
+ return [
+ withoutKeys,
+ async function (messages: BaseMessage[]): Promise<(TAttachment | null)[] | undefined> {
+ try {
+ return await processMemory({
+ res,
+ userId,
+ messages,
+ validKeys,
+ llmConfig,
+ messageId,
+ tokenLimit,
+ conversationId,
+ memory: withKeys,
+ totalTokens: totalTokens || 0,
+ instructions: finalInstructions,
+ setMemory: memoryMethods.setMemory,
+ deleteMemory: memoryMethods.deleteMemory,
+ });
+ } catch (error) {
+ logger.error('Memory Agent failed to process memory', error);
+ }
+ },
+ ];
+}
+
+async function handleMemoryArtifact({
+ res,
+ data,
+ metadata,
+}: {
+ res: ServerResponse;
+ data: ToolEndData;
+ metadata?: ToolEndMetadata;
+}) {
+ const output = data?.output;
+ if (!output) {
+ return null;
+ }
+
+ if (!output.artifact) {
+ return null;
+ }
+
+ const memoryArtifact = output.artifact[Tools.memory] as MemoryArtifact | undefined;
+ if (!memoryArtifact) {
+ return null;
+ }
+
+ const attachment: Partial<TAttachment> = {
+ type: Tools.memory,
+ toolCallId: output.tool_call_id,
+ messageId: metadata?.run_id ?? '',
+ conversationId: metadata?.thread_id ?? '',
+ [Tools.memory]: memoryArtifact,
+ };
+ if (!res.headersSent) {
+ return attachment;
+ }
+ res.write(`event: attachment\ndata: ${JSON.stringify(attachment)}\n\n`);
+ return attachment;
+}
+
+/**
+ * Creates a memory callback for handling memory artifacts
+ * @param params - The parameters object
+ * @param params.res - The server response object
+ * @param params.artifactPromises - Array to collect artifact promises
+ * @returns The memory callback function
+ */
+export function createMemoryCallback({
+ res,
+ artifactPromises,
+}: {
+ res: ServerResponse;
+ artifactPromises: Promise<Partial<TAttachment> | null>[];
+}): ToolEndCallback {
+ return async (data: ToolEndData, metadata?: Record<string, unknown>) => {
+ const output = data?.output;
+ const memoryArtifact = output?.artifact?.[Tools.memory] as MemoryArtifact;
+ if (memoryArtifact == null) {
+ return;
+ }
+ artifactPromises.push(
+ handleMemoryArtifact({ res, data, metadata }).catch((error) => {
+ logger.error('Error processing memory artifact content:', error);
+ return null;
+ }),
+ );
+ };
+}
diff --git a/packages/api/src/agents/resources.test.ts b/packages/api/src/agents/resources.test.ts
new file mode 100644
index 0000000000..8789856ab7
--- /dev/null
+++ b/packages/api/src/agents/resources.test.ts
@@ -0,0 +1,543 @@
+import { primeResources } from './resources';
+import { logger } from '@librechat/data-schemas';
+import { EModelEndpoint, EToolResources, AgentCapabilities } from 'librechat-data-provider';
+import type { Request as ServerRequest } from 'express';
+import type { TFile } from 'librechat-data-provider';
+import type { TGetFiles } from './resources';
+
+// Mock logger
+jest.mock('@librechat/data-schemas', () => ({
+ logger: {
+ error: jest.fn(),
+ },
+}));
+
+describe('primeResources', () => {
+ let mockReq: ServerRequest;
+ let mockGetFiles: jest.MockedFunction<TGetFiles>;
+ let requestFileSet: Set<string>;
+
+ beforeEach(() => {
+ // Reset mocks
+ jest.clearAllMocks();
+
+ // Setup mock request
+ mockReq = {
+ app: {
+ locals: {
+ [EModelEndpoint.agents]: {
+ capabilities: [AgentCapabilities.ocr],
+ },
+ },
+ },
+ } as unknown as ServerRequest;
+
+ // Setup mock getFiles function
+ mockGetFiles = jest.fn();
+
+ // Setup request file set
+ requestFileSet = new Set(['file1', 'file2', 'file3']);
+ });
+
+ describe('when OCR is enabled and tool_resources has OCR file_ids', () => {
+ it('should fetch OCR files and include them in attachments', async () => {
+ const mockOcrFiles: TFile[] = [
+ {
+ user: 'user1',
+ file_id: 'ocr-file-1',
+ filename: 'document.pdf',
+ filepath: '/uploads/document.pdf',
+ object: 'file',
+ type: 'application/pdf',
+ bytes: 1024,
+ embedded: false,
+ usage: 0,
+ },
+ ];
+
+ mockGetFiles.mockResolvedValue(mockOcrFiles);
+
+ const tool_resources = {
+ [EToolResources.ocr]: {
+ file_ids: ['ocr-file-1'],
+ },
+ };
+
+ const result = await primeResources({
+ req: mockReq,
+ getFiles: mockGetFiles,
+ requestFileSet,
+ attachments: undefined,
+ tool_resources,
+ });
+
+ expect(mockGetFiles).toHaveBeenCalledWith({ file_id: { $in: ['ocr-file-1'] } }, {}, {});
+ expect(result.attachments).toEqual(mockOcrFiles);
+ expect(result.tool_resources).toEqual(tool_resources);
+ });
+ });
+
+ describe('when OCR is disabled', () => {
+ it('should not fetch OCR files even if tool_resources has OCR file_ids', async () => {
+ (mockReq.app as ServerRequest['app']).locals[EModelEndpoint.agents].capabilities = [];
+
+ const tool_resources = {
+ [EToolResources.ocr]: {
+ file_ids: ['ocr-file-1'],
+ },
+ };
+
+ const result = await primeResources({
+ req: mockReq,
+ getFiles: mockGetFiles,
+ requestFileSet,
+ attachments: undefined,
+ tool_resources,
+ });
+
+ expect(mockGetFiles).not.toHaveBeenCalled();
+ expect(result.attachments).toBeUndefined();
+ expect(result.tool_resources).toEqual(tool_resources);
+ });
+ });
+
+ describe('when attachments are provided', () => {
+ it('should process files with fileIdentifier as execute_code resources', async () => {
+ const mockFiles: TFile[] = [
+ {
+ user: 'user1',
+ file_id: 'file1',
+ filename: 'script.py',
+ filepath: '/uploads/script.py',
+ object: 'file',
+ type: 'text/x-python',
+ bytes: 512,
+ embedded: false,
+ usage: 0,
+ metadata: {
+ fileIdentifier: 'python-script',
+ },
+ },
+ ];
+
+ const attachments = Promise.resolve(mockFiles);
+
+ const result = await primeResources({
+ req: mockReq,
+ getFiles: mockGetFiles,
+ requestFileSet,
+ attachments,
+ tool_resources: {},
+ });
+
+ expect(result.attachments).toEqual(mockFiles);
+ expect(result.tool_resources?.[EToolResources.execute_code]?.files).toEqual(mockFiles);
+ });
+
+ it('should process embedded files as file_search resources', async () => {
+ const mockFiles: TFile[] = [
+ {
+ user: 'user1',
+ file_id: 'file2',
+ filename: 'document.txt',
+ filepath: '/uploads/document.txt',
+ object: 'file',
+ type: 'text/plain',
+ bytes: 256,
+ embedded: true,
+ usage: 0,
+ },
+ ];
+
+ const attachments = Promise.resolve(mockFiles);
+
+ const result = await primeResources({
+ req: mockReq,
+ getFiles: mockGetFiles,
+ requestFileSet,
+ attachments,
+ tool_resources: {},
+ });
+
+ expect(result.attachments).toEqual(mockFiles);
+ expect(result.tool_resources?.[EToolResources.file_search]?.files).toEqual(mockFiles);
+ });
+
+ it('should process image files in requestFileSet as image_edit resources', async () => {
+ const mockFiles: TFile[] = [
+ {
+ user: 'user1',
+ file_id: 'file1',
+ filename: 'image.png',
+ filepath: '/uploads/image.png',
+ object: 'file',
+ type: 'image/png',
+ bytes: 2048,
+ embedded: false,
+ usage: 0,
+ height: 800,
+ width: 600,
+ },
+ ];
+
+ const attachments = Promise.resolve(mockFiles);
+
+ const result = await primeResources({
+ req: mockReq,
+ getFiles: mockGetFiles,
+ requestFileSet,
+ attachments,
+ tool_resources: {},
+ });
+
+ expect(result.attachments).toEqual(mockFiles);
+ expect(result.tool_resources?.[EToolResources.image_edit]?.files).toEqual(mockFiles);
+ });
+
+ it('should not process image files not in requestFileSet', async () => {
+ const mockFiles: TFile[] = [
+ {
+ user: 'user1',
+ file_id: 'file-not-in-set',
+ filename: 'image.png',
+ filepath: '/uploads/image.png',
+ object: 'file',
+ type: 'image/png',
+ bytes: 2048,
+ embedded: false,
+ usage: 0,
+ height: 800,
+ width: 600,
+ },
+ ];
+
+ const attachments = Promise.resolve(mockFiles);
+
+ const result = await primeResources({
+ req: mockReq,
+ getFiles: mockGetFiles,
+ requestFileSet,
+ attachments,
+ tool_resources: {},
+ });
+
+ expect(result.attachments).toEqual(mockFiles);
+ expect(result.tool_resources?.[EToolResources.image_edit]).toBeUndefined();
+ });
+
+ it('should not process image files without height and width', async () => {
+ const mockFiles: TFile[] = [
+ {
+ user: 'user1',
+ file_id: 'file1',
+ filename: 'image.png',
+ filepath: '/uploads/image.png',
+ object: 'file',
+ type: 'image/png',
+ bytes: 2048,
+ embedded: false,
+ usage: 0,
+ // Missing height and width
+ },
+ ];
+
+ const attachments = Promise.resolve(mockFiles);
+
+ const result = await primeResources({
+ req: mockReq,
+ getFiles: mockGetFiles,
+ requestFileSet,
+ attachments,
+ tool_resources: {},
+ });
+
+ expect(result.attachments).toEqual(mockFiles);
+ expect(result.tool_resources?.[EToolResources.image_edit]).toBeUndefined();
+ });
+
+ it('should filter out null files from attachments', async () => {
+ const mockFiles: Array<TFile | null> = [
+ {
+ user: 'user1',
+ file_id: 'file1',
+ filename: 'valid.txt',
+ filepath: '/uploads/valid.txt',
+ object: 'file',
+ type: 'text/plain',
+ bytes: 256,
+ embedded: false,
+ usage: 0,
+ },
+ null,
+ {
+ user: 'user1',
+ file_id: 'file2',
+ filename: 'valid2.txt',
+ filepath: '/uploads/valid2.txt',
+ object: 'file',
+ type: 'text/plain',
+ bytes: 128,
+ embedded: false,
+ usage: 0,
+ },
+ ];
+
+ const attachments = Promise.resolve(mockFiles);
+
+ const result = await primeResources({
+ req: mockReq,
+ getFiles: mockGetFiles,
+ requestFileSet,
+ attachments,
+ tool_resources: {},
+ });
+
+ expect(result.attachments).toHaveLength(2);
+ expect(result.attachments?.[0]?.file_id).toBe('file1');
+ expect(result.attachments?.[1]?.file_id).toBe('file2');
+ });
+
+ it('should merge existing tool_resources with new files', async () => {
+ const mockFiles: TFile[] = [
+ {
+ user: 'user1',
+ file_id: 'file1',
+ filename: 'script.py',
+ filepath: '/uploads/script.py',
+ object: 'file',
+ type: 'text/x-python',
+ bytes: 512,
+ embedded: false,
+ usage: 0,
+ metadata: {
+ fileIdentifier: 'python-script',
+ },
+ },
+ ];
+
+ const existingToolResources = {
+ [EToolResources.execute_code]: {
+ files: [
+ {
+ user: 'user1',
+ file_id: 'existing-file',
+ filename: 'existing.py',
+ filepath: '/uploads/existing.py',
+ object: 'file' as const,
+ type: 'text/x-python',
+ bytes: 256,
+ embedded: false,
+ usage: 0,
+ },
+ ],
+ },
+ };
+
+ const attachments = Promise.resolve(mockFiles);
+
+ const result = await primeResources({
+ req: mockReq,
+ getFiles: mockGetFiles,
+ requestFileSet,
+ attachments,
+ tool_resources: existingToolResources,
+ });
+
+ expect(result.tool_resources?.[EToolResources.execute_code]?.files).toHaveLength(2);
+ expect(result.tool_resources?.[EToolResources.execute_code]?.files?.[0]?.file_id).toBe(
+ 'existing-file',
+ );
+ expect(result.tool_resources?.[EToolResources.execute_code]?.files?.[1]?.file_id).toBe(
+ 'file1',
+ );
+ });
+ });
+
+ describe('when both OCR and attachments are provided', () => {
+ it('should include both OCR files and attachment files', async () => {
+ const mockOcrFiles: TFile[] = [
+ {
+ user: 'user1',
+ file_id: 'ocr-file-1',
+ filename: 'document.pdf',
+ filepath: '/uploads/document.pdf',
+ object: 'file',
+ type: 'application/pdf',
+ bytes: 1024,
+ embedded: false,
+ usage: 0,
+ },
+ ];
+
+ const mockAttachmentFiles: TFile[] = [
+ {
+ user: 'user1',
+ file_id: 'file1',
+ filename: 'attachment.txt',
+ filepath: '/uploads/attachment.txt',
+ object: 'file',
+ type: 'text/plain',
+ bytes: 256,
+ embedded: false,
+ usage: 0,
+ },
+ ];
+
+ mockGetFiles.mockResolvedValue(mockOcrFiles);
+ const attachments = Promise.resolve(mockAttachmentFiles);
+
+ const tool_resources = {
+ [EToolResources.ocr]: {
+ file_ids: ['ocr-file-1'],
+ },
+ };
+
+ const result = await primeResources({
+ req: mockReq,
+ getFiles: mockGetFiles,
+ requestFileSet,
+ attachments,
+ tool_resources,
+ });
+
+ expect(result.attachments).toHaveLength(2);
+ expect(result.attachments?.[0]?.file_id).toBe('ocr-file-1');
+ expect(result.attachments?.[1]?.file_id).toBe('file1');
+ });
+ });
+
+ describe('error handling', () => {
+ it('should handle errors gracefully and log them', async () => {
+ const mockFiles: TFile[] = [
+ {
+ user: 'user1',
+ file_id: 'file1',
+ filename: 'test.txt',
+ filepath: '/uploads/test.txt',
+ object: 'file',
+ type: 'text/plain',
+ bytes: 256,
+ embedded: false,
+ usage: 0,
+ },
+ ];
+
+ const attachments = Promise.resolve(mockFiles);
+ const error = new Error('Test error');
+
+ // Mock getFiles to throw an error when called for OCR
+ mockGetFiles.mockRejectedValue(error);
+
+ const tool_resources = {
+ [EToolResources.ocr]: {
+ file_ids: ['ocr-file-1'],
+ },
+ };
+
+ const result = await primeResources({
+ req: mockReq,
+ getFiles: mockGetFiles,
+ requestFileSet,
+ attachments,
+ tool_resources,
+ });
+
+ expect(logger.error).toHaveBeenCalledWith('Error priming resources', error);
+ expect(result.attachments).toEqual(mockFiles);
+ expect(result.tool_resources).toEqual(tool_resources);
+ });
+
+ it('should handle promise rejection in attachments', async () => {
+ const error = new Error('Attachment error');
+ const attachments = Promise.reject(error);
+
+ // The function should now handle rejected attachment promises gracefully
+ const result = await primeResources({
+ req: mockReq,
+ getFiles: mockGetFiles,
+ requestFileSet,
+ attachments,
+ tool_resources: {},
+ });
+
+ // Should log both the main error and the attachment error
+ expect(logger.error).toHaveBeenCalledWith('Error priming resources', error);
+ expect(logger.error).toHaveBeenCalledWith(
+ 'Error resolving attachments in catch block',
+ error,
+ );
+
+ // Should return empty array when attachments promise is rejected
+ expect(result.attachments).toEqual([]);
+ expect(result.tool_resources).toEqual({});
+ });
+ });
+
+ describe('edge cases', () => {
+ it('should handle missing app.locals gracefully', async () => {
+ const reqWithoutLocals = {} as ServerRequest;
+
+ const result = await primeResources({
+ req: reqWithoutLocals,
+ getFiles: mockGetFiles,
+ requestFileSet,
+ attachments: undefined,
+ tool_resources: {
+ [EToolResources.ocr]: {
+ file_ids: ['ocr-file-1'],
+ },
+ },
+ });
+
+ expect(mockGetFiles).not.toHaveBeenCalled();
+ // When app.locals is missing and there's an error accessing properties,
+ // the function falls back to the catch block which returns an empty array
+ expect(result.attachments).toEqual([]);
+ });
+
+ it('should handle undefined tool_resources', async () => {
+ const result = await primeResources({
+ req: mockReq,
+ getFiles: mockGetFiles,
+ requestFileSet,
+ attachments: undefined,
+ tool_resources: undefined,
+ });
+
+ expect(result.tool_resources).toEqual({});
+ expect(result.attachments).toBeUndefined();
+ });
+
+ it('should handle empty requestFileSet', async () => {
+ const mockFiles: TFile[] = [
+ {
+ user: 'user1',
+ file_id: 'file1',
+ filename: 'image.png',
+ filepath: '/uploads/image.png',
+ object: 'file',
+ type: 'image/png',
+ bytes: 2048,
+ embedded: false,
+ usage: 0,
+ height: 800,
+ width: 600,
+ },
+ ];
+
+ const attachments = Promise.resolve(mockFiles);
+ const emptyRequestFileSet = new Set<string>();
+
+ const result = await primeResources({
+ req: mockReq,
+ getFiles: mockGetFiles,
+ requestFileSet: emptyRequestFileSet,
+ attachments,
+ tool_resources: {},
+ });
+
+ expect(result.attachments).toEqual(mockFiles);
+ expect(result.tool_resources?.[EToolResources.image_edit]).toBeUndefined();
+ });
+ });
+});
diff --git a/packages/api/src/agents/resources.ts b/packages/api/src/agents/resources.ts
new file mode 100644
index 0000000000..644813bdfa
--- /dev/null
+++ b/packages/api/src/agents/resources.ts
@@ -0,0 +1,114 @@
+import { logger } from '@librechat/data-schemas';
+import { EModelEndpoint, EToolResources, AgentCapabilities } from 'librechat-data-provider';
+import type { FilterQuery, QueryOptions, ProjectionType } from 'mongoose';
+import type { AgentToolResources, TFile } from 'librechat-data-provider';
+import type { IMongoFile } from '@librechat/data-schemas';
+import type { Request as ServerRequest } from 'express';
+
+export type TGetFiles = (
+ filter: FilterQuery<IMongoFile>,
+ _sortOptions: ProjectionType<IMongoFile> | null | undefined,
+ selectFields: QueryOptions<IMongoFile> | null | undefined,
+) => Promise<Array<TFile>>;
+
+/**
+ * @param params
+ * @param params.req
+ * @param params.attachments
+ * @param params.requestFileSet
+ * @param params.tool_resources
+ */
+export const primeResources = async ({
+ req,
+ getFiles,
+ requestFileSet,
+ attachments: _attachments,
+ tool_resources: _tool_resources,
+}: {
+ req: ServerRequest;
+ requestFileSet: Set<string>;
+ attachments: Promise<Array<TFile | null>> | undefined;
+ tool_resources: AgentToolResources | undefined;
+ getFiles: TGetFiles;
+}): Promise<{
+ attachments: Array<TFile | undefined> | undefined;
+ tool_resources: AgentToolResources | undefined;
+}> => {
+ try {
+ let attachments: Array<TFile | undefined> | undefined;
+ const tool_resources = _tool_resources ?? {};
+ const isOCREnabled = (req.app.locals?.[EModelEndpoint.agents]?.capabilities ?? []).includes(
+ AgentCapabilities.ocr,
+ );
+ if (tool_resources[EToolResources.ocr]?.file_ids && isOCREnabled) {
+ const context = await getFiles(
+ {
+ file_id: { $in: tool_resources.ocr.file_ids },
+ },
+ {},
+ {},
+ );
+ attachments = (attachments ?? []).concat(context);
+ }
+ if (!_attachments) {
+ return { attachments, tool_resources };
+ }
+ const files = await _attachments;
+ if (!attachments) {
+ attachments = [];
+ }
+
+ for (const file of files) {
+ if (!file) {
+ continue;
+ }
+ if (file.metadata?.fileIdentifier) {
+ const execute_code = tool_resources[EToolResources.execute_code] ?? {};
+ if (!execute_code.files) {
+ tool_resources[EToolResources.execute_code] = { ...execute_code, files: [] };
+ }
+ tool_resources[EToolResources.execute_code]?.files?.push(file);
+ } else if (file.embedded === true) {
+ const file_search = tool_resources[EToolResources.file_search] ?? {};
+ if (!file_search.files) {
+ tool_resources[EToolResources.file_search] = { ...file_search, files: [] };
+ }
+ tool_resources[EToolResources.file_search]?.files?.push(file);
+ } else if (
+ requestFileSet.has(file.file_id) &&
+ file.type.startsWith('image') &&
+ file.height &&
+ file.width
+ ) {
+ const image_edit = tool_resources[EToolResources.image_edit] ?? {};
+ if (!image_edit.files) {
+ tool_resources[EToolResources.image_edit] = { ...image_edit, files: [] };
+ }
+ tool_resources[EToolResources.image_edit]?.files?.push(file);
+ }
+
+ attachments.push(file);
+ }
+ return { attachments, tool_resources };
+ } catch (error) {
+ logger.error('Error priming resources', error);
+
+ // Safely try to get attachments without rethrowing
+ let safeAttachments: Array<TFile | undefined> = [];
+ if (_attachments) {
+ try {
+ const attachmentFiles = await _attachments;
+ safeAttachments = (attachmentFiles?.filter((file) => !!file) ?? []) as Array<TFile>;
+ } catch (attachmentError) {
+ // If attachments promise is also rejected, just use empty array
+ logger.error('Error resolving attachments in catch block', attachmentError);
+ safeAttachments = [];
+ }
+ }
+
+ return {
+ attachments: safeAttachments,
+ tool_resources: _tool_resources,
+ };
+ }
+};
diff --git a/packages/api/src/agents/run.ts b/packages/api/src/agents/run.ts
new file mode 100644
index 0000000000..22f8c4ae93
--- /dev/null
+++ b/packages/api/src/agents/run.ts
@@ -0,0 +1,90 @@
+import { Run, Providers } from '@librechat/agents';
+import { providerEndpointMap, KnownEndpoints } from 'librechat-data-provider';
+import type { StandardGraphConfig, EventHandler, GraphEvents, IState } from '@librechat/agents';
+import type { Agent } from 'librechat-data-provider';
+import type * as t from '~/types';
+
+const customProviders = new Set([
+ Providers.XAI,
+ Providers.OLLAMA,
+ Providers.DEEPSEEK,
+ Providers.OPENROUTER,
+]);
+
+/**
+ * Creates a new Run instance with custom handlers and configuration.
+ *
+ * @param options - The options for creating the Run instance.
+ * @param options.agent - The agent for this run.
+ * @param options.signal - The signal for this run.
+ * @param options.req - The server request.
+ * @param options.runId - Optional run ID; otherwise, a new run ID will be generated.
+ * @param options.customHandlers - Custom event handlers.
+ * @param options.streaming - Whether to use streaming.
+ * @param options.streamUsage - Whether to stream usage information.
+ * @returns {Promise<Run<IState>>} A promise that resolves to a new Run instance.
+ */
+export async function createRun({
+ runId,
+ agent,
+ signal,
+ customHandlers,
+ streaming = true,
+ streamUsage = true,
+}: {
+ agent: Agent;
+ signal: AbortSignal;
+ runId?: string;
+ streaming?: boolean;
+ streamUsage?: boolean;
+ customHandlers?: Record<GraphEvents, EventHandler>;
+}): Promise<Run<IState>> {
+ const provider =
+ providerEndpointMap[agent.provider as keyof typeof providerEndpointMap] ?? agent.provider;
+ const llmConfig: t.RunLLMConfig = Object.assign(
+ {
+ provider,
+ streaming,
+ streamUsage,
+ },
+ agent.model_parameters,
+ );
+
+ /** Resolves issues with new OpenAI usage field */
+ if (
+ customProviders.has(agent.provider) ||
+ (agent.provider === Providers.OPENAI && agent.endpoint !== agent.provider)
+ ) {
+ llmConfig.streamUsage = false;
+ llmConfig.usage = true;
+ }
+
+ let reasoningKey: 'reasoning_content' | 'reasoning' | undefined;
+ if (
+ llmConfig.configuration?.baseURL?.includes(KnownEndpoints.openrouter) ||
+ (agent.endpoint && agent.endpoint.toLowerCase().includes(KnownEndpoints.openrouter))
+ ) {
+ reasoningKey = 'reasoning';
+ }
+
+ const graphConfig: StandardGraphConfig = {
+ signal,
+ llmConfig,
+ reasoningKey,
+ tools: agent.tools,
+ instructions: agent.instructions,
+ additional_instructions: agent.additional_instructions,
+ // toolEnd: agent.end_after_tools,
+ };
+
+ // TEMPORARY FOR TESTING
+ if (agent.provider === Providers.ANTHROPIC || agent.provider === Providers.BEDROCK) {
+ graphConfig.streamBuffer = 2000;
+ }
+
+ return Run.create({
+ runId,
+ graphConfig,
+ customHandlers,
+ });
+}
diff --git a/packages/api/src/endpoints/index.ts b/packages/api/src/endpoints/index.ts
new file mode 100644
index 0000000000..e919f9e429
--- /dev/null
+++ b/packages/api/src/endpoints/index.ts
@@ -0,0 +1 @@
+export * from './openai';
diff --git a/packages/api/src/endpoints/openai/index.ts b/packages/api/src/endpoints/openai/index.ts
new file mode 100644
index 0000000000..46ec106869
--- /dev/null
+++ b/packages/api/src/endpoints/openai/index.ts
@@ -0,0 +1,2 @@
+export * from './llm';
+export * from './initialize';
diff --git a/packages/api/src/endpoints/openai/initialize.ts b/packages/api/src/endpoints/openai/initialize.ts
new file mode 100644
index 0000000000..d921af781d
--- /dev/null
+++ b/packages/api/src/endpoints/openai/initialize.ts
@@ -0,0 +1,176 @@
+import {
+ ErrorTypes,
+ EModelEndpoint,
+ resolveHeaders,
+ mapModelToAzureConfig,
+} from 'librechat-data-provider';
+import type {
+ LLMConfigOptions,
+ UserKeyValues,
+ InitializeOpenAIOptionsParams,
+ OpenAIOptionsResult,
+} from '~/types';
+import { createHandleLLMNewToken } from '~/utils/generators';
+import { getAzureCredentials } from '~/utils/azure';
+import { isUserProvided } from '~/utils/common';
+import { getOpenAIConfig } from './llm';
+
+/**
+ * Initializes OpenAI options for agent usage. This function always returns configuration
+ * options and never creates a client instance (equivalent to optionsOnly=true behavior).
+ *
+ * @param params - Configuration parameters
+ * @returns Promise resolving to OpenAI configuration options
+ * @throws Error if API key is missing or user key has expired
+ */
+export const initializeOpenAI = async ({
+ req,
+ overrideModel,
+ endpointOption,
+ overrideEndpoint,
+ getUserKeyValues,
+ checkUserKeyExpiry,
+}: InitializeOpenAIOptionsParams): Promise<OpenAIOptionsResult> => {
+ const { PROXY, OPENAI_API_KEY, AZURE_API_KEY, OPENAI_REVERSE_PROXY, AZURE_OPENAI_BASEURL } =
+ process.env;
+
+ const { key: expiresAt } = req.body;
+ const modelName = overrideModel ?? req.body.model;
+ const endpoint = overrideEndpoint ?? req.body.endpoint;
+
+ if (!endpoint) {
+ throw new Error('Endpoint is required');
+ }
+
+ const credentials = {
+ [EModelEndpoint.openAI]: OPENAI_API_KEY,
+ [EModelEndpoint.azureOpenAI]: AZURE_API_KEY,
+ };
+
+ const baseURLOptions = {
+ [EModelEndpoint.openAI]: OPENAI_REVERSE_PROXY,
+ [EModelEndpoint.azureOpenAI]: AZURE_OPENAI_BASEURL,
+ };
+
+ const userProvidesKey = isUserProvided(credentials[endpoint as keyof typeof credentials]);
+ const userProvidesURL = isUserProvided(baseURLOptions[endpoint as keyof typeof baseURLOptions]);
+
+ let userValues: UserKeyValues | null = null;
+ if (expiresAt && (userProvidesKey || userProvidesURL)) {
+ checkUserKeyExpiry(expiresAt, endpoint);
+ userValues = await getUserKeyValues({ userId: req.user.id, name: endpoint });
+ }
+
+ let apiKey = userProvidesKey
+ ? userValues?.apiKey
+ : credentials[endpoint as keyof typeof credentials];
+ const baseURL = userProvidesURL
+ ? userValues?.baseURL
+ : baseURLOptions[endpoint as keyof typeof baseURLOptions];
+
+ const clientOptions: LLMConfigOptions = {
+ proxy: PROXY ?? undefined,
+ reverseProxyUrl: baseURL || undefined,
+ streaming: true,
+ };
+
+ const isAzureOpenAI = endpoint === EModelEndpoint.azureOpenAI;
+ const azureConfig = isAzureOpenAI && req.app.locals[EModelEndpoint.azureOpenAI];
+
+ if (isAzureOpenAI && azureConfig) {
+ const { modelGroupMap, groupMap } = azureConfig;
+ const {
+ azureOptions,
+ baseURL: configBaseURL,
+ headers = {},
+ serverless,
+ } = mapModelToAzureConfig({
+ modelName: modelName || '',
+ modelGroupMap,
+ groupMap,
+ });
+
+ clientOptions.reverseProxyUrl = configBaseURL ?? clientOptions.reverseProxyUrl;
+ clientOptions.headers = resolveHeaders({ ...headers, ...(clientOptions.headers ?? {}) });
+
+ const groupName = modelGroupMap[modelName || '']?.group;
+ if (groupName && groupMap[groupName]) {
+ clientOptions.addParams = groupMap[groupName]?.addParams;
+ clientOptions.dropParams = groupMap[groupName]?.dropParams;
+ }
+
+ apiKey = azureOptions.azureOpenAIApiKey;
+ clientOptions.azure = !serverless ? azureOptions : undefined;
+
+ if (serverless === true) {
+ clientOptions.defaultQuery = azureOptions.azureOpenAIApiVersion
+ ? { 'api-version': azureOptions.azureOpenAIApiVersion }
+ : undefined;
+
+ if (!clientOptions.headers) {
+ clientOptions.headers = {};
+ }
+ clientOptions.headers['api-key'] = apiKey;
+ }
+ } else if (isAzureOpenAI) {
+ clientOptions.azure =
+ userProvidesKey && userValues?.apiKey ? JSON.parse(userValues.apiKey) : getAzureCredentials();
+ apiKey = clientOptions.azure?.azureOpenAIApiKey;
+ }
+
+ if (userProvidesKey && !apiKey) {
+ throw new Error(
+ JSON.stringify({
+ type: ErrorTypes.NO_USER_KEY,
+ }),
+ );
+ }
+
+ if (!apiKey) {
+ throw new Error(`${endpoint} API Key not provided.`);
+ }
+
+ const modelOptions = {
+ ...endpointOption.model_parameters,
+ model: modelName,
+ user: req.user.id,
+ };
+
+ const finalClientOptions: LLMConfigOptions = {
+ ...clientOptions,
+ modelOptions,
+ };
+
+ const options = getOpenAIConfig(apiKey, finalClientOptions, endpoint);
+
+ const openAIConfig = req.app.locals[EModelEndpoint.openAI];
+ const allConfig = req.app.locals.all;
+ const azureRate = modelName?.includes('gpt-4') ? 30 : 17;
+
+ let streamRate: number | undefined;
+
+ if (isAzureOpenAI && azureConfig) {
+ streamRate = azureConfig.streamRate ?? azureRate;
+ } else if (!isAzureOpenAI && openAIConfig) {
+ streamRate = openAIConfig.streamRate;
+ }
+
+ if (allConfig?.streamRate) {
+ streamRate = allConfig.streamRate;
+ }
+
+ if (streamRate) {
+ options.llmConfig.callbacks = [
+ {
+ handleLLMNewToken: createHandleLLMNewToken(streamRate),
+ },
+ ];
+ }
+
+ const result: OpenAIOptionsResult = {
+ ...options,
+ streamRate,
+ };
+
+ return result;
+};
diff --git a/packages/api/src/endpoints/openai/llm.ts b/packages/api/src/endpoints/openai/llm.ts
new file mode 100644
index 0000000000..2f9a3f6011
--- /dev/null
+++ b/packages/api/src/endpoints/openai/llm.ts
@@ -0,0 +1,156 @@
+import { HttpsProxyAgent } from 'https-proxy-agent';
+import { KnownEndpoints } from 'librechat-data-provider';
+import type * as t from '~/types';
+import { sanitizeModelName, constructAzureURL } from '~/utils/azure';
+import { isEnabled } from '~/utils/common';
+
+/**
+ * Generates configuration options for creating a language model (LLM) instance.
+ * @param apiKey - The API key for authentication.
+ * @param options - Additional options for configuring the LLM.
+ * @param endpoint - The endpoint name
+ * @returns Configuration options for creating an LLM instance.
+ */
+export function getOpenAIConfig(
+ apiKey: string,
+ options: t.LLMConfigOptions = {},
+ endpoint?: string | null,
+): t.LLMConfigResult {
+ const {
+ modelOptions = {},
+ reverseProxyUrl,
+ defaultQuery,
+ headers,
+ proxy,
+ azure,
+ streaming = true,
+ addParams,
+ dropParams,
+ } = options;
+
+ const llmConfig: Partial<t.ClientOptions> & Partial<t.OpenAIParameters> = Object.assign(
+ {
+ streaming,
+ model: modelOptions.model ?? '',
+ },
+ modelOptions,
+ );
+
+ if (addParams && typeof addParams === 'object') {
+ Object.assign(llmConfig, addParams);
+ }
+
+ // Note: OpenAI Web Search models do not support any known parameters besides `max_tokens`
+ if (modelOptions.model && /gpt-4o.*search/.test(modelOptions.model)) {
+ const searchExcludeParams = [
+ 'frequency_penalty',
+ 'presence_penalty',
+ 'temperature',
+ 'top_p',
+ 'top_k',
+ 'stop',
+ 'logit_bias',
+ 'seed',
+ 'response_format',
+ 'n',
+ 'logprobs',
+ 'user',
+ ];
+
+ const updatedDropParams = dropParams || [];
+ const combinedDropParams = [...new Set([...updatedDropParams, ...searchExcludeParams])];
+
+ combinedDropParams.forEach((param) => {
+ if (param in llmConfig) {
+ delete llmConfig[param as keyof t.ClientOptions];
+ }
+ });
+ } else if (dropParams && Array.isArray(dropParams)) {
+ dropParams.forEach((param) => {
+ if (param in llmConfig) {
+ delete llmConfig[param as keyof t.ClientOptions];
+ }
+ });
+ }
+
+ let useOpenRouter = false;
+ const configOptions: t.OpenAIConfiguration = {};
+
+ if (
+ (reverseProxyUrl && reverseProxyUrl.includes(KnownEndpoints.openrouter)) ||
+ (endpoint && endpoint.toLowerCase().includes(KnownEndpoints.openrouter))
+ ) {
+ useOpenRouter = true;
+ llmConfig.include_reasoning = true;
+ configOptions.baseURL = reverseProxyUrl;
+ configOptions.defaultHeaders = Object.assign(
+ {
+ 'HTTP-Referer': 'https://librechat.ai',
+ 'X-Title': 'LibreChat',
+ },
+ headers,
+ );
+ } else if (reverseProxyUrl) {
+ configOptions.baseURL = reverseProxyUrl;
+ if (headers) {
+ configOptions.defaultHeaders = headers;
+ }
+ }
+
+ if (defaultQuery) {
+ configOptions.defaultQuery = defaultQuery;
+ }
+
+ if (proxy) {
+ const proxyAgent = new HttpsProxyAgent(proxy);
+ configOptions.httpAgent = proxyAgent;
+ }
+
+ if (azure) {
+ const useModelName = isEnabled(process.env.AZURE_USE_MODEL_AS_DEPLOYMENT_NAME);
+ const updatedAzure = { ...azure };
+ updatedAzure.azureOpenAIApiDeploymentName = useModelName
+ ? sanitizeModelName(llmConfig.model || '')
+ : azure.azureOpenAIApiDeploymentName;
+
+ if (process.env.AZURE_OPENAI_DEFAULT_MODEL) {
+ llmConfig.model = process.env.AZURE_OPENAI_DEFAULT_MODEL;
+ }
+
+ if (configOptions.baseURL) {
+ const azureURL = constructAzureURL({
+ baseURL: configOptions.baseURL,
+ azureOptions: updatedAzure,
+ });
+ updatedAzure.azureOpenAIBasePath = azureURL.split(
+ `/${updatedAzure.azureOpenAIApiDeploymentName}`,
+ )[0];
+ }
+
+ Object.assign(llmConfig, updatedAzure);
+ llmConfig.model = updatedAzure.azureOpenAIApiDeploymentName;
+ } else {
+ llmConfig.apiKey = apiKey;
+ }
+
+ if (process.env.OPENAI_ORGANIZATION && azure) {
+ configOptions.organization = process.env.OPENAI_ORGANIZATION;
+ }
+
+ if (useOpenRouter && llmConfig.reasoning_effort != null) {
+ llmConfig.reasoning = {
+ effort: llmConfig.reasoning_effort,
+ };
+ delete llmConfig.reasoning_effort;
+ }
+
+ if (llmConfig.max_tokens != null) {
+ llmConfig.maxTokens = llmConfig.max_tokens;
+ delete llmConfig.max_tokens;
+ }
+
+ return {
+ llmConfig,
+ configOptions,
+ };
+}
diff --git a/packages/mcp/src/flow/manager.spec.ts b/packages/api/src/flow/manager.spec.ts
similarity index 100%
rename from packages/mcp/src/flow/manager.spec.ts
rename to packages/api/src/flow/manager.spec.ts
diff --git a/packages/mcp/src/flow/manager.ts b/packages/api/src/flow/manager.ts
similarity index 100%
rename from packages/mcp/src/flow/manager.ts
rename to packages/api/src/flow/manager.ts
diff --git a/packages/mcp/src/flow/types.ts b/packages/api/src/flow/types.ts
similarity index 100%
rename from packages/mcp/src/flow/types.ts
rename to packages/api/src/flow/types.ts
diff --git a/packages/api/src/index.ts b/packages/api/src/index.ts
new file mode 100644
index 0000000000..09a3665e42
--- /dev/null
+++ b/packages/api/src/index.ts
@@ -0,0 +1,14 @@
+/* MCP */
+export * from './mcp/manager';
+/* Utilities */
+export * from './mcp/utils';
+export * from './utils';
+/* Flow */
+export * from './flow/manager';
+/* Agents */
+export * from './agents';
+/* Endpoints */
+export * from './endpoints';
+/* types */
+export type * from './mcp/types';
+export type * from './flow/types';
diff --git a/packages/mcp/src/connection.ts b/packages/api/src/mcp/connection.ts
similarity index 99%
rename from packages/mcp/src/connection.ts
rename to packages/api/src/mcp/connection.ts
index aff0e080ad..381834ba2a 100644
--- a/packages/mcp/src/connection.ts
+++ b/packages/api/src/mcp/connection.ts
@@ -11,7 +11,7 @@ import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/
import type { Transport } from '@modelcontextprotocol/sdk/shared/transport.js';
import type { JSONRPCMessage } from '@modelcontextprotocol/sdk/types.js';
import type { Logger } from 'winston';
-import type * as t from './types/mcp.js';
+import type * as t from './types';
function isStdioOptions(options: t.MCPOptions): options is t.StdioOptions {
return 'command' in options;
@@ -87,7 +87,7 @@ export class MCPConnection extends EventEmitter {
this.lastPingTime = Date.now();
this.client = new Client(
{
- name: 'librechat-mcp-client',
+ name: '@librechat/api-client',
version: '1.2.2',
},
{
diff --git a/packages/mcp/src/enum.ts b/packages/api/src/mcp/enum.ts
similarity index 100%
rename from packages/mcp/src/enum.ts
rename to packages/api/src/mcp/enum.ts
diff --git a/packages/mcp/src/manager.ts b/packages/api/src/mcp/manager.ts
similarity index 99%
rename from packages/mcp/src/manager.ts
rename to packages/api/src/mcp/manager.ts
index 0d8a1e4653..93655dd3bb 100644
--- a/packages/mcp/src/manager.ts
+++ b/packages/api/src/mcp/manager.ts
@@ -2,7 +2,7 @@ import { CallToolResultSchema, ErrorCode, McpError } from '@modelcontextprotocol
import type { RequestOptions } from '@modelcontextprotocol/sdk/shared/protocol.js';
import type { JsonSchemaType, MCPOptions } from 'librechat-data-provider';
import type { Logger } from 'winston';
-import type * as t from './types/mcp';
+import type * as t from './types';
import { formatToolContent } from './parsers';
import { MCPConnection } from './connection';
import { CONSTANTS } from './enum';
diff --git a/packages/mcp/src/parsers.ts b/packages/api/src/mcp/parsers.ts
similarity index 99%
rename from packages/mcp/src/parsers.ts
rename to packages/api/src/mcp/parsers.ts
index 0ebcb077a5..e377f4b70c 100644
--- a/packages/mcp/src/parsers.ts
+++ b/packages/api/src/mcp/parsers.ts
@@ -1,4 +1,4 @@
-import type * as t from './types/mcp';
+import type * as t from './types';
const RECOGNIZED_PROVIDERS = new Set([
'google',
'anthropic',
diff --git a/packages/mcp/src/types/mcp.ts b/packages/api/src/mcp/types/index.ts
similarity index 92%
rename from packages/mcp/src/types/mcp.ts
rename to packages/api/src/mcp/types/index.ts
index 024ed8fd30..bfd73633eb 100644
--- a/packages/mcp/src/types/mcp.ts
+++ b/packages/api/src/mcp/types/index.ts
@@ -8,7 +8,6 @@ import {
StreamableHTTPOptionsSchema,
} from 'librechat-data-provider';
import type { JsonSchemaType, TPlugin } from 'librechat-data-provider';
-import { ToolSchema, ListToolsResultSchema } from '@modelcontextprotocol/sdk/types.js';
import type * as t from '@modelcontextprotocol/sdk/types.js';
export type StdioOptions = z.infer<typeof StdioOptionsSchema>;
@@ -45,8 +44,8 @@ export interface MCPPrompt {
export type ConnectionState = 'disconnected' | 'connecting' | 'connected' | 'error';
-export type MCPTool = z.infer<typeof ToolSchema>;
-export type MCPToolListResponse = z.infer<typeof ListToolsResultSchema>;
+export type MCPTool = z.infer<typeof t.ToolSchema>;
+export type MCPToolListResponse = z.infer<typeof t.ListToolsResultSchema>;
export type ToolContentPart = t.TextContent | t.ImageContent | t.EmbeddedResource | t.AudioContent;
export type ImageContent = Extract<ToolContentPart, { type: 'image' }>;
export type MCPToolCallResponse =
diff --git a/packages/mcp/src/utils.test.ts b/packages/api/src/mcp/utils.test.ts
similarity index 100%
rename from packages/mcp/src/utils.test.ts
rename to packages/api/src/mcp/utils.test.ts
diff --git a/packages/mcp/src/utils.ts b/packages/api/src/mcp/utils.ts
similarity index 100%
rename from packages/mcp/src/utils.ts
rename to packages/api/src/mcp/utils.ts
diff --git a/packages/api/src/types/azure.ts b/packages/api/src/types/azure.ts
new file mode 100644
index 0000000000..d21e1e1d01
--- /dev/null
+++ b/packages/api/src/types/azure.ts
@@ -0,0 +1,19 @@
+/**
+ * Azure OpenAI configuration interface
+ */
+export interface AzureOptions {
+ azureOpenAIApiKey?: string;
+ azureOpenAIApiInstanceName?: string;
+ azureOpenAIApiDeploymentName?: string;
+ azureOpenAIApiVersion?: string;
+ azureOpenAIBasePath?: string;
+}
+
+/**
+ * Client with azure property for setting deployment name
+ */
+export interface GenericClient {
+ azure: {
+ azureOpenAIApiDeploymentName?: string;
+ };
+}
diff --git a/packages/api/src/types/events.ts b/packages/api/src/types/events.ts
new file mode 100644
index 0000000000..1e866fa840
--- /dev/null
+++ b/packages/api/src/types/events.ts
@@ -0,0 +1,4 @@
+export type ServerSentEvent = {
+ data: string | Record<string, unknown>;
+ event?: string;
+};
diff --git a/packages/api/src/types/index.ts b/packages/api/src/types/index.ts
new file mode 100644
index 0000000000..488bcbf0e1
--- /dev/null
+++ b/packages/api/src/types/index.ts
@@ -0,0 +1,4 @@
+export * from './azure';
+export * from './events';
+export * from './openai';
+export * from './run';
diff --git a/packages/api/src/types/openai.ts b/packages/api/src/types/openai.ts
new file mode 100644
index 0000000000..cb11be984f
--- /dev/null
+++ b/packages/api/src/types/openai.ts
@@ -0,0 +1,97 @@
+import { z } from 'zod';
+import { openAISchema, EModelEndpoint } from 'librechat-data-provider';
+import type { TEndpointOption, TAzureConfig, TEndpoint } from 'librechat-data-provider';
+import type { OpenAIClientOptions } from '@librechat/agents';
+import type { AzureOptions } from './azure';
+
+export type OpenAIParameters = z.infer<typeof openAISchema>;
+
+/**
+ * Configuration options for the getLLMConfig function
+ */
+export interface LLMConfigOptions {
+ modelOptions?: Partial<OpenAIParameters>;
+ reverseProxyUrl?: string;
+ defaultQuery?: Record<string, string | undefined>;
+ headers?: Record<string, string>;
+ proxy?: string;
+ azure?: AzureOptions;
+ streaming?: boolean;
+ addParams?: Record<string, unknown>;
+ dropParams?: string[];
+}
+
+export type OpenAIConfiguration = OpenAIClientOptions['configuration'];
+
+export type ClientOptions = OpenAIClientOptions & {
+ include_reasoning?: boolean;
+};
+
+/**
+ * Return type for getLLMConfig function
+ */
+export interface LLMConfigResult {
+ llmConfig: ClientOptions;
+ configOptions: OpenAIConfiguration;
+}
+
+/**
+ * Interface for user values retrieved from the database
+ */
+export interface UserKeyValues {
+ apiKey?: string;
+ baseURL?: string;
+}
+
+/**
+ * Request interface with only the properties we need (avoids Express typing conflicts)
+ */
+export interface RequestData {
+ user: {
+ id: string;
+ };
+ body: {
+ model?: string;
+ endpoint?: string;
+ key?: string;
+ };
+ app: {
+ locals: {
+ [EModelEndpoint.azureOpenAI]?: TAzureConfig;
+ [EModelEndpoint.openAI]?: TEndpoint;
+ all?: TEndpoint;
+ };
+ };
+}
+
+/**
+ * Function type for getting user key values
+ */
+export type GetUserKeyValuesFunction = (params: {
+ userId: string;
+ name: string;
+}) => Promise<UserKeyValues>;
+
+/**
+ * Function type for checking user key expiry
+ */
+export type CheckUserKeyExpiryFunction = (expiresAt: string, endpoint: string) => void;
+
+/**
+ * Parameters for the initializeOpenAI function
+ */
+export interface InitializeOpenAIOptionsParams {
+ req: RequestData;
+ overrideModel?: string;
+ overrideEndpoint?: string;
+ endpointOption: Partial<TEndpointOption>;
+ getUserKeyValues: GetUserKeyValuesFunction;
+ checkUserKeyExpiry: CheckUserKeyExpiryFunction;
+}
+
+/**
+ * Extended LLM config result with stream rate handling
+ */
+export interface OpenAIOptionsResult extends LLMConfigResult {
+ streamRate?: number;
+}
diff --git a/packages/api/src/types/run.ts b/packages/api/src/types/run.ts
new file mode 100644
index 0000000000..080b3adba1
--- /dev/null
+++ b/packages/api/src/types/run.ts
@@ -0,0 +1,10 @@
+import type { AgentModelParameters, EModelEndpoint } from 'librechat-data-provider';
+import type { OpenAIConfiguration } from './openai';
+
+export type RunLLMConfig = {
+ provider: EModelEndpoint;
+ streaming: boolean;
+ streamUsage: boolean;
+ usage?: boolean;
+ configuration?: OpenAIConfiguration;
+} & AgentModelParameters;
diff --git a/api/utils/azureUtils.spec.js b/packages/api/src/utils/azure.spec.ts
similarity index 97%
rename from api/utils/azureUtils.spec.js
rename to packages/api/src/utils/azure.spec.ts
index 4d84451385..5f4a769ed8 100644
--- a/api/utils/azureUtils.spec.js
+++ b/packages/api/src/utils/azure.spec.ts
@@ -1,10 +1,11 @@
-const {
- sanitizeModelName,
- genAzureEndpoint,
+import {
genAzureChatCompletion,
getAzureCredentials,
constructAzureURL,
-} = require('./azureUtils');
+ sanitizeModelName,
+ genAzureEndpoint,
+} from './azure';
+import type { GenericClient } from '~/types';
describe('sanitizeModelName', () => {
test('removes periods from the model name', () => {
@@ -118,7 +119,7 @@ describe('genAzureChatCompletion', () => {
// Test with client parameter and model name
test('updates client with sanitized model name when provided and feature enabled', () => {
process.env.AZURE_USE_MODEL_AS_DEPLOYMENT_NAME = 'true';
- const clientMock = { azure: {} };
+ const clientMock = { azure: {} } as GenericClient;
const url = genAzureChatCompletion(
{
azureOpenAIApiInstanceName: 'instanceName',
@@ -135,7 +136,7 @@ describe('genAzureChatCompletion', () => {
// Test with client parameter but without model name
test('does not update client when model name is not provided', () => {
- const clientMock = { azure: {} };
+ const clientMock = { azure: {} } as GenericClient;
const url = genAzureChatCompletion(
{
azureOpenAIApiInstanceName: 'instanceName',
@@ -154,7 +155,7 @@ describe('genAzureChatCompletion', () => {
// Test with client parameter and deployment name when feature is disabled
test('does not update client when feature is disabled', () => {
process.env.AZURE_USE_MODEL_AS_DEPLOYMENT_NAME = 'false';
- const clientMock = { azure: {} };
+ const clientMock = { azure: {} } as GenericClient;
const url = genAzureChatCompletion(
{
azureOpenAIApiInstanceName: 'instanceName',
diff --git a/api/utils/azureUtils.js b/packages/api/src/utils/azure.ts
similarity index 50%
rename from api/utils/azureUtils.js
rename to packages/api/src/utils/azure.ts
index 7adb133449..b4051d3d80 100644
--- a/api/utils/azureUtils.js
+++ b/packages/api/src/utils/azure.ts
@@ -1,57 +1,74 @@
-const { isEnabled } = require('~/server/utils/handleText');
+import { isEnabled } from './common';
+import type { AzureOptions, GenericClient } from '~/types';
/**
* Sanitizes the model name to be used in the URL by removing or replacing disallowed characters.
- * @param {string} modelName - The model name to be sanitized.
- * @returns {string} The sanitized model name.
+ * @param modelName - The model name to be sanitized.
+ * @returns The sanitized model name.
*/
-const sanitizeModelName = (modelName) => {
+export const sanitizeModelName = (modelName: string): string => {
// Replace periods with empty strings and other disallowed characters as needed.
return modelName.replace(/\./g, '');
};
/**
* Generates the Azure OpenAI API endpoint URL.
- * @param {Object} params - The parameters object.
- * @param {string} params.azureOpenAIApiInstanceName - The Azure OpenAI API instance name.
- * @param {string} params.azureOpenAIApiDeploymentName - The Azure OpenAI API deployment name.
- * @returns {string} The complete endpoint URL for the Azure OpenAI API.
+ * @param params - The parameters object.
+ * @param params.azureOpenAIApiInstanceName - The Azure OpenAI API instance name.
+ * @param params.azureOpenAIApiDeploymentName - The Azure OpenAI API deployment name.
+ * @returns The complete endpoint URL for the Azure OpenAI API.
*/
-const genAzureEndpoint = ({ azureOpenAIApiInstanceName, azureOpenAIApiDeploymentName }) => {
+export const genAzureEndpoint = ({
+ azureOpenAIApiInstanceName,
+ azureOpenAIApiDeploymentName,
+}: {
+ azureOpenAIApiInstanceName: string;
+ azureOpenAIApiDeploymentName: string;
+}): string => {
return `https://${azureOpenAIApiInstanceName}.openai.azure.com/openai/deployments/${azureOpenAIApiDeploymentName}`;
};
/**
* Generates the Azure OpenAI API chat completion endpoint URL with the API version.
* If both deploymentName and modelName are provided, modelName takes precedence.
- * @param {Object} AzureConfig - The Azure configuration object.
- * @param {string} AzureConfig.azureOpenAIApiInstanceName - The Azure OpenAI API instance name.
- * @param {string} [AzureConfig.azureOpenAIApiDeploymentName] - The Azure OpenAI API deployment name (optional).
- * @param {string} AzureConfig.azureOpenAIApiVersion - The Azure OpenAI API version.
- * @param {string} [modelName] - The model name to be included in the deployment name (optional).
- * @param {Object} [client] - The API Client class for optionally setting properties (optional).
- * @returns {string} The complete chat completion endpoint URL for the Azure OpenAI API.
- * @throws {Error} If neither azureOpenAIApiDeploymentName nor modelName is provided.
+ * @param azureConfig - The Azure configuration object.
+ * @param azureConfig.azureOpenAIApiInstanceName - The Azure OpenAI API instance name.
+ * @param azureConfig.azureOpenAIApiDeploymentName - The Azure OpenAI API deployment name (optional).
+ * @param azureConfig.azureOpenAIApiVersion - The Azure OpenAI API version.
+ * @param modelName - The model name to be included in the deployment name (optional).
+ * @param client - The API Client class for optionally setting properties (optional).
+ * @returns The complete chat completion endpoint URL for the Azure OpenAI API.
+ * @throws Error if neither azureOpenAIApiDeploymentName nor modelName is provided.
*/
-const genAzureChatCompletion = (
- { azureOpenAIApiInstanceName, azureOpenAIApiDeploymentName, azureOpenAIApiVersion },
- modelName,
- client,
-) => {
+export const genAzureChatCompletion = (
+ {
+ azureOpenAIApiInstanceName,
+ azureOpenAIApiDeploymentName,
+ azureOpenAIApiVersion,
+ }: {
+ azureOpenAIApiInstanceName: string;
+ azureOpenAIApiDeploymentName?: string;
+ azureOpenAIApiVersion: string;
+ },
+ modelName?: string,
+ client?: GenericClient,
+): string => {
// Determine the deployment segment of the URL based on provided modelName or azureOpenAIApiDeploymentName
- let deploymentSegment;
+ let deploymentSegment: string;
if (isEnabled(process.env.AZURE_USE_MODEL_AS_DEPLOYMENT_NAME) && modelName) {
const sanitizedModelName = sanitizeModelName(modelName);
- deploymentSegment = `${sanitizedModelName}`;
- client &&
- typeof client === 'object' &&
- (client.azure.azureOpenAIApiDeploymentName = sanitizedModelName);
+ deploymentSegment = sanitizedModelName;
+ if (client && typeof client === 'object') {
+ client.azure.azureOpenAIApiDeploymentName = sanitizedModelName;
+ }
} else if (azureOpenAIApiDeploymentName) {
deploymentSegment = azureOpenAIApiDeploymentName;
} else if (!process.env.AZURE_OPENAI_BASEURL) {
throw new Error(
'Either a model name with the `AZURE_USE_MODEL_AS_DEPLOYMENT_NAME` setting or a deployment name must be provided if `AZURE_OPENAI_BASEURL` is omitted.',
);
+ } else {
+ deploymentSegment = '';
}
return `https://${azureOpenAIApiInstanceName}.openai.azure.com/openai/deployments/${deploymentSegment}/chat/completions?api-version=${azureOpenAIApiVersion}`;
@@ -59,9 +76,9 @@ const genAzureChatCompletion = (
/**
* Retrieves the Azure OpenAI API credentials from environment variables.
- * @returns {AzureOptions} An object containing the Azure OpenAI API credentials.
+ * @returns An object containing the Azure OpenAI API credentials.
*/
-const getAzureCredentials = () => {
+export const getAzureCredentials = (): AzureOptions => {
return {
azureOpenAIApiKey: process.env.AZURE_API_KEY ?? process.env.AZURE_OPENAI_API_KEY,
azureOpenAIApiInstanceName: process.env.AZURE_OPENAI_API_INSTANCE_NAME,
@@ -76,12 +93,18 @@ const getAzureCredentials = () => {
* them with 'azureOpenAIApiInstanceName' and 'azureOpenAIApiDeploymentName' from the azure object.
* If the respective azure property is not provided, the placeholder is replaced with an empty string.
*
- * @param {Object} params - The parameters object.
- * @param {string} params.baseURL - The baseURL to inspect for replacement placeholders.
- * @param {AzureOptions} params.azureOptions - The azure options object containing the instance and deployment names.
- * @returns {string} The complete baseURL with credentials injected for the Azure OpenAI API.
+ * @param params - The parameters object.
+ * @param params.baseURL - The baseURL to inspect for replacement placeholders.
+ * @param params.azureOptions - The azure options object containing the instance and deployment names.
+ * @returns The complete baseURL with credentials injected for the Azure OpenAI API.
*/
-function constructAzureURL({ baseURL, azureOptions }) {
+export function constructAzureURL({
+ baseURL,
+ azureOptions,
+}: {
+ baseURL: string;
+ azureOptions?: AzureOptions;
+}): string {
let finalURL = baseURL;
// Replace INSTANCE_NAME and DEPLOYMENT_NAME placeholders with actual values if available
@@ -95,11 +118,3 @@ function constructAzureURL({ baseURL, azureOptions }) {
return finalURL;
}
-
-module.exports = {
- sanitizeModelName,
- genAzureEndpoint,
- genAzureChatCompletion,
- getAzureCredentials,
- constructAzureURL,
-};
diff --git a/packages/api/src/utils/common.spec.ts b/packages/api/src/utils/common.spec.ts
new file mode 100644
index 0000000000..c735fb057d
--- /dev/null
+++ b/packages/api/src/utils/common.spec.ts
@@ -0,0 +1,55 @@
+/* eslint-disable @typescript-eslint/ban-ts-comment */
+import { isEnabled } from './common';
+
+describe('isEnabled', () => {
+ test('should return true when input is "true"', () => {
+ expect(isEnabled('true')).toBe(true);
+ });
+
+ test('should return true when input is "TRUE"', () => {
+ expect(isEnabled('TRUE')).toBe(true);
+ });
+
+ test('should return true when input is true', () => {
+ expect(isEnabled(true)).toBe(true);
+ });
+
+ test('should return false when input is "false"', () => {
+ expect(isEnabled('false')).toBe(false);
+ });
+
+ test('should return false when input is false', () => {
+ expect(isEnabled(false)).toBe(false);
+ });
+
+ test('should return false when input is null', () => {
+ expect(isEnabled(null)).toBe(false);
+ });
+
+ test('should return false when input is undefined', () => {
+ expect(isEnabled()).toBe(false);
+ });
+
+ test('should return false when input is an empty string', () => {
+ expect(isEnabled('')).toBe(false);
+ });
+
+ test('should return false when input is a whitespace string', () => {
+ expect(isEnabled(' ')).toBe(false);
+ });
+
+ test('should return false when input is a number', () => {
+ // @ts-expect-error
+ expect(isEnabled(123)).toBe(false);
+ });
+
+ test('should return false when input is an object', () => {
+ // @ts-expect-error
+ expect(isEnabled({})).toBe(false);
+ });
+
+ test('should return false when input is an array', () => {
+ // @ts-expect-error
+ expect(isEnabled([])).toBe(false);
+ });
+});
diff --git a/packages/api/src/utils/common.ts b/packages/api/src/utils/common.ts
new file mode 100644
index 0000000000..693c00d487
--- /dev/null
+++ b/packages/api/src/utils/common.ts
@@ -0,0 +1,48 @@
+/**
+ * Checks if the given value is truthy by being either the boolean `true` or a string
+ * that case-insensitively matches 'true'.
+ *
+ * @param value - The value to check.
+ * @returns Returns `true` if the value is the boolean `true` or a case-insensitive
+ * match for the string 'true', otherwise returns `false`.
+ * @example
+ *
+ * isEnabled("True"); // returns true
+ * isEnabled("TRUE"); // returns true
+ * isEnabled(true); // returns true
+ * isEnabled("false"); // returns false
+ * isEnabled(false); // returns false
+ * isEnabled(null); // returns false
+ * isEnabled(); // returns false
+ */
+export function isEnabled(value?: string | boolean | null | undefined): boolean {
+ if (typeof value === 'boolean') {
+ return value;
+ }
+ if (typeof value === 'string') {
+ return value.toLowerCase().trim() === 'true';
+ }
+ return false;
+}
+
+/**
+ * Checks if the provided value is 'user_provided'.
+ *
+ * @param value - The value to check.
+ * @returns - Returns true if the value is 'user_provided', otherwise false.
+ */
+export const isUserProvided = (value?: string): boolean => value === 'user_provided';
+
+/**
+ * @param values
+ */
+export function optionalChainWithEmptyCheck(
+ ...values: (string | number | undefined)[]
+): string | number | undefined {
+ for (const value of values) {
+ if (value !== undefined && value !== null && value !== '') {
+ return value;
+ }
+ }
+ return values[values.length - 1];
+}
diff --git a/packages/api/src/utils/events.ts b/packages/api/src/utils/events.ts
new file mode 100644
index 0000000000..76d1361d9b
--- /dev/null
+++ b/packages/api/src/utils/events.ts
@@ -0,0 +1,16 @@
+import type { Response as ServerResponse } from 'express';
+import type { ServerSentEvent } from '~/types';
+
+/**
+ * Sends message data in Server Sent Events format.
+ * @param res - The server response.
+ * @param event - The message event.
+ * @param event.event - The type of event.
+ * @param event.data - The message to be sent.
+ */
+export function sendEvent(res: ServerResponse, event: ServerSentEvent): void {
+ if (typeof event.data === 'string' && event.data.length === 0) {
+ return;
+ }
+ res.write(`event: message\ndata: ${JSON.stringify(event)}\n\n`);
+}
diff --git a/packages/api/src/utils/files.spec.ts b/packages/api/src/utils/files.spec.ts
new file mode 100644
index 0000000000..db51d51e0f
--- /dev/null
+++ b/packages/api/src/utils/files.spec.ts
@@ -0,0 +1,115 @@
+import { sanitizeFilename } from './files';
+
+jest.mock('node:crypto', () => {
+ const actualModule = jest.requireActual('node:crypto');
+ return {
+ ...actualModule,
+ randomBytes: jest.fn().mockReturnValue(Buffer.from('abc123', 'hex')),
+ };
+});
+
+describe('sanitizeFilename', () => {
+ test('removes directory components (1/2)', () => {
+ expect(sanitizeFilename('/path/to/file.txt')).toBe('file.txt');
+ });
+
+ test('removes directory components (2/2)', () => {
+ expect(sanitizeFilename('../../../../file.txt')).toBe('file.txt');
+ });
+
+ test('replaces non-alphanumeric characters', () => {
+ expect(sanitizeFilename('file name@#$.txt')).toBe('file_name___.txt');
+ });
+
+ test('preserves dots and hyphens', () => {
+ expect(sanitizeFilename('file-name.with.dots.txt')).toBe('file-name.with.dots.txt');
+ });
+
+ test('prepends underscore to filenames starting with a dot', () => {
+ expect(sanitizeFilename('.hiddenfile')).toBe('_.hiddenfile');
+ });
+
+ test('truncates long filenames', () => {
+ const longName = 'a'.repeat(300) + '.txt';
+ const result = sanitizeFilename(longName);
+ expect(result.length).toBe(255);
+ expect(result).toMatch(/^a+-abc123\.txt$/);
+ });
+
+ test('handles filenames with no extension', () => {
+ const longName = 'a'.repeat(300);
+ const result = sanitizeFilename(longName);
+ expect(result.length).toBe(255);
+ expect(result).toMatch(/^a+-abc123$/);
+ });
+
+ test('handles empty input', () => {
+ expect(sanitizeFilename('')).toBe('_');
+ });
+
+ test('handles input with only special characters', () => {
+ expect(sanitizeFilename('@#$%^&*')).toBe('_______');
+ });
+});
+
+describe('sanitizeFilename with real crypto', () => {
+ // Temporarily unmock crypto for these tests
+ beforeAll(() => {
+ jest.resetModules();
+ jest.unmock('node:crypto');
+ });
+
+ afterAll(() => {
+ jest.resetModules();
+ jest.mock('node:crypto', () => {
+ const actualModule = jest.requireActual('node:crypto');
+ return {
+ ...actualModule,
+ randomBytes: jest.fn().mockReturnValue(Buffer.from('abc123', 'hex')),
+ };
+ });
+ });
+
+ test('truncates long filenames with real crypto', async () => {
+ const { sanitizeFilename: realSanitizeFilename } = await import('./files');
+ const longName = 'b'.repeat(300) + '.pdf';
+ const result = realSanitizeFilename(longName);
+
+ expect(result.length).toBe(255);
+ expect(result).toMatch(/^b+-[a-f0-9]{6}\.pdf$/);
+ expect(result.endsWith('.pdf')).toBe(true);
+ });
+
+ test('handles filenames with no extension with real crypto', async () => {
+ const { sanitizeFilename: realSanitizeFilename } = await import('./files');
+ const longName = 'c'.repeat(300);
+ const result = realSanitizeFilename(longName);
+
+ expect(result.length).toBe(255);
+ expect(result).toMatch(/^c+-[a-f0-9]{6}$/);
+ expect(result).not.toContain('.');
+ });
+
+ test('generates unique suffixes for identical long filenames', async () => {
+ const { sanitizeFilename: realSanitizeFilename } = await import('./files');
+ const longName = 'd'.repeat(300) + '.doc';
+ const result1 = realSanitizeFilename(longName);
+ const result2 = realSanitizeFilename(longName);
+
+ expect(result1.length).toBe(255);
+ expect(result2.length).toBe(255);
+ expect(result1).not.toBe(result2); // Should be different due to random suffix
+ expect(result1.endsWith('.doc')).toBe(true);
+ expect(result2.endsWith('.doc')).toBe(true);
+ });
+
+ test('real crypto produces valid hex strings', async () => {
+ const { sanitizeFilename: realSanitizeFilename } = await import('./files');
+ const longName = 'test'.repeat(100) + '.txt';
+ const result = realSanitizeFilename(longName);
+
+ const hexMatch = result.match(/-([a-f0-9]{6})\.txt$/);
+ expect(hexMatch).toBeTruthy();
+ expect(hexMatch![1]).toMatch(/^[a-f0-9]{6}$/);
+ });
+});
diff --git a/packages/api/src/utils/files.ts b/packages/api/src/utils/files.ts
new file mode 100644
index 0000000000..0d8f111a26
--- /dev/null
+++ b/packages/api/src/utils/files.ts
@@ -0,0 +1,33 @@
+import path from 'path';
+import crypto from 'node:crypto';
+
+/**
+ * Sanitize a filename by removing any directory components, replacing non-alphanumeric characters
+ * @param inputName
+ */
+export function sanitizeFilename(inputName: string): string {
+ // Remove any directory components
+ let name = path.basename(inputName);
+
+ // Replace any non-alphanumeric characters except for '.' and '-'
+ name = name.replace(/[^a-zA-Z0-9.-]/g, '_');
+
+ // Ensure the name doesn't start with a dot (hidden file in Unix-like systems)
+ if (name.startsWith('.') || name === '') {
+ name = '_' + name;
+ }
+
+ // Limit the length of the filename
+ const MAX_LENGTH = 255;
+ if (name.length > MAX_LENGTH) {
+ const ext = path.extname(name);
+ const nameWithoutExt = path.basename(name, ext);
+ name =
+ nameWithoutExt.slice(0, MAX_LENGTH - ext.length - 7) +
+ '-' +
+ crypto.randomBytes(3).toString('hex') +
+ ext;
+ }
+
+ return name;
+}
diff --git a/packages/api/src/utils/generators.ts b/packages/api/src/utils/generators.ts
new file mode 100644
index 0000000000..902721df34
--- /dev/null
+++ b/packages/api/src/utils/generators.ts
@@ -0,0 +1,75 @@
+import fetch from 'node-fetch';
+import { logger } from '@librechat/data-schemas';
+import { GraphEvents, sleep } from '@librechat/agents';
+import type { Response as ServerResponse } from 'express';
+import type { ServerSentEvent } from '~/types';
+import { sendEvent } from './events';
+
+/**
+ * Makes a function to make HTTP request and logs the process.
+ * @param params
+ * @param params.directEndpoint - Whether to use a direct endpoint.
+ * @param params.reverseProxyUrl - The reverse proxy URL to use for the request.
+ * @returns A promise that resolves to the response of the fetch request.
+ */
+export function createFetch({
+ directEndpoint = false,
+ reverseProxyUrl = '',
+}: {
+ directEndpoint?: boolean;
+ reverseProxyUrl?: string;
+}) {
+ /**
+ * Makes an HTTP request and logs the process.
+ * @param url - The URL to make the request to. Can be a string or a Request object.
+ * @param init - Optional init options for the request.
+ * @returns A promise that resolves to the response of the fetch request.
+ */
+ return async function (
+ _url: fetch.RequestInfo,
+ init: fetch.RequestInit,
+ ): Promise<fetch.Response> {
+ let url = _url;
+ if (directEndpoint) {
+ url = reverseProxyUrl;
+ }
+ logger.debug(`Making request to ${url}`);
+ if (typeof Bun !== 'undefined') {
+ return await fetch(url, init);
+ }
+ return await fetch(url, init);
+ };
+}
+
+/**
+ * Creates event handlers for stream events that don't capture client references
+ * @param res - The response object to send events to
+ * @returns Object containing handler functions
+ */
+export function createStreamEventHandlers(res: ServerResponse) {
+ return {
+ [GraphEvents.ON_RUN_STEP]: function (event: ServerSentEvent) {
+ if (res) {
+ sendEvent(res, event);
+ }
+ },
+ [GraphEvents.ON_MESSAGE_DELTA]: function (event: ServerSentEvent) {
+ if (res) {
+ sendEvent(res, event);
+ }
+ },
+ [GraphEvents.ON_REASONING_DELTA]: function (event: ServerSentEvent) {
+ if (res) {
+ sendEvent(res, event);
+ }
+ },
+ };
+}
+
+export function createHandleLLMNewToken(streamRate: number) {
+ return async function () {
+ if (streamRate) {
+ await sleep(streamRate);
+ }
+ };
+}
diff --git a/packages/api/src/utils/index.ts b/packages/api/src/utils/index.ts
new file mode 100644
index 0000000000..ae6157b6c1
--- /dev/null
+++ b/packages/api/src/utils/index.ts
@@ -0,0 +1,5 @@
+export * from './azure';
+export * from './common';
+export * from './events';
+export * from './generators';
+export { default as Tokenizer } from './tokenizer';
diff --git a/api/server/services/Tokenizer.spec.js b/packages/api/src/utils/tokenizer.spec.ts
similarity index 90%
rename from api/server/services/Tokenizer.spec.js
rename to packages/api/src/utils/tokenizer.spec.ts
index 2f93489dcc..edd6fe14de 100644
--- a/api/server/services/Tokenizer.spec.js
+++ b/packages/api/src/utils/tokenizer.spec.ts
@@ -5,13 +5,20 @@
* Make sure to install `tiktoken` and have it configured properly.
*/
-const Tokenizer = require('./Tokenizer'); // <-- Adjust path to your singleton file
-const { logger } = require('~/config');
+import { logger } from '@librechat/data-schemas';
+import type { Tiktoken } from 'tiktoken';
+import Tokenizer from './tokenizer';
+
+jest.mock('@librechat/data-schemas', () => ({
+ logger: {
+ error: jest.fn(),
+ },
+}));
describe('Tokenizer', () => {
- it('should be a singleton (same instance)', () => {
- const AnotherTokenizer = require('./Tokenizer'); // same path
- expect(Tokenizer).toBe(AnotherTokenizer);
+ it('should be a singleton (same instance)', async () => {
+ const AnotherTokenizer = await import('./tokenizer'); // same path
+ expect(Tokenizer).toBe(AnotherTokenizer.default);
});
describe('getTokenizer', () => {
@@ -73,7 +80,7 @@ describe('Tokenizer', () => {
free() {
throw new Error('Intentional free error');
},
- };
+ } as unknown as Tiktoken;
// Should not throw uncaught errors
Tokenizer.freeAndResetAllEncoders();
@@ -103,7 +110,7 @@ describe('Tokenizer', () => {
});
it('should reset encoders if an error is thrown', () => {
- // We can simulate an error by temporarily overriding the selected tokenizer’s `encode` method.
+ // We can simulate an error by temporarily overriding the selected tokenizer's `encode` method.
const tokenizer = Tokenizer.getTokenizer('cl100k_base', false);
const originalEncode = tokenizer.encode;
tokenizer.encode = () => {
diff --git a/api/server/services/Tokenizer.js b/packages/api/src/utils/tokenizer.ts
similarity index 56%
rename from api/server/services/Tokenizer.js
rename to packages/api/src/utils/tokenizer.ts
index ac620defa8..2a2088cad3 100644
--- a/api/server/services/Tokenizer.js
+++ b/packages/api/src/utils/tokenizer.ts
@@ -1,28 +1,41 @@
-const { encoding_for_model: encodingForModel, get_encoding: getEncoding } = require('tiktoken');
-const { logger } = require('~/config');
+import { logger } from '@librechat/data-schemas';
+import { encoding_for_model as encodingForModel, get_encoding as getEncoding } from 'tiktoken';
+import type { Tiktoken, TiktokenModel, TiktokenEncoding } from 'tiktoken';
+
+interface TokenizerOptions {
+ debug?: boolean;
+}
class Tokenizer {
+ tokenizersCache: Record<string, Tiktoken>;
+ tokenizerCallsCount: number;
+ private options?: TokenizerOptions;
+
constructor() {
this.tokenizersCache = {};
this.tokenizerCallsCount = 0;
}
- getTokenizer(encoding, isModelName = false, extendSpecialTokens = {}) {
- let tokenizer;
+ getTokenizer(
+ encoding: TiktokenModel | TiktokenEncoding,
+ isModelName = false,
+ extendSpecialTokens: Record<string, number> = {},
+ ): Tiktoken {
+ let tokenizer: Tiktoken;
if (this.tokenizersCache[encoding]) {
tokenizer = this.tokenizersCache[encoding];
} else {
if (isModelName) {
- tokenizer = encodingForModel(encoding, extendSpecialTokens);
+ tokenizer = encodingForModel(encoding as TiktokenModel, extendSpecialTokens);
} else {
- tokenizer = getEncoding(encoding, extendSpecialTokens);
+ tokenizer = getEncoding(encoding as TiktokenEncoding, extendSpecialTokens);
}
this.tokenizersCache[encoding] = tokenizer;
}
return tokenizer;
}
- freeAndResetAllEncoders() {
+ freeAndResetAllEncoders(): void {
try {
Object.keys(this.tokenizersCache).forEach((key) => {
if (this.tokenizersCache[key]) {
@@ -36,7 +49,7 @@ class Tokenizer {
}
}
- resetTokenizersIfNecessary() {
+ resetTokenizersIfNecessary(): void {
if (this.tokenizerCallsCount >= 25) {
if (this.options?.debug) {
logger.debug('[Tokenizer] freeAndResetAllEncoders: reached 25 encodings, resetting...');
@@ -46,12 +59,13 @@ class Tokenizer {
this.tokenizerCallsCount++;
}
- getTokenCount(text, encoding = 'cl100k_base') {
+ getTokenCount(text: string, encoding: TiktokenModel | TiktokenEncoding = 'cl100k_base'): number {
this.resetTokenizersIfNecessary();
try {
const tokenizer = this.getTokenizer(encoding);
return tokenizer.encode(text, 'all').length;
} catch (error) {
+ logger.error('[Tokenizer] Error getting token count:', error);
this.freeAndResetAllEncoders();
const tokenizer = this.getTokenizer(encoding);
return tokenizer.encode(text, 'all').length;
@@ -61,4 +75,4 @@ class Tokenizer {
const TokenizerSingleton = new Tokenizer();
-module.exports = TokenizerSingleton;
+export default TokenizerSingleton;
diff --git a/packages/mcp/tsconfig-paths-bootstrap.mjs b/packages/api/tsconfig-paths-bootstrap.mjs
similarity index 100%
rename from packages/mcp/tsconfig-paths-bootstrap.mjs
rename to packages/api/tsconfig-paths-bootstrap.mjs
diff --git a/packages/mcp/tsconfig.json b/packages/api/tsconfig.json
similarity index 92%
rename from packages/mcp/tsconfig.json
rename to packages/api/tsconfig.json
index 7c03dc1fb4..ccdf3ebb2e 100644
--- a/packages/mcp/tsconfig.json
+++ b/packages/api/tsconfig.json
@@ -18,7 +18,10 @@
"isolatedModules": true,
"noEmit": true,
"sourceMap": true,
- "baseUrl": "."
+ "baseUrl": ".",
+ "paths": {
+ "~/*": ["./src/*"]
+ }
},
"ts-node": {
"experimentalSpecifierResolution": "node",
diff --git a/packages/mcp/tsconfig.spec.json b/packages/api/tsconfig.spec.json
similarity index 100%
rename from packages/mcp/tsconfig.spec.json
rename to packages/api/tsconfig.spec.json
diff --git a/packages/data-provider/package.json b/packages/data-provider/package.json
index 7cdd37eae7..146121cb44 100644
--- a/packages/data-provider/package.json
+++ b/packages/data-provider/package.json
@@ -1,6 +1,6 @@
{
"name": "librechat-data-provider",
- "version": "0.7.86",
+ "version": "0.7.87",
"description": "data services for librechat apps",
"main": "dist/index.js",
"module": "dist/index.es.js",
diff --git a/packages/data-provider/src/api-endpoints.ts b/packages/data-provider/src/api-endpoints.ts
index 63a00acef1..88a9d4a148 100644
--- a/packages/data-provider/src/api-endpoints.ts
+++ b/packages/data-provider/src/api-endpoints.ts
@@ -254,6 +254,7 @@ export const getAllPromptGroups = () => `${prompts()}/all`;
export const roles = () => '/api/roles';
export const getRole = (roleName: string) => `${roles()}/${roleName.toLowerCase()}`;
export const updatePromptPermissions = (roleName: string) => `${getRole(roleName)}/prompts`;
+export const updateMemoryPermissions = (roleName: string) => `${getRole(roleName)}/memories`;
export const updateAgentPermissions = (roleName: string) => `${getRole(roleName)}/agents`;
/* Conversation Tags */
@@ -283,3 +284,8 @@ export const confirmTwoFactor = () => '/api/auth/2fa/confirm';
export const disableTwoFactor = () => '/api/auth/2fa/disable';
export const regenerateBackupCodes = () => '/api/auth/2fa/backup/regenerate';
export const verifyTwoFactorTemp = () => '/api/auth/2fa/verify-temp';
+
+/* Memories */
+export const memories = () => '/api/memories';
+export const memory = (key: string) => `${memories()}/${encodeURIComponent(key)}`;
+export const memoryPreferences = () => `${memories()}/preferences`;
diff --git a/packages/data-provider/src/config.ts b/packages/data-provider/src/config.ts
index 005efa4434..beadb4c3f3 100644
--- a/packages/data-provider/src/config.ts
+++ b/packages/data-provider/src/config.ts
@@ -493,6 +493,7 @@ export const intefaceSchema = z
sidePanel: z.boolean().optional(),
multiConvo: z.boolean().optional(),
bookmarks: z.boolean().optional(),
+ memories: z.boolean().optional(),
presets: z.boolean().optional(),
prompts: z.boolean().optional(),
agents: z.boolean().optional(),
@@ -508,6 +509,7 @@ export const intefaceSchema = z
presets: true,
multiConvo: true,
bookmarks: true,
+ memories: true,
prompts: true,
agents: true,
temporaryChat: true,
@@ -649,11 +651,35 @@ export const balanceSchema = z.object({
refillAmount: z.number().optional().default(10000),
});
+export const memorySchema = z.object({
+ disabled: z.boolean().optional(),
+ validKeys: z.array(z.string()).optional(),
+ tokenLimit: z.number().optional(),
+ personalize: z.boolean().default(true),
+ messageWindowSize: z.number().optional().default(5),
+ agent: z
+ .union([
+ z.object({
+ id: z.string(),
+ }),
+ z.object({
+ provider: z.string(),
+ model: z.string(),
+ instructions: z.string().optional(),
+ model_parameters: z.record(z.any()).optional(),
+ }),
+ ])
+ .optional(),
+});
+
+export type TMemoryConfig = z.infer<typeof memorySchema>;
+
export const configSchema = z.object({
version: z.string(),
cache: z.boolean().default(true),
ocr: ocrSchema.optional(),
webSearch: webSearchSchema.optional(),
+ memory: memorySchema.optional(),
secureImageLinks: z.boolean().optional(),
imageOutputType: z.nativeEnum(EImageOutputType).default(EImageOutputType.PNG),
includedTools: z.array(z.string()).optional(),
@@ -1291,6 +1317,10 @@ export enum SettingsTabValues {
* Chat input commands
*/
COMMANDS = 'commands',
+ /**
+ * Tab for Personalization Settings
+ */
+ PERSONALIZATION = 'personalization',
}
export enum STTProviders {
diff --git a/packages/data-provider/src/createPayload.ts b/packages/data-provider/src/createPayload.ts
index edd8f0e426..1640d877d8 100644
--- a/packages/data-provider/src/createPayload.ts
+++ b/packages/data-provider/src/createPayload.ts
@@ -13,11 +13,11 @@ export default function createPayload(submission: t.TSubmission) {
ephemeralAgent,
} = submission;
const { conversationId } = s.tConvoUpdateSchema.parse(conversation);
- const { endpoint, endpointType } = endpointOption as {
+ const { endpoint: _e, endpointType } = endpointOption as {
endpoint: s.EModelEndpoint;
endpointType?: s.EModelEndpoint;
};
-
+ const endpoint = _e as s.EModelEndpoint;
let server = EndpointURLs[endpointType ?? endpoint];
const isEphemeral = s.isEphemeralAgent(endpoint, ephemeralAgent);
@@ -32,6 +32,7 @@ export default function createPayload(submission: t.TSubmission) {
const payload: t.TPayload = {
...userMessage,
...endpointOption,
+ endpoint,
ephemeralAgent: isEphemeral ? ephemeralAgent : undefined,
isContinued: !!(isEdited && isContinued),
conversationId,
diff --git a/packages/data-provider/src/data-service.ts b/packages/data-provider/src/data-service.ts
index 036d7928c7..c78553f3a3 100644
--- a/packages/data-provider/src/data-service.ts
+++ b/packages/data-provider/src/data-service.ts
@@ -718,6 +718,12 @@ export function updateAgentPermissions(
return request.put(endpoints.updateAgentPermissions(variables.roleName), variables.updates);
}
+export function updateMemoryPermissions(
+ variables: m.UpdateMemoryPermVars,
+): Promise<m.UpdatePermResponse> {
+ return request.put(endpoints.updateMemoryPermissions(variables.roleName), variables.updates);
+}
+
/* Tags */
export function getConversationTags(): Promise<t.TConversationTagsResponse> {
return request.get(endpoints.conversationTags());
@@ -799,3 +805,33 @@ export function verifyTwoFactorTemp(
): Promise<t.TVerify2FATempResponse> {
return request.post(endpoints.verifyTwoFactorTemp(), payload);
}
+
+/* Memories */
+export const getMemories = (): Promise<q.MemoriesResponse> => {
+ return request.get(endpoints.memories());
+};
+
+export const deleteMemory = (key: string): Promise<void> => {
+ return request.delete(endpoints.memory(key));
+};
+
+export const updateMemory = (
+ key: string,
+ value: string,
+ originalKey?: string,
+): Promise<q.TUserMemory> => {
+ return request.patch(endpoints.memory(originalKey || key), { key, value });
+};
+
+export const updateMemoryPreferences = (preferences: {
+ memories: boolean;
+}): Promise<{ updated: boolean; preferences: { memories: boolean } }> => {
+ return request.patch(endpoints.memoryPreferences(), preferences);
+};
+
+export const createMemory = (data: {
+ key: string;
+ value: string;
+}): Promise<{ created: boolean; memory: q.TUserMemory }> => {
+ return request.post(endpoints.memories(), data);
+};
diff --git a/packages/data-provider/src/index.ts b/packages/data-provider/src/index.ts
index f6c9579eb6..9869029adf 100644
--- a/packages/data-provider/src/index.ts
+++ b/packages/data-provider/src/index.ts
@@ -16,6 +16,8 @@ export * from './models';
export * from './mcp';
/* web search */
export * from './web';
+/* memory */
+export * from './memory';
/* RBAC */
export * from './permissions';
export * from './roles';
diff --git a/packages/data-provider/src/keys.ts b/packages/data-provider/src/keys.ts
index f9e51dc094..93e21c7e45 100644
--- a/packages/data-provider/src/keys.ts
+++ b/packages/data-provider/src/keys.ts
@@ -46,6 +46,8 @@ export enum QueryKeys {
health = 'health',
userTerms = 'userTerms',
banner = 'banner',
+ /* Memories */
+ memories = 'memories',
}
export enum MutationKeys {
@@ -70,4 +72,5 @@ export enum MutationKeys {
updateRole = 'updateRole',
enableTwoFactor = 'enableTwoFactor',
verifyTwoFactor = 'verifyTwoFactor',
+ updateMemoryPreferences = 'updateMemoryPreferences',
}
diff --git a/packages/data-provider/src/memory.ts b/packages/data-provider/src/memory.ts
new file mode 100644
index 0000000000..ca52ef5248
--- /dev/null
+++ b/packages/data-provider/src/memory.ts
@@ -0,0 +1,62 @@
+import type { TCustomConfig, TMemoryConfig } from './config';
+
+/**
+ * Loads the memory configuration and validates it
+ * @param config - The memory configuration from librechat.yaml
+ * @returns The validated memory configuration
+ */
+export function loadMemoryConfig(config: TCustomConfig['memory']): TMemoryConfig | undefined {
+ if (!config) {
+ return undefined;
+ }
+
+ // If disabled is explicitly true, return the config as-is
+ if (config.disabled === true) {
+ return config;
+ }
+
+ // Check if the agent configuration is valid
+ const hasValidAgent =
+ config.agent &&
+ (('id' in config.agent && !!config.agent.id) ||
+ ('provider' in config.agent &&
+ 'model' in config.agent &&
+ !!config.agent.provider &&
+ !!config.agent.model));
+
+ // If agent config is invalid, treat as disabled
+ if (!hasValidAgent) {
+ return {
+ ...config,
+ disabled: true,
+ };
+ }
+
+ return config;
+}
+
+/**
+ * Checks if memory feature is enabled based on the configuration
+ * @param config - The memory configuration
+ * @returns True if memory is enabled, false otherwise
+ */
+export function isMemoryEnabled(config: TMemoryConfig | undefined): boolean {
+ if (!config) {
+ return false;
+ }
+
+ if (config.disabled === true) {
+ return false;
+ }
+
+ // Check if agent configuration is valid
+ const hasValidAgent =
+ config.agent &&
+ (('id' in config.agent && !!config.agent.id) ||
+ ('provider' in config.agent &&
+ 'model' in config.agent &&
+ !!config.agent.provider &&
+ !!config.agent.model));
+
+ return !!hasValidAgent;
+}
diff --git a/packages/data-provider/src/parsers.ts b/packages/data-provider/src/parsers.ts
index 16a1b86fc1..1c3eb511d0 100644
--- a/packages/data-provider/src/parsers.ts
+++ b/packages/data-provider/src/parsers.ts
@@ -225,13 +225,15 @@ const extractOmniVersion = (modelStr: string): string => {
export const getResponseSender = (endpointOption: t.TEndpointOption): string => {
const {
model: _m,
- endpoint,
+ endpoint: _e,
endpointType,
modelDisplayLabel: _mdl,
chatGptLabel: _cgl,
modelLabel: _ml,
} = endpointOption;
+ const endpoint = _e as EModelEndpoint;
+
const model = _m ?? '';
const modelDisplayLabel = _mdl ?? '';
const chatGptLabel = _cgl ?? '';
diff --git a/packages/data-provider/src/permissions.ts b/packages/data-provider/src/permissions.ts
index c75f210536..1711c8b4e0 100644
--- a/packages/data-provider/src/permissions.ts
+++ b/packages/data-provider/src/permissions.ts
@@ -16,6 +16,10 @@ export enum PermissionTypes {
* Type for Agent Permissions
*/
AGENTS = 'AGENTS',
+ /**
+ * Type for Memory Permissions
+ */
+ MEMORIES = 'MEMORIES',
/**
* Type for Multi-Conversation Permissions
*/
@@ -45,6 +49,8 @@ export enum Permissions {
READ = 'READ',
READ_AUTHOR = 'READ_AUTHOR',
SHARE = 'SHARE',
+ /** Can disable if desired */
+ OPT_OUT = 'OPT_OUT',
}
export const promptPermissionsSchema = z.object({
@@ -60,6 +66,15 @@ export const bookmarkPermissionsSchema = z.object({
});
export type TBookmarkPermissions = z.infer<typeof bookmarkPermissionsSchema>;
+export const memoryPermissionsSchema = z.object({
+ [Permissions.USE]: z.boolean().default(true),
+ [Permissions.CREATE]: z.boolean().default(true),
+ [Permissions.UPDATE]: z.boolean().default(true),
+ [Permissions.READ]: z.boolean().default(true),
+ [Permissions.OPT_OUT]: z.boolean().default(true),
+});
+export type TMemoryPermissions = z.infer<typeof memoryPermissionsSchema>;
+
export const agentPermissionsSchema = z.object({
[Permissions.SHARED_GLOBAL]: z.boolean().default(false),
[Permissions.USE]: z.boolean().default(true),
@@ -92,6 +107,7 @@ export type TWebSearchPermissions = z.infer<typeof webSearchPermissionsSchema>;
export const permissionsSchema = z.object({
[PermissionTypes.PROMPTS]: promptPermissionsSchema,
[PermissionTypes.BOOKMARKS]: bookmarkPermissionsSchema,
+ [PermissionTypes.MEMORIES]: memoryPermissionsSchema,
[PermissionTypes.AGENTS]: agentPermissionsSchema,
[PermissionTypes.MULTI_CONVO]: multiConvoPermissionsSchema,
[PermissionTypes.TEMPORARY_CHAT]: temporaryChatPermissionsSchema,
diff --git a/packages/data-provider/src/roles.ts b/packages/data-provider/src/roles.ts
index bb7b1b5ff7..5ec82efaa2 100644
--- a/packages/data-provider/src/roles.ts
+++ b/packages/data-provider/src/roles.ts
@@ -5,6 +5,7 @@ import {
permissionsSchema,
agentPermissionsSchema,
promptPermissionsSchema,
+ memoryPermissionsSchema,
runCodePermissionsSchema,
webSearchPermissionsSchema,
bookmarkPermissionsSchema,
@@ -48,6 +49,13 @@ const defaultRolesSchema = z.object({
[PermissionTypes.BOOKMARKS]: bookmarkPermissionsSchema.extend({
[Permissions.USE]: z.boolean().default(true),
}),
+ [PermissionTypes.MEMORIES]: memoryPermissionsSchema.extend({
+ [Permissions.USE]: z.boolean().default(true),
+ [Permissions.CREATE]: z.boolean().default(true),
+ [Permissions.UPDATE]: z.boolean().default(true),
+ [Permissions.READ]: z.boolean().default(true),
+ [Permissions.OPT_OUT]: z.boolean().default(true),
+ }),
[PermissionTypes.AGENTS]: agentPermissionsSchema.extend({
[Permissions.SHARED_GLOBAL]: z.boolean().default(true),
[Permissions.USE]: z.boolean().default(true),
@@ -86,6 +94,13 @@ export const roleDefaults = defaultRolesSchema.parse({
[PermissionTypes.BOOKMARKS]: {
[Permissions.USE]: true,
},
+ [PermissionTypes.MEMORIES]: {
+ [Permissions.USE]: true,
+ [Permissions.CREATE]: true,
+ [Permissions.UPDATE]: true,
+ [Permissions.READ]: true,
+ [Permissions.OPT_OUT]: true,
+ },
[PermissionTypes.AGENTS]: {
[Permissions.SHARED_GLOBAL]: true,
[Permissions.USE]: true,
@@ -110,6 +125,7 @@ export const roleDefaults = defaultRolesSchema.parse({
permissions: {
[PermissionTypes.PROMPTS]: {},
[PermissionTypes.BOOKMARKS]: {},
+ [PermissionTypes.MEMORIES]: {},
[PermissionTypes.AGENTS]: {},
[PermissionTypes.MULTI_CONVO]: {},
[PermissionTypes.TEMPORARY_CHAT]: {},
diff --git a/packages/data-provider/src/schemas.ts b/packages/data-provider/src/schemas.ts
index 96116c1f5b..149b10062c 100644
--- a/packages/data-provider/src/schemas.ts
+++ b/packages/data-provider/src/schemas.ts
@@ -522,11 +522,19 @@ export const tMessageSchema = z.object({
feedback: feedbackSchema.optional(),
});
+export type MemoryArtifact = {
+ key: string;
+ value?: string;
+ tokenCount?: number;
+ type: 'update' | 'delete';
+};
+
export type TAttachmentMetadata = {
type?: Tools;
messageId: string;
toolCallId: string;
[Tools.web_search]?: SearchResultData;
+ [Tools.memory]?: MemoryArtifact;
};
export type TAttachment =
diff --git a/packages/data-provider/src/types.ts b/packages/data-provider/src/types.ts
index 706d2a2e8b..275c405c1a 100644
--- a/packages/data-provider/src/types.ts
+++ b/packages/data-provider/src/types.ts
@@ -1,17 +1,18 @@
import type OpenAI from 'openai';
import type { InfiniteData } from '@tanstack/react-query';
import type {
+ TBanner,
TMessage,
TResPlugin,
- ImageDetail,
TSharedLink,
TConversation,
EModelEndpoint,
TConversationTag,
- TBanner,
+ TAttachment,
} from './schemas';
-import { TMinimalFeedback } from './feedback';
-import { SettingDefinition } from './generate';
+import type { SettingDefinition } from './generate';
+import type { TMinimalFeedback } from './feedback';
+import type { Agent } from './types/assistants';
export type TOpenAIMessage = OpenAI.Chat.ChatCompletionMessageParam;
@@ -20,28 +21,78 @@ export * from './schemas';
export type TMessages = TMessage[];
/* TODO: Cleanup EndpointOption types */
-export type TEndpointOption = {
- spec?: string | null;
- iconURL?: string | null;
- endpoint: EModelEndpoint;
- endpointType?: EModelEndpoint;
+export type TEndpointOption = Pick<
+ TConversation,
+ // Core conversation fields
+ | 'endpoint'
+ | 'endpointType'
+ | 'model'
+ | 'modelLabel'
+ | 'chatGptLabel'
+ | 'promptPrefix'
+ | 'temperature'
+ | 'topP'
+ | 'topK'
+ | 'top_p'
+ | 'frequency_penalty'
+ | 'presence_penalty'
+ | 'maxOutputTokens'
+ | 'maxContextTokens'
+ | 'max_tokens'
+ | 'maxTokens'
+ | 'resendFiles'
+ | 'imageDetail'
+ | 'reasoning_effort'
+ | 'instructions'
+ | 'additional_instructions'
+ | 'append_current_datetime'
+ | 'tools'
+ | 'stop'
+ | 'region'
+ | 'additionalModelRequestFields'
+ // Anthropic-specific
+ | 'promptCache'
+ | 'thinking'
+ | 'thinkingBudget'
+ // Assistant/Agent fields
+ | 'assistant_id'
+ | 'agent_id'
+ // UI/Display fields
+ | 'iconURL'
+ | 'greeting'
+ | 'spec'
+ // Artifacts
+ | 'artifacts'
+ // Files
+ | 'file_ids'
+ // System field
+ | 'system'
+ // Google examples
+ | 'examples'
+ // Context
+ | 'context'
+> & {
+ // Fields specific to endpoint options that don't exist on TConversation
modelDisplayLabel?: string;
- resendFiles?: boolean;
- promptCache?: boolean;
- maxContextTokens?: number;
- imageDetail?: ImageDetail;
- model?: string | null;
- promptPrefix?: string;
- temperature?: number;
- chatGptLabel?: string | null;
- modelLabel?: string | null;
- jailbreak?: boolean;
key?: string | null;
- /* assistant */
+ /** @deprecated Assistants API */
thread_id?: string;
- /* multi-response stream */
+ // Conversation identifiers for multi-response streams
overrideConvoId?: string;
overrideUserMessageId?: string;
+ // Model parameters (used by different endpoints)
+ modelOptions?: Record<string, unknown>;
+ model_parameters?: Record<string, unknown>;
+ // Configuration data (added by middleware)
+ modelsConfig?: TModelsConfig;
+ // File attachments (processed by middleware)
+ attachments?: TAttachment[];
+ // Generated prompts
+ artifactsPrompt?: string;
+ // Agent-specific fields
+ agent?: Promise<Agent>;
+ // Client-specific options
+ clientOptions?: Record<string, unknown>;
};
export type TEphemeralAgent = {
@@ -130,6 +181,9 @@ export type TUser = {
plugins?: string[];
twoFactorEnabled?: boolean;
backupCodes?: TBackupCode[];
+ personalization?: {
+ memories?: boolean;
+ };
createdAt: string;
updatedAt: string;
};
@@ -557,7 +611,7 @@ export type TUpdateFeedbackResponse = {
messageId: string;
conversationId: string;
feedback?: TMinimalFeedback;
-}
+};
export type TBalanceResponse = {
tokenCredits: number;
diff --git a/packages/data-provider/src/types/assistants.ts b/packages/data-provider/src/types/assistants.ts
index 963d3396e2..cbcc51687b 100644
--- a/packages/data-provider/src/types/assistants.ts
+++ b/packages/data-provider/src/types/assistants.ts
@@ -22,6 +22,7 @@ export enum Tools {
web_search = 'web_search',
retrieval = 'retrieval',
function = 'function',
+ memory = 'memory',
}
export enum EToolResources {
diff --git a/packages/data-provider/src/types/mutations.ts b/packages/data-provider/src/types/mutations.ts
index 7193e1d355..4e1f01debe 100644
--- a/packages/data-provider/src/types/mutations.ts
+++ b/packages/data-provider/src/types/mutations.ts
@@ -278,7 +278,7 @@ export type UpdatePermVars<T> = {
};
export type UpdatePromptPermVars = UpdatePermVars<p.TPromptPermissions>;
-
+export type UpdateMemoryPermVars = UpdatePermVars<p.TMemoryPermissions>;
export type UpdateAgentPermVars = UpdatePermVars<p.TAgentPermissions>;
export type UpdatePermResponse = r.TRole;
@@ -290,6 +290,13 @@ export type UpdatePromptPermOptions = MutationOptions<
types.TError | null | undefined
>;
+export type UpdateMemoryPermOptions = MutationOptions<
+ UpdatePermResponse,
+ UpdateMemoryPermVars,
+ unknown,
+ types.TError | null | undefined
+>;
+
export type UpdateAgentPermOptions = MutationOptions<
UpdatePermResponse,
UpdateAgentPermVars,
diff --git a/packages/data-provider/src/types/queries.ts b/packages/data-provider/src/types/queries.ts
index d396c1fff8..cd792e4bda 100644
--- a/packages/data-provider/src/types/queries.ts
+++ b/packages/data-provider/src/types/queries.ts
@@ -109,3 +109,18 @@ export type VerifyToolAuthResponse = {
export type GetToolCallParams = { conversationId: string };
export type ToolCallResults = a.ToolCallResult[];
+
+/* Memories */
+export type TUserMemory = {
+ key: string;
+ value: string;
+ updated_at: string;
+ tokenCount?: number;
+};
+
+export type MemoriesResponse = {
+ memories: TUserMemory[];
+ totalTokens: number;
+ tokenLimit: number | null;
+ usagePercentage: number | null;
+};
diff --git a/packages/data-provider/src/web.ts b/packages/data-provider/src/web.ts
index 103d906653..0b0ab63c86 100644
--- a/packages/data-provider/src/web.ts
+++ b/packages/data-provider/src/web.ts
@@ -5,8 +5,8 @@ import type {
SearchProviders,
TWebSearchConfig,
} from './config';
-import { extractVariableName } from './utils';
import { SearchCategories, SafeSearchTypes } from './config';
+import { extractVariableName } from './utils';
import { AuthType } from './schemas';
export function loadWebSearchConfig(
@@ -64,23 +64,29 @@ export const webSearchAuth = {
/**
* Extracts all API keys from the webSearchAuth configuration object
*/
-export const webSearchKeys: TWebSearchKeys[] = [];
+export function getWebSearchKeys(): TWebSearchKeys[] {
+ const keys: TWebSearchKeys[] = [];
-// Iterate through each category (providers, scrapers, rerankers)
-for (const category of Object.keys(webSearchAuth)) {
- const categoryObj = webSearchAuth[category as TWebSearchCategories];
+ // Iterate through each category (providers, scrapers, rerankers)
+ for (const category of Object.keys(webSearchAuth)) {
+ const categoryObj = webSearchAuth[category as TWebSearchCategories];
- // Iterate through each service within the category
- for (const service of Object.keys(categoryObj)) {
- const serviceObj = categoryObj[service as keyof typeof categoryObj];
+ // Iterate through each service within the category
+ for (const service of Object.keys(categoryObj)) {
+ const serviceObj = categoryObj[service as keyof typeof categoryObj];
- // Extract the API keys from the service
- for (const key of Object.keys(serviceObj)) {
- webSearchKeys.push(key as TWebSearchKeys);
+ // Extract the API keys from the service
+ for (const key of Object.keys(serviceObj)) {
+ keys.push(key as TWebSearchKeys);
+ }
}
}
+
+ return keys;
}
+export const webSearchKeys: TWebSearchKeys[] = getWebSearchKeys();
+
export function extractWebSearchEnvVars({
keys,
config,
diff --git a/packages/data-schemas/README.md b/packages/data-schemas/README.md
deleted file mode 100644
index f9362e8238..0000000000
--- a/packages/data-schemas/README.md
+++ /dev/null
@@ -1,114 +0,0 @@
-# `@librechat/data-schemas`
-
-Mongoose schemas and models for LibreChat. This package provides a comprehensive collection of Mongoose schemas used across the LibreChat project, enabling robust data modeling and validation for various entities such as actions, agents, messages, users, and more.
-
-
-## Features
-
-- **Modular Schemas:** Includes schemas for actions, agents, assistants, balance, banners, categories, conversation tags, conversations, files, keys, messages, plugin authentication, presets, projects, prompts, prompt groups, roles, sessions, shared links, tokens, tool calls, transactions, and users.
-- **TypeScript Support:** Provides TypeScript definitions for type-safe development.
-- **Ready for Mongoose Integration:** Easily integrate with Mongoose to create models and interact with your MongoDB database.
-- **Flexible & Extensible:** Designed to support the evolving needs of LibreChat while being adaptable to other projects.
-
-
-## Installation
-
-Install the package via npm or yarn:
-
-```bash
-npm install @librechat/data-schemas
-```
-
-Or with yarn:
-
-```bash
-yarn add @librechat/data-schemas
-```
-
-
-## Usage
-
-After installation, you can import and use the schemas in your project. For example, to create a Mongoose model for a user:
-
-```js
-import mongoose from 'mongoose';
-import { userSchema } from '@librechat/data-schemas';
-
-const UserModel = mongoose.model('User', userSchema);
-
-// Now you can use UserModel to create, read, update, and delete user documents.
-```
-
-You can also import other schemas as needed:
-
-```js
-import { actionSchema, agentSchema, messageSchema } from '@librechat/data-schemas';
-```
-
-Each schema is designed to integrate seamlessly with Mongoose and provides indexes, timestamps, and validations tailored for LibreChat’s use cases.
-
-
-## Development
-
-This package uses Rollup and TypeScript for building and bundling.
-
-### Available Scripts
-
-- **Build:**
- Cleans the `dist` directory and builds the package.
- ```bash
- npm run build
- ```
-
-- **Build Watch:**
- Rebuilds automatically on file changes.
- ```bash
- npm run build:watch
- ```
-
-- **Test:**
- Runs tests with coverage in watch mode.
- ```bash
- npm run test
- ```
-
-- **Test (CI):**
- Runs tests with coverage for CI environments.
- ```bash
- npm run test:ci
- ```
-
-- **Verify:**
- Runs tests in CI mode to verify code integrity.
- ```bash
- npm run verify
- ```
-
-- **Clean:**
- Removes the `dist` directory.
- ```bash
- npm run clean
- ```
-
-For those using Bun, equivalent scripts are available:
-- **Bun Clean:** `bun run b:clean`
-- **Bun Build:** `bun run b:build`
-
-
-## Repository & Issues
-
-The source code is maintained on GitHub.
-- **Repository:** [LibreChat Repository](https://github.com/danny-avila/LibreChat.git)
-- **Issues & Bug Reports:** [LibreChat Issues](https://github.com/danny-avila/LibreChat/issues)
-
-
-## License
-
-This project is licensed under the [MIT License](LICENSE).
-
-
-## Contributing
-
-Contributions to improve and expand the data schemas are welcome. If you have suggestions, improvements, or bug fixes, please open an issue or submit a pull request on the [GitHub repository](https://github.com/danny-avila/LibreChat/issues).
-
-For more detailed documentation on each schema and model, please refer to the source code or visit the [LibreChat website](https://librechat.ai).
diff --git a/packages/data-schemas/src/index.ts b/packages/data-schemas/src/index.ts
index a5a4ec59ed..d445eecb47 100644
--- a/packages/data-schemas/src/index.ts
+++ b/packages/data-schemas/src/index.ts
@@ -3,5 +3,6 @@ export * from './schema';
export { createModels } from './models';
export { createMethods } from './methods';
export type * from './types';
+export type * from './methods';
export { default as logger } from './config/winston';
export { default as meiliLogger } from './config/meiliLogger';
diff --git a/packages/data-schemas/src/methods/index.ts b/packages/data-schemas/src/methods/index.ts
index 324159b0ae..33dcdc918d 100644
--- a/packages/data-schemas/src/methods/index.ts
+++ b/packages/data-schemas/src/methods/index.ts
@@ -2,6 +2,8 @@ import { createUserMethods, type UserMethods } from './user';
import { createSessionMethods, type SessionMethods } from './session';
import { createTokenMethods, type TokenMethods } from './token';
import { createRoleMethods, type RoleMethods } from './role';
+/* Memories */
+import { createMemoryMethods, type MemoryMethods } from './memory';
/**
* Creates all database methods for all collections
@@ -12,7 +14,9 @@ export function createMethods(mongoose: typeof import('mongoose')) {
...createSessionMethods(mongoose),
...createTokenMethods(mongoose),
...createRoleMethods(mongoose),
+ ...createMemoryMethods(mongoose),
};
}
-export type AllMethods = UserMethods & SessionMethods & TokenMethods & RoleMethods;
+export type { MemoryMethods };
+export type AllMethods = UserMethods & SessionMethods & TokenMethods & RoleMethods & MemoryMethods;
diff --git a/packages/data-schemas/src/methods/memory.ts b/packages/data-schemas/src/methods/memory.ts
new file mode 100644
index 0000000000..021354eab2
--- /dev/null
+++ b/packages/data-schemas/src/methods/memory.ts
@@ -0,0 +1,168 @@
+import { Types } from 'mongoose';
+import logger from '~/config/winston';
+import type * as t from '~/types';
+
+/**
+ * Formats a date in YYYY-MM-DD format
+ */
+const formatDate = (date: Date): string => {
+ return date.toISOString().split('T')[0];
+};
+
+// Factory function that takes mongoose instance and returns the methods
+export function createMemoryMethods(mongoose: typeof import('mongoose')) {
+ const MemoryEntry = mongoose.models.MemoryEntry;
+
+ /**
+ * Creates a new memory entry for a user
+ * Throws an error if a memory with the same key already exists
+ */
+ async function createMemory({
+ userId,
+ key,
+ value,
+ tokenCount = 0,
+ }: t.SetMemoryParams): Promise<t.MemoryResult> {
+ try {
+ if (key?.toLowerCase() === 'nothing') {
+ return { ok: false };
+ }
+
+ const existingMemory = await MemoryEntry.findOne({ userId, key });
+ if (existingMemory) {
+ throw new Error('Memory with this key already exists');
+ }
+
+ await MemoryEntry.create({
+ userId,
+ key,
+ value,
+ tokenCount,
+ updated_at: new Date(),
+ });
+
+ return { ok: true };
+ } catch (error) {
+ throw new Error(
+ `Failed to create memory: ${error instanceof Error ? error.message : 'Unknown error'}`,
+ );
+ }
+ }
+
+ /**
+ * Sets or updates a memory entry for a user
+ */
+ async function setMemory({
+ userId,
+ key,
+ value,
+ tokenCount = 0,
+ }: t.SetMemoryParams): Promise<t.MemoryResult> {
+ try {
+ if (key?.toLowerCase() === 'nothing') {
+ return { ok: false };
+ }
+
+ await MemoryEntry.findOneAndUpdate(
+ { userId, key },
+ {
+ value,
+ tokenCount,
+ updated_at: new Date(),
+ },
+ {
+ upsert: true,
+ new: true,
+ },
+ );
+
+ return { ok: true };
+ } catch (error) {
+ throw new Error(
+ `Failed to set memory: ${error instanceof Error ? error.message : 'Unknown error'}`,
+ );
+ }
+ }
+
+ /**
+ * Deletes a specific memory entry for a user
+ */
+ async function deleteMemory({ userId, key }: t.DeleteMemoryParams): Promise<t.MemoryResult> {
+ try {
+ const result = await MemoryEntry.findOneAndDelete({ userId, key });
+ return { ok: !!result };
+ } catch (error) {
+ throw new Error(
+ `Failed to delete memory: ${error instanceof Error ? error.message : 'Unknown error'}`,
+ );
+ }
+ }
+
+ /**
+ * Gets all memory entries for a user
+ */
+ async function getAllUserMemories(
+ userId: string | Types.ObjectId,
+ ): Promise<t.IMemoryEntryLean[]> {
+ try {
+ return (await MemoryEntry.find({ userId }).lean()) as t.IMemoryEntryLean[];
+ } catch (error) {
+ throw new Error(
+ `Failed to get all memories: ${error instanceof Error ? error.message : 'Unknown error'}`,
+ );
+ }
+ }
+
+ /**
+ * Gets and formats all memories for a user in two different formats
+ */
+ async function getFormattedMemories({
+ userId,
+ }: t.GetFormattedMemoriesParams): Promise<t.FormattedMemoriesResult> {
+ try {
+ const memories = await getAllUserMemories(userId);
+
+ if (!memories || memories.length === 0) {
+ return { withKeys: '', withoutKeys: '', totalTokens: 0 };
+ }
+
+ const sortedMemories = memories.sort(
+ (a, b) => new Date(a.updated_at!).getTime() - new Date(b.updated_at!).getTime(),
+ );
+
+ const totalTokens = sortedMemories.reduce((sum, memory) => {
+ return sum + (memory.tokenCount || 0);
+ }, 0);
+
+ const withKeys = sortedMemories
+ .map((memory, index) => {
+ const date = formatDate(new Date(memory.updated_at!));
+ const tokenInfo = memory.tokenCount ? ` [${memory.tokenCount} tokens]` : '';
+ return `${index + 1}. [${date}]. ["key": "${memory.key}"]${tokenInfo}. ["value": "${memory.value}"]`;
+ })
+ .join('\n\n');
+
+ const withoutKeys = sortedMemories
+ .map((memory, index) => {
+ const date = formatDate(new Date(memory.updated_at!));
+ return `${index + 1}. [${date}]. ${memory.value}`;
+ })
+ .join('\n\n');
+
+ return { withKeys, withoutKeys, totalTokens };
+ } catch (error) {
+ logger.error('Failed to get formatted memories:', error);
+ return { withKeys: '', withoutKeys: '', totalTokens: 0 };
+ }
+ }
+
+ return {
+ setMemory,
+ createMemory,
+ deleteMemory,
+ getAllUserMemories,
+ getFormattedMemories,
+ };
+}
+
+export type MemoryMethods = ReturnType<typeof createMemoryMethods>;
diff --git a/packages/data-schemas/src/methods/user.ts b/packages/data-schemas/src/methods/user.ts
index b3460faa72..f29486e0e9 100644
--- a/packages/data-schemas/src/methods/user.ts
+++ b/packages/data-schemas/src/methods/user.ts
@@ -170,6 +170,35 @@ export function createUserMethods(mongoose: typeof import('mongoose')) {
});
}
+ /**
+ * Update a user's personalization memories setting.
+ * Handles the edge case where the personalization object doesn't exist.
+ */
+ async function toggleUserMemories(
+ userId: string,
+ memoriesEnabled: boolean,
+ ): Promise<IUser | null> {
+ const User = mongoose.models.User;
+
+ // First, ensure the personalization object exists
+ const user = await User.findById(userId);
+ if (!user) {
+ return null;
+ }
+
+ // Use $set to update the nested field, which will create the personalization object if it doesn't exist
+ const updateOperation = {
+ $set: {
+ 'personalization.memories': memoriesEnabled,
+ },
+ };
+
+ return (await User.findByIdAndUpdate(userId, updateOperation, {
+ new: true,
+ runValidators: true,
+ }).lean()) as IUser | null;
+ }
+
// Return all methods
return {
findUser,
@@ -179,6 +208,7 @@ export function createUserMethods(mongoose: typeof import('mongoose')) {
getUserById,
deleteUserById,
generateToken,
+ toggleUserMemories,
};
}
diff --git a/packages/data-schemas/src/models/index.ts b/packages/data-schemas/src/models/index.ts
index 5be6a67d0e..a68a25bcfb 100644
--- a/packages/data-schemas/src/models/index.ts
+++ b/packages/data-schemas/src/models/index.ts
@@ -20,6 +20,7 @@ import { createPromptGroupModel } from './promptGroup';
import { createConversationTagModel } from './conversationTag';
import { createSharedLinkModel } from './sharedLink';
import { createToolCallModel } from './toolCall';
+import { createMemoryModel } from './memory';
/**
* Creates all database models for all collections
@@ -48,5 +49,6 @@ export function createModels(mongoose: typeof import('mongoose')) {
ConversationTag: createConversationTagModel(mongoose),
SharedLink: createSharedLinkModel(mongoose),
ToolCall: createToolCallModel(mongoose),
+ MemoryEntry: createMemoryModel(mongoose),
};
}
diff --git a/packages/data-schemas/src/models/memory.ts b/packages/data-schemas/src/models/memory.ts
new file mode 100644
index 0000000000..fb970c04ce
--- /dev/null
+++ b/packages/data-schemas/src/models/memory.ts
@@ -0,0 +1,6 @@
+import memorySchema from '~/schema/memory';
+import type { IMemoryEntry } from '~/types/memory';
+
+export function createMemoryModel(mongoose: typeof import('mongoose')) {
+ return mongoose.models.MemoryEntry || mongoose.model<IMemoryEntry>('MemoryEntry', memorySchema);
+}
diff --git a/packages/data-schemas/src/models/plugins/mongoMeili.ts b/packages/data-schemas/src/models/plugins/mongoMeili.ts
index 1956be9f30..77597cdf63 100644
--- a/packages/data-schemas/src/models/plugins/mongoMeili.ts
+++ b/packages/data-schemas/src/models/plugins/mongoMeili.ts
@@ -1,6 +1,14 @@
import _ from 'lodash';
import { MeiliSearch, Index } from 'meilisearch';
-import type { FilterQuery, Types, Schema, Document, Model, Query } from 'mongoose';
+import type {
+ CallbackWithoutResultAndOptionalError,
+ FilterQuery,
+ Document,
+ Schema,
+ Query,
+ Types,
+ Model,
+} from 'mongoose';
import logger from '~/config/meiliLogger';
interface MongoMeiliOptions {
@@ -24,12 +32,12 @@ interface ContentItem {
interface DocumentWithMeiliIndex extends Document {
_meiliIndex?: boolean;
preprocessObjectForIndex?: () => Record<string, unknown>;
- addObjectToMeili?: () => Promise<void>;
- updateObjectToMeili?: () => Promise<void>;
- deleteObjectFromMeili?: () => Promise<void>;
- postSaveHook?: () => void;
- postUpdateHook?: () => void;
- postRemoveHook?: () => void;
+ addObjectToMeili?: (next: CallbackWithoutResultAndOptionalError) => Promise<void>;
+ updateObjectToMeili?: (next: CallbackWithoutResultAndOptionalError) => Promise<void>;
+ deleteObjectFromMeili?: (next: CallbackWithoutResultAndOptionalError) => Promise<void>;
+ postSaveHook?: (next: CallbackWithoutResultAndOptionalError) => void;
+ postUpdateHook?: (next: CallbackWithoutResultAndOptionalError) => void;
+ postRemoveHook?: (next: CallbackWithoutResultAndOptionalError) => void;
conversationId?: string;
content?: ContentItem[];
messageId?: string;
@@ -220,7 +228,7 @@ const createMeiliMongooseModel = ({
);
}
} catch (error) {
- logger.error('[syncWithMeili] Error adding document to Meili', error);
+ logger.error('[syncWithMeili] Error adding document to Meili:', error);
}
}
@@ -306,28 +314,48 @@ const createMeiliMongooseModel = ({
/**
* Adds the current document to the MeiliSearch index
*/
- async addObjectToMeili(this: DocumentWithMeiliIndex): Promise<void> {
+ async addObjectToMeili(
+ this: DocumentWithMeiliIndex,
+ next: CallbackWithoutResultAndOptionalError,
+ ): Promise<void> {
const object = this.preprocessObjectForIndex!();
try {
await index.addDocuments([object]);
} catch (error) {
- logger.error('[addObjectToMeili] Error adding document to Meili', error);
+ logger.error('[addObjectToMeili] Error adding document to Meili:', error);
+ return next();
}
- await this.collection.updateMany(
- { _id: this._id as Types.ObjectId },
- { $set: { _meiliIndex: true } },
- );
+ try {
+ await this.collection.updateMany(
+ { _id: this._id as Types.ObjectId },
+ { $set: { _meiliIndex: true } },
+ );
+ } catch (error) {
+ logger.error('[addObjectToMeili] Error updating _meiliIndex field:', error);
+ return next();
+ }
+
+ next();
}
/**
* Updates the current document in the MeiliSearch index
*/
- async updateObjectToMeili(this: DocumentWithMeiliIndex): Promise<void> {
- const object = _.omitBy(_.pick(this.toJSON(), attributesToIndex), (v, k) =>
- k.startsWith('$'),
- );
- await index.updateDocuments([object]);
+ async updateObjectToMeili(
+ this: DocumentWithMeiliIndex,
+ next: CallbackWithoutResultAndOptionalError,
+ ): Promise<void> {
+ try {
+ const object = _.omitBy(_.pick(this.toJSON(), attributesToIndex), (v, k) =>
+ k.startsWith('$'),
+ );
+ await index.updateDocuments([object]);
+ next();
+ } catch (error) {
+ logger.error('[updateObjectToMeili] Error updating document in Meili:', error);
+ return next();
+ }
}
/**
@@ -335,8 +363,17 @@ const createMeiliMongooseModel = ({
*
* @returns {Promise<void>}
*/
- async deleteObjectFromMeili(this: DocumentWithMeiliIndex): Promise<void> {
- await index.deleteDocument(this._id as string);
+ async deleteObjectFromMeili(
+ this: DocumentWithMeiliIndex,
+ next: CallbackWithoutResultAndOptionalError,
+ ): Promise<void> {
+ try {
+ await index.deleteDocument(this._id as string);
+ next();
+ } catch (error) {
+ logger.error('[deleteObjectFromMeili] Error deleting document from Meili:', error);
+ return next();
+ }
}
/**
@@ -345,11 +382,11 @@ const createMeiliMongooseModel = ({
* If the document is already indexed (i.e. `_meiliIndex` is true), it updates it;
* otherwise, it adds the document to the index.
*/
- postSaveHook(this: DocumentWithMeiliIndex): void {
+ postSaveHook(this: DocumentWithMeiliIndex, next: CallbackWithoutResultAndOptionalError): void {
if (this._meiliIndex) {
- this.updateObjectToMeili!();
+ this.updateObjectToMeili!(next);
} else {
- this.addObjectToMeili!();
+ this.addObjectToMeili!(next);
}
}
@@ -359,9 +396,14 @@ const createMeiliMongooseModel = ({
* This hook is triggered after a document update, ensuring that changes are
* propagated to the MeiliSearch index if the document is indexed.
*/
- postUpdateHook(this: DocumentWithMeiliIndex): void {
+ postUpdateHook(
+ this: DocumentWithMeiliIndex,
+ next: CallbackWithoutResultAndOptionalError,
+ ): void {
if (this._meiliIndex) {
- this.updateObjectToMeili!();
+ this.updateObjectToMeili!(next);
+ } else {
+ next();
}
}
@@ -371,9 +413,14 @@ const createMeiliMongooseModel = ({
* This hook is triggered after a document is removed, ensuring that the document
* is also removed from the MeiliSearch index if it was previously indexed.
*/
- postRemoveHook(this: DocumentWithMeiliIndex): void {
+ postRemoveHook(
+ this: DocumentWithMeiliIndex,
+ next: CallbackWithoutResultAndOptionalError,
+ ): void {
if (this._meiliIndex) {
- this.deleteObjectFromMeili!();
+ this.deleteObjectFromMeili!(next);
+ } else {
+ next();
}
}
}
@@ -429,16 +476,16 @@ export default function mongoMeili(schema: Schema, options: MongoMeiliOptions):
schema.loadClass(createMeiliMongooseModel({ index, attributesToIndex }));
// Register Mongoose hooks
- schema.post('save', function (doc: DocumentWithMeiliIndex) {
- doc.postSaveHook?.();
+ schema.post('save', function (doc: DocumentWithMeiliIndex, next) {
+ doc.postSaveHook?.(next);
});
- schema.post('updateOne', function (doc: DocumentWithMeiliIndex) {
- doc.postUpdateHook?.();
+ schema.post('updateOne', function (doc: DocumentWithMeiliIndex, next) {
+ doc.postUpdateHook?.(next);
});
- schema.post('deleteOne', function (doc: DocumentWithMeiliIndex) {
- doc.postRemoveHook?.();
+ schema.post('deleteOne', function (doc: DocumentWithMeiliIndex, next) {
+ doc.postRemoveHook?.(next);
});
// Pre-deleteMany hook: remove corresponding documents from MeiliSearch when multiple documents are deleted.
@@ -486,13 +533,13 @@ export default function mongoMeili(schema: Schema, options: MongoMeiliOptions):
});
// Post-findOneAndUpdate hook
- schema.post('findOneAndUpdate', async function (doc: DocumentWithMeiliIndex) {
+ schema.post('findOneAndUpdate', async function (doc: DocumentWithMeiliIndex, next) {
if (!meiliEnabled) {
- return;
+ return next();
}
if (doc.unfinished) {
- return;
+ return next();
}
let meiliDoc: Record<string, unknown> | undefined;
@@ -509,9 +556,9 @@ export default function mongoMeili(schema: Schema, options: MongoMeiliOptions):
}
if (meiliDoc && meiliDoc.title === doc.title) {
- return;
+ return next();
}
- doc.postSaveHook?.();
+ doc.postSaveHook?.(next);
});
}
diff --git a/packages/data-schemas/src/schema/index.ts b/packages/data-schemas/src/schema/index.ts
index 8a69a0cb2c..e95f560b23 100644
--- a/packages/data-schemas/src/schema/index.ts
+++ b/packages/data-schemas/src/schema/index.ts
@@ -21,3 +21,4 @@ export { default as tokenSchema } from './token';
export { default as toolCallSchema } from './toolCall';
export { default as transactionSchema } from './transaction';
export { default as userSchema } from './user';
+export { default as memorySchema } from './memory';
diff --git a/packages/data-schemas/src/schema/memory.ts b/packages/data-schemas/src/schema/memory.ts
new file mode 100644
index 0000000000..b6eadf04a7
--- /dev/null
+++ b/packages/data-schemas/src/schema/memory.ts
@@ -0,0 +1,33 @@
+import { Schema } from 'mongoose';
+import type { IMemoryEntry } from '~/types/memory';
+
+const MemoryEntrySchema: Schema<IMemoryEntry> = new Schema({
+ userId: {
+ type: Schema.Types.ObjectId,
+ ref: 'User',
+ index: true,
+ required: true,
+ },
+ key: {
+ type: String,
+ required: true,
+ validate: {
+ validator: (v: string) => /^[a-z_]+$/.test(v),
+ message: 'Key must only contain lowercase letters and underscores',
+ },
+ },
+ value: {
+ type: String,
+ required: true,
+ },
+ tokenCount: {
+ type: Number,
+ default: 0,
+ },
+ updated_at: {
+ type: Date,
+ default: Date.now,
+ },
+});
+
+export default MemoryEntrySchema;
diff --git a/packages/data-schemas/src/schema/role.ts b/packages/data-schemas/src/schema/role.ts
index bd20fa4ca0..1f6b5f5d57 100644
--- a/packages/data-schemas/src/schema/role.ts
+++ b/packages/data-schemas/src/schema/role.ts
@@ -13,6 +13,13 @@ const rolePermissionsSchema = new Schema(
[Permissions.USE]: { type: Boolean, default: true },
[Permissions.CREATE]: { type: Boolean, default: true },
},
+ [PermissionTypes.MEMORIES]: {
+ [Permissions.USE]: { type: Boolean, default: true },
+ [Permissions.CREATE]: { type: Boolean, default: true },
+ [Permissions.UPDATE]: { type: Boolean, default: true },
+ [Permissions.READ]: { type: Boolean, default: true },
+ [Permissions.OPT_OUT]: { type: Boolean, default: true },
+ },
[PermissionTypes.AGENTS]: {
[Permissions.SHARED_GLOBAL]: { type: Boolean, default: false },
[Permissions.USE]: { type: Boolean, default: true },
@@ -45,6 +52,12 @@ const roleSchema: Schema<IRole> = new Schema({
[Permissions.USE]: true,
[Permissions.CREATE]: true,
},
+ [PermissionTypes.MEMORIES]: {
+ [Permissions.USE]: true,
+ [Permissions.CREATE]: true,
+ [Permissions.UPDATE]: true,
+ [Permissions.READ]: true,
+ },
[PermissionTypes.AGENTS]: {
[Permissions.SHARED_GLOBAL]: false,
[Permissions.USE]: true,
diff --git a/packages/data-schemas/src/schema/user.ts b/packages/data-schemas/src/schema/user.ts
index 6107488ee8..cc8748ac05 100644
--- a/packages/data-schemas/src/schema/user.ts
+++ b/packages/data-schemas/src/schema/user.ts
@@ -129,6 +129,15 @@ const userSchema = new Schema<IUser>(
type: Boolean,
default: false,
},
+ personalization: {
+ type: {
+ memories: {
+ type: Boolean,
+ default: true,
+ },
+ },
+ default: {},
+ },
},
{ timestamps: true },
);
diff --git a/packages/data-schemas/src/types/index.ts b/packages/data-schemas/src/types/index.ts
index 27914d2721..7b7037c8ed 100644
--- a/packages/data-schemas/src/types/index.ts
+++ b/packages/data-schemas/src/types/index.ts
@@ -1,3 +1,6 @@
+import type { Types } from 'mongoose';
+
+export type ObjectId = Types.ObjectId;
export * from './user';
export * from './token';
export * from './convo';
@@ -10,3 +13,5 @@ export * from './role';
export * from './action';
export * from './assistant';
export * from './file';
+/* Memories */
+export * from './memory';
diff --git a/packages/data-schemas/src/types/memory.ts b/packages/data-schemas/src/types/memory.ts
new file mode 100644
index 0000000000..6ab6c29345
--- /dev/null
+++ b/packages/data-schemas/src/types/memory.ts
@@ -0,0 +1,48 @@
+import type { Types, Document } from 'mongoose';
+
+// Base memory interfaces
+export interface IMemoryEntry extends Document {
+ userId: Types.ObjectId;
+ key: string;
+ value: string;
+ tokenCount?: number;
+ updated_at?: Date;
+}
+
+export interface IMemoryEntryLean {
+ _id: Types.ObjectId;
+ userId: Types.ObjectId;
+ key: string;
+ value: string;
+ tokenCount?: number;
+ updated_at?: Date;
+ __v?: number;
+}
+
+// Method parameter interfaces
+export interface SetMemoryParams {
+ userId: string | Types.ObjectId;
+ key: string;
+ value: string;
+ tokenCount?: number;
+}
+
+export interface DeleteMemoryParams {
+ userId: string | Types.ObjectId;
+ key: string;
+}
+
+export interface GetFormattedMemoriesParams {
+ userId: string | Types.ObjectId;
+}
+
+// Result interfaces
+export interface MemoryResult {
+ ok: boolean;
+}
+
+export interface FormattedMemoriesResult {
+ withKeys: string;
+ withoutKeys: string;
+ totalTokens?: number;
+}
diff --git a/packages/data-schemas/src/types/role.ts b/packages/data-schemas/src/types/role.ts
index 773a1a3929..590da9432e 100644
--- a/packages/data-schemas/src/types/role.ts
+++ b/packages/data-schemas/src/types/role.ts
@@ -12,6 +12,12 @@ export interface IRole extends Document {
[Permissions.USE]?: boolean;
[Permissions.CREATE]?: boolean;
};
+ [PermissionTypes.MEMORIES]?: {
+ [Permissions.USE]?: boolean;
+ [Permissions.CREATE]?: boolean;
+ [Permissions.UPDATE]?: boolean;
+ [Permissions.READ]?: boolean;
+ };
[PermissionTypes.AGENTS]?: {
[Permissions.SHARED_GLOBAL]?: boolean;
[Permissions.USE]?: boolean;
diff --git a/packages/data-schemas/src/types/user.ts b/packages/data-schemas/src/types/user.ts
index 206d051819..9d2c3fc735 100644
--- a/packages/data-schemas/src/types/user.ts
+++ b/packages/data-schemas/src/types/user.ts
@@ -30,6 +30,9 @@ export interface IUser extends Document {
}>;
expiresAt?: Date;
termsAccepted?: boolean;
+ personalization?: {
+ memories?: boolean;
+ };
createdAt?: Date;
updatedAt?: Date;
}
diff --git a/packages/mcp/src/demo/everything.ts b/packages/mcp/src/demo/everything.ts
deleted file mode 100644
index 58e677a3e4..0000000000
--- a/packages/mcp/src/demo/everything.ts
+++ /dev/null
@@ -1,231 +0,0 @@
-import express from 'express';
-import { EventSource } from 'eventsource';
-import { MCPConnection } from '../connection';
-import type { MCPOptions } from '../types/mcp';
-
-// Set up EventSource for Node environment
-global.EventSource = EventSource;
-
-const app = express();
-app.use(express.json());
-
-let mcp: MCPConnection;
-
-const initializeMCP = async () => {
- console.log('Initializing MCP with SSE transport...');
-
- const mcpOptions: MCPOptions = {
- type: 'sse' as const,
- url: 'http://localhost:3001/sse',
- // type: 'stdio' as const,
- // 'command': 'npx',
- // 'args': [
- // '-y',
- // '@modelcontextprotocol/server-everything',
- // ],
- };
-
- try {
- await MCPConnection.destroyInstance();
- mcp = MCPConnection.getInstance('everything', mcpOptions);
-
- mcp.on('connectionChange', (state) => {
- console.log(`MCP connection state changed to: ${state}`);
- });
-
- mcp.on('error', (error) => {
- console.error('MCP error:', error);
- });
-
- console.log('Connecting to MCP server...');
- await mcp.connectClient();
- console.log('Connected to MCP server');
-
- // Test the connection
- try {
- const resources = await mcp.fetchResources();
- console.log('Available resources:', resources);
- } catch (error) {
- console.error('Error fetching resources:', error);
- }
- } catch (error) {
- console.error('Failed to connect to MCP server:', error);
- }
-};
-
-// API Endpoints
-app.get('/status', (req, res) => {
- res.json({
- connected: mcp.isConnected(),
- state: mcp.getConnectionState(),
- error: mcp.getLastError()?.message,
- });
-});
-
-// Resources endpoint
-app.get('/resources', async (req, res) => {
- try {
- const resources = await mcp.fetchResources();
- res.json({ resources });
- } catch (error) {
- res.status(500).json({ error: String(error) });
- }
-});
-
-// Tools endpoint with all tool operations
-app.get('/tools', async (req, res) => {
- try {
- const tools = await mcp.fetchTools();
- res.json({ tools });
- } catch (error) {
- res.status(500).json({ error: String(error) });
- }
-});
-
-// Echo tool endpoint
-app.post('/tools/echo', async (req, res) => {
- try {
- const { message } = req.body;
- const result = await mcp.client.callTool({
- name: 'echo',
- arguments: { message },
- });
- res.json(result);
- } catch (error) {
- res.status(500).json({ error: String(error) });
- }
-});
-
-// Add tool endpoint
-app.post('/tools/add', async (req, res) => {
- try {
- const { a, b } = req.body;
- const result = await mcp.client.callTool({
- name: 'add',
- arguments: { a, b },
- });
- res.json(result);
- } catch (error) {
- res.status(500).json({ error: String(error) });
- }
-});
-
-// Long running operation endpoint
-app.post('/tools/long-operation', async (req, res) => {
- try {
- const { duration, steps } = req.body;
- const result = await mcp.client.callTool({
- name: 'longRunningOperation',
- arguments: { duration, steps },
- });
- res.json(result);
- } catch (error) {
- res.status(500).json({ error: String(error) });
- }
-});
-
-// Sample LLM endpoint
-app.post('/tools/sample', async (req, res) => {
- try {
- const { prompt, maxTokens } = req.body;
- const result = await mcp.client.callTool({
- name: 'sampleLLM',
- arguments: { prompt, maxTokens },
- });
- res.json(result);
- } catch (error) {
- res.status(500).json({ error: String(error) });
- }
-});
-
-// Get tiny image endpoint
-app.get('/tools/tiny-image', async (req, res) => {
- try {
- const result = await mcp.client.callTool({
- name: 'getTinyImage',
- arguments: {},
- });
- res.json(result);
- } catch (error) {
- res.status(500).json({ error: String(error) });
- }
-});
-
-// Prompts endpoints
-app.get('/prompts', async (req, res) => {
- try {
- const prompts = await mcp.fetchPrompts();
- res.json({ prompts });
- } catch (error) {
- res.status(500).json({ error: String(error) });
- }
-});
-
-app.post('/prompts/simple', async (req, res) => {
- try {
- const result = await mcp.client.getPrompt({
- name: 'simple_prompt',
- });
- res.json(result);
- } catch (error) {
- res.status(500).json({ error: String(error) });
- }
-});
-
-app.post('/prompts/complex', async (req, res) => {
- try {
- const { temperature, style } = req.body;
- const result = await mcp.client.getPrompt({
- name: 'complex_prompt',
- arguments: { temperature, style },
- });
- res.json(result);
- } catch (error) {
- res.status(500).json({ error: String(error) });
- }
-});
-
-// Resource subscription endpoints
-app.post('/resources/subscribe', async (req, res) => {
- try {
- const { uri } = req.body;
- await mcp.client.subscribeResource({ uri });
- res.json({ success: true });
- } catch (error) {
- res.status(500).json({ error: String(error) });
- }
-});
-
-app.post('/resources/unsubscribe', async (req, res) => {
- try {
- const { uri } = req.body;
- await mcp.client.unsubscribeResource({ uri });
- res.json({ success: true });
- } catch (error) {
- res.status(500).json({ error: String(error) });
- }
-});
-
-// Error handling
-// eslint-disable-next-line @typescript-eslint/no-unused-vars
-app.use((err: Error, req: express.Request, res: express.Response, next: express.NextFunction) => {
- console.error('Unhandled error:', err);
- res.status(500).json({
- error: 'Internal server error',
- message: err.message,
- });
-});
-
-// Cleanup on shutdown
-process.on('SIGINT', async () => {
- console.log('Shutting down...');
- await MCPConnection.destroyInstance();
- process.exit(0);
-});
-
-// Start server
-const PORT = process.env.MCP_PORT ?? 3000;
-app.listen(PORT, () => {
- console.log(`Server running on http://localhost:${PORT}`);
- initializeMCP();
-});
diff --git a/packages/mcp/src/demo/filesystem.ts b/packages/mcp/src/demo/filesystem.ts
deleted file mode 100644
index 4f3a21411f..0000000000
--- a/packages/mcp/src/demo/filesystem.ts
+++ /dev/null
@@ -1,211 +0,0 @@
-import express from 'express';
-import { EventSource } from 'eventsource';
-import { MCPConnection } from '../connection';
-import type { MCPOptions } from '../types/mcp';
-
-// Set up EventSource for Node environment
-global.EventSource = EventSource;
-
-const app = express();
-app.use(express.json());
-
-let mcp: MCPConnection;
-
-const initializeMCP = async () => {
- console.log('Initializing MCP with SSE transport...');
-
- const mcpOptions: MCPOptions = {
- // type: 'sse' as const,
- // url: 'http://localhost:3001/sse',
- type: 'stdio' as const,
- command: 'npx',
- args: ['-y', '@modelcontextprotocol/server-filesystem', '/home/danny/LibreChat/'],
- };
-
- try {
- // Clean up any existing instance
- await MCPConnection.destroyInstance();
-
- // Get singleton instance
- mcp = MCPConnection.getInstance('filesystem', mcpOptions);
-
- // Add event listeners
- mcp.on('connectionChange', (state) => {
- console.log(`MCP connection state changed to: ${state}`);
- });
-
- mcp.on('error', (error) => {
- console.error('MCP error:', error);
- });
-
- // Connect to server
- console.log('Connecting to MCP server...');
- await mcp.connectClient();
- console.log('Connected to MCP server');
- } catch (error) {
- console.error('Failed to connect to MCP server:', error);
- }
-};
-
-// Initialize MCP connection
-initializeMCP();
-
-// API Endpoints
-app.get('/status', (req, res) => {
- res.json({
- connected: mcp.isConnected(),
- state: mcp.getConnectionState(),
- error: mcp.getLastError()?.message,
- });
-});
-
-app.get('/resources', async (req, res) => {
- try {
- const resources = await mcp.fetchResources();
- res.json({ resources });
- } catch (error) {
- res.status(500).json({ error: String(error) });
- }
-});
-
-app.get('/tools', async (req, res) => {
- try {
- const tools = await mcp.fetchTools();
- res.json({ tools });
- } catch (error) {
- res.status(500).json({ error: String(error) });
- }
-});
-
-// File operations
-// @ts-ignore
-app.get('/files/read', async (req, res) => {
- const filePath = req.query.path as string;
- if (!filePath) {
- return res.status(400).json({ error: 'Path parameter is required' });
- }
-
- try {
- const result = await mcp.client.callTool({
- name: 'read_file',
- arguments: { path: filePath },
- });
- res.json(result);
- } catch (error) {
- res.status(500).json({ error: String(error) });
- }
-});
-
-// @ts-ignore
-app.post('/files/write', async (req, res) => {
- const { path, content } = req.body;
- if (!path || content === undefined) {
- return res.status(400).json({ error: 'Path and content are required' });
- }
-
- try {
- const result = await mcp.client.callTool({
- name: 'write_file',
- arguments: { path, content },
- });
- res.json(result);
- } catch (error) {
- res.status(500).json({ error: String(error) });
- }
-});
-
-// @ts-ignore
-app.post('/files/edit', async (req, res) => {
- const { path, edits, dryRun = false } = req.body;
- if (!path || !edits) {
- return res.status(400).json({ error: 'Path and edits are required' });
- }
-
- try {
- const result = await mcp.client.callTool({
- name: 'edit_file',
- arguments: { path, edits, dryRun },
- });
- res.json(result);
- } catch (error) {
- res.status(500).json({ error: String(error) });
- }
-});
-
-// Directory operations
-// @ts-ignore
-app.get('/directory/list', async (req, res) => {
- const dirPath = req.query.path as string;
- if (!dirPath) {
- return res.status(400).json({ error: 'Path parameter is required' });
- }
-
- try {
- const result = await mcp.client.callTool({
- name: 'list_directory',
- arguments: { path: dirPath },
- });
- res.json(result);
- } catch (error) {
- res.status(500).json({ error: String(error) });
- }
-});
-
-// @ts-ignore
-app.post('/directory/create', async (req, res) => {
- const { path } = req.body;
- if (!path) {
- return res.status(400).json({ error: 'Path is required' });
- }
-
- try {
- const result = await mcp.client.callTool({
- name: 'create_directory',
- arguments: { path },
- });
- res.json(result);
- } catch (error) {
- res.status(500).json({ error: String(error) });
- }
-});
-
-// Search endpoint
-// @ts-ignore
-app.get('/search', async (req, res) => {
- const { path, pattern } = req.query;
- if (!path || !pattern) {
- return res.status(400).json({ error: 'Path and pattern parameters are required' });
- }
-
- try {
- const result = await mcp.client.callTool({
- name: 'search_files',
- arguments: { path, pattern },
- });
- res.json(result);
- } catch (error) {
- res.status(500).json({ error: String(error) });
- }
-});
-
-// Error handling
-app.use((err: Error, req: express.Request, res: express.Response, next: express.NextFunction) => {
- console.error('Unhandled error:', err);
- res.status(500).json({
- error: 'Internal server error',
- message: err.message,
- });
-});
-
-// Cleanup on shutdown
-process.on('SIGINT', async () => {
- console.log('Shutting down...');
- await MCPConnection.destroyInstance();
- process.exit(0);
-});
-
-// Start server
-const PORT = process.env.MCP_PORT || 3000;
-app.listen(PORT, () => {
- console.log(`Server running on http://localhost:${PORT}`);
-});
diff --git a/packages/mcp/src/demo/servers.ts b/packages/mcp/src/demo/servers.ts
deleted file mode 100644
index 1c5cad926c..0000000000
--- a/packages/mcp/src/demo/servers.ts
+++ /dev/null
@@ -1,226 +0,0 @@
-// server.ts
-import express from 'express';
-import { EventSource } from 'eventsource';
-import { MCPManager } from '../manager';
-import { MCPConnection } from '../connection';
-import type * as t from '../types/mcp';
-
-// Set up EventSource for Node environment
-global.EventSource = EventSource;
-
-const app = express();
-app.use(express.json());
-
-const mcpManager = MCPManager.getInstance();
-
-const mcpServers: t.MCPServers = {
- everything: {
- type: 'sse' as const,
- url: 'http://localhost:3001/sse',
- },
- filesystem: {
- type: 'stdio' as const,
- command: 'npx',
- args: ['-y', '@modelcontextprotocol/server-filesystem', '/home/danny/LibreChat/'],
- },
-};
-
-// Generic helper to get connection and handle errors
-const withConnection = async (
- serverName: string,
- res: express.Response,
- callback: (connection: MCPConnection) => Promise<void>,
-) => {
- const connection = mcpManager.getConnection(serverName);
- if (!connection) {
- return res.status(404).json({ error: `Server "${serverName}" not found` });
- }
- try {
- await callback(connection);
- } catch (error) {
- res.status(500).json({ error: String(error) });
- }
-};
-
-// Common endpoints for all servers
-// @ts-ignore
-app.get('/status/:server', (req, res) => {
- const connection = mcpManager.getConnection(req.params.server);
- if (!connection) {
- return res.status(404).json({ error: 'Server not found' });
- }
-
- res.json({
- connected: connection.isConnected(),
- state: connection.getConnectionState(),
- error: connection.getLastError()?.message,
- });
-});
-
-app.get('/resources/:server', async (req, res) => {
- await withConnection(req.params.server, res, async (connection) => {
- const resources = await connection.fetchResources();
- res.json({ resources });
- });
-});
-
-app.get('/tools/:server', async (req, res) => {
- await withConnection(req.params.server, res, async (connection) => {
- const tools = await connection.fetchTools();
- res.json({ tools });
- });
-});
-
-// "Everything" server specific endpoints
-app.post('/everything/tools/echo', async (req, res) => {
- await withConnection('everything', res, async (connection) => {
- const { message } = req.body;
- const result = await connection.client.callTool({
- name: 'echo',
- arguments: { message },
- });
- res.json(result);
- });
-});
-
-app.post('/everything/tools/add', async (req, res) => {
- await withConnection('everything', res, async (connection) => {
- const { a, b } = req.body;
- const result = await connection.client.callTool({
- name: 'add',
- arguments: { a, b },
- });
- res.json(result);
- });
-});
-
-app.post('/everything/tools/long-operation', async (req, res) => {
- await withConnection('everything', res, async (connection) => {
- const { duration, steps } = req.body;
- const result = await connection.client.callTool({
- name: 'longRunningOperation',
- arguments: { duration, steps },
- });
- res.json(result);
- });
-});
-
-// Filesystem server specific endpoints
-// @ts-ignore
-app.get('/filesystem/files/read', async (req, res) => {
- const filePath = req.query.path as string;
- if (!filePath) {
- return res.status(400).json({ error: 'Path parameter is required' });
- }
-
- await withConnection('filesystem', res, async (connection) => {
- const result = await connection.client.callTool({
- name: 'read_file',
- arguments: { path: filePath },
- });
- res.json(result);
- });
-});
-
-// @ts-ignore
-app.post('/filesystem/files/write', async (req, res) => {
- const { path, content } = req.body;
- if (!path || content === undefined) {
- return res.status(400).json({ error: 'Path and content are required' });
- }
-
- await withConnection('filesystem', res, async (connection) => {
- const result = await connection.client.callTool({
- name: 'write_file',
- arguments: { path, content },
- });
- res.json(result);
- });
-});
-
-// @ts-ignore
-app.post('/filesystem/files/edit', async (req, res) => {
- const { path, edits, dryRun = false } = req.body;
- if (!path || !edits) {
- return res.status(400).json({ error: 'Path and edits are required' });
- }
-
- await withConnection('filesystem', res, async (connection) => {
- const result = await connection.client.callTool({
- name: 'edit_file',
- arguments: { path, edits, dryRun },
- });
- res.json(result);
- });
-});
-
-// @ts-ignore
-app.get('/filesystem/directory/list', async (req, res) => {
- const dirPath = req.query.path as string;
- if (!dirPath) {
- return res.status(400).json({ error: 'Path parameter is required' });
- }
-
- await withConnection('filesystem', res, async (connection) => {
- const result = await connection.client.callTool({
- name: 'list_directory',
- arguments: { path: dirPath },
- });
- res.json(result);
- });
-});
-
-// @ts-ignore
-app.post('/filesystem/directory/create', async (req, res) => {
- const { path } = req.body;
- if (!path) {
- return res.status(400).json({ error: 'Path is required' });
- }
-
- await withConnection('filesystem', res, async (connection) => {
- const result = await connection.client.callTool({
- name: 'create_directory',
- arguments: { path },
- });
- res.json(result);
- });
-});
-
-// @ts-ignore
-app.get('/filesystem/search', async (req, res) => {
- const { path, pattern } = req.query;
- if (!path || !pattern) {
- return res.status(400).json({ error: 'Path and pattern parameters are required' });
- }
-
- await withConnection('filesystem', res, async (connection) => {
- const result = await connection.client.callTool({
- name: 'search_files',
- arguments: { path, pattern },
- });
- res.json(result);
- });
-});
-
-// Error handling
-app.use((err: Error, req: express.Request, res: express.Response, next: express.NextFunction) => {
- console.error('Unhandled error:', err);
- res.status(500).json({
- error: 'Internal server error',
- message: err.message,
- });
-});
-
-// Cleanup on shutdown
-process.on('SIGINT', async () => {
- console.log('Shutting down...');
- await MCPManager.destroyInstance();
- process.exit(0);
-});
-
-// Start server
-const PORT = process.env.MCP_PORT ?? 3000;
-app.listen(PORT, async () => {
- console.log(`Server running on http://localhost:${PORT}`);
- await mcpManager.initializeMCP(mcpServers);
-});
diff --git a/packages/mcp/src/examples/everything/everything.ts b/packages/mcp/src/examples/everything/everything.ts
deleted file mode 100644
index 6080ff4ff4..0000000000
--- a/packages/mcp/src/examples/everything/everything.ts
+++ /dev/null
@@ -1,426 +0,0 @@
-import { Server } from '@modelcontextprotocol/sdk/server/index.js';
-import {
- CallToolRequestSchema,
- CreateMessageRequest,
- CreateMessageResultSchema,
- GetPromptRequestSchema,
- ListPromptsRequestSchema,
- ListResourcesRequestSchema,
- ListResourceTemplatesRequestSchema,
- ListToolsRequestSchema,
- ReadResourceRequestSchema,
- Resource,
- SetLevelRequestSchema,
- SubscribeRequestSchema,
- Tool,
- ToolSchema,
- UnsubscribeRequestSchema,
-} from '@modelcontextprotocol/sdk/types.js';
-import { z } from 'zod';
-import { zodToJsonSchema } from 'zod-to-json-schema';
-
-const ToolInputSchema = ToolSchema.shape.inputSchema;
-type ToolInput = z.infer<typeof ToolInputSchema>;
-
-/* Input schemas for tools implemented in this server */
-const EchoSchema = z.object({
- message: z.string().describe('Message to echo'),
-});
-
-const AddSchema = z.object({
- a: z.number().describe('First number'),
- b: z.number().describe('Second number'),
-});
-
-const LongRunningOperationSchema = z.object({
- duration: z.number().default(10).describe('Duration of the operation in seconds'),
- steps: z.number().default(5).describe('Number of steps in the operation'),
-});
-
-const SampleLLMSchema = z.object({
- prompt: z.string().describe('The prompt to send to the LLM'),
- maxTokens: z.number().default(100).describe('Maximum number of tokens to generate'),
-});
-
-const GetTinyImageSchema = z.object({});
-
-enum ToolName {
- ECHO = 'echo',
- ADD = 'add',
- LONG_RUNNING_OPERATION = 'longRunningOperation',
- SAMPLE_LLM = 'sampleLLM',
- GET_TINY_IMAGE = 'getTinyImage',
-}
-
-enum PromptName {
- SIMPLE = 'simple_prompt',
- COMPLEX = 'complex_prompt',
-}
-
-export const createServer = () => {
- const server = new Server(
- {
- name: 'example-servers/everything',
- version: '1.0.0',
- },
- {
- capabilities: {
- prompts: {},
- resources: { subscribe: true },
- tools: {},
- logging: {},
- },
- },
- );
-
- const subscriptions: Set<string> = new Set();
- let updateInterval: NodeJS.Timeout | undefined;
-
- // Set up update interval for subscribed resources
- // eslint-disable-next-line prefer-const
- updateInterval = setInterval(() => {
- // @ts-ignore
- for (const uri of subscriptions) {
- server.notification({
- method: 'notifications/resources/updated',
- params: { uri },
- });
- }
- }, 5000);
-
- // Helper method to request sampling from client
- const requestSampling = async (context: string, uri: string, maxTokens = 100) => {
- const request: CreateMessageRequest = {
- method: 'sampling/createMessage',
- params: {
- messages: [
- {
- role: 'user',
- content: {
- type: 'text',
- text: `Resource ${uri} context: ${context}`,
- },
- },
- ],
- systemPrompt: 'You are a helpful test server.',
- maxTokens,
- temperature: 0.7,
- includeContext: 'thisServer',
- },
- };
-
- return await server.request(request, CreateMessageResultSchema);
- };
-
- const ALL_RESOURCES: Resource[] = Array.from({ length: 100 }, (_, i) => {
- const uri = `test://static/resource/${i + 1}`;
- if (i % 2 === 0) {
- return {
- uri,
- name: `Resource ${i + 1}`,
- mimeType: 'text/plain',
- text: `Resource ${i + 1}: This is a plaintext resource`,
- };
- } else {
- const buffer = Buffer.from(`Resource ${i + 1}: This is a base64 blob`);
- return {
- uri,
- name: `Resource ${i + 1}`,
- mimeType: 'application/octet-stream',
- blob: buffer.toString('base64'),
- };
- }
- });
-
- const PAGE_SIZE = 10;
-
- server.setRequestHandler(ListResourcesRequestSchema, async (request) => {
- const cursor = request.params?.cursor;
- let startIndex = 0;
-
- if (cursor) {
- const decodedCursor = parseInt(atob(cursor), 10);
- if (!isNaN(decodedCursor)) {
- startIndex = decodedCursor;
- }
- }
-
- const endIndex = Math.min(startIndex + PAGE_SIZE, ALL_RESOURCES.length);
- const resources = ALL_RESOURCES.slice(startIndex, endIndex);
-
- let nextCursor: string | undefined;
- if (endIndex < ALL_RESOURCES.length) {
- nextCursor = btoa(endIndex.toString());
- }
-
- return {
- resources,
- nextCursor,
- };
- });
-
- server.setRequestHandler(ListResourceTemplatesRequestSchema, async () => {
- return {
- resourceTemplates: [
- {
- uriTemplate: 'test://static/resource/{id}',
- name: 'Static Resource',
- description: 'A static resource with a numeric ID',
- },
- ],
- };
- });
-
- server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
- const uri = request.params.uri;
-
- if (uri.startsWith('test://static/resource/')) {
- const index = parseInt(uri.split('/').pop() ?? '', 10) - 1;
- if (index >= 0 && index < ALL_RESOURCES.length) {
- const resource = ALL_RESOURCES[index];
- return {
- contents: [resource],
- };
- }
- }
-
- throw new Error(`Unknown resource: ${uri}`);
- });
-
- server.setRequestHandler(SubscribeRequestSchema, async (request) => {
- const { uri } = request.params;
- subscriptions.add(uri);
-
- // Request sampling from client when someone subscribes
- await requestSampling('A new subscription was started', uri);
- return {};
- });
-
- server.setRequestHandler(UnsubscribeRequestSchema, async (request) => {
- subscriptions.delete(request.params.uri);
- return {};
- });
-
- server.setRequestHandler(ListPromptsRequestSchema, async () => {
- return {
- prompts: [
- {
- name: PromptName.SIMPLE,
- description: 'A prompt without arguments',
- },
- {
- name: PromptName.COMPLEX,
- description: 'A prompt with arguments',
- arguments: [
- {
- name: 'temperature',
- description: 'Temperature setting',
- required: true,
- },
- {
- name: 'style',
- description: 'Output style',
- required: false,
- },
- ],
- },
- ],
- };
- });
-
- server.setRequestHandler(GetPromptRequestSchema, async (request) => {
- const { name, arguments: args } = request.params;
-
- if (name === PromptName.SIMPLE) {
- return {
- messages: [
- {
- role: 'user',
- content: {
- type: 'text',
- text: 'This is a simple prompt without arguments.',
- },
- },
- ],
- };
- }
-
- if (name === PromptName.COMPLEX) {
- return {
- messages: [
- {
- role: 'user',
- content: {
- type: 'text',
- text: `This is a complex prompt with arguments: temperature=${args?.temperature}, style=${args?.style}`,
- },
- },
- {
- role: 'assistant',
- content: {
- type: 'text',
- text: 'I understand. You\'ve provided a complex prompt with temperature and style arguments. How would you like me to proceed?',
- },
- },
- {
- role: 'user',
- content: {
- type: 'image',
- data: MCP_TINY_IMAGE,
- mimeType: 'image/png',
- },
- },
- ],
- };
- }
-
- throw new Error(`Unknown prompt: ${name}`);
- });
-
- server.setRequestHandler(ListToolsRequestSchema, async () => {
- const tools: Tool[] = [
- {
- name: ToolName.ECHO,
- description: 'Echoes back the input',
- inputSchema: zodToJsonSchema(EchoSchema) as ToolInput,
- },
- {
- name: ToolName.ADD,
- description: 'Adds two numbers',
- inputSchema: zodToJsonSchema(AddSchema) as ToolInput,
- },
- {
- name: ToolName.LONG_RUNNING_OPERATION,
- description: 'Demonstrates a long running operation with progress updates',
- inputSchema: zodToJsonSchema(LongRunningOperationSchema) as ToolInput,
- },
- {
- name: ToolName.SAMPLE_LLM,
- description: 'Samples from an LLM using MCP\'s sampling feature',
- inputSchema: zodToJsonSchema(SampleLLMSchema) as ToolInput,
- },
- {
- name: ToolName.GET_TINY_IMAGE,
- description: 'Returns the MCP_TINY_IMAGE',
- inputSchema: zodToJsonSchema(GetTinyImageSchema) as ToolInput,
- },
- ];
-
- return { tools };
- });
-
- server.setRequestHandler(CallToolRequestSchema, async (request) => {
- const { name, arguments: args } = request.params;
-
- if (name === ToolName.ECHO) {
- const validatedArgs = EchoSchema.parse(args);
- return {
- content: [{ type: 'text', text: `Echo: ${validatedArgs.message}` }],
- };
- }
-
- if (name === ToolName.ADD) {
- const validatedArgs = AddSchema.parse(args);
- const sum = validatedArgs.a + validatedArgs.b;
- return {
- content: [
- {
- type: 'text',
- text: `The sum of ${validatedArgs.a} and ${validatedArgs.b} is ${sum}.`,
- },
- ],
- };
- }
-
- if (name === ToolName.LONG_RUNNING_OPERATION) {
- const validatedArgs = LongRunningOperationSchema.parse(args);
- const { duration, steps } = validatedArgs;
- const stepDuration = duration / steps;
- const progressToken = request.params._meta?.progressToken;
-
- for (let i = 1; i < steps + 1; i++) {
- await new Promise((resolve) => setTimeout(resolve, stepDuration * 1000));
-
- if (progressToken !== undefined) {
- await server.notification({
- method: 'notifications/progress',
- params: {
- progress: i,
- total: steps,
- progressToken,
- },
- });
- }
- }
-
- return {
- content: [
- {
- type: 'text',
- text: `Long running operation completed. Duration: ${duration} seconds, Steps: ${steps}.`,
- },
- ],
- };
- }
-
- if (name === ToolName.SAMPLE_LLM) {
- const validatedArgs = SampleLLMSchema.parse(args);
- const { prompt, maxTokens } = validatedArgs;
-
- const result = await requestSampling(prompt, ToolName.SAMPLE_LLM, maxTokens);
- return {
- content: [{ type: 'text', text: `LLM sampling result: ${result}` }],
- };
- }
-
- if (name === ToolName.GET_TINY_IMAGE) {
- GetTinyImageSchema.parse(args);
- return {
- content: [
- {
- type: 'text',
- text: 'This is a tiny image:',
- },
- {
- type: 'image',
- data: MCP_TINY_IMAGE,
- mimeType: 'image/png',
- },
- {
- type: 'text',
- text: 'The image above is the MCP tiny image.',
- },
- ],
- };
- }
-
- throw new Error(`Unknown tool: ${name}`);
- });
-
- server.setRequestHandler(SetLevelRequestSchema, async (request) => {
- const { level } = request.params;
-
- // Demonstrate different log levels
- await server.notification({
- method: 'notifications/message',
- params: {
- level: 'debug',
- logger: 'test-server',
- data: `Logging level set to: ${level}`,
- },
- });
-
- return {};
- });
-
- const cleanup = async () => {
- if (updateInterval) {
- clearInterval(updateInterval);
- }
- };
-
- return { server, cleanup };
-};
-
-const MCP_TINY_IMAGE =
- 'iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAKsGlDQ1BJQ0MgUHJvZmlsZQAASImVlwdUU+kSgOfe9JDQEiIgJfQmSCeAlBBaAAXpYCMkAUKJMRBU7MriClZURLCs6KqIgo0idizYFsWC3QVZBNR1sWDDlXeBQ9jdd9575805c+a7c+efmf+e/z9nLgCdKZDJMlF1gCxpjjwyyI8dn5DIJvUABRiY0kBdIMyWcSMiwgCTUft3+dgGyJC9YzuU69/f/1fREImzhQBIBMbJomxhFsbHMe0TyuQ5ALg9mN9kbo5siK9gzJRjDWL8ZIhTR7hviJOHGY8fjomO5GGsDUCmCQTyVACaKeZn5wpTsTw0f4ztpSKJFGPsGbyzsmaLMMbqgiUWI8N4KD8n+S95Uv+WM1mZUyBIVfLIXoaF7C/JlmUK5v+fn+N/S1amYrSGOaa0NHlwJGaxvpAHGbNDlSxNnhI+yhLRcPwwpymCY0ZZmM1LHGWRwD9UuTZzStgop0gC+co8OfzoURZnB0SNsnx2pLJWipzHHWWBfKyuIiNG6U8T85X589Ki40Y5VxI7ZZSzM6JCx2J4Sr9cEansXywN8hurG6jce1b2X/Yr4SvX5qRFByv3LhjrXyzljuXMjlf2JhL7B4zFxCjjZTl+ylqyzAhlvDgzSOnPzo1Srs3BDuTY2gjlN0wXhESMMoRBELAhBjIhB+QggECQgBTEOeJ5Q2cUeLNl8+WS1LQcNhe7ZWI2Xyq0m8B2tHd0Bhi6syNH4j1r+C4irGtjvhWVAF4nBgcHT475Qm4BHEkCoNaO+SxnAKh3A1w5JVTIc0d8Q9cJCEAFNWCCDhiACViCLTiCK3iCLwRACIRDNCTATBBCGmRhnc+FhbAMCqAI1sNmKIOdsBv2wyE4CvVwCs7DZbgOt+AePIZ26IJX0AcfYQBBEBJCRxiIDmKImCE2iCPCQbyRACQMiUQSkCQkFZEiCmQhsgIpQoqRMmQXUokcQU4g55GrSCvyEOlAepF3yFcUh9JQJqqPmqMTUQ7KRUPRaHQGmorOQfPQfHQtWopWoAfROvQ8eh29h7ajr9B+HOBUcCycEc4Wx8HxcOG4RFwKTo5bjCvEleAqcNW4Rlwz7g6uHfca9wVPxDPwbLwt3hMfjI/BC/Fz8Ivxq/Fl+P34OvxF/B18B74P/51AJ+gRbAgeBD4hnpBKmEsoIJQQ9hJqCZcI9whdhI9EIpFFtCC6EYOJCcR04gLiauJ2Yg3xHLGV2EnsJ5FIOiQbkhcpnCQg5ZAKSFtJB0lnSbdJXaTPZBWyIdmRHEhOJEvJy8kl5APkM+Tb5G7yAEWdYkbxoIRTRJT5lHWUPZRGyk1KF2WAqkG1oHpRo6np1GXUUmo19RL1CfW9ioqKsYq7ylQVicpSlVKVwypXVDpUvtA0adY0Hm06TUFbS9tHO0d7SHtPp9PN6b70RHoOfS29kn6B/oz+WZWhaqfKVxWpLlEtV61Tva36Ro2iZqbGVZuplqdWonZM7abaa3WKurk6T12gvli9XP2E+n31fg2GhoNGuEaWxmqNAxpXNXo0SZrmmgGaIs18zd2aFzQ7GTiGCYPHEDJWMPYwLjG6mESmBZPPTGcWMQ8xW5h9WppazlqxWvO0yrVOa7WzcCxzFp+VyVrHOspqY30dpz+OO048btW46nG3x33SHq/tqy3WLtSu0b6n/VWHrROgk6GzQade56kuXtdad6ruXN0dupd0X49njvccLxxfOP7o+Ed6qJ61XqTeAr3dejf0+vUN9IP0Zfpb9S/ovzZgGfgapBtsMjhj0GvIMPQ2lBhuMjxr+JKtxeayM9ml7IvsPiM9o2AjhdEuoxajAWML4xjj5cY1xk9NqCYckxSTTSZNJn2mhqaTTReaVpk+MqOYcczSzLaYNZt9MrcwjzNfaV5v3mOhbcG3yLOosnhiSbf0sZxjWWF514poxbHKsNpudcsatXaxTrMut75pg9q42khsttu0TiBMcJ8gnVAx4b4tzZZrm2tbZdthx7ILs1tuV2/3ZqLpxMSJGyY2T/xu72Kfab/H/rGDpkOIw3KHRod3jtaOQsdyx7tOdKdApyVODU5vnW2cxc47nB+4MFwmu6x0aXL509XNVe5a7drrZuqW5LbN7T6HyYngrOZccSe4+7kvcT/l/sXD1SPH46jHH562nhmeBzx7JllMEk/aM6nTy9hL4LXLq92b7Z3k/ZN3u4+Rj8Cnwue5r4mvyHevbzfXipvOPch942fvJ/er9fvE8+At4p3zx/kH+Rf6twRoBsQElAU8CzQOTA2sCuwLcglaEHQumBAcGrwh+D5fny/kV/L7QtxCFoVcDKWFRoWWhT4Psw6ThzVORieHTN44+ckUsynSKfXhEM4P3xj+NMIiYk7EyanEqRFTy6e+iHSIXBjZHMWImhV1IOpjtF/0uujHMZYxipimWLXY6bGVsZ/i/OOK49rjJ8Yvir+eoJsgSWhIJCXGJu5N7J8WMG3ztK7pLtMLprfNsJgxb8bVmbozM2eenqU2SzDrWBIhKS7pQNI3QbigQtCfzE/eltwn5Am3CF+JfEWbRL1iL3GxuDvFK6U4pSfVK3Vjam+aT1pJ2msJT1ImeZsenL4z/VNGeMa+jMHMuMyaLHJWUtYJqaY0Q3pxtsHsebNbZTayAln7HI85m+f0yUPle7OR7BnZDTlMbDi6obBU/KDoyPXOLc/9PDd27rF5GvOk827Mt56/an53XmDezwvwC4QLmhYaLVy2sGMRd9Guxcji5MVNS0yW5C/pWhq0dP8y6rKMZb8st19evPzDirgVjfn6+UvzO38I+qGqQLVAXnB/pefKnT/if5T82LLKadXWVd8LRYXXiuyLSoq+rRauvrbGYU3pmsG1KWtb1rmu27GeuF66vm2Dz4b9xRrFecWdGydvrNvE3lS46cPmWZuvljiX7NxC3aLY0l4aVtqw1XTr+q3fytLK7pX7ldds09u2atun7aLtt3f47qjeqb+zaOfXnyQ/PdgVtKuuwryiZDdxd+7uF3ti9zT/zPm5cq/u3qK9f+6T7mvfH7n/YqVbZeUBvQPrqtAqRVXvwekHbx3yP9RQbVu9q4ZVU3QYDisOvzySdKTtaOjRpmOcY9XHzY5vq2XUFtYhdfPr+urT6tsbEhpaT4ScaGr0bKw9aXdy3ymjU+WntU6vO0M9k39m8Gze2f5zsnOvz6ee72ya1fT4QvyFuxenXmy5FHrpyuXAyxeauc1nr3hdOXXV4+qJa5xr9dddr9fdcLlR+4vLL7Utri11N91uNtzyv9XYOqn1zG2f2+fv+N+5fJd/9/q9Kfda22LaHtyffr/9gehBz8PMh28f5T4aeLz0CeFJ4VP1pyXP9J5V/Gr1a027a/vpDv+OG8+jnj/uFHa++i37t29d+S/oL0q6Dbsrexx7TvUG9t56Oe1l1yvZq4HXBb9r/L7tjeWb43/4/nGjL76v66387eC71e913u/74PyhqT+i/9nHrI8Dnwo/63ze/4Xzpflr3NfugbnfSN9K/7T6s/F76Pcng1mDgzKBXDA8CuAwRVNSAN7tA6AnADCwGYI6bWSmHhZk5D9gmOA/8cjcPSyuANWYGRqNeOcADmNqvhRAzRdgaCyK9gXUyUmpo/Pv8Kw+JAbYv8K0HECi2x6tebQU/iEjc/xf+v6nBWXWv9l/AV0EC6JTIblRAAAAeGVYSWZNTQAqAAAACAAFARIAAwAAAAEAAQAAARoABQAAAAEAAABKARsABQAAAAEAAABSASgAAwAAAAEAAgAAh2kABAAAAAEAAABaAAAAAAAAAJAAAAABAAAAkAAAAAEAAqACAAQAAAABAAAAFKADAAQAAAABAAAAFAAAAAAXNii1AAAACXBIWXMAABYlAAAWJQFJUiTwAAAB82lUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iWE1QIENvcmUgNi4wLjAiPgogICA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPgogICAgICA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIgogICAgICAgICAgICB4bWxuczp0aWZmPSJodHRwOi8vbnMuYWRvYmUuY29tL3RpZmYvMS4wLyI+CiAgICAgICAgIDx0aWZmOllSZXNvbHV0aW9uPjE0NDwvdGlmZjpZUmVzb2x1dGlvbj4KICAgICAgICAgPHRpZmY6T3JpZW50YXRpb24+MTwvdGlmZjpPcmllbnRhdGlvbj4KICAgICAgICAgPHRpZmY6WFJlc29sdXRpb24+MTQ0PC90aWZmOlhSZXNvbHV0aW9uPgogICAgICAgICA8dGlmZjpSZXNvbHV0aW9uVW5pdD4yPC90aWZmOlJlc29sdXRpb25Vbml0PgogICAgICA8L3JkZjpEZXNjcmlwdGlvbj4KICAgPC9yZGY6UkRGPgo8L3g6eG1wbWV0YT4KReh49gAAAjRJREFUOBGFlD2vMUEUx2clvoNCcW8hCqFAo1dKhEQpvsF9KrWEBh/ALbQ0KkInBI3SWyGPCCJEQliXgsTLefaca/bBWjvJzs6cOf/fnDkzOQJIjWm06/XKBEGgD8c6nU5VIWgBtQDPZPWtJE8O63a7LBgMMo/Hw0ql0jPjcY4RvmqXy4XMjUYDUwLtdhtmsxnYbDbI5/O0djqdFFKmsEiGZ9jP9gem0yn0ej2Yz+fg9XpfycimAD7DttstQTDKfr8Po9GIIg6Hw1Cr1RTgB+A72GAwgMPhQLBMJgNSXsFqtUI2myUo18pA6QJogefsPrLBX4QdCVatViklw+EQRFGEj88P2O12pEUGATmsXq+TaLPZ0AXgMRF2vMEqlQoJTSYTpNNpApvNZliv1/+BHDaZTAi2Wq1A3Ig0xmMej7+RcZjdbodUKkWAaDQK+GHjHPnImB88JrZIJAKFQgH2+z2BOczhcMiwRCIBgUAA+NN5BP6mj2DYff35gk6nA61WCzBn2JxO5wPM7/fLz4vD0E+OECfn8xl/0Gw2KbLxeAyLxQIsFgt8p75pDSO7h/HbpUWpewCike9WLpfB7XaDy+WCYrFI/slk8i0MnRRAUt46hPMI4vE4+Hw+ec7t9/44VgWigEeby+UgFArJWjUYOqhWG6x50rpcSfR6PVUfNOgEVRlTX0HhrZBKz4MZjUYWi8VoA+lc9H/VaRZYjBKrtXR8tlwumcFgeMWRbZpA9ORQWfVm8A/FsrLaxebd5wAAAABJRU5ErkJggg==';
diff --git a/packages/mcp/src/examples/everything/index.ts b/packages/mcp/src/examples/everything/index.ts
deleted file mode 100644
index 4688e4bf88..0000000000
--- a/packages/mcp/src/examples/everything/index.ts
+++ /dev/null
@@ -1,23 +0,0 @@
-#!/usr/bin/env node
-
-import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
-import { createServer } from './everything';
-
-async function main() {
- const transport = new StdioServerTransport();
- const { server, cleanup } = createServer();
-
- await server.connect(transport);
-
- // Cleanup on exit
- process.on('SIGINT', async () => {
- await cleanup();
- await server.close();
- process.exit(0);
- });
-}
-
-main().catch((error) => {
- console.error('Server error:', error);
- process.exit(1);
-});
diff --git a/packages/mcp/src/examples/everything/sse.ts b/packages/mcp/src/examples/everything/sse.ts
deleted file mode 100644
index 38b329ae76..0000000000
--- a/packages/mcp/src/examples/everything/sse.ts
+++ /dev/null
@@ -1,24 +0,0 @@
-import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js';
-import express from 'express';
-import { createServer } from './everything.js';
-const app = express();
-const { server, cleanup } = createServer();
-let transport: SSEServerTransport;
-app.get('/sse', async (req, res) => {
- console.log('Received connection');
- transport = new SSEServerTransport('/message', res);
- await server.connect(transport);
- server.onclose = async () => {
- await cleanup();
- await server.close();
- process.exit(0);
- };
-});
-app.post('/message', async (req, res) => {
- console.log('Received message');
- await transport.handlePostMessage(req, res);
-});
-const PORT = process.env.SSE_PORT ?? 3001;
-app.listen(PORT, () => {
- console.log(`Server is running on port ${PORT}`);
-});
diff --git a/packages/mcp/src/examples/filesystem.ts b/packages/mcp/src/examples/filesystem.ts
deleted file mode 100644
index c2e3c430f2..0000000000
--- a/packages/mcp/src/examples/filesystem.ts
+++ /dev/null
@@ -1,700 +0,0 @@
-#!/usr/bin/env node
-// eslint-disable-next-line @typescript-eslint/ban-ts-comment
-// @ts-nocheck
-import { Server } from '@modelcontextprotocol/sdk/server/index.js';
-import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
-import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js';
-import {
- JSONRPCMessage,
- CallToolRequestSchema,
- ListToolsRequestSchema,
- InitializeRequestSchema,
- ToolSchema,
-} from '@modelcontextprotocol/sdk/types.js';
-import fs from 'fs/promises';
-import path from 'path';
-import os from 'os';
-import { z } from 'zod';
-import { zodToJsonSchema } from 'zod-to-json-schema';
-import { diffLines, createTwoFilesPatch } from 'diff';
-import { IncomingMessage, ServerResponse } from 'node:http';
-import { minimatch } from 'minimatch';
-import express from 'express';
-
-function normalizePath(p: string): string {
- return path.normalize(p).toLowerCase();
-}
-
-function expandHome(filepath: string): string {
- if (filepath.startsWith('~/') || filepath === '~') {
- return path.join(os.homedir(), filepath.slice(1));
- }
- return filepath;
-}
-
-// Command line argument parsing
-const args = process.argv.slice(2);
-
-// Parse command line arguments for transport type
-const transportArg = args.find((arg) => arg.startsWith('--transport='));
-const portArg = args.find((arg) => arg.startsWith('--port='));
-const directories = args.filter((arg) => !arg.startsWith('--'));
-
-if (directories.length === 0) {
- console.error(
- 'Usage: mcp-server-filesystem [--transport=stdio|sse] [--port=3000] <allowed-directory> [additional-directories...]',
- );
- process.exit(1);
-}
-
-// Extract transport type and port from arguments
-const transport = transportArg ? (transportArg.split('=')[1] as 'stdio' | 'sse') : 'stdio';
-
-const port = portArg ? parseInt(portArg.split('=')[1], 10) : undefined;
-
-// Store allowed directories in normalized form
-const allowedDirectories = directories.map((dir) => normalizePath(path.resolve(expandHome(dir))));
-
-// Validate that all directories exist and are accessible
-/** @ts-ignore */
-await Promise.all(
- directories.map(async (dir) => {
- try {
- const stats = await fs.stat(dir);
- if (!stats.isDirectory()) {
- console.error(`Error: ${dir} is not a directory`);
- process.exit(1);
- }
- } catch (error) {
- console.error(`Error accessing directory ${dir}:`, error);
- process.exit(1);
- }
- }),
-);
-
-// Security utilities
-async function validatePath(requestedPath: string): Promise<string> {
- const expandedPath = expandHome(requestedPath);
- const absolute = path.isAbsolute(expandedPath)
- ? path.resolve(expandedPath)
- : path.resolve(process.cwd(), expandedPath);
-
- const normalizedRequested = normalizePath(absolute);
-
- // Check if path is within allowed directories
- const isAllowed = allowedDirectories.some((dir) => normalizedRequested.startsWith(dir));
- if (!isAllowed) {
- throw new Error(
- `Access denied - path outside allowed directories: ${absolute} not in ${allowedDirectories.join(
- ', ',
- )}`,
- );
- }
-
- // Handle symlinks by checking their real path
- try {
- const realPath = await fs.realpath(absolute);
- const normalizedReal = normalizePath(realPath);
- const isRealPathAllowed = allowedDirectories.some((dir) => normalizedReal.startsWith(dir));
- if (!isRealPathAllowed) {
- throw new Error('Access denied - symlink target outside allowed directories');
- }
- return realPath;
- } catch (error) {
- // For new files that don't exist yet, verify parent directory
- const parentDir = path.dirname(absolute);
- try {
- const realParentPath = await fs.realpath(parentDir);
- const normalizedParent = normalizePath(realParentPath);
- const isParentAllowed = allowedDirectories.some((dir) => normalizedParent.startsWith(dir));
- if (!isParentAllowed) {
- throw new Error('Access denied - parent directory outside allowed directories');
- }
- return absolute;
- } catch {
- throw new Error(`Parent directory does not exist: ${parentDir}`);
- }
- }
-}
-
-// Schema definitions
-const ReadFileArgsSchema = z.object({
- path: z.string(),
-});
-
-const ReadMultipleFilesArgsSchema = z.object({
- paths: z.array(z.string()),
-});
-
-const WriteFileArgsSchema = z.object({
- path: z.string(),
- content: z.string(),
-});
-
-const EditOperation = z.object({
- oldText: z.string().describe('Text to search for - must match exactly'),
- newText: z.string().describe('Text to replace with'),
-});
-
-const EditFileArgsSchema = z.object({
- path: z.string(),
- edits: z.array(EditOperation),
- dryRun: z.boolean().default(false).describe('Preview changes using git-style diff format'),
-});
-
-const CreateDirectoryArgsSchema = z.object({
- path: z.string(),
-});
-
-const ListDirectoryArgsSchema = z.object({
- path: z.string(),
-});
-
-const MoveFileArgsSchema = z.object({
- source: z.string(),
- destination: z.string(),
-});
-
-const SearchFilesArgsSchema = z.object({
- path: z.string(),
- pattern: z.string(),
- excludePatterns: z.array(z.string()).optional().default([]),
-});
-
-const GetFileInfoArgsSchema = z.object({
- path: z.string(),
-});
-
-const ToolInputSchema = ToolSchema.shape.inputSchema;
-type ToolInput = z.infer<typeof ToolInputSchema>;
-
-interface FileInfo {
- size: number;
- created: Date;
- modified: Date;
- accessed: Date;
- isDirectory: boolean;
- isFile: boolean;
- permissions: string;
-}
-
-// Server setup
-const server = new Server(
- {
- name: 'secure-filesystem-server',
- version: '0.2.0',
- },
- {
- capabilities: {
- tools: {},
- },
- },
-);
-
-// Tool implementations
-async function getFileStats(filePath: string): Promise<FileInfo> {
- const stats = await fs.stat(filePath);
- return {
- size: stats.size,
- created: stats.birthtime,
- modified: stats.mtime,
- accessed: stats.atime,
- isDirectory: stats.isDirectory(),
- isFile: stats.isFile(),
- permissions: stats.mode.toString(8).slice(-3),
- };
-}
-
-async function searchFiles(
- rootPath: string,
- pattern: string,
- excludePatterns: string[] = [],
-): Promise<string[]> {
- const results: string[] = [];
-
- async function search(currentPath: string) {
- const entries = await fs.readdir(currentPath, { withFileTypes: true });
-
- for (const entry of entries) {
- const fullPath = path.join(currentPath, entry.name);
-
- try {
- // Validate each path before processing
- await validatePath(fullPath);
-
- // Check if path matches any exclude pattern
- const relativePath = path.relative(rootPath, fullPath);
- const shouldExclude = excludePatterns.some((pattern) => {
- const globPattern = pattern.includes('*') ? pattern : `**/${pattern}/**`;
- return minimatch(relativePath, globPattern, { dot: true });
- });
-
- if (shouldExclude) {
- continue;
- }
-
- if (entry.name.toLowerCase().includes(pattern.toLowerCase())) {
- results.push(fullPath);
- }
-
- if (entry.isDirectory()) {
- await search(fullPath);
- }
- } catch (error) {
- // Skip invalid paths during search
- continue;
- }
- }
- }
-
- await search(rootPath);
- return results;
-}
-
-// file editing and diffing utilities
-function normalizeLineEndings(text: string): string {
- return text.replace(/\r\n/g, '\n');
-}
-
-function createUnifiedDiff(originalContent: string, newContent: string, filepath = 'file'): string {
- // Ensure consistent line endings for diff
- const normalizedOriginal = normalizeLineEndings(originalContent);
- const normalizedNew = normalizeLineEndings(newContent);
-
- return createTwoFilesPatch(
- filepath,
- filepath,
- normalizedOriginal,
- normalizedNew,
- 'original',
- 'modified',
- );
-}
-
-async function applyFileEdits(
- filePath: string,
- edits: Array<{ oldText: string; newText: string }>,
- dryRun = false,
-): Promise<string> {
- // Read file content and normalize line endings
- const content = normalizeLineEndings(await fs.readFile(filePath, 'utf-8'));
-
- // Apply edits sequentially
- let modifiedContent = content;
- for (const edit of edits) {
- const normalizedOld = normalizeLineEndings(edit.oldText);
- const normalizedNew = normalizeLineEndings(edit.newText);
-
- // If exact match exists, use it
- if (modifiedContent.includes(normalizedOld)) {
- modifiedContent = modifiedContent.replace(normalizedOld, normalizedNew);
- continue;
- }
-
- // Otherwise, try line-by-line matching with flexibility for whitespace
- const oldLines = normalizedOld.split('\n');
- const contentLines = modifiedContent.split('\n');
- let matchFound = false;
-
- for (let i = 0; i <= contentLines.length - oldLines.length; i++) {
- const potentialMatch = contentLines.slice(i, i + oldLines.length);
-
- // Compare lines with normalized whitespace
- const isMatch = oldLines.every((oldLine, j) => {
- const contentLine = potentialMatch[j];
- return oldLine.trim() === contentLine.trim();
- });
-
- if (isMatch) {
- // Preserve original indentation of first line
- const originalIndent = contentLines[i].match(/^\s*/)?.[0] || '';
- const newLines = normalizedNew.split('\n').map((line, j) => {
- if (j === 0) {
- return originalIndent + line.trimStart();
- }
- // For subsequent lines, try to preserve relative indentation
- const oldIndent = oldLines[j]?.match(/^\s*/)?.[0] || '';
- const newIndent = line.match(/^\s*/)?.[0] || '';
- if (oldIndent && newIndent) {
- const relativeIndent = newIndent.length - oldIndent.length;
- return originalIndent + ' '.repeat(Math.max(0, relativeIndent)) + line.trimStart();
- }
- return line;
- });
-
- contentLines.splice(i, oldLines.length, ...newLines);
- modifiedContent = contentLines.join('\n');
- matchFound = true;
- break;
- }
- }
-
- if (!matchFound) {
- throw new Error(`Could not find exact match for edit:\n${edit.oldText}`);
- }
- }
-
- // Create unified diff
- const diff = createUnifiedDiff(content, modifiedContent, filePath);
-
- // Format diff with appropriate number of backticks
- let numBackticks = 3;
- while (diff.includes('`'.repeat(numBackticks))) {
- numBackticks++;
- }
- const formattedDiff = `${'`'.repeat(numBackticks)}diff\n${diff}${'`'.repeat(numBackticks)}\n\n`;
-
- if (!dryRun) {
- await fs.writeFile(filePath, modifiedContent, 'utf-8');
- }
-
- return formattedDiff;
-}
-
-// Tool handlers
-server.setRequestHandler(ListToolsRequestSchema, async () => {
- return {
- tools: [
- {
- name: 'read_file',
- description:
- 'Read the complete contents of a file from the file system. ' +
- 'Handles various text encodings and provides detailed error messages ' +
- 'if the file cannot be read. Use this tool when you need to examine ' +
- 'the contents of a single file. Only works within allowed directories.',
- inputSchema: zodToJsonSchema(ReadFileArgsSchema) as ToolInput,
- },
- {
- name: 'read_multiple_files',
- description:
- 'Read the contents of multiple files simultaneously. This is more ' +
- 'efficient than reading files one by one when you need to analyze ' +
- 'or compare multiple files. Each file\'s content is returned with its ' +
- 'path as a reference. Failed reads for individual files won\'t stop ' +
- 'the entire operation. Only works within allowed directories.',
- inputSchema: zodToJsonSchema(ReadMultipleFilesArgsSchema) as ToolInput,
- },
- {
- name: 'write_file',
- description:
- 'Create a new file or completely overwrite an existing file with new content. ' +
- 'Use with caution as it will overwrite existing files without warning. ' +
- 'Handles text content with proper encoding. Only works within allowed directories.',
- inputSchema: zodToJsonSchema(WriteFileArgsSchema) as ToolInput,
- },
- {
- name: 'edit_file',
- description:
- 'Make line-based edits to a text file. Each edit replaces exact line sequences ' +
- 'with new content. Returns a git-style diff showing the changes made. ' +
- 'Only works within allowed directories.',
- inputSchema: zodToJsonSchema(EditFileArgsSchema) as ToolInput,
- },
- {
- name: 'create_directory',
- description:
- 'Create a new directory or ensure a directory exists. Can create multiple ' +
- 'nested directories in one operation. If the directory already exists, ' +
- 'this operation will succeed silently. Perfect for setting up directory ' +
- 'structures for projects or ensuring required paths exist. Only works within allowed directories.',
- inputSchema: zodToJsonSchema(CreateDirectoryArgsSchema) as ToolInput,
- },
- {
- name: 'list_directory',
- description:
- 'Get a detailed listing of all files and directories in a specified path. ' +
- 'Results clearly distinguish between files and directories with [FILE] and [DIR] ' +
- 'prefixes. This tool is essential for understanding directory structure and ' +
- 'finding specific files within a directory. Only works within allowed directories.',
- inputSchema: zodToJsonSchema(ListDirectoryArgsSchema) as ToolInput,
- },
- {
- name: 'move_file',
- description:
- 'Move or rename files and directories. Can move files between directories ' +
- 'and rename them in a single operation. If the destination exists, the ' +
- 'operation will fail. Works across different directories and can be used ' +
- 'for simple renaming within the same directory. Both source and destination must be within allowed directories.',
- inputSchema: zodToJsonSchema(MoveFileArgsSchema) as ToolInput,
- },
- {
- name: 'search_files',
- description:
- 'Recursively search for files and directories matching a pattern. ' +
- 'Searches through all subdirectories from the starting path. The search ' +
- 'is case-insensitive and matches partial names. Returns full paths to all ' +
- 'matching items. Great for finding files when you don\'t know their exact location. ' +
- 'Only searches within allowed directories.',
- inputSchema: zodToJsonSchema(SearchFilesArgsSchema) as ToolInput,
- },
- {
- name: 'get_file_info',
- description:
- 'Retrieve detailed metadata about a file or directory. Returns comprehensive ' +
- 'information including size, creation time, last modified time, permissions, ' +
- 'and type. This tool is perfect for understanding file characteristics ' +
- 'without reading the actual content. Only works within allowed directories.',
- inputSchema: zodToJsonSchema(GetFileInfoArgsSchema) as ToolInput,
- },
- {
- name: 'list_allowed_directories',
- description:
- 'Returns the list of directories that this server is allowed to access. ' +
- 'Use this to understand which directories are available before trying to access files.',
- inputSchema: {
- type: 'object',
- properties: {},
- required: [],
- },
- },
- ],
- };
-});
-
-server.setRequestHandler(CallToolRequestSchema, async (request) => {
- try {
- const { name, arguments: args } = request.params;
-
- switch (name) {
- case 'read_file': {
- const parsed = ReadFileArgsSchema.safeParse(args);
- if (!parsed.success) {
- throw new Error(`Invalid arguments for read_file: ${parsed.error}`);
- }
- const validPath = await validatePath(parsed.data.path);
- const content = await fs.readFile(validPath, 'utf-8');
- return {
- content: [{ type: 'text', text: content }],
- };
- }
-
- case 'read_multiple_files': {
- const parsed = ReadMultipleFilesArgsSchema.safeParse(args);
- if (!parsed.success) {
- throw new Error(`Invalid arguments for read_multiple_files: ${parsed.error}`);
- }
- const results = await Promise.all(
- parsed.data.paths.map(async (filePath: string) => {
- try {
- const validPath = await validatePath(filePath);
- const content = await fs.readFile(validPath, 'utf-8');
- return `${filePath}:\n${content}\n`;
- } catch (error) {
- const errorMessage = error instanceof Error ? error.message : String(error);
- return `${filePath}: Error - ${errorMessage}`;
- }
- }),
- );
- return {
- content: [{ type: 'text', text: results.join('\n---\n') }],
- };
- }
-
- case 'write_file': {
- const parsed = WriteFileArgsSchema.safeParse(args);
- if (!parsed.success) {
- throw new Error(`Invalid arguments for write_file: ${parsed.error}`);
- }
- const validPath = await validatePath(parsed.data.path);
- await fs.writeFile(validPath, parsed.data.content, 'utf-8');
- return {
- content: [{ type: 'text', text: `Successfully wrote to ${parsed.data.path}` }],
- };
- }
-
- case 'edit_file': {
- const parsed = EditFileArgsSchema.safeParse(args);
- if (!parsed.success) {
- throw new Error(`Invalid arguments for edit_file: ${parsed.error}`);
- }
- const validPath = await validatePath(parsed.data.path);
- const result = await applyFileEdits(validPath, parsed.data.edits, parsed.data.dryRun);
- return {
- content: [{ type: 'text', text: result }],
- };
- }
-
- case 'create_directory': {
- const parsed = CreateDirectoryArgsSchema.safeParse(args);
- if (!parsed.success) {
- throw new Error(`Invalid arguments for create_directory: ${parsed.error}`);
- }
- const validPath = await validatePath(parsed.data.path);
- await fs.mkdir(validPath, { recursive: true });
- return {
- content: [{ type: 'text', text: `Successfully created directory ${parsed.data.path}` }],
- };
- }
-
- case 'list_directory': {
- const parsed = ListDirectoryArgsSchema.safeParse(args);
- if (!parsed.success) {
- throw new Error(`Invalid arguments for list_directory: ${parsed.error}`);
- }
- const validPath = await validatePath(parsed.data.path);
- const entries = await fs.readdir(validPath, { withFileTypes: true });
- const formatted = entries
- .map((entry) => `${entry.isDirectory() ? '[DIR]' : '[FILE]'} ${entry.name}`)
- .join('\n');
- return {
- content: [{ type: 'text', text: formatted }],
- };
- }
-
- case 'move_file': {
- const parsed = MoveFileArgsSchema.safeParse(args);
- if (!parsed.success) {
- throw new Error(`Invalid arguments for move_file: ${parsed.error}`);
- }
- const validSourcePath = await validatePath(parsed.data.source);
- const validDestPath = await validatePath(parsed.data.destination);
- await fs.rename(validSourcePath, validDestPath);
- return {
- content: [
- {
- type: 'text',
- text: `Successfully moved ${parsed.data.source} to ${parsed.data.destination}`,
- },
- ],
- };
- }
-
- case 'search_files': {
- const parsed = SearchFilesArgsSchema.safeParse(args);
- if (!parsed.success) {
- throw new Error(`Invalid arguments for search_files: ${parsed.error}`);
- }
- const validPath = await validatePath(parsed.data.path);
- const results = await searchFiles(
- validPath,
- parsed.data.pattern,
- parsed.data.excludePatterns,
- );
- return {
- content: [
- { type: 'text', text: results.length > 0 ? results.join('\n') : 'No matches found' },
- ],
- };
- }
-
- case 'get_file_info': {
- const parsed = GetFileInfoArgsSchema.safeParse(args);
- if (!parsed.success) {
- throw new Error(`Invalid arguments for get_file_info: ${parsed.error}`);
- }
- const validPath = await validatePath(parsed.data.path);
- const info = await getFileStats(validPath);
- return {
- content: [
- {
- type: 'text',
- text: Object.entries(info)
- .map(([key, value]) => `${key}: ${value}`)
- .join('\n'),
- },
- ],
- };
- }
-
- case 'list_allowed_directories': {
- return {
- content: [
- {
- type: 'text',
- text: `Allowed directories:\n${allowedDirectories.join('\n')}`,
- },
- ],
- };
- }
-
- default:
- throw new Error(`Unknown tool: ${name}`);
- }
- } catch (error) {
- const errorMessage = error instanceof Error ? error.message : String(error);
- return {
- content: [{ type: 'text', text: `Error: ${errorMessage}` }],
- isError: true,
- };
- }
-});
-
-// Start server
-// async function runServer() {
-// const transport = new StdioServerTransport();
-// await server.connect(transport);
-// console.error('Secure MCP Filesystem Server running on stdio');
-// console.error('Allowed directories:', allowedDirectories);
-// }
-
-// runServer().catch((error) => {
-// console.error('Fatal error running server:', error);
-// process.exit(1);
-// });
-
-async function runServer(transport: 'stdio' | 'sse', port?: number) {
- if (transport === 'stdio') {
- const stdioTransport = new StdioServerTransport();
- await server.connect(stdioTransport);
- console.error('Secure MCP Filesystem Server running on stdio');
- console.error('Allowed directories:', allowedDirectories);
- } else {
- const app = express();
- app.use(express.json());
-
- // Set up CORS
- app.use((req, res, next) => {
- res.header('Access-Control-Allow-Origin', '*');
- res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
- res.header('Access-Control-Allow-Headers', 'Content-Type');
- if (req.method === 'OPTIONS') {
- return res.sendStatus(200);
- }
- next();
- });
-
- let transport: SSEServerTransport;
-
- // SSE endpoint
- app.get('/sse', async (req, res) => {
- console.log('New SSE connection');
- transport = new SSEServerTransport('/message', res);
- await server.connect(transport);
-
- // Cleanup on close
- res.on('close', async () => {
- console.log('SSE connection closed');
- await server.close();
- });
- });
-
- // Message endpoint
- app.post('/message', async (req, res) => {
- if (!transport) {
- return res.status(503).send('SSE connection not established');
- }
- await transport.handlePostMessage(req, res);
- });
-
- const serverPort = port || 3001;
- app.listen(serverPort, () => {
- console.log(
- `Secure MCP Filesystem Server running on SSE at http://localhost:${serverPort}/sse`,
- );
- console.log('Allowed directories:', allowedDirectories);
- });
- }
-}
-
-if (directories.length === 0) {
- console.error(
- 'Usage: mcp-server-filesystem [--transport=stdio|sse] [--port=3000] <allowed-directory> [additional-directories...]',
- );
- process.exit(1);
-}
-
-// Start the server with the specified transport
-runServer(transport, port).catch((error) => {
- console.error('Fatal error running server:', error);
- process.exit(1);
-});
diff --git a/packages/mcp/src/index.ts b/packages/mcp/src/index.ts
deleted file mode 100644
index ba063ef1e3..0000000000
--- a/packages/mcp/src/index.ts
+++ /dev/null
@@ -1,9 +0,0 @@
-/* MCP */
-export * from './manager';
-/* Utilities */
-export * from './utils';
-/* Flow */
-export * from './flow/manager';
-/* types */
-export type * from './types/mcp';
-export type * from './flow/types';