import type { Message } from 'ai'; import { generateId } from './fileUtils'; import { detectProjectCommands, createCommandsMessage } from './projectCommands'; export const createChatFromFolder = async ( files: File[], binaryFiles: string[], folderName: string, ): Promise => { const fileArtifacts = await Promise.all( files.map(async (file) => { return new Promise<{ content: string; path: string }>((resolve, reject) => { const reader = new FileReader(); reader.onload = () => { const content = reader.result as string; const relativePath = file.webkitRelativePath.split('/').slice(1).join('/'); resolve({ content, path: relativePath, }); }; reader.onerror = reject; reader.readAsText(file); }); }), ); const commands = await detectProjectCommands(fileArtifacts); const commandsMessage = createCommandsMessage(commands); const binaryFilesMessage = binaryFiles.length > 0 ? `\n\nSkipped ${binaryFiles.length} binary files:\n${binaryFiles.map((f) => `- ${f}`).join('\n')}` : ''; const filesMessage: Message = { role: 'assistant', content: `I've imported the contents of the "${folderName}" folder.${binaryFilesMessage} ${fileArtifacts .map( (file) => ` ${file.content} `, ) .join('\n\n')} `, id: generateId(), createdAt: new Date(), }; const userMessage: Message = { role: 'user', id: generateId(), content: `Import the "${folderName}" folder`, createdAt: new Date(), }; const messages = [userMessage, filesMessage]; if (commandsMessage) { messages.push(commandsMessage); } return messages; };