2121import com .google .adk .SchemaUtils ;
2222import com .google .adk .agents .BaseAgent ;
2323import com .google .adk .agents .LlmAgent ;
24+ import com .google .adk .artifacts .BaseArtifactService ;
25+ import com .google .adk .artifacts .InMemoryArtifactService ;
2426import com .google .adk .events .Event ;
25- import com .google .adk .runner .InMemoryRunner ;
27+ import com .google .adk .memory .BaseMemoryService ;
28+ import com .google .adk .memory .InMemoryMemoryService ;
29+ import com .google .adk .plugins .BasePlugin ;
2630import com .google .adk .runner .Runner ;
31+ import com .google .adk .sessions .BaseSessionService ;
32+ import com .google .adk .sessions .InMemorySessionService ;
2733import com .google .common .collect .ImmutableList ;
2834import com .google .common .collect .ImmutableMap ;
2935import com .google .genai .types .Content ;
3036import com .google .genai .types .FunctionDeclaration ;
3137import com .google .genai .types .Part ;
3238import com .google .genai .types .Schema ;
3339import io .reactivex .rxjava3 .core .Single ;
40+ import java .util .List ;
3441import java .util .Map ;
3542import java .util .Optional ;
3643
@@ -39,19 +46,42 @@ public class AgentTool extends BaseTool {
3946
4047 private final BaseAgent agent ;
4148 private final boolean skipSummarization ;
49+ private final List <BasePlugin > plugins ;
50+ private final BaseSessionService sessionService ;
51+ private final BaseArtifactService artifactService ;
52+ private final BaseMemoryService memoryService ;
53+
54+ public static AgentTool create (
55+ BaseAgent agent ,
56+ BaseSessionService sessionService ,
57+ BaseArtifactService artifactService ,
58+ BaseMemoryService memoryService ,
59+ List <BasePlugin > plugins ) {
60+ return new AgentTool (agent , false , sessionService , artifactService , memoryService , plugins );
61+ }
4262
4363 public static AgentTool create (BaseAgent agent , boolean skipSummarization ) {
44- return new AgentTool (agent , skipSummarization );
64+ return new AgentTool (agent , skipSummarization , null , null , null , ImmutableList . of () );
4565 }
4666
4767 public static AgentTool create (BaseAgent agent ) {
48- return new AgentTool (agent , false );
68+ return new AgentTool (agent , false , null , null , null , ImmutableList . of () );
4969 }
5070
51- protected AgentTool (BaseAgent agent , boolean skipSummarization ) {
71+ protected AgentTool (
72+ BaseAgent agent ,
73+ boolean skipSummarization ,
74+ BaseSessionService sessionService ,
75+ BaseArtifactService artifactService ,
76+ BaseMemoryService memoryService ,
77+ List <BasePlugin > plugins ) {
5278 super (agent .name (), agent .description ());
5379 this .agent = agent ;
5480 this .skipSummarization = skipSummarization ;
81+ this .sessionService = sessionService ;
82+ this .artifactService = artifactService ;
83+ this .memoryService = memoryService ;
84+ this .plugins = plugins != null ? plugins : ImmutableList .of ();
5585 }
5686
5787 @ Override
@@ -104,12 +134,34 @@ public Single<Map<String, Object>> runAsync(Map<String, Object> args, ToolContex
104134 content = Content .fromParts (Part .fromText (input .toString ()));
105135 }
106136
107- Runner runner = new InMemoryRunner (this .agent , toolContext .agentName ());
137+ // Determine effective services: use injected singletons if present, otherwise create fresh
138+ // instances per run (default behavior)
139+ BaseSessionService effectiveSessionService =
140+ this .sessionService != null ? this .sessionService : new InMemorySessionService ();
141+ BaseArtifactService effectiveArtifactService =
142+ this .artifactService != null ? this .artifactService : new InMemoryArtifactService ();
143+ BaseMemoryService effectiveMemoryService =
144+ this .memoryService != null ? this .memoryService : new InMemoryMemoryService ();
145+
146+ Runner runner =
147+ new Runner (
148+ this .agent ,
149+ toolContext .agentName (),
150+ effectiveArtifactService ,
151+ effectiveSessionService ,
152+ effectiveMemoryService ,
153+ this .plugins );
154+
155+ String userId = "tmp-user" ;
156+ if (toolContext .userId () != null ) {
157+ userId = toolContext .userId ();
158+ }
159+
108160 // Session state is final, can't update to toolContext state
109161 // session.toBuilder().setState(toolContext.getState());
110162 return runner
111163 .sessionService ()
112- .createSession (toolContext .agentName (), "tmp-user" , toolContext .state (), null )
164+ .createSession (toolContext .agentName (), userId , toolContext .state (), null )
113165 .flatMapPublisher (session -> runner .runAsync (session .userId (), session .id (), content ))
114166 .lastElement ()
115167 .map (Optional ::of )
0 commit comments