|
| 1 | +package create |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/agnath18/lumo/pkg/ai" |
| 9 | +) |
| 10 | + |
| 11 | +// Generator handles project creation |
| 12 | +type Generator struct { |
| 13 | + aiClient ai.Client |
| 14 | +} |
| 15 | + |
| 16 | +// NewGenerator creates a new project generator |
| 17 | +func NewGenerator(aiClient ai.Client) *Generator { |
| 18 | + return &Generator{ |
| 19 | + aiClient: aiClient, |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +// Execute processes a project creation command |
| 24 | +func (g *Generator) Execute(query string) (string, error) { |
| 25 | + // If no query is provided, show help |
| 26 | + if query == "" { |
| 27 | + return g.showHelp(), nil |
| 28 | + } |
| 29 | + |
| 30 | + // Parse the query to determine project type |
| 31 | + projectType, framework, options, err := g.parseQuery(query) |
| 32 | + if err != nil { |
| 33 | + return "", err |
| 34 | + } |
| 35 | + |
| 36 | + // Generate the project |
| 37 | + return g.generateProject(projectType, framework, options) |
| 38 | +} |
| 39 | + |
| 40 | +// parseQuery analyzes the natural language query to determine project details |
| 41 | +func (g *Generator) parseQuery(query string) (string, string, map[string]string, error) { |
| 42 | + // Create a prompt for the AI to analyze the query |
| 43 | + prompt := fmt.Sprintf(` |
| 44 | +You are a project creation assistant. Analyze the following query and extract the following information: |
| 45 | +1. Project type/framework (e.g., Flutter, React, Next.js) |
| 46 | +2. State management approach (e.g., Bloc, Provider, Riverpod for Flutter) |
| 47 | +3. Any other specific requirements or options |
| 48 | +
|
| 49 | +Query: %s |
| 50 | +
|
| 51 | +Respond in the following JSON format: |
| 52 | +{ |
| 53 | + "projectType": "flutter|react|nextjs|etc", |
| 54 | + "framework": "bloc|provider|riverpod|redux|etc", |
| 55 | + "options": { |
| 56 | + "name": "project_name", |
| 57 | + "additionalFeatures": ["feature1", "feature2"] |
| 58 | + } |
| 59 | +} |
| 60 | +
|
| 61 | +Only include fields that you can confidently determine from the query. Use snake_case for project names. |
| 62 | +`, query) |
| 63 | + |
| 64 | + // Get response from AI |
| 65 | + response, err := g.aiClient.Query(prompt) |
| 66 | + if err != nil { |
| 67 | + return "", "", nil, fmt.Errorf("failed to analyze query: %w", err) |
| 68 | + } |
| 69 | + |
| 70 | + // Extract JSON from response (this is a simplified approach) |
| 71 | + // In a real implementation, you would parse the JSON properly |
| 72 | + projectType := extractValue(response, "projectType") |
| 73 | + framework := extractValue(response, "framework") |
| 74 | + |
| 75 | + // Extract options |
| 76 | + options := make(map[string]string) |
| 77 | + |
| 78 | + // Extract project name |
| 79 | + name := extractValue(response, "name") |
| 80 | + if name != "" { |
| 81 | + options["name"] = name |
| 82 | + } else { |
| 83 | + // Default project name based on project type |
| 84 | + switch strings.ToLower(projectType) { |
| 85 | + case "flutter": |
| 86 | + options["name"] = "my_flutter_app" |
| 87 | + case "react": |
| 88 | + options["name"] = "my-react-app" |
| 89 | + case "nextjs": |
| 90 | + options["name"] = "my-nextjs-app" |
| 91 | + default: |
| 92 | + options["name"] = "my-app" |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // If project type is not specified, default to Flutter |
| 97 | + if projectType == "" { |
| 98 | + projectType = "flutter" |
| 99 | + } |
| 100 | + |
| 101 | + return projectType, framework, options, nil |
| 102 | +} |
| 103 | + |
| 104 | +// generateProject creates a project based on the specified type and framework |
| 105 | +func (g *Generator) generateProject(projectType, framework string, options map[string]string) (string, error) { |
| 106 | + // Convert project type to lowercase for case-insensitive comparison |
| 107 | + projectType = strings.ToLower(projectType) |
| 108 | + |
| 109 | + // Generate the project based on type |
| 110 | + switch projectType { |
| 111 | + case "flutter": |
| 112 | + return generateFlutterProject(framework, options) |
| 113 | + // Add more project types here as needed |
| 114 | + default: |
| 115 | + return "", fmt.Errorf("unsupported project type: %s", projectType) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +// showHelp returns help information for the create command |
| 120 | +func (g *Generator) showHelp() string { |
| 121 | + return ` |
| 122 | +╭─────────────────── Lumo Project Creator ───────────────────╮ |
| 123 | +│ │ |
| 124 | +│ Create new projects with natural language descriptions. │ |
| 125 | +│ │ |
| 126 | +│ Usage: │ |
| 127 | +│ lumo create:"<project description>" │ |
| 128 | +│ │ |
| 129 | +│ Examples: │ |
| 130 | +│ lumo create:"Flutter app with bloc architecture" │ |
| 131 | +│ lumo create:"Flutter app with provider state management"│ |
| 132 | +│ │ |
| 133 | +│ Supported Frameworks: │ |
| 134 | +│ • Flutter (with Bloc, Provider, Riverpod) │ |
| 135 | +│ │ |
| 136 | +╰────────────────────────────────────────────────────────────╯ |
| 137 | +` |
| 138 | +} |
| 139 | + |
| 140 | +// extractValue is a simple helper to extract values from the AI response |
| 141 | +// In a real implementation, you would use proper JSON parsing |
| 142 | +func extractValue(response, key string) string { |
| 143 | + // This is a very simplified approach - in production code, use proper JSON parsing |
| 144 | + pattern := fmt.Sprintf(`"%s":\s*"([^"]+)"`, key) |
| 145 | + matches := regexp.MustCompile(pattern).FindStringSubmatch(response) |
| 146 | + if len(matches) > 1 { |
| 147 | + return matches[1] |
| 148 | + } |
| 149 | + return "" |
| 150 | +} |
0 commit comments