Package com.tnsai.llm

Class LLMResolver

java.lang.Object
com.tnsai.llm.LLMResolver

public final class LLMResolver extends Object
Resolves the appropriate LLM client using a fallback chain.

Implements the LLM resolution hierarchy:

  1. Role's LLM - if role has dedicated LLM config
  2. Agent's LLM - fallback to agent's default LLM
  3. Error - if none configured and LLM is required

Note: LLM is required only when action type is LLM or LLM.

Usage - Simple


 // Role → Agent fallback
 Optional<LLMClient> llm = LLMResolver.resolve(agent, role);
 

Usage - With Builder


 // Role → Agent fallback with config factory
 LLMClient llm = LLMResolver.builder()
     .roleLLMSpec(() -> roleLLMSpec)
     .agentLLM(agent::llm)
     .configFactory(cfg -> LLMClient.create(cfg.model(), cfg.temperature()))
     .context(action.getName())
     .resolve();
 

Resolution Examples

Role SpecAgent LLMResult
claudeGPT-3.5claude (role)
nullGPT-3.5GPT-3.5 (agent)
nullnullERROR
Author:
TnsAI Team
See Also:
  • Method Details

    • resolve

      public static Optional<LLMClient> resolve(Agent agent, Role role)
      Resolves the LLM client for the given agent and role.

      Resolution priority:

      1. Role's LLM (if set)
      2. Agent's LLM (if set)
      3. Empty (no LLM configured)
      Parameters:
      agent - the agent (may have LLM configured)
      role - the role being executed (may have dedicated LLM)
      Returns:
      the resolved LLM client, or empty if none configured
    • resolve

      public static Optional<LLMClient> resolve(Agent agent)
      Resolves the LLM client for an agent without role context.
      Parameters:
      agent - the agent
      Returns:
      the agent's LLM, or empty if none configured
    • hasLLM

      public static boolean hasLLM(Agent agent, Role role)
      Checks if the given agent-role combination has an LLM configured.
      Parameters:
      agent - the agent
      role - the role (may be null)
      Returns:
      true if LLM is available
    • resolveOrThrow

      public static LLMClient resolveOrThrow(Agent agent, Role role)
      Gets the LLM client or throws if none configured.
      Parameters:
      agent - the agent
      role - the role
      Returns:
      the resolved LLM client
      Throws:
      IllegalStateException - if no LLM is configured
    • builder

      public static LLMResolver.Builder builder()
      Creates a new builder for advanced LLM resolution with Action support.
      Returns:
      a new builder instance