From f3588e9d9e486055452873d1908bbda28b331b99 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Fri, 4 Sep 2026 09:12:08 +0000 Subject: [PATCH] [backport camel-4.22.x] CAMEL-24614: camel-langchain4j-agent - fail fast when no agent can be resolved --- .../agent/LangChain4jAgentProducer.java | 12 ++++ .../LangChain4jAgentMissingAgentTest.java | 60 +++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 components/camel-ai/camel-langchain4j-agent/src/test/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentMissingAgentTest.java 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 197481eb90543..797fcda39bb9e 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 @@ -137,6 +137,18 @@ public void process(Exchange exchange) throws Exception { String tags = endpoint.getConfiguration().getTags(); Agent agent = agentFactory != null ? agentFactory.createAgent(exchange) : this.agent; + if (agent == null && agentFactory == null) { + // Support an agent configured on the endpoint after the route started, and give a clear error + // instead of an opaque NullPointerException when nothing resolves to an agent. + agent = endpoint.getConfiguration().getAgent(); + if (agent == null) { + throw new IllegalArgumentException( + "No agent could be resolved for endpoint " + endpoint.getEndpointUri() + + ". Configure 'agent', 'agentConfiguration' or 'agentFactory', or bind a bean named '" + + endpoint.getAgentId() + "' of type " + Agent.class.getName() + + " in the registry."); + } + } AiAgentBody aiAgentBody = exchange.getMessage().getMandatoryBody(AiAgentBody.class); diff --git a/components/camel-ai/camel-langchain4j-agent/src/test/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentMissingAgentTest.java b/components/camel-ai/camel-langchain4j-agent/src/test/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentMissingAgentTest.java new file mode 100644 index 0000000000000..76c2549429d9b --- /dev/null +++ b/components/camel-ai/camel-langchain4j-agent/src/test/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentMissingAgentTest.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.langchain4j.agent; + +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.langchain4j.agent.api.AiAgentBody; +import org.apache.camel.impl.DefaultCamelContext; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class LangChain4jAgentMissingAgentTest { + + @Test + void missingAgentReportsAClearError() throws Exception { + try (DefaultCamelContext context = new DefaultCamelContext()) { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() { + // No agent / agentConfiguration / agentFactory, and no registry bean named "noSuchAgent". + from("direct:x").to("langchain4j-agent:noSuchAgent"); + } + }); + context.start(); + + Exchange result = context.createProducerTemplate() + .request("direct:x", e -> e.getIn().setBody(new AiAgentBody<>("hello"))); + + Throwable cause = result.getException(); + assertNotNull(cause, "an error was expected"); + boolean clearError = false; + while (cause != null) { + if (cause instanceof IllegalArgumentException && cause.getMessage() != null + && cause.getMessage().contains("No agent could be resolved")) { + clearError = true; + break; + } + cause = cause.getCause(); + } + assertTrue(clearError, "expected a clear 'No agent could be resolved' IllegalArgumentException, got: " + + result.getException()); + } + } +}