observation.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from opendevin.events.observation.agent import AgentStateChangedObservation
  2. from opendevin.events.observation.browse import BrowserOutputObservation
  3. from opendevin.events.observation.commands import (
  4. CmdOutputObservation,
  5. IPythonRunCellObservation,
  6. )
  7. from opendevin.events.observation.delegate import AgentDelegateObservation
  8. from opendevin.events.observation.empty import NullObservation
  9. from opendevin.events.observation.error import ErrorObservation
  10. from opendevin.events.observation.files import FileReadObservation, FileWriteObservation
  11. from opendevin.events.observation.observation import Observation
  12. from opendevin.events.observation.reject import UserRejectObservation
  13. from opendevin.events.observation.success import SuccessObservation
  14. observations = (
  15. NullObservation,
  16. CmdOutputObservation,
  17. IPythonRunCellObservation,
  18. BrowserOutputObservation,
  19. FileReadObservation,
  20. FileWriteObservation,
  21. AgentDelegateObservation,
  22. SuccessObservation,
  23. ErrorObservation,
  24. AgentStateChangedObservation,
  25. UserRejectObservation,
  26. )
  27. OBSERVATION_TYPE_TO_CLASS = {
  28. observation_class.observation: observation_class # type: ignore[attr-defined]
  29. for observation_class in observations
  30. }
  31. def observation_from_dict(observation: dict) -> Observation:
  32. observation = observation.copy()
  33. if 'observation' not in observation:
  34. raise KeyError(f"'observation' key is not found in {observation=}")
  35. observation_class = OBSERVATION_TYPE_TO_CLASS.get(observation['observation'])
  36. if observation_class is None:
  37. raise KeyError(
  38. f"'{observation['observation']=}' is not defined. Available observations: {OBSERVATION_TYPE_TO_CLASS.keys()}"
  39. )
  40. observation.pop('observation')
  41. observation.pop('message', None)
  42. content = observation.pop('content', '')
  43. extras = observation.pop('extras', {})
  44. return observation_class(content=content, **extras)