Repository navigation
fix(agent): enable MCP file preview via doc_id - #15399
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughCentralizes file-response handling, adds agent attachment preview streaming and preview URLs, updates document preview responses, and extends client/test coverage for the preview flow. ChangesPreview and download attachment infrastructure
Estimated code review effort: 3 (Moderate) | ~25 minutes Suggested labels: Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
api/apps/backward_compat.py (1)
605-619:⚠️ Potential issue | 🟠 Major | ⚡ Quick winKeep legacy
/download/...aliases on download semantics.These handlers now return the preview endpoint, which changes a backward-compatibility route from
Content-Disposition: attachmentto inline behavior. Existing clients calling/document/download/...can start opening files in-browser instead of downloading them, which is a breaking API change unrelated to the new preview route itself. Please keep these aliases wired to the download handler and let MCP clients use/previewexplicitly.Also applies to: 622-636
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@api/apps/backward_compat.py` around lines 605 - 619, The deprecated `/document/download/<doc_id>` route (function deprecated_document_download) must preserve download semantics instead of delegating to the preview endpoint: change its implementation to call the agent API's download behavior (the download-specific handler on agent_api that returns a response with Content-Disposition: attachment) or, if no such helper exists, wrap the preview response and set the Content-Disposition header to attachment with the original filename; do the same fix for the other deprecated download alias noted in the same area (the second deprecated `/document/download/...` handler around lines 622-636) so both legacy aliases continue forcing a file download while `/preview` remains for inline preview.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@api/utils/file_response.py`:
- Around line 139-146: The helper agent_attachment_preview_path currently
appends raw ext and mime_type values into the query string, which breaks values
containing reserved characters (e.g., "image/svg+xml"); fix by URL-encoding
these parameters before building the suffix. Update
agent_attachment_preview_path to build a params dict only with non-None values
and use urllib.parse.urlencode(params, safe='') (import urllib.parse) to produce
the query string, then append that encoded string as the suffix when returning
the preview URL.
In `@test/unit_test/api/utils/test_file_response_headers.py`:
- Around line 20-48: Add pytest priority markers to each test function in this
file: decorate test_apply_preview_sets_inline_for_pdf,
test_apply_preview_forces_attachment_for_html,
test_apply_download_sets_attachment_for_pdf,
test_resolve_attachment_content_type_prefers_mime_type, and
test_agent_attachment_preview_path_includes_query with the appropriate
`@pytest.mark.p1` / p2 / p3 marker per guidelines so they are picked up by the
repo test-selection; update imports to include pytest.mark if missing and ensure
the chosen priority matches project policy for backend tests.
In `@web/src/utils/api.ts`:
- Around line 226-227: getAttachmentFilePreview currently builds a preview URL
from just docId which omits required metadata (ext/mime_type) and causes backend
to default to application/octet-stream; update getAttachmentFilePreview to
accept optional parameters ext and mime_type (or a single metadata object) and
include them as query params when constructing the URL, or stop reconstructing
the URL and instead return the preview_url supplied by DocGenerator (prefer the
latter if available); locate the helper named getAttachmentFilePreview in
web/src/utils/api.ts and modify its signature and implementation to pass-through
or append ext and/or mime_type, or wire the DocGenerator preview_url into
callers instead of reconstructing.
---
Outside diff comments:
In `@api/apps/backward_compat.py`:
- Around line 605-619: The deprecated `/document/download/<doc_id>` route
(function deprecated_document_download) must preserve download semantics instead
of delegating to the preview endpoint: change its implementation to call the
agent API's download behavior (the download-specific handler on agent_api that
returns a response with Content-Disposition: attachment) or, if no such helper
exists, wrap the preview response and set the Content-Disposition header to
attachment with the original filename; do the same fix for the other deprecated
download alias noted in the same area (the second deprecated
`/document/download/...` handler around lines 622-636) so both legacy aliases
continue forcing a file download while `/preview` remains for inline preview.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: cc404b07-3317-42c5-80ca-68da7d2f3331
📒 Files selected for processing (8)
agent/component/docs_generator.pyapi/apps/backward_compat.pyapi/apps/restful_apis/agent_api.pyapi/apps/restful_apis/document_api.pyapi/utils/file_response.pyapi/utils/web_utils.pytest/unit_test/api/utils/test_file_response_headers.pyweb/src/utils/api.ts
|
@monsterdavidliu-ux |
|
I'd classify this as an existing bug, not a new feature. Why bug: For MCP-wrapped agents, looking up a file by What the PR does: Restores the intended behavior — inline preview for previewable types via explicit preview routes/headers and The new Happy to retitle or adjust labels if the team prefers |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #15399 +/- ##
=======================================
Coverage 93.16% 93.16%
=======================================
Files 10 10
Lines 717 717
Branches 118 118
=======================================
Hits 668 668
Misses 29 29
Partials 20 20 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
This PR currently has merge conflicts with Please run locally: git fetch origin main
git rebase origin/main
# or
git merge origin/mainAfter resolving the conflicts, push to the current head branch and CI will re-run the review checks.
|
Add /agents/attachments/{id}/preview with inline Content-Disposition for
previewable types, resolve mime_type/ext on attachment streams, set inline
headers on /documents/{doc_id}/preview, and expose preview_url in DocGenerator
output. Legacy document/download aliases now route to preview.
Fixes infiniflow#15398
URL-encode preview query params, keep legacy download aliases on download semantics, extend getAttachmentFilePreview with ext/mime metadata, and add pytest p2 markers plus svg mime encoding coverage.
Co-authored-by: Cursor <[email protected]>
90173c8 to
a621d3a
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (1)
api/apps/restful_apis/agent_api.py (1)
2496-2515: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winConsider route-level tests for the new preview/download endpoints.
The referenced test suite (
test/unit_test/api/utils/test_file_response_headers.py) covers the header-utility functions but doesn't appear to exercisepreview_attachment/download_attachmentat the route level (e.g.,disposition=inlineon the download route, the not-found branch, orext/mime_typequery resolution end-to-end). Given this is the core deliverable of the PR, route-level coverage would give higher confidence.Want me to draft unit tests for these two routes?
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@api/apps/restful_apis/agent_api.py` around lines 2496 - 2515, Add route-level tests for the new preview_attachment and download_attachment endpoints, since the current coverage only validates lower-level header helpers. Exercise preview_attachment and download_attachment end-to-end, including the download route’s disposition=inline path, the not-found/error branch, and query-driven ext/mime_type resolution through _stream_agent_attachment. Use the preview_attachment, download_attachment, and _stream_agent_attachment symbols to locate the route behavior and verify the response headers and status handling.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@api/apps/restful_apis/agent_api.py`:
- Around line 2496-2515: Add route-level tests for the new preview_attachment
and download_attachment endpoints, since the current coverage only validates
lower-level header helpers. Exercise preview_attachment and download_attachment
end-to-end, including the download route’s disposition=inline path, the
not-found/error branch, and query-driven ext/mime_type resolution through
_stream_agent_attachment. Use the preview_attachment, download_attachment, and
_stream_agent_attachment symbols to locate the route behavior and verify the
response headers and status handling.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 53e57719-3508-4335-8abb-6b2c647e0ee9
📒 Files selected for processing (5)
agent/component/docs_generator.pyapi/apps/restful_apis/agent_api.pyapi/apps/restful_apis/document_api.pyapi/utils/file_response.pyapi/utils/web_utils.py
💤 Files with no reviewable changes (3)
- api/utils/web_utils.py
- api/utils/file_response.py
- api/apps/restful_apis/document_api.py
🚧 Files skipped from review as they are similar to previous changes (1)
- agent/component/docs_generator.py
## Summary
MCP-wrapped agents could only force-download files looked up by
`doc_id`. This adds an explicit preview path and inline response headers
for previewable file types.
- **New** `GET /api/v1/agents/attachments/{attachment_id}/preview` —
inline preview for PDFs, images, and other safe types (pass `ext` and/or
`mime_type`)
- **Improved** `GET /api/v1/documents/{doc_id}/preview` — sets inline
disposition using the document filename
- **Improved** attachment download routing — resolves `mime_type` /
`ext` query params (no default `markdown`), supports
`disposition=inline`
- **DocGenerator output** — includes URL-encoded `preview_url` for MCP
clients
- **Legacy `/document/download/...` aliases** — still use download
semantics; MCP clients should call `/preview` explicitly
Fixes infiniflow#15398
## Test plan
- [x] `pytest test/unit_test/api/utils/test_file_response_headers.py`
(6/6)
---------
Co-authored-by: MkDev11 <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Ling Qin <[email protected]>
## Summary
MCP-wrapped agents could only force-download files looked up by
`doc_id`. This adds an explicit preview path and inline response headers
for previewable file types.
- **New** `GET /api/v1/agents/attachments/{attachment_id}/preview` —
inline preview for PDFs, images, and other safe types (pass `ext` and/or
`mime_type`)
- **Improved** `GET /api/v1/documents/{doc_id}/preview` — sets inline
disposition using the document filename
- **Improved** attachment download routing — resolves `mime_type` /
`ext` query params (no default `markdown`), supports
`disposition=inline`
- **DocGenerator output** — includes URL-encoded `preview_url` for MCP
clients
- **Legacy `/document/download/...` aliases** — still use download
semantics; MCP clients should call `/preview` explicitly
Fixes infiniflow#15398
## Test plan
- [x] `pytest test/unit_test/api/utils/test_file_response_headers.py`
(6/6)
---------
Co-authored-by: MkDev11 <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Ling Qin <[email protected]>
Summary
MCP-wrapped agents could only force-download files looked up by
doc_id. This adds an explicit preview path and inline response headers for previewable file types.GET /api/v1/agents/attachments/{attachment_id}/preview— inline preview for PDFs, images, and other safe types (passextand/ormime_type)GET /api/v1/documents/{doc_id}/preview— sets inline disposition using the document filenamemime_type/extquery params (no defaultmarkdown), supportsdisposition=inlinepreview_urlfor MCP clients/document/download/...aliases — still use download semantics; MCP clients should call/previewexplicitlyFixes #15398
Test plan
pytest test/unit_test/api/utils/test_file_response_headers.py(6/6)