test_codeact_agent.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. from unittest.mock import Mock
  2. import pytest
  3. from openhands.agenthub.codeact_agent.codeact_agent import CodeActAgent
  4. from openhands.core.config import AgentConfig, LLMConfig
  5. from openhands.core.message import TextContent
  6. from openhands.events.observation.commands import (
  7. CmdOutputObservation,
  8. IPythonRunCellObservation,
  9. )
  10. from openhands.events.observation.delegate import AgentDelegateObservation
  11. from openhands.events.observation.error import ErrorObservation
  12. from openhands.llm.llm import LLM
  13. @pytest.fixture
  14. def agent() -> CodeActAgent:
  15. agent = CodeActAgent(llm=LLM(LLMConfig()), config=AgentConfig())
  16. agent.llm = Mock()
  17. agent.llm.config = Mock()
  18. agent.llm.config.max_message_chars = 100
  19. return agent
  20. def test_cmd_output_observation_message(agent: CodeActAgent):
  21. obs = CmdOutputObservation(
  22. command='echo hello', content='Command output', command_id=1, exit_code=0
  23. )
  24. result = agent.get_observation_message(obs)
  25. assert result is not None
  26. assert result.role == 'user'
  27. assert len(result.content) == 1
  28. assert isinstance(result.content[0], TextContent)
  29. assert 'OBSERVATION:' in result.content[0].text
  30. assert 'Command output' in result.content[0].text
  31. assert 'Command 1 finished with exit code 0' in result.content[0].text
  32. def test_ipython_run_cell_observation_message(agent: CodeActAgent):
  33. obs = IPythonRunCellObservation(
  34. code='plt.plot()',
  35. content='IPython output\n![image](data:image/png;base64,ABC123)',
  36. )
  37. result = agent.get_observation_message(obs)
  38. assert result is not None
  39. assert result.role == 'user'
  40. assert len(result.content) == 1
  41. assert isinstance(result.content[0], TextContent)
  42. assert 'OBSERVATION:' in result.content[0].text
  43. assert 'IPython output' in result.content[0].text
  44. assert (
  45. '![image](data:image/png;base64, ...) already displayed to user'
  46. in result.content[0].text
  47. )
  48. assert 'ABC123' not in result.content[0].text
  49. def test_agent_delegate_observation_message(agent: CodeActAgent):
  50. obs = AgentDelegateObservation(
  51. content='Content', outputs={'content': 'Delegated agent output'}
  52. )
  53. result = agent.get_observation_message(obs)
  54. assert result is not None
  55. assert result.role == 'user'
  56. assert len(result.content) == 1
  57. assert isinstance(result.content[0], TextContent)
  58. assert 'OBSERVATION:' in result.content[0].text
  59. assert 'Delegated agent output' in result.content[0].text
  60. def test_error_observation_message(agent: CodeActAgent):
  61. obs = ErrorObservation('Error message')
  62. result = agent.get_observation_message(obs)
  63. assert result is not None
  64. assert result.role == 'user'
  65. assert len(result.content) == 1
  66. assert isinstance(result.content[0], TextContent)
  67. assert 'OBSERVATION:' in result.content[0].text
  68. assert 'Error message' in result.content[0].text
  69. assert 'Error occurred in processing last action' in result.content[0].text
  70. def test_unknown_observation_message(agent: CodeActAgent):
  71. obs = Mock()
  72. with pytest.raises(ValueError, match='Unknown observation type:'):
  73. agent.get_observation_message(obs)