Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
feat(service): support custom agent class within the agent service (#1838)
DavdGao653c8f9680c4
diff --git a/examples/agent_service/main.py b/examples/agent_service/main.pyindex 36e4a2572..be3c8175e 100644--- a/examples/agent_service/main.py+++ b/examples/agent_service/main.py@@ -54,7 +54,7 @@ app = create_app(default_mcps=default_mcps,),# Customize your own subagent templates- sub_agent_templates=[+ custom_subagent_templates=[SubAgentTemplate(type="explorer",description=(diff --git a/src/agentscope/app/_app.py b/src/agentscope/app/_app.pyindex 638efc292..3e7403e7a 100644--- a/src/agentscope/app/_app.py+++ b/src/agentscope/app/_app.py@@ -16,6 +16,7 @@ from ._router import (from ._types import AgentMiddlewareFactory, AgentToolFactory, SubAgentTemplatefrom .message_bus import MessageBusfrom .storage import StorageBase+from ..agent import Agentfrom ..credential import CredentialFactory, CredentialBasefrom .._version import __version__@@ -37,7 +38,8 @@ def create_app(extra_middlewares: list[FastAPIMiddleware] | None = None,extra_agent_middlewares: AgentMiddlewareFactory | None = None,extra_agent_tools: AgentToolFactory | None = None,- sub_agent_templates: list[SubAgentTemplate] | None = None,+ custom_subagent_templates: list[SubAgentTemplate] | None = None,+ custom_agent_cls: Type[Agent] | None = None,title: str = "AgentScope",version: str = __version__,) -> FastAPI:@@ -106,7 +108,7 @@ def create_app(availability depends on the caller (per-tenant integrations,user-specific credentials). The returned tools are added tothe workspace-derived tools in the toolkit's ``"basic"`` group.- sub_agent_templates (`list[SubAgentTemplate] | None`, optional):+ custom_subagent_templates (`list[SubAgentTemplate] | None`, optional):Reusable blueprints for sub-agent creation within teams.Each template defines a sub-agent *type* (e.g. ``"researcher"``,``"coder"``) with pre-configured system prompt, context config,@@ -115,6 +117,10 @@ def create_app(``subagent_type`` parameter so the leader agent can route tothe appropriate template. See:class:`~agentscope.app._types.SubAgentTemplate` for details.+ custom_agent_cls (`Type[Agent] | None`, optional):+ A custom :class:`~agentscope.agent.Agent` subclass to use+ when assembling agents. When ``None`` (default), the+ built-in :class:`~agentscope.agent.Agent` is used.title (`str`, defaults to ``"AgentScope"``):OpenAPI title shown in the docs UI.version (`str`, defaults to the package version):@@ -137,7 +143,11 @@ def create_app(app.state.workspace_manager = workspace_managerapp.state.extra_agent_middlewares = extra_agent_middlewaresapp.state.extra_agent_tools = extra_agent_tools- templates = sub_agent_templates or []+ app.state.custom_agent_cls = custom_agent_cls++ # Validate custom sub-agent templates for duplicate types and store in+ # app.state+ templates = custom_subagent_templates or []seen_types: set[str] = set()duplicates: set[str] = set()for t in templates:@@ -148,7 +158,7 @@ def create_app(raise ValueError(f"Duplicate sub_agent_template type(s): {duplicates}",)- app.state.sub_agent_templates = {t.type: t for t in templates}+ app.state.custom_subagent_templates = {t.type: t for t in templates}# Built-in routersfor router in (diff --git a/src/agentscope/app/_lifespan.py b/src/agentscope/app/_lifespan.pyindex 031436528..c99bfef3a 100644--- a/src/agentscope/app/_lifespan.py+++ b/src/agentscope/app/_lifespan.py@@ -69,7 +69,8 @@ async def lifespan(app: FastAPI) -> AsyncIterator[None]:message_bus=message_bus,extra_agent_middlewares=app.state.extra_agent_middlewares,extra_agent_tools=app.state.extra_agent_tools,- sub_agent_templates=app.state.sub_agent_templates,+ custom_subagent_templates=app.state.custom_subagent_templates,+ custom_agent_cls=app.state.custom_agent_cls,)app.state.chat_service = chat_servicediff --git a/src/agentscope/app/_service/_chat.py b/src/agentscope/app/_service/_chat.pyindex a1a1d2ee9..060372ca5 100644--- a/src/agentscope/app/_service/_chat.py+++ b/src/agentscope/app/_service/_chat.py@@ -64,7 +64,8 @@ class ChatService:message_bus: MessageBus,extra_agent_middlewares: AgentMiddlewareFactory | None = None,extra_agent_tools: AgentToolFactory | None = None,- sub_agent_templates: dict[str, SubAgentTemplate] | None = None,+ custom_subagent_templates: dict[str, SubAgentTemplate] | None = None,+ custom_agent_cls: type[Agent] | None = None,) -> None:"""Initialize chat service.@@ -94,12 +95,15 @@ class ChatService:extra_agent_tools (`AgentToolFactory | None`, optional):Async factory invoked at every chat turn to produceuser/session-specific tools to register in the toolkit.- sub_agent_templates (`dict[str, SubAgentTemplate] | None`, \+ custom_subagent_templates (`dict[str, SubAgentTemplate] | None`,\optional):Sub-agent template registry, keyed by template type.Passed through to :func:`get_toolkit` so that``AgentCreate`` can route to the appropriate templatewhen a ``subagent_type`` is specified.+ custom_agent_cls (`type[Agent] | None`, optional):+ Custom :class:`Agent` subclass for assembling agents.+ Falls back to :class:`Agent` when ``None``."""self._storage = storageself._workspace_manager = workspace_manager@@ -108,7 +112,8 @@ class ChatService:self._message_bus = message_busself._extra_agent_middlewares = extra_agent_middlewaresself._extra_agent_tools = extra_agent_tools- self._sub_agent_templates = sub_agent_templates+ self._sub_agent_templates = custom_subagent_templates+ self._agent_cls = custom_agent_cls or Agentasync def run(self,@@ -282,7 +287,7 @@ class ChatService:# ----------------------------------------------------------------agent_state = session_record.stateagent_state.session_id = session_id- agent = Agent(+ agent = self._agent_cls(name=agent_record.data.name,system_prompt=agent_record.data.system_prompt,model=model,