Temporary agents and chats

This commit is contained in:
Shivering Ocelot committed 2026-04-16 15:16:03 -05:00
1 parent fe596761dc
commit e07f8ee05f
15 files changed
+447 -172

No files matched your search

+44 -11
View File
@@ -13,6 +13,7 @@ import replicate
import database as db
import agent as agent_mod
import agents as agents_mod
import temporary as tmp
from server import start_server
from chat import ChatView, Message, MESSAGE_PAGE_SIZE
@@ -25,6 +26,8 @@ class App(ChatMixin):
self.remote_connections = {}
self.streams = {}
self.tmp = tmp.State()
# Clients
self.qdrant_client = QdrantClient(path=f"{self.data_directory}/qdrant_data")
@@ -67,9 +70,41 @@ class App(ChatMixin):
# --- Agent Methods (Asynchronous) ---
async def create_agent(self, user_id, name, agentname, description, picture, kind, data, main_chat_id=None):
async def get_agent(self, agent_id):
if tmp.is_tmp_id(agent_id):
agent = self.tmp.agents.get(agent_id)
else:
agent = await db.Agent.get_or_none(id=agent_id)
if not agent:
raise Exception(f"Agent not found with id {agent_id}")
return agent
async def create_agent(self, user_id, name, agentname, description, picture, kind, data, main_chat_id=None, temporary=False):
print("create_agent; user_id, name:", user_id, name)
if temporary:
agent = tmp.Agent.create(
self,
user_id=user_id,
name=name,
agentname=agentname,
main_chat_id=main_chat_id,
description=description,
picture=picture,
kind=kind,
data=data or {}
)
if main_chat_id is not None:
if tmp.is_tmp_id(main_chat_id):
self.tmp.chats[main_chat_id].participants.append(agent.id)
else:
print(f"ERROR: cannot use {main_chat_id} as main chat for temporary agent {agentname or agent.id}")
agent.main_chat_id = None
return agent.id
if main_chat_id is None:
chat = await db.Chat.create(
user_id=user_id,
@@ -97,9 +132,7 @@ class App(ChatMixin):
return agent.id
async def update_agent(self, agent_id, name=None, description=None, picture=None, agentname=None, data=None):
agent = await db.Agent.get_or_none(id=agent_id)
if not agent:
raise Exception(f"Agent not found with id {agent_id}")
agent = await self.get_agent(agent_id)
summary = []
if name is not None:
@@ -120,11 +153,15 @@ class App(ChatMixin):
agent.data.update(data)
summary.extend([f"data.{k}" for k in data.keys()])
await agent.save()
if db.is_instance(agent):
await agent.save()
print(f"Updated agent #{agent.id} fields: {summary}")
async def get_agents(self, user_id):
agents = await db.Agent.filter(user_id=user_id).all()
if user_id in self.tmp.user_agents:
agents += [self.tmp.agents[agent_id] for agent_id in self.tmp.user_agents[user_id]]
return [{
"id": agent.id,
"name": agent.name,
@@ -176,9 +213,7 @@ class App(ChatMixin):
# --- Agent Control & Actions ---
async def get_agent_control(self, agent_id):
agent = await db.Agent.get_or_none(id=agent_id)
if not agent:
raise Exception(f"Agent not found with id {agent_id}")
agent = await self.get_agent(agent_id)
agent_class = next((kind for kind in self.agent_classes if kind.identifier == agent.kind), None)
if agent_class is None:
@@ -189,9 +224,7 @@ class App(ChatMixin):
return control
async def get_agent_config_fields(self, agent_id):
agent = await db.Agent.get_or_none(id=agent_id)
if not agent:
raise Exception(f"Agent not found with id {agent_id}")
agent = await self.get_agent(agent_id)
agent_class = next((it for it in self.agent_classes if it.identifier == agent.kind), None)
if not agent_class: