adk_agent/
llm_agent.rs

1use adk_core::{
2    AfterAgentCallback, AfterModelCallback, AfterToolCallback, Agent, BeforeAgentCallback,
3    BeforeModelCallback, BeforeModelResult, BeforeToolCallback, CallbackContext, Content, Event,
4    EventActions, GlobalInstructionProvider, InstructionProvider, InvocationContext, Llm,
5    LlmRequest, MemoryEntry, Part, ReadonlyContext, Result, Tool, ToolContext,
6};
7use async_stream::stream;
8use async_trait::async_trait;
9use std::sync::Arc;
10
11pub struct LlmAgent {
12    name: String,
13    description: String,
14    model: Arc<dyn Llm>,
15    instruction: Option<String>,
16    instruction_provider: Option<Arc<InstructionProvider>>,
17    global_instruction: Option<String>,
18    global_instruction_provider: Option<Arc<GlobalInstructionProvider>>,
19    #[allow(dead_code)] // Part of public API via builder
20    input_schema: Option<serde_json::Value>,
21    output_schema: Option<serde_json::Value>,
22    #[allow(dead_code)] // Part of public API via builder
23    disallow_transfer_to_parent: bool,
24    #[allow(dead_code)] // Part of public API via builder
25    disallow_transfer_to_peers: bool,
26    include_contents: adk_core::IncludeContents,
27    tools: Vec<Arc<dyn Tool>>,
28    sub_agents: Vec<Arc<dyn Agent>>,
29    output_key: Option<String>,
30    before_callbacks: Arc<Vec<BeforeAgentCallback>>,
31    after_callbacks: Arc<Vec<AfterAgentCallback>>,
32    before_model_callbacks: Arc<Vec<BeforeModelCallback>>,
33    after_model_callbacks: Arc<Vec<AfterModelCallback>>,
34    before_tool_callbacks: Arc<Vec<BeforeToolCallback>>,
35    after_tool_callbacks: Arc<Vec<AfterToolCallback>>,
36}
37
38impl std::fmt::Debug for LlmAgent {
39    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40        f.debug_struct("LlmAgent")
41            .field("name", &self.name)
42            .field("description", &self.description)
43            .field("model", &self.model.name())
44            .field("instruction", &self.instruction)
45            .field("tools_count", &self.tools.len())
46            .field("sub_agents_count", &self.sub_agents.len())
47            .finish()
48    }
49}
50
51pub struct LlmAgentBuilder {
52    name: String,
53    description: Option<String>,
54    model: Option<Arc<dyn Llm>>,
55    instruction: Option<String>,
56    instruction_provider: Option<Arc<InstructionProvider>>,
57    global_instruction: Option<String>,
58    global_instruction_provider: Option<Arc<GlobalInstructionProvider>>,
59    input_schema: Option<serde_json::Value>,
60    output_schema: Option<serde_json::Value>,
61    disallow_transfer_to_parent: bool,
62    disallow_transfer_to_peers: bool,
63    include_contents: adk_core::IncludeContents,
64    tools: Vec<Arc<dyn Tool>>,
65    sub_agents: Vec<Arc<dyn Agent>>,
66    output_key: Option<String>,
67    before_callbacks: Vec<BeforeAgentCallback>,
68    after_callbacks: Vec<AfterAgentCallback>,
69    before_model_callbacks: Vec<BeforeModelCallback>,
70    after_model_callbacks: Vec<AfterModelCallback>,
71    before_tool_callbacks: Vec<BeforeToolCallback>,
72    after_tool_callbacks: Vec<AfterToolCallback>,
73}
74
75impl LlmAgentBuilder {
76    pub fn new(name: impl Into<String>) -> Self {
77        Self {
78            name: name.into(),
79            description: None,
80            model: None,
81            instruction: None,
82            instruction_provider: None,
83            global_instruction: None,
84            global_instruction_provider: None,
85            input_schema: None,
86            output_schema: None,
87            disallow_transfer_to_parent: false,
88            disallow_transfer_to_peers: false,
89            include_contents: adk_core::IncludeContents::Default,
90            tools: Vec::new(),
91            sub_agents: Vec::new(),
92            output_key: None,
93            before_callbacks: Vec::new(),
94            after_callbacks: Vec::new(),
95            before_model_callbacks: Vec::new(),
96            after_model_callbacks: Vec::new(),
97            before_tool_callbacks: Vec::new(),
98            after_tool_callbacks: Vec::new(),
99        }
100    }
101
102    pub fn description(mut self, desc: impl Into<String>) -> Self {
103        self.description = Some(desc.into());
104        self
105    }
106
107    pub fn model(mut self, model: Arc<dyn Llm>) -> Self {
108        self.model = Some(model);
109        self
110    }
111
112    pub fn instruction(mut self, instruction: impl Into<String>) -> Self {
113        self.instruction = Some(instruction.into());
114        self
115    }
116
117    pub fn instruction_provider(mut self, provider: InstructionProvider) -> Self {
118        self.instruction_provider = Some(Arc::new(provider));
119        self
120    }
121
122    pub fn global_instruction(mut self, instruction: impl Into<String>) -> Self {
123        self.global_instruction = Some(instruction.into());
124        self
125    }
126
127    pub fn global_instruction_provider(mut self, provider: GlobalInstructionProvider) -> Self {
128        self.global_instruction_provider = Some(Arc::new(provider));
129        self
130    }
131
132    pub fn input_schema(mut self, schema: serde_json::Value) -> Self {
133        self.input_schema = Some(schema);
134        self
135    }
136
137    pub fn output_schema(mut self, schema: serde_json::Value) -> Self {
138        self.output_schema = Some(schema);
139        self
140    }
141
142    pub fn disallow_transfer_to_parent(mut self, disallow: bool) -> Self {
143        self.disallow_transfer_to_parent = disallow;
144        self
145    }
146
147    pub fn disallow_transfer_to_peers(mut self, disallow: bool) -> Self {
148        self.disallow_transfer_to_peers = disallow;
149        self
150    }
151
152    pub fn include_contents(mut self, include: adk_core::IncludeContents) -> Self {
153        self.include_contents = include;
154        self
155    }
156
157    pub fn output_key(mut self, key: impl Into<String>) -> Self {
158        self.output_key = Some(key.into());
159        self
160    }
161
162    pub fn tool(mut self, tool: Arc<dyn Tool>) -> Self {
163        self.tools.push(tool);
164        self
165    }
166
167    pub fn sub_agent(mut self, agent: Arc<dyn Agent>) -> Self {
168        self.sub_agents.push(agent);
169        self
170    }
171
172    pub fn before_callback(mut self, callback: BeforeAgentCallback) -> Self {
173        self.before_callbacks.push(callback);
174        self
175    }
176
177    pub fn after_callback(mut self, callback: AfterAgentCallback) -> Self {
178        self.after_callbacks.push(callback);
179        self
180    }
181
182    pub fn before_model_callback(mut self, callback: BeforeModelCallback) -> Self {
183        self.before_model_callbacks.push(callback);
184        self
185    }
186
187    pub fn after_model_callback(mut self, callback: AfterModelCallback) -> Self {
188        self.after_model_callbacks.push(callback);
189        self
190    }
191
192    pub fn before_tool_callback(mut self, callback: BeforeToolCallback) -> Self {
193        self.before_tool_callbacks.push(callback);
194        self
195    }
196
197    pub fn after_tool_callback(mut self, callback: AfterToolCallback) -> Self {
198        self.after_tool_callbacks.push(callback);
199        self
200    }
201
202    pub fn build(self) -> Result<LlmAgent> {
203        let model =
204            self.model.ok_or_else(|| adk_core::AdkError::Agent("Model is required".to_string()))?;
205
206        Ok(LlmAgent {
207            name: self.name,
208            description: self.description.unwrap_or_default(),
209            model,
210            instruction: self.instruction,
211            instruction_provider: self.instruction_provider,
212            global_instruction: self.global_instruction,
213            global_instruction_provider: self.global_instruction_provider,
214            input_schema: self.input_schema,
215            output_schema: self.output_schema,
216            disallow_transfer_to_parent: self.disallow_transfer_to_parent,
217            disallow_transfer_to_peers: self.disallow_transfer_to_peers,
218            include_contents: self.include_contents,
219            tools: self.tools,
220            sub_agents: self.sub_agents,
221            output_key: self.output_key,
222            before_callbacks: Arc::new(self.before_callbacks),
223            after_callbacks: Arc::new(self.after_callbacks),
224            before_model_callbacks: Arc::new(self.before_model_callbacks),
225            after_model_callbacks: Arc::new(self.after_model_callbacks),
226            before_tool_callbacks: Arc::new(self.before_tool_callbacks),
227            after_tool_callbacks: Arc::new(self.after_tool_callbacks),
228        })
229    }
230}
231
232// AgentToolContext wraps the parent InvocationContext and preserves all context
233// instead of throwing it away like SimpleToolContext did
234struct AgentToolContext {
235    parent_ctx: Arc<dyn InvocationContext>,
236    function_call_id: String,
237    actions: EventActions,
238}
239
240impl AgentToolContext {
241    fn new(parent_ctx: Arc<dyn InvocationContext>, function_call_id: String) -> Self {
242        Self { parent_ctx, function_call_id, actions: EventActions::default() }
243    }
244}
245
246#[async_trait]
247impl ReadonlyContext for AgentToolContext {
248    fn invocation_id(&self) -> &str {
249        self.parent_ctx.invocation_id()
250    }
251
252    fn agent_name(&self) -> &str {
253        self.parent_ctx.agent_name()
254    }
255
256    fn user_id(&self) -> &str {
257        // ✅ Delegate to parent - now tools get the real user_id!
258        self.parent_ctx.user_id()
259    }
260
261    fn app_name(&self) -> &str {
262        // ✅ Delegate to parent - now tools get the real app_name!
263        self.parent_ctx.app_name()
264    }
265
266    fn session_id(&self) -> &str {
267        // ✅ Delegate to parent - now tools get the real session_id!
268        self.parent_ctx.session_id()
269    }
270
271    fn branch(&self) -> &str {
272        self.parent_ctx.branch()
273    }
274
275    fn user_content(&self) -> &Content {
276        self.parent_ctx.user_content()
277    }
278}
279
280#[async_trait]
281impl CallbackContext for AgentToolContext {
282    fn artifacts(&self) -> Option<Arc<dyn adk_core::Artifacts>> {
283        // ✅ Delegate to parent - tools can now access artifacts!
284        self.parent_ctx.artifacts()
285    }
286}
287
288#[async_trait]
289impl ToolContext for AgentToolContext {
290    fn function_call_id(&self) -> &str {
291        &self.function_call_id
292    }
293
294    fn actions(&self) -> &EventActions {
295        &self.actions
296    }
297
298    async fn search_memory(&self, query: &str) -> Result<Vec<MemoryEntry>> {
299        // ✅ Delegate to parent's memory if available
300        if let Some(memory) = self.parent_ctx.memory() {
301            memory.search(query).await
302        } else {
303            Ok(vec![])
304        }
305    }
306}
307
308#[async_trait]
309impl Agent for LlmAgent {
310    fn name(&self) -> &str {
311        &self.name
312    }
313
314    fn description(&self) -> &str {
315        &self.description
316    }
317
318    fn sub_agents(&self) -> &[Arc<dyn Agent>] {
319        &self.sub_agents
320    }
321
322    #[adk_telemetry::instrument(
323        skip(self, ctx),
324        fields(
325            agent.name = %self.name,
326            agent.description = %self.description,
327            invocation.id = %ctx.invocation_id(),
328            user.id = %ctx.user_id(),
329            session.id = %ctx.session_id()
330        )
331    )]
332    async fn run(&self, ctx: Arc<dyn InvocationContext>) -> Result<adk_core::EventStream> {
333        adk_telemetry::info!("Starting agent execution");
334
335        let agent_name = self.name.clone();
336        let invocation_id = ctx.invocation_id().to_string();
337        let model = self.model.clone();
338        let tools = self.tools.clone();
339        let sub_agents = self.sub_agents.clone();
340
341        let instruction = self.instruction.clone();
342        let instruction_provider = self.instruction_provider.clone();
343        let global_instruction = self.global_instruction.clone();
344        let global_instruction_provider = self.global_instruction_provider.clone();
345        let output_key = self.output_key.clone();
346        let output_schema = self.output_schema.clone();
347        let include_contents = self.include_contents;
348        // Clone Arc references (cheap)
349        let before_agent_callbacks = self.before_callbacks.clone();
350        let after_agent_callbacks = self.after_callbacks.clone();
351        let before_model_callbacks = self.before_model_callbacks.clone();
352        let after_model_callbacks = self.after_model_callbacks.clone();
353        let _before_tool_callbacks = self.before_tool_callbacks.clone();
354        let _after_tool_callbacks = self.after_tool_callbacks.clone();
355
356        let s = stream! {
357            // ===== BEFORE AGENT CALLBACKS =====
358            // Execute before the agent starts running
359            // If any returns content, skip agent execution
360            for callback in before_agent_callbacks.as_ref() {
361                match callback(ctx.clone() as Arc<dyn CallbackContext>).await {
362                    Ok(Some(content)) => {
363                        // Callback returned content - yield it and skip agent execution
364                        let mut early_event = Event::new(&invocation_id);
365                        early_event.author = agent_name.clone();
366                        early_event.llm_response.content = Some(content);
367                        yield Ok(early_event);
368
369                        // Skip rest of agent execution and go to after callbacks
370                        for after_callback in after_agent_callbacks.as_ref() {
371                            match after_callback(ctx.clone() as Arc<dyn CallbackContext>).await {
372                                Ok(Some(after_content)) => {
373                                    let mut after_event = Event::new(&invocation_id);
374                                    after_event.author = agent_name.clone();
375                                    after_event.llm_response.content = Some(after_content);
376                                    yield Ok(after_event);
377                                    return;
378                                }
379                                Ok(None) => continue,
380                                Err(e) => {
381                                    yield Err(e);
382                                    return;
383                                }
384                            }
385                        }
386                        return;
387                    }
388                    Ok(None) => {
389                        // Continue to next callback
390                        continue;
391                    }
392                    Err(e) => {
393                        // Callback failed - propagate error
394                        yield Err(e);
395                        return;
396                    }
397                }
398            }
399
400            // ===== MAIN AGENT EXECUTION =====
401            let mut conversation_history = Vec::new();
402
403            // ===== PROCESS GLOBAL INSTRUCTION =====
404            // GlobalInstruction provides tree-wide personality/identity
405            if let Some(provider) = &global_instruction_provider {
406                // Dynamic global instruction via provider
407                let global_inst = provider(ctx.clone() as Arc<dyn ReadonlyContext>).await?;
408                if !global_inst.is_empty() {
409                    conversation_history.push(Content {
410                        role: "user".to_string(),
411                        parts: vec![Part::Text { text: global_inst }],
412                    });
413                }
414            } else if let Some(ref template) = global_instruction {
415                // Static global instruction with template injection
416                let processed = adk_core::inject_session_state(ctx.as_ref(), template).await?;
417                if !processed.is_empty() {
418                    conversation_history.push(Content {
419                        role: "user".to_string(),
420                        parts: vec![Part::Text { text: processed }],
421                    });
422                }
423            }
424
425            // ===== PROCESS AGENT INSTRUCTION =====
426            // Agent-specific instruction
427            if let Some(provider) = &instruction_provider {
428                // Dynamic instruction via provider
429                let inst = provider(ctx.clone() as Arc<dyn ReadonlyContext>).await?;
430                if !inst.is_empty() {
431                    conversation_history.push(Content {
432                        role: "user".to_string(),
433                        parts: vec![Part::Text { text: inst }],
434                    });
435                }
436            } else if let Some(ref template) = instruction {
437                // Static instruction with template injection
438                let processed = adk_core::inject_session_state(ctx.as_ref(), template).await?;
439                if !processed.is_empty() {
440                    conversation_history.push(Content {
441                        role: "user".to_string(),
442                        parts: vec![Part::Text { text: processed }],
443                    });
444                }
445            }
446
447            // ===== LOAD SESSION HISTORY =====
448            // Load previous conversation turns from the session
449            let session_history = ctx.session().conversation_history();
450            conversation_history.extend(session_history);
451
452            // Add user content (current turn)
453            conversation_history.push(ctx.user_content().clone());
454
455            // ===== APPLY INCLUDE_CONTENTS FILTERING =====
456            // Control what conversation history the agent sees
457            let mut conversation_history = match include_contents {
458                adk_core::IncludeContents::None => {
459                    // Agent operates solely on current turn - only keep the latest user input
460                    // Remove all previous history except instructions and current user message
461                    let mut filtered = Vec::new();
462
463                    // Keep global and agent instructions (already added above)
464                    let instruction_count = conversation_history.iter()
465                        .take_while(|c| c.role == "user" && c.parts.iter().any(|p| {
466                            if let Part::Text { text } = p {
467                                // These are likely instructions, not user queries
468                                !text.is_empty()
469                            } else {
470                                false
471                            }
472                        }))
473                        .count();
474
475                    // Take instructions
476                    filtered.extend(conversation_history.iter().take(instruction_count).cloned());
477
478                    // Take only the last user message (current turn)
479                    if let Some(last) = conversation_history.last() {
480                        if last.role == "user" {
481                            filtered.push(last.clone());
482                        }
483                    }
484
485                    filtered
486                }
487                adk_core::IncludeContents::Default => {
488                    // Default behavior - keep full conversation history
489                    conversation_history
490                }
491            };
492
493            // Build tool declarations for Gemini
494            // Uses enhanced_description() which includes NOTE for long-running tools
495            let mut tool_declarations = std::collections::HashMap::new();
496            for tool in &tools {
497                // Build FunctionDeclaration JSON with enhanced description
498                // For long-running tools, this includes a warning not to call again if pending
499                let mut decl = serde_json::json!({
500                    "name": tool.name(),
501                    "description": tool.enhanced_description(),
502                });
503
504                if let Some(params) = tool.parameters_schema() {
505                    decl["parameters"] = params;
506                }
507
508                if let Some(response) = tool.response_schema() {
509                    decl["response"] = response;
510                }
511
512                tool_declarations.insert(tool.name().to_string(), decl);
513            }
514
515            // Inject transfer_to_agent tool if sub-agents exist
516            if !sub_agents.is_empty() {
517                let transfer_tool_name = "transfer_to_agent";
518                let transfer_tool_decl = serde_json::json!({
519                    "name": transfer_tool_name,
520                    "description": "Transfer execution to another agent.",
521                    "parameters": {
522                        "type": "object",
523                        "properties": {
524                            "agent_name": {
525                                "type": "string",
526                                "description": "The name of the agent to transfer to."
527                            }
528                        },
529                        "required": ["agent_name"]
530                    }
531                });
532                tool_declarations.insert(transfer_tool_name.to_string(), transfer_tool_decl);
533            }
534
535
536            // Multi-turn loop with max iterations
537            let max_iterations = 10;
538            let mut iteration = 0;
539
540            loop {
541                iteration += 1;
542                if iteration > max_iterations {
543                    yield Err(adk_core::AdkError::Agent(
544                        format!("Max iterations ({}) exceeded", max_iterations)
545                    ));
546                    return;
547                }
548
549                // Build request with conversation history
550                let config = output_schema.as_ref().map(|schema| {
551                    adk_core::GenerateContentConfig {
552                        temperature: None,
553                        top_p: None,
554                        top_k: None,
555                        max_output_tokens: None,
556                        response_schema: Some(schema.clone()),
557                    }
558                });
559
560                let request = LlmRequest {
561                    model: model.name().to_string(),
562                    contents: conversation_history.clone(),
563                    tools: tool_declarations.clone(),
564                    config,
565                };
566
567                // ===== BEFORE MODEL CALLBACKS =====
568                // These can modify the request or skip the model call by returning a response
569                let mut current_request = request;
570                let mut model_response_override = None;
571                for callback in before_model_callbacks.as_ref() {
572                    match callback(ctx.clone() as Arc<dyn CallbackContext>, current_request.clone()).await {
573                        Ok(BeforeModelResult::Continue(modified_request)) => {
574                            // Callback may have modified the request, continue with it
575                            current_request = modified_request;
576                        }
577                        Ok(BeforeModelResult::Skip(response)) => {
578                            // Callback returned a response - skip model call
579                            model_response_override = Some(response);
580                            break;
581                        }
582                        Err(e) => {
583                            // Callback failed - propagate error
584                            yield Err(e);
585                            return;
586                        }
587                    }
588                }
589                let request = current_request;
590
591                // Determine streaming source: cached response or real model
592                let mut accumulated_content: Option<Content> = None;
593
594                if let Some(cached_response) = model_response_override {
595                    // Use callback-provided response (e.g., from cache)
596                    // Yield it as an event
597                    let mut cached_event = Event::new(&invocation_id);
598                    cached_event.author = agent_name.clone();
599                    cached_event.llm_response.content = cached_response.content.clone();
600
601                    // Populate long_running_tool_ids for function calls from long-running tools
602                    if let Some(ref content) = cached_response.content {
603                        let long_running_ids: Vec<String> = content.parts.iter()
604                            .filter_map(|p| {
605                                if let Part::FunctionCall { name, .. } = p {
606                                    if let Some(tool) = tools.iter().find(|t| t.name() == name) {
607                                        if tool.is_long_running() {
608                                            return Some(name.clone());
609                                        }
610                                    }
611                                }
612                                None
613                            })
614                            .collect();
615                        cached_event.long_running_tool_ids = long_running_ids;
616                    }
617
618                    yield Ok(cached_event);
619
620                    accumulated_content = cached_response.content;
621                } else {
622                    // Call model with STREAMING ENABLED
623                    let mut response_stream = model.generate_content(request, true).await?;
624
625                    use futures::StreamExt;
626
627                    // Stream and process chunks with AfterModel callbacks
628                    while let Some(chunk_result) = response_stream.next().await {
629                        let mut chunk = match chunk_result {
630                            Ok(c) => c,
631                            Err(e) => {
632                                yield Err(e);
633                                return;
634                            }
635                        };
636
637                        // ===== AFTER MODEL CALLBACKS (per chunk) =====
638                        // Callbacks can modify each streaming chunk
639                        for callback in after_model_callbacks.as_ref() {
640                            match callback(ctx.clone() as Arc<dyn CallbackContext>, chunk.clone()).await {
641                                Ok(Some(modified_chunk)) => {
642                                    // Callback modified this chunk
643                                    chunk = modified_chunk;
644                                    break;
645                                }
646                                Ok(None) => {
647                                    // Continue to next callback
648                                    continue;
649                                }
650                                Err(e) => {
651                                    // Callback failed - propagate error
652                                    yield Err(e);
653                                    return;
654                                }
655                            }
656                        }
657
658                        // Yield the (possibly modified) partial event
659                        let mut partial_event = Event::new(&invocation_id);
660                        partial_event.author = agent_name.clone();
661                        partial_event.llm_response.content = chunk.content.clone();
662
663                        // Populate long_running_tool_ids for function calls from long-running tools
664                        if let Some(ref content) = chunk.content {
665                            let long_running_ids: Vec<String> = content.parts.iter()
666                                .filter_map(|p| {
667                                    if let Part::FunctionCall { name, .. } = p {
668                                        // Check if this tool is long-running
669                                        if let Some(tool) = tools.iter().find(|t| t.name() == name) {
670                                            if tool.is_long_running() {
671                                                // Use tool name as ID (we don't have explicit call IDs)
672                                                return Some(name.clone());
673                                            }
674                                        }
675                                    }
676                                    None
677                                })
678                                .collect();
679                            partial_event.long_running_tool_ids = long_running_ids;
680                        }
681
682                        yield Ok(partial_event);
683
684                        // Accumulate content for history
685                        if let Some(chunk_content) = chunk.content {
686                            if let Some(ref mut acc) = accumulated_content {
687                                // Merge parts from this chunk into accumulated content
688                                acc.parts.extend(chunk_content.parts);
689                            } else {
690                                // First chunk - initialize accumulator
691                                accumulated_content = Some(chunk_content);
692                            }
693                        }
694
695                        // Check if turn is complete
696                        if chunk.turn_complete {
697                            break;
698                        }
699                    }
700                }
701
702                // After streaming/caching completes, check for function calls in accumulated content
703                let function_call_names: Vec<String> = accumulated_content.as_ref()
704                    .map(|c| c.parts.iter()
705                        .filter_map(|p| {
706                            if let Part::FunctionCall { name, .. } = p {
707                                Some(name.clone())
708                            } else {
709                                None
710                            }
711                        })
712                        .collect())
713                    .unwrap_or_default();
714
715                let has_function_calls = !function_call_names.is_empty();
716
717                // Check if ALL function calls are from long-running tools
718                // If so, we should NOT continue the loop - the tool returned a pending status
719                // and the agent/client will poll for completion later
720                let all_calls_are_long_running = has_function_calls && function_call_names.iter().all(|name| {
721                    tools.iter()
722                        .find(|t| t.name() == name)
723                        .map(|t| t.is_long_running())
724                        .unwrap_or(false)
725                });
726
727                // Add final content to history
728                if let Some(ref content) = accumulated_content {
729                    conversation_history.push(content.clone());
730
731                    // Handle output_key: save final agent output to state_delta
732                    if let Some(ref output_key) = output_key {
733                        if !has_function_calls {  // Only save if not calling tools
734                            let mut text_parts = String::new();
735                            for part in &content.parts {
736                                if let Part::Text { text } = part {
737                                    text_parts.push_str(text);
738                                }
739                            }
740                            if !text_parts.is_empty() {
741                                // Yield a final state update event
742                                let mut state_event = Event::new(&invocation_id);
743                                state_event.author = agent_name.clone();
744                                state_event.actions.state_delta.insert(
745                                    output_key.clone(),
746                                    serde_json::Value::String(text_parts),
747                                );
748                                yield Ok(state_event);
749                            }
750                        }
751                    }
752                }
753
754                if !has_function_calls {
755                    // No function calls, we're done
756                    break;
757                }
758
759                // Execute function calls and add responses to history
760                if let Some(content) = &accumulated_content {
761                    for part in &content.parts {
762                        if let Part::FunctionCall { name, args } = part {
763                            // Handle transfer_to_agent specially
764                            if name == "transfer_to_agent" {
765                                let target_agent = args.get("agent_name")
766                                    .and_then(|v| v.as_str())
767                                    .unwrap_or_default()
768                                    .to_string();
769
770                                let mut transfer_event = Event::new(&invocation_id);
771                                transfer_event.author = agent_name.clone();
772                                transfer_event.actions.transfer_to_agent = Some(target_agent);
773
774                                yield Ok(transfer_event);
775                                return;
776                            }
777
778
779                            // Find and execute tool
780                            let tool_result = if let Some(tool) = tools.iter().find(|t| t.name() == name) {
781                                // ✅ Use AgentToolContext that preserves parent context
782                                let tool_ctx = Arc::new(AgentToolContext::new(
783                                    ctx.clone(),
784                                    format!("{}_{}", invocation_id, name),
785                                )) as Arc<dyn ToolContext>;
786
787                                match tool.execute(tool_ctx, args.clone()).await {
788                                    Ok(result) => result,
789                                    Err(e) => serde_json::json!({ "error": e.to_string() }),
790                                }
791                            } else {
792                                serde_json::json!({ "error": format!("Tool {} not found", name) })
793                            };
794
795                            // Yield tool execution event
796                            let mut tool_event = Event::new(&invocation_id);
797                            tool_event.author = agent_name.clone();
798                            tool_event.llm_response.content = Some(Content {
799                                role: "function".to_string(),
800                                parts: vec![Part::FunctionResponse {
801                                    name: name.clone(),
802                                    response: tool_result.clone(),
803                                }],
804                            });
805                            yield Ok(tool_event);
806
807                            // Add function response to history
808                            conversation_history.push(Content {
809                                role: "function".to_string(),
810                                parts: vec![Part::FunctionResponse {
811                                    name: name.clone(),
812                                    response: tool_result,
813                                }],
814                            });
815                        }
816                    }
817                }
818
819                // If all function calls were from long-running tools, treat as final response
820                // The tools have been executed and returned pending status - don't continue the loop
821                if all_calls_are_long_running {
822                    break;
823                }
824            }
825
826            // ===== AFTER AGENT CALLBACKS =====
827            // Execute after the agent completes
828            for callback in after_agent_callbacks.as_ref() {
829                match callback(ctx.clone() as Arc<dyn CallbackContext>).await {
830                    Ok(Some(content)) => {
831                        // Callback returned content - yield it
832                        let mut after_event = Event::new(&invocation_id);
833                        after_event.author = agent_name.clone();
834                        after_event.llm_response.content = Some(content);
835                        yield Ok(after_event);
836                        break; // First callback that returns content wins
837                    }
838                    Ok(None) => {
839                        // Continue to next callback
840                        continue;
841                    }
842                    Err(e) => {
843                        // Callback failed - propagate error
844                        yield Err(e);
845                        return;
846                    }
847                }
848            }
849        };
850
851        Ok(Box::pin(s))
852    }
853}