SubagentsMiddleware
public class SubagentsMiddleware implements HarnessRuntimeMiddleware
io/agentscope/harness/agent/middleware/SubagentsMiddleware.java:75–728Subagents 中间件,在智能体执行流程中提供横切扩展。位于 io.agentscope.harness.agent.middleware 包,实现 HarnessRuntimeMiddleware。核心成员包括 enableAgentGenerateTool、getTaskRepository、wireMessageBus、setGatewayBridge、getAgentManager、getTools。
Middleware that provides the managed subagent mechanism.
In default mode (standalone harness setup), this middleware creates an
AgentSpawnTool backed by a DefaultAgentManager. In session mode
(orchestrated via AgentBootstrap), an external tool (typically SessionsTool)
is injected, replacing the default AgentSpawnTool.
Responsibilities:
-
Exposes the subagent tool and
TaskToolas agent tools (callers query via#getTools()and register them on the toolkit at orchestration time). -
On every
#onAgent, reloads subagent declarations from the workspace filesystem (namespace-scoped) to support per-user subagent isolation. -
Prepends rich subagent usage guidance and current async task summary to the leading SYSTEM message of every
ReasoningInput. Because the framework rebuilds the SYSTEM message from a frozen base each iteration, this is safe — content never accumulates across iterations.
构造器摘要Constructor Summary
方法摘要Method Summary
构造器详情Constructor Detail
SubagentsMiddleware
public SubagentsMiddleware(List<SubagentEntry> entries, TaskRepository taskRepository, WorkspaceManager workspaceManager, AbstractFilesystem filesystem, Path mainWorkspace, Function<SubagentDeclaration, SubagentFactory> factoryBuilder)
初始化 SubagentsMiddleware 实例。
Default mode: creates AgentSpawnTool + DefaultAgentManager internally.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
entries | List<SubagentEntry> | 条目(类型 `List`)。The entries. |
taskRepository | TaskRepository | 任务仓库(类型 `TaskRepository`)。The task repository. |
workspaceManager | WorkspaceManager | 工作区管理器(类型 `WorkspaceManager`)。The workspace manager. |
filesystem | AbstractFilesystem | 文件系统(类型 `AbstractFilesystem`)。The filesystem. |
mainWorkspace | Path | 主工作区(类型 `Path`)。The main workspace. |
factoryBuilder | Function<SubagentDeclaration, SubagentFactory> | 工厂构建器(类型 `Function`)。The factory builder. |
io/agentscope/harness/agent/middleware/SubagentsMiddleware.java:200–219SubagentsMiddleware
public SubagentsMiddleware(List<SubagentEntry> entries, TaskRepository taskRepository, WorkspaceManager workspaceManager)
初始化 SubagentsMiddleware 实例。
Default mode without dynamic reload support.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
entries | List<SubagentEntry> | 条目(类型 `List`)。The entries. |
taskRepository | TaskRepository | 任务仓库(类型 `TaskRepository`)。The task repository. |
workspaceManager | WorkspaceManager | 工作区管理器(类型 `WorkspaceManager`)。The workspace manager. |
io/agentscope/harness/agent/middleware/SubagentsMiddleware.java:224–229SubagentsMiddleware
public SubagentsMiddleware(List<SubagentEntry> entries, Object externalSubagentTool, TaskRepository taskRepository)
初始化 SubagentsMiddleware 实例。
AgentStateStore mode: uses the externally provided tool (typically SessionsTool).
| 名称Name | 类型Type | 说明Description |
|---|---|---|
entries | List<SubagentEntry> | 条目(类型 `List`)。The entries. |
externalSubagentTool | Object | 外部子智能体工具(类型 `Object`)。The external subagent tool. |
taskRepository | TaskRepository | 任务仓库(类型 `TaskRepository`)。The task repository. |
io/agentscope/harness/agent/middleware/SubagentsMiddleware.java:234–249方法详情Method Detail
enableAgentGenerateTool
public SubagentsMiddleware enableAgentGenerateTool(SubagentSpecGenerator generator)
启用 enableAgentGenerateTool。
Enables the agent_generate tool, which lets the LLM author new subagent specs from a description.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
generator | SubagentSpecGenerator | generator(类型 `SubagentSpecGenerator`)。a `SubagentSpecGenerator` backed by the model that should author specs |
SubagentsMiddlewareio/agentscope/harness/agent/middleware/SubagentsMiddleware.java:263–270getTaskRepository
public TaskRepository getTaskRepository()
获取任务仓库。
Returns the TaskRepository used by this middleware.
TaskRepositoryio/agentscope/harness/agent/middleware/SubagentsMiddleware.java:273–275wireMessageBus
public SubagentsMiddleware wireMessageBus(io.agentscope.harness.agent.bus.MessageBus messageBus, String agentId)
执行 wireMessageBus 方法。
Wires a io.agentscope.harness.agent.bus.MessageBus so that background task completions are pushed to the session inbox and a wakeup signal is enqueued.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
messageBus | io.agentscope.harness.agent.bus.MessageBus | 应用消息总线。the application message bus |
agentId | String | 智能体标识(类型 `String`)。the parent agent id (for wakeup routing) |
SubagentsMiddlewareio/agentscope/harness/agent/middleware/SubagentsMiddleware.java:290–336setGatewayBridge
public SubagentsMiddleware setGatewayBridge(io.agentscope.harness.agent.gateway.SubagentGatewayBridge bridge)
设置 setGatewayBridge。
Wires a gateway bridge into the internal AgentSpawnTool, enabling spawned subagents to be exposed as user-addressable threads.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
bridge | io.agentscope.harness.agent.gateway.SubagentGatewayBridge | bridge(类型 `io.agentscope.harness.agent.gateway.SubagentGatewayBridge`)。the bridge implementation (typically obtained from `io.agentscope.harness.agent.gateway.GatewayBootstrap#gatewayBridge()`) |
SubagentsMiddlewareio/agentscope/harness/agent/middleware/SubagentsMiddleware.java:346–362getAgentManager
public DefaultAgentManager getAgentManager()
获取智能体管理器。
Returns the internal DefaultAgentManager that can re-materialize subagents, or null in session mode (external tool).
DefaultAgentManagerio/agentscope/harness/agent/middleware/SubagentsMiddleware.java:369–371getTools
public List<Object> getTools()
获取工具。
Returns the tool instances this middleware contributes to the agent toolkit.
List<Object>io/agentscope/harness/agent/middleware/SubagentsMiddleware.java:380–389onAgent
public Flux<AgentEvent> onAgent(Agent agent, RuntimeContext ctx, AgentInput input, Function<AgentInput, Flux<AgentEvent>> next)
处理智能体事件。
Performs the on agent operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
agent | Agent | 智能体(类型 `Agent`)。The agent. |
ctx | RuntimeContext | 运行时上下文(类型 `RuntimeContext`)。The ctx. |
input | AgentInput | 输入消息(类型 `AgentInput`)。The input. |
next | Function<AgentInput, Flux<AgentEvent>> | 下一个(类型 `Function >`)。The next. |
Flux<AgentEvent>io/agentscope/harness/agent/middleware/SubagentsMiddleware.java:391–401onReasoning
public Flux<AgentEvent> onReasoning(Agent agent, RuntimeContext ctx, ReasoningInput input, Function<ReasoningInput, Flux<AgentEvent>> next)
处理推理事件。
Performs the on reasoning operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
agent | Agent | 智能体(类型 `Agent`)。The agent. |
ctx | RuntimeContext | 运行时上下文(类型 `RuntimeContext`)。The ctx. |
input | ReasoningInput | 输入消息(类型 `ReasoningInput`)。The input. |
next | Function<ReasoningInput, Flux<AgentEvent>> | 下一个(类型 `Function >`)。The next. |
Flux<AgentEvent>io/agentscope/harness/agent/middleware/SubagentsMiddleware.java:403–481renderSubagentSection
public static String renderSubagentSection(List<SubagentEntry> entries, boolean isSessionMode)
渲染 renderSubagentSection。
Renders the ## Subagents system-prompt section for the supplied entries.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
entries | List<SubagentEntry> | 条目(类型 `List`)。The entries. |
isSessionMode | boolean | 是会话模式(类型 `boolean`)。The is session mode. |
Stringio/agentscope/harness/agent/middleware/SubagentsMiddleware.java:653–663buildTaskSummary
public static String buildTaskSummary(TaskRepository repo, RuntimeContext ctx)
构建任务摘要。
Builds a concise summary of in-flight tasks for the current session, or null if none.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
repo | TaskRepository | 仓库(类型 `TaskRepository`)。The repo. |
ctx | RuntimeContext | 运行时上下文(类型 `RuntimeContext`)。The ctx. |
Stringio/agentscope/harness/agent/middleware/SubagentsMiddleware.java:685–727