event.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import os
  2. import json
  3. import opendevin.lib.actions as actions
  4. ACTION_TYPES = ['run', 'kill', 'browse', 'read', 'write', 'recall', 'think', 'output', 'error', 'finish']
  5. RUNNABLE_ACTIONS = ['run', 'kill', 'browse', 'read', 'write', 'recall']
  6. class Event:
  7. def __init__(self, action, args):
  8. if action not in ACTION_TYPES:
  9. raise ValueError('Invalid action type: ' + action)
  10. self.action = action
  11. self.args = args
  12. def __str__(self):
  13. return self.action + " " + str(self.args)
  14. def to_dict(self):
  15. return {
  16. 'action': self.action,
  17. 'args': self.args
  18. }
  19. def is_runnable(self):
  20. return self.action in RUNNABLE_ACTIONS
  21. def run(self, agent_controller):
  22. if self.action == 'run':
  23. cmd = self.args['command']
  24. background = False
  25. if 'background' in self.args and self.args['background']:
  26. background = True
  27. return agent_controller.command_manager.run_command(cmd, background)
  28. if self.action == 'kill':
  29. id = self.args['id']
  30. return agent_controller.command_manager.kill_command(id)
  31. elif self.action == 'browse':
  32. url = self.args['url']
  33. return actions.browse(url)
  34. elif self.action == 'read':
  35. path = self.args['path']
  36. return actions.read(agent_controller.command_manager.directory, path)
  37. elif self.action == 'write':
  38. path = self.args['path']
  39. contents = self.args['contents']
  40. return actions.write(agent_controller.command_manager.directory, path, contents)
  41. elif self.action == 'recall':
  42. return agent_controller.agent.search_memory(self.args['query'])
  43. else:
  44. raise ValueError('Invalid action type')