Class AdaptiveReplanEngine

java.lang.Object
com.tnsai.planning.llm.AdaptiveReplanEngine

public final class AdaptiveReplanEngine extends Object
Executes LLM-generated plans with adaptive replanning on failure.

When a step fails during execution, the engine sends the failure context (error, completed steps, remaining steps) back to the LLM to generate an updated plan for the remaining work.

Replanning Process

  1. Execute steps sequentially using the provided StepExecutor
  2. On failure: collect context (completed steps, error, remaining goal)
  3. Call LLM with failure context to generate a revised plan
  4. Continue execution with the revised plan
  5. Repeat up to maxReplanAttempts times

Usage


 AdaptiveReplanEngine engine = AdaptiveReplanEngine.builder()
     .llm(client)
     .planner(planner)
     .stepExecutor(step -> {
         // execute the step
         return StepExecutionResult.success("done");
     })
     .maxReplanAttempts(3)
     .build();

 PlanExecutionResult result = engine.execute("Deploy the application");
 
Since:
0.3.0
See Also:
  • Method Details

    • builder

      public static AdaptiveReplanEngine.Builder builder()
      Creates a new builder.
    • execute

      public PlanExecutionResult execute(String goal)
      Generates and executes a plan for the given goal, replanning on failure.
      Parameters:
      goal - the natural-language goal
      Returns:
      execution result
    • execute

      public PlanExecutionResult execute(String goal, Map<String,Object> state)
      Generates and executes a plan for the given goal with state context.
      Parameters:
      goal - the natural-language goal
      state - current state context
      Returns:
      execution result
    • executePlan

      public PlanExecutionResult executePlan(LLMPlan plan)
      Executes an already-generated plan, replanning on failure.
      Parameters:
      plan - the plan to execute
      Returns:
      execution result
    • executePlan

      public PlanExecutionResult executePlan(LLMPlan plan, Map<String,Object> state)
      Executes an already-generated plan with state context, replanning on failure.
      Parameters:
      plan - the plan to execute
      state - current state context
      Returns:
      execution result