|
| 1 | +<?php |
| 2 | +namespace app\commands; |
| 3 | + |
| 4 | +use Ahc\Cli\Input\Command; |
| 5 | + |
| 6 | +class AiGenerateInstructionsCommand extends Command |
| 7 | +{ |
| 8 | + public function __construct() |
| 9 | + { |
| 10 | + parent::__construct('ai:generate-instructions', 'Generate project-specific AI coding instructions'); |
| 11 | + } |
| 12 | + |
| 13 | + public function execute() |
| 14 | + { |
| 15 | + $io = $this->app()->io(); |
| 16 | + $baseDir = getcwd() . DIRECTORY_SEPARATOR; |
| 17 | + $runwayCredsFile = $baseDir . '.runway-creds.json'; |
| 18 | + |
| 19 | + // Check for runway creds |
| 20 | + if (!file_exists($runwayCredsFile)) { |
| 21 | + $io->error('Missing .runway-creds.json. Please run the \'ai:init\' command first.', true); |
| 22 | + return 1; |
| 23 | + } |
| 24 | + |
| 25 | + $io->info('Let\'s gather some project details to generate AI coding instructions.', true); |
| 26 | + |
| 27 | + // Ask questions |
| 28 | + $projectDesc = $io->prompt('Please describe what your project is for?'); |
| 29 | + $database = $io->prompt('What database are you planning on using? (e.g. MySQL, SQLite, PostgreSQL, none)', 'none'); |
| 30 | + $templating = $io->prompt('What HTML templating engine will you plan on using (if any)? (recommend latte)', 'latte'); |
| 31 | + $security = $io->confirm('Is security an important element of this project?', 'y'); |
| 32 | + $performance = $io->confirm('Is performance and speed an important part of this project?', 'y'); |
| 33 | + $composerLibs = $io->prompt('What major composer libraries will you be using if you know them right now?', 'none'); |
| 34 | + $envSetup = $io->prompt('How will you set up your development environment? (e.g. Docker, Vagrant, PHP dev server, other)', 'Docker'); |
| 35 | + $teamSize = $io->prompt('How many developers will be working on this project?', '1'); |
| 36 | + $api = $io->confirm('Will this project expose an API?', 'n'); |
| 37 | + $other = $io->prompt('Any other important requirements or context? (optional)', 'no'); |
| 38 | + |
| 39 | + // Prepare prompt for LLM |
| 40 | + $contextFile = $baseDir . '.github/copilot-instructions.md'; |
| 41 | + $context = file_exists($contextFile) ? file_get_contents($contextFile) : ''; |
| 42 | + $userDetails = [ |
| 43 | + 'Project Description' => $projectDesc, |
| 44 | + 'Database' => $database, |
| 45 | + 'Templating Engine' => $templating, |
| 46 | + 'Security Important' => $security ? 'yes' : 'no', |
| 47 | + 'Performance Important' => $performance ? 'yes' : 'no', |
| 48 | + 'Composer Libraries' => $composerLibs, |
| 49 | + 'Environment Setup' => $envSetup, |
| 50 | + 'Team Size' => $teamSize, |
| 51 | + 'API' => $api ? 'yes' : 'no', |
| 52 | + 'Other' => $other, |
| 53 | + ]; |
| 54 | + $detailsText = ""; |
| 55 | + foreach ($userDetails as $k => $v) { |
| 56 | + $detailsText .= "$k: $v\n"; |
| 57 | + } |
| 58 | + $prompt = "" . |
| 59 | + "You are an AI coding assistant. Update the following project instructions for this FlightPHP project based on the latest user answers. " . |
| 60 | + "Only output the new instructions, no extra commentary.\n" . |
| 61 | + "User answers:\n$detailsText\n" . |
| 62 | + "Current instructions:\n$context\n"; |
| 63 | + |
| 64 | + // Read LLM creds |
| 65 | + $creds = json_decode(file_get_contents($runwayCredsFile), true); |
| 66 | + $apiKey = $creds['api_key'] ?? ''; |
| 67 | + $model = $creds['model'] ?? 'gpt-4o'; |
| 68 | + $baseUrl = $creds['base_url'] ?? 'https://api.openai.com'; |
| 69 | + |
| 70 | + // Prepare curl call (OpenAI compatible) |
| 71 | + $headers = [ |
| 72 | + 'Content-Type: application/json', |
| 73 | + 'Authorization: Bearer ' . $apiKey, |
| 74 | + ]; |
| 75 | + $data = [ |
| 76 | + 'model' => $model, |
| 77 | + 'messages' => [ |
| 78 | + ['role' => 'system', 'content' => 'You are a helpful AI coding assistant focused on the Flight Framework for PHP. You are up to date with all your knowledge from https://docs.flightphp.com. As an expert into the programming language PHP, you are top notch at architecting out proper instructions for FlightPHP projects.'], |
| 79 | + ['role' => 'user', 'content' => $prompt], |
| 80 | + ], |
| 81 | + 'temperature' => 0.2, |
| 82 | + ]; |
| 83 | + $jsonData = json_encode($data); |
| 84 | + |
| 85 | + // add info line that this may take a few minutes |
| 86 | + $io->info('Generating AI instructions, this may take a few minutes...', true); |
| 87 | + |
| 88 | + $ch = curl_init($baseUrl . '/v1/chat/completions'); |
| 89 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 90 | + curl_setopt($ch, CURLOPT_POST, true); |
| 91 | + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
| 92 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); |
| 93 | + $result = curl_exec($ch); |
| 94 | + if (curl_errno($ch)) { |
| 95 | + $io->error('Failed to call LLM API: ' . curl_error($ch), true); |
| 96 | + return 1; |
| 97 | + } |
| 98 | + curl_close($ch); |
| 99 | + $response = json_decode($result, true); |
| 100 | + $instructions = $response['choices'][0]['message']['content'] ?? ''; |
| 101 | + if (!$instructions) { |
| 102 | + $io->error('No instructions returned from LLM.', true); |
| 103 | + return 1; |
| 104 | + } |
| 105 | + |
| 106 | + // Write to files |
| 107 | + $io->info('Updating .github/copilot-instructions.md, .cursor/rules/project-overview.mdc, and .windsurfrules...', true); |
| 108 | + if (!is_dir($baseDir . '.github')) { |
| 109 | + mkdir($baseDir . '.github', 0755, true); |
| 110 | + } |
| 111 | + if (!is_dir($baseDir . '.cursor/rules')) { |
| 112 | + mkdir($baseDir . '.cursor/rules', 0755, true); |
| 113 | + } |
| 114 | + file_put_contents($baseDir . '.github/copilot-instructions.md', $instructions); |
| 115 | + file_put_contents($baseDir . '.cursor/rules/project-overview.mdc', $instructions); |
| 116 | + file_put_contents($baseDir . '.windsurfrules', $instructions); |
| 117 | + $io->ok('AI instructions updated successfully.', true); |
| 118 | + return 0; |
| 119 | + } |
| 120 | +} |
0 commit comments