Temporary agents and chats
This commit is contained in:
1 parent
fe596761dc
commit
e07f8ee05f
15 files changed
+447
-172
No files matched your search
+41
-14
@@ -3,17 +3,19 @@ from sqlalchemy.orm import Session
|
||||
from sqlalchemy.sql.expression import select
|
||||
|
||||
import database as db
|
||||
import temporary as tmp
|
||||
|
||||
MESSAGE_PAGE_SIZE = 256
|
||||
|
||||
class Message:
|
||||
def __init__(self, id, content, sender, time=None, kind='message', subkind=None):
|
||||
def __init__(self, id, content, sender, time=None, kind='message', subkind=None, thread_id=None):
|
||||
self.id = id
|
||||
self.content = content
|
||||
self.sender = sender
|
||||
self.time = time
|
||||
self.kind = kind
|
||||
self.subkind = subkind
|
||||
self.thread_id = thread_id
|
||||
|
||||
def from_db_record(record):
|
||||
return Message(record.id, record.content, record.sender, record.time)
|
||||
@@ -23,6 +25,16 @@ class Message:
|
||||
|
||||
def is_from_agent(self, agent_id):
|
||||
return self.sender == str(agent_id)
|
||||
|
||||
def to_json(self):
|
||||
return {
|
||||
"id": self.id,
|
||||
"time": self.time,
|
||||
"sender": self.sender,
|
||||
"kind": self.kind,
|
||||
"content": self.content,
|
||||
"threadId": self.thread_id,
|
||||
}
|
||||
|
||||
def __str__(self):
|
||||
time = self.time if self.time != None else 'no-time'
|
||||
@@ -67,14 +79,18 @@ class ChatView:
|
||||
self.current_thread_id = None
|
||||
|
||||
self._earliest_message_id = None
|
||||
self._earliest_message_time = None
|
||||
|
||||
async def init_load(self):
|
||||
await self._load_basic_data()
|
||||
await self._load_next_messages(MESSAGE_PAGE_SIZE)
|
||||
|
||||
async def _load_basic_data(self):
|
||||
# Using get_or_none for cleaner existence checks
|
||||
record = await db.Chat.get_or_none(id=self.chat_id)
|
||||
if tmp.is_tmp_id(self.chat_id):
|
||||
record = self.app.tmp.chats.get(self.chat_id)
|
||||
else:
|
||||
# Using get_or_none for cleaner existence checks
|
||||
record = await db.Chat.get_or_none(id=self.chat_id)
|
||||
|
||||
if not record:
|
||||
raise Exception(f"Chat not found with id {self.chat_id}")
|
||||
@@ -83,21 +99,32 @@ class ChatView:
|
||||
self.description = record.description
|
||||
|
||||
async def _load_next_messages(self, load_count):
|
||||
# Start building the filter
|
||||
filters = {"chat_id": self.chat_id}
|
||||
|
||||
# Add the 'earliest message' condition if it exists
|
||||
if self._earliest_message_id is not None:
|
||||
filters["id__lte"] = self._earliest_message_id
|
||||
if tmp.is_tmp_id(self.chat_id):
|
||||
chat = self.app.tmp.chats[self.chat_id]
|
||||
record_batch = [
|
||||
msg for msg in chat.messages
|
||||
if not self._earliest_message_time or msg.id < self._earliest_message_time
|
||||
][:load_count]
|
||||
record_batch.reverse()
|
||||
records_rev = record_batch
|
||||
else:
|
||||
# Start building the filter
|
||||
filters = {"chat_id": self.chat_id}
|
||||
|
||||
# Add the 'earliest message' condition if it exists
|
||||
if self._earliest_message_id is not None:
|
||||
filters["id__lte"] = self._earliest_message_id
|
||||
|
||||
# Tortoise query: filter, order, and limit
|
||||
# ordering by id is more robust than by time
|
||||
records_rev = await db.Message.filter(**filters).order_by("-id").limit(load_count)
|
||||
# Tortoise query: filter, order, and limit
|
||||
# ordering by id is more robust than by time
|
||||
records_rev = await db.Message.filter(**filters).order_by("-id").limit(load_count)
|
||||
|
||||
# Update the earliest id for the next load operation
|
||||
if records_rev:
|
||||
# Note: Since we ordered by -time (descending), index 0 is the newest in this batch
|
||||
self._earliest_message_id = records_rev[-1].id # The "earliest" is the one with the smallest ID/Time in this set
|
||||
# Note: Since we ordered by -time (descending), index 0 is the newest in this batch (highest id/time)
|
||||
# The "earliest" or oldest is the one with the smallest ID/Time in this set
|
||||
self._earliest_message_id = records_rev[-1].id
|
||||
self._earliest_message_time = records_rev[-1].time
|
||||
|
||||
if not self.threads:
|
||||
# current thread is the latest known from the newest record in batch
|
||||
|
||||
Reference in new issue
Block a user