Skip to content
Closed
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
1 change: 1 addition & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
['name' => 'chattyLLM#updateSessionTitle', 'url' => '/chat/update_session', 'verb' => 'PATCH'],
['name' => 'chattyLLM#deleteSession', 'url' => '/chat/delete_session', 'verb' => 'DELETE'],
['name' => 'chattyLLM#getSessions', 'url' => '/chat/sessions', 'verb' => 'GET'],
['name' => 'chattyLLM#searchChat', 'url' => '/chat/search', 'verb' => 'GET'],
['name' => 'chattyLLM#newMessage', 'url' => '/chat/new_message', 'verb' => 'PUT'],
['name' => 'chattyLLM#deleteMessage', 'url' => '/chat/delete_message', 'verb' => 'DELETE'],
['name' => 'chattyLLM#getMessages', 'url' => '/chat/messages', 'verb' => 'GET'],
Expand Down
33 changes: 33 additions & 0 deletions lib/Controller/ChattyLLMController.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,39 @@
}
}

/**
* Search chat messages by content
*
* Returns sessions that contain matching messages.
*
* @param string $query Search query (min 2 characters)
* @return JSONResponse<Http::STATUS_OK, array{sessions: list<AssistantChatSession>}, array{}>|JSONResponse<Http::STATUS_UNAUTHORIZED|Http::STATUS_INTERNAL_SERVER_ERROR, array{error: string}, array{}>
*

Check failure on line 321 in lib/Controller/ChattyLLMController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable33

MoreSpecificReturnType

lib/Controller/ChattyLLMController.php:321:13: MoreSpecificReturnType: The declared return type 'OCP\AppFramework\Http\JSONResponse<200|401|500, array{error?: string, sessions?: list<array{agency_conversation_token: null|string, agency_pending_actions: null|string, id: int, is_remembered: bool, is_summary_up_to_date: bool, summary: null|string, timestamp: int|null, title: null|string, user_id: string}>}, array<never, never>>' for OCA\Assistant\Controller\ChattyLLMController::searchChat is more specific than the inferred return type 'OCP\AppFramework\Http\JSONResponse<200|401|500, array{error?: string, sessions?: array<array-key, OCA\Assistant\Db\ChattyLLM\Session>}, array<never, never>>' (see https://psalm.dev/070)

Check failure on line 321 in lib/Controller/ChattyLLMController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

MoreSpecificReturnType

lib/Controller/ChattyLLMController.php:321:13: MoreSpecificReturnType: The declared return type 'OCP\AppFramework\Http\JSONResponse<200|401|500, array{error?: string, sessions?: list<array{agency_conversation_token: null|string, agency_pending_actions: null|string, id: int, is_remembered: bool, is_summary_up_to_date: bool, summary: null|string, timestamp: int|null, title: null|string, user_id: string}>}, array<never, never>>' for OCA\Assistant\Controller\ChattyLLMController::searchChat is more specific than the inferred return type 'OCP\AppFramework\Http\JSONResponse<200|401|500, array{error?: string, sessions?: array<array-key, OCA\Assistant\Db\ChattyLLM\Session>}, array<never, never>>' (see https://psalm.dev/070)
* 200: Search results
* 401: Not logged in
* 500: Failed to search
*/
#[NoAdminRequired]
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT, tags: ['chat_api'])]
public function searchChat(string $query = ''): JSONResponse {
if ($this->userId === null) {
return new JSONResponse(['error' => $this->l10n->t('User not logged in')], Http::STATUS_UNAUTHORIZED);
}

$query = trim($query);
if (empty($query) || strlen($query) < 2) {
return new JSONResponse(['sessions' => []]);
}

try {
$sessions = $this->sessionMapper->searchSessionsByMessageContent($this->userId, $query);
return new JSONResponse(['sessions' => $sessions]);
} catch (\OCP\DB\Exception $e) {

Check failure on line 341 in lib/Controller/ChattyLLMController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable33

LessSpecificReturnStatement

lib/Controller/ChattyLLMController.php:341:11: LessSpecificReturnStatement: The type 'OCP\AppFramework\Http\JSONResponse<200, array{sessions: array<array-key, OCA\Assistant\Db\ChattyLLM\Session>}, array<never, never>>' is more general than the declared return type 'OCP\AppFramework\Http\JSONResponse<200|401|500, array{error?: string, sessions?: list<array{agency_conversation_token: null|string, agency_pending_actions: null|string, id: int, is_remembered: bool, is_summary_up_to_date: bool, summary: null|string, timestamp: int|null, title: null|string, user_id: string}>}, array<never, never>>' for OCA\Assistant\Controller\ChattyLLMController::searchChat (see https://psalm.dev/129)

Check failure on line 341 in lib/Controller/ChattyLLMController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

LessSpecificReturnStatement

lib/Controller/ChattyLLMController.php:341:11: LessSpecificReturnStatement: The type 'OCP\AppFramework\Http\JSONResponse<200, array{sessions: array<array-key, OCA\Assistant\Db\ChattyLLM\Session>}, array<never, never>>' is more general than the declared return type 'OCP\AppFramework\Http\JSONResponse<200|401|500, array{error?: string, sessions?: list<array{agency_conversation_token: null|string, agency_pending_actions: null|string, id: int, is_remembered: bool, is_summary_up_to_date: bool, summary: null|string, timestamp: int|null, title: null|string, user_id: string}>}, array<never, never>>' for OCA\Assistant\Controller\ChattyLLMController::searchChat (see https://psalm.dev/129)
$this->logger->warning('Failed to search chat messages', ['exception' => $e]);
return new JSONResponse(['error' => $this->l10n->t('Failed to search chat sessions')], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}

/**
* Add a message
*
Expand Down
20 changes: 20 additions & 0 deletions lib/Db/ChattyLLM/SessionMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,24 @@ public function updateSessionIsRemembered(?string $userId, int $sessionId, bool
$session->setIsRemembered($is_remembered);
$this->update($session);
}

/**
* @return array<Session>
* @throws \OCP\DB\Exception
*/
public function searchSessionsByMessageContent(string $userId, string $query): array {
$qb = $this->db->getQueryBuilder();
$qb->select('s.id', 's.user_id', 's.title', 's.timestamp', 's.agency_conversation_token', 's.agency_pending_actions', 's.summary', 's.is_summary_up_to_date', 's.is_remembered')
->from($this->getTableName(), 's')
->innerJoin('s', 'assistant_chat_msgs', 'm', $qb->expr()->eq('s.id', 'm.session_id'))
->where($qb->expr()->eq('s.user_id', $qb->createPositionalParameter($userId, IQueryBuilder::PARAM_STR)))
->andWhere($qb->expr()->in('m.role', $qb->createParameter('roles')))
->andWhere($qb->expr()->iLike('m.content', $qb->createPositionalParameter('%' . $query . '%', IQueryBuilder::PARAM_STR)))
->groupBy('s.id')
->orderBy('s.timestamp', 'DESC');

$qb->setParameter('roles', ['human', 'assistant'], IQueryBuilder::PARAM_STR_ARRAY);

return $this->findEntities($qb);
}
}
Loading
Loading