initial commit
This commit is contained in:
commit
aaba8753ef
36 changed files with 3682 additions and 0 deletions
111
src/services/session_service.py
Normal file
111
src/services/session_service.py
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
import httpx
|
||||
import uuid
|
||||
from typing import Optional, Dict, Any, List
|
||||
from ..config import settings
|
||||
|
||||
class SessionService:
|
||||
"""Service for managing chat sessions with talestorm-ai"""
|
||||
|
||||
def __init__(self):
|
||||
self.base_url = settings.TALESTORM_API_BASE_URL
|
||||
self.api_key = settings.TALESTORM_API_KEY
|
||||
self.agent_id = settings.TALESTORM_AGENT_ID
|
||||
|
||||
async def get_client(self) -> httpx.AsyncClient:
|
||||
"""Get HTTP client for talestorm-ai API"""
|
||||
headers = {}
|
||||
if self.api_key:
|
||||
headers["X-API-Key"] = self.api_key
|
||||
|
||||
return httpx.AsyncClient(
|
||||
base_url=self.base_url,
|
||||
headers=headers
|
||||
)
|
||||
|
||||
async def list_agents(self) -> List[Dict[str, Any]]:
|
||||
"""List available agents from talestorm-ai"""
|
||||
async with await self.get_client() as client:
|
||||
try:
|
||||
response = await client.get("/agents/")
|
||||
if response.status_code == 200:
|
||||
return response.json()
|
||||
else:
|
||||
return []
|
||||
except Exception:
|
||||
return []
|
||||
|
||||
async def get_default_agent(self) -> Optional[Dict[str, Any]]:
|
||||
"""Get the configured agent or the first available agent"""
|
||||
# First try to get the configured agent
|
||||
if self.agent_id:
|
||||
async with await self.get_client() as client:
|
||||
try:
|
||||
response = await client.get(f"/agents/{self.agent_id}")
|
||||
if response.status_code == 200:
|
||||
return response.json()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Fallback to first available agent
|
||||
agents = await self.list_agents()
|
||||
if agents:
|
||||
return agents[0]
|
||||
|
||||
return None
|
||||
|
||||
async def create_session(self, agent_id: Optional[str] = None) -> Optional[str]:
|
||||
"""Create a new chat session in talestorm-ai"""
|
||||
async with await self.get_client() as client:
|
||||
try:
|
||||
# Use provided agent_id, then configured agent_id, then fallback to default
|
||||
if not agent_id:
|
||||
if self.agent_id:
|
||||
agent_id = self.agent_id
|
||||
else:
|
||||
default_agent = await self.get_default_agent()
|
||||
if not default_agent:
|
||||
# Create a simple session ID for now
|
||||
return str(uuid.uuid4())
|
||||
agent_id = str(default_agent["id"])
|
||||
|
||||
# Create session with talestorm-ai
|
||||
response = await client.post("/sessions/", params={"agent_id": agent_id})
|
||||
if response.status_code == 200:
|
||||
session_data = response.json()
|
||||
return str(session_data["id"])
|
||||
else:
|
||||
# Fallback to local session ID
|
||||
return str(uuid.uuid4())
|
||||
except Exception:
|
||||
# Fallback to local session ID
|
||||
return str(uuid.uuid4())
|
||||
|
||||
async def get_session(self, session_id: str) -> Optional[Dict[str, Any]]:
|
||||
"""Get session details from talestorm-ai"""
|
||||
async with await self.get_client() as client:
|
||||
try:
|
||||
response = await client.get(f"/sessions/{session_id}")
|
||||
if response.status_code == 200:
|
||||
return response.json()
|
||||
return None
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
async def list_sessions(self) -> List[Dict[str, Any]]:
|
||||
"""List all sessions from talestorm-ai"""
|
||||
async with await self.get_client() as client:
|
||||
try:
|
||||
response = await client.get("/sessions/")
|
||||
if response.status_code == 200:
|
||||
return response.json()
|
||||
return []
|
||||
except Exception:
|
||||
return []
|
||||
|
||||
async def validate_session(self, session_id: str) -> bool:
|
||||
"""Validate if a session exists and is accessible"""
|
||||
session = await self.get_session(session_id)
|
||||
return session is not None
|
||||
|
||||
# Global session service instance
|
||||
session_service = SessionService()
|
||||
Loading…
Add table
Add a link
Reference in a new issue