From c3b02fa2692859e8a64c865a3600e850c2f3e6db Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Mon, 3 Feb 2025 13:03:28 +0100 Subject: [PATCH] fix(regeneration): store task id in messages so the check endpoint can return a message ID Signed-off-by: Julien Veyssier --- appinfo/info.xml | 2 +- lib/Controller/ChattyLLMController.php | 6 +-- lib/Db/ChattyLLM/Message.php | 8 ++++ lib/Db/ChattyLLM/MessageMapper.php | 18 ++++++++ lib/Listener/ChattyLLMTaskListener.php | 1 + .../Version020400Date20250203125359.php | 45 +++++++++++++++++++ 6 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 lib/Migration/Version020400Date20250203125359.php diff --git a/appinfo/info.xml b/appinfo/info.xml index cc176b2b..24a2ec6a 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -62,7 +62,7 @@ Known providers: More details on how to set this up in the [admin docs](https://docs.nextcloud.com/server/latest/admin_manual/ai/index.html) ]]> - 2.3.0 + 2.4.0 agpl Julien Veyssier Assistant diff --git a/lib/Controller/ChattyLLMController.php b/lib/Controller/ChattyLLMController.php index b234c9b0..2288a33f 100644 --- a/lib/Controller/ChattyLLMController.php +++ b/lib/Controller/ChattyLLMController.php @@ -420,11 +420,7 @@ public function checkMessageGenerationTask(int $taskId, int $sessionId): JSONRes } if ($task->getStatus() === Task::STATUS_SUCCESSFUL) { try { - $message = new Message(); - $message->setSessionId($sessionId); - $message->setRole('assistant'); - $message->setContent(trim($task->getOutput()['output'] ?? '')); - $message->setTimestamp(time()); + $message = $this->messageMapper->getMessageByTaskId($sessionId, $taskId); $jsonMessage = $message->jsonSerialize(); $session = $this->sessionMapper->getUserSession($this->userId, $sessionId); $jsonMessage['sessionAgencyPendingActions'] = $session->getAgencyPendingActions(); diff --git a/lib/Db/ChattyLLM/Message.php b/lib/Db/ChattyLLM/Message.php index 1dd6bf8b..faad0a0f 100644 --- a/lib/Db/ChattyLLM/Message.php +++ b/lib/Db/ChattyLLM/Message.php @@ -21,6 +21,8 @@ * @method \void setContent(string $content) * @method \int getTimestamp() * @method \void setTimestamp(int $timestamp) + * @method \int getOcpTaskId() + * @method \void setOcpTaskId(int $ocpTaskId) */ class Message extends Entity implements \JsonSerializable { /** @var int */ @@ -31,6 +33,8 @@ class Message extends Entity implements \JsonSerializable { protected $content; /** @var int */ protected $timestamp; + /** @var int */ + protected $ocpTaskId; public static $columns = [ 'id', @@ -38,6 +42,7 @@ class Message extends Entity implements \JsonSerializable { 'role', 'content', 'timestamp', + 'ocp_task_id', ]; public static $fields = [ 'id', @@ -45,6 +50,7 @@ class Message extends Entity implements \JsonSerializable { 'role', 'content', 'timestamp', + 'ocpTaskId', ]; public function __construct() { @@ -52,6 +58,7 @@ public function __construct() { $this->addType('role', Types::STRING); $this->addType('content', Types::STRING); $this->addType('timestamp', Types::INTEGER); + $this->addType('ocp_task_id', Types::INTEGER); } #[\ReturnTypeWillChange] @@ -62,6 +69,7 @@ public function jsonSerialize() { 'role' => $this->role, 'content' => $this->content, 'timestamp' => $this->timestamp, + 'ocp_task_id' => $this->ocpTaskId, ]; } } diff --git a/lib/Db/ChattyLLM/MessageMapper.php b/lib/Db/ChattyLLM/MessageMapper.php index f3023f50..b2c619a7 100644 --- a/lib/Db/ChattyLLM/MessageMapper.php +++ b/lib/Db/ChattyLLM/MessageMapper.php @@ -101,6 +101,24 @@ public function getMessageById(int $messageId): Message { return $this->findEntity($qb); } + /** + * @param int $sessionId + * @param int $ocpTaskId + * @return Message + * @throws DoesNotExistException + * @throws MultipleObjectsReturnedException + * @throws \OCP\DB\Exception + */ + public function getMessageByTaskId(int $sessionId, int $ocpTaskId): Message { + $qb = $this->db->getQueryBuilder(); + $qb->select(Message::$columns) + ->from($this->getTableName()) + ->where($qb->expr()->eq('session_id', $qb->createPositionalParameter($sessionId, IQueryBuilder::PARAM_INT))) + ->andWhere($qb->expr()->eq('ocp_task_id', $qb->createPositionalParameter($ocpTaskId, IQueryBuilder::PARAM_INT))); + + return $this->findEntity($qb); + } + /** * @param int $sessionId * @throws \OCP\DB\Exception diff --git a/lib/Listener/ChattyLLMTaskListener.php b/lib/Listener/ChattyLLMTaskListener.php index 7c39aeb7..4cb46b62 100644 --- a/lib/Listener/ChattyLLMTaskListener.php +++ b/lib/Listener/ChattyLLMTaskListener.php @@ -57,6 +57,7 @@ public function handle(Event $event): void { $message = new Message(); $message->setSessionId($sessionId); + $message->setOcpTaskId($task->getId()); $message->setRole('assistant'); $message->setContent(trim($task->getOutput()['output'] ?? '')); $message->setTimestamp(time()); diff --git a/lib/Migration/Version020400Date20250203125359.php b/lib/Migration/Version020400Date20250203125359.php new file mode 100644 index 00000000..311b8ceb --- /dev/null +++ b/lib/Migration/Version020400Date20250203125359.php @@ -0,0 +1,45 @@ +hasTable('assistant_chat_msgs')) { + $table = $schema->getTable('assistant_chat_msgs'); + if (!$table->hasColumn('ocp_task_id')) { + $table->addColumn('ocp_task_id', Types::BIGINT, [ + 'notnull' => true, + 'default' => 0, + 'unsigned' => true, + ]); + $table->addIndex(['session_id', 'ocp_task_id'], 'assistant_ch_sid_tid'); + $schemaChanged = true; + } + } + + return $schemaChanged ? $schema : null; + } +}