Skip to content

Agent Zero AI framework

feat: make agentcontexttype background invisible to user (#653)

ehl0wr0ld1a5ec4c264ec
diff --git a/python/api/poll.py b/python/api/poll.py
index f36291d47..698321cbd 100644
--- a/python/api/poll.py
+++ b/python/api/poll.py
@@ -1,6 +1,6 @@
from python.helpers.api import ApiHandler, Request, Response
-from agent import AgentContext
+from agent import AgentContext, AgentContextType
from python.helpers.task_scheduler import TaskScheduler
from python.helpers.localization import Localization
@@ -48,6 +48,11 @@ class Poll(ApiHandler):
if ctx.id in processed_contexts:
continue
+ # Skip BACKGROUND contexts as they should be invisible to users
+ if ctx.type == AgentContextType.BACKGROUND:
+ processed_contexts.add(ctx.id)
+ continue
+
# Create the base context data that will be returned
context_data = ctx.serialize()
diff --git a/python/extensions/message_loop_end/_90_save_chat.py b/python/extensions/message_loop_end/_90_save_chat.py
index 0d4cebb8e..ea81bed0c 100644
--- a/python/extensions/message_loop_end/_90_save_chat.py
+++ b/python/extensions/message_loop_end/_90_save_chat.py
@@ -1,8 +1,12 @@
from python.helpers.extension import Extension
-from agent import LoopData
+from agent import LoopData, AgentContextType
from python.helpers import persist_chat
class SaveChat(Extension):
async def execute(self, loop_data: LoopData = LoopData(), **kwargs):
- persist_chat.save_tmp_chat(self.agent.context)
\ No newline at end of file
+ # Skip saving BACKGROUND contexts as they should be ephemeral
+ if self.agent.context.type == AgentContextType.BACKGROUND:
+ return
+
+ persist_chat.save_tmp_chat(self.agent.context)
diff --git a/python/helpers/persist_chat.py b/python/helpers/persist_chat.py
index 357455b3a..f6ea35bb2 100644
--- a/python/helpers/persist_chat.py
+++ b/python/helpers/persist_chat.py
@@ -29,6 +29,10 @@ def get_chat_folder_path(ctxid: str):
def save_tmp_chat(context: AgentContext):
"""Save context to the chats folder"""
+ # Skip saving BACKGROUND contexts as they should be ephemeral
+ if context.type == AgentContextType.BACKGROUND:
+ return
+
path = _get_chat_file_path(context.id)
files.make_dirs(path)
data = _serialize_context(context)
@@ -39,6 +43,9 @@ def save_tmp_chat(context: AgentContext):
def save_tmp_chats():
"""Save all contexts to the chats folder"""
for _, context in AgentContext._contexts.items():
+ # Skip BACKGROUND contexts as they should be ephemeral
+ if context.type == AgentContextType.BACKGROUND:
+ continue
save_tmp_chat(context)
@@ -180,7 +187,7 @@ def _deserialize_context(data):
agents = data.get("agents", [])
agent0 = _deserialize_agents(agents, config, context)
streaming_agent = agent0
- while streaming_agent.number != data.get("streaming_agent", 0):
+ while streaming_agent and streaming_agent.number != data.get("streaming_agent", 0):
streaming_agent = streaming_agent.data.get(Agent.DATA_NAME_SUBORDINATE, None)
context.agent0 = agent0