Class ScatterGather

java.lang.Object
com.tnsai.coordination.ScatterGather

public final class ScatterGather extends Object
Scatter-Gather pattern for multi-agent communication. Sends a task to multiple agents in parallel and gathers their responses.

Patterns Supported:

  • Scatter-Gather: Send to all agents, collect all responses
  • Fan-Out/Fan-In: Distribute work, aggregate results
  • Broadcast-Collect: Broadcast query, collect answers
  • Race: First response wins
  • Quorum: Wait for N responses

Example usage:


 // Scatter to multiple agents and gather all responses
 List<AgentResponse> responses = ScatterGather.scatter(
     "Analyze market trends",
     List.of(analyst1, analyst2, analyst3),
     30, TimeUnit.SECONDS
 );

 // Gather with consensus (majority vote)
 Optional<AgentResponse> consensus = ScatterGather.gatherWithConsensus(
     "What is 2+2?",
     agents,
     30, TimeUnit.SECONDS
 );
 
  • Method Details

    • scatter

      public static ScatterGather.ScatterResults scatter(String message, List<? extends com.tnsai.agents.Agent> agents, long timeout, TimeUnit unit)
      Scatters a message to multiple agents in parallel and gathers all responses.
      Parameters:
      message - The message to send to all agents
      agents - List of agents to query
      timeout - Timeout value
      unit - Timeout unit
      Returns:
      Aggregated results from all agents
    • scatter

      public static ScatterGather.ScatterResults scatter(String message, List<? extends com.tnsai.agents.Agent> agents)
      Scatters with default 60 second timeout.
    • race

      public static Optional<ScatterGather.AgentResponse> race(String message, List<? extends com.tnsai.agents.Agent> agents, long timeout, TimeUnit unit)
      Sends message to multiple agents, returns the first successful response.
      Parameters:
      message - The message to send
      agents - List of agents to race
      timeout - Timeout value
      unit - Timeout unit
      Returns:
      First successful response, or empty if all fail
    • quorum

      public static ScatterGather.ScatterResults quorum(String message, List<? extends com.tnsai.agents.Agent> agents, int quorumSize, long timeout, TimeUnit unit)
      Waits for at least N successful responses from agents.
      Parameters:
      message - The message to send
      agents - List of agents to query
      quorumSize - Minimum number of responses required
      timeout - Timeout value
      unit - Timeout unit
      Returns:
      Results once quorum is reached or timeout
    • gatherWithScore

      public static Optional<ScatterGather.AgentResponse> gatherWithScore(String message, List<? extends com.tnsai.agents.Agent> agents, Function<String,Double> scorer, long timeout, TimeUnit unit)
      Gathers responses and selects the best one using a scoring function.
      Parameters:
      message - The message to send
      agents - List of agents to query
      scorer - Function to score each response
      timeout - Timeout value
      unit - Timeout unit
      Returns:
      Best response based on scoring
    • gatherWithConsensus

      public static Optional<ScatterGather.AgentResponse> gatherWithConsensus(String message, List<? extends com.tnsai.agents.Agent> agents, long timeout, TimeUnit unit)
      Gathers responses and selects by majority voting.
      Parameters:
      message - The message to send
      agents - List of agents to query
      timeout - Timeout value
      unit - Timeout unit
      Returns:
      Most common response pattern
    • gatherAndAggregate

      public static Optional<String> gatherAndAggregate(String message, List<? extends com.tnsai.agents.Agent> agents, com.tnsai.agents.Agent aggregator, long timeout, TimeUnit unit)
      Gathers responses from multiple agents and combines them using an aggregator agent.
      Parameters:
      message - The original message
      agents - List of agents for initial responses
      aggregator - Agent to synthesize final response
      timeout - Timeout value
      unit - Timeout unit
      Returns:
      Synthesized response