Class ChatCompletionsStreamingAdapter

java.lang.Object
io.agentscope.core.chat.completions.streaming.ChatCompletionsStreamingAdapter

public class ChatCompletionsStreamingAdapter extends Object
Framework-agnostic adapter for handling streaming chat completion responses.

This adapter converts agent events to OpenAI-compatible ChatCompletionsChunk objects. It is designed to be used by framework-specific adapters (e.g., Spring SSE, Quarkus Multi) that handle the final serialization and transport.

Design Philosophy:

  • Core streaming logic is framework-agnostic
  • Returns Flux of ChatCompletionsChunk (not framework-specific types)
  • Framework adapters (in starter modules) handle SSE/transport specifics

Supported Event Types:

  • EventType.REASONING - Text content and tool call decisions
  • EventType.TOOL_RESULT - Tool execution results

Output Chunk Types:

  • Text chunks - Incremental text content
  • Tool call chunks - When agent decides to call tools
  • Finish chunks - Stream completion with finish_reason
  • Constructor Details

    • ChatCompletionsStreamingAdapter

      public ChatCompletionsStreamingAdapter()
      Constructs a new ChatCompletionsStreamingAdapter.
  • Method Details

    • stream

      public reactor.core.publisher.Flux<ChatCompletionsChunk> stream(io.agentscope.core.ReActAgent agent, List<io.agentscope.core.message.Msg> messages, String requestId, String model)
      Stream agent events as OpenAI-compatible chunks.

      Subscribes to agent's event stream and converts each event to one or more ChatCompletionsChunk objects following OpenAI's streaming format.

      Text Deduplication: In incremental mode, agents send text deltas (isLast=false) followed by a final accumulated event (isLast=true). To avoid duplication, we filter text from the last REASONING event if incremental chunks were seen, while preserving tool calls and finish reasons.

      Parameters:
      agent - The agent to stream from
      messages - The messages to send to the agent
      requestId - The request ID for tracking (used as chunk ID)
      model - The model name to include in chunks
      Returns:
      A Flux of ChatCompletionsChunk objects
    • convertEventToChunks

      public reactor.core.publisher.Flux<ChatCompletionsChunk> convertEventToChunks(io.agentscope.core.agent.Event event, String requestId, String model)
      Convert an agent event to one or more streaming chunks.

      A single event may produce multiple chunks if it contains both text and tool calls.

      Parameters:
      event - The agent event
      requestId - The request ID for tracking
      model - The model name
      Returns:
      Flux of ChatCompletionsChunk objects
    • createErrorChunk

      public ChatCompletionsChunk createErrorChunk(Throwable error, String requestId, String model)
      Create an error chunk.

      This can be used by framework adapters to handle errors in a consistent way.

      Parameters:
      error - The error that occurred
      requestId - The request ID for tracking
      model - The model name
      Returns:
      A ChatCompletionsChunk representing the error