2121namespace Fusio \Impl \Backend \View \Agent ;
2222
2323use Fusio \Engine \ContextInterface ;
24+ use Fusio \Impl \Backend \Filter \QueryFilter ;
2425use Fusio \Impl \Service ;
2526use Fusio \Impl \Table ;
2627use PSX \Nested \Builder ;
@@ -45,14 +46,18 @@ public function getCollection(int $agentId, ?string $chatId, ContextInterface $c
4546
4647 if (!empty ($ chatId )) {
4748 $ condition ->equals (Table \Generated \AgentMessageColumn::CHAT_ID , $ chatId );
49+
50+ $ count = $ this ->getTable (Table \Agent \Message::class)->getCount ($ condition );
51+ $ startIndex = max (0 , $ count - Service \Agent \Sender::CONTEXT_MESSAGES_LENGTH );
4852 $ sortOrder = OrderBy::ASC ;
4953 } else {
5054 $ condition ->equals (Table \Generated \AgentMessageColumn::CHILD , 0 );
55+
56+ $ startIndex = 0 ;
57+ $ count = QueryFilter::DEFAULT_LENGTH ;
5158 $ sortOrder = OrderBy::DESC ;
5259 }
5360
54- $ count = $ this ->getTable (Table \Agent \Message::class)->getCount ($ condition );
55- $ startIndex = max (0 , $ count - Service \Agent \Sender::CONTEXT_MESSAGES_LENGTH );
5661 $ sortBy = Table \Generated \AgentMessageColumn::ID ;
5762
5863 $ builder = new Builder ($ this ->connection );
@@ -63,6 +68,7 @@ public function getCollection(int $agentId, ?string $chatId, ContextInterface $c
6368 'itemsPerPage ' => Service \Agent \Sender::CONTEXT_MESSAGES_LENGTH ,
6469 'entry ' => $ builder ->doCollection ([$ this ->getTable (Table \Agent \Message::class), 'findBy ' ], [$ condition , $ startIndex , $ count , $ sortBy , $ sortOrder ], [
6570 'id ' => $ builder ->fieldInteger (Table \Generated \AgentMessageTable::COLUMN_ID ),
71+ 'chatId ' => Table \Generated \AgentMessageTable::COLUMN_CHAT_ID ,
6672 'role ' => $ builder ->fieldCallback (Table \Generated \AgentMessageTable::COLUMN_ORIGIN , function ($ value ) {
6773 return match ($ value ) {
6874 Table \Agent \Message::ORIGIN_ASSISTANT => 'assistant ' ,
0 commit comments