import ignore from 'ignore'; import { useGit } from '~/lib/hooks/useGit'; import type { Message } from 'ai'; import WithTooltip from '~/components/ui/Tooltip'; import { IGNORE_PATTERNS } from '~/constants/ignorePatterns'; const ig = ignore().add(IGNORE_PATTERNS); const generateId = () => Math.random().toString(36).substring(2, 15); 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'); const message: Message = { role: 'assistant', content: `Cloning the repo ${repoUrl} into ${workdir} ${filePaths .map((filePath) => { const { data: content, encoding } = data[filePath]; if (encoding === 'utf8') { return ` ${content} `; } else if (content instanceof Uint8Array) { return ` ${textDecoder.decode(content)} `; } else { return ''; } }) .join('\n')} `, id: generateId(), createdAt: new Date(), }; console.log(JSON.stringify(message)); importChat(`Git Project:${repoUrl.split('/').slice(-1)[0]}`, [message]); } } }; return ( ); }