/** * ReadToolViewer * * Renders the Read tool result using CodeBlockViewer. */ import React, { memo } from 'react'; import { CodeBlockViewer, MarkdownViewer } from '@renderer/components/chat/viewers'; import type { LinkedToolItem } from '@renderer/types/groups'; interface ReadToolViewerProps { linkedTool: LinkedToolItem; } export const ReadToolViewer = memo(function ReadToolViewer({ linkedTool }: ReadToolViewerProps) { const filePath = linkedTool.input.file_path as string; // Prefer enriched toolUseResult data const toolUseResult = linkedTool.result?.toolUseResult as Record | undefined; const fileData = toolUseResult?.file as | { content?: string; startLine?: number; totalLines?: number; numLines?: number; } | undefined; // Get content: prefer enriched file data, fall back to raw result content let content: string; if (fileData?.content) { content = fileData.content; } else { const resultContent = linkedTool.result?.content; content = typeof resultContent === 'string' ? resultContent : Array.isArray(resultContent) ? resultContent .map((item: unknown) => (typeof item === 'string' ? item : JSON.stringify(item))) .join('\n') : JSON.stringify(resultContent, null, 2); } // Get line range const startLine = fileData?.startLine ?? (linkedTool.input.offset as number | undefined) ?? 1; const numLinesRead = fileData?.numLines; const limit = linkedTool.input.limit as number | undefined; const endLine = numLinesRead ? startLine + numLinesRead - 1 : limit ? startLine + limit - 1 : undefined; const isMarkdownFile = /\.mdx?$/i.test(filePath); const [viewMode, setViewMode] = React.useState<'code' | 'preview'>( isMarkdownFile ? 'preview' : 'code' ); return (
{isMarkdownFile && (
)} {isMarkdownFile && viewMode === 'preview' ? ( ) : ( )}
); });