Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active
TypeScript aianthropicartifactsawsazurechatgptchatgpt-cloneclaudeclonedeepseekgeminigooglegpt-5librechatmcpo1openairesponses-apivisionwebui
🪸 fix: Restore File Search on Later Agent Turns (#16516)
lia-by-librechat[bot]f995ba17da8c
diff --git a/e2e/specs/mock/file-provisioning.spec.ts b/e2e/specs/mock/file-provisioning.spec.tsindex e5dcef61ba..e099f2ce06 100644--- a/e2e/specs/mock/file-provisioning.spec.ts+++ b/e2e/specs/mock/file-provisioning.spec.ts@@ -5,7 +5,9 @@ import {NEW_CHAT_PATH,uniqueName,sendMessage,+ getRagQueries,getRagEmbedded,+ sendMessageAndWaitForCompletion,enableFileSearch,selectMockEndpoint,resetProvisioning,@@ -118,10 +120,10 @@ test.describe('file provisioning — lazy (unified upload, at tool-execute)', ().toContain(fileName);});- test('a unified attachment is embedded into the vector DB when file_search runs', async ({+ test('a unified attachment stays searchable after lazy embedding on a later turn', async ({page,}) => {- test.setTimeout(120000);+ test.setTimeout(180000);await page.goto(NEW_CHAT_PATH, { timeout: 10000 });await selectMockEndpoint(page, MOCK_ENDPOINTS[1]);await resetProvisioning(page);@@ -144,11 +146,20 @@ test.describe('file provisioning — lazy (unified upload, at tool-execute)', ()).not.toContain(fileId);await expect(page.getByRole('button', { name: fileName })).toBeVisible({ timeout: 15000 });- // Embedding fires at ON_TOOL_EXECUTE, independent of the file_search tool result.- await sendMessage(page, `E2E_FILE_SEARCH:${uniqueName('q')}`);-+ // The first tool call lazily embeds the attachment and searches it.+ await sendMessageAndWaitForCompletion(page, `E2E_FILE_SEARCH:${uniqueName('first')}`);await expect.poll(async () => (await getRagEmbedded(page)).map((e) => e.file_id), { timeout: 30000 }).toContain(fileId);+ await expect+ .poll(async () => (await getRagQueries(page)).filter((q) => q.file_id === fileId).length)+ .toBe(1);++ // A completed, persisted turn must still expose the embedded file to the next call.+ await sendMessageAndWaitForCompletion(page, `E2E_FILE_SEARCH:${uniqueName('second')}`);+ await expect+ .poll(async () => (await getRagQueries(page)).filter((q) => q.file_id === fileId).length)+ .toBe(2);+ expect((await getRagEmbedded(page)).filter((e) => e.file_id === fileId)).toHaveLength(1);});});diff --git a/e2e/specs/mock/helpers.ts b/e2e/specs/mock/helpers.tsindex d5cf26f9ad..6f62b5ac10 100644--- a/e2e/specs/mock/helpers.ts+++ b/e2e/specs/mock/helpers.ts@@ -401,6 +401,15 @@ export type CodeProvisionRecord = {};export type RagEmbedRecord = { file_id: string; filename: string; entity_id: string };+export type RagQueryRecord = { file_id: string; query: string };++/** Every `/query` the fake RAG service received from file_search. */+export async function getRagQueries(page: Page): Promise<RagQueryRecord[]> {+ const response = await page.request.get(`${RAG_API_BASE}/__debug/embedded`);+ expect(response.ok(), 'fake RAG server /__debug/embedded should respond').toBeTruthy();+ const body = (await response.json()) as { queries: RagQueryRecord[] };+ return body.queries;+}/** Files the fake code server received via /upload (proof they reached the code env). */export async function getCodeProvisionedUploads(page: Page): Promise<CodeProvisionRecord[]> {diff --git a/packages/api/src/agents/__tests__/initialize.test.ts b/packages/api/src/agents/__tests__/initialize.test.tsindex c20c0af9e5..488c68007e 100644--- a/packages/api/src/agents/__tests__/initialize.test.ts+++ b/packages/api/src/agents/__tests__/initialize.test.ts@@ -1595,6 +1595,123 @@ describe('initializeAgent — attachment scoping', () => {).resolves.toBeDefined();});+ it.each([+ ['no conversation file IDs', [], true, true],+ ['only a sibling-branch file ID', ['sibling-file'], true, true],+ ['no file references on the active branch', ['sibling-file'], false, true],+ ['an unanchored continuation', ['embedded-message-file'], true, false],+ ])(+ 'restores search files with %s',+ async (_case, conversationFiles, hasBranchFile, hasAnchor) => {+ const { primeResources } = jest.requireMock('../resources') as { primeResources: jest.Mock };+ const { filterFilesByEndpointRuntimeConfig } = jest.requireMock('~/files') as {+ filterFilesByEndpointRuntimeConfig: jest.Mock;+ };+ const file = {+ file_id: 'embedded-message-file',+ user: 'user-1',+ filename: 'report.pdf',+ filepath: '/uploads/report.pdf',+ object: 'file',+ source: FileSources.local,+ type: 'application/pdf',+ bytes: 1024,+ usage: 0,+ context: FileContext.message_attachment,+ embedded: true,+ llmDeliveryPath: 'none',+ metadata: { destinationChosen: false, embeddedEntities: ['user-1'] },+ };+ const { agent, req, res, loadTools, db } = createMocks({+ loadedToolDefinitions: [{ name: Tools.file_search }],+ });+ agent.tools = [Tools.file_search];+ req.resolvedConversation = { conversationId: 'conv-1', files: conversationFiles };+ mockExtractLibreChatParams.mockReturnValueOnce({+ resendFiles: true,+ maxContextTokens: undefined,+ modelOptions: { model: agent.model },+ });+ if (hasAnchor) {+ mockGetThreadData.mockImplementationOnce(realUtils.getThreadData);+ }+ filterFilesByEndpointRuntimeConfig.mockImplementation(+ (_config: ServerRequest['config'], { files }: { files: IMongoFile[] }) => files,+ );+ primeResources.mockImplementationOnce(+ jest.requireActual<typeof import('../resources')>('../resources').primeResources,+ );+ const getMessages = jest.fn().mockResolvedValue([+ {+ messageId: 'uploaded-message',+ parentMessageId: Constants.NO_PARENT,+ files: hasBranchFile ? [{ file_id: file.file_id }] : [],+ },+ { messageId: 'first-reply', parentMessageId: 'uploaded-message' },+ {+ messageId: 'sibling-message',+ parentMessageId: Constants.NO_PARENT,+ files: [{ file_id: 'sibling-file' }],+ },+ ]);+ const allFiles = new Map([+ [file.file_id, file],+ ['sibling-file', { ...file, file_id: 'sibling-file', filename: 'sibling.pdf' }],+ ]);+ const getToolFilesByIds = jest+ .fn()+ .mockImplementation(async (ids: string[]) =>+ ids.map((id) => allFiles.get(id)).filter((entry) => entry != null),+ );+ const getFiles = jest+ .fn()+ .mockImplementation(async (filter: { file_id: { $in: string[] } }) =>+ filter.file_id.$in.map((id) => allFiles.get(id)).filter((entry) => entry != null),+ );++ const result = await initializeAgent(+ {+ req,+ res,+ agent,+ loadTools,+ endpointOption: { endpoint: EModelEndpoint.agents },+ conversationId: 'conv-1',+ parentMessageId: hasAnchor ? 'first-reply' : undefined,+ allowedProviders: new Set([Providers.OPENAI]),+ isInitialAgent: true,+ fileSearchAvailable: true,+ },+ {+ ...db,+ getMessages,+ getToolFilesByIds,+ getFiles,+ getDeferredProvisionFiles: jest.fn().mockResolvedValue([]),+ },+ );++ const expectedFileIds = hasBranchFile ? [file.file_id] : [];+ expect(getToolFilesByIds).toHaveBeenCalledWith(+ expectedFileIds,+ new Set([EToolResources.file_search]),+ {+ userId: 'user-1',+ tenantId: undefined,+ },+ );+ expect(+ result.tool_resources?.[EToolResources.file_search]?.files?.map((entry) => entry.file_id) ??+ [],+ ).toEqual(expectedFileIds);+ expect(result.provisionState?.vectorDBFiles ?? []).toEqual([]);+ expect(db.getConvoFiles).not.toHaveBeenCalled();+ if (!hasAnchor) {+ expect(getMessages).not.toHaveBeenCalled();+ }+ },+ );+it('owner-scopes request file usage updates while preserving trusted tool files', async () => {const { primeResources } = jest.requireMock('../resources') as {primeResources: jest.Mock;diff --git a/packages/api/src/agents/initialize.ts b/packages/api/src/agents/initialize.tsindex 6f0af0c7c7..cd192e7e06 100644--- a/packages/api/src/agents/initialize.ts+++ b/packages/api/src/agents/initialize.ts@@ -1521,6 +1521,10 @@ export async function initializeAgent(* references no files to the whole conversation would provision a sibling branch's* attachments, sending files this branch never mentioned to the Code API or RAG. */const provisionFileIds = threadAnchor == null ? fileIds : (threadFileIds ?? []);+ /* Conversation.files can omit a previous turn's embedded upload. Follow the same+ * parent chain for provisioned tool files so search restores it without loading a+ * sibling branch; unanchored continuations retain the conversation-level list. */+ const replayToolFileIds = needsThreadWalk ? (threadFileIds ?? []) : fileIds;/*** Retrieve execute_code files filtered to the current thread.@@ -1538,9 +1542,11 @@ export async function initializeAgent(* three, and this runs on the agent initialization path. */const [toolFiles, codeGeneratedFiles, userCodeFiles, deferredFiles] = await Promise.all([resendFiles && requestFileOwnerScope- ? (db.getToolFilesByIds(fileIds, toolResourceSet, requestFileOwnerScope) as Promise<- IMongoFile[]- >)+ ? (db.getToolFilesByIds(+ replayToolFileIds,+ toolResourceSet,+ requestFileOwnerScope,+ ) as Promise<IMongoFile[]>): ([] as IMongoFile[]),resendFiles && wantsCodeFiles && db.getCodeGeneratedFiles && requestFileOwnerScope? (db.getCodeGeneratedFiles(