diff --git a/src/Command/GenerateCommitMessage.php b/src/Command/GenerateCommitMessage.php index 5800ba2..b7cff37 100644 --- a/src/Command/GenerateCommitMessage.php +++ b/src/Command/GenerateCommitMessage.php @@ -10,12 +10,20 @@ class GenerateCommitMessage extends Command { + private OpenAI\Client $client; + protected function configure() { $this->setName('generate-commit-message'); $this->addOption('from-uncommitted', 'u', InputOption::VALUE_NONE, 'Generate commit message from uncommitted changes'); } + protected function initialize(InputInterface $input, OutputInterface $output): void + { + $yourApiKey = getenv('OPENAI_API_KEY'); + $this->client = OpenAI::client($yourApiKey); + } + protected function execute(InputInterface $input, OutputInterface $output): int { $diffLines = []; @@ -28,15 +36,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int $diff = join("\n", $diffLines); - // When the diff is too long, simply truncate it to <1MB and hope for the best - if (strlen($diff) > 1048576) { - $diff = substr($diff, 0, 1048576); + // When the diff is too long, simply truncate it to <256K and hope for the best + // The theoretical message limit is 1MB, but there's also the maximum token limit to consider + if (strlen($diff) > 256 * 1024) { + $diff = substr($diff, 0, 256 * 1024); } - $yourApiKey = getenv('OPENAI_API_KEY'); - $client = OpenAI::client($yourApiKey); - - $result = $client->chat()->create([ + $result = $this->client->chat()->create([ 'model' => 'gpt-4o-mini', 'messages' => [ ['role' => 'system', 'content' => 'You will be provided a Git diff. Generate a commit message from it, following the conventional commit message format. Provide the output in plain text, without any additional formatting.'], diff --git a/src/Command/GenerateReleaseNotes.php b/src/Command/GenerateReleaseNotes.php index a14e5a5..9eb96e4 100644 --- a/src/Command/GenerateReleaseNotes.php +++ b/src/Command/GenerateReleaseNotes.php @@ -23,9 +23,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int $diff = join("\n", $diffLines); - // When the diff is too long, simply truncate it to <1MB and hope for the best - if (strlen($diff) > 1048576) { - $diff = substr($diff, 0, 1048576); + // When the diff is too long, simply truncate it to <256K and hope for the best + // The theoretical message limit is 1MB, but there's also the maximum token limit to consider + if (strlen($diff) > 256 * 1024) { + $diff = substr($diff, 0, 256 * 1024); } $yourApiKey = getenv('OPENAI_API_KEY');