Spaces:
Sleeping
Sleeping
File size: 16,937 Bytes
27cde0c 54ec9cb 27cde0c 54ec9cb 27cde0c e497b8a 54ec9cb e497b8a 27cde0c 54ec9cb 27cde0c 54ec9cb 27cde0c 54ec9cb 27cde0c 54ec9cb 27cde0c 54ec9cb 27cde0c | 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 | """Agent management endpoints."""
import logging
from enum import Enum
from typing import Any
from uuid import uuid4
from fastapi import APIRouter, HTTPException, status
from pydantic import BaseModel, Field
router = APIRouter(prefix="/agents")
logger = logging.getLogger(__name__)
class AgentType(str, Enum):
"""Types of agents in the system."""
PLANNER = "planner"
NAVIGATOR = "navigator"
EXTRACTOR = "extractor"
VERIFIER = "verifier"
MEMORY = "memory"
COORDINATOR = "coordinator"
class AgentStatus(str, Enum):
"""Agent execution status."""
IDLE = "idle"
PLANNING = "planning"
EXECUTING = "executing"
WAITING = "waiting"
COMPLETED = "completed"
FAILED = "failed"
class AgentRunRequest(BaseModel):
"""Request to run an agent."""
agent_type: AgentType
episode_id: str
task_context: dict[str, Any] = Field(default_factory=dict)
observation: dict[str, Any] | None = None
config: dict[str, Any] = Field(default_factory=dict)
class AgentRunResponse(BaseModel):
"""Response from agent execution."""
run_id: str
agent_type: AgentType
status: AgentStatus
action: dict[str, Any] | None = None
reasoning: str | None = None
confidence: float | None = None
tokens_used: int = 0
execution_time_ms: float = 0.0
class PlanRequest(BaseModel):
"""Request for creating a plan."""
episode_id: str
task_description: str
current_state: dict[str, Any] = Field(default_factory=dict)
constraints: list[str] = Field(default_factory=list)
class PlanStep(BaseModel):
"""A single step in a plan."""
step_number: int
action_type: str
description: str
agent: AgentType
dependencies: list[int] = Field(default_factory=list)
estimated_cost: float = 0.0
class PlanResponse(BaseModel):
"""Response containing a generated plan."""
plan_id: str
episode_id: str
steps: list[PlanStep]
total_estimated_steps: int
reasoning: str
confidence: float
class AgentState(BaseModel):
"""Current state of an agent."""
agent_id: str
agent_type: AgentType
status: AgentStatus
current_task: str | None = None
messages_processed: int = 0
actions_taken: int = 0
last_action: dict[str, Any] | None = None
memory_snapshot: dict[str, Any] = Field(default_factory=dict)
class AgentModule(BaseModel):
"""Installable/browsable agent module definition."""
id: str
name: str
role: str
description: str
version: str
installed: bool
default: bool
orchestrator_compatible: bool = True
class AgentModuleAction(BaseModel):
"""Install/uninstall request for an agent module."""
agent_id: str
# Store for agent states
_agent_states: dict[str, AgentState] = {}
_AGENT_MODULE_CATALOG: list[dict[str, Any]] = [
{
"id": "planner-agent",
"name": "Planner Agent",
"role": "planner",
"description": "Creates scrape plans and execution strategy",
"version": "1.0.0",
"default": True,
"orchestrator_compatible": True,
},
{
"id": "navigator-agent",
"name": "Navigator Agent",
"role": "navigator",
"description": "Finds links and chooses crawl paths",
"version": "1.0.0",
"default": True,
"orchestrator_compatible": True,
},
{
"id": "extractor-agent",
"name": "Extractor Agent",
"role": "extractor",
"description": "Extracts structured data from fetched content",
"version": "1.0.0",
"default": True,
"orchestrator_compatible": True,
},
{
"id": "verifier-agent",
"name": "Verifier Agent",
"role": "verifier",
"description": "Validates extracted values and output quality",
"version": "1.0.0",
"default": True,
"orchestrator_compatible": True,
},
{
"id": "memory-agent",
"name": "Memory Agent",
"role": "memory",
"description": "Manages memory writes and retrieval",
"version": "1.0.0",
"default": True,
"orchestrator_compatible": True,
},
{
"id": "coordinator-agent",
"name": "Coordinator Agent",
"role": "coordinator",
"description": "Orchestrates multi-agent execution",
"version": "1.0.0",
"default": True,
"orchestrator_compatible": True,
},
{
"id": "research-agent",
"name": "Research Agent",
"role": "research",
"description": "Focused web search and source discovery",
"version": "1.0.0",
"default": False,
"orchestrator_compatible": True,
},
{
"id": "dataset-agent",
"name": "Dataset Builder Agent",
"role": "dataset",
"description": "Builds/normalizes datasets from scraped files",
"version": "1.0.0",
"default": False,
"orchestrator_compatible": True,
},
]
_DEFAULT_AGENT_MODULES: set[str] = {
item["id"] for item in _AGENT_MODULE_CATALOG if item.get("default")
}
_installed_agent_modules: set[str] = set(_DEFAULT_AGENT_MODULES)
@router.get(
"/list",
status_code=status.HTTP_200_OK,
summary="List all agents",
description="Get list of all available agents and their status",
)
async def list_agents() -> dict[str, Any]:
"""
List all available agents and their current states.
Returns:
Dictionary with agent types and their states.
"""
agent_types = [
{
"type": at.value,
"name": at.name.title(),
"description": f"{at.name.title()} agent for web scraping tasks",
}
for at in AgentType
]
active_agents = [
{
"agent_id": agent_id,
"type": state.agent_type,
"status": state.status,
}
for agent_id, state in _agent_states.items()
]
return {
"agent_types": agent_types,
"active_agents": active_agents,
"installed_agents": sorted(_installed_agent_modules),
"total_types": len(AgentType),
"active_count": len(_agent_states),
}
@router.post(
"/run",
response_model=AgentRunResponse,
status_code=status.HTTP_200_OK,
summary="Run an agent",
description="Execute an agent to produce an action",
)
async def run_agent(request: AgentRunRequest) -> AgentRunResponse:
"""
Run an agent to produce an action for the current observation.
Args:
request: Agent run configuration.
Returns:
AgentRunResponse: Result of agent execution.
"""
run_id = str(uuid4())
logger.info(f"Running {request.agent_type} agent for episode {request.episode_id}")
try:
# Import and instantiate the appropriate agent
from app.agents.coordinator import AgentCoordinator
coordinator = AgentCoordinator()
result = await coordinator.run_agent(
agent_type=request.agent_type,
episode_id=request.episode_id,
observation=request.observation,
config=request.config,
)
return AgentRunResponse(
run_id=run_id,
agent_type=request.agent_type,
status=AgentStatus.COMPLETED,
action=result.get("action"),
reasoning=result.get("reasoning"),
confidence=result.get("confidence"),
tokens_used=result.get("tokens_used", 0),
execution_time_ms=result.get("execution_time_ms", 0.0),
)
except Exception as e:
logger.error(f"Agent execution failed: {e}")
return AgentRunResponse(
run_id=run_id,
agent_type=request.agent_type,
status=AgentStatus.FAILED,
reasoning=str(e),
)
@router.post(
"/plan",
response_model=PlanResponse,
status_code=status.HTTP_200_OK,
summary="Generate a plan",
description="Use the planner agent to generate an execution plan",
)
async def generate_plan(request: PlanRequest) -> PlanResponse:
"""
Generate a plan for completing a task.
Args:
request: Planning request with task details.
Returns:
PlanResponse: Generated plan with steps.
"""
plan_id = str(uuid4())
logger.info(f"Generating plan for episode {request.episode_id}")
steps = [
PlanStep(
step_number=1,
action_type="create_plan",
description=f"Analyze task goal: {request.task_description}",
agent=AgentType.PLANNER,
estimated_cost=0.001,
),
PlanStep(
step_number=2,
action_type="navigate",
description="Navigate to target pages and gather context",
agent=AgentType.NAVIGATOR,
dependencies=[1],
estimated_cost=0.01,
),
PlanStep(
step_number=3,
action_type="extract_field",
description="Extract required fields from observed content",
agent=AgentType.EXTRACTOR,
dependencies=[2],
estimated_cost=0.02,
),
PlanStep(
step_number=4,
action_type="verify_field",
description="Validate extracted fields against constraints",
agent=AgentType.VERIFIER,
dependencies=[3],
estimated_cost=0.005,
),
]
if request.constraints:
steps.append(
PlanStep(
step_number=len(steps) + 1,
action_type="apply_constraints",
description=f"Apply constraints: {', '.join(request.constraints)}",
agent=AgentType.PLANNER,
dependencies=[4],
estimated_cost=0.001,
)
)
return PlanResponse(
plan_id=plan_id,
episode_id=request.episode_id,
steps=steps,
total_estimated_steps=len(steps),
reasoning="Generated a deterministic multi-agent plan for navigation, extraction, and verification.",
confidence=0.82,
)
@router.get(
"/state/{agent_id}",
response_model=AgentState,
status_code=status.HTTP_200_OK,
summary="Get agent state",
description="Get the current state of an agent",
)
async def get_agent_state(agent_id: str) -> AgentState:
"""
Get the current state of an agent.
Args:
agent_id: ID of the agent.
Returns:
AgentState: Current agent state.
"""
if agent_id not in _agent_states:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Agent {agent_id} not found",
)
return _agent_states[agent_id]
@router.get(
"/types/",
status_code=status.HTTP_200_OK,
summary="Get agent types",
description="Get all available agent types",
)
async def get_agent_types() -> dict[str, list[dict[str, str]]]:
"""
Get available agent types with descriptions.
Returns:
Dict with agent type information.
"""
agent_info = [
{"type": AgentType.PLANNER.value, "description": "Creates execution plans for tasks"},
{"type": AgentType.NAVIGATOR.value, "description": "Handles page navigation and URL management"},
{"type": AgentType.EXTRACTOR.value, "description": "Extracts data from web pages"},
{"type": AgentType.VERIFIER.value, "description": "Validates extracted data"},
{"type": AgentType.MEMORY.value, "description": "Manages memory operations"},
{"type": AgentType.COORDINATOR.value, "description": "Orchestrates multi-agent collaboration"},
]
return {"agents": agent_info}
@router.get(
"/catalog",
status_code=status.HTTP_200_OK,
summary="Get installable agents catalog",
description="List all agent modules with install status and orchestrator compatibility",
)
async def get_agent_catalog() -> dict[str, Any]:
"""Get catalog of agent modules available for installation."""
agents = [
AgentModule(
id=item["id"],
name=item["name"],
role=item["role"],
description=item["description"],
version=item["version"],
installed=item["id"] in _installed_agent_modules,
default=bool(item.get("default")),
orchestrator_compatible=bool(item.get("orchestrator_compatible", True)),
).model_dump()
for item in _AGENT_MODULE_CATALOG
]
return {
"agents": agents,
"stats": {
"total": len(agents),
"installed": len(_installed_agent_modules),
"available": len(agents) - len(_installed_agent_modules),
},
}
@router.get(
"/installed",
status_code=status.HTTP_200_OK,
summary="Get installed agent modules",
description="List currently installed agent modules",
)
async def get_installed_agents() -> dict[str, Any]:
"""Get installed agent module list."""
installed = []
for item in _AGENT_MODULE_CATALOG:
if item["id"] in _installed_agent_modules:
installed.append(
AgentModule(
id=item["id"],
name=item["name"],
role=item["role"],
description=item["description"],
version=item["version"],
installed=True,
default=bool(item.get("default")),
orchestrator_compatible=bool(item.get("orchestrator_compatible", True)),
).model_dump()
)
return {"agents": installed, "count": len(installed)}
@router.post(
"/install",
status_code=status.HTTP_200_OK,
summary="Install an agent module",
description="Install an available agent module for orchestration",
)
async def install_agent(action: AgentModuleAction) -> dict[str, Any]:
"""Install an agent module."""
selected = next((item for item in _AGENT_MODULE_CATALOG if item["id"] == action.agent_id), None)
if not selected:
raise HTTPException(status_code=404, detail=f"Agent module not found: {action.agent_id}")
if action.agent_id in _installed_agent_modules:
return {
"status": "already_installed",
"message": f"{selected['name']} is already installed",
"agent": {
**selected,
"installed": True,
},
}
_installed_agent_modules.add(action.agent_id)
return {
"status": "success",
"message": f"{selected['name']} installed successfully",
"agent": {
**selected,
"installed": True,
},
}
@router.post(
"/uninstall",
status_code=status.HTTP_200_OK,
summary="Uninstall an agent module",
description="Uninstall a non-default agent module",
)
async def uninstall_agent(action: AgentModuleAction) -> dict[str, Any]:
"""Uninstall an installed non-default agent module."""
selected = next((item for item in _AGENT_MODULE_CATALOG if item["id"] == action.agent_id), None)
if not selected:
raise HTTPException(status_code=404, detail=f"Agent module not found: {action.agent_id}")
if action.agent_id not in _installed_agent_modules:
return {
"status": "not_installed",
"message": f"{selected['name']} is not installed",
"agent": {
**selected,
"installed": False,
},
}
if action.agent_id in _DEFAULT_AGENT_MODULES:
raise HTTPException(
status_code=400,
detail=f"Cannot uninstall default agent module: {selected['name']}",
)
_installed_agent_modules.discard(action.agent_id)
return {
"status": "success",
"message": f"{selected['name']} uninstalled successfully",
"agent": {
**selected,
"installed": False,
},
}
@router.post(
"/message",
status_code=status.HTTP_200_OK,
summary="Send inter-agent message",
description="Send a message between agents",
)
async def send_agent_message(
from_agent: str,
to_agent: str,
message_type: str,
content: dict[str, Any],
) -> dict[str, Any]:
"""
Send a message between agents.
Args:
from_agent: Source agent ID.
to_agent: Target agent ID.
message_type: Type of message.
content: Message content.
Returns:
Acknowledgment of message delivery.
"""
message_id = str(uuid4())
logger.info(f"Message {message_id}: {from_agent} -> {to_agent} ({message_type})")
# In production, this would go through a message broker
return {
"message_id": message_id,
"status": "delivered",
"from": from_agent,
"to": to_agent,
"type": message_type,
}
|