refactor: use a single agent model path
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import uuid
|
||||
from collections.abc import AsyncIterator
|
||||
from contextlib import asynccontextmanager
|
||||
@@ -16,17 +15,6 @@ class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
|
||||
class ConversationMode(Base):
|
||||
__tablename__ = "agent_conversation_modes"
|
||||
__table_args__ = (UniqueConstraint("user_id", "chat_id"),)
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
user_id: Mapped[str] = mapped_column(String(128), index=True)
|
||||
chat_id: Mapped[str] = mapped_column(String(256), index=True)
|
||||
mode: Mapped[str] = mapped_column(String(16))
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(UTC))
|
||||
|
||||
|
||||
class RunEvent(Base):
|
||||
__tablename__ = "agent_run_events"
|
||||
|
||||
@@ -64,8 +52,6 @@ class RuntimeStore:
|
||||
def __init__(self, url: str) -> None:
|
||||
self.engine: AsyncEngine = create_async_engine(url, pool_pre_ping=True)
|
||||
self.sessions = async_sessionmaker(self.engine, expire_on_commit=False)
|
||||
self._mode_locks: dict[tuple[str, str], asyncio.Lock] = {}
|
||||
self._lock_guard = asyncio.Lock()
|
||||
|
||||
async def initialize(self) -> None:
|
||||
async with self.engine.begin() as connection:
|
||||
@@ -79,37 +65,6 @@ class RuntimeStore:
|
||||
async with self.sessions() as session:
|
||||
yield session
|
||||
|
||||
async def _mode_lock(self, user_id: str, chat_id: str) -> asyncio.Lock:
|
||||
key = (user_id, chat_id)
|
||||
async with self._lock_guard:
|
||||
return self._mode_locks.setdefault(key, asyncio.Lock())
|
||||
|
||||
async def select_mode(self, user_id: str, chat_id: str, requested: str) -> str:
|
||||
if requested not in {"chat", "work"}:
|
||||
raise ValueError("Invalid conversation mode")
|
||||
if not chat_id:
|
||||
return requested
|
||||
lock = await self._mode_lock(user_id, chat_id)
|
||||
async with lock:
|
||||
async with self.session() as session:
|
||||
row = await session.scalar(
|
||||
select(ConversationMode).where(
|
||||
ConversationMode.user_id == user_id,
|
||||
ConversationMode.chat_id == chat_id,
|
||||
)
|
||||
)
|
||||
if row is None:
|
||||
session.add(ConversationMode(user_id=user_id, chat_id=chat_id, mode=requested))
|
||||
await session.commit()
|
||||
return requested
|
||||
if row.mode == "work" and requested == "chat":
|
||||
return "work"
|
||||
if row.mode != requested:
|
||||
row.mode = requested
|
||||
row.updated_at = datetime.now(UTC)
|
||||
await session.commit()
|
||||
return row.mode
|
||||
|
||||
async def append_event(
|
||||
self,
|
||||
run_id: str,
|
||||
|
||||
Reference in New Issue
Block a user