File size: 3,861 Bytes
56e31ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Agent API Endpoints
New FastAPI router for Agent-based interactions
"""
from fastapi import APIRouter, HTTPException, UploadFile, File, Form
from pydantic import BaseModel
from typing import Optional, List, Dict, Any
import tempfile
import os
import logging

logger = logging.getLogger(__name__)

router = APIRouter(prefix="/api/agent", tags=["agent"])


class AgentChatRequest(BaseModel):
    """Request model for agent chat"""
    session_id: str
    message: str
    file_path: Optional[str] = None
    boundary_coords: Optional[List[List[float]]] = None


class AgentChatResponse(BaseModel):
    """Response model for agent chat"""
    status: str
    response: str
    tool_calls: List[Dict[str, Any]] = []
    session_id: str


@router.post("/chat", response_model=AgentChatResponse)
async def agent_chat(request: AgentChatRequest):
    """
    Chat with the REMB Agent
    
    The agent can:
    - Read and parse DXF files
    - Generate optimized layouts
    - Check regulatory compliance
    - Export results to DXF
    - Self-correct on errors
    """
    try:
        from src.agent import get_agent
        
        agent = get_agent()
        result = agent.invoke(
            user_input=request.message,
            session_id=request.session_id,
            file_path=request.file_path,
            boundary_coords=request.boundary_coords
        )
        
        return AgentChatResponse(**result)
        
    except ValueError as e:
        raise HTTPException(400, str(e))
    except Exception as e:
        logger.error(f"Agent chat error: {e}")
        raise HTTPException(500, f"Agent error: {str(e)}")


@router.post("/process-file")
async def agent_process_file(
    file: UploadFile = File(...),
    message: str = Form(default="Analyze this site and suggest optimal layouts"),
    session_id: str = Form(default="default")
):
    """
    Upload a CAD file and process with the agent
    
    The agent will:
    1. Parse the file
    2. Analyze the site
    3. Generate layout options
    4. Check compliance
    5. Return comprehensive results
    """
    if not file.filename:
        raise HTTPException(400, "No file provided")
    
    # Save uploaded file
    try:
        content = await file.read()
        suffix = ".dwg" if file.filename.lower().endswith(".dwg") else ".dxf"
        
        with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as tmp:
            tmp.write(content)
            tmp_path = tmp.name
        
        # Process with agent
        from src.agent import get_agent
        
        agent = get_agent()
        result = agent.invoke(
            user_input=f"[File uploaded: {file.filename}]\n{message}",
            session_id=session_id,
            file_path=tmp_path
        )
        
        # Clean up temp file
        try:
            os.unlink(tmp_path)
        except:
            pass
        
        return {
            "status": result.get("status"),
            "response": result.get("response"),
            "tool_calls": result.get("tool_calls", []),
            "session_id": session_id,
            "file_processed": file.filename
        }
        
    except Exception as e:
        logger.error(f"File processing error: {e}")
        raise HTTPException(500, f"Processing error: {str(e)}")


@router.get("/health")
async def agent_health():
    """Check agent status"""
    try:
        from src.agent import get_agent
        from src.tools import all_tools
        
        agent = get_agent()
        
        return {
            "status": "healthy",
            "agent_type": "ReAct",
            "model": agent.model_name,
            "tools_available": len(all_tools),
            "tool_names": [t.name for t in all_tools]
        }
    except Exception as e:
        return {
            "status": "error",
            "message": str(e)
        }