Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
feat: insert system message when source is attached (#1158)
Sarah Woodersa7f9ed540580
diff --git a/letta/constants.py b/letta/constants.pyindex bf6679e55..e06984a3f 100644--- a/letta/constants.py+++ b/letta/constants.py@@ -146,6 +146,9 @@ MESSAGE_SUMMARY_WARNING_STR = " ".join(# "Remember to pass request_heartbeat = true if you would like to send a message immediately after.",])+DATA_SOURCE_ATTACH_ALERT = (+ "[ALERT] New data was just uploaded to archival memory. You can view this data by calling the archival_memory_search tool."+)# The ackknowledgement message used in the summarize sequenceMESSAGE_SUMMARY_REQUEST_ACK = "Understood, I will respond with a summary of the message (and only the summary, nothing else) once I receive the conversation history. I'm ready."diff --git a/letta/services/agent_manager.py b/letta/services/agent_manager.pyindex d57ab21cf..204a36be3 100644--- a/letta/services/agent_manager.py+++ b/letta/services/agent_manager.py@@ -4,7 +4,7 @@ from typing import Dict, List, Optionalimport numpy as npfrom sqlalchemy import Select, and_, func, literal, or_, select, union_all-from letta.constants import BASE_MEMORY_TOOLS, BASE_TOOLS, MAX_EMBEDDING_DIM, MULTI_AGENT_TOOLS+from letta.constants import BASE_MEMORY_TOOLS, BASE_TOOLS, DATA_SOURCE_ATTACH_ALERT, MAX_EMBEDDING_DIM, MULTI_AGENT_TOOLSfrom letta.embeddings import embedding_modelfrom letta.helpers.datetime_helpers import get_utc_timefrom letta.log import get_logger@@ -670,6 +670,7 @@ class AgentManager:ValueError: If either agent or source doesn't existIntegrityError: If the source is already attached to the agent"""+with self.session_maker() as session:# Verify both agent and source exist and user has permission to access themagent = AgentModel.read(db_session=session, identifier=agent_id, actor=actor)@@ -687,7 +688,27 @@ class AgentManager:# Commit the changesagent.update(session, actor=actor)- return agent.to_pydantic()++ # Add system messsage alert to agent+ self.append_system_message(+ agent_id=agent_id,+ content=DATA_SOURCE_ATTACH_ALERT,+ actor=actor,+ )++ return agent.to_pydantic()++ @enforce_types+ def append_system_message(self, agent_id: str, content: str, actor: PydanticUser):++ # get the agent+ agent = self.get_agent_by_id(agent_id=agent_id, actor=actor)+ message = PydanticMessage.dict_to_message(+ agent_id=agent.id, user_id=actor.id, model=agent.llm_config.model, openai_message_dict={"role": "system", "content": content}+ )++ # update agent in-context message IDs+ self.append_to_in_context_messages(messages=[message], agent_id=agent_id, actor=actor)@enforce_typesdef list_attached_sources(self, agent_id: str, actor: PydanticUser) -> List[PydanticSource]: