Package com.tnsai.llm

Class LLMResolver.Builder

java.lang.Object
com.tnsai.llm.LLMResolver.Builder
Enclosing class:
LLMResolver

public static final class LLMResolver.Builder extends Object
Builder for LLM resolution with full fallback chain support.

Supports the hierarchy:

  1. Role's LLM config (requires configFactory)
  2. Agent's LLM

Example


 LLMClient llm = LLMResolver.builder()
     .roleLLMSpec(() -> role.getLLMSpec().orElse(null))
     .agentLLM(agent::llm)
     .configFactory(cfg -> new OpenAIClient(cfg.model(), cfg.temperature()))
     .context("analyze action")
     .resolve();
 
  • Method Details

    • roleLLMSpec

      public LLMResolver.Builder roleLLMSpec(Supplier<? extends LLMSpecSource> supplier)
      Sets the role's LLM config supplier.

      Called lazily. If returns non-null, requires configFactory(Function) to convert to LLMClient. If returns null, falls back to agent's LLM.

      Accepts any LLMSpecSource supplier.

      Parameters:
      supplier - supplies the role's LLM config (may return null)
      Returns:
      this builder
    • agentLLM

      public LLMResolver.Builder agentLLM(Supplier<LLMClient> supplier)
      Sets the agent's LLM supplier.

      Called lazily only if role has no LLM config.

      Parameters:
      supplier - supplies the agent's LLM (may return null)
      Returns:
      this builder
    • configFactory

      public LLMResolver.Builder configFactory(Function<LLMSpecSource,LLMClient> factory)
      Sets the factory for creating LLMClient from LLM configuration.

      Required if actionLLMSpec or roleLLMSpec is used. The factory receives the config (model, temperature) and should return an appropriate LLMClient.

      The factory receives LLMSpecSource, allowing it to handle both basic LLMResolver.LLMSpec and extended types like PortableLLMSpec.

      Example

      
       .configFactory(cfg -> {
           OpenAIClient.Builder builder = OpenAIClient.builder()
               .model(cfg.model());
           if (cfg.hasTemperature()) {
               builder.temperature(cfg.temperature());
           }
           return builder.build();
       })
       
      Parameters:
      factory - function that creates LLMClient from config
      Returns:
      this builder
    • context

      public LLMResolver.Builder context(String context)
      Sets context for error messages.
      Parameters:
      context - descriptive context (e.g., action name)
      Returns:
      this builder
    • resolve

      public LLMClient resolve()
      Resolves the LLM using the fallback chain.
      Returns:
      the resolved LLM client
      Throws:
      IllegalStateException - if no LLM found in chain
    • resolveOptional

      public Optional<LLMClient> resolveOptional()
      Resolves the LLM, returning empty if none found.
      Returns:
      Optional containing the LLM, or empty
    • resolveWithSource

      public LLMResolver.ResolutionResult resolveWithSource()
      Resolves the LLM with source tracking.

      Use this method when you need to know where the LLM came from (for logging, debugging, or conditional logic).

      Returns:
      resolution result with LLM and source