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 (
{expanded && hasExpandableContent && (
{filediff && } {content && !filediff && }
)}
) } function fileDiffs(part: SessionMessageAssistantTool): FileDiffInfo[] { if (part.state.status !== 'completed') 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 | 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 = getToolInputPath(input) if (part.name === 'edit' || part.name === 'patch') { const diffs = fileDiffs(part) if (diffs.length === 0) return null const toolName = part.name === 'edit' ? 'Edit' : 'Patch' return ( <> {diffs.map((filediff, index) => ( ))} ) } if (part.name === 'write' && inputPath) { const content = input.content as string | undefined return } if (part.name === 'read' && inputPath) { return } return null }