Skip to content

Commit 1902b7f

Browse files
committed
Add more validation and handle null userid
Signed-off-by: Lukas Schaefer <lukas@lschaefer.xyz>
1 parent 1808448 commit 1902b7f

3 files changed

Lines changed: 22 additions & 5 deletions

File tree

‎lib/Service/OpenAiAPIService.php‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,23 @@ private function buildChatCompletionRequestParams(
733733
if (isset($message['content']) && is_array($message['content'])) {
734734
$content = [];
735735
foreach ($message['content'] as $item) {
736+
if (!isset($item['type'])) {
737+
throw new UserFacingProcessingException(
738+
'Invalid message history content',
739+
0,
740+
null,
741+
$this->l10n->t('Invalid message history content'),
742+
);
743+
}
736744
if ($item['type'] === 'file') {
745+
if (!isset($item['file_id'])) {
746+
throw new UserFacingProcessingException(
747+
'Invalid message history content',
748+
0,
749+
null,
750+
$this->l10n->t('Invalid message history content'),
751+
);
752+
}
737753
$content = array_merge($content, $this->openAiFileService->buildFileContentFromId($item['file_id'], $userId, $item['ocp_task_id'] ?? null));
738754
} else {
739755
$content[] = $item;

‎lib/Service/OpenAiFileService.php‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ public function __construct(
8282
* Builds file content from a file ID within a given user folder.
8383
*
8484
* @param int $fileId The ID of the file to build content from.
85-
* @param string $userId The user ID.
85+
* @param ?string $userId The user ID.
8686
* @param ?int $taskId The ID of the task
8787
* @return list<array<string, mixed>> Content parts suitable for OpenAI chat message content.
8888
* @throws ProcessingException
8989
* @throws UserFacingProcessingException
9090
*/
91-
public function buildFileContentFromId(int $fileId, string $userId, ?int $taskId): array {
91+
public function buildFileContentFromId(int $fileId, ?string $userId, ?int $taskId): array {
9292
$file = null;
9393
if ($taskId !== null) {
9494
$task = $this->taskProcessingManager->getUserTask($taskId, $userId);
@@ -102,7 +102,8 @@ public function buildFileContentFromId(int $fileId, string $userId, ?int $taskId
102102
if ($file === null) {
103103
$file = $this->rootFolder->getFirstNodeByIdInPath($fileId, '/' . $this->rootFolder->getAppDataDirectoryName() . '/');
104104
}
105-
} else {
105+
// If the userId is not specified they don't have a user folder
106+
} elseif ($userId !== null) {
106107
$userFolder = $this->rootFolder->getUserFolder($userId);
107108
$file = $userFolder->getFirstNodeById($fileId);
108109
}

‎lib/TaskProcessing/MultimodalChatWithToolsProvider.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,12 @@ public function process(
214214
foreach ($returnValue['images'] as $image) {
215215
if ($image['type'] === 'image_url') {
216216
$url = $image['image_url']['url'];
217-
$base64Str = explode(',', $url)[1] ?? '';
217+
$base64Str = explode(',', $url)[1] ?? throw new ProcessingException('Invalid image URL in multimodal chat: ' . $url);
218218
$image = base64_decode($base64Str);
219219
$image = $this->watermarkingService->markImage($image);
220220
$attachments[] = $image;
221221
} else {
222-
$this->logger->warning('Encountered an unknown image type in multimodal chat: ' . $image['type']);
222+
throw new ProcessingException('Unknown image type in multimodal chat: ' . $image['type']);
223223
}
224224
}
225225

0 commit comments

Comments
 (0)