adaptai / aiml /02_models /nova /plane /agents /agents_hub.py
ADAPT-Chase's picture
Add files using upload-large-folder tool
2021f39 verified
#!/usr/bin/env python3
"""
Nova Agents Hub: multi-agent orchestration with simple task queue.
Endpoints:
- POST /agents {name, role} -> id
- POST /tasks {agent_id, task, params?}
- GET /tasks/{agent_id} -> queue
Agents pull tasks and use router/gateway to execute (stubbed).
"""
import os
import uuid
from typing import Dict, Any, List
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
import requests
import uvicorn
PORT = int(os.getenv("PORT", "7016"))
ROUTER_BASE = os.getenv("ROUTER_BASE", "http://127.0.0.1:7000")
GATEWAY_BASE = os.getenv("GATEWAY_BASE", "http://127.0.0.1:8088")
app = FastAPI(title="Nova Agents Hub", version="0.1.0")
app.add_middleware(
CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"],
)
AGENTS: Dict[str, Dict[str, Any]] = {}
TASKS: Dict[str, List[Dict[str, Any]]] = {}
@app.get("/health")
def health():
return {"status": "ok", "port": PORT, "agents": len(AGENTS)}
@app.post("/agents")
async def create_agent(req: Request) -> JSONResponse:
body = await req.json()
aid = str(uuid.uuid4())
AGENTS[aid] = {"name": body.get("name", f"agent-{aid[:6]}"), "role": body.get("role", "worker")}
TASKS[aid] = []
return JSONResponse(status_code=200, content={"agent_id": aid})
@app.post("/tasks")
async def add_task(req: Request) -> JSONResponse:
body = await req.json()
aid = body.get("agent_id")
if aid not in AGENTS:
return JSONResponse(status_code=404, content={"error": "agent not found"})
TASKS[aid].append({"task": body.get("task"), "params": body.get("params", {})})
return JSONResponse(status_code=200, content={"queued": len(TASKS[aid])})
@app.get("/tasks/{agent_id}")
def list_tasks(agent_id: str):
return {"tasks": TASKS.get(agent_id, [])}
# Basic worker loop could be added here to auto-execute tasks
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=PORT)