π OpenHands: AI-Driven Development
Add ability to customize configuration model on per-agent basis (#8576)
Chase4ee269c3f7dd
diff --git a/openhands/controller/agent.py b/openhands/controller/agent.pyindex ccc178af5a..7613b1ce23 100644--- a/openhands/controller/agent.py+++ b/openhands/controller/agent.py@@ -5,12 +5,12 @@ from typing import TYPE_CHECKINGif TYPE_CHECKING:from openhands.controller.state.state import State- from openhands.core.config import AgentConfigfrom openhands.events.action import Actionfrom openhands.events.action.message import SystemMessageActionfrom openhands.utils.prompt import PromptManagerfrom litellm import ChatCompletionToolParam+from openhands.core.config import AgentConfigfrom openhands.core.exceptions import (AgentAlreadyRegisteredError,AgentNotRegisteredError,@@ -33,10 +33,13 @@ class Agent(ABC):_registry: dict[str, type['Agent']] = {}sandbox_plugins: list[PluginRequirement] = []+ config_model: type[AgentConfig] = AgentConfig+ """Class field that specifies the config model to use for the agent. Subclasses may override with a derived config model if needed."""+def __init__(self,llm: LLM,- config: 'AgentConfig',+ config: AgentConfig,):self.llm = llmself.config = configdiff --git a/openhands/core/config/agent_config.py b/openhands/core/config/agent_config.pyindex 0fe459000a..26870da5cc 100644--- a/openhands/core/config/agent_config.py+++ b/openhands/core/config/agent_config.py@@ -5,6 +5,7 @@ from pydantic import BaseModel, Field, ValidationErrorfrom openhands.core.config.condenser_config import CondenserConfig, NoOpCondenserConfigfrom openhands.core.config.extended_config import ExtendedConfigfrom openhands.core.logger import openhands_logger as logger+from openhands.utils.import_utils import get_implclass AgentConfig(BaseModel):@@ -98,7 +99,27 @@ class AgentConfig(BaseModel):try:# Merge base config with overridesmerged = {**base_config.model_dump(), **overrides}- custom_config = cls.model_validate(merged)+ if merged.get('classpath'):+ # if an explicit classpath is given, try to load it and look up its config model class+ from openhands.controller.agent import Agent++ try:+ agent_cls = get_impl(Agent, merged.get('classpath'))+ custom_config = agent_cls.config_model.model_validate(merged)+ except Exception as e:+ logger.warning(+ f"Failed to load custom agent class [{merged.get('classpath')}]: {e}. Using default config model."+ )+ custom_config = cls.model_validate(merged)+ else:+ # otherwise, try to look up the agent class by name (i.e. if it's a built-in)+ # if that fails, just use the default AgentConfig class.+ try:+ agent_cls = Agent.get_cls(name)+ custom_config = agent_cls.config_model.model_validate(merged)+ except Exception as e:+ # otherwise, just fall back to the default config model+ custom_config = cls.model_validate(merged)agent_mapping[name] = custom_configexcept ValidationError as e:logger.warning(