Files
agent-zero/extensions/python/agent_init/_10_initial_message.py
T
linkliti a159b4b624 fix(chat-naming): exclude greeting seed from naming and schedule from monologue_start
The agent-init greeting extension seeds every new chat with a synthetic user turn that chat naming treated as a real user message, so fresh chats were named after the greeting and later band-aids delayed or re-defaulted naming to escape the trap. Mark the seed turn with initial_seed metadata at creation, skip marked turns in user-message selection so seed-only chats schedule no Utility Model call and a real first user message names the chat immediately, and schedule naming from monologue_start again since the background task cannot delay the Main Model.

Validation: pytest plugins/_chat_naming/tests/test_chat_naming.py tests/test_initial_message.py (19 passed) on the framework runtime.
2026-10-13 18:06:47 +03:00

55 lines
2.1 KiB
Python

import json
from agent import LoopData, UserMessage
from helpers.extension import Extension
from helpers.llm_result import LLMResult
class InitialMessage(Extension):
def execute(self, **kwargs):
"""
Add an initial greeting message when first user message is processed.
Called only once per session via _process_chain method.
"""
if not self.agent:
return
# Only add initial message for main agent (A0), not subordinate agents
if self.agent.number != 0:
return
# If the context already contains log messages, do not add another initial message
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()
seed_message = self.agent.hist_add_user_message(UserMessage(message=initial_user_message))
seed_message.metadata["initial_seed"] = True
# 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, llm_result=LLMResult.non_llm())
# Add to log (green bubble) for immediate UI display
self.agent.context.log.log(
type="response",
content=initial_message_text,
finished=True,
update_progress="none",
id=msg.id,
)