Class ChatCompletionsStreamingService

java.lang.Object
io.agentscope.spring.boot.chat.service.ChatCompletionsStreamingService

public class ChatCompletionsStreamingService extends Object
Spring-specific service for streaming chat completion responses.

This service is a thin adapter layer that:

  • Delegates core streaming logic to ChatCompletionsStreamingAdapter
  • Converts ChatCompletionsChunk to Spring's ServerSentEvent
  • Handles JSON serialization for SSE data field

Architecture:

 ChatCompletionsStreamingAdapter (framework-agnostic, in extension-core)
           ↓ Flux<ChatCompletionsChunk>
 ChatCompletionsStreamingService (Spring-specific, in starter)
           ↓ Flux<ServerSentEvent<String>>
 HTTP Response (SSE stream)
 

This design allows the core streaming logic to be reused across different frameworks (Spring, Quarkus, etc.) while keeping framework-specific concerns in the starter modules.

  • Constructor Details

    • ChatCompletionsStreamingService

      public ChatCompletionsStreamingService(io.agentscope.core.chat.completions.streaming.ChatCompletionsStreamingAdapter streamingAdapter)
      Constructs a new ChatCompletionsStreamingService.
      Parameters:
      streamingAdapter - The framework-agnostic streaming adapter
  • Method Details

    • streamAsSse

      public reactor.core.publisher.Flux<org.springframework.http.codec.ServerSentEvent<String>> streamAsSse(io.agentscope.core.ReActAgent agent, List<io.agentscope.core.message.Msg> messages, String requestId, String model)
      Stream agent events as Server-Sent Events (SSE).

      Each SSE "data" field contains a JSON-serialized ChatCompletionsChunk following OpenAI's streaming format. The stream ends with a "data: [DONE]" event.

      Parameters:
      agent - The agent to stream from
      messages - The messages to send to the agent
      requestId - The request ID for tracking
      model - The model name for the response
      Returns:
      A Flux of ServerSentEvent objects
    • createErrorSseEvent

      public org.springframework.http.codec.ServerSentEvent<String> createErrorSseEvent(Throwable error, String requestId, String model)
      Create an error SSE event.
      Parameters:
      error - The error that occurred
      requestId - The request ID for tracking
      model - The model name
      Returns:
      A ServerSentEvent with error information