Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions contrib/planners/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2025 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<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">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.google.adk</groupId>
<artifactId>google-adk-parent</artifactId>
<version>0.5.1-SNAPSHOT</version><!-- {x-version-update:google-adk:current} -->
<relativePath>../../pom.xml</relativePath>
</parent>

<artifactId>google-adk-planners</artifactId>
<name>Agent Development Kit - Planners</name>
<description>Built-in planner implementations for the ADK PlannerAgent, including GOAP (Goal-Oriented Action Planning), P2P (Peer-to-Peer), and Supervisor planners.</description>

<dependencies>
<!-- Main dependencies -->
<dependency>
<groupId>com.google.adk</groupId>
<artifactId>google-adk</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.genai</groupId>
<artifactId>google-genai</artifactId>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.adk.planner;

import com.google.adk.agents.BaseAgent;
import com.google.adk.agents.Planner;
import com.google.adk.agents.PlannerAction;
import com.google.adk.agents.PlanningContext;
import com.google.adk.events.Event;
import com.google.common.collect.ImmutableList;
import io.reactivex.rxjava3.core.Single;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
* A planner that cycles through sub-agents repeatedly, stopping when an escalate event is detected
* or the maximum number of cycles is reached.
*/
public final class LoopPlanner implements Planner {

private final int maxCycles;
private final AtomicInteger cursor = new AtomicInteger(0);
private final AtomicInteger cycleCount = new AtomicInteger(0);
private ImmutableList<BaseAgent> agents;

public LoopPlanner(int maxCycles) {
this.maxCycles = maxCycles;
}

@Override
public void init(PlanningContext context) {
agents = context.availableAgents();
cursor.set(0);
cycleCount.set(0);
}

@Override
public Single<PlannerAction> firstAction(PlanningContext context) {
cursor.set(0);
cycleCount.set(0);
return selectNext(context);
}

@Override
public Single<PlannerAction> nextAction(PlanningContext context) {
if (hasEscalateEvent(context.events())) {
return Single.just(new PlannerAction.Done());
}
return selectNext(context);
}

private Single<PlannerAction> selectNext(PlanningContext context) {
if (agents == null || agents.isEmpty()) {
return Single.just(new PlannerAction.Done());
}

int idx = cursor.getAndIncrement();
if (idx >= agents.size()) {
int cycle = cycleCount.incrementAndGet();
if (cycle >= maxCycles) {
return Single.just(new PlannerAction.Done());
}
cursor.set(1);
idx = 0;
}
return Single.just(new PlannerAction.RunAgents(agents.get(idx)));
}

private static boolean hasEscalateEvent(List<Event> events) {
if (events.isEmpty()) {
return false;
}
Event lastEvent = events.get(events.size() - 1);
return lastEvent.actions().escalate().orElse(false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.adk.planner;

import com.google.adk.agents.Planner;
import com.google.adk.agents.PlannerAction;
import com.google.adk.agents.PlanningContext;
import io.reactivex.rxjava3.core.Single;

/** A planner that runs all sub-agents in parallel, then completes. */
public final class ParallelPlanner implements Planner {

@Override
public Single<PlannerAction> firstAction(PlanningContext context) {
if (context.availableAgents().isEmpty()) {
return Single.just(new PlannerAction.Done());
}
return Single.just(new PlannerAction.RunAgents(context.availableAgents()));
}

@Override
public Single<PlannerAction> nextAction(PlanningContext context) {
return Single.just(new PlannerAction.Done());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.adk.planner;

import com.google.adk.agents.BaseAgent;
import com.google.adk.agents.Planner;
import com.google.adk.agents.PlannerAction;
import com.google.adk.agents.PlanningContext;
import com.google.common.collect.ImmutableList;
import io.reactivex.rxjava3.core.Single;
import java.util.concurrent.atomic.AtomicInteger;

/** A planner that runs sub-agents one at a time in order. */
public final class SequentialPlanner implements Planner {

private final AtomicInteger cursor = new AtomicInteger(0);
private ImmutableList<BaseAgent> agents;

@Override
public void init(PlanningContext context) {
agents = context.availableAgents();
cursor.set(0);
}

@Override
public Single<PlannerAction> firstAction(PlanningContext context) {
cursor.set(0);
return selectNext();
}

@Override
public Single<PlannerAction> nextAction(PlanningContext context) {
return selectNext();
}

private Single<PlannerAction> selectNext() {
int idx = cursor.getAndIncrement();
if (agents == null || idx >= agents.size()) {
return Single.just(new PlannerAction.Done());
}
return Single.just(new PlannerAction.RunAgents(agents.get(idx)));
}
}
Loading
Loading