|
1 | | -"""Module for a Dummy agent.""" |
| 1 | +import time |
| 2 | +from typing import List, TypedDict |
2 | 3 |
|
3 | | -from typing import List |
4 | | - |
5 | | -from opendevin.action import Action |
6 | | -from opendevin.action.base import NullAction |
| 4 | +from opendevin.action import ( |
| 5 | + Action, |
| 6 | + AddTaskAction, |
| 7 | + AgentFinishAction, |
| 8 | + AgentRecallAction, |
| 9 | + AgentThinkAction, |
| 10 | + BrowseURLAction, |
| 11 | + CmdRunAction, |
| 12 | + FileReadAction, |
| 13 | + FileWriteAction, |
| 14 | + ModifyTaskAction, |
| 15 | +) |
7 | 16 | from opendevin.agent import Agent |
8 | | -from opendevin.controller.agent_controller import AgentController |
9 | | -from opendevin.observation.base import NullObservation, Observation |
| 17 | +from opendevin.llm.llm import LLM |
| 18 | +from opendevin.observation import ( |
| 19 | + AgentRecallObservation, |
| 20 | + CmdOutputObservation, |
| 21 | + FileReadObservation, |
| 22 | + FileWriteObservation, |
| 23 | + NullObservation, |
| 24 | + Observation, |
| 25 | +) |
10 | 26 | from opendevin.state import State |
11 | 27 |
|
| 28 | +""" |
| 29 | +FIXME: There are a few problems this surfaced |
| 30 | +* FileWrites seem to add an unintended newline at the end of the file |
| 31 | +* command_id is sometimes a number, sometimes a string |
| 32 | +* Why isn't the output of the background command split between two steps? |
| 33 | +* Browser not working |
| 34 | +""" |
| 35 | + |
| 36 | +ActionObs = TypedDict('ActionObs', {'action': Action, 'observations': List[Observation]}) |
| 37 | + |
| 38 | +BACKGROUND_CMD = 'echo "This is in the background" && sleep .1 && echo "This too"' |
| 39 | + |
12 | 40 |
|
13 | 41 | class DummyAgent(Agent): |
14 | | - """A dummy agent that does nothing but can be used in testing.""" |
| 42 | + """ |
| 43 | + The DummyAgent is used for e2e testing. It just sends the same set of actions deterministically, |
| 44 | + without making any LLM calls. |
| 45 | + """ |
15 | 46 |
|
16 | | - async def run(self, controller: AgentController) -> Observation: |
17 | | - return NullObservation('') |
| 47 | + def __init__(self, llm: LLM): |
| 48 | + super().__init__(llm) |
| 49 | + self.steps: List[ActionObs] = [{ |
| 50 | + 'action': AddTaskAction(parent='0', goal='check the current directory'), |
| 51 | + 'observations': [NullObservation('')], |
| 52 | + }, { |
| 53 | + 'action': AddTaskAction(parent='0.0', goal='run ls'), |
| 54 | + 'observations': [NullObservation('')], |
| 55 | + }, { |
| 56 | + 'action': ModifyTaskAction(id='0.0', state='in_progress'), |
| 57 | + 'observations': [NullObservation('')], |
| 58 | + }, { |
| 59 | + 'action': AgentThinkAction(thought='Time to get started!'), |
| 60 | + 'observations': [NullObservation('')], |
| 61 | + }, { |
| 62 | + 'action': CmdRunAction(command='echo "foo"'), |
| 63 | + 'observations': [CmdOutputObservation('foo', command_id=-1, command='echo "foo"')], |
| 64 | + }, { |
| 65 | + 'action': FileWriteAction(content='echo "Hello, World!"', path='hello.sh'), |
| 66 | + 'observations': [FileWriteObservation('', path='hello.sh')], |
| 67 | + }, { |
| 68 | + 'action': FileReadAction(path='hello.sh'), |
| 69 | + 'observations': [FileReadObservation('echo "Hello, World!"\n', path='hello.sh')], |
| 70 | + }, { |
| 71 | + 'action': CmdRunAction(command='bash hello.sh'), |
| 72 | + 'observations': [CmdOutputObservation('Hello, World!', command_id=-1, command='bash hello.sh')], |
| 73 | + }, { |
| 74 | + 'action': CmdRunAction(command=BACKGROUND_CMD, background=True), |
| 75 | + 'observations': [ |
| 76 | + CmdOutputObservation('Background command started. To stop it, send a `kill` action with id 42', command_id='42', command=BACKGROUND_CMD), # type: ignore[arg-type] |
| 77 | + CmdOutputObservation('This is in the background\nThis too\n', command_id='42', command=BACKGROUND_CMD), # type: ignore[arg-type] |
| 78 | + ] |
| 79 | + }, { |
| 80 | + 'action': AgentRecallAction(query='who am I?'), |
| 81 | + 'observations': [ |
| 82 | + AgentRecallObservation('', memories=['I am a computer.']), |
| 83 | + # CmdOutputObservation('This too\n', command_id='42', command=BACKGROUND_CMD), |
| 84 | + ], |
| 85 | + }, { |
| 86 | + 'action': BrowseURLAction(url='https://google.com'), |
| 87 | + 'observations': [ |
| 88 | + # BrowserOutputObservation('<html></html>', url='https://google.com', screenshot=""), |
| 89 | + ], |
| 90 | + }, { |
| 91 | + 'action': AgentFinishAction(), |
| 92 | + 'observations': [], |
| 93 | + }] |
18 | 94 |
|
19 | 95 | def step(self, state: State) -> Action: |
20 | | - return NullAction('') |
| 96 | + time.sleep(0.1) |
| 97 | + if state.iteration > 0: |
| 98 | + prev_step = self.steps[state.iteration - 1] |
| 99 | + if 'observations' in prev_step: |
| 100 | + expected_observations = prev_step['observations'] |
| 101 | + hist_start = len(state.history) - len(expected_observations) |
| 102 | + for i in range(len(expected_observations)): |
| 103 | + hist_obs = state.history[hist_start + i][1].to_dict() |
| 104 | + expected_obs = expected_observations[i].to_dict() |
| 105 | + if 'command_id' in hist_obs['extras'] and hist_obs['extras']['command_id'] != -1: |
| 106 | + del hist_obs['extras']['command_id'] |
| 107 | + hist_obs['content'] = '' |
| 108 | + if 'command_id' in expected_obs['extras'] and expected_obs['extras']['command_id'] != -1: |
| 109 | + del expected_obs['extras']['command_id'] |
| 110 | + expected_obs['content'] = '' |
| 111 | + if hist_obs != expected_obs: |
| 112 | + print('\nactual', hist_obs) |
| 113 | + print('\nexpect', expected_obs) |
| 114 | + assert hist_obs == expected_obs, f'Expected observation {expected_obs}, got {hist_obs}' |
| 115 | + return self.steps[state.iteration]['action'] |
21 | 116 |
|
22 | 117 | def search_memory(self, query: str) -> List[str]: |
23 | | - return [] |
| 118 | + return ['I am a computer.'] |
0 commit comments