"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"; 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; }; function EditStatusIcon({ status }: { status: string | undefined }) { if (status === "complete") return ; if (status === "error") return ; if (status === "running") return ; return null; } 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; }; 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; }; function EditExpandedContent({ diffData, writeContent }: EditExpandedContentProps) { if (diffData?.diff) return ; if (writeContent) { return (
          {writeContent}
        
); } return null; } 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, writeContent, isWriteOperation, diffData, hasExpandableContent, isSuccess, lineCount, }; } export const ToolEditMessage = memo(function ToolEditMessage({ comment, worktreePath, onOpenFile, }: ToolEditMessageProps) { const { status, filePath, 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); } }; return ( } header={
{summary} {!isSuccess && } {filePath && ( )}
} hasExpandableContent={hasExpandableContent} isExpanded={isExpanded} onToggle={handleToggle} >
); });