1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
CopilotPopupItem.php
См. документацию.
1<?php
2
4
11
13{
14 private ?array $roleCodes = null;
15
16 private ?array $engineCodes = null;
17
18 private ?MessageCollection $messages = null;
19
23 private ?array $chats = null;
24
25 private array $messageIds;
26
27 private array $chatIds;
28
29 public function __construct(array $chatIds = [], array $messageIds = [])
30 {
31 $chatIds = array_filter($chatIds, 'is_int');
32 $messageIds = array_filter($messageIds, 'is_int');
33
34 $this->chatIds = array_combine($chatIds, $chatIds);
35 $this->messageIds = array_combine($messageIds, $messageIds);
36 }
37
38 public static function getInstanceByChatIdsAndMessages(MessageCollection $messages, array $chatIds): self
39 {
40 $messages = clone $messages;
41 $instance = new self(chatIds: $chatIds, messageIds: $messages->getIds());
42 $instance->messages = $messages;
43
44 return $instance;
45 }
46
47 public static function getInstanceByMessages(MessageCollection $messages): self
48 {
49 $messages = clone $messages;
50 $instance = new self(messageIds: $messages->getIds());
51 $instance->messages = $messages;
52
53 return $instance;
54 }
55
56 public static function getInstanceByChatIds(array $chatIds): self
57 {
58 return new self(chatIds: $chatIds);
59 }
60
61 public function merge(PopupDataItem $item): self
62 {
63 if ($item instanceof static)
64 {
65 $this
66 ->mergeArray($this->chatIds, $item->chatIds)
67 ->mergeArray($this->messageIds, $item->messageIds)
68 ->mergeArray($this->chats, $item->chats)
69 ->mergeArray($this->engineCodes, $item->engineCodes)
70 ->mergeArray($this->roleCodes, $item->roleCodes)
71 ->mergeMessages($item->messages)
72 ;
73 }
74
75 return $this;
76 }
77
78 protected function mergeArray(?array &$target, ?array $source): self
79 {
80 if (isset($target, $source))
81 {
82 $target += $source;
83 }
84 elseif (isset($source))
85 {
86 $target = $source;
87 }
88
89 return $this;
90 }
91
92 protected function mergeMessages(?MessageCollection $messages): self
93 {
94 if (isset($this->messages, $messages))
95 {
96 $this->messages->fillParams();
97 $messages->fillParams();
98
99 $this->messages->mergeRegistry($messages);
100 }
101 elseif (isset($messages))
102 {
103 $this->messages = clone $messages;
104 }
105
106 return $this;
107 }
108
109 public static function getRestEntityName(): string
110 {
111 return 'copilot';
112 }
113
114 public function toRestFormat(array $option = []): ?array
115 {
116 $chats = $this->getChatsForRest();
117 $messages = $this->getMessagesForRest();
118
119 if (empty($chats) && empty($messages))
120 {
121 return null;
122 }
123
124 $engines = $this->getEnginesForRest();
125 $roles = $this->getRolesForRest();
126
127 return [
128 'chats' => !empty($chats) ? $chats : null,
129 'messages' => !empty($messages) ? $messages : null,
130 'engines' => !empty($engines) ? $engines : null,
131 'roles' => !empty($roles) ? $roles : null,
132 'aiProvider' => EngineManager::getDefaultEngineName(),
133 ];
134 }
135
136 protected function getChatsForRest(): array
137 {
138 $result = [];
139
140 foreach ($this->getChats() as $chat)
141 {
142 $engineCode = $chat->getEngineCode() ?: EngineManager::getDefaultEngineCode();
143
144 $result[] = [
145 'dialogId' => $chat->getDialogId(),
146 'role' => $chat->getCopilotRole(),
147 'engine' => $engineCode,
148 ];
149 }
150
151 return $result;
152 }
153
154 protected function getMessagesForRest(): array
155 {
156 $result = [];
157
158 $rolesData = $this->getMessages()->getCopilotRoles();
159
160 foreach ($rolesData as $messageId => $roleCode)
161 {
162 $result[] = [
163 'id' => $messageId,
164 'role' => $roleCode,
165 ];
166 }
167
168 return $result;
169 }
170
171 protected function getRolesForRest(): array
172 {
173 if (!isset($this->roleCodes))
174 {
175 $chatRoleCodes = [];
176
177 foreach ($this->getChats() as $chat)
178 {
179 $roleCode = $chat->getCopilotRole();
180 $chatRoleCodes[$roleCode] = $roleCode;
181 }
182
183 $messagesRoleCodes = array_values($this->getMessages()->getCopilotRoles());
184 $messagesRoleCodes = array_combine($messagesRoleCodes, $messagesRoleCodes);
185
186 $this->roleCodes = $chatRoleCodes + $messagesRoleCodes;
187 }
188
189 return (new RoleManager())->getRoles(array_values($this->roleCodes)) ?? [];
190 }
191
192 protected function getEnginesForRest(): array
193 {
194 if (!isset($this->engineCodes))
195 {
196 $engineCodes = [];
197
198 foreach ($this->getChats() as $chat)
199 {
200 $engineCode = $chat->getEngineCode() ?: EngineManager::getDefaultEngineCode();
201
202 if (isset($engineCode))
203 {
204 $engineCodes[$engineCode] = $engineCode;
205 }
206 }
207
208 $this->engineCodes = $engineCodes;
209 }
210
211 $engines = (new EngineManager())->getEnginesByCodes(array_values($this->engineCodes));
212
213 $result = [];
214 foreach ($engines as $engine)
215 {
216 $engine = $engine->getIEngine();
217 $result[] = [
218 'code' => $engine->getCode(),
219 'name' => $engine->getName(),
220 ];
221 }
222
223 return $result;
224 }
225
226 protected function getMessages(): MessageCollection
227 {
228 if (isset($this->messages))
229 {
230 return $this->messages;
231 }
232
233 $this->messages = new MessageCollection($this->messageIds);
234
235 return $this->messages;
236 }
237
241 protected function getChats(): array
242 {
243 if (isset($this->chats))
244 {
245 return array_values($this->chats);
246 }
247
248 $result = [];
249 foreach ($this->chatIds as $chatId)
250 {
251 $chat = Chat::getInstance($chatId);
252 if ($chat instanceof Chat\GroupChat && $chat->containsCopilot())
253 {
254 $result[$chat->getId()] = $chat;
255 }
256 }
257
258 $this->chats = $result;
259
260 return array_values($this->chats);
261 }
262
263 public static function convertArrayDataForChats(array $data): array
264 {
265 $result = [];
266
267 foreach ($data as $id => $item)
268 {
269 $result[] = ['dialogId' => $id, 'role' => $item];
270 }
271
272 return $result;
273 }
274
276 {
277 $result = [];
278
279 foreach ($data as $id => $item)
280 {
281 $result[] = ['id' => $id, 'role' => $item];
282 }
283
284 return $result;
285 }
286}
if(! $messageFields||!isset($messageFields['message_id'])||!isset($messageFields['status'])||!CModule::IncludeModule("messageservice")) $messageId
Определения callback_ismscenter.php:26
static convertArrayDataForMessages(array $data)
Определения CopilotPopupItem.php:275
static getInstanceByChatIds(array $chatIds)
Определения CopilotPopupItem.php:56
toRestFormat(array $option=[])
Определения CopilotPopupItem.php:114
static convertArrayDataForChats(array $data)
Определения CopilotPopupItem.php:263
mergeMessages(?MessageCollection $messages)
Определения CopilotPopupItem.php:92
static getInstanceByChatIdsAndMessages(MessageCollection $messages, array $chatIds)
Определения CopilotPopupItem.php:38
static getInstanceByMessages(MessageCollection $messages)
Определения CopilotPopupItem.php:47
__construct(array $chatIds=[], array $messageIds=[])
Определения CopilotPopupItem.php:29
mergeArray(?array &$target, ?array $source)
Определения CopilotPopupItem.php:78
merge(PopupDataItem $item)
Определения CopilotPopupItem.php:61
containsCopilot()
Определения GroupChat.php:649
static getInstance()
Определения application.php:98
$data['IS_AVAILABLE']
Определения .description.php:13
</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( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$instance
Определения ps_b24_final.php:14
$messages
Определения template.php:8
$option
Определения options.php:1711
$engine
Определения options.php:121