@@ -45,24 +45,25 @@ public class InvocationContext {
4545 private final BaseMemoryService memoryService ;
4646 private final Plugin pluginManager ;
4747 private final Optional <LiveRequestQueue > liveRequestQueue ;
48- private final Map <String , ActiveStreamingTool > activeStreamingTools = new ConcurrentHashMap <>() ;
48+ private final Map <String , ActiveStreamingTool > activeStreamingTools ;
4949 private final String invocationId ;
5050 private final Session session ;
5151 private final Optional <Content > userContent ;
5252 private final RunConfig runConfig ;
5353 private final ResumabilityConfig resumabilityConfig ;
54- private final InvocationCostManager invocationCostManager = new InvocationCostManager () ;
54+ private final InvocationCostManager invocationCostManager ;
5555
5656 private Optional <String > branch ;
5757 private BaseAgent agent ;
5858 private boolean endInvocation ;
5959
60- private InvocationContext (Builder builder ) {
60+ protected InvocationContext (Builder builder ) {
6161 this .sessionService = builder .sessionService ;
6262 this .artifactService = builder .artifactService ;
6363 this .memoryService = builder .memoryService ;
6464 this .pluginManager = builder .pluginManager ;
6565 this .liveRequestQueue = builder .liveRequestQueue ;
66+ this .activeStreamingTools = builder .activeStreamingTools ;
6667 this .branch = builder .branch ;
6768 this .invocationId = builder .invocationId ;
6869 this .agent = builder .agent ;
@@ -71,6 +72,7 @@ private InvocationContext(Builder builder) {
7172 this .runConfig = builder .runConfig ;
7273 this .endInvocation = builder .endInvocation ;
7374 this .resumabilityConfig = builder .resumabilityConfig ;
75+ this .invocationCostManager = builder .invocationCostManager ;
7476 }
7577
7678 /**
@@ -188,7 +190,7 @@ public static InvocationContext create(
188190 .artifactService (artifactService )
189191 .agent (agent )
190192 .session (session )
191- .liveRequestQueue (Optional . ofNullable ( liveRequestQueue ) )
193+ .liveRequestQueue (liveRequestQueue )
192194 .runConfig (runConfig )
193195 .build ();
194196 }
@@ -198,26 +200,19 @@ public static Builder builder() {
198200 return new Builder ();
199201 }
200202
201- /** Creates a shallow copy of the given {@link InvocationContext}. */
203+ /** Returns a {@link Builder} initialized with the values of this instance. */
204+ public Builder toBuilder () {
205+ return new Builder (this );
206+ }
207+
208+ /**
209+ * Creates a shallow copy of the given {@link InvocationContext}.
210+ *
211+ * @deprecated Use {@code other.toBuilder().build()} instead.
212+ */
213+ @ Deprecated (forRemoval = true )
202214 public static InvocationContext copyOf (InvocationContext other ) {
203- InvocationContext newContext =
204- builder ()
205- .sessionService (other .sessionService )
206- .artifactService (other .artifactService )
207- .memoryService (other .memoryService )
208- .pluginManager (other .pluginManager )
209- .liveRequestQueue (other .liveRequestQueue )
210- .branch (other .branch )
211- .invocationId (other .invocationId )
212- .agent (other .agent )
213- .session (other .session )
214- .userContent (other .userContent )
215- .runConfig (other .runConfig )
216- .endInvocation (other .endInvocation )
217- .resumabilityConfig (other .resumabilityConfig )
218- .build ();
219- newContext .activeStreamingTools .putAll (other .activeStreamingTools );
220- return newContext ;
215+ return other .toBuilder ().build ();
221216 }
222217
223218 /** Returns the session service for managing session state. */
@@ -258,7 +253,10 @@ public String invocationId() {
258253 /**
259254 * Sets the [branch] ID for the current invocation. A branch represents a fork in the conversation
260255 * history.
256+ *
257+ * @deprecated Use {@link #toBuilder()} and {@link Builder#branch(String)} instead.
261258 */
259+ @ Deprecated (forRemoval = true )
262260 public void branch (@ Nullable String branch ) {
263261 this .branch = Optional .ofNullable (branch );
264262 }
@@ -276,7 +274,12 @@ public BaseAgent agent() {
276274 return agent ;
277275 }
278276
279- /** Sets the [agent] being invoked. This is useful when delegating to a sub-agent. */
277+ /**
278+ * Sets the [agent] being invoked. This is useful when delegating to a sub-agent.
279+ *
280+ * @deprecated Use {@link #toBuilder()} and {@link Builder#agent(BaseAgent)} instead.
281+ */
282+ @ Deprecated (forRemoval = true )
280283 public void agent (BaseAgent agent ) {
281284 this .agent = agent ;
282285 }
@@ -370,15 +373,53 @@ void incrementAndEnforceLlmCallsLimit(RunConfig runConfig)
370373 "Max number of llm calls limit of " + runConfig .maxLlmCalls () + " exceeded" );
371374 }
372375 }
376+
377+ @ Override
378+ public boolean equals (Object o ) {
379+ if (this == o ) {
380+ return true ;
381+ }
382+ if (!(o instanceof InvocationCostManager that )) {
383+ return false ;
384+ }
385+ return numberOfLlmCalls == that .numberOfLlmCalls ;
386+ }
387+
388+ @ Override
389+ public int hashCode () {
390+ return Integer .hashCode (numberOfLlmCalls );
391+ }
373392 }
374393
375394 /** Builder for {@link InvocationContext}. */
376395 public static class Builder {
396+
397+ private Builder () {}
398+
399+ private Builder (InvocationContext context ) {
400+ this .sessionService = context .sessionService ;
401+ this .artifactService = context .artifactService ;
402+ this .memoryService = context .memoryService ;
403+ this .pluginManager = context .pluginManager ;
404+ this .liveRequestQueue = context .liveRequestQueue ;
405+ this .activeStreamingTools = new ConcurrentHashMap <>(context .activeStreamingTools );
406+ this .branch = context .branch ;
407+ this .invocationId = context .invocationId ;
408+ this .agent = context .agent ;
409+ this .session = context .session ;
410+ this .userContent = context .userContent ;
411+ this .runConfig = context .runConfig ;
412+ this .endInvocation = context .endInvocation ;
413+ this .resumabilityConfig = context .resumabilityConfig ;
414+ this .invocationCostManager = context .invocationCostManager ;
415+ }
416+
377417 private BaseSessionService sessionService ;
378418 private BaseArtifactService artifactService ;
379419 private BaseMemoryService memoryService ;
380420 private Plugin pluginManager = new PluginManager ();
381421 private Optional <LiveRequestQueue > liveRequestQueue = Optional .empty ();
422+ private Map <String , ActiveStreamingTool > activeStreamingTools = new ConcurrentHashMap <>();
382423 private Optional <String > branch = Optional .empty ();
383424 private String invocationId = newInvocationContextId ();
384425 private BaseAgent agent ;
@@ -387,6 +428,7 @@ public static class Builder {
387428 private RunConfig runConfig = RunConfig .builder ().build ();
388429 private boolean endInvocation = false ;
389430 private ResumabilityConfig resumabilityConfig = new ResumabilityConfig ();
431+ private InvocationCostManager invocationCostManager = new InvocationCostManager ();
390432
391433 /**
392434 * Sets the session service for managing session state.
@@ -458,8 +500,8 @@ public Builder liveRequestQueue(Optional<LiveRequestQueue> liveRequestQueue) {
458500 * @return this builder instance for chaining.
459501 */
460502 @ CanIgnoreReturnValue
461- public Builder liveRequestQueue (LiveRequestQueue liveRequestQueue ) {
462- this .liveRequestQueue = Optional .of (liveRequestQueue );
503+ public Builder liveRequestQueue (@ Nullable LiveRequestQueue liveRequestQueue ) {
504+ this .liveRequestQueue = Optional .ofNullable (liveRequestQueue );
463505 return this ;
464506 }
465507
@@ -618,7 +660,8 @@ public boolean equals(Object o) {
618660 && Objects .equals (session , that .session )
619661 && Objects .equals (userContent , that .userContent )
620662 && Objects .equals (runConfig , that .runConfig )
621- && Objects .equals (resumabilityConfig , that .resumabilityConfig );
663+ && Objects .equals (resumabilityConfig , that .resumabilityConfig )
664+ && Objects .equals (invocationCostManager , that .invocationCostManager );
622665 }
623666
624667 @ Override
@@ -637,6 +680,7 @@ public int hashCode() {
637680 userContent ,
638681 runConfig ,
639682 endInvocation ,
640- resumabilityConfig );
683+ resumabilityConfig ,
684+ invocationCostManager );
641685 }
642686}
0 commit comments