1717package com .google .adk .agents ;
1818
1919import com .google .adk .artifacts .BaseArtifactService ;
20+ import com .google .adk .events .Event ;
21+ import com .google .adk .flows .llmflows .ResumabilityConfig ;
2022import com .google .adk .memory .BaseMemoryService ;
2123import com .google .adk .models .LlmCallsLimitExceededException ;
2224import com .google .adk .plugins .PluginManager ;
2325import com .google .adk .sessions .BaseSessionService ;
2426import com .google .adk .sessions .Session ;
27+ import com .google .common .collect .ImmutableSet ;
2528import com .google .errorprone .annotations .CanIgnoreReturnValue ;
2629import com .google .errorprone .annotations .InlineMe ;
2730import com .google .genai .types .Content ;
31+ import com .google .genai .types .FunctionCall ;
2832import java .util .Map ;
2933import java .util .Objects ;
3034import java .util .Optional ;
@@ -45,6 +49,7 @@ public class InvocationContext {
4549 private final Session session ;
4650 private final Optional <Content > userContent ;
4751 private final RunConfig runConfig ;
52+ private final ResumabilityConfig resumabilityConfig ;
4853 private final InvocationCostManager invocationCostManager = new InvocationCostManager ();
4954
5055 private Optional <String > branch ;
@@ -64,6 +69,7 @@ private InvocationContext(Builder builder) {
6469 this .userContent = builder .userContent ;
6570 this .runConfig = builder .runConfig ;
6671 this .endInvocation = builder .endInvocation ;
72+ this .resumabilityConfig = builder .resumabilityConfig ;
6773 }
6874
6975 /**
@@ -207,6 +213,7 @@ public static InvocationContext copyOf(InvocationContext other) {
207213 .userContent (other .userContent )
208214 .runConfig (other .runConfig )
209215 .endInvocation (other .endInvocation )
216+ .resumabilityConfig (other .resumabilityConfig )
210217 .build ();
211218 newContext .activeStreamingTools .putAll (other .activeStreamingTools );
212219 return newContext ;
@@ -248,10 +255,8 @@ public String invocationId() {
248255 }
249256
250257 /**
251- * Sets the branch ID for the current invocation. A branch represents a fork in the conversation
258+ * Sets the [ branch] ID for the current invocation. A branch represents a fork in the conversation
252259 * history.
253- *
254- * @param branch the branch ID, or null to clear it
255260 */
256261 public void branch (@ Nullable String branch ) {
257262 this .branch = Optional .ofNullable (branch );
@@ -270,11 +275,7 @@ public BaseAgent agent() {
270275 return agent ;
271276 }
272277
273- /**
274- * Sets the agent being invoked. This is useful when delegating to a sub-agent.
275- *
276- * @param agent the agent to set
277- */
278+ /** Sets the [agent] being invoked. This is useful when delegating to a sub-agent. */
278279 public void agent (BaseAgent agent ) {
279280 this .agent = agent ;
280281 }
@@ -302,11 +303,7 @@ public boolean endInvocation() {
302303 return endInvocation ;
303304 }
304305
305- /**
306- * Sets whether this invocation should be ended.
307- *
308- * @param endInvocation true if the invocation should end, false otherwise
309- */
306+ /** Sets whether this invocation should be ended. */
310307 public void setEndInvocation (boolean endInvocation ) {
311308 this .endInvocation = endInvocation ;
312309 }
@@ -336,6 +333,28 @@ public void incrementLlmCallsCount() throws LlmCallsLimitExceededException {
336333 this .invocationCostManager .incrementAndEnforceLlmCallsLimit (this .runConfig );
337334 }
338335
336+ /** Returns whether the current invocation is resumable. */
337+ public boolean isResumable () {
338+ return resumabilityConfig .isResumable ();
339+ }
340+
341+ /** Returns whether to pause the invocation right after this [event]. */
342+ public boolean shouldPauseInvocation (Event event ) {
343+ if (!isResumable ()) {
344+ return false ;
345+ }
346+
347+ var longRunningToolIds = event .longRunningToolIds ().orElse (ImmutableSet .of ());
348+ if (longRunningToolIds .isEmpty ()) {
349+ return false ;
350+ }
351+
352+ return event .functionCalls ().stream ()
353+ .map (FunctionCall ::id )
354+ .flatMap (Optional ::stream )
355+ .anyMatch (functionCallId -> longRunningToolIds .contains (functionCallId ));
356+ }
357+
339358 private static class InvocationCostManager {
340359 private int numberOfLlmCalls = 0 ;
341360
@@ -366,6 +385,7 @@ public static class Builder {
366385 private Optional <Content > userContent = Optional .empty ();
367386 private RunConfig runConfig = RunConfig .builder ().build ();
368387 private boolean endInvocation = false ;
388+ private ResumabilityConfig resumabilityConfig = new ResumabilityConfig ();
369389
370390 /**
371391 * Sets the session service for managing session state.
@@ -553,6 +573,18 @@ public Builder endInvocation(boolean endInvocation) {
553573 return this ;
554574 }
555575
576+ /**
577+ * Sets the resumability configuration for the current agent run.
578+ *
579+ * @param resumabilityConfig the resumability configuration.
580+ * @return this builder instance for chaining.
581+ */
582+ @ CanIgnoreReturnValue
583+ public Builder resumabilityConfig (ResumabilityConfig resumabilityConfig ) {
584+ this .resumabilityConfig = resumabilityConfig ;
585+ return this ;
586+ }
587+
556588 /**
557589 * Builds the {@link InvocationContext} instance.
558590 *
@@ -584,7 +616,8 @@ public boolean equals(Object o) {
584616 && Objects .equals (agent , that .agent )
585617 && Objects .equals (session , that .session )
586618 && Objects .equals (userContent , that .userContent )
587- && Objects .equals (runConfig , that .runConfig );
619+ && Objects .equals (runConfig , that .runConfig )
620+ && Objects .equals (resumabilityConfig , that .resumabilityConfig );
588621 }
589622
590623 @ Override
@@ -602,6 +635,7 @@ public int hashCode() {
602635 session ,
603636 userContent ,
604637 runConfig ,
605- endInvocation );
638+ endInvocation ,
639+ resumabilityConfig );
606640 }
607641}
0 commit comments