Engel Nyst 446eaec1e6
Refactor config to dataclasses (#1552)
* mypy is invaluable

* fix config, add test

* Add new-style toml support

* add singleton, small doc fixes

* fix some cases of loading toml, clean up, try to make it clearer

* Add defaults_dict for UI

* allow config to be mutable
error handling
fix toml parsing

* remove debug stuff

* Adapt Makefile

* Add defaults for temperature and top_p

* update to CodeActAgent

* comments

* fix unit tests

* implement groups of llm settings (CLI)

* fix merge issue

* small fix sandboxes, small refactoring

* adapt LLM init to accept overrides at runtime

* reading config is enough

* Encapsulate minimally embeddings initialization

* agent bug fix; fix tests

* fix sandboxes tests

* refactor globals in sandboxes to properties
2024-05-09 22:48:29 +02:00

93 lines
2.7 KiB
Python

import os
import pathlib
from dataclasses import dataclass
from typing import TYPE_CHECKING
from opendevin.core.config import config
from opendevin.core.schema import ActionType
from .action import Action
if TYPE_CHECKING:
from opendevin.controller import AgentController
from opendevin.events.observation import CmdOutputObservation, Observation
from opendevin.events.observation import IPythonRunCellObservation
@dataclass
class CmdRunAction(Action):
command: str
background: bool = False
thought: str = ''
action: str = ActionType.RUN
async def run(self, controller: 'AgentController') -> 'Observation':
return controller.action_manager.run_command(self.command, self.background)
@property
def message(self) -> str:
return f'Running command: {self.command}'
def __str__(self) -> str:
ret = '**CmdRunAction**\n'
if self.thought:
ret += f'THOUGHT:{self.thought}\n'
ret += f'COMMAND:\n{self.command}'
return ret
@dataclass
class CmdKillAction(Action):
id: int
thought: str = ''
action: str = ActionType.KILL
async def run(self, controller: 'AgentController') -> 'CmdOutputObservation':
return controller.action_manager.kill_command(self.id)
@property
def message(self) -> str:
return f'Killing command: {self.id}'
def __str__(self) -> str:
return f'**CmdKillAction**\n{self.id}'
@dataclass
class IPythonRunCellAction(Action):
code: str
thought: str = ''
action: str = ActionType.RUN_IPYTHON
async def run(self, controller: 'AgentController') -> 'IPythonRunCellObservation':
# echo "import math" | execute_cli
# write code to a temporary file and pass it to `execute_cli` via stdin
tmp_filepath = os.path.join(
config.workspace_base, '.tmp', '.ipython_execution_tmp.py'
)
pathlib.Path(os.path.dirname(tmp_filepath)).mkdir(parents=True, exist_ok=True)
with open(tmp_filepath, 'w') as tmp_file:
tmp_file.write(self.code)
tmp_filepath_inside_sandbox = os.path.join(
config.workspace_mount_path_in_sandbox,
'.tmp',
'.ipython_execution_tmp.py',
)
obs = controller.action_manager.run_command(
f'execute_cli < {tmp_filepath_inside_sandbox}', background=False
)
return IPythonRunCellObservation(content=obs.content, code=self.code)
def __str__(self) -> str:
ret = '**IPythonRunCellAction**\n'
if self.thought:
ret += f'THOUGHT:{self.thought}\n'
ret += f'CODE:\n{self.code}'
return ret
@property
def message(self) -> str:
return f'Running Python code interactively: {self.code}'