2424use OCP \TaskProcessing \Task as TaskProcessingTask ;
2525use OCP \TaskProcessing \TaskTypes \TextToText ;
2626use OCP \TextProcessing \FreePromptTaskType ;
27- use OCP \TextProcessing \IManager ;
27+ use OCP \TextProcessing \IManager as TextProcessingManager ;
2828use OCP \TextProcessing \SummaryTaskType ;
29- use OCP \TextProcessing \Task ;
30- use Psr \Container \ContainerExceptionInterface ;
31- use Psr \Container \ContainerInterface ;
29+ use OCP \TextProcessing \Task as TextProcessingTask ;
3230use Psr \Log \LoggerInterface ;
3331
3432use function array_map ;
@@ -46,13 +44,13 @@ class AiIntegrationsService {
4644PROMPT ;
4745
4846 public function __construct (
49- private ContainerInterface $ container ,
5047 private LoggerInterface $ logger ,
5148 private IConfig $ config ,
5249 private Cache $ cache ,
5350 private IMAPClientFactory $ clientFactory ,
5451 private IMailManager $ mailManager ,
5552 private TaskProcessingManager $ taskProcessingManager ,
53+ private TextProcessingManager $ textProcessingManager ,
5654 ) {
5755 }
5856
@@ -129,12 +127,7 @@ public function summarizeMessages(Account $account, array $messages): void {
129127 * @throws ServiceException
130128 */
131129 public function summarizeThread (Account $ account , string $ threadId , array $ messages , string $ currentUserId ): ?string {
132- try {
133- $ manager = $ this ->container ->get (IManager::class);
134- } catch (\Throwable $ e ) {
135- throw new ServiceException ('Text processing is not available in your current Nextcloud version ' , 0 , $ e );
136- }
137- if (in_array (SummaryTaskType::class, $ manager ->getAvailableTaskTypes (), true )) {
130+ if (in_array (SummaryTaskType::class, $ this ->textProcessingManager ->getAvailableTaskTypes (), true )) {
138131 $ messageIds = array_map (function ($ message ) {
139132 return $ message ->getMessageId ();
140133 }, $ messages );
@@ -160,8 +153,8 @@ public function summarizeThread(Account $account, string $threadId, array $messa
160153 }
161154
162155 $ taskPrompt = implode ("\n" , $ messagesBodies );
163- $ summaryTask = new Task (SummaryTaskType::class, $ taskPrompt , 'mail ' , $ currentUserId , $ threadId );
164- $ manager ->runTask ($ summaryTask );
156+ $ summaryTask = new TextProcessingTask (SummaryTaskType::class, $ taskPrompt , 'mail ' , $ currentUserId , $ threadId );
157+ $ this -> textProcessingManager ->runTask ($ summaryTask );
165158 $ summary = $ summaryTask ->getOutput ();
166159
167160 $ this ->cache ->addValue ($ this ->cache ->buildUrlKey ($ messageIds ), $ summary );
@@ -176,13 +169,7 @@ public function summarizeThread(Account $account, string $threadId, array $messa
176169 * @param Message[] $messages
177170 */
178171 public function generateEventData (Account $ account , string $ threadId , array $ messages , string $ currentUserId ): ?EventData {
179- try {
180- /** @var IManager $manager */
181- $ manager = $ this ->container ->get (IManager::class);
182- } catch (ContainerExceptionInterface $ e ) {
183- return null ;
184- }
185- if (!in_array (FreePromptTaskType::class, $ manager ->getAvailableTaskTypes (), true )) {
172+ if (!in_array (FreePromptTaskType::class, $ this ->textProcessingManager ->getAvailableTaskTypes (), true )) {
186173 return null ;
187174 }
188175 $ client = $ this ->clientFactory ->getClient ($ account );
@@ -201,14 +188,14 @@ public function generateEventData(Account $account, string $threadId, array $mes
201188 $ client ->logout ();
202189 }
203190
204- $ task = new Task (
191+ $ task = new TextProcessingTask (
205192 FreePromptTaskType::class,
206193 self ::EVENT_DATA_PROMPT_PREAMBLE . implode ("\n\n--- \n\n" , $ messageBodies ),
207194 'mail ' ,
208195 $ currentUserId ,
209196 "event_data_ $ threadId " ,
210197 );
211- $ result = $ manager ->runTask ($ task );
198+ $ result = $ this -> textProcessingManager ->runTask ($ task );
212199 try {
213200 $ decoded = json_decode ($ result , true , 512 , JSON_THROW_ON_ERROR );
214201 return new EventData ($ decoded ['title ' ], $ decoded ['agenda ' ]);
@@ -222,12 +209,7 @@ public function generateEventData(Account $account, string $threadId, array $mes
222209 * @throws ServiceException
223210 */
224211 public function getSmartReply (Account $ account , Mailbox $ mailbox , Message $ message , string $ currentUserId ): ?array {
225- try {
226- $ manager = $ this ->container ->get (IManager::class);
227- } catch (\Throwable $ e ) {
228- throw new ServiceException ('Text processing is not available in your current Nextcloud version ' , 0 , $ e );
229- }
230- if (in_array (FreePromptTaskType::class, $ manager ->getAvailableTaskTypes (), true )) {
212+ if (in_array (FreePromptTaskType::class, $ this ->textProcessingManager ->getAvailableTaskTypes (), true )) {
231213 $ cachedReplies = $ this ->cache ->getValue ('smartReplies_ ' . $ message ->getId ());
232214 if ($ cachedReplies ) {
233215 return json_decode ($ cachedReplies , true , 512 );
@@ -262,8 +244,8 @@ public function getSmartReply(Account $account, Mailbox $mailbox, Message $messa
262244
263245 Please, output *ONLY* a valid JSON string with the keys 'reply1' and 'reply2' for the reply suggestions. Leave out any other text besides the JSON! Be extremely succinct and write the replies from my point of view.
264246 " ;
265- $ task = new Task (FreePromptTaskType::class, $ prompt , 'mail, ' , $ currentUserId );
266- $ manager ->runTask ($ task );
247+ $ task = new TextProcessingTask (FreePromptTaskType::class, $ prompt , 'mail, ' , $ currentUserId );
248+ $ this -> textProcessingManager ->runTask ($ task );
267249 $ replies = $ task ->getOutput ();
268250 try {
269251 $ cleaned = preg_replace ('/^```json\s*|\s*```$/ ' , '' , trim ($ replies ));
@@ -289,17 +271,7 @@ public function requiresFollowUp(
289271 Message $ message ,
290272 string $ currentUserId ,
291273 ): bool {
292- try {
293- $ manager = $ this ->container ->get (IManager::class);
294- } catch (ContainerExceptionInterface $ e ) {
295- throw new ServiceException (
296- 'Text processing is not available in your current Nextcloud version ' ,
297- 0 ,
298- $ e ,
299- );
300- }
301-
302- if (!in_array (FreePromptTaskType::class, $ manager ->getAvailableTaskTypes (), true )) {
274+ if (!in_array (FreePromptTaskType::class, $ this ->textProcessingManager ->getAvailableTaskTypes (), true )) {
303275 throw new ServiceException ('No language model available for smart replies ' );
304276 }
305277
@@ -338,21 +310,16 @@ public function requiresFollowUp(
338310emailText: \"$ messageBody \"
339311The JSON output should be in the form: { \"expectsReply \": true}
340312Never return null or undefined. " ;
341- $ task = new Task (FreePromptTaskType::class, $ prompt , Application::APP_ID , $ currentUserId );
313+ $ task = new TextProcessingTask (FreePromptTaskType::class, $ prompt , Application::APP_ID , $ currentUserId );
342314
343- $ manager ->runTask ($ task );
315+ $ this -> textProcessingManager ->runTask ($ task );
344316
345317 // Can't use json_decode() here because the output contains additional garbage
346318 return preg_match ('/{\s*"expectsReply"\s*:\s*true\s*}/i ' , $ task ->getOutput ()) === 1 ;
347319 }
348320
349321 public function isLlmAvailable (string $ taskType ): bool {
350- try {
351- $ manager = $ this ->container ->get (IManager::class);
352- } catch (\Throwable $ e ) {
353- return false ;
354- }
355- return in_array ($ taskType , $ manager ->getAvailableTaskTypes (), true );
322+ return in_array ($ taskType , $ this ->textProcessingManager ->getAvailableTaskTypes (), true );
356323 }
357324
358325 public function isTaskAvailable (string $ taskName ): bool {
0 commit comments