test_micro_agents.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.action import MessageAction
  9. from opendevin.events.observation import NullObservation
  10. from opendevin.events.serialization import EventSource
  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.completion.return_value = {'choices': [{'message': {'content': content}}]}
  29. coder_agent = Agent.get_cls('CoderAgent')(llm=mock_llm)
  30. assert coder_agent is not None
  31. task = 'This is a dummy task'
  32. history = [(MessageAction(content=task), NullObservation(''))]
  33. history[0][0]._source = EventSource.USER
  34. summary = 'This is a dummy summary about this repo'
  35. state = State(history=history, inputs={'summary': summary})
  36. coder_agent.step(state)
  37. mock_llm.completion.assert_called_once()
  38. _, kwargs = mock_llm.completion.call_args
  39. prompt = kwargs['messages'][0]['content']
  40. assert task in prompt
  41. assert "Here's a summary of the codebase, as it relates to this task" in prompt
  42. assert summary in prompt
  43. def test_coder_agent_without_summary():
  44. """
  45. When there's no codebase_summary available, there shouldn't be any prompt
  46. about 'code summary'
  47. """
  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')(llm=mock_llm)
  52. assert coder_agent is not None
  53. task = 'This is a dummy task'
  54. history = [(MessageAction(content=task), NullObservation(''))]
  55. history[0][0]._source = EventSource.USER
  56. state = State(history=history)
  57. coder_agent.step(state)
  58. mock_llm.completion.assert_called_once()
  59. _, kwargs = mock_llm.completion.call_args
  60. prompt = kwargs['messages'][0]['content']
  61. assert task in prompt
  62. assert "Here's a summary of the codebase, as it relates to this task" not in prompt