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:
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\AiProviders;
|
||||
|
||||
use App\Models\AiProvider;
|
||||
use Illuminate\Http\Client\ConnectionException;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
/**
|
||||
* Text-to-speech via ElevenLabs. Used for narration/voiceover. Unlike the
|
||||
* other drivers, a successful response body IS the generated content
|
||||
* (raw audio bytes), not JSON.
|
||||
*/
|
||||
class ElevenLabsDriver implements AiProviderDriver
|
||||
{
|
||||
public function generate(AiProvider $provider, string $prompt, array $options = []): AiGenerationResult
|
||||
{
|
||||
$voiceId = $options['voice_id'] ?? $provider->config['voice_id'] ?? null;
|
||||
|
||||
if (! $voiceId) {
|
||||
throw new AiGenerationException("AI provider '{$provider->name}' has no voice_id configured for text-to-speech.");
|
||||
}
|
||||
|
||||
$modelId = $options['model_id'] ?? $provider->config['model_id'] ?? 'eleven_multilingual_v2';
|
||||
|
||||
try {
|
||||
$response = Http::withHeaders(['xi-api-key' => $provider->api_key])
|
||||
->post("https://api.elevenlabs.io/v1/text-to-speech/{$voiceId}", [
|
||||
'text' => $prompt,
|
||||
'model_id' => $modelId,
|
||||
]);
|
||||
} catch (ConnectionException $e) {
|
||||
throw new AiGenerationException("could not connect to ElevenLabs: {$e->getMessage()}");
|
||||
}
|
||||
|
||||
if ($response->failed()) {
|
||||
$error = $response->json('detail.message') ?? $response->json('detail') ?? 'unexpected response from ElevenLabs';
|
||||
|
||||
throw new AiGenerationException(is_string($error) ? $error : 'unexpected response from ElevenLabs');
|
||||
}
|
||||
|
||||
return new AiGenerationResult($response->body(), 'audio/mpeg');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user