Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

fix: Fix agent state injection for new async tool executor (#1456)

Matthew Zhouf92cb6e5af38
diff --git a/letta/services/tool_executor/tool_executor.py b/letta/services/tool_executor/tool_executor.py
index 96d270f4b..e84f68657 100644
--- a/letta/services/tool_executor/tool_executor.py
+++ b/letta/services/tool_executor/tool_executor.py
@@ -1,3 +1,4 @@
+import ast
import math
from abc import ABC, abstractmethod
from typing import Any, Optional, Tuple
@@ -323,6 +324,7 @@ class SandboxToolExecutor(ToolExecutor):
async def execute(
self, function_name: str, function_args: dict, agent_state: AgentState, tool: Tool, actor: User
) -> Tuple[Any, Optional[SandboxRunResult]]:
+
# Store original memory state
orig_memory_str = agent_state.memory.compile()
@@ -330,12 +332,19 @@ class SandboxToolExecutor(ToolExecutor):
# Prepare function arguments
function_args = self._prepare_function_args(function_args, tool, function_name)
- # TODO: This is brittle, think about better way to do this?
agent_state_copy = self._create_agent_state_copy(agent_state)
+ # TODO: This is brittle, think about better way to do this?
+ if "agent_state" in self.parse_function_arguments(tool.source_code, tool.name):
+ inject_agent_state = True
+ else:
+ inject_agent_state = False
+
+
# Execute in sandbox
sandbox_run_result = await AsyncToolExecutionSandbox(function_name, function_args, actor, tool_object=tool).run(
- agent_state=agent_state_copy
+ agent_state=agent_state_copy,
+ inject_agent_state=inject_agent_state
)
function_response, updated_agent_state = sandbox_run_result.func_return, sandbox_run_result.agent_state
@@ -364,6 +373,16 @@ class SandboxToolExecutor(ToolExecutor):
# This is defensive programming - we try to coerce but fall back if it fails
return function_args
+ def parse_function_arguments(self, source_code: str, tool_name: str):
+ """Get arguments of a function from its source code"""
+ tree = ast.parse(source_code)
+ args = []
+ for node in ast.walk(tree):
+ if isinstance(node, ast.FunctionDef) and node.name == tool_name:
+ for arg in node.args.args:
+ args.append(arg.arg)
+ return args
+
def _create_agent_state_copy(self, agent_state: AgentState):
"""Create a copy of agent state for sandbox execution."""
agent_state_copy = agent_state.__deepcopy__()
diff --git a/tests/integration_test_experimental.py b/tests/integration_test_experimental.py
index 85a2140cf..98802554e 100644
--- a/tests/integration_test_experimental.py
+++ b/tests/integration_test_experimental.py
@@ -56,20 +56,20 @@ def server_url():
@pytest.fixture(scope="session")
def client(server_url):
"""Creates a REST client for testing."""
- # client = Letta(base_url=server_url)
- llm_config = LLMConfig(
- model="claude-3-7-sonnet-latest",
- model_endpoint_type="anthropic",
- model_endpoint="https://api.anthropic.com/v1",
- context_window=32000,
- handle=f"anthropic/claude-3-7-sonnet-latest",
- put_inner_thoughts_in_kwargs=True,
- max_tokens=4096,
- )
-
- client = create_client(base_url=server_url, token=None)
- client.set_default_llm_config(llm_config)
- client.set_default_embedding_config(EmbeddingConfig.default_config(provider="openai"))
+ client = Letta(base_url=server_url)
+ # llm_config = LLMConfig(
+ # model="claude-3-7-sonnet-latest",
+ # model_endpoint_type="anthropic",
+ # model_endpoint="https://api.anthropic.com/v1",
+ # context_window=32000,
+ # handle=f"anthropic/claude-3-7-sonnet-latest",
+ # put_inner_thoughts_in_kwargs=True,
+ # max_tokens=4096,
+ # )
+ #
+ # client = create_client(base_url=server_url, token=None)
+ # client.set_default_llm_config(llm_config)
+ # client.set_default_embedding_config(EmbeddingConfig.default_config(provider="openai"))
yield client
@@ -87,8 +87,8 @@ def roll_dice_tool(client):
time.sleep(1)
return "Rolled a 10!"
- tool = client.create_or_update_tool(func=roll_dice)
- # tool = client.tools.upsert_from_function(func=roll_dice)
+ # tool = client.create_or_update_tool(func=roll_dice)
+ tool = client.tools.upsert_from_function(func=roll_dice)
# Yield the created tool
yield tool
@@ -123,8 +123,8 @@ def weather_tool(client):
else:
raise RuntimeError(f"Failed to get weather data, status code: {response.status_code}")
- tool = client.create_or_update_tool(func=get_weather)
- # tool = client.tools.upsert_from_function(func=get_weather)
+ # tool = client.create_or_update_tool(func=get_weather)
+ tool = client.tools.upsert_from_function(func=get_weather)
# Yield the created tool
yield tool
@@ -161,6 +161,15 @@ def composio_gmail_get_profile_tool(default_user):
@pytest.fixture(scope="function")
def agent_state(client, roll_dice_tool, weather_tool, rethink_tool):
"""Creates an agent and ensures cleanup after tests."""
+ llm_config = LLMConfig(
+ model="claude-3-7-sonnet-latest",
+ model_endpoint_type="anthropic",
+ model_endpoint="https://api.anthropic.com/v1",
+ context_window=32000,
+ handle=f"anthropic/claude-3-7-sonnet-latest",
+ put_inner_thoughts_in_kwargs=True,
+ max_tokens=4096,
+ )
agent_state = client.agents.create(
name=f"test_compl_{str(uuid.uuid4())[5:]}",
tool_ids=[roll_dice_tool.id, weather_tool.id, rethink_tool.id],
@@ -175,6 +184,8 @@ def agent_state(client, roll_dice_tool, weather_tool, rethink_tool):
"value": "Friendly agent",
},
],
+ llm_config=llm_config,
+ embedding_config=EmbeddingConfig.default_config(provider="openai")
)
yield agent_state
client.agents.delete(agent_state.id)