接口 AgentEventEmitter

函数接口:
这是一个函数接口, 因此可用作 lambda 表达式或方法引用的赋值目标。

@FunctionalInterface public interface AgentEventEmitter
Public interface for emitting AgentEvents into the parent agent's streamEvents() event stream from within tool execution code.

An instance is created inside ReActAgent.streamEvents(java.util.List<io.agentscope.core.message.Msg>) and stored in the Reactor Context under CONTEXT_KEY. When a tool method (e.g. agent_spawn) runs inside that stream pipeline, it can retrieve the emitter via fromContext(ContextView) and inject custom events (e.g. SubagentExposedEvent).

When the parent agent is invoked via call() (non-streaming), no emitter is present in the context and fromContext(reactor.util.context.ContextView) returns Optional.empty(), allowing callers to fall back gracefully.

This is the AgentEvent counterpart of SubagentEventBus, which serves the deprecated Event type.

  • 字段详细资料

    • CONTEXT_KEY

      static final String CONTEXT_KEY
      Reactor Context key under which the emitter instance is stored. Use fromContext(ContextView) for retrieval.
      另请参阅:
    • FORWARDING_CONTEXT_KEY

      static final String FORWARDING_CONTEXT_KEY
      Reactor Context key for a forwarding emitter injected by a parent tool (e.g. agent_spawn) into a child agent's Reactor Context. When present, the child's publishEvent() uses this emitter instead of the direct FluxSink, so every child event is tagged with a source path before entering the parent's stream.

      This is intentionally separate from CONTEXT_KEY: the regular key carries the parent's own emitter (used by tools to emit custom events like SubagentExposedEvent), while this key carries a source-tagging wrapper for the child's internal events.

      另请参阅:
  • 方法详细资料

    • emit

      void emit(AgentEvent event)
      Emits an AgentEvent into the parent's streamEvents() stream.

      This method is safe to call from any thread; the underlying FluxSink.next is thread-safe.

      参数:
      event - the event to emit
    • fromContext

      static Optional<AgentEventEmitter> fromContext(reactor.util.context.ContextView ctx)
      Retrieves the AgentEventEmitter from the Reactor Context, if present.
      参数:
      ctx - the current Reactor subscriber context
      返回:
      an Optional containing the emitter, or empty when running outside a streaming pipeline
    • fromForwardingContext

      static Optional<AgentEventEmitter> fromForwardingContext(reactor.util.context.ContextView ctx)
      Retrieves the forwarding AgentEventEmitter from the Reactor Context, if present.
      参数:
      ctx - the current Reactor subscriber context
      返回:
      an Optional containing the forwarding emitter, or empty when not in a child-agent forwarding context