fix(files): resolve workspace paths and add sandboxed HTML preview (#367)

* fix(files): resolve workspace paths and add sandboxed HTML preview

Accept workspace-absolute paths in the file service and return repo-relative paths for file reads and directory listings. Serve raw content under a CSP sandbox and preview HTML files in a sandboxed iframe with a raw toggle.

Resolve file-click paths from the session directory and repo local path, detect absolute and html/htm file references while ignoring URLs, accept the legacy filePath input key and filediff metadata, fix the file browser navigating into an initially selected directory, and use focus-visible rings across shared UI primitives.

* fix(files): address review feedback on navigation and path fallback

Key initial directory navigation to the initial selection so viewport changes do not reset it, and open the mobile preview from a separate effect.

Fall back to the legacy filePath input when path is empty, and cover both with regression tests.
This commit is contained in:
Chris Scott authored and GitHub committed 2026-09-28 22:27:18 -04:00
1 parent 5bd688de4b
commit 68b13c8e35
19 files changed
+307 -58

No files matched your search

@@ -134,16 +134,24 @@ function FileToolRender({ part, filediff, filePath, content, toolName, onFileCli
function fileDiffs(part: SessionMessageAssistantTool): FileDiffInfo[] {
if (part.state.status !== 'completed') return []
const files = part.state.metadata?.files
if (!Array.isArray(files)) return []
const metadata = part.state.metadata
const files: unknown[] = Array.isArray(metadata?.files) ? metadata.files : [metadata?.filediff]
return files.filter(isFileDiff)
}
/** Returns the file path a file tool was called with, accepting both the `path` and legacy `filePath` input keys. */
export function getToolInputPath(input: Record<string, unknown> | undefined): string | undefined {
const path = input?.path
if (typeof path === 'string' && path) return path
const filePath = input?.filePath
return typeof filePath === 'string' && filePath ? filePath : undefined
}
export function getToolSpecificRender(part: SessionMessageAssistantTool, onFileClick?: (filePath: string) => void): React.ReactElement | null {
if (part.state.status !== 'completed') return null
const input = part.state.input
const inputPath = typeof input.path === 'string' ? input.path : undefined
const inputPath = getToolInputPath(input)
if (part.name === 'edit' || part.name === 'patch') {
const diffs = fileDiffs(part)
@@ -377,6 +377,54 @@ describe('MessagePart', () => {
expect(screen.getByText('/test/file.txt')).toBeInTheDocument()
})
it('makes legacy filePath read, write, and filediff edit calls clickable', () => {
const onFileClick = vi.fn()
const legacy = (name: string, metadata: Record<string, unknown>) =>
toolPart(name, {
status: 'completed',
input: { filePath: `/test/${name}.html` },
content: [{ type: 'text', text: 'ok' }],
metadata,
})
renderWithProviders(
<>
<MessagePart part={legacy('read', {})} onFileClick={onFileClick} />
<MessagePart part={legacy('write', {})} onFileClick={onFileClick} />
<MessagePart
part={legacy('edit', { filediff: { file: '/test/edit.html', patch: '@@ -1 +1 @@\n-a\n+b', additions: 1, deletions: 1 } })}
onFileClick={onFileClick}
/>
</>,
)
fireEvent.click(screen.getByText('/test/read.html'))
fireEvent.click(screen.getByText('/test/write.html'))
fireEvent.click(screen.getByText('/test/edit.html'))
expect(onFileClick.mock.calls.map(([filePath]) => filePath)).toEqual(['/test/read.html', '/test/write.html', '/test/edit.html'])
expect(screen.getByText('+1')).toBeInTheDocument()
})
it('falls back to filePath when path is empty', () => {
const onFileClick = vi.fn()
renderWithProviders(
<MessagePart
part={toolPart('read', {
status: 'completed',
input: { path: '', filePath: '/test/fallback.html' },
content: [{ type: 'text', text: 'ok' }],
metadata: {},
})}
onFileClick={onFileClick}
/>,
)
fireEvent.click(screen.getByText('/test/fallback.html'))
expect(onFileClick).toHaveBeenCalledWith('/test/fallback.html')
})
it('renders every file of a multi-file patch with per-file links', () => {
const onFileClick = vi.fn()
renderWithProviders(
@@ -9,7 +9,7 @@ import { detectFileReferences } from '@/lib/fileReferences'
import { ExternalLink, Loader2, Shield } from 'lucide-react'
import { Badge } from '@/components/ui/badge'
import { CopyButton } from '@/components/ui/copy-button'
import { getToolSpecificRender } from './FileToolRender'
import { getToolInputPath, getToolSpecificRender } from './FileToolRender'
const DISPLAY_LIMIT = 30_000
const DISPLAY_HEAD_LENGTH = 20_000
@@ -174,7 +174,7 @@ export const ToolCallPart = memo(function ToolCallPart({ part, messageID, onFile
case 'write':
case 'edit':
case 'patch':
return (input.path as string) || null
return getToolInputPath(input) ?? null
case 'shell':
return displayCommand || null
case 'glob':