Class DefaultAgentManager

java.lang.Object
io.agentscope.harness.agent.subagent.DefaultAgentManager

public final class DefaultAgentManager extends Object
Pure agent factory and invoker — knows how to create agents from registered factories and invoke them with a prompt.

This is the agent-internal layer. It has no session registry, no lane management, no run tracking. The agent-internal AgentSpawnTool uses this directly for lightweight subagent invocation.

  • Constructor Details

  • Method Details

    • refreshEntries

      public void refreshEntries(List<SubagentEntry> entries)
      Replaces the current set of entries with a new snapshot.
    • replaceAgents

      public void replaceAgents(List<SubagentEntry> entries)
      Atomic alias of refreshEntries(List) used by DynamicSubagentsMiddleware to swap the registered subagent set on every reasoning step. The two volatile reference assignments below ensure any concurrent reader observes either the previous snapshot or the new one fully — never a partial state.
    • createAgentIfPresent

      public Optional<io.agentscope.core.agent.Agent> createAgentIfPresent(String agentId, io.agentscope.core.agent.RuntimeContext parentRc)
      Race-safe lookup-and-create. Returns Optional.empty() when no factory is registered for agentId at the moment of the volatile read, when the registered declaration is SubagentDeclaration.Mode.PRIMARY-only (cannot be spawned as a subagent), otherwise returns a freshly created agent. Preferred over the two-step hasAgent(String) + createAgent(String, RuntimeContext) pair when the registry may be replaced concurrently (e.g. dynamic reload between calls).

      parentRc is forwarded to SubagentFactory.create(RuntimeContext) so child agents can bucket their persisted state by parent identity. Pass RuntimeContext.empty() when no parent context is available.

    • isPrimaryOnly

      public boolean isPrimaryOnly(String agentId)
      Returns true when agentId is registered with a PRIMARY-only declaration. Lets AgentSpawnTool produce a more helpful error message ("PRIMARY-only, cannot be spawned") instead of the generic "Unknown agent_id" when createAgentIfPresent(java.lang.String, io.agentscope.core.agent.RuntimeContext) returns empty.
    • hasAgent

      public boolean hasAgent(String agentId)
      Whether a factory is registered for the given agent id.
    • getAgentFactories

      public Map<String,SubagentFactory> getAgentFactories()
      Immutable view of registered subagent factories keyed by agent_id.
    • getDeclaration

      public Optional<SubagentDeclaration> getDeclaration(String agentId)
      Optional declaration metadata for the given agent_id (e.g. remote URL).
    • createAgent

      public io.agentscope.core.agent.Agent createAgent(String agentId, io.agentscope.core.agent.RuntimeContext parentRc)
      Creates a new agent instance from the registered factory.
      Throws:
      IllegalArgumentException - if no factory is registered for the given id
    • invokeAgent

      public reactor.core.publisher.Mono<io.agentscope.core.message.Msg> invokeAgent(io.agentscope.core.agent.Agent agent, String sessionId, String userId, String prompt)
      Invokes an agent with a user prompt. Handles both plain Agent and HarnessAgent (injects RuntimeContext for the latter).

      For HarnessAgent children, userId is propagated so that isolation-key resolution (e.g. USER-scoped sandbox slots) works correctly. A fresh sessionId is always assigned independently of the parent session.

      Parameters:
      agent - the agent to invoke
      sessionId - a new, child-specific session id
      userId - the parent's user-id (may be null)
      prompt - the user message to send
    • invokeAgent

      public reactor.core.publisher.Mono<io.agentscope.core.message.Msg> invokeAgent(io.agentscope.core.agent.Agent agent, String sessionId, String userId, String prompt, io.agentscope.core.agent.RuntimeContext parentRc)
    • invokeAgentStream

      public reactor.core.publisher.Flux<io.agentscope.core.agent.Event> invokeAgentStream(io.agentscope.core.agent.Agent agent, String sessionId, String userId, String prompt, io.agentscope.core.agent.EventSource source, io.agentscope.core.agent.StreamOptions options)
      Invokes an agent and returns its execution as a tagged Flux of Events.

      Every event in the returned flux carries an EventSource built from source combined with the child's agentId/sessionId. This allows parent consumers to identify which subagent emitted each event without out-of-band metadata.

      The parentSource argument should be the EventSource already stored in the parent's Reactor Context (if any). When the parent itself is a subagent, its path is used as the prefix so the full call-hierarchy path is preserved across multiple nesting levels.

      Parameters:
      agent - the agent to invoke
      sessionId - a new, child-specific session id
      userId - the parent's user-id (may be null)
      prompt - the user message to send
      source - the EventSource that will be stamped onto every emitted event
      options - stream configuration passed to the child agent
      Returns:
      Flux of tagged events; never null
    • invokeAgentStream

      public reactor.core.publisher.Flux<io.agentscope.core.agent.Event> invokeAgentStream(io.agentscope.core.agent.Agent agent, String sessionId, String userId, String prompt, io.agentscope.core.agent.EventSource source, io.agentscope.core.agent.StreamOptions options, io.agentscope.core.agent.RuntimeContext parentRc)
    • getWorkspaceManager

      public WorkspaceManager getWorkspaceManager()