Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
feat: make tool return messages more explicit [LET-7145] (#8986)
Kevin Linb5519f02fb45
diff --git a/letta/functions/function_sets/base.py b/letta/functions/function_sets/base.pyindex 8b907889f..45cede978 100644--- a/letta/functions/function_sets/base.py+++ b/letta/functions/function_sets/base.py@@ -383,12 +383,12 @@ def memory_replace(agent_state: "AgentState", label: str, old_str: str, new_str:# snippet = "\n".join(new_value.split("\n")[start_line : end_line + 1])# Prepare the success message- success_msg = f"The core memory block with label `{label}` has been edited. "- # success_msg += self._make_output(- # snippet, f"a snippet of {path}", start_line + 1- # )- # success_msg += f"A snippet of core memory block `{label}`:\n{snippet}\n"- success_msg += "Review the changes and make sure they are as expected (correct indentation, no duplicate lines, etc). Edit the memory block again if necessary."+ success_msg = (+ f"The core memory block with label `{label}` has been successfully edited. "+ f"Your system prompt has been recompiled with the updated memory contents and is now active in your context. "+ f"Review the changes and make sure they are as expected (correct indentation, "+ f"no duplicate lines, etc). Edit the memory block again if necessary."+ )# return Nonereturn success_msg@@ -454,14 +454,12 @@ def memory_insert(agent_state: "AgentState", label: str, new_str: str, insert_liagent_state.memory.update_block_value(label=label, value=new_value)# Prepare the success message- success_msg = f"The core memory block with label `{label}` has been edited. "- # success_msg += self._make_output(- # snippet,- # "a snippet of the edited file",- # max(1, insert_line - SNIPPET_LINES + 1),- # )- # success_msg += f"A snippet of core memory block `{label}`:\n{snippet}\n"- success_msg += "Review the changes and make sure they are as expected (correct indentation, no duplicate lines, etc). Edit the memory block again if necessary."+ success_msg = (+ f"The core memory block with label `{label}` has been successfully edited. "+ f"Your system prompt has been recompiled with the updated memory contents and is now active in your context. "+ f"Review the changes and make sure they are as expected (correct indentation, "+ f"no duplicate lines, etc). Edit the memory block again if necessary."+ )return success_msg@@ -532,12 +530,12 @@ def memory_rethink(agent_state: "AgentState", label: str, new_memory: str) -> Noagent_state.memory.update_block_value(label=label, value=new_memory)# Prepare the success message- success_msg = f"The core memory block with label `{label}` has been edited. "- # success_msg += self._make_output(- # snippet, f"a snippet of {path}", start_line + 1- # )- # success_msg += f"A snippet of core memory block `{label}`:\n{snippet}\n"- success_msg += "Review the changes and make sure they are as expected (correct indentation, no duplicate lines, etc). Edit the memory block again if necessary."+ success_msg = (+ f"The core memory block with label `{label}` has been successfully edited. "+ f"Your system prompt has been recompiled with the updated memory contents and is now active in your context. "+ f"Review the changes and make sure they are as expected (correct indentation, "+ f"no duplicate lines, etc). Edit the memory block again if necessary."+ )# return Nonereturn success_msgdiff --git a/letta/services/tool_executor/core_tool_executor.py b/letta/services/tool_executor/core_tool_executor.pyindex 73b934a2e..f69b31bf1 100644--- a/letta/services/tool_executor/core_tool_executor.py+++ b/letta/services/tool_executor/core_tool_executor.py@@ -401,14 +401,11 @@ class LettaCoreToolExecutor(ToolExecutor):snippet = "\n".join(new_value.split("\n")[start_line : end_line + 1])# Prepare the success message- success_msg = f"The core memory block with label `{label}` has been edited. "- # success_msg += self._make_output(- # snippet, f"a snippet of {path}", start_line + 1- # )- # success_msg += f"A snippet of core memory block `{label}`:\n{snippet}\n"- success_msg += (- "Review the changes and make sure they are as expected (correct indentation, "- "no duplicate lines, etc). Edit the memory block again if necessary."+ success_msg = (+ f"The core memory block with label `{label}` has been successfully edited. "+ f"Your system prompt has been recompiled with the updated memory contents and is now active in your context. "+ f"Review the changes and make sure they are as expected (correct indentation, "+ f"no duplicate lines, etc). Edit the memory block again if necessary.")# return None@@ -549,9 +546,10 @@ class LettaCoreToolExecutor(ToolExecutor):await self.agent_manager.update_memory_if_changed_async(agent_id=agent_state.id, new_memory=agent_state.memory, actor=actor)return (- f"The core memory block with label `{label}` has been edited. "- "Review the changes and make sure they are as expected (correct indentation, no duplicate lines, etc). "- "Edit the memory block again if necessary."+ f"The core memory block with label `{label}` has been successfully edited. "+ f"Your system prompt has been recompiled with the updated memory contents and is now active in your context. "+ f"Review the changes and make sure they are as expected (correct indentation, no duplicate lines, etc). "+ f"Edit the memory block again if necessary.")# Extended mode: parse codex-like patch operations for memory blocks@@ -692,7 +690,11 @@ class LettaCoreToolExecutor(ToolExecutor):else:raise ValueError(f"Unknown operation kind: {kind}")- return "Successfully applied memory patch operations:\n- " + "\n- ".join(results)+ return (+ "Successfully applied memory patch operations. "+ "Your system prompt has been recompiled with the updated memory contents and is now active in your context.\n\n"+ "Operations completed:\n- " + "\n- ".join(results)+ )async def memory_insert(self,@@ -753,16 +755,11 @@ class LettaCoreToolExecutor(ToolExecutor):await self.agent_manager.update_memory_if_changed_async(agent_id=agent_state.id, new_memory=agent_state.memory, actor=actor)# Prepare the success message- success_msg = f"The core memory block with label `{label}` has been edited. "- # success_msg += self._make_output(- # snippet,- # "a snippet of the edited file",- # max(1, insert_line - SNIPPET_LINES + 1),- # )- # success_msg += f"A snippet of core memory block `{label}`:\n{snippet}\n"- success_msg += (- "Review the changes and make sure they are as expected (correct indentation, "- "no duplicate lines, etc). Edit the memory block again if necessary."+ success_msg = (+ f"The core memory block with label `{label}` has been successfully edited. "+ f"Your system prompt has been recompiled with the updated memory contents and is now active in your context. "+ f"Review the changes and make sure they are as expected (correct indentation, "+ f"no duplicate lines, etc). Edit the memory block again if necessary.")return success_msg@@ -798,14 +795,11 @@ class LettaCoreToolExecutor(ToolExecutor):await self.agent_manager.update_memory_if_changed_async(agent_id=agent_state.id, new_memory=agent_state.memory, actor=actor)# Prepare the success message- success_msg = f"The core memory block with label `{label}` has been edited. "- # success_msg += self._make_output(- # snippet, f"a snippet of {path}", start_line + 1- # )- # success_msg += f"A snippet of core memory block `{label}`:\n{snippet}\n"- success_msg += (- "Review the changes and make sure they are as expected (correct indentation, "- "no duplicate lines, etc). Edit the memory block again if necessary."+ success_msg = (+ f"The core memory block with label `{label}` has been successfully edited. "+ f"Your system prompt has been recompiled with the updated memory contents and is now active in your context. "+ f"Review the changes and make sure they are as expected (correct indentation, "+ f"no duplicate lines, etc). Edit the memory block again if necessary.")# return None@@ -833,7 +827,10 @@ class LettaCoreToolExecutor(ToolExecutor):# Update the agent state with the updated memory from the databaseagent_state.memory = updated_agent_state.memory- return f"Successfully deleted memory block '{label}'"+ return (+ f"Successfully deleted memory block '{label}'. "+ f"Your system prompt has been recompiled without this memory block and is now active in your context."+ )except NoResultFound:# Catch the specific error and re-raise with human-readable names@@ -856,7 +853,10 @@ class LettaCoreToolExecutor(ToolExecutor):)await self.agent_manager.rebuild_system_prompt_async(agent_id=agent_state.id, actor=actor, force=True)- return f"Successfully updated description of memory block '{label}'"+ return (+ f"Successfully updated description of memory block '{label}'. "+ f"Your system prompt has been recompiled with the updated description and is now active in your context."+ )except NoResultFound:# Catch the specific error and re-raise with human-readable names@@ -879,7 +879,10 @@ class LettaCoreToolExecutor(ToolExecutor):await self.block_manager.update_block_async(block_id=memory_block.id, block_update=BlockUpdate(label=new_label), actor=actor)await self.agent_manager.rebuild_system_prompt_async(agent_id=agent_state.id, actor=actor, force=True)- return f"Successfully renamed memory block '{old_label}' to '{new_label}'"+ return (+ f"Successfully renamed memory block '{old_label}' to '{new_label}'. "+ f"Your system prompt has been recompiled with the renamed memory block and is now active in your context."+ )except NoResultFound:# Catch the specific error and re-raise with human-readable names@@ -906,7 +909,10 @@ class LettaCoreToolExecutor(ToolExecutor):agent_state.memory.set_block(persisted_block)await self.agent_manager.update_memory_if_changed_async(agent_id=agent_state.id, new_memory=agent_state.memory, actor=actor)- return f"Successfully created memory block '{label}'"+ return (+ f"Successfully created memory block '{label}'. "+ f"Your system prompt has been recompiled with the new memory block and is now active in your context."+ )async def memory_str_replace(self, agent_state: AgentState, actor: User, path: str, old_str: str, new_str: str) -> str:"""Replace text in a memory block."""@@ -963,10 +969,11 @@ class LettaCoreToolExecutor(ToolExecutor):await self.agent_manager.rebuild_system_prompt_async(agent_id=agent_state.id, actor=actor, force=True)# Prepare the success message- success_msg = f"The core memory block with label `{label}` has been edited. "- success_msg += (- "Review the changes and make sure they are as expected (correct indentation, "- "no duplicate lines, etc). Edit the memory block again if necessary."+ success_msg = (+ f"The core memory block with label `{label}` has been successfully edited. "+ f"Your system prompt has been recompiled with the updated memory contents and is now active in your context. "+ f"Review the changes and make sure they are as expected (correct indentation, "+ f"no duplicate lines, etc). Edit the memory block again if necessary.")return success_msg@@ -1029,10 +1036,11 @@ class LettaCoreToolExecutor(ToolExecutor):await self.agent_manager.rebuild_system_prompt_async(agent_id=agent_state.id, actor=actor, force=True)# Prepare the success message- success_msg = f"The core memory block with label `{label}` has been edited. "- success_msg += (- "Review the changes and make sure they are as expected (correct indentation, "- "no duplicate lines, etc). Edit the memory block again if necessary."+ success_msg = (+ f"The core memory block with label `{label}` has been successfully edited. "+ f"Your system prompt has been recompiled with the updated memory contents and is now active in your context. "+ f"Review the changes and make sure they are as expected (correct indentation, "+ f"no duplicate lines, etc). Edit the memory block again if necessary.")return success_msg