Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

button disable fix + make fallback strings same for translation ease #94

Merged
merged 3 commits into from
Jul 1, 2024
Merged
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
30 changes: 20 additions & 10 deletions lib/Controller/ChattyLLMController.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function updateSessionTitle(int $sessionId, string $title): JSONResponse
}

try {
$this->sessionMapper->updateSessionTitle($sessionId, $title);
$this->sessionMapper->updateSessionTitle($this->userId, $sessionId, $title);
return new JSONResponse();
} catch (\OCP\DB\Exception | \RuntimeException $e) {
$this->logger->warning('Failed to update the chat session', ['exception' => $e]);
Expand All @@ -124,7 +124,7 @@ public function deleteSession(int $sessionId): JSONResponse {
}

try {
$this->sessionMapper->deleteSession($sessionId);
$this->sessionMapper->deleteSession($this->userId, $sessionId);
$this->messageMapper->deleteMessagesBySession($sessionId);
return new JSONResponse();
} catch (\OCP\DB\Exception | \RuntimeException $e) {
Expand Down Expand Up @@ -170,7 +170,7 @@ public function newMessage(int $sessionId, string $role, string $content, int $t
}

try {
$sessionExists = $this->sessionMapper->exists($sessionId);
$sessionExists = $this->sessionMapper->exists($this->userId, $sessionId);
if (!$sessionExists) {
return new JSONResponse(['error' => $this->l10n->t('Session not found')], Http::STATUS_NOT_FOUND);
}
Expand All @@ -189,7 +189,11 @@ public function newMessage(int $sessionId, string $role, string $content, int $t

if ($firstHumanMessage) {
// set the title of the session based on first human message
$this->sessionMapper->updateSessionTitle($sessionId, strlen($content) > 140 ? mb_substr($content, 0, 140) . '...' : $content);
$this->sessionMapper->updateSessionTitle(
$this->userId,
$sessionId,
strlen($content) > 140 ? mb_substr($content, 0, 140) . '...' : $content,
);
}

return new JSONResponse($message);
Expand All @@ -214,7 +218,7 @@ public function getMessages(int $sessionId, int $limit = 20, int $cursor = 0): J
}

try {
$sessionExists = $this->sessionMapper->exists($sessionId);
$sessionExists = $this->sessionMapper->exists($this->userId, $sessionId);
if (!$sessionExists) {
return new JSONResponse(['error' => $this->l10n->t('Session not found')], Http::STATUS_NOT_FOUND);
}
Expand All @@ -235,15 +239,21 @@ public function getMessages(int $sessionId, int $limit = 20, int $cursor = 0): J
* Delete a chat message by ID
*
* @param integer $messageId
* @param integer $sessionId
* @return JSONResponse
*/
#[NoAdminRequired]
public function deleteMessage(int $messageId): JSONResponse {
public function deleteMessage(int $messageId, int $sessionId): JSONResponse {
if ($this->userId === null) {
return new JSONResponse(['error' => $this->l10n->t('User not logged in')], Http::STATUS_UNAUTHORIZED);
}

try {
$sessionExists = $this->sessionMapper->exists($this->userId, $sessionId);
if (!$sessionExists) {
return new JSONResponse(['error' => $this->l10n->t('Session not found')], Http::STATUS_NOT_FOUND);
}

$this->messageMapper->deleteMessageById($messageId);
return new JSONResponse();
} catch (\OCP\DB\Exception | \RuntimeException $e) {
Expand All @@ -265,7 +275,7 @@ public function generateForSession(int $sessionId): JSONResponse {
}

try {
$sessionExists = $this->sessionMapper->exists($sessionId);
$sessionExists = $this->sessionMapper->exists($this->userId, $sessionId);
if (!$sessionExists) {
return new JSONResponse(['error' => $this->l10n->t('Session not found')], Http::STATUS_NOT_FOUND);
}
Expand Down Expand Up @@ -306,7 +316,7 @@ public function regenerateForSession(int $sessionId, int $messageId): JSONRespon
}

try {
$sessionExists = $this->sessionMapper->exists($sessionId);
$sessionExists = $this->sessionMapper->exists($this->userId, $sessionId);
if (!$sessionExists) {
return new JSONResponse(['error' => $this->l10n->t('Session not found')], Http::STATUS_NOT_FOUND);
}
Expand Down Expand Up @@ -372,7 +382,7 @@ public function generateTitle(int $sessionId): JSONResponse {
}

try {
$sessionExists = $this->sessionMapper->exists($sessionId);
$sessionExists = $this->sessionMapper->exists($this->userId, $sessionId);
if (!$sessionExists) {
return new JSONResponse(['error' => $this->l10n->t('Session not found')], Http::STATUS_NOT_FOUND);
}
Expand All @@ -394,7 +404,7 @@ public function generateTitle(int $sessionId): JSONResponse {
$title = explode(PHP_EOL, $title)[0];
$title = trim($title);

$this->sessionMapper->updateSessionTitle($sessionId, $title);
$this->sessionMapper->updateSessionTitle($this->userId, $sessionId, $title);

return new JSONResponse(['result' => $title]);
} catch (\OCP\DB\Exception $e) {
Expand Down
18 changes: 12 additions & 6 deletions lib/Db/ChattyLLM/SessionMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,17 @@ public function __construct(IDBConnection $db) {
}

/**
* @param string $userId
* @param integer $sessionId
* @return boolean
* @throws \OCP\DB\Exception
*/
public function exists(int $sessionId): bool {
public function exists(string $userId, int $sessionId): bool {
$qb = $this->db->getQueryBuilder();
$qb->select('id')
->from($this->getTableName())
->where($qb->expr()->eq('id', $qb->createPositionalParameter($sessionId, IQueryBuilder::PARAM_INT)));
->where($qb->expr()->eq('id', $qb->createPositionalParameter($sessionId, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId, IQueryBuilder::PARAM_STR)));

try {
return $this->findEntity($qb) !== null;
Expand All @@ -73,29 +75,33 @@ public function getUserSessions(string $userId): array {
}

/**
* @param string $userId
* @param integer $sessionId
* @param string $title
* @throws \OCP\DB\Exception
* @throws \RuntimeException
*/
public function updateSessionTitle(int $sessionId, string $title) {
public function updateSessionTitle(string $userId, int $sessionId, string $title) {
$qb = $this->db->getQueryBuilder();
$qb->update($this->getTableName())
->set('title', $qb->createPositionalParameter($title, IQueryBuilder::PARAM_STR))
->where($qb->expr()->eq('id', $qb->createPositionalParameter($sessionId, IQueryBuilder::PARAM_INT)));
->where($qb->expr()->eq('id', $qb->createPositionalParameter($sessionId, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId, IQueryBuilder::PARAM_STR)));

$qb->executeStatement();
}

/**
* @param string $userId
* @param integer $sessionId
* @throws \OCP\DB\Exception
* @throws \RuntimeException
*/
public function deleteSession(int $sessionId) {
public function deleteSession(string $userId, int $sessionId) {
$qb = $this->db->getQueryBuilder();
$qb->delete($this->getTableName())
->where($qb->expr()->eq('id', $qb->createPositionalParameter($sessionId, IQueryBuilder::PARAM_INT)));
->where($qb->expr()->eq('id', $qb->createPositionalParameter($sessionId, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId, IQueryBuilder::PARAM_STR)));

$qb->executeStatement();
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/ChattyLLM/ChattyLLMInputForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ export default {
try {
this.loading.messageDelete = true
await axios.delete(getChatURL('/delete_message'), {
params: { messageId },
params: { messageId, sessionId: this.active.id },
})
this.messages = this.messages.filter((message) => message.id !== messageId)
} catch (error) {
Expand Down
4 changes: 0 additions & 4 deletions src/components/ChattyLLM/ConversationBox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ import NcLoadingIcon from '@nextcloud/vue/dist/Components/NcLoadingIcon.js'
import Message from './Message.vue'
import NoSession from './NoSession.vue'

import { getCurrentUser } from '@nextcloud/auth'

export default {
name: 'ConversationBox',

Expand Down Expand Up @@ -74,8 +72,6 @@ export default {

data: () => {
return {
displayName: getCurrentUser()?.displayName ?? getCurrentUser()?.uid ?? t('assistant', 'You'),
userId: getCurrentUser()?.uid ?? t('assistant', 'yooniquely-you'),
regenerateFromId: null,
deleteMessageId: null,
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/ChattyLLM/EditableTextField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export default {
overflow: hidden;
position: relative;
min-height: var(--default-clickable-area);
align-items: flex-end;
align-items: center;

&__edit {
margin-left: var(--default-clickable-area);
Expand Down
2 changes: 1 addition & 1 deletion src/components/ChattyLLM/Message.vue
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export default {
data: () => {
return {
displayName: getCurrentUser()?.displayName ?? getCurrentUser()?.uid ?? t('assistant', 'You'),
userId: getCurrentUser()?.uid ?? t('assistant', 'yooniquely-you'),
userId: getCurrentUser()?.uid ?? t('assistant', 'You'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is no user in the context, something is wrong, it does not matter that much to have a fallback there, right? Especially since this userId is only passed to the avatar. Anyway, can't hurt 👍

showMessageActions: false,
}
},
Expand Down
2 changes: 2 additions & 0 deletions src/components/ChattyLLM/MessageActions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
type="tertiary"
:aria-label="t('assistant', 'Regenerate message')"
:title="t('assistant', 'Regenerate message')"
:disabled="regenerateLoading"
@click="$emit('regenerate', $event)">
<template v-if="regenerateLoading" #icon>
<NcLoadingIcon :size="20" />
Expand All @@ -25,6 +26,7 @@
type="tertiary"
:aria-label="t('assistant', 'Delete message')"
:title="t('assistant', 'Delete message')"
:disabled="regenerateLoading"
@click="$emit('delete', $event)">
<template v-if="deleteLoading" #icon>
<NcLoadingIcon :size="20" />
Expand Down
Loading