Skip to content
Merged
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
107 changes: 50 additions & 57 deletions core/src/main/java/com/google/adk/agents/BaseAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.DoNotCall;
import com.google.genai.types.Content;
import io.opentelemetry.context.Context;
import io.reactivex.rxjava3.core.Completable;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.Maybe;
import io.reactivex.rxjava3.core.Single;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -312,47 +312,38 @@ public Flowable<Event> runAsync(InvocationContext parentContext) {
private Flowable<Event> run(
InvocationContext parentContext,
Function<InvocationContext, Flowable<Event>> runImplementation) {
Context otelParentContext = Context.current();
InvocationContext invocationContext = createInvocationContext(parentContext);

return Flowable.defer(
() -> {
return callCallback(
beforeCallbacksToFunctions(
invocationContext.pluginManager(), beforeAgentCallback),
invocationContext)
.flatMapPublisher(
beforeEvent -> {
if (invocationContext.endInvocation()) {
return Flowable.just(beforeEvent);
}

return Flowable.just(beforeEvent)
.concatWith(runMainAndAfter(invocationContext, runImplementation));
})
.switchIfEmpty(
Flowable.defer(() -> runMainAndAfter(invocationContext, runImplementation)));
})
.compose(
Tracing.traceAgent(
otelParentContext,
"invoke_agent " + name(),
name(),
description(),
invocationContext));
}

private Flowable<Event> runMainAndAfter(
InvocationContext invocationContext,
Function<InvocationContext, Flowable<Event>> runImplementation) {
Flowable<Event> mainEvents = runImplementation.apply(invocationContext);
Flowable<Event> afterEvents =
callCallback(
afterCallbacksToFunctions(invocationContext.pluginManager(), afterAgentCallback),
invocationContext)
.flatMapPublisher(Flowable::just);

return Flowable.concat(mainEvents, afterEvents);
() -> {
InvocationContext invocationContext = createInvocationContext(parentContext);

return callCallback(
beforeCallbacksToFunctions(
invocationContext.pluginManager(), beforeAgentCallback),
invocationContext)
.flatMapPublisher(
beforeEventOpt -> {
if (invocationContext.endInvocation()) {
return Flowable.fromOptional(beforeEventOpt);
}

Flowable<Event> beforeEvents = Flowable.fromOptional(beforeEventOpt);
Flowable<Event> mainEvents =
Flowable.defer(() -> runImplementation.apply(invocationContext));
Flowable<Event> afterEvents =
Flowable.defer(
() ->
callCallback(
afterCallbacksToFunctions(
invocationContext.pluginManager(), afterAgentCallback),
invocationContext)
.flatMapPublisher(Flowable::fromOptional));

return Flowable.concat(beforeEvents, mainEvents, afterEvents);
})
.compose(
Tracing.traceAgent(
"invoke_agent " + name(), name(), description(), invocationContext));
});
}

/**
Expand Down Expand Up @@ -392,13 +383,13 @@ private <T> ImmutableList<Function<CallbackContext, Maybe<Content>>> callbacksTo
*
* @param agentCallbacks Callback functions.
* @param invocationContext Current invocation context.
* @return Maybe emitting first event, or empty if none.
* @return single emitting first event, or empty if none.
*/
private Maybe<Event> callCallback(
private Single<Optional<Event>> callCallback(
List<Function<CallbackContext, Maybe<Content>>> agentCallbacks,
InvocationContext invocationContext) {
if (agentCallbacks.isEmpty()) {
return Maybe.empty();
return Single.just(Optional.empty());
}

CallbackContext callbackContext =
Expand All @@ -407,25 +398,27 @@ private Maybe<Event> callCallback(
return Flowable.fromIterable(agentCallbacks)
.concatMap(
callback -> {
return callback
.apply(callbackContext)
Maybe<Content> maybeContent = callback.apply(callbackContext);

return maybeContent
.map(
content -> {
invocationContext.setEndInvocation(true);
return Event.builder()
.id(Event.generateEventId())
.invocationId(invocationContext.invocationId())
.author(name())
.branch(invocationContext.branch().orElse(null))
.actions(callbackContext.eventActions())
.content(content)
.build();
return Optional.of(
Event.builder()
.id(Event.generateEventId())
.invocationId(invocationContext.invocationId())
.author(name())
.branch(invocationContext.branch().orElse(null))
.actions(callbackContext.eventActions())
.content(content)
.build());
})
.toFlowable();
})
.firstElement()
.switchIfEmpty(
Maybe.defer(
Single.defer(
() -> {
if (callbackContext.state().hasDelta()) {
Event.Builder eventBuilder =
Expand All @@ -436,9 +429,9 @@ private Maybe<Event> callCallback(
.branch(invocationContext.branch().orElse(null))
.actions(callbackContext.eventActions());

return Maybe.just(eventBuilder.build());
return Single.just(Optional.of(eventBuilder.build()));
} else {
return Maybe.empty();
return Single.just(Optional.empty());
}
}));
}
Expand Down
Loading