import ignore from 'ignore'; import { useGit } from '~/lib/hooks/useGit'; import type { Message } from 'ai'; import { detectProjectCommands, createCommandsMessage } from '~/utils/projectCommands'; import { generateId } from '~/utils/fileUtils'; const IGNORE_PATTERNS = [ 'node_modules/**', '.git/**', '.github/**', '.vscode/**', '**/*.jpg', '**/*.jpeg', '**/*.png', 'dist/**', 'build/**', '.next/**', 'coverage/**', '.cache/**', '.vscode/**', '.idea/**', '**/*.log', '**/.DS_Store', '**/npm-debug.log*', '**/yarn-debug.log*', '**/yarn-error.log*', '**/*lock.json', '**/*lock.yaml', ]; const ig = ignore().add(IGNORE_PATTERNS); interface GitCloneButtonProps { className?: string; importChat?: (description: string, messages: Message[]) => Promise; } export default function GitCloneButton({ importChat }: GitCloneButtonProps) { const { ready, gitClone } = useGit(); const onClick = async (_e: any) => { if (!ready) { return; } const repoUrl = prompt('Enter the Git url'); if (repoUrl) { const { workdir, data } = await gitClone(repoUrl); if (importChat) { const filePaths = Object.keys(data).filter((filePath) => !ig.ignores(filePath)); console.log(filePaths); const textDecoder = new TextDecoder('utf-8'); // Convert files to common format for command detection const fileContents = filePaths .map((filePath) => { const { data: content, encoding } = data[filePath]; return { path: filePath, content: encoding === 'utf8' ? content : content instanceof Uint8Array ? textDecoder.decode(content) : '', }; }) .filter((f) => f.content); // Detect and create commands message const commands = await detectProjectCommands(fileContents); const commandsMessage = createCommandsMessage(commands); // Create files message const filesMessage: Message = { role: 'assistant', content: `Cloning the repo ${repoUrl} into ${workdir} ${fileContents .map( (file) => ` ${file.content} `, ) .join('\n')} `, id: generateId(), createdAt: new Date(), }; const messages = [filesMessage]; if (commandsMessage) { messages.push(commandsMessage); } await importChat(`Git Project:${repoUrl.split('/').slice(-1)[0]}`, messages); } } }; return ( ); }