diff --git a/app/Http/Controllers/SettingController.php b/app/Http/Controllers/SettingController.php new file mode 100644 index 00000000..49f6209f --- /dev/null +++ b/app/Http/Controllers/SettingController.php @@ -0,0 +1,122 @@ + [ + 'setup_secrets' => false, + ], + 'meta_data' => [ + 'openai' => [ + 'models' => [], + ], + ], + 'secrets' => [ + 'openai' => [ + 'api_key' => null, + 'api_url' => 'https://api.openai.com/v1', + ], + 'claude' => [ + 'api_key' => null, + ], + 'groq' => [ + 'api_key' => null, + 'api_url' => 'https://api.groq.com/openai/v1/', + ], + 'ollama' => [ + 'api_key' => 'ollama', + 'api_url' => 'http://localhost:11434/api/', + ], + ], + ]); + } else { + $setting = Setting::first(); + } + + return inertia('Settings/Show', [ + 'setting' => new SettingResource($setting), + ]); + } + + protected function updateStep(Setting $setting): Setting + { + $steps = $setting->steps; + $steps['setup_secrets'] = true; + $setting->steps = $steps; + $setting->save(); + + return $setting; + } + + public function updateClaude(Request $request, Setting $setting) + { + $validated = $request->validate([ + 'api_key' => 'string|required', + ]); + + $secrets = $setting->secrets; + $secrets['claude'] = $validated; + $setting->secrets = $secrets; + $setting->save(); + $this->updateStep($setting); + + return back(); + } + + public function updateOllama(Request $request, Setting $setting) + { + $validated = $request->validate([ + 'api_key' => 'string|required', + 'api_url' => 'string|required', + ]); + + $secrets = $setting->secrets; + $secrets['ollama'] = $validated; + $setting->secrets = $secrets; + $setting->save(); + $this->updateStep($setting); + + return back(); + } + + public function updateGroq(Request $request, Setting $setting) + { + $validated = $request->validate([ + 'api_key' => 'string|required', + 'api_url' => 'string|required', + ]); + + $secrets = $setting->secrets; + $secrets['groq'] = $validated; + $setting->secrets = $secrets; + $setting->save(); + $this->updateStep($setting); + + return back(); + } + + public function updateOpenAi(Request $request, Setting $setting) + { + $validated = $request->validate([ + 'api_key' => 'string|required', + 'api_url' => 'string|required', + ]); + + $secrets = $setting->secrets; + $secrets['openai'] = $validated; + $setting->secrets = $secrets; + $setting->save(); + $this->updateStep($setting); + + return back(); + } +} diff --git a/app/Http/Resources/SettingResource.php b/app/Http/Resources/SettingResource.php new file mode 100644 index 00000000..f05e431a --- /dev/null +++ b/app/Http/Resources/SettingResource.php @@ -0,0 +1,19 @@ + + */ + public function toArray(Request $request): array + { + return parent::toArray($request); + } +} diff --git a/app/Models/Setting.php b/app/Models/Setting.php new file mode 100644 index 00000000..c8f9ad1e --- /dev/null +++ b/app/Models/Setting.php @@ -0,0 +1,25 @@ + 'array', + 'meta_data' => 'array', + 'secrets' => 'encrypted:array', + ]; + + public function user(): BelongsTo + { + return $this->belongsTo(User::class); + } +} diff --git a/database/factories/SettingFactory.php b/database/factories/SettingFactory.php new file mode 100644 index 00000000..9697ef57 --- /dev/null +++ b/database/factories/SettingFactory.php @@ -0,0 +1,39 @@ + + */ +class SettingFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'steps' => [ + 'setup_secrets' => false, + ], + 'meta_data' => [ + 'openai' => [ + 'models' => [ + 'completion_model' => 'gpt-3.5-turbo', + ], + ], + ], + 'secrets' => [ + 'openai' => [ + 'api_key' => 'foobar', + ], + ], + 'user_id' => User::factory(), + ]; + } +} diff --git a/database/migrations/2024_06_11_183813_create_settings_table.php b/database/migrations/2024_06_11_183813_create_settings_table.php new file mode 100644 index 00000000..19d1607f --- /dev/null +++ b/database/migrations/2024_06_11_183813_create_settings_table.php @@ -0,0 +1,31 @@ +id(); + $table->json('steps')->nullable(); + $table->json('meta_data')->nullable(); + $table->longText('secrets')->nullable(); + $table->foreignIdFor(\App\Models\User::class)->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('settings'); + } +}; diff --git a/resources/js/Components/ActionMessage.vue b/resources/js/Components/ActionMessage.vue index 59741485..87d1d758 100644 --- a/resources/js/Components/ActionMessage.vue +++ b/resources/js/Components/ActionMessage.vue @@ -7,7 +7,7 @@ defineProps({