AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

StoreGeneration.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 string|null $provider
  29. * @property string|null $model
  30. * @property int $input_tokens
  31. * @property int $output_tokens
  32. * @property CarbonInterface|null $started_at
  33. * @property CarbonInterface|null $finished_at
  34. * @property Carbon|null $created_at
  35. * @property Carbon|null $updated_at
  36. * @property-read Store $store
  37. */
  38. #[Fillable(['user_id', 'type', 'status', 'is_regeneration', 'instruction'])]
  39. class StoreGeneration extends Model
  40. {
  41. /** @use HasFactory<StoreGenerationFactory> */
  42. use HasFactory;
  43. public const string ACTIONS_PENDING = 'pending';
  44. public const string ACTIONS_CONFIRMED = 'confirmed';
  45. public const string ACTIONS_CANCELLED = 'cancelled';
  46. /**
  47. * @return BelongsTo<Store, $this>
  48. */
  49. public function store(): BelongsTo
  50. {
  51. return $this->belongsTo(Store::class);
  52. }
  53. /**
  54. * Jobs that are waiting or running.
  55. *
  56. * @param Builder<StoreGeneration> $query
  57. */
  58. public function scopeActive(Builder $query): void
  59. {
  60. $query->whereIn('status', [GenerationStatus::Queued, GenerationStatus::Running]);
  61. }
  62. /**
  63. * Mark jobs that have been waiting or running for too long as failed.
  64. * This happens when the background worker is not running or was stopped mid-job.
  65. */
  66. public static function failStale(int $storeId): void
  67. {
  68. static::query()
  69. ->where('store_id', $storeId)
  70. ->active()
  71. ->where('created_at', '<', now()->subMinutes((int) config('plans.stale_after_minutes', 10)))
  72. ->update([
  73. 'status' => GenerationStatus::Failed,
  74. 'error' => 'This took too long and was stopped. Please try again.',
  75. 'finished_at' => now(),
  76. ]);
  77. }
  78. public function markFailed(string $message): void
  79. {
  80. $this->status = GenerationStatus::Failed;
  81. $this->error = mb_substr($message, 0, 500);
  82. $this->finished_at = now();
  83. $this->save();
  84. }
  85. /**
  86. * @return array<string, string>
  87. */
  88. protected function casts(): array
  89. {
  90. return [
  91. 'type' => GenerationType::class,
  92. 'status' => GenerationStatus::class,
  93. 'is_regeneration' => 'boolean',
  94. 'changed' => 'boolean',
  95. 'actions' => 'array',
  96. 'started_at' => 'datetime',
  97. 'finished_at' => 'datetime',
  98. ];
  99. }
  100. }