Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(web): show document summaries using outline source identities (#45153)

Jyong254b0df42ea1
diff --git a/web/features/new-rag/documents/detail/__tests__/model.spec.ts b/web/features/new-rag/documents/detail/__tests__/model.spec.ts
index 726627dd54f..62fcf43afae 100644
--- a/web/features/new-rag/documents/detail/__tests__/model.spec.ts
+++ b/web/features/new-rag/documents/detail/__tests__/model.spec.ts
@@ -242,6 +242,73 @@ describe('document detail model', () => {
expect(tree.roots[0]?.children[0]?.targetChunkId).toBe('tax-table')
})
+ it('shows a fallback outline summary once for unsectioned source chunks', () => {
+ const first = chunk({ id: 'source-1', ordinal: 0, text: 'Knowledge sources ground agents.' })
+ const second = chunk({
+ id: 'source-2',
+ ordinal: 1,
+ text: 'Generative orchestration filters sources.',
+ })
+ const model = buildDocumentDetailModel(
+ [second, first],
+ [
+ outlineNode({
+ section_path: ['Document'],
+ source_node_ids: ['source-2', 'source-1'],
+ summary: 'Agents use knowledge sources and filter them during orchestration.',
+ title: 'Document',
+ toc_source: 'fallback',
+ }),
+ ],
+ )
+
+ expect(model.tree.roots).toEqual([
+ { children: [], id: 'outline-1', label: 'Document', targetChunkId: 'source-1' },
+ ])
+ expect(model.contentBlocks.map((block) => block.body)).toEqual([first.text, second.text])
+ expect(model.contentBlocksByChunkId.get('source-1')).toMatchObject({
+ heading: { level: 1, text: 'Document' },
+ summary: 'Agents use knowledge sources and filter them during orchestration.',
+ })
+ expect(model.contentBlocksByChunkId.get('source-2')?.heading).toBeUndefined()
+ expect(model.contentBlocksByChunkId.get('source-2')?.summary).toBeUndefined()
+ })
+
+ it('uses source identities to distinguish outline sections with the same path', () => {
+ const model = buildDocumentDetailModel(
+ [
+ chunk({ id: 'first', ordinal: 0, sectionPath: ['Guide'] }),
+ chunk({ id: 'second', ordinal: 1, sectionPath: ['Guide'] }),
+ ],
+ [
+ outlineNode({ id: 'first-outline', source_node_ids: ['first'], summary: 'First section.' }),
+ outlineNode({
+ id: 'second-outline',
+ source_node_ids: ['second'],
+ summary: 'Second section.',
+ }),
+ ],
+ )
+
+ expect(model.tree.roots.map((node) => node.targetChunkId)).toEqual(['first', 'second'])
+ expect(model.contentBlocksByChunkId.get('first')?.summary).toBe('First section.')
+ expect(model.contentBlocksByChunkId.get('second')).toMatchObject({
+ heading: { level: 1, text: 'Guide' },
+ summary: 'Second section.',
+ })
+ })
+
+ it('does not attach an unloaded source summary to a different chunk with the same path', () => {
+ const model = buildDocumentDetailModel(
+ [chunk({ id: 'loaded', sectionPath: ['Guide'] })],
+ [outlineNode({ source_node_ids: ['not-loaded-yet'], summary: 'Another chunk summary.' })],
+ )
+
+ expect(model.contentBlocksByChunkId.get('loaded')?.summary).toBeUndefined()
+ expect(model.tree.byId.has('outline-1')).toBe(false)
+ expect(model.contentBlocksByChunkId.has('loaded')).toBe(true)
+ })
+
it('uses the persisted outline hierarchy and hides a legacy HTML title chunk', () => {
const titleChunk = chunk({
id: 'html-title',
diff --git a/web/features/new-rag/documents/detail/__tests__/page.spec.tsx b/web/features/new-rag/documents/detail/__tests__/page.spec.tsx
index 854b4fda8c6..1cf6a7eec43 100644
--- a/web/features/new-rag/documents/detail/__tests__/page.spec.tsx
+++ b/web/features/new-rag/documents/detail/__tests__/page.spec.tsx
@@ -947,6 +947,69 @@ describe('DocumentDetailPage', () => {
expect(screen.getByRole('article').querySelector('br')).toBeNull()
})
+ it('renders the returned document summary when its source chunks have no section paths', async () => {
+ chunksQuery.data = {
+ pages: [
+ {
+ items: [
+ chunk({
+ id: 'source-1',
+ ordinal: 0,
+ sectionPath: [],
+ text: 'Knowledge sources ground agents.',
+ }),
+ chunk({
+ id: 'source-2',
+ ordinal: 1,
+ sectionPath: [],
+ text: 'Generative orchestration filters sources.',
+ }),
+ ],
+ },
+ ],
+ }
+ outlineQuery.data = {
+ artifact_hash: 'artifact-hash',
+ created_at: '2026-10-30T09:30:00Z',
+ document_asset_id: 'asset-1',
+ id: 'outline-1',
+ knowledge_space_id: 'space-1',
+ metadata: {},
+ nodes: [
+ {
+ children: [],
+ id: 'document-outline',
+ level: 1,
+ metadata: {},
+ section_path: ['Document'],
+ source_node_ids: ['source-1', 'source-2'],
+ summary: 'Agents use knowledge sources and filter them during orchestration.',
+ title: 'Document',
+ toc_source: 'fallback',
+ },
+ ],
+ outline_version: 'document-outline-v1',
+ parse_artifact_id: 'parse-artifact-1',
+ version: 1,
+ }
+
+ await render(<DocumentDetailPage documentId="document-1" knowledgeSpaceId="space-1" />)
+
+ expect(
+ await screen.findByText('Agents use knowledge sources and filter them during orchestration.'),
+ ).toBeInTheDocument()
+ expect(
+ screen.getAllByRole('button', { name: 'knowledgeDocuments.documentSummary' }),
+ ).toHaveLength(1)
+ expect(within(screen.getByRole('tree')).getAllByRole('treeitem')).toHaveLength(1)
+ expect(
+ within(screen.getByRole('tree')).getByRole('treeitem', { name: 'Document' }),
+ ).toBeInTheDocument()
+ expect(screen.getAllByRole('heading', { name: 'Document' })).toHaveLength(1)
+ expect(screen.getByText('Knowledge sources ground agents.')).toBeInTheDocument()
+ expect(screen.getByText('Generative orchestration filters sources.')).toBeInTheDocument()
+ })
+
it('renders collapsible outline summaries and semantic heading levels', async () => {
const user = userEvent.setup()
chunksQuery.data = {
diff --git a/web/features/new-rag/documents/detail/model.ts b/web/features/new-rag/documents/detail/model.ts
index 5bd3c69a40c..c996f7029ca 100644
--- a/web/features/new-rag/documents/detail/model.ts
+++ b/web/features/new-rag/documents/detail/model.ts
@@ -290,7 +290,17 @@ function buildOutlineBackedDocumentModel(
): DocumentChunkTreeNode | undefined => {
const explicitPath = normalizedSectionPath(outlineNode.section_path ?? [])
const path = explicitPath.length ? explicitPath : [...parentPath, outlineNode.title.trim()]
- const exactChunks = chunksBySection.get(sectionPathKey(path)) ?? []
+ const sourceNodeIds = outlineNode.source_node_ids ?? []
+ // Source identities survive fallback outlines and section-path changes.
+ // Only legacy outlines without them need to be joined by section path.
+ const exactChunks = sourceNodeIds.length
+ ? [...new Set(sourceNodeIds)]
+ .flatMap((id) => {
+ const chunk = contentChunksById.get(id)
+ return chunk ? [chunk] : []
+ })
+ .sort(compareChunks)
+ : (chunksBySection.get(sectionPathKey(path)) ?? [])
for (const chunk of exactChunks) {
matchedChunkIds.add(chunk.id)
if (!outlineNodesByChunkId.has(chunk.id)) outlineNodesByChunkId.set(chunk.id, outlineNode)
@@ -302,7 +312,9 @@ function buildOutlineBackedDocumentModel(
const node = buildNode(child, outlineNode.id, path)
return node ? [node] : []
})
- const descendantChunk = firstChunkBySectionPrefix.get(sectionPathKey(path))
+ const descendantChunk = sourceNodeIds.length
+ ? undefined
+ : firstChunkBySectionPrefix.get(sectionPathKey(path))
const childChunk = children[0] ? contentChunksById.get(children[0].targetChunkId) : undefined
const chunk = exactChunks[0] ?? childChunk ?? descendantChunk
if (!chunk) return undefined
@@ -408,8 +420,12 @@ function createDocumentDetailModel(
summariesByChunkId?: ReadonlyMap<string, string>
},
): DocumentDetailModel {
+ const firstChunkIdByOutline = new Map<string, string>()
const firstChunkIdBySection = new Map<string, string>()
for (const chunk of contentChunks) {
+ const outlineNode = options.outlineNodesByChunkId?.get(chunk.id)
+ if (outlineNode && !firstChunkIdByOutline.has(outlineNode.id))
+ firstChunkIdByOutline.set(outlineNode.id, chunk.id)
const sectionPath = normalizedSectionPath(chunk.sectionPath)
if (!sectionPath.length) continue
const key = sectionPathKey(sectionPath)
@@ -419,9 +435,10 @@ function createDocumentDetailModel(
const contentBlocks: DocumentContentBlock[] = contentChunks.map((chunk) => {
const content = chunkContentParts(chunk)
const sectionPath = normalizedSectionPath(chunk.sectionPath)
- const ownsSectionHeading =
- !sectionPath.length || firstChunkIdBySection.get(sectionPathKey(sectionPath)) === chunk.id
const outlineNode = options.outlineNodesByChunkId?.get(chunk.id)
+ const ownsSectionHeading = outlineNode
+ ? firstChunkIdByOutline.get(outlineNode.id) === chunk.id
+ : !sectionPath.length || firstChunkIdBySection.get(sectionPathKey(sectionPath)) === chunk.id
const headingText = outlineNode?.title.trim() || content.heading || undefined
return {
body: content.body,