observation.py 1.8 KB

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