Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
from __future__ import annotations
|
|
|
|
import asyncio
|
|
from dataclasses import dataclass, field
|
|
from datetime import UTC, datetime
|
|
from typing import Any
|
|
|
|
|
|
@dataclass(slots=True)
|
|
class HermesSessionHandle:
|
|
conversation_id: str
|
|
user_id: str
|
|
hermes_session_id: str
|
|
last_used_at: datetime = field(default_factory=lambda: datetime.now(UTC))
|
|
restart_count: int = 0
|
|
lock: asyncio.Lock = field(default_factory=asyncio.Lock)
|
|
metadata: dict[str, Any] = field(default_factory=dict)
|
|
|
|
|
|
class HermesSessionManager:
|
|
def __init__(self) -> None:
|
|
self._sessions: dict[str, HermesSessionHandle] = {}
|
|
|
|
def get_or_create(self, *, conversation_id: str, user_id: str) -> HermesSessionHandle:
|
|
handle = self._sessions.get(conversation_id)
|
|
if handle is None:
|
|
handle = HermesSessionHandle(
|
|
conversation_id=conversation_id,
|
|
user_id=user_id,
|
|
hermes_session_id=f"jarvis-{conversation_id}",
|
|
)
|
|
self._sessions[conversation_id] = handle
|
|
handle.last_used_at = datetime.now(UTC)
|
|
return handle
|
|
|
|
|
|
hermes_session_manager = HermesSessionManager()
|