Package com.tnsai.agents.chat
Class AgentChatOrchestrator
java.lang.Object
com.tnsai.agents.chat.AgentChatOrchestrator
Handles chat orchestration for an Agent.
This class is extracted from Agent to separate chat concerns:
- LLM invocation and response processing
- History management during chat
- Tool call handling (delegation to toolExecutor)
- Tracing of chat operations
The orchestrator is created during agent initialization and delegates back to the agent for certain operations (like tool execution).
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceContext interface for Agent - provides access to agent internals needed for chat. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new AgentChatOrchestrator. -
Method Summary
Modifier and TypeMethodDescriptionchatInternal(String message, boolean useHistory) Internal chat without tool execution (prevents recursion in tool calls).chatInternal(String message, boolean useHistory, String retrievalQuery, ChatRetrievalContext retrievalContext) Internal no-tools follow-up preserving the originating RAG turn.chatInternalWithTools(String message, boolean useHistory) Internal chat WITH tools enabled for multi-turn tool loops.chatInternalWithTools(String message, boolean useHistory, String retrievalQuery, ChatRetrievalContext retrievalContext) Internal tool-loop follow-up preserving the originating RAG turn.<T> TchatWithFormat(String message, Class<T> targetClass, int maxRetries) Chat with structured output using annotation-defined format or JSON default.<T> TchatWithFormat(String message, Class<T> targetClass, int maxRetries, ChatRetrievalContext retrievalContext) Chat with annotation-defined output and retrieval context.<T> TchatWithFormat(String message, Class<T> targetClass, OutputFormat format, int maxRetries) Chat with structured output in a specific format.<T> TchatWithFormat(String message, Class<T> targetClass, OutputFormat format, int maxRetries, ChatRetrievalContext retrievalContext) Chat with structured output and request-local retrieval context.<T> TchatWithStructure(String message, OutputParser<T> parser, int maxRetries) Sends a message and returns a structured, validated response using guardrails pattern.<T> TchatWithStructure(String message, OutputParser<T> parser, int maxRetries, ChatRetrievalContext retrievalContext) Sends a structured chat turn with request-local retrieval context.Invokes the LLM with the given message.Invokes the LLM with the given message and tool settings.Invokes the LLM with full control over all settings.invokeLLM(String message, boolean useHistory, boolean useTools, boolean trace, ChatRetrievalContext retrievalContext) Invokes the LLM with request-local retrieval identity and lifecycle context.invokeLLM(String message, boolean useHistory, boolean useTools, boolean trace, String retrievalQuery, ChatRetrievalContext retrievalContext) Invokes the LLM while retrieving against the originating turn query.invokeReasoning(ReasoningStrategyHandle strategy, String message, ChatRetrievalContext retrievalContext, List<Map<String, Object>> tools, List<Map<String, Object>> history) Invokes a reasoning strategy through canonical chat grounding.processLLMResponse(Object response, boolean useHistory) Processes an LLM response, handling text responses and tool calls.processLLMResponse(Object response, boolean useHistory, String retrievalQuery, ChatRetrievalContext retrievalContext) Processes a response while preserving the originating RAG turn.processLLMResponseInternal(Object response, boolean useHistory) Internal version of processLLMResponse that does not trace.
-
Constructor Details
-
AgentChatOrchestrator
Creates a new AgentChatOrchestrator.- Parameters:
context- the chat context providing access to agent internals
-
-
Method Details
-
invokeLLM
Invokes the LLM with the given message.- Parameters:
message- the user messageuseHistory- whether to include conversation history- Returns:
- the LLM response (String or ChatResponse)
-
invokeLLM
Invokes the LLM with the given message and tool settings.- Parameters:
message- the user messageuseHistory- whether to include conversation historyuseTools- whether to include tools in the request- Returns:
- the LLM response (String or ChatResponse)
-
invokeLLM
Invokes the LLM with full control over all settings.- Parameters:
message- the user messageuseHistory- whether to include conversation historyuseTools- whether to include tools in the requesttrace- whether to trace the request/response- Returns:
- the LLM response (String or ChatResponse)
-
invokeLLM
public Object invokeLLM(String message, boolean useHistory, boolean useTools, boolean trace, ChatRetrievalContext retrievalContext) Invokes the LLM with request-local retrieval identity and lifecycle context. -
invokeLLM
public Object invokeLLM(String message, boolean useHistory, boolean useTools, boolean trace, String retrievalQuery, ChatRetrievalContext retrievalContext) Invokes the LLM while retrieving against the originating turn query. -
invokeReasoning
public ReasoningStrategyHandle.ReasoningResult invokeReasoning(ReasoningStrategyHandle strategy, String message, ChatRetrievalContext retrievalContext, List<Map<String, Object>> tools, List<Map<String, Object>> history) Invokes a reasoning strategy through canonical chat grounding. -
processLLMResponse
Processes an LLM response, handling text responses and tool calls.- Parameters:
response- the LLM response (String or ChatResponse)useHistory- whether to update conversation history- Returns:
- the processed response string
-
processLLMResponse
public String processLLMResponse(Object response, boolean useHistory, String retrievalQuery, ChatRetrievalContext retrievalContext) Processes a response while preserving the originating RAG turn. -
processLLMResponseInternal
Internal version of processLLMResponse that does not trace. Used by chatInternal to avoid duplicate trace output.- Parameters:
response- the LLM response (String or ChatResponse)useHistory- whether to update conversation history- Returns:
- the processed response string
-
chatInternal
Internal chat without tool execution (prevents recursion in tool calls). Does not trace request/response to avoid duplicate output.- Parameters:
message- the message to senduseHistory- whether to use conversation history- Returns:
- the LLM response
-
chatInternal
public String chatInternal(String message, boolean useHistory, String retrievalQuery, ChatRetrievalContext retrievalContext) Internal no-tools follow-up preserving the originating RAG turn. -
chatInternalWithTools
Internal chat WITH tools enabled for multi-turn tool loops. Returns raw Object (ChatResponse or String) so the caller can check for additional tool calls and continue the loop.- Parameters:
message- the message to send (typically tool execution results)useHistory- whether to use conversation history- Returns:
- raw LLM response (ChatResponse or String)
-
chatInternalWithTools
public Object chatInternalWithTools(String message, boolean useHistory, String retrievalQuery, ChatRetrievalContext retrievalContext) Internal tool-loop follow-up preserving the originating RAG turn. -
chatWithStructure
Sends a message and returns a structured, validated response using guardrails pattern.- Type Parameters:
T- the output type- Parameters:
message- the user message to sendparser- the output parser defining structure and validation rulesmaxRetries- maximum number of correction attempts (0 = no retries)- Returns:
- the parsed structured object
-
chatWithStructure
public <T> T chatWithStructure(String message, OutputParser<T> parser, int maxRetries, ChatRetrievalContext retrievalContext) Sends a structured chat turn with request-local retrieval context. The original query and context are retained across parse retries. -
chatWithFormat
public <T> T chatWithFormat(String message, Class<T> targetClass, OutputFormat format, int maxRetries) Chat with structured output in a specific format.- Type Parameters:
T- The output type- Parameters:
message- The user messagetargetClass- The expected output classformat- The output format (JSON, YAML, TOON, TONL)maxRetries- Maximum correction attempts- Returns:
- The parsed structured object
-
chatWithFormat
public <T> T chatWithFormat(String message, Class<T> targetClass, OutputFormat format, int maxRetries, ChatRetrievalContext retrievalContext) Chat with structured output and request-local retrieval context. -
chatWithFormat
Chat with structured output using annotation-defined format or JSON default.- Type Parameters:
T- The output type- Parameters:
message- The user messagetargetClass- The expected output classmaxRetries- Maximum correction attempts- Returns:
- The parsed structured object
-
chatWithFormat
public <T> T chatWithFormat(String message, Class<T> targetClass, int maxRetries, ChatRetrievalContext retrievalContext) Chat with annotation-defined output and retrieval context.
-