Skip to content

Agent Zero AI framework

Add initial user turn to preserve AI greeting turn order

linkliti85cc95264fa5
diff --git a/extensions/python/agent_init/_10_initial_message.py b/extensions/python/agent_init/_10_initial_message.py
index cca5f798e..a017db2dc 100644
--- a/extensions/python/agent_init/_10_initial_message.py
+++ b/extensions/python/agent_init/_10_initial_message.py
@@ -1,5 +1,5 @@
import json
-from agent import LoopData
+from agent import LoopData, UserMessage
from helpers.extension import Extension
@@ -21,19 +21,27 @@ class InitialMessage(Extension):
if self.agent.context.log.logs:
return
+ # Add initial user turn ("Hello!") so AI greeting is not the first message
+ initial_user_message = self.agent.read_prompt("fw.initial_user_message.md").strip()
+ self.agent.hist_add_user_message(UserMessage(message=initial_user_message))
+
# Construct the initial message from prompt template
initial_message = self.agent.read_prompt("fw.initial_message.md")
+ # Minify JSON message; fall back to raw text if parsing fails
+ try:
+ initial_message_json = json.loads(initial_message)
+ initial_message = json.dumps(initial_message_json, separators=(",", ":"))
+ initial_message_text = initial_message_json.get("tool_args", {}).get("text", "Hello! How can I help you?")
+ except (json.JSONDecodeError, TypeError):
+ initial_message_text = initial_message
+
# add initial loop data to agent (for hist_add_ai_response)
self.agent.loop_data = LoopData(user_message=None)
# Add the message to history as an AI response
msg = 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
self.agent.context.log.log(
type="response",
diff --git a/prompts/AGENTS.md b/prompts/AGENTS.md
index 8f74aab5d..960fb39a5 100644
--- a/prompts/AGENTS.md
+++ b/prompts/AGENTS.md
@@ -26,6 +26,7 @@
schemas.
- Summary prompts that compress history should preserve loaded skill names from `skill_instructions` metadata without copying full skill bodies.
- `Agent.read_prompt` and `Agent.parse_prompt` strip trailing newlines from returned content, so prompt template files may safely keep a final newline. Do not rely on trailing newlines for formatting; render-time joins in `output_text()` already separate turns.
+- `fw.initial_user_message.md` supplies the placeholder user turn (`Hello!`) added by `extensions/python/agent_init/_10_initial_message.py` before the AI greeting from `fw.initial_message.md`. The user-first pair guarantees `output_langchain` does not pop the greeting as a leading `AIMessage`, so the initial message appears on both project and non-project paths. Do not remove either prompt without replacing the turn-order guarantee.
## Work Guidance
diff --git a/prompts/fw.initial_user_message.md b/prompts/fw.initial_user_message.md
new file mode 100644
index 000000000..10ddd6d25
--- /dev/null
+++ b/prompts/fw.initial_user_message.md
@@ -0,0 +1 @@
+Hello!