Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:A616567126 GPT WEB JAVA ConsoleStreamListener

From Leeroopedia
Knowledge Sources
Domains SSE_Streaming, Real_Time_Communication
Last Updated 2026-10-13 00:00 GMT

Overview

Concrete tool for bridging OpenAI SSE stream events to client-side message delivery, provided by the OkHttp SSE framework.

Description

The ConsoleStreamListener extends AbstractStreamListener and implements the onMsg and onError callbacks. When a new token delta arrives from the OpenAI stream, onMsg delegates to SendMessageUtil.sendMessage() to push the content to the user's SSE or WebSocket connection. On error, it disables the API key, refunds the user's credits, sends an error message, and persists the failed conversation.

Usage

Import this class when you need to:

  • Forward GPT streaming tokens to a client via SSE/WS
  • Handle stream errors with automatic key state update and credit refund
  • Set a completion callback for conversation persistence

Code Reference

Source Location

  • Repository: GPT-WEB-JAVA
  • File: src/main/java/com/intelligent/bot/listener/gpt/ConsoleStreamListener.java

Signature

@Builder
public class ConsoleStreamListener extends AbstractStreamListener {
    final Long userId;
    final Long logId;
    final AsyncService asyncService;
    final IUserService userService;
    final IGptKeyService gptKeyService;
    final IMessageLogService messageLogService;

    @Override
    public void onMsg(Object message);

    @Override
    public void onError(Throwable throwable);
}

Import

import com.intelligent.bot.listener.gpt.ConsoleStreamListener;

I/O Contract

Inputs

Name Type Required Description
userId Long Yes Target user ID for message delivery
logId Long Yes Conversation log ID for persistence
asyncService AsyncService Yes Async service for key/credit/log operations

Outputs

Name Type Description
onMsg void Pushes Message delta to client via SendMessageUtil
onError void Disables key, refunds credits, sends error message, persists conversation

Usage Examples

Creating a Listener

ConsoleStreamListener listener = ConsoleStreamListener.builder()
        .userId(JwtUtil.getUserId())
        .userService(userService)
        .gptKeyService(gptKeyService)
        .asyncService(asyncService)
        .logId(logId)
        .build();

// Set completion callback (called when [DONE] received)
listener.setOnComplate(msg -> {
    asyncService.endOfAnswer(logId, msg.toString());
});

// Pass to ChatGPTStream
chatGPTStream.streamChatCompletion(messages, listener, type, req, imgMessages);

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment