Skip to content

Commit 8f4a890

Browse files
committed
ref
1 parent c42f83f commit 8f4a890

File tree

3 files changed

+28
-29
lines changed

3 files changed

+28
-29
lines changed

src/platform/src/Bridge/ElevenLabs/ElevenLabsClient.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,29 +87,23 @@ private function doTextToSpeechRequest(Model $model, array|string $payload, arra
8787
$voice = $options['voice'] ??= $model->getOptions()['voice'];
8888
$stream = $options['stream'] ??= $model->getOptions()['stream'] ?? false;
8989

90-
if ($stream) {
91-
$streamSource = $this->httpClient->request('POST', \sprintf('%s/text-to-speech/%s/stream', $this->hostUrl, $voice), [
92-
'headers' => [
93-
'xi-api-key' => $this->apiKey,
94-
],
95-
'json' => [
96-
'text' => $payload['text'],
97-
'model_id' => $model->getName(),
98-
],
99-
]);
100-
101-
return new RawHttpResult($this->httpClient->stream($streamSource));
102-
}
90+
$url = $stream
91+
? \sprintf('%s/text-to-speech/%s/stream', $this->hostUrl, $voice)
92+
: \sprintf('%s/text-to-speech/%s', $this->hostUrl, $voice);
10393

104-
return new RawHttpResult($this->httpClient->request('POST', \sprintf('%s/text-to-speech/%s', $this->hostUrl, $voice), [
94+
$response = $this->httpClient->request('POST', $url, [
10595
'headers' => [
10696
'xi-api-key' => $this->apiKey,
10797
],
10898
'json' => [
10999
'text' => $payload['text'],
110100
'model_id' => $model->getName(),
111101
],
112-
]));
102+
]);
103+
104+
return $stream
105+
? new RawHttpResult($this->httpClient->stream($response))
106+
: new RawHttpResult($response);
113107
}
114108

115109
/**

src/platform/src/Bridge/ElevenLabs/ElevenLabsResultConverter.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,27 @@ public function convert(RawResultInterface $result, array $options = []): Result
3838
$response = $result->getObject();
3939

4040
return match (true) {
41-
$response instanceof ResponseStreamInterface => new StreamResult((static function () use ($response): \Generator {
42-
foreach ($response as $chunk) {
43-
if ($chunk->isFirst()) {
44-
continue;
45-
}
46-
47-
if ('' === $chunk->getContent()) {
48-
continue;
49-
}
50-
51-
yield $chunk;
52-
}
53-
})()),
41+
$response instanceof ResponseStreamInterface => new StreamResult($this->convertToGenerator($response)),
5442
str_contains($response->getInfo('url'), 'speech-to-text') => new TextResult($result->getData()['text']),
5543
str_contains($response->getInfo('url'), 'text-to-speech') => new BinaryResult($result->getObject()->getContent(), 'audio/mpeg'),
5644
default => throw new RuntimeException('Unsupported ElevenLabs response.'),
5745
};
5846
}
47+
48+
private function convertToGenerator(ResponseStreamInterface $response): \Generator
49+
{
50+
return (static function () use ($response): \Generator {
51+
foreach ($response as $chunk) {
52+
if ($chunk->isFirst()) {
53+
continue;
54+
}
55+
56+
if ('' === $chunk->getContent()) {
57+
continue;
58+
}
59+
60+
yield $chunk;
61+
}
62+
})();
63+
}
5964
}

src/platform/tests/Bridge/ElevenLabs/ElevenLabsClientTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#[UsesClass(Model::class)]
3131
#[UsesClass(Audio::class)]
3232
#[UsesClass(AudioNormalizer::class)]
33-
#[UsesClass(RawHttpStreamResult::class)]
33+
#[UsesClass(RawHttpResult::class)]
3434
final class ElevenLabsClientTest extends TestCase
3535
{
3636
public function testSupportsModel()

0 commit comments

Comments
 (0)