feat: initial message in chat (WIP, not working)
Rafael Uzarowski committed
Jul 21, 2025 at 14:10 UTC
41bc8d8132eee695cd309a74a9b175d80b3d0d17
3 files changed
+70
agent.py
+11
@@ -296,6 +296,17 @@ class Agent:
296
self.intervention: UserMessage | None = None
297
self.data = {} # free data object all the tools can use
298
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
+
308
+
309
+
310
async def monologue(self):
311
while True:
312
try:
prompts/default/fw.initial_message.md
new
+14
@@ -0,0 +1,14 @@
1
+```json
2
+{
3
+ "thoughts": [
4
+ "This is a new conversation, I should greet the user warmly and let them know I'm ready to help.",
5
+ "I'll use the response tool with proper JSON formatting to demonstrate the expected structure.",
6
+ "Including some friendly emojis will set a welcoming tone for our conversation."
7
+ ],
8
+ "headline": "Greeting user and starting conversation",
9
+ "tool_name": "response",
10
+ "tool_args": {
11
+ "text": "Hello! 👋 Welcome to our conversation. I'm Agent Zero, your AI assistant, and I'm here to help you with any questions, tasks, or problems you might have.\n\nI can assist you with:\n- 💻 Programming and coding tasks\n- 📊 Data analysis and research \n- 🛠️ System administration and troubleshooting\n- 📝 Writing and documentation\n- 🔍 Web searches and information gathering\n- And much more!\n\nJust let me know what you'd like to work on, and I'll do my best to help you! 😊\n\nWhat can I help you with today?"
12
+ }
13
+}
14
+```
python/extensions/agent_init/_10_initial_message.py
new
+45
@@ -0,0 +1,45 @@
1
+import json
2
+from agent import LoopData
3
+from python.helpers.extension import Extension
4
+
5
+
6
+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.
13
+ """
14
+
15
+ # Only add initial message for main agent (A0), not subordinate agents
16
+ if self.agent.number != 0:
17
+ return
18
+
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
+
25
+ # Construct the initial message from prompt template
26
+ initial_message = self.agent.read_prompt("fw.initial_message.md")
27
+
28
+ # add loop data to agent
29
+ self.agent.loop_data = LoopData(user_message=None)
30
+
31
+ # Add the message to history as an AI response
32
+ self.agent.hist_add_ai_response(initial_message)
33
+
34
+ # json parse the message, get the tool_args text
35
+ initial_message_json = json.loads(initial_message)
36
+ initial_message_text = initial_message_json.get("tool_args", {}).get("text", "Hello! How can I help you?")
37
+
38
+ # Add to log (green bubble) for immediate UI display
39
+ log_item = self.agent.context.log.log(
40
+ type="response",
41
+ heading=f"{self.agent.agent_name}: Welcome",
42
+ content=initial_message_text
43
+ )
44
+ # Mark the log item as finished to clear from status bar
45
+ log_item.update(finished=True)