fix(artifacts): render artifacts from the checked path and serve raw files once

- show_artifact returns the validated path as tool metadata; the chat card
  renders from it and maps repo paths with getWorkspaceFilePath, so local
  repos and moved sessions load the right file
- serve .htm as text/html
- raw file route stats and reads once and adds ETag/Last-Modified with 304
- reuse isPathWithinRoot from utils/fs-safe
- move readShowArtifactCall to frontend/src/lib/artifactTool
This commit is contained in:
Chris Scott committed 2026-10-12 16:43:04 -04:00
1 parent f7b2ffb865
commit 5e2452172c
26 files changed
+489 -58

No files matched your search

@@ -11,6 +11,8 @@ interface ExplorationGroupProps {
parts: SessionMessageAssistantTool[]
messageID: string
directory?: string
repoFullPath?: string
repoLocalPath?: string
onFileClick?: (filePath: string, lineNumber?: number) => void
onChildSessionClick?: (sessionId: string) => void
onOpenWalkthrough?: (source?: WalkthroughSource) => void
@@ -326,6 +326,34 @@ describe('MessagePart', () => {
expect(iframe.tagName).toBe('IFRAME')
expect(iframe).toHaveAttribute('src', getFileApiUrl('/tmp/artifacts/report.html', { params: { raw: true } }))
})
it('passes the resolved metadata path and repo mapping through to the artifact frame', () => {
setupSettings({
simpleChatMode: true,
showReasoning: false,
expandToolCalls: false,
expandDiffs: true,
})
renderWithProviders(
<MessagePart
part={toolPart('ocm', {
status: 'completed',
input: { action: 'show_artifact', params: { title: 'Report', path: 'reports/summary.html' } },
content: [{ type: 'text', text: 'Artifact shown' }],
metadata: { path: '/workspace/repos/assistant/reports/summary.html' },
})}
messageID="msg_1"
directory="/workspace/repos/assistant"
repoFullPath="/workspace/repos/assistant"
repoLocalPath="assistant"
/>,
)
const iframe = screen.getByTitle('Report')
expect(iframe.tagName).toBe('IFRAME')
expect(iframe).toHaveAttribute('src', getFileApiUrl('assistant/reports/summary.html', { params: { raw: true } }))
})
})
describe('sandbox indicator', () => {
@@ -1,7 +1,8 @@
import { memo } from 'react'
import { readShowArtifactCall, type SessionMessageAssistant } from '@opencode-manager/shared/opencode'
import type { SessionMessageAssistant } from '@opencode-manager/shared/opencode'
import { TextPart } from './TextPart'
import { ToolCallPart } from './ToolCallPart'
import { readShowArtifactCall } from '@/lib/artifactTool'
import { useSettings } from '@/hooks/useSettings'
import type { ShellNoticeOutcome } from '@/lib/backgroundWork'
import type { WalkthroughSource } from '@opencode-manager/shared/schemas'
@@ -12,13 +13,15 @@ interface MessagePartProps {
part: AssistantContentPart
messageID?: string
directory?: string
repoFullPath?: string
repoLocalPath?: string
shellOutcome?: ShellNoticeOutcome
onFileClick?: (filePath: string, lineNumber?: number) => void
onChildSessionClick?: (sessionId: string) => void
onOpenWalkthrough?: (source?: WalkthroughSource) => void
}
export const MessagePart = memo(function MessagePart({ part, messageID, directory, shellOutcome, onFileClick, onChildSessionClick, onOpenWalkthrough }: MessagePartProps) {
export const MessagePart = memo(function MessagePart({ part, messageID, directory, repoFullPath, repoLocalPath, shellOutcome, onFileClick, onChildSessionClick, onOpenWalkthrough }: MessagePartProps) {
const { preferences } = useSettings()
const simpleChatMode = preferences?.simpleChatMode ?? false
const showReasoning = preferences?.showReasoning ?? false
@@ -46,6 +49,8 @@ export const MessagePart = memo(function MessagePart({ part, messageID, director
part={part}
messageID={messageID}
directory={directory}
repoFullPath={repoFullPath}
repoLocalPath={repoLocalPath}
shellOutcome={shellOutcome}
onFileClick={onFileClick}
onChildSessionClick={onChildSessionClick}
@@ -1,6 +1,6 @@
import { memo, useCallback, useMemo, useState } from 'react'
import { Pencil, Loader2 } from 'lucide-react'
import { assistantText, formatOpenCodeModelRef, readShowArtifactCall } from '@opencode-manager/shared/opencode'
import { assistantText, formatOpenCodeModelRef } from '@opencode-manager/shared/opencode'
import type {
PromptAgentAttachment,
PromptFileAttachment,
@@ -23,6 +23,7 @@ import { useSettings } from '@/hooks/useSettings'
import { CopyButton } from '@/components/ui/copy-button'
import { TTSButton } from '@/components/ui/tts-button'
import { backgroundShellID, collectBackgroundParts, type ShellNoticeOutcome } from '@/lib/backgroundWork'
import { readShowArtifactCall } from '@/lib/artifactTool'
import { groupExplorationParts, type AssistantContentItem } from '@/lib/explorationGroups'
import type { WalkthroughSource } from '@opencode-manager/shared/schemas'
import { ExplorationGroup } from './ExplorationGroup'
@@ -329,6 +330,8 @@ interface MessageRowProps {
handleCancelEdit: () => void
sessionID: string
directory?: string
repoFullPath?: string
repoLocalPath?: string
onFileClick?: (filePath: string, lineNumber?: number) => void
onChildSessionClick?: (sessionId: string) => void
onOpenWalkthrough?: (source?: WalkthroughSource) => void
@@ -350,6 +353,8 @@ const MessageRow = memo(function MessageRow({
handleCancelEdit,
sessionID,
directory,
repoFullPath,
repoLocalPath,
onFileClick,
onChildSessionClick,
onOpenWalkthrough,
@@ -458,6 +463,8 @@ const MessageRow = memo(function MessageRow({
part={part}
messageID={message.id}
directory={directory}
repoFullPath={repoFullPath}
repoLocalPath={repoLocalPath}
shellOutcome={shellOutcomeFor(part)}
onFileClick={onFileClick}
onChildSessionClick={onChildSessionClick}
@@ -502,6 +509,8 @@ const MessageRow = memo(function MessageRow({
parts={item.parts}
messageID={message.id}
directory={directory}
repoFullPath={repoFullPath}
repoLocalPath={repoLocalPath}
onFileClick={onFileClick}
onChildSessionClick={onChildSessionClick}
onOpenWalkthrough={onOpenWalkthrough}
@@ -511,6 +520,8 @@ const MessageRow = memo(function MessageRow({
part={item.part}
messageID={message.id}
directory={directory}
repoFullPath={repoFullPath}
repoLocalPath={repoLocalPath}
shellOutcome={shellOutcomeFor(item.part)}
onFileClick={onFileClick}
onChildSessionClick={onChildSessionClick}
@@ -578,6 +589,8 @@ const MessageRow = memo(function MessageRow({
interface MessageThreadProps {
sessionID: string
directory?: string
repoFullPath?: string
repoLocalPath?: string
messages: SessionMessageInfo[]
pending: SessionInboxInfo[]
onFileClick?: (filePath: string, lineNumber?: number) => void
@@ -591,6 +604,8 @@ interface MessageThreadProps {
export const MessageThread = memo(function MessageThread({
sessionID,
directory,
repoFullPath,
repoLocalPath,
messages,
pending,
onFileClick,
@@ -661,6 +676,8 @@ export const MessageThread = memo(function MessageThread({
handleCancelEdit={handleCancelEdit}
sessionID={sessionID}
directory={directory}
repoFullPath={repoFullPath}
repoLocalPath={repoLocalPath}
onFileClick={onFileClick}
onChildSessionClick={onChildSessionClick}
onOpenWalkthrough={onOpenWalkthrough}
@@ -395,7 +395,7 @@ describe('ToolCallPart show artifact action', () => {
mocks.useToolCallPermission.mockReturnValue(null)
})
const showArtifact = (params: unknown): SessionMessageAssistantTool => ({
const showArtifact = (params: unknown, metadata?: Record<string, string>): SessionMessageAssistantTool => ({
type: 'tool',
id: 'call_artifact',
name: 'ocm',
@@ -404,6 +404,7 @@ describe('ToolCallPart show artifact action', () => {
status: 'completed',
input: { action: 'show_artifact', params },
content: [{ type: 'text', text: 'Artifact shown' }],
metadata,
},
})
@@ -433,6 +434,40 @@ describe('ToolCallPart show artifact action', () => {
expect(link).toHaveAttribute('rel', 'noopener noreferrer')
})
it('prefers the backend-resolved metadata path over the directory join', () => {
const src = getFileApiUrl('/workspace/sessions/ses_1/report.html', { params: { raw: true } })
renderWithProviders(
<ToolCallPart
part={showArtifact(
{ title: 'Report', path: 'reports/summary.html' },
{ path: '/workspace/sessions/ses_1/report.html' },
)}
messageID="msg_1"
directory="/repo"
/>,
)
expect(screen.getByTitle('Report')).toHaveAttribute('src', src)
expect(screen.getByText('reports/summary.html')).toHaveAttribute('title', 'reports/summary.html')
})
it('maps a repo path outside the workspace to the repo-local path', () => {
const src = getFileApiUrl('assistant/reports/summary.html', { params: { raw: true } })
renderWithProviders(
<ToolCallPart
part={showArtifact({ title: 'Report', path: 'reports/summary.html' })}
messageID="msg_1"
directory="/workspace/repos/assistant"
repoFullPath="/workspace/repos/assistant"
repoLocalPath="assistant"
/>,
)
expect(screen.getByTitle('Report')).toHaveAttribute('src', src)
})
it('resolves an absolute artifact path without a session directory', () => {
const src = getFileApiUrl('/tmp/artifacts/report.html', { params: { raw: true } })
@@ -1,6 +1,6 @@
import { useState, useRef, useEffect, useMemo, memo } from 'react'
import { unwrapSandboxExecCommand } from '@opencode-manager/shared/utils'
import { readShowArtifactCall, toolContentText, type SessionMessageAssistantTool } from '@opencode-manager/shared/opencode'
import { toolContentText, type SessionMessageAssistantTool } from '@opencode-manager/shared/opencode'
import { useSettings } from '@/hooks/useSettings'
import { useUserBash } from '@/stores/userBashStore'
import { useChildLifecycleForSession } from '@/stores/sessionStatusStore'
@@ -28,6 +28,8 @@ import {
} from '@/lib/backgroundWork'
import { getToolInputPath, getToolSpecificRender } from './FileToolRender'
import { readOpenWalkthroughCall } from '@/lib/walkthroughTool'
import { readShowArtifactCall } from '@/lib/artifactTool'
import { getWorkspaceFilePath } from '@/lib/markdownLinks'
import { getFileApiUrl } from '@/api/files'
import { ArtifactPart } from './ArtifactPart'
import type { WalkthroughSource } from '@opencode-manager/shared/schemas'
@@ -63,6 +65,8 @@ interface ToolCallPartProps {
part: SessionMessageAssistantTool
messageID?: string
directory?: string
repoFullPath?: string
repoLocalPath?: string
shellOutcome?: ShellNoticeOutcome
onFileClick?: (filePath: string, lineNumber?: number) => void
onChildSessionClick?: (sessionId: string) => void
@@ -119,7 +123,7 @@ function ClickableJson({ json, onFileClick }: { json: unknown; onFileClick?: (fi
return <pre className="bg-accent p-2 rounded text-xs overflow-x-auto whitespace-pre-wrap break-words">{parts}</pre>
}
export const ToolCallPart = memo(function ToolCallPart({ part, messageID, directory, shellOutcome, onFileClick, onChildSessionClick, onOpenWalkthrough }: ToolCallPartProps) {
export const ToolCallPart = memo(function ToolCallPart({ part, messageID, directory, repoFullPath, repoLocalPath, shellOutcome, onFileClick, onChildSessionClick, onOpenWalkthrough }: ToolCallPartProps) {
const { preferences } = useSettings()
const { userBashCommands } = useUserBash()
const isSubagent = part.name === 'subagent'
@@ -254,11 +258,13 @@ export const ToolCallPart = memo(function ToolCallPart({ part, messageID, direct
) : null
const artifact = readShowArtifactCall(part)
if (artifact && (artifact.path.startsWith('/') || directory)) {
const fullPath = artifact.path.startsWith('/')
? artifact.path
: `${directory?.replace(/\/+$/, '')}/${artifact.path}`
return <ArtifactPart title={artifact.title} path={artifact.path} src={getFileApiUrl(fullPath, { params: { raw: true } })} />
if (artifact && (artifact.resolvedPath || artifact.path.startsWith('/') || directory)) {
const servedPath = getWorkspaceFilePath(artifact.resolvedPath ?? artifact.path, {
directory,
repoFullPath,
repoLocalPath,
})
return <ArtifactPart title={artifact.title} path={artifact.path} src={getFileApiUrl(servedPath, { params: { raw: true } })} />
}
if (isSubagent) {