fix(subagent): keep the subagent prompt prefix cacheable (#12062)
This commit is contained in:
@@ -24,7 +24,6 @@ pub type OnMessageCallback = Arc<dyn Fn(&Message) + Send + Sync>;
|
||||
#[derive(Serialize)]
|
||||
pub struct SubagentPromptContext {
|
||||
pub max_turns: usize,
|
||||
pub subagent_id: String,
|
||||
pub task_instructions: String,
|
||||
pub tool_count: usize,
|
||||
pub available_tools: String,
|
||||
@@ -163,11 +162,15 @@ fn get_agent_messages(params: SubagentRunParams) -> AgentMessagesFuture {
|
||||
.apply_recipe_components(recipe.response.clone(), true)
|
||||
.await?;
|
||||
|
||||
let max_turns = task_config
|
||||
.max_turns
|
||||
.expect("TaskConfig always sets max_turns");
|
||||
let subagent_prompt =
|
||||
build_subagent_prompt(&agent, &task_config, &session_id, system_instructions).await?;
|
||||
build_subagent_prompt(&agent, max_turns, &session_id, system_instructions).await?;
|
||||
agent.override_system_prompt(subagent_prompt).await;
|
||||
|
||||
let user_message = Message::user().with_text(user_task);
|
||||
let user_message =
|
||||
Message::user().with_text(format!("Subagent ID: {session_id}\n\n{user_task}"));
|
||||
let mut conversation = Conversation::new_unvalidated(vec![user_message.clone()]);
|
||||
|
||||
agent
|
||||
@@ -245,30 +248,25 @@ fn get_agent_messages(params: SubagentRunParams) -> AgentMessagesFuture {
|
||||
|
||||
async fn build_subagent_prompt(
|
||||
agent: &Agent,
|
||||
task_config: &TaskConfig,
|
||||
max_turns: usize,
|
||||
session_id: &str,
|
||||
system_instructions: String,
|
||||
) -> Result<String> {
|
||||
let tools: Vec<_> = agent
|
||||
let mut tool_names: Vec<_> = agent
|
||||
.list_tools(session_id, None)
|
||||
.await
|
||||
.into_iter()
|
||||
.filter(super::reply_parts::is_tool_visible_to_model)
|
||||
.map(|t| t.name.to_string())
|
||||
.collect();
|
||||
tool_names.sort_unstable();
|
||||
render_template(
|
||||
"subagent_system.md",
|
||||
&SubagentPromptContext {
|
||||
max_turns: task_config
|
||||
.max_turns
|
||||
.expect("TaskConfig always sets max_turns"),
|
||||
subagent_id: session_id.to_string(),
|
||||
max_turns,
|
||||
task_instructions: system_instructions,
|
||||
tool_count: tools.len(),
|
||||
available_tools: tools
|
||||
.iter()
|
||||
.map(|t| t.name.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join(", "),
|
||||
tool_count: tool_names.len(),
|
||||
available_tools: tool_names.join(", "),
|
||||
},
|
||||
)
|
||||
.map_err(|e| anyhow!("Failed to render subagent system prompt: {}", e))
|
||||
|
||||
@@ -9,10 +9,6 @@ You are an autonomous subagent with these characteristics:
|
||||
- **Security**: Cannot spawn additional subagents
|
||||
The maximum number of turns to respond is {{max_turns}}.
|
||||
|
||||
{% if subagent_id is defined %}
|
||||
**Subagent ID**: {{subagent_id}}
|
||||
{% endif %}
|
||||
|
||||
{% if task_instructions %}
|
||||
# Task Instructions
|
||||
{{task_instructions}}
|
||||
|
||||
Reference in New Issue
Block a user