@lobehub/chat 1.11.8 → 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 +50 -0
- package/Dockerfile.database +2 -0
- package/docs/self-hosting/advanced/knowledge-base.zh-CN.mdx +65 -0
- package/locales/ar/chat.json +46 -3
- package/locales/ar/common.json +1 -0
- package/locales/ar/components.json +2 -0
- package/locales/ar/error.json +1 -0
- package/locales/ar/file.json +91 -0
- package/locales/ar/knowledgeBase.json +31 -0
- package/locales/ar/portal.json +1 -0
- package/locales/bg-BG/chat.json +46 -3
- package/locales/bg-BG/common.json +1 -0
- package/locales/bg-BG/components.json +2 -0
- package/locales/bg-BG/error.json +1 -0
- package/locales/bg-BG/file.json +91 -0
- package/locales/bg-BG/knowledgeBase.json +31 -0
- package/locales/bg-BG/portal.json +1 -0
- package/locales/de-DE/chat.json +46 -3
- package/locales/de-DE/common.json +1 -0
- package/locales/de-DE/components.json +2 -0
- package/locales/de-DE/error.json +1 -0
- package/locales/de-DE/file.json +91 -0
- package/locales/de-DE/knowledgeBase.json +31 -0
- package/locales/de-DE/portal.json +1 -0
- package/locales/en-US/chat.json +46 -3
- package/locales/en-US/common.json +1 -0
- package/locales/en-US/components.json +2 -0
- package/locales/en-US/error.json +1 -0
- package/locales/en-US/file.json +91 -0
- package/locales/en-US/knowledgeBase.json +31 -0
- package/locales/en-US/portal.json +1 -0
- package/locales/es-ES/chat.json +46 -3
- package/locales/es-ES/common.json +1 -0
- package/locales/es-ES/components.json +2 -0
- package/locales/es-ES/error.json +1 -0
- package/locales/es-ES/file.json +91 -0
- package/locales/es-ES/knowledgeBase.json +31 -0
- package/locales/es-ES/portal.json +1 -0
- package/locales/fr-FR/chat.json +46 -3
- package/locales/fr-FR/common.json +1 -0
- package/locales/fr-FR/components.json +2 -0
- package/locales/fr-FR/error.json +1 -0
- package/locales/fr-FR/file.json +91 -0
- package/locales/fr-FR/knowledgeBase.json +31 -0
- package/locales/fr-FR/portal.json +1 -0
- package/locales/it-IT/chat.json +46 -3
- package/locales/it-IT/common.json +1 -0
- package/locales/it-IT/components.json +2 -0
- package/locales/it-IT/error.json +1 -0
- package/locales/it-IT/file.json +91 -0
- package/locales/it-IT/knowledgeBase.json +31 -0
- package/locales/it-IT/portal.json +1 -0
- package/locales/ja-JP/chat.json +46 -3
- package/locales/ja-JP/common.json +1 -0
- package/locales/ja-JP/components.json +2 -0
- package/locales/ja-JP/error.json +1 -0
- package/locales/ja-JP/file.json +91 -0
- package/locales/ja-JP/knowledgeBase.json +31 -0
- package/locales/ja-JP/portal.json +1 -0
- package/locales/ko-KR/chat.json +46 -3
- package/locales/ko-KR/common.json +1 -0
- package/locales/ko-KR/components.json +2 -0
- package/locales/ko-KR/error.json +1 -0
- package/locales/ko-KR/file.json +91 -0
- package/locales/ko-KR/knowledgeBase.json +31 -0
- package/locales/ko-KR/portal.json +1 -0
- package/locales/nl-NL/chat.json +46 -3
- package/locales/nl-NL/common.json +1 -0
- package/locales/nl-NL/components.json +2 -0
- package/locales/nl-NL/error.json +1 -0
- package/locales/nl-NL/file.json +91 -0
- package/locales/nl-NL/knowledgeBase.json +31 -0
- package/locales/nl-NL/portal.json +1 -0
- package/locales/pl-PL/chat.json +46 -3
- package/locales/pl-PL/common.json +1 -0
- package/locales/pl-PL/components.json +2 -0
- package/locales/pl-PL/error.json +1 -0
- package/locales/pl-PL/file.json +91 -0
- package/locales/pl-PL/knowledgeBase.json +31 -0
- package/locales/pl-PL/portal.json +1 -0
- package/locales/pt-BR/chat.json +46 -3
- package/locales/pt-BR/common.json +1 -0
- package/locales/pt-BR/components.json +2 -0
- package/locales/pt-BR/error.json +1 -0
- package/locales/pt-BR/file.json +91 -0
- package/locales/pt-BR/knowledgeBase.json +31 -0
- package/locales/pt-BR/portal.json +1 -0
- package/locales/ru-RU/chat.json +46 -3
- package/locales/ru-RU/common.json +1 -0
- package/locales/ru-RU/components.json +2 -0
- package/locales/ru-RU/error.json +1 -0
- package/locales/ru-RU/file.json +91 -0
- package/locales/ru-RU/knowledgeBase.json +31 -0
- package/locales/ru-RU/portal.json +1 -0
- package/locales/tr-TR/chat.json +46 -3
- package/locales/tr-TR/common.json +1 -0
- package/locales/tr-TR/components.json +2 -0
- package/locales/tr-TR/error.json +1 -0
- package/locales/tr-TR/file.json +91 -0
- package/locales/tr-TR/knowledgeBase.json +31 -0
- package/locales/tr-TR/portal.json +1 -0
- package/locales/vi-VN/chat.json +46 -3
- package/locales/vi-VN/common.json +1 -0
- package/locales/vi-VN/components.json +2 -0
- package/locales/vi-VN/error.json +1 -0
- package/locales/vi-VN/file.json +91 -0
- package/locales/vi-VN/knowledgeBase.json +31 -0
- package/locales/vi-VN/portal.json +1 -0
- package/locales/zh-CN/chat.json +46 -3
- package/locales/zh-CN/common.json +1 -0
- package/locales/zh-CN/components.json +2 -0
- package/locales/zh-CN/error.json +1 -0
- package/locales/zh-CN/file.json +91 -0
- package/locales/zh-CN/knowledgeBase.json +31 -0
- package/locales/zh-CN/portal.json +1 -0
- package/locales/zh-TW/chat.json +46 -3
- package/locales/zh-TW/common.json +1 -0
- package/locales/zh-TW/components.json +2 -0
- package/locales/zh-TW/error.json +1 -0
- package/locales/zh-TW/file.json +91 -0
- package/locales/zh-TW/knowledgeBase.json +31 -0
- package/locales/zh-TW/portal.json +1 -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/config/modelProviders/bedrock.ts +5 -5
- 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,133 @@
|
|
|
1
|
+
import { produce } from 'immer';
|
|
2
|
+
|
|
3
|
+
import { FileUploadState, FileUploadStatus, UploadFileItem } from '@/types/files/upload';
|
|
4
|
+
|
|
5
|
+
interface AddFile {
|
|
6
|
+
atStart?: boolean;
|
|
7
|
+
file: UploadFileItem;
|
|
8
|
+
type: 'addFile';
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface AddFiles {
|
|
12
|
+
atStart?: boolean;
|
|
13
|
+
files: UploadFileItem[];
|
|
14
|
+
type: 'addFiles';
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface UpdateFile {
|
|
18
|
+
id: string;
|
|
19
|
+
type: 'updateFile';
|
|
20
|
+
value: Partial<UploadFileItem>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface UpdateFileStatus {
|
|
24
|
+
id: string;
|
|
25
|
+
status: FileUploadStatus;
|
|
26
|
+
type: 'updateFileStatus';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface UpdateFileUploadState {
|
|
30
|
+
id: string;
|
|
31
|
+
type: 'updateFileUploadState';
|
|
32
|
+
uploadState: FileUploadState;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface RemoveFile {
|
|
36
|
+
id: string;
|
|
37
|
+
type: 'removeFile';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface RemoveFiles {
|
|
41
|
+
ids: string[];
|
|
42
|
+
type: 'removeFiles';
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export type UploadFileListDispatch =
|
|
46
|
+
| AddFile
|
|
47
|
+
| UpdateFileStatus
|
|
48
|
+
| UpdateFileUploadState
|
|
49
|
+
| RemoveFile
|
|
50
|
+
| AddFiles
|
|
51
|
+
| UpdateFile
|
|
52
|
+
| RemoveFiles;
|
|
53
|
+
|
|
54
|
+
export const uploadFileListReducer = (
|
|
55
|
+
state: UploadFileItem[],
|
|
56
|
+
action: UploadFileListDispatch,
|
|
57
|
+
): UploadFileItem[] => {
|
|
58
|
+
switch (action.type) {
|
|
59
|
+
case 'addFile': {
|
|
60
|
+
return produce(state, (draftState) => {
|
|
61
|
+
const { atStart, file } = action;
|
|
62
|
+
|
|
63
|
+
if (atStart) {
|
|
64
|
+
draftState.unshift(file);
|
|
65
|
+
} else {
|
|
66
|
+
draftState.push(file);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
case 'addFiles': {
|
|
72
|
+
return produce(state, (draftState) => {
|
|
73
|
+
const { atStart, files } = action;
|
|
74
|
+
|
|
75
|
+
for (const file of files) {
|
|
76
|
+
if (atStart) {
|
|
77
|
+
draftState.unshift(file);
|
|
78
|
+
} else {
|
|
79
|
+
draftState.push(file);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
case 'updateFile': {
|
|
85
|
+
return produce(state, (draftState) => {
|
|
86
|
+
const file = draftState.find((f) => f.id === action.id);
|
|
87
|
+
if (file) {
|
|
88
|
+
Object.assign(file, action.value);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
case 'updateFileStatus': {
|
|
94
|
+
return produce(state, (draftState) => {
|
|
95
|
+
const file = draftState.find((f) => f.id === action.id);
|
|
96
|
+
if (file) {
|
|
97
|
+
file.status = action.status;
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
case 'updateFileUploadState': {
|
|
102
|
+
return produce(state, (draftState) => {
|
|
103
|
+
const file = draftState.find((f) => f.id === action.id);
|
|
104
|
+
if (file) {
|
|
105
|
+
file.uploadState = action.uploadState;
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
case 'removeFile': {
|
|
111
|
+
return produce(state, (draftState) => {
|
|
112
|
+
const index = draftState.findIndex((f) => f.id === action.id);
|
|
113
|
+
if (index !== -1) {
|
|
114
|
+
draftState.splice(index, 1);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
case 'removeFiles': {
|
|
120
|
+
return produce(state, (draftState) => {
|
|
121
|
+
for (const id of action.ids) {
|
|
122
|
+
const index = draftState.findIndex((f) => f.id === id);
|
|
123
|
+
if (index !== -1) {
|
|
124
|
+
draftState.splice(index, 1);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
default: {
|
|
130
|
+
throw new Error('Unhandled action type');
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
};
|
|
@@ -44,45 +44,45 @@ beforeEach(() => {
|
|
|
44
44
|
vi.resetAllMocks();
|
|
45
45
|
});
|
|
46
46
|
|
|
47
|
-
describe('useFileStore:
|
|
48
|
-
it('
|
|
47
|
+
describe('useFileStore:chat', () => {
|
|
48
|
+
it('clearChatUploadFileList should clear the inputFilesList', () => {
|
|
49
49
|
const { result } = renderHook(() => useStore());
|
|
50
50
|
|
|
51
51
|
// Populate the list to clear it later
|
|
52
52
|
act(() => {
|
|
53
|
-
useStore.setState({
|
|
53
|
+
useStore.setState({ chatUploadFileList: [{ id: 'abc' }] as any });
|
|
54
54
|
});
|
|
55
55
|
|
|
56
|
-
expect(result.current.
|
|
56
|
+
expect(result.current.chatUploadFileList).toEqual([{ id: 'abc' }]);
|
|
57
57
|
|
|
58
58
|
act(() => {
|
|
59
|
-
result.current.
|
|
59
|
+
result.current.clearChatUploadFileList();
|
|
60
60
|
});
|
|
61
61
|
|
|
62
|
-
expect(result.current.
|
|
62
|
+
expect(result.current.chatUploadFileList).toEqual([]);
|
|
63
63
|
});
|
|
64
64
|
|
|
65
|
-
it('removeFile should call fileService.removeFile and update the store', async () => {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
});
|
|
65
|
+
// it('removeFile should call fileService.removeFile and update the store', async () => {
|
|
66
|
+
// const { result } = renderHook(() => useStore());
|
|
67
|
+
//
|
|
68
|
+
// const fileId = 'test-id';
|
|
69
|
+
//
|
|
70
|
+
// // Mock the fileService.removeFile to resolve
|
|
71
|
+
// vi.spyOn(fileService, 'removeFile').mockResolvedValue(undefined);
|
|
72
|
+
//
|
|
73
|
+
// // Populate the list to remove an item later
|
|
74
|
+
// act(() => {
|
|
75
|
+
// useStore.setState(({ inputFilesList }) => ({ inputFilesList: [...inputFilesList, fileId] }));
|
|
76
|
+
// // // result.current.inputFilesList.push(fileId);
|
|
77
|
+
// });
|
|
78
|
+
//
|
|
79
|
+
// await act(async () => {
|
|
80
|
+
// await result.current.removeFile(fileId);
|
|
81
|
+
// });
|
|
82
|
+
//
|
|
83
|
+
// expect(fileService.removeFile).toHaveBeenCalledWith(fileId);
|
|
84
|
+
// expect(result.current.inputFilesList).toEqual([]);
|
|
85
|
+
// });
|
|
86
86
|
|
|
87
87
|
// Test for useFetchFile
|
|
88
88
|
it('useFetchFile should call useSWR and update the store', async () => {
|
|
@@ -118,67 +118,67 @@ describe('useFileStore:images', () => {
|
|
|
118
118
|
// Instead, we would need to use a test renderer that can work with hooks, like @testing-library/react
|
|
119
119
|
});
|
|
120
120
|
|
|
121
|
-
describe('uploadFile', () => {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
});
|
|
121
|
+
// describe('uploadFile', () => {
|
|
122
|
+
// it('uploadFile should handle errors', async () => {
|
|
123
|
+
// const { result } = renderHook(() => useStore());
|
|
124
|
+
// const testFile = new File(['content'], 'test.png', { type: 'image/png' });
|
|
125
|
+
//
|
|
126
|
+
// // 模拟 fileService.uploadFile 抛出错误
|
|
127
|
+
// const errorMessage = 'Upload failed';
|
|
128
|
+
// vi.spyOn(uploadService, 'uploadFile').mockRejectedValue(new Error(errorMessage));
|
|
129
|
+
//
|
|
130
|
+
// // Mock console.error for testing
|
|
131
|
+
//
|
|
132
|
+
// await act(async () => {
|
|
133
|
+
// await result.current.uploadFile(testFile);
|
|
134
|
+
// });
|
|
135
|
+
//
|
|
136
|
+
// expect(uploadService.uploadFile).toHaveBeenCalledWith({
|
|
137
|
+
// createdAt: testFile.lastModified,
|
|
138
|
+
// data: await testFile.arrayBuffer(),
|
|
139
|
+
// fileType: testFile.type,
|
|
140
|
+
// name: testFile.name,
|
|
141
|
+
// saveMode: 'local',
|
|
142
|
+
// size: testFile.size,
|
|
143
|
+
// });
|
|
144
|
+
// // 由于上传失败,inputFilesList 应该没有变化
|
|
145
|
+
// expect(result.current.inputFilesList).toEqual([]);
|
|
146
|
+
//
|
|
147
|
+
// // 确保错误提示被调用
|
|
148
|
+
// expect(notification.error).toHaveBeenCalled();
|
|
149
|
+
// });
|
|
150
|
+
//
|
|
151
|
+
// it('uploadFile should upload the file and update inputFilesList', async () => {
|
|
152
|
+
// const { result } = renderHook(() => useStore());
|
|
153
|
+
// const testFile = new File(['content'], 'test.png', { type: 'image/png' });
|
|
154
|
+
//
|
|
155
|
+
// // 模拟 fileService.uploadFile 返回的数据
|
|
156
|
+
// const uploadedFileData = {
|
|
157
|
+
// createdAt: testFile.lastModified,
|
|
158
|
+
// data: await testFile.arrayBuffer(),
|
|
159
|
+
// fileType: testFile.type,
|
|
160
|
+
// name: testFile.name,
|
|
161
|
+
// saveMode: 'local',
|
|
162
|
+
// size: testFile.size,
|
|
163
|
+
// };
|
|
164
|
+
//
|
|
165
|
+
// // Mock the fileService.uploadFile to resolve with uploadedFileData
|
|
166
|
+
// vi.spyOn(uploadService, 'uploadFile').mockResolvedValue(uploadedFileData as DB_File);
|
|
167
|
+
// vi.spyOn(fileService, 'createFile').mockResolvedValue({ id: 'new-file-id', url: '' });
|
|
168
|
+
//
|
|
169
|
+
// await act(async () => {
|
|
170
|
+
// await result.current.uploadFile(testFile);
|
|
171
|
+
// });
|
|
172
|
+
//
|
|
173
|
+
// expect(fileService.createFile).toHaveBeenCalledWith({
|
|
174
|
+
// createdAt: testFile.lastModified,
|
|
175
|
+
// data: await testFile.arrayBuffer(),
|
|
176
|
+
// fileType: testFile.type,
|
|
177
|
+
// name: testFile.name,
|
|
178
|
+
// saveMode: 'local',
|
|
179
|
+
// size: testFile.size,
|
|
180
|
+
// });
|
|
181
|
+
// expect(result.current.inputFilesList).toContain('new-file-id');
|
|
182
|
+
// });
|
|
183
|
+
// });
|
|
184
184
|
});
|
|
@@ -1,33 +1,48 @@
|
|
|
1
|
-
import { t } from 'i18next';
|
|
2
1
|
import { produce } from 'immer';
|
|
3
2
|
import useSWR, { SWRResponse } from 'swr';
|
|
4
3
|
import { StateCreator } from 'zustand/vanilla';
|
|
5
4
|
|
|
6
|
-
import {
|
|
5
|
+
import { FILE_UPLOAD_BLACKLIST } from '@/const/file';
|
|
7
6
|
import { fileService } from '@/services/file';
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
7
|
+
import { ServerService } from '@/services/file/server';
|
|
8
|
+
import { ragService } from '@/services/rag';
|
|
9
|
+
import { userService } from '@/services/user';
|
|
10
|
+
import { useAgentStore } from '@/store/agent';
|
|
11
|
+
import {
|
|
12
|
+
UploadFileListDispatch,
|
|
13
|
+
uploadFileListReducer,
|
|
14
|
+
} from '@/store/file/reducers/uploadFileList';
|
|
15
|
+
import { useUserStore } from '@/store/user';
|
|
16
|
+
import { preferenceSelectors } from '@/store/user/selectors';
|
|
17
|
+
import { FileListItem, FilePreview } from '@/types/files';
|
|
18
|
+
import { UploadFileItem } from '@/types/files/upload';
|
|
19
|
+
import { sleep } from '@/utils/sleep';
|
|
10
20
|
import { setNamespace } from '@/utils/storeDebug';
|
|
11
21
|
|
|
12
22
|
import { FileStore } from '../../store';
|
|
13
23
|
|
|
14
|
-
const n = setNamespace('
|
|
24
|
+
const n = setNamespace('chat');
|
|
15
25
|
|
|
16
|
-
|
|
17
|
-
clearImageList: () => void;
|
|
18
|
-
|
|
19
|
-
internal_addFile: (id: string) => void;
|
|
20
|
-
internal_removeFile: (id: string) => void;
|
|
21
|
-
internal_toggleLoading: (id: string, loading: boolean) => void;
|
|
22
|
-
loadFileDetail: (id: string) => Promise<void>;
|
|
23
|
-
removeAllFiles: () => Promise<void>;
|
|
24
|
-
removeFile: (id: string) => Promise<void>;
|
|
25
|
-
|
|
26
|
-
setImageMapItem: (id: string, item: FilePreview) => void;
|
|
26
|
+
const serverFileService = new ServerService();
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
export interface FileAction {
|
|
29
|
+
clearChatUploadFileList: () => void;
|
|
30
|
+
dispatchChatUploadFileList: (payload: UploadFileListDispatch) => void;
|
|
31
|
+
|
|
32
|
+
removeChatUploadFile: (id: string) => Promise<void>;
|
|
33
|
+
startAsyncTask: (
|
|
34
|
+
fileId: string,
|
|
35
|
+
runner: (id: string) => Promise<string>,
|
|
36
|
+
onFileItemChange: (fileItem: FileListItem) => void,
|
|
37
|
+
) => Promise<void>;
|
|
38
|
+
|
|
39
|
+
uploadChatFiles: (files: File[]) => Promise<void>;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* en: delete it after refactoring the Dalle plugin
|
|
43
|
+
* @deprecated
|
|
44
|
+
*/
|
|
29
45
|
useFetchFile: (id: string) => SWRResponse<FilePreview>;
|
|
30
|
-
useFetchFiles: (ids: string[]) => SWRResponse<void>;
|
|
31
46
|
}
|
|
32
47
|
|
|
33
48
|
export const createFileSlice: StateCreator<
|
|
@@ -36,118 +51,158 @@ export const createFileSlice: StateCreator<
|
|
|
36
51
|
[],
|
|
37
52
|
FileAction
|
|
38
53
|
> = (set, get) => ({
|
|
39
|
-
|
|
40
|
-
set({
|
|
54
|
+
clearChatUploadFileList: () => {
|
|
55
|
+
set({ chatUploadFileList: [] }, false, n('clearChatUploadFileList'));
|
|
41
56
|
},
|
|
42
|
-
|
|
43
|
-
|
|
57
|
+
dispatchChatUploadFileList: (payload) => {
|
|
58
|
+
const nextValue = uploadFileListReducer(get().chatUploadFileList, payload);
|
|
59
|
+
if (nextValue === get().chatUploadFileList) return;
|
|
60
|
+
|
|
61
|
+
set({ chatUploadFileList: nextValue }, false, `dispatchChatFileList/${payload.type}`);
|
|
44
62
|
},
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
);
|
|
63
|
+
removeChatUploadFile: async (id) => {
|
|
64
|
+
const { dispatchChatUploadFileList } = get();
|
|
65
|
+
|
|
66
|
+
dispatchChatUploadFileList({ id, type: 'removeFile' });
|
|
67
|
+
await fileService.removeFile(id);
|
|
51
68
|
},
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
69
|
+
|
|
70
|
+
startAsyncTask: async (id, runner, onFileItemUpdate) => {
|
|
71
|
+
await runner(id);
|
|
72
|
+
|
|
73
|
+
let isFinished = false;
|
|
74
|
+
|
|
75
|
+
while (!isFinished) {
|
|
76
|
+
// 每间隔 2s 查询一次任务状态
|
|
77
|
+
await sleep(2000);
|
|
78
|
+
|
|
79
|
+
let fileItem: FileListItem | undefined = undefined;
|
|
80
|
+
|
|
81
|
+
try {
|
|
82
|
+
fileItem = await serverFileService.getFileItem(id);
|
|
83
|
+
} catch (e) {
|
|
84
|
+
console.error('getFileItem Error:', e);
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (!fileItem) return;
|
|
89
|
+
|
|
90
|
+
onFileItemUpdate(fileItem);
|
|
91
|
+
|
|
92
|
+
if (fileItem.finishEmbedding) {
|
|
93
|
+
isFinished = true;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// if error, also break
|
|
97
|
+
else if (fileItem.chunkingStatus === 'error' || fileItem.embeddingStatus === 'error') {
|
|
98
|
+
isFinished = true;
|
|
99
|
+
}
|
|
65
100
|
}
|
|
66
101
|
},
|
|
67
102
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
103
|
+
uploadChatFiles: async (rawFiles) => {
|
|
104
|
+
const { dispatchChatUploadFileList, startAsyncTask } = get();
|
|
105
|
+
// 0. skip file in blacklist
|
|
106
|
+
const files = rawFiles.filter((file) => !FILE_UPLOAD_BLACKLIST.includes(file.name));
|
|
107
|
+
// 1. add files with base64
|
|
108
|
+
const uploadFiles: UploadFileItem[] = await Promise.all(
|
|
109
|
+
files.map(async (file) => {
|
|
110
|
+
let previewUrl: string | undefined = undefined;
|
|
111
|
+
let base64Url: string | undefined = undefined;
|
|
73
112
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
removeFile: async (id) => {
|
|
78
|
-
get().internal_removeFile(id);
|
|
113
|
+
// only image and video can be previewed, we create a previewUrl and base64Url for them
|
|
114
|
+
if (file.type.startsWith('image') || file.type.startsWith('video')) {
|
|
115
|
+
const data = await file.arrayBuffer();
|
|
79
116
|
|
|
80
|
-
|
|
81
|
-
},
|
|
117
|
+
previewUrl = URL.createObjectURL(new Blob([data!], { type: file.type }));
|
|
82
118
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
if (draft.imagesMap[id]) return;
|
|
119
|
+
const base64 = Buffer.from(data!).toString('base64');
|
|
120
|
+
base64Url = `data:${file.type};base64,${base64}`;
|
|
121
|
+
}
|
|
87
122
|
|
|
88
|
-
|
|
123
|
+
return { base64Url, file, id: file.name, previewUrl, status: 'pending' } as UploadFileItem;
|
|
89
124
|
}),
|
|
90
|
-
false,
|
|
91
|
-
n('setImageMapItem'),
|
|
92
125
|
);
|
|
93
|
-
},
|
|
94
126
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
size: file.size,
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
// at first create a temp id for the file for optimistic rendering
|
|
106
|
-
const tempId = Date.now().toString();
|
|
107
|
-
get().internal_addFile(tempId);
|
|
108
|
-
|
|
109
|
-
get().internal_toggleLoading(tempId, true);
|
|
110
|
-
|
|
111
|
-
// create a local Url for display
|
|
112
|
-
const url = URL.createObjectURL(new Blob([fileItem.data!], { type: fileItem.fileType }));
|
|
113
|
-
get().setImageMapItem(tempId, { ...fileItem, id: tempId, url });
|
|
114
|
-
|
|
115
|
-
try {
|
|
116
|
-
// after finish upload, mark the `loading=false` to show the uploaded item
|
|
117
|
-
const result = await uploadService.uploadFile(fileItem);
|
|
118
|
-
const data = await fileService.createFile(result);
|
|
119
|
-
get().internal_toggleLoading(tempId, false);
|
|
120
|
-
|
|
121
|
-
// after finish upload, remove the temp id and add the final one
|
|
122
|
-
get().internal_removeFile(tempId);
|
|
123
|
-
get().internal_addFile(data.id);
|
|
124
|
-
|
|
125
|
-
// initFile preview
|
|
126
|
-
await get().loadFileDetail(data.id);
|
|
127
|
-
} catch (error) {
|
|
128
|
-
get().internal_removeFile(tempId);
|
|
129
|
-
get().internal_toggleLoading(tempId, false);
|
|
130
|
-
|
|
131
|
-
// show error message
|
|
132
|
-
notification.error({
|
|
133
|
-
description: t('upload.desc', { detail: error, ns: 'error' }),
|
|
134
|
-
message: t('upload.title', { ns: 'error' }),
|
|
127
|
+
dispatchChatUploadFileList({ files: uploadFiles, type: 'addFiles' });
|
|
128
|
+
|
|
129
|
+
// upload files and process it
|
|
130
|
+
const pools = files.map(async (file) => {
|
|
131
|
+
const fileResult = await get().uploadWithProgress({
|
|
132
|
+
file,
|
|
133
|
+
onStatusUpdate: dispatchChatUploadFileList,
|
|
135
134
|
});
|
|
136
|
-
|
|
135
|
+
|
|
136
|
+
if (!fileResult) return;
|
|
137
|
+
|
|
138
|
+
// image don't need to be chunked and embedding
|
|
139
|
+
if (file.type.startsWith('image')) return;
|
|
140
|
+
|
|
141
|
+
// 3. auto chunk and embedding
|
|
142
|
+
dispatchChatUploadFileList({
|
|
143
|
+
id: fileResult.id,
|
|
144
|
+
type: 'updateFile',
|
|
145
|
+
// make the taks empty to hint the user that the task is starting but not triggered
|
|
146
|
+
value: { tasks: {} },
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
await startAsyncTask(
|
|
150
|
+
fileResult.id,
|
|
151
|
+
async (id) => {
|
|
152
|
+
const data = await ragService.createParseFileTask(id);
|
|
153
|
+
if (!data || !data.id) throw new Error('failed to createParseFileTask');
|
|
154
|
+
|
|
155
|
+
// run the assignment
|
|
156
|
+
useAgentStore
|
|
157
|
+
.getState()
|
|
158
|
+
.addFilesToAgent([id], false)
|
|
159
|
+
.then(() => {
|
|
160
|
+
// trigger the tip if it's the first time
|
|
161
|
+
if (!preferenceSelectors.shouldTriggerFileInKnowledgeBaseTip(useUserStore.getState()))
|
|
162
|
+
return;
|
|
163
|
+
|
|
164
|
+
userService.updateGuide({ uploadFileInKnowledgeBase: true });
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
return data.id;
|
|
168
|
+
},
|
|
169
|
+
|
|
170
|
+
(fileItem) => {
|
|
171
|
+
dispatchChatUploadFileList({
|
|
172
|
+
id: fileResult.id,
|
|
173
|
+
type: 'updateFile',
|
|
174
|
+
value: {
|
|
175
|
+
tasks: {
|
|
176
|
+
chunkCount: fileItem.chunkCount,
|
|
177
|
+
chunkingError: fileItem.chunkingError,
|
|
178
|
+
chunkingStatus: fileItem.chunkingStatus,
|
|
179
|
+
embeddingError: fileItem.embeddingError,
|
|
180
|
+
embeddingStatus: fileItem.embeddingStatus,
|
|
181
|
+
finishEmbedding: fileItem.finishEmbedding,
|
|
182
|
+
},
|
|
183
|
+
},
|
|
184
|
+
});
|
|
185
|
+
},
|
|
186
|
+
);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
await Promise.all(pools);
|
|
137
190
|
},
|
|
138
191
|
|
|
139
192
|
useFetchFile: (id) =>
|
|
140
193
|
useSWR(id, async (id) => {
|
|
141
194
|
const item = await fileService.getFile(id);
|
|
142
195
|
|
|
143
|
-
|
|
196
|
+
set(
|
|
197
|
+
produce((draft) => {
|
|
198
|
+
if (draft.imagesMap[id]) return;
|
|
199
|
+
|
|
200
|
+
draft.imagesMap[id] = item;
|
|
201
|
+
}),
|
|
202
|
+
false,
|
|
203
|
+
n('useFetchFile'),
|
|
204
|
+
);
|
|
144
205
|
|
|
145
206
|
return item;
|
|
146
207
|
}),
|
|
147
|
-
useFetchFiles: (ids) =>
|
|
148
|
-
useSWR(ids, async (ids) => {
|
|
149
|
-
const pools = ids.map(async (id) => get().loadFileDetail(id));
|
|
150
|
-
|
|
151
|
-
await Promise.all(pools);
|
|
152
|
-
}),
|
|
153
208
|
});
|