test_micro_agents.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 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 import EventSource
  12. from openhands.events.action import MessageAction
  13. from openhands.events.stream import EventStream
  14. from openhands.memory.history import ShortTermHistory
  15. from openhands.storage import get_file_store
  16. @pytest.fixture
  17. def temp_dir(tmp_path_factory: TempPathFactory) -> str:
  18. return str(tmp_path_factory.mktemp('test_micro_agents'))
  19. @pytest.fixture
  20. def event_stream(temp_dir):
  21. file_store = get_file_store('local', temp_dir)
  22. event_stream = EventStream('asdf', file_store)
  23. yield event_stream
  24. # clear after each test
  25. event_stream.clear()
  26. @pytest.fixture
  27. def agent_configs():
  28. return {
  29. 'CoderAgent': AgentConfig(memory_enabled=True),
  30. 'PlannerAgent': AgentConfig(memory_enabled=True),
  31. }
  32. def test_all_agents_are_loaded():
  33. assert all_microagents is not None
  34. assert len(all_microagents) > 1
  35. base = os.path.join('agenthub', 'micro')
  36. full_path = os.path.dirname(__file__) + '/../../' + base
  37. agent_names = set()
  38. for root, _, files in os.walk(full_path):
  39. for file in files:
  40. if file == 'agent.yaml':
  41. file_path = os.path.join(root, file)
  42. with open(file_path, 'r') as yaml_file:
  43. data = yaml.safe_load(yaml_file)
  44. agent_names.add(data['name'])
  45. assert agent_names == set(all_microagents.keys())
  46. def test_coder_agent_with_summary(event_stream: EventStream, agent_configs: dict):
  47. """Coder agent should render code summary as part of prompt"""
  48. mock_llm = MagicMock()
  49. content = json.dumps({'action': 'finish', 'args': {}})
  50. mock_llm.completion.return_value = {'choices': [{'message': {'content': content}}]}
  51. coder_agent = Agent.get_cls('CoderAgent')(
  52. llm=mock_llm, config=agent_configs['CoderAgent']
  53. )
  54. assert coder_agent is not None
  55. task = 'This is a dummy task'
  56. history = ShortTermHistory()
  57. history.set_event_stream(event_stream)
  58. event_stream.add_event(MessageAction(content=task), EventSource.USER)
  59. summary = 'This is a dummy summary about this repo'
  60. state = State(history=history, inputs={'summary': summary})
  61. coder_agent.step(state)
  62. mock_llm.completion.assert_called_once()
  63. _, kwargs = mock_llm.completion.call_args
  64. prompt = kwargs['messages'][0]['content'][0]['text']
  65. assert task in prompt
  66. assert "Here's a summary of the codebase, as it relates to this task" in prompt
  67. assert summary in prompt
  68. def test_coder_agent_without_summary(event_stream: EventStream, agent_configs: dict):
  69. """When there's no codebase_summary available, there shouldn't be any prompt
  70. about 'code summary'
  71. """
  72. mock_llm = MagicMock()
  73. content = json.dumps({'action': 'finish', 'args': {}})
  74. mock_llm.completion.return_value = {'choices': [{'message': {'content': content}}]}
  75. coder_agent = Agent.get_cls('CoderAgent')(
  76. llm=mock_llm, config=agent_configs['CoderAgent']
  77. )
  78. assert coder_agent is not None
  79. task = 'This is a dummy task'
  80. history = ShortTermHistory()
  81. history.set_event_stream(event_stream)
  82. event_stream.add_event(MessageAction(content=task), EventSource.USER)
  83. # set state without codebase summary
  84. state = State(history=history)
  85. coder_agent.step(state)
  86. mock_llm.completion.assert_called_once()
  87. _, kwargs = mock_llm.completion.call_args
  88. prompt = kwargs['messages'][0]['content'][0]['text']
  89. assert task in prompt
  90. assert "Here's a summary of the codebase, as it relates to this task" not in prompt