2declare(strict_types=1);
4namespace Bitrix\Landing\Copilot;
7use Bitrix\Landing\Agent;
8use Bitrix\Landing\Copilot\Connector\Chat\ChatBotMessageDto;
9use Bitrix\Landing\Copilot\Generation\GenerationException;
10use Bitrix\Landing\Copilot\Generation\Scenario\IScenario;
11use Bitrix\Landing\Copilot\Generation\Scenario\Scenarist;
12use Bitrix\Landing\Copilot\Generation\Timer;
13use Bitrix\Landing\Copilot\Model\GenerationsTable;
14use Bitrix\Main\Application;
15use Bitrix\Main\ArgumentException;
16use Bitrix\Main\ORM\Query\Query;
17use Bitrix\Main\ORM\Query\Filter;
18use Bitrix\Main\Web\Json;
25 private const EVENT_ERROR =
'onCopilotError';
26 private const EVENT_TIMER =
'onCopilotTimeIsOver';
27 private const EVENT_GENERATION_CREATE =
'onGenerationCreate';
28 private const EVENT_GENERATION_ERROR =
'onGenerationError';
29 private const EVENT_GENERATION_FINISH =
'onGenerationFinish';
32 private ?
int $chatId =
null;
35 private ?
array $data =
null;
37 private int $authorId;
48 $this->timer =
new Timer();
57 return $this->authorId;
69 $this->chatId = $chatId;
90 $this->scenario = $scenario;
100 return $this->scenario ??
null;
105 $this->siteData = $siteData;
112 return $this->siteData;
127 return $this->data[
$key] ??
null;
138 if (!isset($this->data))
143 $this->data[
$key] = $data;
153 if (is_array($this->data) && array_key_exists(
$key, $this->data))
155 unset($this->data[
$key]);
161 $this->siteData->setWishes($wishes);
168 return $this->
id ??
null;
173 return $this->step ??
null;
190 if ($generationId <= 0)
197 ->where(
'ID',
'=', $generationId)
200 return $this->initExists(
$filter);
214 ->where(
'SCENARIO',
'=', $scenario::class)
215 ->where(
'SITE_ID',
'=',
$siteId)
218 return $this->initExists(
$filter);
223 $generation = GenerationsTable::query()
225 ->setSelect([
'ID',
'SCENARIO',
'STEP',
'CHAT_ID',
'SITE_DATA',
'DATA',
'CREATED_BY_ID'])
234 $this->
id = (int)$generation[
'ID'];
235 $this->authorId = (int)$generation[
'CREATED_BY_ID'];
237 if (isset($generation[
'STEP']))
239 $this->step = (int)$generation[
'STEP'];
242 if (isset($generation[
'CHAT_ID']))
244 $this->setChatId((
int)$generation[
'CHAT_ID']);
247 if (!class_exists($generation[
'SCENARIO']))
251 $this->
setScenario(
new ($generation[
'SCENARIO'])());
254 isset($generation[
'SITE_DATA'])
255 && is_array($generation[
'SITE_DATA'])
258 $this->siteData = Data\Site::fromArray($generation[
'SITE_DATA']);
262 is_array($generation[
'DATA'])
263 && !empty($generation[
'DATA'])
266 $this->data = $generation[
'DATA'];
269 if (!$this->initScenarist())
283 if (!isset($this->
id) && !$this->save())
297 !$this->isExecutable()
298 || !$this->initScenarist()
304 $this->timer->start();
305 $this->deleteAgent();
310 ->onStepChange(fn(
int $newStep) => $this->step = $newStep)
311 ->onSave(fn() => $this->save())
315 if ($this->scenarist->isFinished())
328 $this->
getEvent()->send(self::EVENT_GENERATION_ERROR);
332 catch (\RuntimeException $e)
335 $this->
getEvent()->send(self::EVENT_TIMER);
354 private function isExecutable(): bool
363 private function getLockName(): string
365 return 'landing_copilot_generation_' . ($this->
id ?? 0);
368 private function setAgent(): void
373 private function deleteAgent(): void
384 if (!$this->initScenarist())
389 $this->scenarist->finish();
399 if (!$this->initScenarist())
404 return $this->scenarist->isFinished();
407 private function onFinish(): void
409 $this->getEvent()->send(self::EVENT_GENERATION_FINISH);
418 if (!$this->initScenarist())
423 return $this->scenarist->isError();
432 if ($this->initScenarist())
434 $this->scenarist->clearErrors();
440 private function initScenarist(): bool
451 if (!isset($this->scenarist))
462 private function save(): bool
473 'SCENARIO' => $this->scenario::class,
474 'STEP' => $this->step ??
null,
476 'SITE_ID' => $this->siteData->getSiteId(),
477 'SITE_DATA' => $this->siteData->toArray(),
482 if (isset($this->
id) && $this->
id)
484 $res = GenerationsTable::update($this->
id,
$fields);
485 if (
$res->isSuccess())
493 if (
$res->isSuccess())
495 $this->
id =
$res->getId();
496 $this->
getEvent()->send(self::EVENT_GENERATION_CREATE);
516 if (isset($this->siteData))
519 ->setSiteId($this->siteData->getSiteId())
520 ->setLandingId($this->siteData->getLandingId())
529 static $generations = [];
530 if (!isset($generations[$id]))
532 $generation = GenerationsTable::query()
533 ->where(
'ID',
'=', $id)
537 $generations[$id] = (bool)$generation;
540 return $generations[$id];
546 $imagesSet = $siteData->getImagesSet();
549 foreach ($blocks as $block)
552 'id' => $block->getId(),
553 'anchor' => $block->getAnchor($block->getId()),
554 'images' => $imagesSet[$block->getId()] ?? [],
556 $blocksData[] = $blockData;
560 $blockDataEncoded = Json::encode($blocksData);
564 $blockDataEncoded =
'';
567 return $blockDataEncoded;
static deleteUniqueAgent(string $funcName, array $params=[])
static addUniqueAgent(string $funcName, array $params=[], int $time=7200, ?int $nextExecDelay=null)
setSiteData(Data\Site $siteData)
initBySiteId(int $siteId, IScenario $scenario)
setData(string $key, mixed $data)
setWishes(Data\Wishes $wishes)
initById(int $generationId)
setScenario(IScenario $scenario)
getData(?string $key=null)
getBlocksData(array $blocks)
static checkExists(int $id)
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
if(empty($signedUserToken)) $key