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

Co-authored-by: Rafael Uzarowski <uzarowski.rafael@proton.me>

ehl0wr0ld committed Aug 7, 2025 at 14:40 UTC 1a5ec4c264ece10143587e4752e7308556613dae
3 files changed +20 -4
python/api/poll.py
+6 -1
@@ -1,6 +1,6 @@
1 from python.helpers.api import ApiHandler, Request, Response
2
3 -from agent import AgentContext
3 +from agent import AgentContext, AgentContextType
4
5 from python.helpers.task_scheduler import TaskScheduler
6 from python.helpers.localization import Localization
@@ -48,6 +48,11 @@ class Poll(ApiHandler):
48 if ctx.id in processed_contexts:
49 continue
50
51 + # Skip BACKGROUND contexts as they should be invisible to users
52 + if ctx.type == AgentContextType.BACKGROUND:
53 + processed_contexts.add(ctx.id)
54 + continue
55 +
56 # Create the base context data that will be returned
57 context_data = ctx.serialize()
58
python/extensions/message_loop_end/_90_save_chat.py
+6 -2
@@ -1,8 +1,12 @@
1 from python.helpers.extension import Extension
2 -from agent import LoopData
2 +from agent import LoopData, AgentContextType
3 from python.helpers import persist_chat
4
5
6 class SaveChat(Extension):
7 async def execute(self, loop_data: LoopData = LoopData(), **kwargs):
8 - persist_chat.save_tmp_chat(self.agent.context)
\ No newline at end of file
8 + # Skip saving BACKGROUND contexts as they should be ephemeral
9 + if self.agent.context.type == AgentContextType.BACKGROUND:
10 + return
11 +
12 + persist_chat.save_tmp_chat(self.agent.context)
python/helpers/persist_chat.py
+8 -1
@@ -29,6 +29,10 @@ def get_chat_folder_path(ctxid: str):
29
30 def save_tmp_chat(context: AgentContext):
31 """Save context to the chats folder"""
32 + # Skip saving BACKGROUND contexts as they should be ephemeral
33 + if context.type == AgentContextType.BACKGROUND:
34 + return
35 +
36 path = _get_chat_file_path(context.id)
37 files.make_dirs(path)
38 data = _serialize_context(context)
@@ -39,6 +43,9 @@ def save_tmp_chat(context: AgentContext):
43 def save_tmp_chats():
44 """Save all contexts to the chats folder"""
45 for _, context in AgentContext._contexts.items():
46 + # Skip BACKGROUND contexts as they should be ephemeral
47 + if context.type == AgentContextType.BACKGROUND:
48 + continue
49 save_tmp_chat(context)
50
51
@@ -180,7 +187,7 @@ def _deserialize_context(data):
187 agents = data.get("agents", [])
188 agent0 = _deserialize_agents(agents, config, context)
189 streaming_agent = agent0
183 - while streaming_agent.number != data.get("streaming_agent", 0):
190 + while streaming_agent and streaming_agent.number != data.get("streaming_agent", 0):
191 streaming_agent = streaming_agent.data.get(Agent.DATA_NAME_SUBORDINATE, None)
192
193 context.agent0 = agent0