Skip to content

Commit 08d099d

Browse files
committed
address Bo comments, move runtime to platform
1 parent 57ddf8f commit 08d099d

6 files changed

Lines changed: 107 additions & 49 deletions

File tree

src/google/adk/events/event.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
from typing import Optional
1919
import uuid
2020

21-
from google.adk import runtime
21+
from google.adk.platform import time as platform_time
22+
from google.adk.platform import uuid as platform_uuid
2223
from google.genai import types
2324
from pydantic import alias_generators
2425
from pydantic import ConfigDict
@@ -71,7 +72,7 @@ class Event(LlmResponse):
7172
# Do not assign the ID. It will be assigned by the session.
7273
id: str = ''
7374
"""The unique identifier of the event."""
74-
timestamp: float = Field(default_factory=lambda: runtime.get_time())
75+
timestamp: float = Field(default_factory=lambda: platform_time.get_time())
7576
"""The timestamp of the event."""
7677

7778
def model_post_init(self, __context):
@@ -126,4 +127,4 @@ def has_trailing_code_execution_result(
126127

127128
@staticmethod
128129
def new_id():
129-
return runtime.new_uuid()
130+
return platform_uuid.new_uuid()
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2025 Google LLC
1+
# Copyright 2026 Google LLC
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -12,14 +12,13 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
"""Runtime module for abstracting system primitives like time and UUIDs."""
15+
"""Platform module for abstracting system time generation."""
1616

1717
import time
18-
import uuid
1918
from typing import Callable
2019

21-
_time_provider: Callable[[], float] = time.time
22-
_id_provider: Callable[[], str] = lambda: str(uuid.uuid4())
20+
_default_time_provider: Callable[[], float] = time.time
21+
_time_provider: Callable[[], float] = _default_time_provider
2322

2423

2524
def set_time_provider(provider: Callable[[], float]) -> None:
@@ -33,21 +32,12 @@ def set_time_provider(provider: Callable[[], float]) -> None:
3332
_time_provider = provider
3433

3534

36-
def set_id_provider(provider: Callable[[], str]) -> None:
37-
"""Sets the provider for generating unique IDs.
38-
39-
Args:
40-
provider: A callable that returns a unique ID string.
41-
"""
42-
global _id_provider
43-
_id_provider = provider
35+
def reset_time_provider() -> None:
36+
"""Resets the time provider to its default implementation."""
37+
global _time_provider
38+
_time_provider = _default_time_provider
4439

4540

4641
def get_time() -> float:
4742
"""Returns the current time in seconds since the epoch."""
4843
return _time_provider()
49-
50-
51-
def new_uuid() -> str:
52-
"""Returns a new unique ID."""
53-
return _id_provider()

src/google/adk/platform/uuid.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Platform module for abstracting unique ID generation."""
16+
17+
import uuid
18+
from typing import Callable
19+
20+
_default_id_provider: Callable[[], str] = lambda: str(uuid.uuid4())
21+
_id_provider: Callable[[], str] = _default_id_provider
22+
23+
24+
def set_id_provider(provider: Callable[[], str]) -> None:
25+
"""Sets the provider for generating unique IDs.
26+
27+
Args:
28+
provider: A callable that returns a unique ID string.
29+
"""
30+
global _id_provider
31+
_id_provider = provider
32+
33+
34+
def reset_id_provider() -> None:
35+
"""Resets the ID provider to its default implementation."""
36+
global _id_provider
37+
_id_provider = _default_id_provider
38+
39+
40+
def new_uuid() -> str:
41+
"""Returns a new unique ID."""
42+
return _id_provider()

src/google/adk/sessions/in_memory_session_service.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222

2323
from typing_extensions import override
2424

25-
from google.adk import runtime
25+
from google.adk.platform import time as platform_time
26+
from google.adk.platform import uuid as platform_uuid
2627
from . import _session_util
2728
from ..errors.already_exists_error import AlreadyExistsError
2829
from ..events.event import Event
@@ -109,14 +110,14 @@ def _create_session_impl(
109110
session_id = (
110111
session_id.strip()
111112
if session_id and session_id.strip()
112-
else runtime.new_uuid()
113+
else platform_uuid.new_uuid()
113114
)
114115
session = Session(
115116
app_name=app_name,
116117
user_id=user_id,
117118
id=session_id,
118119
state=session_state or {},
119-
last_update_time=runtime.get_time(),
120+
last_update_time=platform_time.get_time(),
120121
)
121122

122123
if app_name not in self.sessions:
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2025 Google LLC
1+
# Copyright 2026 Google LLC
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -12,45 +12,29 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
"""Unit tests for the runtime module."""
15+
"""Unit tests for the platform time module."""
1616

1717
import time
18-
import uuid
1918
import unittest
20-
from unittest.mock import MagicMock, patch
2119

22-
from google.adk import runtime
20+
from google.adk.platform import time as platform_time
2321

2422

25-
class TestRuntime(unittest.TestCase):
23+
class TestTime(unittest.TestCase):
2624

2725
def tearDown(self):
28-
# Reset providers to default after each test
29-
runtime.set_time_provider(time.time)
30-
runtime.set_id_provider(lambda: str(uuid.uuid4()))
26+
# Reset provider to default after each test
27+
platform_time.reset_time_provider()
3128

3229
def test_default_time_provider(self):
3330
# Verify it returns a float that is close to now
3431
now = time.time()
35-
rt_time = runtime.get_time()
32+
rt_time = platform_time.get_time()
3633
self.assertIsInstance(rt_time, float)
3734
self.assertAlmostEqual(rt_time, now, delta=1.0)
3835

39-
def test_default_id_provider(self):
40-
# Verify it returns a string uuid
41-
uid = runtime.new_uuid()
42-
self.assertIsInstance(uid, str)
43-
# Should be parseable as uuid
44-
uuid.UUID(uid)
45-
4636
def test_custom_time_provider(self):
4737
# Test override
4838
mock_time = 123456789.0
49-
runtime.set_time_provider(lambda: mock_time)
50-
self.assertEqual(runtime.get_time(), mock_time)
51-
52-
def test_custom_id_provider(self):
53-
# Test override
54-
mock_id = "test-id-123"
55-
runtime.set_id_provider(lambda: mock_id)
56-
self.assertEqual(runtime.new_uuid(), mock_id)
39+
platform_time.set_time_provider(lambda: mock_time)
40+
self.assertEqual(platform_time.get_time(), mock_time)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Unit tests for the platform uuid module."""
16+
17+
import uuid
18+
import unittest
19+
20+
from google.adk.platform import uuid as platform_uuid
21+
22+
23+
class TestUUID(unittest.TestCase):
24+
25+
def tearDown(self):
26+
# Reset provider to default after each test
27+
platform_uuid.reset_id_provider()
28+
29+
def test_default_id_provider(self):
30+
# Verify it returns a string uuid
31+
uid = platform_uuid.new_uuid()
32+
self.assertIsInstance(uid, str)
33+
# Should be parseable as uuid
34+
uuid.UUID(uid)
35+
36+
def test_custom_id_provider(self):
37+
# Test override
38+
mock_id = "test-id-123"
39+
platform_uuid.set_id_provider(lambda: mock_id)
40+
self.assertEqual(platform_uuid.new_uuid(), mock_id)

0 commit comments

Comments
 (0)