[PR #27600] feat: show “View original” in citations and expose document_url in retrieval metadata #31813

Closed
opened 2026-02-21 20:50:11 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/27600

State: closed
Merged: No


Summary

Fixes #27596

Motivation: Enterprise users want to view the original file/web page for any recalled chunk from both internal and external knowledge bases, and API consumers need a document URL in responses for auditability and deep linking. Scope includes Chatflow and Chatbot apps.

What’s included:

  • API: Add document_url to retrieval metadata so clients can open the source document.
    • Internal KBs: For dataset documents, generate a user-accessible URL based on source type:
      • Uploads: signed preview URL
      • Website crawl: original page URL
      • Notion import: Notion page link (or derived from page id)
      • Fallback: best-effort from source_url-like metadata
    • External KBs: Use url/source_url metadata when available.
  • UI: In the chat "Citation and Attributions" view, add an action to open the original source ("View original"). Applies to Chatflow and Chatbot.

Notable changes:

  • Backend
    • api/core/rag/utils/document_url.py: New helpers to derive document URLs for internal and external sources.
    • api/core/rag/entities/citation_metadata.py: Extend RetrievalSourceMetadata with document_url.
    • api/core/rag/retrieval/dataset_retrieval.py: Populate document_url for internal and external retrieval sources.
    • api/controllers/service_api/app/message.py: Rebuild retriever_resources on read to include fresh document_url for each resource.
  • Frontend
    • web/app/components/base/chat/chat/citation/popup.tsx: Add UI affordance to open the original document from citations/attributions.
    • web/app/components/base/chat/chat/type.ts: Type updates to include document_url.
    • web/i18n/*/common.ts: i18n strings for the new UI text.

Behavioral notes:

  • No breaking changes; document_url is additive on existing retrieval metadata.
  • Works for app modes Chatflow and Chatbot, in both draft mode and published mode.
  • Signed URLs for uploads respect existing file helper security and expiration behavior.

Example API shape (trimmed):

{
  "data": [
    {
      "retriever_resources": [
        {
          "dataset_id": "...",
          "document_id": "...",
          "score": 0.87,
          "content": "...",
          "document_url": "https://..."  
        }
      ]
    }
  ]
}

Screenshots

Chatflow

  • the kb node now returns document_url field
  • a 'view original' button displays on the citation area if document_url field exists, click it to either view the file content(if browser supports it or download the file)
    image

also works for published chatflow:
image

also works for api call:
image

Chatbot

in draft mode:
image

in published mode:
image

api:
image

Checklist

  • This change requires a documentation update, included: Dify Document (docs to describe document_url in API responses and the new UI action)
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • [] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • [] I've updated the documentation accordingly.
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods
**Original Pull Request:** https://github.com/langgenius/dify/pull/27600 **State:** closed **Merged:** No --- ## Summary Fixes #27596 Motivation: Enterprise users want to view the original file/web page for any recalled chunk from both internal and external knowledge bases, and API consumers need a document URL in responses for auditability and deep linking. Scope includes Chatflow and Chatbot apps. What’s included: - API: Add `document_url` to retrieval metadata so clients can open the source document. - Internal KBs: For dataset documents, generate a user-accessible URL based on source type: - Uploads: signed preview URL - Website crawl: original page URL - Notion import: Notion page link (or derived from page id) - Fallback: best-effort from `source_url`-like metadata - External KBs: Use `url`/`source_url` metadata when available. - UI: In the chat "Citation and Attributions" view, add an action to open the original source ("View original"). Applies to Chatflow and Chatbot. Notable changes: - Backend - `api/core/rag/utils/document_url.py`: New helpers to derive document URLs for internal and external sources. - `api/core/rag/entities/citation_metadata.py`: Extend `RetrievalSourceMetadata` with `document_url`. - `api/core/rag/retrieval/dataset_retrieval.py`: Populate `document_url` for internal and external retrieval sources. - `api/controllers/service_api/app/message.py`: Rebuild `retriever_resources` on read to include fresh `document_url` for each resource. - Frontend - `web/app/components/base/chat/chat/citation/popup.tsx`: Add UI affordance to open the original document from citations/attributions. - `web/app/components/base/chat/chat/type.ts`: Type updates to include `document_url`. - `web/i18n/*/common.ts`: i18n strings for the new UI text. Behavioral notes: - No breaking changes; `document_url` is additive on existing retrieval metadata. - Works for app modes Chatflow and Chatbot, in both draft mode and published mode. - Signed URLs for uploads respect existing file helper security and expiration behavior. Example API shape (trimmed): ```json { "data": [ { "retriever_resources": [ { "dataset_id": "...", "document_id": "...", "score": 0.87, "content": "...", "document_url": "https://..." } ] } ] } ``` ## Screenshots ### Chatflow - the kb node now returns document_url field - a 'view original' button displays on the citation area if document_url field exists, click it to either view the file content(if browser supports it or download the file) <img width="1666" height="733" alt="image" src="https://github.com/user-attachments/assets/d8cebfd1-58db-41c0-a470-e5894ea1821a" /> also works for published chatflow: <img width="900" height="865" alt="image" src="https://github.com/user-attachments/assets/aa367c63-4fc2-46da-b5d5-fd6eee5ab3ca" /> also works for api call: <img width="1475" height="587" alt="image" src="https://github.com/user-attachments/assets/49eb44b3-7be8-4b40-9c34-be5307c42b45" /> ### Chatbot in draft mode: <img width="1649" height="685" alt="image" src="https://github.com/user-attachments/assets/cdcdd046-c44d-4ee2-9920-07edc3870310" /> in published mode: <img width="836" height="766" alt="image" src="https://github.com/user-attachments/assets/e34432e6-ea16-4b96-b582-b0b5ea0fdb92" /> api: <img width="744" height="357" alt="image" src="https://github.com/user-attachments/assets/495f96b7-cf80-40e4-afbc-2b6f2d9fbf49" /> ## Checklist - [ ] This change requires a documentation update, included: Dify Document (docs to describe `document_url` in API responses and the new UI action) - [x] I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!) - [] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change. - [] I've updated the documentation accordingly. - [x] I ran `dev/reformat`(backend) and `cd web && npx lint-staged`(frontend) to appease the lint gods
yindo added the pull-request label 2026-02-21 20:50:11 -05:00
yindo closed this issue 2026-02-21 20:50:11 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#31813