mirror of
https://github.com/OpenHands/OpenHands.git
synced 2026-03-22 13:47:19 +08:00
This PR fixes #1897. In addition, this PR fixes and tweaks a few micro-agents. For the first time, I am able to use ManagerAgent to complete test_write_simple_script and test_edits tasks in integration tests, so this PR also adds ManagerAgent as part of integration tests. test_write_simple_script involves delegation to CoderAgent while test_edits involves delegation to TypoFixerAgent. Also for the first time, I am able to use DelegateAgent to complete test_write_simple_script and test_edits tasks in integration tests, so this PR also adds DelegateAgent as part of integration tests. It involves delegation to StudyRepoForTaskAgent, CoderAgent and VerifierAgent. This PR is a blocker for #1735 and likely #1945.
80 lines
2.8 KiB
Python
80 lines
2.8 KiB
Python
import base64
|
|
import pickle
|
|
from dataclasses import dataclass, field
|
|
|
|
from opendevin.controller.state.task import RootTask
|
|
from opendevin.core.logger import opendevin_logger as logger
|
|
from opendevin.core.metrics import Metrics
|
|
from opendevin.core.schema import AgentState
|
|
from opendevin.events.action import (
|
|
Action,
|
|
MessageAction,
|
|
)
|
|
from opendevin.events.observation import (
|
|
CmdOutputObservation,
|
|
Observation,
|
|
)
|
|
from opendevin.storage import get_file_store
|
|
|
|
RESUMABLE_STATES = [
|
|
AgentState.RUNNING,
|
|
AgentState.PAUSED,
|
|
AgentState.AWAITING_USER_INPUT,
|
|
AgentState.FINISHED,
|
|
]
|
|
|
|
|
|
@dataclass
|
|
class State:
|
|
root_task: RootTask = field(default_factory=RootTask)
|
|
iteration: int = 0
|
|
max_iterations: int = 100
|
|
# number of characters we have sent to and received from LLM so far for current task
|
|
num_of_chars: int = 0
|
|
background_commands_obs: list[CmdOutputObservation] = field(default_factory=list)
|
|
history: list[tuple[Action, Observation]] = field(default_factory=list)
|
|
updated_info: list[tuple[Action, Observation]] = field(default_factory=list)
|
|
inputs: dict = field(default_factory=dict)
|
|
outputs: dict = field(default_factory=dict)
|
|
error: str | None = None
|
|
agent_state: AgentState = AgentState.LOADING
|
|
resume_state: AgentState | None = None
|
|
metrics: Metrics = Metrics()
|
|
# root agent has level 0, and every delegate increases the level by one
|
|
delegate_level: int = 0
|
|
|
|
def save_to_session(self, sid: str):
|
|
fs = get_file_store()
|
|
pickled = pickle.dumps(self)
|
|
encoded = base64.b64encode(pickled).decode('utf-8')
|
|
try:
|
|
fs.write(f'sessions/{sid}/agent_state.pkl', encoded)
|
|
except Exception as e:
|
|
logger.error(f'Failed to save state to session: {e}')
|
|
raise e
|
|
|
|
@staticmethod
|
|
def restore_from_session(sid: str) -> 'State':
|
|
fs = get_file_store()
|
|
try:
|
|
encoded = fs.read(f'sessions/{sid}/agent_state.pkl')
|
|
pickled = base64.b64decode(encoded)
|
|
state = pickle.loads(pickled)
|
|
except Exception as e:
|
|
logger.error(f'Failed to restore state from session: {e}')
|
|
raise e
|
|
if state.agent_state in RESUMABLE_STATES:
|
|
state.resume_state = state.agent_state
|
|
else:
|
|
state.resume_state = None
|
|
state.agent_state = AgentState.LOADING
|
|
return state
|
|
|
|
def get_current_user_intent(self):
|
|
# TODO: this is used to understand the user's main goal, but it's possible
|
|
# the latest message is an interruption. We should look for a space where
|
|
# the agent goes to FINISHED, and then look for the next user message.
|
|
for action, obs in reversed(self.history):
|
|
if isinstance(action, MessageAction) and action.source == 'user':
|
|
return action.content
|