Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions agentplatform/agent_engines/templates/adk.py
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,7 @@ async def streaming_agent_run_with_events(self, request_json: str):
self.set_up()

# Try to get the session, if it doesn't exist, create a new one.
state_delta = None
if request.session_id:
session_service = self._tmpl_attrs.get("session_service")
artifact_service = self._tmpl_attrs.get("artifact_service")
Expand All @@ -1349,6 +1350,11 @@ async def streaming_agent_run_with_events(self, request_json: str):
artifact_service=artifact_service,
request=request,
)
if request.authorizations:
state_delta = {}
for auth_id, auth in request.authorizations.items():
auth = _Authorization(**auth)
state_delta[auth_id] = auth.access_token
except ClientError:
pass
if not session:
Expand Down Expand Up @@ -1380,6 +1386,7 @@ async def streaming_agent_run_with_events(self, request_json: str):
user_id=request.user_id,
session_id=session.id,
new_message=message_for_agent,
state_delta=state_delta,
):
converted_event = await self._convert_response_events(
user_id=request.user_id,
Expand Down
66 changes: 66 additions & 0 deletions tests/unit/vertex_adk/test_agent_engine_templates_adk.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,72 @@ async def test_streaming_agent_run_with_events(
events.append(event)
assert len(events) == 1

@pytest.mark.asyncio
async def test_streaming_agent_run_with_events_existing_session(
self,
default_instrumentor_builder_mock: mock.Mock,
get_project_id_mock: mock.Mock,
):
app = agent_engines.AdkApp(agent=_TEST_AGENT)
app.set_up()

# Pre-create a session in the real in-memory session service
await app.async_create_session(
user_id=_TEST_USER_ID, session_id="test_session_id"
)

# Mock the main runner
runner_mock = mock.Mock()

# Define an async generator for run_async mock return value
async def mock_run_async(*args, **kwargs):
from google.adk.events import event
yield event.Event(
**{
"author": "currency_exchange_agent",
"content": {
"parts": [{"text": "Sweden"}],
"role": "model",
},
"id": "9aaItGK9",
"invocation_id": "e-6543c213-6417-484b-9551-b67915d1d5f7",
}
)

spy = mock.MagicMock(side_effect=mock_run_async)
runner_mock.run_async = spy
app._tmpl_attrs["runner"] = runner_mock

request_json = json.dumps(
{
"authorizations": {
"test_user_id1": {"access_token": "test_access_token"},
},
"user_id": _TEST_USER_ID,
"session_id": "test_session_id",
"message": {
"parts": [{"text": "What is the exchange rate from USD to SEK?"}],
"role": "user",
},
}
)

events = []
async for event in app.streaming_agent_run_with_events(
request_json=request_json,
):
events.append(event)

assert len(events) == 1

# Assert that run_async was called with the expected state_delta!
spy.assert_called_once_with(
user_id=_TEST_USER_ID,
session_id="test_session_id",
new_message=mock.ANY,
state_delta={"test_user_id1": "test_access_token"},
)

@pytest.mark.asyncio
@mock.patch.dict(
os.environ,
Expand Down
7 changes: 7 additions & 0 deletions vertexai/agent_engines/templates/adk.py
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,7 @@ async def streaming_agent_run_with_events(self, request_json: str):
self.set_up()

# Try to get the session, if it doesn't exist, create a new one.
state_delta = None
if request.session_id:
session_service = self._tmpl_attrs.get("session_service")
artifact_service = self._tmpl_attrs.get("artifact_service")
Expand All @@ -1349,6 +1350,11 @@ async def streaming_agent_run_with_events(self, request_json: str):
artifact_service=artifact_service,
request=request,
)
if request.authorizations:
state_delta = {}
for auth_id, auth in request.authorizations.items():
auth = _Authorization(**auth)
state_delta[auth_id] = auth.access_token
except ClientError:
pass
if not session:
Expand Down Expand Up @@ -1380,6 +1386,7 @@ async def streaming_agent_run_with_events(self, request_json: str):
user_id=request.user_id,
session_id=session.id,
new_message=message_for_agent,
state_delta=state_delta,
):
converted_event = await self._convert_response_events(
user_id=request.user_id,
Expand Down
7 changes: 7 additions & 0 deletions vertexai/preview/reasoning_engines/templates/adk.py
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,7 @@ async def _invoke_agent_async():
):
self.set_up()
# Try to get the session, if it doesn't exist, create a new one.
state_delta = None
if request.session_id:
session_service = self._tmpl_attrs.get("session_service")
artifact_service = self._tmpl_attrs.get("artifact_service")
Expand All @@ -1077,6 +1078,11 @@ async def _invoke_agent_async():
artifact_service=artifact_service,
request=request,
)
if request.authorizations:
state_delta = {}
for auth_id, auth in request.authorizations.items():
auth = _Authorization(**auth)
state_delta[auth_id] = auth.access_token
except ClientError:
pass
if not session:
Expand Down Expand Up @@ -1107,6 +1113,7 @@ async def _invoke_agent_async():
user_id=request.user_id,
session_id=session.id,
new_message=message_for_agent,
state_delta=state_delta,
):
converted_event = await self._convert_response_events(
user_id=request.user_id,
Expand Down
Loading