@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,159 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
import { INBOX_SESSION_ID } from '@/const/session';
|
|
4
|
+
import { AgentModel } from '@/database/server/models/agent';
|
|
5
|
+
import { FileModel } from '@/database/server/models/file';
|
|
6
|
+
import { KnowledgeBaseModel } from '@/database/server/models/knowledgeBase';
|
|
7
|
+
import { SessionModel } from '@/database/server/models/session';
|
|
8
|
+
import { pino } from '@/libs/logger';
|
|
9
|
+
import { authedProcedure, router } from '@/libs/trpc';
|
|
10
|
+
import { KnowledgeItem, KnowledgeType } from '@/types/knowledgeBase';
|
|
11
|
+
|
|
12
|
+
const agentProcedure = authedProcedure.use(async (opts) => {
|
|
13
|
+
const { ctx } = opts;
|
|
14
|
+
|
|
15
|
+
return opts.next({
|
|
16
|
+
ctx: {
|
|
17
|
+
agentModel: new AgentModel(ctx.userId),
|
|
18
|
+
fileModel: new FileModel(ctx.userId),
|
|
19
|
+
knowledgeBaseModel: new KnowledgeBaseModel(ctx.userId),
|
|
20
|
+
sessionModel: new SessionModel(ctx.userId),
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
export const agentRouter = router({
|
|
26
|
+
createAgentFiles: agentProcedure
|
|
27
|
+
.input(
|
|
28
|
+
z.object({
|
|
29
|
+
agentId: z.string(),
|
|
30
|
+
enabled: z.boolean().optional(),
|
|
31
|
+
fileIds: z.array(z.string()),
|
|
32
|
+
}),
|
|
33
|
+
)
|
|
34
|
+
.mutation(async ({ input, ctx }) => {
|
|
35
|
+
return ctx.agentModel.createAgentFiles(input.agentId, input.fileIds, input.enabled);
|
|
36
|
+
}),
|
|
37
|
+
|
|
38
|
+
createAgentKnowledgeBase: agentProcedure
|
|
39
|
+
.input(
|
|
40
|
+
z.object({
|
|
41
|
+
agentId: z.string(),
|
|
42
|
+
enabled: z.boolean().optional(),
|
|
43
|
+
knowledgeBaseId: z.string(),
|
|
44
|
+
}),
|
|
45
|
+
)
|
|
46
|
+
.mutation(async ({ input, ctx }) => {
|
|
47
|
+
return ctx.agentModel.createAgentKnowledgeBase(
|
|
48
|
+
input.agentId,
|
|
49
|
+
input.knowledgeBaseId,
|
|
50
|
+
input.enabled,
|
|
51
|
+
);
|
|
52
|
+
}),
|
|
53
|
+
|
|
54
|
+
deleteAgentFile: agentProcedure
|
|
55
|
+
.input(
|
|
56
|
+
z.object({
|
|
57
|
+
agentId: z.string(),
|
|
58
|
+
fileId: z.string(),
|
|
59
|
+
}),
|
|
60
|
+
)
|
|
61
|
+
.mutation(async ({ input, ctx }) => {
|
|
62
|
+
return ctx.agentModel.deleteAgentFile(input.agentId, input.fileId);
|
|
63
|
+
}),
|
|
64
|
+
|
|
65
|
+
deleteAgentKnowledgeBase: agentProcedure
|
|
66
|
+
.input(
|
|
67
|
+
z.object({
|
|
68
|
+
agentId: z.string(),
|
|
69
|
+
knowledgeBaseId: z.string(),
|
|
70
|
+
}),
|
|
71
|
+
)
|
|
72
|
+
.mutation(async ({ input, ctx }) => {
|
|
73
|
+
return ctx.agentModel.deleteAgentKnowledgeBase(input.agentId, input.knowledgeBaseId);
|
|
74
|
+
}),
|
|
75
|
+
|
|
76
|
+
getAgentConfig: agentProcedure
|
|
77
|
+
.input(
|
|
78
|
+
z.object({
|
|
79
|
+
sessionId: z.string(),
|
|
80
|
+
}),
|
|
81
|
+
)
|
|
82
|
+
.query(async ({ input, ctx }) => {
|
|
83
|
+
if (input.sessionId === INBOX_SESSION_ID) {
|
|
84
|
+
const item = await ctx.sessionModel.findByIdOrSlug(INBOX_SESSION_ID);
|
|
85
|
+
// if there is no session for user, create one
|
|
86
|
+
if (!item) {
|
|
87
|
+
const res = await ctx.sessionModel.createInbox();
|
|
88
|
+
pino.info('create inbox session', res);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const session = await ctx.sessionModel.findByIdOrSlug(input.sessionId);
|
|
93
|
+
|
|
94
|
+
if (!session) throw new Error('Session not found');
|
|
95
|
+
const sessionId = session.id;
|
|
96
|
+
|
|
97
|
+
return await ctx.agentModel.findBySessionId(sessionId);
|
|
98
|
+
}),
|
|
99
|
+
|
|
100
|
+
getKnowledgeBasesAndFiles: agentProcedure
|
|
101
|
+
.input(
|
|
102
|
+
z.object({
|
|
103
|
+
agentId: z.string(),
|
|
104
|
+
}),
|
|
105
|
+
)
|
|
106
|
+
.query(async ({ ctx, input }): Promise<KnowledgeItem[]> => {
|
|
107
|
+
const knowledgeBases = await ctx.knowledgeBaseModel.query();
|
|
108
|
+
|
|
109
|
+
const files = await ctx.fileModel.query({ showFilesInKnowledgeBase: false });
|
|
110
|
+
|
|
111
|
+
const knowledge = await ctx.agentModel.getAgentAssignedKnowledge(input.agentId);
|
|
112
|
+
|
|
113
|
+
return [
|
|
114
|
+
...files.map((file) => ({
|
|
115
|
+
enabled: knowledge.files.some((item) => item.id === file.id),
|
|
116
|
+
fileType: file.fileType,
|
|
117
|
+
id: file.id,
|
|
118
|
+
name: file.name,
|
|
119
|
+
type: KnowledgeType.File,
|
|
120
|
+
})),
|
|
121
|
+
...knowledgeBases.map((knowledgeBase) => ({
|
|
122
|
+
avatar: knowledgeBase.avatar,
|
|
123
|
+
description: knowledgeBase.description,
|
|
124
|
+
enabled: knowledge.knowledgeBases.some((item) => item.id === knowledgeBase.id),
|
|
125
|
+
id: knowledgeBase.id,
|
|
126
|
+
name: knowledgeBase.name,
|
|
127
|
+
type: KnowledgeType.KnowledgeBase,
|
|
128
|
+
})),
|
|
129
|
+
];
|
|
130
|
+
}),
|
|
131
|
+
|
|
132
|
+
toggleFile: agentProcedure
|
|
133
|
+
.input(
|
|
134
|
+
z.object({
|
|
135
|
+
agentId: z.string(),
|
|
136
|
+
enabled: z.boolean().optional(),
|
|
137
|
+
fileId: z.string(),
|
|
138
|
+
}),
|
|
139
|
+
)
|
|
140
|
+
.mutation(async ({ input, ctx }) => {
|
|
141
|
+
return ctx.agentModel.toggleFile(input.agentId, input.fileId, input.enabled);
|
|
142
|
+
}),
|
|
143
|
+
|
|
144
|
+
toggleKnowledgeBase: agentProcedure
|
|
145
|
+
.input(
|
|
146
|
+
z.object({
|
|
147
|
+
agentId: z.string(),
|
|
148
|
+
enabled: z.boolean().optional(),
|
|
149
|
+
knowledgeBaseId: z.string(),
|
|
150
|
+
}),
|
|
151
|
+
)
|
|
152
|
+
.mutation(async ({ input, ctx }) => {
|
|
153
|
+
return ctx.agentModel.toggleKnowledgeBase(
|
|
154
|
+
input.agentId,
|
|
155
|
+
input.knowledgeBaseId,
|
|
156
|
+
input.enabled,
|
|
157
|
+
);
|
|
158
|
+
}),
|
|
159
|
+
});
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { inArray } from 'drizzle-orm';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
import { initAgentRuntimeWithUserPayload } from '@/app/api/chat/agentRuntime';
|
|
5
|
+
import { DEFAULT_EMBEDDING_MODEL } from '@/const/settings';
|
|
6
|
+
import { serverDB } from '@/database/server';
|
|
7
|
+
import { AsyncTaskModel } from '@/database/server/models/asyncTask';
|
|
8
|
+
import { ChunkModel } from '@/database/server/models/chunk';
|
|
9
|
+
import { EmbeddingModel } from '@/database/server/models/embedding';
|
|
10
|
+
import { FileModel } from '@/database/server/models/file';
|
|
11
|
+
import { MessageModel } from '@/database/server/models/message';
|
|
12
|
+
import { knowledgeBaseFiles } from '@/database/server/schemas/lobechat';
|
|
13
|
+
import { ModelProvider } from '@/libs/agent-runtime';
|
|
14
|
+
import { authedProcedure, router } from '@/libs/trpc';
|
|
15
|
+
import { keyVaults } from '@/libs/trpc/middleware/keyVaults';
|
|
16
|
+
import { ChunkService } from '@/server/services/chunk';
|
|
17
|
+
import { SemanticSearchSchema } from '@/types/rag';
|
|
18
|
+
|
|
19
|
+
const chunkProcedure = authedProcedure.use(keyVaults).use(async (opts) => {
|
|
20
|
+
const { ctx } = opts;
|
|
21
|
+
|
|
22
|
+
return opts.next({
|
|
23
|
+
ctx: {
|
|
24
|
+
asyncTaskModel: new AsyncTaskModel(ctx.userId),
|
|
25
|
+
chunkModel: new ChunkModel(ctx.userId),
|
|
26
|
+
chunkService: new ChunkService(ctx.userId),
|
|
27
|
+
embeddingModel: new EmbeddingModel(ctx.userId),
|
|
28
|
+
fileModel: new FileModel(ctx.userId),
|
|
29
|
+
messageModel: new MessageModel(ctx.userId),
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
export const chunkRouter = router({
|
|
35
|
+
createEmbeddingChunksTask: chunkProcedure
|
|
36
|
+
.input(
|
|
37
|
+
z.object({
|
|
38
|
+
id: z.string(),
|
|
39
|
+
}),
|
|
40
|
+
)
|
|
41
|
+
.mutation(async ({ ctx, input }) => {
|
|
42
|
+
const asyncTaskId = await ctx.chunkService.asyncEmbeddingFileChunks(input.id, ctx.jwtPayload);
|
|
43
|
+
|
|
44
|
+
return { id: asyncTaskId, success: true };
|
|
45
|
+
}),
|
|
46
|
+
|
|
47
|
+
createParseFileTask: chunkProcedure
|
|
48
|
+
.input(
|
|
49
|
+
z.object({
|
|
50
|
+
id: z.string(),
|
|
51
|
+
skipExist: z.boolean().optional(),
|
|
52
|
+
}),
|
|
53
|
+
)
|
|
54
|
+
.mutation(async ({ ctx, input }) => {
|
|
55
|
+
const asyncTaskId = await ctx.chunkService.asyncParseFileToChunks(
|
|
56
|
+
input.id,
|
|
57
|
+
ctx.jwtPayload,
|
|
58
|
+
input.skipExist,
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
return { id: asyncTaskId, success: true };
|
|
62
|
+
}),
|
|
63
|
+
|
|
64
|
+
getChunksByFileId: chunkProcedure
|
|
65
|
+
.input(
|
|
66
|
+
z.object({
|
|
67
|
+
cursor: z.number().nullish(),
|
|
68
|
+
id: z.string(),
|
|
69
|
+
}),
|
|
70
|
+
)
|
|
71
|
+
.query(async ({ ctx, input }) => {
|
|
72
|
+
return {
|
|
73
|
+
items: await ctx.chunkModel.findByFileId(input.id, input.cursor || 0),
|
|
74
|
+
nextCursor: input.cursor ? input.cursor + 1 : 1,
|
|
75
|
+
};
|
|
76
|
+
}),
|
|
77
|
+
|
|
78
|
+
retryParseFileTask: chunkProcedure
|
|
79
|
+
.input(
|
|
80
|
+
z.object({
|
|
81
|
+
id: z.string(),
|
|
82
|
+
}),
|
|
83
|
+
)
|
|
84
|
+
.mutation(async ({ ctx, input }) => {
|
|
85
|
+
const result = await ctx.fileModel.findById(input.id);
|
|
86
|
+
|
|
87
|
+
if (!result) return;
|
|
88
|
+
|
|
89
|
+
// 1. delete the previous task if exist
|
|
90
|
+
if (result.chunkTaskId) {
|
|
91
|
+
await ctx.asyncTaskModel.delete(result.chunkTaskId);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// 2. create a new asyncTask for chunking
|
|
95
|
+
const asyncTaskId = await ctx.chunkService.asyncParseFileToChunks(input.id, ctx.jwtPayload);
|
|
96
|
+
|
|
97
|
+
return { id: asyncTaskId, success: true };
|
|
98
|
+
}),
|
|
99
|
+
|
|
100
|
+
semanticSearch: chunkProcedure
|
|
101
|
+
.input(
|
|
102
|
+
z.object({
|
|
103
|
+
fileIds: z.array(z.string()).optional(),
|
|
104
|
+
model: z.string().default(DEFAULT_EMBEDDING_MODEL),
|
|
105
|
+
query: z.string(),
|
|
106
|
+
}),
|
|
107
|
+
)
|
|
108
|
+
.mutation(async ({ ctx, input }) => {
|
|
109
|
+
console.time('embedding');
|
|
110
|
+
const agentRuntime = await initAgentRuntimeWithUserPayload(
|
|
111
|
+
ModelProvider.OpenAI,
|
|
112
|
+
ctx.jwtPayload,
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
const embeddings = await agentRuntime.embeddings({
|
|
116
|
+
dimensions: 1024,
|
|
117
|
+
input: input.query,
|
|
118
|
+
model: input.model,
|
|
119
|
+
});
|
|
120
|
+
console.timeEnd('embedding');
|
|
121
|
+
|
|
122
|
+
return ctx.chunkModel.semanticSearch({
|
|
123
|
+
embedding: embeddings![0].embedding,
|
|
124
|
+
fileIds: input.fileIds,
|
|
125
|
+
query: input.query,
|
|
126
|
+
});
|
|
127
|
+
}),
|
|
128
|
+
|
|
129
|
+
semanticSearchForChat: chunkProcedure
|
|
130
|
+
.input(SemanticSearchSchema)
|
|
131
|
+
.mutation(async ({ ctx, input }) => {
|
|
132
|
+
const item = await ctx.messageModel.findMessageQueriesById(input.messageId);
|
|
133
|
+
let embedding: number[];
|
|
134
|
+
let ragQueryId: string;
|
|
135
|
+
|
|
136
|
+
// if there is no message rag or it's embeddings, then we need to create one
|
|
137
|
+
if (!item || !item.embeddings) {
|
|
138
|
+
// TODO: need to support customize
|
|
139
|
+
const agentRuntime = await initAgentRuntimeWithUserPayload(
|
|
140
|
+
ModelProvider.OpenAI,
|
|
141
|
+
ctx.jwtPayload,
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
const embeddings = await agentRuntime.embeddings({
|
|
145
|
+
dimensions: 1024,
|
|
146
|
+
input: input.rewriteQuery,
|
|
147
|
+
model: input.model || DEFAULT_EMBEDDING_MODEL,
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
embedding = embeddings![0].embedding;
|
|
151
|
+
const embeddingsId = await ctx.embeddingModel.create({
|
|
152
|
+
embeddings: embedding,
|
|
153
|
+
model: input.model,
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
const result = await ctx.messageModel.createMessageQuery({
|
|
157
|
+
embeddingsId,
|
|
158
|
+
messageId: input.messageId,
|
|
159
|
+
rewriteQuery: input.rewriteQuery,
|
|
160
|
+
userQuery: input.userQuery,
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
ragQueryId = result.id;
|
|
164
|
+
} else {
|
|
165
|
+
embedding = item.embeddings;
|
|
166
|
+
ragQueryId = item.id;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
console.time('semanticSearch');
|
|
170
|
+
let finalFileIds = input.fileIds ?? [];
|
|
171
|
+
|
|
172
|
+
if (input.knowledgeIds && input.knowledgeIds.length > 0) {
|
|
173
|
+
const knowledgeFiles = await serverDB.query.knowledgeBaseFiles.findMany({
|
|
174
|
+
where: inArray(knowledgeBaseFiles.knowledgeBaseId, input.knowledgeIds),
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
finalFileIds = knowledgeFiles.map((f) => f.fileId).concat(finalFileIds);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const chunks = await ctx.chunkModel.semanticSearchForChat({
|
|
181
|
+
embedding,
|
|
182
|
+
fileIds: finalFileIds,
|
|
183
|
+
query: input.rewriteQuery,
|
|
184
|
+
});
|
|
185
|
+
console.timeEnd('semanticSearch');
|
|
186
|
+
|
|
187
|
+
return { chunks, queryId: ragQueryId };
|
|
188
|
+
}),
|
|
189
|
+
});
|
|
@@ -1,32 +1,66 @@
|
|
|
1
|
+
import { TRPCError } from '@trpc/server';
|
|
2
|
+
import urlJoin from 'url-join';
|
|
1
3
|
import { z } from 'zod';
|
|
2
4
|
|
|
5
|
+
import { fileEnv } from '@/config/file';
|
|
6
|
+
import { AsyncTaskModel } from '@/database/server/models/asyncTask';
|
|
7
|
+
import { ChunkModel } from '@/database/server/models/chunk';
|
|
3
8
|
import { FileModel } from '@/database/server/models/file';
|
|
4
9
|
import { authedProcedure, router } from '@/libs/trpc';
|
|
5
|
-
import {
|
|
10
|
+
import { S3 } from '@/server/modules/S3';
|
|
11
|
+
import { getFullFileUrl } from '@/server/utils/files';
|
|
12
|
+
import { AsyncTaskStatus, AsyncTaskType } from '@/types/asyncTask';
|
|
13
|
+
import { FileListItem, QueryFileListSchema, UploadFileSchema } from '@/types/files';
|
|
6
14
|
|
|
7
15
|
const fileProcedure = authedProcedure.use(async (opts) => {
|
|
8
16
|
const { ctx } = opts;
|
|
9
17
|
|
|
10
18
|
return opts.next({
|
|
11
|
-
ctx: {
|
|
19
|
+
ctx: {
|
|
20
|
+
asyncTaskModel: new AsyncTaskModel(ctx.userId),
|
|
21
|
+
chunkModel: new ChunkModel(ctx.userId),
|
|
22
|
+
fileModel: new FileModel(ctx.userId),
|
|
23
|
+
},
|
|
12
24
|
});
|
|
13
25
|
});
|
|
14
26
|
|
|
15
27
|
export const fileRouter = router({
|
|
28
|
+
checkFileHash: fileProcedure
|
|
29
|
+
.input(z.object({ hash: z.string() }))
|
|
30
|
+
.mutation(async ({ ctx, input }) => {
|
|
31
|
+
return ctx.fileModel.checkHash(input.hash);
|
|
32
|
+
}),
|
|
33
|
+
|
|
16
34
|
createFile: fileProcedure
|
|
17
35
|
.input(
|
|
18
36
|
UploadFileSchema.omit({ data: true, saveMode: true, url: true }).extend({ url: z.string() }),
|
|
19
37
|
)
|
|
20
38
|
.mutation(async ({ ctx, input }) => {
|
|
21
|
-
|
|
39
|
+
const { isExist } = await ctx.fileModel.checkHash(input.hash!);
|
|
40
|
+
|
|
41
|
+
// if the file is not exist in global file, create a new one
|
|
42
|
+
if (!isExist) {
|
|
43
|
+
await ctx.fileModel.createGlobalFile({
|
|
44
|
+
fileType: input.fileType,
|
|
45
|
+
hashId: input.hash!,
|
|
46
|
+
metadata: input.metadata,
|
|
47
|
+
size: input.size,
|
|
48
|
+
url: input.url,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const { id } = await ctx.fileModel.create({
|
|
53
|
+
fileHash: input.hash,
|
|
22
54
|
fileType: input.fileType,
|
|
55
|
+
knowledgeBaseId: input.knowledgeBaseId,
|
|
23
56
|
metadata: input.metadata,
|
|
24
57
|
name: input.name,
|
|
25
58
|
size: input.size,
|
|
26
59
|
url: input.url,
|
|
27
60
|
});
|
|
28
|
-
}),
|
|
29
61
|
|
|
62
|
+
return { id, url: getFullFileUrl(input.url) };
|
|
63
|
+
}),
|
|
30
64
|
findById: fileProcedure
|
|
31
65
|
.input(
|
|
32
66
|
z.object({
|
|
@@ -37,13 +71,103 @@ export const fileRouter = router({
|
|
|
37
71
|
return ctx.fileModel.findById(input.id);
|
|
38
72
|
}),
|
|
39
73
|
|
|
74
|
+
getFileItemById: fileProcedure
|
|
75
|
+
.input(
|
|
76
|
+
z.object({
|
|
77
|
+
id: z.string(),
|
|
78
|
+
}),
|
|
79
|
+
)
|
|
80
|
+
.query(async ({ ctx, input }): Promise<FileListItem | undefined> => {
|
|
81
|
+
const item = await ctx.fileModel.findById(input.id);
|
|
82
|
+
|
|
83
|
+
if (!item) throw new TRPCError({ code: 'BAD_REQUEST', message: 'File not found' });
|
|
84
|
+
|
|
85
|
+
let embeddingTask = null;
|
|
86
|
+
if (item.embeddingTaskId) {
|
|
87
|
+
embeddingTask = await ctx.asyncTaskModel.findById(item.embeddingTaskId);
|
|
88
|
+
}
|
|
89
|
+
let chunkingTask = null;
|
|
90
|
+
if (item.chunkTaskId) {
|
|
91
|
+
chunkingTask = await ctx.asyncTaskModel.findById(item.chunkTaskId);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const chunkCount = await ctx.chunkModel.countByFileId(input.id);
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
...item,
|
|
98
|
+
chunkCount,
|
|
99
|
+
chunkingError: chunkingTask?.error,
|
|
100
|
+
chunkingStatus: chunkingTask?.status as AsyncTaskStatus,
|
|
101
|
+
embeddingError: embeddingTask?.error,
|
|
102
|
+
embeddingStatus: embeddingTask?.status as AsyncTaskStatus,
|
|
103
|
+
finishEmbedding: embeddingTask?.status === AsyncTaskStatus.Success,
|
|
104
|
+
url: getFullFileUrl(item.url!),
|
|
105
|
+
};
|
|
106
|
+
}),
|
|
107
|
+
|
|
108
|
+
getFiles: fileProcedure.input(QueryFileListSchema).query(async ({ ctx, input }) => {
|
|
109
|
+
const fileList = await ctx.fileModel.query(input);
|
|
110
|
+
|
|
111
|
+
const fileIds = fileList.map((item) => item.id);
|
|
112
|
+
const chunks = await ctx.chunkModel.countByFileIds(fileIds);
|
|
113
|
+
|
|
114
|
+
const chunkTaskIds = fileList.map((result) => result.chunkTaskId).filter(Boolean) as string[];
|
|
115
|
+
|
|
116
|
+
const chunkTasks = await ctx.asyncTaskModel.findByIds(chunkTaskIds, AsyncTaskType.Chunking);
|
|
117
|
+
|
|
118
|
+
const embeddingTaskIds = fileList
|
|
119
|
+
.map((result) => result.embeddingTaskId)
|
|
120
|
+
.filter(Boolean) as string[];
|
|
121
|
+
const embeddingTasks = await ctx.asyncTaskModel.findByIds(
|
|
122
|
+
embeddingTaskIds,
|
|
123
|
+
AsyncTaskType.Embedding,
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
return fileList.map(({ chunkTaskId, embeddingTaskId, ...item }): FileListItem => {
|
|
127
|
+
const chunkTask = chunkTaskId ? chunkTasks.find((task) => task.id === chunkTaskId) : null;
|
|
128
|
+
const embeddingTask = embeddingTaskId
|
|
129
|
+
? embeddingTasks.find((task) => task.id === embeddingTaskId)
|
|
130
|
+
: null;
|
|
131
|
+
|
|
132
|
+
return {
|
|
133
|
+
...item,
|
|
134
|
+
chunkCount: chunks.find((chunk) => chunk.id === item.id)?.count ?? null,
|
|
135
|
+
chunkingError: chunkTask?.error ?? null,
|
|
136
|
+
chunkingStatus: chunkTask?.status as AsyncTaskStatus,
|
|
137
|
+
embeddingError: embeddingTask?.error ?? null,
|
|
138
|
+
embeddingStatus: embeddingTask?.status as AsyncTaskStatus,
|
|
139
|
+
finishEmbedding: embeddingTask?.status === AsyncTaskStatus.Success,
|
|
140
|
+
url: urlJoin(fileEnv.NEXT_PUBLIC_S3_DOMAIN!, item.url!),
|
|
141
|
+
};
|
|
142
|
+
});
|
|
143
|
+
}),
|
|
144
|
+
|
|
40
145
|
removeAllFiles: fileProcedure.mutation(async ({ ctx }) => {
|
|
41
146
|
return ctx.fileModel.clear();
|
|
42
147
|
}),
|
|
43
148
|
|
|
44
149
|
removeFile: fileProcedure.input(z.object({ id: z.string() })).mutation(async ({ input, ctx }) => {
|
|
45
|
-
|
|
150
|
+
const file = await ctx.fileModel.delete(input.id);
|
|
151
|
+
|
|
152
|
+
if (!file) return;
|
|
153
|
+
|
|
154
|
+
// delele the file from remove from S3 if it is not used by other files
|
|
155
|
+
const s3Client = new S3();
|
|
156
|
+
await s3Client.deleteFile(file.url!);
|
|
46
157
|
}),
|
|
158
|
+
|
|
159
|
+
removeFiles: fileProcedure
|
|
160
|
+
.input(z.object({ ids: z.array(z.string()) }))
|
|
161
|
+
.mutation(async ({ input, ctx }) => {
|
|
162
|
+
const needToRemoveFileList = await ctx.fileModel.deleteMany(input.ids);
|
|
163
|
+
|
|
164
|
+
if (!needToRemoveFileList || needToRemoveFileList.length === 0) return;
|
|
165
|
+
|
|
166
|
+
// remove from S3
|
|
167
|
+
const s3Client = new S3();
|
|
168
|
+
|
|
169
|
+
await s3Client.deleteFiles(needToRemoveFileList.map((file) => file.url!));
|
|
170
|
+
}),
|
|
47
171
|
});
|
|
48
172
|
|
|
49
173
|
export type FileRouter = typeof fileRouter;
|
|
@@ -3,9 +3,12 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import { publicProcedure, router } from '@/libs/trpc';
|
|
5
5
|
|
|
6
|
+
import { agentRouter } from './agent';
|
|
7
|
+
import { chunkRouter } from './chunk';
|
|
6
8
|
// router that connect to db
|
|
7
9
|
import { fileRouter } from './file';
|
|
8
10
|
import { importerRouter } from './importer';
|
|
11
|
+
import { knowledgeBaseRouter } from './knowledgeBase';
|
|
9
12
|
import { messageRouter } from './message';
|
|
10
13
|
import { pluginRouter } from './plugin';
|
|
11
14
|
import { sessionRouter } from './session';
|
|
@@ -14,9 +17,12 @@ import { topicRouter } from './topic';
|
|
|
14
17
|
import { userRouter } from './user';
|
|
15
18
|
|
|
16
19
|
export const lambdaRouter = router({
|
|
20
|
+
agent: agentRouter,
|
|
21
|
+
chunk: chunkRouter,
|
|
17
22
|
file: fileRouter,
|
|
18
23
|
healthcheck: publicProcedure.query(() => "i'm live!"),
|
|
19
24
|
importer: importerRouter,
|
|
25
|
+
knowledgeBase: knowledgeBaseRouter,
|
|
20
26
|
message: messageRouter,
|
|
21
27
|
plugin: pluginRouter,
|
|
22
28
|
session: sessionRouter,
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
import { KnowledgeBaseModel } from '@/database/server/models/knowledgeBase';
|
|
4
|
+
import { insertKnowledgeBasesSchema } from '@/database/server/schemas/lobechat';
|
|
5
|
+
import { authedProcedure, router } from '@/libs/trpc';
|
|
6
|
+
import { KnowledgeBaseItem } from '@/types/knowledgeBase';
|
|
7
|
+
|
|
8
|
+
const knowledgeBaseProcedure = authedProcedure.use(async (opts) => {
|
|
9
|
+
const { ctx } = opts;
|
|
10
|
+
|
|
11
|
+
return opts.next({
|
|
12
|
+
ctx: {
|
|
13
|
+
knowledgeBaseModel: new KnowledgeBaseModel(ctx.userId),
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
export const knowledgeBaseRouter = router({
|
|
19
|
+
addFilesToKnowledgeBase: knowledgeBaseProcedure
|
|
20
|
+
.input(z.object({ ids: z.array(z.string()), knowledgeBaseId: z.string() }))
|
|
21
|
+
.mutation(async ({ input, ctx }) => {
|
|
22
|
+
return ctx.knowledgeBaseModel.addFilesToKnowledgeBase(input.knowledgeBaseId, input.ids);
|
|
23
|
+
}),
|
|
24
|
+
|
|
25
|
+
createKnowledgeBase: knowledgeBaseProcedure
|
|
26
|
+
.input(
|
|
27
|
+
z.object({
|
|
28
|
+
avatar: z.string().optional(),
|
|
29
|
+
description: z.string().optional(),
|
|
30
|
+
name: z.string(),
|
|
31
|
+
}),
|
|
32
|
+
)
|
|
33
|
+
.mutation(async ({ input, ctx }) => {
|
|
34
|
+
const data = await ctx.knowledgeBaseModel.create({
|
|
35
|
+
avatar: input.avatar,
|
|
36
|
+
description: input.description,
|
|
37
|
+
name: input.name,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
return data?.id;
|
|
41
|
+
}),
|
|
42
|
+
|
|
43
|
+
getKnowledgeBaseById: knowledgeBaseProcedure
|
|
44
|
+
.input(z.object({ id: z.string() }))
|
|
45
|
+
.query(async ({ ctx, input }): Promise<KnowledgeBaseItem | undefined> => {
|
|
46
|
+
return ctx.knowledgeBaseModel.findById(input.id);
|
|
47
|
+
}),
|
|
48
|
+
|
|
49
|
+
getKnowledgeBases: knowledgeBaseProcedure.query(async ({ ctx }): Promise<KnowledgeBaseItem[]> => {
|
|
50
|
+
return ctx.knowledgeBaseModel.query();
|
|
51
|
+
}),
|
|
52
|
+
|
|
53
|
+
removeAllKnowledgeBases: knowledgeBaseProcedure.mutation(async ({ ctx }) => {
|
|
54
|
+
return ctx.knowledgeBaseModel.deleteAll();
|
|
55
|
+
}),
|
|
56
|
+
|
|
57
|
+
removeFilesFromKnowledgeBase: knowledgeBaseProcedure
|
|
58
|
+
.input(z.object({ ids: z.array(z.string()), knowledgeBaseId: z.string() }))
|
|
59
|
+
.mutation(async ({ input, ctx }) => {
|
|
60
|
+
return ctx.knowledgeBaseModel.removeFilesFromKnowledgeBase(input.knowledgeBaseId, input.ids);
|
|
61
|
+
}),
|
|
62
|
+
|
|
63
|
+
removeKnowledgeBase: knowledgeBaseProcedure
|
|
64
|
+
.input(z.object({ id: z.string(), removeFiles: z.boolean().optional() }))
|
|
65
|
+
.mutation(async ({ input, ctx }) => {
|
|
66
|
+
return ctx.knowledgeBaseModel.delete(input.id);
|
|
67
|
+
}),
|
|
68
|
+
|
|
69
|
+
updateKnowledgeBase: knowledgeBaseProcedure
|
|
70
|
+
.input(
|
|
71
|
+
z.object({
|
|
72
|
+
id: z.string(),
|
|
73
|
+
value: insertKnowledgeBasesSchema.partial(),
|
|
74
|
+
}),
|
|
75
|
+
)
|
|
76
|
+
.mutation(async ({ input, ctx }) => {
|
|
77
|
+
return ctx.knowledgeBaseModel.update(input.id, input.value);
|
|
78
|
+
}),
|
|
79
|
+
});
|
|
@@ -81,6 +81,12 @@ export const messageRouter = router({
|
|
|
81
81
|
return ctx.messageModel.deleteMessage(input.id);
|
|
82
82
|
}),
|
|
83
83
|
|
|
84
|
+
removeMessageQuery: messageProcedure
|
|
85
|
+
.input(z.object({ id: z.string() }))
|
|
86
|
+
.mutation(async ({ input, ctx }) => {
|
|
87
|
+
return ctx.messageModel.deleteMessageQuery(input.id);
|
|
88
|
+
}),
|
|
89
|
+
|
|
84
90
|
removeMessages: messageProcedure
|
|
85
91
|
.input(z.object({ ids: z.array(z.string()) }))
|
|
86
92
|
.mutation(async ({ input, ctx }) => {
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
|
|
3
|
-
import { INBOX_SESSION_ID } from '@/const/session';
|
|
4
3
|
import { SessionModel } from '@/database/server/models/session';
|
|
5
4
|
import { SessionGroupModel } from '@/database/server/models/sessionGroup';
|
|
6
5
|
import { insertAgentSchema, insertSessionSchema } from '@/database/server/schemas/lobechat';
|
|
7
|
-
import { pino } from '@/libs/logger';
|
|
8
6
|
import { authedProcedure, publicProcedure, router } from '@/libs/trpc';
|
|
9
7
|
import { AgentChatConfigSchema } from '@/types/agent';
|
|
10
8
|
import { LobeMetaDataSchema } from '@/types/meta';
|
|
@@ -91,29 +89,6 @@ export const sessionRouter = router({
|
|
|
91
89
|
return sessionModel.queryWithGroups();
|
|
92
90
|
}),
|
|
93
91
|
|
|
94
|
-
getSessionConfig: sessionProcedure
|
|
95
|
-
.input(
|
|
96
|
-
z.object({
|
|
97
|
-
id: z.string(),
|
|
98
|
-
}),
|
|
99
|
-
)
|
|
100
|
-
.query(async ({ input, ctx }) => {
|
|
101
|
-
if (input.id === INBOX_SESSION_ID) {
|
|
102
|
-
const item = await ctx.sessionModel.findByIdOrSlug(INBOX_SESSION_ID);
|
|
103
|
-
// if there is no session for user, create one
|
|
104
|
-
if (!item) {
|
|
105
|
-
const res = await ctx.sessionModel.createInbox();
|
|
106
|
-
pino.info('create inbox session', res);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
const session = await ctx.sessionModel.findByIdOrSlug(input.id);
|
|
111
|
-
|
|
112
|
-
if (!session) throw new Error('Session not found');
|
|
113
|
-
|
|
114
|
-
return session.agent;
|
|
115
|
-
}),
|
|
116
|
-
|
|
117
92
|
getSessions: sessionProcedure
|
|
118
93
|
.input(
|
|
119
94
|
z.object({
|