1313use OCA \Assistant \Db \ChattyLLM \Session ;
1414use OCA \Assistant \Db \ChattyLLM \SessionMapper ;
1515use OCA \Assistant \ResponseDefinitions ;
16+ use OCA \Assistant \Service \SessionSummaryService ;
1617use OCP \AppFramework \Db \DoesNotExistException ;
1718use OCP \AppFramework \Db \MultipleObjectsReturnedException ;
1819use OCP \AppFramework \Http ;
@@ -55,6 +56,7 @@ public function __construct(
5556 private IAppConfig $ appConfig ,
5657 private IUserManager $ userManager ,
5758 private ?string $ userId ,
59+ private SessionSummaryService $ sessionSummaryService ,
5860 ) {
5961 parent ::__construct ($ appName , $ request );
6062 $ this ->agencyActionData = [
@@ -190,6 +192,49 @@ public function updateSessionTitle(int $sessionId, string $title): JSONResponse
190192 }
191193 }
192194
195+ /**
196+ * Update session
197+ *
198+ * @param integer $sessionId The chat session ID
199+ * @param string|null $title The new chat session title
200+ * @param bool|null $is_remembered The new is_remembered status: Whether to remember the insights from this chat session across all chat session
201+ * @return JSONResponse<Http::STATUS_OK, list{}, array{}>|JSONResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_NOT_FOUND, array{error: string}, array{}>
202+ *
203+ * 200: The title has been updated successfully
204+ * 404: The session was not found
205+ */
206+ #[NoAdminRequired]
207+ #[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT , tags: ['chat_api ' ])]
208+ public function updateChatSession (int $ sessionId , ?string $ title = null , ?bool $ is_remembered = null ): JSONResponse {
209+ if ($ this ->userId === null ) {
210+ return new JSONResponse (['error ' => $ this ->l10n ->t ('Could not find session ' )], Http::STATUS_NOT_FOUND );
211+ }
212+ if ($ title === null && $ is_remembered === null ) {
213+ return new JSONResponse ();
214+ }
215+
216+ try {
217+ $ session = $ this ->sessionMapper ->getUserSession ($ this ->userId , $ sessionId );
218+ if ($ title !== null ) {
219+ $ session ->setTitle ($ title );
220+ }
221+ if ($ is_remembered !== null ) {
222+ $ session ->setIsRemembered ($ is_remembered );
223+ // schedule summarizer jobs for this chat user
224+ if ($ is_remembered ) {
225+ $ this ->sessionSummaryService ->scheduleJobsForUser ($ this ->userId );
226+ }
227+ }
228+ $ this ->sessionMapper ->update ($ session );
229+ return new JSONResponse ();
230+ } catch (\OCP \DB \Exception |\RuntimeException $ e ) {
231+ $ this ->logger ->warning ('Failed to update the chat session ' , ['exception ' => $ e ]);
232+ return new JSONResponse (['error ' => $ this ->l10n ->t ('Failed to update the chat session ' )], Http::STATUS_INTERNAL_SERVER_ERROR );
233+ } catch (DoesNotExistException |MultipleObjectsReturnedException $ e ) {
234+ return new JSONResponse (['error ' => $ this ->l10n ->t ('Could not find session ' )], Http::STATUS_NOT_FOUND );
235+ }
236+ }
237+
193238 /**
194239 * Delete a chat session
195240 *
@@ -779,6 +824,7 @@ public function checkSession(int $sessionId): JSONResponse {
779824 'messageTaskId ' => null ,
780825 'titleTaskId ' => null ,
781826 'sessionTitle ' => $ session ->getTitle (),
827+ 'is_remembered ' => $ session ->getIsRemembered (),
782828 'sessionAgencyPendingActions ' => $ p ,
783829 ];
784830 if (!empty ($ messageTasks )) {
@@ -990,6 +1036,9 @@ private function scheduleLLMChatTask(
9901036 'system_prompt ' => $ systemPrompt ,
9911037 'history ' => $ history ,
9921038 ];
1039+ if (isset ($ this ->taskProcessingManager ->getAvailableTaskTypes ()[TextToTextChat::ID ]['optionalInputShape ' ]['memories ' ])) {
1040+ $ input ['memories ' ] = $ this ->sessionSummaryService ->getUserSessionSummaries ($ this ->userId );
1041+ }
9931042 $ task = new Task (TextToTextChat::ID , $ input , Application::APP_ID . ':chatty-llm ' , $ this ->userId , $ customId );
9941043 $ this ->taskProcessingManager ->scheduleTask ($ task );
9951044 return $ task ->getId () ?? 0 ;
@@ -1017,6 +1066,10 @@ private function scheduleAgencyTask(string $content, int $confirmation, string $
10171066 'conversation_token ' => $ conversationToken ,
10181067 ];
10191068 /** @psalm-suppress UndefinedClass */
1069+ if (isset ($ this ->taskProcessingManager ->getAvailableTaskTypes ()[\OCP \TaskProcessing \TaskTypes \ContextAgentInteraction::ID ]['optionalInputShape ' ]['memories ' ])) {
1070+ $ taskInput ['memories ' ] = $ this ->sessionSummaryService ->getUserSessionSummaries ($ this ->userId );
1071+ }
1072+ /** @psalm-suppress UndefinedClass */
10201073 $ task = new Task (
10211074 \OCP \TaskProcessing \TaskTypes \ContextAgentInteraction::ID ,
10221075 $ taskInput ,
@@ -1039,6 +1092,10 @@ private function scheduleAudioChatTask(
10391092 'history ' => $ history ,
10401093 ];
10411094 /** @psalm-suppress UndefinedClass */
1095+ if (isset ($ this ->taskProcessingManager ->getAvailableTaskTypes ()[\OCP \TaskProcessing \TaskTypes \AudioToAudioChat::ID ]['optionalInputShape ' ]['memories ' ])) {
1096+ $ input ['memories ' ] = $ this ->sessionSummaryService ->getUserSessionSummaries ($ this ->userId );
1097+ }
1098+ /** @psalm-suppress UndefinedClass */
10421099 $ task = new Task (
10431100 \OCP \TaskProcessing \TaskTypes \AudioToAudioChat::ID ,
10441101 $ input ,
@@ -1061,6 +1118,10 @@ private function scheduleAgencyAudioTask(
10611118 'conversation_token ' => $ conversationToken ,
10621119 ];
10631120 /** @psalm-suppress UndefinedClass */
1121+ if (isset ($ this ->taskProcessingManager ->getAvailableTaskTypes ()[\OCP \TaskProcessing \TaskTypes \ContextAgentAudioInteraction::ID ]['optionalInputShape ' ]['memories ' ])) {
1122+ $ taskInput ['memories ' ] = $ this ->sessionSummaryService ->getUserSessionSummaries ($ this ->userId );
1123+ }
1124+ /** @psalm-suppress UndefinedClass */
10641125 $ task = new Task (
10651126 \OCP \TaskProcessing \TaskTypes \ContextAgentAudioInteraction::ID ,
10661127 $ taskInput ,
0 commit comments