, summary: string}>|null $actions Store changes waiting for the merchant's OK * @property string|null $actions_status "pending", "confirmed" or "cancelled" * @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 */ use HasFactory; public const string ACTIONS_PENDING = 'pending'; public const string ACTIONS_CONFIRMED = 'confirmed'; public const string ACTIONS_CANCELLED = 'cancelled'; /** * @return BelongsTo */ public function store(): BelongsTo { return $this->belongsTo(Store::class); } /** * Jobs that are waiting or running. * * @param Builder $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 */ protected function casts(): array { return [ 'type' => GenerationType::class, 'status' => GenerationStatus::class, 'is_regeneration' => 'boolean', 'changed' => 'boolean', 'actions' => 'array', 'started_at' => 'datetime', 'finished_at' => 'datetime', ]; } }