| Conditions | 7 |
| Paths | 29 |
| Total Lines | 74 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 72 | private function requestGrokAI(string $prompt, string $toolName, array $options = []): ?string |
||
| 73 | { |
||
| 74 | $userId = $this->getUserId(); |
||
| 75 | if (!$userId) { |
||
| 76 | throw new \RuntimeException('User not authenticated.'); |
||
| 77 | } |
||
| 78 | |||
| 79 | // Build system prompt to instruct document generation |
||
| 80 | $systemContent = 'You are a helpful assistant that generates well-structured documents. Output in the specified format (e.g., Markdown for easy conversion to PDF).'; |
||
| 81 | |||
| 82 | // Append format instruction to user prompt if provided |
||
| 83 | $userContent = $prompt; |
||
| 84 | $format = $options['format'] ?? $this->defaultOptions['format']; |
||
| 85 | if ($format) { |
||
| 86 | $userContent .= "\n\nOutput the document in {$format} format."; |
||
| 87 | } |
||
| 88 | |||
| 89 | $payload = [ |
||
| 90 | 'model' => $this->model, |
||
| 91 | 'input' => [ |
||
| 92 | ['role' => 'system', 'content' => $systemContent], |
||
| 93 | ['role' => 'user', 'content' => $userContent], |
||
| 94 | ], |
||
| 95 | ...array_merge($this->defaultOptions, $options), |
||
| 96 | ]; |
||
| 97 | |||
| 98 | try { |
||
| 99 | $response = $this->httpClient->request('POST', $this->apiUrl, [ |
||
| 100 | 'headers' => [ |
||
| 101 | 'Authorization' => 'Bearer ' . $this->apiKey, |
||
| 102 | 'Content-Type' => 'application/json', |
||
| 103 | ], |
||
| 104 | 'json' => $payload, |
||
| 105 | ]); |
||
| 106 | |||
| 107 | $statusCode = $response->getStatusCode(); |
||
| 108 | if ($statusCode !== 200) { |
||
| 109 | throw new \RuntimeException('API request failed with status: ' . $statusCode); |
||
| 110 | } |
||
| 111 | |||
| 112 | $data = $response->toArray(); |
||
| 113 | |||
| 114 | // Check for error key first |
||
| 115 | if (isset($data['error'])) { |
||
| 116 | throw new \RuntimeException('API error: ' . $data['error']['message']); |
||
| 117 | } |
||
| 118 | |||
| 119 | // Extract generated content from response structure |
||
| 120 | if (isset($data['output'][0]['content'][0]['text'])) { |
||
| 121 | $generatedContent = $data['output'][0]['content'][0]['text']; |
||
| 122 | |||
| 123 | // Usage is available for text generation |
||
| 124 | $usage = $data['usage'] ?? ['prompt_tokens' => 0, 'completion_tokens' => 0, 'total_tokens' => 0]; |
||
| 125 | |||
| 126 | // Log request |
||
| 127 | $aiRequest = new AiRequests(); |
||
| 128 | $aiRequest->setUserId($userId) |
||
| 129 | ->setToolName($toolName) |
||
| 130 | ->setRequestText($prompt) |
||
| 131 | ->setPromptTokens($usage['prompt_tokens']) |
||
| 132 | ->setCompletionTokens($usage['completion_tokens']) |
||
| 133 | ->setTotalTokens($usage['total_tokens']) |
||
| 134 | ->setAiProvider('grok') |
||
| 135 | ; |
||
| 136 | |||
| 137 | $this->aiRequestsRepository->save($aiRequest); |
||
| 138 | |||
| 139 | return $generatedContent; |
||
| 140 | } |
||
| 141 | |||
| 142 | return null; |
||
| 143 | } catch (\Exception $e) { |
||
| 144 | error_log('[AI][Grok] Exception: ' . $e->getMessage()); |
||
| 145 | return null; |
||
| 146 | } |
||
| 154 |