diff --git a/src/components/ai/AssistantContent.tsx b/src/components/ai/AssistantContent.tsx index a5b0b58b..02c96463 100644 --- a/src/components/ai/AssistantContent.tsx +++ b/src/components/ai/AssistantContent.tsx @@ -9,6 +9,7 @@ import { FileViewer } from '@/components/ai/FileViewer'; import { FileWriter } from '@/components/ai/FileWriter'; import { ShellCommand } from '@/components/ai/ShellCommand'; import { PackageManager } from '@/components/ai/PackageManager'; +import { GitCommit } from '@/components/ai/GitCommit'; import type { CoreAssistantMessage, CoreToolMessage } from 'ai'; interface AssistantContentProps { @@ -312,6 +313,21 @@ export const AssistantContent = memo(({ content, toolResults = [] }: AssistantCo ); } + // Special rendering for git_commit tool + if (item.toolName === 'git_commit' && item.args) { + const { message } = item.args as { message: string }; + const { result, isError } = extractToolResult(item.toolCallId); + + return ( + + ); + } + // Default tool rendering for other tools return ( diff --git a/src/components/ai/GitCommit.test.tsx b/src/components/ai/GitCommit.test.tsx new file mode 100644 index 00000000..c0905bc4 --- /dev/null +++ b/src/components/ai/GitCommit.test.tsx @@ -0,0 +1,113 @@ +import { describe, it, expect } from 'vitest'; +import { render, screen } from '@testing-library/react'; +import { TestApp } from '@/test/TestApp'; +import { GitCommit } from './GitCommit'; + +describe('GitCommit', () => { + it('renders commit message and shows committing state', () => { + render( + + + + ); + + expect(screen.getByText('feat: add new feature')).toBeInTheDocument(); + expect(screen.getByText('Committing')).toBeInTheDocument(); + expect(screen.getByText('Git commit')).toBeInTheDocument(); + }); + + it('shows success state with commit details', () => { + const result = `✅ Successfully committed 3 files (abc1234) + +📝 Commit: "feat: add new feature" +🔗 Hash: abc1234 +🌿 Branch: main +📊 Changes: + • 2 files added + • 1 file modified`; + + render( + + + + ); + + expect(screen.getByText('Committed')).toBeInTheDocument(); + expect(screen.getByText('Successfully committed 3 files')).toBeInTheDocument(); + expect(screen.getByText('abc1234')).toBeInTheDocument(); + expect(screen.getByText('2 added')).toBeInTheDocument(); + expect(screen.getByText('1 modified')).toBeInTheDocument(); + }); + + it('shows error state', () => { + render( + + + + ); + + expect(screen.getByText('Failed')).toBeInTheDocument(); + expect(screen.getByText('fix: broken feature')).toBeInTheDocument(); + }); + + it('shows only first line of multi-line commit messages and provides expandable full message', () => { + const multiLineMessage = "feat: add new feature\n\nThis is a detailed description\nof the new feature implementation"; + + render( + + + + ); + + expect(screen.getByText('feat: add new feature')).toBeInTheDocument(); + expect(screen.queryByText('This is a detailed description')).not.toBeInTheDocument(); + expect(screen.getByText('View full commit message')).toBeInTheDocument(); + }); + + it('does not show expandable section for single-line commit messages', () => { + const singleLineMessage = "feat: add new feature"; + + render( + + + + ); + + expect(screen.getByText('feat: add new feature')).toBeInTheDocument(); + expect(screen.queryByText('View full commit message')).not.toBeInTheDocument(); + }); + + it('shows appropriate emoji for different commit types', () => { + const { rerender } = render( + + + + ); + + expect(screen.getByText('✨')).toBeInTheDocument(); + + rerender( + + + + ); + + expect(screen.getByText('🐛')).toBeInTheDocument(); + + rerender( + + + + ); + + expect(screen.getByText('📚')).toBeInTheDocument(); + }); +}); \ No newline at end of file diff --git a/src/components/ai/GitCommit.tsx b/src/components/ai/GitCommit.tsx new file mode 100644 index 00000000..31a2759b --- /dev/null +++ b/src/components/ai/GitCommit.tsx @@ -0,0 +1,214 @@ +import { Card, CardContent, CardHeader } from '@/components/ui/card'; +import { Badge } from '@/components/ui/badge'; +import { GitCommit as GitCommitIcon, CheckCircle, XCircle, GitBranch, Plus, Minus, Edit } from 'lucide-react'; +import { cn } from '@/lib/utils'; + +interface GitCommitProps { + message: string; + result?: string; + isError?: boolean; + className?: string; +} + +export function GitCommit({ + message, + result, + isError = false, + className +}: GitCommitProps) { + // Parse the git commit result for useful information + const parseCommitResult = (output: string) => { + if (!output) return { summary: '', stats: null }; + + // Extract commit hash + const hashMatch = output.match(/🔗 Hash: ([a-f0-9]{7})/); + const hash = hashMatch ? hashMatch[1] : null; + + // Extract branch + const branchMatch = output.match(/🌿 Branch: (.+)/); + const branch = branchMatch ? branchMatch[1] : null; + + // Extract file statistics + const addedMatch = output.match(/• (\d+) files? added/); + const modifiedMatch = output.match(/• (\d+) files? modified/); + const deletedMatch = output.match(/• (\d+) files? deleted/); + + const stats = { + added: addedMatch ? parseInt(addedMatch[1]) : 0, + modified: modifiedMatch ? parseInt(modifiedMatch[1]) : 0, + deleted: deletedMatch ? parseInt(deletedMatch[1]) : 0, + hash, + branch + }; + + // Extract summary line without hash + const summaryMatch = output.match(/✅ Successfully committed (\d+) files?/); + const summary = summaryMatch ? `Successfully committed ${summaryMatch[1]} file${summaryMatch[1] !== '1' ? 's' : ''}` : ''; + + return { summary, stats }; + }; + + const { summary, stats } = parseCommitResult(result || ''); + + // Get first line of commit message + const getFirstLine = (msg: string) => { + return msg.split('\n')[0]; + }; + + // Get emoji for commit type based on conventional commits + const getCommitEmoji = (msg: string) => { + const lowerMsg = msg.toLowerCase(); + if (lowerMsg.startsWith('feat')) return '✨'; + if (lowerMsg.startsWith('fix')) return '🐛'; + if (lowerMsg.startsWith('docs')) return '📚'; + if (lowerMsg.startsWith('style')) return '💄'; + if (lowerMsg.startsWith('refactor')) return '♻️'; + if (lowerMsg.startsWith('perf')) return '⚡'; + if (lowerMsg.startsWith('test')) return '✅'; + if (lowerMsg.startsWith('build')) return '👷'; + if (lowerMsg.startsWith('ci')) return '💚'; + if (lowerMsg.startsWith('chore')) return '🔧'; + if (lowerMsg.startsWith('revert')) return '⏪'; + if (lowerMsg.startsWith('merge')) return '🔀'; + if (lowerMsg.startsWith('init')) return '🎉'; + if (lowerMsg.startsWith('add')) return '➕'; + if (lowerMsg.startsWith('remove') || lowerMsg.startsWith('delete')) return '➖'; + if (lowerMsg.startsWith('update')) return '⬆️'; + return '📝'; + }; + + const firstLine = getFirstLine(message); + const commitEmoji = getCommitEmoji(message); + + return ( + + + + + + + {commitEmoji} + + + + {firstLine} + + + Git commit + {stats?.branch && ( + + + {stats.branch} + + )} + {stats?.hash && ( + + {stats.hash} + + )} + + + + + {result !== undefined ? ( + isError ? ( + + + Failed + + ) : ( + + + Committed + + ) + ) : ( + + + Committing + + )} + + + + + {/* Full commit message - only show if multi-line */} + {message.includes('\n') && ( + + + + ▶ + View full commit message + + + + + {message} + + + + + + )} + + {result && ( + + {/* Summary */} + {summary && ( + + {summary} + + )} + + {/* File changes summary */} + {stats && !isError && (stats.added > 0 || stats.modified > 0 || stats.deleted > 0) && ( + + {stats.added > 0 && ( + + + {stats.added} added + + )} + {stats.modified > 0 && ( + + + {stats.modified} modified + + )} + {stats.deleted > 0 && ( + + + {stats.deleted} deleted + + )} + + )} + + {/* Commit info footer */} + + + + Git repository + + {stats?.branch && ( + + + {stats.branch} branch + + )} + {!isError && ( + + Changes committed + + )} + + + )} + + ); +} \ No newline at end of file
+ {message} +