Class Chat
java.lang.Object
uno.anahata.ai.Chat
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final ThreadLocal<Chat> A ThreadLocal reference to the Chat instance currently executing a tool. -
Constructor Summary
ConstructorsConstructorDescriptionChat(ChatConfig config, FunctionPrompter prompter) Constructs a new Chat instance with the specified configuration and prompter. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddContextListener(ContextListener listener) Adds a listener to be notified of changes in the conversation context.voidaddStatusListener(StatusListener listener) Adds a listener to be notified of changes in the chat's operational status.voidclear()Clears the conversation history and resets the status manager.static ChatGets the Chat instance associated with the current thread.Gets the current conversation history as a list ofChatMessageobjects.floatCalculates the current context window usage as a percentage of the token threshold.Gets a formatted string representing the current context window usage percentage.com.google.genai.ClientGets the underlying Google GenAI client used by this chat session.Gets a short, human-readable identifier for the chat session.voidinit()Initializes the chat session, sending any configured startup instructions to the model.voidnotifyJobCompletion(JobInfo jobInfo) Notifies the chat session that an asynchronous job has completed.voidremoveStatusListener(StatusListener listener) Removes a previously added status listener.voidresetMessageCounter(long value) Resets the sequential message counter to a specific value.voidsendContent(com.google.genai.types.Content content) Sends a structuredContentobject to the model and initiates the processing loop.voidSends a simple text message from the user to the model.voidshutdown()Shuts down the chat session and its associated executor service.
-
Field Details
-
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
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
Adds a listener to be notified of changes in the conversation context.- Parameters:
listener- The listener to add.
-
addStatusListener
Adds a listener to be notified of changes in the chat's operational status.- Parameters:
listener- The listener to add.
-
removeStatusListener
Removes a previously added status listener.- Parameters:
listener- The listener to remove.
-
getCallingInstance
Gets the Chat instance associated with the current thread.- Returns:
- The active Chat instance, or
nullif 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
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 structuredContentobject 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
Gets the current conversation history as a list ofChatMessageobjects.- Returns:
- The conversation history.
-
notifyJobCompletion
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
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
Gets a formatted string representing the current context window usage percentage.- Returns:
- A formatted usage string (e.g., "45.2%").
-
