action.py 2.1 KB

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