Add Video Projects schema and workflow engine core (tasks #31, #32)

Foundational layer for the YouTube Content Creation System project,
built as a fully independent domain per the earlier decision (task
#47) — no foreign keys into the existing projects/tasks/ideas tables.

Schema: video_projects, workflow_steps, assets, ai_providers,
workflow_templates (named to avoid colliding with the existing
Post-templates `templates` table).

WorkflowEngine (app/Support/VideoWorkflow/) drives a project's step
sequence: start/startFromTemplate, currentStep, completeStep (with
optional approval gating), approveStep/rejectStep, failStep/retryStep,
skipStep. It only manages state transitions — actual step execution
(AI calls, rendering, etc.) is out of scope here (tasks #33+).

Also fixes four Publisher classes (WordPress/Reddit/Twitter/LinkedIn)
that were missing a `use App\Models\SocialAccount;` import after an
earlier interface change, which broke the entire test suite at
bootstrap; and PostFactory, which still set the now-removed
`social_account_id` column instead of the SocialAccount pivot.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
m105
2026-08-06 20:38:52 +03:00
co-authored by Claude Sonnet 5
parent efdec9eabd
commit 6ff7f78c9c
25 changed files with 1049 additions and 25 deletions
+26
View File
@@ -0,0 +1,26 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class AiProvider extends Model
{
use HasFactory;
public const PROVIDERS = ['openai', 'stability_ai', 'elevenlabs', 'other'];
protected $fillable = [
'name',
'provider',
'api_key',
'config',
'is_active',
];
protected $casts = [
'config' => 'array',
'is_active' => 'boolean',
];
}
+39
View File
@@ -0,0 +1,39 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Asset extends Model
{
use HasFactory;
public const TYPES = ['script', 'image', 'video', 'audio', 'other'];
protected $fillable = [
'video_project_id',
'workflow_step_id',
'type',
'disk_path',
'mime_type',
'size_bytes',
'version',
'metadata',
];
protected $casts = [
'metadata' => 'array',
];
public function videoProject(): BelongsTo
{
return $this->belongsTo(VideoProject::class);
}
public function workflowStep(): BelongsTo
{
return $this->belongsTo(WorkflowStep::class);
}
}
+38
View File
@@ -0,0 +1,38 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
class VideoProject extends Model
{
use HasFactory, SoftDeletes;
public const STATUSES = ['draft', 'in_progress', 'completed', 'archived'];
protected $fillable = [
'title',
'description',
'status',
'workflow_template_id',
];
public function workflowTemplate(): BelongsTo
{
return $this->belongsTo(WorkflowTemplate::class);
}
public function steps(): HasMany
{
return $this->hasMany(WorkflowStep::class)->orderBy('order');
}
public function assets(): HasMany
{
return $this->hasMany(Asset::class);
}
}
+62
View File
@@ -0,0 +1,62 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class WorkflowStep extends Model
{
use HasFactory;
// Mirrors the content-generation modules in the YouTube Content
// Creation System project (script through publish).
public const TYPES = [
'script_generation',
'tts_voiceover',
'storyboard',
'video_generation',
'video_editing',
'thumbnail_generation',
'metadata_generation',
'youtube_publish',
];
public const STATUSES = [
'pending', 'in_progress', 'awaiting_approval',
'completed', 'rejected', 'failed', 'skipped',
];
protected $fillable = [
'video_project_id',
'type',
'order',
'status',
'requires_approval',
'input',
'output',
'error',
'started_at',
'completed_at',
];
protected $casts = [
'requires_approval' => 'boolean',
'input' => 'array',
'output' => 'array',
'started_at' => 'datetime',
'completed_at' => 'datetime',
];
public function videoProject(): BelongsTo
{
return $this->belongsTo(VideoProject::class);
}
public function assets(): HasMany
{
return $this->hasMany(Asset::class);
}
}
+27
View File
@@ -0,0 +1,27 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class WorkflowTemplate extends Model
{
use HasFactory;
protected $fillable = [
'name',
'description',
'steps',
];
protected $casts = [
'steps' => 'array',
];
public function videoProjects(): HasMany
{
return $this->hasMany(VideoProject::class);
}
}
@@ -3,6 +3,7 @@
namespace App\Support\Publishing;
use App\Models\Post;
use App\Models\SocialAccount;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Process;
@@ -3,6 +3,7 @@
namespace App\Support\Publishing;
use App\Models\Post;
use App\Models\SocialAccount;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Process;
@@ -3,6 +3,7 @@
namespace App\Support\Publishing;
use App\Models\Post;
use App\Models\SocialAccount;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Process;
@@ -4,6 +4,7 @@ namespace App\Support\Publishing;
use App\Models\Post;
use App\Models\Site;
use App\Models\SocialAccount;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\Http;
@@ -0,0 +1,191 @@
<?php
namespace App\Support\VideoWorkflow;
use App\Models\VideoProject;
use App\Models\WorkflowStep;
use App\Models\WorkflowTemplate;
/**
* Drives a VideoProject's pipeline: creates its step sequence and manages
* step state transitions (execution, approval, failure, retry).
*
* Steps run strictly in `order`. Each step is one of:
* pending -> in_progress -> awaiting_approval -> completed
* \-> completed (skips approval if not required)
* \-> failed -> in_progress (retry)
* awaiting_approval -> rejected -> in_progress (retry)
* pending | in_progress -> skipped
*
* This class only manages state — it doesn't know how to actually generate
* a script, render video, etc. Whatever does that work (a future AI
* provider integration, a human, an Artisan command) calls completeStep()/
* failStep() to report the result.
*/
class WorkflowEngine
{
/**
* Create the step sequence for a project and activate the first one.
*
* @param array<int, array{type: string, requires_approval?: bool}> $steps
*/
public function start(VideoProject $project, array $steps): WorkflowStep
{
if ($project->steps()->exists()) {
throw new WorkflowTransitionException("Video project {$project->id} already has workflow steps.");
}
if (empty($steps)) {
throw new WorkflowTransitionException('Cannot start a workflow with no steps.');
}
foreach (array_values($steps) as $order => $step) {
$project->steps()->create([
'type' => $step['type'],
'order' => $order,
'status' => 'pending',
'requires_approval' => $step['requires_approval'] ?? false,
]);
}
$project->update(['status' => 'in_progress']);
$first = $project->steps()->orderBy('order')->firstOrFail();
return $this->activate($first);
}
public function startFromTemplate(VideoProject $project, WorkflowTemplate $template): WorkflowStep
{
$project->update(['workflow_template_id' => $template->id]);
return $this->start($project, $template->steps);
}
/**
* The step currently blocking progress — pending, running, awaiting
* approval, or stuck on failure/rejection. Null once every step is
* completed or skipped.
*/
public function currentStep(VideoProject $project): ?WorkflowStep
{
return $project->steps()
->whereNotIn('status', ['completed', 'skipped'])
->orderBy('order')
->first();
}
public function completeStep(WorkflowStep $step, array $output = []): WorkflowStep
{
$this->assertStatus($step, 'in_progress');
$step->update([
'output' => $output,
'error' => null,
'completed_at' => now(),
'status' => $step->requires_approval ? 'awaiting_approval' : 'completed',
]);
if ($step->status === 'completed') {
$this->activateNext($step);
}
return $step->fresh();
}
public function approveStep(WorkflowStep $step): WorkflowStep
{
$this->assertStatus($step, 'awaiting_approval');
$step->update(['status' => 'completed']);
$this->activateNext($step);
return $step->fresh();
}
/**
* Rejecting does not advance the pipeline — it stays blocked on this
* step until retryStep() is called.
*/
public function rejectStep(WorkflowStep $step, ?string $reason = null): WorkflowStep
{
$this->assertStatus($step, 'awaiting_approval');
$step->update(['status' => 'rejected', 'error' => $reason]);
return $step->fresh();
}
public function failStep(WorkflowStep $step, string $error): WorkflowStep
{
$this->assertStatus($step, 'in_progress');
$step->update(['status' => 'failed', 'error' => $error]);
return $step->fresh();
}
public function retryStep(WorkflowStep $step): WorkflowStep
{
if (! in_array($step->status, ['failed', 'rejected'], true)) {
throw new WorkflowTransitionException("Step {$step->id} can only be retried from failed or rejected, currently '{$step->status}'.");
}
$step->update([
'status' => 'in_progress',
'error' => null,
'output' => null,
'started_at' => now(),
'completed_at' => null,
]);
return $step->fresh();
}
public function skipStep(WorkflowStep $step): WorkflowStep
{
if (! in_array($step->status, ['pending', 'in_progress'], true)) {
throw new WorkflowTransitionException("Step {$step->id} can only be skipped from pending or in_progress, currently '{$step->status}'.");
}
$step->update(['status' => 'skipped', 'completed_at' => now()]);
$this->activateNext($step);
return $step->fresh();
}
private function activate(WorkflowStep $step): WorkflowStep
{
$step->update(['status' => 'in_progress', 'started_at' => now()]);
return $step->fresh();
}
private function activateNext(WorkflowStep $justFinished): void
{
$project = $justFinished->videoProject;
$next = $project->steps()
->where('order', '>', $justFinished->order)
->whereNotIn('status', ['completed', 'skipped'])
->orderBy('order')
->first();
if ($next) {
$this->activate($next);
return;
}
$project->update(['status' => 'completed']);
}
private function assertStatus(WorkflowStep $step, string $expected): void
{
if ($step->status !== $expected) {
throw new WorkflowTransitionException("Step {$step->id} must be '{$expected}' for this transition, currently '{$step->status}'.");
}
}
}
@@ -0,0 +1,7 @@
<?php
namespace App\Support\VideoWorkflow;
use RuntimeException;
class WorkflowTransitionException extends RuntimeException {}