|
| 1 | + |
| 2 | +'use server'; |
| 3 | +/** |
| 4 | + * @fileOverview AI agent for generating project file structures and content. |
| 5 | + * |
| 6 | + * - generateProjectScaffold - A function that generates a list of files (path and content) based on a prompt. |
| 7 | + * - GenerateProjectScaffoldInput - The input type for the generateProjectScaffold function. |
| 8 | + * - GenerateProjectScaffoldOutput - The return type for the generateProjectScaffold function. |
| 9 | + */ |
| 10 | + |
| 11 | +import {ai} from '@/ai/genkit'; |
| 12 | +import {z} from 'genkit'; |
| 13 | + |
| 14 | +const GenerateProjectScaffoldInputSchema = z.object({ |
| 15 | + prompt: z.string().describe('A prompt describing the desired project structure and functionality.'), |
| 16 | +}); |
| 17 | +export type GenerateProjectScaffoldInput = z.infer<typeof GenerateProjectScaffoldInputSchema>; |
| 18 | + |
| 19 | +const FileScaffoldSchema = z.object({ |
| 20 | + filePath: z.string().describe('The relative path for the file (e.g., "src/index.js" or "README.md").'), |
| 21 | + content: z.string().describe('The content of the file.'), |
| 22 | +}); |
| 23 | + |
| 24 | +const GenerateProjectScaffoldOutputSchema = z.object({ |
| 25 | + files: z.array(FileScaffoldSchema).describe('A list of files with their paths and content.'), |
| 26 | +}); |
| 27 | +export type GenerateProjectScaffoldOutput = z.infer<typeof GenerateProjectScaffoldOutputSchema>; |
| 28 | + |
| 29 | +export async function generateProjectScaffold( |
| 30 | + input: GenerateProjectScaffoldInput |
| 31 | +): Promise<GenerateProjectScaffoldOutput> { |
| 32 | + return generateProjectScaffoldFlow(input); |
| 33 | +} |
| 34 | + |
| 35 | +const prompt = ai.definePrompt({ |
| 36 | + name: 'generateProjectScaffoldPrompt', |
| 37 | + input: {schema: GenerateProjectScaffoldInputSchema}, |
| 38 | + output: {schema: GenerateProjectScaffoldOutputSchema}, |
| 39 | + prompt: `You are an expert software architect and developer. Your task is to generate a complete file structure and content for a small project based on the user's prompt. |
| 40 | +
|
| 41 | +User Prompt: {{{prompt}}} |
| 42 | +
|
| 43 | +You MUST respond with a JSON object matching the following Zod schema: |
| 44 | +\`\`\`json |
| 45 | +{ |
| 46 | + "files": [ |
| 47 | + { |
| 48 | + "filePath": "string (e.g., 'index.html', 'src/app.js', 'styles/main.css')", |
| 49 | + "content": "string (the full content of the file)" |
| 50 | + } |
| 51 | + // ... more files |
| 52 | + ] |
| 53 | +} |
| 54 | +\`\`\` |
| 55 | +
|
| 56 | +Ensure all file paths are relative (e.g., 'index.html', not '/index.html'). |
| 57 | +Generate appropriate content for each file. For example, if asked for a simple web page, generate HTML, CSS, and JS files. |
| 58 | +If the user asks for a Python script, generate the .py file. |
| 59 | +
|
| 60 | +Provide the complete project structure as a flat list of files, where \`filePath\` includes any necessary subdirectories. |
| 61 | +Example for a simple website: |
| 62 | +[ |
| 63 | + { "filePath": "index.html", "content": "<!DOCTYPE html>..." }, |
| 64 | + { "filePath": "css/style.css", "content": "body { font-family: sans-serif; }" }, |
| 65 | + { "filePath": "js/script.js", "content": "console.log('Hello');" } |
| 66 | +] |
| 67 | +
|
| 68 | +Do not include any explanations or conversational text outside the JSON output. |
| 69 | +Focus on creating a functional and logical set of files. |
| 70 | +`, |
| 71 | +}); |
| 72 | + |
| 73 | +const generateProjectScaffoldFlow = ai.defineFlow( |
| 74 | + { |
| 75 | + name: 'generateProjectScaffoldFlow', |
| 76 | + inputSchema: GenerateProjectScaffoldInputSchema, |
| 77 | + outputSchema: GenerateProjectScaffoldOutputSchema, |
| 78 | + }, |
| 79 | + async input => { |
| 80 | + const {output} = await prompt(input); |
| 81 | + // Ensure the output is not null and adheres to the schema. |
| 82 | + // If output is null or doesn't fit, return an empty files array or throw an error. |
| 83 | + if (!output || !output.files) { |
| 84 | + console.error("AI did not return the expected file structure."); |
| 85 | + return { files: [] }; |
| 86 | + } |
| 87 | + return output; |
| 88 | + } |
| 89 | +); |
| 90 | + |
0 commit comments