mirror of
https://github.com/microsoft/autogen.git
synced 2025-07-25 01:41:01 +00:00

* add skeleton worflow manager * add test notebook * update test nb * add sample team spec * refactor requirements to agentchat and ext * add base provider to return agentchat agents from json spec * initial api refactor, update dbmanager * api refactor * refactor tests * ags api tutorial update * ui refactor * general refactor * minor refactor updates * backend api refaactor * ui refactor and update * implement v1 for streaming connection with ui updates * backend refactor * ui refactor * minor ui tweak * minor refactor and tweaks * general refactor * update tests * sync uv.lock with main * uv lock update
115 lines
2.7 KiB
Python
115 lines
2.7 KiB
Python
# api/routes/sessions.py
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from typing import Dict
|
|
from ..deps import get_db
|
|
from ...datamodel import Session, Message
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/")
|
|
async def list_sessions(
|
|
user_id: str,
|
|
db=Depends(get_db)
|
|
) -> Dict:
|
|
"""List all sessions for a user"""
|
|
response = db.get(Session, filters={"user_id": user_id})
|
|
return {
|
|
"status": True,
|
|
"data": response.data
|
|
}
|
|
|
|
|
|
@router.get("/{session_id}")
|
|
async def get_session(
|
|
session_id: int,
|
|
user_id: str,
|
|
db=Depends(get_db)
|
|
) -> Dict:
|
|
"""Get a specific session"""
|
|
response = db.get(
|
|
Session,
|
|
filters={"id": session_id, "user_id": user_id}
|
|
)
|
|
if not response.status or not response.data:
|
|
raise HTTPException(status_code=404, detail="Session not found")
|
|
return {
|
|
"status": True,
|
|
"data": response.data[0]
|
|
}
|
|
|
|
|
|
@router.post("/")
|
|
async def create_session(
|
|
session: Session,
|
|
db=Depends(get_db)
|
|
) -> Dict:
|
|
"""Create a new session"""
|
|
response = db.upsert(session)
|
|
if not response.status:
|
|
raise HTTPException(status_code=400, detail=response.message)
|
|
return {
|
|
"status": True,
|
|
"data": response.data
|
|
}
|
|
|
|
|
|
@router.put("/{session_id}")
|
|
async def update_session(
|
|
session_id: int,
|
|
user_id: str,
|
|
session: Session,
|
|
db=Depends(get_db)
|
|
) -> Dict:
|
|
"""Update an existing session"""
|
|
# First verify the session belongs to user
|
|
existing = db.get(
|
|
Session,
|
|
filters={"id": session_id, "user_id": user_id}
|
|
)
|
|
if not existing.status or not existing.data:
|
|
raise HTTPException(status_code=404, detail="Session not found")
|
|
|
|
# Update the session
|
|
response = db.upsert(session)
|
|
if not response.status:
|
|
raise HTTPException(status_code=400, detail=response.message)
|
|
|
|
return {
|
|
"status": True,
|
|
"data": response.data,
|
|
"message": "Session updated successfully"
|
|
}
|
|
|
|
|
|
@router.delete("/{session_id}")
|
|
async def delete_session(
|
|
session_id: int,
|
|
user_id: str,
|
|
db=Depends(get_db)
|
|
) -> Dict:
|
|
"""Delete a session"""
|
|
response = db.delete(
|
|
filters={"id": session_id, "user_id": user_id},
|
|
model_class=Session
|
|
)
|
|
return {
|
|
"status": True,
|
|
"message": "Session deleted successfully"
|
|
}
|
|
|
|
|
|
@router.get("/{session_id}/messages")
|
|
async def list_messages(
|
|
session_id: int,
|
|
user_id: str,
|
|
db=Depends(get_db)
|
|
) -> Dict:
|
|
"""List all messages for a session"""
|
|
filters = {"session_id": session_id, "user_id": user_id}
|
|
response = db.get(Message, filters=filters, order="asc")
|
|
return {
|
|
"status": True,
|
|
"data": response.data
|
|
}
|