test_micro_agents.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import json
  2. import os
  3. from unittest.mock import MagicMock
  4. import pytest
  5. import yaml
  6. from pytest import TempPathFactory
  7. from openhands.agenthub.micro.registry import all_microagents
  8. from openhands.controller.agent import Agent
  9. from openhands.controller.state.state import State
  10. from openhands.core.config import AgentConfig
  11. from openhands.events.action import MessageAction
  12. from openhands.events.stream import EventStream
  13. from openhands.storage import get_file_store
  14. @pytest.fixture
  15. def temp_dir(tmp_path_factory: TempPathFactory) -> str:
  16. return str(tmp_path_factory.mktemp('test_micro_agents'))
  17. @pytest.fixture
  18. def event_stream(temp_dir):
  19. file_store = get_file_store('local', temp_dir)
  20. event_stream = EventStream('asdf', file_store)
  21. yield event_stream
  22. # clear after each test
  23. event_stream.clear()
  24. @pytest.fixture
  25. def agent_configs():
  26. return {
  27. 'CoderAgent': AgentConfig(memory_enabled=True),
  28. 'PlannerAgent': AgentConfig(memory_enabled=True),
  29. }
  30. def test_all_agents_are_loaded():
  31. assert all_microagents is not None
  32. assert len(all_microagents) > 1
  33. base = os.path.join('openhands', 'agenthub', 'micro')
  34. full_path = os.path.dirname(__file__) + '/../../' + base
  35. agent_names = set()
  36. for root, _, files in os.walk(full_path):
  37. for file in files:
  38. if file == 'agent.yaml':
  39. file_path = os.path.join(root, file)
  40. with open(file_path, 'r') as yaml_file:
  41. data = yaml.safe_load(yaml_file)
  42. agent_names.add(data['name'])
  43. assert agent_names == set(all_microagents.keys())
  44. def test_coder_agent_with_summary(event_stream: EventStream, agent_configs: dict):
  45. """Coder agent should render code summary as part of prompt"""
  46. mock_llm = MagicMock()
  47. content = json.dumps({'action': 'finish', 'args': {}})
  48. mock_llm.completion.return_value = {'choices': [{'message': {'content': content}}]}
  49. mock_llm.format_messages_for_llm.return_value = [
  50. {
  51. 'role': 'user',
  52. 'content': "This is a dummy task. This is a dummy summary about this repo. Here's a summary of the codebase, as it relates to this task.",
  53. }
  54. ]
  55. coder_agent = Agent.get_cls('CoderAgent')(
  56. llm=mock_llm, config=agent_configs['CoderAgent']
  57. )
  58. assert coder_agent is not None
  59. # give it some history
  60. task = 'This is a dummy task'
  61. history = list()
  62. history.append(MessageAction(content=task))
  63. summary = 'This is a dummy summary about this repo'
  64. state = State(history=history, inputs={'summary': summary})
  65. coder_agent.step(state)
  66. mock_llm.completion.assert_called_once()
  67. _, kwargs = mock_llm.completion.call_args
  68. prompt_element = kwargs['messages'][0]['content']
  69. if isinstance(prompt_element, dict):
  70. prompt = prompt_element['content']
  71. else:
  72. prompt = prompt_element
  73. assert task in prompt
  74. assert "Here's a summary of the codebase, as it relates to this task" in prompt
  75. assert summary in prompt
  76. def test_coder_agent_without_summary(event_stream: EventStream, agent_configs: dict):
  77. """When there's no codebase_summary available, there shouldn't be any prompt
  78. about 'code summary'
  79. """
  80. mock_llm = MagicMock()
  81. content = json.dumps({'action': 'finish', 'args': {}})
  82. mock_llm.completion.return_value = {'choices': [{'message': {'content': content}}]}
  83. mock_llm.format_messages_for_llm.return_value = [
  84. {
  85. 'role': 'user',
  86. 'content': [
  87. {
  88. 'type': 'text',
  89. 'text': "This is a dummy task. This is a dummy summary about this repo. Here's a summary of the codebase, as it relates to this task.",
  90. }
  91. ],
  92. }
  93. ]
  94. coder_agent = Agent.get_cls('CoderAgent')(
  95. llm=mock_llm, config=agent_configs['CoderAgent']
  96. )
  97. assert coder_agent is not None
  98. # give it some history
  99. task = 'This is a dummy task'
  100. history = list()
  101. history.append(MessageAction(content=task))
  102. # set state without codebase summary
  103. state = State(history=history)
  104. coder_agent.step(state)
  105. mock_llm.completion.assert_called_once()
  106. _, kwargs = mock_llm.completion.call_args
  107. prompt_element = kwargs['messages'][0]['content']
  108. if isinstance(prompt_element, dict):
  109. prompt = prompt_element['content']
  110. else:
  111. prompt = prompt_element
  112. print(f'\n{prompt_element}\n')
  113. assert "Here's a summary of the codebase, as it relates to this task" not in prompt