-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add the settings page now to make it work
- Loading branch information
Showing
16 changed files
with
711 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Http\Resources\SettingResource; | ||
use App\Models\Setting; | ||
use Illuminate\Http\Request; | ||
|
||
class SettingController extends Controller | ||
{ | ||
public function show() | ||
{ | ||
if (! Setting::exists()) { | ||
$setting = Setting::create([ | ||
'steps' => [ | ||
'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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class SettingResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function toArray(Request $request): array | ||
{ | ||
return parent::toArray($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
|
||
class Setting extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $guarded = []; | ||
|
||
protected $casts = [ | ||
'steps' => 'array', | ||
'meta_data' => 'array', | ||
'secrets' => 'encrypted:array', | ||
]; | ||
|
||
public function user(): BelongsTo | ||
{ | ||
return $this->belongsTo(User::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Database\Factories; | ||
|
||
use App\Models\User; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
/** | ||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Setting> | ||
*/ | ||
class SettingFactory extends Factory | ||
{ | ||
/** | ||
* Define the model's default state. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
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(), | ||
]; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
database/migrations/2024_06_11_183813_create_settings_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('settings', function (Blueprint $table) { | ||
$table->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'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<script setup> | ||
import { ref } from 'vue'; | ||
import { Link, router, useForm } from '@inertiajs/vue3'; | ||
import ActionMessage from '@/Components/ActionMessage.vue'; | ||
import FormSection from '@/Components/FormSection.vue'; | ||
import InputError from '@/Components/InputError.vue'; | ||
import InputLabel from '@/Components/InputLabel.vue'; | ||
import PrimaryButton from '@/Components/PrimaryButton.vue'; | ||
import SecondaryButton from '@/Components/SecondaryButton.vue'; | ||
import TextInput from '@/Components/TextInput.vue'; | ||
const props = defineProps({ | ||
setting: Object, | ||
}); | ||
const form = useForm({ | ||
_method: 'PUT', | ||
api_key: props.setting.secrets?.claude?.api_key, | ||
}); | ||
const updateSecrets = () => { | ||
form.put(route('settings.update.claude', { | ||
setting: props.setting.id, | ||
}), { | ||
errorBag: 'updateProfileInformation', | ||
preserveScroll: true, | ||
}); | ||
}; | ||
</script> | ||
<template> | ||
<FormSection @submitted="updateSecrets"> | ||
<template #title> | ||
Claude API Key and Token | ||
</template> | ||
<template #description> | ||
If you want to use this service make sure to setup your API Token. | ||
You can get your keys | ||
<a | ||
class="underline" | ||
href="https://docs.anthropic.com/en/docs/intro-to-claude" target="_blank">here</a> | ||
</template> | ||
<template #form> | ||
<!-- Name --> | ||
<div class="col-span-6 sm:col-span-4"> | ||
<InputLabel for="name" value="Api Token" /> | ||
<TextInput | ||
id="name" | ||
v-model="form.api_key" | ||
type="text" | ||
class="mt-1 block w-full" | ||
required | ||
/> | ||
<InputError :message="form.errors.api_key" class="mt-2" /> | ||
</div> | ||
</template> | ||
<template #actions> | ||
<ActionMessage :on="form.recentlySuccessful" class="me-3"> | ||
Saved. | ||
</ActionMessage> | ||
<PrimaryButton :class="{ 'opacity-25': form.processing }" :disabled="form.processing"> | ||
Save | ||
</PrimaryButton> | ||
</template> | ||
</FormSection> | ||
</template> |
Oops, something went wrong.