AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StoreGeneration.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace App\Models;
  3. use App\Enums\GenerationStatus;
  4. use App\Enums\GenerationType;
  5. use Carbon\CarbonInterface;
  6. use Database\Factories\StoreGenerationFactory;
  7. use Illuminate\Database\Eloquent\Attributes\Fillable;
  8. use Illuminate\Database\Eloquent\Builder;
  9. use Illuminate\Database\Eloquent\Factories\HasFactory;
  10. use Illuminate\Database\Eloquent\Model;
  11. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  12. use Illuminate\Support\Carbon;
  13. /**
  14. * One AI job for a store: a full design or one chat edit.
  15. *
  16. * @property int $id
  17. * @property int $store_id
  18. * @property int $user_id
  19. * @property GenerationType $type
  20. * @property GenerationStatus $status
  21. * @property bool $is_regeneration
  22. * @property string|null $instruction
  23. * @property string|null $reply
  24. * @property bool|null $changed
  25. * @property list<array{type: string, input: array<string, mixed>, summary: string}>|null $actions Store changes waiting for the merchant's OK
  26. * @property string|null $actions_status "pending", "confirmed" or "cancelled"
  27. * @property string|null $error
  28. * @property array{section: string, label: string, detail: string|null}|null $target The part the merchant selected in the preview, if any.
  29. * @property string|null $provider
  30. * @property string|null $model
  31. * @property int $input_tokens
  32. * @property int $output_tokens
  33. * @property CarbonInterface|null $started_at
  34. * @property CarbonInterface|null $finished_at
  35. * @property Carbon|null $created_at
  36. * @property Carbon|null $updated_at
  37. * @property-read Store $store
  38. */
  39. #[Fillable(['user_id', 'type', 'status', 'is_regeneration', 'instruction', 'target'])]
  40. class StoreGeneration extends Model
  41. {
  42. /** @use HasFactory<StoreGenerationFactory> */
  43. use HasFactory;
  44. public const string ACTIONS_PENDING = 'pending';
  45. public const string ACTIONS_CONFIRMED = 'confirmed';
  46. public const string ACTIONS_CANCELLED = 'cancelled';
  47. /**
  48. * @return BelongsTo<Store, $this>
  49. */
  50. public function store(): BelongsTo
  51. {
  52. return $this->belongsTo(Store::class);
  53. }
  54. /**
  55. * Jobs that are waiting or running.
  56. *
  57. * @param Builder<StoreGeneration> $query
  58. */
  59. public function scopeActive(Builder $query): void
  60. {
  61. $query->whereIn('status', [GenerationStatus::Queued, GenerationStatus::Running]);
  62. }
  63. /**
  64. * Mark jobs that have been waiting or running for too long as failed.
  65. * This happens when the background worker is not running or was stopped mid-job.
  66. */
  67. public static function failStale(int $storeId): void
  68. {
  69. static::query()
  70. ->where('store_id', $storeId)
  71. ->active()
  72. ->where('created_at', '<', now()->subMinutes((int) config('plans.stale_after_minutes', 10)))
  73. ->update([
  74. 'status' => GenerationStatus::Failed,
  75. 'error' => 'This took too long and was stopped. Please try again.',
  76. 'finished_at' => now(),
  77. ]);
  78. }
  79. public function markFailed(string $message): void
  80. {
  81. $this->status = GenerationStatus::Failed;
  82. $this->error = mb_substr($message, 0, 500);
  83. $this->finished_at = now();
  84. $this->save();
  85. }
  86. /**
  87. * @return array<string, string>
  88. */
  89. protected function casts(): array
  90. {
  91. return [
  92. 'type' => GenerationType::class,
  93. 'status' => GenerationStatus::class,
  94. 'is_regeneration' => 'boolean',
  95. 'changed' => 'boolean',
  96. 'actions' => 'array',
  97. 'target' => 'array',
  98. 'started_at' => 'datetime',
  99. 'finished_at' => 'datetime',
  100. ];
  101. }
  102. }