import React, { useState } from 'react' import type { FileDiffInfo, SessionMessageAssistantTool } from '@opencode-manager/shared/opencode' import { useSettings } from '@/hooks/useSettings' import { DiffStats } from './DiffStats' import { CodePreview } from './CodePreview' import { ToolContentFiles } from './ToolContentFiles' import { ChevronDown, ChevronUp } from 'lucide-react' function isFileDiff(data: unknown): data is FileDiffInfo { return ( typeof data === 'object' && data !== null && 'file' in data && 'patch' in data && 'additions' in data && 'deletions' in data && typeof (data as FileDiffInfo).file === 'string' && typeof (data as FileDiffInfo).patch === 'string' && typeof (data as FileDiffInfo).additions === 'number' && typeof (data as FileDiffInfo).deletions === 'number' ) } export function getRelativePath(filePath: string): string { const reposIndex = filePath.indexOf('/repos/') if (reposIndex !== -1) { return filePath.substring(reposIndex + 7) } const workspaceIndex = filePath.indexOf('/workspace/') if (workspaceIndex !== -1) { return filePath.substring(workspaceIndex + 11) } if (filePath.startsWith('/Users/') || filePath.startsWith('/home/')) { const parts = filePath.split('/') const lastThree = parts.slice(-3) return lastThree.join('/') } return filePath } function PatchViewer({ patch }: { patch: string }) { const lines = patch.split('\n') return (
{lines.map((line, index) => {
const tone = line.startsWith('@@')
? 'text-info'
: line.startsWith('+++') || line.startsWith('---')
? 'text-muted-foreground'
: line.startsWith('+')
? 'text-diff-add'
: line.startsWith('-')
? 'text-diff-delete'
: 'text-muted-foreground'
return {line || ' '}
})}
)
}
interface FileToolRenderProps {
part: SessionMessageAssistantTool
filediff?: FileDiffInfo
filePath?: string
content?: string
toolName: string
onFileClick?: (filePath: string, lineNumber?: number) => void
}
function FileToolRender({ part, filediff, filePath, content, toolName, onFileClick }: FileToolRenderProps) {
const { preferences } = useSettings()
const isReadTool = toolName === 'Read'
const isEditTool = toolName === 'Edit'
const isWriteTool = toolName === 'Write'
const hasExpandableContent = !isReadTool && (filediff || content)
const isFileMutatingTool = isEditTool || isWriteTool
const defaultExpanded = isFileMutatingTool
? (preferences?.expandDiffs ?? true)
: (preferences?.expandToolCalls ?? false)
const [expanded, setExpanded] = useState(defaultExpanded)
const getDuration = () => {
if (part.time.ran === undefined || part.time.completed === undefined) return ''
return ((part.time.completed - part.time.ran) / 1000).toFixed(2) + 's'
}
const handleFileClick = (e: React.MouseEvent) => {
e.stopPropagation()
if (onFileClick && filePath) {
onFileClick(filePath)
}
}
return (