Class GenaiAdapter

java.lang.Object
org.machanism.machai.ai.provider.GenaiAdapter
All Implemented Interfaces:
Genai
Direct Known Subclasses:
CodeMieProvider

public class GenaiAdapter extends Object implements Genai
Delegating Genai implementation.

This adapter forwards all Genai calls to an underlying provider instance configured via setProvider(Genai).

Intended use cases include decorating providers (for example, adding cross-cutting concerns like logging, metrics, retries, or request shaping) while preserving the Genai contract.

Thread-safety: Instances are not thread-safe unless the delegated provider is thread-safe and access is externally synchronized.

  • Field Details

    • provider

      protected Genai provider
      Delegate provider.

      Subclasses typically set this value during construction or initialization using setProvider(Genai).

  • Constructor Details

    • GenaiAdapter

      public GenaiAdapter()
      Creates an adapter without a delegate.

      Call setProvider(Genai) before invoking any other methods.

  • Method Details

    • setProvider

      public void setProvider(Genai provider)
      Sets the delegate provider.
      Parameters:
      provider - the provider to delegate to
      Throws:
      IllegalArgumentException - if provider is null
    • init

      public void init(Configurator conf)
      Description copied from interface: Genai
      Initializes the provider with application configuration.
      Specified by:
      init in interface Genai
      Parameters:
      conf - configuration source
    • prompt

      public void prompt(String text)
      Description copied from interface: Genai
      Adds a user prompt to the current session.
      Specified by:
      prompt in interface Genai
      Parameters:
      text - the prompt text
    • embedding

      public List<Double> embedding(String text, long dimensions)
      Description copied from interface: Genai
      Computes an embedding vector for the provided text.
      Specified by:
      embedding in interface Genai
      Parameters:
      text - the input text
      dimensions - desired embedding dimensionality (provider-specific)
      Returns:
      the embedding vector
    • clear

      public void clear()
      Description copied from interface: Genai
      Clears any stored files and session/provider state.
      Specified by:
      clear in interface Genai
    • addTool

      public void addTool(String name, String description, ToolFunction function, String... paramsDesc)
      Description copied from interface: Genai
      Registers a custom tool function that the provider may invoke at runtime.

      The expected argument structure passed to function is provider-specific.

      Specified by:
      addTool in interface Genai
      Parameters:
      name - tool name (unique per provider instance)
      description - human-readable description of the tool
      function - function implementation; receives an argument array and returns a result
      paramsDesc - parameter descriptors (format is provider-specific)
    • instructions

      public void instructions(String instructions)
      Description copied from interface: Genai
      Sets system/session instructions for the current conversation.
      Specified by:
      instructions in interface Genai
      Parameters:
      instructions - instruction text
    • perform

      public String perform()
      Description copied from interface: Genai
      Executes the provider to produce a response based on the accumulated prompts and state.
      Specified by:
      perform in interface Genai
      Returns:
      the provider response
    • inputsLog

      public void inputsLog(File bindexTempDir)
      Description copied from interface: Genai
      Enables logging of provider inputs to the given directory.
      Specified by:
      inputsLog in interface Genai
      Parameters:
      bindexTempDir - directory used for writing log files
    • setWorkingDir

      public void setWorkingDir(File workingDir)
      Description copied from interface: Genai
      Configures the working directory used for file and tool operations.
      Specified by:
      setWorkingDir in interface Genai
      Parameters:
      workingDir - the working directory
    • usage

      public Usage usage()
      Description copied from interface: Genai
      Returns token usage metrics for the most recent Genai.perform() invocation.
      Specified by:
      usage in interface Genai
      Returns:
      usage metrics; implementations may return zero values if not supported