Class ChatApiManager

java.lang.Object
org.codelibs.fess.api.BaseApiManager
org.codelibs.fess.api.chat.ChatApiManager
All Implemented Interfaces:
WebApiManager

public class ChatApiManager extends BaseApiManager
API Manager for RAG chat endpoints with SSE streaming support.
Author:
FessProject
  • Field Details

    • pathPrefix

      protected String pathPrefix
      The path prefix for the chat API endpoints.
  • Constructor Details

    • ChatApiManager

      public ChatApiManager()
      Default constructor.
  • Method Details

    • register

      @PostConstruct public void register()
      Registers this API manager with the WebApiManagerFactory.
    • matches

      public boolean matches(jakarta.servlet.http.HttpServletRequest request)
      Description copied from interface: WebApiManager
      Checks if the request matches this API manager.
      Parameters:
      request - The HTTP servlet request.
      Returns:
      True if the request matches, false otherwise.
    • process

      public void process(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain chain) throws IOException, jakarta.servlet.ServletException
      Description copied from interface: WebApiManager
      Processes the request through this API manager.
      Parameters:
      request - The HTTP servlet request.
      response - The HTTP servlet response.
      chain - The filter chain.
      Throws:
      IOException - If an input/output error occurs.
      jakarta.servlet.ServletException - If a servlet error occurs.
    • processChatRequest

      protected void processChatRequest(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws IOException
      Processes a non-streaming chat request.
      Parameters:
      request - the HTTP request
      response - the HTTP response
      Throws:
      IOException - if an I/O error occurs
    • processStreamRequest

      protected void processStreamRequest(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws IOException
      Processes a streaming chat request using Server-Sent Events (SSE). Uses the enhanced multi-phase RAG flow with intent detection and result evaluation.
      Parameters:
      request - the HTTP request
      response - the HTTP response
      Throws:
      IOException - if an I/O error occurs
    • sendSseEvent

      protected void sendSseEvent(PrintWriter writer, String event, Map<String,Object> data)
      Sends a Server-Sent Event (SSE) to the client.
      Parameters:
      writer - the print writer to write the event to
      event - the event name
      data - the event data to serialize as JSON
    • writeJsonResponse

      protected void writeJsonResponse(jakarta.servlet.http.HttpServletResponse response, int status, Map<String,Object> data) throws IOException
      Writes a JSON response to the HTTP response.
      Parameters:
      response - the HTTP response
      status - the HTTP status code
      data - the data to serialize as JSON
      Throws:
      IOException - if an I/O error occurs
    • createSuccessResponse

      protected Map<String,Object> createSuccessResponse(String sessionId, String content, List<ChatMessage.ChatSource> sources)
      Creates a success response map.
      Parameters:
      sessionId - the session ID
      content - the response content
      sources - the list of chat sources
      Returns:
      a map containing the success response data
    • createErrorResponse

      protected Map<String,Object> createErrorResponse(String message)
      Creates an error response map.
      Parameters:
      message - the error message
      Returns:
      a map containing the error response data
    • getUserId

      protected String getUserId(jakarta.servlet.http.HttpServletRequest request)
      Gets the user ID from the request.
      Parameters:
      request - the HTTP request
      Returns:
      the user ID, or null if the user is a guest
    • getMaxMessageLength

      protected int getMaxMessageLength(org.codelibs.fess.mylasta.direction.FessConfig fessConfig)
      Returns the maximum message length for chat messages.
      Parameters:
      fessConfig - the Fess configuration
      Returns:
      the maximum message length
    • parseFieldFilters

      protected Map<String,String[]> parseFieldFilters(jakarta.servlet.http.HttpServletRequest request)
      Parses and validates field filter parameters from the request. Only configured label values are accepted to prevent query injection.
      Parameters:
      request - the HTTP request
      Returns:
      a map of field names to their validated filter values
    • parseExtraQueries

      protected String[] parseExtraQueries(jakarta.servlet.http.HttpServletRequest request)
      Parses and validates extra query parameters from the request. Only configured facet query values are accepted to prevent query injection.
      Parameters:
      request - the HTTP request
      Returns:
      an array of validated extra query strings
    • writeHeaders

      protected void writeHeaders(jakarta.servlet.http.HttpServletResponse response)
      Description copied from class: BaseApiManager
      Writes custom headers to the HTTP response.
      Specified by:
      writeHeaders in class BaseApiManager
      Parameters:
      response - The HTTP servlet response.