test_micro_agents.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import json
  2. import os
  3. from unittest.mock import MagicMock
  4. import yaml
  5. from agenthub.micro.registry import all_microagents
  6. from opendevin.controller.agent import Agent
  7. from opendevin.controller.state.state import State
  8. from opendevin.events import EventSource
  9. from opendevin.events.action import MessageAction
  10. from opendevin.events.observation import NullObservation
  11. def test_all_agents_are_loaded():
  12. full_path = os.path.join('agenthub', 'micro')
  13. agent_names = set()
  14. for root, _, files in os.walk(full_path):
  15. for file in files:
  16. if file == 'agent.yaml':
  17. file_path = os.path.join(root, file)
  18. with open(file_path, 'r') as yaml_file:
  19. data = yaml.safe_load(yaml_file)
  20. agent_names.add(data['name'])
  21. assert agent_names == set(all_microagents.keys())
  22. def test_coder_agent_with_summary():
  23. """
  24. Coder agent should render code summary as part of prompt
  25. """
  26. mock_llm = MagicMock()
  27. content = json.dumps({'action': 'finish', 'args': {}})
  28. mock_llm.do_completion.return_value = {
  29. 'choices': [{'message': {'content': content}}]
  30. }
  31. coder_agent = Agent.get_cls('CoderAgent')(llm=mock_llm)
  32. assert coder_agent is not None
  33. task = 'This is a dummy task'
  34. history = [(MessageAction(content=task), NullObservation(''))]
  35. history[0][0]._source = EventSource.USER
  36. summary = 'This is a dummy summary about this repo'
  37. state = State(history=history, inputs={'summary': summary})
  38. coder_agent.step(state)
  39. mock_llm.do_completion.assert_called_once()
  40. _, kwargs = mock_llm.do_completion.call_args
  41. prompt = kwargs['messages'][0]['content']
  42. assert task in prompt
  43. assert "Here's a summary of the codebase, as it relates to this task" in prompt
  44. assert summary in prompt
  45. def test_coder_agent_without_summary():
  46. """
  47. When there's no codebase_summary available, there shouldn't be any prompt
  48. about 'code summary'
  49. """
  50. mock_llm = MagicMock()
  51. content = json.dumps({'action': 'finish', 'args': {}})
  52. mock_llm.do_completion.return_value = {
  53. 'choices': [{'message': {'content': content}}]
  54. }
  55. coder_agent = Agent.get_cls('CoderAgent')(llm=mock_llm)
  56. assert coder_agent is not None
  57. task = 'This is a dummy task'
  58. history = [(MessageAction(content=task), NullObservation(''))]
  59. history[0][0]._source = EventSource.USER
  60. state = State(history=history)
  61. coder_agent.step(state)
  62. mock_llm.do_completion.assert_called_once()
  63. _, kwargs = mock_llm.do_completion.call_args
  64. prompt = kwargs['messages'][0]['content']
  65. assert task in prompt
  66. assert "Here's a summary of the codebase, as it relates to this task" not in prompt