-
Notifications
You must be signed in to change notification settings - Fork 0
Rest api #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rest api #21
Changes from all commits
c4b5912
89bda18
6fbcce4
914e931
5153106
ce56830
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,32 @@ | ||
| from pydantic import BaseModel | ||
| from typing import List, Optional | ||
|
|
||
| class QueryRequest(BaseModel): | ||
| query: str | ||
|
|
||
|
|
||
| class QueryResponse(BaseModel): | ||
| response: str | ||
| used_tools: Optional[List[str]] = [] | ||
|
|
||
|
|
||
| class ToolInfo(BaseModel): | ||
| name: str | ||
| description: str | ||
| enabled: bool | ||
| parameters: dict | ||
|
|
||
|
|
||
| class ToolsListResponse(BaseModel): | ||
| tools: List[ToolInfo] | ||
|
|
||
|
|
||
| class ToolToggleRequest(BaseModel): | ||
| tool_name: str | ||
| enabled: bool | ||
|
|
||
|
|
||
| class ToolToggleResponse(BaseModel): | ||
| tool_name: str | ||
| enabled: bool | ||
| message: str |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,66 @@ | ||
| from fastapi import APIRouter, Depends | ||
| from fastapi import APIRouter, Depends, HTTPException | ||
|
|
||
| from api.auth import get_api_key | ||
| from api.models import QueryRequest, QueryResponse | ||
| from agent import Agent | ||
| from api.models import QueryRequest, QueryResponse, ToolsListResponse, ToolToggleRequest, ToolToggleResponse, ToolInfo | ||
| from agent import agent_instance | ||
| from core.mcp.sessions_manager import MCPSessionManager | ||
|
|
||
| agent = Agent() | ||
| session_manager = MCPSessionManager() | ||
|
|
||
| router = APIRouter(prefix="/api", dependencies=[Depends(get_api_key)]) | ||
|
|
||
| @router.post("/ask", response_model=QueryResponse) | ||
| async def ask_agent(request: QueryRequest) -> QueryResponse: | ||
| try: | ||
| response = await agent.process_query(request.query) | ||
| return QueryResponse(response=response) | ||
| response, used_tools = await agent_instance.process_query(request.query) | ||
| return QueryResponse( | ||
| response=response, | ||
| used_tools=list(used_tools) | ||
| ) | ||
| except Exception as e: | ||
| return QueryResponse(response=f"Sorry, I encountered an error: {str(e)}") | ||
|
|
||
|
|
||
| @router.get("/tools", response_model=ToolsListResponse) | ||
| async def list_tools() -> ToolsListResponse: | ||
| try: | ||
| tools_info = agent_instance.get_tools() | ||
| tools = [ | ||
| ToolInfo( | ||
| name=info.name, | ||
| description=info.description, | ||
| enabled=info.enabled, | ||
| parameters=info.parameters | ||
| ) | ||
| for info in tools_info | ||
| ] | ||
| return ToolsListResponse(tools=tools) | ||
| except Exception as e: | ||
| raise HTTPException(status_code=500, detail=f"Error listing tools: {str(e)}") | ||
|
|
||
|
|
||
| @router.post("/tools/toggle", response_model=ToolToggleResponse) | ||
| async def toggle_tool(request: ToolToggleRequest) -> ToolToggleResponse: | ||
| try: | ||
| if request.enabled: | ||
| success = agent_instance.enable_tool(request.tool_name) | ||
| action = "enabled" | ||
| else: | ||
| success = agent_instance.disable_tool(request.tool_name) | ||
| action = "disabled" | ||
|
|
||
| if not success: | ||
| raise HTTPException( | ||
| status_code=404, | ||
| detail=f"Tool '{request.tool_name}' not found" | ||
| ) | ||
|
|
||
| return ToolToggleResponse( | ||
| tool_name=request.tool_name, | ||
| enabled=request.enabled, | ||
| message=f"Tool '{request.tool_name}' has been {action}" | ||
| ) | ||
| except HTTPException: | ||
| raise | ||
| except Exception as e: | ||
| raise HTTPException(status_code=500, detail=f"Error toggling tool: {str(e)}") |
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -18,11 +18,35 @@ class Chat: | |||||||||||||||
| def __init__(self, tool_list: List[Tool] = []): | ||||||||||||||||
| self.chat_client: ChatClient = ChatClient() | ||||||||||||||||
| self.tool_map = {tool.name: tool for tool in tool_list} | ||||||||||||||||
| self.tools = [tool.define() for tool in tool_list] | ||||||||||||||||
| self.tools: List[Tool] = [tool for tool in tool_list] | ||||||||||||||||
|
|
||||||||||||||||
| def add_tool(self, tool: Tool) -> None: | ||||||||||||||||
| self.tool_map[tool.name] = tool | ||||||||||||||||
| self.tools.append(tool.define()) | ||||||||||||||||
| self.tools.append(tool) | ||||||||||||||||
| self.tools = list(set(self.tools)) # Ensure tools are unique | ||||||||||||||||
|
|
||||||||||||||||
| def get_tools(self) -> List[Dict[str, Any]]: | ||||||||||||||||
| return self.tools | ||||||||||||||||
|
|
||||||||||||||||
| def enable_tool(self, tool_name: str) -> None: | ||||||||||||||||
| self._set_tool_state(tool_name, active=True) | ||||||||||||||||
|
|
||||||||||||||||
| def disable_tool(self, tool_name: str) -> None: | ||||||||||||||||
| self._set_tool_state(tool_name, active=False) | ||||||||||||||||
|
|
||||||||||||||||
| def _set_tool_state(self, tool_name: str, active = True) -> None: | ||||||||||||||||
| for tool in self.tools: | ||||||||||||||||
| print(f"Checking tool: {tool.name} against {tool_name} ") | ||||||||||||||||
|
||||||||||||||||
| print(f"Checking tool: {tool.name} against {tool_name} ") | |
| if is_debug(): | |
| print(f"Checking tool: {tool.name} against {tool_name} ") |
Copilot
AI
Jun 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The disable-then-enable pattern is redundant when activating a tool. Consider simplifying to if active: tool.enable() else: tool.disable() for clarity.
| tool.disable() | |
| if active: | |
| tool.enable() | |
| if active: | |
| tool.enable() | |
| else: | |
| tool.disable() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return type should be annotated as
-> boolto reflect that this method returns True/False, improving type clarity.