Class SubagentsMiddleware
- All Implemented Interfaces:
io.agentscope.core.middleware.MiddlewareBase,HarnessRuntimeMiddleware
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 viagetTools()and register them on the toolkit at orchestration time). - On every
onAgent(io.agentscope.core.agent.Agent, io.agentscope.core.agent.RuntimeContext, io.agentscope.core.middleware.AgentInput, java.util.function.Function<io.agentscope.core.middleware.AgentInput, reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent>>), 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
ConstructorsConstructorDescriptionSubagentsMiddleware(List<SubagentEntry> entries, TaskRepository taskRepository, WorkspaceManager workspaceManager) Default mode without dynamic reload support.SubagentsMiddleware(List<SubagentEntry> entries, TaskRepository taskRepository, WorkspaceManager workspaceManager, AbstractFilesystem filesystem, Path mainWorkspace, Function<SubagentDeclaration, SubagentFactory> factoryBuilder) Default mode: createsAgentSpawnTool+DefaultAgentManagerinternally.SubagentsMiddleware(List<SubagentEntry> entries, Object externalSubagentTool, TaskRepository taskRepository) AgentStateStore mode: uses the externally provided tool (typicallySessionsTool). -
Method Summary
Modifier and TypeMethodDescriptionstatic StringbuildTaskSummary(TaskRepository repo, io.agentscope.core.agent.RuntimeContext ctx) Builds a concise summary of in-flight tasks for the current session, ornullif none.enableAgentGenerateTool(SubagentSpecGenerator generator) Enables theagent_generatetool, which lets the LLM author new subagent specs from a description.Returns the internalDefaultAgentManagerthat can re-materialize subagents, ornullin session mode (external tool).Returns theTaskRepositoryused by this middleware.getTools()Returns the tool instances this middleware contributes to the agent toolkit.reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent>onAgent(io.agentscope.core.agent.Agent agent, io.agentscope.core.agent.RuntimeContext ctx, io.agentscope.core.middleware.AgentInput input, Function<io.agentscope.core.middleware.AgentInput, reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent>> next) reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent>onReasoning(io.agentscope.core.agent.Agent agent, io.agentscope.core.agent.RuntimeContext ctx, io.agentscope.core.middleware.ReasoningInput input, Function<io.agentscope.core.middleware.ReasoningInput, reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent>> next) static StringrenderSubagentSection(List<SubagentEntry> entries, boolean isSessionMode) Renders the## Subagentssystem-prompt section for the supplied entries.Wires a gateway bridge into the internalAgentSpawnTool, enabling spawned subagents to be exposed as user-addressable threads.wireMessageBus(MessageBus messageBus, String agentId) Wires aMessageBusso that background task completions are pushed to the session inbox and a wakeup signal is enqueued.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.agentscope.core.middleware.MiddlewareBase
onActing, onModelCall, onSystemPrompt, order
-
Constructor Details
-
SubagentsMiddleware
public SubagentsMiddleware(List<SubagentEntry> entries, TaskRepository taskRepository, WorkspaceManager workspaceManager, AbstractFilesystem filesystem, Path mainWorkspace, Function<SubagentDeclaration, SubagentFactory> factoryBuilder) Default mode: createsAgentSpawnTool+DefaultAgentManagerinternally. -
SubagentsMiddleware
public SubagentsMiddleware(List<SubagentEntry> entries, TaskRepository taskRepository, WorkspaceManager workspaceManager) Default mode without dynamic reload support. -
SubagentsMiddleware
public SubagentsMiddleware(List<SubagentEntry> entries, Object externalSubagentTool, TaskRepository taskRepository) AgentStateStore mode: uses the externally provided tool (typicallySessionsTool).
-
-
Method Details
-
enableAgentGenerateTool
Enables theagent_generatetool, which lets the LLM author new subagent specs from a description. Off by default — the generator needs aModeland writes to the workspace, so callers opt in explicitly.Only effective in default (non-session) mode, where this middleware owns a
DefaultAgentManager. In session mode the manager is external and this call is a no-op.- Parameters:
generator- aSubagentSpecGeneratorbacked by the model that should author specs- Returns:
- this middleware for chaining
-
getTaskRepository
Returns theTaskRepositoryused by this middleware. -
wireMessageBus
Wires aMessageBusso that background task completions are pushed to the session inbox and a wakeup signal is enqueued. This enables theWakeupDispatcherto re-trigger idle sessions when subagent work finishes.Registers a
TaskRepository.TaskCompletionCallbackon the underlying repository. Safe to call multiple times — each call replaces the previous callback.- Parameters:
messageBus- the application message busagentId- the parent agent id (for wakeup routing)- Returns:
- this middleware for chaining
-
setGatewayBridge
Wires a gateway bridge into the internalAgentSpawnTool, enabling spawned subagents to be exposed as user-addressable threads. Only effective in default (non-session) mode.- Parameters:
bridge- the bridge implementation (typically obtained fromGatewayBootstrap.gatewayBridge())- Returns:
- this middleware for chaining
-
getAgentManager
Returns the internalDefaultAgentManagerthat can re-materialize subagents, ornullin session mode (external tool). Used to wire a gateway materializer for cross-node exposed-subagent recovery. -
getTools
Returns the tool instances this middleware contributes to the agent toolkit. The caller is responsible for registering them on the toolkit at orchestration time.When
enableAgentGenerateTool(io.agentscope.harness.agent.subagent.SubagentSpecGenerator)has been called, the returned list additionally contains anAgentGenerateTool. -
onAgent
public reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent> onAgent(io.agentscope.core.agent.Agent agent, io.agentscope.core.agent.RuntimeContext ctx, io.agentscope.core.middleware.AgentInput input, Function<io.agentscope.core.middleware.AgentInput, reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent>> next) - Specified by:
onAgentin interfaceio.agentscope.core.middleware.MiddlewareBase
-
onReasoning
public reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent> onReasoning(io.agentscope.core.agent.Agent agent, io.agentscope.core.agent.RuntimeContext ctx, io.agentscope.core.middleware.ReasoningInput input, Function<io.agentscope.core.middleware.ReasoningInput, reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent>> next) - Specified by:
onReasoningin interfaceio.agentscope.core.middleware.MiddlewareBase
-
renderSubagentSection
Renders the## Subagentssystem-prompt section for the supplied entries. Shared withDynamicSubagentsMiddleware.Filters out entries whose declaration sets
hidden=trueormode=PRIMARY: the LLM should not see internal-use subagents (hidden) and cannot spawn primary-only declarations (theDefaultAgentManagerrejects such spawns anyway, so advertising them would be a footgun). Programmatic registrations without a declaration (entry.declaration() == null) are always shown. -
buildTaskSummary
public static String buildTaskSummary(TaskRepository repo, io.agentscope.core.agent.RuntimeContext ctx) Builds a concise summary of in-flight tasks for the current session, ornullif none. Shared withDynamicSubagentsMiddleware.Phase B-3: already-delivered tasks are filtered out — they live in the conversation history as
<system-reminder>blocks, so re-mentioning them here would (a) waste tokens, (b) push genuinely still-running tasks past theMAX_TASK_SUMMARY_ENTRIEScap, and (c) invite the LLM to second-guess the push by re-fetching results.
-