File size: 1,325 Bytes
e5dfbda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3642af
e5dfbda
 
 
 
 
 
 
 
 
d3642af
 
e5dfbda
 
 
 
 
d3642af
 
 
 
e5dfbda
 
 
 
 
 
 
d3642af
e5dfbda
 
 
d3642af
 
e5dfbda
 
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
# routers/agent.py

from fastapi import APIRouter, HTTPException
from backend.services.agent_service import agent_service
from agentctl.k8s_client import K8sClient

router = APIRouter()


@router.post("/agent")
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
    }