12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
+ from __future__ import annotations
16
+
15
17
import asyncio
16
18
import json
17
19
import sys
18
20
import threading
19
21
import time
20
22
import types as ptypes
21
23
from typing import AsyncGenerator
24
+ from typing import TYPE_CHECKING
22
25
23
- from google .adk .agents import BaseAgent
24
- from google .adk .agents import LiveRequest
26
+ from google .adk .agents . base_agent import BaseAgent
27
+ from google .adk .agents . live_request_queue import LiveRequest
25
28
from google .adk .agents .run_config import RunConfig
26
29
from google .adk .cli .fast_api import AgentRunRequest
27
30
from google .adk .cli .fast_api import get_fast_api_app
28
31
from google .adk .cli .utils import envs
29
- from google .adk .events import Event
30
32
from google .adk .runners import Runner
31
33
from google .genai import types
32
34
import httpx
33
35
import pytest
34
36
from uvicorn .main import run as uvicorn_run
35
37
import websockets
36
38
39
+ if TYPE_CHECKING :
40
+ from google .adk .events import Event
41
+
37
42
38
43
# Here we “fake” the agent module that get_fast_api_app expects.
39
44
# The server code does: `agent_module = importlib.import_module(agent_name)`
@@ -49,33 +54,45 @@ class DummyAgent(BaseAgent):
49
54
sys .modules ["test_app" ] = dummy_module
50
55
envs .load_dotenv_for_agent ("test_app" , "." )
51
56
52
- event1 = Event (
53
- author = "dummy agent" ,
54
- invocation_id = "invocation_id" ,
55
- content = types .Content (
56
- role = "model" , parts = [types .Part (text = "LLM reply" , inline_data = None )]
57
- ),
58
- )
59
57
60
- event2 = Event (
61
- author = "dummy agent" ,
62
- invocation_id = "invocation_id" ,
63
- content = types .Content (
64
- role = "model" ,
65
- parts = [
66
- types .Part (
67
- text = None ,
68
- inline_data = types .Blob (
69
- mime_type = "audio/pcm;rate=24000" , data = b"\x00 \xFF "
70
- ),
71
- )
72
- ],
73
- ),
74
- )
58
+ def _event_1 ():
59
+ from google .adk .events import Event
75
60
76
- event3 = Event (
77
- author = "dummy agent" , invocation_id = "invocation_id" , interrupted = True
78
- )
61
+ return Event (
62
+ author = "dummy agent" ,
63
+ invocation_id = "invocation_id" ,
64
+ content = types .Content (
65
+ role = "model" , parts = [types .Part (text = "LLM reply" , inline_data = None )]
66
+ ),
67
+ )
68
+
69
+
70
+ def _event_2 ():
71
+ from google .adk .events import Event
72
+
73
+ return Event (
74
+ author = "dummy agent" ,
75
+ invocation_id = "invocation_id" ,
76
+ content = types .Content (
77
+ role = "model" ,
78
+ parts = [
79
+ types .Part (
80
+ text = None ,
81
+ inline_data = types .Blob (
82
+ mime_type = "audio/pcm;rate=24000" , data = b"\x00 \xFF "
83
+ ),
84
+ )
85
+ ],
86
+ ),
87
+ )
88
+
89
+
90
+ def _event_3 ():
91
+ from google .adk .events import Event
92
+
93
+ return Event (
94
+ author = "dummy agent" , invocation_id = "invocation_id" , interrupted = True
95
+ )
79
96
80
97
81
98
# For simplicity, we patch Runner.run_live to yield dummy events.
@@ -84,13 +101,13 @@ async def dummy_run_live(
84
101
self , session , live_request_queue
85
102
) -> AsyncGenerator [Event , None ]:
86
103
# Immediately yield a dummy event with a text reply.
87
- yield event1
104
+ yield _event_1 ()
88
105
await asyncio .sleep (0 )
89
106
90
- yield event2
107
+ yield _event_2 ()
91
108
await asyncio .sleep (0 )
92
109
93
- yield event3
110
+ yield _event_3 ()
94
111
95
112
raise Exception ()
96
113
@@ -103,13 +120,13 @@ async def dummy_run_async(
103
120
run_config : RunConfig = RunConfig (),
104
121
) -> AsyncGenerator [Event , None ]:
105
122
# Immediately yield a dummy event with a text reply.
106
- yield event1
123
+ yield _event_1 ()
107
124
await asyncio .sleep (0 )
108
125
109
- yield event2
126
+ yield _event_2 ()
110
127
await asyncio .sleep (0 )
111
128
112
- yield event3
129
+ yield _event_3 ()
113
130
114
131
return
115
132
@@ -199,15 +216,15 @@ async def test_sse_endpoint():
199
216
if event_data :
200
217
event_count += 1
201
218
if event_count == 1 :
202
- assert event_data == event1 .model_dump_json (
219
+ assert event_data == _event_1 () .model_dump_json (
203
220
exclude_none = True , by_alias = True
204
221
)
205
222
elif event_count == 2 :
206
- assert event_data == event2 .model_dump_json (
223
+ assert event_data == _event_2 () .model_dump_json (
207
224
exclude_none = True , by_alias = True
208
225
)
209
226
elif event_count == 3 :
210
- assert event_data == event3 .model_dump_json (
227
+ assert event_data == _event_3 () .model_dump_json (
211
228
exclude_none = True , by_alias = True
212
229
)
213
230
else :
0 commit comments