@@ -152,6 +152,7 @@ async def _create_txn(transaction: firestore.AsyncTransaction) -> None:
152152 snap = await session_ref .get (transaction = transaction )
153153 if snap .exists :
154154 from ...errors .already_exists_error import AlreadyExistsError
155+
155156 raise AlreadyExistsError (f"Session { session_id } already exists." )
156157 transaction .set (session_ref , session_data )
157158
@@ -240,7 +241,9 @@ async def list_sessions(
240241 ) -> ListSessionsResponse :
241242 """Lists sessions from Firestore."""
242243 if user_id :
243- query = self ._get_sessions_ref (app_name , user_id ).where ("appName" , "==" , app_name )
244+ query = self ._get_sessions_ref (app_name , user_id ).where (
245+ "appName" , "==" , app_name
246+ )
244247 docs = await query .get ()
245248 else :
246249 query = self .client .collection_group (self .sessions_collection ).where (
@@ -320,52 +323,6 @@ async def delete_session(
320323
321324 await session_ref .delete ()
322325
323-
324- async def _update_app_state_transactional (
325- self , app_name : str , delta : dict [str , Any ]
326- ) -> dict [str , Any ]:
327- """Atomically applies delta to app state inside a transaction."""
328- from google .cloud import firestore
329-
330- doc_ref = self .client .collection (self .app_state_collection ).document (
331- app_name
332- )
333-
334- @firestore .async_transactional # type: ignore[untyped-decorator]
335- async def _txn (transaction : firestore .AsyncTransaction ) -> dict [str , Any ]:
336- snap = await doc_ref .get (transaction = transaction )
337- current = snap .to_dict () if snap .exists else {}
338- current .update (delta )
339- transaction .set (doc_ref , current , merge = True )
340- return current
341-
342- transaction = self .client .transaction ()
343- return cast (dict [str , Any ], await _txn (transaction ))
344-
345- async def _update_user_state_transactional (
346- self , app_name : str , user_id : str , delta : dict [str , Any ]
347- ) -> dict [str , Any ]:
348- """Atomically applies delta to user state inside a transaction."""
349- from google .cloud import firestore
350-
351- doc_ref = (
352- self .client .collection (self .user_state_collection )
353- .document (app_name )
354- .collection ("users" )
355- .document (user_id )
356- )
357-
358- @firestore .async_transactional # type: ignore[untyped-decorator]
359- async def _txn (transaction : firestore .AsyncTransaction ) -> dict [str , Any ]:
360- snap = await doc_ref .get (transaction = transaction )
361- current = snap .to_dict () if snap .exists else {}
362- current .update (delta )
363- transaction .set (doc_ref , current , merge = True )
364- return current
365-
366- transaction = self .client .transaction ()
367- return cast (dict [str , Any ], await _txn (transaction ))
368-
369326 async def append_event (self , session : Session , event : Event ) -> Event :
370327 """Appends an event to a session in Firestore."""
371328 from google .cloud import firestore
@@ -376,7 +333,9 @@ async def append_event(self, session: Session, event: Event) -> Event:
376333 self ._apply_temp_state (session , event )
377334 event = self ._trim_temp_delta_state (event )
378335
379- session_ref = self ._get_sessions_ref (session .app_name , session .user_id ).document (session .id )
336+ session_ref = self ._get_sessions_ref (
337+ session .app_name , session .user_id
338+ ).document (session .id )
380339
381340 if event .actions and event .actions .state_delta :
382341 state_delta = event .actions .state_delta
@@ -392,7 +351,9 @@ async def append_event(self, session: Session, event: Event) -> Event:
392351 else :
393352 session_updates [key ] = value
394353
395- app_ref = self .client .collection (self .app_state_collection ).document (session .app_name )
354+ app_ref = self .client .collection (self .app_state_collection ).document (
355+ session .app_name
356+ )
396357 user_ref = (
397358 self .client .collection (self .user_state_collection )
398359 .document (session .app_name )
@@ -403,8 +364,14 @@ async def append_event(self, session: Session, event: Event) -> Event:
403364 @firestore .async_transactional # type: ignore[untyped-decorator]
404365 async def _append_txn (transaction : firestore .AsyncTransaction ) -> None :
405366 # 1. Reads
406- app_snap = await app_ref .get (transaction = transaction ) if app_updates else None
407- user_snap = await user_ref .get (transaction = transaction ) if user_updates else None
367+ app_snap = (
368+ await app_ref .get (transaction = transaction ) if app_updates else None
369+ )
370+ user_snap = (
371+ await user_ref .get (transaction = transaction )
372+ if user_updates
373+ else None
374+ )
408375
409376 # 2. Writes
410377 if app_updates and app_snap is not None :
@@ -429,7 +396,9 @@ async def _append_txn(transaction: firestore.AsyncTransaction) -> None:
429396 )
430397
431398 event_id = event .id
432- event_ref = session_ref .collection (self .events_collection ).document (event_id )
399+ event_ref = session_ref .collection (self .events_collection ).document (
400+ event_id
401+ )
433402 event_data = event .model_dump (exclude_none = True , mode = "json" )
434403 transaction .set (
435404 event_ref ,
0 commit comments