Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement feature request and bug fixes #15

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion app/Services/ChatAssistant.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -82,6 +85,7 @@ public function createNewAssistant(): Assistant
$folderName = basename($path);

$service = $this->selectService();
$this->ensureAPIKey($service);
$models = $this->getModels($service);

$assistant = form()
Expand Down Expand Up @@ -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);
}
}
}
35 changes: 13 additions & 22 deletions app/Utils/OnBoardingSteps.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -21,7 +21,6 @@ public function isCompleted($droidCommand): bool
{
return $this->configurationFileExists()
&& $this->viewsFolderExists()
&& $this->APIKeysExist()
&& $this->setupDatabase($droidCommand);
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -144,4 +135,4 @@ public function loadConfigFile(): bool

return false;
}
}
}