1313# limitations under the License.
1414from __future__ import annotations
1515
16+ import asyncio
1617import datetime
1718import json
1819import logging
@@ -104,7 +105,7 @@ async def create_session(
104105
105106 if _is_vertex_express_mode (self ._project , self ._location ):
106107 config ['wait_for_completion' ] = False
107- api_response = api_client .agent_engines .sessions .create (
108+ api_response = await api_client . aio .agent_engines .sessions .create (
108109 name = f'reasoningEngines/{ reasoning_engine_id } ' ,
109110 user_id = user_id ,
110111 config = config ,
@@ -123,7 +124,7 @@ async def create_session(
123124 )
124125 async def _poll_session_resource ():
125126 try :
126- return api_client .agent_engines .sessions .get (
127+ return await api_client . aio .agent_engines .sessions .get (
127128 name = f'reasoningEngines/{ reasoning_engine_id } /sessions/{ session_id } '
128129 )
129130 except ClientError :
@@ -135,11 +136,11 @@ async def _poll_session_resource():
135136 except Exception as exc :
136137 raise ValueError ('Failed to create session.' ) from exc
137138
138- get_session_response = api_client .agent_engines .sessions .get (
139+ get_session_response = await api_client . aio .agent_engines .sessions .get (
139140 name = f'reasoningEngines/{ reasoning_engine_id } /sessions/{ session_id } '
140141 )
141142 else :
142- api_response = api_client .agent_engines .sessions .create (
143+ api_response = await api_client . aio .agent_engines .sessions .create (
143144 name = f'reasoningEngines/{ reasoning_engine_id } ' ,
144145 user_id = user_id ,
145146 config = config ,
@@ -168,10 +169,28 @@ async def get_session(
168169 ) -> Optional [Session ]:
169170 reasoning_engine_id = self ._get_reasoning_engine_id (app_name )
170171 api_client = self ._get_api_client ()
172+ session_resource_name = (
173+ f'reasoningEngines/{ reasoning_engine_id } /sessions/{ session_id } '
174+ )
175+
176+ # Get session resource and events in parallel.
177+ list_events_kwargs = {}
178+ if config and not config .num_recent_events and config .after_timestamp :
179+ # Filter events based on timestamp.
180+ list_events_kwargs ['config' ] = {
181+ 'filter' : 'timestamp>="{}"' .format (
182+ datetime .datetime .fromtimestamp (
183+ config .after_timestamp , tz = datetime .timezone .utc
184+ ).isoformat ()
185+ )
186+ }
171187
172- # Get session resource
173- get_session_response = api_client .agent_engines .sessions .get (
174- name = f'reasoningEngines/{ reasoning_engine_id } /sessions/{ session_id } ' ,
188+ get_session_response , events_iterator = await asyncio .gather (
189+ api_client .aio .agent_engines .sessions .get (name = session_resource_name ),
190+ api_client .aio .agent_engines .sessions .events .list (
191+ name = session_resource_name ,
192+ ** list_events_kwargs ,
193+ ),
175194 )
176195
177196 if get_session_response .user_id != user_id :
@@ -187,29 +206,14 @@ async def get_session(
187206 state = getattr (get_session_response , 'session_state' , None ) or {},
188207 last_update_time = update_timestamp ,
189208 )
190-
191- list_events_kwargs = {}
192- if config and not config .num_recent_events and config .after_timestamp :
193- list_events_kwargs ['config' ] = {
194- 'filter' : 'timestamp>="{}"' .format (
195- datetime .datetime .fromtimestamp (
196- config .after_timestamp , tz = datetime .timezone .utc
197- ).isoformat ()
198- )
199- }
200-
201- events_iterator = api_client .agent_engines .sessions .events .list (
202- name = f'reasoningEngines/{ reasoning_engine_id } /sessions/{ session_id } ' ,
203- ** list_events_kwargs ,
204- )
205209 session .events += [
206210 _from_api_event (event )
207211 for event in events_iterator
208212 if event .timestamp .timestamp () <= update_timestamp
209213 ]
210214
211- # Filter events based on config
212215 if config :
216+ # Filter events based on num_recent_events.
213217 if config .num_recent_events :
214218 session .events = session .events [- config .num_recent_events :]
215219
@@ -226,7 +230,7 @@ async def list_sessions(
226230 config = {}
227231 if user_id is not None :
228232 config ['filter' ] = f'user_id="{ user_id } "'
229- sessions_iterator = api_client .agent_engines .sessions .list (
233+ sessions_iterator = await api_client . aio .agent_engines .sessions .list (
230234 name = f'reasoningEngines/{ reasoning_engine_id } ' ,
231235 config = config ,
232236 )
@@ -251,7 +255,7 @@ async def delete_session(
251255 api_client = self ._get_api_client ()
252256
253257 try :
254- api_client .agent_engines .sessions .delete (
258+ await api_client . aio .agent_engines .sessions .delete (
255259 name = f'reasoningEngines/{ reasoning_engine_id } /sessions/{ session_id } ' ,
256260 )
257261 except Exception as e :
@@ -308,7 +312,7 @@ async def append_event(self, session: Session, event: Event) -> Event:
308312 )
309313 config ['event_metadata' ] = metadata_dict
310314
311- api_client .agent_engines .sessions .events .append (
315+ await api_client . aio .agent_engines .sessions .events .append (
312316 name = f'reasoningEngines/{ reasoning_engine_id } /sessions/{ session .id } ' ,
313317 author = event .author ,
314318 invocation_id = event .invocation_id ,
0 commit comments