diff --git a/app/Services/ChatAssistant.php b/app/Services/ChatAssistant.php index bfbe31d..059a58c 100644 --- a/app/Services/ChatAssistant.php +++ b/app/Services/ChatAssistant.php @@ -11,6 +11,7 @@ use App\Tools\UpdateFile; use App\Tools\WriteToFile; use App\Traits\HasTools; +use App\Utils\OnBoardingSteps; use Exception; use Illuminate\Support\Collection; use ReflectionException; @@ -27,12 +28,14 @@ class ChatAssistant use HasTools; private const DEFAULT_SERVICE = 'openai'; + private OnBoardingSteps $onBoardingSteps; /** * @throws ReflectionException */ - public function __construct() + public function __construct(OnBoardingSteps $onBoardingSteps) { + $this->onBoardingSteps = $onBoardingSteps; $this->register([ ExecuteCommand::class, WriteToFile::class, @@ -82,6 +85,7 @@ public function createNewAssistant(): Assistant $folderName = basename($path); $service = $this->selectService(); + $this->ensureAPIKey($service); $models = $this->getModels($service); $assistant = form() @@ -280,4 +284,12 @@ private function executeToolCall($thread, $toolCall): void throw new Exception("Error calling tool: {$e->getMessage()}"); } } + + private function ensureAPIKey(string $service): void + { + $apiKeyConfigName = strtoupper($service).'_API_KEY'; + if (!config("aiproviders.{$service}.api_key")) { + $this->onBoardingSteps->requestAPIKey($service); + } + } } \ No newline at end of file diff --git a/app/Utils/OnBoardingSteps.php b/app/Utils/OnBoardingSteps.php index b73edbf..1ac1d03 100644 --- a/app/Utils/OnBoardingSteps.php +++ b/app/Utils/OnBoardingSteps.php @@ -6,7 +6,7 @@ use Exception; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Config; -use Illuminate\Support\Facades\Storage; // Import Artisan facade to run commands +use Illuminate\Support\Facades\Storage; use function Laravel\Prompts\password; @@ -21,7 +21,6 @@ public function isCompleted($droidCommand): bool { return $this->configurationFileExists() && $this->viewsFolderExists() - && $this->APIKeysExist() && $this->setupDatabase($droidCommand); } @@ -56,26 +55,18 @@ private function configurationFileExists(): bool return true; } - /** - * @throws Exception - */ - private function APIKeysExist(): bool + public function requestAPIKey(string $service): string { - $services = ['openai', 'claude']; // List all supported services here - - foreach ($services as $service) { - $apiKeyConfigName = strtoupper($service).'_API_KEY'; - if (! config("aiproviders.{$service}.api_key")) { - $apiKey = password( - label: "🤖: Enter your {$service} API key to continue", - placeholder: 'sk-xxxxxx-xxxxxx-xxxxxx-xxxxxx', - hint: "You can find your API key in your {$service} dashboard" - ); - $this->setConfigValue($apiKeyConfigName, $apiKey); - } - } - - return true; + $apiKey = password( + label: "🤖: Enter your {$service} API key to continue", + placeholder: 'sk-xxxxxx-xxxxxx-xxxxxx-xxxxxx', + hint: "You can find your API key in your {$service} dashboard" + ); + + $apiKeyConfigName = strtoupper($service).'_API_KEY'; + $this->setConfigValue($apiKeyConfigName, $apiKey); + + return $apiKey; } protected function setupDatabase($droidCommand): bool @@ -144,4 +135,4 @@ public function loadConfigFile(): bool return false; } -} +} \ No newline at end of file