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>
50 lines
1.7 KiB
PHP
50 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Support\AiProviders;
|
|
|
|
use App\Models\AiProvider;
|
|
use Illuminate\Http\Client\ConnectionException;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
/**
|
|
* Image generation via StabilityAI's Stable Image Core API. Used for
|
|
* storyboard frames, thumbnails, and scene visuals.
|
|
*/
|
|
class StabilityAiDriver implements AiProviderDriver
|
|
{
|
|
public function generate(AiProvider $provider, string $prompt, array $options = []): AiGenerationResult
|
|
{
|
|
$outputFormat = $options['output_format'] ?? $provider->config['output_format'] ?? 'png';
|
|
|
|
try {
|
|
$response = Http::withToken($provider->api_key)
|
|
->accept('application/json')
|
|
->asMultipart()
|
|
->attach('prompt', $prompt)
|
|
->attach('output_format', $outputFormat)
|
|
->post('https://api.stability.ai/v2beta/stable-image/generate/core');
|
|
} catch (ConnectionException $e) {
|
|
throw new AiGenerationException("could not connect to StabilityAI: {$e->getMessage()}");
|
|
}
|
|
|
|
if ($response->failed()) {
|
|
$error = $response->json('errors.0') ?? $response->json('message') ?? 'unexpected response from StabilityAI';
|
|
|
|
throw new AiGenerationException($error);
|
|
}
|
|
|
|
$base64 = $response->json('image');
|
|
|
|
if (! $base64) {
|
|
throw new AiGenerationException('unexpected response from StabilityAI: missing image data');
|
|
}
|
|
|
|
$bytes = base64_decode($base64);
|
|
|
|
return new AiGenerationResult($bytes, "image/{$outputFormat}", [
|
|
'seed' => $response->json('seed'),
|
|
'finish_reason' => $response->json('finish_reason'),
|
|
]);
|
|
}
|
|
}
|