π€― LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
π fix(file): move a file's knowledge-base document along with it (#20083)
Arvin Xud404374f7f0a
diff --git a/apps/server/src/routers/lambda/__tests__/file.test.ts b/apps/server/src/routers/lambda/__tests__/file.test.tsindex 5d40e72b1da..d5792b273f8 100644--- a/apps/server/src/routers/lambda/__tests__/file.test.ts+++ b/apps/server/src/routers/lambda/__tests__/file.test.ts@@ -278,6 +278,7 @@ const mockDocumentModelFindById = vi.fn();const mockDocumentModelFindBySlug = vi.fn();const mockDocumentModelTransferTo = vi.fn();const mockDocumentModelSubtreeHasForeignRows = vi.fn().mockResolvedValue(false);+const mockDocumentModelSyncFromFile = vi.fn().mockResolvedValue([]);vi.mock('@/database/repositories/knowledge', () => ({KnowledgeRepo: vi.fn(function () {@@ -295,6 +296,7 @@ vi.mock('@/database/models/document', () => ({findById: mockDocumentModelFindById,findBySlug: mockDocumentModelFindBySlug,subtreeHasForeignRows: mockDocumentModelSubtreeHasForeignRows,+ syncFromFile: mockDocumentModelSyncFromFile,transferTo: mockDocumentModelTransferTo,};}),@@ -1379,6 +1381,10 @@ describe('fileRouter', () => {await caller.updateFile({ id: 'file-1', parentId: 'parent-folder' });expect(mockFileModelUpdate).toHaveBeenCalledWith('file-1', { parentId: 'docs_parent' });+ expect(mockDocumentModelSyncFromFile).toHaveBeenCalledWith('file-1', {+ name: undefined,+ parentId: 'docs_parent',+ });});it('should strip forged agent-share provenance from metadata updates', async () => {@@ -1393,6 +1399,7 @@ describe('fileRouter', () => {});expect(mockFileModelUpdate).toHaveBeenCalledWith('file-1', { metadata: { width: 100 } });+ expect(mockDocumentModelSyncFromFile).not.toHaveBeenCalled();});});diff --git a/apps/server/src/routers/lambda/__tests__/integration/file.updateFile.integration.test.ts b/apps/server/src/routers/lambda/__tests__/integration/file.updateFile.integration.test.tsnew file mode 100644index 00000000000..8071e55900c--- /dev/null+++ b/apps/server/src/routers/lambda/__tests__/integration/file.updateFile.integration.test.ts@@ -0,0 +1,261 @@+// @vitest-environment node+import type { LobeChatDatabase } from '@lobechat/database';+import { agentDocuments, agents, documents, files } from '@lobechat/database/schemas';+import { getTestDB } from '@lobechat/database/test-utils';+import { eq } from 'drizzle-orm';+import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';++import { AgentDocumentModel } from '@/database/models/agentDocuments';++import { fileRouter } from '../../file';+import { cleanupTestUser, createTestUser } from './setup';++// Moving / renaming never touches object storage; avoid S3 initialization.+vi.mock('@/server/services/file', () => ({+ FileService: vi.fn().mockImplementation(function () {+ return {};+ }),+}));++let testDB: LobeChatDatabase;+vi.mock('@/database/core/db-adaptor', () => ({+ getServerDB: vi.fn(function () {+ return testDB;+ }),+}));++const context = (userId: string) => ({ jwtPayload: { userId }, userId });++const createFolder = async (db: LobeChatDatabase, userId: string, title: string) => {+ const [folder] = await db+ .insert(documents)+ .values({+ fileType: 'custom/folder',+ filename: title,+ source: `folder-${title}-${userId}`,+ sourceType: 'api',+ title,+ totalCharCount: 0,+ totalLineCount: 0,+ userId,+ })+ .returning();+ return folder;+};++/** A knowledge-base file: the `files` row plus its mirror `documents` row. */+const createFileWithDocument = async (+ db: LobeChatDatabase,+ userId: string,+ name: string,+ parentId: string,+) => {+ const [file] = await db+ .insert(files)+ .values({+ fileType: 'application/pdf',+ name,+ parentId,+ size: 1024,+ url: `files/${name}`,+ userId,+ })+ .returning();+ const [document] = await db+ .insert(documents)+ .values({+ fileId: file.id,+ fileType: 'application/pdf',+ filename: name,+ parentId,+ source: `files/${name}`,+ sourceType: 'file',+ title: name,+ totalCharCount: 0,+ totalLineCount: 0,+ userId,+ })+ .returning();+ return { document, file };+};++const readDocument = async (db: LobeChatDatabase, id: string) => {+ const [row] = await db.select().from(documents).where(eq(documents.id, id));+ return row;+};++describe('fileRouter.updateFile integration', () => {+ let db: LobeChatDatabase;+ let userId: string;+ let otherUserId: string;++ // The first getTestDB() call runs the migrations, which can exceed the default hook timeout.+ beforeAll(async () => {+ db = await getTestDB();+ testDB = db;+ }, 60_000);++ beforeEach(async () => {+ [userId, otherUserId] = await Promise.all([createTestUser(db), createTestUser(db)]);+ });++ afterEach(async () => {+ await Promise.all([userId, otherUserId].map((id) => cleanupTestUser(db, id)));+ });++ it('moves the backing knowledge-base document together with the file', async () => {+ const oldFolder = await createFolder(db, userId, 'nf5bak');+ const newFolder = await createFolder(db, userId, 'archive');+ const { document, file } = await createFileWithDocument(db, userId, 'report.pdf', oldFolder.id);++ await fileRouter.createCaller(context(userId)).updateFile({+ id: file.id,+ parentId: newFolder.id,+ });++ const [movedFile] = await db.select().from(files).where(eq(files.id, file.id));+ expect(movedFile.parentId).toBe(newFolder.id);+ // The KB tree is rendered from documents.parent_id β it must follow the file,+ // otherwise deleting the old folder cascades into the "moved" file.+ expect((await readDocument(db, document.id)).parentId).toBe(newFolder.id);+ });++ it('moves the backing document to the root when parentId is null', async () => {+ const folder = await createFolder(db, userId, 'nf5bak');+ const { document, file } = await createFileWithDocument(db, userId, 'notes.pdf', folder.id);++ await fileRouter.createCaller(context(userId)).updateFile({ id: file.id, parentId: null });++ expect((await readDocument(db, document.id)).parentId).toBeNull();+ });++ it('renames the backing document together with the file', async () => {+ const folder = await createFolder(db, userId, 'nf5bak');+ const { document, file } = await createFileWithDocument(db, userId, 'draft.pdf', folder.id);++ await fileRouter.createCaller(context(userId)).updateFile({ id: file.id, name: 'final.pdf' });++ const renamed = await readDocument(db, document.id);+ expect(renamed.title).toBe('final.pdf');+ expect(renamed.filename).toBe('final.pdf');+ expect(renamed.parentId).toBe(folder.id);+ });++ /** The row AgentDocumentsService.importFile creates: document + binding in one transaction. */+ const importIntoAgent = async (+ file: typeof files.$inferSelect,+ filename: string,+ parentId: string,+ ) => {+ const [agent] = await db.insert(agents).values({ userId }).returning();+ return new AgentDocumentModel(db, userId).create(agent.id, filename, '', {+ fileId: file.id,+ fileType: file.fileType,+ parentId,+ source: file.url,+ sourceType: 'file',+ title: file.name,+ });+ };++ it.each([+ ['an uploaded file', 'application/pdf'],+ // DocumentService.createDocument gives a knowledge-base page's backing file this type.+ ['a knowledge-base page backing file', 'custom/document'],+ ])("leaves an agent's imported copy of %s in its agent folder", async (_label, fileType) => {+ const kbFolder = await createFolder(db, userId, 'nf5bak');+ const newKbFolder = await createFolder(db, userId, 'archive');+ const agentFolder = await createFolder(db, userId, 'agent-notes');+ const { document: mirror, file } = await createFileWithDocument(+ db,+ userId,+ 'spec.pdf',+ kbFolder.id,+ );+ await db.update(files).set({ fileType }).where(eq(files.id, file.id));+ const agentCopy = await importIntoAgent({ ...file, fileType }, 'spec 2.pdf', agentFolder.id);++ await fileRouter+ .createCaller(context(userId))+ .updateFile({ id: file.id, name: 'spec-final.pdf', parentId: newKbFolder.id });++ const movedMirror = await readDocument(db, mirror.id);+ expect(movedMirror.parentId).toBe(newKbFolder.id);+ expect(movedMirror.filename).toBe('spec-final.pdf');+ const untouched = await readDocument(db, agentCopy.documentId);+ expect(untouched.parentId).toBe(agentFolder.id);+ expect(untouched.filename).toBe('spec 2.pdf');+ expect(untouched.title).toBe('spec.pdf');+ });++ it('still moves a knowledge-base parse row that an agent has merely associated', async () => {+ // agentDocument.associateDocument binds a pre-existing document (see+ // agentDocumentsOwnership.ts); it stays the KB mirror and must follow the file, or+ // deleting the old folder would take the file with it again.+ const oldFolder = await createFolder(db, userId, 'nf5bak');+ const newFolder = await createFolder(db, userId, 'archive');+ const [file] = await db+ .insert(files)+ .values({+ fileType: 'text/markdown',+ name: 'engine.md',+ parentId: oldFolder.id,+ size: 64,+ url: 'files/engine.md',+ userId,+ })+ .returning();+ const [parseRow] = await db+ .insert(documents)+ .values({+ content: '# Chapter engine',+ fileId: file.id,+ fileType: 'custom/document',+ filename: 'engine.md',+ parentId: oldFolder.id,+ source: 'files/engine.md',+ sourceType: 'file',+ title: 'engine.md',+ totalCharCount: 16,+ totalLineCount: 1,+ userId,+ })+ .returning();+ const [agent] = await db.insert(agents).values({ userId }).returning();+ await db.insert(agentDocuments).values({ agentId: agent.id, documentId: parseRow.id, userId });++ await fileRouter+ .createCaller(context(userId))+ .updateFile({ id: file.id, parentId: newFolder.id });++ expect((await readDocument(db, parseRow.id)).parentId).toBe(newFolder.id);+ });++ it("does not touch another user's document that references the same file", async () => {+ const oldFolder = await createFolder(db, userId, 'nf5bak');+ const newFolder = await createFolder(db, userId, 'archive');+ const foreignFolder = await createFolder(db, otherUserId, 'foreign');+ const { file } = await createFileWithDocument(db, userId, 'shared.pdf', oldFolder.id);+ const [foreignDocument] = await db+ .insert(documents)+ .values({+ fileId: file.id,+ fileType: 'application/pdf',+ parentId: foreignFolder.id,+ source: 'files/shared.pdf',+ sourceType: 'file',+ title: 'shared.pdf',+ totalCharCount: 0,+ totalLineCount: 0,+ userId: otherUserId,+ })+ .returning();++ await fileRouter.createCaller(context(userId)).updateFile({+ id: file.id,+ parentId: newFolder.id,+ });++ expect((await readDocument(db, foreignDocument.id)).parentId).toBe(foreignFolder.id);+ });+});diff --git a/apps/server/src/routers/lambda/file.ts b/apps/server/src/routers/lambda/file.tsindex 02dc5c280b3..4aee003859a 100644--- a/apps/server/src/routers/lambda/file.ts+++ b/apps/server/src/routers/lambda/file.ts@@ -8,6 +8,7 @@ import {RESOURCE_CONTENT_PREVIEW_SOURCE_LENGTH,UPLOAD_FILE_SIZE_LIMIT_ERROR_MESSAGE,} from '@lobechat/const';+import { type LobeChatDatabase } from '@lobechat/database';import { TRPCError } from '@trpc/server';import isEqual from 'fast-deep-equal';import pMap from 'p-map';@@ -1015,7 +1016,21 @@ export const fileRouter = router({}if (Object.keys(updates).length > 0) {- await ctx.fileModel.update(id, updates);+ const wsId = ctx.workspaceId ?? undefined;+ await ctx.serverDB.transaction(async (tx) => {+ const trx = tx as unknown as LobeChatDatabase;+ // The knowledge-base tree reads `documents.parent_id`, so the file's+ // backing document row(s) must move (and rename) together with it.+ // Documents are written before the file, matching updateDocument's+ // lock order so concurrent moves cannot deadlock.+ if (updates.parentId !== undefined || updates.name !== undefined) {+ await new DocumentModel(trx, ctx.userId, wsId).syncFromFile(id, {+ name: updates.name,+ parentId: updates.parentId,+ });+ }+ await new FileModel(trx, ctx.userId, wsId).update(id, updates);+ });}return { success: true };diff --git a/packages/database/src/models/document.ts b/packages/database/src/models/document.tsindex cd62b9b4298..18c9f47dfff 100644--- a/packages/database/src/models/document.ts+++ b/packages/database/src/models/document.ts@@ -15,6 +15,7 @@ import {inArray,isNull,ne,+ notExists,notInArray,or,sum,@@ -22,6 +23,7 @@ import {import type { DocumentItem, NewDocument } from '../schemas';import {+ agentDocuments,DOCUMENT_FOLDER_TYPE,documentCommentMentions,documentComments,@@ -428,6 +430,51 @@ export class DocumentModel {return row?.updatedAt;};+ /**+ * Mirror a file's placement / name onto the document row(s) backed by it+ * (`documents.file_id = fileId`). The knowledge-base tree is rendered from+ * `documents.parent_id`, so moving only the `files` row leaves the item under+ * its old folder. Counterpart of the document β file sync in+ * `DocumentService.updateDocument`.+ */+ syncFromFile = async (fileId: string, value: { name?: string; parentId?: string | null }) => {+ const patch: Partial<DocumentItem> = {};+ if (value.name !== undefined) {+ patch.title = value.name;+ patch.filename = value.name;+ }+ if (value.parentId !== undefined) patch.parentId = value.parentId;+ if (Object.keys(patch).length === 0) return [];++ return this.db+ .update(documents)+ .set({ ...patch, updatedAt: nextDocumentUpdatedAt() })+ .where(+ and(+ this.readScope(),+ eq(documents.fileId, fileId),+ // Skip documents that are an agent's own copy of the file+ // (AgentDocumentsService.importFile): they keep their collision-safe VFS filename and+ // agent-folder parent. Such a copy is created together with its agent_documents+ // binding in one transaction, so both rows share `created_at` (defaultNow() is fixed+ // per transaction). `associateDocument` binds a document that already existed, so an+ // associated knowledge-base mirror or page keeps following the file.+ notExists(+ this.db+ .select({ id: agentDocuments.id })+ .from(agentDocuments)+ .where(+ and(+ eq(agentDocuments.documentId, documents.id),+ eq(agentDocuments.createdAt, documents.createdAt),+ ),+ ),+ ),+ ),+ )+ .returning({ id: documents.id });+ };+/*** Publish one private document into the workspace. Convenience wrapper* around `setVisibility(rootId, 'public')`; kept as a named method for the