Spaces:
Build error
Build error
Download backend/routers/agent.py from codeninja3d/agentctl: direct link, hf CLI and curl.
- Browser
- Download file 1.33 kB
-
https://huggingface.co/spaces/codeninja3d/agentctl/resolve/main/backend/routers/agent.py
- Command line
-
hf download hf://spaces/codeninja3d/agentctl/backend/routers/agent.py
-
curl -L -o agent.py https://huggingface.co/spaces/codeninja3d/agentctl/resolve/main/backend/routers/agent.py
1.33 kB
| # routers/agent.py | |
| from fastapi import APIRouter, HTTPException | |
| from backend.services.agent_service import agent_service | |
| from agentctl.k8s_client import K8sClient | |
| router = APIRouter() | |
| async def run_agent(payload: dict): | |
| """ | |
| Agentic flow: | |
| 1. LLM → Plan → YAML (fallback: K8sAgent) | |
| 2. Apply YAML to Kubernetes | |
| 3. Return yaml, mode, model, result | |
| """ | |
| instruction = payload.get("instruction") | |
| namespace = payload.get("namespace") | |
| kind = payload.get("kind") | |
| if not instruction: | |
| raise HTTPException(status_code=400, detail="Missing instruction") | |
| # 1. Generate YAML (LLM + fallback) → returns dict | |
| agent_output = await agent_service.generate_yaml( | |
| instruction, | |
| namespace, | |
| kind | |
| ) | |
| yaml_text = agent_output["yaml"] | |
| mode = agent_output["mode"] | |
| model = agent_output["model"] | |
| # 2. Apply YAML to K8s | |
| client = K8sClient() | |
| try: | |
| apply_result = client.apply_manifest(yaml_text) | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=f"K8s apply failed: {e}") | |
| # 3. Return everything to UI | |
| return { | |
| "instruction": instruction, | |
| "yaml": yaml_text, | |
| "mode": mode, | |
| "model": model, | |
| "result": apply_result | |
| } | |