Skip to content

Agent Zero AI framework

feat: initial message in chat (WIP, not working)

Rafael Uzarowski41bc8d8132ee
diff --git a/agent.py b/agent.py
index abe5276ca..9f2bfa1fd 100644
--- a/agent.py
+++ b/agent.py
@@ -296,6 +296,17 @@ 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):
while True:
try:
diff --git a/prompts/default/fw.initial_message.md b/prompts/default/fw.initial_message.md
new file mode 100644
index 000000000..76ae3f2c0
--- /dev/null
+++ b/prompts/default/fw.initial_message.md
@@ -0,0 +1,14 @@
+```json
+{
+ "thoughts": [
+ "This is a new conversation, I should greet the user warmly and let them know I'm ready to help.",
+ "I'll use the response tool with proper JSON formatting to demonstrate the expected structure.",
+ "Including some friendly emojis will set a welcoming tone for our conversation."
+ ],
+ "headline": "Greeting user and starting conversation",
+ "tool_name": "response",
+ "tool_args": {
+ "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?"
+ }
+}
+```
diff --git a/python/extensions/agent_init/_10_initial_message.py b/python/extensions/agent_init/_10_initial_message.py
new file mode 100644
index 000000000..294ca339a
--- /dev/null
+++ b/python/extensions/agent_init/_10_initial_message.py
@@ -0,0 +1,45 @@
+import json
+from agent import LoopData
+from python.helpers.extension import Extension
+
+
+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.
+ """
+
+ # 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")
+
+ # add loop data to agent
+ self.agent.loop_data = LoopData(user_message=None)
+
+ # Add the message to history as an AI response
+ self.agent.hist_add_ai_response(initial_message)
+
+ # json parse the message, get the tool_args text
+ initial_message_json = json.loads(initial_message)
+ 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(
+ 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)