Skip to content

Agent Zero AI framework

feat: initial message now after first user message (working)

Rafael Uzarowski851301f6d6eb
diff --git a/agent.py b/agent.py
index 9f2bfa1fd..5cb0e576b 100644
--- a/agent.py
+++ b/agent.py
@@ -186,6 +186,11 @@ class AgentContext:
# this wrapper ensures that superior agents are called back if the chat was loaded from file and original callstack is gone
async def _process_chain(self, agent: "Agent", msg: "UserMessage|str", user=True):
try:
+ # Initialize agent with welcome message only once per session (before first user message)
+ if user and not agent.get_data("_session_initialized"):
+ await agent.call_extensions("agent_init")
+ agent.set_data("_session_initialized", True)
+
msg_template = (
agent.hist_add_user_message(msg) # type: ignore
if user
@@ -296,15 +301,6 @@ class Agent:
self.intervention: UserMessage | None = None
self.data = {} # free data object all the tools can use
- # Initialize main agent with extensions (including initial message)
- # Only for main agent (A0) to avoid multiple calls for subordinate agents
- if self.number == 0:
- try:
- asyncio.run(self.call_extensions("agent_init"))
- except RuntimeError:
- # No event loop running, which is fine - initialization will happen on first use
- pass
-
async def monologue(self):
diff --git a/python/extensions/agent_init/_10_initial_message.py b/python/extensions/agent_init/_10_initial_message.py
index 294ca339a..21650d8a8 100644
--- a/python/extensions/agent_init/_10_initial_message.py
+++ b/python/extensions/agent_init/_10_initial_message.py
@@ -7,21 +7,14 @@ class InitialMessage(Extension):
async def execute(self, **kwargs):
"""
- Add an initial greeting message to the chat when agent is initialized.
- This happens when a new chat is created or when chat is reset.
- Only adds the message once per chat session using persisted metadata.
+ Add an initial greeting message when first user message is processed.
+ Called only once per session via _process_chain method.
"""
# Only add initial message for main agent (A0), not subordinate agents
if self.agent.number != 0:
return
- # Check if initial message already exists in history
- history_output = self.agent.history.output()
- for msg in history_output:
- if "content" in msg and msg["content"]:
- return # Initial message already exists, skip
-
# Construct the initial message from prompt template
initial_message = self.agent.read_prompt("fw.initial_message.md")
@@ -36,10 +29,8 @@ class InitialMessage(Extension):
initial_message_text = initial_message_json.get("tool_args", {}).get("text", "Hello! How can I help you?")
# Add to log (green bubble) for immediate UI display
- log_item = self.agent.context.log.log(
+ self.agent.context.log.log(
type="response",
heading=f"{self.agent.agent_name}: Welcome",
content=initial_message_text
- )
- # Mark the log item as finished to clear from status bar
- log_item.update(finished=True)
+ ).update(finished=True)