From 277fbd10cbb54a7a9ef75baef035e391d3819a08 Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Mon, 19 Jan 2026 12:01:43 -0500 Subject: [PATCH 001/245] =?UTF-8?q?=F0=9F=94=92=20fix:=20Session=20Expiry?= =?UTF-8?q?=20Management=20for=20OpenID/SAML=20(#11407)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Added session cookie options for OpenID and SAML configurations, including maxAge and secure attributes based on the environment. - Introduced DEFAULT_SESSION_EXPIRY from data-schemas for better session handling. --- api/server/socialLogins.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/api/server/socialLogins.js b/api/server/socialLogins.js index 0a89313ba9..bad64eee77 100644 --- a/api/server/socialLogins.js +++ b/api/server/socialLogins.js @@ -1,8 +1,8 @@ const passport = require('passport'); const session = require('express-session'); const { isEnabled } = require('@librechat/api'); -const { logger } = require('@librechat/data-schemas'); const { CacheKeys } = require('librechat-data-provider'); +const { logger, DEFAULT_SESSION_EXPIRY } = require('@librechat/data-schemas'); const { openIdJwtLogin, facebookLogin, @@ -22,11 +22,17 @@ const { getLogStores } = require('~/cache'); */ async function configureOpenId(app) { logger.info('Configuring OpenID Connect...'); + const isProduction = process.env.NODE_ENV === 'production'; + const sessionExpiry = Number(process.env.SESSION_EXPIRY) || DEFAULT_SESSION_EXPIRY; const sessionOptions = { secret: process.env.OPENID_SESSION_SECRET, resave: false, saveUninitialized: false, store: getLogStores(CacheKeys.OPENID_SESSION), + cookie: { + maxAge: sessionExpiry, + secure: isProduction, + }, }; app.use(session(sessionOptions)); app.use(passport.session()); @@ -82,11 +88,17 @@ const configureSocialLogins = async (app) => { process.env.SAML_SESSION_SECRET ) { logger.info('Configuring SAML Connect...'); + const isProduction = process.env.NODE_ENV === 'production'; + const sessionExpiry = Number(process.env.SESSION_EXPIRY) || DEFAULT_SESSION_EXPIRY; const sessionOptions = { secret: process.env.SAML_SESSION_SECRET, resave: false, saveUninitialized: false, store: getLogStores(CacheKeys.SAML_SESSION), + cookie: { + maxAge: sessionExpiry, + secure: isProduction, + }, }; app.use(session(sessionOptions)); app.use(passport.session()); From 9134471143cd883366f9b595e498a4a956ff071b Mon Sep 17 00:00:00 2001 From: Dustin Healy <54083382+dustinhealy@users.noreply.github.com> Date: Mon, 19 Jan 2026 09:02:24 -0800 Subject: [PATCH 002/245] =?UTF-8?q?=F0=9F=94=8E=20fix:=20Focus=20Credentia?= =?UTF-8?q?l=20Inputs=20in=20Agent=20Tools=20(#11394)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: focus transfer on add tool in Add Tools dialog * fix: focus transfer to inputs on add mcp server in Add MCP Server Tools dialog * chore: add comments disabling ESLint autfocus and documenting the purpose of the override * chore: remove stray newline --- client/src/components/MCP/CustomUserVarsSection.tsx | 12 ++++++++++-- .../src/components/Plugins/Store/PluginAuthForm.tsx | 7 ++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/client/src/components/MCP/CustomUserVarsSection.tsx b/client/src/components/MCP/CustomUserVarsSection.tsx index 987613e24c..339b78f6b9 100644 --- a/client/src/components/MCP/CustomUserVarsSection.tsx +++ b/client/src/components/MCP/CustomUserVarsSection.tsx @@ -23,9 +23,10 @@ interface AuthFieldProps { hasValue: boolean; control: any; errors: any; + autoFocus?: boolean; } -function AuthField({ name, config, hasValue, control, errors }: AuthFieldProps) { +function AuthField({ name, config, hasValue, control, errors, autoFocus }: AuthFieldProps) { const localize = useLocalize(); const statusText = hasValue ? localize('com_ui_set') : localize('com_ui_unset'); @@ -85,6 +86,11 @@ function AuthField({ name, config, hasValue, control, errors }: AuthFieldProps)
- {Object.entries(fields).map(([key, config]) => { + {Object.entries(fields).map(([key, config], index) => { const hasValue = authValuesData?.authValueFlags?.[key] || false; return ( @@ -161,6 +167,8 @@ export default function CustomUserVarsSection({ hasValue={hasValue} control={control} errors={errors} + // eslint-disable-next-line jsx-a11y/no-autofocus -- See AuthField autoFocus comment for more details + autoFocus={index === 0} /> ); })} diff --git a/client/src/components/Plugins/Store/PluginAuthForm.tsx b/client/src/components/Plugins/Store/PluginAuthForm.tsx index f6eec0a6af..5af1948c11 100644 --- a/client/src/components/Plugins/Store/PluginAuthForm.tsx +++ b/client/src/components/Plugins/Store/PluginAuthForm.tsx @@ -56,6 +56,11 @@ function PluginAuthForm({ plugin, onSubmit, isEntityTool }: TPluginAuthFormProps aria-describedby={`${authField}-error`} aria-label={config.label} aria-required="true" + /* autoFocus is generally disabled due to the fact that it can disorient users, + * but in this case, the required field must be navigated to anyways, and the component's functionality + * emulates that of a new modal opening, where users would expect focus to be shifted to the new content */ + // eslint-disable-next-line jsx-a11y/no-autofocus + autoFocus={i === 0} {...register(authField, { required: `${config.label} is required.`, minLength: { @@ -70,7 +75,7 @@ function PluginAuthForm({ plugin, onSubmit, isEntityTool }: TPluginAuthFormProps {errors[authField] && ( - {errors?.[authField]?.message ?? ''} + {String(errors?.[authField]?.message ?? '')} )} From 4a1d2b0d941a565ef8514f9abce2218ba5b0f2ff Mon Sep 17 00:00:00 2001 From: Andrei Blizorukov <55080535+ablizorukov@users.noreply.github.com> Date: Mon, 19 Jan 2026 22:32:57 +0100 Subject: [PATCH 003/245] =?UTF-8?q?=F0=9F=93=8A=20fix:=20MeiliSearch=20Syn?= =?UTF-8?q?c=20Threshold=20&=20Document=20Count=20Accuracy=20(#11406)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🔧 fix: meilisearch incorrect count of total documents & performance improvement Temporary documents were counted & removed 2 redundant heavy calls to the database, use known information instead 🔧 fix: respect MEILI_SYNC_THRESHOLD value Do not sync with meili if threshold was not reached * refactor: reformat lint * fix: forces update if meili index settingsUpdated --- api/db/indexSync.js | 39 ++-- api/db/indexSync.spec.js | 465 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 487 insertions(+), 17 deletions(-) create mode 100644 api/db/indexSync.spec.js diff --git a/api/db/indexSync.js b/api/db/indexSync.js index b39f018b3a..8e8e999d92 100644 --- a/api/db/indexSync.js +++ b/api/db/indexSync.js @@ -13,6 +13,11 @@ const searchEnabled = isEnabled(process.env.SEARCH); const indexingDisabled = isEnabled(process.env.MEILI_NO_SYNC); let currentTimeout = null; +const defaultSyncThreshold = 1000; +const syncThreshold = process.env.MEILI_SYNC_THRESHOLD + ? parseInt(process.env.MEILI_SYNC_THRESHOLD, 10) + : defaultSyncThreshold; + class MeiliSearchClient { static instance = null; @@ -221,25 +226,25 @@ async function performSync(flowManager, flowId, flowType) { } // Check if we need to sync messages + logger.info('[indexSync] Requesting message sync progress...'); const messageProgress = await Message.getSyncProgress(); if (!messageProgress.isComplete || settingsUpdated) { logger.info( `[indexSync] Messages need syncing: ${messageProgress.totalProcessed}/${messageProgress.totalDocuments} indexed`, ); - // Check if we should do a full sync or incremental - const messageCount = await Message.countDocuments(); + const messageCount = messageProgress.totalDocuments; const messagesIndexed = messageProgress.totalProcessed; - const syncThreshold = parseInt(process.env.MEILI_SYNC_THRESHOLD || '1000', 10); + const unindexedMessages = messageCount - messagesIndexed; - if (messageCount - messagesIndexed > syncThreshold) { - logger.info('[indexSync] Starting full message sync due to large difference'); - await Message.syncWithMeili(); - messagesSync = true; - } else if (messageCount !== messagesIndexed) { - logger.warn('[indexSync] Messages out of sync, performing incremental sync'); + if (settingsUpdated || unindexedMessages > syncThreshold) { + logger.info(`[indexSync] Starting message sync (${unindexedMessages} unindexed)`); await Message.syncWithMeili(); messagesSync = true; + } else if (unindexedMessages > 0) { + logger.info( + `[indexSync] ${unindexedMessages} messages unindexed (below threshold: ${syncThreshold}, skipping)`, + ); } } else { logger.info( @@ -254,18 +259,18 @@ async function performSync(flowManager, flowId, flowType) { `[indexSync] Conversations need syncing: ${convoProgress.totalProcessed}/${convoProgress.totalDocuments} indexed`, ); - const convoCount = await Conversation.countDocuments(); + const convoCount = convoProgress.totalDocuments; const convosIndexed = convoProgress.totalProcessed; - const syncThreshold = parseInt(process.env.MEILI_SYNC_THRESHOLD || '1000', 10); - if (convoCount - convosIndexed > syncThreshold) { - logger.info('[indexSync] Starting full conversation sync due to large difference'); - await Conversation.syncWithMeili(); - convosSync = true; - } else if (convoCount !== convosIndexed) { - logger.warn('[indexSync] Convos out of sync, performing incremental sync'); + const unindexedConvos = convoCount - convosIndexed; + if (settingsUpdated || unindexedConvos > syncThreshold) { + logger.info(`[indexSync] Starting convos sync (${unindexedConvos} unindexed)`); await Conversation.syncWithMeili(); convosSync = true; + } else if (unindexedConvos > 0) { + logger.info( + `[indexSync] ${unindexedConvos} convos unindexed (below threshold: ${syncThreshold}, skipping)`, + ); } } else { logger.info( diff --git a/api/db/indexSync.spec.js b/api/db/indexSync.spec.js new file mode 100644 index 0000000000..c2e5901d6a --- /dev/null +++ b/api/db/indexSync.spec.js @@ -0,0 +1,465 @@ +/** + * Unit tests for performSync() function in indexSync.js + * + * Tests use real mongoose with mocked model methods, only mocking external calls. + */ + +const mongoose = require('mongoose'); + +// Mock only external dependencies (not internal classes/models) +const mockLogger = { + info: jest.fn(), + warn: jest.fn(), + error: jest.fn(), + debug: jest.fn(), +}; + +const mockMeiliHealth = jest.fn(); +const mockMeiliIndex = jest.fn(); +const mockBatchResetMeiliFlags = jest.fn(); +const mockIsEnabled = jest.fn(); +const mockGetLogStores = jest.fn(); + +// Create mock models that will be reused +const createMockModel = (collectionName) => ({ + collection: { name: collectionName }, + getSyncProgress: jest.fn(), + syncWithMeili: jest.fn(), + countDocuments: jest.fn(), +}); + +const originalMessageModel = mongoose.models.Message; +const originalConversationModel = mongoose.models.Conversation; + +// Mock external modules +jest.mock('@librechat/data-schemas', () => ({ + logger: mockLogger, +})); + +jest.mock('meilisearch', () => ({ + MeiliSearch: jest.fn(() => ({ + health: mockMeiliHealth, + index: mockMeiliIndex, + })), +})); + +jest.mock('./utils', () => ({ + batchResetMeiliFlags: mockBatchResetMeiliFlags, +})); + +jest.mock('@librechat/api', () => ({ + isEnabled: mockIsEnabled, + FlowStateManager: jest.fn(), +})); + +jest.mock('~/cache', () => ({ + getLogStores: mockGetLogStores, +})); + +// Set environment before module load +process.env.MEILI_HOST = 'http://localhost:7700'; +process.env.MEILI_MASTER_KEY = 'test-key'; +process.env.SEARCH = 'true'; +process.env.MEILI_SYNC_THRESHOLD = '1000'; // Set threshold before module loads + +describe('performSync() - syncThreshold logic', () => { + const ORIGINAL_ENV = process.env; + let Message; + let Conversation; + + beforeAll(() => { + Message = createMockModel('messages'); + Conversation = createMockModel('conversations'); + + mongoose.models.Message = Message; + mongoose.models.Conversation = Conversation; + }); + + beforeEach(() => { + // Reset all mocks + jest.clearAllMocks(); + // Reset modules to ensure fresh load of indexSync.js and its top-level consts (like syncThreshold) + jest.resetModules(); + + // Set up environment + process.env = { ...ORIGINAL_ENV }; + process.env.MEILI_HOST = 'http://localhost:7700'; + process.env.MEILI_MASTER_KEY = 'test-key'; + process.env.SEARCH = 'true'; + delete process.env.MEILI_NO_SYNC; + + // Re-ensure models are available in mongoose after resetModules + // We must require mongoose again to get the fresh instance that indexSync will use + const mongoose = require('mongoose'); + mongoose.models.Message = Message; + mongoose.models.Conversation = Conversation; + + // Mock isEnabled + mockIsEnabled.mockImplementation((val) => val === 'true' || val === true); + + // Mock MeiliSearch client responses + mockMeiliHealth.mockResolvedValue({ status: 'available' }); + mockMeiliIndex.mockReturnValue({ + getSettings: jest.fn().mockResolvedValue({ filterableAttributes: ['user'] }), + updateSettings: jest.fn().mockResolvedValue({}), + search: jest.fn().mockResolvedValue({ hits: [] }), + }); + + mockBatchResetMeiliFlags.mockResolvedValue(undefined); + }); + + afterEach(() => { + process.env = ORIGINAL_ENV; + }); + + afterAll(() => { + mongoose.models.Message = originalMessageModel; + mongoose.models.Conversation = originalConversationModel; + }); + + test('triggers sync when unindexed messages exceed syncThreshold', async () => { + // Arrange: Set threshold before module load + process.env.MEILI_SYNC_THRESHOLD = '1000'; + + // Arrange: 1050 unindexed messages > 1000 threshold + Message.getSyncProgress.mockResolvedValue({ + totalProcessed: 100, + totalDocuments: 1150, // 1050 unindexed + isComplete: false, + }); + + Conversation.getSyncProgress.mockResolvedValue({ + totalProcessed: 50, + totalDocuments: 50, + isComplete: true, + }); + + Message.syncWithMeili.mockResolvedValue(undefined); + + // Act + const indexSync = require('./indexSync'); + await indexSync(); + + // Assert: No countDocuments calls + expect(Message.countDocuments).not.toHaveBeenCalled(); + expect(Conversation.countDocuments).not.toHaveBeenCalled(); + + // Assert: Message sync triggered because 1050 > 1000 + expect(Message.syncWithMeili).toHaveBeenCalledTimes(1); + expect(mockLogger.info).toHaveBeenCalledWith( + '[indexSync] Messages need syncing: 100/1150 indexed', + ); + expect(mockLogger.info).toHaveBeenCalledWith( + '[indexSync] Starting message sync (1050 unindexed)', + ); + + // Assert: Conversation sync NOT triggered (already complete) + expect(Conversation.syncWithMeili).not.toHaveBeenCalled(); + }); + + test('skips sync when unindexed messages are below syncThreshold', async () => { + // Arrange: 50 unindexed messages < 1000 threshold + Message.getSyncProgress.mockResolvedValue({ + totalProcessed: 100, + totalDocuments: 150, // 50 unindexed + isComplete: false, + }); + + Conversation.getSyncProgress.mockResolvedValue({ + totalProcessed: 50, + totalDocuments: 50, + isComplete: true, + }); + + process.env.MEILI_SYNC_THRESHOLD = '1000'; + + // Act + const indexSync = require('./indexSync'); + await indexSync(); + + // Assert: No countDocuments calls + expect(Message.countDocuments).not.toHaveBeenCalled(); + expect(Conversation.countDocuments).not.toHaveBeenCalled(); + + // Assert: Message sync NOT triggered because 50 < 1000 + expect(Message.syncWithMeili).not.toHaveBeenCalled(); + expect(mockLogger.info).toHaveBeenCalledWith( + '[indexSync] Messages need syncing: 100/150 indexed', + ); + expect(mockLogger.info).toHaveBeenCalledWith( + '[indexSync] 50 messages unindexed (below threshold: 1000, skipping)', + ); + + // Assert: Conversation sync NOT triggered (already complete) + expect(Conversation.syncWithMeili).not.toHaveBeenCalled(); + }); + + test('respects syncThreshold at boundary (exactly at threshold)', async () => { + // Arrange: 1000 unindexed messages = 1000 threshold (NOT greater than) + Message.getSyncProgress.mockResolvedValue({ + totalProcessed: 100, + totalDocuments: 1100, // 1000 unindexed + isComplete: false, + }); + + Conversation.getSyncProgress.mockResolvedValue({ + totalProcessed: 0, + totalDocuments: 0, + isComplete: true, + }); + + process.env.MEILI_SYNC_THRESHOLD = '1000'; + + // Act + const indexSync = require('./indexSync'); + await indexSync(); + + // Assert: No countDocuments calls + expect(Message.countDocuments).not.toHaveBeenCalled(); + + // Assert: Message sync NOT triggered because 1000 is NOT > 1000 + expect(Message.syncWithMeili).not.toHaveBeenCalled(); + expect(mockLogger.info).toHaveBeenCalledWith( + '[indexSync] Messages need syncing: 100/1100 indexed', + ); + expect(mockLogger.info).toHaveBeenCalledWith( + '[indexSync] 1000 messages unindexed (below threshold: 1000, skipping)', + ); + }); + + test('triggers sync when unindexed is threshold + 1', async () => { + // Arrange: 1001 unindexed messages > 1000 threshold + Message.getSyncProgress.mockResolvedValue({ + totalProcessed: 100, + totalDocuments: 1101, // 1001 unindexed + isComplete: false, + }); + + Conversation.getSyncProgress.mockResolvedValue({ + totalProcessed: 0, + totalDocuments: 0, + isComplete: true, + }); + + Message.syncWithMeili.mockResolvedValue(undefined); + + process.env.MEILI_SYNC_THRESHOLD = '1000'; + + // Act + const indexSync = require('./indexSync'); + await indexSync(); + + // Assert: No countDocuments calls + expect(Message.countDocuments).not.toHaveBeenCalled(); + + // Assert: Message sync triggered because 1001 > 1000 + expect(Message.syncWithMeili).toHaveBeenCalledTimes(1); + expect(mockLogger.info).toHaveBeenCalledWith( + '[indexSync] Messages need syncing: 100/1101 indexed', + ); + expect(mockLogger.info).toHaveBeenCalledWith( + '[indexSync] Starting message sync (1001 unindexed)', + ); + }); + + test('uses totalDocuments from convoProgress for conversation sync decisions', async () => { + // Arrange: Messages complete, conversations need sync + Message.getSyncProgress.mockResolvedValue({ + totalProcessed: 100, + totalDocuments: 100, + isComplete: true, + }); + + Conversation.getSyncProgress.mockResolvedValue({ + totalProcessed: 50, + totalDocuments: 1100, // 1050 unindexed > 1000 threshold + isComplete: false, + }); + + Conversation.syncWithMeili.mockResolvedValue(undefined); + + process.env.MEILI_SYNC_THRESHOLD = '1000'; + + // Act + const indexSync = require('./indexSync'); + await indexSync(); + + // Assert: No countDocuments calls (the optimization) + expect(Message.countDocuments).not.toHaveBeenCalled(); + expect(Conversation.countDocuments).not.toHaveBeenCalled(); + + // Assert: Only conversation sync triggered + expect(Message.syncWithMeili).not.toHaveBeenCalled(); + expect(Conversation.syncWithMeili).toHaveBeenCalledTimes(1); + expect(mockLogger.info).toHaveBeenCalledWith( + '[indexSync] Conversations need syncing: 50/1100 indexed', + ); + expect(mockLogger.info).toHaveBeenCalledWith( + '[indexSync] Starting convos sync (1050 unindexed)', + ); + }); + + test('skips sync when collections are fully synced', async () => { + // Arrange: Everything already synced + Message.getSyncProgress.mockResolvedValue({ + totalProcessed: 100, + totalDocuments: 100, + isComplete: true, + }); + + Conversation.getSyncProgress.mockResolvedValue({ + totalProcessed: 50, + totalDocuments: 50, + isComplete: true, + }); + + // Act + const indexSync = require('./indexSync'); + await indexSync(); + + // Assert: No countDocuments calls + expect(Message.countDocuments).not.toHaveBeenCalled(); + expect(Conversation.countDocuments).not.toHaveBeenCalled(); + + // Assert: No sync triggered + expect(Message.syncWithMeili).not.toHaveBeenCalled(); + expect(Conversation.syncWithMeili).not.toHaveBeenCalled(); + + // Assert: Correct logs + expect(mockLogger.info).toHaveBeenCalledWith('[indexSync] Messages are fully synced: 100/100'); + expect(mockLogger.info).toHaveBeenCalledWith( + '[indexSync] Conversations are fully synced: 50/50', + ); + }); + + test('triggers message sync when settingsUpdated even if below syncThreshold', async () => { + // Arrange: Only 50 unindexed messages (< 1000 threshold), but settings were updated + Message.getSyncProgress.mockResolvedValue({ + totalProcessed: 100, + totalDocuments: 150, // 50 unindexed + isComplete: false, + }); + + Conversation.getSyncProgress.mockResolvedValue({ + totalProcessed: 50, + totalDocuments: 50, + isComplete: true, + }); + + Message.syncWithMeili.mockResolvedValue(undefined); + + // Mock settings update scenario + mockMeiliIndex.mockReturnValue({ + getSettings: jest.fn().mockResolvedValue({ filterableAttributes: [] }), // No user field + updateSettings: jest.fn().mockResolvedValue({}), + search: jest.fn().mockResolvedValue({ hits: [] }), + }); + + process.env.MEILI_SYNC_THRESHOLD = '1000'; + + // Act + const indexSync = require('./indexSync'); + await indexSync(); + + // Assert: Flags were reset due to settings update + expect(mockBatchResetMeiliFlags).toHaveBeenCalledWith(Message.collection); + expect(mockBatchResetMeiliFlags).toHaveBeenCalledWith(Conversation.collection); + + // Assert: Message sync triggered despite being below threshold (50 < 1000) + expect(Message.syncWithMeili).toHaveBeenCalledTimes(1); + expect(mockLogger.info).toHaveBeenCalledWith( + '[indexSync] Settings updated. Forcing full re-sync to reindex with new configuration...', + ); + expect(mockLogger.info).toHaveBeenCalledWith( + '[indexSync] Starting message sync (50 unindexed)', + ); + }); + + test('triggers conversation sync when settingsUpdated even if below syncThreshold', async () => { + // Arrange: Messages complete, conversations have 50 unindexed (< 1000 threshold), but settings were updated + Message.getSyncProgress.mockResolvedValue({ + totalProcessed: 100, + totalDocuments: 100, + isComplete: true, + }); + + Conversation.getSyncProgress.mockResolvedValue({ + totalProcessed: 50, + totalDocuments: 100, // 50 unindexed + isComplete: false, + }); + + Conversation.syncWithMeili.mockResolvedValue(undefined); + + // Mock settings update scenario + mockMeiliIndex.mockReturnValue({ + getSettings: jest.fn().mockResolvedValue({ filterableAttributes: [] }), // No user field + updateSettings: jest.fn().mockResolvedValue({}), + search: jest.fn().mockResolvedValue({ hits: [] }), + }); + + process.env.MEILI_SYNC_THRESHOLD = '1000'; + + // Act + const indexSync = require('./indexSync'); + await indexSync(); + + // Assert: Flags were reset due to settings update + expect(mockBatchResetMeiliFlags).toHaveBeenCalledWith(Message.collection); + expect(mockBatchResetMeiliFlags).toHaveBeenCalledWith(Conversation.collection); + + // Assert: Conversation sync triggered despite being below threshold (50 < 1000) + expect(Conversation.syncWithMeili).toHaveBeenCalledTimes(1); + expect(mockLogger.info).toHaveBeenCalledWith( + '[indexSync] Settings updated. Forcing full re-sync to reindex with new configuration...', + ); + expect(mockLogger.info).toHaveBeenCalledWith('[indexSync] Starting convos sync (50 unindexed)'); + }); + + test('triggers both message and conversation sync when settingsUpdated even if both below syncThreshold', async () => { + // Arrange: Set threshold before module load + process.env.MEILI_SYNC_THRESHOLD = '1000'; + + // Arrange: Both have documents below threshold (50 each), but settings were updated + Message.getSyncProgress.mockResolvedValue({ + totalProcessed: 100, + totalDocuments: 150, // 50 unindexed + isComplete: false, + }); + + Conversation.getSyncProgress.mockResolvedValue({ + totalProcessed: 50, + totalDocuments: 100, // 50 unindexed + isComplete: false, + }); + + Message.syncWithMeili.mockResolvedValue(undefined); + Conversation.syncWithMeili.mockResolvedValue(undefined); + + // Mock settings update scenario + mockMeiliIndex.mockReturnValue({ + getSettings: jest.fn().mockResolvedValue({ filterableAttributes: [] }), // No user field + updateSettings: jest.fn().mockResolvedValue({}), + search: jest.fn().mockResolvedValue({ hits: [] }), + }); + + // Act + const indexSync = require('./indexSync'); + await indexSync(); + + // Assert: Flags were reset due to settings update + expect(mockBatchResetMeiliFlags).toHaveBeenCalledWith(Message.collection); + expect(mockBatchResetMeiliFlags).toHaveBeenCalledWith(Conversation.collection); + + // Assert: Both syncs triggered despite both being below threshold + expect(Message.syncWithMeili).toHaveBeenCalledTimes(1); + expect(Conversation.syncWithMeili).toHaveBeenCalledTimes(1); + expect(mockLogger.info).toHaveBeenCalledWith( + '[indexSync] Settings updated. Forcing full re-sync to reindex with new configuration...', + ); + expect(mockLogger.info).toHaveBeenCalledWith( + '[indexSync] Starting message sync (50 unindexed)', + ); + expect(mockLogger.info).toHaveBeenCalledWith('[indexSync] Starting convos sync (50 unindexed)'); + }); +}); From e509ba5be046736307536c695c5d2c1701d5eb77 Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Tue, 20 Jan 2026 08:45:43 -0500 Subject: [PATCH 004/245] =?UTF-8?q?=F0=9F=AA=84=20fix:=20Code=20Block=20ha?= =?UTF-8?q?ndling=20in=20Artifact=20Updates=20(#11417)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Improved detection of code blocks to support both language identifiers and plain code fences. * Updated tests to cover various scenarios, including edge cases with different language identifiers and multiline content. * Ensured proper handling of code blocks with trailing whitespace and complex syntax. --- api/server/services/Artifacts/update.js | 18 +- api/server/services/Artifacts/update.spec.js | 263 +++++++++++++++++++ 2 files changed, 277 insertions(+), 4 deletions(-) diff --git a/api/server/services/Artifacts/update.js b/api/server/services/Artifacts/update.js index d068593f8c..be1644b11c 100644 --- a/api/server/services/Artifacts/update.js +++ b/api/server/services/Artifacts/update.js @@ -73,15 +73,25 @@ const replaceArtifactContent = (originalText, artifact, original, updated) => { return null; } - // Check if there are code blocks - const codeBlockStart = artifactContent.indexOf('```\n', contentStart); + // Check if there are code blocks - handle both ```\n and ```lang\n formats + let codeBlockStart = artifactContent.indexOf('```', contentStart); const codeBlockEnd = artifactContent.lastIndexOf('\n```', contentEnd); + // If we found opening backticks, find the actual newline (skipping any language identifier) + if (codeBlockStart !== -1) { + const newlineAfterBackticks = artifactContent.indexOf('\n', codeBlockStart); + if (newlineAfterBackticks !== -1 && newlineAfterBackticks < contentEnd) { + codeBlockStart = newlineAfterBackticks; + } else { + codeBlockStart = -1; + } + } + // Determine where to look for the original content let searchStart, searchEnd; if (codeBlockStart !== -1) { - // Code block starts - searchStart = codeBlockStart + 4; // after ```\n + // Code block starts - searchStart is right after the newline following ```[lang] + searchStart = codeBlockStart + 1; // after the newline if (codeBlockEnd !== -1 && codeBlockEnd > codeBlockStart) { // Code block has proper ending diff --git a/api/server/services/Artifacts/update.spec.js b/api/server/services/Artifacts/update.spec.js index 2a3e0bbe39..39a4f02863 100644 --- a/api/server/services/Artifacts/update.spec.js +++ b/api/server/services/Artifacts/update.spec.js @@ -494,5 +494,268 @@ ${original}`; /```\n {2}function test\(\) \{\n {4}return \{\n {6}value: 100\n {4}\};\n {2}\}\n```/, ); }); + + test('should handle code blocks with language identifiers (```svg, ```html, etc.)', () => { + const svgContent = ` + + +`; + + /** Artifact with language identifier in code block */ + const artifactText = `${ARTIFACT_START}{identifier="test-svg" type="image/svg+xml" title="Test SVG"} +\`\`\`svg +${svgContent} +\`\`\` +${ARTIFACT_END}`; + + const message = { text: artifactText }; + const artifacts = findAllArtifacts(message); + expect(artifacts).toHaveLength(1); + + const updatedSvg = svgContent.replace('#FFFFFF', '#131313'); + const result = replaceArtifactContent(artifactText, artifacts[0], svgContent, updatedSvg); + + expect(result).not.toBeNull(); + expect(result).toContain('#131313'); + expect(result).not.toContain('#FFFFFF'); + expect(result).toMatch(/```svg\n/); + }); + + test('should handle code blocks with complex language identifiers', () => { + const htmlContent = ` + +Test +Hello +`; + + const artifactText = `${ARTIFACT_START}{identifier="test-html" type="text/html" title="Test HTML"} +\`\`\`html +${htmlContent} +\`\`\` +${ARTIFACT_END}`; + + const message = { text: artifactText }; + const artifacts = findAllArtifacts(message); + + const updatedHtml = htmlContent.replace('Hello', 'Updated'); + const result = replaceArtifactContent(artifactText, artifacts[0], htmlContent, updatedHtml); + + expect(result).not.toBeNull(); + expect(result).toContain('Updated'); + expect(result).toMatch(/```html\n/); + }); + }); + + describe('code block edge cases', () => { + test('should handle code block without language identifier (```\\n)', () => { + const content = 'const x = 1;\nconst y = 2;'; + const artifactText = `${ARTIFACT_START}{identifier="test" type="text/plain" title="Test"} +\`\`\` +${content} +\`\`\` +${ARTIFACT_END}`; + + const message = { text: artifactText }; + const artifacts = findAllArtifacts(message); + + const result = replaceArtifactContent(artifactText, artifacts[0], content, 'updated'); + + expect(result).not.toBeNull(); + expect(result).toContain('updated'); + expect(result).toMatch(/```\nupdated\n```/); + }); + + test('should handle various language identifiers', () => { + const languages = [ + 'javascript', + 'typescript', + 'python', + 'jsx', + 'tsx', + 'css', + 'json', + 'xml', + 'markdown', + 'md', + ]; + + for (const lang of languages) { + const content = `test content for ${lang}`; + const artifactText = `${ARTIFACT_START}{identifier="test-${lang}" type="text/plain" title="Test"} +\`\`\`${lang} +${content} +\`\`\` +${ARTIFACT_END}`; + + const message = { text: artifactText }; + const artifacts = findAllArtifacts(message); + expect(artifacts).toHaveLength(1); + + const result = replaceArtifactContent(artifactText, artifacts[0], content, 'updated'); + + expect(result).not.toBeNull(); + expect(result).toContain('updated'); + expect(result).toMatch(new RegExp(`\`\`\`${lang}\\n`)); + } + }); + + test('should handle single character language identifier', () => { + const content = 'single char lang'; + const artifactText = `${ARTIFACT_START}{identifier="test" type="text/plain" title="Test"} +\`\`\`r +${content} +\`\`\` +${ARTIFACT_END}`; + + const message = { text: artifactText }; + const artifacts = findAllArtifacts(message); + + const result = replaceArtifactContent(artifactText, artifacts[0], content, 'updated'); + + expect(result).not.toBeNull(); + expect(result).toContain('updated'); + expect(result).toMatch(/```r\n/); + }); + + test('should handle code block with content that looks like code fence', () => { + const content = 'Line 1\nSome text with ``` backticks in middle\nLine 3'; + const artifactText = `${ARTIFACT_START}{identifier="test" type="text/plain" title="Test"} +\`\`\`text +${content} +\`\`\` +${ARTIFACT_END}`; + + const message = { text: artifactText }; + const artifacts = findAllArtifacts(message); + + const result = replaceArtifactContent(artifactText, artifacts[0], content, 'updated'); + + expect(result).not.toBeNull(); + expect(result).toContain('updated'); + }); + + test('should handle code block with trailing whitespace in language line', () => { + const content = 'whitespace test'; + /** Note: trailing spaces after 'python' */ + const artifactText = `${ARTIFACT_START}{identifier="test" type="text/plain" title="Test"} +\`\`\`python +${content} +\`\`\` +${ARTIFACT_END}`; + + const message = { text: artifactText }; + const artifacts = findAllArtifacts(message); + + const result = replaceArtifactContent(artifactText, artifacts[0], content, 'updated'); + + expect(result).not.toBeNull(); + expect(result).toContain('updated'); + }); + + test('should handle react/jsx content with complex syntax', () => { + const jsxContent = `function App() { + const [count, setCount] = useState(0); + return ( +
+

Count: {count}

+ +
+ ); +}`; + + const artifactText = `${ARTIFACT_START}{identifier="react-app" type="application/vnd.react" title="React App"} +\`\`\`jsx +${jsxContent} +\`\`\` +${ARTIFACT_END}`; + + const message = { text: artifactText }; + const artifacts = findAllArtifacts(message); + + const updatedJsx = jsxContent.replace('Increment', 'Click me'); + const result = replaceArtifactContent(artifactText, artifacts[0], jsxContent, updatedJsx); + + expect(result).not.toBeNull(); + expect(result).toContain('Click me'); + expect(result).not.toContain('Increment'); + expect(result).toMatch(/```jsx\n/); + }); + + test('should handle mermaid diagram content', () => { + const mermaidContent = `graph TD + A[Start] --> B{Is it?} + B -->|Yes| C[OK] + B -->|No| D[End]`; + + const artifactText = `${ARTIFACT_START}{identifier="diagram" type="application/vnd.mermaid" title="Flow"} +\`\`\`mermaid +${mermaidContent} +\`\`\` +${ARTIFACT_END}`; + + const message = { text: artifactText }; + const artifacts = findAllArtifacts(message); + + const updatedMermaid = mermaidContent.replace('Start', 'Begin'); + const result = replaceArtifactContent( + artifactText, + artifacts[0], + mermaidContent, + updatedMermaid, + ); + + expect(result).not.toBeNull(); + expect(result).toContain('Begin'); + expect(result).toMatch(/```mermaid\n/); + }); + + test('should handle artifact without code block (plain text)', () => { + const content = 'Just plain text without code fences'; + const artifactText = `${ARTIFACT_START}{identifier="plain" type="text/plain" title="Plain"} +${content} +${ARTIFACT_END}`; + + const message = { text: artifactText }; + const artifacts = findAllArtifacts(message); + + const result = replaceArtifactContent( + artifactText, + artifacts[0], + content, + 'updated plain text', + ); + + expect(result).not.toBeNull(); + expect(result).toContain('updated plain text'); + expect(result).not.toContain('```'); + }); + + test('should handle multiline content with various newline patterns', () => { + const content = `Line 1 +Line 2 + +Line 4 after empty line + Indented line + Double indented`; + + const artifactText = `${ARTIFACT_START}{identifier="test" type="text/plain" title="Test"} +\`\`\` +${content} +\`\`\` +${ARTIFACT_END}`; + + const message = { text: artifactText }; + const artifacts = findAllArtifacts(message); + + const updated = content.replace('Line 1', 'First Line'); + const result = replaceArtifactContent(artifactText, artifacts[0], content, updated); + + expect(result).not.toBeNull(); + expect(result).toContain('First Line'); + expect(result).toContain(' Indented line'); + expect(result).toContain(' Double indented'); + }); }); }); From 32e6f3b8e50edd0cf3cdbbdd50927f588c4a827d Mon Sep 17 00:00:00 2001 From: Dustin Healy <54083382+dustinhealy@users.noreply.github.com> Date: Tue, 20 Jan 2026 11:41:28 -0800 Subject: [PATCH 005/245] =?UTF-8?q?=F0=9F=93=A2=20fix:=20Alert=20for=20Age?= =?UTF-8?q?nt=20Builder=20Name=20Invalidation=20(#11430)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/src/components/SidePanel/Agents/AgentConfig.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/client/src/components/SidePanel/Agents/AgentConfig.tsx b/client/src/components/SidePanel/Agents/AgentConfig.tsx index 2e247a00f0..a81ef780a9 100644 --- a/client/src/components/SidePanel/Agents/AgentConfig.tsx +++ b/client/src/components/SidePanel/Agents/AgentConfig.tsx @@ -209,6 +209,7 @@ export default function AgentConfig() { 'mt-1 w-56 text-sm text-red-500', errors.name ? 'visible h-auto' : 'invisible h-0', )} + role="alert" > {errors.name ? errors.name.message : ' '} From 36c5a88c4eca0a489cfd42bdf489a39d4dceb19d Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Tue, 20 Jan 2026 14:43:19 -0500 Subject: [PATCH 006/245] =?UTF-8?q?=F0=9F=92=B0=20fix:=20Multi-Agent=20Tok?= =?UTF-8?q?en=20Spending=20&=20Prevent=20Double-Spend=20(#11433)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: Token Spending Logic for Multi-Agents on Abort Scenarios * Implemented logic to skip token spending if a conversation is aborted, preventing double-spending. * Introduced `spendCollectedUsage` function to handle token spending for multiple models during aborts, ensuring accurate accounting for parallel agents. * Updated `GenerationJobManager` to store and retrieve collected usage data for improved abort handling. * Added comprehensive tests for the new functionality, covering various scenarios including cache token handling and parallel agent usage. * fix: Memory Context Handling for Multi-Agents * Refactored `buildMessages` method to pass memory context to parallel agents, ensuring they share the same user context. * Improved handling of memory context when no existing instructions are present for parallel agents. * Added comprehensive tests to verify memory context propagation and behavior under various scenarios, including cases with no memory available and empty agent configurations. * Enhanced logging for better traceability of memory context additions to agents. * chore: Memory Context Documentation for Parallel Agents * Updated documentation in the `AgentClient` class to clarify the in-place mutation of agentConfig objects when passing memory context to parallel agents. * Added notes on the implications of mutating objects directly to ensure all parallel agents receive the correct memory context before execution. * chore: UsageMetadata Interface docs for Token Spending * Expanded the UsageMetadata interface to support both OpenAI and Anthropic cache token formats. * Added detailed documentation for cache token properties, including mutually exclusive fields for different model types. * Improved clarity on how to access cache token details for accurate token spending tracking. * fix: Enhance Token Spending Logic in Abort Middleware * Refactored `spendCollectedUsage` function to utilize Promise.all for concurrent token spending, improving performance and ensuring all operations complete before clearing the collectedUsage array. * Added documentation to clarify the importance of clearing the collectedUsage array to prevent double-spending in abort scenarios. * Updated tests to verify the correct behavior of the spending logic and the clearing of the array after spending operations. --- api/server/controllers/agents/client.js | 45 +- api/server/controllers/agents/client.test.js | 220 ++++++++ api/server/middleware/abortMiddleware.js | 100 +++- api/server/middleware/abortMiddleware.spec.js | 428 ++++++++++++++++ .../services/Endpoints/agents/initialize.js | 9 +- .../api/src/stream/GenerationJobManager.ts | 41 +- .../stream/__tests__/collectedUsage.spec.ts | 482 ++++++++++++++++++ .../implementations/InMemoryJobStore.ts | 31 +- .../stream/implementations/RedisJobStore.ts | 38 +- packages/api/src/stream/index.ts | 5 +- .../api/src/stream/interfaces/IJobStore.ts | 69 +++ 11 files changed, 1440 insertions(+), 28 deletions(-) create mode 100644 api/server/middleware/abortMiddleware.spec.js create mode 100644 packages/api/src/stream/__tests__/collectedUsage.spec.ts diff --git a/api/server/controllers/agents/client.js b/api/server/controllers/agents/client.js index 2b5872411b..5f3618de4c 100644 --- a/api/server/controllers/agents/client.js +++ b/api/server/controllers/agents/client.js @@ -522,14 +522,36 @@ class AgentClient extends BaseClient { } const withoutKeys = await this.useMemory(); - if (withoutKeys) { - systemContent += `${memoryInstructions}\n\n# Existing memory about the user:\n${withoutKeys}`; + const memoryContext = withoutKeys + ? `${memoryInstructions}\n\n# Existing memory about the user:\n${withoutKeys}` + : ''; + if (memoryContext) { + systemContent += memoryContext; } if (systemContent) { this.options.agent.instructions = systemContent; } + /** + * Pass memory context to parallel agents (addedConvo) so they have the same user context. + * + * NOTE: This intentionally mutates the agentConfig objects in place. The agentConfigs Map + * holds references to config objects that will be passed to the graph runtime. Mutating + * them here ensures all parallel agents receive the memory context before execution starts. + * Creating new objects would not work because the Map references would still point to the old objects. + */ + if (memoryContext && this.agentConfigs?.size > 0) { + for (const [agentId, agentConfig] of this.agentConfigs.entries()) { + if (agentConfig.instructions) { + agentConfig.instructions = agentConfig.instructions + '\n\n' + memoryContext; + } else { + agentConfig.instructions = memoryContext; + } + logger.debug(`[AgentClient] Added memory context to parallel agent: ${agentId}`); + } + } + return result; } @@ -1084,11 +1106,20 @@ class AgentClient extends BaseClient { this.artifactPromises.push(...attachments); } - await this.recordCollectedUsage({ - context: 'message', - balance: balanceConfig, - transactions: transactionsConfig, - }); + /** Skip token spending if aborted - the abort handler (abortMiddleware.js) handles it + This prevents double-spending when user aborts via `/api/agents/chat/abort` */ + const wasAborted = abortController?.signal?.aborted; + if (!wasAborted) { + await this.recordCollectedUsage({ + context: 'message', + balance: balanceConfig, + transactions: transactionsConfig, + }); + } else { + logger.debug( + '[api/server/controllers/agents/client.js #chatCompletion] Skipping token spending - handled by abort middleware', + ); + } } catch (err) { logger.error( '[api/server/controllers/agents/client.js #chatCompletion] Error in cleanup phase', diff --git a/api/server/controllers/agents/client.test.js b/api/server/controllers/agents/client.test.js index 14f0df9bb0..402d011fd6 100644 --- a/api/server/controllers/agents/client.test.js +++ b/api/server/controllers/agents/client.test.js @@ -1849,4 +1849,224 @@ describe('AgentClient - titleConvo', () => { }); }); }); + + describe('buildMessages - memory context for parallel agents', () => { + let client; + let mockReq; + let mockRes; + let mockAgent; + let mockOptions; + + beforeEach(() => { + jest.clearAllMocks(); + + mockAgent = { + id: 'primary-agent', + name: 'Primary Agent', + endpoint: EModelEndpoint.openAI, + provider: EModelEndpoint.openAI, + instructions: 'Primary agent instructions', + model_parameters: { + model: 'gpt-4', + }, + tools: [], + }; + + mockReq = { + user: { + id: 'user-123', + personalization: { + memories: true, + }, + }, + body: { + endpoint: EModelEndpoint.openAI, + }, + config: { + memory: { + disabled: false, + }, + }, + }; + + mockRes = {}; + + mockOptions = { + req: mockReq, + res: mockRes, + agent: mockAgent, + endpoint: EModelEndpoint.agents, + }; + + client = new AgentClient(mockOptions); + client.conversationId = 'convo-123'; + client.responseMessageId = 'response-123'; + client.shouldSummarize = false; + client.maxContextTokens = 4096; + }); + + it('should pass memory context to parallel agents (addedConvo)', async () => { + const memoryContent = 'User prefers dark mode. User is a software developer.'; + client.useMemory = jest.fn().mockResolvedValue(memoryContent); + + const parallelAgent1 = { + id: 'parallel-agent-1', + name: 'Parallel Agent 1', + instructions: 'Parallel agent 1 instructions', + provider: EModelEndpoint.openAI, + }; + + const parallelAgent2 = { + id: 'parallel-agent-2', + name: 'Parallel Agent 2', + instructions: 'Parallel agent 2 instructions', + provider: EModelEndpoint.anthropic, + }; + + client.agentConfigs = new Map([ + ['parallel-agent-1', parallelAgent1], + ['parallel-agent-2', parallelAgent2], + ]); + + const messages = [ + { + messageId: 'msg-1', + parentMessageId: null, + sender: 'User', + text: 'Hello', + isCreatedByUser: true, + }, + ]; + + await client.buildMessages(messages, null, { + instructions: 'Base instructions', + additional_instructions: null, + }); + + expect(client.useMemory).toHaveBeenCalled(); + + expect(client.options.agent.instructions).toContain('Base instructions'); + expect(client.options.agent.instructions).toContain(memoryContent); + + expect(parallelAgent1.instructions).toContain('Parallel agent 1 instructions'); + expect(parallelAgent1.instructions).toContain(memoryContent); + + expect(parallelAgent2.instructions).toContain('Parallel agent 2 instructions'); + expect(parallelAgent2.instructions).toContain(memoryContent); + }); + + it('should not modify parallel agents when no memory context is available', async () => { + client.useMemory = jest.fn().mockResolvedValue(undefined); + + const parallelAgent = { + id: 'parallel-agent-1', + name: 'Parallel Agent 1', + instructions: 'Original parallel instructions', + provider: EModelEndpoint.openAI, + }; + + client.agentConfigs = new Map([['parallel-agent-1', parallelAgent]]); + + const messages = [ + { + messageId: 'msg-1', + parentMessageId: null, + sender: 'User', + text: 'Hello', + isCreatedByUser: true, + }, + ]; + + await client.buildMessages(messages, null, { + instructions: 'Base instructions', + additional_instructions: null, + }); + + expect(parallelAgent.instructions).toBe('Original parallel instructions'); + }); + + it('should handle parallel agents without existing instructions', async () => { + const memoryContent = 'User is a data scientist.'; + client.useMemory = jest.fn().mockResolvedValue(memoryContent); + + const parallelAgentNoInstructions = { + id: 'parallel-agent-no-instructions', + name: 'Parallel Agent No Instructions', + provider: EModelEndpoint.openAI, + }; + + client.agentConfigs = new Map([ + ['parallel-agent-no-instructions', parallelAgentNoInstructions], + ]); + + const messages = [ + { + messageId: 'msg-1', + parentMessageId: null, + sender: 'User', + text: 'Hello', + isCreatedByUser: true, + }, + ]; + + await client.buildMessages(messages, null, { + instructions: null, + additional_instructions: null, + }); + + expect(parallelAgentNoInstructions.instructions).toContain(memoryContent); + }); + + it('should not modify agentConfigs when none exist', async () => { + const memoryContent = 'User prefers concise responses.'; + client.useMemory = jest.fn().mockResolvedValue(memoryContent); + + client.agentConfigs = null; + + const messages = [ + { + messageId: 'msg-1', + parentMessageId: null, + sender: 'User', + text: 'Hello', + isCreatedByUser: true, + }, + ]; + + await expect( + client.buildMessages(messages, null, { + instructions: 'Base instructions', + additional_instructions: null, + }), + ).resolves.not.toThrow(); + + expect(client.options.agent.instructions).toContain(memoryContent); + }); + + it('should handle empty agentConfigs map', async () => { + const memoryContent = 'User likes detailed explanations.'; + client.useMemory = jest.fn().mockResolvedValue(memoryContent); + + client.agentConfigs = new Map(); + + const messages = [ + { + messageId: 'msg-1', + parentMessageId: null, + sender: 'User', + text: 'Hello', + isCreatedByUser: true, + }, + ]; + + await expect( + client.buildMessages(messages, null, { + instructions: 'Base instructions', + additional_instructions: null, + }), + ).resolves.not.toThrow(); + + expect(client.options.agent.instructions).toContain(memoryContent); + }); + }); }); diff --git a/api/server/middleware/abortMiddleware.js b/api/server/middleware/abortMiddleware.js index b85f1439cc..d07a09682d 100644 --- a/api/server/middleware/abortMiddleware.js +++ b/api/server/middleware/abortMiddleware.js @@ -7,13 +7,89 @@ const { sanitizeMessageForTransmit, } = require('@librechat/api'); const { isAssistantsEndpoint, ErrorTypes } = require('librechat-data-provider'); +const { spendTokens, spendStructuredTokens } = require('~/models/spendTokens'); const { truncateText, smartTruncateText } = require('~/app/clients/prompts'); const clearPendingReq = require('~/cache/clearPendingReq'); const { sendError } = require('~/server/middleware/error'); -const { spendTokens } = require('~/models/spendTokens'); const { saveMessage, getConvo } = require('~/models'); const { abortRun } = require('./abortRun'); +/** + * Spend tokens for all models from collected usage. + * This handles both sequential and parallel agent execution. + * + * IMPORTANT: After spending, this function clears the collectedUsage array + * to prevent double-spending. The array is shared with AgentClient.collectedUsage, + * so clearing it here prevents the finally block from also spending tokens. + * + * @param {Object} params + * @param {string} params.userId - User ID + * @param {string} params.conversationId - Conversation ID + * @param {Array} params.collectedUsage - Usage metadata from all models + * @param {string} [params.fallbackModel] - Fallback model name if not in usage + */ +async function spendCollectedUsage({ userId, conversationId, collectedUsage, fallbackModel }) { + if (!collectedUsage || collectedUsage.length === 0) { + return; + } + + const spendPromises = []; + + for (const usage of collectedUsage) { + if (!usage) { + continue; + } + + // Support both OpenAI format (input_token_details) and Anthropic format (cache_*_input_tokens) + const cache_creation = + Number(usage.input_token_details?.cache_creation) || + Number(usage.cache_creation_input_tokens) || + 0; + const cache_read = + Number(usage.input_token_details?.cache_read) || Number(usage.cache_read_input_tokens) || 0; + + const txMetadata = { + context: 'abort', + conversationId, + user: userId, + model: usage.model ?? fallbackModel, + }; + + if (cache_creation > 0 || cache_read > 0) { + spendPromises.push( + spendStructuredTokens(txMetadata, { + promptTokens: { + input: usage.input_tokens, + write: cache_creation, + read: cache_read, + }, + completionTokens: usage.output_tokens, + }).catch((err) => { + logger.error('[abortMiddleware] Error spending structured tokens for abort', err); + }), + ); + continue; + } + + spendPromises.push( + spendTokens(txMetadata, { + promptTokens: usage.input_tokens, + completionTokens: usage.output_tokens, + }).catch((err) => { + logger.error('[abortMiddleware] Error spending tokens for abort', err); + }), + ); + } + + // Wait for all token spending to complete + await Promise.all(spendPromises); + + // Clear the array to prevent double-spending from the AgentClient finally block. + // The collectedUsage array is shared by reference with AgentClient.collectedUsage, + // so clearing it here ensures recordCollectedUsage() sees an empty array and returns early. + collectedUsage.length = 0; +} + /** * Abort an active message generation. * Uses GenerationJobManager for all agent requests. @@ -39,9 +115,8 @@ async function abortMessage(req, res) { return; } - const { jobData, content, text } = abortResult; + const { jobData, content, text, collectedUsage } = abortResult; - // Count tokens and spend them const completionTokens = await countTokens(text); const promptTokens = jobData?.promptTokens ?? 0; @@ -62,10 +137,21 @@ async function abortMessage(req, res) { tokenCount: completionTokens, }; - await spendTokens( - { ...responseMessage, context: 'incomplete', user: userId }, - { promptTokens, completionTokens }, - ); + // Spend tokens for ALL models from collectedUsage (handles parallel agents/addedConvo) + if (collectedUsage && collectedUsage.length > 0) { + await spendCollectedUsage({ + userId, + conversationId: jobData?.conversationId, + collectedUsage, + fallbackModel: jobData?.model, + }); + } else { + // Fallback: no collected usage, use text-based token counting for primary model only + await spendTokens( + { ...responseMessage, context: 'incomplete', user: userId }, + { promptTokens, completionTokens }, + ); + } await saveMessage( req, diff --git a/api/server/middleware/abortMiddleware.spec.js b/api/server/middleware/abortMiddleware.spec.js new file mode 100644 index 0000000000..93f2ce558b --- /dev/null +++ b/api/server/middleware/abortMiddleware.spec.js @@ -0,0 +1,428 @@ +/** + * Tests for abortMiddleware - spendCollectedUsage function + * + * This tests the token spending logic for abort scenarios, + * particularly for parallel agents (addedConvo) where multiple + * models need their tokens spent. + */ + +const mockSpendTokens = jest.fn().mockResolvedValue(); +const mockSpendStructuredTokens = jest.fn().mockResolvedValue(); + +jest.mock('~/models/spendTokens', () => ({ + spendTokens: (...args) => mockSpendTokens(...args), + spendStructuredTokens: (...args) => mockSpendStructuredTokens(...args), +})); + +jest.mock('@librechat/data-schemas', () => ({ + logger: { + debug: jest.fn(), + error: jest.fn(), + warn: jest.fn(), + info: jest.fn(), + }, +})); + +jest.mock('@librechat/api', () => ({ + countTokens: jest.fn().mockResolvedValue(100), + isEnabled: jest.fn().mockReturnValue(false), + sendEvent: jest.fn(), + GenerationJobManager: { + abortJob: jest.fn(), + }, + sanitizeMessageForTransmit: jest.fn((msg) => msg), +})); + +jest.mock('librechat-data-provider', () => ({ + isAssistantsEndpoint: jest.fn().mockReturnValue(false), + ErrorTypes: { INVALID_REQUEST: 'INVALID_REQUEST', NO_SYSTEM_MESSAGES: 'NO_SYSTEM_MESSAGES' }, +})); + +jest.mock('~/app/clients/prompts', () => ({ + truncateText: jest.fn((text) => text), + smartTruncateText: jest.fn((text) => text), +})); + +jest.mock('~/cache/clearPendingReq', () => jest.fn().mockResolvedValue()); + +jest.mock('~/server/middleware/error', () => ({ + sendError: jest.fn(), +})); + +jest.mock('~/models', () => ({ + saveMessage: jest.fn().mockResolvedValue(), + getConvo: jest.fn().mockResolvedValue({ title: 'Test Chat' }), +})); + +jest.mock('./abortRun', () => ({ + abortRun: jest.fn(), +})); + +// Import the module after mocks are set up +// We need to extract the spendCollectedUsage function for testing +// Since it's not exported, we'll test it through the handleAbort flow + +describe('abortMiddleware - spendCollectedUsage', () => { + beforeEach(() => { + jest.clearAllMocks(); + }); + + describe('spendCollectedUsage logic', () => { + // Since spendCollectedUsage is not exported, we test the logic directly + // by replicating the function here for unit testing + + const spendCollectedUsage = async ({ + userId, + conversationId, + collectedUsage, + fallbackModel, + }) => { + if (!collectedUsage || collectedUsage.length === 0) { + return; + } + + const spendPromises = []; + + for (const usage of collectedUsage) { + if (!usage) { + continue; + } + + const cache_creation = + Number(usage.input_token_details?.cache_creation) || + Number(usage.cache_creation_input_tokens) || + 0; + const cache_read = + Number(usage.input_token_details?.cache_read) || + Number(usage.cache_read_input_tokens) || + 0; + + const txMetadata = { + context: 'abort', + conversationId, + user: userId, + model: usage.model ?? fallbackModel, + }; + + if (cache_creation > 0 || cache_read > 0) { + spendPromises.push( + mockSpendStructuredTokens(txMetadata, { + promptTokens: { + input: usage.input_tokens, + write: cache_creation, + read: cache_read, + }, + completionTokens: usage.output_tokens, + }).catch(() => { + // Log error but don't throw + }), + ); + continue; + } + + spendPromises.push( + mockSpendTokens(txMetadata, { + promptTokens: usage.input_tokens, + completionTokens: usage.output_tokens, + }).catch(() => { + // Log error but don't throw + }), + ); + } + + // Wait for all token spending to complete + await Promise.all(spendPromises); + + // Clear the array to prevent double-spending + collectedUsage.length = 0; + }; + + it('should return early if collectedUsage is empty', async () => { + await spendCollectedUsage({ + userId: 'user-123', + conversationId: 'convo-123', + collectedUsage: [], + fallbackModel: 'gpt-4', + }); + + expect(mockSpendTokens).not.toHaveBeenCalled(); + expect(mockSpendStructuredTokens).not.toHaveBeenCalled(); + }); + + it('should return early if collectedUsage is null', async () => { + await spendCollectedUsage({ + userId: 'user-123', + conversationId: 'convo-123', + collectedUsage: null, + fallbackModel: 'gpt-4', + }); + + expect(mockSpendTokens).not.toHaveBeenCalled(); + expect(mockSpendStructuredTokens).not.toHaveBeenCalled(); + }); + + it('should skip null entries in collectedUsage', async () => { + const collectedUsage = [ + { input_tokens: 100, output_tokens: 50, model: 'gpt-4' }, + null, + { input_tokens: 200, output_tokens: 60, model: 'gpt-4' }, + ]; + + await spendCollectedUsage({ + userId: 'user-123', + conversationId: 'convo-123', + collectedUsage, + fallbackModel: 'gpt-4', + }); + + expect(mockSpendTokens).toHaveBeenCalledTimes(2); + }); + + it('should spend tokens for single model', async () => { + const collectedUsage = [{ input_tokens: 100, output_tokens: 50, model: 'gpt-4' }]; + + await spendCollectedUsage({ + userId: 'user-123', + conversationId: 'convo-123', + collectedUsage, + fallbackModel: 'gpt-4', + }); + + expect(mockSpendTokens).toHaveBeenCalledTimes(1); + expect(mockSpendTokens).toHaveBeenCalledWith( + expect.objectContaining({ + context: 'abort', + conversationId: 'convo-123', + user: 'user-123', + model: 'gpt-4', + }), + { promptTokens: 100, completionTokens: 50 }, + ); + }); + + it('should spend tokens for multiple models (parallel agents)', async () => { + const collectedUsage = [ + { input_tokens: 100, output_tokens: 50, model: 'gpt-4' }, + { input_tokens: 80, output_tokens: 40, model: 'claude-3' }, + { input_tokens: 120, output_tokens: 60, model: 'gemini-pro' }, + ]; + + await spendCollectedUsage({ + userId: 'user-123', + conversationId: 'convo-123', + collectedUsage, + fallbackModel: 'gpt-4', + }); + + expect(mockSpendTokens).toHaveBeenCalledTimes(3); + + // Verify each model was called + expect(mockSpendTokens).toHaveBeenNthCalledWith( + 1, + expect.objectContaining({ model: 'gpt-4' }), + { promptTokens: 100, completionTokens: 50 }, + ); + expect(mockSpendTokens).toHaveBeenNthCalledWith( + 2, + expect.objectContaining({ model: 'claude-3' }), + { promptTokens: 80, completionTokens: 40 }, + ); + expect(mockSpendTokens).toHaveBeenNthCalledWith( + 3, + expect.objectContaining({ model: 'gemini-pro' }), + { promptTokens: 120, completionTokens: 60 }, + ); + }); + + it('should use fallbackModel when usage.model is missing', async () => { + const collectedUsage = [{ input_tokens: 100, output_tokens: 50 }]; + + await spendCollectedUsage({ + userId: 'user-123', + conversationId: 'convo-123', + collectedUsage, + fallbackModel: 'fallback-model', + }); + + expect(mockSpendTokens).toHaveBeenCalledWith( + expect.objectContaining({ model: 'fallback-model' }), + expect.any(Object), + ); + }); + + it('should use spendStructuredTokens for OpenAI format cache tokens', async () => { + const collectedUsage = [ + { + input_tokens: 100, + output_tokens: 50, + model: 'gpt-4', + input_token_details: { + cache_creation: 20, + cache_read: 10, + }, + }, + ]; + + await spendCollectedUsage({ + userId: 'user-123', + conversationId: 'convo-123', + collectedUsage, + fallbackModel: 'gpt-4', + }); + + expect(mockSpendStructuredTokens).toHaveBeenCalledTimes(1); + expect(mockSpendTokens).not.toHaveBeenCalled(); + expect(mockSpendStructuredTokens).toHaveBeenCalledWith( + expect.objectContaining({ model: 'gpt-4', context: 'abort' }), + { + promptTokens: { + input: 100, + write: 20, + read: 10, + }, + completionTokens: 50, + }, + ); + }); + + it('should use spendStructuredTokens for Anthropic format cache tokens', async () => { + const collectedUsage = [ + { + input_tokens: 100, + output_tokens: 50, + model: 'claude-3', + cache_creation_input_tokens: 25, + cache_read_input_tokens: 15, + }, + ]; + + await spendCollectedUsage({ + userId: 'user-123', + conversationId: 'convo-123', + collectedUsage, + fallbackModel: 'claude-3', + }); + + expect(mockSpendStructuredTokens).toHaveBeenCalledTimes(1); + expect(mockSpendTokens).not.toHaveBeenCalled(); + expect(mockSpendStructuredTokens).toHaveBeenCalledWith( + expect.objectContaining({ model: 'claude-3' }), + { + promptTokens: { + input: 100, + write: 25, + read: 15, + }, + completionTokens: 50, + }, + ); + }); + + it('should handle mixed cache and non-cache entries', async () => { + const collectedUsage = [ + { input_tokens: 100, output_tokens: 50, model: 'gpt-4' }, + { + input_tokens: 150, + output_tokens: 30, + model: 'claude-3', + cache_creation_input_tokens: 20, + cache_read_input_tokens: 10, + }, + { input_tokens: 200, output_tokens: 20, model: 'gemini-pro' }, + ]; + + await spendCollectedUsage({ + userId: 'user-123', + conversationId: 'convo-123', + collectedUsage, + fallbackModel: 'gpt-4', + }); + + expect(mockSpendTokens).toHaveBeenCalledTimes(2); + expect(mockSpendStructuredTokens).toHaveBeenCalledTimes(1); + }); + + it('should handle real-world parallel agent abort scenario', async () => { + // Simulates: Primary agent (gemini) + addedConvo agent (gpt-5) aborted mid-stream + const collectedUsage = [ + { input_tokens: 31596, output_tokens: 151, model: 'gemini-3-flash-preview' }, + { input_tokens: 28000, output_tokens: 120, model: 'gpt-5.2' }, + ]; + + await spendCollectedUsage({ + userId: 'user-123', + conversationId: 'convo-123', + collectedUsage, + fallbackModel: 'gemini-3-flash-preview', + }); + + expect(mockSpendTokens).toHaveBeenCalledTimes(2); + + // Primary model + expect(mockSpendTokens).toHaveBeenNthCalledWith( + 1, + expect.objectContaining({ model: 'gemini-3-flash-preview' }), + { promptTokens: 31596, completionTokens: 151 }, + ); + + // Parallel model (addedConvo) + expect(mockSpendTokens).toHaveBeenNthCalledWith( + 2, + expect.objectContaining({ model: 'gpt-5.2' }), + { promptTokens: 28000, completionTokens: 120 }, + ); + }); + + it('should clear collectedUsage array after spending to prevent double-spending', async () => { + // This tests the race condition fix: after abort middleware spends tokens, + // the collectedUsage array is cleared so AgentClient.recordCollectedUsage() + // (which shares the same array reference) sees an empty array and returns early. + const collectedUsage = [ + { input_tokens: 100, output_tokens: 50, model: 'gpt-4' }, + { input_tokens: 80, output_tokens: 40, model: 'claude-3' }, + ]; + + expect(collectedUsage.length).toBe(2); + + await spendCollectedUsage({ + userId: 'user-123', + conversationId: 'convo-123', + collectedUsage, + fallbackModel: 'gpt-4', + }); + + expect(mockSpendTokens).toHaveBeenCalledTimes(2); + + // The array should be cleared after spending + expect(collectedUsage.length).toBe(0); + }); + + it('should await all token spending operations before clearing array', async () => { + // Ensure we don't clear the array before spending completes + let spendCallCount = 0; + mockSpendTokens.mockImplementation(async () => { + spendCallCount++; + // Simulate async delay + await new Promise((resolve) => setTimeout(resolve, 10)); + }); + + const collectedUsage = [ + { input_tokens: 100, output_tokens: 50, model: 'gpt-4' }, + { input_tokens: 80, output_tokens: 40, model: 'claude-3' }, + ]; + + await spendCollectedUsage({ + userId: 'user-123', + conversationId: 'convo-123', + collectedUsage, + fallbackModel: 'gpt-4', + }); + + // Both spend calls should have completed + expect(spendCallCount).toBe(2); + + // Array should be cleared after awaiting + expect(collectedUsage.length).toBe(0); + }); + }); +}); diff --git a/api/server/services/Endpoints/agents/initialize.js b/api/server/services/Endpoints/agents/initialize.js index 626beed153..a691480119 100644 --- a/api/server/services/Endpoints/agents/initialize.js +++ b/api/server/services/Endpoints/agents/initialize.js @@ -3,10 +3,11 @@ const { createContentAggregator } = require('@librechat/agents'); const { initializeAgent, validateAgentModel, - getCustomEndpointConfig, - createSequentialChainEdges, createEdgeCollector, filterOrphanedEdges, + GenerationJobManager, + getCustomEndpointConfig, + createSequentialChainEdges, } = require('@librechat/api'); const { EModelEndpoint, @@ -314,6 +315,10 @@ const initializeClient = async ({ req, res, signal, endpointOption }) => { endpoint: isEphemeralAgentId(primaryConfig.id) ? primaryConfig.endpoint : EModelEndpoint.agents, }); + if (streamId) { + GenerationJobManager.setCollectedUsage(streamId, collectedUsage); + } + return { client, userMCPAuthMap }; }; diff --git a/packages/api/src/stream/GenerationJobManager.ts b/packages/api/src/stream/GenerationJobManager.ts index 13544fc445..26c2ef73a6 100644 --- a/packages/api/src/stream/GenerationJobManager.ts +++ b/packages/api/src/stream/GenerationJobManager.ts @@ -1,9 +1,11 @@ import { logger } from '@librechat/data-schemas'; import type { StandardGraph } from '@librechat/agents'; -import type { Agents } from 'librechat-data-provider'; +import { parseTextParts } from 'librechat-data-provider'; +import type { Agents, TMessageContentParts } from 'librechat-data-provider'; import type { SerializableJobData, IEventTransport, + UsageMetadata, AbortResult, IJobStore, } from './interfaces/IJobStore'; @@ -585,7 +587,14 @@ class GenerationJobManagerClass { if (!jobData) { logger.warn(`[GenerationJobManager] Cannot abort - job not found: ${streamId}`); - return { success: false, jobData: null, content: [], finalEvent: null }; + return { + text: '', + content: [], + jobData: null, + success: false, + finalEvent: null, + collectedUsage: [], + }; } // Emit abort signal for cross-replica support (Redis mode) @@ -599,15 +608,21 @@ class GenerationJobManagerClass { runtime.abortController.abort(); } - // Get content before clearing state + /** Content before clearing state */ const result = await this.jobStore.getContentParts(streamId); const content = result?.content ?? []; - // Detect "early abort" - aborted before any generation happened (e.g., during tool loading) - // In this case, no messages were saved to DB, so frontend shouldn't navigate to conversation + /** Collected usage for all models */ + const collectedUsage = this.jobStore.getCollectedUsage(streamId); + + /** Text from content parts for fallback token counting */ + const text = parseTextParts(content as TMessageContentParts[]); + + /** Detect "early abort" - aborted before any generation happened (e.g., during tool loading) + In this case, no messages were saved to DB, so frontend shouldn't navigate to conversation */ const isEarlyAbort = content.length === 0 && !jobData.responseMessageId; - // Create final event for abort + /** Final event for abort */ const userMessageId = jobData.userMessage?.messageId; const abortFinalEvent: t.ServerSentEvent = { @@ -669,6 +684,8 @@ class GenerationJobManagerClass { jobData, content, finalEvent: abortFinalEvent, + text, + collectedUsage, }; } @@ -933,6 +950,18 @@ class GenerationJobManagerClass { this.jobStore.setContentParts(streamId, contentParts); } + /** + * Set reference to the collectedUsage array. + * This array accumulates token usage from all models during generation. + */ + setCollectedUsage(streamId: string, collectedUsage: UsageMetadata[]): void { + // Use runtime state check for performance (sync check) + if (!this.runtimeState.has(streamId)) { + return; + } + this.jobStore.setCollectedUsage(streamId, collectedUsage); + } + /** * Set reference to the graph instance. */ diff --git a/packages/api/src/stream/__tests__/collectedUsage.spec.ts b/packages/api/src/stream/__tests__/collectedUsage.spec.ts new file mode 100644 index 0000000000..3e534b537a --- /dev/null +++ b/packages/api/src/stream/__tests__/collectedUsage.spec.ts @@ -0,0 +1,482 @@ +/** + * Tests for collected usage functionality in GenerationJobManager. + * + * This tests the storage and retrieval of collectedUsage for abort handling, + * ensuring all models (including parallel agents from addedConvo) have their + * tokens spent when a conversation is aborted. + */ + +import type { UsageMetadata } from '../interfaces/IJobStore'; + +describe('CollectedUsage - InMemoryJobStore', () => { + beforeEach(() => { + jest.resetModules(); + }); + + it('should store and retrieve collectedUsage', async () => { + const { InMemoryJobStore } = await import('../implementations/InMemoryJobStore'); + const store = new InMemoryJobStore(); + await store.initialize(); + + const streamId = 'test-stream-1'; + await store.createJob(streamId, 'user-1'); + + const collectedUsage: UsageMetadata[] = [ + { input_tokens: 100, output_tokens: 50, model: 'gpt-4' }, + { input_tokens: 80, output_tokens: 40, model: 'claude-3' }, + ]; + + store.setCollectedUsage(streamId, collectedUsage); + const retrieved = store.getCollectedUsage(streamId); + + expect(retrieved).toEqual(collectedUsage); + expect(retrieved).toHaveLength(2); + + await store.destroy(); + }); + + it('should return empty array when no collectedUsage set', async () => { + const { InMemoryJobStore } = await import('../implementations/InMemoryJobStore'); + const store = new InMemoryJobStore(); + await store.initialize(); + + const streamId = 'test-stream-2'; + await store.createJob(streamId, 'user-1'); + + const retrieved = store.getCollectedUsage(streamId); + + expect(retrieved).toEqual([]); + + await store.destroy(); + }); + + it('should return empty array for non-existent stream', async () => { + const { InMemoryJobStore } = await import('../implementations/InMemoryJobStore'); + const store = new InMemoryJobStore(); + await store.initialize(); + + const retrieved = store.getCollectedUsage('non-existent-stream'); + + expect(retrieved).toEqual([]); + + await store.destroy(); + }); + + it('should update collectedUsage when set multiple times', async () => { + const { InMemoryJobStore } = await import('../implementations/InMemoryJobStore'); + const store = new InMemoryJobStore(); + await store.initialize(); + + const streamId = 'test-stream-3'; + await store.createJob(streamId, 'user-1'); + + const usage1: UsageMetadata[] = [{ input_tokens: 100, output_tokens: 50, model: 'gpt-4' }]; + store.setCollectedUsage(streamId, usage1); + + // Simulate more usage being added + const usage2: UsageMetadata[] = [ + { input_tokens: 100, output_tokens: 50, model: 'gpt-4' }, + { input_tokens: 80, output_tokens: 40, model: 'claude-3' }, + ]; + store.setCollectedUsage(streamId, usage2); + + const retrieved = store.getCollectedUsage(streamId); + expect(retrieved).toHaveLength(2); + + await store.destroy(); + }); + + it('should clear collectedUsage when clearContentState is called', async () => { + const { InMemoryJobStore } = await import('../implementations/InMemoryJobStore'); + const store = new InMemoryJobStore(); + await store.initialize(); + + const streamId = 'test-stream-4'; + await store.createJob(streamId, 'user-1'); + + const collectedUsage: UsageMetadata[] = [ + { input_tokens: 100, output_tokens: 50, model: 'gpt-4' }, + ]; + store.setCollectedUsage(streamId, collectedUsage); + + expect(store.getCollectedUsage(streamId)).toHaveLength(1); + + store.clearContentState(streamId); + + expect(store.getCollectedUsage(streamId)).toEqual([]); + + await store.destroy(); + }); + + it('should clear collectedUsage when job is deleted', async () => { + const { InMemoryJobStore } = await import('../implementations/InMemoryJobStore'); + const store = new InMemoryJobStore(); + await store.initialize(); + + const streamId = 'test-stream-5'; + await store.createJob(streamId, 'user-1'); + + const collectedUsage: UsageMetadata[] = [ + { input_tokens: 100, output_tokens: 50, model: 'gpt-4' }, + ]; + store.setCollectedUsage(streamId, collectedUsage); + + await store.deleteJob(streamId); + + expect(store.getCollectedUsage(streamId)).toEqual([]); + + await store.destroy(); + }); +}); + +describe('CollectedUsage - GenerationJobManager', () => { + beforeEach(() => { + jest.resetModules(); + }); + + it('should set and retrieve collectedUsage through manager', async () => { + const { GenerationJobManager } = await import('../GenerationJobManager'); + const { InMemoryJobStore } = await import('../implementations/InMemoryJobStore'); + const { InMemoryEventTransport } = await import('../implementations/InMemoryEventTransport'); + + GenerationJobManager.configure({ + jobStore: new InMemoryJobStore(), + eventTransport: new InMemoryEventTransport(), + isRedis: false, + cleanupOnComplete: false, + }); + + await GenerationJobManager.initialize(); + + const streamId = `manager-test-${Date.now()}`; + await GenerationJobManager.createJob(streamId, 'user-1'); + + const collectedUsage: UsageMetadata[] = [ + { input_tokens: 100, output_tokens: 50, model: 'gpt-4' }, + { input_tokens: 80, output_tokens: 40, model: 'claude-3' }, + ]; + + GenerationJobManager.setCollectedUsage(streamId, collectedUsage); + + // Retrieve through abort + const abortResult = await GenerationJobManager.abortJob(streamId); + + expect(abortResult.collectedUsage).toEqual(collectedUsage); + expect(abortResult.collectedUsage).toHaveLength(2); + + await GenerationJobManager.destroy(); + }); + + it('should return empty collectedUsage when none set', async () => { + const { GenerationJobManager } = await import('../GenerationJobManager'); + const { InMemoryJobStore } = await import('../implementations/InMemoryJobStore'); + const { InMemoryEventTransport } = await import('../implementations/InMemoryEventTransport'); + + GenerationJobManager.configure({ + jobStore: new InMemoryJobStore(), + eventTransport: new InMemoryEventTransport(), + isRedis: false, + cleanupOnComplete: false, + }); + + await GenerationJobManager.initialize(); + + const streamId = `no-usage-test-${Date.now()}`; + await GenerationJobManager.createJob(streamId, 'user-1'); + + const abortResult = await GenerationJobManager.abortJob(streamId); + + expect(abortResult.collectedUsage).toEqual([]); + + await GenerationJobManager.destroy(); + }); + + it('should not set collectedUsage if job does not exist', async () => { + const { GenerationJobManager } = await import('../GenerationJobManager'); + const { InMemoryJobStore } = await import('../implementations/InMemoryJobStore'); + const { InMemoryEventTransport } = await import('../implementations/InMemoryEventTransport'); + + GenerationJobManager.configure({ + jobStore: new InMemoryJobStore(), + eventTransport: new InMemoryEventTransport(), + isRedis: false, + }); + + await GenerationJobManager.initialize(); + + const collectedUsage: UsageMetadata[] = [ + { input_tokens: 100, output_tokens: 50, model: 'gpt-4' }, + ]; + + // This should not throw, just silently do nothing + GenerationJobManager.setCollectedUsage('non-existent-stream', collectedUsage); + + const abortResult = await GenerationJobManager.abortJob('non-existent-stream'); + expect(abortResult.success).toBe(false); + + await GenerationJobManager.destroy(); + }); +}); + +describe('AbortJob - Text and CollectedUsage', () => { + beforeEach(() => { + jest.resetModules(); + }); + + it('should extract text from content parts on abort', async () => { + const { GenerationJobManager } = await import('../GenerationJobManager'); + const { InMemoryJobStore } = await import('../implementations/InMemoryJobStore'); + const { InMemoryEventTransport } = await import('../implementations/InMemoryEventTransport'); + + GenerationJobManager.configure({ + jobStore: new InMemoryJobStore(), + eventTransport: new InMemoryEventTransport(), + isRedis: false, + cleanupOnComplete: false, + }); + + await GenerationJobManager.initialize(); + + const streamId = `text-extract-${Date.now()}`; + await GenerationJobManager.createJob(streamId, 'user-1'); + + // Set content parts with text + const contentParts = [ + { type: 'text', text: 'Hello ' }, + { type: 'text', text: 'world!' }, + ]; + GenerationJobManager.setContentParts(streamId, contentParts as never); + + const abortResult = await GenerationJobManager.abortJob(streamId); + + expect(abortResult.text).toBe('Hello world!'); + expect(abortResult.success).toBe(true); + + await GenerationJobManager.destroy(); + }); + + it('should return empty text when no content parts', async () => { + const { GenerationJobManager } = await import('../GenerationJobManager'); + const { InMemoryJobStore } = await import('../implementations/InMemoryJobStore'); + const { InMemoryEventTransport } = await import('../implementations/InMemoryEventTransport'); + + GenerationJobManager.configure({ + jobStore: new InMemoryJobStore(), + eventTransport: new InMemoryEventTransport(), + isRedis: false, + cleanupOnComplete: false, + }); + + await GenerationJobManager.initialize(); + + const streamId = `empty-text-${Date.now()}`; + await GenerationJobManager.createJob(streamId, 'user-1'); + + const abortResult = await GenerationJobManager.abortJob(streamId); + + expect(abortResult.text).toBe(''); + + await GenerationJobManager.destroy(); + }); + + it('should return both text and collectedUsage on abort', async () => { + const { GenerationJobManager } = await import('../GenerationJobManager'); + const { InMemoryJobStore } = await import('../implementations/InMemoryJobStore'); + const { InMemoryEventTransport } = await import('../implementations/InMemoryEventTransport'); + + GenerationJobManager.configure({ + jobStore: new InMemoryJobStore(), + eventTransport: new InMemoryEventTransport(), + isRedis: false, + cleanupOnComplete: false, + }); + + await GenerationJobManager.initialize(); + + const streamId = `full-abort-${Date.now()}`; + await GenerationJobManager.createJob(streamId, 'user-1'); + + // Set content parts + const contentParts = [{ type: 'text', text: 'Partial response...' }]; + GenerationJobManager.setContentParts(streamId, contentParts as never); + + // Set collected usage + const collectedUsage: UsageMetadata[] = [ + { input_tokens: 100, output_tokens: 50, model: 'gpt-4' }, + { input_tokens: 80, output_tokens: 40, model: 'claude-3' }, + ]; + GenerationJobManager.setCollectedUsage(streamId, collectedUsage); + + const abortResult = await GenerationJobManager.abortJob(streamId); + + expect(abortResult.success).toBe(true); + expect(abortResult.text).toBe('Partial response...'); + expect(abortResult.collectedUsage).toEqual(collectedUsage); + expect(abortResult.content).toHaveLength(1); + + await GenerationJobManager.destroy(); + }); + + it('should return empty values for non-existent job', async () => { + const { GenerationJobManager } = await import('../GenerationJobManager'); + const { InMemoryJobStore } = await import('../implementations/InMemoryJobStore'); + const { InMemoryEventTransport } = await import('../implementations/InMemoryEventTransport'); + + GenerationJobManager.configure({ + jobStore: new InMemoryJobStore(), + eventTransport: new InMemoryEventTransport(), + isRedis: false, + }); + + await GenerationJobManager.initialize(); + + const abortResult = await GenerationJobManager.abortJob('non-existent-job'); + + expect(abortResult.success).toBe(false); + expect(abortResult.text).toBe(''); + expect(abortResult.collectedUsage).toEqual([]); + expect(abortResult.content).toEqual([]); + expect(abortResult.jobData).toBeNull(); + + await GenerationJobManager.destroy(); + }); +}); + +describe('Real-world Scenarios', () => { + beforeEach(() => { + jest.resetModules(); + }); + + it('should handle parallel agent abort with collected usage', async () => { + /** + * Scenario: User aborts a conversation with addedConvo (parallel agents) + * - Primary agent: gemini-3-flash-preview + * - Parallel agent: gpt-5.2 + * Both should have their tokens spent on abort + */ + const { GenerationJobManager } = await import('../GenerationJobManager'); + const { InMemoryJobStore } = await import('../implementations/InMemoryJobStore'); + const { InMemoryEventTransport } = await import('../implementations/InMemoryEventTransport'); + + GenerationJobManager.configure({ + jobStore: new InMemoryJobStore(), + eventTransport: new InMemoryEventTransport(), + isRedis: false, + cleanupOnComplete: false, + }); + + await GenerationJobManager.initialize(); + + const streamId = `parallel-abort-${Date.now()}`; + await GenerationJobManager.createJob(streamId, 'user-1'); + + // Simulate content from primary agent + const contentParts = [ + { type: 'text', text: 'Primary agent output...' }, + { type: 'text', text: 'More content...' }, + ]; + GenerationJobManager.setContentParts(streamId, contentParts as never); + + // Simulate collected usage from both agents (as would happen during generation) + const collectedUsage: UsageMetadata[] = [ + { + input_tokens: 31596, + output_tokens: 151, + model: 'gemini-3-flash-preview', + }, + { + input_tokens: 28000, + output_tokens: 120, + model: 'gpt-5.2', + }, + ]; + GenerationJobManager.setCollectedUsage(streamId, collectedUsage); + + // Abort the job + const abortResult = await GenerationJobManager.abortJob(streamId); + + // Verify both models' usage is returned + expect(abortResult.success).toBe(true); + expect(abortResult.collectedUsage).toHaveLength(2); + expect(abortResult.collectedUsage[0].model).toBe('gemini-3-flash-preview'); + expect(abortResult.collectedUsage[1].model).toBe('gpt-5.2'); + + // Verify text is extracted + expect(abortResult.text).toContain('Primary agent output'); + + await GenerationJobManager.destroy(); + }); + + it('should handle abort with cache tokens from Anthropic', async () => { + const { GenerationJobManager } = await import('../GenerationJobManager'); + const { InMemoryJobStore } = await import('../implementations/InMemoryJobStore'); + const { InMemoryEventTransport } = await import('../implementations/InMemoryEventTransport'); + + GenerationJobManager.configure({ + jobStore: new InMemoryJobStore(), + eventTransport: new InMemoryEventTransport(), + isRedis: false, + cleanupOnComplete: false, + }); + + await GenerationJobManager.initialize(); + + const streamId = `cache-abort-${Date.now()}`; + await GenerationJobManager.createJob(streamId, 'user-1'); + + // Anthropic-style cache tokens + const collectedUsage: UsageMetadata[] = [ + { + input_tokens: 788, + output_tokens: 163, + cache_creation_input_tokens: 30808, + cache_read_input_tokens: 0, + model: 'claude-opus-4-5-20251101', + }, + ]; + GenerationJobManager.setCollectedUsage(streamId, collectedUsage); + + const abortResult = await GenerationJobManager.abortJob(streamId); + + expect(abortResult.collectedUsage[0].cache_creation_input_tokens).toBe(30808); + + await GenerationJobManager.destroy(); + }); + + it('should handle abort with sequential tool calls usage', async () => { + /** + * Scenario: Single agent with multiple tool calls, aborted mid-execution + * Usage accumulates for each LLM call + */ + const { GenerationJobManager } = await import('../GenerationJobManager'); + const { InMemoryJobStore } = await import('../implementations/InMemoryJobStore'); + const { InMemoryEventTransport } = await import('../implementations/InMemoryEventTransport'); + + GenerationJobManager.configure({ + jobStore: new InMemoryJobStore(), + eventTransport: new InMemoryEventTransport(), + isRedis: false, + cleanupOnComplete: false, + }); + + await GenerationJobManager.initialize(); + + const streamId = `sequential-abort-${Date.now()}`; + await GenerationJobManager.createJob(streamId, 'user-1'); + + // Usage from multiple sequential LLM calls (tool use pattern) + const collectedUsage: UsageMetadata[] = [ + { input_tokens: 100, output_tokens: 50, model: 'gpt-4' }, // Initial call + { input_tokens: 150, output_tokens: 30, model: 'gpt-4' }, // After tool result 1 + { input_tokens: 180, output_tokens: 20, model: 'gpt-4' }, // After tool result 2 (aborted here) + ]; + GenerationJobManager.setCollectedUsage(streamId, collectedUsage); + + const abortResult = await GenerationJobManager.abortJob(streamId); + + expect(abortResult.collectedUsage).toHaveLength(3); + // All three entries should be present for proper token accounting + + await GenerationJobManager.destroy(); + }); +}); diff --git a/packages/api/src/stream/implementations/InMemoryJobStore.ts b/packages/api/src/stream/implementations/InMemoryJobStore.ts index e4a5d5d3ad..cc82a69963 100644 --- a/packages/api/src/stream/implementations/InMemoryJobStore.ts +++ b/packages/api/src/stream/implementations/InMemoryJobStore.ts @@ -1,7 +1,12 @@ import { logger } from '@librechat/data-schemas'; import type { StandardGraph } from '@librechat/agents'; import type { Agents } from 'librechat-data-provider'; -import type { IJobStore, SerializableJobData, JobStatus } from '~/stream/interfaces/IJobStore'; +import type { + SerializableJobData, + UsageMetadata, + IJobStore, + JobStatus, +} from '~/stream/interfaces/IJobStore'; /** * Content state for a job - volatile, in-memory only. @@ -10,6 +15,7 @@ import type { IJobStore, SerializableJobData, JobStatus } from '~/stream/interfa interface ContentState { contentParts: Agents.MessageContentComplex[]; graphRef: WeakRef | null; + collectedUsage: UsageMetadata[]; } /** @@ -240,6 +246,7 @@ export class InMemoryJobStore implements IJobStore { this.contentState.set(streamId, { contentParts: [], graphRef: new WeakRef(graph), + collectedUsage: [], }); } } @@ -252,10 +259,30 @@ export class InMemoryJobStore implements IJobStore { if (existing) { existing.contentParts = contentParts; } else { - this.contentState.set(streamId, { contentParts, graphRef: null }); + this.contentState.set(streamId, { contentParts, graphRef: null, collectedUsage: [] }); } } + /** + * Set collected usage reference for a job. + */ + setCollectedUsage(streamId: string, collectedUsage: UsageMetadata[]): void { + const existing = this.contentState.get(streamId); + if (existing) { + existing.collectedUsage = collectedUsage; + } else { + this.contentState.set(streamId, { contentParts: [], graphRef: null, collectedUsage }); + } + } + + /** + * Get collected usage for a job. + */ + getCollectedUsage(streamId: string): UsageMetadata[] { + const state = this.contentState.get(streamId); + return state?.collectedUsage ?? []; + } + /** * Get content parts for a job. * Returns live content from stored reference. diff --git a/packages/api/src/stream/implementations/RedisJobStore.ts b/packages/api/src/stream/implementations/RedisJobStore.ts index 421fa30f2c..cce636d5a1 100644 --- a/packages/api/src/stream/implementations/RedisJobStore.ts +++ b/packages/api/src/stream/implementations/RedisJobStore.ts @@ -1,9 +1,14 @@ import { logger } from '@librechat/data-schemas'; import { createContentAggregator } from '@librechat/agents'; -import type { IJobStore, SerializableJobData, JobStatus } from '~/stream/interfaces/IJobStore'; import type { StandardGraph } from '@librechat/agents'; import type { Agents } from 'librechat-data-provider'; import type { Redis, Cluster } from 'ioredis'; +import type { + SerializableJobData, + UsageMetadata, + IJobStore, + JobStatus, +} from '~/stream/interfaces/IJobStore'; /** * Key prefixes for Redis storage. @@ -90,6 +95,13 @@ export class RedisJobStore implements IJobStore { */ private localGraphCache = new Map>(); + /** + * Local cache for collectedUsage arrays. + * Generation happens on a single instance, so collectedUsage is only available locally. + * For cross-replica abort, the abort handler falls back to text-based token counting. + */ + private localCollectedUsageCache = new Map(); + /** Cleanup interval in ms (1 minute) */ private cleanupIntervalMs = 60000; @@ -227,6 +239,7 @@ export class RedisJobStore implements IJobStore { async deleteJob(streamId: string): Promise { // Clear local caches this.localGraphCache.delete(streamId); + this.localCollectedUsageCache.delete(streamId); // Note: userJobs cleanup is handled lazily via self-healing in getActiveJobIdsByUser // In cluster mode, separate runningJobs (global) from stream-specific keys (same slot) @@ -290,6 +303,7 @@ export class RedisJobStore implements IJobStore { if (!job) { await this.redis.srem(KEYS.runningJobs, streamId); this.localGraphCache.delete(streamId); + this.localCollectedUsageCache.delete(streamId); cleaned++; continue; } @@ -298,6 +312,7 @@ export class RedisJobStore implements IJobStore { if (job.status !== 'running') { await this.redis.srem(KEYS.runningJobs, streamId); this.localGraphCache.delete(streamId); + this.localCollectedUsageCache.delete(streamId); cleaned++; continue; } @@ -382,6 +397,7 @@ export class RedisJobStore implements IJobStore { } // Clear local caches this.localGraphCache.clear(); + this.localCollectedUsageCache.clear(); // Don't close the Redis connection - it's shared logger.info('[RedisJobStore] Destroyed'); } @@ -406,11 +422,28 @@ export class RedisJobStore implements IJobStore { * No-op for Redis - content parts are reconstructed from chunks. * Metadata (agentId, groupId) is embedded directly on content parts by the agent runtime. */ - setContentParts(_streamId: string, _contentParts: Agents.MessageContentComplex[]): void { + setContentParts(): void { // Content parts are reconstructed from chunks during getContentParts // No separate storage needed } + /** + * Store collectedUsage reference in local cache. + * This is used for abort handling to spend tokens for all models. + * Note: Only available on the generating instance; cross-replica abort uses fallback. + */ + setCollectedUsage(streamId: string, collectedUsage: UsageMetadata[]): void { + this.localCollectedUsageCache.set(streamId, collectedUsage); + } + + /** + * Get collected usage for a job. + * Only available if this is the generating instance. + */ + getCollectedUsage(streamId: string): UsageMetadata[] { + return this.localCollectedUsageCache.get(streamId) ?? []; + } + /** * Get aggregated content - tries local cache first, falls back to Redis reconstruction. * @@ -528,6 +561,7 @@ export class RedisJobStore implements IJobStore { clearContentState(streamId: string): void { // Clear local caches immediately this.localGraphCache.delete(streamId); + this.localCollectedUsageCache.delete(streamId); // Fire and forget - async cleanup for Redis this.clearContentStateAsync(streamId).catch((err) => { diff --git a/packages/api/src/stream/index.ts b/packages/api/src/stream/index.ts index 4e9bab324c..74c13a2bf0 100644 --- a/packages/api/src/stream/index.ts +++ b/packages/api/src/stream/index.ts @@ -5,11 +5,12 @@ export { } from './GenerationJobManager'; export type { - AbortResult, SerializableJobData, + IEventTransport, + UsageMetadata, + AbortResult, JobStatus, IJobStore, - IEventTransport, } from './interfaces/IJobStore'; export { createStreamServices } from './createStreamServices'; diff --git a/packages/api/src/stream/interfaces/IJobStore.ts b/packages/api/src/stream/interfaces/IJobStore.ts index 14611a7fad..af681fb2e9 100644 --- a/packages/api/src/stream/interfaces/IJobStore.ts +++ b/packages/api/src/stream/interfaces/IJobStore.ts @@ -45,6 +45,54 @@ export interface SerializableJobData { promptTokens?: number; } +/** + * Usage metadata for token spending across different LLM providers. + * + * This interface supports two mutually exclusive cache token formats: + * + * **OpenAI format** (GPT-4, o1, etc.): + * - Uses `input_token_details.cache_creation` and `input_token_details.cache_read` + * - Cache tokens are nested under the `input_token_details` object + * + * **Anthropic format** (Claude models): + * - Uses `cache_creation_input_tokens` and `cache_read_input_tokens` + * - Cache tokens are top-level properties + * + * When processing usage data, check both formats: + * ```typescript + * const cacheCreation = usage.input_token_details?.cache_creation + * || usage.cache_creation_input_tokens || 0; + * ``` + */ +export interface UsageMetadata { + /** Total input tokens (prompt tokens) */ + input_tokens?: number; + /** Total output tokens (completion tokens) */ + output_tokens?: number; + /** Model identifier that generated this usage */ + model?: string; + /** + * OpenAI-style cache token details. + * Present for OpenAI models (GPT-4, o1, etc.) + */ + input_token_details?: { + /** Tokens written to cache */ + cache_creation?: number; + /** Tokens read from cache */ + cache_read?: number; + }; + /** + * Anthropic-style cache creation tokens. + * Present for Claude models. Mutually exclusive with input_token_details. + */ + cache_creation_input_tokens?: number; + /** + * Anthropic-style cache read tokens. + * Present for Claude models. Mutually exclusive with input_token_details. + */ + cache_read_input_tokens?: number; +} + /** * Result returned from aborting a job - contains all data needed * for token spending and message saving without storing callbacks @@ -58,6 +106,10 @@ export interface AbortResult { content: Agents.MessageContentComplex[]; /** Final event to send to client */ finalEvent: unknown; + /** Concatenated text from all content parts for token counting fallback */ + text: string; + /** Collected usage metadata from all models for token spending */ + collectedUsage: UsageMetadata[]; } /** @@ -210,6 +262,23 @@ export interface IJobStore { * @param runSteps - Run steps to save */ saveRunSteps?(streamId: string, runSteps: Agents.RunStep[]): Promise; + + /** + * Set collected usage reference for a job. + * This array accumulates token usage from all models during generation. + * + * @param streamId - The stream identifier + * @param collectedUsage - Array of usage metadata from all models + */ + setCollectedUsage(streamId: string, collectedUsage: UsageMetadata[]): void; + + /** + * Get collected usage for a job. + * + * @param streamId - The stream identifier + * @returns Array of usage metadata or empty array + */ + getCollectedUsage(streamId: string): UsageMetadata[]; } /** From f09eec846253e50d47bf2c88ae8ca969e5beffcf Mon Sep 17 00:00:00 2001 From: Dustin Healy <54083382+dustinhealy@users.noreply.github.com> Date: Tue, 20 Jan 2026 11:45:07 -0800 Subject: [PATCH 007/245] =?UTF-8?q?=E2=9C=85=20feat:=20Zod=20Email=20Valid?= =?UTF-8?q?ation=20at=20Login=20(#11434)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/src/components/Auth/LoginForm.tsx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/client/src/components/Auth/LoginForm.tsx b/client/src/components/Auth/LoginForm.tsx index 0a6e1e8614..c51c2002e3 100644 --- a/client/src/components/Auth/LoginForm.tsx +++ b/client/src/components/Auth/LoginForm.tsx @@ -5,6 +5,7 @@ import { ThemeContext, Spinner, Button, isDark } from '@librechat/client'; import type { TLoginUser, TStartupConfig } from 'librechat-data-provider'; import type { TAuthContext } from '~/common'; import { useResendVerificationEmail, useGetStartupConfig } from '~/data-provider'; +import { validateEmail } from '~/utils'; import { useLocalize } from '~/hooks'; type TLoginFormProps = { @@ -96,10 +97,9 @@ const LoginForm: React.FC = ({ onSubmit, startupConfig, error, {...register('email', { required: localize('com_auth_email_required'), maxLength: { value: 120, message: localize('com_auth_email_max_length') }, - pattern: { - value: useUsernameLogin ? /\S+/ : /\S+@\S+\.\S+/, - message: localize('com_auth_email_pattern'), - }, + validate: useUsernameLogin + ? undefined + : (value) => validateEmail(value, localize('com_auth_email_pattern')), })} aria-invalid={!!errors.email} className="webkit-dark-styles transition-color peer w-full rounded-2xl border border-border-light bg-surface-primary px-3.5 pb-2.5 pt-3 text-text-primary duration-200 focus:border-green-500 focus:outline-none" From c5113a75a0ca16444f38c46c05d5bfdbb7e036f5 Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Tue, 20 Jan 2026 14:45:27 -0500 Subject: [PATCH 008/245] =?UTF-8?q?=F0=9F=94=A7=20fix:=20Add=20`hasAgentAc?= =?UTF-8?q?cess`=20to=20dependencies=20in=20`useNewConvo`=20hook=20(#11427?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Updated the dependency array in the useNewConvo hook to include hasAgentAccess for improved state management and functionality. --- client/src/hooks/useNewConvo.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/hooks/useNewConvo.ts b/client/src/hooks/useNewConvo.ts index fd2e20e0ee..c468ab30a2 100644 --- a/client/src/hooks/useNewConvo.ts +++ b/client/src/hooks/useNewConvo.ts @@ -249,7 +249,7 @@ const useNewConvo = (index = 0) => { state: disableFocus ? {} : { focusChat: true }, }); }, - [endpointsConfig, defaultPreset, assistantsListMap, modelsQuery.data], + [endpointsConfig, defaultPreset, assistantsListMap, modelsQuery.data, hasAgentAccess], ); const newConversation = useCallback( From 24e182d20e64cb7ea8179d2975c28c2a42005d6a Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 21 Jan 2026 09:25:02 -0500 Subject: [PATCH 009/245] =?UTF-8?q?=F0=9F=8C=8D=20i18n:=20Update=20transla?= =?UTF-8?q?tion.json=20with=20latest=20translations=20(#11439)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- client/src/locales/lv/translation.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/locales/lv/translation.json b/client/src/locales/lv/translation.json index f17bb9cb46..89a5d0552d 100644 --- a/client/src/locales/lv/translation.json +++ b/client/src/locales/lv/translation.json @@ -1398,7 +1398,7 @@ "com_ui_upload_image_input": "Augšupielādēt failu kā attēlu", "com_ui_upload_invalid": "Nederīgs augšupielādējamais fails. Attēlam jābūt tādam, kas nepārsniedz ierobežojumu.", "com_ui_upload_invalid_var": "Nederīgs augšupielādējams fails. Attēlam jābūt ne lielākam par {{0}} MB", - "com_ui_upload_ocr_text": "Augšupielādēt failu kā tekstu", + "com_ui_upload_ocr_text": "Augšupielādēt failu kā kontekstu", "com_ui_upload_provider": "Augšupielādēt pakalpojumu sniedzējam", "com_ui_upload_success": "Fails veiksmīgi augšupielādēts", "com_ui_upload_type": "Izvēlieties augšupielādes veidu", From e608c652e56f43994d5e6dfba70c367e7fee5f59 Mon Sep 17 00:00:00 2001 From: Dustin Healy <54083382+dustinhealy@users.noreply.github.com> Date: Wed, 21 Jan 2026 10:44:20 -0800 Subject: [PATCH 010/245] =?UTF-8?q?=E2=9C=82=EF=B8=8F=20fix:=20Clipped=20F?= =?UTF-8?q?ocus=20Outlines=20in=20Conversation=20Panel=20(#11438)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: focus outline clipping in Conversations panel * chore: address Copilot comments --- client/src/components/Conversations/Conversations.tsx | 2 +- client/src/components/Conversations/Convo.tsx | 2 +- client/src/components/Conversations/ConvoLink.tsx | 2 +- client/src/components/Nav/Bookmarks/BookmarkNav.tsx | 1 + client/src/components/Nav/Favorites/FavoriteItem.tsx | 2 +- client/src/components/Nav/Favorites/FavoritesList.tsx | 2 +- client/src/components/Nav/NewChat.tsx | 6 +++--- 7 files changed, 9 insertions(+), 8 deletions(-) diff --git a/client/src/components/Conversations/Conversations.tsx b/client/src/components/Conversations/Conversations.tsx index b972d251b0..fc66c0977a 100644 --- a/client/src/components/Conversations/Conversations.tsx +++ b/client/src/components/Conversations/Conversations.tsx @@ -82,7 +82,7 @@ const ChatsHeader: FC = memo(({ isExpanded, onToggle }) => { return ( {isSelected && ( -
- - - -
+ <> +