Skip to content

Commit 2f0d356

Browse files
committed
add general controller
1 parent 402d501 commit 2f0d356

2 files changed

Lines changed: 153 additions & 2 deletions

File tree

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
namespace Genaker\MagentoMcpAi\Controller\Adminhtml\Chat;
3+
4+
use Magento\Backend\App\Action;
5+
use Magento\Backend\App\Action\Context;
6+
use Magento\Framework\View\Result\LayoutFactory;
7+
use Magento\Framework\Controller\Result\JsonFactory;
8+
use Genaker\MagentoMcpAi\Model\MenuAIAPI;
9+
use Genaker\MagentoMcpAi\Model\Service\OpenAiService;
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Store\Model\ScopeInterface;
12+
13+
/**
14+
* Class Process
15+
*/
16+
class Query extends Action
17+
{
18+
/**
19+
* @var JsonFactory
20+
*/
21+
protected $resultJsonFactory;
22+
23+
/**
24+
* @var MenuAIAPI
25+
*/
26+
protected $menuAIAPI;
27+
28+
/**
29+
* @var OpenAiService
30+
*/
31+
protected $openAiService;
32+
33+
/**
34+
* @var ScopeConfigInterface
35+
*/
36+
protected $_scopeConfig;
37+
38+
/**
39+
* @param Context $context
40+
* @param JsonFactory $resultJsonFactory
41+
* @param MenuAIAPI $menuAIAPI
42+
*/
43+
/**
44+
* @param Context $context
45+
* @param JsonFactory $resultJsonFactory
46+
* @param MenuAIAPI $menuAIAPI
47+
* @param ScopeConfigInterface $scopeConfig
48+
*/
49+
public function __construct(
50+
Context $context,
51+
JsonFactory $resultJsonFactory,
52+
MenuAIAPI $menuAIAPI,
53+
OpenAiService $openAiService,
54+
ScopeConfigInterface $scopeConfig
55+
) {
56+
parent::__construct($context);
57+
$this->resultJsonFactory = $resultJsonFactory;
58+
$this->menuAIAPI = $menuAIAPI;
59+
$this->openAiService = $openAiService;
60+
$this->_scopeConfig = $scopeConfig;
61+
}
62+
63+
/**
64+
* Process chat request
65+
*
66+
* @return \Magento\Framework\Controller\Result\Json
67+
*/
68+
public function execute()
69+
{
70+
$query = $this->getRequest()->getParam('query');
71+
if (!$query) {
72+
return $this->resultJsonFactory->create()->setData([
73+
'error' => 'No query provided'
74+
]);
75+
}
76+
77+
try {
78+
// Get API key from configuration
79+
$apiKey = $this->_scopeConfig->getValue(
80+
'magentomcpai/general/api_key',
81+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
82+
);
83+
84+
if (!$apiKey) {
85+
throw new \Exception('API key not configured');
86+
}
87+
88+
$systemContent = '1.You are Magento 2 Expert and Linux server expert. Please, provide short answer about the question related for magento performace and server configuration rejetc another one if not related to magento and servers ';
89+
90+
// Create messages array for OpenAI
91+
$messages = [
92+
['role' => 'system', 'content' => $systemContent]
93+
];
94+
95+
// Add the current user query
96+
$messages[] = ['role' => 'user', 'content' => $query];
97+
98+
// Process the request through OpenAiService with GPT-5 mini model
99+
$response = $this->openAiService->getChatCompletion($messages, 'gpt-5', $apiKey);
100+
// Extract the content from the response array
101+
$messageContent = is_array($response) && isset($response['content']) ? $response['content'] : $response;
102+
103+
// Format the message content with HTML
104+
$formattedContent = $this->formatResponseContent($messageContent);
105+
106+
// Include usage statistics if available
107+
$responseData = [
108+
'completion_tokens' => $this->openAiService->completion_tokens,
109+
'total_tokens' => $this->openAiService->total_tokens,
110+
'prompt_tokens_details' => $this->openAiService->prompt_tokens_details,
111+
'cached_tokens' => $this->openAiService->cached_tokens,
112+
'audio_tokens' => $this->openAiService->audio_tokens,
113+
'message' => $formattedContent,
114+
'hash' => md5($messageContent),
115+
'url' => null
116+
];
117+
118+
// Add usage data if available
119+
if (is_array($response) && isset($response['usage'])) {
120+
$responseData['usage'] = $response['usage'];
121+
}
122+
123+
return $this->resultJsonFactory->create()->setData($responseData);
124+
} catch (\Exception $e) {
125+
return $this->resultJsonFactory->create()->setData([
126+
'error' => $e->getMessage()
127+
]);
128+
}
129+
}
130+
131+
/**
132+
* Check permission via ACL resource
133+
*
134+
* @return bool
135+
*/
136+
protected function _isAllowed()
137+
{
138+
return $this->_authorization->isAllowed('Genaker_MagentoMcpAi::mcpai');
139+
}
140+
141+
/**
142+
* Format the response content with HTML formatting
143+
*
144+
* @param string $content
145+
* @return string
146+
*/
147+
private function formatResponseContent($content)
148+
{
149+
return $content;
150+
}
151+
}

Model/Service/OpenAiService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public function sendChatRequest(
137137
try {
138138
$model = $model ?: $this->getDefaultModel();
139139

140-
if(in_array($model, ['o1', 'o3', 'o4-mini-high', 'gpt-5-chat', 'gpt-5-nano', 'gpt-5-mini'])){
140+
if(in_array($model, ['o1', 'o3', 'o4-mini-high', 'gpt-5', 'gpt-5-chat', 'gpt-5-nano', 'gpt-5-mini'])){
141141
$data = [
142142
'model' => $model,
143143
'messages' => $messages,
@@ -2044,7 +2044,7 @@ public function getChatCompletion(
20442044
}
20452045

20462046
$model = $model ?: $this->getDefaultModel();
2047-
if(in_array($model, ['gpt-5-mini', 'gpt-5-nano'])){
2047+
if(in_array($model, ['gpt-5-mini', 'gpt-5-nano', 'gpt-5'])){
20482048
$data = [
20492049
'model' => $model,
20502050
'messages' => $messages,

0 commit comments

Comments
 (0)