Files
InvoiceShelf/app/Platform/Ai/Http/Setup/AiConfigurationController.php
T
Darko Gjorgjijoski 5ef7804e60 refactor: adopt modular domain architecture (#747)
* refactor: stabilize model identities for domain migration

* refactor: extract module platform context

* refactor: assign models to domain contexts

* refactor: extract ai platform context

* refactor: extract storage platform context

* refactor: extract mail platform context

* refactor: extract pdf platform context

* refactor: extract operations platform context

* refactor: move installation into operations platform

* refactor: extract money domain context

* refactor: extract taxation domain context

* refactor: extract catalog domain context

* refactor: extract metadata domain context

* refactor: extract reporting domain context

* refactor: extract purchases domain context

* refactor: extract receivables domain context

* refactor: extract accounts domain context

* refactor: complete reporting statement boundary

* refactor: extract contacts domain context

* refactor: extract sales domain context

* refactor: remove legacy application layers

* fix: migrate legacy bouncer role identities
2026-08-05 17:40:03 +02:00

76 lines
2.9 KiB
PHP

<?php
namespace App\Platform\Ai\Http\Setup;
use App\Platform\Ai\Application\AiConfigurationService;
use App\Platform\Http\Controller;
use App\Platform\Operations\Models\Setting;
use App\Rules\PublicHttpUrl;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Validation\ValidationException;
/**
* Installer wizard step: optional "Enable AI" configuration.
*
* Runs without an authenticated user — the installer middleware allows public
* access until `profile_complete` is marked COMPLETED. Persists a minimal global
* AI config so the first super-admin doesn't have to revisit the admin panel
* just to turn on chat/text-generation after install.
*
* Skipping the step (posting ai_enabled=NO) is the default path — users can
* always configure AI later from Admin → Settings → AI Configuration.
*/
class AiConfigurationController extends Controller
{
public function __construct(
private readonly AiConfigurationService $aiConfigurationService,
) {}
/**
* Return the current AI config defaults plus the driver list for the wizard form.
*/
public function show(): JsonResponse
{
return response()->json([
'config' => $this->aiConfigurationService->getGlobalConfig(),
'drivers' => $this->aiConfigurationService->listDrivers(),
]);
}
/**
* Persist the installer's AI config choice and advance the wizard step.
*
* @throws ValidationException
*/
public function save(Request $request): JsonResponse
{
Artisan::call('optimize:clear');
$validated = $this->validate($request, [
'ai_enabled' => 'required|in:YES,NO',
'ai_driver' => 'required_if:ai_enabled,YES|nullable|string',
'ai_api_key' => 'required_if:ai_enabled,YES|nullable|string',
'ai_base_url' => ['nullable', 'string', 'url', new PublicHttpUrl],
'ai_chat_enabled' => 'nullable|in:YES,NO',
'ai_chat_model' => 'nullable|string|max:200',
'ai_text_generation_enabled' => 'nullable|in:YES,NO',
'ai_text_generation_model' => 'nullable|string|max:200',
]);
$this->aiConfigurationService->saveGlobalConfig($validated);
// Advance the installer's profile_complete marker if we're the first to touch it.
// Mail uses `4`; we'll use the next sentinel but leave actual completion to the
// final Preferences step (which sets 'COMPLETED'). The sentinel value is ignored
// once COMPLETED is written — it only matters for step-tracking during install.
$profileComplete = Setting::getSetting('profile_complete');
if ($profileComplete !== 'COMPLETED' && (int) $profileComplete < 5) {
Setting::setSetting('profile_complete', 5);
}
return response()->json(['success' => true]);
}
}