Add AI provider abstraction and asset storage (tasks #33, #34)

AiProviderDriver: one generic interface (generate(prompt, options) ->
AiGenerationResult) rather than per-capability interfaces, since text
(OpenAI chat completions), image (StabilityAI stable-image), and
speech (ElevenLabs TTS) generation all reduce to prompt-in/content-out.
Drivers throw AiGenerationException on failure; AiProviderManager
resolves the right driver from an AiProvider record's `provider`
column and guards on is_active. Real request/response shapes per each
provider's API, verified with Http::fake() — no live API keys
available yet to smoke test against the real services.

AssetStorage: stores generated content under
video-projects/{id}/{type}/v{n}.{ext}, versioned per (project, type[,
step]) slot so retries don't clobber earlier output, plus
contents/url/delete and pruneOldVersions for cleanup.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
m105
2026-08-06 20:47:06 +03:00
co-authored by Claude Sonnet 5
parent a4ff6c0ccb
commit 0e315ada04
14 changed files with 657 additions and 0 deletions
+94
View File
@@ -0,0 +1,94 @@
<?php
namespace App\Support\AssetStorage;
use App\Models\Asset;
use App\Models\VideoProject;
use App\Models\WorkflowStep;
use Illuminate\Support\Facades\Storage;
/**
* Stores generated content (scripts, images, video, audio) on disk and
* records it as an Asset. Versioned per (project, type[, step]) slot —
* storing again for the same slot doesn't overwrite, it adds the next
* version, so a rejected/retried step's earlier output stays around.
*/
class AssetStorage
{
/**
* @param array<string, mixed> $metadata
*/
public function store(
VideoProject $project,
string $type,
string $contents,
string $extension,
?WorkflowStep $step = null,
?string $mimeType = null,
array $metadata = [],
): Asset {
$version = $this->nextVersion($project, $type, $step);
$path = "video-projects/{$project->id}/{$type}/v{$version}.".ltrim($extension, '.');
Storage::put($path, $contents);
return $project->assets()->create([
'workflow_step_id' => $step?->id,
'type' => $type,
'disk_path' => $path,
'mime_type' => $mimeType,
'size_bytes' => strlen($contents),
'version' => $version,
'metadata' => $metadata,
]);
}
public function nextVersion(VideoProject $project, string $type, ?WorkflowStep $step = null): int
{
$query = $project->assets()->where('type', $type);
if ($step) {
$query->where('workflow_step_id', $step->id);
}
return ((int) $query->max('version')) + 1;
}
public function contents(Asset $asset): string
{
return Storage::get($asset->disk_path);
}
public function url(Asset $asset): ?string
{
return Storage::exists($asset->disk_path) ? Storage::url($asset->disk_path) : null;
}
public function delete(Asset $asset): void
{
Storage::delete($asset->disk_path);
$asset->delete();
}
/**
* Keep only the newest $keep versions for a (project, type[, step])
* slot; delete the file + record for the rest. Returns how many were
* pruned.
*/
public function pruneOldVersions(VideoProject $project, string $type, ?WorkflowStep $step = null, int $keep = 3): int
{
$query = $project->assets()->where('type', $type);
if ($step) {
$query->where('workflow_step_id', $step->id);
}
$stale = $query->orderByDesc('version')->get()->slice($keep);
foreach ($stale as $asset) {
$this->delete($asset);
}
return $stale->count();
}
}