-
Notifications
You must be signed in to change notification settings - Fork 55
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
feat: display local pdf files #85
Conversation
🦋 Changeset detectedLatest commit: d54fe0a The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
WalkthroughThis update introduces a new API endpoint for fetching file data, enhances the chat component to handle and display PDF files, and includes support for serving static files in a FastAPI application. Additionally, new dependencies have been added to facilitate these features. Changes
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
Out of diff range and nitpick comments (2)
templates/types/streaming/express/index.ts (1)
34-34
: Consider documenting the contents of the/data
directory.It's a good practice to document what types of files are expected to be in the
/data
directory to prevent accidental exposure of sensitive files.templates/types/streaming/fastapi/main.py (1)
41-41
: Consider documenting the contents of the/data
directory.As with any public file serving, documenting the expected contents of the
/data
directory can help prevent the accidental exposure of sensitive files.
Review Details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (6)
- templates/types/streaming/express/index.ts (1 hunks)
- templates/types/streaming/fastapi/main.py (3 hunks)
- templates/types/streaming/nextjs/app/api/data/route.ts (1 hunks)
- templates/types/streaming/nextjs/app/components/ui/chat/chat-sources.tsx (4 hunks)
- templates/types/streaming/nextjs/app/components/ui/drawer.tsx (1 hunks)
- templates/types/streaming/nextjs/package.json (1 hunks)
Additional Context Used
Ruff (10)
templates/types/streaming/fastapi/main.py (10)
5-5: Module level import not at top of file
6-6: Module level import not at top of file
7-7: Module level import not at top of file
8-8: Module level import not at top of file
9-9: Module level import not at top of file
10-10: Module level import not at top of file
11-11: Module level import not at top of file
12-12: Module level import not at top of file
13-13: Module level import not at top of file
14-14: Module level import not at top of file
Additional comments not posted (3)
templates/types/streaming/nextjs/package.json (1)
36-36
: Addition of PDF viewer library aligns with new features.The inclusion of
@llamaindex/pdf-viewer
is appropriate for the new PDF handling capabilities introduced in the project.templates/types/streaming/nextjs/app/components/ui/drawer.tsx (1)
1-118
: Drawer components are well-implemented and structured.The use of
vaul
library components, along withReact.forwardRef
and proper use ofdisplayName
, enhances the maintainability and debuggability of the UI components.templates/types/streaming/nextjs/app/components/ui/chat/chat-sources.tsx (1)
Line range hint
1-153
: Verify the consistent and secure use of environment variables.Ensure that environment variables are used consistently and securely throughout the project.
Verification successful
The verification of the usage of environment variables across the project has been successful. The results show that environment variables are used consistently and securely throughout various components and settings files. The usage patterns include checks for the existence of variables, default values, and secure handling of potentially undefined variables.
Here are some key points observed from the output:
- Environment variables are used in both frontend (
nextjs
) and backend (express
) components.- Variables are accessed securely with fallbacks or checks (e.g.,
process.env.MODEL ?? "gpt-3.5-turbo"
).- Critical configurations like API endpoints and database connections are controlled via environment variables, which is a best practice for maintaining security and flexibility.
Given these observations, the use of environment variables in the project aligns with common practices for secure and flexible configuration management.
Scripts Executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the usage of environment variables in the Next.js project. # Test: Search for environment variable usage. Expect: Consistent and secure usage. rg --type typescript $'process.env'Length of output: 69
Script:
#!/bin/bash # Description: Verify the usage of environment variables in the Next.js project, correcting the file type issue. # Define 'typescript' as a type including .ts and .tsx files rg --type-add 'typescript:*.ts' --type-add 'typescript:*.tsx' --type typescript 'process.env'Length of output: 8482
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review Details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (1)
- .changeset/weak-bobcats-trade.md (1 hunks)
Files skipped from review due to trivial changes (1)
- .changeset/weak-bobcats-trade.md
templates/types/streaming/nextjs/app/components/ui/chat/chat-sources.tsx
Outdated
Show resolved
Hide resolved
templates/types/streaming/nextjs/app/components/ui/chat/chat-sources.tsx
Outdated
Show resolved
Hide resolved
templates/types/streaming/nextjs/app/components/ui/chat/chat-sources.tsx
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 5
Out of diff range and nitpick comments (2)
templates/types/streaming/nextjs/app/components/ui/chat/chat-sources.tsx (2)
Line range hint
39-93
: Consider memoizing thegetNodeInfo
function and moving thePdfDialog
component to its own file.To improve performance and maintainability, consider memoizing the
getNodeInfo
function and moving thePdfDialog
component to its own file. This will make the codebase more modular and easier to navigate.import { useMemo, useCallback } from "react"; import PdfDialog from "./widgets/PdfDialog"; const SCORE_THRESHOLD = 0.5; function SourceNumberButton({ index }: { index: number }) { return ( <div className="text-xs w-5 h-5 rounded-full bg-gray-100 mb-2 flex items-center justify-center hover:text-white hover:bg-primary hover:cursor-pointer"> {index + 1} </div> ); } enum NODE_TYPE { URL, LOCAL_FILE, } const getNodeInfo = useCallback((node: SourceNode): { path: string; type: NODE_TYPE } | undefined => { if (typeof node.metadata["URL"] === "string") { return { path: node.metadata["URL"], type: NODE_TYPE.URL, }; } if (typeof node.metadata["file_path"] === "string") { return { path: node.metadata["file_path"], type: NODE_TYPE.LOCAL_FILE, }; } return undefined; }, []); export function ChatSources({ data }: { data: SourceData }) { const sources = useMemo(() => { // aggregate nodes by url or file_path (get the highest one by score) const nodePaths = new Set<string>(); const uniqueNodes: SourceNode[] = []; data.nodes .sort((a, b) => (b.score ?? 1) - (a.score ?? 1)) .forEach((node) => { const nodeInfo = getNodeInfo(node); if (!nodeInfo) { uniqueNodes.push(node); // Always add nodes with unknown type } else if (!nodePaths.has(nodeInfo.path)) { uniqueNodes.push(node); nodePaths.add(nodeInfo.path); } }); return uniqueNodes .filter((node) => Object.keys(node.metadata).length > 0) .filter((node) => (node.score ?? 1) > SCORE_THRESHOLD); }, [data.nodes, getNodeInfo]); if (sources.length === 0) return null; return ( <div className="space-x-2 text-sm"> <span className="font-semibold">Sources:</span> <div className="inline-flex gap-1 items-center"> {sources.map((node: SourceNode, index: number) => { const nodeInfo = getNodeInfo(node); if (nodeInfo?.path.endsWith(".pdf")) { return ( <PdfDialog key={node.id} documentId={node.id} filePath={nodeInfo.path} trigger={<SourceNumberButton index={index} />} /> ); } return ( <div key={node.id}> <HoverCard> <HoverCardTrigger> <SourceNumberButton index={index} /> </HoverCardTrigger> <HoverCardContent className="w-[320px]"> <NodeInfo node={node} /> </HoverCardContent> </HoverCard> </div> ); })} </div> </div> ); } function NodeInfo({ node }: { node: SourceNode }) { const { isCopied, copyToClipboard } = useCopyToClipboard({ timeout: 1000 }); const nodeInfo = getNodeInfo(node); if (nodeInfo?.type === NODE_TYPE.URL) { // this is a node generated by the web loader, it contains an external URL // add a link to view this URL return ( <a className="space-x-2 flex items-center my-2 hover:text-blue-900" href={nodeInfo.path} target="_blank" > <span>{nodeInfo.path}</span> <ArrowUpRightSquare className="w-4 h-4" /> </a> ); } if (nodeInfo?.type === NODE_TYPE.LOCAL_FILE) { // this is a node generated by the file loader, it contains file path // add a button to copy the path to the clipboard return ( <div className="flex items-center justify-between gap-2"> <span className="flex-1 break-all">{nodeInfo.path}</span> <Button onClick={() => copyToClipboard(nodeInfo.path)} size="icon" variant="ghost" className="h-12 w-12 shrink-0" > {isCopied ? ( <Check className="h-4 w-4" /> ) : ( <Copy className="h-4 w-4" /> )} </Button> </div> ); } // node generated by unknown loader, implement renderer by analyzing logged out metadata console.log("Node metadata", node.metadata); return ( <p> Sorry, unknown node type. Please add a new renderer in the NodeInfo component. </p> ); }
Line range hint
100-135
: Consider adding type annotations and comments for theNodeInfo
component.The
NodeInfo
component is correctly implemented, but consider adding type annotations and comments to describe each part of the component for better clarity and maintainability.function NodeInfo({ node }: { node: SourceNode }) { const { isCopied, copyToClipboard } = useCopyToClipboard({ timeout: 1000 }); const nodeInfo = getNodeInfo(node); if (nodeInfo?.type === NODE_TYPE.URL) { // this is a node generated by the web loader, it contains an external URL // add a link to view this URL return ( <a className="space-x-2 flex items-center my-2 hover:text-blue-900" href={nodeInfo.path} target="_blank" > <span>{nodeInfo.path}</span> <ArrowUpRightSquare className="w-4 h-4" /> </a> ); } if (nodeInfo?.type === NODE_TYPE.LOCAL_FILE) { // this is a node generated by the file loader, it contains file path // add a button to copy the path to the clipboard return ( <div className="flex items-center justify-between gap-2"> <span className="flex-1 break-all">{nodeInfo.path}</span> <Button onClick={() => copyToClipboard(nodeInfo.path)} size="icon" variant="ghost" className="h-12 w-12 shrink-0" > {isCopied ? ( <Check className="h-4 w-4" /> ) : ( <Copy className="h-4 w-4" /> )} </Button> </div> ); } // node generated by unknown loader, implement renderer by analyzing logged out metadata console.log("Node metadata", node.metadata); return ( <p> Sorry, unknown node type. Please add a new renderer in the NodeInfo component. </p> ); }
Review Details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (3)
- .changeset/weak-bobcats-trade.md (1 hunks)
- templates/types/streaming/nextjs/app/components/ui/chat/chat-sources.tsx (2 hunks)
- templates/types/streaming/nextjs/app/components/ui/chat/widgets/PdfDialog.tsx (1 hunks)
Files skipped from review as they are similar to previous changes (1)
- .changeset/weak-bobcats-trade.md
templates/types/streaming/nextjs/app/components/ui/chat/widgets/PdfDialog.tsx
Outdated
Show resolved
Hide resolved
templates/types/streaming/nextjs/app/components/ui/chat/widgets/PdfDialog.tsx
Outdated
Show resolved
Hide resolved
templates/types/streaming/nextjs/app/components/ui/chat/widgets/PdfDialog.tsx
Outdated
Show resolved
Hide resolved
templates/types/streaming/nextjs/app/components/ui/chat/chat-sources.tsx
Show resolved
Hide resolved
templates/types/streaming/nextjs/app/components/ui/chat/chat-sources.tsx
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review Details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (1)
- templates/types/streaming/nextjs/app/api/data/route.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
- templates/types/streaming/nextjs/app/api/data/route.ts
templates/types/streaming/nextjs/app/components/ui/chat/widgets/PdfDialog.tsx
Outdated
Show resolved
Hide resolved
templates/types/streaming/nextjs/app/components/ui/chat/chat-sources.tsx
Outdated
Show resolved
Hide resolved
templates/types/streaming/nextjs/app/components/ui/chat/chat-sources.tsx
Outdated
Show resolved
Hide resolved
templates/types/streaming/nextjs/app/components/ui/chat/chat-sources.tsx
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
Review Details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (4)
- templates/types/streaming/nextjs/app/api/data/[path]/route.ts (1 hunks)
- templates/types/streaming/nextjs/app/components/ui/chat/chat-sources.tsx (2 hunks)
- templates/types/streaming/nextjs/app/components/ui/chat/widgets/PdfDialog.tsx (1 hunks)
- templates/types/streaming/nextjs/next.config.mjs (1 hunks)
Files skipped from review as they are similar to previous changes (2)
- templates/types/streaming/nextjs/app/components/ui/chat/chat-sources.tsx
- templates/types/streaming/nextjs/app/components/ui/chat/widgets/PdfDialog.tsx
Additional comments not posted (1)
templates/types/streaming/nextjs/next.config.mjs (1)
8-16
: Ensure the/api/data/:path*
endpoint is correctly implemented.The rewrites configuration looks correct. Please verify that the
/api/data/:path*
endpoint is correctly implemented and handles the requests as expected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 5
Review Details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (3)
- templates/types/streaming/express/index.ts (1 hunks)
- templates/types/streaming/fastapi/main.py (3 hunks)
- templates/types/streaming/nextjs/app/components/ui/lib/url.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
- templates/types/streaming/express/index.ts
Additional Context Used
Ruff (10)
templates/types/streaming/fastapi/main.py (10)
5-5: Module level import not at top of file
6-6: Module level import not at top of file
7-7: Module level import not at top of file
8-8: Module level import not at top of file
9-9: Module level import not at top of file
10-10: Module level import not at top of file
11-11: Module level import not at top of file
12-12: Module level import not at top of file
13-13: Module level import not at top of file
14-14: Module level import not at top of file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review Details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (4)
- templates/types/streaming/nextjs/app/api/data/[path]/route.ts (1 hunks)
- templates/types/streaming/nextjs/app/components/ui/chat/chat-sources.tsx (2 hunks)
- templates/types/streaming/nextjs/app/components/ui/chat/widgets/PdfDialog.tsx (1 hunks)
- templates/types/streaming/nextjs/app/components/ui/lib/url.ts (1 hunks)
Files skipped from review as they are similar to previous changes (4)
- templates/types/streaming/nextjs/app/api/data/[path]/route.ts
- templates/types/streaming/nextjs/app/components/ui/chat/chat-sources.tsx
- templates/types/streaming/nextjs/app/components/ui/chat/widgets/PdfDialog.tsx
- templates/types/streaming/nextjs/app/components/ui/lib/url.ts
templates/types/streaming/nextjs/app/components/ui/chat/chat-sources.tsx
Outdated
Show resolved
Hide resolved
templates/types/streaming/nextjs/app/components/ui/chat/chat-sources.tsx
Outdated
Show resolved
Hide resolved
templates/types/streaming/nextjs/app/components/ui/chat/chat-sources.tsx
Outdated
Show resolved
Hide resolved
templates/types/streaming/nextjs/app/components/ui/chat/chat-sources.tsx
Outdated
Show resolved
Hide resolved
templates/types/streaming/nextjs/app/components/ui/chat/chat-sources.tsx
Outdated
Show resolved
Hide resolved
templates/types/streaming/nextjs/app/components/ui/chat/chat-sources.tsx
Outdated
Show resolved
Hide resolved
templates/types/streaming/nextjs/app/components/ui/chat/chat-sources.tsx
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review Details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (1)
- templates/types/streaming/nextjs/app/components/ui/chat/chat-sources.tsx (3 hunks)
Files skipped from review as they are similar to previous changes (1)
- templates/types/streaming/nextjs/app/components/ui/chat/chat-sources.tsx
templates/types/streaming/nextjs/app/components/ui/chat/chat-sources.tsx
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great job @thucpn
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review Details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (1)
- templates/types/streaming/nextjs/package.json (1 hunks)
Files skipped from review as they are similar to previous changes (1)
- templates/types/streaming/nextjs/package.json
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
Review Details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (1)
- templates/types/streaming/fastapi/main.py (3 hunks)
Additional Context Used
Ruff (10)
templates/types/streaming/fastapi/main.py (10)
5-5: Module level import not at top of file
6-6: Module level import not at top of file
7-7: Module level import not at top of file
8-8: Module level import not at top of file
9-9: Module level import not at top of file
10-10: Module level import not at top of file
11-11: Module level import not at top of file
12-12: Module level import not at top of file
13-13: Module level import not at top of file
14-14: Module level import not at top of file
@@ -11,6 +11,7 @@ | |||
from app.api.routers.chat import chat_router | |||
from app.settings import init_settings | |||
from app.observability import init_observability | |||
from fastapi.staticfiles import StaticFiles |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move the import statement to the top of the file to follow best practices.
- from fastapi.staticfiles import StaticFiles
+ from fastapi.staticfiles import StaticFiles
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
from fastapi.staticfiles import StaticFiles | |
from fastapi.staticfiles import StaticFiles |
if os.path.exists("data"): | ||
app.mount("/api/data", StaticFiles(directory="data"), name="static") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tip
Codebase Verification
The data
directory is missing or not readable, which will prevent the static file serving configuration from working as intended.
- Ensure the
data
directory exists in the root of the repository. - Verify that the directory has the correct read permissions.
Analysis chain
Ensure the data
directory exists and has the correct permissions for serving static files.
Scripts Executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Verify the existence and permissions of the `data` directory.
# Test: Check if the `data` directory exists and has read permissions.
if [ -d "data" ] && [ -r "data" ]; then
echo "The 'data' directory exists and is readable."
else
echo "The 'data' directory is missing or not readable."
exit 1
fi
Length of output: 139
Summary by CodeRabbit
New Features
PdfDialog
component to display PDF content within a drawer.ChatSources
component to aggregate and display nodes by URL or file path.Enhancements
ChatSources
rendering logic to differentiate between PDFs and other sources.Dependencies
vaul
version0.9.1
and@llamaindex/pdf-viewer
version1.1.1
to dependencies.Backend
/data
endpoint in FastAPI.