@@ -44,24 +44,25 @@ public class InvocationContext {
4444 private final BaseMemoryService memoryService ;
4545 private final PluginManager pluginManager ;
4646 private final Optional <LiveRequestQueue > liveRequestQueue ;
47- private final Map <String , ActiveStreamingTool > activeStreamingTools = new ConcurrentHashMap <>() ;
47+ private final Map <String , ActiveStreamingTool > activeStreamingTools ;
4848 private final String invocationId ;
4949 private final Session session ;
5050 private final Optional <Content > userContent ;
5151 private final RunConfig runConfig ;
5252 private final ResumabilityConfig resumabilityConfig ;
53- private final InvocationCostManager invocationCostManager = new InvocationCostManager () ;
53+ private final InvocationCostManager invocationCostManager ;
5454
5555 private Optional <String > branch ;
5656 private BaseAgent agent ;
5757 private boolean endInvocation ;
5858
59- private InvocationContext (Builder builder ) {
59+ protected InvocationContext (Builder builder ) {
6060 this .sessionService = builder .sessionService ;
6161 this .artifactService = builder .artifactService ;
6262 this .memoryService = builder .memoryService ;
6363 this .pluginManager = builder .pluginManager ;
6464 this .liveRequestQueue = builder .liveRequestQueue ;
65+ this .activeStreamingTools = builder .activeStreamingTools ;
6566 this .branch = builder .branch ;
6667 this .invocationId = builder .invocationId ;
6768 this .agent = builder .agent ;
@@ -70,6 +71,7 @@ private InvocationContext(Builder builder) {
7071 this .runConfig = builder .runConfig ;
7172 this .endInvocation = builder .endInvocation ;
7273 this .resumabilityConfig = builder .resumabilityConfig ;
74+ this .invocationCostManager = builder .invocationCostManager ;
7375 }
7476
7577 /**
@@ -187,7 +189,7 @@ public static InvocationContext create(
187189 .artifactService (artifactService )
188190 .agent (agent )
189191 .session (session )
190- .liveRequestQueue (Optional . ofNullable ( liveRequestQueue ) )
192+ .liveRequestQueue (liveRequestQueue )
191193 .runConfig (runConfig )
192194 .build ();
193195 }
@@ -197,26 +199,19 @@ public static Builder builder() {
197199 return new Builder ();
198200 }
199201
200- /** Creates a shallow copy of the given {@link InvocationContext}. */
202+ /** Returns a {@link Builder} initialized with the values of this instance. */
203+ public Builder toBuilder () {
204+ return new Builder (this );
205+ }
206+
207+ /**
208+ * Creates a shallow copy of the given {@link InvocationContext}.
209+ *
210+ * @deprecated Use {@code other.toBuilder().build()} instead.
211+ */
212+ @ Deprecated (forRemoval = true )
201213 public static InvocationContext copyOf (InvocationContext other ) {
202- InvocationContext newContext =
203- builder ()
204- .sessionService (other .sessionService )
205- .artifactService (other .artifactService )
206- .memoryService (other .memoryService )
207- .pluginManager (other .pluginManager )
208- .liveRequestQueue (other .liveRequestQueue )
209- .branch (other .branch )
210- .invocationId (other .invocationId )
211- .agent (other .agent )
212- .session (other .session )
213- .userContent (other .userContent )
214- .runConfig (other .runConfig )
215- .endInvocation (other .endInvocation )
216- .resumabilityConfig (other .resumabilityConfig )
217- .build ();
218- newContext .activeStreamingTools .putAll (other .activeStreamingTools );
219- return newContext ;
214+ return other .toBuilder ().build ();
220215 }
221216
222217 /** Returns the session service for managing session state. */
@@ -257,7 +252,10 @@ public String invocationId() {
257252 /**
258253 * Sets the [branch] ID for the current invocation. A branch represents a fork in the conversation
259254 * history.
255+ *
256+ * @deprecated Use {@link #toBuilder()} and {@link Builder#branch(String)} instead.
260257 */
258+ @ Deprecated (forRemoval = true )
261259 public void branch (@ Nullable String branch ) {
262260 this .branch = Optional .ofNullable (branch );
263261 }
@@ -275,7 +273,12 @@ public BaseAgent agent() {
275273 return agent ;
276274 }
277275
278- /** Sets the [agent] being invoked. This is useful when delegating to a sub-agent. */
276+ /**
277+ * Sets the [agent] being invoked. This is useful when delegating to a sub-agent.
278+ *
279+ * @deprecated Use {@link #toBuilder()} and {@link Builder#agent(BaseAgent)} instead.
280+ */
281+ @ Deprecated (forRemoval = true )
279282 public void agent (BaseAgent agent ) {
280283 this .agent = agent ;
281284 }
@@ -369,15 +372,53 @@ void incrementAndEnforceLlmCallsLimit(RunConfig runConfig)
369372 "Max number of llm calls limit of " + runConfig .maxLlmCalls () + " exceeded" );
370373 }
371374 }
375+
376+ @ Override
377+ public boolean equals (Object o ) {
378+ if (this == o ) {
379+ return true ;
380+ }
381+ if (!(o instanceof InvocationCostManager that )) {
382+ return false ;
383+ }
384+ return numberOfLlmCalls == that .numberOfLlmCalls ;
385+ }
386+
387+ @ Override
388+ public int hashCode () {
389+ return Integer .hashCode (numberOfLlmCalls );
390+ }
372391 }
373392
374393 /** Builder for {@link InvocationContext}. */
375394 public static class Builder {
395+
396+ private Builder () {}
397+
398+ private Builder (InvocationContext context ) {
399+ this .sessionService = context .sessionService ;
400+ this .artifactService = context .artifactService ;
401+ this .memoryService = context .memoryService ;
402+ this .pluginManager = context .pluginManager ;
403+ this .liveRequestQueue = context .liveRequestQueue ;
404+ this .activeStreamingTools = new ConcurrentHashMap <>(context .activeStreamingTools );
405+ this .branch = context .branch ;
406+ this .invocationId = context .invocationId ;
407+ this .agent = context .agent ;
408+ this .session = context .session ;
409+ this .userContent = context .userContent ;
410+ this .runConfig = context .runConfig ;
411+ this .endInvocation = context .endInvocation ;
412+ this .resumabilityConfig = context .resumabilityConfig ;
413+ this .invocationCostManager = context .invocationCostManager ;
414+ }
415+
376416 private BaseSessionService sessionService ;
377417 private BaseArtifactService artifactService ;
378418 private BaseMemoryService memoryService ;
379419 private PluginManager pluginManager = new PluginManager ();
380420 private Optional <LiveRequestQueue > liveRequestQueue = Optional .empty ();
421+ private Map <String , ActiveStreamingTool > activeStreamingTools = new ConcurrentHashMap <>();
381422 private Optional <String > branch = Optional .empty ();
382423 private String invocationId = newInvocationContextId ();
383424 private BaseAgent agent ;
@@ -386,6 +427,7 @@ public static class Builder {
386427 private RunConfig runConfig = RunConfig .builder ().build ();
387428 private boolean endInvocation = false ;
388429 private ResumabilityConfig resumabilityConfig = new ResumabilityConfig ();
430+ private InvocationCostManager invocationCostManager = new InvocationCostManager ();
389431
390432 /**
391433 * Sets the session service for managing session state.
@@ -457,8 +499,8 @@ public Builder liveRequestQueue(Optional<LiveRequestQueue> liveRequestQueue) {
457499 * @return this builder instance for chaining.
458500 */
459501 @ CanIgnoreReturnValue
460- public Builder liveRequestQueue (LiveRequestQueue liveRequestQueue ) {
461- this .liveRequestQueue = Optional .of (liveRequestQueue );
502+ public Builder liveRequestQueue (@ Nullable LiveRequestQueue liveRequestQueue ) {
503+ this .liveRequestQueue = Optional .ofNullable (liveRequestQueue );
462504 return this ;
463505 }
464506
@@ -617,7 +659,8 @@ public boolean equals(Object o) {
617659 && Objects .equals (session , that .session )
618660 && Objects .equals (userContent , that .userContent )
619661 && Objects .equals (runConfig , that .runConfig )
620- && Objects .equals (resumabilityConfig , that .resumabilityConfig );
662+ && Objects .equals (resumabilityConfig , that .resumabilityConfig )
663+ && Objects .equals (invocationCostManager , that .invocationCostManager );
621664 }
622665
623666 @ Override
@@ -636,6 +679,7 @@ public int hashCode() {
636679 userContent ,
637680 runConfig ,
638681 endInvocation ,
639- resumabilityConfig );
682+ resumabilityConfig ,
683+ invocationCostManager );
640684 }
641685}
0 commit comments