Skip to content

Commit 965d8ce

Browse files
Tongzhou-Jiangcopybara-github
authored andcommitted
fix: Update authorization for streaming_agent_run_with_events
PiperOrigin-RevId: 924908903
1 parent 89f965c commit 965d8ce

4 files changed

Lines changed: 87 additions & 0 deletions

File tree

agentplatform/agent_engines/templates/adk.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,7 @@ async def streaming_agent_run_with_events(self, request_json: str):
13661366
self.set_up()
13671367

13681368
# Try to get the session, if it doesn't exist, create a new one.
1369+
state_delta = None
13691370
if request.session_id:
13701371
session_service = self._tmpl_attrs.get("session_service")
13711372
artifact_service = self._tmpl_attrs.get("artifact_service")
@@ -1383,6 +1384,11 @@ async def streaming_agent_run_with_events(self, request_json: str):
13831384
artifact_service=artifact_service,
13841385
request=request,
13851386
)
1387+
if request.authorizations:
1388+
state_delta = {}
1389+
for auth_id, auth in request.authorizations.items():
1390+
auth = _Authorization(**auth)
1391+
state_delta[auth_id] = auth.access_token
13861392
except ClientError:
13871393
pass
13881394
if not session:
@@ -1414,6 +1420,7 @@ async def streaming_agent_run_with_events(self, request_json: str):
14141420
user_id=request.user_id,
14151421
session_id=session.id,
14161422
new_message=message_for_agent,
1423+
state_delta=state_delta,
14171424
):
14181425
converted_event = await self._convert_response_events(
14191426
user_id=request.user_id,

tests/unit/vertex_adk/test_agent_engine_templates_adk.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,72 @@ async def test_streaming_agent_run_with_events(
507507
events.append(event)
508508
assert len(events) == 1
509509

510+
@pytest.mark.asyncio
511+
async def test_streaming_agent_run_with_events_existing_session(
512+
self,
513+
default_instrumentor_builder_mock: mock.Mock,
514+
get_project_id_mock: mock.Mock,
515+
):
516+
app = agent_engines.AdkApp(agent=_TEST_AGENT)
517+
app.set_up()
518+
519+
# Pre-create a session in the real in-memory session service
520+
await app.async_create_session(
521+
user_id=_TEST_USER_ID, session_id="test_session_id"
522+
)
523+
524+
# Mock the main runner
525+
runner_mock = mock.Mock()
526+
527+
# Define an async generator for run_async mock return value
528+
async def mock_run_async(*args, **kwargs):
529+
from google.adk.events import event
530+
yield event.Event(
531+
**{
532+
"author": "currency_exchange_agent",
533+
"content": {
534+
"parts": [{"text": "Sweden"}],
535+
"role": "model",
536+
},
537+
"id": "9aaItGK9",
538+
"invocation_id": "e-6543c213-6417-484b-9551-b67915d1d5f7",
539+
}
540+
)
541+
542+
spy = mock.MagicMock(side_effect=mock_run_async)
543+
runner_mock.run_async = spy
544+
app._tmpl_attrs["runner"] = runner_mock
545+
546+
request_json = json.dumps(
547+
{
548+
"authorizations": {
549+
"test_user_id1": {"access_token": "test_access_token"},
550+
},
551+
"user_id": _TEST_USER_ID,
552+
"session_id": "test_session_id",
553+
"message": {
554+
"parts": [{"text": "What is the exchange rate from USD to SEK?"}],
555+
"role": "user",
556+
},
557+
}
558+
)
559+
560+
events = []
561+
async for event in app.streaming_agent_run_with_events(
562+
request_json=request_json,
563+
):
564+
events.append(event)
565+
566+
assert len(events) == 1
567+
568+
# Assert that run_async was called with the expected state_delta!
569+
spy.assert_called_once_with(
570+
user_id=_TEST_USER_ID,
571+
session_id="test_session_id",
572+
new_message=mock.ANY,
573+
state_delta={"test_user_id1": "test_access_token"},
574+
)
575+
510576
@pytest.mark.asyncio
511577
@mock.patch.dict(
512578
os.environ,

vertexai/agent_engines/templates/adk.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,7 @@ async def streaming_agent_run_with_events(self, request_json: str):
13321332
self.set_up()
13331333

13341334
# Try to get the session, if it doesn't exist, create a new one.
1335+
state_delta = None
13351336
if request.session_id:
13361337
session_service = self._tmpl_attrs.get("session_service")
13371338
artifact_service = self._tmpl_attrs.get("artifact_service")
@@ -1349,6 +1350,11 @@ async def streaming_agent_run_with_events(self, request_json: str):
13491350
artifact_service=artifact_service,
13501351
request=request,
13511352
)
1353+
if request.authorizations:
1354+
state_delta = {}
1355+
for auth_id, auth in request.authorizations.items():
1356+
auth = _Authorization(**auth)
1357+
state_delta[auth_id] = auth.access_token
13521358
except ClientError:
13531359
pass
13541360
if not session:
@@ -1380,6 +1386,7 @@ async def streaming_agent_run_with_events(self, request_json: str):
13801386
user_id=request.user_id,
13811387
session_id=session.id,
13821388
new_message=message_for_agent,
1389+
state_delta=state_delta,
13831390
):
13841391
converted_event = await self._convert_response_events(
13851392
user_id=request.user_id,

vertexai/preview/reasoning_engines/templates/adk.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,7 @@ async def _invoke_agent_async():
10601060
):
10611061
self.set_up()
10621062
# Try to get the session, if it doesn't exist, create a new one.
1063+
state_delta = None
10631064
if request.session_id:
10641065
session_service = self._tmpl_attrs.get("session_service")
10651066
artifact_service = self._tmpl_attrs.get("artifact_service")
@@ -1077,6 +1078,11 @@ async def _invoke_agent_async():
10771078
artifact_service=artifact_service,
10781079
request=request,
10791080
)
1081+
if request.authorizations:
1082+
state_delta = {}
1083+
for auth_id, auth in request.authorizations.items():
1084+
auth = _Authorization(**auth)
1085+
state_delta[auth_id] = auth.access_token
10801086
except ClientError:
10811087
pass
10821088
if not session:
@@ -1107,6 +1113,7 @@ async def _invoke_agent_async():
11071113
user_id=request.user_id,
11081114
session_id=session.id,
11091115
new_message=message_for_agent,
1116+
state_delta=state_delta,
11101117
):
11111118
converted_event = await self._convert_response_events(
11121119
user_id=request.user_id,

0 commit comments

Comments
 (0)