| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
-
- namespace App\Models;
-
- use App\Enums\GenerationStatus;
- use App\Enums\GenerationType;
- use Carbon\CarbonInterface;
- use Database\Factories\StoreGenerationFactory;
- use Illuminate\Database\Eloquent\Attributes\Fillable;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Support\Carbon;
-
- /**
- * One AI job for a store: a full design or one chat edit.
- *
- * @property int $id
- * @property int $store_id
- * @property int $user_id
- * @property GenerationType $type
- * @property GenerationStatus $status
- * @property bool $is_regeneration
- * @property string|null $instruction
- * @property string|null $reply
- * @property bool|null $changed
- * @property string|null $error
- * @property string|null $provider
- * @property string|null $model
- * @property int $input_tokens
- * @property int $output_tokens
- * @property CarbonInterface|null $started_at
- * @property CarbonInterface|null $finished_at
- * @property Carbon|null $created_at
- * @property Carbon|null $updated_at
- * @property-read Store $store
- */
- #[Fillable(['user_id', 'type', 'status', 'is_regeneration', 'instruction'])]
- class StoreGeneration extends Model
- {
- /** @use HasFactory<StoreGenerationFactory> */
- use HasFactory;
-
- /**
- * @return BelongsTo<Store, $this>
- */
- public function store(): BelongsTo
- {
- return $this->belongsTo(Store::class);
- }
-
- /**
- * Jobs that are waiting or running.
- *
- * @param Builder<StoreGeneration> $query
- */
- public function scopeActive(Builder $query): void
- {
- $query->whereIn('status', [GenerationStatus::Queued, GenerationStatus::Running]);
- }
-
- /**
- * Mark jobs that have been waiting or running for too long as failed.
- * This happens when the background worker is not running or was stopped mid-job.
- */
- public static function failStale(int $storeId): void
- {
- static::query()
- ->where('store_id', $storeId)
- ->active()
- ->where('created_at', '<', now()->subMinutes((int) config('plans.stale_after_minutes', 10)))
- ->update([
- 'status' => GenerationStatus::Failed,
- 'error' => 'This took too long and was stopped. Please try again.',
- 'finished_at' => now(),
- ]);
- }
-
- public function markFailed(string $message): void
- {
- $this->status = GenerationStatus::Failed;
- $this->error = mb_substr($message, 0, 500);
- $this->finished_at = now();
- $this->save();
- }
-
- /**
- * @return array<string, string>
- */
- protected function casts(): array
- {
- return [
- 'type' => GenerationType::class,
- 'status' => GenerationStatus::class,
- 'is_regeneration' => 'boolean',
- 'changed' => 'boolean',
- 'started_at' => 'datetime',
- 'finished_at' => 'datetime',
- ];
- }
- }
|