File size: 1,968 Bytes
0d3f7cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""Agent router."""

from __future__ import annotations

import logging

from fastapi import APIRouter, Depends, HTTPException
from pydantic import BaseModel, Field

from hermes.agents.orchestrator.agent import OrchestratorAgent
from hermes.api.middleware import sanitize_input
from hermes.core.auth import get_api_key_dependency

logger = logging.getLogger(__name__)

router = APIRouter(dependencies=[Depends(get_api_key_dependency)])


class RunAgentRequest(BaseModel):
    task: str = Field(..., max_length=2000, description="Task to execute")
    strategy: str = Field(default="react", description="Reasoning strategy")


class AgentResponse(BaseModel):
    status: str
    result: str
    steps: int


orchestrator = OrchestratorAgent()


@router.post("/run-agent", response_model=AgentResponse)
async def run_agent(request: RunAgentRequest) -> AgentResponse:
    task = sanitize_input(request.task, max_length=2000)
    try:
        report = await orchestrator.execute_research(task)
        return AgentResponse(
            status="completed",
            result=report.summary,
            steps=len(report.findings),
        )
    except Exception as e:
        logger.error(f"Agent execution failed: {e}", exc_info=True)
        raise HTTPException(status_code=500, detail="Agent execution failed") from e


@router.get("/agents")
async def list_agents() -> dict:
    return {
        "agents": [
            {"type": "orchestrator", "description": "Coordinates multi-agent workflows"},
            {"type": "research", "description": "Conducts research and information gathering"},
            {"type": "code_analysis", "description": "Analyzes code quality and patterns"},
            {"type": "security", "description": "Performs security scans and vulnerability analysis"},
            {"type": "planning", "description": "Creates implementation plans"},
            {"type": "report", "description": "Generates comprehensive reports"},
        ]
    }