Add script/TTS step handlers, dashboard, and pipeline UI (#35, #36, #43, #44)

StepHandler abstraction (app/Support/VideoWorkflow/StepHandlers/) plugs
into WorkflowEngine without coupling it to how any given step type is
actually executed:
- ScriptGenerationHandler: builds a prompt from the project's title/
  description (or an explicit outline/tone/keywords/target length in
  step input), generates via an active OpenAI AiProvider, stores the
  result as a 'script' asset.
- TtsVoiceoverHandler: voices step.input.text, or falls back to the
  project's latest script asset, via an active ElevenLabs AiProvider;
  voice_id/model_id are configurable per step. No direct "speed"
  control — ElevenLabs' base TTS endpoint doesn't expose one.

RunWorkflowStepJob resolves a step's handler by type and runs it on
the queue, matching the existing PublishPostJob pattern (including a
stale-dispatch guard: no-op if the step isn't 'in_progress' anymore).

UI, under its own /video-projects routes and nav entry — separate
from /projects per the earlier decision:
- VideoProjects/Index (#43): list projects with status and step
  progress, create new ones.
- VideoProjects/Show (#44): start a project from a WorkflowTemplate or
  a hardcoded default 8-step pipeline; per-step Run/Approve/Reject/
  Retry/Skip actions; auto-queues the next step after any transition
  when a handler exists for its type (steps with no handler yet just
  sit in_progress rather than auto-failing); lists project assets.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
m105
2026-08-06 20:56:01 +03:00
co-authored by Claude Sonnet 5
parent 0e315ada04
commit 55fa2ebfda
17 changed files with 1105 additions and 0 deletions
@@ -0,0 +1,91 @@
<?php
use App\Models\AiProvider;
use App\Models\VideoProject;
use App\Support\AiProviders\AiProviderManager;
use App\Support\AssetStorage\AssetStorage;
use App\Support\VideoWorkflow\StepHandlers\ScriptGenerationHandler;
use App\Support\VideoWorkflow\WorkflowEngine;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
beforeEach(function () {
Storage::fake();
$this->engine = new WorkflowEngine;
$this->handler = new ScriptGenerationHandler(app(AiProviderManager::class), new AssetStorage);
});
test('it generates a script, stores it as an asset, and completes the step', function () {
Http::fake([
'api.openai.com/*' => Http::response([
'choices' => [['message' => ['content' => 'Welcome back to the channel today']]],
]),
]);
AiProvider::factory()->create(['provider' => 'openai', 'is_active' => true]);
$project = VideoProject::factory()->create(['title' => 'Best Coffee Gadgets 2026']);
$step = $this->engine->start($project, [['type' => 'script_generation']]);
$this->handler->handle($step, $this->engine);
$fresh = $step->fresh();
expect($fresh->status)->toBe('completed');
expect($fresh->output['word_count'])->toBe(6);
$asset = \App\Models\Asset::find($fresh->output['asset_id']);
expect($asset->type)->toBe('script');
expect($asset->video_project_id)->toBe($project->id);
expect(Storage::get($asset->disk_path))->toBe('Welcome back to the channel today');
Http::assertSent(fn ($request) => str_contains($request['messages'][0]['content'], 'Best Coffee Gadgets 2026'));
});
test('it uses an explicit outline, tone, keywords, and target length when given', function () {
Http::fake(['api.openai.com/*' => Http::response(['choices' => [['message' => ['content' => 'script']]]])]);
AiProvider::factory()->create(['provider' => 'openai']);
$project = VideoProject::factory()->create();
$step = $this->engine->start($project, [['type' => 'script_generation']]);
$step->update(['input' => [
'outline' => 'A history of espresso machines',
'tone' => 'casual and funny',
'seo_keywords' => ['espresso', 'coffee history'],
'target_words' => 800,
]]);
$this->handler->handle($step->fresh(), $this->engine);
Http::assertSent(function ($request) {
$prompt = $request['messages'][0]['content'];
return str_contains($prompt, 'A history of espresso machines')
&& str_contains($prompt, 'casual and funny')
&& str_contains($prompt, 'espresso, coffee history')
&& str_contains($prompt, 'approximately 800 words');
});
});
test('it fails the step when no active OpenAI provider is configured', function () {
$project = VideoProject::factory()->create();
$step = $this->engine->start($project, [['type' => 'script_generation']]);
$this->handler->handle($step, $this->engine);
expect($step->fresh())
->status->toBe('failed')
->error->toContain('No active OpenAI provider');
});
test('it fails the step when the AI provider call fails', function () {
Http::fake(['api.openai.com/*' => Http::response(['error' => ['message' => 'rate limited']], 429)]);
AiProvider::factory()->create(['provider' => 'openai']);
$project = VideoProject::factory()->create();
$step = $this->engine->start($project, [['type' => 'script_generation']]);
$this->handler->handle($step, $this->engine);
expect($step->fresh())
->status->toBe('failed')
->error->toBe('rate limited');
});
@@ -0,0 +1,92 @@
<?php
use App\Models\AiProvider;
use App\Models\VideoProject;
use App\Support\AiProviders\AiProviderManager;
use App\Support\AssetStorage\AssetStorage;
use App\Support\VideoWorkflow\StepHandlers\TtsVoiceoverHandler;
use App\Support\VideoWorkflow\WorkflowEngine;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
beforeEach(function () {
Storage::fake();
$this->engine = new WorkflowEngine;
$this->assets = new AssetStorage;
$this->handler = new TtsVoiceoverHandler(app(AiProviderManager::class), $this->assets);
});
test('it voices explicit input text and completes the step', function () {
Http::fake(['api.elevenlabs.io/*' => Http::response('fake-mp3-bytes', 200, ['Content-Type' => 'audio/mpeg'])]);
AiProvider::factory()->create(['provider' => 'elevenlabs', 'config' => ['voice_id' => 'v1']]);
$project = VideoProject::factory()->create();
$step = $this->engine->start($project, [['type' => 'tts_voiceover']]);
$step->update(['input' => ['text' => 'Hello and welcome']]);
$this->handler->handle($step->fresh(), $this->engine);
$fresh = $step->fresh();
expect($fresh->status)->toBe('completed');
expect($fresh->output['characters'])->toBe(strlen('Hello and welcome'));
$asset = \App\Models\Asset::find($fresh->output['asset_id']);
expect($asset->type)->toBe('audio');
expect($asset->mime_type)->toBe('audio/mpeg');
expect(Storage::get($asset->disk_path))->toBe('fake-mp3-bytes');
Http::assertSent(fn ($request) => $request['text'] === 'Hello and welcome');
});
test('it falls back to the latest script asset when no input text is given', function () {
Http::fake(['api.elevenlabs.io/*' => Http::response('bytes')]);
AiProvider::factory()->create(['provider' => 'elevenlabs', 'config' => ['voice_id' => 'v1']]);
$project = VideoProject::factory()->create();
$this->assets->store($project, 'script', 'Old draft', 'txt');
$this->assets->store($project, 'script', 'Latest draft', 'txt');
$step = $this->engine->start($project, [['type' => 'tts_voiceover']]);
$this->handler->handle($step, $this->engine);
Http::assertSent(fn ($request) => $request['text'] === 'Latest draft');
});
test('a voice_id in step input overrides the provider default', function () {
Http::fake(['api.elevenlabs.io/*' => Http::response('bytes')]);
AiProvider::factory()->create(['provider' => 'elevenlabs', 'config' => ['voice_id' => 'default-voice']]);
$project = VideoProject::factory()->create();
$step = $this->engine->start($project, [['type' => 'tts_voiceover']]);
$step->update(['input' => ['text' => 'hi', 'voice_id' => 'custom-voice']]);
$this->handler->handle($step->fresh(), $this->engine);
Http::assertSent(fn ($request) => str_contains($request->url(), '/text-to-speech/custom-voice'));
});
test('it fails the step when there is no text to voice', function () {
AiProvider::factory()->create(['provider' => 'elevenlabs', 'config' => ['voice_id' => 'v1']]);
$project = VideoProject::factory()->create();
$step = $this->engine->start($project, [['type' => 'tts_voiceover']]);
$this->handler->handle($step, $this->engine);
expect($step->fresh())
->status->toBe('failed')
->error->toContain('No script text available');
});
test('it fails the step when no active ElevenLabs provider is configured', function () {
$project = VideoProject::factory()->create();
$step = $this->engine->start($project, [['type' => 'tts_voiceover']]);
$step->update(['input' => ['text' => 'hi']]);
$this->handler->handle($step->fresh(), $this->engine);
expect($step->fresh())
->status->toBe('failed')
->error->toContain('No active ElevenLabs provider');
});