3333import com .google .adk .sessions .BaseSessionService ;
3434import com .google .adk .sessions .Session ;
3535import com .google .adk .summarizer .EventsCompactionConfig ;
36+ import com .google .adk .workflow .Node ;
3637import com .google .common .collect .ImmutableList ;
3738import com .google .common .collect .ImmutableMap ;
3839import com .google .common .collect .ImmutableSet ;
@@ -84,6 +85,7 @@ public class InvocationContext {
8485
8586 @ Nullable private String branch ;
8687 private BaseAgent agent ;
88+ private final @ Nullable Node node ;
8789 private boolean endInvocation ;
8890
8991 protected InvocationContext (Builder builder ) {
@@ -97,6 +99,7 @@ protected InvocationContext(Builder builder) {
9799 // Resolved here so subclasses that construct from a builder without build() also get an id.
98100 this .invocationId = builder .resolveInvocationId ();
99101 this .agent = builder .agent ;
102+ this .node = builder .node ;
100103 this .session = builder .session ;
101104 this .userContent = builder .userContent ;
102105 this .runConfig = builder .runConfig ;
@@ -182,6 +185,15 @@ public BaseAgent agent() {
182185 return agent ;
183186 }
184187
188+ /**
189+ * Returns the workflow node this invocation runs, or empty when it runs an agent. When it runs a
190+ * node, {@link #agent()} returns a {@link NodeViewAgent} that stands in for it.
191+ */
192+ @ Experimental
193+ public Optional <Node > node () {
194+ return Optional .ofNullable (node );
195+ }
196+
185197 /** Returns the session associated with this invocation. */
186198 public Session session () {
187199 return session ;
@@ -653,6 +665,7 @@ private Builder(InvocationContext context) {
653665 this .invocationId = context .invocationId ;
654666 this .invocationIdSet = true ;
655667 this .agent = context .agent ;
668+ this .node = context .node ;
656669 this .session = context .session ;
657670 this .userContent = context .userContent ;
658671 this .runConfig = context .runConfig ;
@@ -684,6 +697,7 @@ private Builder(InvocationContext context) {
684697 // Distinguishes "never set" (an id is minted from the UuidProvider) from an explicit null.
685698 private boolean invocationIdSet ;
686699 private BaseAgent agent ;
700+ private @ Nullable Node node ;
687701 private Session session ;
688702 @ Nullable private Content userContent = null ;
689703 private RunConfig runConfig = RunConfig .builder ().build ();
@@ -796,6 +810,17 @@ public Builder agent(BaseAgent agent) {
796810 return this ;
797811 }
798812
813+ /**
814+ * Sets the workflow node the invocation runs, or null when it runs an agent. When the
815+ * invocation runs a node, its agent is a {@link NodeViewAgent} that stands in for the node.
816+ */
817+ @ CanIgnoreReturnValue
818+ @ Experimental
819+ public Builder node (@ Nullable Node node ) {
820+ this .node = node ;
821+ return this ;
822+ }
823+
799824 /**
800825 * Sets the session associated with this invocation.
801826 *
@@ -968,6 +993,9 @@ private static void validate(Builder builder) {
968993 if (builder .agent == null ) {
969994 throw new IllegalStateException ("Agent must be set." );
970995 }
996+ if (builder .agent instanceof NodeViewAgent && builder .node == null ) {
997+ throw new IllegalStateException ("A node view agent requires the node it stands in for." );
998+ }
971999 if (builder .session == null ) {
9721000 throw new IllegalStateException ("Session must be set." );
9731001 }
@@ -994,6 +1022,7 @@ public boolean equals(Object o) {
9941022 && Objects .equals (branch , that .branch )
9951023 && Objects .equals (invocationId , that .invocationId )
9961024 && Objects .equals (agent , that .agent )
1025+ && Objects .equals (node , that .node )
9971026 && Objects .equals (session , that .session )
9981027 && Objects .equals (userContent , that .userContent )
9991028 && Objects .equals (runConfig , that .runConfig )
@@ -1019,6 +1048,7 @@ public int hashCode() {
10191048 branch ,
10201049 invocationId ,
10211050 agent ,
1051+ node ,
10221052 session ,
10231053 userContent ,
10241054 runConfig ,
0 commit comments