|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.adk.planner; |
| 18 | + |
| 19 | +import com.google.adk.agents.BaseAgent; |
| 20 | +import com.google.adk.agents.Planner; |
| 21 | +import com.google.adk.agents.PlannerAction; |
| 22 | +import com.google.adk.agents.PlanningContext; |
| 23 | +import com.google.adk.events.Event; |
| 24 | +import com.google.common.collect.ImmutableList; |
| 25 | +import io.reactivex.rxjava3.core.Single; |
| 26 | +import java.util.List; |
| 27 | +import java.util.concurrent.atomic.AtomicInteger; |
| 28 | + |
| 29 | +/** |
| 30 | + * A planner that cycles through sub-agents repeatedly, stopping when an escalate event is detected |
| 31 | + * or the maximum number of cycles is reached. |
| 32 | + */ |
| 33 | +public final class LoopPlanner implements Planner { |
| 34 | + |
| 35 | + private final int maxCycles; |
| 36 | + private final AtomicInteger cursor = new AtomicInteger(0); |
| 37 | + private final AtomicInteger cycleCount = new AtomicInteger(0); |
| 38 | + private ImmutableList<BaseAgent> agents; |
| 39 | + |
| 40 | + public LoopPlanner(int maxCycles) { |
| 41 | + this.maxCycles = maxCycles; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public void init(PlanningContext context) { |
| 46 | + agents = context.availableAgents(); |
| 47 | + cursor.set(0); |
| 48 | + cycleCount.set(0); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public Single<PlannerAction> firstAction(PlanningContext context) { |
| 53 | + cursor.set(0); |
| 54 | + cycleCount.set(0); |
| 55 | + return selectNext(context); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public Single<PlannerAction> nextAction(PlanningContext context) { |
| 60 | + if (hasEscalateEvent(context.events())) { |
| 61 | + return Single.just(new PlannerAction.Done()); |
| 62 | + } |
| 63 | + return selectNext(context); |
| 64 | + } |
| 65 | + |
| 66 | + private Single<PlannerAction> selectNext(PlanningContext context) { |
| 67 | + if (agents == null || agents.isEmpty()) { |
| 68 | + return Single.just(new PlannerAction.Done()); |
| 69 | + } |
| 70 | + |
| 71 | + int idx = cursor.getAndIncrement(); |
| 72 | + if (idx >= agents.size()) { |
| 73 | + int cycle = cycleCount.incrementAndGet(); |
| 74 | + if (cycle >= maxCycles) { |
| 75 | + return Single.just(new PlannerAction.Done()); |
| 76 | + } |
| 77 | + cursor.set(1); |
| 78 | + idx = 0; |
| 79 | + } |
| 80 | + return Single.just(new PlannerAction.RunAgents(agents.get(idx))); |
| 81 | + } |
| 82 | + |
| 83 | + private static boolean hasEscalateEvent(List<Event> events) { |
| 84 | + if (events.isEmpty()) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + Event lastEvent = events.get(events.size() - 1); |
| 88 | + return lastEvent.actions().escalate().orElse(false); |
| 89 | + } |
| 90 | +} |
0 commit comments