Class HarnessGateway
java.lang.Object
io.agentscope.harness.agent.gateway.HarnessGateway
- All Implemented Interfaces:
Gateway,WakeupDispatcher.WakeupTarget
Default
Gateway implementation. Routes inbound turns by MsgContext.canonicalKey(),
serializes concurrent turns per session via SessionTurnGate, and dispatches to the
appropriate registered HarnessAgent.
This implementation provides:
- Multi-agent registry with fallback to a main agent
- Stable session mapping: same
MsgContext.canonicalKey()always resolves to the same session id, giving each logical conversation its own memory - Per-session fair mutual exclusion preventing concurrent turns from racing
- Outbound address tracking for proactive delivery (e.g. subagent announces)
-
Method Summary
Modifier and TypeMethodDescriptionvoidbindMainAgent(HarnessAgent agent) Binds the primary harness agent after construction.The channel manager for outbound delivery, or null if not configured.static HarnessGatewaycreate()Creates a gateway without outbound delivery support.static HarnessGatewaycreate(ChannelManager channelManager) Creates a gateway with a channel manager for outbound delivery.static HarnessGatewaycreate(ChannelManager channelManager, MessageBus messageBus) Creates a gateway with a channel manager and message bus.booleandeliverToSession(String sessionId, List<io.agentscope.core.message.Msg> messages) Delivers proactive outbound messages through the channel manager using the session's last recorded outbound address.exposeSubagent(String agentId, String sessionId, io.agentscope.core.agent.Agent agent, OutboundAddress replyTo) Exposes a spawned subagent as a user-addressable entry point.Returns the agent registered underagentId, or null if not found.Returns the configuredChannelRuntimeContextResolver, ornull.booleanisSessionRunning(String sessionId) Returnstruewhen the given session is currently inside a gated turn (a run is in progress).Returns the message bus, or null if not configured.voidregisterAgent(String agentId, HarnessAgent agent) Registers a named harness agent for routing.voidregisterExternalSession(String sessionId, String gateKey) Adopts a control-plane-allocated session id sorunWakeup(String, String)can resolve it.voidrevokeSubagent(String subagentId) Revokes a previously exposed subagent, removing it from the live cache and the registry.reactor.core.publisher.Mono<io.agentscope.core.message.Msg>run(MsgContext context, List<io.agentscope.core.message.Msg> messages) Inbound turn (direct API or channel adapter).reactor.core.publisher.Mono<io.agentscope.core.message.Msg>run(MsgContext context, List<io.agentscope.core.message.Msg> messages, OutboundAddress outboundAddress) Inbound turn with outbound address.reactor.core.publisher.Mono<io.agentscope.core.message.Msg>run(MsgContext context, List<io.agentscope.core.message.Msg> messages, OutboundAddress outboundAddress, io.agentscope.core.agent.RuntimeContext callerContext) Inbound turn with outbound address and an optional callerRuntimeContext.reactor.core.publisher.Mono<io.agentscope.core.message.Msg>run(MsgContext context, List<io.agentscope.core.message.Msg> messages, OutboundAddress outboundAddress, io.agentscope.core.agent.RuntimeContext callerContext, InboundMessage inboundMessage) Inbound turn with full Channel metadata forChannelRuntimeContextResolver.reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent>runStream(MsgContext context, List<io.agentscope.core.message.Msg> messages, OutboundAddress outboundAddress) Streaming variant ofGateway.run(MsgContext, List, OutboundAddress).reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent>runStream(MsgContext context, List<io.agentscope.core.message.Msg> messages, OutboundAddress outboundAddress, io.agentscope.core.agent.RuntimeContext callerContext) Streaming variant ofGateway.run(MsgContext, List, OutboundAddress, RuntimeContext).reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent>runStream(MsgContext context, List<io.agentscope.core.message.Msg> messages, OutboundAddress outboundAddress, io.agentscope.core.agent.RuntimeContext callerContext, InboundMessage inboundMessage) Streaming variant ofGateway.run(MsgContext, List, OutboundAddress, RuntimeContext, InboundMessage).reactor.core.publisher.Mono<io.agentscope.core.message.Msg>runSubagent(String subagentId, List<io.agentscope.core.message.Msg> messages) Routes messages directly to an exposed subagent session identified bysubagentId, bypassing normal binding-based routing.reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent>runSubagentStream(String subagentId, List<io.agentscope.core.message.Msg> messages) Streaming variant ofGateway.runSubagent(String, List).reactor.core.publisher.Mono<io.agentscope.core.message.Msg>Deprecated.reactor.core.publisher.Mono<io.agentscope.core.message.Msg>Triggers a wakeup-driven run for an idle session, propagating the owning user id when present.voidsetBaseStore(BaseStore store) Installs theBaseStoreused to persist session routing metadata across nodes.voidsetRuntimeContextResolver(ChannelRuntimeContextResolver runtimeContextResolver) Sets an optionalChannelRuntimeContextResolverused when building the per-turnRuntimeContext.voidInstalls theSessionTurnGateused to serialize concurrent turns per session.voidsetSubagentMaterializer(SubagentMaterializer materializer) Installs theSubagentMaterializerused to rebuild subagents that are not present in the local live cache (cross-node / post-restart recovery).voidsetSubagentRegistry(SubagentRegistry registry) Installs the durableSubagentRegistry.
-
Method Details
-
create
Creates a gateway with a channel manager for outbound delivery. -
create
Creates a gateway with a channel manager and message bus. -
create
Creates a gateway without outbound delivery support. -
messageBus
Returns the message bus, or null if not configured. -
channelManager
The channel manager for outbound delivery, or null if not configured. -
setRuntimeContextResolver
Sets an optionalChannelRuntimeContextResolverused when building the per-turnRuntimeContext. Passingnullclears any previously configured resolver. -
getRuntimeContextResolver
Returns the configuredChannelRuntimeContextResolver, ornull. -
bindMainAgent
Description copied from interface:GatewayBinds the primary harness agent after construction. The agent is registered under itsHarnessAgent.getAgentId()for routing byGateway.run(io.agentscope.harness.agent.gateway.MsgContext, java.util.List<io.agentscope.core.message.Msg>).- Specified by:
bindMainAgentin interfaceGateway
-
registerAgent
Description copied from interface:GatewayRegisters a named harness agent for routing.Gateway.run(io.agentscope.harness.agent.gateway.MsgContext, java.util.List<io.agentscope.core.message.Msg>)will resolve the target agent by theagentIdsupplied inMsgContext.extra()(key"agentId"), falling back to the agent registered viaGateway.bindMainAgent(io.agentscope.harness.agent.HarnessAgent).- Specified by:
registerAgentin interfaceGateway
-
findAgent
Returns the agent registered underagentId, or null if not found. Exposed for introspection (e.g. enumerate an agent's skill repositories). -
run
public reactor.core.publisher.Mono<io.agentscope.core.message.Msg> run(MsgContext context, List<io.agentscope.core.message.Msg> messages) Description copied from interface:GatewayInbound turn (direct API or channel adapter). -
run
public reactor.core.publisher.Mono<io.agentscope.core.message.Msg> run(MsgContext context, List<io.agentscope.core.message.Msg> messages, OutboundAddress outboundAddress) Description copied from interface:GatewayInbound turn with outbound address. TheoutboundAddressis recorded as the session's "last route" so that proactive outbound messages (e.g. subagent announces) can be delivered back to the originating channel/peer. -
run
public reactor.core.publisher.Mono<io.agentscope.core.message.Msg> run(MsgContext context, List<io.agentscope.core.message.Msg> messages, OutboundAddress outboundAddress, io.agentscope.core.agent.RuntimeContext callerContext) Description copied from interface:GatewayInbound turn with outbound address and an optional callerRuntimeContext.The caller context is merged into the turn; gateway identity fields take precedence. The default implementation ignores
callerContextand delegates toGateway.run(MsgContext, List). -
run
public reactor.core.publisher.Mono<io.agentscope.core.message.Msg> run(MsgContext context, List<io.agentscope.core.message.Msg> messages, OutboundAddress outboundAddress, io.agentscope.core.agent.RuntimeContext callerContext, InboundMessage inboundMessage) Description copied from interface:GatewayInbound turn with full Channel metadata forChannelRuntimeContextResolver. -
runStream
public reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent> runStream(MsgContext context, List<io.agentscope.core.message.Msg> messages, OutboundAddress outboundAddress) Description copied from interface:GatewayStreaming variant ofGateway.run(MsgContext, List, OutboundAddress). -
runStream
public reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent> runStream(MsgContext context, List<io.agentscope.core.message.Msg> messages, OutboundAddress outboundAddress, io.agentscope.core.agent.RuntimeContext callerContext) Description copied from interface:GatewayStreaming variant ofGateway.run(MsgContext, List, OutboundAddress, RuntimeContext).The default signals unsupported streaming.
-
runStream
public reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent> runStream(MsgContext context, List<io.agentscope.core.message.Msg> messages, OutboundAddress outboundAddress, io.agentscope.core.agent.RuntimeContext callerContext, InboundMessage inboundMessage) Description copied from interface:GatewayStreaming variant ofGateway.run(MsgContext, List, OutboundAddress, RuntimeContext, InboundMessage). -
deliverToSession
Delivers proactive outbound messages through the channel manager using the session's last recorded outbound address. Returns false if no route or no channel manager is available. -
exposeSubagent
public String exposeSubagent(String agentId, String sessionId, io.agentscope.core.agent.Agent agent, OutboundAddress replyTo) Exposes a spawned subagent as a user-addressable entry point. Returns the assigned subagentId.- Parameters:
agentId- the subagent type identifiersessionId- the session id assigned to the subagentagent- the agent instancereplyTo- the outbound address for delivering replies; may be null- Returns:
- the subagentId handle for direct addressing
-
revokeSubagent
Revokes a previously exposed subagent, removing it from the live cache and the registry. -
setSubagentRegistry
Installs the durableSubagentRegistry. Called during gateway wiring; defaults to an in-memory registry when not set. -
setSubagentMaterializer
Installs theSubagentMaterializerused to rebuild subagents that are not present in the local live cache (cross-node / post-restart recovery). -
setBaseStore
Installs theBaseStoreused to persist session routing metadata across nodes. Called during gateway wiring when a distributed store is configured. -
setSessionTurnGate
Installs theSessionTurnGateused to serialize concurrent turns per session. Defaults toLocalSessionTurnGatewhen not set.- Parameters:
gate- turn gate implementation; ignored whennull
-
runSubagent
public reactor.core.publisher.Mono<io.agentscope.core.message.Msg> runSubagent(String subagentId, List<io.agentscope.core.message.Msg> messages) Description copied from interface:GatewayRoutes messages directly to an exposed subagent session identified bysubagentId, bypassing normal binding-based routing. Returns the subagent's reply.- Specified by:
runSubagentin interfaceGateway- Parameters:
subagentId- the handle returned bySubagentGatewayBridge.ExposeResult.subagentId()messages- the messages to send to the subagent
-
runSubagentStream
public reactor.core.publisher.Flux<io.agentscope.core.event.AgentEvent> runSubagentStream(String subagentId, List<io.agentscope.core.message.Msg> messages) Description copied from interface:GatewayStreaming variant ofGateway.runSubagent(String, List).- Specified by:
runSubagentStreamin interfaceGateway
-
registerExternalSession
Adopts a control-plane-allocated session id sorunWakeup(String, String)can resolve it. Used by BYOteam_join; Managed sessions already use product ids that the managed turn path accepts without this mapping.- Parameters:
sessionId- external session id assigned by the control planegateKey- stable gate key (e.g.team:{teamName}:{role})
-
isSessionRunning
Returnstruewhen the given session is currently inside a gated turn (a run is in progress). Used byWakeupDispatcherto skip sessions that will naturally drain their inbox on the current reasoning step.- Specified by:
isSessionRunningin interfaceWakeupDispatcher.WakeupTarget
-
runWakeup
@Deprecated public reactor.core.publisher.Mono<io.agentscope.core.message.Msg> runWakeup(String sessionId) Deprecated.userunWakeup(String, String)so the wakeup-driven round loads agent state from the correct(userId, sessionId)slot. This overload preserves legacy anonymous-wakeup behavior.Triggers a wakeup run without an owning user id.Delegates to
runWakeup(String, String)with anulluser id.- Specified by:
runWakeupin interfaceWakeupDispatcher.WakeupTarget- Parameters:
sessionId- the session to wake up- Returns:
- the agent's response, or
Mono.empty()if the session is unknown
-
runWakeup
public reactor.core.publisher.Mono<io.agentscope.core.message.Msg> runWakeup(String userId, String sessionId) Triggers a wakeup-driven run for an idle session, propagating the owning user id when present. Called byWakeupDispatcherwhen a background task completes or a team message arrives. The agent starts a reasoning round with no user input;InboxMiddlewaredrains the inbox and injects the pending results as context.When
userIdis non-blank it is set on theRuntimeContext, so the agent loads state from the same(userId, sessionId)slot as the original user-driven run — matchingrunStream. A blank or null user id preserves the legacy anonymous behavior.- Specified by:
runWakeupin interfaceWakeupDispatcher.WakeupTarget- Parameters:
userId- the owning user id carried by the wakeup entry; may benullor blanksessionId- the session to wake up- Returns:
- the agent's response, or
Mono.empty()if the session is unknown
-
runWakeup(String, String)so the wakeup-driven round loads agent state from the correct(userId, sessionId)slot.