Execute concurrent agent tasks with maximum efficiency This bypasses the workflow system entirely for pure speed
(
&self,
prompts: Vec<String>,
agent_id: crate::types::AgentId,
)
| 2789 | /// Execute concurrent agent tasks with maximum efficiency |
| 2790 | /// This bypasses the workflow system entirely for pure speed |
| 2791 | pub async fn execute_concurrent_agent_tasks( |
| 2792 | &self, |
| 2793 | prompts: Vec<String>, |
| 2794 | agent_id: crate::types::AgentId, |
| 2795 | ) -> GraphBitResult<Vec<Result<serde_json::Value, GraphBitError>>> { |
| 2796 | if prompts.is_empty() { |
| 2797 | return Ok(Vec::new()); |
| 2798 | } |
| 2799 | |
| 2800 | // Ensure the agent exists |
| 2801 | let agent = { |
| 2802 | let agents_guard = self.agents.read().await; |
| 2803 | agents_guard.get(&agent_id).cloned() |
| 2804 | }; |
| 2805 | |
| 2806 | let agent = if let Some(agent) = agent { |
| 2807 | agent |
| 2808 | } else { |
| 2809 | return Err(GraphBitError::workflow_execution(format!( |
| 2810 | "Agent {agent_id} not found. Please register the agent first.", |
| 2811 | ))); |
| 2812 | }; |
| 2813 | |
| 2814 | // Execute all prompts concurrently with minimal overhead |
| 2815 | let concurrent_tasks: Vec<_> = prompts |
| 2816 | .into_iter() |
| 2817 | .enumerate() |
| 2818 | .map(|(index, prompt)| { |
| 2819 | let agent_clone = agent.clone(); |
| 2820 | let agent_id_clone = agent_id.clone(); |
| 2821 | |
| 2822 | tokio::spawn(async move { |
| 2823 | // Create a minimal agent message for this prompt |
| 2824 | let message = AgentMessage::new( |
| 2825 | agent_id_clone.clone(), |
| 2826 | None, // No specific recipient |
| 2827 | MessageContent::Text(prompt), |
| 2828 | ); |
| 2829 | |
| 2830 | // Execute the agent task directly using the execute method for better performance |
| 2831 | agent_clone.execute(message).await.map_err(|e| { |
| 2832 | GraphBitError::workflow_execution( |
| 2833 | format!("Agent task {index} failed: {e}",), |
| 2834 | ) |
| 2835 | }) |
| 2836 | }) |
| 2837 | }) |
| 2838 | .collect(); |
| 2839 | |
| 2840 | // Collect all results |
| 2841 | let results = futures::future::join_all(concurrent_tasks).await; |
| 2842 | let mut task_results = Vec::with_capacity(results.len()); |
| 2843 | |
| 2844 | for task_result in results { |
| 2845 | match task_result { |
| 2846 | Ok(result) => task_results.push(result), |
| 2847 | Err(e) => task_results.push(Err(GraphBitError::workflow_execution(format!( |
| 2848 | "Task join failed: {e}" |