Class Chat

java.lang.Object
uno.anahata.ai.Chat

public class Chat extends Object
The central orchestrator for a Gemini AI chat session.

This class manages the conversation flow, including sending user input to the model, handling model responses, executing local tools (function calling), and maintaining the conversation context.

It integrates various components such as ContextManager for history, ToolManager for function execution, and StatusManager for state tracking.

  • Field Details

    • callingInstance

      public static final ThreadLocal<Chat> callingInstance
      A ThreadLocal reference to the Chat instance currently executing a tool. This allows tools to access the chat context without explicit passing.
  • Constructor Details

    • Chat

      public Chat(ChatConfig config, FunctionPrompter prompter)
      Constructs a new Chat instance with the specified configuration and prompter.
      Parameters:
      config - The configuration for this chat session.
      prompter - The prompter used to handle user confirmation for tool calls.
  • Method Details

    • resetMessageCounter

      public void resetMessageCounter(long value)
      Resets the sequential message counter to a specific value. Useful when restoring a session from persistent storage.
      Parameters:
      value - The new starting value for the message counter.
    • shutdown

      public void shutdown()
      Shuts down the chat session and its associated executor service.
    • addContextListener

      public void addContextListener(ContextListener listener)
      Adds a listener to be notified of changes in the conversation context.
      Parameters:
      listener - The listener to add.
    • addStatusListener

      public void addStatusListener(StatusListener listener)
      Adds a listener to be notified of changes in the chat's operational status.
      Parameters:
      listener - The listener to add.
    • removeStatusListener

      public void removeStatusListener(StatusListener listener)
      Removes a previously added status listener.
      Parameters:
      listener - The listener to remove.
    • getCallingInstance

      public static Chat getCallingInstance()
      Gets the Chat instance associated with the current thread.
      Returns:
      The active Chat instance, or null if not in a tool execution context.
    • init

      public void init()
      Initializes the chat session, sending any configured startup instructions to the model.
    • clear

      public void clear()
      Clears the conversation history and resets the status manager.
    • sendText

      public void sendText(String message)
      Sends a simple text message from the user to the model.
      Parameters:
      message - The text message to send.
    • sendContent

      public void sendContent(com.google.genai.types.Content content)
      Sends a structured Content object to the model and initiates the processing loop.
      Parameters:
      content - The content to send.
    • getGoogleGenAIClient

      public com.google.genai.Client getGoogleGenAIClient()
      Gets the underlying Google GenAI client used by this chat session.
      Returns:
      The Google GenAI client.
    • getContext

      public List<ChatMessage> getContext()
      Gets the current conversation history as a list of ChatMessage objects.
      Returns:
      The conversation history.
    • notifyJobCompletion

      public void notifyJobCompletion(JobInfo jobInfo)
      Notifies the chat session that an asynchronous job has completed. The result is added to the conversation context as a tool response.
      Parameters:
      jobInfo - Information about the completed job.
    • getShortId

      public String getShortId()
      Gets a short, human-readable identifier for the chat session.
      Returns:
      A short session ID.
    • getContextWindowUsage

      public float getContextWindowUsage()
      Calculates the current context window usage as a percentage of the token threshold.
      Returns:
      The usage percentage (0.0 to 1.0).
    • getContextWindowUsageFormatted

      public String getContextWindowUsageFormatted()
      Gets a formatted string representing the current context window usage percentage.
      Returns:
      A formatted usage string (e.g., "45.2%").