"use client"; import { useState, memo } from "react"; import { IconCheck, IconX, IconEdit, IconFilePlus, IconExternalLink, IconCopy, } from "@tabler/icons-react"; import { GridSpinner } from "@/components/grid-spinner"; import { transformPathsInText } from "@/lib/utils"; import type { Message } from "@/lib/types/http"; import { DiffViewBlock } from "./diff-view-block"; import { ExpandableRow } from "./expandable-row"; import { transformFileMutation, type FileMutation } from "@/lib/diff"; import { useExpandState } from "./use-expand-state"; import { useOpenFileAtLine } from "@/hooks/use-file-editors"; type ModifyFilePayload = { file_path?: string; mutations?: FileMutation[]; }; type ToolEditMetadata = { tool_call_id?: string; status?: "pending" | "running" | "complete" | "error"; normalized?: { modify_file?: ModifyFilePayload }; }; type ToolEditMessageProps = { comment: Message; worktreePath?: string; onOpenFile?: (path: string) => void; }; // EditStatusIcon renders the status glyph (complete/error/running) for an edit card. function EditStatusIcon({ status }: { status: string | undefined }) { if (status === "complete") return ; if (status === "error") return ; if (status === "running") return ; return null; } // getEditSummary returns the short header label for an edit/write card. function getEditSummary( content: string, worktreePath: string | undefined, isWriteOperation: boolean, lineCount: number, ): string { const baseSummary = transformPathsInText(content, worktreePath); if (isWriteOperation && lineCount > 0) { return `${baseSummary} (${lineCount} line${lineCount !== 1 ? "s" : ""})`; } return baseSummary; } type FileActionButtonProps = { filePath: string; worktreePath: string | undefined; onOpenFile: ((path: string) => void) | undefined; copied: boolean; onCopyPath: (e: React.MouseEvent) => void; }; // FileActionButton renders the file path with an optional open-in-editor action. function FileActionButton({ filePath, worktreePath, onOpenFile, copied, onCopyPath, }: FileActionButtonProps) { const isFileInWorktree = worktreePath && filePath.startsWith(worktreePath); if (onOpenFile && isFileInWorktree) { return ( ); } if (!isFileInWorktree) { return ( ); } return null; } type EditExpandedContentProps = { diffData: import("@/lib/diff/types").FileDiffData | null; writeContent: string | undefined; }; // EditExpandedContent renders the expanded body of an edit card: a diff or write content. function EditExpandedContent({ diffData, writeContent }: EditExpandedContentProps) { if (diffData?.diff) return ; if (writeContent) { return (
          {writeContent}
        
); } return null; } // parseEditMetadata pulls the edit status, file path, first-changed line, // diff/write content, and expandability out of a tool_edit message's metadata. function parseEditMetadata(comment: Message) { const metadata = comment.metadata as ToolEditMetadata | undefined; const status = metadata?.status; const modifyFile = metadata?.normalized?.modify_file; const filePath = modifyFile?.file_path; const mutation = modifyFile?.mutations?.[0]; const writeContent = mutation?.content; const isWriteOperation = mutation?.type === "create"; const diffData = filePath && mutation ? transformFileMutation(filePath, mutation) : null; const hasExpandableContent = !!(diffData?.diff || writeContent); const isSuccess = status === "complete"; const lineCount = writeContent ? writeContent.split("\n").length : 0; return { status, filePath, startLine: mutation?.start_line, writeContent, isWriteOperation, diffData, hasExpandableContent, isSuccess, lineCount, }; } // ToolEditMessage renders an edit/write tool call: the file link and expandable diff. export const ToolEditMessage = memo(function ToolEditMessage({ comment, worktreePath, onOpenFile, }: ToolEditMessageProps) { const { status, filePath, startLine, writeContent, isWriteOperation, diffData, hasExpandableContent, isSuccess, lineCount, } = parseEditMetadata(comment); const [copied, setCopied] = useState(false); const autoExpanded = status === "running"; const { isExpanded, handleToggle } = useExpandState(status, autoExpanded); const Icon = isWriteOperation ? IconFilePlus : IconEdit; const summary = getEditSummary(comment.content, worktreePath, isWriteOperation, lineCount); const handleCopyPath = (e: React.MouseEvent) => { e.stopPropagation(); if (filePath) { navigator.clipboard?.writeText(filePath); setCopied(true); setTimeout(() => setCopied(false), 1500); } }; const handleOpenFile = useOpenFileAtLine(onOpenFile, startLine, worktreePath); return ( } header={
{summary} {!isSuccess && } {filePath && ( )}
} hasExpandableContent={hasExpandableContent} isExpanded={isExpanded} onToggle={handleToggle} >
); });