ZARAI / backend /app /api /tools.py
Srajal2579
Deploy JARVIS
3e646d9
Raw History Blame Contribute Delete
1.8 kB
"""
JARVIS — Tools API Route
POST /api/tools/execute — run a tool directly
GET /api/tools/list — list available tools
"""
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
from typing import Optional, Any
from app.tools.tool_executor import execute_tool, TOOL_DEFINITIONS
router = APIRouter()
class ToolRequest(BaseModel):
tool_name: str
argument: str
class Config:
json_schema_extra = {
"example": {
"tool_name": "calculator",
"argument": "sqrt(144) + 2**8",
}
}
class ToolResponse(BaseModel):
success: bool
result: str
tool: str
argument: str
value: Optional[Any] = None
@router.post("/execute", response_model=ToolResponse)
async def execute_tool_endpoint(request: ToolRequest):
"""Execute a named tool with an argument."""
if request.tool_name not in TOOL_DEFINITIONS:
raise HTTPException(
status_code=400,
detail=f"Unknown tool '{request.tool_name}'. Available: {list(TOOL_DEFINITIONS.keys())}",
)
result = execute_tool(request.tool_name, request.argument)
return ToolResponse(
success=result.get("success", False),
result=result.get("result", ""),
tool=result.get("tool", request.tool_name),
argument=result.get("argument", request.argument),
value=result.get("value"),
)
@router.get("/list")
async def list_tools():
"""List all available tools with descriptions."""
return {
"tools": [
{
"name": t["name"],
"description": t["description"],
"example": t["example"],
}
for t in TOOL_DEFINITIONS.values()
]
}