1
+ # Copyright 2025 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
1
15
import logging
2
16
from typing import AsyncGenerator
3
17
from typing_extensions import override
@@ -219,7 +233,7 @@ async def _run_async_impl(
219
233
# --- Setup Runner and Session ---
220
234
session_service = InMemorySessionService ()
221
235
initial_state = {"topic" : "a brave kitten exploring a haunted house" }
222
- session = session_service .create_session (
236
+ session = await session_service .create_session (
223
237
app_name = APP_NAME ,
224
238
user_id = USER_ID ,
225
239
session_id = SESSION_ID ,
@@ -234,12 +248,12 @@ async def _run_async_impl(
234
248
)
235
249
236
250
# --- Function to Interact with the Agent ---
237
- def call_agent (user_input_topic : str ):
251
+ async def call_agent (user_input_topic : str ):
238
252
"""
239
253
Sends a new topic to the agent (overwriting the initial one if needed)
240
254
and runs the workflow.
241
255
"""
242
- current_session = session_service .get_session (app_name = APP_NAME ,
256
+ current_session = await session_service .get_session (app_name = APP_NAME ,
243
257
user_id = USER_ID ,
244
258
session_id = SESSION_ID )
245
259
if not current_session :
@@ -261,7 +275,7 @@ def call_agent(user_input_topic: str):
261
275
print ("\n --- Agent Interaction Result ---" )
262
276
print ("Agent Final Response: " , final_response )
263
277
264
- final_session = session_service .get_session (app_name = APP_NAME ,
278
+ final_session = await session_service .get_session (app_name = APP_NAME ,
265
279
user_id = USER_ID ,
266
280
session_id = SESSION_ID )
267
281
print ("Final Session State:" )
@@ -270,5 +284,5 @@ def call_agent(user_input_topic: str):
270
284
print ("-------------------------------\n " )
271
285
272
286
# --- Run the Agent ---
273
- call_agent ("a lonely robot finding a friend in a junkyard" )
287
+ await call_agent ("a lonely robot finding a friend in a junkyard" )
274
288
# --8<-- [end:story_flow_agent]
0 commit comments