event.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import opendevin.lib.actions as actions
  2. ACTION_TYPES = ['initialize', 'start', 'summarize', 'run', 'kill', 'browse', 'read', 'write', 'recall', 'think', 'output', 'error', 'finish']
  3. RUNNABLE_ACTIONS = ['run', 'kill', 'browse', 'read', 'write', 'recall']
  4. class Event:
  5. def __init__(self, action, args, message=None):
  6. if action not in ACTION_TYPES:
  7. raise ValueError('Invalid action type: ' + action)
  8. self.action = action
  9. self.args = args
  10. self.message = message
  11. def __str__(self):
  12. return self.action + " " + str(self.args)
  13. def str_truncated(self, max_len=1000):
  14. s = str(self)
  15. if len(s) > max_len:
  16. s = s[:max_len] + '...'
  17. return s
  18. def to_dict(self):
  19. return {
  20. 'action': self.action,
  21. 'args': self.args
  22. }
  23. def get_message(self) -> str:
  24. if self.message is not None:
  25. return self.message
  26. if self.action == 'run':
  27. return 'Running command: ' + self.args['command']
  28. elif self.action == 'kill':
  29. return 'Killing command: ' + self.args['id']
  30. elif self.action == 'browse':
  31. return 'Browsing: ' + self.args['url']
  32. elif self.action == 'read':
  33. return 'Reading file: ' + self.args['path']
  34. elif self.action == 'write':
  35. return 'Writing to file: ' + self.args['path']
  36. elif self.action == 'recall':
  37. return 'Recalling memory: ' + self.args['query']
  38. elif self.action == 'think':
  39. return self.args['thought']
  40. elif self.action == 'output':
  41. return "Got output."
  42. elif self.action == 'error':
  43. return "Got an error: " + self.args['output']
  44. elif self.action == 'finish':
  45. return "Finished!"
  46. else:
  47. return ""
  48. def is_runnable(self):
  49. return self.action in RUNNABLE_ACTIONS
  50. def run(self, agent_controller):
  51. if not self.is_runnable():
  52. return None
  53. action = 'output'
  54. try:
  55. output = self._run_and_get_output(agent_controller)
  56. except Exception as e:
  57. output = 'Error: ' + str(e)
  58. action = 'error'
  59. out_event = Event(action, {'output': output})
  60. return out_event
  61. def _run_and_get_output(self, agent_controller) -> str:
  62. if self.action == 'run':
  63. cmd = self.args['command']
  64. background = False
  65. if 'background' in self.args and self.args['background']:
  66. background = True
  67. return agent_controller.command_manager.run_command(cmd, background)
  68. if self.action == 'kill':
  69. id = self.args['id']
  70. return agent_controller.command_manager.kill_command(id)
  71. elif self.action == 'browse':
  72. url = self.args['url']
  73. return actions.browse(url)
  74. elif self.action == 'read':
  75. path = self.args['path']
  76. return actions.read(agent_controller.command_manager.directory, path)
  77. elif self.action == 'write':
  78. path = self.args['path']
  79. contents = self.args['contents']
  80. return actions.write(agent_controller.command_manager.directory, path, contents)
  81. elif self.action == 'recall':
  82. return agent_controller.agent.search_memory(self.args['query'])
  83. else:
  84. raise ValueError('Invalid action type')