agent.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from dataclasses import dataclass, field
  2. from typing import Any
  3. from openhands.core.schema import ActionType
  4. from openhands.events.action.action import Action
  5. @dataclass
  6. class ChangeAgentStateAction(Action):
  7. """Fake action, just to notify the client that a task state has changed."""
  8. agent_state: str
  9. thought: str = ''
  10. action: str = ActionType.CHANGE_AGENT_STATE
  11. @property
  12. def message(self) -> str:
  13. return f'Agent state changed to {self.agent_state}'
  14. @dataclass
  15. class AgentSummarizeAction(Action):
  16. summary: str
  17. action: str = ActionType.SUMMARIZE
  18. @property
  19. def message(self) -> str:
  20. return self.summary
  21. def __str__(self) -> str:
  22. ret = '**AgentSummarizeAction**\n'
  23. ret += f'SUMMARY: {self.summary}'
  24. return ret
  25. @dataclass
  26. class AgentFinishAction(Action):
  27. """An action where the agent finishes the task.
  28. Attributes:
  29. outputs (dict): The outputs of the agent, for instance "content".
  30. thought (str): The agent's explanation of its actions.
  31. action (str): The action type, namely ActionType.FINISH.
  32. """
  33. outputs: dict[str, Any] = field(default_factory=dict)
  34. thought: str = ''
  35. action: str = ActionType.FINISH
  36. @property
  37. def message(self) -> str:
  38. if self.thought != '':
  39. return self.thought
  40. return "All done! What's next on the agenda?"
  41. @dataclass
  42. class AgentRejectAction(Action):
  43. outputs: dict = field(default_factory=dict)
  44. thought: str = ''
  45. action: str = ActionType.REJECT
  46. @property
  47. def message(self) -> str:
  48. msg: str = 'Task is rejected by the agent.'
  49. if 'reason' in self.outputs:
  50. msg += ' Reason: ' + self.outputs['reason']
  51. return msg
  52. @dataclass
  53. class AgentDelegateAction(Action):
  54. agent: str
  55. inputs: dict
  56. thought: str = ''
  57. action: str = ActionType.DELEGATE
  58. @property
  59. def message(self) -> str:
  60. return f"I'm asking {self.agent} for help with this task."