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 $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(); } }