Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix: add document parsing to knowledge base chunking pipeline (#13221)

Arvin Xu72ba8c8923df
diff --git a/src/features/Conversation/Messages/AssistantGroup/Tool/Inspector/ToolTitle.tsx b/src/features/Conversation/Messages/AssistantGroup/Tool/Inspector/ToolTitle.tsx
index 6063a17116e..8200c91bcfc 100644
--- a/src/features/Conversation/Messages/AssistantGroup/Tool/Inspector/ToolTitle.tsx
+++ b/src/features/Conversation/Messages/AssistantGroup/Tool/Inspector/ToolTitle.tsx
@@ -79,7 +79,7 @@ const ToolTitle = memo<ToolTitleProps>(
const pluginMeta = useToolStore(toolSelectors.getMetaById(identifier), isEqual);
const isBuiltinPlugin = builtinToolIdentifiers.includes(identifier);
- const pluginTitle = pluginHelpers.getPluginTitle(pluginMeta) ?? t('unknownPlugin');
+ const pluginTitle = pluginHelpers.getPluginTitle(pluginMeta) ?? identifier;
const params = useMemo(() => {
const argsToUse = args || partialArgs || {};
diff --git a/src/features/PluginAvatar/index.tsx b/src/features/PluginAvatar/index.tsx
index 14c6e524318..e9d85378707 100644
--- a/src/features/PluginAvatar/index.tsx
+++ b/src/features/PluginAvatar/index.tsx
@@ -18,7 +18,7 @@ const PluginAvatar = memo<PluginAvatarProps>(({ identifier, size = 32 }) => {
const pluginMeta = useToolStore(toolSelectors.getMetaById(identifier), isEqual);
const pluginAvatar = pluginHelpers.getPluginAvatar(pluginMeta);
- const pluginTitle = pluginHelpers.getPluginTitle(pluginMeta) ?? t('unknownPlugin');
+ const pluginTitle = pluginHelpers.getPluginTitle(pluginMeta) ?? identifier;
return pluginAvatar ? (
<Avatar alt={pluginTitle} avatar={pluginAvatar} size={size} />
diff --git a/src/features/Portal/Home/Body/Plugins/ArtifactList/Item/index.tsx b/src/features/Portal/Home/Body/Plugins/ArtifactList/Item/index.tsx
index 1f9d52e6f03..c77f154398d 100644
--- a/src/features/Portal/Home/Body/Plugins/ArtifactList/Item/index.tsx
+++ b/src/features/Portal/Home/Body/Plugins/ArtifactList/Item/index.tsx
@@ -28,7 +28,7 @@ const ArtifactItem = memo<ArtifactItemProps>(({ payload, messageId, identifier =
const pluginMeta = useToolStore(toolSelectors.getMetaById(identifier), isEqual);
const isToolHasUI = useToolStore(toolSelectors.isToolHasUI(identifier));
const openToolUI = useChatStore((s) => s.openToolUI);
- const pluginTitle = pluginHelpers.getPluginTitle(pluginMeta) ?? t('unknownPlugin');
+ const pluginTitle = pluginHelpers.getPluginTitle(pluginMeta) ?? identifier;
return (
<Flexbox
diff --git a/src/features/Portal/Plugins/Title.tsx b/src/features/Portal/Plugins/Title.tsx
index 498f35659d1..20ff2e1556d 100644
--- a/src/features/Portal/Plugins/Title.tsx
+++ b/src/features/Portal/Plugins/Title.tsx
@@ -18,7 +18,7 @@ const Title = () => {
const { t } = useTranslation('plugin');
const pluginMeta = useToolStore(toolSelectors.getMetaById(toolUIIdentifier), isEqual);
- const pluginTitle = pluginHelpers.getPluginTitle(pluginMeta) ?? t('unknownPlugin');
+ const pluginTitle = pluginHelpers.getPluginTitle(pluginMeta) ?? toolUIIdentifier;
if (toolUIIdentifier === WebBrowsingManifest.identifier) {
return (
diff --git a/src/server/routers/async/file.ts b/src/server/routers/async/file.ts
index f60e1f36e45..65992ec93e2 100644
--- a/src/server/routers/async/file.ts
+++ b/src/server/routers/async/file.ts
@@ -18,6 +18,7 @@ import { asyncAuthedProcedure, asyncRouter as router } from '@/libs/trpc/async';
import { getServerDefaultFilesConfig } from '@/server/globalConfig';
import { initModelRuntimeFromDB } from '@/server/modules/ModelRuntime';
import { ChunkService } from '@/server/services/chunk';
+import { DocumentService } from '@/server/services/document';
import { FileService } from '@/server/services/file';
import { type IAsyncTaskError } from '@/types/asyncTask';
import { AsyncTaskError, AsyncTaskErrorType, AsyncTaskStatus } from '@/types/asyncTask';
@@ -32,6 +33,7 @@ const fileProcedure = asyncAuthedProcedure.use(async (opts) => {
asyncTaskModel: new AsyncTaskModel(ctx.serverDB, ctx.userId),
chunkModel: new ChunkModel(ctx.serverDB, ctx.userId),
chunkService: new ChunkService(ctx.serverDB, ctx.userId),
+ documentService: new DocumentService(ctx.serverDB, ctx.userId),
embeddingModel: new EmbeddingModel(ctx.serverDB, ctx.userId),
fileModel: new FileModel(ctx.serverDB, ctx.userId),
fileService: new FileService(ctx.serverDB, ctx.userId),
@@ -203,6 +205,17 @@ export const fileRouter = router({
// update the task status to processing
await ctx.asyncTaskModel.update(input.taskId, { status: AsyncTaskStatus.Processing });
+ // parse file to document record first (for detailed content viewing)
+ try {
+ await ctx.documentService.parseFile(input.fileId);
+ } catch (e) {
+ // document parsing failure should not block chunking
+ console.warn(
+ '[parseFileToChunks] document parsing failed, continuing with chunking:',
+ e,
+ );
+ }
+
// partition file to chunks
const chunkResult = await chunkService.chunkContent({
content,
diff --git a/src/server/services/document/__tests__/index.test.ts b/src/server/services/document/__tests__/index.test.ts
index c885c612f10..db180f9ae01 100644
--- a/src/server/services/document/__tests__/index.test.ts
+++ b/src/server/services/document/__tests__/index.test.ts
@@ -42,6 +42,7 @@ describe('DocumentService', () => {
mockDocumentModel = {
create: vi.fn(),
delete: vi.fn(),
+ findByFileId: vi.fn().mockResolvedValue(null),
findById: vi.fn(),
query: vi.fn(),
update: vi.fn(),
diff --git a/src/server/services/document/index.ts b/src/server/services/document/index.ts
index 3348f45fe56..c32efac4c43 100644
--- a/src/server/services/document/index.ts
+++ b/src/server/services/document/index.ts
@@ -305,6 +305,10 @@ export class DocumentService {
*
*/
async parseFile(fileId: string): Promise<LobeDocument> {
+ // Idempotent: return existing document if already parsed
+ const existingDoc = await this.documentModel.findByFileId(fileId);
+ if (existingDoc) return existingDoc as LobeDocument;
+
const { filePath, file, cleanup } = await this.fileService.downloadFileToLocal(fileId);
const logPrefix = `[${file.name}]`;