Class ChatCompletionsStreamingAdapter
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
FluxofChatCompletionsChunk(not framework-specific types) - Framework adapters (in starter modules) handle SSE/transport specifics
Supported Event Types:
EventType.REASONING- Text content and tool call decisionsEventType.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 Summary
ConstructorsConstructorDescriptionConstructs a newChatCompletionsStreamingAdapter. -
Method Summary
Modifier and TypeMethodDescriptionreactor.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.createErrorChunk(Throwable error, String requestId, String model) Create an error chunk.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.
-
Constructor Details
-
ChatCompletionsStreamingAdapter
public ChatCompletionsStreamingAdapter()Constructs a newChatCompletionsStreamingAdapter.
-
-
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
ChatCompletionsChunkobjects 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 frommessages- The messages to send to the agentrequestId- The request ID for tracking (used as chunk ID)model- The model name to include in chunks- Returns:
- A
FluxofChatCompletionsChunkobjects
-
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 eventrequestId- The request ID for trackingmodel- The model name- Returns:
- Flux of ChatCompletionsChunk objects
-
createErrorChunk
Create an error chunk.This can be used by framework adapters to handle errors in a consistent way.
- Parameters:
error- The error that occurredrequestId- The request ID for trackingmodel- The model name- Returns:
- A ChatCompletionsChunk representing the error
-