Files
nc-talk-ai/lib/Db/ConversationMapper.php
Pascal Kienast 0739d3da6a Initial open-source release of Talk AI
Talk AI is a multi-bot AI assistant manager for Nextcloud Talk:
per-bot prompts and models, agentic tool calling (MCP + built-in
tools), RAG over Nextcloud files, room-document search, vision and
speech-to-text attachments, persistent bot wikis, approval workflows,
rate limiting, and multi-provider LLM support (any OpenAI-compatible
endpoint).

Developed within EDUC - the European Digital UniverCity
(https://educalliance.eu), where it runs as the 'EDUC AI' assistant on
the alliance-wide Nextcloud portal. This public repository is the
upstream point of truth; deployment-specific tools plug in via the
tool-provider extension point (docs/TOOL_PROVIDERS.md).

License: AGPL-3.0-or-later.
2026-07-08 21:13:13 +02:00

125 lines
3.2 KiB
PHP

<?php
declare(strict_types=1);
namespace OCA\EducAI\Db;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
/**
* @template-extends QBMapper<Conversation>
*/
class ConversationMapper extends QBMapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'educai_conversations', Conversation::class);
}
/**
* Find conversation history for a bot in a specific room
*
* @param int $botId
* @param string $roomToken
* @param int $limit Maximum messages to retrieve (default 50 for token-based filtering)
* @return Conversation[]
*/
public function findByBotAndRoom(int $botId, string $roomToken, int $limit = 50): array {
return $this->findByBotRoomAndThread($botId, $roomToken, null, $limit);
}
/**
* Find conversation history for a bot in a room and Talk thread.
*
* @param int $botId
* @param string $roomToken
* @param int|null $threadRootMessageId
* @param int $limit Maximum messages to retrieve (default 50 for token-based filtering)
* @return Conversation[]
*/
public function findByBotRoomAndThread(int $botId, string $roomToken, ?int $threadRootMessageId, int $limit = 50): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('bot_id', $qb->createNamedParameter($botId, IQueryBuilder::PARAM_INT))
)
->andWhere(
$qb->expr()->eq('room_token', $qb->createNamedParameter($roomToken, IQueryBuilder::PARAM_STR))
);
if ($threadRootMessageId === null) {
$qb->andWhere($qb->expr()->isNull('thread_root_message_id'));
} else {
$qb->andWhere(
$qb->expr()->eq('thread_root_message_id', $qb->createNamedParameter($threadRootMessageId, IQueryBuilder::PARAM_INT))
);
}
$qb->orderBy('created_at', 'DESC')
->setMaxResults($limit);
$entities = $this->findEntities($qb);
// Return in chronological order (oldest first)
return array_reverse($entities);
}
/**
* Delete all conversations for a specific bot
*
* @param int $botId
* @return void
*/
public function deleteByBot(int $botId): void {
$qb = $this->db->getQueryBuilder();
$qb->delete($this->getTableName())
->where(
$qb->expr()->eq('bot_id', $qb->createNamedParameter($botId, IQueryBuilder::PARAM_INT))
);
$qb->executeStatement();
}
/**
* Delete old conversations (older than specified timestamp)
*
* @param int $timestamp
* @return void
*/
public function deleteOlderThan(int $timestamp): void {
$qb = $this->db->getQueryBuilder();
$qb->delete($this->getTableName())
->where(
$qb->expr()->lt('created_at', $qb->createNamedParameter($timestamp, IQueryBuilder::PARAM_INT))
);
$qb->executeStatement();
}
/**
* Delete all conversations for a specific bot in a specific room
*
* @param int $botId
* @param string $roomToken
* @return void
*/
public function deleteByBotAndRoom(int $botId, string $roomToken): void {
$qb = $this->db->getQueryBuilder();
$qb->delete($this->getTableName())
->where(
$qb->expr()->eq('bot_id', $qb->createNamedParameter($botId, IQueryBuilder::PARAM_INT))
)
->andWhere(
$qb->expr()->eq('room_token', $qb->createNamedParameter($roomToken, IQueryBuilder::PARAM_STR))
);
$qb->executeStatement();
}
}