Skip to content

Commit fdad96a

Browse files
Merge pull request #8 from joomla-projects/update-resposne-class
Update Response class to implement PSR7s ResponseInterface
2 parents 5c80fd3 + e21c4c7 commit fdad96a

File tree

9 files changed

+25
-109
lines changed

9 files changed

+25
-109
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ phpunit.*.xml
1515
.idea/
1616
/.phpunit.result.cache
1717
/.phpunit.cache/
18+
config.json

src/AbstractProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ abstract class AbstractProvider implements ProviderInterface
5858
* @throws \InvalidArgumentException
5959
* @since ___DEPLOY_VERSION___
6060
*/
61-
public function __construct(array $options = [], ?HttpFactory $httpFactory = null)
61+
public function __construct($options = [], ?HttpFactory $httpFactory = null)
6262
{
6363
// Validate provider is suported
6464
if (!\is_array($options) && !($options instanceof \ArrayAccess)) {
@@ -340,9 +340,9 @@ protected function getExtensionFromMimeType(string $mimeType): string
340340
}
341341

342342
/**
343-
* Get audio MIME type from file path.
343+
* Get audio MIME type from the input.
344344
*
345-
* @param string $filepath The file path
345+
* @param string $input
346346
*
347347
* @return string The MIME type
348348
* @since __DEPLOY_VERSION__

src/Exception/QuotaExceededException.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,11 @@ class QuotaExceededException extends AIException
2020
* Constructor.
2121
*
2222
* @param string $provider The AI provider name
23-
* @param string $message The error message
24-
* @param array $context Additional context data
2523
* @param int|null $httpStatusCode The actual HTTP status code from response
26-
* @param string|null $providerErrorCode The provider-specific error code
2724
*
2825
* @since __DEPLOY_VERSION__
2926
*/
30-
public function __construct(string $provider, array $errorData, int $httpStatusCode)
27+
public function __construct(string $provider, array $errorData, $httpStatusCode)
3128
{
3229
$context = ['error_data' => $errorData];
3330
$providerErrorCode = $errorData['code'] ?? $errorData['error']['code'] ?? null;

src/Exception/RateLimitException.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@ class RateLimitException extends AIException
2020
* Constructor.
2121
*
2222
* @param string $provider The AI provider name
23-
* @param string $message The error message
24-
* @param array $context Additional context data
23+
* @param array $errorData Additional error data
2524
* @param int|null $httpStatusCode The actual HTTP status code from response
26-
* @param string|null $providerErrorCode The provider-specific error code
2725
*
2826
* @since __DEPLOY_VERSION__
2927
*/
30-
public function __construct(string $provider, array $errorData, int $httpStatusCode)
28+
public function __construct(string $provider, array $errorData, $httpStatusCode)
3129
{
3230
$context = ['error_data' => $errorData];
3331
$providerErrorCode = $errorData['code'] ?? $errorData['error']['code'] ?? null;

src/Interface/AudioInterface.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ interface AudioInterface
2222
* Generate speech audio from text input.
2323
*
2424
* @param string $text The text to convert to speech
25-
* @param string $model The TTS model to use for speech generation
26-
* @param string $voice The voice to use for speech generation
2725
* @param array $options Additional options for speech generation
2826
*
2927
* @return Response
@@ -59,7 +57,6 @@ public function getSupportedAudioFormats(): array;
5957
* Transcribe audio to text.
6058
*
6159
* @param string $audioFile Path to the audio file to transcribe
62-
* @param string $model The transcription model to use
6360
* @param array $options Additional options for transcription
6461
*
6562
* @return Response
@@ -71,7 +68,6 @@ public function transcribe(string $audioFile, array $options = []): Response;
7168
* Translate audio to English text.
7269
*
7370
* @param string $audioFile Path to audio file to translate
74-
* @param string $model Model to use for translation
7571
* @param array $options Additional options
7672
*
7773
* @return Response

src/Provider/AnthropicProvider.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class AnthropicProvider extends AbstractProvider implements ProviderInterface, C
4242
*
4343
* @since __DEPLOY_VERSION__
4444
*/
45-
public function __construct(array $options = [], ?HttpFactory $httpFactory = null)
45+
public function __construct($options = [], ?HttpFactory $httpFactory = null)
4646
{
4747
parent::__construct($options, $httpFactory);
4848

@@ -143,9 +143,7 @@ private function getModelsEndpoint(): string
143143
/**
144144
* List available models from Anthropic.
145145
*
146-
* @param array $options Optional parameters for the request
147-
*
148-
* @return Response The response containing model list
146+
* @return array
149147
* @since __DEPLOY_VERSION__
150148
*/
151149
public function getAvailableModels(): array

src/Provider/OllamaProvider.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class OllamaProvider extends AbstractProvider
3232
*
3333
* @since __DEPLOY_VERSION__
3434
*/
35-
public function __construct(array $options = [], ?HttpFactory $httpFactory = null)
35+
public function __construct($options = [], ?HttpFactory $httpFactory = null)
3636
{
3737
parent::__construct($options, $httpFactory);
3838

@@ -165,7 +165,6 @@ public function getAvailableModels(): array
165165
/**
166166
* List models currently loaded into memory (running) and echo their names.
167167
*
168-
* @return array Array of running model info
169168
* @throws ProviderException If the request fails
170169
* @since __DEPLOY_VERSION__
171170
*/
@@ -319,7 +318,6 @@ public function deleteModel(string $modelName): bool
319318
* @param bool $stream Whether to stream the response (for progress updates)
320319
* @param bool $insecure Allow insecure connections to the library
321320
*
322-
* @return bool True if model was pulled successfully
323321
* @throws InvalidArgumentException If model doesn't exist in Ollama library
324322
* @throws ProviderException If pull fails for other reasons
325323
* @since __DEPLOY_VERSION__
@@ -626,7 +624,7 @@ public function buildGenerateRequestPayload(string $prompt, array $options = [])
626624
*
627625
* @param string $prompt The prompt to generate a response for
628626
* @param array $options Additional options
629-
* @param callable $callback Optional callback function for streaming responses
627+
*
630628
* @return Response The AI response
631629
* @throws ProviderException If the request fails
632630
* @since __DEPLOY_VERSION__

src/Provider/OpenAIProvider.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class OpenAIProvider extends AbstractProvider implements ChatInterface, ModelInt
124124
*
125125
* @since __DEPLOY_VERSION__
126126
*/
127-
public function __construct(array $options = [], ?HttpFactory $httpFactory = null)
127+
public function __construct($options = [], ?HttpFactory $httpFactory = null)
128128
{
129129
parent::__construct($options, $httpFactory);
130130

@@ -573,8 +573,6 @@ public function editImage($images, string $prompt, array $options = []): Respons
573573
* Generate speech audio from text input.
574574
*
575575
* @param string $text The text to convert to speech
576-
* @param string $model The model to use for speech synthesis
577-
* @param string $voice The voice to use for speech synthesis
578576
* @param array $options Additional options for speech generation
579577
*
580578
* @return Response
@@ -602,7 +600,6 @@ public function speech(string $text, array $options = []): Response
602600
* Transcribe audio into text.
603601
*
604602
* @param string $audioFile Path to audio file
605-
* @param string $model The transcription model to use
606603
* @param array $options Additional options for transcription
607604
*
608605
* @return Response The AI response containing transcribed text
@@ -627,7 +624,6 @@ public function transcribe(string $audioFile, array $options = []): Response
627624
* Translate audio to English text.
628625
*
629626
* @param string $audioFile Path to audio file
630-
* @param string $model Model to use for translation
631627
* @param array $options Additional options
632628
*
633629
* @return Response Translation response
@@ -722,7 +718,6 @@ public function moderate($input, array $options = []): array
722718
* @param array $moderationResult Result from moderate() method
723719
*
724720
* @return bool
725-
* @throws \InvalidArgumentException
726721
* @since __DEPLOY_VERSION__
727722
*/
728723
public function isContentFlagged(array $moderationResult): bool
@@ -909,7 +904,6 @@ private function buildVisionRequestPayload(string $message, string $image, strin
909904
*
910905
* @param string $prompt The image generation prompt.
911906
* @param array $options Additional options for the request.
912-
* @param string $capability Required capability.
913907
*
914908
* @return array The request payload.
915909
* @since __DEPLOY_VERSION__
@@ -1194,8 +1188,6 @@ private function buildImageEditPayload($images, string $prompt, array $options):
11941188
* Build payload for text-to-speech request.
11951189
*
11961190
* @param string $text The text to convert to speech
1197-
* @param string $model The model to use for speech synthesis
1198-
* @param string $voice The voice to use for speech synthesis
11991191
* @param array $options Additional options for speech generation
12001192
*
12011193
* @return array The request payload.
@@ -1268,7 +1260,6 @@ private function buildSpeechPayload(string $text, array $options): array
12681260
* Build payload for transcription request.
12691261
*
12701262
* @param string $audioFile The audio file
1271-
* @param string $model The transcription model
12721263
* @param array $options Additional options
12731264
*
12741265
* @return array Form data for multipart request
@@ -1378,7 +1369,6 @@ private function buildTranscriptionPayload(string $audioFile, array $options): a
13781369
* Build payload for translation request.
13791370
*
13801371
* @param string $audioFile Path to the audio file
1381-
* @param string $model The translation model to use
13821372
* @param array $options Additional options for translation
13831373
*
13841374
* @return array Form data for multipart request

src/Response/Response.php

Lines changed: 13 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,30 @@
1212
use Joomla\Filesystem\File;
1313
use Joomla\Filesystem\Folder;
1414
use Joomla\Filesystem\Path;
15+
use Joomla\Http\Response as HttpResponse;
1516

1617
/**
1718
* AI response data object class.
1819
*
1920
* @since __DEPLOY_VERSION__
2021
*/
21-
class Response
22+
class Response extends HttpResponse
2223
{
2324
/**
24-
* The content of the response.
25+
* The provider of the response.
2526
*
2627
* @var string
2728
* @since __DEPLOY_VERSION__
2829
*/
29-
private $content;
30-
31-
/**
32-
* The status code of the response.
33-
*
34-
* @var int
35-
* @since __DEPLOY_VERSION__
36-
*/
37-
private $statusCode;
30+
private string $provider;
3831

3932
/**
4033
* The metadata of the response.
4134
*
4235
* @var array
4336
* @since __DEPLOY_VERSION__
4437
*/
45-
private $metadata;
46-
47-
/**
48-
* The provider of the response.
49-
*
50-
* @var string
51-
* @since __DEPLOY_VERSION__
52-
*/
53-
private $provider;
38+
private array $metadata;
5439

5540
/**
5641
* Constructor.
@@ -64,21 +49,25 @@ class Response
6449
*/
6550
public function __construct(string $content, string $provider, array $metadata = [], int $status = 200)
6651
{
67-
$this->content = $content;
52+
parent::__construct('php://memory', $status);
53+
54+
$body = $this->getBody();
55+
$body->write($content);
56+
$body->rewind();
57+
6858
$this->provider = $provider;
6959
$this->metadata = $metadata;
70-
$this->statusCode = $status;
7160
}
7261

7362
/**
7463
* Get the content of the response.
7564
*
76-
* @return string The content of the response.
65+
* @return string
7766
* @since __DEPLOY_VERSION__
7867
*/
7968
public function getContent(): string
8069
{
81-
return $this->content;
70+
return (string) $this->getBody();
8271
}
8372

8473
/**
@@ -191,55 +180,4 @@ public function getProvider(): string
191180
{
192181
return $this->provider;
193182
}
194-
195-
/**
196-
* Get the status code of the response.
197-
*
198-
* @return int The status code of the response.
199-
* @since __DEPLOY_VERSION__
200-
*/
201-
public function getStatusCode(): int
202-
{
203-
return $this->statusCode;
204-
}
205-
206-
/**
207-
* Magic method to access properties of the response object.
208-
*
209-
* @param string $name The name of the property to get.
210-
*
211-
* @return mixed The value of the property.
212-
* @since __DEPLOY_VERSION__
213-
*/
214-
public function __get($name)
215-
{
216-
switch (strtolower($name)) {
217-
case 'content':
218-
return $this->getContent();
219-
220-
case 'metadata':
221-
return $this->getMetadata();
222-
223-
case 'provider':
224-
return $this->getProvider();
225-
226-
case 'statuscode':
227-
return $this->getStatusCode();
228-
229-
default:
230-
$trace = debug_backtrace();
231-
232-
trigger_error(
233-
sprintf(
234-
'Undefined property via __get(): %s in %s on line %s',
235-
$name,
236-
$trace[0]['file'],
237-
$trace[0]['line']
238-
),
239-
E_USER_NOTICE
240-
);
241-
242-
break;
243-
}
244-
}
245183
}

0 commit comments

Comments
 (0)