@lobehub/chat 1.11.9 → 1.12.0
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 +25 -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/meta/0005_snapshot.json +2119 -0
- package/src/database/server/migrations/meta/0006_snapshot.json +3006 -0
- package/src/database/server/migrations/meta/_journal.json +14 -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 +47 -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 +51 -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
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { Icon } from '@lobehub/ui';
|
|
4
|
+
import { Descriptions, Divider, Tag } from 'antd';
|
|
5
|
+
import { useTheme } from 'antd-style';
|
|
6
|
+
import dayjs from 'dayjs';
|
|
7
|
+
import { BoltIcon } from 'lucide-react';
|
|
8
|
+
import { memo } from 'react';
|
|
9
|
+
import { useTranslation } from 'react-i18next';
|
|
10
|
+
import { Flexbox } from 'react-layout-kit';
|
|
11
|
+
|
|
12
|
+
import { FileListItem } from '@/types/files';
|
|
13
|
+
import { formatSize } from '@/utils/format';
|
|
14
|
+
|
|
15
|
+
export const DETAIL_PANEL_WIDTH = 300;
|
|
16
|
+
|
|
17
|
+
const FileDetail = memo<FileListItem>((props) => {
|
|
18
|
+
const { name, embeddingStatus, size, createdAt, updatedAt, chunkCount } = props || {};
|
|
19
|
+
const { t } = useTranslation('file');
|
|
20
|
+
const theme = useTheme();
|
|
21
|
+
|
|
22
|
+
if (!props) return null;
|
|
23
|
+
|
|
24
|
+
const items = [
|
|
25
|
+
{ children: name, key: 'name', label: t('detail.basic.filename') },
|
|
26
|
+
{ children: formatSize(size), key: 'size', label: t('detail.basic.size') },
|
|
27
|
+
{
|
|
28
|
+
children: name.split('.').pop()?.toUpperCase(),
|
|
29
|
+
key: 'type',
|
|
30
|
+
label: t('detail.basic.type'),
|
|
31
|
+
},
|
|
32
|
+
|
|
33
|
+
{
|
|
34
|
+
children: dayjs(createdAt).format('YYYY-MM-DD HH:mm'),
|
|
35
|
+
key: 'createdAt',
|
|
36
|
+
label: t('detail.basic.createdAt'),
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
children: dayjs(updatedAt).format('YYYY-MM-DD HH:mm'),
|
|
40
|
+
key: 'updatedAt',
|
|
41
|
+
label: t('detail.basic.updatedAt'),
|
|
42
|
+
},
|
|
43
|
+
];
|
|
44
|
+
|
|
45
|
+
const dataItems = [
|
|
46
|
+
{
|
|
47
|
+
children: (
|
|
48
|
+
<Tag bordered={false} icon={<Icon icon={BoltIcon} />}>
|
|
49
|
+
{' '}
|
|
50
|
+
{chunkCount}
|
|
51
|
+
</Tag>
|
|
52
|
+
),
|
|
53
|
+
key: 'chunkCount',
|
|
54
|
+
label: t('detail.data.chunkCount'),
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
children: (
|
|
58
|
+
<Tag bordered={false} color={embeddingStatus || 'default'}>
|
|
59
|
+
{t(`detail.data.embedding.${embeddingStatus || 'default'}`)}
|
|
60
|
+
</Tag>
|
|
61
|
+
),
|
|
62
|
+
key: 'embeddingStatus',
|
|
63
|
+
label: t('detail.data.embeddingStatus'),
|
|
64
|
+
},
|
|
65
|
+
];
|
|
66
|
+
|
|
67
|
+
return (
|
|
68
|
+
<Flexbox
|
|
69
|
+
padding={16}
|
|
70
|
+
style={{ borderInlineStart: `1px solid ${theme.colorSplit}` }}
|
|
71
|
+
width={DETAIL_PANEL_WIDTH}
|
|
72
|
+
>
|
|
73
|
+
<Descriptions
|
|
74
|
+
colon={false}
|
|
75
|
+
column={1}
|
|
76
|
+
items={items}
|
|
77
|
+
labelStyle={{ width: 120 }}
|
|
78
|
+
size={'small'}
|
|
79
|
+
title={t('detail.basic.title')}
|
|
80
|
+
/>
|
|
81
|
+
<Divider />
|
|
82
|
+
<Descriptions
|
|
83
|
+
colon={false}
|
|
84
|
+
column={1}
|
|
85
|
+
items={dataItems}
|
|
86
|
+
labelStyle={{ width: 120 }}
|
|
87
|
+
size={'small'}
|
|
88
|
+
/>
|
|
89
|
+
</Flexbox>
|
|
90
|
+
);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
export default FileDetail;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { notFound } from 'next/navigation';
|
|
2
|
+
import { PropsWithChildren } from 'react';
|
|
3
|
+
|
|
4
|
+
import { serverFeatureFlags } from '@/config/featureFlags';
|
|
5
|
+
|
|
6
|
+
export default ({ children }: PropsWithChildren) => {
|
|
7
|
+
const enableKnowledgeBase = serverFeatureFlags().enableKnowledgeBase;
|
|
8
|
+
|
|
9
|
+
if (!enableKnowledgeBase) return notFound();
|
|
10
|
+
|
|
11
|
+
return children;
|
|
12
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { Icon } from '@lobehub/ui';
|
|
4
|
+
import { Typography } from 'antd';
|
|
5
|
+
import { LoaderCircle } from 'lucide-react';
|
|
6
|
+
import { useTranslation } from 'react-i18next';
|
|
7
|
+
import { Center, Flexbox } from 'react-layout-kit';
|
|
8
|
+
|
|
9
|
+
export default () => {
|
|
10
|
+
const { t } = useTranslation('common');
|
|
11
|
+
return (
|
|
12
|
+
<Center height={'100%'} width={'100%'}>
|
|
13
|
+
<Flexbox align={'center'} gap={8}>
|
|
14
|
+
<div>
|
|
15
|
+
<Icon icon={LoaderCircle} size={'large'} spin />
|
|
16
|
+
</div>
|
|
17
|
+
<Typography.Text type={'secondary'}>{t('loading')}</Typography.Text>
|
|
18
|
+
</Flexbox>
|
|
19
|
+
</Center>
|
|
20
|
+
);
|
|
21
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { Skeleton, Typography } from 'antd';
|
|
4
|
+
import { memo } from 'react';
|
|
5
|
+
import { Center, Flexbox } from 'react-layout-kit';
|
|
6
|
+
|
|
7
|
+
import GoBack from '@/components/GoBack';
|
|
8
|
+
import RepoIcon from '@/components/RepoIcon';
|
|
9
|
+
|
|
10
|
+
import { useKnowledgeBaseItem } from '../../hooks/useKnowledgeItem';
|
|
11
|
+
|
|
12
|
+
const Head = memo<{ id: string }>(({ id }) => {
|
|
13
|
+
const { data, isLoading } = useKnowledgeBaseItem(id);
|
|
14
|
+
|
|
15
|
+
return (
|
|
16
|
+
<Flexbox gap={8}>
|
|
17
|
+
<GoBack href={'/files'} />
|
|
18
|
+
<Flexbox align={'center'} gap={8} height={36} horizontal>
|
|
19
|
+
<Center style={{ minWidth: 24 }} width={24}>
|
|
20
|
+
<RepoIcon />
|
|
21
|
+
</Center>
|
|
22
|
+
{isLoading ? (
|
|
23
|
+
<Skeleton active paragraph={{ rows: 1, style: { marginBottom: 0 } }} title={false} />
|
|
24
|
+
) : (
|
|
25
|
+
<Typography.Text style={{ fontSize: 16, fontWeight: 'bold' }}>
|
|
26
|
+
{data?.name}
|
|
27
|
+
</Typography.Text>
|
|
28
|
+
)}
|
|
29
|
+
</Flexbox>
|
|
30
|
+
</Flexbox>
|
|
31
|
+
);
|
|
32
|
+
});
|
|
33
|
+
export default Head;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { Icon } from '@lobehub/ui';
|
|
4
|
+
import { FileText, Settings2Icon } from 'lucide-react';
|
|
5
|
+
import Link from 'next/link';
|
|
6
|
+
import { usePathname } from 'next/navigation';
|
|
7
|
+
import { memo, useMemo, useState } from 'react';
|
|
8
|
+
import { useTranslation } from 'react-i18next';
|
|
9
|
+
import { Flexbox } from 'react-layout-kit';
|
|
10
|
+
|
|
11
|
+
import Menu from '@/components/Menu';
|
|
12
|
+
import type { MenuProps } from '@/components/Menu';
|
|
13
|
+
|
|
14
|
+
const FileMenu = memo<{ id: string }>(({ id }) => {
|
|
15
|
+
const { t } = useTranslation('knowledgeBase');
|
|
16
|
+
const pathname = usePathname();
|
|
17
|
+
|
|
18
|
+
const [activeKey, setActiveKey] = useState(pathname);
|
|
19
|
+
|
|
20
|
+
const items: MenuProps['items'] = useMemo(
|
|
21
|
+
() => [
|
|
22
|
+
{
|
|
23
|
+
icon: <Icon icon={FileText} />,
|
|
24
|
+
key: `/repos/${id}`,
|
|
25
|
+
label: <Link href={`/repos/${id}`}>{t('tab.files')}</Link>,
|
|
26
|
+
},
|
|
27
|
+
// {
|
|
28
|
+
// icon: <Icon icon={TestTubeDiagonal} />,
|
|
29
|
+
// key: `/repos/${id}/testing`,
|
|
30
|
+
// label: <Link href={`/repos/${id}/testing`}>{t('tab.testing')}</Link>,
|
|
31
|
+
// },
|
|
32
|
+
{
|
|
33
|
+
icon: <Icon icon={Settings2Icon} />,
|
|
34
|
+
key: `/repos/${id}/settings`,
|
|
35
|
+
label: <Link href={`/repos/${id}/settings`}>{t('tab.settings')}</Link>,
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
[t],
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
return (
|
|
42
|
+
<Flexbox>
|
|
43
|
+
<Menu
|
|
44
|
+
items={items}
|
|
45
|
+
onClick={({ key }) => {
|
|
46
|
+
setActiveKey(key);
|
|
47
|
+
}}
|
|
48
|
+
selectable
|
|
49
|
+
selectedKeys={[activeKey]}
|
|
50
|
+
variant={'compact'}
|
|
51
|
+
/>
|
|
52
|
+
</Flexbox>
|
|
53
|
+
);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
export default FileMenu;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Flexbox } from 'react-layout-kit';
|
|
2
|
+
|
|
3
|
+
import Head from './Head';
|
|
4
|
+
import Menu from './Menu';
|
|
5
|
+
|
|
6
|
+
interface Params {
|
|
7
|
+
id: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
type Props = { params: Params };
|
|
11
|
+
|
|
12
|
+
const MenuPage = ({ params }: Props) => {
|
|
13
|
+
const id = params.id;
|
|
14
|
+
|
|
15
|
+
return (
|
|
16
|
+
<Flexbox gap={16} height={'100%'} paddingInline={12} style={{ paddingTop: 12 }}>
|
|
17
|
+
<Head id={id} />
|
|
18
|
+
<Menu id={id} />
|
|
19
|
+
</Flexbox>
|
|
20
|
+
);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
MenuPage.displayName = 'Menu';
|
|
24
|
+
|
|
25
|
+
export default MenuPage;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Flexbox } from 'react-layout-kit';
|
|
2
|
+
|
|
3
|
+
import FilePanel from '@/features/FileSidePanel';
|
|
4
|
+
|
|
5
|
+
import { LayoutProps } from '../type';
|
|
6
|
+
|
|
7
|
+
const Layout = ({ children, menu }: LayoutProps) => {
|
|
8
|
+
return (
|
|
9
|
+
<Flexbox
|
|
10
|
+
height={'100%'}
|
|
11
|
+
horizontal
|
|
12
|
+
style={{ maxWidth: 'calc(100vw - 64px)', overflow: 'hidden', position: 'relative' }}
|
|
13
|
+
width={'100%'}
|
|
14
|
+
>
|
|
15
|
+
<FilePanel>{menu}</FilePanel>
|
|
16
|
+
<Flexbox flex={1} style={{ overflow: 'hidden', position: 'relative' }}>
|
|
17
|
+
{children}
|
|
18
|
+
</Flexbox>
|
|
19
|
+
</Flexbox>
|
|
20
|
+
);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
Layout.displayName = 'DesktopRepoLayout';
|
|
24
|
+
|
|
25
|
+
export default Layout;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { createStyles } from 'antd-style';
|
|
4
|
+
import { memo } from 'react';
|
|
5
|
+
import { Flexbox } from 'react-layout-kit';
|
|
6
|
+
|
|
7
|
+
import { useQuery } from '@/hooks/useQuery';
|
|
8
|
+
|
|
9
|
+
import { LayoutProps } from './type';
|
|
10
|
+
|
|
11
|
+
const useStyles = createStyles(({ css, token }) => ({
|
|
12
|
+
main: css`
|
|
13
|
+
position: relative;
|
|
14
|
+
overflow: hidden;
|
|
15
|
+
background: ${token.colorBgLayout};
|
|
16
|
+
`,
|
|
17
|
+
}));
|
|
18
|
+
|
|
19
|
+
const Layout = memo<LayoutProps>(({ children, menu }) => {
|
|
20
|
+
const { showMobileWorkspace } = useQuery();
|
|
21
|
+
const { styles } = useStyles();
|
|
22
|
+
|
|
23
|
+
return (
|
|
24
|
+
<>
|
|
25
|
+
<Flexbox
|
|
26
|
+
className={styles.main}
|
|
27
|
+
height="100%"
|
|
28
|
+
style={showMobileWorkspace ? { display: 'none' } : undefined}
|
|
29
|
+
width="100%"
|
|
30
|
+
>
|
|
31
|
+
{menu}
|
|
32
|
+
</Flexbox>
|
|
33
|
+
<Flexbox
|
|
34
|
+
className={styles.main}
|
|
35
|
+
height="100%"
|
|
36
|
+
style={showMobileWorkspace ? undefined : { display: 'none' }}
|
|
37
|
+
width="100%"
|
|
38
|
+
>
|
|
39
|
+
{children}
|
|
40
|
+
</Flexbox>
|
|
41
|
+
</>
|
|
42
|
+
);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
Layout.displayName = 'MobileChatLayout';
|
|
46
|
+
|
|
47
|
+
export default Layout;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import ServerLayout from '@/components/server/ServerLayout';
|
|
2
|
+
|
|
3
|
+
import Desktop from './_layout/Desktop';
|
|
4
|
+
import Mobile from './_layout/Mobile';
|
|
5
|
+
import { LayoutProps } from './_layout/type';
|
|
6
|
+
|
|
7
|
+
const Layout = ServerLayout<LayoutProps>({ Desktop, Mobile });
|
|
8
|
+
|
|
9
|
+
Layout.displayName = 'RepoLayout';
|
|
10
|
+
|
|
11
|
+
export default (props: LayoutProps) => {
|
|
12
|
+
return <Layout {...props} />;
|
|
13
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { redirect } from 'next/navigation';
|
|
2
|
+
|
|
3
|
+
import { KnowledgeBaseModel } from '@/database/server/models/knowledgeBase';
|
|
4
|
+
import FileManager from '@/features/FileManager';
|
|
5
|
+
|
|
6
|
+
interface Params {
|
|
7
|
+
id: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
type Props = { params: Params };
|
|
11
|
+
|
|
12
|
+
export default async ({ params }: Props) => {
|
|
13
|
+
const item = await KnowledgeBaseModel.findById(params.id);
|
|
14
|
+
|
|
15
|
+
if (!item) return redirect('/repos');
|
|
16
|
+
|
|
17
|
+
return <FileManager knowledgeBaseId={params.id} title={item.name} />;
|
|
18
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { notFound } from 'next/navigation';
|
|
2
|
+
import { PropsWithChildren } from 'react';
|
|
3
|
+
|
|
4
|
+
import { serverFeatureFlags } from '@/config/featureFlags';
|
|
5
|
+
import { isServerMode } from '@/const/version';
|
|
6
|
+
|
|
7
|
+
export default ({ children }: PropsWithChildren) => {
|
|
8
|
+
const enableKnowledgeBase = serverFeatureFlags().enableKnowledgeBase;
|
|
9
|
+
|
|
10
|
+
if (!isServerMode || !enableKnowledgeBase) return notFound();
|
|
11
|
+
|
|
12
|
+
return children;
|
|
13
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { fetchRequestHandler } from '@trpc/server/adapters/fetch';
|
|
2
|
+
import type { NextRequest } from 'next/server';
|
|
3
|
+
|
|
4
|
+
import { pino } from '@/libs/logger';
|
|
5
|
+
import { createAsyncRouteContext } from '@/server/asyncContext';
|
|
6
|
+
import { asyncRouter } from '@/server/routers/async';
|
|
7
|
+
|
|
8
|
+
export const maxDuration = 60;
|
|
9
|
+
|
|
10
|
+
const handler = (req: NextRequest) =>
|
|
11
|
+
fetchRequestHandler({
|
|
12
|
+
/**
|
|
13
|
+
* @link https://trpc.io/docs/v11/context
|
|
14
|
+
*/
|
|
15
|
+
createContext: () => createAsyncRouteContext(req),
|
|
16
|
+
|
|
17
|
+
endpoint: '/trpc/async',
|
|
18
|
+
|
|
19
|
+
onError: ({ error, path, type }) => {
|
|
20
|
+
pino.info(`Error in tRPC handler (async) on path: ${path}, type: ${type}`);
|
|
21
|
+
console.error(error);
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
req,
|
|
25
|
+
router: asyncRouter,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
export { handler as GET, handler as POST };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { DEFAULT_MODEL } from '@/const/settings';
|
|
2
|
+
import { ChatStreamPayload } from '@/types/openai/chat';
|
|
3
|
+
|
|
4
|
+
export const chainAbstractChunkText = (text: string): Partial<ChatStreamPayload> => {
|
|
5
|
+
return {
|
|
6
|
+
messages: [
|
|
7
|
+
{
|
|
8
|
+
content:
|
|
9
|
+
'你是一名擅长从 chunk 中提取摘要的助理,你需要将用户的会话总结为 1~2 句话的摘要,输出成 chunk 所使用的语种',
|
|
10
|
+
role: 'system',
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
content: `chunk: ${text}`,
|
|
14
|
+
role: 'user',
|
|
15
|
+
},
|
|
16
|
+
],
|
|
17
|
+
model: DEFAULT_MODEL,
|
|
18
|
+
};
|
|
19
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { ChatStreamPayload } from '@/types/openai/chat';
|
|
2
|
+
|
|
3
|
+
export const chainAnswerWithContext = ({
|
|
4
|
+
context,
|
|
5
|
+
knowledge,
|
|
6
|
+
question,
|
|
7
|
+
}: {
|
|
8
|
+
context: string[];
|
|
9
|
+
knowledge: string[];
|
|
10
|
+
question: string;
|
|
11
|
+
}): Partial<ChatStreamPayload> => ({
|
|
12
|
+
messages: [
|
|
13
|
+
{
|
|
14
|
+
content: `You are a helpful assistant good answering questions related to ${knowledge.join('/')}. And you'll be provided with a question and several passages that might be relevant. And your task is to provide answer based on the question and passages.
|
|
15
|
+
|
|
16
|
+
Note that passages might not be relevant to the question, please only use the passages that are relevant. Or if there is no relevant passage, please answer using your knowledge.
|
|
17
|
+
|
|
18
|
+
Answer should use the same original language as the question and follow markdown syntax.
|
|
19
|
+
|
|
20
|
+
The provided passages as context:
|
|
21
|
+
|
|
22
|
+
<Context>
|
|
23
|
+
${context.join('\n')}
|
|
24
|
+
</Context>`,
|
|
25
|
+
role: 'system',
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
content: `The question to answer:
|
|
29
|
+
|
|
30
|
+
${question}`,
|
|
31
|
+
role: 'user',
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ChatStreamPayload } from '@/types/openai/chat';
|
|
2
|
+
|
|
3
|
+
export const chainRewriteQuery = (
|
|
4
|
+
query: string,
|
|
5
|
+
context: string[],
|
|
6
|
+
): Partial<ChatStreamPayload> => ({
|
|
7
|
+
messages: [
|
|
8
|
+
{
|
|
9
|
+
content: `Given the following conversation and a follow-up question, rephrase the follow up question to be a standalone question, in its original language. Keep as much details as possible from previous messages. Keep entity names and all.
|
|
10
|
+
|
|
11
|
+
<chatHistory>
|
|
12
|
+
${context.join('\n')}
|
|
13
|
+
</chatHistory>
|
|
14
|
+
`,
|
|
15
|
+
role: 'system',
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
content: `Follow Up Input: ${query}, it's standalone query:`,
|
|
19
|
+
role: 'user',
|
|
20
|
+
},
|
|
21
|
+
],
|
|
22
|
+
});
|
|
@@ -1,14 +1,15 @@
|
|
|
1
|
+
/* eslint-disable no-undef */
|
|
1
2
|
import { Icon } from '@lobehub/ui';
|
|
2
3
|
import { createStyles } from 'antd-style';
|
|
3
4
|
import { FileImage, FileText, FileUpIcon } from 'lucide-react';
|
|
4
5
|
import { darken, lighten } from 'polished';
|
|
5
|
-
import { memo
|
|
6
|
+
import { memo } from 'react';
|
|
6
7
|
import { createPortal } from 'react-dom';
|
|
7
8
|
import { useTranslation } from 'react-i18next';
|
|
8
9
|
import { Center, Flexbox } from 'react-layout-kit';
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
import { getContainer, useDragUpload } from './useDragUpload';
|
|
12
|
+
|
|
12
13
|
const BLOCK_SIZE = 64;
|
|
13
14
|
const ICON_SIZE = 36;
|
|
14
15
|
|
|
@@ -64,110 +65,16 @@ const useStyles = createStyles(({ css, token }) => {
|
|
|
64
65
|
};
|
|
65
66
|
});
|
|
66
67
|
|
|
67
|
-
const handleDragOver = (e: DragEvent) => {
|
|
68
|
-
if (!e.dataTransfer?.items || e.dataTransfer.items.length === 0) return;
|
|
69
|
-
|
|
70
|
-
const isFile = e.dataTransfer.types.includes('Files');
|
|
71
|
-
if (isFile) {
|
|
72
|
-
e.preventDefault();
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
|
-
|
|
76
68
|
interface DragUploadProps {
|
|
77
69
|
enabledFiles?: boolean;
|
|
78
|
-
onUploadFiles: (files
|
|
70
|
+
onUploadFiles: (files: File[]) => Promise<void>;
|
|
79
71
|
}
|
|
80
72
|
|
|
81
73
|
const DragUpload = memo<DragUploadProps>(({ enabledFiles = true, onUploadFiles }) => {
|
|
82
74
|
const { styles, theme } = useStyles();
|
|
83
75
|
const { t } = useTranslation('components');
|
|
84
|
-
const [isDragging, setIsDragging] = useState(false);
|
|
85
|
-
// When a file is dragged to a different area, the 'dragleave' event may be triggered,
|
|
86
|
-
// causing isDragging to be mistakenly set to false.
|
|
87
|
-
// to fix this issue, use a counter to ensure the status change only when drag event left the browser window .
|
|
88
|
-
const dragCounter = useRef(0);
|
|
89
|
-
|
|
90
|
-
const handleDragEnter = (e: DragEvent) => {
|
|
91
|
-
if (!e.dataTransfer?.items || e.dataTransfer.items.length === 0) return;
|
|
92
|
-
|
|
93
|
-
const isFile = e.dataTransfer.types.includes('Files');
|
|
94
|
-
if (isFile) {
|
|
95
|
-
dragCounter.current += 1;
|
|
96
|
-
e.preventDefault();
|
|
97
|
-
setIsDragging(true);
|
|
98
|
-
}
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
const handleDragLeave = (e: DragEvent) => {
|
|
102
|
-
if (!e.dataTransfer?.items || e.dataTransfer.items.length === 0) return;
|
|
103
|
-
|
|
104
|
-
const isFile = e.dataTransfer.types.includes('Files');
|
|
105
|
-
if (isFile) {
|
|
106
|
-
e.preventDefault();
|
|
107
|
-
|
|
108
|
-
// reset counter
|
|
109
|
-
dragCounter.current -= 1;
|
|
110
|
-
|
|
111
|
-
if (dragCounter.current === 0) {
|
|
112
|
-
setIsDragging(false);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
const handleDrop = async (e: DragEvent) => {
|
|
118
|
-
if (!e.dataTransfer?.items || e.dataTransfer.items.length === 0) return;
|
|
119
|
-
|
|
120
|
-
const isFile = e.dataTransfer.types.includes('Files');
|
|
121
|
-
if (isFile) {
|
|
122
|
-
e.preventDefault();
|
|
123
|
-
|
|
124
|
-
// reset counter
|
|
125
|
-
dragCounter.current = 0;
|
|
126
|
-
|
|
127
|
-
setIsDragging(false);
|
|
128
|
-
|
|
129
|
-
// get filesList
|
|
130
|
-
// TODO: support folder files upload
|
|
131
|
-
const files = e.dataTransfer?.files;
|
|
132
|
-
|
|
133
|
-
// upload files
|
|
134
|
-
onUploadFiles(files);
|
|
135
|
-
}
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
const handlePaste = (event: ClipboardEvent) => {
|
|
139
|
-
// get files from clipboard
|
|
140
|
-
const files = event.clipboardData?.files;
|
|
141
|
-
|
|
142
|
-
onUploadFiles(files);
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
useEffect(() => {
|
|
146
|
-
if (getContainer()) return;
|
|
147
|
-
const root = document.createElement('div');
|
|
148
|
-
root.id = DRAGGING_ROOT_ID;
|
|
149
|
-
document.body.append(root);
|
|
150
|
-
|
|
151
|
-
return () => {
|
|
152
|
-
root.remove();
|
|
153
|
-
};
|
|
154
|
-
}, []);
|
|
155
|
-
|
|
156
|
-
useEffect(() => {
|
|
157
|
-
window.addEventListener('dragenter', handleDragEnter);
|
|
158
|
-
window.addEventListener('dragover', handleDragOver);
|
|
159
|
-
window.addEventListener('dragleave', handleDragLeave);
|
|
160
|
-
window.addEventListener('drop', handleDrop);
|
|
161
|
-
window.addEventListener('paste', handlePaste);
|
|
162
76
|
|
|
163
|
-
|
|
164
|
-
window.removeEventListener('dragenter', handleDragEnter);
|
|
165
|
-
window.removeEventListener('dragover', handleDragOver);
|
|
166
|
-
window.removeEventListener('dragleave', handleDragLeave);
|
|
167
|
-
window.removeEventListener('drop', handleDrop);
|
|
168
|
-
window.removeEventListener('paste', handlePaste);
|
|
169
|
-
};
|
|
170
|
-
}, [handleDragEnter, handleDragOver, handleDragLeave, handleDrop, handlePaste]);
|
|
77
|
+
const isDragging = useDragUpload(onUploadFiles);
|
|
171
78
|
|
|
172
79
|
if (!isDragging) return;
|
|
173
80
|
|