From 4c5a5e6e23d828e36207f35da015985b88ebe549 Mon Sep 17 00:00:00 2001 From: ammmr Date: Mon, 8 Sep 2025 18:10:47 +0900 Subject: [PATCH] feat: Add user_id property to ReadonlyContext --- src/google/adk/agents/readonly_context.py | 5 +++++ tests/unittests/agents/test_readonly_context.py | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/src/google/adk/agents/readonly_context.py b/src/google/adk/agents/readonly_context.py index 548425367d..98c159ef4c 100644 --- a/src/google/adk/agents/readonly_context.py +++ b/src/google/adk/agents/readonly_context.py @@ -52,3 +52,8 @@ def agent_name(self) -> str: def state(self) -> MappingProxyType[str, Any]: """The state of the current session. READONLY field.""" return MappingProxyType(self._invocation_context.session.state) + + @property + def user_id(self) -> str: + """The id of the user. READONLY field.""" + return self._invocation_context.user_id diff --git a/tests/unittests/agents/test_readonly_context.py b/tests/unittests/agents/test_readonly_context.py index fbea0b0aff..67a749d56e 100644 --- a/tests/unittests/agents/test_readonly_context.py +++ b/tests/unittests/agents/test_readonly_context.py @@ -11,6 +11,7 @@ def mock_invocation_context(): mock_context.invocation_id = "test-invocation-id" mock_context.agent.name = "test-agent-name" mock_context.session.state = {"key1": "value1", "key2": "value2"} + mock_context.user_id = "test-user-id" return mock_context @@ -31,3 +32,8 @@ def test_state_content(mock_invocation_context): assert isinstance(state, MappingProxyType) assert state["key1"] == "value1" assert state["key2"] == "value2" + + +def test_user_id(mock_invocation_context): + readonly_context = ReadonlyContext(mock_invocation_context) + assert readonly_context.user_id == "test-user-id"