Package com.tnsai.coordination
Class ScatterGather
java.lang.Object
com.tnsai.coordination.ScatterGather
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
);
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordResponse from a single agent.static final recordAggregated responses from scatter operation. -
Method Summary
Modifier and TypeMethodDescriptiongatherAndAggregate(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.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.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.static ScatterGather.ScatterResultsquorum(String message, List<? extends com.tnsai.agents.Agent> agents, int quorumSize, long timeout, TimeUnit unit) Waits for at least N successful responses from agents.static Optional<ScatterGather.AgentResponse> Sends message to multiple agents, returns the first successful response.static ScatterGather.ScatterResultsScatters with default 60 second timeout.static ScatterGather.ScatterResultsScatters a message to multiple agents in parallel and gathers all responses.
-
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 agentsagents- List of agents to querytimeout- Timeout valueunit- 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 sendagents- List of agents to racetimeout- Timeout valueunit- 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 sendagents- List of agents to queryquorumSize- Minimum number of responses requiredtimeout- Timeout valueunit- 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 sendagents- List of agents to queryscorer- Function to score each responsetimeout- Timeout valueunit- 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 sendagents- List of agents to querytimeout- Timeout valueunit- 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 messageagents- List of agents for initial responsesaggregator- Agent to synthesize final responsetimeout- Timeout valueunit- Timeout unit- Returns:
- Synthesized response
-