| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © Melograno Venture Studio. All rights reserved. |
| 5 |
* @licence See LICENCE.md for license details. |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace IvyForms\Services\Ai; |
| 9 |
|
| 10 |
use IvyForms\Common\Exceptions\ServiceUnavailableException; |
| 11 |
use IvyForms\Services\Translations\BackendStrings; |
| 12 |
|
| 13 |
/** |
| 14 |
* Thin wrapper around the WordPress AI Client for text generation features. |
| 15 |
*/ |
| 16 |
class AiTextClient |
| 17 |
{ |
| 18 |
/** |
| 19 |
* Generate a JSON text response using the configured AI provider. |
| 20 |
* |
| 21 |
* @param array<string, mixed> $schema |
| 22 |
* |
| 23 |
* @throws ServiceUnavailableException |
| 24 |
*/ |
| 25 |
public function generateJsonText( |
| 26 |
string $userPrompt, |
| 27 |
string $systemInstruction, |
| 28 |
array $schema, |
| 29 |
int $maxTokens = 4096 |
| 30 |
): string { |
| 31 |
$this->assertClientAvailable(); |
| 32 |
|
| 33 |
// Omit using_provider() so WordPress AI Client picks any configured provider. |
| 34 |
$result = wp_ai_client_prompt($userPrompt) |
| 35 |
->using_system_instruction($systemInstruction) |
| 36 |
->using_max_tokens($maxTokens) |
| 37 |
->as_json_response($schema) |
| 38 |
->generate_text(); |
| 39 |
|
| 40 |
return $this->extractGeneratedText($result); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Generate plain text using the configured AI provider. |
| 45 |
* |
| 46 |
* @throws ServiceUnavailableException |
| 47 |
*/ |
| 48 |
public function generateText( |
| 49 |
string $userPrompt, |
| 50 |
string $systemInstruction = '', |
| 51 |
int $maxTokens = 1024 |
| 52 |
): string { |
| 53 |
$this->assertClientAvailable(); |
| 54 |
|
| 55 |
$builder = wp_ai_client_prompt($userPrompt)->using_max_tokens($maxTokens); |
| 56 |
if ($systemInstruction !== '') { |
| 57 |
$builder = $builder->using_system_instruction($systemInstruction); |
| 58 |
} |
| 59 |
|
| 60 |
return $this->extractGeneratedText($builder->generate_text()); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* @throws ServiceUnavailableException |
| 65 |
*/ |
| 66 |
private function assertClientAvailable(): void |
| 67 |
{ |
| 68 |
if (function_exists('wp_ai_client_prompt')) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
throw new ServiceUnavailableException( |
| 73 |
BackendStrings::getAiStrings()['ai_not_available'] |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* @param mixed $result |
| 79 |
* |
| 80 |
* @throws ServiceUnavailableException |
| 81 |
*/ |
| 82 |
private function extractGeneratedText($result): string |
| 83 |
{ |
| 84 |
if (is_wp_error($result)) { |
| 85 |
$this->logProviderFailure($result->get_error_message()); |
| 86 |
|
| 87 |
throw new ServiceUnavailableException( |
| 88 |
BackendStrings::getAiStrings()['ai_generation_failed'] |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
$text = is_string($result) ? trim($result) : ''; |
| 93 |
if ($text === '') { |
| 94 |
$this->logProviderFailure('Empty or non-string generate_text() result'); |
| 95 |
|
| 96 |
throw new ServiceUnavailableException( |
| 97 |
BackendStrings::getAiStrings()['ai_generation_failed'] |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
return $text; |
| 102 |
} |
| 103 |
|
| 104 |
private function logProviderFailure(string $detail): void |
| 105 |
{ |
| 106 |
error_log('IvyForms AI provider error: ' . $detail); |
| 107 |
} |
| 108 |
} |
| 109 |
|