sirus / backend /SQL_Agent /agentOS_crud.md
ranilmukesh's picture
Deploy SiRUS SQL Agent backend
a8c9ee8
Let me search for how Agno handles session storage, chat history retrieval, and session management.Great news — Agno already stores everything you need in your `SqliteDb`. Here's how to build the full CRUD for your custom setup:
---
## 1. Retrieve Chat History (for frontend)
Since you already have `db=agent_db` on your agent, all sessions/runs are persisted in `agent_sessions.db`. You can retrieve them programmatically:
```python
# Get a session with all runs and messages
session = gemini_sql_agent.get_session(session_id="your-session-id")
print(session.runs) # All runs with messages
# Get just user+assistant messages (for chat UI)
chat_history = gemini_sql_agent.get_chat_history(session_id="your-session-id")
```
## 2. Add Custom CRUD Endpoints to Your App
Add these routes to your `agentOS_app` just like you did with `/tenant-run`:
```python
# LIST sessions for a user
@app.get("/chats/{user_id}")
async def list_user_sessions(user_id: str):
session = gemini_sql_agent.get_session(session_id=None) # won't work directly
# Instead, query the DB directly:
all_sessions = agent_db.get_all_sessions(user_id=user_id)
return {"sessions": all_sessions}
# GET chat history for a session
@app.get("/chats/{user_id}/{session_id}")
async def get_chat(user_id: str, session_id: str):
chat = gemini_sql_agent.get_chat_history(session_id=session_id)
return {"messages": [{"role": m.role, "content": m.content} for m in chat]}
# DELETE a session (and all its runs)
@app.delete("/chats/{session_id}")
async def delete_chat(session_id: str):
agent_db.delete_session(session_id=session_id)
return {"status": "deleted"}
# RENAME a session
@app.post("/chats/{session_id}/rename")
async def rename_chat(session_id: str, name: str = Body(...)):
gemini_sql_agent.set_session_name(session_id=session_id, session_name=name)
return {"status": "renamed"}
# CANCEL a running agent
@app.post("/chats/{session_id}/cancel/{run_id}")
async def cancel_run(session_id: str, run_id: str):
success = gemini_sql_agent.cancel_run(run_id)
return {"cancelled": success}
```
## 3. AgentOS Built-in REST Endpoints
Your `AgentOS` already exposes session CRUD endpoints automatically:
| Operation | Endpoint |
|-----------|----------|
| List sessions | `GET /sessions?user_id=X&type=agent` |
| Get session | `GET /sessions/{session_id}?type=agent` |
| Get runs | `GET /sessions/{session_id}/runs?type=agent` |
| Delete session | `DELETE /sessions/{session_id}` |
| Rename | `POST /sessions/{session_id}/rename` |
| Cancel run | `POST /agents/{agent_id}/runs/{run_id}/cancel` |
These are already available on your `agentOS_app` — you can call them from your frontend directly.
Want to learn more? These pages may help:
```suggestions
(Session Storage & Retrieval)[/database/session-storage]
(Session Management (IDs, naming, caching))[/sessions/session-management]
(Cancel Agent Run)[/run-cancellation/agent-cancel-run]
```