Skip to content

Commit 05917ca

Browse files
google-genai-botcopybara-github
authored andcommitted
ADK changes
PiperOrigin-RevId: 759259620
1 parent 1804ca3 commit 05917ca

23 files changed

Lines changed: 264 additions & 268 deletions

‎src/google/adk/cli/cli.py‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ async def run_input_file(
5555
input_file = InputFile.model_validate_json(f.read())
5656
input_file.state['_time'] = datetime.now()
5757

58-
session = session_service.create_session(
58+
session = await session_service.create_session(
5959
app_name=app_name, user_id=user_id, state=input_file.state
6060
)
6161
for query in input_file.queries:
@@ -130,7 +130,7 @@ async def run_cli(
130130
agent_module_path = os.path.join(agent_parent_dir, agent_folder_name)
131131
agent_module = importlib.import_module(agent_folder_name)
132132
user_id = 'test_user'
133-
session = session_service.create_session(
133+
session = await session_service.create_session(
134134
app_name=agent_folder_name, user_id=user_id
135135
)
136136
root_agent = agent_module.agent.root_agent
@@ -145,14 +145,12 @@ async def run_cli(
145145
input_path=input_file,
146146
)
147147
elif saved_session_file:
148-
149-
loaded_session = None
150148
with open(saved_session_file, 'r') as f:
151149
loaded_session = Session.model_validate_json(f.read())
152150

153151
if loaded_session:
154152
for event in loaded_session.events:
155-
session_service.append_event(session, event)
153+
await session_service.append_event(session, event)
156154
content = event.content
157155
if not content or not content.parts or not content.parts[0].text:
158156
continue
@@ -181,7 +179,7 @@ async def run_cli(
181179
session_path = f'{agent_module_path}/{session_id}.session.json'
182180

183181
# Fetch the session again to get all the details.
184-
session = session_service.get_session(
182+
session = await session_service.get_session(
185183
app_name=session.app_name,
186184
user_id=session.user_id,
187185
session_id=session.id,

‎src/google/adk/cli/fast_api.py‎

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,12 @@ def get_session_trace(session_id: str) -> Any:
333333
"/apps/{app_name}/users/{user_id}/sessions/{session_id}",
334334
response_model_exclude_none=True,
335335
)
336-
def get_session(app_name: str, user_id: str, session_id: str) -> Session:
336+
async def get_session(
337+
app_name: str, user_id: str, session_id: str
338+
) -> Session:
337339
# Connect to managed session if agent_engine_id is set.
338340
app_name = agent_engine_id if agent_engine_id else app_name
339-
session = session_service.get_session(
341+
session = await session_service.get_session(
340342
app_name=app_name, user_id=user_id, session_id=session_id
341343
)
342344
if not session:
@@ -347,14 +349,15 @@ def get_session(app_name: str, user_id: str, session_id: str) -> Session:
347349
"/apps/{app_name}/users/{user_id}/sessions",
348350
response_model_exclude_none=True,
349351
)
350-
def list_sessions(app_name: str, user_id: str) -> list[Session]:
352+
async def list_sessions(app_name: str, user_id: str) -> list[Session]:
351353
# Connect to managed session if agent_engine_id is set.
352354
app_name = agent_engine_id if agent_engine_id else app_name
355+
list_sessions_response = await session_service.list_sessions(
356+
app_name=app_name, user_id=user_id
357+
)
353358
return [
354359
session
355-
for session in session_service.list_sessions(
356-
app_name=app_name, user_id=user_id
357-
).sessions
360+
for session in list_sessions_response.sessions
358361
# Remove sessions that were generated as a part of Eval.
359362
if not session.id.startswith(EVAL_SESSION_ID_PREFIX)
360363
]
@@ -363,7 +366,7 @@ def list_sessions(app_name: str, user_id: str) -> list[Session]:
363366
"/apps/{app_name}/users/{user_id}/sessions/{session_id}",
364367
response_model_exclude_none=True,
365368
)
366-
def create_session_with_id(
369+
async def create_session_with_id(
367370
app_name: str,
368371
user_id: str,
369372
session_id: str,
@@ -372,7 +375,7 @@ def create_session_with_id(
372375
# Connect to managed session if agent_engine_id is set.
373376
app_name = agent_engine_id if agent_engine_id else app_name
374377
if (
375-
session_service.get_session(
378+
await session_service.get_session(
376379
app_name=app_name, user_id=user_id, session_id=session_id
377380
)
378381
is not None
@@ -382,23 +385,23 @@ def create_session_with_id(
382385
status_code=400, detail=f"Session already exists: {session_id}"
383386
)
384387
logger.info("New session created: %s", session_id)
385-
return session_service.create_session(
388+
return await session_service.create_session(
386389
app_name=app_name, user_id=user_id, state=state, session_id=session_id
387390
)
388391

389392
@app.post(
390393
"/apps/{app_name}/users/{user_id}/sessions",
391394
response_model_exclude_none=True,
392395
)
393-
def create_session(
396+
async def create_session(
394397
app_name: str,
395398
user_id: str,
396399
state: Optional[dict[str, Any]] = None,
397400
) -> Session:
398401
# Connect to managed session if agent_engine_id is set.
399402
app_name = agent_engine_id if agent_engine_id else app_name
400403
logger.info("New session created")
401-
return session_service.create_session(
404+
return await session_service.create_session(
402405
app_name=app_name, user_id=user_id, state=state
403406
)
404407

@@ -442,7 +445,7 @@ async def add_session_to_eval_set(
442445
app_name: str, eval_set_id: str, req: AddSessionToEvalSetRequest
443446
):
444447
# Get the session
445-
session = session_service.get_session(
448+
session = await session_service.get_session(
446449
app_name=app_name, user_id=req.user_id, session_id=req.session_id
447450
)
448451
assert session, "Session not found."
@@ -530,7 +533,7 @@ async def run_eval(
530533
session_id=eval_case_result.session_id,
531534
)
532535
)
533-
eval_case_result.session_details = session_service.get_session(
536+
eval_case_result.session_details = await session_service.get_session(
534537
app_name=app_name,
535538
user_id=eval_case_result.user_id,
536539
session_id=eval_case_result.session_id,
@@ -615,10 +618,10 @@ def list_eval_results(app_name: str) -> list[str]:
615618
return eval_result_files
616619

617620
@app.delete("/apps/{app_name}/users/{user_id}/sessions/{session_id}")
618-
def delete_session(app_name: str, user_id: str, session_id: str):
621+
async def delete_session(app_name: str, user_id: str, session_id: str):
619622
# Connect to managed session if agent_engine_id is set.
620623
app_name = agent_engine_id if agent_engine_id else app_name
621-
session_service.delete_session(
624+
await session_service.delete_session(
622625
app_name=app_name, user_id=user_id, session_id=session_id
623626
)
624627

@@ -713,7 +716,7 @@ async def delete_artifact(
713716
async def agent_run(req: AgentRunRequest) -> list[Event]:
714717
# Connect to managed session if agent_engine_id is set.
715718
app_id = agent_engine_id if agent_engine_id else req.app_name
716-
session = session_service.get_session(
719+
session = await session_service.get_session(
717720
app_name=app_id, user_id=req.user_id, session_id=req.session_id
718721
)
719722
if not session:
@@ -735,7 +738,7 @@ async def agent_run_sse(req: AgentRunRequest) -> StreamingResponse:
735738
# Connect to managed session if agent_engine_id is set.
736739
app_id = agent_engine_id if agent_engine_id else req.app_name
737740
# SSE endpoint
738-
session = session_service.get_session(
741+
session = await session_service.get_session(
739742
app_name=app_id, user_id=req.user_id, session_id=req.session_id
740743
)
741744
if not session:
@@ -776,7 +779,7 @@ async def get_event_graph(
776779
):
777780
# Connect to managed session if agent_engine_id is set.
778781
app_id = agent_engine_id if agent_engine_id else app_name
779-
session = session_service.get_session(
782+
session = await session_service.get_session(
780783
app_name=app_id, user_id=user_id, session_id=session_id
781784
)
782785
session_events = session.events if session else []
@@ -833,7 +836,7 @@ async def agent_live_run(
833836

834837
# Connect to managed session if agent_engine_id is set.
835838
app_id = agent_engine_id if agent_engine_id else app_name
836-
session = session_service.get_session(
839+
session = await session_service.get_session(
837840
app_name=app_id, user_id=user_id, session_id=session_id
838841
)
839842
if not session:

‎src/google/adk/evaluation/evaluation_generator.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ async def _generate_inferences_from_root_agent(
126126
user_id = initial_session.user_id if initial_session else "test_user_id"
127127
session_id = session_id if session_id else str(uuid.uuid4())
128128

129-
_ = session_service.create_session(
129+
_ = await session_service.create_session(
130130
app_name=app_name,
131131
user_id=user_id,
132132
state=initial_session.state if initial_session else {},

‎src/google/adk/runners.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ async def run_async(
173173
The events generated by the agent.
174174
"""
175175
with tracer.start_as_current_span('invocation'):
176-
session = self.session_service.get_session(
176+
session = await self.session_service.get_session(
177177
app_name=self.app_name, user_id=user_id, session_id=session_id
178178
)
179179
if not session:
@@ -197,7 +197,7 @@ async def run_async(
197197
invocation_context.agent = self._find_agent_to_run(session, root_agent)
198198
async for event in invocation_context.agent.run_async(invocation_context):
199199
if not event.partial:
200-
self.session_service.append_event(session=session, event=event)
200+
await self.session_service.append_event(session=session, event=event)
201201
yield event
202202

203203
async def _append_new_message_to_session(
@@ -242,7 +242,7 @@ async def _append_new_message_to_session(
242242
author='user',
243243
content=new_message,
244244
)
245-
self.session_service.append_event(session=session, event=event)
245+
await self.session_service.append_event(session=session, event=event)
246246

247247
async def run_live(
248248
self,
@@ -324,7 +324,7 @@ async def run_live(
324324
)
325325

326326
async for event in invocation_context.agent.run_live(invocation_context):
327-
self.session_service.append_event(session=session, event=event)
327+
await self.session_service.append_event(session=session, event=event)
328328
yield event
329329

330330
async def close_session(self, session: Session):
@@ -335,7 +335,7 @@ async def close_session(self, session: Session):
335335
"""
336336
if self.memory_service:
337337
await self.memory_service.add_session_to_memory(session)
338-
self.session_service.close_session(session=session)
338+
await self.session_service.close_session(session=session)
339339

340340
def _find_agent_to_run(
341341
self, session: Session, root_agent: BaseAgent

‎src/google/adk/sessions/base_session_service.py‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class BaseSessionService(abc.ABC):
4747
"""
4848

4949
@abc.abstractmethod
50-
def create_session(
50+
async def create_session(
5151
self,
5252
*,
5353
app_name: str,
@@ -67,10 +67,9 @@ def create_session(
6767
Returns:
6868
session: The newly created session instance.
6969
"""
70-
pass
7170

7271
@abc.abstractmethod
73-
def get_session(
72+
async def get_session(
7473
self,
7574
*,
7675
app_name: str,
@@ -79,28 +78,24 @@ def get_session(
7978
config: Optional[GetSessionConfig] = None,
8079
) -> Optional[Session]:
8180
"""Gets a session."""
82-
pass
8381

8482
@abc.abstractmethod
85-
def list_sessions(
83+
async def list_sessions(
8684
self, *, app_name: str, user_id: str
8785
) -> ListSessionsResponse:
8886
"""Lists all the sessions."""
89-
pass
9087

9188
@abc.abstractmethod
92-
def delete_session(
89+
async def delete_session(
9390
self, *, app_name: str, user_id: str, session_id: str
9491
) -> None:
9592
"""Deletes a session."""
96-
pass
9793

98-
def close_session(self, *, session: Session):
94+
async def close_session(self, *, session: Session):
9995
"""Closes a session."""
10096
# TODO: determine whether we want to finalize the session here.
101-
pass
10297

103-
def append_event(self, session: Session, event: Event) -> Event:
98+
async def append_event(self, session: Session, event: Event) -> Event:
10499
"""Appends an event to a session object."""
105100
if event.partial:
106101
return event

‎src/google/adk/sessions/database_session_service.py‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ def __init__(self, db_url: str):
283283
Base.metadata.create_all(self.db_engine)
284284

285285
@override
286-
def create_session(
286+
async def create_session(
287287
self,
288288
*,
289289
app_name: str,
@@ -357,7 +357,7 @@ def create_session(
357357
return session
358358

359359
@override
360-
def get_session(
360+
async def get_session(
361361
self,
362362
*,
363363
app_name: str,
@@ -431,7 +431,7 @@ def get_session(
431431
return session
432432

433433
@override
434-
def list_sessions(
434+
async def list_sessions(
435435
self, *, app_name: str, user_id: str
436436
) -> ListSessionsResponse:
437437
with self.DatabaseSessionFactory() as sessionFactory:
@@ -454,7 +454,7 @@ def list_sessions(
454454
return ListSessionsResponse(sessions=sessions)
455455

456456
@override
457-
def delete_session(
457+
async def delete_session(
458458
self, app_name: str, user_id: str, session_id: str
459459
) -> None:
460460
with self.DatabaseSessionFactory() as sessionFactory:
@@ -467,7 +467,7 @@ def delete_session(
467467
sessionFactory.commit()
468468

469469
@override
470-
def append_event(self, session: Session, event: Event) -> Event:
470+
async def append_event(self, session: Session, event: Event) -> Event:
471471
logger.info(f"Append event: {event} to session {session.id}")
472472

473473
if event.partial:
@@ -552,9 +552,10 @@ def append_event(self, session: Session, event: Event) -> Event:
552552
session.last_update_time = storage_session.update_time.timestamp()
553553

554554
# Also update the in-memory session
555-
super().append_event(session=session, event=event)
555+
await super().append_event(session=session, event=event)
556556
return event
557557

558+
558559
def convert_event(event: StorageEvent) -> Event:
559560
"""Converts a storage event to an event."""
560561
return Event(

0 commit comments

Comments
 (0)