11# Common imports for all agent folders
22# Day 1 imports
33from google .adk .agents .llm_agent import Agent
4- from google .adk .agents import Agent , SequentialAgent , ParallelAgent , LoopAgent
4+ from google .adk .agents import SequentialAgent , ParallelAgent , LoopAgent
55from google .adk .runners import Runner , InMemoryRunner
66from google .adk .tools import AgentTool , FunctionTool , google_search
77from google .genai import types
8-
98# Day 2 imports, incremental
109# 2a
1110from google .adk .agents import LlmAgent
1211from google .adk .models .google_llm import Gemini
13- from google .adk .sessions import InMemorySessionService
12+ from google .adk .sessions import InMemorySessionService , DatabaseSessionService
13+ from google .adk .memory import InMemoryMemoryService
1414from google .adk .tools import ToolContext
15+ from google .adk .tools import load_memory , preload_memory
1516from google .adk .code_executors import BuiltInCodeExecutor
1617# 2b
1718import uuid
2122from google .adk .apps .app import App , ResumabilityConfig
2223from google .adk .tools .function_tool import FunctionTool
2324from mcp import StdioServerParameters
25+ # Day 3 imports, incremental
26+ from typing import Any , Dict
27+ from google .adk .apps .app import EventsCompactionConfig
28+ from google .adk .sessions import DatabaseSessionService
2429
2530print ("✅ ADK components imported successfully." )
2631
32+ APP_NAME = "default" # Application
33+ USER_ID = "default" # User
34+ SESSION = "default" # Session
35+ MODEL_NAME = "gemini-2.5-flash-lite"
36+
37+ # Day 2 - Helper functions
2738def show_python_code_and_result (response ):
2839 for i in range (len (response )):
2940 # Check if the response contains a valid function call result from the code executor
@@ -43,18 +54,14 @@ def show_python_code_and_result(response):
4354 else :
4455 print ("Generated Python Response >> " , response_code ["result" ])
4556
46- print ("✅ Helper functions defined." )
47-
4857retry_config = types .HttpRetryOptions (
58+ # Retry configuration
4959 attempts = 5 , # Maximum retry attempts
5060 exp_base = 7 , # Delay multiplier
5161 initial_delay = 1 ,
5262 http_status_codes = [429 , 500 , 503 , 504 ], # Retry on these HTTP errors
5363)
5464
55- print ("✅ retry_config created." )
56-
57-
5865def check_for_approval (events ):
5966 """Check if events contain an approval request.
6067
@@ -74,7 +81,6 @@ def check_for_approval(events):
7481 }
7582 return None
7683
77-
7884def print_agent_response (events ):
7985 """Print agent's text responses from events."""
8086 for event in events :
@@ -83,7 +89,6 @@ def print_agent_response(events):
8389 if part .text :
8490 print (f"Agent > { part .text } " )
8591
86-
8792def create_approval_response (approval_info , approved ):
8893 """Create approval response message."""
8994 confirmation_response = types .FunctionResponse (
@@ -95,5 +100,66 @@ def create_approval_response(approval_info, approved):
95100 role = "user" , parts = [types .Part (function_response = confirmation_response )]
96101 )
97102
103+ # Day 3 - Helper functions
104+ async def run_session (
105+ runner_instance : Runner ,
106+ session_service : InMemorySessionService ,
107+ user_id : str = USER_ID ,
108+ model_name : str = MODEL_NAME ,
109+ user_queries : list [str ] | str = None ,
110+ session_name : str = SESSION ):
111+ """Run a session asynchronously.
112+
113+ Args:
114+ runner_instance: The runner instance
115+ user_queries: The user queries
116+ session_name: The session name
117+ """
118+ print (f"\n ### Session: { session_name } " )
119+
120+ # Get app name from the Runner
121+ app_name = runner_instance .app_name
122+
123+ # Attempt to create a new session or retrieve an existing one
124+ try :
125+ session = await session_service .create_session (
126+ app_name = app_name ,
127+ user_id = user_id ,
128+ session_id = session_name
129+ )
130+ except :
131+ session = await session_service .get_session (
132+ app_name = app_name ,
133+ user_id = user_id ,
134+ session_id = session_name
135+ )
136+
137+ # Process queries if provided
138+ if user_queries :
139+ # Convert single query to list for uniform processing
140+ if type (user_queries ) == str :
141+ user_queries = [user_queries ]
142+
143+ # Process each query in the list sequentially
144+ for query in user_queries :
145+ print (f"\n User > { query } " )
146+
147+ # Convert the query string to the ADK Content format
148+ query = types .Content (role = "user" , parts = [types .Part (text = query )])
149+
150+ # Stream the agent's response asynchronously
151+ async for event in runner_instance .run_async (
152+ user_id = user_id , session_id = session .id , new_message = query
153+ ):
154+ # Check if the event contains valid content
155+ if event .content and event .content .parts :
156+ # Filter out empty or "None" responses before printing
157+ if (
158+ event .content .parts [0 ].text != "None"
159+ and event .content .parts [0 ].text
160+ ):
161+ print (f"{ model_name } > " , event .content .parts [0 ].text )
162+ else :
163+ print ("No queries!" )
98164
99- print ("✅ Helper functions defined" )
165+ print ("✅ Helper functions defined. " )
0 commit comments