| 1 | import json |
| 2 | from agent import LoopData |
| 3 | from helpers.extension import Extension |
| 4 | |
| 5 | |
| 6 | class InitialMessage(Extension): |
| 7 | |
| 8 | def execute(self, **kwargs): |
| 9 | """ |
| 10 | Add an initial greeting message when first user message is processed. |
| 11 | Called only once per session via _process_chain method. |
| 12 | """ |
| 13 | if not self.agent: |
| 14 | return |
| 15 | |
| 16 | # Only add initial message for main agent (A0), not subordinate agents |
| 17 | if self.agent.number != 0: |
| 18 | return |
| 19 | |
| 20 | # If the context already contains log messages, do not add another initial message |
| 21 | if self.agent.context.log.logs: |
| 22 | return |
| 23 | |
| 24 | # Construct the initial message from prompt template |
| 25 | initial_message = self.agent.read_prompt("fw.initial_message.md") |
| 26 | |
| 27 | # add initial loop data to agent (for hist_add_ai_response) |
| 28 | self.agent.loop_data = LoopData(user_message=None) |
| 29 | |
| 30 | # Add the message to history as an AI response |
| 31 | msg = self.agent.hist_add_ai_response(initial_message) |
| 32 | |
| 33 | # json parse the message, get the tool_args text |
| 34 | initial_message_json = json.loads(initial_message) |
| 35 | initial_message_text = initial_message_json.get("tool_args", {}).get("text", "Hello! How can I help you?") |
| 36 | |
| 37 | # Add to log (green bubble) for immediate UI display |
| 38 | self.agent.context.log.log( |
| 39 | type="response", |
| 40 | content=initial_message_text, |
| 41 | finished=True, |
| 42 | update_progress="none", |
| 43 | id=msg.id, |
| 44 | ) |