Class CopilotServerClient

java.lang.Object
com.vaadin.copilot.CopilotServerClient
Direct Known Subclasses:
AnalyticsClient, FeedbackClient, TestGenerationServerClient, UserInfoServerClient

public class CopilotServerClient extends Object
Base class for Copilot Server API clients
  • Field Details

  • Constructor Details

    • CopilotServerClient

      public CopilotServerClient()
      Constructor initializing HttpClient and ObjectMapper
  • Method Details

    • getHttpClient

      public HttpClient getHttpClient()
    • buildRequest

      public HttpRequest buildRequest(URI uri, Object request)
    • getQueryURI

      public URI getQueryURI(String path)
    • getServerBaseUrl

      protected static String getServerBaseUrl()
    • sendCopilotRequest

      public <REQ, RESP> void sendCopilotRequest(String path, REQ request, Class<RESP> responseType, Consumer<RESP> responseHandler, com.vaadin.base.devserver.DevToolsInterface devToolsInterface, String command, tools.jackson.databind.JsonNode respData)
    • reportRequestFailure

      public static String reportRequestFailure(org.slf4j.Logger logger, String context, Throwable error)
      Logs a failed Copilot server request and returns a user-facing error message. A proxy authentication failure is logged at WARN with a clear explanation and returned as-is; any other error is logged at ERROR with its stack trace and yields a generic message. Centralizes failure reporting so every (synchronous) caller behaves consistently.
      Parameters:
      logger - the caller's logger
      context - a short description of what failed, used for the error-level log
      error - the error that was thrown
      Returns:
      a message suitable for showing to the user
    • sendCopilotRequest

      public <REQ, RESP> void sendCopilotRequest(String path, REQ request, Class<RESP> responseType, Consumer<RESP> responseHandler, Consumer<Throwable> errorHandler)
    • sendFireAndForget

      public void sendFireAndForget(String path, Object request)
      Sends a best-effort, fire-and-forget POST request to the Copilot server.

      The caller never observes the result, and a failure must never affect the calling thread. It reuses the shared HttpClient and swallows request failures so best-effort traffic stays isolated.

      Parameters:
      path - server path to POST to
      request - payload serialized to JSON
    • sendStreamingRequest

      public Runnable sendStreamingRequest(URI uri, Object request, Duration idleTimeout, Consumer<String> lineHandler, Consumer<Throwable> errorHandler, Runnable completionHandler)
      Sends a POST request to the Copilot server and streams the response body to the given handler, one line at a time, as it arrives.

      Exactly one of errorHandler and completionHandler is called, unless the request is cancelled through the returned handle, after which no handler is called anymore. The request fails with an HttpTimeoutException when no response, or no further data, is received within idleTimeout, and with a CopilotServerResponseException when the server, or a proxy on the way, answers with a status other than 200.

      Parameters:
      uri - the URI to POST to
      request - payload serialized to JSON
      idleTimeout - the maximum time to wait for the response and for each further piece of the response body
      lineHandler - receives each line of the response body
      errorHandler - called if the request or reading the response fails
      completionHandler - called when the whole response body has been read
      Returns:
      a handle that cancels the request when run
    • getCopilotServerReachability

      public static Boolean getCopilotServerReachability()
      Returns the Copilot server reachability state. null means no request has been attempted, false means an attempt failed, and true means a response was received.
    • markCopilotServerReached

      protected static void markCopilotServerReached()
    • markCopilotServerUnreachable

      protected static void markCopilotServerUnreachable()
      Records a failed request unless the server has already been reached in this application run.
    • resolveAiProvider

      public static CopilotServerClient.AIProvider resolveAiProvider()
      Resolves which AI provider region should be used, based on the current user's project settings.