legends810/testingnew
0
1import type { Message } from 'ai';2import { generateId } from './fileUtils';3import { detectProjectCommands, createCommandsMessage, escapeBoltTags } from './projectCommands';4 5export const createChatFromFolder = async (6 files: File[],7 binaryFiles: string[],8 folderName: string,9): Promise<Message[]> => {10 const fileArtifacts = await Promise.all(11 files.map(async (file) => {12 return new Promise<{ content: string; path: string }>((resolve, reject) => {13 const reader = new FileReader();14 15 reader.onload = () => {16 const content = reader.result as string;17 const relativePath = file.webkitRelativePath.split('/').slice(1).join('/');18 resolve({19 content,20 path: relativePath,21 });22 };23 reader.onerror = reject;24 reader.readAsText(file);25 });26 }),27 );28 29 const commands = await detectProjectCommands(fileArtifacts);30 const commandsMessage = createCommandsMessage(commands);31 32 const binaryFilesMessage =33 binaryFiles.length > 034 ? `\n\nSkipped ${binaryFiles.length} binary files:\n${binaryFiles.map((f) => `- ${f}`).join('\n')}`35 : '';36 37 const filesMessage: Message = {38 role: 'assistant',39 content: `I've imported the contents of the "${folderName}" folder.${binaryFilesMessage}40 41<boltArtifact id="imported-files" title="Imported Files" type="bundled" >42${fileArtifacts43 .map(44 (file) => `<boltAction type="file" filePath="${file.path}">45${escapeBoltTags(file.content)}46</boltAction>`,47 )48 .join('\n\n')}49</boltArtifact>`,50 id: generateId(),51 createdAt: new Date(),52 };53 54 const userMessage: Message = {55 role: 'user',56 id: generateId(),57 content: `Import the "${folderName}" folder`,58 createdAt: new Date(),59 };60 61 const messages = [userMessage, filesMessage];62 63 if (commandsMessage) {64 messages.push({65 role: 'user',66 id: generateId(),67 content: 'Setup the codebase and Start the application',68 });69 messages.push(commandsMessage);70 }71 72 return messages;73};74 