Skip to content

Commit

Permalink
chore: comment out unused route for now
Browse files Browse the repository at this point in the history
  • Loading branch information
cpacker committed Dec 3, 2024
1 parent 703365b commit 0895587
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 57 deletions.
25 changes: 13 additions & 12 deletions letta/server/rest_api/routers/v1/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from letta.errors import LettaToolCreateError
from letta.orm.errors import UniqueConstraintViolationError
from letta.schemas.letta_message import FunctionReturn
from letta.schemas.tool import Tool, ToolCreate, ToolRun, ToolRunFromSource, ToolUpdate
from letta.schemas.tool import Tool, ToolCreate, ToolRunFromSource, ToolUpdate
from letta.server.rest_api.utils import get_letta_server
from letta.server.server import SyncServer

Expand Down Expand Up @@ -160,18 +160,19 @@ def add_base_tools(
return server.tool_manager.add_base_tools(actor=actor)


@router.post("/{tool_id}/run", response_model=FunctionReturn, operation_id="run_tool")
def run_tool(
server: SyncServer = Depends(get_letta_server),
request: ToolRun = Body(...),
user_id: Optional[str] = Header(None, alias="user_id"), # Extract user_id from header, default to None if not present
):
"""
Run an existing tool on provided arguments
"""
actor = server.get_user_or_default(user_id=user_id)
# NOTE: can re-enable if needed
# @router.post("/{tool_id}/run", response_model=FunctionReturn, operation_id="run_tool")
# def run_tool(
# server: SyncServer = Depends(get_letta_server),
# request: ToolRun = Body(...),
# user_id: Optional[str] = Header(None, alias="user_id"), # Extract user_id from header, default to None if not present
# ):
# """
# Run an existing tool on provided arguments
# """
# actor = server.get_user_or_default(user_id=user_id)

return server.run_tool(tool_id=request.tool_id, tool_args=request.tool_args, user_id=actor.id)
# return server.run_tool(tool_id=request.tool_id, tool_args=request.tool_args, user_id=actor.id)


@router.post("/run", response_model=FunctionReturn, operation_id="run_tool_from_source")
Expand Down
90 changes: 45 additions & 45 deletions letta/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -1760,51 +1760,51 @@ def get_agent_block_by_label(self, user_id: str, agent_id: str, label: str) -> B
return block
return None

def run_tool(self, tool_id: str, tool_args: str, user_id: str) -> FunctionReturn:
"""Run a tool using the sandbox and return the result"""

try:
tool_args_dict = json.loads(tool_args)
except json.JSONDecodeError:
raise ValueError("Invalid JSON string for tool_args")

# Get the tool by ID
user = self.user_manager.get_user_by_id(user_id=user_id)
tool = self.tool_manager.get_tool_by_id(tool_id=tool_id, actor=user)
if tool.name is None:
raise ValueError(f"Tool with id {tool_id} does not have a name")

# TODO eventually allow using agent state in tools
agent_state = None

try:
sandbox_run_result = ToolExecutionSandbox(tool.name, tool_args_dict, user_id).run(agent_state=agent_state)
if sandbox_run_result is None:
raise ValueError(f"Tool with id {tool_id} returned execution with None")
function_response = str(sandbox_run_result.func_return)

return FunctionReturn(
id="null",
function_call_id="null",
date=get_utc_time(),
status="success",
function_return=function_response,
)
except Exception as e:
# same as agent.py
from letta.constants import MAX_ERROR_MESSAGE_CHAR_LIMIT

error_msg = f"Error executing tool {tool.name}: {e}"
if len(error_msg) > MAX_ERROR_MESSAGE_CHAR_LIMIT:
error_msg = error_msg[:MAX_ERROR_MESSAGE_CHAR_LIMIT]

return FunctionReturn(
id="null",
function_call_id="null",
date=get_utc_time(),
status="error",
function_return=error_msg,
)
# def run_tool(self, tool_id: str, tool_args: str, user_id: str) -> FunctionReturn:
# """Run a tool using the sandbox and return the result"""

# try:
# tool_args_dict = json.loads(tool_args)
# except json.JSONDecodeError:
# raise ValueError("Invalid JSON string for tool_args")

# # Get the tool by ID
# user = self.user_manager.get_user_by_id(user_id=user_id)
# tool = self.tool_manager.get_tool_by_id(tool_id=tool_id, actor=user)
# if tool.name is None:
# raise ValueError(f"Tool with id {tool_id} does not have a name")

# # TODO eventually allow using agent state in tools
# agent_state = None

# try:
# sandbox_run_result = ToolExecutionSandbox(tool.name, tool_args_dict, user_id).run(agent_state=agent_state)
# if sandbox_run_result is None:
# raise ValueError(f"Tool with id {tool_id} returned execution with None")
# function_response = str(sandbox_run_result.func_return)

# return FunctionReturn(
# id="null",
# function_call_id="null",
# date=get_utc_time(),
# status="success",
# function_return=function_response,
# )
# except Exception as e:
# # same as agent.py
# from letta.constants import MAX_ERROR_MESSAGE_CHAR_LIMIT

# error_msg = f"Error executing tool {tool.name}: {e}"
# if len(error_msg) > MAX_ERROR_MESSAGE_CHAR_LIMIT:
# error_msg = error_msg[:MAX_ERROR_MESSAGE_CHAR_LIMIT]

# return FunctionReturn(
# id="null",
# function_call_id="null",
# date=get_utc_time(),
# status="error",
# function_return=error_msg,
# )

def run_tool_from_source(
self,
Expand Down

0 comments on commit 0895587

Please sign in to comment.