-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathChattyLLMController.php
More file actions
1135 lines (1056 loc) · 45.6 KB
/
ChattyLLMController.php
File metadata and controls
1135 lines (1056 loc) · 45.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Assistant\Controller;
use OCA\Assistant\AppInfo\Application;
use OCA\Assistant\Db\ChattyLLM\Message;
use OCA\Assistant\Db\ChattyLLM\MessageMapper;
use OCA\Assistant\Db\ChattyLLM\Session;
use OCA\Assistant\Db\ChattyLLM\SessionMapper;
use OCA\Assistant\ResponseDefinitions;
use OCA\Assistant\Service\SessionSummaryService;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\OCSController;
use OCP\Exceptions\AppConfigTypeConflictException;
use OCP\IAppConfig;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUserManager;
use OCP\TaskProcessing\Exception\Exception;
use OCP\TaskProcessing\Exception\NotFoundException;
use OCP\TaskProcessing\Exception\PreConditionNotMetException;
use OCP\TaskProcessing\Exception\UnauthorizedException;
use OCP\TaskProcessing\Exception\ValidationException;
use OCP\TaskProcessing\IManager as ITaskProcessingManager;
use OCP\TaskProcessing\Task;
use OCP\TaskProcessing\TaskTypes\TextToTextChat;
use Psr\Log\LoggerInterface;
/**
* @psalm-import-type AssistantChatSession from ResponseDefinitions
* @psalm-import-type AssistantChatMessage from ResponseDefinitions
* @psalm-import-type AssistantChatAgencyMessage from ResponseDefinitions
* @psalm-import-type AssistantChatSessionCheck from ResponseDefinitions
*/
class ChattyLLMController extends OCSController {
private array $agencyActionData;
public function __construct(
string $appName,
IRequest $request,
private SessionMapper $sessionMapper,
private MessageMapper $messageMapper,
private IL10N $l10n,
private LoggerInterface $logger,
private ITaskProcessingManager $taskProcessingManager,
private IAppConfig $appConfig,
private IUserManager $userManager,
private ?string $userId,
private SessionSummaryService $sessionSummaryService,
) {
parent::__construct($appName, $request);
$this->agencyActionData = [
// talk
'send_message_to_conversation' => [
'title' => $this->l10n->t('Send a message to a Talk conversation'),
'icon' => 'Send',
],
'create_public_conversation' => [
'title' => $this->l10n->t('Create a conversation'),
'icon' => 'ChatPlus',
],
// mail
'send_email' => [
'title' => $this->l10n->t('Send an email'),
'icon' => 'EmailPlus',
],
// calendar
'schedule_event' => [
'title' => $this->l10n->t('Schedule a calendar event'),
'icon' => 'CalendarPlus',
],
'add_task' => [
'title' => $this->l10n->t('Add a calendar task'),
'icon' => 'CalendarCheck',
],
// deck
'add_card' => [
'title' => $this->l10n->t('Create a Deck card'),
'icon' => 'CardPlus',
],
];
}
private function improveAgencyActionNames(array $actions): array {
return array_map(function ($action) {
if (isset($action->name, $this->agencyActionData[$action->name])) {
if (isset($this->agencyActionData[$action->name]['icon'])) {
$action->icon = $this->agencyActionData[$action->name]['icon'];
}
if (isset($this->agencyActionData[$action->name]['title'])) {
$action->name = $this->agencyActionData[$action->name]['title'];
}
}
return $action;
}, $actions);
}
/**
* Create chat session
*
* Create a new chat session, add a system message with user instructions
*
* @param int $timestamp The session creation date
* @param ?string $title The session title
* @return JSONResponse<Http::STATUS_OK, array{session: AssistantChatSession}, array{}>|JSONResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_UNAUTHORIZED, array{error: string}, array{}>
* @throws AppConfigTypeConflictException
*
* 200: Chat session has been successfully created
* 401: User is either not logged in or not found
*/
#[NoAdminRequired]
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT, tags: ['chat_api'])]
public function newSession(int $timestamp, ?string $title = null): JSONResponse {
if ($this->userId === null) {
return new JSONResponse(['error' => $this->l10n->t('User not logged in')], Http::STATUS_UNAUTHORIZED);
}
$user = $this->userManager->get($this->userId);
if ($user === null) {
return new JSONResponse(['error' => $this->l10n->t('User not found')], Http::STATUS_UNAUTHORIZED);
}
$userInstructions = $this->appConfig->getValueString(
Application::APP_ID,
'chat_user_instructions',
Application::CHAT_USER_INSTRUCTIONS,
) ?: Application::CHAT_USER_INSTRUCTIONS;
$userInstructions = str_replace('{user}', $user->getDisplayName(), $userInstructions);
try {
$session = new Session();
$session->setUserId($this->userId);
$session->setTitle($title);
$session->setTimestamp($timestamp);
$session->setAgencyConversationToken(null);
$session->setAgencyPendingActions(null);
$this->sessionMapper->insert($session);
$systemMsg = new Message();
$systemMsg->setSessionId($session->getId());
$systemMsg->setRole('system');
$systemMsg->setAttachments('[]');
$systemMsg->setContent($userInstructions);
$systemMsg->setTimestamp($session->getTimestamp());
$systemMsg->setSources('[]');
$this->messageMapper->insert($systemMsg);
return new JSONResponse([
'session' => $session->jsonSerialize(),
]);
} catch (\OCP\DB\Exception|\RuntimeException $e) {
$this->logger->warning('Failed to create a chat session', ['exception' => $e]);
return new JSONResponse(['error' => $this->l10n->t('Failed to create a chat session')], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Update session title
*
* Update the title of a chat session
*
* @param integer $sessionId The chat session ID
* @param string $title The new chat session title
* @return JSONResponse<Http::STATUS_OK, list{}, array{}>|JSONResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_UNAUTHORIZED, array{error: string}, array{}>
*
* 200: The title has been updated successfully
* 401: Not logged in
*/
#[NoAdminRequired]
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT, tags: ['chat_api'])]
public function updateSessionTitle(int $sessionId, string $title): JSONResponse {
if ($this->userId === null) {
return new JSONResponse(['error' => $this->l10n->t('User not logged in')], Http::STATUS_UNAUTHORIZED);
}
try {
$this->sessionMapper->updateSessionTitle($this->userId, $sessionId, $title);
return new JSONResponse();
} catch (\OCP\DB\Exception|\RuntimeException $e) {
$this->logger->warning('Failed to update the chat session', ['exception' => $e]);
return new JSONResponse(['error' => $this->l10n->t('Failed to update the chat session')], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Update session
*
* @param integer $sessionId The chat session ID
* @param string|null $title The new chat session title
* @param bool|null $is_remembered The new is_remembered status: Whether to remember the insights from this chat session across all chat session
* @return JSONResponse<Http::STATUS_OK, list{}, array{}>|JSONResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_NOT_FOUND, array{error: string}, array{}>
*
* 200: The title has been updated successfully
* 404: The session was not found
*/
#[NoAdminRequired]
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT, tags: ['chat_api'])]
public function updateChatSession(int $sessionId, ?string $title = null, ?bool $is_remembered = null): JSONResponse {
if ($this->userId === null) {
return new JSONResponse(['error' => $this->l10n->t('Could not find session')], Http::STATUS_NOT_FOUND);
}
if ($title === null && $is_remembered === null) {
return new JSONResponse();
}
try {
$session = $this->sessionMapper->getUserSession($this->userId, $sessionId);
if ($title !== null) {
$session->setTitle($title);
}
if ($is_remembered !== null) {
$session->setIsRemembered($is_remembered);
// schedule summarizer jobs for this chat user
if ($is_remembered) {
$this->sessionSummaryService->scheduleJobsForUser($this->userId);
}
}
$this->sessionMapper->update($session);
return new JSONResponse();
} catch (\OCP\DB\Exception|\RuntimeException $e) {
$this->logger->warning('Failed to update the chat session', ['exception' => $e]);
return new JSONResponse(['error' => $this->l10n->t('Failed to update the chat session')], Http::STATUS_INTERNAL_SERVER_ERROR);
} catch (DoesNotExistException|MultipleObjectsReturnedException $e) {
return new JSONResponse(['error' => $this->l10n->t('Could not find session')], Http::STATUS_NOT_FOUND);
}
}
/**
* Delete a chat session
*
* Delete a chat session by ID
*
* @param integer $sessionId The session ID
* @return JSONResponse<Http::STATUS_OK, list{}, array{}>|JSONResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_UNAUTHORIZED, array{error: string}, array{}>
*
* 200: The session has been deleted successfully
* 401: Not logged in
*/
#[NoAdminRequired]
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT, tags: ['chat_api'])]
public function deleteSession(int $sessionId): JSONResponse {
if ($this->userId === null) {
return new JSONResponse(['error' => $this->l10n->t('User not logged in')], Http::STATUS_UNAUTHORIZED);
}
try {
$this->deleteSessionTasks($this->userId, $sessionId);
$this->sessionMapper->deleteSession($this->userId, $sessionId);
$this->messageMapper->deleteMessagesBySession($sessionId);
return new JSONResponse();
} catch (\OCP\DB\Exception|\RuntimeException $e) {
$this->logger->warning('Failed to delete the chat session', ['exception' => $e]);
return new JSONResponse(['error' => $this->l10n->t('Failed to delete the chat session')], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
private function deleteSessionTasks(string $userId, int $sessionId): void {
$sessionExists = $this->sessionMapper->exists($this->userId, $sessionId);
if (!$sessionExists) {
return;
}
$messages = $this->messageMapper->getMessages($sessionId, 0, 0);
foreach ($messages as $message) {
$ocpTaskId = $message->getOcpTaskId();
if ($ocpTaskId !== 0) {
try {
$task = $this->taskProcessingManager->getTask($ocpTaskId);
$this->taskProcessingManager->deleteTask($task);
} catch (\OCP\TaskProcessing\Exception\Exception) {
// silent failure here because:
// if the task is not found: all good nothing to delete
// if the task couldn't be deleted, it will be deleted by the task processing cleanup job later anyway
}
}
}
}
/**
* Get chat sessions
*
* Get all chat sessions for the current user
*
* @return JSONResponse<Http::STATUS_OK, list<AssistantChatSession>, array{}>|JSONResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_UNAUTHORIZED, array{error: string}, array{}>
*
* 200: The session list has been obtained successfully
* 401: Not logged in
*/
#[NoAdminRequired]
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT, tags: ['chat_api'])]
public function getSessions(): JSONResponse {
if ($this->userId === null) {
return new JSONResponse(['error' => $this->l10n->t('User not logged in')], Http::STATUS_UNAUTHORIZED);
}
try {
$sessions = $this->sessionMapper->getUserSessions($this->userId);
return new JSONResponse($sessions);
} catch (\OCP\DB\Exception $e) {
$this->logger->warning('Failed to get chat sessions', ['exception' => $e]);
return new JSONResponse(['error' => $this->l10n->t('Failed to get chat sessions')], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Add a message
*
* Add a new chat message to the session
*
* @param int $sessionId The chat session ID
* @param string $role Role of the message (human, assistant etc...)
* @param string $content Content of the message
* @param int $timestamp Date of the message
* @param ?list<array{type: string, file_id: int}> $attachments List of attachment objects
* @param bool $firstHumanMessage Is it the first human message of the session?
* @return JSONResponse<Http::STATUS_OK, AssistantChatMessage, array{}>|JSONResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_UNAUTHORIZED|Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, array{error: string}, array{}>
*
* 200: The session list has been obtained successfully
* 401: Not logged in
* 404: Session was not found
* 400: Message is malformed
*/
#[NoAdminRequired]
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT, tags: ['chat_api'])]
public function newMessage(
int $sessionId, string $role, string $content, int $timestamp, ?array $attachments = null, bool $firstHumanMessage = false,
): JSONResponse {
if ($this->userId === null) {
return new JSONResponse(['error' => $this->l10n->t('User not logged in')], Http::STATUS_UNAUTHORIZED);
}
if (strlen($content) > Application::MAX_TEXT_INPUT_LENGTH) {
return new JSONResponse(['error' => $this->l10n->t('The new message is too long')], Http::STATUS_BAD_REQUEST);
}
try {
$sessionExists = $this->sessionMapper->exists($this->userId, $sessionId);
if (!$sessionExists) {
return new JSONResponse(['error' => $this->l10n->t('Session not found')], Http::STATUS_NOT_FOUND);
}
// refuse empty text content if context agent is not available (we do classic chat) AND there is no attachment
// in other words: accept empty content if we are using agency OR there are attachments
$content = trim($content);
if (empty($content)
&& (!class_exists('OCP\\TaskProcessing\\TaskTypes\\ContextAgentInteraction')
|| !isset($this->taskProcessingManager->getAvailableTaskTypes()[\OCP\TaskProcessing\TaskTypes\ContextAgentInteraction::ID]))
&& $attachments === null
) {
return new JSONResponse(['error' => $this->l10n->t('Message content is empty')], Http::STATUS_BAD_REQUEST);
}
$message = new Message();
$message->setSessionId($sessionId);
$message->setRole($role);
$message->setContent($content);
$message->setTimestamp($timestamp);
$message->setSources('[]');
$message->setAttachments('[]');
if ($attachments !== null) {
$encodedAttachments = json_encode($attachments);
if ($encodedAttachments !== false) {
$message->setAttachments($encodedAttachments);
}
}
$this->messageMapper->insert($message);
if ($firstHumanMessage) {
// set the title of the session based on first human message
$this->sessionMapper->updateSessionTitle(
$this->userId,
$sessionId,
strlen($content) > 140 ? mb_substr($content, 0, 140) . '...' : $content,
);
}
return new JSONResponse($message->jsonSerialize());
} catch (\OCP\DB\Exception $e) {
$this->logger->warning('Failed to add a chat message', ['exception' => $e]);
return new JSONResponse(['error' => $this->l10n->t('Failed to add a chat message')], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Get session messages
*
* Get chat messages for the session without the system message
*
* @param int $sessionId The session ID
* @param int $limit The max number of messages to return
* @param int $cursor The index of the first result to return
* @return JSONResponse<Http::STATUS_OK, list<AssistantChatMessage>, array{}>|JSONResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_UNAUTHORIZED|Http::STATUS_NOT_FOUND, array{error: string}, array{}>
*
* 200: The message list has been successfully obtained
* 401: Not logged in
* 404: The session was not found
*/
#[NoAdminRequired]
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT, tags: ['chat_api'])]
public function getMessages(int $sessionId, int $limit = 20, int $cursor = 0): JSONResponse {
if ($this->userId === null) {
return new JSONResponse(['error' => $this->l10n->t('User not logged in')], Http::STATUS_UNAUTHORIZED);
}
try {
$sessionExists = $this->sessionMapper->exists($this->userId, $sessionId);
if (!$sessionExists) {
return new JSONResponse(['error' => $this->l10n->t('Session not found')], Http::STATUS_NOT_FOUND);
}
/** @var list<Message> $messages */
$messages = $this->messageMapper->getMessages($sessionId, $cursor, $limit);
if ($messages[0]->getRole() === 'system') {
array_shift($messages);
}
return new JSONResponse(array_map(static function (Message $message) { return $message->jsonSerialize(); }, $messages));
} catch (\OCP\DB\Exception $e) {
$this->logger->warning('Failed to get chat messages', ['exception' => $e]);
return new JSONResponse(['error' => $this->l10n->t('Failed to get chat messages')], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Get a message
*
* Get a chat message in a session
*
* @param int $sessionId The session ID
* @param int $messageId The message ID
* @return JSONResponse<Http::STATUS_OK, AssistantChatMessage, array{}>|JSONResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_UNAUTHORIZED|Http::STATUS_NOT_FOUND, array{error: string}, array{}>
*
* 200: The message has been successfully obtained
* 401: Not logged in
* 404: The session or the message was not found
*/
#[NoAdminRequired]
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT, tags: ['chat_api'])]
public function getMessage(int $sessionId, int $messageId): JSONResponse {
if ($this->userId === null) {
return new JSONResponse(['error' => $this->l10n->t('User not logged in')], Http::STATUS_UNAUTHORIZED);
}
try {
$sessionExists = $this->sessionMapper->exists($this->userId, $sessionId);
if (!$sessionExists) {
return new JSONResponse(['error' => $this->l10n->t('Session not found')], Http::STATUS_NOT_FOUND);
}
$message = $this->messageMapper->getMessageById($sessionId, $messageId);
return new JSONResponse($message->jsonSerialize());
} catch (\OCP\DB\Exception $e) {
$this->logger->warning('Failed to get chat messages', ['exception' => $e]);
return new JSONResponse(['error' => $this->l10n->t('Failed to get chat message')], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Delete a message
*
* Delete a chat message by ID
*
* @param integer $messageId The message ID
* @param integer $sessionId The session ID
* @return JSONResponse<Http::STATUS_OK, list{}, array{}>|JSONResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_UNAUTHORIZED|Http::STATUS_NOT_FOUND, array{error: string}, array{}>
*
* 200: The message has been successfully deleted
* 401: Not logged in
* 404: The session was not found
*/
#[NoAdminRequired]
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT, tags: ['chat_api'])]
public function deleteMessage(int $messageId, int $sessionId): JSONResponse {
if ($this->userId === null) {
return new JSONResponse(['error' => $this->l10n->t('User not logged in')], Http::STATUS_UNAUTHORIZED);
}
try {
$sessionExists = $this->sessionMapper->exists($this->userId, $sessionId);
if (!$sessionExists) {
return new JSONResponse(['error' => $this->l10n->t('Session not found')], Http::STATUS_NOT_FOUND);
}
$message = $this->messageMapper->getMessageById($sessionId, $messageId);
$ocpTaskId = $message->getOcpTaskId();
$this->messageMapper->deleteMessageById($sessionId, $messageId);
// delete the related task
if ($ocpTaskId !== 0) {
try {
$task = $this->taskProcessingManager->getTask($ocpTaskId);
$this->taskProcessingManager->deleteTask($task);
} catch (\OCP\TaskProcessing\Exception\Exception) {
}
}
return new JSONResponse();
} catch (\OCP\DB\Exception|\RuntimeException $e) {
$this->logger->warning('Failed to delete a chat message', ['exception' => $e]);
return new JSONResponse(['error' => $this->l10n->t('Failed to delete a chat message')], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Generate a new assistant message
*
* Schedule a task to generate a new message for a session
*
* @param integer $sessionId The session ID
* @param int $agencyConfirm Potential agency sensitive actions confirmation (1: accept, 0: reject)
* @return JSONResponse<Http::STATUS_OK, array{taskId: int}, array{}>|JSONResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_UNAUTHORIZED|Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, array{error: string}, array{}>
* @throws AppConfigTypeConflictException
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
* @throws \OCP\DB\Exception
*
* 200: The task has been successfully scheduled
* 401: Not logged in
* 404: Session was not found
* 400: Task was not scheduled
*/
#[NoAdminRequired]
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT, tags: ['chat_api'])]
public function generateForSession(int $sessionId, int $agencyConfirm = 0): JSONResponse {
if ($this->userId === null) {
return new JSONResponse(['error' => $this->l10n->t('User not logged in')], Http::STATUS_UNAUTHORIZED);
}
$sessionExists = $this->sessionMapper->exists($this->userId, $sessionId);
if (!$sessionExists) {
return new JSONResponse(['error' => $this->l10n->t('Session not found')], Http::STATUS_NOT_FOUND);
}
if (class_exists('OCP\\TaskProcessing\\TaskTypes\\ContextAgentInteraction')
&& isset($this->taskProcessingManager->getAvailableTaskTypes()[\OCP\TaskProcessing\TaskTypes\ContextAgentInteraction::ID])
) {
$lastUserMessage = $this->messageMapper->getLastHumanMessage($sessionId);
$session = $this->sessionMapper->getUserSession($this->userId, $sessionId);
$lastConversationToken = $session->getAgencyConversationToken() ?? '{}';
$lastAttachments = $lastUserMessage->jsonSerialize()['attachments'];
$audioAttachment = $lastAttachments[0] ?? null;
// see https://github.com/vimeo/psalm/issues/7980
$isContextAgentAudioAvailable = false;
if (class_exists('OCP\\TaskProcessing\\TaskTypes\\ContextAgentAudioInteraction')) {
$isContextAgentAudioAvailable = isset($this->taskProcessingManager->getAvailableTaskTypes()[\OCP\TaskProcessing\TaskTypes\ContextAgentAudioInteraction::ID]);
}
if ($audioAttachment !== null
&& isset($audioAttachment['type'])
&& $audioAttachment['type'] === 'Audio'
&& $isContextAgentAudioAvailable
) {
// audio agency
$fileId = $audioAttachment['file_id'];
try {
$taskId = $this->scheduleAgencyAudioTask($fileId, $agencyConfirm, $lastConversationToken, $sessionId, $lastUserMessage->getId());
} catch (\Exception $e) {
return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
}
} else {
// classic agency
$prompt = $lastUserMessage->getContent();
try {
$taskId = $this->scheduleAgencyTask($prompt, $agencyConfirm, $lastConversationToken, $sessionId);
} catch (\Exception $e) {
return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
}
}
} else {
// classic chat
$systemPrompt = '';
$firstMessage = $this->messageMapper->getFirstNMessages($sessionId, 1);
if ($firstMessage->getRole() === 'system') {
$systemPrompt = $firstMessage->getContent();
}
$history = $this->getRawLastMessages($sessionId);
do {
$lastUserMessage = array_pop($history);
} while ($lastUserMessage->getRole() !== 'human');
$lastAttachments = $lastUserMessage->jsonSerialize()['attachments'];
$audioAttachment = $lastAttachments[0] ?? null;
$isAudioToAudioAvailable = false;
if (class_exists('OCP\\TaskProcessing\\TaskTypes\\AudioToAudioChat')) {
$isAudioToAudioAvailable = isset($this->taskProcessingManager->getAvailableTaskTypes()[\OCP\TaskProcessing\TaskTypes\AudioToAudioChat::ID]);
}
if ($audioAttachment !== null
&& isset($audioAttachment['type'])
&& $audioAttachment['type'] === 'Audio'
&& $isAudioToAudioAvailable
) {
// for an audio chat task, let's try to get the remote audio IDs for all the previous audio messages
$history = $this->getAudioHistory($history);
$fileId = $audioAttachment['file_id'];
try {
$taskId = $this->scheduleAudioChatTask($fileId, $systemPrompt, $history, $sessionId, $lastUserMessage->getId());
} catch (\Exception $e) {
return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
}
} else {
// for a text chat task, let's only use text in the history
$history = array_map(static function (Message $message) {
return json_encode([
'role' => $message->getRole(),
'content' => $message->getContent(),
]);
}, $history);
try {
$taskId = $this->scheduleLLMChatTask($lastUserMessage->getContent(), $systemPrompt, $history, $sessionId);
} catch (\Exception $e) {
return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
}
}
}
return new JSONResponse(['taskId' => $taskId]);
}
private function getAudioHistory(array $history): array {
// history is a list of JSON strings
// The content is the remote audio ID (or the transcription as fallback)
// We only use the audio ID for assistant messages, if we have one and if it's not expired
// The audio ID is found in integration_openai's AudioToAudioChat response for example
// It is an optional output of AudioToAudioChat tasks
return array_map(static function (Message $message) {
$entry = [
'role' => $message->getRole(),
];
$attachments = $message->jsonSerialize()['attachments'];
if ($message->getRole() === 'assistant'
&& count($attachments) > 0
&& $attachments[0]['type'] === 'Audio'
&& isset($attachments[0]['remote_audio_id'])
) {
if (!isset($attachments[0]['remote_audio_expires_at'])
|| time() < $attachments[0]['remote_audio_expires_at']
) {
$entry['audio'] = ['id' => $attachments[0]['remote_audio_id']];
return json_encode($entry);
}
}
$entry['content'] = $message->getContent();
return json_encode($entry);
}, $history);
}
/**
* Regenerate response for a message
*
* Delete the message with the given message ID and all following ones,
* then schedule a task to generate a new message for the session
*
* @param int $sessionId The chat session ID
* @param int $messageId The chat message ID
* @return JSONResponse<Http::STATUS_OK, array{taskId: int}, array{}>|JSONResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_UNAUTHORIZED|Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, array{error: string}, array{}>
* @throws AppConfigTypeConflictException
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
* @throws \OCP\DB\Exception
*
* 200: The task has been successfully scheduled
* 401: Not logged in
* 404: Session was not found
* 400: Task was not scheduled
*/
#[NoAdminRequired]
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT, tags: ['chat_api'])]
public function regenerateForSession(int $sessionId, int $messageId): JSONResponse {
if ($this->userId === null) {
return new JSONResponse(['error' => $this->l10n->t('User not logged in')], Http::STATUS_UNAUTHORIZED);
}
$sessionExists = $this->sessionMapper->exists($this->userId, $sessionId);
if (!$sessionExists) {
return new JSONResponse(['error' => $this->l10n->t('Session not found')], Http::STATUS_NOT_FOUND);
}
$message = $this->messageMapper->getMessageById($sessionId, $messageId);
$ocpTaskId = $message->getOcpTaskId();
try {
$this->messageMapper->deleteMessageById($sessionId, $messageId);
} catch (\OCP\DB\Exception|\RuntimeException $e) {
$this->logger->warning('Failed to delete the last message', ['exception' => $e]);
return new JSONResponse(['error' => $this->l10n->t('Failed to delete the last message')], Http::STATUS_INTERNAL_SERVER_ERROR);
}
// delete the related task
if ($ocpTaskId !== 0) {
try {
$task = $this->taskProcessingManager->getTask($ocpTaskId);
$this->taskProcessingManager->deleteTask($task);
} catch (\OCP\TaskProcessing\Exception\Exception) {
}
}
return $this->generateForSession($sessionId);
}
/**
* Check the status of a generation task. The value of slow_pickup will be set to true if the task is not being picked up.
*
* Used by the frontend to poll a generation task status. If the task succeeds, a new message is stored and returned.
*
* @param int $taskId The message generation task ID
* @param int $sessionId The chat session ID
* @return JSONResponse<Http::STATUS_OK, AssistantChatAgencyMessage, array{}>|JSONResponse<Http::STATUS_EXPECTATION_FAILED, array{task_status: int, slow_pickup: bool}, array{}>|JSONResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_UNAUTHORIZED|Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, array{error: string}, array{}>
* @throws MultipleObjectsReturnedException
* @throws \OCP\DB\Exception
*
* 200: The task was successful, a message has been generated
* 401: Not logged in
* 404: Session was not found
* 400: Task processing failed
* 417: The task is still running or has not been picked up yet
*/
#[NoAdminRequired]
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT, tags: ['chat_api'])]
public function checkMessageGenerationTask(int $taskId, int $sessionId): JSONResponse {
if ($this->userId === null) {
return new JSONResponse(['error' => $this->l10n->t('User not logged in')], Http::STATUS_UNAUTHORIZED);
}
$sessionExists = $this->sessionMapper->exists($this->userId, $sessionId);
if (!$sessionExists) {
return new JSONResponse(['error' => $this->l10n->t('Session not found')], Http::STATUS_NOT_FOUND);
}
try {
$task = $this->taskProcessingManager->getTask($taskId);
} catch (NotFoundException $e) {
return new JSONResponse(['error' => 'task_not_found'], Http::STATUS_NOT_FOUND);
} catch (\OCP\TaskProcessing\Exception\Exception $e) {
return new JSONResponse(['error' => 'task_query_failed'], Http::STATUS_BAD_REQUEST);
}
if ($task->getStatus() === Task::STATUS_SUCCESSFUL) {
try {
$message = $this->messageMapper->getMessageByTaskId($sessionId, $taskId);
$jsonMessage = $message->jsonSerialize();
$session = $this->sessionMapper->getUserSession($this->userId, $sessionId);
$jsonMessage['sessionAgencyPendingActions'] = $session->getAgencyPendingActions();
if ($jsonMessage['sessionAgencyPendingActions'] !== null) {
$jsonMessage['sessionAgencyPendingActions'] = json_decode($jsonMessage['sessionAgencyPendingActions']);
$jsonMessage['sessionAgencyPendingActions'] = $this->improveAgencyActionNames($jsonMessage['sessionAgencyPendingActions']);
}
// do not insert here, it is done by the listener
return new JSONResponse($jsonMessage);
} catch (\OCP\DB\Exception $e) {
$this->logger->warning('Failed to add a chat message into the DB', ['exception' => $e]);
return new JSONResponse(['error' => $this->l10n->t('Failed to add a chat message into DB')], Http::STATUS_INTERNAL_SERVER_ERROR);
} catch (DoesNotExistException $e) {
$this->logger->debug('Task finished successfully but failed to find the chat message in the DB. It should be created soon.', ['exception' => $e]);
return new JSONResponse(['task_status' => $task->getstatus()], Http::STATUS_EXPECTATION_FAILED);
}
} elseif ($task->getstatus() === Task::STATUS_RUNNING || $task->getstatus() === Task::STATUS_SCHEDULED) {
$startTime = $task->getStartedAt() ?? time();
$slowPickup = ($task->getScheduledAt() + (60 * 5)) < $startTime;
return new JSONResponse(['task_status' => $task->getstatus(), 'slow_pickup' => $slowPickup], Http::STATUS_EXPECTATION_FAILED);
} elseif ($task->getstatus() === Task::STATUS_FAILED || $task->getstatus() === Task::STATUS_CANCELLED) {
return new JSONResponse(['error' => 'task_failed_or_canceled', 'task_status' => $task->getstatus()], Http::STATUS_BAD_REQUEST);
}
return new JSONResponse(['error' => 'unknown_error', 'task_status' => $task->getstatus()], Http::STATUS_BAD_REQUEST);
}
/**
* Check the status of a session
*
* Used by the frontend to determine if it should poll a generation task status.
*
* @param int $sessionId The chat session ID
* @return JSONResponse<Http::STATUS_OK, AssistantChatSessionCheck, array{}>|JSONResponse<Http::STATUS_UNAUTHORIZED|Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, array{error: string}, array{}>
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
* @throws \JsonException
* @throws \OCP\DB\Exception
*
* 200: The session status has been successfully obtained
* 401: Not logged in
* 404: Session was not found
* 400: Task processing failed, impossible to check the related tasks
*/
#[NoAdminRequired]
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT, tags: ['chat_api'])]
public function checkSession(int $sessionId): JSONResponse {
if ($this->userId === null) {
return new JSONResponse(['error' => $this->l10n->t('User not logged in')], Http::STATUS_UNAUTHORIZED);
}
$sessionExists = $this->sessionMapper->exists($this->userId, $sessionId);
if (!$sessionExists) {
return new JSONResponse(['error' => $this->l10n->t('Session not found')], Http::STATUS_NOT_FOUND);
}
try {
$messageTasks = $this->taskProcessingManager->getUserTasksByApp($this->userId, Application::APP_ID . ':chatty-llm', 'chatty-llm:' . $sessionId);
$titleTasks = $this->taskProcessingManager->getUserTasksByApp($this->userId, Application::APP_ID . ':chatty-llm', 'chatty-title:' . $sessionId);
} catch (\OCP\TaskProcessing\Exception\Exception $e) {
return new JSONResponse(['error' => 'task_query_failed'], Http::STATUS_BAD_REQUEST);
}
$messageTasks = array_filter($messageTasks, static function (Task $task) {
return $task->getStatus() === Task::STATUS_RUNNING || $task->getStatus() === Task::STATUS_SCHEDULED;
});
$titleTasks = array_filter($titleTasks, static function (Task $task) {
return $task->getStatus() === Task::STATUS_RUNNING || $task->getStatus() === Task::STATUS_SCHEDULED;
});
$session = $this->sessionMapper->getUserSession($this->userId, $sessionId);
$pendingActions = $session->getAgencyPendingActions();
if ($pendingActions !== null) {
$pendingActions = json_decode($pendingActions);
$pendingActions = $this->improveAgencyActionNames($pendingActions);
}
/** @var ?array<string, mixed> $p */
$p = $pendingActions;
$responseData = [
'messageTaskId' => null,
'titleTaskId' => null,
'sessionTitle' => $session->getTitle(),
'is_remembered' => $session->getIsRemembered(),
'sessionAgencyPendingActions' => $p,
];
if (!empty($messageTasks)) {
$task = array_pop($messageTasks);
$responseData['messageTaskId'] = $task->getId();
}
if (!empty($titleTasks)) {
$task = array_pop($titleTasks);
$responseData['titleTaskId'] = $task->getId();
}
return new JSONResponse($responseData);
}
/**
* Generate a session title
*
* Schedule a task to generate a title for a chat session
*
* @param integer $sessionId The chat session ID
* @return JSONResponse<Http::STATUS_OK, array{taskId: int}, array{}>|JSONResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_UNAUTHORIZED|Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, array{error: string}, array{}>
* @throws AppConfigTypeConflictException
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
* @throws \OCP\DB\Exception
*
* 200: The task has been successfully scheduled
* 401: Not logged in
* 404: Session was not found
* 400: Task was not scheduled
*/
#[NoAdminRequired]
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT, tags: ['chat_api'])]
public function generateTitle(int $sessionId): JSONResponse {
if ($this->userId === null) {
return new JSONResponse(['error' => $this->l10n->t('User not logged in')], Http::STATUS_UNAUTHORIZED);
}
$user = $this->userManager->get($this->userId);
if ($user === null) {
return new JSONResponse(['error' => $this->l10n->t('User not found')], Http::STATUS_UNAUTHORIZED);
}
$sessionExists = $this->sessionMapper->exists($this->userId, $sessionId);
if (!$sessionExists) {
return new JSONResponse(['error' => $this->l10n->t('Session not found')], Http::STATUS_NOT_FOUND);
}
try {
$userInstructions = $this->appConfig->getValueString(
Application::APP_ID,
'chat_user_instructions_title',
Application::CHAT_USER_INSTRUCTIONS_TITLE,
) ?: Application::CHAT_USER_INSTRUCTIONS_TITLE;
$userInstructions = str_replace('{user}', $user->getDisplayName(), $userInstructions);
$systemPrompt = '';
$firstMessage = $this->messageMapper->getFirstNMessages($sessionId, 1);
if ($firstMessage->getRole() === 'system') {
$systemPrompt = $firstMessage->getContent();
}
$history = $this->getRawLastMessages($sessionId);
// history is a list of JSON strings
$history = array_map(static function (Message $message) {
return json_encode([
'role' => $message->getRole(),
'content' => $message->getContent(),
]);
}, $history);
try {
$taskId = $this->scheduleLLMChatTask($userInstructions, $systemPrompt, $history, $sessionId, false);
} catch (\Exception $e) {
return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
}
return new JSONResponse(['taskId' => $taskId]);
} catch (\OCP\DB\Exception $e) {
$this->logger->warning('Failed to generate a title for the chat session', ['exception' => $e]);
return new JSONResponse(['error' => $this->l10n->t('Failed to generate a title for the chat session')], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Check the status of a title generation task
*
* Used by the frontend to poll a title generation task status. If the task succeeds, the new title is set and returned.
*
* @param int $taskId The title generation task ID
* @param int $sessionId The chat session ID
* @return JSONResponse<Http::STATUS_OK, array{result: string}, array{}>|JSONResponse<Http::STATUS_EXPECTATION_FAILED, array{task_status: int}, array{}>|JSONResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_UNAUTHORIZED|Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, array{error: string}, array{}>
* @throws AppConfigTypeConflictException
* @throws \OCP\DB\Exception 200: The task was successful, a message has been generated
*
* 200: Title has been successfully generated
* 401: Not logged in
* 404: Session was not found
* 400: Task processing failed
* 417: The task is still running or has not been picked up yet
*/
#[NoAdminRequired]
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT, tags: ['chat_api'])]
public function checkTitleGenerationTask(int $taskId, int $sessionId): JSONResponse {
if ($this->userId === null) {
return new JSONResponse(['error' => $this->l10n->t('User not logged in')], Http::STATUS_UNAUTHORIZED);
}
$user = $this->userManager->get($this->userId);
if ($user === null) {
return new JSONResponse(['error' => $this->l10n->t('User not found')], Http::STATUS_UNAUTHORIZED);
}
$sessionExists = $this->sessionMapper->exists($this->userId, $sessionId);
if (!$sessionExists) {
return new JSONResponse(['error' => $this->l10n->t('Session not found')], Http::STATUS_NOT_FOUND);
}
try {
$task = $this->taskProcessingManager->getTask($taskId);
} catch (NotFoundException $e) {
return new JSONResponse(['error' => 'task_not_found'], Http::STATUS_NOT_FOUND);
} catch (\OCP\TaskProcessing\Exception\Exception $e) {
return new JSONResponse(['error' => 'task_query_failed'], Http::STATUS_BAD_REQUEST);
}
if ($task->getStatus() === Task::STATUS_SUCCESSFUL) {
try {
$taskOutput = trim($task->getOutput()['output'] ?? '');
$userInstructions = $this->appConfig->getValueString(
Application::APP_ID,
'chat_user_instructions_title',
Application::CHAT_USER_INSTRUCTIONS_TITLE,
) ?: Application::CHAT_USER_INSTRUCTIONS_TITLE;
$userInstructions = str_replace('{user}', $user->getDisplayName(), $userInstructions);
$title = str_replace($userInstructions, '', $taskOutput);
$title = str_replace('"', '', $title);
$title = explode(PHP_EOL, $title)[0];
$title = trim($title);
// do not write the title here since it's done in the listener
return new JSONResponse(['result' => $title]);
} catch (\OCP\DB\Exception $e) {
$this->logger->warning('Failed to generate a title for the chat session', ['exception' => $e]);
return new JSONResponse(['error' => $this->l10n->t('Failed to generate a title for the chat session')], Http::STATUS_INTERNAL_SERVER_ERROR);
}
} elseif ($task->getstatus() === Task::STATUS_RUNNING || $task->getstatus() === Task::STATUS_SCHEDULED) {
return new JSONResponse(['task_status' => $task->getstatus()], Http::STATUS_EXPECTATION_FAILED);
} elseif ($task->getstatus() === Task::STATUS_FAILED || $task->getstatus() === Task::STATUS_CANCELLED) {
return new JSONResponse(['error' => 'task_failed_or_canceled', 'task_status' => $task->getstatus()], Http::STATUS_BAD_REQUEST);
}
return new JSONResponse(['error' => 'unknown_error', 'task_status' => $task->getstatus()], Http::STATUS_BAD_REQUEST);
}
/**
* Get the last N messages (assistant and user messages, avoid initial system prompt) as an array
*
* @param integer $sessionId
* @return array<Message>
* @throws AppConfigTypeConflictException
* @throws \OCP\DB\Exception
*/
private function getRawLastMessages(int $sessionId): array {
$lastNMessages = intval($this->appConfig->getValueString(Application::APP_ID, 'chat_last_n_messages', '10'));
$messages = $this->messageMapper->getMessages($sessionId, 0, $lastNMessages);
if ($messages[0]->getRole() === 'system') {
array_shift($messages);
}
return $messages;
}
private function checkIfSessionIsThinking(string $customId): void {
try {
$tasks = $this->taskProcessingManager->getUserTasksByApp($this->userId, Application::APP_ID . ':chatty-llm', $customId);