Skip to content

Commit fd97bc7

Browse files
committed
Planner modeule initial codebase
1 parent cc11093 commit fd97bc7

19 files changed

Lines changed: 2183 additions & 0 deletions

contrib/planners/pom.xml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2025 Google LLC
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
18+
<modelVersion>4.0.0</modelVersion>
19+
20+
<parent>
21+
<groupId>com.google.adk</groupId>
22+
<artifactId>google-adk-parent</artifactId>
23+
<version>0.5.1-SNAPSHOT</version><!-- {x-version-update:google-adk:current} -->
24+
<relativePath>../../pom.xml</relativePath>
25+
</parent>
26+
27+
<artifactId>google-adk-planners</artifactId>
28+
<name>Agent Development Kit - Planners</name>
29+
<description>Built-in planner implementations for the ADK PlannerAgent, including GOAP (Goal-Oriented Action Planning), P2P (Peer-to-Peer), and Supervisor planners.</description>
30+
31+
<dependencies>
32+
<!-- Main dependencies -->
33+
<dependency>
34+
<groupId>com.google.adk</groupId>
35+
<artifactId>google-adk</artifactId>
36+
<version>${project.version}</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>com.google.genai</groupId>
40+
<artifactId>google-genai</artifactId>
41+
</dependency>
42+
43+
<!-- Test dependencies -->
44+
<dependency>
45+
<groupId>org.junit.jupiter</groupId>
46+
<artifactId>junit-jupiter-api</artifactId>
47+
<scope>test</scope>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.junit.jupiter</groupId>
51+
<artifactId>junit-jupiter-engine</artifactId>
52+
<scope>test</scope>
53+
</dependency>
54+
<dependency>
55+
<groupId>com.google.truth</groupId>
56+
<artifactId>truth</artifactId>
57+
<scope>test</scope>
58+
</dependency>
59+
<dependency>
60+
<groupId>org.mockito</groupId>
61+
<artifactId>mockito-core</artifactId>
62+
<scope>test</scope>
63+
</dependency>
64+
</dependencies>
65+
</project>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.Planner;
20+
import com.google.adk.agents.PlannerAction;
21+
import com.google.adk.agents.PlanningContext;
22+
import io.reactivex.rxjava3.core.Single;
23+
24+
/** A planner that runs all sub-agents in parallel, then completes. */
25+
public final class ParallelPlanner implements Planner {
26+
27+
@Override
28+
public Single<PlannerAction> firstAction(PlanningContext context) {
29+
if (context.availableAgents().isEmpty()) {
30+
return Single.just(new PlannerAction.Done());
31+
}
32+
return Single.just(new PlannerAction.RunAgents(context.availableAgents()));
33+
}
34+
35+
@Override
36+
public Single<PlannerAction> nextAction(PlanningContext context) {
37+
return Single.just(new PlannerAction.Done());
38+
}
39+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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.common.collect.ImmutableList;
24+
import io.reactivex.rxjava3.core.Single;
25+
import java.util.concurrent.atomic.AtomicInteger;
26+
27+
/** A planner that runs sub-agents one at a time in order. */
28+
public final class SequentialPlanner implements Planner {
29+
30+
private final AtomicInteger cursor = new AtomicInteger(0);
31+
private ImmutableList<BaseAgent> agents;
32+
33+
@Override
34+
public void init(PlanningContext context) {
35+
agents = context.availableAgents();
36+
cursor.set(0);
37+
}
38+
39+
@Override
40+
public Single<PlannerAction> firstAction(PlanningContext context) {
41+
cursor.set(0);
42+
return selectNext();
43+
}
44+
45+
@Override
46+
public Single<PlannerAction> nextAction(PlanningContext context) {
47+
return selectNext();
48+
}
49+
50+
private Single<PlannerAction> selectNext() {
51+
int idx = cursor.getAndIncrement();
52+
if (agents == null || idx >= agents.size()) {
53+
return Single.just(new PlannerAction.Done());
54+
}
55+
return Single.just(new PlannerAction.RunAgents(agents.get(idx)));
56+
}
57+
}

0 commit comments

Comments
 (0)