Plan-and-Execute Agent - 先规划后执行
| 39 | * Plan-and-Execute Agent - 先规划后执行 |
| 40 | */ |
| 41 | public class PlanExecuteAgent { |
| 42 | private static final Logger log = LoggerFactory.getLogger(PlanExecuteAgent.class); |
| 43 | private static final ObjectMapper JSON_MAPPER = new ObjectMapper(); |
| 44 | private record PlanRunOutcome(String result, boolean persistAssistantMessage) { |
| 45 | static PlanRunOutcome executed(String result) { |
| 46 | return new PlanRunOutcome(result, true); |
| 47 | } |
| 48 | |
| 49 | static PlanRunOutcome canceled(String result) { |
| 50 | return new PlanRunOutcome(result, false); |
| 51 | } |
| 52 | |
| 53 | static PlanRunOutcome failed(String result) { |
| 54 | return new PlanRunOutcome(result, true); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | private record TaskRunResult(String result, boolean streamedOutput) { |
| 59 | static TaskRunResult of(String result, boolean streamedOutput) { |
| 60 | return new TaskRunResult(result, streamedOutput); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | private record TaskExecutionResult(Task task, String result, boolean streamedOutput, Exception error) { |
| 65 | static TaskExecutionResult success(Task task, TaskRunResult taskRunResult) { |
| 66 | return new TaskExecutionResult(task, taskRunResult.result(), taskRunResult.streamedOutput(), null); |
| 67 | } |
| 68 | |
| 69 | static TaskExecutionResult failure(Task task, Exception error) { |
| 70 | return new TaskExecutionResult(task, null, false, error); |
| 71 | } |
| 72 | |
| 73 | boolean failed() { |
| 74 | return error != null; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | public interface PlanReviewHandler { |
| 79 | PlanReviewDecision review(String goal, ExecutionPlan plan); |
| 80 | } |
| 81 | |
| 82 | public enum PlanReviewAction { |
| 83 | EXECUTE, |
| 84 | SUPPLEMENT, |
| 85 | CANCEL |
| 86 | } |
| 87 | |
| 88 | public record PlanReviewDecision(PlanReviewAction action, String feedback) { |
| 89 | public static PlanReviewDecision execute() { |
| 90 | return new PlanReviewDecision(PlanReviewAction.EXECUTE, null); |
| 91 | } |
| 92 | |
| 93 | public static PlanReviewDecision supplement(String feedback) { |
| 94 | return new PlanReviewDecision(PlanReviewAction.SUPPLEMENT, feedback); |
| 95 | } |
| 96 | |
| 97 | public static PlanReviewDecision cancel() { |
| 98 | return new PlanReviewDecision(PlanReviewAction.CANCEL, null); |
nothing calls this directly
no test coverage detected