Skip to content

Commit c3b02fa

Browse files
committed
fix(regeneration): store task id in messages so the check endpoint can return a message ID
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
1 parent 673dff4 commit c3b02fa

File tree

6 files changed

+74
-6
lines changed

6 files changed

+74
-6
lines changed

appinfo/info.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Known providers:
6262
6363
More details on how to set this up in the [admin docs](https://docs.nextcloud.com/server/latest/admin_manual/ai/index.html)
6464
]]> </description>
65-
<version>2.3.0</version>
65+
<version>2.4.0</version>
6666
<licence>agpl</licence>
6767
<author>Julien Veyssier</author>
6868
<namespace>Assistant</namespace>

lib/Controller/ChattyLLMController.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -420,11 +420,7 @@ public function checkMessageGenerationTask(int $taskId, int $sessionId): JSONRes
420420
}
421421
if ($task->getStatus() === Task::STATUS_SUCCESSFUL) {
422422
try {
423-
$message = new Message();
424-
$message->setSessionId($sessionId);
425-
$message->setRole('assistant');
426-
$message->setContent(trim($task->getOutput()['output'] ?? ''));
427-
$message->setTimestamp(time());
423+
$message = $this->messageMapper->getMessageByTaskId($sessionId, $taskId);
428424
$jsonMessage = $message->jsonSerialize();
429425
$session = $this->sessionMapper->getUserSession($this->userId, $sessionId);
430426
$jsonMessage['sessionAgencyPendingActions'] = $session->getAgencyPendingActions();

lib/Db/ChattyLLM/Message.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* @method \void setContent(string $content)
2222
* @method \int getTimestamp()
2323
* @method \void setTimestamp(int $timestamp)
24+
* @method \int getOcpTaskId()
25+
* @method \void setOcpTaskId(int $ocpTaskId)
2426
*/
2527
class Message extends Entity implements \JsonSerializable {
2628
/** @var int */
@@ -31,27 +33,32 @@ class Message extends Entity implements \JsonSerializable {
3133
protected $content;
3234
/** @var int */
3335
protected $timestamp;
36+
/** @var int */
37+
protected $ocpTaskId;
3438

3539
public static $columns = [
3640
'id',
3741
'session_id',
3842
'role',
3943
'content',
4044
'timestamp',
45+
'ocp_task_id',
4146
];
4247
public static $fields = [
4348
'id',
4449
'sessionId',
4550
'role',
4651
'content',
4752
'timestamp',
53+
'ocpTaskId',
4854
];
4955

5056
public function __construct() {
5157
$this->addType('session_id', Types::INTEGER);
5258
$this->addType('role', Types::STRING);
5359
$this->addType('content', Types::STRING);
5460
$this->addType('timestamp', Types::INTEGER);
61+
$this->addType('ocp_task_id', Types::INTEGER);
5562
}
5663

5764
#[\ReturnTypeWillChange]
@@ -62,6 +69,7 @@ public function jsonSerialize() {
6269
'role' => $this->role,
6370
'content' => $this->content,
6471
'timestamp' => $this->timestamp,
72+
'ocp_task_id' => $this->ocpTaskId,
6573
];
6674
}
6775
}

lib/Db/ChattyLLM/MessageMapper.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,24 @@ public function getMessageById(int $messageId): Message {
101101
return $this->findEntity($qb);
102102
}
103103

104+
/**
105+
* @param int $sessionId
106+
* @param int $ocpTaskId
107+
* @return Message
108+
* @throws DoesNotExistException
109+
* @throws MultipleObjectsReturnedException
110+
* @throws \OCP\DB\Exception
111+
*/
112+
public function getMessageByTaskId(int $sessionId, int $ocpTaskId): Message {
113+
$qb = $this->db->getQueryBuilder();
114+
$qb->select(Message::$columns)
115+
->from($this->getTableName())
116+
->where($qb->expr()->eq('session_id', $qb->createPositionalParameter($sessionId, IQueryBuilder::PARAM_INT)))
117+
->andWhere($qb->expr()->eq('ocp_task_id', $qb->createPositionalParameter($ocpTaskId, IQueryBuilder::PARAM_INT)));
118+
119+
return $this->findEntity($qb);
120+
}
121+
104122
/**
105123
* @param int $sessionId
106124
* @throws \OCP\DB\Exception

lib/Listener/ChattyLLMTaskListener.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public function handle(Event $event): void {
5757

5858
$message = new Message();
5959
$message->setSessionId($sessionId);
60+
$message->setOcpTaskId($task->getId());
6061
$message->setRole('assistant');
6162
$message->setContent(trim($task->getOutput()['output'] ?? ''));
6263
$message->setTimestamp(time());
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
9+
namespace OCA\Assistant\Migration;
10+
11+
use Closure;
12+
use OCP\DB\ISchemaWrapper;
13+
use OCP\DB\Types;
14+
use OCP\Migration\IOutput;
15+
use OCP\Migration\SimpleMigrationStep;
16+
17+
class Version020400Date20250203125359 extends SimpleMigrationStep {
18+
19+
/**
20+
* @param IOutput $output
21+
* @param Closure(): ISchemaWrapper $schemaClosure
22+
* @param array $options
23+
* @return null|ISchemaWrapper
24+
*/
25+
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
26+
/** @var ISchemaWrapper $schema */
27+
$schema = $schemaClosure();
28+
$schemaChanged = false;
29+
30+
if ($schema->hasTable('assistant_chat_msgs')) {
31+
$table = $schema->getTable('assistant_chat_msgs');
32+
if (!$table->hasColumn('ocp_task_id')) {
33+
$table->addColumn('ocp_task_id', Types::BIGINT, [
34+
'notnull' => true,
35+
'default' => 0,
36+
'unsigned' => true,
37+
]);
38+
$table->addIndex(['session_id', 'ocp_task_id'], 'assistant_ch_sid_tid');
39+
$schemaChanged = true;
40+
}
41+
}
42+
43+
return $schemaChanged ? $schema : null;
44+
}
45+
}

0 commit comments

Comments
 (0)