test_micro_agents.py 3.1 KB

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