Files
m105andClaude Sonnet 5 6a51fda61e Add thumbnail, metadata, and YouTube publishing handlers (#40, #41, #42)
ThumbnailGenerationHandler (#40): generates thumbnail candidates via
StabilityAI, one per style (default: two contrasting styles) — all
stored as assets on the same step, so the existing approval UI's
"show every asset on this step side by side" behavior is the A/B
comparison the task calls for, no extra UI needed.

MetadataGenerationHandler (#41): SEO title/description/tags/hashtags
from the project's script via OpenAI in JSON mode, same pattern as
StoryboardHandler.

YoutubePublishHandler (#42): real OAuth2 + YouTube Data API v3
resumable upload (token refresh -> init session -> PUT video bytes),
supporting scheduling via publishAt (forces privacyStatus=private per
YouTube's own constraint). Reuses the AiProvider table for credentials
(provider='youtube', api_key=refresh_token, config=client_id/secret)
rather than a new table — added 'youtube' to AiProvider::PROVIDERS.
Does not cover analytics retrieval: that's an ongoing query against a
published video, not a one-shot publish action, so it doesn't fit the
per-video step model at all.

All 8 WorkflowStep::TYPES now have real handlers, so the "unhandled
type" test fixtures (which used real-but-not-yet-implemented types)
would go stale every time a new module lands — switched them to
fabricated type strings so they stay meaningful regardless of what
gets built next.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-06 21:26:56 +03:00

87 lines
2.8 KiB
PHP

<?php
namespace App\Support\VideoWorkflow\StepHandlers;
use App\Models\AiProvider;
use App\Models\VideoProject;
use App\Models\WorkflowStep;
use App\Support\AiProviders\AiGenerationException;
use App\Support\AiProviders\AiProviderManager;
use App\Support\AssetStorage\AssetStorage;
use App\Support\VideoWorkflow\WorkflowEngine;
/**
* Generates thumbnail candidates via a StabilityAI-backed AiProvider. One
* variant per style in step.input.styles (default: two contrasting styles),
* all stored as assets on the same step — the approval UI already shows
* every asset on an awaiting_approval step side by side, so this is the
* A/B comparison the task calls for, with no extra UI needed.
*/
class ThumbnailGenerationHandler implements StepHandler
{
private const DEFAULT_STYLES = ['vibrant and colorful', 'minimalist and clean'];
public function __construct(
private readonly AiProviderManager $aiProviders,
private readonly AssetStorage $assets,
) {}
public function type(): string
{
return 'thumbnail_generation';
}
public function handle(WorkflowStep $step, WorkflowEngine $engine): void
{
$provider = AiProvider::query()->where('provider', 'stability_ai')->where('is_active', true)->first();
if (! $provider) {
$engine->failStep($step, 'No active StabilityAI provider configured.');
return;
}
$input = $step->input ?? [];
$basePrompt = $input['prompt'] ?? $this->defaultPrompt($step->videoProject);
$styles = $input['styles'] ?? self::DEFAULT_STYLES;
$assetIds = [];
foreach (array_values($styles) as $i => $style) {
$prompt = "{$basePrompt}. Eye-catching YouTube thumbnail, bold, high contrast, {$style}.";
try {
$result = $this->aiProviders->generate($provider, $prompt);
} catch (AiGenerationException $e) {
$engine->failStep($step, 'variant '.($i + 1).": {$e->getMessage()}");
return;
}
$extension = str($result->contentType)->after('image/')->toString() ?: 'png';
$asset = $this->assets->store(
$step->videoProject,
'image',
$result->content,
$extension,
step: $step,
mimeType: $result->contentType,
metadata: ['kind' => 'thumbnail', 'variant' => $i + 1, 'style' => $style],
);
$assetIds[] = $asset->id;
}
$engine->completeStep($step, [
'asset_ids' => $assetIds,
'variant_count' => count($assetIds),
]);
}
private function defaultPrompt(VideoProject $project): string
{
return trim(($project->title ?? '').'. '.($project->description ?? ''));
}
}