@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
|
@@ -35,6 +35,20 @@
|
|
|
35
35
|
"when": 1721724512422,
|
|
36
36
|
"tag": "0004_add_next_auth",
|
|
37
37
|
"breakpoints": true
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
"idx": 5,
|
|
41
|
+
"version": "7",
|
|
42
|
+
"when": 1722944166657,
|
|
43
|
+
"tag": "0005_pgvector",
|
|
44
|
+
"breakpoints": true
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
"idx": 6,
|
|
48
|
+
"version": "7",
|
|
49
|
+
"when": 1724089032064,
|
|
50
|
+
"tag": "0006_add_knowledge_base",
|
|
51
|
+
"breakpoints": true
|
|
38
52
|
}
|
|
39
53
|
],
|
|
40
54
|
"version": "6"
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
// @vitest-environment node
|
|
2
|
+
import { eq } from 'drizzle-orm';
|
|
3
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
4
|
+
|
|
5
|
+
import { getTestDBInstance } from '@/database/server/core/dbForTest';
|
|
6
|
+
|
|
7
|
+
import { sessionGroups, users } from '../../schemas/lobechat';
|
|
8
|
+
import { SessionGroupModel } from '../sessionGroup';
|
|
9
|
+
|
|
10
|
+
let serverDB = await getTestDBInstance();
|
|
11
|
+
|
|
12
|
+
vi.mock('@/database/server/core/db', async () => ({
|
|
13
|
+
get serverDB() {
|
|
14
|
+
return serverDB;
|
|
15
|
+
},
|
|
16
|
+
}));
|
|
17
|
+
|
|
18
|
+
const userId = 'session-group-model-test-user-id';
|
|
19
|
+
const sessionGroupModel = new SessionGroupModel(userId);
|
|
20
|
+
|
|
21
|
+
beforeEach(async () => {
|
|
22
|
+
await serverDB.delete(users);
|
|
23
|
+
await serverDB.insert(users).values([{ id: userId }, { id: 'user2' }]);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
afterEach(async () => {
|
|
27
|
+
await serverDB.delete(users).where(eq(users.id, userId));
|
|
28
|
+
await serverDB.delete(sessionGroups).where(eq(sessionGroups.userId, userId));
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('SessionGroupModel', () => {
|
|
32
|
+
describe('create', () => {
|
|
33
|
+
it('should create a new session group', async () => {
|
|
34
|
+
const params = {
|
|
35
|
+
name: 'Test Group',
|
|
36
|
+
sort: 1,
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const result = await sessionGroupModel.create(params);
|
|
40
|
+
expect(result.id).toBeDefined();
|
|
41
|
+
expect(result).toMatchObject({ ...params, userId });
|
|
42
|
+
|
|
43
|
+
const group = await serverDB.query.sessionGroups.findFirst({
|
|
44
|
+
where: eq(sessionGroups.id, result.id),
|
|
45
|
+
});
|
|
46
|
+
expect(group).toMatchObject({ ...params, userId });
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
describe('delete', () => {
|
|
50
|
+
it('should delete a session group by id', async () => {
|
|
51
|
+
const { id } = await sessionGroupModel.create({ name: 'Test Group' });
|
|
52
|
+
|
|
53
|
+
await sessionGroupModel.delete(id);
|
|
54
|
+
|
|
55
|
+
const group = await serverDB.query.sessionGroups.findFirst({
|
|
56
|
+
where: eq(sessionGroups.id, id),
|
|
57
|
+
});
|
|
58
|
+
expect(group).toBeUndefined();
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
describe('deleteAll', () => {
|
|
62
|
+
it('should delete all session groups for the user', async () => {
|
|
63
|
+
await sessionGroupModel.create({ name: 'Test Group 1' });
|
|
64
|
+
await sessionGroupModel.create({ name: 'Test Group 2' });
|
|
65
|
+
|
|
66
|
+
await sessionGroupModel.deleteAll();
|
|
67
|
+
|
|
68
|
+
const userGroups = await serverDB.query.sessionGroups.findMany({
|
|
69
|
+
where: eq(sessionGroups.userId, userId),
|
|
70
|
+
});
|
|
71
|
+
expect(userGroups).toHaveLength(0);
|
|
72
|
+
});
|
|
73
|
+
it('should only delete session groups for the user, not others', async () => {
|
|
74
|
+
await sessionGroupModel.create({ name: 'Test Group 1' });
|
|
75
|
+
await sessionGroupModel.create({ name: 'Test Group 333' });
|
|
76
|
+
|
|
77
|
+
const anotherSessionGroupModel = new SessionGroupModel('user2');
|
|
78
|
+
await anotherSessionGroupModel.create({ name: 'Test Group 2' });
|
|
79
|
+
|
|
80
|
+
await sessionGroupModel.deleteAll();
|
|
81
|
+
|
|
82
|
+
const userGroups = await serverDB.query.sessionGroups.findMany({
|
|
83
|
+
where: eq(sessionGroups.userId, userId),
|
|
84
|
+
});
|
|
85
|
+
const total = await serverDB.query.sessionGroups.findMany();
|
|
86
|
+
expect(userGroups).toHaveLength(0);
|
|
87
|
+
expect(total).toHaveLength(1);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
describe('query', () => {
|
|
92
|
+
it('should query session groups for the user', async () => {
|
|
93
|
+
await sessionGroupModel.create({ name: 'Test Group 1', sort: 2 });
|
|
94
|
+
await sessionGroupModel.create({ name: 'Test Group 2', sort: 1 });
|
|
95
|
+
|
|
96
|
+
const userGroups = await sessionGroupModel.query();
|
|
97
|
+
expect(userGroups).toHaveLength(2);
|
|
98
|
+
expect(userGroups[0].name).toBe('Test Group 2');
|
|
99
|
+
expect(userGroups[1].name).toBe('Test Group 1');
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
describe('findById', () => {
|
|
104
|
+
it('should find a session group by id', async () => {
|
|
105
|
+
const { id } = await sessionGroupModel.create({ name: 'Test Group' });
|
|
106
|
+
|
|
107
|
+
const group = await sessionGroupModel.findById(id);
|
|
108
|
+
expect(group).toMatchObject({
|
|
109
|
+
id,
|
|
110
|
+
name: 'Test Group',
|
|
111
|
+
userId,
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe('update', () => {
|
|
117
|
+
it('should update a session group', async () => {
|
|
118
|
+
const { id } = await sessionGroupModel.create({ name: 'Test Group' });
|
|
119
|
+
|
|
120
|
+
await sessionGroupModel.update(id, { name: 'Updated Test Group', sort: 3 });
|
|
121
|
+
|
|
122
|
+
const updatedGroup = await serverDB.query.sessionGroups.findFirst({
|
|
123
|
+
where: eq(sessionGroups.id, id),
|
|
124
|
+
});
|
|
125
|
+
expect(updatedGroup).toMatchObject({
|
|
126
|
+
id,
|
|
127
|
+
name: 'Updated Test Group',
|
|
128
|
+
sort: 3,
|
|
129
|
+
userId,
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
describe('updateOrder', () => {
|
|
135
|
+
it('should update order of session groups', async () => {
|
|
136
|
+
const group1 = await sessionGroupModel.create({ name: 'Test Group 1', sort: 1 });
|
|
137
|
+
const group2 = await sessionGroupModel.create({ name: 'Test Group 2', sort: 2 });
|
|
138
|
+
|
|
139
|
+
await sessionGroupModel.updateOrder([
|
|
140
|
+
{ id: group1.id, sort: 3 },
|
|
141
|
+
{ id: group2.id, sort: 4 },
|
|
142
|
+
]);
|
|
143
|
+
|
|
144
|
+
const updatedGroup1 = await serverDB.query.sessionGroups.findFirst({
|
|
145
|
+
where: eq(sessionGroups.id, group1.id),
|
|
146
|
+
});
|
|
147
|
+
const updatedGroup2 = await serverDB.query.sessionGroups.findFirst({
|
|
148
|
+
where: eq(sessionGroups.id, group2.id),
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
expect(updatedGroup1?.sort).toBe(3);
|
|
152
|
+
expect(updatedGroup2?.sort).toBe(4);
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
});
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
// @vitest-environment node
|
|
2
|
+
import { eq } from 'drizzle-orm';
|
|
3
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
4
|
+
|
|
5
|
+
import { getTestDBInstance } from '@/database/server/core/dbForTest';
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
agents,
|
|
9
|
+
agentsFiles,
|
|
10
|
+
agentsKnowledgeBases,
|
|
11
|
+
agentsToSessions,
|
|
12
|
+
files,
|
|
13
|
+
knowledgeBases,
|
|
14
|
+
sessions,
|
|
15
|
+
users,
|
|
16
|
+
} from '../../schemas/lobechat';
|
|
17
|
+
import { AgentModel } from '../agent';
|
|
18
|
+
|
|
19
|
+
let serverDB = await getTestDBInstance();
|
|
20
|
+
|
|
21
|
+
vi.mock('@/database/server/core/db', async () => ({
|
|
22
|
+
get serverDB() {
|
|
23
|
+
return serverDB;
|
|
24
|
+
},
|
|
25
|
+
}));
|
|
26
|
+
|
|
27
|
+
const userId = 'agent-model-test-user-id';
|
|
28
|
+
const agentModel = new AgentModel(userId);
|
|
29
|
+
|
|
30
|
+
const knowledgeBase = { id: 'kb1', userId, name: 'knowledgeBase' };
|
|
31
|
+
const fileList = [
|
|
32
|
+
{
|
|
33
|
+
id: '1',
|
|
34
|
+
name: 'document.pdf',
|
|
35
|
+
url: 'https://a.com/document.pdf',
|
|
36
|
+
size: 1000,
|
|
37
|
+
fileType: 'application/pdf',
|
|
38
|
+
userId,
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
id: '2',
|
|
42
|
+
name: 'image.jpg',
|
|
43
|
+
url: 'https://a.com/image.jpg',
|
|
44
|
+
size: 500,
|
|
45
|
+
fileType: 'image/jpeg',
|
|
46
|
+
userId,
|
|
47
|
+
},
|
|
48
|
+
];
|
|
49
|
+
|
|
50
|
+
beforeEach(async () => {
|
|
51
|
+
await serverDB.delete(users);
|
|
52
|
+
await serverDB.insert(users).values([{ id: userId }]);
|
|
53
|
+
await serverDB.insert(knowledgeBases).values(knowledgeBase);
|
|
54
|
+
await serverDB.insert(files).values(fileList);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
afterEach(async () => {
|
|
58
|
+
await serverDB.delete(users).where(eq(users.id, userId));
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe('AgentModel', () => {
|
|
62
|
+
describe('getAgentConfigById', () => {
|
|
63
|
+
it('should return agent config with assigned knowledge', async () => {
|
|
64
|
+
const agentId = 'test-agent-id';
|
|
65
|
+
await serverDB.insert(agents).values({ id: agentId, userId });
|
|
66
|
+
await serverDB
|
|
67
|
+
.insert(agentsKnowledgeBases)
|
|
68
|
+
.values({ agentId, knowledgeBaseId: 'kb1', userId });
|
|
69
|
+
await serverDB.insert(agentsFiles).values({ agentId, fileId: '1', userId });
|
|
70
|
+
|
|
71
|
+
const result = await agentModel.getAgentConfigById(agentId);
|
|
72
|
+
|
|
73
|
+
expect(result).toBeDefined();
|
|
74
|
+
expect(result.id).toBe(agentId);
|
|
75
|
+
expect(result.knowledgeBases).toHaveLength(1);
|
|
76
|
+
expect(result.files).toHaveLength(1);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe('findBySessionId', () => {
|
|
81
|
+
it('should find agent by session id', async () => {
|
|
82
|
+
const agentId = 'test-agent-id';
|
|
83
|
+
const sessionId = 'test-session-id';
|
|
84
|
+
await serverDB.insert(agents).values({ id: agentId, userId });
|
|
85
|
+
await serverDB.insert(sessions).values({ id: sessionId, userId });
|
|
86
|
+
await serverDB.insert(agentsToSessions).values({ agentId, sessionId });
|
|
87
|
+
|
|
88
|
+
const result = await agentModel.findBySessionId(sessionId);
|
|
89
|
+
|
|
90
|
+
expect(result).toBeDefined();
|
|
91
|
+
expect(result?.id).toBe(agentId);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe('createAgentKnowledgeBase', () => {
|
|
96
|
+
it('should create a new agent knowledge base association', async () => {
|
|
97
|
+
const agent = await serverDB
|
|
98
|
+
.insert(agents)
|
|
99
|
+
.values({ userId })
|
|
100
|
+
.returning()
|
|
101
|
+
.then((res) => res[0]);
|
|
102
|
+
|
|
103
|
+
await agentModel.createAgentKnowledgeBase(agent.id, knowledgeBase.id);
|
|
104
|
+
|
|
105
|
+
const result = await serverDB.query.agentsKnowledgeBases.findFirst({
|
|
106
|
+
where: eq(agentsKnowledgeBases.agentId, agent.id),
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
expect(result).toMatchObject({
|
|
110
|
+
agentId: agent.id,
|
|
111
|
+
knowledgeBaseId: knowledgeBase.id,
|
|
112
|
+
userId,
|
|
113
|
+
enabled: true,
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
describe('deleteAgentKnowledgeBase', () => {
|
|
119
|
+
it('should delete an agent knowledge base association', async () => {
|
|
120
|
+
const agent = await serverDB
|
|
121
|
+
.insert(agents)
|
|
122
|
+
.values({ userId })
|
|
123
|
+
.returning()
|
|
124
|
+
.then((res) => res[0]);
|
|
125
|
+
await serverDB
|
|
126
|
+
.insert(agentsKnowledgeBases)
|
|
127
|
+
.values({ agentId: agent.id, knowledgeBaseId: knowledgeBase.id, userId });
|
|
128
|
+
|
|
129
|
+
await agentModel.deleteAgentKnowledgeBase(agent.id, knowledgeBase.id);
|
|
130
|
+
|
|
131
|
+
const result = await serverDB.query.agentsKnowledgeBases.findFirst({
|
|
132
|
+
where: eq(agentsKnowledgeBases.agentId, agent.id),
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
expect(result).toBeUndefined();
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
describe('toggleKnowledgeBase', () => {
|
|
140
|
+
it('should toggle the enabled status of an agent knowledge base association', async () => {
|
|
141
|
+
const agent = await serverDB
|
|
142
|
+
.insert(agents)
|
|
143
|
+
.values({ userId })
|
|
144
|
+
.returning()
|
|
145
|
+
.then((res) => res[0]);
|
|
146
|
+
|
|
147
|
+
await serverDB
|
|
148
|
+
.insert(agentsKnowledgeBases)
|
|
149
|
+
.values({ agentId: agent.id, knowledgeBaseId: knowledgeBase.id, userId, enabled: true });
|
|
150
|
+
|
|
151
|
+
await agentModel.toggleKnowledgeBase(agent.id, knowledgeBase.id, false);
|
|
152
|
+
|
|
153
|
+
const result = await serverDB.query.agentsKnowledgeBases.findFirst({
|
|
154
|
+
where: eq(agentsKnowledgeBases.agentId, agent.id),
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
expect(result?.enabled).toBe(false);
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
describe('createAgentFiles', () => {
|
|
162
|
+
it('should create new agent file associations', async () => {
|
|
163
|
+
const agent = await serverDB
|
|
164
|
+
.insert(agents)
|
|
165
|
+
.values({ userId })
|
|
166
|
+
.returning()
|
|
167
|
+
.then((res) => res[0]);
|
|
168
|
+
|
|
169
|
+
await agentModel.createAgentFiles(agent.id, ['1', '2']);
|
|
170
|
+
|
|
171
|
+
const results = await serverDB.query.agentsFiles.findMany({
|
|
172
|
+
where: eq(agentsFiles.agentId, agent.id),
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
expect(results).toHaveLength(2);
|
|
176
|
+
expect(results).toEqual(
|
|
177
|
+
expect.arrayContaining([
|
|
178
|
+
expect.objectContaining({ agentId: agent.id, fileId: '1', userId, enabled: true }),
|
|
179
|
+
expect.objectContaining({ agentId: agent.id, fileId: '2', userId, enabled: true }),
|
|
180
|
+
]),
|
|
181
|
+
);
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
describe('deleteAgentFile', () => {
|
|
186
|
+
it('should delete an agent file association', async () => {
|
|
187
|
+
const agent = await serverDB
|
|
188
|
+
.insert(agents)
|
|
189
|
+
.values({ userId })
|
|
190
|
+
.returning()
|
|
191
|
+
.then((res) => res[0]);
|
|
192
|
+
|
|
193
|
+
await serverDB.insert(agentsFiles).values({ agentId: agent.id, fileId: '1', userId });
|
|
194
|
+
|
|
195
|
+
await agentModel.deleteAgentFile(agent.id, '1');
|
|
196
|
+
|
|
197
|
+
const result = await serverDB.query.agentsFiles.findFirst({
|
|
198
|
+
where: eq(agentsFiles.agentId, agent.id),
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
expect(result).toBeUndefined();
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
describe('toggleFile', () => {
|
|
206
|
+
it('should toggle the enabled status of an agent file association', async () => {
|
|
207
|
+
const agent = await serverDB
|
|
208
|
+
.insert(agents)
|
|
209
|
+
.values({ userId })
|
|
210
|
+
.returning()
|
|
211
|
+
.then((res) => res[0]);
|
|
212
|
+
|
|
213
|
+
await serverDB
|
|
214
|
+
.insert(agentsFiles)
|
|
215
|
+
.values({ agentId: agent.id, fileId: '1', userId, enabled: true });
|
|
216
|
+
|
|
217
|
+
await agentModel.toggleFile(agent.id, '1', false);
|
|
218
|
+
|
|
219
|
+
const result = await serverDB.query.agentsFiles.findFirst({
|
|
220
|
+
where: eq(agentsFiles.agentId, agent.id),
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
expect(result?.enabled).toBe(false);
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
});
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
// @vitest-environment node
|
|
2
|
+
import { eq } from 'drizzle-orm';
|
|
3
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
4
|
+
|
|
5
|
+
import { getTestDBInstance } from '@/database/server/core/dbForTest';
|
|
6
|
+
import { AsyncTaskStatus, AsyncTaskType } from '@/types/asyncTask';
|
|
7
|
+
|
|
8
|
+
import { asyncTasks, users } from '../../schemas/lobechat';
|
|
9
|
+
import { ASYNC_TASK_TIMEOUT, AsyncTaskModel } from '../asyncTask';
|
|
10
|
+
|
|
11
|
+
let serverDB = await getTestDBInstance();
|
|
12
|
+
|
|
13
|
+
vi.mock('@/database/server/core/db', async () => ({
|
|
14
|
+
get serverDB() {
|
|
15
|
+
return serverDB;
|
|
16
|
+
},
|
|
17
|
+
}));
|
|
18
|
+
|
|
19
|
+
const userId = 'async-task-model-test-user-id';
|
|
20
|
+
const asyncTaskModel = new AsyncTaskModel(userId);
|
|
21
|
+
|
|
22
|
+
beforeEach(async () => {
|
|
23
|
+
await serverDB.delete(users);
|
|
24
|
+
await serverDB.insert(users).values([{ id: userId }]);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
afterEach(async () => {
|
|
28
|
+
await serverDB.delete(users).where(eq(users.id, userId));
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('AsyncTaskModel', () => {
|
|
32
|
+
describe('create', () => {
|
|
33
|
+
it('should create a new async task', async () => {
|
|
34
|
+
const params = {
|
|
35
|
+
type: AsyncTaskType.Chunking,
|
|
36
|
+
status: AsyncTaskStatus.Processing,
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const taskId = await asyncTaskModel.create(params);
|
|
40
|
+
|
|
41
|
+
const task = await serverDB.query.asyncTasks.findFirst({
|
|
42
|
+
where: eq(asyncTasks.id, taskId),
|
|
43
|
+
});
|
|
44
|
+
expect(task).toMatchObject({ ...params, userId });
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe('delete', () => {
|
|
49
|
+
it('should delete an async task by id', async () => {
|
|
50
|
+
const { id } = await serverDB
|
|
51
|
+
.insert(asyncTasks)
|
|
52
|
+
.values({
|
|
53
|
+
type: AsyncTaskType.Chunking,
|
|
54
|
+
status: AsyncTaskStatus.Processing,
|
|
55
|
+
userId,
|
|
56
|
+
})
|
|
57
|
+
.returning()
|
|
58
|
+
.then((res) => res[0]);
|
|
59
|
+
|
|
60
|
+
await asyncTaskModel.delete(id);
|
|
61
|
+
|
|
62
|
+
const task = await serverDB.query.asyncTasks.findFirst({
|
|
63
|
+
where: eq(asyncTasks.id, id),
|
|
64
|
+
});
|
|
65
|
+
expect(task).toBeUndefined();
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe('findById', () => {
|
|
70
|
+
it('should find an async task by id', async () => {
|
|
71
|
+
const { id } = await serverDB
|
|
72
|
+
.insert(asyncTasks)
|
|
73
|
+
.values({
|
|
74
|
+
type: AsyncTaskType.Chunking,
|
|
75
|
+
status: AsyncTaskStatus.Processing,
|
|
76
|
+
userId,
|
|
77
|
+
})
|
|
78
|
+
.returning()
|
|
79
|
+
.then((res) => res[0]);
|
|
80
|
+
|
|
81
|
+
const task = await asyncTaskModel.findById(id);
|
|
82
|
+
expect(task).toBeDefined();
|
|
83
|
+
expect(task?.id).toBe(id);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
describe('update', () => {
|
|
88
|
+
it('should update an async task', async () => {
|
|
89
|
+
const { id } = await serverDB
|
|
90
|
+
.insert(asyncTasks)
|
|
91
|
+
.values({
|
|
92
|
+
type: AsyncTaskType.Chunking,
|
|
93
|
+
status: AsyncTaskStatus.Processing,
|
|
94
|
+
userId,
|
|
95
|
+
})
|
|
96
|
+
.returning()
|
|
97
|
+
.then((res) => res[0]);
|
|
98
|
+
|
|
99
|
+
await asyncTaskModel.update(id, { status: AsyncTaskStatus.Success });
|
|
100
|
+
|
|
101
|
+
const updatedTask = await serverDB.query.asyncTasks.findFirst({
|
|
102
|
+
where: eq(asyncTasks.id, id),
|
|
103
|
+
});
|
|
104
|
+
expect(updatedTask?.status).toBe(AsyncTaskStatus.Success);
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
describe('findByIds', () => {
|
|
109
|
+
it('should find async tasks by ids and type', async () => {
|
|
110
|
+
const tasks = await serverDB
|
|
111
|
+
.insert(asyncTasks)
|
|
112
|
+
.values([
|
|
113
|
+
{ type: AsyncTaskType.Chunking, status: AsyncTaskStatus.Processing, userId },
|
|
114
|
+
{ type: AsyncTaskType.Chunking, status: AsyncTaskStatus.Success, userId },
|
|
115
|
+
{ type: AsyncTaskType.Embedding, status: AsyncTaskStatus.Processing, userId },
|
|
116
|
+
])
|
|
117
|
+
.returning();
|
|
118
|
+
|
|
119
|
+
const chunkTasks = await asyncTaskModel.findByIds(
|
|
120
|
+
tasks.map((t) => t.id),
|
|
121
|
+
AsyncTaskType.Chunking,
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
expect(chunkTasks).toHaveLength(2);
|
|
125
|
+
expect(chunkTasks.every((t) => t.type === AsyncTaskType.Chunking)).toBe(true);
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
describe('checkTimeoutTasks', () => {
|
|
130
|
+
it('should mark tasks as error if they timeout', async () => {
|
|
131
|
+
vi.useFakeTimers();
|
|
132
|
+
|
|
133
|
+
const { id } = await serverDB
|
|
134
|
+
.insert(asyncTasks)
|
|
135
|
+
.values({
|
|
136
|
+
type: AsyncTaskType.Chunking,
|
|
137
|
+
status: AsyncTaskStatus.Processing,
|
|
138
|
+
userId,
|
|
139
|
+
createdAt: new Date(Date.now() - ASYNC_TASK_TIMEOUT - 1000), // Make sure it's older than the timeout
|
|
140
|
+
})
|
|
141
|
+
.returning()
|
|
142
|
+
.then((res) => res[0]);
|
|
143
|
+
|
|
144
|
+
await asyncTaskModel.checkTimeoutTasks([id]);
|
|
145
|
+
|
|
146
|
+
const updatedTask = await serverDB.query.asyncTasks.findFirst({
|
|
147
|
+
where: eq(asyncTasks.id, id),
|
|
148
|
+
});
|
|
149
|
+
expect(updatedTask?.status).toBe(AsyncTaskStatus.Error);
|
|
150
|
+
expect(updatedTask?.error).toBeDefined();
|
|
151
|
+
|
|
152
|
+
vi.useRealTimers();
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('should not mark tasks as error if they are not timed out', async () => {
|
|
156
|
+
const { id } = await serverDB
|
|
157
|
+
.insert(asyncTasks)
|
|
158
|
+
.values({
|
|
159
|
+
type: AsyncTaskType.Chunking,
|
|
160
|
+
status: AsyncTaskStatus.Processing,
|
|
161
|
+
userId,
|
|
162
|
+
createdAt: new Date(), // Current time, should not timeout
|
|
163
|
+
})
|
|
164
|
+
.returning()
|
|
165
|
+
.then((res) => res[0]);
|
|
166
|
+
|
|
167
|
+
await asyncTaskModel.checkTimeoutTasks([id]);
|
|
168
|
+
|
|
169
|
+
const updatedTask = await serverDB.query.asyncTasks.findFirst({
|
|
170
|
+
where: eq(asyncTasks.id, id),
|
|
171
|
+
});
|
|
172
|
+
expect(updatedTask?.status).toBe(AsyncTaskStatus.Processing);
|
|
173
|
+
expect(updatedTask?.error).toBeNull();
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
});
|