| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
-
- namespace App\Http\Controllers;
-
- use App\Billing\UsageLimits;
- use App\Commerce\ChatActions;
- use App\Enums\GenerationStatus;
- use App\Enums\GenerationType;
- use App\Jobs\RunStoreGeneration;
- use App\Models\Store;
- use App\Models\StoreGeneration;
- use Illuminate\Http\RedirectResponse;
- use Illuminate\Http\Request;
- use Illuminate\Validation\ValidationException;
- use Inertia\Inertia;
-
- /**
- * Starts AI jobs for a store. The work itself happens in the background (RunStoreGeneration).
- */
- class StoreGenerationController extends Controller
- {
- /**
- * Design the whole store with AI. The first design is free; later ones count as regenerations.
- */
- public function generate(Request $request, UsageLimits $usage, string $slug): RedirectResponse
- {
- $store = $this->ownedStore($request, $slug);
- $isRegeneration = $store->plan !== null;
-
- if ($this->isBusy($store)) {
- return $this->refuse($store, __('Your store is already being worked on. Please wait for it to finish.'));
- }
-
- if (! $usage->hasPlan($request->user())) {
- return $this->refuse($store, __('Your free trial has ended. Choose a plan to keep using AI.'));
- }
-
- if ($isRegeneration && ! $usage->canRegenerate($request->user())) {
- return $this->refuse($store, __('You have used all the regenerations in your plan. Upgrade for more.'));
- }
-
- $this->start($request, $store, [
- 'type' => GenerationType::Generate,
- 'is_regeneration' => $isRegeneration,
- ]);
-
- return to_route('stores.show', $store->slug);
- }
-
- /**
- * Ask the AI for one small change, in plain words.
- */
- public function edit(Request $request, UsageLimits $usage, string $slug): RedirectResponse
- {
- $store = $this->ownedStore($request, $slug);
-
- abort_if($store->plan === null, 404);
-
- $validated = $request->validate([
- 'instruction' => ['required', 'string', 'min:3', 'max:500'],
- ]);
-
- if ($this->isBusy($store)) {
- return $this->refuse($store, __('Your store is already being worked on. Please wait for it to finish.'));
- }
-
- if (! $usage->canEdit($request->user())) {
- return $this->refuse($store, $usage->hasPlan($request->user())
- ? __('You have used all the AI edits in your plan. Upgrade for more.')
- : __('Your free trial has ended. Choose a plan to keep using AI.'));
- }
-
- $this->start($request, $store, [
- 'type' => GenerationType::Edit,
- 'instruction' => $validated['instruction'],
- ]);
-
- return to_route('stores.show', $store->slug);
- }
-
- /**
- * Do the store changes the AI proposed in a chat edit. They are checked again first,
- * and either all of them happen or none.
- */
- public function confirm(Request $request, string $slug, int $generation): RedirectResponse
- {
- $store = $this->ownedStore($request, $slug);
- $edit = $this->pendingActions($store, $generation);
-
- try {
- ChatActions::apply($store, $edit->actions ?? []);
- } catch (ValidationException $e) {
- // Something changed since the AI proposed it, such as a code made by hand in the meantime.
- return $this->refuse($store, __('Nothing was changed: :problem', ['problem' => collect($e->errors())->flatten()->first()]));
- }
-
- $edit->actions_status = StoreGeneration::ACTIONS_CONFIRMED;
- $edit->save();
-
- Inertia::flash('toast', ['type' => 'success', 'message' => __('Done. Your store is updated.')]);
-
- return to_route('stores.show', $store->slug);
- }
-
- public function cancel(Request $request, string $slug, int $generation): RedirectResponse
- {
- $store = $this->ownedStore($request, $slug);
- $edit = $this->pendingActions($store, $generation);
-
- $edit->actions_status = StoreGeneration::ACTIONS_CANCELLED;
- $edit->save();
-
- return to_route('stores.show', $store->slug);
- }
-
- private function pendingActions(Store $store, int $id): StoreGeneration
- {
- return $store->generations()
- ->whereKey($id)
- ->where('actions_status', StoreGeneration::ACTIONS_PENDING)
- ->firstOrFail();
- }
-
- /**
- * @param array<string, mixed> $attributes
- */
- private function start(Request $request, Store $store, array $attributes): void
- {
- $generation = $store->generations()->create([
- ...$attributes,
- 'user_id' => $request->user()->id,
- 'status' => GenerationStatus::Queued,
- ]);
-
- RunStoreGeneration::dispatch($generation->id);
- }
-
- /**
- * One AI job per store at a time.
- */
- private function isBusy(Store $store): bool
- {
- StoreGeneration::failStale($store->id);
-
- return $store->generations()->active()->exists();
- }
-
- private function refuse(Store $store, string $message): RedirectResponse
- {
- Inertia::flash('toast', ['type' => 'error', 'message' => $message]);
-
- return to_route('stores.show', $store->slug);
- }
-
- private function ownedStore(Request $request, string $slug): Store
- {
- return $request->user()->stores()->where('slug', $slug)->firstOrFail();
- }
- }
|