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 Summary

    Fields
    Modifier and Type
    Field
    Description
    protected Genai
    Delegate provider.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an adapter without a delegate.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addPrompts(FunctionTools functionTool)
    Scans the provided FunctionTools instance for methods annotated with Prompt, and registers each prompt for use during a run.
    void
    Registers a set of tool functions that may be invoked during a run.
    void
    Clears any stored files and session/provider state.
    void
    init(String model, Configurator conf)
    Initializes the provider with application configuration.
    void
    inputsLog(File bindexTempDir)
    Enables logging of provider inputs to the given directory.
    void
    instructions(String instructions)
    Sets system/session instructions for the current conversation.
    Executes the provider to produce a response based on the accumulated prompts and state.
    void
    prompt(String text)
    Adds a user prompt to the current session.
    void
    setErrorHandling(boolean errorHandling)
    Configures whether tool invocation errors should be returned to the model for conversational recovery or propagated as exceptions.
    void
    setProjectDir(File projectDir)
    Sets the working directory for the provider, which may be used by tool handlers.
    void
    setProvider(Genai provider)
    Sets the delegate provider.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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(String model, Configurator conf)
      Initializes the provider with application configuration.
      Specified by:
      init in interface Genai
      Parameters:
      model - the model identifier or name to use
      conf - configuration source
    • prompt

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

      public void clear()
      Clears any stored files and session/provider state.

      This resets the conversation and any accumulated context.

      Specified by:
      clear in interface Genai
    • instructions

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

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

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

      public void setProjectDir(File projectDir)
      Sets the working directory for the provider, which may be used by tool handlers.
      Specified by:
      setProjectDir in interface Genai
      Parameters:
      projectDir - the project directory
    • addTools

      public void addTools(FunctionTools tools)
      Registers a set of tool functions that may be invoked during a run.
      Specified by:
      addTools in interface Genai
      Parameters:
      tools - the FunctionTools instance containing tool methods
    • addPrompts

      public void addPrompts(FunctionTools functionTool)
      Scans the provided FunctionTools instance for methods annotated with Prompt, and registers each prompt for use during a run.
      Specified by:
      addPrompts in interface Genai
      Parameters:
      functionTool - the FunctionTools instance whose methods will be scanned for Prompt annotations
    • setErrorHandling

      public void setErrorHandling(boolean errorHandling)
      Description copied from interface: Genai
      Configures whether tool invocation errors should be returned to the model for conversational recovery or propagated as exceptions.
      Specified by:
      setErrorHandling in interface Genai
      Parameters:
      errorHandling - true to return tool errors as response text; false to propagate them immediately