Skip to content

Commit

Permalink
fix(regeneration): store task id in messages so the check endpoint ca…
Browse files Browse the repository at this point in the history
…n return a message ID

Signed-off-by: Julien Veyssier <[email protected]>
  • Loading branch information
julien-nc committed Feb 3, 2025
1 parent 673dff4 commit c3b02fa
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 6 deletions.
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
]]> </description>
<version>2.3.0</version>
<version>2.4.0</version>
<licence>agpl</licence>
<author>Julien Veyssier</author>
<namespace>Assistant</namespace>
Expand Down
6 changes: 1 addition & 5 deletions lib/Controller/ChattyLLMController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 8 additions & 0 deletions lib/Db/ChattyLLM/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -31,27 +33,32 @@ class Message extends Entity implements \JsonSerializable {
protected $content;
/** @var int */
protected $timestamp;
/** @var int */
protected $ocpTaskId;

public static $columns = [
'id',
'session_id',
'role',
'content',
'timestamp',
'ocp_task_id',
];
public static $fields = [
'id',
'sessionId',
'role',
'content',
'timestamp',
'ocpTaskId',
];

public function __construct() {
$this->addType('session_id', Types::INTEGER);
$this->addType('role', Types::STRING);
$this->addType('content', Types::STRING);
$this->addType('timestamp', Types::INTEGER);
$this->addType('ocp_task_id', Types::INTEGER);
}

#[\ReturnTypeWillChange]
Expand All @@ -62,6 +69,7 @@ public function jsonSerialize() {
'role' => $this->role,
'content' => $this->content,
'timestamp' => $this->timestamp,
'ocp_task_id' => $this->ocpTaskId,
];
}
}
18 changes: 18 additions & 0 deletions lib/Db/ChattyLLM/MessageMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/Listener/ChattyLLMTaskListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
45 changes: 45 additions & 0 deletions lib/Migration/Version020400Date20250203125359.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Assistant\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

class Version020400Date20250203125359 extends SimpleMigrationStep {

/**
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
$schemaChanged = false;

if ($schema->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;
}
}

0 comments on commit c3b02fa

Please sign in to comment.