1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
RoleManager.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Integration\AI;
4
5use Bitrix\Im\V2\Analytics\CopilotAnalytics;
6use Bitrix\Im\V2\Chat;
7use Bitrix\Im\V2\Chat\Param\Params;
8use Bitrix\Im\V2\Common\ContextCustomer;
9use Bitrix\Im\V2\Result;
10use Bitrix\Main\Loader;
11
13{
14 use ContextCustomer;
15
16 protected const PROMPT_CATEGORY = 'chat';
17
18 protected static array $roles = [];
19 protected static array $prompts = [];
20
21 protected ?\Bitrix\AI\Role\RoleManager $aiManager = null;
22
23 public static function getDefaultRoleCode(): ?string
24 {
25 if (!Loader::includeModule('ai'))
26 {
27 return null;
28 }
29
30 return \Bitrix\AI\Role\RoleManager::getUniversalRoleCode();
31 }
32
33 public function getRolesShort(array $roleCodes): ?array
34 {
35 if (!Loader::includeModule('imbot') || !Loader::includeModule('ai'))
36 {
37 return null;
38 }
39
40 $roleCodes[] = self::getDefaultRoleCode();
41 $roleCodes = array_unique($roleCodes);
42
43 $roleData = [];
44 foreach ($this->getAiManager()->getRolesAvatarsFromCache($roleCodes) as $roleCode => $avatar)
45 {
46 $roleData[$roleCode] = $this->formatRoleDataShort([
47 'code' => $roleCode,
48 'avatar' => $avatar,
49 ]);
50 }
51
52 return !empty($roleData) ? $roleData : null;
53 }
54
55 public function getRoles(array $roleCodes, bool $withPrompts = true): ?array
56 {
57 if (!Loader::includeModule('imbot') || !Loader::includeModule('ai'))
58 {
59 return null;
60 }
61
62 $roleCodes[] = self::getDefaultRoleCode();
63 $roleCodes = array_unique($roleCodes);
64 $this->fillRoles($roleCodes);
65
66 if ($withPrompts)
67 {
68 $this->fillPrompts($roleCodes);
69 }
70
71 $roleData = [];
72 foreach ($roleCodes as $code)
73 {
74 $role = self::$roles[$code] ?? null;
75 if (isset($role))
76 {
77 if ($withPrompts)
78 {
79 $role['prompts'] = self::$prompts[$code] ?? [];
80 }
81 $roleData[$code] = $role;
82 }
83 }
84
85 return !empty($roleData) ? $roleData : null;
86 }
87
88 protected function fillRoles(array $roleCodes): void
89 {
90 $roleManager = $this->getAiManager();
91
92 $codesWithoutCache = array_diff($roleCodes, array_keys(self::$roles));
93 if (empty($codesWithoutCache))
94 {
95 return;
96 }
97
98 foreach ($roleManager->getRolesByCode($codesWithoutCache) as $role)
99 {
100 self::$roles[$role['code']] = $this->formatRoleData($role);
101 }
102 }
103
104 protected function fillPrompts(array $roleCodes): void
105 {
106 $roleManager = $this->getAiManager();
107 $rolesWithoutPrompts = array_diff($roleCodes, array_keys(self::$prompts));
108
109 if (empty($rolesWithoutPrompts))
110 {
111 return;
112 }
113
114 $prompts = $roleManager->getPromptsByCategoryAndRoleCodes(self::PROMPT_CATEGORY, $rolesWithoutPrompts);
115 foreach ($rolesWithoutPrompts as $code)
116 {
117 self::$prompts[$code] =
120 ?? self::$prompts[self::getDefaultRoleCode()]
121 ?? []
122 ;
123 }
124 }
125
126 protected function formatRoleData(array $role): array
127 {
128 return [
129 'code' => $role['code'],
130 'name' => $role['name'],
131 'desc' => $role['description'],
132 'avatar' => $role['avatar'],
133 'default' => $role['code'] === self::getDefaultRoleCode(),
134 'prompts' => [],
135 ];
136 }
137
138 protected function formatRoleDataShort(array $role): array
139 {
140 return [
141 'code' => $role['code'],
142 'avatar' => $role['avatar'],
143 'default' => $role['code'] === self::getDefaultRoleCode(),
144 ];
145 }
146
147 public function getMainRole(?int $chatId): ?string
148 {
149 if (!isset($chatId) || !Loader::includeModule('ai'))
150 {
151 return null;
152 }
153
154 $params = Params::getInstance($chatId);
155
156 if ($params->get(Params::COPILOT_MAIN_ROLE) === null)
157 {
159 }
160
161 return (string)$params->get(Params::COPILOT_MAIN_ROLE)->getValue();
162 }
163
164 public function updateRole(Chat $chat, ?string $roleCode): Result
165 {
166 $result = new Result();
167
168 if (!Loader::includeModule('ai'))
169 {
171
172 return $result;
173 }
174
175 if (!isset($roleCode))
176 {
177 $roleCode = self::getDefaultRoleCode();
178 }
179
180 $roleData = $this->getRoles([$roleCode]);
181 if (empty($roleData))
182 {
184
185 return $result;
186 }
187
188 $params = Params::getInstance($chat->getChatId());
189 if (
190 $params->get(Params::COPILOT_MAIN_ROLE) !== null
191 && $params->get(Params::COPILOT_MAIN_ROLE)->getValue() === $roleCode
192 )
193 {
195
196 return $result;
197 }
198
199 $oldRole = $params->get(Params::COPILOT_MAIN_ROLE)?->getValue() ?? self::getDefaultRoleCode();
200 $params->addParamByName(Params::COPILOT_MAIN_ROLE, $roleCode);
201 (new CopilotAnalytics($chat))->addChangeRole((string)$oldRole);
202
203 if (!isset($roleData[$roleCode]))
204 {
205 return $result;
206 }
207 $this->sendPushCopilotRole($chat, $roleData[$roleCode]);
208
209 if ($chat instanceof Chat\CopilotChat)
210 {
211 $chat->sendBanner(null, $roleData[$roleCode]['name'], true);
212 }
213
214 return $result;
215 }
216
217 protected function sendPushCopilotRole(Chat $chat, array $roleData): array
218 {
219 if (!\Bitrix\Main\Loader::includeModule('pull'))
220 {
221 return [];
222 }
223
224 $pushMessage = [
225 'module_id' => 'im',
226 'command' => 'chatCopilotRoleUpdate',
227 'params' => [
228 'chatId' => $chat->getChatId(),
229 'dialogId' => 'chat' . $chat->getChatId(),
230 'copilotRole' => [
231 'chats' => [['dialogId' => $chat->getDialogId(), 'role' => $this->getMainRole($chat->getChatId())]],
232 'roles' => [$roleData['code'] => $roleData],
233 ],
234 ],
236 ];
237
238 \Bitrix\Pull\Event::add(array_values($chat->getRelations()->getUserIds()), $pushMessage);
239
240 return $pushMessage;
241 }
242
243 public function getRecentKeyRoles(): array
244 {
245 $roles = $this->getRecommendedRoles();
246
247 $roleCodes = [];
248 foreach ($roles as $role)
249 {
250 $roleCodes[] = $role['code'];
251 }
252
253 return $roleCodes;
254 }
255
256 protected function getRecommendedRoles(): array
257 {
258 if (!Loader::includeModule('ai'))
259 {
260 return [];
261 }
262
263 $roleManager = $this->getAiManager();
264 $roles = $roleManager->getRecommendedRoles(4);
265 array_unshift($roles, $roleManager->getUniversalRole());
266
267 return $roles;
268 }
269
270 protected function getAiManager(): \Bitrix\AI\Role\RoleManager
271 {
272 $this->aiManager ??= new \Bitrix\AI\Role\RoleManager($this->getContext()->getUserId(), LANGUAGE_ID);
273
274 return $this->aiManager;
275 }
276}
static getPullExtra()
Определения common.php:127
formatRoleData(array $role)
Определения RoleManager.php:126
Bitrix AI Role RoleManager $aiManager
Определения RoleManager.php:21
updateRole(Chat $chat, ?string $roleCode)
Определения RoleManager.php:164
fillPrompts(array $roleCodes)
Определения RoleManager.php:104
formatRoleDataShort(array $role)
Определения RoleManager.php:138
sendPushCopilotRole(Chat $chat, array $roleData)
Определения RoleManager.php:217
getRoles(array $roleCodes, bool $withPrompts=true)
Определения RoleManager.php:55
getRolesShort(array $roleCodes)
Определения RoleManager.php:33
getMainRole(?int $chatId)
Определения RoleManager.php:147
fillRoles(array $roleCodes)
Определения RoleManager.php:88
static array $prompts
Определения RoleManager.php:19
Определения result.php:20
static add($recipient, array $parameters, $channelType=\CPullChannel::TYPE_PRIVATE)
Определения event.php:22
</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
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799