find($this->generationId); if ($generation === null || $generation->status !== GenerationStatus::Queued) { return; } $generation->status = GenerationStatus::Running; $generation->started_at = now(); $generation->save(); try { $store = $generation->store; $result = $generation->type === GenerationType::Edit ? $writer->edit($store, (string) $generation->instruction) : $writer->generate($store); // Compare the design before and after, so the app (not the AI's reply) says whether anything changed. $changed = $result->plan != $store->plan; $store->plan = $result->plan; $store->save(); $generation->status = GenerationStatus::Completed; $generation->reply = $result->reply; $generation->changed = $changed; $generation->provider = $result->provider; $generation->model = $result->model; $generation->input_tokens = $result->inputTokens; $generation->output_tokens = $result->outputTokens; $generation->finished_at = now(); $generation->save(); } catch (Throwable $e) { report($e); $generation->markFailed($e instanceof AiException ? $e->friendlyMessage() : 'Something went wrong. Please try again.'); } } /** * Called when the job dies without reaching the catch block, for example on a timeout. */ public function failed(?Throwable $e): void { StoreGeneration::query() ->whereKey($this->generationId) ->active() ->first() ?->markFailed('This took too long and was stopped. Please try again.'); } }