Skip to content

Commit a9a382f

Browse files
Jacksunweicopybara-github
authored andcommitted
chore: Copies latest session state to the passed-in session for runAsync with Session
NOTE: `runAsync` with `Session` object was an early interface and doesn't comply with ADK interfaces across languages. Adding this behavior back is to temporarily restore previous behavior. Here are some clarifications of the behavior: - Any changes to the passed-in `Session` object at `runAsync` callsite won't affect agent run; - Callsite should always use session service to get latest session if they need to access session, instead of reading into the passed-in session object. - All runAsync with Session object will be deprecated and removed in future versions. PiperOrigin-RevId: 820077843
1 parent 9f887c7 commit a9a382f

1 file changed

Lines changed: 29 additions & 9 deletions

File tree

core/src/main/java/com/google/adk/runner/Runner.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,12 @@ public Flowable<Event> runAsync(String userId, String sessionId, Content newMess
225225
return runAsync(userId, sessionId, newMessage, RunConfig.builder().build());
226226
}
227227

228-
/** See {@link #runAsync(Session, Content, RunConfig, Map)}. */
228+
/**
229+
* See {@link #runAsync(Session, Content, RunConfig, Map)}.
230+
*
231+
* @deprecated Use runAsync with sessionId.
232+
*/
233+
@Deprecated(since = "0.4.0", forRemoval = true)
229234
public Flowable<Event> runAsync(Session session, Content newMessage, RunConfig runConfig) {
230235
return runAsync(session, newMessage, runConfig, /* stateDelta= */ null);
231236
}
@@ -238,7 +243,9 @@ public Flowable<Event> runAsync(Session session, Content newMessage, RunConfig r
238243
* @param runConfig Configuration for the agent run.
239244
* @param stateDelta Optional map of state updates to merge into the session for this run.
240245
* @return A Flowable stream of {@link Event} objects generated by the agent during execution.
246+
* @deprecated Use runAsync with sessionId.
241247
*/
248+
@Deprecated(since = "0.4.0", forRemoval = true)
242249
public Flowable<Event> runAsync(
243250
Session session,
244251
Content newMessage,
@@ -321,14 +328,20 @@ public Flowable<Event> runAsync(
321328
this.sessionService
322329
.appendEvent(updatedSession, agentEvent)
323330
.flatMap(
324-
registeredEvent ->
325-
contextWithUpdatedSession
326-
.pluginManager()
327-
.runOnEventCallback(
328-
contextWithUpdatedSession,
329-
registeredEvent)
330-
.defaultIfEmpty(
331-
registeredEvent))
331+
registeredEvent -> {
332+
// TODO: remove this hack after
333+
// deprecating runAsync with
334+
// Session.
335+
copySessionStates(
336+
updatedSession, session);
337+
return contextWithUpdatedSession
338+
.pluginManager()
339+
.runOnEventCallback(
340+
contextWithUpdatedSession,
341+
registeredEvent)
342+
.defaultIfEmpty(
343+
registeredEvent);
344+
})
332345
.toFlowable());
333346

334347
// If beforeRunCallback returns content, emit it and skip
@@ -359,6 +372,13 @@ public Flowable<Event> runAsync(
359372
}
360373
}
361374

375+
private void copySessionStates(Session source, Session target) {
376+
// TODO: remove this hack when deprecating all runAsync with Session.
377+
for (var entry : source.state().entrySet()) {
378+
target.state().put(entry.getKey(), entry.getValue());
379+
}
380+
}
381+
362382
/**
363383
* Creates an {@link InvocationContext} for a live (streaming) run.
364384
*

0 commit comments

Comments
 (0)