Class AssistantWebSocket

java.lang.Object
ecmwf.ecpds.master.plugin.http.AssistantWebSocket
All Implemented Interfaces:
org.eclipse.jetty.ee8.websocket.api.WebSocketConnectionListener, org.eclipse.jetty.ee8.websocket.api.WebSocketListener

public class AssistantWebSocket extends Object implements org.eclipse.jetty.ee8.websocket.api.WebSocketListener
WebSocket endpoint for streaming AI assistant interactions to clients.

Supports bidirectional communication between the client and AssistantService. Each WebSocket maintains a conversation per HTTP session, streams AI-generated tokens in real time, and supports cancellation and heartbeat pings.

Features:

  • Streaming tokens from AI assistant to client
  • Maintains per-session chat history
  • Heartbeat pings to prevent WebSocket timeouts
  • Context-aware AI queries using JSON context
  • Graceful cancellation on disconnect or error
  • Token batching to reduce message overhead

Implements the (legacy/"ee8") WebSocketListener interface directly rather than relying on the annotation-based API, since the endpoint is registered through JettyWebSocketServletContainerInitializer, whose frame-handler factory only recognises org.eclipse.jetty.ee8.websocket.api.annotations.* annotations (not the similarly named ones in org.eclipse.jetty.websocket.api.annotations), so implementing the interface avoids that ambiguity.

  • Constructor Details

    • AssistantWebSocket

      public AssistantWebSocket(javax.servlet.http.HttpSession httpSession)
      Constructs a WebSocket instance bound to the given HTTP session.
      Parameters:
      httpSession - the HTTP session for maintaining conversation state
  • Method Details

    • onWebSocketConnect

      public void onWebSocketConnect(org.eclipse.jetty.ee8.websocket.api.Session session)
      Called when the WebSocket is connected.

      Schedules periodic token flush and heartbeat ping tasks, and sets idle timeout.

      Specified by:
      onWebSocketConnect in interface org.eclipse.jetty.ee8.websocket.api.WebSocketConnectionListener
      Parameters:
      session - the connected WebSocket session
    • onWebSocketText

      public void onWebSocketText(String message)
      Called when a message is received from the client.

      Expected JSON format:

      {
        "question": "user question",
        "context": { ... optional AI context ... }
      }
      
      Supports cancellation messages:
      {
        "type": "cancel"
      }
      
      Specified by:
      onWebSocketText in interface org.eclipse.jetty.ee8.websocket.api.WebSocketListener
      Parameters:
      message - the raw JSON message from the client
    • onWebSocketClose

      public void onWebSocketClose(int statusCode, String reason)
      Called when the WebSocket is closed.
      Specified by:
      onWebSocketClose in interface org.eclipse.jetty.ee8.websocket.api.WebSocketConnectionListener
      Parameters:
      statusCode - the close status code
      reason - the reason for closure
    • onWebSocketError

      public void onWebSocketError(Throwable error)
      Called on WebSocket error.
      Specified by:
      onWebSocketError in interface org.eclipse.jetty.ee8.websocket.api.WebSocketConnectionListener
      Parameters:
      error - the thrown error