Skip to content

Commit 1cafa7d

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 84e755c commit 1cafa7d

1 file changed

Lines changed: 21 additions & 8 deletions

File tree

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -321,14 +321,20 @@ public Flowable<Event> runAsync(
321321
this.sessionService
322322
.appendEvent(updatedSession, agentEvent)
323323
.flatMap(
324-
registeredEvent ->
325-
contextWithUpdatedSession
326-
.pluginManager()
327-
.runOnEventCallback(
328-
contextWithUpdatedSession,
329-
registeredEvent)
330-
.defaultIfEmpty(
331-
registeredEvent))
324+
registeredEvent -> {
325+
// TODO: remove this hack after
326+
// deprecating runAsync with
327+
// Session.
328+
copySessionStates(
329+
updatedSession, session);
330+
return contextWithUpdatedSession
331+
.pluginManager()
332+
.runOnEventCallback(
333+
contextWithUpdatedSession,
334+
registeredEvent)
335+
.defaultIfEmpty(
336+
registeredEvent);
337+
})
332338
.toFlowable());
333339

334340
// If beforeRunCallback returns content, emit it and skip
@@ -359,6 +365,13 @@ public Flowable<Event> runAsync(
359365
}
360366
}
361367

368+
private void copySessionStates(Session source, Session target) {
369+
// TODO: remove this hack when deprecating all runAsync with Session.
370+
for (var entry : source.state().entrySet()) {
371+
target.state().put(entry.getKey(), entry.getValue());
372+
}
373+
}
374+
362375
/**
363376
* Creates an {@link InvocationContext} for a live (streaming) run.
364377
*

0 commit comments

Comments
 (0)