buildInvariantSystemMessages builds system messages that are identical for all users of a given agent configuration. These messages can be cached efficiently as they don't change between sessions, users, or projects. These messages are determined solely by the agent configuration and remain constan
(a *agent.Agent)
| 1051 | // These messages are determined solely by the agent configuration and |
| 1052 | // remain constant across different sessions, users, and working directories. |
| 1053 | func buildInvariantSystemMessages(a *agent.Agent) []chat.Message { |
| 1054 | var messages []chat.Message |
| 1055 | |
| 1056 | if a.HasSubAgents() { |
| 1057 | subAgents := a.SubAgents() |
| 1058 | |
| 1059 | var text strings.Builder |
| 1060 | var validAgentIDs []string |
| 1061 | for _, subAgent := range subAgents { |
| 1062 | text.WriteString("Name: ") |
| 1063 | text.WriteString(subAgent.Name()) |
| 1064 | text.WriteString(" | Description: ") |
| 1065 | text.WriteString(subAgent.Description()) |
| 1066 | text.WriteString("\n") |
| 1067 | |
| 1068 | validAgentIDs = append(validAgentIDs, subAgent.Name()) |
| 1069 | } |
| 1070 | |
| 1071 | messages = append(messages, chat.Message{ |
| 1072 | Role: chat.MessageRoleSystem, |
| 1073 | Content: "You are a multi-agent system, make sure to answer the user query in the most helpful way possible. You have access to these sub-agents:\n" + text.String() + "\nIMPORTANT: You can ONLY transfer tasks to the agents listed above using their ID. The valid agent names are: " + strings.Join(validAgentIDs, ", ") + ". You MUST NOT attempt to transfer to any other agent IDs - doing so will cause system errors.\n\nIf you are the best to answer the question according to your description, you can answer it.\n\nIf another agent is better for answering the question according to its description, call `transfer_task` function to transfer the question to that agent using the agent's ID. When transferring, do not generate any text other than the function call.\n\nWhen the task involves files, always include their absolute paths in the `task` description (never just bare filenames). Sub-agents start in a fresh session and do not see the conversation history or files attached by the user, so a non-absolute path may resolve to the wrong file or force the sub-agent to scan the filesystem.\n\n", |
| 1074 | }) |
| 1075 | } |
| 1076 | |
| 1077 | if handoffs := a.Handoffs(); len(handoffs) > 0 { |
| 1078 | var text strings.Builder |
| 1079 | var validAgentIDs []string |
| 1080 | for _, agent := range handoffs { |
| 1081 | text.WriteString("Name: ") |
| 1082 | text.WriteString(agent.Name()) |
| 1083 | text.WriteString(" | Description: ") |
| 1084 | text.WriteString(agent.Description()) |
| 1085 | text.WriteString("\n") |
| 1086 | |
| 1087 | validAgentIDs = append(validAgentIDs, agent.Name()) |
| 1088 | } |
| 1089 | |
| 1090 | handoffPrompt := "You are part of a multi-agent team. Your goal is to answer the user query in the most helpful way possible.\n\n" + |
| 1091 | "Available agents in your team:\n" + text.String() + "\n" + |
| 1092 | "You can hand off the conversation to any of these agents at any time by using the `handoff` function with their ID. " + |
| 1093 | "The valid agent IDs are: " + strings.Join(validAgentIDs, ", ") + ".\n\n" + |
| 1094 | "When to hand off:\n" + |
| 1095 | "- If another agent's description indicates they are better suited for the current task or question\n" + |
| 1096 | "- If the user explicitly asks for a specific agent\n" + |
| 1097 | "- If you need specialized capabilities that another agent provides\n\n" + |
| 1098 | "If you are the best agent to handle the current request based on your capabilities, respond directly. " + |
| 1099 | "When handing off to another agent, only handoff without talking about the handoff." |
| 1100 | |
| 1101 | messages = append(messages, chat.Message{ |
| 1102 | Role: chat.MessageRoleSystem, |
| 1103 | Content: handoffPrompt, |
| 1104 | }) |
| 1105 | } |
| 1106 | |
| 1107 | if instructions := a.Instruction(); instructions != "" { |
| 1108 | messages = append(messages, chat.Message{ |
| 1109 | Role: chat.MessageRoleSystem, |
| 1110 | Content: instructions, |
no test coverage detected