Class OpenAiCompatibleClient

java.lang.Object
org.egothor.methodatlas.ai.OpenAiCompatibleClient
All Implemented Interfaces:
AiProviderClient

public final class OpenAiCompatibleClient extends Object implements AiProviderClient
AiProviderClient implementation for AI providers that expose an OpenAI-compatible chat completion API.

This client supports providers that expose an OpenAI-compatible chat completions endpoint. The path appended to the configured base URL is provider-specific: most providers use /v1/chat/completions, while AiProvider.GITHUB_MODELS, AiProvider.XAI, and AiProvider.MISTRAL use /chat/completions because their default base URLs already include the /v1 segment (or omit it entirely, as is the case for GitHub Models).

The client constructs a chat-style prompt consisting of a system message defining the classification rules and a user message containing the test class source together with the taxonomy definition. The model response is expected to contain a JSON object describing the security classification.

Operational Responsibilities

  • constructing OpenAI-compatible chat completion requests
  • injecting the taxonomy-driven classification prompt
  • performing authenticated HTTP requests
  • extracting JSON content from the model response
  • normalizing the result into AiClassSuggestion

The implementation is provider-neutral for APIs that follow the OpenAI protocol, which allows reuse across multiple compatible services such as OpenRouter.

Instances are typically created through AiProviderFactory.create(AiOptions).

See Also:
  • Constructor Details

    • OpenAiCompatibleClient

      public OpenAiCompatibleClient(AiOptions options)
      Creates a new client for an OpenAI-compatible provider with no rate-limit notification.

      Rate-limit pauses are handled transparently. Use OpenAiCompatibleClient(AiOptions, RateLimitListener) when callers need to be notified of such pauses.

      Parameters:
      options - AI runtime configuration
    • OpenAiCompatibleClient

      public OpenAiCompatibleClient(AiOptions options, RateLimitListener rateLimitListener)
      Creates a new client for an OpenAI-compatible provider that notifies rateLimitListener before each rate-limit sleep.
      Parameters:
      options - AI runtime configuration
      rateLimitListener - callback invoked before each HTTP 429 pause; must not be null
      See Also:
  • Method Details

    • isAvailable

      public boolean isAvailable()
      Determines whether the configured provider can be used in the current runtime environment.

      For OpenAI-compatible providers, availability is determined by the presence of a usable API key resolved through AiOptions.resolvedApiKey().

      Specified by:
      isAvailable in interface AiProviderClient
      Returns:
      true if a usable API key is available
    • suggestForClass

      public AiClassSuggestion suggestForClass(String fqcn, String classSource, String taxonomyText, List<PromptBuilder.TargetMethod> targetMethods) throws AiSuggestionException
      Submits a classification request to an OpenAI-compatible chat completion API.

      The request payload includes:

      • the configured model identifier
      • a system prompt defining classification rules
      • a user prompt containing the test class source and taxonomy
      • a deterministic temperature setting

      When the selected provider is AiProvider.OPENROUTER, additional HTTP headers are included to identify the calling application.

      The response is expected to contain a JSON object in the message content field. The JSON text is extracted and deserialized into an AiClassSuggestion.

      Specified by:
      suggestForClass in interface AiProviderClient
      Parameters:
      fqcn - fully qualified class name being analyzed
      classSource - complete source code of the class
      taxonomyText - taxonomy definition guiding classification
      targetMethods - deterministically extracted JUnit test methods that must be classified
      Returns:
      normalized classification result
      Throws:
      AiSuggestionException - if the provider request fails, the model response is invalid, or JSON deserialization fails
      See Also: