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: 57 additions & 50 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,38 +312,47 @@ 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(
() -> {
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));
});
() -> {
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);
}

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

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

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

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