Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable4.2] fix: Adjust TaskProcessingListener #10632

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/Listener/NewMessagesSummarizeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(
}

public function handle(Event $event): void {
if ($this->appConfig->getAppValue('llm_processing', 'no') !== 'yes') {
if ($this->appConfig->getAppValueBool('llm_processing', false) === false) {
return;
}
if (!($event instanceof NewMessagesSynchronized)) {
Expand Down
11 changes: 4 additions & 7 deletions lib/Service/AiIntegrations/AiIntegrationsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@
use OCA\Mail\Model\EventData;
use OCA\Mail\Model\IMAPMessage;
use OCP\IConfig;
use OCP\TaskProcessing\Exception\Exception as TaskProcessingException;
use OCP\TaskProcessing\IManager as TaskProcessingManager;
use OCP\TaskProcessing\Task as TaskProcessingTask;
use OCP\TaskProcessing\TaskTypes\TextToText;
use OCP\TaskProcessing\TaskTypes\TextToTextSummary;
use OCP\TextProcessing\FreePromptTaskType;
use OCP\TextProcessing\IManager;
use OCP\TextProcessing\SummaryTaskType;
Expand Down Expand Up @@ -66,17 +65,15 @@ public function __construct(
* @return void
*/
public function summarizeMessages(Account $account, array $messages): void {
try {
$this->taskProcessingManager->getPreferredProvider(TextToText::ID);
} catch (TaskProcessingException $e) {
$availableTaskTypes = $this->taskProcessingManager->getAvailableTaskTypes();
if (!isset($availableTaskTypes[TextToTextSummary::ID])) {
$this->logger->info('No text summary provider available');
return;
}

$client = $this->clientFactory->getClient($account);
try {
foreach ($messages as $entry) {

if (mb_strlen((string)$entry->getSummary()) !== 0) {
continue;
}
Expand Down Expand Up @@ -104,7 +101,7 @@ public function summarizeMessages(Account $account, array $messages): void {
"Here is the ***E-MAIL*** for which you must generate a helpful summary: \r\n" .
"***START_OF_E-MAIL***\r\n$messageBody\r\n***END_OF_E-MAIL***\r\n";
$task = new TaskProcessingTask(
TextToText::ID,
TextToTextSummary::ID,
[
'max_tokens' => 1024,
'input' => $prompt,
Expand Down
12 changes: 6 additions & 6 deletions tests/Unit/Listener/NewMessagesSummarizeListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ public function testLlmEnabled(): void {
->method('getMessages')
->willReturn([]);
$this->appConfig->expects($this->once())
->method('getAppValue')
->with('llm_processing', 'no')
->willReturn('yes');
->method('getAppValueBool')
->with('llm_processing', false)
->willReturn(true);
$this->aiService->expects($this->once())
->method('summarizeMessages')
->with($account, []);
Expand All @@ -69,9 +69,9 @@ public function testLlmEnabled(): void {
public function testLlmDisabled(): void {
$event = $this->createMock(NewMessagesSynchronized::class);
$this->appConfig->expects($this->once())
->method('getAppValue')
->with('llm_processing', 'no')
->willReturn('no');
->method('getAppValueBool')
->with('llm_processing', false)
->willReturn(false);
$this->aiService->expects($this->never())
->method('summarizeMessages');
$this->listener->handle($event);
Expand Down
17 changes: 8 additions & 9 deletions tests/Unit/Service/AiIntegrationsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use OCA\Mail\Service\AiIntegrations\Cache;
use OCP\AppFramework\QueryException;
use OCP\IConfig;
use OCP\TaskProcessing\Exception\Exception as TaskProcessingException;
use OCP\TaskProcessing\IManager as TaskProcessingManager;
use OCP\TaskProcessing\IProvider as TaskProcessingProvider;
use OCP\TextProcessing\FreePromptTaskType;
Expand Down Expand Up @@ -379,8 +378,8 @@ public function testSummarizeMessagesNoProvider() {
$account = new Account(new MailAccount());
$message = new Message();
$this->taskProcessingManager->expects(self::once())
->method('getPreferredProvider')
->willThrowException(new TaskProcessingException());
->method('getAvailableTaskTypes')
->willReturn([]);
$this->logger->expects(self::once())
->method('info')
->with('No text summary provider available');
Expand All @@ -402,8 +401,8 @@ public function testSummarizeMessagesContainsSummary() {
$message->setSummary('Test Summary');

$this->taskProcessingManager->expects(self::once())
->method('getPreferredProvider')
->willReturn($this->taskProcessingProvider);
->method('getAvailableTaskTypes')
->willReturn(['core:text2text:summary' => $this->taskProcessingProvider]);
$this->clientFactory->expects(self::once())
->method('getClient');

Expand Down Expand Up @@ -440,8 +439,8 @@ public function testSummarizeMessagesIsEncrypted() {
->method('isEncrypted')->willReturn(true);

$this->taskProcessingManager->expects(self::once())
->method('getPreferredProvider')
->willReturn($this->taskProcessingProvider);
->method('getAvailableTaskTypes')
->willReturn(['core:text2text:summary' => $this->taskProcessingProvider]);
$this->taskProcessingManager->expects(self::never())
->method('scheduleTask');

Expand Down Expand Up @@ -499,8 +498,8 @@ public function testSummarizeMessages() {
->method('isEncrypted')->willReturn(false);

$this->taskProcessingManager->expects(self::once())
->method('getPreferredProvider')
->willReturn($this->taskProcessingProvider);
->method('getAvailableTaskTypes')
->willReturn(['core:text2text:summary' => $this->taskProcessingProvider]);
$this->taskProcessingManager->expects(self::once())
->method('scheduleTask');

Expand Down