import ignore from 'ignore'; import type { ProviderInfo } from '~/types/model'; import type { Template } from '~/types/template'; import { STARTER_TEMPLATES } from './constants'; const starterTemplateSelectionPrompt = (templates: Template[]) => ` You are an experienced developer who helps people choose the best starter template for their projects. Available templates: ${templates .map( (template) => ` `, ) .join('\n')} Response Format: {selected template name} {a proper title for the project} Examples: User: I need to build a todo app Response: react-basic-starter Simple React todo application User: Write a script to generate numbers from 1 to 100 Response: blank script to generate numbers from 1 to 100 Instructions: 1. For trivial tasks and simple scripts, always recommend the blank template 2. For more complex projects, recommend templates from the provided list 3. Follow the exact XML format 4. Consider both technical requirements and tags 5. If no perfect match exists, recommend the closest option Important: Provide only the selection tags in your response, no additional text. MOST IMPORTANT: YOU DONT HAVE TIME TO THINK JUST START RESPONDING BASED ON HUNCH `; const templates: Template[] = STARTER_TEMPLATES.filter((t) => !t.name.includes('shadcn')); const parseSelectedTemplate = (llmOutput: string): { template: string; title: string } | null => { try { // Extract content between tags const templateNameMatch = llmOutput.match(/(.*?)<\/templateName>/); const titleMatch = llmOutput.match(/(.*?)<\/title>/); if (!templateNameMatch) { return null; } return { template: templateNameMatch[1].trim(), title: titleMatch?.[1].trim() || 'Untitled Project' }; } catch (error) { console.error('Error parsing template selection:', error); return null; } }; export const selectStarterTemplate = async (options: { message: string; model: string; provider: ProviderInfo }) => { const { message, model, provider } = options; const requestBody = { message, model, provider, system: starterTemplateSelectionPrompt(templates), }; const response = await fetch('/api/llmcall', { method: 'POST', body: JSON.stringify(requestBody), }); const respJson: { text: string } = await response.json(); console.log(respJson); const { text } = respJson; const selectedTemplate = parseSelectedTemplate(text); if (selectedTemplate) { return selectedTemplate; } else { console.log('No template selected, using blank template'); return { template: 'blank', title: '', }; } }; const getGitHubRepoContent = async (repoName: string): Promise<{ name: string; path: string; content: string }[]> => { try { // Instead of directly fetching from GitHub, use our own API endpoint as a proxy const response = await fetch(`/api/github-template?repo=${encodeURIComponent(repoName)}`); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } // Our API will return the files in the format we need const files = (await response.json()) as any; return files; } catch (error) { console.error('Error fetching release contents:', error); throw error; } }; export async function getTemplates(templateName: string, title?: string) { const template = STARTER_TEMPLATES.find((t) => t.name == templateName); if (!template) { return null; } const githubRepo = template.githubRepo; const files = await getGitHubRepoContent(githubRepo); let filteredFiles = files; /* * ignoring common unwanted files * exclude .git */ filteredFiles = filteredFiles.filter((x) => x.path.startsWith('.git') == false); /* * exclude lock files * WE NOW INCLUDE LOCK FILES FOR IMPROVED INSTALL TIMES */ { /* *const comminLockFiles = ['package-lock.json', 'yarn.lock', 'pnpm-lock.yaml']; *filteredFiles = filteredFiles.filter((x) => comminLockFiles.includes(x.name) == false); */ } // exclude .bolt filteredFiles = filteredFiles.filter((x) => x.path.startsWith('.bolt') == false); // check for ignore file in .bolt folder const templateIgnoreFile = files.find((x) => x.path.startsWith('.bolt') && x.name == 'ignore'); const filesToImport = { files: filteredFiles, ignoreFile: [] as typeof filteredFiles, }; if (templateIgnoreFile) { // redacting files specified in ignore file const ignorepatterns = templateIgnoreFile.content.split('\n').map((x) => x.trim()); const ig = ignore().add(ignorepatterns); // filteredFiles = filteredFiles.filter(x => !ig.ignores(x.path)) const ignoredFiles = filteredFiles.filter((x) => ig.ignores(x.path)); filesToImport.files = filteredFiles; filesToImport.ignoreFile = ignoredFiles; } const assistantMessage = ` <boltArtifact id="imported-files" title="${title || 'Importing Starter Files'}" type="bundled"> ${filesToImport.files .map( (file) => `<boltAction type="file" filePath="${file.path}"> ${file.content} </boltAction>`, ) .join('\n')} </boltArtifact> `; let userMessage = ``; const templatePromptFile = files.filter((x) => x.path.startsWith('.bolt')).find((x) => x.name == 'prompt'); if (templatePromptFile) { userMessage = ` TEMPLATE INSTRUCTIONS: ${templatePromptFile.content} --- `; } if (filesToImport.ignoreFile.length > 0) { userMessage = userMessage + ` STRICT FILE ACCESS RULES - READ CAREFULLY: The following files are READ-ONLY and must never be modified: ${filesToImport.ignoreFile.map((file) => `- ${file.path}`).join('\n')} Permitted actions: ✓ Import these files as dependencies ✓ Read from these files ✓ Reference these files Strictly forbidden actions: ❌ Modify any content within these files ❌ Delete these files ❌ Rename these files ❌ Move these files ❌ Create new versions of these files ❌ Suggest changes to these files Any attempt to modify these protected files will result in immediate termination of the operation. If you need to make changes to functionality, create new files instead of modifying the protected ones listed above. --- `; } userMessage += ` --- template import is done, and you can now use the imported files, edit only the files that need to be changed, and you can create new files as needed. NO NOT EDIT/WRITE ANY FILES THAT ALREADY EXIST IN THE PROJECT AND DOES NOT NEED TO BE MODIFIED --- Now that the Template is imported please continue with my original request IMPORTANT: Dont Forget to install the dependencies before running the app by using \`npm install && npm run dev\` `; return { assistantMessage, userMessage, }; }