-
Notifications
You must be signed in to change notification settings - Fork 56
fix(chainlit): render source PDF previews in the chat side panel #606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f46bee2
fix(chainlit): render source PDF previews in the chat side panel
Ahmath-Gadji 655f703
fix(chainlit): serve pdf.js worker on the Ray Serve Chainlit origin too
Ahmath-Gadji 5e2657c
fix(chainlit): no-op asset mount when chainlit has no __file__
Ahmath-Gadji 779f4db
fix(chainlit): register RequestIdMiddleware on standalone Ray Serve app
Ahmath-Gadji ed28fd6
fix(download): send X-Content-Type-Options: nosniff on source downloads
Ahmath-Gadji File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| """Serve Chainlit's bundled root-absolute static assets (the pdf.js worker). | ||
|
|
||
| Chainlit is submounted at ``/chainlit``, so its HTML asset references are | ||
| rewritten to ``/chainlit/assets/*``. Its bundled pdf.js worker URL, however, is | ||
| computed at runtime in JS as the root-absolute ``/assets/pdf.worker*.mjs`` (no | ||
| mount prefix), so react-pdf fetches the worker from the origin root. That path | ||
| is not under the ``/chainlit`` auth bypass, so it returns a 403 JSON body and | ||
| the browser blocks the module worker on a bad MIME type — source PDF previews | ||
| then fail with "Failed to load PDF file". Serving the same asset files at | ||
| ``/assets`` too (``AuthMiddleware`` bypasses ``/assets/*``) lets the worker load | ||
| with a JavaScript MIME type. | ||
|
|
||
| Both browser origins that load Chainlit must expose this route: the mounted | ||
| deployment (``api.main``) and the standalone Ray Serve Chainlit process | ||
| (``chainlit_api``, served on its own port). This mirrors how ``chainlit_api`` | ||
| already replicates the ``/static`` download route for the same | ||
| separate-origin reason. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| from fastapi import FastAPI | ||
|
|
||
|
|
||
| def mount_chainlit_root_assets(app: FastAPI) -> None: | ||
| """Mount Chainlit's ``frontend/dist/assets`` at ``/assets`` (no-op if absent).""" | ||
| import chainlit | ||
| from starlette.staticfiles import StaticFiles | ||
|
|
||
| # A regularly installed ``chainlit`` always has ``__file__``; a namespace | ||
| # package or a test double (bare ``ModuleType``) may not — treat that as | ||
| # "bundled assets unavailable" and no-op rather than raising AttributeError. | ||
| chainlit_file = getattr(chainlit, "__file__", None) | ||
| if not chainlit_file: | ||
| return | ||
|
|
||
| assets_dir = Path(chainlit_file).parent / "frontend" / "dist" / "assets" | ||
| if assets_dir.is_dir(): | ||
| app.mount("/assets", StaticFiles(directory=str(assets_dir)), name="chainlit_root_assets") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.