feat: Add presence heartbeat for Matrix online status

- matrix-gateway: POST /internal/matrix/presence/online endpoint
- usePresenceHeartbeat hook with activity tracking
- Auto away after 5 min inactivity
- Offline on page close/visibility change
- Integrated in MatrixChatRoom component
This commit is contained in:
Apple
2025-11-27 00:19:40 -08:00
parent 5bed515852
commit 3de3c8cb36
6371 changed files with 1317450 additions and 932 deletions

View File

@@ -0,0 +1,353 @@
# ✅ Backend для кабінетів агентів - Завершено
**Дата:** 2025-11-23
**Статус:** ✅ Готово до використання
---
## 🎉 Що реалізовано
### ✅ 1. Створено Agent Cabinet Service
**Розташування:** `services/agent-cabinet-service/`
**Структура:**
```
services/agent-cabinet-service/
├── app/
│ └── main.py # FastAPI додаток з усіма endpoints
├── Dockerfile # Docker образ
├── requirements.txt # Python залежності
└── README.md # Документація
```
**Технології:**
- FastAPI для REST API
- Pydantic для валідації даних
- In-memory storage (можна замінити на PostgreSQL)
- Інтеграція з DAGI Router для чату
---
### ✅ 2. API Endpoints
#### 2.1. Health Check
```http
GET /health
```
#### 2.2. Отримати метрики агента
```http
GET /api/agent/{agent_id}/metrics
```
**Response:**
```json
{
"agent_id": "yaromir",
"agent_name": "Яромир",
"status": "active",
"uptime_hours": 150.5,
"total_requests": 2500,
"successful_requests": 2400,
"failed_requests": 100,
"avg_response_time_ms": 450.5,
"last_active": "2025-11-23T12:00:00Z",
"model": "qwen3:8b",
"model_backend": "ollama",
"node": "node-1",
"is_orchestrator": true,
"team_size": 4,
"sub_agents": [
{
"id": "vozhd",
"name": "Вождь",
"role": "Strategic Guardian"
}
]
}
```
#### 2.3. Отримати CrewAI команди
```http
GET /api/agent/{agent_id}/crews
```
**Response:**
```json
[
{
"id": "crew-yaromir-1",
"name": "Yaromir Team",
"agents": [
{
"id": "vozhd",
"name": "Вождь",
"role": "Strategic Guardian"
}
],
"tasks": [
{
"id": "task-1",
"description": "Стратегічне планування",
"status": "completed"
}
],
"status": "active",
"created_at": "2025-11-23T10:00:00Z"
}
]
```
#### 2.4. Стати оркестратором
```http
POST /api/agent/{agent_id}/become-orchestrator
```
**Response:**
```json
{
"status": "success",
"agent_id": "yaromir",
"is_orchestrator": true
}
```
#### 2.5. Створити CrewAI команду
```http
POST /api/agent/{agent_id}/crews/create
Content-Type: application/json
{
"crew_name": "My Crew",
"agents": [
{
"id": "vozhd",
"name": "Вождь",
"role": "Strategic Guardian"
}
],
"tasks": [
{
"id": "task-1",
"description": "Стратегічне планування",
"status": "pending"
}
]
}
```
#### 2.6. Додати агента до команди
```http
POST /api/agent/{agent_id}/add-sub-agent
Content-Type: application/json
{
"id": "vozhd",
"name": "Вождь",
"role": "Strategic Guardian"
}
```
#### 2.7. Видалити агента з команди
```http
POST /api/agent/{agent_id}/remove-sub-agent?sub_agent_id=vozhd
```
#### 2.8. Чат з агентом
```http
POST /api/agent/{agent_id}/chat
Content-Type: application/json
{
"message": "Hello, how are you?"
}
```
**Response:**
```json
{
"status": "success",
"reply": "Hello! I'm doing well...",
"agent_id": "yaromir"
}
```
---
### ✅ 3. Інтеграція з Docker Compose
**Додано до `docker-compose.yml`:**
```yaml
agent-cabinet:
build:
context: ./services/agent-cabinet-service
dockerfile: Dockerfile
container_name: dagi-agent-cabinet
ports:
- "8898:8898"
environment:
- ROUTER_URL=http://router:9102
networks:
- dagi-network
restart: unless-stopped
```
---
### ✅ 4. Оновлено Frontend
**Файл:** `src/pages/AgentCabinetPage.tsx`
**Зміни:**
- Оновлено `API_BASE_URL` для використання нового сервісу
- Всі запити тепер йдуть до `http://localhost:8898`
---
## 🚀 Як запустити
### Локальний запуск
```bash
cd services/agent-cabinet-service
pip install -r requirements.txt
python -m uvicorn app.main:app --host 0.0.0.0 --port 8898 --reload
```
### Docker Compose
```bash
docker-compose up -d agent-cabinet
```
### Перевірка
```bash
# Health check
curl http://localhost:8898/health
# Метрики агента
curl http://localhost:8898/api/agent/yaromir/metrics
# Стати оркестратором
curl -X POST http://localhost:8898/api/agent/yaromir/become-orchestrator
```
---
## 📊 Архітектура
```
Frontend (React)
Agent Cabinet Service (FastAPI, порт 8898)
DAGI Router (порт 9102) ──► LLM Providers
Memory Service (порт 8000) ──► PostgreSQL
```
---
## 🔧 Налаштування
### Environment Variables
- `ROUTER_URL` - URL DAGI Router (default: `http://localhost:9102`)
### Frontend Environment Variables
Додати до `.env`:
```bash
VITE_AGENT_CABINET_URL=http://localhost:8898
```
---
## 📝 Наступні кроки (опціонально)
### 1. Інтеграція з базою даних
Замінити in-memory storage на PostgreSQL:
```python
# Додати до requirements.txt
sqlalchemy>=2.0.0
psycopg2-binary>=2.9.0
# Створити моделі
from sqlalchemy import Column, String, Integer, Float, Boolean, DateTime
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class AgentMetricsModel(Base):
__tablename__ = "agent_metrics"
agent_id = Column(String, primary_key=True)
# ... інші поля
```
### 2. Реальні метрики з Prometheus
```python
from prometheus_client import Counter, Histogram
requests_total = Counter('agent_requests_total', 'Total requests', ['agent_id'])
response_time = Histogram('agent_response_time_ms', 'Response time', ['agent_id'])
```
### 3. CrewAI інтеграція
Підключити до реального CrewAI сервісу:
```python
CREWAI_URL = os.getenv("CREWAI_URL", "http://localhost:9010")
async def create_crew_in_crewai(crew_config):
async with httpx.AsyncClient() as client:
response = await client.post(
f"{CREWAI_URL}/crews/create",
json=crew_config
)
return response.json()
```
### 4. Автоматичне створення кабінету
Додати endpoint для реєстрації нового агента:
```python
@app.post("/api/agent/register")
async def register_agent(agent_data: AgentRegistration):
"""Автоматично створює кабінет при реєстрації агента"""
# Створити базові метрики
# Зберегти в базу даних
# Повернути підтвердження
```
---
## ✅ Статус
**Готово:**
- ✅ FastAPI сервіс створено
-Всі endpoints реалізовано
- ✅ Інтеграція з Docker Compose
- ✅ Frontend оновлено
- ✅ Документація створена
**Потрібно реалізувати (опціонально):**
- ⏳ Інтеграція з PostgreSQL
- ⏳ Реальні метрики з Prometheus
- ⏳ CrewAI інтеграція
- ⏳ Автоматичне створення кабінету
---
**Backend для кабінетів агентів готовий до використання!** 🎉
**URL:** `http://localhost:8898`
**Health:** `http://localhost:8898/health`
**Docs:** `http://localhost:8898/docs` (Swagger UI)