test_micro_agents.py 3.3 KB

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