|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace flight\commands; |
| 6 | + |
| 7 | +use Ahc\Cli\Input\Command; |
| 8 | + |
| 9 | +/** |
| 10 | + * @property-read ?string $credsFile |
| 11 | + * @property-read ?string $baseDir |
| 12 | + */ |
| 13 | +class AiGenerateInstructionsCommand extends Command |
| 14 | +{ |
| 15 | + /** |
| 16 | + * Constructor for the AiGenerateInstructionsCommand class. |
| 17 | + * |
| 18 | + * Initializes a new instance of the command. |
| 19 | + */ |
| 20 | + public function __construct() |
| 21 | + { |
| 22 | + parent::__construct('ai:generate-instructions', 'Generate project-specific AI coding instructions'); |
| 23 | + $this->option('--creds-file', 'Path to .runway-creds.json file', null, ''); |
| 24 | + $this->option('--base-dir', 'Project base directory (for testing or custom use)', null, ''); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Executes the command logic for generating AI instructions. |
| 29 | + * |
| 30 | + * This method is called to perform the main functionality of the |
| 31 | + * AiGenerateInstructionsCommand. It should contain the steps required |
| 32 | + * to generate and output instructions using AI, based on the command's |
| 33 | + * configuration and input. |
| 34 | + * |
| 35 | + * @return int |
| 36 | + */ |
| 37 | + public function execute() |
| 38 | + { |
| 39 | + $io = $this->app()->io(); |
| 40 | + $baseDir = $this->baseDir ? rtrim($this->baseDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR : getcwd() . DIRECTORY_SEPARATOR; |
| 41 | + $runwayCredsFile = $this->credsFile ?: $baseDir . '.runway-creds.json'; |
| 42 | + |
| 43 | + // Check for runway creds |
| 44 | + if (!file_exists($runwayCredsFile)) { |
| 45 | + $io->error('Missing .runway-creds.json. Please run the \'ai:init\' command first.', true); |
| 46 | + return 1; |
| 47 | + } |
| 48 | + |
| 49 | + $io->info('Let\'s gather some project details to generate AI coding instructions.', true); |
| 50 | + |
| 51 | + // Ask questions |
| 52 | + $projectDesc = $io->prompt('Please describe what your project is for?'); |
| 53 | + $database = $io->prompt('What database are you planning on using? (e.g. MySQL, SQLite, PostgreSQL, none)', 'none'); |
| 54 | + $templating = $io->prompt('What HTML templating engine will you plan on using (if any)? (recommend latte)', 'latte'); |
| 55 | + $security = $io->confirm('Is security an important element of this project?', 'y'); |
| 56 | + $performance = $io->confirm('Is performance and speed an important part of this project?', 'y'); |
| 57 | + $composerLibs = $io->prompt('What major composer libraries will you be using if you know them right now?', 'none'); |
| 58 | + $envSetup = $io->prompt('How will you set up your development environment? (e.g. Docker, Vagrant, PHP dev server, other)', 'Docker'); |
| 59 | + $teamSize = $io->prompt('How many developers will be working on this project?', '1'); |
| 60 | + $api = $io->confirm('Will this project expose an API?', 'n'); |
| 61 | + $other = $io->prompt('Any other important requirements or context? (optional)', 'no'); |
| 62 | + |
| 63 | + // Prepare prompt for LLM |
| 64 | + $contextFile = $baseDir . '.github/copilot-instructions.md'; |
| 65 | + $context = file_exists($contextFile) ? file_get_contents($contextFile) : ''; |
| 66 | + $userDetails = [ |
| 67 | + 'Project Description' => $projectDesc, |
| 68 | + 'Database' => $database, |
| 69 | + 'Templating Engine' => $templating, |
| 70 | + 'Security Important' => $security ? 'yes' : 'no', |
| 71 | + 'Performance Important' => $performance ? 'yes' : 'no', |
| 72 | + 'Composer Libraries' => $composerLibs, |
| 73 | + 'Environment Setup' => $envSetup, |
| 74 | + 'Team Size' => $teamSize, |
| 75 | + 'API' => $api ? 'yes' : 'no', |
| 76 | + 'Other' => $other, |
| 77 | + ]; |
| 78 | + $detailsText = ""; |
| 79 | + foreach ($userDetails as $k => $v) { |
| 80 | + $detailsText .= "$k: $v\n"; |
| 81 | + } |
| 82 | + $prompt = <<<EOT |
| 83 | + You are an AI coding assistant. Update the following project instructions for this FlightPHP project based on the latest user answers. Only output the new instructions, no extra commentary. |
| 84 | + User answers: |
| 85 | + $detailsText |
| 86 | + Current instructions: |
| 87 | + $context |
| 88 | + EOT; |
| 89 | + |
| 90 | + // Read LLM creds |
| 91 | + $creds = json_decode(file_get_contents($runwayCredsFile), true); |
| 92 | + $apiKey = $creds['api_key'] ?? ''; |
| 93 | + $model = $creds['model'] ?? 'gpt-4o'; |
| 94 | + $baseUrl = $creds['base_url'] ?? 'https://api.openai.com'; |
| 95 | + |
| 96 | + // Prepare curl call (OpenAI compatible) |
| 97 | + $headers = [ |
| 98 | + 'Content-Type: application/json', |
| 99 | + 'Authorization: Bearer ' . $apiKey, |
| 100 | + ]; |
| 101 | + $data = [ |
| 102 | + 'model' => $model, |
| 103 | + 'messages' => [ |
| 104 | + ['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.'], |
| 105 | + ['role' => 'user', 'content' => $prompt], |
| 106 | + ], |
| 107 | + 'temperature' => 0.2, |
| 108 | + ]; |
| 109 | + $jsonData = json_encode($data); |
| 110 | + |
| 111 | + // add info line that this may take a few minutes |
| 112 | + $io->info('Generating AI instructions, this may take a few minutes...', true); |
| 113 | + |
| 114 | + $result = $this->callLlmApi($baseUrl, $headers, $jsonData, $io); |
| 115 | + if ($result === false) { |
| 116 | + return 1; |
| 117 | + } |
| 118 | + $response = json_decode($result, true); |
| 119 | + $instructions = $response['choices'][0]['message']['content'] ?? ''; |
| 120 | + if (!$instructions) { |
| 121 | + $io->error('No instructions returned from LLM.', true); |
| 122 | + return 1; |
| 123 | + } |
| 124 | + |
| 125 | + // Write to files |
| 126 | + $io->info('Updating .github/copilot-instructions.md, .cursor/rules/project-overview.mdc, and .windsurfrules...', true); |
| 127 | + if (!is_dir($baseDir . '.github')) { |
| 128 | + mkdir($baseDir . '.github', 0755, true); |
| 129 | + } |
| 130 | + if (!is_dir($baseDir . '.cursor/rules')) { |
| 131 | + mkdir($baseDir . '.cursor/rules', 0755, true); |
| 132 | + } |
| 133 | + file_put_contents($baseDir . '.github/copilot-instructions.md', $instructions); |
| 134 | + file_put_contents($baseDir . '.cursor/rules/project-overview.mdc', $instructions); |
| 135 | + file_put_contents($baseDir . '.windsurfrules', $instructions); |
| 136 | + $io->ok('AI instructions updated successfully.', true); |
| 137 | + return 0; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Make the LLM API call using curl |
| 142 | + * |
| 143 | + * @param string $baseUrl |
| 144 | + * @param array<int,string> $headers |
| 145 | + * @param string $jsonData |
| 146 | + * @param object $io |
| 147 | + * |
| 148 | + * @return string|false |
| 149 | + * |
| 150 | + * @codeCoverageIgnore |
| 151 | + */ |
| 152 | + protected function callLlmApi($baseUrl, $headers, $jsonData, $io) |
| 153 | + { |
| 154 | + $ch = curl_init($baseUrl . '/v1/chat/completions'); |
| 155 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 156 | + curl_setopt($ch, CURLOPT_POST, true); |
| 157 | + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
| 158 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); |
| 159 | + $result = curl_exec($ch); |
| 160 | + if (curl_errno($ch)) { |
| 161 | + $io->error('Failed to call LLM API: ' . curl_error($ch), true); |
| 162 | + curl_close($ch); |
| 163 | + return false; |
| 164 | + } |
| 165 | + curl_close($ch); |
| 166 | + return $result; |
| 167 | + } |
| 168 | +} |
0 commit comments