action.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from opendevin.core.exceptions import LLMMalformedActionError
  2. from opendevin.events.action.action import Action
  3. from opendevin.events.action.agent import (
  4. AgentDelegateAction,
  5. AgentFinishAction,
  6. AgentRejectAction,
  7. ChangeAgentStateAction,
  8. )
  9. from opendevin.events.action.browse import BrowseInteractiveAction, BrowseURLAction
  10. from opendevin.events.action.commands import (
  11. CmdRunAction,
  12. IPythonRunCellAction,
  13. )
  14. from opendevin.events.action.empty import NullAction
  15. from opendevin.events.action.files import FileReadAction, FileWriteAction
  16. from opendevin.events.action.message import MessageAction
  17. from opendevin.events.action.tasks import AddTaskAction, ModifyTaskAction
  18. actions = (
  19. NullAction,
  20. CmdRunAction,
  21. IPythonRunCellAction,
  22. BrowseURLAction,
  23. BrowseInteractiveAction,
  24. FileReadAction,
  25. FileWriteAction,
  26. AgentFinishAction,
  27. AgentRejectAction,
  28. AgentDelegateAction,
  29. AddTaskAction,
  30. ModifyTaskAction,
  31. ChangeAgentStateAction,
  32. MessageAction,
  33. )
  34. ACTION_TYPE_TO_CLASS = {action_class.action: action_class for action_class in actions} # type: ignore[attr-defined]
  35. def action_from_dict(action: dict) -> Action:
  36. if not isinstance(action, dict):
  37. raise LLMMalformedActionError('action must be a dictionary')
  38. action = action.copy()
  39. if 'action' not in action:
  40. raise LLMMalformedActionError(f"'action' key is not found in {action=}")
  41. if not isinstance(action['action'], str):
  42. raise LLMMalformedActionError(
  43. f"'{action['action']=}' is not defined. Available actions: {ACTION_TYPE_TO_CLASS.keys()}"
  44. )
  45. action_class = ACTION_TYPE_TO_CLASS.get(action['action'])
  46. if action_class is None:
  47. raise LLMMalformedActionError(
  48. f"'{action['action']=}' is not defined. Available actions: {ACTION_TYPE_TO_CLASS.keys()}"
  49. )
  50. args = action.get('args', {})
  51. try:
  52. decoded_action = action_class(**args)
  53. except TypeError:
  54. raise LLMMalformedActionError(f'action={action} has the wrong arguments')
  55. return decoded_action