@lobehub/chat 1.11.9 → 1.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/release.yml +2 -1
- package/.github/workflows/test.yml +2 -1
- package/CHANGELOG.md +50 -0
- package/Dockerfile.database +2 -0
- package/docs/self-hosting/advanced/knowledge-base.zh-CN.mdx +65 -0
- package/locales/ar/chat.json +13 -3
- package/locales/ar/components.json +2 -0
- package/locales/bg-BG/chat.json +13 -3
- package/locales/bg-BG/components.json +2 -0
- package/locales/de-DE/chat.json +13 -3
- package/locales/de-DE/components.json +2 -0
- package/locales/en-US/chat.json +13 -3
- package/locales/en-US/components.json +2 -0
- package/locales/es-ES/chat.json +13 -3
- package/locales/es-ES/components.json +2 -0
- package/locales/fr-FR/chat.json +13 -3
- package/locales/fr-FR/components.json +2 -0
- package/locales/it-IT/chat.json +13 -3
- package/locales/it-IT/components.json +2 -0
- package/locales/ja-JP/chat.json +13 -3
- package/locales/ja-JP/components.json +2 -0
- package/locales/ko-KR/chat.json +13 -3
- package/locales/ko-KR/components.json +2 -0
- package/locales/nl-NL/chat.json +13 -3
- package/locales/nl-NL/components.json +2 -0
- package/locales/pl-PL/chat.json +13 -3
- package/locales/pl-PL/components.json +2 -0
- package/locales/pt-BR/chat.json +13 -3
- package/locales/pt-BR/components.json +2 -0
- package/locales/ru-RU/chat.json +13 -3
- package/locales/ru-RU/components.json +2 -0
- package/locales/tr-TR/chat.json +13 -3
- package/locales/tr-TR/components.json +2 -0
- package/locales/vi-VN/chat.json +13 -3
- package/locales/vi-VN/components.json +2 -0
- package/locales/zh-CN/chat.json +13 -3
- package/locales/zh-CN/components.json +2 -0
- package/locales/zh-TW/chat.json +13 -3
- package/locales/zh-TW/components.json +2 -0
- package/package.json +3 -2
- package/scripts/migrateServerDB/docker.cjs +6 -0
- package/scripts/migrateServerDB/errorHint.js +17 -0
- package/scripts/migrateServerDB/index.ts +6 -0
- package/src/app/(main)/chat/(workspace)/@conversation/features/ChatInput/Desktop/FilePreview/FileItem/Content.tsx +37 -0
- package/src/app/(main)/chat/(workspace)/@conversation/features/ChatInput/Desktop/FilePreview/FileItem/index.tsx +87 -0
- package/src/app/(main)/chat/(workspace)/@conversation/features/ChatInput/Desktop/FilePreview/FileItem/style.ts +4 -0
- package/src/app/(main)/chat/(workspace)/@conversation/features/ChatInput/Desktop/FilePreview/FileItem/utils.ts +28 -0
- package/src/app/(main)/chat/(workspace)/@conversation/features/ChatInput/Desktop/FilePreview/FileList.tsx +41 -0
- package/src/app/(main)/chat/(workspace)/@conversation/features/ChatInput/Desktop/FilePreview/index.tsx +40 -0
- package/src/app/(main)/chat/(workspace)/@conversation/features/ChatInput/Desktop/Footer/SendMore.tsx +1 -1
- package/src/app/(main)/chat/(workspace)/@conversation/features/ChatInput/Desktop/Footer/index.tsx +7 -22
- package/src/app/(main)/chat/(workspace)/@conversation/features/ChatInput/Desktop/TextArea.tsx +1 -1
- package/src/app/(main)/chat/(workspace)/@conversation/features/ChatInput/Desktop/index.tsx +2 -4
- package/src/app/(main)/chat/(workspace)/@conversation/features/ChatInput/Mobile/Files/FileItem/File.tsx +72 -0
- package/src/app/(main)/chat/(workspace)/@conversation/features/ChatInput/Mobile/Files/FileItem/Image.tsx +74 -0
- package/src/app/(main)/chat/(workspace)/@conversation/features/ChatInput/Mobile/Files/FileItem/index.tsx +39 -0
- package/src/app/(main)/chat/(workspace)/@conversation/features/ChatInput/Mobile/Files/FileItem/style.ts +1 -0
- package/src/app/(main)/chat/(workspace)/@conversation/features/ChatInput/Mobile/Files/index.tsx +33 -0
- package/src/app/(main)/chat/(workspace)/@conversation/features/ChatInput/Mobile/InputArea/Container.tsx +41 -0
- package/src/app/(main)/chat/(workspace)/@conversation/features/ChatInput/Mobile/InputArea/index.tsx +154 -0
- package/src/app/(main)/chat/(workspace)/@conversation/features/ChatInput/Mobile/Send.tsx +34 -0
- package/src/app/(main)/chat/(workspace)/@conversation/features/ChatInput/Mobile/index.tsx +24 -11
- package/src/app/(main)/chat/(workspace)/@conversation/features/ChatInput/components/UploadDetail/UploadStatus.tsx +71 -0
- package/src/app/(main)/chat/(workspace)/@conversation/features/ChatInput/components/UploadDetail/index.tsx +49 -0
- package/src/app/(main)/chat/(workspace)/@portal/FilePreview/index.tsx +26 -0
- package/src/app/(main)/chat/(workspace)/@portal/Home/Files/FileList/Item.tsx +53 -0
- package/src/app/(main)/chat/(workspace)/@portal/Home/Files/FileList/index.tsx +50 -0
- package/src/app/(main)/chat/(workspace)/@portal/Home/Files/index.tsx +21 -0
- package/src/app/(main)/chat/(workspace)/@portal/Home/index.tsx +2 -0
- package/src/app/(main)/chat/(workspace)/@portal/router.tsx +4 -0
- package/src/app/(main)/chat/(workspace)/_layout/Desktop/ChatHeader/KnowledgeTag.tsx +41 -0
- package/src/app/(main)/chat/(workspace)/_layout/Desktop/ChatHeader/Tags.tsx +7 -2
- package/src/app/(main)/chat/(workspace)/_layout/Desktop/HotKeys.tsx +1 -1
- package/src/app/(main)/files/(content)/@menu/default.tsx +27 -0
- package/src/app/(main)/files/(content)/@menu/features/FileMenu/index.tsx +97 -0
- package/src/app/(main)/files/(content)/@menu/features/KnowledgeBase/EmptyStatus.tsx +53 -0
- package/src/app/(main)/files/(content)/@menu/features/KnowledgeBase/Item/Content.tsx +175 -0
- package/src/app/(main)/files/(content)/@menu/features/KnowledgeBase/Item/index.tsx +69 -0
- package/src/app/(main)/files/(content)/@menu/features/KnowledgeBase/KnowledgeBaseList.tsx +30 -0
- package/src/app/(main)/files/(content)/@menu/features/KnowledgeBase/SkeletonList.tsx +57 -0
- package/src/app/(main)/files/(content)/@menu/features/KnowledgeBase/index.tsx +54 -0
- package/src/app/(main)/files/(content)/@modal/(.)[id]/FileDetail.tsx +16 -0
- package/src/app/(main)/files/(content)/@modal/(.)[id]/FilePreview.tsx +15 -0
- package/src/app/(main)/files/(content)/@modal/(.)[id]/FullscreenModal.tsx +85 -0
- package/src/app/(main)/files/(content)/@modal/(.)[id]/page.tsx +19 -0
- package/src/app/(main)/files/(content)/@modal/default.tsx +3 -0
- package/src/app/(main)/files/(content)/NotSupportClient.tsx +152 -0
- package/src/app/(main)/files/(content)/_layout/Desktop/index.tsx +28 -0
- package/src/app/(main)/files/(content)/_layout/Mobile.tsx +47 -0
- package/src/app/(main)/files/(content)/_layout/type.ts +7 -0
- package/src/app/(main)/files/(content)/layout.tsx +18 -0
- package/src/app/(main)/files/(content)/page.tsx +14 -0
- package/src/app/(main)/files/[id]/Header.tsx +63 -0
- package/src/app/(main)/files/[id]/page.tsx +44 -0
- package/src/app/(main)/files/features/FileDetail.tsx +93 -0
- package/src/app/(main)/files/hooks/useFileCategory.ts +9 -0
- package/src/app/(main)/files/layout.tsx +12 -0
- package/src/app/(main)/files/loading.tsx +21 -0
- package/src/app/(main)/repos/[id]/@menu/Head/index.tsx +33 -0
- package/src/app/(main)/repos/[id]/@menu/Menu/index.tsx +56 -0
- package/src/app/(main)/repos/[id]/@menu/default.tsx +25 -0
- package/src/app/(main)/repos/[id]/_layout/Desktop/index.tsx +25 -0
- package/src/app/(main)/repos/[id]/_layout/Mobile.tsx +47 -0
- package/src/app/(main)/repos/[id]/_layout/type.ts +6 -0
- package/src/app/(main)/repos/[id]/hooks/useKnowledgeItem.ts +7 -0
- package/src/app/(main)/repos/[id]/layout.tsx +13 -0
- package/src/app/(main)/repos/[id]/page.tsx +18 -0
- package/src/app/(main)/repos/layout.tsx +13 -0
- package/src/app/(main)/repos/page.tsx +5 -0
- package/src/app/trpc/async/[trpc]/route.ts +28 -0
- package/src/chains/abstractChunk.ts +19 -0
- package/src/chains/answerWithContext.ts +34 -0
- package/src/chains/rewriteQuery.ts +22 -0
- package/src/components/DragUpload/index.tsx +6 -99
- package/src/components/DragUpload/useDragUpload.tsx +146 -0
- package/src/components/FeatureList/index.tsx +64 -0
- package/src/components/FileParsingStatus/index.tsx +230 -0
- package/src/components/ImageItem/index.tsx +10 -2
- package/src/components/KnowledgeIcon/index.tsx +28 -0
- package/src/config/app.ts +6 -1
- package/src/config/featureFlags/schema.ts +1 -1
- package/src/const/file.ts +1 -0
- package/src/const/url.ts +1 -0
- package/src/database/client/models/file.ts +8 -2
- package/src/database/server/migrations/0005_pgvector.sql +2 -0
- package/src/database/server/migrations/0006_add_knowledge_base.sql +307 -0
- package/src/database/server/migrations/0007_fix_embedding_table.sql +18 -0
- package/src/database/server/migrations/meta/0005_snapshot.json +2119 -0
- package/src/database/server/migrations/meta/0006_snapshot.json +3006 -0
- package/src/database/server/migrations/meta/0007_snapshot.json +3012 -0
- package/src/database/server/migrations/meta/_journal.json +21 -0
- package/src/database/server/models/__tests__/_test_template.ts +155 -0
- package/src/database/server/models/__tests__/agent.test.ts +226 -0
- package/src/database/server/models/__tests__/asyncTask.test.ts +176 -0
- package/src/database/server/models/__tests__/chunk.test.ts +336 -0
- package/src/database/server/models/__tests__/file.test.ts +317 -29
- package/src/database/server/models/__tests__/fixtures/embedding.ts +568 -0
- package/src/database/server/models/__tests__/knowledgeBase.test.ts +132 -0
- package/src/database/server/models/__tests__/message.test.ts +7 -4
- package/src/database/server/models/_template.ts +10 -1
- package/src/database/server/models/agent.ts +165 -0
- package/src/database/server/models/asyncTask.ts +96 -0
- package/src/database/server/models/chunk.ts +203 -0
- package/src/database/server/models/embedding.ts +50 -0
- package/src/database/server/models/file.ts +231 -12
- package/src/database/server/models/knowledgeBase.ts +94 -0
- package/src/database/server/models/message.ts +156 -30
- package/src/database/server/models/user.ts +12 -1
- package/src/database/server/schemas/lobechat/agent.ts +93 -0
- package/src/database/server/schemas/lobechat/discover.ts +1 -1
- package/src/database/server/schemas/lobechat/file.ts +118 -1
- package/src/database/server/schemas/lobechat/index.ts +5 -1
- package/src/database/server/schemas/lobechat/message.ts +169 -0
- package/src/database/server/schemas/lobechat/rag.ts +53 -0
- package/src/database/server/schemas/lobechat/relations.ts +68 -48
- package/src/database/server/schemas/lobechat/session.ts +77 -0
- package/src/database/server/schemas/lobechat/topic.ts +32 -0
- package/src/database/server/schemas/lobechat/user.ts +40 -25
- package/src/database/server/utils/idGenerator.ts +1 -0
- package/src/features/ChatInput/ActionBar/Clear.tsx +1 -1
- package/src/features/ChatInput/ActionBar/Knowledge/Dropdown.tsx +160 -0
- package/src/features/ChatInput/ActionBar/Knowledge/ListItem.tsx +52 -0
- package/src/features/ChatInput/ActionBar/Knowledge/index.tsx +54 -0
- package/src/features/ChatInput/ActionBar/Tools/index.tsx +1 -1
- package/src/features/ChatInput/ActionBar/Upload/ClientMode.tsx +52 -0
- package/src/features/ChatInput/ActionBar/Upload/ServerMode.tsx +104 -0
- package/src/features/ChatInput/ActionBar/Upload/index.tsx +8 -0
- package/src/features/ChatInput/ActionBar/config.ts +14 -5
- package/src/features/ChatInput/useSend.ts +16 -7
- package/src/features/Conversation/Messages/Assistant/FileChunks/Item/index.tsx +51 -0
- package/src/features/Conversation/Messages/Assistant/FileChunks/Item/style.ts +38 -0
- package/src/features/Conversation/Messages/Assistant/FileChunks/index.tsx +76 -0
- package/src/features/Conversation/Messages/Assistant/index.tsx +13 -4
- package/src/features/Conversation/Messages/Default.tsx +4 -0
- package/src/features/Conversation/Messages/User/BelowMessage.tsx +78 -0
- package/src/features/Conversation/Messages/User/FileListViewer/Item.tsx +53 -0
- package/src/features/Conversation/Messages/User/FileListViewer/index.tsx +21 -0
- package/src/{components/FileList/FileListViewer.tsx → features/Conversation/Messages/User/ImageFileListViewer.tsx} +10 -2
- package/src/features/Conversation/Messages/{User.tsx → User/index.tsx} +11 -2
- package/src/features/Conversation/Messages/index.ts +8 -3
- package/src/features/Conversation/components/ChatItem/index.tsx +33 -10
- package/src/features/Conversation/components/InboxWelcome/QuestionSuggest.tsx +1 -1
- package/src/features/Conversation/types/index.tsx +1 -0
- package/src/features/FileManager/ChunkDrawer/ChunkList/ChunkItem.tsx +61 -0
- package/src/features/FileManager/ChunkDrawer/ChunkList/index.tsx +42 -0
- package/src/features/FileManager/ChunkDrawer/Content.tsx +38 -0
- package/src/features/FileManager/ChunkDrawer/Loading/index.tsx +16 -0
- package/src/features/FileManager/ChunkDrawer/SimilaritySearchList/Item.tsx +62 -0
- package/src/features/FileManager/ChunkDrawer/SimilaritySearchList/index.tsx +31 -0
- package/src/features/FileManager/ChunkDrawer/index.tsx +48 -0
- package/src/features/FileManager/FileList/EmptyStatus.tsx +153 -0
- package/src/features/FileManager/FileList/FileListItem/ChunkTag.tsx +35 -0
- package/src/features/FileManager/FileList/FileListItem/DropdownMenu.tsx +150 -0
- package/src/features/FileManager/FileList/FileListItem/index.tsx +211 -0
- package/src/features/FileManager/FileList/FileSkeleton.tsx +25 -0
- package/src/features/FileManager/FileList/ToolBar/Config.tsx +28 -0
- package/src/features/FileManager/FileList/ToolBar/MultiSelectActions.tsx +152 -0
- package/src/features/FileManager/FileList/ToolBar/index.tsx +114 -0
- package/src/features/FileManager/FileList/index.tsx +143 -0
- package/src/features/FileManager/FileList/useCheckTaskStatus.ts +27 -0
- package/src/features/FileManager/Header/FilesSearchBar.tsx +41 -0
- package/src/features/FileManager/Header/UploadFileButton.tsx +79 -0
- package/src/features/FileManager/Header/index.tsx +39 -0
- package/src/features/FileManager/UploadDock/Item.tsx +124 -0
- package/src/features/FileManager/UploadDock/index.tsx +183 -0
- package/src/features/FileManager/index.tsx +38 -0
- package/src/features/FileSidePanel/index.tsx +79 -0
- package/src/features/FileViewer/NotSupport/index.tsx +54 -0
- package/src/features/FileViewer/PDFViewer/HighlightLayer.tsx +81 -0
- package/src/features/FileViewer/PDFViewer/index.tsx +93 -0
- package/src/features/FileViewer/PDFViewer/style.ts +20 -0
- package/src/features/FileViewer/PDFViewer/useResizeObserver.ts +33 -0
- package/src/features/FileViewer/TXTViewer/index.tsx +41 -0
- package/src/features/FileViewer/index.tsx +45 -0
- package/src/features/KnowledgeBaseModal/AddFilesToKnowledgeBase/SelectForm.tsx +115 -0
- package/src/features/KnowledgeBaseModal/AddFilesToKnowledgeBase/index.tsx +43 -0
- package/src/features/KnowledgeBaseModal/AssignKnowledgeBase/Item/Action.tsx +103 -0
- package/src/features/KnowledgeBaseModal/AssignKnowledgeBase/Item/EditCustomPlugin.tsx +55 -0
- package/src/features/KnowledgeBaseModal/AssignKnowledgeBase/Item/PluginTag.tsx +58 -0
- package/src/features/KnowledgeBaseModal/AssignKnowledgeBase/Item/index.tsx +70 -0
- package/src/features/KnowledgeBaseModal/AssignKnowledgeBase/List.tsx +49 -0
- package/src/features/KnowledgeBaseModal/AssignKnowledgeBase/Loading.tsx +13 -0
- package/src/features/KnowledgeBaseModal/AssignKnowledgeBase/index.tsx +39 -0
- package/src/features/KnowledgeBaseModal/CreateNew/CreateForm.tsx +73 -0
- package/src/features/KnowledgeBaseModal/CreateNew/index.tsx +35 -0
- package/src/features/KnowledgeBaseModal/index.ts +3 -0
- package/src/libs/langchain/loaders/pdf/index.ts +2 -2
- package/src/libs/trpc/async/asyncAuth.ts +24 -0
- package/src/libs/trpc/async/index.ts +10 -0
- package/src/libs/trpc/async/init.ts +11 -0
- package/src/libs/trpc/client/async.ts +14 -0
- package/src/libs/trpc/client/index.ts +1 -0
- package/src/libs/trpc/client/lambda.ts +9 -0
- package/src/libs/trpc/middleware/keyVaults.ts +18 -0
- package/src/libs/unstructured/__tests__/index.test.ts +0 -10
- package/src/libs/unstructured/index.ts +6 -11
- package/src/locales/default/chat.ts +47 -3
- package/src/locales/default/common.ts +1 -0
- package/src/locales/default/components.ts +2 -0
- package/src/locales/default/error.ts +6 -1
- package/src/locales/default/file.ts +92 -0
- package/src/locales/default/index.ts +4 -0
- package/src/locales/default/knowledgeBase.ts +31 -0
- package/src/locales/default/portal.ts +1 -0
- package/src/middleware.ts +3 -0
- package/src/server/asyncContext.ts +40 -0
- package/src/server/modules/ContentChunk/index.ts +135 -0
- package/src/server/modules/S3/index.ts +30 -5
- package/src/server/routers/async/caller.ts +27 -0
- package/src/server/routers/async/file.ts +247 -0
- package/src/server/routers/async/index.ts +12 -0
- package/src/server/routers/lambda/_template.ts +77 -0
- package/src/server/routers/lambda/agent.ts +159 -0
- package/src/server/routers/lambda/chunk.ts +189 -0
- package/src/server/routers/lambda/file.ts +129 -5
- package/src/server/routers/lambda/index.ts +6 -0
- package/src/server/routers/lambda/knowledgeBase.ts +79 -0
- package/src/server/routers/lambda/message.ts +6 -0
- package/src/server/routers/lambda/session.ts +0 -25
- package/src/server/routers/lambda/user.ts +5 -1
- package/src/server/services/chunk/index.ts +74 -0
- package/src/server/utils/files.ts +9 -0
- package/src/services/__tests__/chat.test.ts +18 -20
- package/src/services/__tests__/{upload.test.ts → upload_legacy.test.ts} +1 -1
- package/src/services/agent.ts +45 -0
- package/src/services/chat.ts +17 -15
- package/src/services/file/client.test.ts +1 -50
- package/src/services/file/client.ts +12 -25
- package/src/services/file/server.ts +25 -3
- package/src/services/file/type.ts +7 -4
- package/src/services/knowledgeBase.ts +34 -0
- package/src/services/message/client.test.ts +1 -1
- package/src/services/message/client.ts +29 -3
- package/src/services/message/index.ts +0 -2
- package/src/services/message/server.ts +9 -3
- package/src/services/message/type.ts +1 -13
- package/src/services/rag.ts +29 -0
- package/src/services/session/server.ts +1 -1
- package/src/services/upload.ts +89 -84
- package/src/services/upload_legacy.ts +104 -0
- package/src/services/user/client.ts +7 -2
- package/src/services/user/server.ts +6 -2
- package/src/services/user/type.ts +3 -2
- package/src/store/agent/slices/chat/action.ts +90 -18
- package/src/store/agent/slices/chat/initialState.ts +1 -0
- package/src/store/agent/slices/chat/selectors.ts +58 -0
- package/src/store/chat/slices/builtinTool/action.test.ts +2 -2
- package/src/store/chat/slices/builtinTool/action.ts +2 -2
- package/src/store/chat/slices/message/action.test.ts +2 -1
- package/src/store/chat/slices/message/action.ts +102 -26
- package/src/store/chat/slices/message/actions/rag.ts +148 -0
- package/src/store/chat/slices/message/initialState.ts +7 -0
- package/src/store/chat/slices/message/reducer.ts +6 -2
- package/src/store/chat/slices/message/selectors.ts +38 -3
- package/src/store/chat/slices/plugin/action.ts +8 -2
- package/src/store/chat/slices/portal/action.ts +8 -0
- package/src/store/chat/slices/portal/initialState.ts +3 -0
- package/src/store/chat/slices/portal/selectors.ts +8 -2
- package/src/store/file/initialState.ts +5 -1
- package/src/store/file/reducers/uploadFileList.ts +133 -0
- package/src/store/file/selectors.ts +3 -0
- package/src/store/file/slices/chat/action.test.ts +90 -90
- package/src/store/file/slices/chat/action.ts +164 -109
- package/src/store/file/slices/chat/initialState.ts +7 -2
- package/src/store/file/slices/chat/selectors.test.ts +84 -61
- package/src/store/file/slices/chat/selectors.ts +22 -32
- package/src/store/file/slices/chunk/action.ts +36 -0
- package/src/store/file/slices/chunk/index.ts +3 -0
- package/src/store/file/slices/chunk/initialState.ts +15 -0
- package/src/store/file/slices/chunk/selectors.ts +10 -0
- package/src/store/file/slices/fileManager/action.ts +187 -0
- package/src/store/file/slices/fileManager/index.ts +3 -0
- package/src/store/file/slices/fileManager/initialState.ts +18 -0
- package/src/store/file/slices/fileManager/selectors.ts +58 -0
- package/src/store/file/slices/tts/action.test.ts +1 -1
- package/src/store/file/slices/tts/action.ts +2 -2
- package/src/store/file/slices/upload/action.ts +164 -0
- package/src/store/file/store.ts +12 -1
- package/src/store/knowledgeBase/index.ts +2 -0
- package/src/store/knowledgeBase/initialState.ts +7 -0
- package/src/store/knowledgeBase/selectors.ts +1 -0
- package/src/store/knowledgeBase/slices/content/action.ts +27 -0
- package/src/store/knowledgeBase/slices/content/index.ts +1 -0
- package/src/store/knowledgeBase/slices/crud/action.ts +78 -0
- package/src/store/knowledgeBase/slices/crud/index.ts +3 -0
- package/src/store/knowledgeBase/slices/crud/initialState.ts +12 -0
- package/src/store/knowledgeBase/slices/crud/selectors.ts +7 -0
- package/src/store/knowledgeBase/store.ts +30 -0
- package/src/store/serverConfig/selectors.test.ts +1 -1
- package/src/store/user/slices/preference/selectors.ts +8 -0
- package/src/store/user/slices/settings/selectors/systemAgent.ts +2 -0
- package/src/tools/dalle/Render/Item/ImageFileItem.tsx +3 -23
- package/src/types/agent/index.ts +9 -0
- package/src/types/asyncTask.ts +31 -0
- package/src/types/chunk/document.ts +9 -0
- package/src/types/chunk/index.ts +52 -0
- package/src/types/files/index.ts +35 -0
- package/src/types/files/list.ts +44 -0
- package/src/types/files/upload.ts +91 -0
- package/src/types/knowledgeBase/index.ts +45 -0
- package/src/types/message/index.ts +54 -5
- package/src/types/rag.ts +16 -0
- package/src/utils/filter.test.ts +2 -0
- package/src/utils/server/auth.ts +23 -0
- package/src/app/(main)/chat/(workspace)/@conversation/features/ChatInput/Desktop/LocalFiles.tsx +0 -46
- package/src/app/(main)/chat/(workspace)/@conversation/features/ChatInput/Mobile/Files.tsx +0 -19
- package/src/components/FileList/EditableFileList.tsx +0 -47
- package/src/components/FileList/index.ts +0 -2
- package/src/components/FileList/type.tsx +0 -7
- package/src/database/server/schemas/lobechat/chat.ts +0 -331
- package/src/features/ChatInput/ActionBar/FileUpload.tsx +0 -69
- package/src/features/ChatInput/useChatInput.ts +0 -45
- package/src/features/FileList/EditableFileList.tsx +0 -31
- package/src/features/FileList/FileListPreviewer.tsx +0 -17
- package/src/features/FileList/index.tsx +0 -2
- package/src/types/files.ts +0 -42
|
@@ -3,8 +3,15 @@ import { eq } from 'drizzle-orm';
|
|
|
3
3
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
4
4
|
|
|
5
5
|
import { getTestDBInstance } from '@/database/server/core/dbForTest';
|
|
6
|
+
import { FilesTabs, SortType } from '@/types/files';
|
|
6
7
|
|
|
7
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
files,
|
|
10
|
+
globalFiles,
|
|
11
|
+
knowledgeBaseFiles,
|
|
12
|
+
knowledgeBases,
|
|
13
|
+
users,
|
|
14
|
+
} from '../../schemas/lobechat';
|
|
8
15
|
import { FileModel } from '../file';
|
|
9
16
|
|
|
10
17
|
let serverDB = await getTestDBInstance();
|
|
@@ -18,30 +25,92 @@ vi.mock('@/database/server/core/db', async () => ({
|
|
|
18
25
|
const userId = 'file-model-test-user-id';
|
|
19
26
|
const fileModel = new FileModel(userId);
|
|
20
27
|
|
|
28
|
+
const knowledgeBase = { id: 'kb1', userId, name: 'knowledgeBase' };
|
|
21
29
|
beforeEach(async () => {
|
|
22
|
-
await serverDB.delete(users)
|
|
23
|
-
await serverDB.insert(users).values({ id: userId });
|
|
30
|
+
await serverDB.delete(users);
|
|
31
|
+
await serverDB.insert(users).values([{ id: userId }, { id: 'user2' }]);
|
|
32
|
+
await serverDB.insert(knowledgeBases).values(knowledgeBase);
|
|
24
33
|
});
|
|
25
34
|
|
|
26
35
|
afterEach(async () => {
|
|
27
|
-
await serverDB.delete(users)
|
|
28
|
-
await serverDB.delete(files)
|
|
36
|
+
await serverDB.delete(users);
|
|
37
|
+
await serverDB.delete(files);
|
|
38
|
+
await serverDB.delete(globalFiles);
|
|
29
39
|
});
|
|
30
40
|
|
|
31
41
|
describe('FileModel', () => {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
42
|
+
describe('create', () => {
|
|
43
|
+
it('should create a new file', async () => {
|
|
44
|
+
const params = {
|
|
45
|
+
name: 'test-file.txt',
|
|
46
|
+
url: 'https://example.com/test-file.txt',
|
|
47
|
+
size: 100,
|
|
48
|
+
fileType: 'text/plain',
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const { id } = await fileModel.create(params);
|
|
52
|
+
expect(id).toBeDefined();
|
|
53
|
+
|
|
54
|
+
const file = await serverDB.query.files.findFirst({ where: eq(files.id, id) });
|
|
55
|
+
expect(file).toMatchObject({ ...params, userId });
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('should create a file with knowledgeBaseId', async () => {
|
|
59
|
+
const params = {
|
|
60
|
+
name: 'test-file.txt',
|
|
61
|
+
url: 'https://example.com/test-file.txt',
|
|
62
|
+
size: 100,
|
|
63
|
+
fileType: 'text/plain',
|
|
64
|
+
knowledgeBaseId: 'kb1',
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const { id } = await fileModel.create(params);
|
|
68
|
+
|
|
69
|
+
const kbFile = await serverDB.query.knowledgeBaseFiles.findFirst({
|
|
70
|
+
where: eq(knowledgeBaseFiles.fileId, id),
|
|
71
|
+
});
|
|
72
|
+
expect(kbFile).toMatchObject({ fileId: id, knowledgeBaseId: 'kb1' });
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('should create a global file', async () => {
|
|
77
|
+
const globalFile = {
|
|
78
|
+
hashId: 'test-hash',
|
|
37
79
|
fileType: 'text/plain',
|
|
80
|
+
size: 100,
|
|
81
|
+
url: 'https://example.com/global-file.txt',
|
|
82
|
+
metadata: { key: 'value' },
|
|
38
83
|
};
|
|
39
84
|
|
|
40
|
-
const
|
|
41
|
-
expect(
|
|
85
|
+
const result = await fileModel.createGlobalFile(globalFile);
|
|
86
|
+
expect(result[0]).toMatchObject(globalFile);
|
|
87
|
+
});
|
|
88
|
+
describe('checkHash', () => {
|
|
89
|
+
it('should return isExist: false for non-existent hash', async () => {
|
|
90
|
+
const result = await fileModel.checkHash('non-existent-hash');
|
|
91
|
+
expect(result).toEqual({ isExist: false });
|
|
92
|
+
});
|
|
42
93
|
|
|
43
|
-
|
|
44
|
-
|
|
94
|
+
it('should return file info for existing hash', async () => {
|
|
95
|
+
const globalFile = {
|
|
96
|
+
hashId: 'existing-hash',
|
|
97
|
+
fileType: 'text/plain',
|
|
98
|
+
size: 100,
|
|
99
|
+
url: 'https://example.com/existing-file.txt',
|
|
100
|
+
metadata: { key: 'value' },
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
await serverDB.insert(globalFiles).values(globalFile);
|
|
104
|
+
|
|
105
|
+
const result = await fileModel.checkHash('existing-hash');
|
|
106
|
+
expect(result).toEqual({
|
|
107
|
+
isExist: true,
|
|
108
|
+
fileType: 'text/plain',
|
|
109
|
+
size: 100,
|
|
110
|
+
url: 'https://example.com/existing-file.txt',
|
|
111
|
+
metadata: { key: 'value' },
|
|
112
|
+
});
|
|
113
|
+
});
|
|
45
114
|
});
|
|
46
115
|
|
|
47
116
|
it('should delete a file by id', async () => {
|
|
@@ -58,27 +127,27 @@ describe('FileModel', () => {
|
|
|
58
127
|
expect(file).toBeUndefined();
|
|
59
128
|
});
|
|
60
129
|
|
|
61
|
-
it('should
|
|
62
|
-
await fileModel.create({
|
|
63
|
-
name: '
|
|
64
|
-
url: 'https://example.com/
|
|
130
|
+
it('should delete multiple files', async () => {
|
|
131
|
+
const file1 = await fileModel.create({
|
|
132
|
+
name: 'file1.txt',
|
|
133
|
+
url: 'https://example.com/file1.txt',
|
|
65
134
|
size: 100,
|
|
66
135
|
fileType: 'text/plain',
|
|
67
136
|
});
|
|
68
|
-
await fileModel.create({
|
|
69
|
-
name: '
|
|
70
|
-
url: 'https://example.com/
|
|
137
|
+
const file2 = await fileModel.create({
|
|
138
|
+
name: 'file2.txt',
|
|
139
|
+
url: 'https://example.com/file2.txt',
|
|
71
140
|
size: 200,
|
|
72
141
|
fileType: 'text/plain',
|
|
73
142
|
});
|
|
74
143
|
|
|
75
|
-
await fileModel.
|
|
144
|
+
await fileModel.deleteMany([file1.id, file2.id]);
|
|
76
145
|
|
|
77
|
-
const
|
|
78
|
-
expect(
|
|
146
|
+
const remainingFiles = await serverDB.query.files.findMany({ where: eq(files.userId, userId) });
|
|
147
|
+
expect(remainingFiles).toHaveLength(0);
|
|
79
148
|
});
|
|
80
149
|
|
|
81
|
-
it('should
|
|
150
|
+
it('should clear all files for the user', async () => {
|
|
82
151
|
await fileModel.create({
|
|
83
152
|
name: 'test-file-1.txt',
|
|
84
153
|
url: 'https://example.com/test-file-1.txt',
|
|
@@ -92,10 +161,177 @@ describe('FileModel', () => {
|
|
|
92
161
|
fileType: 'text/plain',
|
|
93
162
|
});
|
|
94
163
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
expect(userFiles
|
|
164
|
+
await fileModel.clear();
|
|
165
|
+
|
|
166
|
+
const userFiles = await serverDB.query.files.findMany({ where: eq(files.userId, userId) });
|
|
167
|
+
expect(userFiles).toHaveLength(0);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
describe('Query', () => {
|
|
171
|
+
const sharedFileList = [
|
|
172
|
+
{
|
|
173
|
+
name: 'document.pdf',
|
|
174
|
+
url: 'https://example.com/document.pdf',
|
|
175
|
+
size: 1000,
|
|
176
|
+
fileType: 'application/pdf',
|
|
177
|
+
userId,
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
name: 'image.jpg',
|
|
181
|
+
url: 'https://example.com/image.jpg',
|
|
182
|
+
size: 500,
|
|
183
|
+
fileType: 'image/jpeg',
|
|
184
|
+
userId,
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
name: 'audio.mp3',
|
|
188
|
+
url: 'https://example.com/audio.mp3',
|
|
189
|
+
size: 2000,
|
|
190
|
+
fileType: 'audio/mpeg',
|
|
191
|
+
userId,
|
|
192
|
+
},
|
|
193
|
+
];
|
|
194
|
+
|
|
195
|
+
it('should query files for the user', async () => {
|
|
196
|
+
await fileModel.create({
|
|
197
|
+
name: 'test-file-1.txt',
|
|
198
|
+
url: 'https://example.com/test-file-1.txt',
|
|
199
|
+
size: 100,
|
|
200
|
+
fileType: 'text/plain',
|
|
201
|
+
});
|
|
202
|
+
await fileModel.create({
|
|
203
|
+
name: 'test-file-2.txt',
|
|
204
|
+
url: 'https://example.com/test-file-2.txt',
|
|
205
|
+
size: 200,
|
|
206
|
+
fileType: 'text/plain',
|
|
207
|
+
});
|
|
208
|
+
await serverDB.insert(files).values({
|
|
209
|
+
name: 'audio.mp3',
|
|
210
|
+
url: 'https://example.com/audio.mp3',
|
|
211
|
+
size: 2000,
|
|
212
|
+
fileType: 'audio/mpeg',
|
|
213
|
+
userId: 'user2',
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
const userFiles = await fileModel.query();
|
|
217
|
+
expect(userFiles).toHaveLength(2);
|
|
218
|
+
expect(userFiles[0].name).toBe('test-file-2.txt');
|
|
219
|
+
expect(userFiles[1].name).toBe('test-file-1.txt');
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it('should filter files by name', async () => {
|
|
223
|
+
await serverDB.insert(files).values(sharedFileList);
|
|
224
|
+
const filteredFiles = await fileModel.query({ q: 'DOC' });
|
|
225
|
+
expect(filteredFiles).toHaveLength(1);
|
|
226
|
+
expect(filteredFiles[0].name).toBe('document.pdf');
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it('should filter files by category', async () => {
|
|
230
|
+
await serverDB.insert(files).values(sharedFileList);
|
|
231
|
+
|
|
232
|
+
const imageFiles = await fileModel.query({ category: FilesTabs.Images });
|
|
233
|
+
expect(imageFiles).toHaveLength(1);
|
|
234
|
+
expect(imageFiles[0].name).toBe('image.jpg');
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it('should sort files by name in ascending order', async () => {
|
|
238
|
+
await serverDB.insert(files).values(sharedFileList);
|
|
239
|
+
|
|
240
|
+
const sortedFiles = await fileModel.query({ sortType: SortType.Asc, sorter: 'name' });
|
|
241
|
+
expect(sortedFiles[0].name).toBe('audio.mp3');
|
|
242
|
+
expect(sortedFiles[2].name).toBe('image.jpg');
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it('should sort files by size in descending order', async () => {
|
|
246
|
+
await serverDB.insert(files).values(sharedFileList);
|
|
247
|
+
|
|
248
|
+
const sortedFiles = await fileModel.query({ sortType: SortType.Desc, sorter: 'size' });
|
|
249
|
+
expect(sortedFiles[0].name).toBe('audio.mp3');
|
|
250
|
+
expect(sortedFiles[2].name).toBe('image.jpg');
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it('should combine filtering and sorting', async () => {
|
|
254
|
+
await serverDB.insert(files).values([
|
|
255
|
+
...sharedFileList,
|
|
256
|
+
{
|
|
257
|
+
name: 'big_document.pdf',
|
|
258
|
+
url: 'https://example.com/big_document.pdf',
|
|
259
|
+
size: 5000,
|
|
260
|
+
fileType: 'application/pdf',
|
|
261
|
+
userId,
|
|
262
|
+
},
|
|
263
|
+
]);
|
|
264
|
+
|
|
265
|
+
const filteredAndSortedFiles = await fileModel.query({
|
|
266
|
+
category: FilesTabs.Documents,
|
|
267
|
+
sortType: SortType.Desc,
|
|
268
|
+
sorter: 'size',
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
expect(filteredAndSortedFiles).toHaveLength(2);
|
|
272
|
+
expect(filteredAndSortedFiles[0].name).toBe('big_document.pdf');
|
|
273
|
+
expect(filteredAndSortedFiles[1].name).toBe('document.pdf');
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
it('should return an empty array when no files match the query', async () => {
|
|
277
|
+
await serverDB.insert(files).values(sharedFileList);
|
|
278
|
+
const noFiles = await fileModel.query({ q: 'nonexistent' });
|
|
279
|
+
expect(noFiles).toHaveLength(0);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
it('should handle invalid sort field gracefully', async () => {
|
|
283
|
+
await serverDB.insert(files).values(sharedFileList);
|
|
284
|
+
|
|
285
|
+
const result = await fileModel.query({
|
|
286
|
+
sortType: SortType.Asc,
|
|
287
|
+
sorter: 'invalidField' as any,
|
|
288
|
+
});
|
|
289
|
+
expect(result).toHaveLength(3);
|
|
290
|
+
// Should default to sorting by createdAt in descending order
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
describe('Query with knowledge base', () => {
|
|
294
|
+
beforeEach(async () => {
|
|
295
|
+
await serverDB.insert(files).values([
|
|
296
|
+
{
|
|
297
|
+
id: 'file1',
|
|
298
|
+
name: 'file1.txt',
|
|
299
|
+
userId,
|
|
300
|
+
fileType: 'text/plain',
|
|
301
|
+
size: 100,
|
|
302
|
+
url: 'url1',
|
|
303
|
+
},
|
|
304
|
+
{
|
|
305
|
+
id: 'file2',
|
|
306
|
+
name: 'file2.txt',
|
|
307
|
+
userId,
|
|
308
|
+
fileType: 'text/plain',
|
|
309
|
+
size: 200,
|
|
310
|
+
url: 'url2',
|
|
311
|
+
},
|
|
312
|
+
]);
|
|
313
|
+
await serverDB
|
|
314
|
+
.insert(knowledgeBaseFiles)
|
|
315
|
+
.values([{ fileId: 'file1', knowledgeBaseId: 'kb1' }]);
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
it('should query files in a specific knowledge base', async () => {
|
|
319
|
+
const result = await fileModel.query({ knowledgeBaseId: 'kb1' });
|
|
320
|
+
expect(result).toHaveLength(1);
|
|
321
|
+
expect(result[0].id).toBe('file1');
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
it('should exclude files in knowledge bases when showFilesInKnowledgeBase is false', async () => {
|
|
325
|
+
const result = await fileModel.query({ showFilesInKnowledgeBase: false });
|
|
326
|
+
expect(result).toHaveLength(1);
|
|
327
|
+
expect(result[0].id).toBe('file2');
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
it('should include all files when showFilesInKnowledgeBase is true', async () => {
|
|
331
|
+
const result = await fileModel.query({ showFilesInKnowledgeBase: true });
|
|
332
|
+
expect(result).toHaveLength(2);
|
|
333
|
+
});
|
|
334
|
+
});
|
|
99
335
|
});
|
|
100
336
|
|
|
101
337
|
it('should find a file by id', async () => {
|
|
@@ -137,4 +373,56 @@ describe('FileModel', () => {
|
|
|
137
373
|
userId,
|
|
138
374
|
});
|
|
139
375
|
});
|
|
376
|
+
|
|
377
|
+
it('should countFilesByHash', async () => {
|
|
378
|
+
const fileList = [
|
|
379
|
+
{
|
|
380
|
+
id: '1',
|
|
381
|
+
name: 'document.pdf',
|
|
382
|
+
url: 'https://example.com/document.pdf',
|
|
383
|
+
fileHash: 'hash1',
|
|
384
|
+
size: 1000,
|
|
385
|
+
fileType: 'application/pdf',
|
|
386
|
+
userId,
|
|
387
|
+
},
|
|
388
|
+
{
|
|
389
|
+
id: '2',
|
|
390
|
+
name: 'image.jpg',
|
|
391
|
+
url: 'https://example.com/image.jpg',
|
|
392
|
+
fileHash: 'hash2',
|
|
393
|
+
size: 500,
|
|
394
|
+
fileType: 'image/jpeg',
|
|
395
|
+
userId,
|
|
396
|
+
},
|
|
397
|
+
{
|
|
398
|
+
id: '5',
|
|
399
|
+
name: 'document.pdf',
|
|
400
|
+
url: 'https://example.com/document.pdf',
|
|
401
|
+
fileHash: 'hash1',
|
|
402
|
+
size: 1000,
|
|
403
|
+
fileType: 'application/pdf',
|
|
404
|
+
userId: 'user2',
|
|
405
|
+
},
|
|
406
|
+
];
|
|
407
|
+
|
|
408
|
+
await serverDB.insert(globalFiles).values([
|
|
409
|
+
{
|
|
410
|
+
hashId: 'hash1',
|
|
411
|
+
url: 'https://example.com/document.pdf',
|
|
412
|
+
size: 1000,
|
|
413
|
+
fileType: 'application/pdf',
|
|
414
|
+
},
|
|
415
|
+
{
|
|
416
|
+
hashId: 'hash2',
|
|
417
|
+
url: 'https://example.com/image.jpg',
|
|
418
|
+
size: 500,
|
|
419
|
+
fileType: 'image/jpeg',
|
|
420
|
+
},
|
|
421
|
+
]);
|
|
422
|
+
|
|
423
|
+
await serverDB.insert(files).values(fileList);
|
|
424
|
+
|
|
425
|
+
const data = await fileModel.countFilesByHash('hash1');
|
|
426
|
+
expect(data).toEqual(2);
|
|
427
|
+
});
|
|
140
428
|
});
|