langfun 0.1.2.dev202510300805__py3-none-any.whl → 0.1.2.dev202511010804__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of langfun might be problematic. Click here for more details.

@@ -29,7 +29,6 @@ import time
29
29
  from typing import Annotated, Any, Iterator
30
30
 
31
31
  from langfun.env import interface
32
- from langfun.env.event_handlers import base as event_handler_base
33
32
  import pyglove as pg
34
33
 
35
34
 
@@ -198,7 +197,7 @@ class BaseSandbox(interface.Sandbox):
198
197
  for name, feature in self.environment.features.items()
199
198
  if feature.is_applicable(self.image_id)
200
199
  })
201
- self._event_handlers = []
200
+ self._event_handler = self.environment.event_handler
202
201
  self._enable_pre_session_setup = (
203
202
  self.reusable and self.proactive_session_setup
204
203
  )
@@ -247,20 +246,6 @@ class BaseSandbox(interface.Sandbox):
247
246
  """Returns the housekeeping counter."""
248
247
  return self._housekeep_counter
249
248
 
250
- def add_event_handler(
251
- self,
252
- event_handler: event_handler_base.EventHandler | None
253
- ) -> None:
254
- """Sets the event handler for the sandbox."""
255
- self._event_handlers.append(event_handler)
256
-
257
- def remove_event_handler(
258
- self,
259
- event_handler: event_handler_base.EventHandler | None
260
- ) -> None:
261
- """Removes the event handler for the sandbox."""
262
- self._event_handlers.remove(event_handler)
263
-
264
249
  @property
265
250
  def state_errors(self) -> list[interface.SandboxStateError]:
266
251
  """Returns all errors encountered during sandbox lifecycle."""
@@ -648,7 +633,6 @@ class BaseSandbox(interface.Sandbox):
648
633
  shutdown_sandbox = True
649
634
 
650
635
  self._session_start_time = None
651
- self._session_event_handler = None
652
636
 
653
637
  if shutdown_sandbox:
654
638
  self.shutdown()
@@ -771,8 +755,9 @@ class BaseSandbox(interface.Sandbox):
771
755
  error: BaseException | None = None
772
756
  ) -> None:
773
757
  """Called when the sandbox is started."""
774
- for handler in self._event_handlers:
775
- handler.on_sandbox_start(self.environment, self, duration, error)
758
+ self._event_handler.on_sandbox_start(
759
+ self.environment, self, duration, error
760
+ )
776
761
 
777
762
  def on_status_change(
778
763
  self,
@@ -781,14 +766,9 @@ class BaseSandbox(interface.Sandbox):
781
766
  ) -> None:
782
767
  """Called when the sandbox status changes."""
783
768
  status_duration = time.time() - self._status_start_time
784
- for handler in self._event_handlers:
785
- handler.on_sandbox_status_change(
786
- self.environment,
787
- self,
788
- old_status,
789
- new_status,
790
- status_duration
791
- )
769
+ self._event_handler.on_sandbox_status_change(
770
+ self.environment, self, old_status, new_status, status_duration
771
+ )
792
772
 
793
773
  def on_shutdown(
794
774
  self,
@@ -796,14 +776,13 @@ class BaseSandbox(interface.Sandbox):
796
776
  error: BaseException | None = None
797
777
  ) -> None:
798
778
  """Called when the sandbox is shutdown."""
799
- if self._start_time is None:
800
- lifetime = 0.0
801
- else:
802
- lifetime = time.time() - self._start_time
803
- for handler in self._event_handlers:
804
- handler.on_sandbox_shutdown(
805
- self.environment, self, duration, lifetime, error
806
- )
779
+ self._event_handler.on_sandbox_shutdown(
780
+ self.environment,
781
+ self,
782
+ duration,
783
+ 0.0 if self._start_time is None else (time.time() - self._start_time),
784
+ error
785
+ )
807
786
 
808
787
  def on_housekeep(
809
788
  self,
@@ -812,72 +791,14 @@ class BaseSandbox(interface.Sandbox):
812
791
  **kwargs
813
792
  ) -> None:
814
793
  """Called when the sandbox finishes a round of housekeeping."""
815
- counter = self._housekeep_counter
816
- for handler in self._event_handlers:
817
- handler.on_sandbox_housekeep(
818
- self.environment, self, counter, duration, error, **kwargs
819
- )
820
-
821
- def on_feature_setup(
822
- self,
823
- feature: interface.Feature,
824
- duration: float,
825
- error: BaseException | None = None
826
- ) -> None:
827
- """Called when a feature is setup."""
828
- for handler in self._event_handlers:
829
- handler.on_feature_setup(
830
- self.environment, self, feature, duration, error
831
- )
832
-
833
- def on_feature_teardown(
834
- self,
835
- feature: interface.Feature,
836
- duration: float,
837
- error: BaseException | None = None
838
- ) -> None:
839
- """Called when a feature is teardown."""
840
- for handler in self._event_handlers:
841
- handler.on_feature_teardown(
842
- self.environment, self, feature, duration, error
843
- )
844
-
845
- def on_feature_setup_session(
846
- self,
847
- feature: interface.Feature,
848
- duration: float,
849
- error: BaseException | None = None
850
- ) -> None:
851
- """Called when a feature is setup for a user session."""
852
- for handler in self._event_handlers:
853
- handler.on_feature_setup_session(
854
- self.environment, self, feature, self.session_id, duration, error
855
- )
856
-
857
- def on_feature_teardown_session(
858
- self,
859
- feature: interface.Feature,
860
- duration: float,
861
- error: BaseException | None = None
862
- ) -> None:
863
- """Called when a feature is teardown for a user session."""
864
- for handler in self._event_handlers:
865
- handler.on_feature_teardown_session(
866
- self.environment, self, feature, self.session_id, duration, error
867
- )
868
-
869
- def on_feature_housekeep(
870
- self,
871
- feature: interface.Feature,
872
- counter: int,
873
- duration: float,
874
- error: BaseException | None = None
875
- ) -> None:
876
- """Called when a feature is housekeeping."""
877
- for handler in self._event_handlers:
878
- handler.on_feature_housekeep(
879
- self.environment, self, feature, counter, duration, error
880
- )
794
+ self._event_handler.on_sandbox_housekeep(
795
+ self.environment,
796
+ self,
797
+ self._housekeep_counter,
798
+ duration,
799
+ error,
800
+ **kwargs
801
+ )
881
802
 
882
803
  def on_session_start(
883
804
  self,
@@ -886,10 +807,9 @@ class BaseSandbox(interface.Sandbox):
886
807
  error: BaseException | None = None
887
808
  ) -> None:
888
809
  """Called when the user session starts."""
889
- for handler in self._event_handlers:
890
- handler.on_session_start(
891
- self.environment, self, session_id, duration, error
892
- )
810
+ self._event_handler.on_session_start(
811
+ self.environment, self, session_id, duration, error
812
+ )
893
813
 
894
814
  def on_activity(
895
815
  self,
@@ -900,17 +820,16 @@ class BaseSandbox(interface.Sandbox):
900
820
  **kwargs
901
821
  ) -> None:
902
822
  """Called when a sandbox activity is performed."""
903
- for handler in self._event_handlers:
904
- handler.on_sandbox_activity(
905
- name=name,
906
- environment=self.environment,
907
- sandbox=self,
908
- feature=feature,
909
- session_id=self.session_id,
910
- duration=duration,
911
- error=error,
912
- **kwargs
913
- )
823
+ self._event_handler.on_sandbox_activity(
824
+ name,
825
+ self.environment,
826
+ self,
827
+ feature,
828
+ self.session_id,
829
+ duration,
830
+ error,
831
+ **kwargs
832
+ )
914
833
 
915
834
  def on_session_end(
916
835
  self,
@@ -919,8 +838,11 @@ class BaseSandbox(interface.Sandbox):
919
838
  error: BaseException | None = None
920
839
  ) -> None:
921
840
  """Called when the user session ends."""
922
- lifetime = time.time() - self._session_start_time
923
- for handler in self._event_handlers:
924
- handler.on_session_end(
925
- self.environment, self, session_id, duration, lifetime, error
926
- )
841
+ self._event_handler.on_session_end(
842
+ self.environment,
843
+ self,
844
+ session_id,
845
+ duration,
846
+ time.time() - self._session_start_time,
847
+ error
848
+ )