spring-ai icon indicating copy to clipboard operation
spring-ai copied to clipboard

Add static factory shortcuts for low-ceremony agent creation

Open ahmed-sekka opened this issue 6 months ago • 1 comments

Motivation

The current Builder API takes ~16 lines for the 2-agent coordinator example shown in the README's In 60 seconds section. This verbosity hurts the time-to-aha for newcomers and the README pitch itself.

Spring AI exposes both ChatClient.create(chatModel) and ChatClient.builder(...) so callers can pick the right ceremony for their use case. spring-agent-flow should follow the same pattern.

Before / after

Before (16 lines)

ExecutorAgent researcher = ExecutorAgent.builder()
        .chatClient(chatClient)
        .systemPrompt("Find key facts.")
        .build();

ExecutorAgent writer = ExecutorAgent.builder()
        .chatClient(chatClient)
        .systemPrompt("Write a clear report.")
        .build();

CoordinatorAgent coordinator = CoordinatorAgent.builder()
        .executors(Map.of("research", researcher, "writing", writer))
        .routingStrategy(RoutingStrategy.llmDriven(chatClient))
        .build();

AgentResult result = coordinator.execute(
        AgentContext.of("Compare Claude 4 and GPT-5"));

System.out.println(result.text());

After (7 lines, same semantics)

var researcher = ExecutorAgent.of(chatClient, "Find key facts.");
var writer    = ExecutorAgent.of(chatClient, "Write a clear report.");

var coordinator = CoordinatorAgent.of(chatClient, Map.of(
    "research", researcher,
    "writing",  writer));

System.out.println(coordinator.execute("Compare Claude 4 and GPT-5").text());

Acceptance criteria

  • [ ] ExecutorAgent.of(ChatClient chatClient, String systemPrompt) static factory
  • [ ] ExecutorAgent.of(ChatClient chatClient, String name, String systemPrompt) overload (when a name is needed)
  • [ ] CoordinatorAgent.of(ChatClient chatClient, Map<String, Agent> executors) static factory, defaulting to RoutingStrategy.llmDriven(chatClient)
  • [ ] Agent.execute(String userMessage) default method delegating to execute(AgentContext.of(userMessage))
  • [ ] Existing Builder APIs unchanged (additive only, no breaking change)
  • [ ] Unit tests covering each new factory / overload
  • [ ] Update spring-agent-flow-samples MultiAgentCoordination to use the new shortcuts
  • [ ] Update the In 60 seconds code block in README accordingly

Non-goals

  • DSL / fluent-builder DSL such as Squad.named(...).agent(...) — too much surface area for marginal gain
  • Annotation-based agent declaration (@AgentSquad) — heavy machinery, debugger-hostile
  • Kotlin-flavored API — out of scope for this issue

The .of(...) Java idiom is the sweet spot and matches Spring AI's ChatClient.create() pattern.

Why this is a good first issue

  • Additive only — no risk of breaking existing users
  • Clear before / after metric (line count of the README sample)
  • Touches three small files (ExecutorAgent, CoordinatorAgent, Agent) plus one sample plus the README
  • Tests are straightforward (each factory is a thin wrapper over the existing builder)

ahmed-sekka avatar May 05 '26 16:05 ahmed-sekka

Closing — created by mistake

ahmed-sekka avatar May 05 '26 16:05 ahmed-sekka