AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

RunStoreGeneration.php 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Jobs;
  3. use App\Ai\AiException;
  4. use App\Enums\GenerationStatus;
  5. use App\Enums\GenerationType;
  6. use App\Models\StoreGeneration;
  7. use App\StorePlan\Ai\PlanWriter;
  8. use Illuminate\Contracts\Queue\ShouldQueue;
  9. use Illuminate\Foundation\Queue\Queueable;
  10. use Throwable;
  11. /**
  12. * Runs one AI job in the background: a full store design or one chat edit.
  13. * The store's design only changes if the AI answer passes the store plan check.
  14. */
  15. class RunStoreGeneration implements ShouldQueue
  16. {
  17. use Queueable;
  18. /** AI calls cost money, so a failed job is never retried on its own. The merchant retries. */
  19. public int $tries = 1;
  20. /** Seconds. Must stay below DB_QUEUE_RETRY_AFTER in .env. */
  21. public int $timeout = 540;
  22. public function __construct(public int $generationId) {}
  23. public function handle(PlanWriter $writer): void
  24. {
  25. $generation = StoreGeneration::with('store')->find($this->generationId);
  26. if ($generation === null || $generation->status !== GenerationStatus::Queued) {
  27. return;
  28. }
  29. $generation->status = GenerationStatus::Running;
  30. $generation->started_at = now();
  31. $generation->save();
  32. try {
  33. $store = $generation->store;
  34. $result = $generation->type === GenerationType::Edit
  35. ? $writer->edit($store, (string) $generation->instruction)
  36. : $writer->generate($store);
  37. $store->plan = $result->plan;
  38. $store->save();
  39. $generation->status = GenerationStatus::Completed;
  40. $generation->reply = $result->reply;
  41. $generation->provider = $result->provider;
  42. $generation->model = $result->model;
  43. $generation->input_tokens = $result->inputTokens;
  44. $generation->output_tokens = $result->outputTokens;
  45. $generation->finished_at = now();
  46. $generation->save();
  47. } catch (Throwable $e) {
  48. report($e);
  49. $generation->markFailed($e instanceof AiException
  50. ? $e->friendlyMessage()
  51. : 'Something went wrong. Please try again.');
  52. }
  53. }
  54. /**
  55. * Called when the job dies without reaching the catch block, for example on a timeout.
  56. */
  57. public function failed(?Throwable $e): void
  58. {
  59. StoreGeneration::query()
  60. ->whereKey($this->generationId)
  61. ->active()
  62. ->first()
  63. ?->markFailed('This took too long and was stopped. Please try again.');
  64. }
  65. }