Skip to content

Commit 061d030

Browse files
committed
chore: merge latest main to resolve conflicts
2 parents 18b6f6d + 6a5eac0 commit 061d030

File tree

4 files changed

+63
-15
lines changed

4 files changed

+63
-15
lines changed

contributing/samples/agent_engine_code_execution/README

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ This sample data science agent uses Agent Engine Code Execution Sandbox to execu
99

1010
* 1. Follow https://cloud.google.com/vertex-ai/generative-ai/docs/agent-engine/code-execution/overview to create a code execution sandbox environment.
1111

12-
* 2. Replace the SANDBOX_RESOURCE_NAME with the one you just created. If you dont want to create a new sandbox environment directly, the Agent Engine Code Execution Sandbox will create one for you by default using the AGENT_ENGINE_RESOURCE_NAME you specified, however, please ensure to clean up sandboxes after use, otherwise, it will consume quotas.
12+
* 2. Replace the SANDBOX_RESOURCE_NAME with the one you just created. If you dont want to create a new sandbox environment directly, the Agent Engine Code Execution Sandbox will create one for you by default using the AGENT_ENGINE_RESOURCE_NAME you specified, however, please ensure to clean up sandboxes after use, otherwise, it will consume quotas.
1313

1414

1515
## Sample prompt

src/google/adk/cli/adk_web_server.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,12 @@ async def get_runner_async(self, app_name: str) -> Runner:
486486
self.runner_dict[app_name] = runner
487487
return runner
488488

489+
def _get_root_agent(self, agent_or_app: BaseAgent | App) -> BaseAgent:
490+
"""Extract root agent from either a BaseAgent or App object."""
491+
if isinstance(agent_or_app, App):
492+
return agent_or_app.root_agent
493+
return agent_or_app
494+
489495
def _create_runner(self, agentic_app: App) -> Runner:
490496
"""Create a runner with common services."""
491497
return Runner(
@@ -741,7 +747,7 @@ async def create_session_with_id(
741747
is not None
742748
):
743749
raise HTTPException(
744-
status_code=400, detail=f"Session already exists: {session_id}"
750+
status_code=409, detail=f"Session already exists: {session_id}"
745751
)
746752
session = await self.session_service.create_session(
747753
app_name=app_name, user_id=user_id, state=state, session_id=session_id
@@ -763,6 +769,13 @@ async def create_session(
763769
app_name=app_name, user_id=user_id
764770
)
765771

772+
if req.session_id and await self.session_service.get_session(
773+
app_name=app_name, user_id=user_id, session_id=req.session_id
774+
):
775+
raise HTTPException(
776+
status_code=409, detail=f"Session already exists: {req.session_id}"
777+
)
778+
766779
session = await self.session_service.create_session(
767780
app_name=app_name,
768781
user_id=user_id,
@@ -926,9 +939,8 @@ async def add_session_to_eval_set(
926939

927940
# Populate the session with initial session state.
928941
agent_or_app = self.agent_loader.load_agent(app_name)
929-
if isinstance(agent_or_app, App):
930-
agent_or_app = agent_or_app.root_agent
931-
initial_session_state = create_empty_state(agent_or_app)
942+
root_agent = self._get_root_agent(agent_or_app)
943+
initial_session_state = create_empty_state(root_agent)
932944

933945
new_eval_case = EvalCase(
934946
eval_id=req.eval_id,
@@ -1089,7 +1101,8 @@ async def run_eval(
10891101
status_code=400, detail=f"Eval set `{eval_set_id}` not found."
10901102
)
10911103

1092-
root_agent = self.agent_loader.load_agent(app_name)
1104+
agent_or_app = self.agent_loader.load_agent(app_name)
1105+
root_agent = self._get_root_agent(agent_or_app)
10931106

10941107
eval_case_results = []
10951108

@@ -1430,13 +1443,7 @@ async def get_event_graph(
14301443
function_calls = event.get_function_calls()
14311444
function_responses = event.get_function_responses()
14321445
agent_or_app = self.agent_loader.load_agent(app_name)
1433-
# The loader may return an App; unwrap to its root agent so the graph builder
1434-
# receives a BaseAgent instance.
1435-
root_agent = (
1436-
agent_or_app.root_agent
1437-
if isinstance(agent_or_app, App)
1438-
else agent_or_app
1439-
)
1446+
root_agent = self._get_root_agent(agent_or_app)
14401447
dot_graph = None
14411448
if function_calls:
14421449
function_call_highlights = []

src/google/adk/sessions/vertex_ai_session_service.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,23 @@ async def create_session(
7373
user_id: str,
7474
state: Optional[dict[str, Any]] = None,
7575
session_id: Optional[str] = None,
76+
**kwargs: Any,
7677
) -> Session:
78+
"""Creates a new session.
79+
80+
Args:
81+
app_name: The name of the application.
82+
user_id: The ID of the user.
83+
state: The initial state of the session.
84+
session_id: The ID of the session.
85+
**kwargs: Additional arguments to pass to the session creation. E.g. set
86+
expire_time='2025-10-01T00:00:00Z' to set the session expiration time.
87+
See https://cloud.google.com/vertex-ai/generative-ai/docs/reference/rest/v1beta1/projects.locations.reasoningEngines.sessions
88+
for more details.
89+
Returns:
90+
The created session.
91+
"""
92+
7793
if session_id:
7894
raise ValueError(
7995
'User-provided Session id is not supported for'
@@ -84,6 +100,7 @@ async def create_session(
84100
api_client = self._get_api_client()
85101

86102
config = {'session_state': state} if state else {}
103+
config.update(kwargs)
87104

88105
if _is_vertex_express_mode(self._project, self._location):
89106
config['wait_for_completion'] = False

tests/unittests/sessions/test_vertex_ai_session_service.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ def __init__(self) -> None:
242242
self.agent_engines.sessions.create.side_effect = self._create_session
243243
self.agent_engines.sessions.events.list.side_effect = self._list_events
244244
self.agent_engines.sessions.events.append.side_effect = self._append_event
245+
self.last_create_session_config: dict[str, Any] = {}
245246

246247
def _get_session(self, name: str):
247248
session_id = name.split('/')[-1]
@@ -275,6 +276,7 @@ def _delete_session(self, name: str):
275276
self.session_dict.pop(session_id)
276277

277278
def _create_session(self, name: str, user_id: str, config: dict[str, Any]):
279+
self.last_create_session_config = config
278280
new_session_id = '4'
279281
self.session_dict[new_session_id] = {
280282
'name': (
@@ -360,7 +362,8 @@ def mock_vertex_ai_session_service(agent_engine_id: Optional[str] = None):
360362

361363

362364
@pytest.fixture
363-
def mock_get_api_client():
365+
def mock_api_client_instance():
366+
"""Creates a mock API client instance for testing."""
364367
api_client = MockApiClient()
365368
api_client.session_dict = {
366369
'1': MOCK_SESSION_JSON_1,
@@ -373,9 +376,15 @@ def mock_get_api_client():
373376
'1': (copy.deepcopy(MOCK_EVENT_JSON), None),
374377
'2': (copy.deepcopy(MOCK_EVENT_JSON_2), 'my_token'),
375378
}
379+
return api_client
380+
381+
382+
@pytest.fixture
383+
def mock_get_api_client(mock_api_client_instance):
384+
"""Mocks the _get_api_client method to return a mock API client."""
376385
with mock.patch(
377386
'google.adk.sessions.vertex_ai_session_service.VertexAiSessionService._get_api_client',
378-
return_value=api_client,
387+
return_value=mock_api_client_instance,
379388
):
380389
yield
381390

@@ -521,6 +530,21 @@ async def test_create_session_with_custom_session_id():
521530
)
522531

523532

533+
@pytest.mark.asyncio
534+
@pytest.mark.usefixtures('mock_get_api_client')
535+
async def test_create_session_with_custom_config(mock_api_client_instance):
536+
session_service = mock_vertex_ai_session_service()
537+
538+
expire_time = '2025-12-12T12:12:12.123456Z'
539+
await session_service.create_session(
540+
app_name='123', user_id='user', expire_time=expire_time
541+
)
542+
assert (
543+
mock_api_client_instance.last_create_session_config['expire_time']
544+
== expire_time
545+
)
546+
547+
524548
@pytest.mark.asyncio
525549
@pytest.mark.usefixtures('mock_get_api_client')
526550
async def test_append_event():

0 commit comments

Comments
 (0)