1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
RequestBlockContent.php
См. документацию.
1<?php
2declare(strict_types=1);
3
4namespace Bitrix\Landing\Copilot\Generation\Step;
5
6use Bitrix\Landing\Copilot\Connector\AI;
7use Bitrix\Landing\Copilot\Connector\AI\Prompt;
8use Bitrix\Landing\Copilot\Converter;
9use Bitrix\Landing\Copilot\Data\Block\Operator;
10use Bitrix\Landing\Copilot\Data\Type\NodeType;
11use Bitrix\Landing\Copilot\Generation\Error;
12use Bitrix\Landing\Copilot\Generation\GenerationException;
13use Bitrix\Landing\Copilot\Generation\Markers;
14use Bitrix\Landing\Copilot\Generation\Type\Errors;
15use Bitrix\Landing\Copilot\Generation\Type\GenerationErrors;
16use Bitrix\Landing\Copilot\Generation\Type\RequestQuotaDto;
17use Bitrix\Landing\Copilot\Data\Site;
18use Bitrix\Landing;
19use Bitrix\Landing\Rights;
20
22{
23 public function __construct()
24 {
25 parent::__construct();
26 if (class_exists(self::getConnectorClass()))
27 {
28 $this->connector = new (self::getConnectorClass())();
29 }
30 }
31
35 public static function getConnectorClass(): string
36 {
37 return AI\Text::class;
38 }
39
44 {
45 return new RequestQuotaDto(self::getConnectorClass(), 1);
46 }
47
48 protected function getPrompt(): Prompt
49 {
50 $prompt = new Prompt('landing_ai_block_content');
51 $prompt->setMarkers(Markers::getBlockContentPromptMarkers($this->siteData));
52
53 return $prompt;
54 }
55
56 protected function applyResponse(): bool
57 {
58 $result = $this->request->getResult();
59
60 if ($result)
61 {
63
64 Converter\Json::initSiteBlock($this->siteData, $result);
66 self::setDefaultSrcForImgNodes($this->siteData);
67 }
68
69 return true;
70 }
71
77 public function verifyResponse(): void
78 {
79 $result = $this->request->getResult();
80
81 if (!$result)
82 {
83 throw new GenerationException(GenerationErrors::notExistResponse);
84 }
85
86 if (
87 !isset($result['isAllowedRequest'])
88 || $result['isAllowedRequest'] !== "yes"
89 )
90 {
91 $this->request->saveError(Error::createError(Errors::requestNotAllowed));
92
93 throw new GenerationException(GenerationErrors::restrictedRequest);
94 }
95 }
96
106 protected function prepareResult(array $result): array
107 {
108 if (!isset($result['blocks']))
109 {
110 return [];
111 }
112
113 foreach ($result['blocks'] as &$block)
114 {
115 if (!isset($block['nodes']))
116 {
117 continue;
118 }
119
120 foreach ($block['nodes'] as &$node)
121 {
122 if (is_array($node))
123 {
124 $node = array_filter($node, static function($value) {
125 return $value !== '';
126 });
127 }
128 else
129 {
130 $node = [];
131 }
132 }
133 unset($node);
134
135 $block['nodes'] = array_filter($block['nodes'], static function($node) {
136 return !empty($node);
137 });
138 }
139 unset($block);
140
141 return $result;
142 }
143
151 protected static function setDefaultSrcForImgNodes(Site $siteData): void
152 {
154 Landing\Landing::setEditMode();
155 $landingInstance = Landing\Landing::createInstance($siteData->getLandingId());
156 foreach ($siteData->getBlocks() as $blockData)
157 {
158 $blockId = $blockData->getId();
159 $blockInstance = $landingInstance->getBlockById($blockId);
160 if ($blockInstance)
161 {
162 $blockContent = $blockInstance->getContent();
163 }
164
165 foreach ($blockData->getNodes() as $nodeData)
166 {
167 if ($nodeData->getType() !== NodeType::Img)
168 {
169 continue;
170 }
171
172 $promptTexts = $nodeData->getPromptTexts();
173 if (isset($blockContent) && $promptTexts)
174 {
175 if (
176 method_exists($nodeData, 'setSrc')
177 && method_exists($nodeData, 'setDefaultSrc')
178 )
179 {
180 $defaultSrcDataPrepared = [];
181 $nodeDataValues = $nodeData->getValues();
182 foreach ($nodeDataValues as $nodeDataValue)
183 {
184 $defaultSrcDataPrepared[] = [
185 'src' => $nodeDataValue['defaultSrc'],
186 'src2x' => $nodeDataValue['defaultSrc2x']
187 ];
188 }
189 $nodeData->setSrc($defaultSrcDataPrepared);
190 }
191 }
192 }
193 }
195 }
196}
static initSiteBlock(Site $siteData, array $json)
Определения Json.php:208
static saveImgNodesDataFromResponse(Site $siteData, array $result)
Определения Json.php:239
static createError(?Errors $code=null)
Определения Error.php:53
static getBlockContentPromptMarkers(Data\Site $siteData)
Определения Markers.php:81
static setGlobalOn()
Определения rights.php:116
static setGlobalOff()
Определения rights.php:107
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
Определения cookies.php:2