-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathNotifier.php
More file actions
422 lines (344 loc) · 14.3 KB
/
Copy pathNotifier.php
File metadata and controls
422 lines (344 loc) · 14.3 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
<?php
/**
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Assistant\Notification;
use OCA\Assistant\AppInfo\Application;
use OCP\App\IAppManager;
use OCP\IURLGenerator;
use OCP\L10N\IFactory;
use OCP\Notification\IAction;
use OCP\Notification\INotification;
use OCP\Notification\INotifier;
use OCP\Notification\UnknownNotificationException;
use OCP\TaskProcessing\IManager as ITaskProcessingManager;
use OCP\TaskProcessing\TaskTypes\AudioToText;
use OCP\TaskProcessing\TaskTypes\TextToImage;
use OCP\TaskProcessing\TaskTypes\TextToText;
use OCP\TaskProcessing\TaskTypes\TextToTextSummary;
use Psr\Log\LoggerInterface;
class Notifier implements INotifier {
public function __construct(
private IFactory $factory,
private IURLGenerator $url,
private IAppManager $appManager,
private ITaskProcessingManager $taskProcessingManager,
private LoggerInterface $logger,
) {
}
/**
* Identifier of the notifier, only use [a-z0-9_]
*
* @return string
* @since 17.0.0
*/
public function getID(): string {
return Application::APP_ID;
}
/**
* Human readable name describing the notifier
*
* @return string
* @since 17.0.0
*/
public function getName(): string {
return $this->factory->get(Application::APP_ID)->t('Nextcloud Assistant');
}
/**
* @param INotification $notification
* @param string $languageCode The code of the language that should be used to prepare the notification
* @return INotification
* @throws UnknownNotificationException When the notification was not prepared by a notifier
* @since 9.0.0
*/
public function prepare(INotification $notification, string $languageCode): INotification {
if ($notification->getApp() !== Application::APP_ID) {
// Not my app => throw
throw new UnknownNotificationException();
}
$l = $this->factory->get(Application::APP_ID, $languageCode);
$params = $notification->getSubjectParameters();
if (in_array($notification->getSubject(), ['success', 'failure'], true)) {
// ignore old notifications (before meta tasks were introduced)
if (!isset($params['target'], $params['inputs'])) {
throw new UnknownNotificationException();
}
$schedulingAppId = $params['appId'];
$schedulingAppInfo = $this->appManager->getAppInfo($schedulingAppId);
if ($schedulingAppInfo === null) {
throw new UnknownNotificationException();
}
$schedulingAppName = $schedulingAppInfo['name'];
$taskTypeName = null;
$taskInput = $params['inputs']['input'] ?? null;
try {
if (!isset($params['taskTypeId'])) {
$taskTypeName = $l->t('Assistant task');
} elseif ($params['taskTypeId'] === TextToText::ID) {
$taskTypeName = $l->t('AI text generation');
} elseif ($params['taskTypeId'] === TextToImage::ID) {
$taskTypeName = $l->t('AI image generation');
} elseif ($params['taskTypeId'] === AudioToText::ID) {
$taskTypeName = $l->t('AI audio transcription');
} elseif ($params['taskTypeId'] === 'copywriter') {
// TODO adjust that when we have copywriter back on its feet
// Catch the custom copywriter task type built on top of the FreePrompt task type.
$taskTypeName = $l->t('AI context writer');
$taskInput = $l->t('Writing style: %1$s; Source material: %2$s', [$params['inputs']['writingStyle'], $params['inputs']['sourceMaterial']]);
} elseif (
$params['taskTypeId'] === 'context_chat:context_chat'
|| $params['taskTypeId'] === 'legacy:TextProcessing:OCA\ContextChat\TextProcessing\ContextChatTaskType'
) {
$taskInput = $params['inputs']['prompt'] ?? null;
$taskTypeName = $l->t('Context Chat');
} else {
$availableTaskTypes = $this->taskProcessingManager->getAvailableTaskTypes();
if (isset($availableTaskTypes[$params['taskTypeId']])) {
$taskType = $availableTaskTypes[$params['taskTypeId']];
$taskTypeName = $taskType['name'];
}
}
} catch (\Exception|\Throwable $e) {
$this->logger->debug('Impossible to get task type ' . $params['taskTypeId'], ['exception' => $e]);
}
}
switch ($notification->getSubject()) {
case 'success':
$subject = $taskTypeName === null
? $l->t('Task for "%1$s" has finished', [$schedulingAppName])
: $l->t('"%1$s" task for "%2$s" has finished', [$taskTypeName, $schedulingAppName]);
$content = '';
if ($taskInput) {
$content .= $l->t('Input: %1$s', [$taskInput]);
}
if (isset($params['result'])) {
$content === '' ?: $content .= '\n';
$content .= $l->t('Result: %1$s', [$params['result']]);
}
$link = $params['target'];
$iconUrl = $this->url->getAbsoluteURL($this->url->imagePath(Application::APP_ID, 'app-dark.svg'));
$notification
->setParsedSubject($subject)
->setParsedMessage($content)
->setLink($link)
->setIcon($iconUrl);
$actionLabel = $params['actionLabel'] ?? $l->t('View results');
$action = $notification->createAction();
$action->setLabel($actionLabel)
->setParsedLabel($actionLabel)
->setLink($notification->getLink(), IAction::TYPE_WEB)
->setPrimary(true);
$notification->addParsedAction($action);
return $notification;
case 'failure':
$subject = $taskTypeName === null
? $l->t('Task for "%1$s" has failed', [$schedulingAppName])
: $l->t('"%1$s" task for "%2$s" has failed', [$taskTypeName, $schedulingAppName]);
$content = '';
if ($taskInput) {
$content .= $l->t('Input: %1$s', [$taskInput]);
}
$link = $params['target'] ?? $this->url->linkToRouteAbsolute(Application::APP_ID . '.assistant.getAssistantTaskResultPage', ['taskId' => $params['id']]);
$iconUrl = $this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/error.svg'));
$notification
->setParsedSubject($subject)
->setParsedMessage($content)
->setLink($link)
->setIcon($iconUrl);
$actionLabel = $params['actionLabel'] ?? $l->t('View task');
$action = $notification->createAction();
$action->setLabel($actionLabel)
->setParsedLabel($actionLabel)
->setLink($notification->getLink(), IAction::TYPE_WEB)
->setPrimary(true);
$notification->addParsedAction($action);
return $notification;
case 'file_action_success':
$subject = $l->t('File action has finished');
$sourceFileLink = $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $params['source_file_id']]);
$targetFileLink = $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $params['target_file_id']]);
$taskLink = $params['target'];
$iconUrl = $this->url->getAbsoluteURL($this->url->imagePath(Application::APP_ID, 'app-dark.svg'));
switch ($params['task_type_id']) {
case TextToTextSummary::ID:
$message = $l->t('{sourceFile} has been summarized in {targetFile}');
break;
case AudioToText::ID:
$message = $l->t('{sourceFile} has been transcribed in {targetFile}');
break;
case class_exists('OCP\\TaskProcessing\\TaskTypes\\TextToSpeech') ? \OCP\TaskProcessing\TaskTypes\TextToSpeech::ID : 'nope':
$message = $l->t('{sourceFile} has been converted to audio in {targetFile}');
break;
default:
$message = $l->t('{sourceFile} has been processed, {targetFile} was created');
}
$notification
->setParsedSubject($subject)
->setRichMessage($message, [
'sourceFile' => [
'type' => 'file',
'id' => (string)$params['source_file_id'],
'name' => $params['source_file_name'],
'path' => $params['source_file_path'],
'link' => $sourceFileLink,
],
'targetFile' => [
'type' => 'file',
'id' => (string)$params['target_file_id'],
'name' => $params['target_file_name'],
'path' => $params['target_file_path'],
'link' => $targetFileLink,
],
])
->setLink($taskLink)
->setIcon($iconUrl);
$actionLabel = $l->t('View results');
$action = $notification->createAction();
$action->setLabel($actionLabel)
->setParsedLabel($actionLabel)
->setLink($taskLink, IAction::TYPE_WEB)
->setPrimary(true);
$notification->addParsedAction($action);
return $notification;
case 'file_action_failure':
$subject = $l->t('File action has failed');
$iconUrl = $this->url->getAbsoluteURL($this->url->imagePath(Application::APP_ID, 'app-dark.svg'));
switch ($params['task_type_id']) {
case TextToTextSummary::ID:
$message = $l->t('Summarization of {sourceFile} has failed');
break;
case AudioToText::ID:
$message = $l->t('Transcription of {sourceFile} has failed');
break;
case class_exists('OCP\\TaskProcessing\\TaskTypes\\TextToSpeech') ? \OCP\TaskProcessing\TaskTypes\TextToSpeech::ID : 'nope':
$message = $l->t('The text-to-speech process for {sourceFile} has failed');
break;
default:
$message = $l->t('The processing of {sourceFile} has failed');
}
$notification
->setParsedSubject($subject)
->setRichMessage($message, [
'sourceFile' => [
'type' => 'file',
'id' => (string)$params['source_file_id'],
'name' => $params['source_file_name'],
'path' => $params['source_file_path'],
'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $params['source_file_id']]),
],
])
->setIcon($iconUrl);
return $notification;
case 'new_image_file_success':
$subject = $l->t('New image file has been generated');
$targetDirLink = $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $params['target_directory_id']]);
$targetFileLink = $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $params['target_file_id']]);
$taskLink = $params['target'];
$iconUrl = $this->url->getAbsoluteURL($this->url->imagePath(Application::APP_ID, 'app-dark.svg'));
$message = $l->t('{targetFile} has been generated in {targetDirectory}');
$notification
->setParsedSubject($subject)
->setRichMessage($message, [
'targetDirectory' => [
'type' => 'file',
'id' => (string)$params['target_directory_id'],
'name' => $params['target_directory_name'],
'path' => $params['target_directory_path'],
'link' => $targetDirLink,
],
'targetFile' => [
'type' => 'file',
'id' => (string)$params['target_file_id'],
'name' => $params['target_file_name'],
'path' => $params['target_file_path'],
'link' => $targetFileLink,
],
])
->setLink($taskLink)
->setIcon($iconUrl);
$actionLabel = $l->t('View results');
$action = $notification->createAction();
$action->setLabel($actionLabel)
->setParsedLabel($actionLabel)
->setLink($taskLink, IAction::TYPE_WEB)
->setPrimary(true);
$notification->addParsedAction($action);
return $notification;
case 'new_image_file_failure':
$subject = $l->t('Image file generation has failed');
$iconUrl = $this->url->getAbsoluteURL($this->url->imagePath(Application::APP_ID, 'app-dark.svg'));
$message = $l->t('Generation of a new image file in {targetDirectory} has failed');
$notification
->setParsedSubject($subject)
->setRichMessage($message, [
'targetDirectory' => [
'type' => 'file',
'id' => (string)$params['target_directory_id'],
'name' => $params['target_directory_name'],
'path' => $params['target_directory_path'],
'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $params['target_directory_id']]),
],
])
->setIcon($iconUrl);
return $notification;
case 'assignment_approval_pending':
$subject = $l->t('Scheduled task is pending review');
$iconUrl = $this->url->getAbsoluteURL($this->url->imagePath(Application::APP_ID, 'app-dark.svg'));
$message = $l->t('"%s" awaits your review before continuing.', [$params['sessionTitle']]);
$notification
->setParsedSubject($subject)
->setParsedMessage($message)
// TODO: link directly to assignment
->setLink($this->url->linkToRouteAbsolute(Application::APP_ID . '.assistant.getAssistantStandalonePage', ['sessionId' => $params['sessionId']]))
->setIcon($iconUrl);
$actionLabel = $l->t('View scheduled task');
$action = $notification->createAction();
$action->setLabel($actionLabel)
->setParsedLabel($actionLabel)
->setLink($notification->getLink(), IAction::TYPE_WEB)
->setPrimary(true);
$notification->addParsedAction($action);
return $notification;
case 'assignment_successful':
$subject = $l->t('Scheduled task succeeded');
$iconUrl = $this->url->getAbsoluteURL($this->url->imagePath(Application::APP_ID, 'app-dark.svg'));
$message = $l->t('"%s" was run successfully.', [$params['sessionTitle']]);
$notification
->setParsedSubject($subject)
->setParsedMessage($message)
// TODO: link directly to assignment
->setLink($this->url->linkToRouteAbsolute(Application::APP_ID . '.assistant.getAssistantStandalonePage', ['sessionId' => $params['sessionId']]))
->setIcon($iconUrl);
$actionLabel = $l->t('View result');
$action = $notification->createAction();
$action->setLabel($actionLabel)
->setParsedLabel($actionLabel)
->setLink($notification->getLink(), IAction::TYPE_WEB)
->setPrimary(true);
$notification->addParsedAction($action);
return $notification;
case 'assignment_failure':
$subject = $l->t('Scheduled task failed');
$iconUrl = $this->url->getAbsoluteURL($this->url->imagePath(Application::APP_ID, 'app-dark.svg'));
$message = $l->t('"%s" failed to run.', [$params['sessionTitle']]);
$notification
->setParsedSubject($subject)
->setParsedMessage($message)
// TODO: link directly to assignment
->setLink($this->url->linkToRouteAbsolute(Application::APP_ID . '.assistant.getAssistantStandalonePage', ['sessionId' => $params['sessionId']]))
->setIcon($iconUrl);
$actionLabel = $l->t('View result');
$action = $notification->createAction();
$action->setLabel($actionLabel)
->setParsedLabel($actionLabel)
->setLink($notification->getLink(), IAction::TYPE_WEB)
->setPrimary(true);
$notification->addParsedAction($action);
return $notification;
default:
// Unknown subject => Unknown notification => throw
throw new UnknownNotificationException();
}
}
}