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.

feat: Add agent environment variable serialization (#1198)

Matthew Zhoud79c8f948492
diff --git a/letta/serialize_schemas/agent.py b/letta/serialize_schemas/agent.py
index dfb3ff0e2..7975cc46e 100644
--- a/letta/serialize_schemas/agent.py
+++ b/letta/serialize_schemas/agent.py
@@ -5,6 +5,7 @@ from marshmallow import fields, post_dump
from letta.orm import Agent
from letta.schemas.agent import AgentState as PydanticAgentState
from letta.schemas.user import User
+from letta.serialize_schemas.agent_environment_variable import SerializedAgentEnvironmentVariableSchema
from letta.serialize_schemas.base import BaseSchema
from letta.serialize_schemas.block import SerializedBlockSchema
from letta.serialize_schemas.custom_fields import EmbeddingConfigField, LLMConfigField, ToolRulesField
@@ -28,6 +29,7 @@ class SerializedAgentSchema(BaseSchema):
messages = fields.List(fields.Nested(SerializedMessageSchema))
core_memory = fields.List(fields.Nested(SerializedBlockSchema))
tools = fields.List(fields.Nested(SerializedToolSchema))
+ tool_exec_environment_variables = fields.List(fields.Nested(SerializedAgentEnvironmentVariableSchema))
def __init__(self, *args, session: SessionLocal, actor: User, **kwargs):
super().__init__(*args, actor=actor, **kwargs)
diff --git a/letta/serialize_schemas/agent_environment_variable.py b/letta/serialize_schemas/agent_environment_variable.py
new file mode 100644
index 000000000..a631ebd8e
--- /dev/null
+++ b/letta/serialize_schemas/agent_environment_variable.py
@@ -0,0 +1,20 @@
+import uuid
+
+from letta.orm.sandbox_config import AgentEnvironmentVariable
+from letta.serialize_schemas.base import BaseSchema
+
+
+class SerializedAgentEnvironmentVariableSchema(BaseSchema):
+ """
+ Marshmallow schema for serializing/deserializing AgentEnvironmentVariable objects.
+ """
+
+ __pydantic_model__ = None
+
+ def generate_id(self) -> str:
+ # TODO: This is brittle and duplicated in orm/sandbox_config.py
+ return f"agent-env-{uuid.uuid4()}"
+
+ class Meta(BaseSchema.Meta):
+ model = AgentEnvironmentVariable
+ exclude = BaseSchema.Meta.exclude + ("agent",)
diff --git a/letta/serialize_schemas/base.py b/letta/serialize_schemas/base.py
index e40514087..e30906def 100644
--- a/letta/serialize_schemas/base.py
+++ b/letta/serialize_schemas/base.py
@@ -22,9 +22,12 @@ class BaseSchema(SQLAlchemyAutoSchema):
super().__init__(*args, **kwargs)
self.actor = actor
+ def generate_id(self) -> str:
+ return self.__pydantic_model__.generate_id()
+
@post_dump
- def sanitize_ids(self, data: Dict, **kwargs):
- data["id"] = self.__pydantic_model__.generate_id()
+ def sanitize_ids(self, data: Dict, **kwargs) -> Dict:
+ data["id"] = self.generate_id()
for sensitive_id in BaseSchema.sensitive_ids.union(BaseSchema.sensitive_relationships):
if sensitive_id in data:
@@ -33,7 +36,7 @@ class BaseSchema(SQLAlchemyAutoSchema):
return data
@pre_load
- def regenerate_ids(self, data: Dict, **kwargs):
+ def regenerate_ids(self, data: Dict, **kwargs) -> Dict:
if self.Meta.model:
mapper = inspect(self.Meta.model)
for sensitive_id in BaseSchema.sensitive_ids:
diff --git a/letta/services/agent_manager.py b/letta/services/agent_manager.py
index 06e0ef22f..3aa32e4d0 100644
--- a/letta/services/agent_manager.py
+++ b/letta/services/agent_manager.py
@@ -461,6 +461,8 @@ class AgentManager:
value=value,
agent_id=agent_id,
organization_id=actor.organization_id,
+ created_by_id=actor.id,
+ last_updated_by_id=actor.id,
)
)
diff --git a/tests/test_agent_serialization.py b/tests/test_agent_serialization.py
index 0e0348936..b88eb7534 100644
--- a/tests/test_agent_serialization.py
+++ b/tests/test_agent_serialization.py
@@ -11,7 +11,7 @@ from letta import create_client
from letta.config import LettaConfig
from letta.orm import Base
from letta.schemas.agent import AgentState, CreateAgent
-from letta.schemas.block import CreateBlock
+from letta.schemas.block import Block, CreateBlock
from letta.schemas.embedding_config import EmbeddingConfig
from letta.schemas.enums import MessageRole
from letta.schemas.llm_config import LLMConfig
@@ -84,26 +84,51 @@ def other_user(server: SyncServer, other_organization):
@pytest.fixture
-def serialize_test_agent(server: SyncServer, default_user, default_organization):
+def weather_tool(local_client, weather_tool_func):
+ weather_tool = local_client.create_or_update_tool(func=weather_tool_func)
+ yield weather_tool
+
+
+def default_block(server: SyncServer, default_user):
+ """Fixture to create and return a default block."""
+ block_data = Block(
+ label="default_label",
+ value="Default Block Content",
+ description="A default test block",
+ limit=1000,
+ metadata={"type": "test"},
+ )
+ block = server.block_manager.create_or_update_block(block_data, actor=default_user)
+ yield block
+
+
+def serialize_test_agent(server: SyncServer, default_user, default_organization, default_block, weather_tool):
"""Fixture to create and return a sample agent within the default organization."""
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
- agent_name = f"serialize_test_agent_{timestamp}"
+ f"serialize_test_agent_{timestamp}"
server.tool_manager.upsert_base_tools(actor=default_user)
+ memory_blocks = [CreateBlock(label="human", value="BananaBoy"), CreateBlock(label="persona", value="I am a helpful assistant")]
+ create_agent_request = CreateAgent(
+ system="test system",
+ memory_blocks=memory_blocks,
+ llm_config=LLMConfig.default_config("gpt-4"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ block_ids=[default_block.id],
+ tool_ids=[weather_tool.id],
+ # tags=["a", "b"],
+ description="test_description",
+ metadata={"test_key": "test_value"},
+ initial_message_sequence=[MessageCreate(role=MessageRole.user, content="hello world")],
+ tool_exec_environment_variables={"test_env_var_key_a": "test_env_var_value_a", "test_env_var_key_b": "test_env_var_value_b"},
+ message_buffer_autoclear=True,
+ )
+
agent_state = server.agent_manager.create_agent(
- agent_create=CreateAgent(
- name=agent_name,
- memory_blocks=[
- CreateBlock(
- value="Name: Caren",
- label="human",
- ),
- ],
- llm_config=LLMConfig.default_config("gpt-4o-mini"),
- embedding_config=EmbeddingConfig.default_config(provider="openai"),
- include_base_tools=True,
- ),
+ agent_create=create_agent_request,
actor=default_user,
)
yield agent_state
@@ -188,7 +213,7 @@ def _compare_agent_state_model_dump(d1: Dict[str, Any], d2: Dict[str, Any], log:
- Datetime fields are ignored.
- Order-independent comparison for lists of dicts.
"""
- ignore_prefix_fields = {"id", "last_updated_by_id", "organization_id", "created_by_id"}
+ ignore_prefix_fields = {"id", "last_updated_by_id", "organization_id", "created_by_id", "agent_id"}
# Remove datetime fields upfront
d1 = strip_datetime_fields(d1)
@@ -360,13 +385,8 @@ def test_agent_serialize_with_user_messages(local_client, server, serialize_test
)
-def test_agent_serialize_tool_calls(
- mock_e2b_api_key_none, local_client, server, serialize_test_agent, default_user, other_user, weather_tool_func
-):
+def test_agent_serialize_tool_calls(mock_e2b_api_key_none, local_client, server, serialize_test_agent, default_user, other_user):
"""Test deserializing JSON into an Agent instance."""
- weather_tool = local_client.create_or_update_tool(func=weather_tool_func)
- server.agent_manager.attach_tool(agent_id=serialize_test_agent.id, tool_id=weather_tool.id, actor=default_user)
-
mark_as_copy = False
server.send_messages(
actor=default_user,