feat: open local file links from chat#119
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds “open local file link” behavior to chat markdown so paths referenced by the agent can be opened directly in the app’s existing file viewer, including (when authorized) files outside the current project root by scoping access to known roots or files explicitly referenced in the session log.
Changes:
- Add client-side parsing/normalization of local file hrefs and wire “open file” callbacks through chat rendering.
- Propagate a
sourceSessionIdinto file-viewer requests so/api/filescan authorize reads/downloads based on session references. - Add session-log reference detection utilities + tests to support server-side authorization checks.
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| lib/session-reader.ts | Exposes getSessionEntries() for reading a session’s entry list from disk. |
| lib/session-file-references.ts | Adds server helper to authorize a path by checking whether it appears in a session log. |
| lib/session-file-references-core.ts | Implements “exact path reference” detection across session entries. |
| lib/session-file-references.test.mjs | Adds unit tests for exact session-file reference detection. |
| lib/file-links.ts | Adds local-file href parsing/normalization for markdown links (absolute + cwd-relative). |
| lib/file-links.test.mjs | Adds unit tests for resolveLocalFileHref(). |
| components/TabBar.tsx | Extends file-tab metadata to include optional sourceSessionId. |
| components/MessageView.tsx | Threads cwd and onOpenFile through message rendering into MarkdownBody. |
| components/MarkdownBody.tsx | Intercepts qualifying local file links and triggers onOpenFile instead of navigation. |
| components/FileViewer.tsx | Adds sessionId query param plumbing to file API requests (read/meta/preview/watch/download). |
| components/ChatWindow.tsx | Supplies message cwd and onOpenFile handler to MessageView. |
| components/AppShell.tsx | Implements “open linked file” behavior and stores sourceSessionId on file tabs. |
| app/api/files/[...path]/route.ts | Allows file reads outside allowed roots when the file is referenced by the provided session id. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+292
to
+296
| const allowedBySessionReference = | ||
| !allowedByRoot && | ||
| type !== "list" && | ||
| await isFilePathReferencedBySession(filePath, sessionId); | ||
| if (!allowedByRoot && !allowedBySessionReference) { |
Comment on lines
+81
to
+85
| const handleClick = (event: MouseEvent<HTMLAnchorElement>) => { | ||
| if (event.defaultPrevented || event.button !== 0) return; | ||
| event.preventDefault(); | ||
| openFile(filePath); | ||
| }; |
Comment on lines
+1
to
+9
| import { getSessionEntries, resolveSessionPath } from "./session-reader"; | ||
| export { isFilePathReferencedByEntries } from "./session-file-references-core"; | ||
| import { isFilePathReferencedByEntries } from "./session-file-references-core"; | ||
|
|
||
| export async function isFilePathReferencedBySession(filePath: string, sessionId: string | null): Promise<boolean> { | ||
| if (!sessionId) return false; | ||
| try { | ||
| const sessionPath = await resolveSessionPath(sessionId); | ||
| if (!sessionPath) return false; |
Comment on lines
+60
to
+68
| function fileUrlToPath(href: string): string | null { | ||
| try { | ||
| const url = new URL(href); | ||
| if (url.protocol !== "file:") return null; | ||
| return safeDecode(url.pathname); | ||
| } catch { | ||
| return null; | ||
| } | ||
| } |
Comment on lines
+82
to
+86
| if (lowerHref.startsWith("/api/") || lowerHref.startsWith("/_next/")) return null; | ||
| if (/^(https?|mailto|tel|data|blob|about):/i.test(normalizedHref)) return null; | ||
|
|
||
| if (lowerHref.startsWith("file:")) { | ||
| candidate = fileUrlToPath(normalizedHref); |
…wing # Conflicts: # components/MessageView.tsx
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Why
Agents often mention local file paths in their responses. Opening those links directly in the app avoids manual path copying while keeping the file endpoint constrained.
Validation