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: enable sleeptime agent creation (#1523)

cthomas860f8707cff7
diff --git a/alembic/versions/6fe79c0525f2_enable_sleeptime_agent_fields.py b/alembic/versions/6fe79c0525f2_enable_sleeptime_agent_fields.py
new file mode 100644
index 000000000..5cff137ef
--- /dev/null
+++ b/alembic/versions/6fe79c0525f2_enable_sleeptime_agent_fields.py
@@ -0,0 +1,33 @@
+"""enable sleeptime agent fields
+
+Revision ID: 6fe79c0525f2
+Revises: e991d2e3b428
+Create Date: 2025-04-02 08:32:57.412903
+
+"""
+
+from typing import Sequence, Union
+
+import sqlalchemy as sa
+
+from alembic import op
+
+# revision identifiers, used by Alembic.
+revision: str = "6fe79c0525f2"
+down_revision: Union[str, None] = "e991d2e3b428"
+branch_labels: Union[str, Sequence[str], None] = None
+depends_on: Union[str, Sequence[str], None] = None
+
+
+def upgrade() -> None:
+ # ### commands auto generated by Alembic - please adjust! ###
+ op.add_column("agents", sa.Column("enable_sleeptime", sa.Boolean(), nullable=True))
+ op.alter_column("groups", "background_agents_interval", new_column_name="background_agents_frequency")
+ # ### end Alembic commands ###
+
+
+def downgrade() -> None:
+ # ### commands auto generated by Alembic - please adjust! ###
+ op.alter_column("groups", "background_agents_frequency", new_column_name="background_agents_interval")
+ op.drop_column("agents", "enable_sleeptime")
+ # ### end Alembic commands ###
diff --git a/letta/agent.py b/letta/agent.py
index 631020b62..d09c18286 100644
--- a/letta/agent.py
+++ b/letta/agent.py
@@ -1243,7 +1243,7 @@ class Agent(BaseAgent):
callable_func = get_function_from_module(LETTA_MULTI_AGENT_TOOL_MODULE_NAME, function_name)
function_args["self"] = self # need to attach self to arg since it's dynamically linked
function_response = callable_func(**function_args)
- elif target_letta_tool.tool_type == ToolType.LETTA_MEMORY_CORE:
+ elif target_letta_tool.tool_type == ToolType.LETTA_MEMORY_CORE or target_letta_tool.tool_type == ToolType.LETTA_SLEEPTIME_CORE:
callable_func = get_function_from_module(LETTA_CORE_TOOL_MODULE_NAME, function_name)
agent_state_copy = self.agent_state.__deepcopy__()
function_args["agent_state"] = agent_state_copy # need to attach self to arg since it's dynamically linked
diff --git a/letta/constants.py b/letta/constants.py
index 57fb1319e..d9f096d20 100644
--- a/letta/constants.py
+++ b/letta/constants.py
@@ -52,10 +52,12 @@ DEFAULT_PRESET = "memgpt_chat"
BASE_TOOLS = ["send_message", "conversation_search", "archival_memory_insert", "archival_memory_search"]
# Base memory tools CAN be edited, and are added by default by the server
BASE_MEMORY_TOOLS = ["core_memory_append", "core_memory_replace"]
+# Base memory tools for sleeptime agent
+BASE_SLEEPTIME_TOOLS = ["rethink_memory", "finish_rethinking_memory"]
# Multi agent tools
MULTI_AGENT_TOOLS = ["send_message_to_agent_and_wait_for_reply", "send_message_to_agents_matching_tags", "send_message_to_agent_async"]
# Set of all built-in Letta tools
-LETTA_TOOL_SET = set(BASE_TOOLS + BASE_MEMORY_TOOLS + MULTI_AGENT_TOOLS)
+LETTA_TOOL_SET = set(BASE_TOOLS + BASE_MEMORY_TOOLS + MULTI_AGENT_TOOLS + BASE_SLEEPTIME_TOOLS)
# The name of the tool used to send message to the user
# May not be relevant in cases where the agent has multiple ways to message to user (send_imessage, send_discord_mesasge, ...)
diff --git a/letta/functions/function_sets/base.py b/letta/functions/function_sets/base.py
index 89521c8c8..095ab4694 100644
--- a/letta/functions/function_sets/base.py
+++ b/letta/functions/function_sets/base.py
@@ -164,3 +164,34 @@ def core_memory_replace(agent_state: "AgentState", label: str, old_content: str,
new_value = current_value.replace(str(old_content), str(new_content))
agent_state.memory.update_block_value(label=label, value=new_value)
return None
+
+
+def rethink_memory(agent_state: "AgentState", new_memory: str, target_block_label: str) -> None: # type: ignore
+ """
+ Re-evaluate the memory in block_name, integrating new and updated facts.
+ Replace outdated information with the most likely truths, avoiding redundancy with original memories.
+ Ensure consistency with other memory blocks.
+
+ Args:
+ new_memory (str): The new memory with information integrated from the memory block. If there is no new information, then this should be the same as the content in the source block.
+ target_block_label (str): The name of the block to write to.
+
+ Returns:
+ None: None is always returned as this function does not produce a response.
+ """
+
+ if target_block_label is not None:
+ if agent_state.memory.get_block(target_block_label) is None:
+ agent_state.memory.create_block(label=target_block_label, value=new_memory)
+ agent_state.memory.update_block_value(label=target_block_label, value=new_memory)
+ return None
+
+
+def finish_rethinking_memory(agent_state: "AgentState") -> None: # type: ignore
+ """
+ This function is called when the agent is done rethinking the memory.
+
+ Returns:
+ Optional[str]: None is always returned as this function does not produce a response.
+ """
+ return None
diff --git a/letta/groups/background_multi_agent.py b/letta/groups/background_multi_agent.py
index 49a7567f6..13be7f707 100644
--- a/letta/groups/background_multi_agent.py
+++ b/letta/groups/background_multi_agent.py
@@ -13,6 +13,7 @@ from letta.schemas.letta_message_content import TextContent
from letta.schemas.message import Message, MessageCreate
from letta.schemas.run import Run
from letta.schemas.usage import LettaUsageStatistics
+from letta.server.rest_api.interface import StreamingServerInterface
from letta.services.group_manager import GroupManager
from letta.services.job_manager import JobManager
from letta.services.message_manager import MessageManager
@@ -29,13 +30,13 @@ class BackgroundMultiAgent(Agent):
group_id: str = "",
agent_ids: List[str] = [],
description: str = "",
- background_agents_interval: Optional[int] = None,
+ background_agents_frequency: Optional[int] = None,
):
super().__init__(interface, agent_state, user)
self.group_id = group_id
self.agent_ids = agent_ids
self.description = description
- self.background_agents_interval = background_agents_interval
+ self.background_agents_frequency = background_agents_frequency
self.group_manager = GroupManager()
self.message_manager = MessageManager()
self.job_manager = JobManager()
@@ -112,12 +113,12 @@ class BackgroundMultiAgent(Agent):
participant_agent_state = self.agent_manager.get_agent_by_id(participant_agent_id, actor=self.user)
participant_agent = Agent(
agent_state=participant_agent_state,
- interface=self.interface,
+ interface=StreamingServerInterface(),
user=self.user,
)
prior_messages = []
- if self.background_agents_interval:
+ if self.background_agents_frequency:
try:
prior_messages = self.message_manager.list_messages_for_agent(
agent_id=self.agent_state.id,
@@ -212,11 +213,11 @@ class BackgroundMultiAgent(Agent):
)
turns_counter = None
- if self.background_agents_interval is not None and self.background_agents_interval > 0:
+ if self.background_agents_frequency is not None and self.background_agents_frequency > 0:
turns_counter = self.group_manager.bump_turns_counter(group_id=self.group_id, actor=self.user)
- if self.background_agents_interval is None or (
- turns_counter is not None and turns_counter % self.background_agents_interval == 0
+ if self.background_agents_frequency is None or (
+ turns_counter is not None and turns_counter % self.background_agents_frequency == 0
):
last_response_messages = [message for sublist in usage_stats.steps_messages for message in sublist]
last_processed_message_id = self.group_manager.get_last_processed_message_id_and_update(
diff --git a/letta/groups/helpers.py b/letta/groups/helpers.py
index 680128362..8755e1907 100644
--- a/letta/groups/helpers.py
+++ b/letta/groups/helpers.py
@@ -1,7 +1,8 @@
import json
-from typing import Optional, Union
+from typing import Dict, Optional, Union
from letta.agent import Agent
+from letta.functions.mcp_client.base_client import BaseMCPClient
from letta.interface import AgentInterface
from letta.orm.group import Group
from letta.orm.user import User
@@ -15,6 +16,7 @@ def load_multi_agent(
agent_state: Optional[AgentState],
actor: User,
interface: Union[AgentInterface, None] = None,
+ mcp_clients: Optional[Dict[str, BaseMCPClient]] = None,
) -> Agent:
if len(group.agent_ids) == 0:
raise ValueError("Empty group: group must have at least one agent")
@@ -60,6 +62,14 @@ def load_multi_agent(
description=group.description,
)
case ManagerType.background:
+ if not agent_state.enable_sleeptime:
+ return Agent(
+ agent_state=agent_state,
+ interface=interface,
+ user=actor,
+ mcp_clients=mcp_clients,
+ )
+
from letta.groups.background_multi_agent import BackgroundMultiAgent
return BackgroundMultiAgent(
@@ -69,7 +79,7 @@ def load_multi_agent(
group_id=group.id,
agent_ids=group.agent_ids,
description=group.description,
- background_agents_interval=group.background_agents_interval,
+ background_agents_frequency=group.background_agents_frequency,
)
case _:
raise ValueError(f"Type {group.manager_type} is not supported.")
diff --git a/letta/orm/agent.py b/letta/orm/agent.py
index a4487963c..39a549acb 100644
--- a/letta/orm/agent.py
+++ b/letta/orm/agent.py
@@ -68,6 +68,9 @@ class Agent(SqlalchemyBase, OrganizationMixin):
message_buffer_autoclear: Mapped[bool] = mapped_column(
Boolean, doc="If set to True, the agent will not remember previous messages. Not recommended unless you have an advanced use case."
)
+ enable_sleeptime: Mapped[Optional[bool]] = mapped_column(
+ Boolean, doc="If set to True, memory management will move to a background agent thread."
+ )
# relationships
organization: Mapped["Organization"] = relationship("Organization", back_populates="agents")
@@ -190,6 +193,7 @@ class Agent(SqlalchemyBase, OrganizationMixin):
"identity_ids": [],
"multi_agent_group": None,
"tool_exec_environment_variables": [],
+ "enable_sleeptime": None,
}
# Optional fields: only included if requested
@@ -201,6 +205,7 @@ class Agent(SqlalchemyBase, OrganizationMixin):
"identity_ids": lambda: [i.id for i in self.identities],
"multi_agent_group": lambda: self.multi_agent_group,
"tool_exec_environment_variables": lambda: self.tool_exec_environment_variables,
+ "enable_sleeptime": lambda: self.enable_sleeptime,
}
include_relationships = set(optional_fields.keys() if include_relationships is None else include_relationships)
diff --git a/letta/orm/enums.py b/letta/orm/enums.py
index 5bf0648ce..62aa5a420 100644
--- a/letta/orm/enums.py
+++ b/letta/orm/enums.py
@@ -6,6 +6,7 @@ class ToolType(str, Enum):
LETTA_CORE = "letta_core"
LETTA_MEMORY_CORE = "letta_memory_core"
LETTA_MULTI_AGENT_CORE = "letta_multi_agent_core"
+ LETTA_SLEEPTIME_CORE = "letta_sleeptime_core"
EXTERNAL_COMPOSIO = "external_composio"
EXTERNAL_LANGCHAIN = "external_langchain"
# TODO is "external" the right name here? Since as of now, MCP is local / doesn't support remote?
diff --git a/letta/orm/group.py b/letta/orm/group.py
index 6f939eb0c..c77d33cb5 100644
--- a/letta/orm/group.py
+++ b/letta/orm/group.py
@@ -20,7 +20,7 @@ class Group(SqlalchemyBase, OrganizationMixin):
manager_agent_id: Mapped[Optional[str]] = mapped_column(String, ForeignKey("agents.id", ondelete="RESTRICT"), nullable=True, doc="")
termination_token: Mapped[Optional[str]] = mapped_column(nullable=True, doc="")
max_turns: Mapped[Optional[int]] = mapped_column(nullable=True, doc="")
- background_agents_interval: Mapped[Optional[int]] = mapped_column(nullable=True, doc="")
+ background_agents_frequency: Mapped[Optional[int]] = mapped_column(nullable=True, doc="")
turns_counter: Mapped[Optional[int]] = mapped_column(nullable=True, doc="")
last_processed_message_id: Mapped[Optional[str]] = mapped_column(nullable=True, doc="")
diff --git a/letta/schemas/agent.py b/letta/schemas/agent.py
index f80552635..a5476cf60 100644
--- a/letta/schemas/agent.py
+++ b/letta/schemas/agent.py
@@ -26,7 +26,7 @@ class AgentType(str, Enum):
memgpt_agent = "memgpt_agent"
split_thread_agent = "split_thread_agent"
- offline_memory_agent = "offline_memory_agent"
+ sleeptime_agent = "sleeptime_agent"
class AgentState(OrmMetadataBase, validate_assignment=True):
@@ -90,6 +90,10 @@ class AgentState(OrmMetadataBase, validate_assignment=True):
False,
description="If set to True, the agent will not remember previous messages (though the agent will still retain state via core memory blocks and archival/recall memory). Not recommended unless you have an advanced use case.",
)
+ enable_sleeptime: Optional[bool] = Field(
+ None,
+ description="If set to True, memory management will move to a background agent thread.",
+ )
multi_agent_group: Optional[Group] = Field(None, description="The multi-agent group that this agent manages")
@@ -174,7 +178,7 @@ class CreateAgent(BaseModel, validate_assignment=True): #
False,
description="If set to True, the agent will not remember previous messages (though the agent will still retain state via core memory blocks and archival/recall memory). Not recommended unless you have an advanced use case.",
)
- enable_sleeptime: Optional[bool] = Field(False, description="If set to True, memory management will move to a background agent thread.")
+ enable_sleeptime: Optional[bool] = Field(None, description="If set to True, memory management will move to a background agent thread.")
@field_validator("name")
@classmethod
@@ -253,7 +257,7 @@ class UpdateAgent(BaseModel):
embedding: Optional[str] = Field(
None, description="The embedding configuration handle used by the agent, specified in the format provider/model-name."
)
- enable_sleeptime: Optional[bool] = Field(False, description="If set to True, memory management will move to a background agent thread.")
+ enable_sleeptime: Optional[bool] = Field(None, description="If set to True, memory management will move to a background agent thread.")
class Config:
extra = "ignore" # Ignores extra fields
diff --git a/letta/schemas/group.py b/letta/schemas/group.py
index 4e5b27d22..669ebff42 100644
--- a/letta/schemas/group.py
+++ b/letta/schemas/group.py
@@ -28,7 +28,7 @@ class Group(GroupBase):
manager_agent_id: Optional[str] = Field(None, description="")
termination_token: Optional[str] = Field(None, description="")
max_turns: Optional[int] = Field(None, description="")
- background_agents_interval: Optional[int] = Field(None, description="")
+ background_agents_frequency: Optional[int] = Field(None, description="")
turns_counter: Optional[int] = Field(None, description="")
last_processed_message_id: Optional[str] = Field(None, description="")
@@ -57,7 +57,7 @@ class DynamicManager(ManagerConfig):
class BackgroundManager(ManagerConfig):
manager_type: Literal[ManagerType.background] = Field(ManagerType.background, description="")
manager_agent_id: str = Field(..., description="")
- background_agents_interval: Optional[int] = Field(None, description="")
+ background_agents_frequency: Optional[int] = Field(None, description="")
# class SwarmGroup(ManagerConfig):
diff --git a/letta/schemas/tool.py b/letta/schemas/tool.py
index 2ff62924b..92dc14711 100644
--- a/letta/schemas/tool.py
+++ b/letta/schemas/tool.py
@@ -104,6 +104,9 @@ class Tool(BaseTool):
elif self.tool_type in {ToolType.LETTA_MULTI_AGENT_CORE}:
# If it's letta multi-agent tool, we also generate the json_schema on the fly here
self.json_schema = get_json_schema_from_module(module_name=LETTA_MULTI_AGENT_TOOL_MODULE_NAME, function_name=self.name)
+ elif self.tool_type in {ToolType.LETTA_SLEEPTIME_CORE}:
+ # If it's letta sleeptime core tool, we generate the json_schema on the fly here
+ self.json_schema = get_json_schema_from_module(module_name=LETTA_CORE_TOOL_MODULE_NAME, function_name=self.name)
# At this point, we need to validate that at least json_schema is populated
if not self.json_schema:
diff --git a/letta/server/server.py b/letta/server/server.py
index 2dcc7181b..3f36991a6 100644
--- a/letta/server/server.py
+++ b/letta/server/server.py
@@ -32,7 +32,6 @@ from letta.helpers.message_helper import prepare_input_message_create
from letta.interface import AgentInterface # abstract
from letta.interface import CLIInterface # for printing to terminal
from letta.log import get_logger
-from letta.offline_memory_agent import OfflineMemoryAgent
from letta.orm.errors import NoResultFound
from letta.schemas.agent import AgentState, AgentType, CreateAgent, UpdateAgent
from letta.schemas.block import BlockUpdate
@@ -41,6 +40,7 @@ from letta.schemas.embedding_config import EmbeddingConfig
# openai schemas
from letta.schemas.enums import JobStatus, MessageStreamStatus
from letta.schemas.environment_variables import SandboxEnvironmentVariableCreate
+from letta.schemas.group import BackgroundManager, GroupCreate
from letta.schemas.job import Job, JobUpdate
from letta.schemas.letta_message import LegacyLettaMessage, LettaMessage, ToolReturnMessage
from letta.schemas.letta_message_content import TextContent
@@ -92,6 +92,7 @@ from letta.services.tool_executor.tool_execution_sandbox import ToolExecutionSan
from letta.services.tool_manager import ToolManager
from letta.services.user_manager import UserManager
from letta.settings import model_settings, settings, tool_settings
+from letta.sleeptime_agent import SleeptimeAgent
from letta.tracing import trace_method
from letta.utils import get_friendly_error_msg
@@ -349,13 +350,15 @@ class SyncServer(Server):
"""Updated method to load agents from persisted storage"""
agent_state = self.agent_manager.get_agent_by_id(agent_id=agent_id, actor=actor)
if agent_state.multi_agent_group:
- return load_multi_agent(group=agent_state.multi_agent_group, agent_state=agent_state, actor=actor, interface=interface)
+ return load_multi_agent(
+ group=agent_state.multi_agent_group, agent_state=agent_state, actor=actor, interface=interface, mcp_clients=self.mcp_clients
+ )
interface = interface or self.default_interface_factory()
if agent_state.agent_type == AgentType.memgpt_agent:
agent = Agent(agent_state=agent_state, interface=interface, user=actor, mcp_clients=self.mcp_clients)
- elif agent_state.agent_type == AgentType.offline_memory_agent:
- agent = OfflineMemoryAgent(agent_state=agent_state, interface=interface, user=actor)
+ elif agent_state.agent_type == AgentType.sleeptime_agent:
+ agent = SleeptimeAgent(agent_state=agent_state, interface=interface, user=actor)
else:
raise ValueError(f"Invalid agent type {agent_state.agent_type}")
@@ -718,13 +721,16 @@ class SyncServer(Server):
handle=request.embedding, embedding_chunk_size=request.embedding_chunk_size or constants.DEFAULT_EMBEDDING_CHUNK_SIZE
)
- """Create a new agent using a config"""
- # Invoke manager
- return self.agent_manager.create_agent(
+ main_agent = self.agent_manager.create_agent(
agent_create=request,
actor=actor,
)
+ if request.enable_sleeptime:
+ main_agent = self.create_sleeptime_agent(main_agent=main_agent, actor=actor)
+
+ return main_agent
+
def update_agent(
self,
agent_id: str,
@@ -737,13 +743,42 @@ class SyncServer(Server):
if request.embedding is not None:
request.embedding_config = self.get_embedding_config_from_handle(handle=request.embedding)
- # Invoke manager
+ if request.enable_sleeptime:
+ agent = self.agent_manager.get_agent_by_id(agent_id=agent_id, actor=actor)
+ if agent.multi_agent_group is None:
+ self.create_sleeptime_agent(main_agent=agent, actor=actor)
+
return self.agent_manager.update_agent(
agent_id=agent_id,
agent_update=request,
actor=actor,
)
+ def create_sleeptime_agent(self, main_agent: AgentState, actor: User) -> AgentState:
+ request = CreateAgent(
+ name=main_agent.name,
+ agent_type=AgentType.sleeptime_agent,
+ block_ids=[block.id for block in main_agent.memory.blocks],
+ llm_config=main_agent.llm_config,
+ embedding_config=main_agent.embedding_config,
+ project_id=main_agent.project_id,
+ )
+ sleeptime_agent = self.agent_manager.create_agent(
+ agent_create=request,
+ actor=actor,
+ )
+ self.group_manager.create_group(
+ group=GroupCreate(
+ description="",
+ agent_ids=[sleeptime_agent.id],
+ manager_config=BackgroundManager(
+ manager_agent_id=main_agent.id,
+ ),
+ ),
+ actor=actor,
+ )
+ return self.agent_manager.get_agent_by_id(agent_id=main_agent.id, actor=actor)
+
# convert name->id
# TODO: These can be moved to agent_manager
diff --git a/letta/services/agent_manager.py b/letta/services/agent_manager.py
index fe74c6769..f0ada5454 100644
--- a/letta/services/agent_manager.py
+++ b/letta/services/agent_manager.py
@@ -4,13 +4,21 @@ from typing import Dict, List, Optional
import numpy as np
from sqlalchemy import Select, and_, func, literal, or_, select, union_all
-from letta.constants import BASE_MEMORY_TOOLS, BASE_TOOLS, DATA_SOURCE_ATTACH_ALERT, MAX_EMBEDDING_DIM, MULTI_AGENT_TOOLS
+from letta.constants import (
+ BASE_MEMORY_TOOLS,
+ BASE_SLEEPTIME_TOOLS,
+ BASE_TOOLS,
+ DATA_SOURCE_ATTACH_ALERT,
+ MAX_EMBEDDING_DIM,
+ MULTI_AGENT_TOOLS,
+)
from letta.embeddings import embedding_model
from letta.helpers.datetime_helpers import get_utc_time
from letta.log import get_logger
from letta.orm import Agent as AgentModel
from letta.orm import AgentPassage, AgentsTags
from letta.orm import Block as BlockModel
+from letta.orm import Group as GroupModel
from letta.orm import Identity as IdentityModel
from letta.orm import Source as SourceModel
from letta.orm import SourcePassage, SourcesAgents
@@ -25,6 +33,7 @@ from letta.schemas.agent import AgentType, CreateAgent, UpdateAgent
from letta.schemas.block import Block as PydanticBlock
from letta.schemas.block import BlockUpdate
from letta.schemas.embedding_config import EmbeddingConfig
+from letta.schemas.group import ManagerType
from letta.schemas.llm_config import LLMConfig
from letta.schemas.memory import Memory
from letta.schemas.message import Message as PydanticMessage
@@ -104,7 +113,13 @@ class AgentManager:
# create passed in `tools`
tool_names = []
if agent_create.include_base_tools:
- tool_names.extend(BASE_TOOLS + BASE_MEMORY_TOOLS)
+ if agent_create.agent_type == AgentType.sleeptime_agent:
+ tool_names.extend(BASE_SLEEPTIME_TOOLS)
+ else:
+ if agent_create.enable_sleeptime:
+ tool_names.extend(BASE_TOOLS)
+ else:
+ tool_names.extend(BASE_TOOLS + BASE_MEMORY_TOOLS)
if agent_create.include_multi_agent_tools:
tool_names.extend(MULTI_AGENT_TOOLS)
if agent_create.tools:
@@ -121,7 +136,7 @@ class AgentManager:
# apply default tool rules
for tool_name in tool_names:
- if tool_name == "send_message" or tool_name == "send_message_to_agent_async":
+ if tool_name == "send_message" or tool_name == "send_message_to_agent_async" or tool_name == "finish_rethinking_memory":
tool_rules.append(PydanticTerminalToolRule(tool_name=tool_name))
elif tool_name in BASE_TOOLS:
tool_rules.append(PydanticContinueToolRule(tool_name=tool_name))
@@ -159,6 +174,7 @@ class AgentManager:
template_id=agent_create.template_id,
base_template_id=agent_create.base_template_id,
message_buffer_autoclear=agent_create.message_buffer_autoclear,
+ enable_sleeptime=agent_create.enable_sleeptime,
)
# If there are provided environment variables, add them in
@@ -223,6 +239,7 @@ class AgentManager:
template_id: Optional[str] = None,
base_template_id: Optional[str] = None,
message_buffer_autoclear: bool = False,
+ enable_sleeptime: Optional[bool] = None,
) -> PydanticAgentState:
"""Create a new agent."""
with self.session_maker() as session:
@@ -241,6 +258,7 @@ class AgentManager:
"template_id": template_id,
"base_template_id": base_template_id,
"message_buffer_autoclear": message_buffer_autoclear,
+ "enable_sleeptime": enable_sleeptime,
}
# Create the new agent using SqlalchemyBase.create
@@ -305,6 +323,7 @@ class AgentManager:
"template_id",
"base_template_id",
"message_buffer_autoclear",
+ "enable_sleeptime",
}
for field in scalar_fields:
value = getattr(agent_update, field, None)
@@ -461,9 +480,33 @@ class AgentManager:
"""
with self.session_maker() as session:
# Retrieve the agent
+ logger.debug(f"Hard deleting Agent with ID: {agent_id} with actor={actor}")
agent = AgentModel.read(db_session=session, identifier=agent_id, actor=actor)
- # TODO check if it is managing a group
- agent.hard_delete(session)
+ agents_to_delete = [agent]
+ sleeptime_group_to_delete = None
+
+ # Delete sleeptime agent and group
+ if agent.multi_agent_group:
+ participant_agent_ids = agent.multi_agent_group.agent_ids
+ if agent.multi_agent_group.manager_type == ManagerType.background and len(participant_agent_ids) == 1:
+ sleeptime_agent = AgentModel.read(db_session=session, identifier=participant_agent_ids[0], actor=actor)
+ if sleeptime_agent.agent_type == AgentType.sleeptime_agent:
+ sleeptime_agent_group = GroupModel.read(db_session=session, identifier=agent.multi_agent_group.id, actor=actor)
+ sleeptime_group_to_delete = sleeptime_agent_group
+ agents_to_delete.append(sleeptime_agent)
+ try:
+ if sleeptime_group_to_delete is not None:
+ session.delete(sleeptime_group_to_delete)
+ session.commit()
+ for agent in agents_to_delete:
+ session.delete(agent)
+ session.commit()
+ except Exception as e:
+ session.rollback()
+ logger.exception(f"Failed to hard delete Agent with ID {agent_id}")
+ raise ValueError(f"Failed to hard delete Agent with ID {agent_id}: {e}")
+ else:
+ logger.debug(f"Agent with ID {agent_id} successfully hard deleted")
@enforce_types
def serialize(self, agent_id: str, actor: PydanticUser) -> AgentSchema:
diff --git a/letta/services/group_manager.py b/letta/services/group_manager.py
index b5ef69ea5..623d0fa48 100644
--- a/letta/services/group_manager.py
+++ b/letta/services/group_manager.py
@@ -74,8 +74,8 @@ class GroupManager:
case ManagerType.background:
new_group.manager_type = ManagerType.background
new_group.manager_agent_id = group.manager_config.manager_agent_id
- new_group.background_agents_interval = group.manager_config.background_agents_interval
- if new_group.background_agents_interval:
+ new_group.background_agents_frequency = group.manager_config.background_agents_frequency
+ if new_group.background_agents_frequency:
new_group.turns_counter = 0
case _:
raise ValueError(f"Unsupported manager type: {group.manager_config.manager_type}")
@@ -93,7 +93,7 @@ class GroupManager:
with self.session_maker() as session:
group = GroupModel.read(db_session=session, identifier=group_id, actor=actor)
- background_agents_interval = None
+ background_agents_frequency = None
max_turns = None
termination_token = None
manager_agent_id = None
@@ -111,14 +111,14 @@ class GroupManager:
manager_agent_id = group_update.manager_config.manager_agent_id
case ManagerType.background:
manager_agent_id = group_update.manager_config.manager_agent_id
- background_agents_interval = group_update.manager_config.background_agents_interval
- if background_agents_interval and group.turns_counter is None:
+ background_agents_frequency = group_update.manager_config.background_agents_frequency
+ if background_agents_frequency and group.turns_counter is None:
group.turns_counter = 0
case _:
raise ValueError(f"Unsupported manager type: {group_update.manager_config.manager_type}")
- if background_agents_interval:
- group.background_agents_interval = background_agents_interval
+ if background_agents_frequency:
+ group.background_agents_frequency = background_agents_frequency
if max_turns:
group.max_turns = max_turns
if termination_token:
@@ -198,7 +198,7 @@ class GroupManager:
group = GroupModel.read(db_session=session, identifier=group_id, actor=actor)
# Update turns counter
- group.turns_counter = (group.turns_counter + 1) % group.background_agents_interval
+ group.turns_counter = (group.turns_counter + 1) % group.background_agents_frequency
group.update(session, actor=actor)
return group.turns_counter
diff --git a/letta/services/helpers/agent_manager_helper.py b/letta/services/helpers/agent_manager_helper.py
index d620a225f..1c04ed33e 100644
--- a/letta/services/helpers/agent_manager_helper.py
+++ b/letta/services/helpers/agent_manager_helper.py
@@ -94,7 +94,7 @@ def derive_system_message(agent_type: AgentType, system: Optional[str] = None):
# TODO: don't hardcode
if agent_type == AgentType.memgpt_agent:
system = gpt_system.get_system_text("memgpt_chat")
- elif agent_type == AgentType.offline_memory_agent:
+ elif agent_type == AgentType.sleeptime_agent:
system = gpt_system.get_system_text("memgpt_offline_memory")
else:
raise ValueError(f"Invalid agent type: {agent_type}")
diff --git a/letta/services/tool_manager.py b/letta/services/tool_manager.py
index 2b6e5a28c..470ca6b6a 100644
--- a/letta/services/tool_manager.py
+++ b/letta/services/tool_manager.py
@@ -2,7 +2,14 @@ import importlib
import warnings
from typing import List, Optional
-from letta.constants import BASE_FUNCTION_RETURN_CHAR_LIMIT, BASE_MEMORY_TOOLS, BASE_TOOLS, MCP_TOOL_TAG_NAME_PREFIX, MULTI_AGENT_TOOLS
+from letta.constants import (
+ BASE_FUNCTION_RETURN_CHAR_LIMIT,
+ BASE_MEMORY_TOOLS,
+ BASE_SLEEPTIME_TOOLS,
+ BASE_TOOLS,
+ MCP_TOOL_TAG_NAME_PREFIX,
+ MULTI_AGENT_TOOLS,
+)
from letta.functions.functions import derive_openai_json_schema, load_function_set
from letta.log import get_logger
from letta.orm.enums import ToolType
@@ -194,7 +201,7 @@ class ToolManager:
# create tool in db
tools = []
for name, schema in functions_to_schema.items():
- if name in BASE_TOOLS + BASE_MEMORY_TOOLS + MULTI_AGENT_TOOLS:
+ if name in BASE_TOOLS + BASE_MEMORY_TOOLS + MULTI_AGENT_TOOLS + BASE_SLEEPTIME_TOOLS:
if name in BASE_TOOLS:
tool_type = ToolType.LETTA_CORE
tags = [tool_type.value]
@@ -204,9 +211,12 @@ class ToolManager:
elif name in MULTI_AGENT_TOOLS:
tool_type = ToolType.LETTA_MULTI_AGENT_CORE
tags = [tool_type.value]
+ elif name in BASE_SLEEPTIME_TOOLS:
+ tool_type = ToolType.LETTA_SLEEPTIME_CORE
+ tags = [tool_type.value]
else:
raise ValueError(
- f"Tool name {name} is not in the list of base tool names: {BASE_TOOLS + BASE_MEMORY_TOOLS + MULTI_AGENT_TOOLS}"
+ f"Tool name {name} is not in the list of base tool names: {BASE_TOOLS + BASE_MEMORY_TOOLS + MULTI_AGENT_TOOLS + BASE_SLEEPTIME_TOOLS}"
)
# create to tool
diff --git a/letta/offline_memory_agent.py b/letta/sleeptime_agent.py
similarity index 99%
rename from letta/offline_memory_agent.py
rename to letta/sleeptime_agent.py
index be8b9b33b..9d0608127 100644
--- a/letta/offline_memory_agent.py
+++ b/letta/sleeptime_agent.py
@@ -120,7 +120,7 @@ def finish_rethinking_memory_convo(agent_state: "AgentState") -> None: # type:
return None
-class OfflineMemoryAgent(Agent):
+class SleeptimeAgent(Agent):
def __init__(
self,
interface: AgentInterface,
diff --git a/tests/integration_test_offline_memory_agent.py b/tests/integration_test_offline_memory_agent.py
index 130fc1ef5..7bdae29bd 100644
--- a/tests/integration_test_offline_memory_agent.py
+++ b/tests/integration_test_offline_memory_agent.py
@@ -3,13 +3,13 @@ import pytest
from letta import BasicBlockMemory
from letta.client.client import create_client
from letta.constants import DEFAULT_HUMAN, DEFAULT_PERSONA
-from letta.offline_memory_agent import finish_rethinking_memory, rethink_memory, trigger_rethink_memory
from letta.prompts import gpt_system
from letta.schemas.agent import AgentType
from letta.schemas.embedding_config import EmbeddingConfig
from letta.schemas.llm_config import LLMConfig
from letta.schemas.memory import BasicBlockMemory, Block
from letta.schemas.tool_rule import TerminalToolRule
+from letta.sleeptime_agent import finish_rethinking_memory, rethink_memory, trigger_rethink_memory
from letta.utils import get_human_text, get_persona_text
@@ -94,7 +94,7 @@ def test_ripple_edit(client, disable_e2b_api_key):
finish_rethinking_memory_tool = client.create_or_update_tool(finish_rethinking_memory)
offline_memory_agent = client.create_agent(
name="offline_memory_agent",
- agent_type=AgentType.offline_memory_agent,
+ agent_type=AgentType.sleeptime_agent,
system=gpt_system.get_system_text("memgpt_offline_memory"),
memory=offline_memory,
llm_config=LLMConfig.default_config("gpt-4"),
@@ -146,7 +146,7 @@ def test_chat_only_agent(client, disable_e2b_api_key):
offline_memory_agent = client.create_agent(
name="offline_memory_agent",
- agent_type=AgentType.offline_memory_agent,
+ agent_type=AgentType.sleeptime_agent,
system=gpt_system.get_system_text("memgpt_memory_only"),
memory=offline_memory,
llm_config=LLMConfig.default_config("gpt-4"),
@@ -209,7 +209,7 @@ def test_initial_message_sequence(client, disable_e2b_api_key):
"""
offline_memory_agent = client.create_agent(
name="offline_memory_agent",
- agent_type=AgentType.offline_memory_agent,
+ agent_type=AgentType.sleeptime_agent,
system=gpt_system.get_system_text("memgpt_offline_memory"),
llm_config=LLMConfig.default_config("gpt-4"),
embedding_config=EmbeddingConfig.default_config("text-embedding-ada-002"),
diff --git a/tests/test_client_legacy.py b/tests/test_client_legacy.py
index ba164542f..471d514a9 100644
--- a/tests/test_client_legacy.py
+++ b/tests/test_client_legacy.py
@@ -11,7 +11,7 @@ from sqlalchemy import delete
from letta import create_client
from letta.client.client import LocalClient, RESTClient
-from letta.constants import BASE_MEMORY_TOOLS, BASE_TOOLS, DEFAULT_PRESET, MULTI_AGENT_TOOLS
+from letta.constants import BASE_MEMORY_TOOLS, BASE_SLEEPTIME_TOOLS, BASE_TOOLS, DEFAULT_PRESET, MULTI_AGENT_TOOLS
from letta.helpers.datetime_helpers import get_utc_time
from letta.orm import FileMetadata, Source
from letta.schemas.agent import AgentState
@@ -347,7 +347,7 @@ def test_list_tools_pagination(client: Union[LocalClient, RESTClient]):
def test_list_tools(client: Union[LocalClient, RESTClient]):
tools = client.upsert_base_tools()
tool_names = [t.name for t in tools]
- expected = BASE_TOOLS + BASE_MEMORY_TOOLS + MULTI_AGENT_TOOLS
+ expected = BASE_TOOLS + BASE_MEMORY_TOOLS + MULTI_AGENT_TOOLS + BASE_SLEEPTIME_TOOLS
assert sorted(tool_names) == sorted(expected)
diff --git a/tests/test_managers.py b/tests/test_managers.py
index d41a79429..212403da5 100644
--- a/tests/test_managers.py
+++ b/tests/test_managers.py
@@ -12,7 +12,14 @@ from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm.exc import StaleDataError
from letta.config import LettaConfig
-from letta.constants import BASE_MEMORY_TOOLS, BASE_TOOLS, LETTA_TOOL_EXECUTION_DIR, MCP_TOOL_TAG_NAME_PREFIX, MULTI_AGENT_TOOLS
+from letta.constants import (
+ BASE_MEMORY_TOOLS,
+ BASE_SLEEPTIME_TOOLS,
+ BASE_TOOLS,
+ LETTA_TOOL_EXECUTION_DIR,
+ MCP_TOOL_TAG_NAME_PREFIX,
+ MULTI_AGENT_TOOLS,
+)
from letta.embeddings import embedding_model
from letta.functions.functions import derive_openai_json_schema, parse_source_code
from letta.functions.mcp_client.types import MCPTool
@@ -2168,7 +2175,7 @@ def test_delete_tool_by_id(server: SyncServer, print_tool, default_user):
def test_upsert_base_tools(server: SyncServer, default_user):
tools = server.tool_manager.upsert_base_tools(actor=default_user)
- expected_tool_names = sorted(BASE_TOOLS + BASE_MEMORY_TOOLS + MULTI_AGENT_TOOLS)
+ expected_tool_names = sorted(BASE_TOOLS + BASE_MEMORY_TOOLS + MULTI_AGENT_TOOLS + BASE_SLEEPTIME_TOOLS)
assert sorted([t.name for t in tools]) == expected_tool_names
# Call it again to make sure it doesn't create duplicates
@@ -2183,6 +2190,8 @@ def test_upsert_base_tools(server: SyncServer, default_user):
assert t.tool_type == ToolType.LETTA_MEMORY_CORE
elif t.name in MULTI_AGENT_TOOLS:
assert t.tool_type == ToolType.LETTA_MULTI_AGENT_CORE
+ elif t.name in BASE_SLEEPTIME_TOOLS:
+ assert t.tool_type == ToolType.LETTA_SLEEPTIME_CORE
else:
pytest.fail(f"The tool name is unrecognized as a base tool: {t.name}")
assert t.source_code is None
diff --git a/tests/test_multi_agent.py b/tests/test_multi_agent.py
index d4349658f..c00df2637 100644
--- a/tests/test_multi_agent.py
+++ b/tests/test_multi_agent.py
@@ -4,12 +4,10 @@ import pytest
from sqlalchemy import delete
from letta.config import LettaConfig
-from letta.constants import BASE_MEMORY_TOOLS, BASE_TOOLS
-from letta.functions.functions import parse_source_code
-from letta.functions.schema_generator import generate_schema
from letta.orm import Provider, Step
+from letta.orm.errors import NoResultFound
from letta.schemas.agent import CreateAgent
-from letta.schemas.block import Block, CreateBlock
+from letta.schemas.block import CreateBlock
from letta.schemas.enums import JobStatus
from letta.schemas.group import (
BackgroundManager,
@@ -21,8 +19,6 @@ from letta.schemas.group import (
SupervisorManager,
)
from letta.schemas.message import MessageCreate
-from letta.schemas.tool import Tool
-from letta.schemas.tool_rule import TerminalToolRule
from letta.server.server import SyncServer
@@ -446,17 +442,7 @@ async def test_dynamic_group_chat(server, actor, manager_agent, participant_agen
@pytest.mark.asyncio
async def test_background_group_chat(server, actor):
- # 1. create shared block
- shared_memory_block = server.block_manager.create_or_update_block(
- Block(
- label="human",
- value="",
- limit=1000,
- ),
- actor=actor,
- )
-
- # 2. create main agent
+ # 1. create sleeptime agent
main_agent = server.create_agent(
request=CreateAgent(
name="main_agent",
@@ -465,75 +451,44 @@ async def test_background_group_chat(server, actor):
label="persona",
value="You are a personal assistant that helps users with requests.",
),
- ],
- model="openai/gpt-4o-mini",
- embedding="openai/text-embedding-ada-002",
- include_base_tools=False,
- tools=BASE_TOOLS,
- ),
- actor=actor,
- )
-
- # 3. create background memory agent
- def skip_memory_update():
- """
- Perform no memory updates. This function is used when the transcript
- does not require any changes to the memory.
- """
-
- skip_memory_update = Tool(
- name=skip_memory_update.__name__,
- description="",
- source_type="python",
- tags=[],
- source_code=parse_source_code(skip_memory_update),
- json_schema=generate_schema(skip_memory_update, None),
- )
- skip_memory_update = server.tool_manager.create_or_update_tool(
- pydantic_tool=skip_memory_update,
- actor=actor,
- )
-
- background_memory_agent = server.create_agent(
- request=CreateAgent(
- name="memory_agent",
- memory_blocks=[
CreateBlock(
- label="persona",
- value="You manage memory for the main agent. You are a background agent and you are not expected to respond to messages. When you receive a conversation snippet from the main thread, perform memory updates only if there are meaningful changes, and otherwise call the skip_memory_update tool.",
+ label="human",
+ value="",
),
],
model="openai/gpt-4o-mini",
embedding="openai/text-embedding-ada-002",
- include_base_tools=False,
- include_base_tool_rules=False,
- tools=BASE_MEMORY_TOOLS + [skip_memory_update.name],
- tool_rules=[
- TerminalToolRule(tool_name="core_memory_append"),
- TerminalToolRule(tool_name="core_memory_replace"),
- TerminalToolRule(tool_name="skip_memory_update"),
- ],
+ enable_sleeptime=True,
),
actor=actor,
)
- # 4. create group
- group = server.group_manager.create_group(
- group=GroupCreate(
- description="",
- agent_ids=[background_memory_agent.id],
+ # 2. Change frequency for test
+ group = server.group_manager.modify_group(
+ group_id=main_agent.multi_agent_group.id,
+ group_update=GroupUpdate(
manager_config=BackgroundManager(
manager_agent_id=main_agent.id,
- background_agents_interval=2,
+ background_agents_frequency=2,
),
- shared_block_ids=[shared_memory_block.id],
),
actor=actor,
)
- agents = server.block_manager.get_agents_for_block(block_id=shared_memory_block.id, actor=actor)
+ assert group.manager_type == ManagerType.background
+ assert group.background_agents_frequency == 2
+ assert len(group.agent_ids) == 1
+
+ sleeptime_agent_id = group.agent_ids[0]
+ shared_block = server.agent_manager.get_block_with_label(agent_id=main_agent.id, block_label="human", actor=actor)
+ agents = server.block_manager.get_agents_for_block(block_id=shared_block.id, actor=actor)
assert len(agents) == 2
+ assert sleeptime_agent_id in [agent.id for agent in agents]
+ assert main_agent.id in [agent.id for agent in agents]
+ assert main_agent.enable_sleeptime == True
+
+ # 6. Send messages
message_text = [
"my favorite color is orange",
"not particularly. today is a good day",
@@ -556,8 +511,8 @@ async def test_background_group_chat(server, actor):
)
assert len(response.messages) > 0
- assert len(response.usage.run_ids) == i % 2
- run_ids.extend(response.usage.run_ids)
+ assert len(response.usage.run_ids or []) == i % 2
+ run_ids.extend(response.usage.run_ids or [])
time.sleep(5)
@@ -565,6 +520,9 @@ async def test_background_group_chat(server, actor):
job = server.job_manager.get_job_by_id(job_id=run_id, actor=actor)
assert job.status == JobStatus.completed
- server.group_manager.delete_group(group_id=group.id, actor=actor)
- server.agent_manager.delete_agent(agent_id=background_memory_agent.id, actor=actor)
server.agent_manager.delete_agent(agent_id=main_agent.id, actor=actor)
+
+ with pytest.raises(NoResultFound):
+ server.group_manager.retrieve_group(group_id=group.id, actor=actor)
+ with pytest.raises(NoResultFound):
+ server.agent_manager.get_agent_by_id(agent_id=sleeptime_agent_id, actor=actor)
diff --git a/tests/test_sdk_client.py b/tests/test_sdk_client.py
index 927e113d5..232092cda 100644
--- a/tests/test_sdk_client.py
+++ b/tests/test_sdk_client.py
@@ -101,9 +101,8 @@ def test_shared_blocks(client: LettaSDKClient):
)
# check agent 2 memory
- assert (
- "charles" in client.blocks.retrieve(block_id=block.id).value.lower()
- ), f"Shared block update failed {client.retrieve_block(block.id).value}"
+ block_value = client.blocks.retrieve(block_id=block.id).value
+ assert "charles" in block_value.lower(), f"Shared block update failed {block_value}"
client.agents.messages.create(
agent_id=agent_state2.id,