File size: 1,799 Bytes
3e646d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
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()
        ]
    }