Skip to content

Commit 5035121

Browse files
committed
fix(TextToImageProvider): Use userFacingErrorMessages
Signed-off-by: Marcel Klehr <[email protected]>
1 parent 0da31b2 commit 5035121

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

lib/TaskProcessing/TextToImageProvider.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
use OCP\IAppConfig;
1717
use OCP\IL10N;
1818
use OCP\TaskProcessing\EShapeType;
19+
use OCP\TaskProcessing\Exception\ProcessingException;
20+
use OCP\TaskProcessing\Exception\UserFacingProcessingException;
1921
use OCP\TaskProcessing\ISynchronousWatermarkingProvider;
2022
use OCP\TaskProcessing\ShapeDescriptor;
2123
use OCP\TaskProcessing\TaskTypes\TextToImage;
2224
use Psr\Log\LoggerInterface;
23-
use RuntimeException;
2425

2526
class TextToImageProvider implements ISynchronousWatermarkingProvider {
2627

@@ -110,19 +111,28 @@ public function process(?string $userId, array $input, callable $reportProgress,
110111
$startTime = time();
111112

112113
if (!isset($input['input']) || !is_string($input['input'])) {
113-
throw new RuntimeException('Invalid prompt');
114+
throw new ProcessingException('Invalid prompt');
114115
}
115116
$prompt = $input['input'];
116117

117118
$nbImages = 1;
118119
if (isset($input['numberOfImages']) && is_int($input['numberOfImages'])) {
119120
$nbImages = $input['numberOfImages'];
120121
}
122+
if ($nbImages > 12) {
123+
throw new UserFacingProcessingException('numberOfImages is out of bounds', userFacingMessage: $this->l->t('Cannot generate more than 12 images at once'));
124+
}
125+
if ($nbImages < 1) {
126+
throw new UserFacingProcessingException('numberOfImages is out of bounds', userFacingMessage: $this->l->t('Cannot generate less than 1 image'));
127+
}
121128

122129
$size = $this->appConfig->getValueString(Application::APP_ID, 'default_image_size', lazy: true) ?: Application::DEFAULT_DEFAULT_IMAGE_SIZE;
123130
if (isset($input['size']) && is_string($input['size']) && preg_match('/^\d+x\d+$/', $input['size'])) {
124131
$size = trim($input['size']);
125132
}
133+
if (array_sum(explode($size, 'x')) > 4096 + 4096) {
134+
throw new UserFacingProcessingException('size is out of bounds', userFacingMessage: $this->l->t('Cannot generate images larger than 4096x4096'));
135+
}
126136

127137
if (isset($input['model']) && is_string($input['model'])) {
128138
$model = $input['model'];
@@ -150,7 +160,7 @@ public function process(?string $userId, array $input, callable $reportProgress,
150160

151161
if (empty($urls) && empty($b64s)) {
152162
$this->logger->warning('OpenAI/LocalAI\'s text to image generation failed: no image returned');
153-
throw new RuntimeException('OpenAI/LocalAI\'s text to image generation failed: no image returned');
163+
throw new ProcessingException('OpenAI/LocalAI\'s text to image generation failed: no image returned');
154164
}
155165
$client = $this->clientService->newClient();
156166
$requestOptions = $this->openAiAPIService->getImageRequestOptions($userId);
@@ -172,7 +182,7 @@ public function process(?string $userId, array $input, callable $reportProgress,
172182
return $output;
173183
} catch (\Exception $e) {
174184
$this->logger->warning('OpenAI/LocalAI\'s text to image generation failed with: ' . $e->getMessage(), ['exception' => $e]);
175-
throw new RuntimeException('OpenAI/LocalAI\'s text to image generation failed with: ' . $e->getMessage());
185+
throw new ProcessingException('OpenAI/LocalAI\'s text to image generation failed with: ' . $e->getMessage());
176186
}
177187
}
178188
}

0 commit comments

Comments
 (0)