Skip to content
Open
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
49 changes: 46 additions & 3 deletions core/src/main/java/com/google/adk/agents/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.adk.annotations.Experimental;
import com.google.adk.artifacts.ListArtifactsResponse;
import com.google.adk.events.Event;
import com.google.adk.events.EventActions;
import com.google.adk.events.ToolConfirmation;
import com.google.adk.memory.SearchMemoryResponse;
Expand All @@ -29,14 +30,17 @@
import com.google.adk.workflow.Route;
import com.google.adk.workflow.RunNodeConfig;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.genai.types.Content;
import com.google.genai.types.Part;
import io.reactivex.rxjava3.core.Completable;
import io.reactivex.rxjava3.core.Maybe;
import io.reactivex.rxjava3.core.Single;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CancellationException;
import org.jspecify.annotations.Nullable;

/**
Expand Down Expand Up @@ -345,6 +349,45 @@ public final int attemptCount() {
return requireNodeState().attemptCount();
}

/**
* Sends {@code event} from this workflow node's {@code FunctionNode} body, after the events of
* earlier calls and before the output. It returns once the run has stored the event and handed it
* to the run's caller, and any thread of the body may call it. The {@link
* com.google.adk.workflow.FunctionNode} documentation gives the rules for state, threads, events,
* cancellation and resumes.
*
* @throws IllegalArgumentException if {@code event} has an output, or has the ID of an event
* another {@code emit} call still waits on
* @throws IllegalStateException if no {@code FunctionNode} body of this node is running
* @throws CancellationException if the node is canceled, or the calling thread is interrupted
* while this call waits; the thread stays interrupted
*/
@Experimental
public final void emit(Event event) {
Preconditions.checkNotNull(event);
if (Strings.isNullOrEmpty(event.id())) {
event = event.toBuilder().id(invocationContext.newUuid()).build();
}
if (nodeState == null || !nodeState.emitFromBody(event)) {
throw new IllegalStateException(
"Context.emit works only while the FunctionNode body of this context's node runs; a"
+ " custom node sends events as items of its Flowable, and a tool or callback"
+ " returns its result.");
}
}

/** Sends {@code message} as an event without an output, as {@link #emit(Event)} does. */
@Experimental
public final void emit(Content message) {
Preconditions.checkNotNull(message);
emit(
Event.builder()
.author("")
.timestamp(invocationContext.now().toEpochMilli())
.content(message)
.build());
}

/**
* Runs {@code node} as a child of this workflow node without an input, as {@link #runNode(Node,
* Object, RunNodeConfig)} does with the default {@link RunNodeConfig}.
Expand Down Expand Up @@ -372,9 +415,9 @@ public final Maybe<Object> runNode(Node node, @Nullable Object nodeInput) {
/**
* Returns a {@link Maybe} that, when subscribed, runs {@code node} as a child of this workflow
* node and emits the child's output, or completes empty if it has none. Include it in this node's
* run and catch only failures you handle, such as {@link DynamicNodeFailedException}: when the
* child waits, it fails with an internal {@link RuntimeException} that must propagate to end this
* node's run, so that it waits too.
* run: when the child waits, the {@code Maybe} fails with an internal {@link Error} so that this
* node waits too, which {@code catch (Exception e)} lets through but {@code catch (Throwable t)}
* and Rx error handlers such as {@code onErrorResumeNext} stop.
*
* <p>It fails with {@link DynamicNodeFailedException} if the child fails; with {@link
* IllegalStateException} if this is not a workflow node's context, if this node's {@link
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,8 @@ public String userId() {
* Returns the {@link Scheduler} on which this invocation runs the work ADK hands to worker
* threads: the sub-agents of a {@code ParallelAgent}, tool calls in {@code
* ToolExecutionMode.PARALLEL_SUBSCRIBE} mode, workflow node retries and attempts with a timeout,
* the bodies of workflow {@code FunctionNode}s, and the hop that starts the live send loop. An
* agent-level setting ({@code ParallelAgent.Builder#scheduler}, {@code
* the hand-back once a workflow {@code FunctionNode} body returns, and the hop that starts the
* live send loop. An agent-level setting ({@code ParallelAgent.Builder#scheduler}, {@code
* LlmAgent.Builder#executor}) takes precedence over it. Defaults to {@link Schedulers#io()}.
*/
public Scheduler scheduler() {
Expand Down
18 changes: 10 additions & 8 deletions core/src/main/java/com/google/adk/runner/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,17 @@ public Builder plugins(Plugin... plugins) {
* Sets the {@link Scheduler} for the work ADK hands off to worker threads in this runner's
* invocations: the sub-agents of a {@code ParallelAgent}, tool calls in {@link
* RunConfig.ToolExecutionMode#PARALLEL_SUBSCRIBE} mode, workflow node retries and attempts with
* a timeout, the bodies of workflow {@code FunctionNode}s, and the hop that starts the live
* send loop. The runs that an {@code AgentTool} nests inherit it. An agent-level {@code
* ParallelAgent.Builder#scheduler} or {@code LlmAgent.Builder#executor} takes precedence.
* {@code null} (the default) selects {@code Schedulers.io()}.
* a timeout, the hand-back once a workflow {@code FunctionNode} body returns, and the hop that
* starts the live send loop. The runs that an {@code AgentTool} nests inherit it. An
* agent-level {@code ParallelAgent.Builder#scheduler} or {@code LlmAgent.Builder#executor}
* takes precedence. {@code null} (the default) selects {@code Schedulers.io()}.
*
* <p>Only these hand-offs are affected; work that completes asynchronously elsewhere (model
* clients, session services, MCP, plugins) stays on its own threads. {@code
* Schedulers.trampoline()} runs the hand-offs inline, so sub-agents and tools are subscribed
* sequentially, but anything they start asynchronously can still overlap.
* <p>Only these hand-offs are affected: work that completes asynchronously elsewhere (model
* clients, session services, MCP, plugins) stays on its own threads, and a workflow {@code
* FunctionNode} runs its body on {@code Schedulers.io()} unless {@code
* FunctionNode.Builder#scheduler} sets another. {@code Schedulers.trampoline()} runs the
* hand-offs inline, so sub-agents and tools are subscribed sequentially, but anything they
* start asynchronously can still overlap.
*/
@CanIgnoreReturnValue
public Builder scheduler(@Nullable Scheduler scheduler) {
Expand Down
Loading
Loading