|
11 | 11 | try: |
12 | 12 | from fastapi import APIRouter, HTTPException, Depends |
13 | 13 | from pydantic import BaseModel, Field |
| 14 | + FASTAPI_AVAILABLE = True |
14 | 15 | except ImportError: |
15 | 16 | # Fallback for environments without FastAPI |
16 | 17 | APIRouter = None |
17 | 18 | HTTPException = None |
18 | 19 | BaseModel = object |
19 | | - Field = None |
| 20 | + Field = lambda *args, **kwargs: None |
| 21 | + FASTAPI_AVAILABLE = False |
20 | 22 |
|
21 | 23 | logger = logging.getLogger(__name__) |
22 | 24 |
|
23 | 25 |
|
24 | 26 | # Request/Response Models |
25 | | -class AgentInvokeRequest(BaseModel): |
26 | | - """Request model for agent invocation.""" |
27 | | - message: str = Field(..., description="Message to send to the agent") |
28 | | - session_id: Optional[str] = Field(None, description="Optional session ID for conversation continuity") |
29 | | - agent_config: Optional[Dict[str, Any]] = Field(None, description="Optional agent configuration overrides") |
30 | | - |
31 | | - |
32 | | -class AgentInvokeResponse(BaseModel): |
33 | | - """Response model for agent invocation.""" |
34 | | - result: str = Field(..., description="Agent response") |
35 | | - session_id: str = Field(..., description="Session ID used for this conversation") |
36 | | - status: str = Field(default="success", description="Response status") |
37 | | - metadata: Optional[Dict[str, Any]] = Field(None, description="Optional response metadata") |
38 | | - |
39 | | - |
40 | | -class ErrorResponse(BaseModel): |
41 | | - """Error response model.""" |
42 | | - error: str = Field(..., description="Error message") |
43 | | - status: str = Field(default="error", description="Error status") |
44 | | - code: Optional[str] = Field(None, description="Error code") |
| 27 | +if FASTAPI_AVAILABLE: |
| 28 | + class AgentInvokeRequest(BaseModel): |
| 29 | + """Request model for agent invocation.""" |
| 30 | + message: str = Field(..., description="Message to send to the agent") |
| 31 | + session_id: Optional[str] = Field(None, description="Optional session ID for conversation continuity") |
| 32 | + agent_config: Optional[Dict[str, Any]] = Field(None, description="Optional agent configuration overrides") |
| 33 | + |
| 34 | + class AgentInvokeResponse(BaseModel): |
| 35 | + """Response model for agent invocation.""" |
| 36 | + result: str = Field(..., description="Agent response") |
| 37 | + session_id: str = Field(..., description="Session ID used for this conversation") |
| 38 | + status: str = Field(default="success", description="Response status") |
| 39 | + metadata: Optional[Dict[str, Any]] = Field(None, description="Optional response metadata") |
| 40 | + |
| 41 | + class ErrorResponse(BaseModel): |
| 42 | + """Error response model.""" |
| 43 | + error: str = Field(..., description="Error message") |
| 44 | + status: str = Field(default="error", description="Error status") |
| 45 | + code: Optional[str] = Field(None, description="Error code") |
| 46 | +else: |
| 47 | + # Simple dict-based fallbacks |
| 48 | + class AgentInvokeRequest: |
| 49 | + def __init__(self, message: str, session_id: Optional[str] = None, agent_config: Optional[Dict[str, Any]] = None): |
| 50 | + self.message = message |
| 51 | + self.session_id = session_id |
| 52 | + self.agent_config = agent_config |
| 53 | + |
| 54 | + class AgentInvokeResponse: |
| 55 | + def __init__(self, result: str, session_id: str, status: str = "success", metadata: Optional[Dict[str, Any]] = None): |
| 56 | + self.result = result |
| 57 | + self.session_id = session_id |
| 58 | + self.status = status |
| 59 | + self.metadata = metadata |
| 60 | + |
| 61 | + class ErrorResponse: |
| 62 | + def __init__(self, error: str, status: str = "error", code: Optional[str] = None): |
| 63 | + self.error = error |
| 64 | + self.status = status |
| 65 | + self.code = code |
45 | 66 |
|
46 | 67 |
|
47 | 68 | # Agent Registry |
@@ -74,7 +95,7 @@ def list_registered_agents() -> list: |
74 | 95 |
|
75 | 96 |
|
76 | 97 | # FastAPI Router (if FastAPI is available) |
77 | | -if APIRouter is not None: |
| 98 | +if FASTAPI_AVAILABLE and APIRouter is not None: |
78 | 99 | router = APIRouter(prefix="/api/v1", tags=["agents"]) |
79 | 100 |
|
80 | 101 | @router.post("/agents/{agent_id}/invoke") |
|
0 commit comments