Skip to content
Open
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
10 changes: 6 additions & 4 deletions lib/Controller/ChattyLLMController.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,16 +311,17 @@ public function deleteSession(int $sessionId): JSONResponse {
*
* Get all chat sessions for the current user
*
* @param bool|null $isAssignment Whether to get only the sessions for assignments (true) or not (false) defaults to false
* @return JSONResponse<Http::STATUS_OK, list<AssistantChatSession>, array{}>|JSONResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_UNAUTHORIZED, array{error: string}, array{}>
*
* 200: The session list has been obtained successfully
* 401: Not logged in
*/
#[NoAdminRequired]
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT, tags: ['chat_api'])]
public function getSessions(): JSONResponse {
public function getSessions(?bool $isAssignment = false): JSONResponse {
try {
$sessions = $this->chatService->getSessionsForUser($this->userId);
$sessions = $this->chatService->getSessionsForUser($this->userId, $isAssignment);
/** @var list<AssistantChatSession> $serializedSessions */
$serializedSessions = array_map(static function ($session) {
return $session->jsonSerialize();
Expand Down Expand Up @@ -380,6 +381,7 @@ public function newMessage(
* @param int $sessionId The session ID
* @param int $limit The max number of messages to return
* @param int $cursor The index of the first result to return
* @param bool $hideUserMessages Whether to hide user messages from the response
* @return JSONResponse<Http::STATUS_OK, list<AssistantChatMessage>, array{}>|JSONResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_UNAUTHORIZED|Http::STATUS_NOT_FOUND, array{error: string}, array{}>
*
* 200: The message list has been successfully obtained
Expand All @@ -388,9 +390,9 @@ public function newMessage(
*/
#[NoAdminRequired]
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT, tags: ['chat_api'])]
public function getMessages(int $sessionId, int $limit = 20, int $cursor = 0): JSONResponse {
public function getMessages(int $sessionId, int $limit = 20, int $cursor = 0, bool $hideUserMessages = false): JSONResponse {
try {
$messages = $this->chatService->getSessionMessages($this->userId, $sessionId, $limit, $cursor);
$messages = $this->chatService->getSessionMessages($this->userId, $sessionId, $limit, $cursor, $hideUserMessages);
return new JSONResponse(array_map(static function (Message $message) {
return $message->jsonSerialize();
}, $messages));
Expand Down
7 changes: 6 additions & 1 deletion lib/Db/ChattyLLM/MessageMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,22 @@ public function getLastNonEmptyHumanMessage(int $sessionId): Message {
* @param int $sessionId
* @param int $cursor
* @param int $limit
* @param bool $hideUserMessages
* @return list<Message>
* @throws \OCP\DB\Exception
*/
public function getMessages(int $sessionId, int $cursor, int $limit): array {
public function getMessages(int $sessionId, int $cursor, int $limit, bool $hideUserMessages = false): array {
$qb = $this->db->getQueryBuilder();
$qb->select(Message::$columns)
->from($this->getTableName())
->where($qb->expr()->eq('session_id', $qb->createPositionalParameter($sessionId, IQueryBuilder::PARAM_INT)))
->orderBy('id', 'DESC')
->setFirstResult($cursor);

if ($hideUserMessages) {
$qb->andWhere($qb->expr()->neq('role', $qb->createPositionalParameter(Message::ROLE_HUMAN, IQueryBuilder::PARAM_STR)));
}

if ($limit > 0) {
$qb->setMaxResults($limit);
}
Expand Down
9 changes: 8 additions & 1 deletion lib/Db/ChattyLLM/SessionMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,23 @@ public function getUserSessionForAssignment(string $userId, int $assignmentId):

/**
* @param string $userId
* @param bool $isAssignment
* @return list<Session>
* @throws \OCP\DB\Exception
*/
public function getUserSessions(string $userId): array {
public function getUserSessions(string $userId, bool $isAssignment): array {
$qb = $this->db->getQueryBuilder();
$qb->select(Session::$columns)
->from($this->getTableName())
->where($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId, IQueryBuilder::PARAM_STR)))
->orderBy('timestamp', 'DESC');

if ($isAssignment) {
$qb->andWhere($qb->expr()->isNotNull('assignment_id'));
} else {
$qb->andWhere($qb->expr()->isNull('assignment_id'));
}

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

Expand Down
21 changes: 21 additions & 0 deletions lib/Service/AssistantService.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class AssistantService {

private const TASK_TYPE_PRIORITIES = [
'chatty-llm' => 1,
'assignments' => 1,
TextToText::ID => 2,
'context_chat:context_chat' => 3,
'legacy:TextProcessing:OCA\ContextChat\TextProcessing\ContextChatTaskType' => 3,
Expand Down Expand Up @@ -287,6 +288,9 @@ private function getCategory(string $typeId): array {
if (str_starts_with($typeId, 'chatty')) {
$categoryId = 'chat';
$categoryName = $this->l10n->t('Chat with AI');
} elseif (str_starts_with($typeId, 'assignments')) {
$categoryId = 'assignments';
$categoryName = $this->l10n->t('Scheduled tasks');
} elseif (str_starts_with($typeId, 'context_chat')) {
$categoryId = 'context';
$categoryName = $this->l10n->t('Context Chat');
Expand Down Expand Up @@ -435,6 +439,23 @@ public function getAvailableTaskTypes(): array {
'priority' => self::TASK_TYPE_PRIORITIES['chatty-llm'] ?? 1000,
'preferredProviderName' => $preferredProviderName,
];
// add the chattyUI assignments virtual task type
$types[] = [
'id' => 'assignments',
'name' => $this->l10n->t('Scheduled tasks'),
'description' => $this->l10n->t('Scheduled tasks'),
'category' => $this->getCategory('assignments'),
'inputShape' => [],
'inputShapeEnumValues' => [],
'inputShapeDefaults' => [],
'outputShape' => [],
'optionalInputShape' => [],
'optionalInputShapeEnumValues' => [],
'optionalInputShapeDefaults' => [],
'optionalOutputShape' => [],
'priority' => self::TASK_TYPE_PRIORITIES['assignments'] ?? 1000,
'preferredProviderName' => $preferredProviderName,
];
// do not add the raw TextToTextChat type
if (!self::DEBUG) {
continue;
Expand Down
8 changes: 4 additions & 4 deletions lib/Service/ChatService.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ public function deleteSession(?string $userId, int $sessionId): void {
* @throws InternalException
* @throws UnauthorizedException
*/
public function getSessionsForUser(?string $userId): array {
public function getSessionsForUser(?string $userId, bool $isAssignment): array {
if ($userId === null) {
throw new UnauthorizedException($this->l10n->t('Unauthorized'));
}
try {
return $this->sessionMapper->getUserSessions($userId);
return $this->sessionMapper->getUserSessions($userId, $isAssignment);
} catch (Exception $e) {
throw new InternalException(previous: $e);
}
Expand Down Expand Up @@ -250,7 +250,7 @@ public function createMessage(?string $userId, int $sessionId, string $role, str
* @throws NotFoundException
* @throws UnauthorizedException
*/
public function getSessionMessages(?string $userId, int $sessionId, int $limit = 20, int $cursor = 0): array {
public function getSessionMessages(?string $userId, int $sessionId, int $limit = 20, int $cursor = 0, bool $hideUserMessages = false): array {
if ($userId === null) {
throw new UnauthorizedException($this->l10n->t('Unauthorized'));
}
Expand All @@ -268,7 +268,7 @@ public function getSessionMessages(?string $userId, int $sessionId, int $limit =

/** @var list<Message> $messages */
try {
$messages = $this->messageMapper->getMessages($sessionId, $cursor, $limit);
$messages = $this->messageMapper->getMessages($sessionId, $cursor, $limit, $hideUserMessages);
} catch (Exception $e) {
throw new InternalException(previous: $e);
}
Expand Down
19 changes: 19 additions & 0 deletions openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -2896,6 +2896,16 @@
}
],
"parameters": [
{
"name": "isAssignment",
"in": "query",
"description": "Whether to get only the sessions for assignments (true) or not (false) defaults to false",
"schema": {
"type": "boolean",
"nullable": true,
"default": false
}
},
{
"name": "OCS-APIRequest",
"in": "header",
Expand Down Expand Up @@ -4041,6 +4051,15 @@
"default": 0
}
},
{
"name": "hideUserMessages",
"in": "query",
"description": "Whether to hide user messages from the response",
"schema": {
"type": "boolean",
"default": false
}
},
{
"name": "OCS-APIRequest",
"in": "header",
Expand Down
Loading
Loading