3namespace Bitrix\Im\V2\Chat;
8use Bitrix\Im\V2\Integration\AI\EngineManager;
9use Bitrix\Im\V2\Integration\AI\AIHelper;
10use Bitrix\Im\V2\Integration\AI\CopilotError;
11use Bitrix\Im\V2\Integration\AI\Restriction;
12use Bitrix\Im\V2\Integration\AiAssistant\AiAssistantService;
13use Bitrix\Im\V2\Message;
14use Bitrix\Im\V2\Message\Send\SendingConfig;
15use Bitrix\Im\V2\Message\Send\SendResult;
16use Bitrix\Im\V2\Relation\AddUsersConfig;
17use Bitrix\Im\V2\Relation\DeleteUserConfig;
18use Bitrix\Im\V2\Result;
19use Bitrix\Im\V2\Service\Context;
20use Bitrix\Im\V2\Message\Params;
22use Bitrix\Imbot\Bot\CopilotChatBot;
23use Bitrix\Main\Application;
24use Bitrix\Main\DI\ServiceLocator;
25use Bitrix\Main\Loader;
26use Bitrix\Main\Localization\Loc;
30 private const COUNTER_CHAT_CODE =
'copilot_chat_counter';
31 private const COPILOT_ROLE_UPDATED =
'COPILOT_ROLE_UPDATED';
32 private const COPILOT_CONVERSION_LOCK_TIMEOUT = 3;
36 Loader::includeModule(
'imbot');
38 parent::__construct($source);
43 return self::IM_TYPE_COPILOT;
48 return self::MANAGE_RIGHTS_MEMBER;
53 return self::MANAGE_RIGHTS_MEMBER;
58 return self::MANAGE_RIGHTS_NONE;
63 return $this::MANAGE_RIGHTS_MEMBER;
68 return $this::MANAGE_RIGHTS_MEMBER;
100 if (!Loader::includeModule(
'imbot'))
105 if (!self::isAvailable())
110 if (!self::isActive())
120 $copilotBotId = AIHelper::getCopilotBotId();
135 $filterUserIds = parent::getValidUsersToAdd($userIds);
136 $copilotChatBot = Loader::includeModule(
'imbot') ? CopilotChatBot::getBotId() :
null;
138 if (!isset($copilotChatBot) || !in_array($copilotChatBot, $userIds,
true))
140 return $filterUserIds;
143 if (!in_array($copilotChatBot, $filterUserIds,
true))
145 $filterUserIds[] = CopilotChatBot::getBotId();
148 return $filterUserIds;
153 if (empty($userIds) || !$this->getChatId())
160 return parent::addUsers($usersToAdd,
$config);
185 return parent::sendMessage(
$message, $sendingConfig);
189 $convertedChat =
$result->getResult();
190 if (!$convertedChat || !
$result->isSuccess())
195 return $convertedChat->markAsConverted()->sendMessage(
$message, $sendingConfig);
205 $lockName = $this->getConversionLockName();
208 $isLocked =
$connection->lock($lockName, self::COPILOT_CONVERSION_LOCK_TIMEOUT);
218 $convertResult = $updateService->updateChat();
219 $convertedChat = $convertResult->getResult();
221 if (!$convertResult->isSuccess())
223 $result->addErrors($convertResult->getErrors());
227 $result->setResult($convertedChat);
242 private function getConversionLockName(): string
244 return 'convert_copilot_to_assistant_chat_' . $this->getChatId();
247 private function getUpdateFieldsForConversion():
Chat\
Update\UpdateFields
249 $aiAssistantService = ServiceLocator::getInstance()->get(AiAssistantService::class);
250 $aiAssistantBotId = $aiAssistantService->getBotId();
251 $copilotBotId = AIHelper::getCopilotBotId();
254 'TYPE' => Chat::IM_TYPE_AI_ASSISTANT,
255 'ADDED_MEMBER_ENTITIES' => [[
'user', $aiAssistantBotId]],
256 'DELETED_MEMBER_ENTITIES' => [[
'user', $copilotBotId]],
262 if (empty($usersToAdd))
267 $oldUsers = array_diff($this->getRelations()->getUserIds(), $usersToAdd);
268 if (
count($oldUsers) === 2)
274 if (in_array(
Bot\CopilotChatBot::getBotId(), $usersToAdd,
true))
276 unset($usersToAdd[
Bot\CopilotChatBot::getBotId()]);
279 parent::sendMessageUsersAdd($usersToAdd,
$config);
287 public function sendBanner(?
int $authorId =
null, ?
string $copilotName =
null, ?
bool $isUpdate =
false): void
289 if (!isset($copilotName))
292 $copilotCode = $roleManager->getMainRole($this->getChatId());
293 $copilotName = $roleManager->getRoles([$copilotCode])[$copilotCode][
'name'];
297 'MESSAGE_TYPE' => $this->
getType(),
298 'TO_CHAT_ID' => $this->getChatId(),
299 'FROM_USER_ID' =>
Bot\CopilotChatBot::getBotId(),
301 'MESSAGE' => $isUpdate
302 ? Loc::getMessage(
'IM_CHAT_CREATE_COPILOT_WELCOME_UPDATE', [
'#COPILOT_NAME#' => $copilotName])
303 : Loc::getMessage(
'IM_CHAT_CREATE_COPILOT_WELCOME_CREATE', [
'#COPILOT_NAME#' => $copilotName])
305 'SKIP_USER_CHECK' =>
'Y',
307 'SKIP_COUNTER_INCREMENTS' =>
'Y',
309 Params::COMPONENT_ID =>
Bot\CopilotChatBot::MESSAGE_COMPONENT_START,
310 Params::COMPONENT_PARAMS => [self::COPILOT_ROLE_UPDATED => $isUpdate],
311 Params::NOTIFY =>
'N',
319 $author = $this->getAuthor();
320 $addedUsers = $this->getRelations()->getUserIds();
321 unset($addedUsers[$author->getId()], $addedUsers[
Bot\CopilotChatBot::getBotId()]);
323 if (empty($addedUsers))
329 'MESSAGE_TYPE' => $this->
getType(),
330 'TO_CHAT_ID' => $this->getChatId(),
331 'FROM_USER_ID' =>
Bot\CopilotChatBot::getBotId(),
333 'MESSAGE' => Loc::getMessage(
334 "IM_CHAT_CREATE_COPILOT_COLLECTIVE_{$author->getGender()}_MSGVER_1",
337 '#USER_2_NAME#' => $this->getUsersForBanner($addedUsers)
342 Params::COMPONENT_ID =>
Bot\CopilotChatBot::MESSAGE_COMPONENT_COLLECTIVE,
343 Params::NOTIFY =>
'N',
344 Params::COMPONENT_PARAMS => [
345 'AUTHOR_ID' => $author->getId(),
346 'ADDED_USERS' => array_values($addedUsers),
358 private function getUsersForBanner(
array $addedUsers): string
361 foreach ($addedUsers as
$userId)
363 $userCodes[] =
"[USER={$userId}][/USER]";
366 return implode(
', ', $userCodes);
383 return parent::prepareParams(
$params);
388 return Loc::getMessage(
'IM_CHAT_COPILOT_CHAT_TITLE');
393 $copilotChatCounter = \CUserOptions::GetOption(
'im', self::COUNTER_CHAT_CODE, 1, $this->getContext()->getUserId());
394 $title = Loc::getMessage(
'IM_CHAT_COPILOT_CHAT_TITLE', [
'#NUMBER#' => $copilotChatCounter]);
395 \CUserOptions::SetOption(
'im', self::COUNTER_CHAT_CODE, $copilotChatCounter + 1);
402 if (empty($usersToAdd))
407 foreach ($usersToAdd as
$userId)
409 $relation = $this->getRelations()->getByUserId(
$userId, $this->getId());
410 if ($relation ===
null)
414 if ($relation->getUser()->isBot())
416 Im\Bot::onJoinChat(
'chat'.$this->getId(), [
417 'CHAT_TYPE' => $this->
getType(),
418 'MESSAGE_TYPE' => $this->
getType(),
420 'USER_ID' => $this->getContext()->getUserId(),
421 'CHAT_ID' => $this->getId(),
422 'CHAT_AUTHOR_ID' => $this->getAuthorId(),
423 'CHAT_ENTITY_TYPE' => $this->getEntityType(),
424 'CHAT_ENTITY_ID' => $this->getEntityId(),
425 'ACCESS_HISTORY' => (
int)$relation->getStartCounter() === 0,
426 'SILENT_JOIN' =>
'Y',
444 if (CopilotChatBot::getBotId() ===
$userId && $this->getContext()->getUserId() !==
$userId)
454 $pull = parent::toPullFormat();
455 $pull[
'ai_provider'] = EngineManager::getDefaultEngineName();
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
static getType($chatData, bool $camelCase=true)
const COPILOT_NOT_INSTALLED
const COPILOT_DELETE_ERROR
const IMBOT_NOT_INSTALLED
sendMessageUsersAdd(array $usersToAdd, AddUsersConfig $config)
sendMessage(Message $message, ?SendingConfig $sendingConfig=null)
getValidUsersToAdd(array $userIds)
setManageUsersDelete(string $manageUsersDelete)
static getTitleTemplate()
add(array $params, ?Context $context=null)
__construct($source=null)
addUsers(array $userIds, AddUsersConfig $config=new AddUsersConfig())
sendEventUsersAdd(array $usersToAdd)
setManageUI(string $manageUI)
setManageSettings(string $manageSettings)
getDefaultManageSettings()
sendBanner(?int $authorId=null, ?string $copilotName=null, ?bool $isUpdate=false)
prepareParams(array $params=[])
getUsersWithoutBots(array $userIds)
setManageUsersAdd(string $manageUsersAdd)
sendDescriptionMessage(?int $authorId=null)
sendGreetingMessage(?int $authorId=null)
setManageMessages(string $manageMessages)
getDefaultManageUsersAdd()
deleteUser(int $userId, DeleteUserConfig $config=new DeleteUserConfig())
getDefaultManageUsersDelete()
static create(array $fields)
static getInstance(?int $id)
const COPILOT_TO_ASSISTANT_CONVERSION_ERROR
const COPILOT_REPLACED_BY_AI_ASSISTANT
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
htmlspecialcharsback($str)
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']