Skip to content

Commit cc96582

Browse files
committed
refactor(a2a): remove unused context id helpers
1 parent fd006db commit cc96582

2 files changed

Lines changed: 0 additions & 214 deletions

File tree

src/google/adk/a2a/converters/utils.py

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
from __future__ import annotations
1616

1717
ADK_METADATA_KEY_PREFIX = "adk_"
18-
ADK_CONTEXT_ID_PREFIX = "ADK"
19-
ADK_CONTEXT_ID_SEPARATOR = "/"
2018

2119

2220
def _get_adk_metadata_key(key: str) -> str:
@@ -34,58 +32,3 @@ def _get_adk_metadata_key(key: str) -> str:
3432
if not key:
3533
raise ValueError("Metadata key cannot be empty or None")
3634
return f"{ADK_METADATA_KEY_PREFIX}{key}"
37-
38-
39-
def _to_a2a_context_id(app_name: str, user_id: str, session_id: str) -> str:
40-
"""Converts app name, user id and session id to an A2A context id.
41-
42-
Args:
43-
app_name: The app name.
44-
user_id: The user id.
45-
session_id: The session id.
46-
47-
Returns:
48-
The A2A context id.
49-
50-
Raises:
51-
ValueError: If any of the input parameters are empty or None.
52-
"""
53-
if not all([app_name, user_id, session_id]):
54-
raise ValueError(
55-
"All parameters (app_name, user_id, session_id) must be non-empty"
56-
)
57-
return ADK_CONTEXT_ID_SEPARATOR.join(
58-
[ADK_CONTEXT_ID_PREFIX, app_name, user_id, session_id]
59-
)
60-
61-
62-
def _from_a2a_context_id(
63-
context_id: str | None,
64-
) -> tuple[str, str, str] | tuple[None, None, None]:
65-
"""Converts an A2A context id to app name, user id and session id.
66-
if context_id is None, return None, None, None
67-
if context_id is not None, but not in the format of
68-
ADK$app_name$user_id$session_id, return None, None, None
69-
70-
Args:
71-
context_id: The A2A context id.
72-
73-
Returns:
74-
The app name, user id and session id, or (None, None, None) if invalid.
75-
"""
76-
if not context_id:
77-
return None, None, None
78-
79-
try:
80-
parts = context_id.split(ADK_CONTEXT_ID_SEPARATOR)
81-
if len(parts) != 4:
82-
return None, None, None
83-
84-
prefix, app_name, user_id, session_id = parts
85-
if prefix == ADK_CONTEXT_ID_PREFIX and app_name and user_id and session_id:
86-
return app_name, user_id, session_id
87-
except ValueError:
88-
# Handle any split errors gracefully
89-
pass
90-
91-
return None, None, None

tests/unittests/a2a/converters/test_utils.py

Lines changed: 0 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from google.adk.a2a.converters.utils import _from_a2a_context_id
1615
from google.adk.a2a.converters.utils import _get_adk_metadata_key
17-
from google.adk.a2a.converters.utils import _to_a2a_context_id
18-
from google.adk.a2a.converters.utils import ADK_CONTEXT_ID_PREFIX
1916
from google.adk.a2a.converters.utils import ADK_METADATA_KEY_PREFIX
2017
import pytest
2118

@@ -48,157 +45,3 @@ def test_get_adk_metadata_key_whitespace(self):
4845
key = " "
4946
result = _get_adk_metadata_key(key)
5047
assert result == f"{ADK_METADATA_KEY_PREFIX}{key}"
51-
52-
def test_to_a2a_context_id_success(self):
53-
"""Test successful context ID generation."""
54-
app_name = "test-app"
55-
user_id = "test-user"
56-
session_id = "test-session"
57-
58-
result = _to_a2a_context_id(app_name, user_id, session_id)
59-
60-
expected = f"{ADK_CONTEXT_ID_PREFIX}/test-app/test-user/test-session"
61-
assert result == expected
62-
63-
def test_to_a2a_context_id_empty_app_name(self):
64-
"""Test context ID generation with empty app name."""
65-
with pytest.raises(
66-
ValueError,
67-
match=(
68-
"All parameters \\(app_name, user_id, session_id\\) must be"
69-
" non-empty"
70-
),
71-
):
72-
_to_a2a_context_id("", "user", "session")
73-
74-
def test_to_a2a_context_id_empty_user_id(self):
75-
"""Test context ID generation with empty user ID."""
76-
with pytest.raises(
77-
ValueError,
78-
match=(
79-
"All parameters \\(app_name, user_id, session_id\\) must be"
80-
" non-empty"
81-
),
82-
):
83-
_to_a2a_context_id("app", "", "session")
84-
85-
def test_to_a2a_context_id_empty_session_id(self):
86-
"""Test context ID generation with empty session ID."""
87-
with pytest.raises(
88-
ValueError,
89-
match=(
90-
"All parameters \\(app_name, user_id, session_id\\) must be"
91-
" non-empty"
92-
),
93-
):
94-
_to_a2a_context_id("app", "user", "")
95-
96-
def test_to_a2a_context_id_none_values(self):
97-
"""Test context ID generation with None values."""
98-
with pytest.raises(
99-
ValueError,
100-
match=(
101-
"All parameters \\(app_name, user_id, session_id\\) must be"
102-
" non-empty"
103-
),
104-
):
105-
_to_a2a_context_id(None, "user", "session")
106-
107-
def test_to_a2a_context_id_special_characters(self):
108-
"""Test context ID generation with special characters."""
109-
app_name = "test-app@2024"
110-
user_id = "user_123"
111-
session_id = "session-456"
112-
113-
result = _to_a2a_context_id(app_name, user_id, session_id)
114-
115-
expected = f"{ADK_CONTEXT_ID_PREFIX}/test-app@2024/user_123/session-456"
116-
assert result == expected
117-
118-
def test_from_a2a_context_id_success(self):
119-
"""Test successful context ID parsing."""
120-
context_id = f"{ADK_CONTEXT_ID_PREFIX}/test-app/test-user/test-session"
121-
122-
app_name, user_id, session_id = _from_a2a_context_id(context_id)
123-
124-
assert app_name == "test-app"
125-
assert user_id == "test-user"
126-
assert session_id == "test-session"
127-
128-
def test_from_a2a_context_id_none_input(self):
129-
"""Test context ID parsing with None input."""
130-
result = _from_a2a_context_id(None)
131-
assert result == (None, None, None)
132-
133-
def test_from_a2a_context_id_empty_string(self):
134-
"""Test context ID parsing with empty string."""
135-
result = _from_a2a_context_id("")
136-
assert result == (None, None, None)
137-
138-
def test_from_a2a_context_id_invalid_prefix(self):
139-
"""Test context ID parsing with invalid prefix."""
140-
context_id = "INVALID/test-app/test-user/test-session"
141-
142-
result = _from_a2a_context_id(context_id)
143-
144-
assert result == (None, None, None)
145-
146-
def test_from_a2a_context_id_too_few_parts(self):
147-
"""Test context ID parsing with too few parts."""
148-
context_id = f"{ADK_CONTEXT_ID_PREFIX}/test-app/test-user"
149-
150-
result = _from_a2a_context_id(context_id)
151-
152-
assert result == (None, None, None)
153-
154-
def test_from_a2a_context_id_too_many_parts(self):
155-
"""Test context ID parsing with too many parts."""
156-
context_id = (
157-
f"{ADK_CONTEXT_ID_PREFIX}/test-app/test-user/test-session/extra"
158-
)
159-
160-
result = _from_a2a_context_id(context_id)
161-
162-
assert result == (None, None, None)
163-
164-
def test_from_a2a_context_id_empty_components(self):
165-
"""Test context ID parsing with empty components."""
166-
context_id = f"{ADK_CONTEXT_ID_PREFIX}//test-user/test-session"
167-
168-
result = _from_a2a_context_id(context_id)
169-
170-
assert result == (None, None, None)
171-
172-
def test_from_a2a_context_id_no_dollar_separator(self):
173-
"""Test context ID parsing without dollar separators."""
174-
context_id = f"{ADK_CONTEXT_ID_PREFIX}-test-app-test-user-test-session"
175-
176-
result = _from_a2a_context_id(context_id)
177-
178-
assert result == (None, None, None)
179-
180-
def test_roundtrip_context_id(self):
181-
"""Test roundtrip conversion: to -> from."""
182-
app_name = "test-app"
183-
user_id = "test-user"
184-
session_id = "test-session"
185-
186-
# Convert to context ID
187-
context_id = _to_a2a_context_id(app_name, user_id, session_id)
188-
189-
# Convert back
190-
parsed_app, parsed_user, parsed_session = _from_a2a_context_id(context_id)
191-
192-
assert parsed_app == app_name
193-
assert parsed_user == user_id
194-
assert parsed_session == session_id
195-
196-
def test_from_a2a_context_id_special_characters(self):
197-
"""Test context ID parsing with special characters."""
198-
context_id = f"{ADK_CONTEXT_ID_PREFIX}/test-app@2024/user_123/session-456"
199-
200-
app_name, user_id, session_id = _from_a2a_context_id(context_id)
201-
202-
assert app_name == "test-app@2024"
203-
assert user_id == "user_123"
204-
assert session_id == "session-456"

0 commit comments

Comments
 (0)