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

Rafael Uzarowski committed Jul 21, 2025 at 15:25 UTC 851301f6d6eb041eded560d17c6297c11fed8fb6
2 files changed +9 -22
agent.py
+5 -9
@@ -186,6 +186,11 @@ class AgentContext:
186 # this wrapper ensures that superior agents are called back if the chat was loaded from file and original callstack is gone
187 async def _process_chain(self, agent: "Agent", msg: "UserMessage|str", user=True):
188 try:
189 + # Initialize agent with welcome message only once per session (before first user message)
190 + if user and not agent.get_data("_session_initialized"):
191 + await agent.call_extensions("agent_init")
192 + agent.set_data("_session_initialized", True)
193 +
194 msg_template = (
195 agent.hist_add_user_message(msg) # type: ignore
196 if user
@@ -296,15 +301,6 @@ class Agent:
301 self.intervention: UserMessage | None = None
302 self.data = {} # free data object all the tools can use
303
299 - # Initialize main agent with extensions (including initial message)
300 - # Only for main agent (A0) to avoid multiple calls for subordinate agents
301 - if self.number == 0:
302 - try:
303 - asyncio.run(self.call_extensions("agent_init"))
304 - except RuntimeError:
305 - # No event loop running, which is fine - initialization will happen on first use
306 - pass
307 -
304
305
306 async def monologue(self):
python/extensions/agent_init/_10_initial_message.py
+4 -13
@@ -7,21 +7,14 @@ class InitialMessage(Extension):
7
8 async def execute(self, **kwargs):
9 """
10 - Add an initial greeting message to the chat when agent is initialized.
11 - This happens when a new chat is created or when chat is reset.
12 - Only adds the message once per chat session using persisted metadata.
10 + Add an initial greeting message when first user message is processed.
11 + Called only once per session via _process_chain method.
12 """
13
14 # Only add initial message for main agent (A0), not subordinate agents
15 if self.agent.number != 0:
16 return
17
19 - # Check if initial message already exists in history
20 - history_output = self.agent.history.output()
21 - for msg in history_output:
22 - if "content" in msg and msg["content"]:
23 - return # Initial message already exists, skip
24 -
18 # Construct the initial message from prompt template
19 initial_message = self.agent.read_prompt("fw.initial_message.md")
20
@@ -36,10 +29,8 @@ class InitialMessage(Extension):
29 initial_message_text = initial_message_json.get("tool_args", {}).get("text", "Hello! How can I help you?")
30
31 # Add to log (green bubble) for immediate UI display
39 - log_item = self.agent.context.log.log(
32 + self.agent.context.log.log(
33 type="response",
34 heading=f"{self.agent.agent_name}: Welcome",
35 content=initial_message_text
43 - )
44 - # Mark the log item as finished to clear from status bar
45 - log_item.update(finished=True)
36 + ).update(finished=True)