Chat Threads, ConversationAgent (WIP)

This commit is contained in:
Shivering Ocelot committed 2025-06-16 19:59:37 -05:00
1 parent 22fe5619d8
commit e8d80c5495
6 files changed
+519 -46

No files matched your search

+64 -3
View File
@@ -12,6 +12,7 @@ import replicate
import database as db
import agent as agent_mod
from server import start_server
from chat import ChatView
MESSAGE_PAGE_SIZE = 32
@@ -36,9 +37,11 @@ class App:
# `Agent` is the kind 'basic', will be class `BasicAgent` in the future
self.agent_classes = [
agent_mod.BasicAgent,
agent_mod.ConversationAgent,
agent_mod.Llama8bAgent,
agent_mod.CoderV1Agent,
agent_mod.ToolUsingAgent,
agent_mod.DebugAgent,
]
def sql_session(self):
@@ -172,8 +175,22 @@ class App:
"time": message.time,
"sender": message.sender,
"kind": message.kind,
"content": message.content
"content": message.content,
"threadId": message.thread_id,
} for message in messages]
def create_chat_thread(self, chat_id, name=None):
with self.sql_session() as session:
thread = db.Chat()
thread.chat_id = chat_id
thread.name = name
session.add(thread)
session.commit()
print(f"Created thread #{thread.id}: {thread.name}")
thread_id = thread.id
return thread_id
def verify_user_credentials(self, username, password):
with self.sql_session() as session:
@@ -217,7 +234,7 @@ class App:
print(f"Added agent #{agent_id} to chat #{chat_id}")
# If an integer is passed in sender, it is interpreted as an agent id
def send_chat_message(self, chat_id, content, sender='user'):
def send_chat_message(self, chat_id, content, sender='user', thread_id=None):
with self.sql_session() as session:
message = db.Message()
message.chat_id = chat_id
@@ -225,6 +242,7 @@ class App:
message.sender = sender
message.kind = 'text' # default kind
message.content = content
message.thread_id = thread_id
session.add(message)
session.commit()
@@ -252,6 +270,9 @@ class App:
def get_chat_context(self, chat_id):
return ChatContext(self.get_chat_messages(chat_id))
def get_chat_view(self, chat_id):
return ChatView(self, chat_id)
def get_agent_control(self, agent_id):
with self.sql_session() as session:
agent = session.execute(
@@ -310,7 +331,8 @@ class App:
'time': message.time,
'sender': message.sender,
'kind': message.kind,
'content': message.content
'content': message.content,
'threadId': message.thread_id,
}
for channel in self.user_connections[user_id]:
asyncio.create_task(channel.put(user_message))
@@ -337,6 +359,45 @@ class App:
picture=None,
members=[agent_id],
)
def create_chat_thread(self, chat_id, name=None):
with self.sql_session() as session:
thread = db.Thread()
thread.chat_id = chat_id
thread.name = name
session.add(thread)
session.commit()
print(f"Created thread #{thread.id}")
return thread.id
def get_agent_resource(self, agent_id, kind, key):
with self.sql_session() as session:
stored_value = session.execute(select(db.AgentResource.value).where(
db.AgentResource.agent_id == agent_id and db.AgentResource.kind == kind and db.AgentResource.key == key
)).scalar()
return stored_value
def save_agent_resource(self, agent_id, kind, key, value):
with self.sql_session() as session:
existing_resource = session.execute(select(db.AgentResource).where(
db.AgentResource.agent_id == agent_id and db.AgentResource.kind == kind and db.AgentResource.key == key
)).scalars().first()
if existing_resource:
existing_resource.value = value
else:
new_resource = db.AgentResource()
new_resource.agent_id = agent_id
new_resource.kind = kind
new_resource.key = key
new_resource.value = value
session.add(new_resource)
session.commit()
print(f"Saved agent resource: {agent_id} - {kind} - {key} = {value}")
if __name__ == "__main__":
if len(sys.argv) > 1: