Skip to content

Commit a85993e

Browse files
committed
Switch away from extend and to dependency injection
Signed-off-by: Lukas Schaefer <lukas@lschaefer.xyz>
1 parent c37f479 commit a85993e

4 files changed

Lines changed: 61 additions & 24 deletions

File tree

lib/TaskProcessing/AudioToTextEnhancedProvider.php

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,81 @@
1111

1212
use OCA\OpenAi\AppInfo\Application;
1313
use OCA\OpenAi\Service\OpenAiAPIService;
14-
use OCP\IAppConfig;
15-
use OCP\IL10N;
1614
use OCP\TaskProcessing\IManager;
15+
use OCP\TaskProcessing\ISynchronousProvider;
1716
use OCP\TaskProcessing\Task;
17+
use OCP\TaskProcessing\TaskTypes\AudioToText;
1818
use Psr\Log\LoggerInterface;
1919
use Throwable;
2020

21-
// Built on top of the AudioToTextProvider to add paragraph reformatting.
22-
class AudioToTextEnhancedProvider extends AudioToTextProvider {
23-
private const REFORMAT_PARAGRAPHS_TASK_TYPE_ID = 'core:text2text:reformatparagraphs';
24-
25-
private IManager $taskProcessingManager;
21+
class AudioToTextEnhancedProvider implements ISynchronousProvider {
2622

2723
public function __construct(
28-
OpenAiAPIService $openAiAPIService,
29-
LoggerInterface $logger,
30-
IAppConfig $appConfig,
31-
IL10N $l,
32-
IManager $taskProcessingManager,
24+
private AudioToTextProvider $audioToTextProvider,
25+
private OpenAiAPIService $openAiAPIService,
26+
private IManager $taskProcessingManager,
27+
private LoggerInterface $logger,
3328
) {
34-
parent::__construct($openAiAPIService, $logger, $appConfig, $l);
35-
$this->taskProcessingManager = $taskProcessingManager;
3629
}
3730

3831
public function getId(): string {
39-
return parent::getId() . '-enhanced';
32+
return $this->audioToTextProvider->getId() . '-enhanced';
4033
}
4134

4235
public function getName(): string {
43-
return parent::getName() . ' ' . $this->l->t('(with paragraph reformatting)');
36+
return $this->audioToTextProvider->getName() . ' (with paragraph reformatting)';
37+
}
38+
39+
public function getTaskTypeId(): string {
40+
return AudioToText::ID;
41+
}
42+
43+
public function getExpectedRuntime(): int {
44+
return $this->audioToTextProvider->getExpectedRuntime();
45+
}
46+
47+
public function getInputShapeEnumValues(): array {
48+
return $this->audioToTextProvider->getInputShapeEnumValues();
49+
}
50+
51+
public function getInputShapeDefaults(): array {
52+
return $this->audioToTextProvider->getInputShapeDefaults();
53+
}
54+
55+
public function getOptionalInputShape(): array {
56+
return $this->audioToTextProvider->getOptionalInputShape();
57+
}
58+
59+
public function getOptionalInputShapeEnumValues(): array {
60+
return $this->audioToTextProvider->getOptionalInputShapeEnumValues();
61+
}
62+
63+
public function getOptionalInputShapeDefaults(): array {
64+
return $this->audioToTextProvider->getOptionalInputShapeDefaults();
65+
}
66+
67+
public function getOutputShapeEnumValues(): array {
68+
return $this->audioToTextProvider->getOutputShapeEnumValues();
69+
}
70+
71+
public function getOptionalOutputShape(): array {
72+
return $this->audioToTextProvider->getOptionalOutputShape();
73+
}
74+
75+
public function getOptionalOutputShapeEnumValues(): array {
76+
return $this->audioToTextProvider->getOptionalOutputShapeEnumValues();
4477
}
4578

4679
public function process(?string $userId, array $input, callable $reportProgress): array {
47-
$transcription = parent::process($userId, $input, $reportProgress)['output'];
80+
$transcription = $this->audioToTextProvider->process($userId, $input, $reportProgress)['output'];
81+
82+
// Skip reformatting if the transcription is empty
83+
if (trim($transcription) === '') {
84+
return ['output' => $transcription];
85+
}
4886

4987
$reformatTask = new Task(
50-
self::REFORMAT_PARAGRAPHS_TASK_TYPE_ID,
88+
\OCP\TaskProcessing\TaskTypes\TextToTextReformatParagraphs::ID,
5189
['input' => $transcription],
5290
Application::APP_ID,
5391
$userId,

lib/TaskProcessing/AudioToTextProvider.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
class AudioToTextProvider implements ISynchronousProvider {
2727

2828
public function __construct(
29-
protected OpenAiAPIService $openAiAPIService,
30-
protected LoggerInterface $logger,
31-
protected IAppConfig $appConfig,
32-
protected IL10N $l,
29+
private OpenAiAPIService $openAiAPIService,
30+
private LoggerInterface $logger,
31+
private IAppConfig $appConfig,
32+
private IL10N $l,
3333
) {
3434
}
3535

lib/TaskProcessing/ReformatParagraphsProvider.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use OCP\TaskProcessing\ISynchronousProvider;
2222
use OCP\TaskProcessing\ShapeDescriptor;
2323
use RuntimeException;
24-
use InvalidArgumentException;
2524

2625
class ReformatParagraphsProvider implements ISynchronousProvider {
2726
private const TASK_TYPE_ID = 'core:text2text:reformatparagraphs';

tests/unit/Providers/OpenAiProviderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ public function testReformatParagraphsProvider(): void {
738738
. 'Thematic breaks only: Do not create a new paragraph for rhythm, style, or sentence flow. '
739739
. 'A break is allowed only when the subject matter changes significantly. '
740740
. 'Output format: For each identified paragraph, return only the first 8 to 12 words verbatim from the input. '
741-
. 'Structure: Return exactly one anchor per line. Do not include bullets, numbering, summaries, quotes, or any additional text. '
741+
. 'Structure: Return exactly one paragraph per line. Do not include bullets, html tags, numbering, summaries, quotes, or any additional text. '
742742
. 'Single topic: If the text covers only one topic, return exactly one line.';
743743

744744
$options = ['timeout' => Application::OPENAI_DEFAULT_REQUEST_TIMEOUT, 'headers' => ['User-Agent' => Application::USER_AGENT, 'Authorization' => self::AUTHORIZATION_HEADER, 'Content-Type' => 'application/json']];

0 commit comments

Comments
 (0)