This is an automated email from the ASF dual-hosted git repository. orpiske pushed a commit to branch camel-4.14.x in repository https://gitbox.apache.org/repos/asf/camel.git
commit f649fea7e7c616c40137cd71fd4f6f28aa709f38 Author: Otavio Rodolfo Piske <angusyo...@gmail.com> AuthorDate: Mon Sep 1 13:03:53 2025 +0200 CAMEL-22397: provide the exchange information to the agent factory This should allow some context to be provided, allowing agents to be created according to their context (i.e.: cached, per route, etc.). --- .../langchain4j/agent/api/AgentFactory.java | 22 +++++++++++++++++++++- .../agent/LangChain4jAgentProducer.java | 2 +- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/components/camel-ai/camel-langchain4j-agent-api/src/main/java/org/apache/camel/component/langchain4j/agent/api/AgentFactory.java b/components/camel-ai/camel-langchain4j-agent-api/src/main/java/org/apache/camel/component/langchain4j/agent/api/AgentFactory.java index e7a3659e491..61cb3e6cfa4 100644 --- a/components/camel-ai/camel-langchain4j-agent-api/src/main/java/org/apache/camel/component/langchain4j/agent/api/AgentFactory.java +++ b/components/camel-ai/camel-langchain4j-agent-api/src/main/java/org/apache/camel/component/langchain4j/agent/api/AgentFactory.java @@ -18,6 +18,7 @@ package org.apache.camel.component.langchain4j.agent.api; import org.apache.camel.CamelContextAware; +import org.apache.camel.Exchange; /** * Factory interface for creating AI agent instances within the Apache Camel LangChain4j integration. @@ -50,5 +51,24 @@ public interface AgentFactory extends CamelContextAware { * @throws Exception if unable to create the agent due to configuration issues, missing dependencies, or * initialization failures */ - Agent createAgent() throws Exception; + @Deprecated + default Agent createAgent() throws Exception { + return createAgent(null); + } + + /** + * Creates a new AI agent instance configured with the appropriate settings. + * + * <p> + * Implementations may choose to cache agent instances for performance optimization, especially when the underlying + * configuration remains unchanged. The returned agent will be fully configured and ready to handle chat + * interactions. + * </p> + * @param exchange the exchange being processed which is triggering the creation of the agent + * + * @return a configured {@link Agent} instance ready for chat interactions + * @throws Exception if unable to create the agent due to configuration issues, missing dependencies, or + * initialization failures + */ + Agent createAgent(Exchange exchange) throws Exception; } diff --git a/components/camel-ai/camel-langchain4j-agent/src/main/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentProducer.java b/components/camel-ai/camel-langchain4j-agent/src/main/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentProducer.java index 40d4d3a84d9..2e7a920037b 100644 --- a/components/camel-ai/camel-langchain4j-agent/src/main/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentProducer.java +++ b/components/camel-ai/camel-langchain4j-agent/src/main/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentProducer.java @@ -60,7 +60,7 @@ public class LangChain4jAgentProducer extends DefaultProducer { Agent agent; if (agentFactory != null) { - agent = agentFactory.createAgent(); + agent = agentFactory.createAgent(exchange); } else { agent = endpoint.getConfiguration().getAgent(); }