This is an automated email from the ASF dual-hosted git repository. JiriOndrusek pushed a commit to branch 3.40.x in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git
commit 7400e37a3f029c5a4baec8ed46f8a816c8e0eebe Author: Jiří Ondrušek <[email protected]> AuthorDate: Wed Nov 25 16:43:00 2026 +0100 ai-tool: test the Camel agent exclusion and nested argSchema, document hints Fixes #9545 Fixes #9532 End-to-end test that a Camel langchain4j-agent with tags sees only its tagged tools, unit tests of isCamelAgentRequest and its interface name constants, and a nested classpath argSchema regression test through the LangChain4j bridge. Docs: title and MCP hints are not passed to Quarkus LangChain4j; native note for a classpath argSchema. Co-Authored-By: Claude Opus 5.5 <[email protected]> Co-authored-by: jiriOndrusek-agent <[email protected]> Co-authored-by: Claude Sonnet 5.5 <[email protected]> (cherry picked from commit 204716fefc96d24c18462893d4ef871dcd39c2de) --- .../ROOT/pages/reference/extensions/ai-tool.adoc | 4 +- extensions/ai-tool/deployment/pom.xml | 5 ++ .../component/ai/tool/CamelAiToolProviderTest.java | 73 ++++++++++++++++++++++ extensions/ai-tool/runtime/src/main/doc/usage.adoc | 4 +- .../component/ai/tool/CamelAiToolProvider.java | 2 +- .../langchain4j/it/AiToolLangchain4jResource.java | 12 ++++ .../langchain4j/it/AiToolLangchain4jRoutes.java | 17 +++++ .../langchain4j/it/model/ScriptedChatModel.java | 18 ++++-- .../src/main/resources/ai-tool/order-schema.json | 26 ++++++++ .../src/main/resources/application.properties | 1 + .../tool/langchain4j/it/AiToolLangchain4jTest.java | 31 ++++++++- 11 files changed, 185 insertions(+), 8 deletions(-) diff --git a/docs/modules/ROOT/pages/reference/extensions/ai-tool.adoc b/docs/modules/ROOT/pages/reference/extensions/ai-tool.adoc index 123754cc174..aeca838df00 100644 --- a/docs/modules/ROOT/pages/reference/extensions/ai-tool.adoc +++ b/docs/modules/ROOT/pages/reference/extensions/ai-tool.adoc @@ -80,7 +80,7 @@ Arguments passed by the LLM are set as exchange headers (e.g. `${header.city}`). [id="extensions-ai-tool-usage-schema-mcp-hints-and-return-direct"] === Schema, MCP hints and return direct -Instead of `parameter.*` options, `argSchema` takes a raw JSON Schema, inline or as a resource such as `classpath:schemas/order.json`. The two are mutually exclusive. The schema needs an object root with at least one property in `properties`. Only the top-level properties are set as headers. +Instead of `parameter.*` options, `argSchema` takes a raw JSON Schema, inline or as a resource such as `classpath:schemas/order.json`. The two are mutually exclusive. The schema needs an object root with at least one property in `properties`. Only the top-level properties are set as headers. In native mode, a `classpath:` schema must be added to the native image, e.g. `quarkus.native.resources.includes=schemas/order.json`. Further options (see the Camel `ai-tool` documentation for details): @@ -88,6 +88,8 @@ Further options (see the Camel `ai-tool` documentation for details): * `readOnlyHint`, `destructiveHint`, `idempotentHint`, `openWorldHint` - MCP hints. They are advisory, Camel does not enforce them, and an unset hint is not published. * `returnDirect` - AI producers that support it (such as `openai`) return the tool result to the caller without sending it back to the model. Also published as an MCP hint. +`title` and the MCP hints are not passed to Quarkus LangChain4j AI services: their tool specification has only the name, description and parameters (`ToolSpecification.metadata()` is empty). + [source,java] ---- from("ai-tool:createOrder?tags=orders&title=Create order&returnDirect=true" diff --git a/extensions/ai-tool/deployment/pom.xml b/extensions/ai-tool/deployment/pom.xml index a2504cf6361..80fd0ca9cd8 100644 --- a/extensions/ai-tool/deployment/pom.xml +++ b/extensions/ai-tool/deployment/pom.xml @@ -49,6 +49,11 @@ <artifactId>langchain4j</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-langchain4j-agent</artifactId> + <scope>test</scope> + </dependency> </dependencies> <build> diff --git a/extensions/ai-tool/deployment/src/test/java/org/apache/camel/quarkus/component/ai/tool/CamelAiToolProviderTest.java b/extensions/ai-tool/deployment/src/test/java/org/apache/camel/quarkus/component/ai/tool/CamelAiToolProviderTest.java new file mode 100644 index 00000000000..76183d30ceb --- /dev/null +++ b/extensions/ai-tool/deployment/src/test/java/org/apache/camel/quarkus/component/ai/tool/CamelAiToolProviderTest.java @@ -0,0 +1,73 @@ +/* + * 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.quarkus.component.ai.tool; + +import dev.langchain4j.data.message.UserMessage; +import dev.langchain4j.invocation.InvocationContext; +import dev.langchain4j.service.tool.ToolProviderRequest; +import org.apache.camel.component.langchain4j.agent.api.AiAgentWithMemoryService; +import org.apache.camel.component.langchain4j.agent.api.AiAgentWithoutMemoryService; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class CamelAiToolProviderTest { + + @Test + void camelAgentInterfacesAreDetected() { + assertTrue(CamelAiToolProvider.isCamelAgentRequest(request(AiAgentWithMemoryService.class.getName()))); + assertTrue(CamelAiToolProvider.isCamelAgentRequest(request(AiAgentWithoutMemoryService.class.getName()))); + } + + @Test + void otherInterfaceIsNotCamelAgent() { + assertFalse(CamelAiToolProvider.isCamelAgentRequest(request("org.acme.MyAiService"))); + } + + @Test + void nullInvocationContextIsNotCamelAgent() { + ToolProviderRequest request = new ToolProviderRequest(builder("org.acme.MyAiService")) { + @Override + public InvocationContext invocationContext() { + return null; + } + }; + assertFalse(CamelAiToolProvider.isCamelAgentRequest(request)); + } + + // The constants are names, not class literals: fail when Camel renames or moves an agent service interface + @Test + void interfaceNameConstantsResolve() throws ClassNotFoundException { + assertEquals(2, CamelAiToolProvider.CAMEL_AGENT_SERVICE_INTERFACES.size()); + for (String name : CamelAiToolProvider.CAMEL_AGENT_SERVICE_INTERFACES) { + Class<?> type = Class.forName(name); + assertTrue(type == AiAgentWithMemoryService.class || type == AiAgentWithoutMemoryService.class, name); + } + } + + private static ToolProviderRequest request(String interfaceName) { + return new ToolProviderRequest(builder(interfaceName)); + } + + private static ToolProviderRequest.Builder builder(String interfaceName) { + return ToolProviderRequest.builder() + .invocationContext(InvocationContext.builder().interfaceName(interfaceName).build()) + .userMessage(UserMessage.from("hello")); + } +} diff --git a/extensions/ai-tool/runtime/src/main/doc/usage.adoc b/extensions/ai-tool/runtime/src/main/doc/usage.adoc index e2be779e2cc..e6a62b10616 100644 --- a/extensions/ai-tool/runtime/src/main/doc/usage.adoc +++ b/extensions/ai-tool/runtime/src/main/doc/usage.adoc @@ -29,7 +29,7 @@ Arguments passed by the LLM are set as exchange headers (e.g. `${header.city}`). === Schema, MCP hints and return direct -Instead of `parameter.*` options, `argSchema` takes a raw JSON Schema, inline or as a resource such as `classpath:schemas/order.json`. The two are mutually exclusive. The schema needs an object root with at least one property in `properties`. Only the top-level properties are set as headers. +Instead of `parameter.*` options, `argSchema` takes a raw JSON Schema, inline or as a resource such as `classpath:schemas/order.json`. The two are mutually exclusive. The schema needs an object root with at least one property in `properties`. Only the top-level properties are set as headers. In native mode, a `classpath:` schema must be added to the native image, e.g. `quarkus.native.resources.includes=schemas/order.json`. Further options (see the Camel `ai-tool` documentation for details): @@ -37,6 +37,8 @@ Further options (see the Camel `ai-tool` documentation for details): * `readOnlyHint`, `destructiveHint`, `idempotentHint`, `openWorldHint` - MCP hints. They are advisory, Camel does not enforce them, and an unset hint is not published. * `returnDirect` - AI producers that support it (such as `openai`) return the tool result to the caller without sending it back to the model. Also published as an MCP hint. +`title` and the MCP hints are not passed to Quarkus LangChain4j AI services: their tool specification has only the name, description and parameters (`ToolSpecification.metadata()` is empty). + [source,java] ---- from("ai-tool:createOrder?tags=orders&title=Create order&returnDirect=true" diff --git a/extensions/ai-tool/runtime/src/main/java/org/apache/camel/quarkus/component/ai/tool/CamelAiToolProvider.java b/extensions/ai-tool/runtime/src/main/java/org/apache/camel/quarkus/component/ai/tool/CamelAiToolProvider.java index 9fa08dfb481..7960d9681a1 100644 --- a/extensions/ai-tool/runtime/src/main/java/org/apache/camel/quarkus/component/ai/tool/CamelAiToolProvider.java +++ b/extensions/ai-tool/runtime/src/main/java/org/apache/camel/quarkus/component/ai/tool/CamelAiToolProvider.java @@ -48,7 +48,7 @@ public class CamelAiToolProvider implements ToolProvider { private static final Logger LOG = Logger.getLogger(CamelAiToolProvider.class); static final Map<String, String> TAG_MAP = new ConcurrentHashMap<>(); - private static final Set<String> CAMEL_AGENT_SERVICE_INTERFACES = Set.of( + static final Set<String> CAMEL_AGENT_SERVICE_INTERFACES = Set.of( "org.apache.camel.component.langchain4j.agent.api.AiAgentWithMemoryService", "org.apache.camel.component.langchain4j.agent.api.AiAgentWithoutMemoryService"); diff --git a/integration-tests/ai-tool-langchain4j/src/main/java/org/apache/camel/quarkus/component/ai/tool/langchain4j/it/AiToolLangchain4jResource.java b/integration-tests/ai-tool-langchain4j/src/main/java/org/apache/camel/quarkus/component/ai/tool/langchain4j/it/AiToolLangchain4jResource.java index 9a76852ddd6..2c42c7c011f 100644 --- a/integration-tests/ai-tool-langchain4j/src/main/java/org/apache/camel/quarkus/component/ai/tool/langchain4j/it/AiToolLangchain4jResource.java +++ b/integration-tests/ai-tool-langchain4j/src/main/java/org/apache/camel/quarkus/component/ai/tool/langchain4j/it/AiToolLangchain4jResource.java @@ -32,6 +32,7 @@ import jakarta.ws.rs.PathParam; import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.MediaType; import org.apache.camel.CamelContext; +import org.apache.camel.ProducerTemplate; import org.apache.camel.component.ai.tool.AiToolRegistry; import org.apache.camel.component.ai.tool.AiToolSpec; import org.apache.camel.quarkus.component.ai.tool.langchain4j.it.service.AdminAiService; @@ -47,6 +48,9 @@ public class AiToolLangchain4jResource { @Inject CamelContext camelContext; + @Inject + ProducerTemplate producerTemplate; + @Inject WeatherAiService weatherAiService; @@ -113,6 +117,14 @@ public class AiToolLangchain4jResource { return weatherStreamingAiService.chat(message).collect().with(Collectors.joining()); } + @Path("/camel-agent/chat") + @POST + @Consumes(MediaType.TEXT_PLAIN) + @Produces(MediaType.TEXT_PLAIN) + public String camelAgentChat(String message) { + return producerTemplate.requestBody("direct:camelAgent", message, String.class); + } + @Path("/tools/{tag}") @GET @Produces(MediaType.TEXT_PLAIN) diff --git a/integration-tests/ai-tool-langchain4j/src/main/java/org/apache/camel/quarkus/component/ai/tool/langchain4j/it/AiToolLangchain4jRoutes.java b/integration-tests/ai-tool-langchain4j/src/main/java/org/apache/camel/quarkus/component/ai/tool/langchain4j/it/AiToolLangchain4jRoutes.java index 198fe55251c..538f3e8b97d 100644 --- a/integration-tests/ai-tool-langchain4j/src/main/java/org/apache/camel/quarkus/component/ai/tool/langchain4j/it/AiToolLangchain4jRoutes.java +++ b/integration-tests/ai-tool-langchain4j/src/main/java/org/apache/camel/quarkus/component/ai/tool/langchain4j/it/AiToolLangchain4jRoutes.java @@ -16,7 +16,12 @@ */ package org.apache.camel.quarkus.component.ai.tool.langchain4j.it; +import dev.langchain4j.memory.chat.MessageWindowChatMemory; import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.langchain4j.agent.api.AgentConfiguration; +import org.apache.camel.component.langchain4j.agent.api.AgentWithMemory; +import org.apache.camel.component.langchain4j.agent.api.Headers; +import org.apache.camel.quarkus.component.ai.tool.langchain4j.it.model.ScriptedChatModel; public class AiToolLangchain4jRoutes extends RouteBuilder { @Override @@ -39,5 +44,17 @@ public class AiToolLangchain4jRoutes extends RouteBuilder { from("ai-tool:failingTool?tags=failTag&description=A tool that always fails") .throwException(new IllegalStateException("Internal failure detail")); + + from("ai-tool:createOrder?tags=orderTag&description=Create an order&argSchema=classpath:ai-tool/order-schema.json") + .setBody(simple( + "${header.customer[name]},${header.items[0][sku]},${header.items[0][quantity]},${header.priority}")); + + // A Camel agent selects its tools by the tags parameter only, CamelAiToolProvider must not add the others + bindToRegistry("camelAgent", new AgentWithMemory(new AgentConfiguration() + .withChatModel(new ScriptedChatModel().get()) + .withChatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(10)))); + from("direct:camelAgent") + .setHeader(Headers.MEMORY_ID, constant(1)) + .to("langchain4j-agent:camelAgent?agent=#camelAgent&tags=weatherTag"); } } diff --git a/integration-tests/ai-tool-langchain4j/src/main/java/org/apache/camel/quarkus/component/ai/tool/langchain4j/it/model/ScriptedChatModel.java b/integration-tests/ai-tool-langchain4j/src/main/java/org/apache/camel/quarkus/component/ai/tool/langchain4j/it/model/ScriptedChatModel.java index 75d5bca0891..5207e117da2 100644 --- a/integration-tests/ai-tool-langchain4j/src/main/java/org/apache/camel/quarkus/component/ai/tool/langchain4j/it/model/ScriptedChatModel.java +++ b/integration-tests/ai-tool-langchain4j/src/main/java/org/apache/camel/quarkus/component/ai/tool/langchain4j/it/model/ScriptedChatModel.java @@ -32,7 +32,8 @@ import dev.langchain4j.model.chat.response.ChatResponse; /** * Mock model driven by the user message: {@code list} answers with the names of the offered tools (or - * {@code none}), {@code call <tool> <arguments>} calls the tool and answers with its result. + * {@code none}), {@code schema <tool>} answers with the JSON of the offered tool specification, + * {@code call <tool> <arguments>} calls the tool and answers with its result. */ public class ScriptedChatModel implements Supplier<ChatModel> { @@ -64,9 +65,18 @@ public class ScriptedChatModel implements Supplier<ChatModel> { } List<ToolSpecification> tools = chatRequest.toolSpecifications(); - return answer(tools == null || tools.isEmpty() - ? "none" - : tools.stream().map(ToolSpecification::name).sorted().collect(Collectors.joining(","))); + if (tools == null || tools.isEmpty()) { + return answer("none"); + } + if (text.startsWith("schema ")) { + String name = text.substring("schema ".length()); + return answer(tools.stream() + .filter(tool -> tool.name().equals(name)) + .map(ToolSpecification::toJson) + .findFirst() + .orElse("none")); + } + return answer(tools.stream().map(ToolSpecification::name).sorted().collect(Collectors.joining(","))); } }; } diff --git a/integration-tests/ai-tool-langchain4j/src/main/resources/ai-tool/order-schema.json b/integration-tests/ai-tool-langchain4j/src/main/resources/ai-tool/order-schema.json new file mode 100644 index 00000000000..ec386a2adb7 --- /dev/null +++ b/integration-tests/ai-tool-langchain4j/src/main/resources/ai-tool/order-schema.json @@ -0,0 +1,26 @@ +{ + "type": "object", + "properties": { + "customer": { + "type": "object", + "properties": { + "name": { "type": "string", "description": "The customer name" }, + "email": { "type": "string" } + }, + "required": ["name"] + }, + "items": { + "type": "array", + "items": { + "type": "object", + "properties": { + "sku": { "type": "string" }, + "quantity": { "type": "integer" } + }, + "required": ["sku", "quantity"] + } + }, + "priority": { "type": "string", "enum": ["LOW", "HIGH"] } + }, + "required": ["customer", "items"] +} diff --git a/integration-tests/ai-tool-langchain4j/src/main/resources/application.properties b/integration-tests/ai-tool-langchain4j/src/main/resources/application.properties index 4c329fe58fd..ae435818ea2 100644 --- a/integration-tests/ai-tool-langchain4j/src/main/resources/application.properties +++ b/integration-tests/ai-tool-langchain4j/src/main/resources/application.properties @@ -17,3 +17,4 @@ quarkus.devservices.enabled=false quarkus.langchain4j.devservices.enabled=false quarkus.langchain4j.ollama.chat-model.model-id=qwen3:1.7b +quarkus.native.resources.includes=ai-tool/order-schema.json diff --git a/integration-tests/ai-tool-langchain4j/src/test/java/org/apache/camel/quarkus/component/ai/tool/langchain4j/it/AiToolLangchain4jTest.java b/integration-tests/ai-tool-langchain4j/src/test/java/org/apache/camel/quarkus/component/ai/tool/langchain4j/it/AiToolLangchain4jTest.java index 07b34317d10..69b5d52ed18 100644 --- a/integration-tests/ai-tool-langchain4j/src/test/java/org/apache/camel/quarkus/component/ai/tool/langchain4j/it/AiToolLangchain4jTest.java +++ b/integration-tests/ai-tool-langchain4j/src/test/java/org/apache/camel/quarkus/component/ai/tool/langchain4j/it/AiToolLangchain4jTest.java @@ -18,11 +18,13 @@ package org.apache.camel.quarkus.component.ai.tool.langchain4j.it; import io.quarkus.test.junit.QuarkusTest; import io.restassured.RestAssured; +import io.restassured.parsing.Parser; import io.restassured.response.ValidatableResponse; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; +import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; @@ -104,7 +106,7 @@ class AiToolLangchain4jTest { @Test void serviceWithoutCamelAiToolsReceivesAllTools() { chat("all-tools", "list") - .body(is("failingTool,getNews,getWeather")); + .body(is("createOrder,failingTool,getNews,getWeather")); } @Test @@ -119,6 +121,33 @@ class AiToolLangchain4jTest { .body(is("Tool execution failed")); } + // Without the isCamelAgentRequest guard in CamelAiToolProvider, the agent gets every tool plus a duplicate getWeather + @Test + void camelAgentReceivesOnlyItsTaggedTools() { + chat("camel-agent", "list") + .body(is("getWeather")); + } + + @Test + void nestedArgSchemaReachesModel() { + chat("all-tools", "schema createOrder") + .parser("text/plain", Parser.JSON) + .body("parameters.properties.customer.properties.name.type", is("string")) + .body("parameters.properties.customer.required", contains("name")) + .body("parameters.properties.items.type", is("array")) + .body("parameters.properties.items.items.properties.quantity.type", is("integer")) + .body("parameters.properties.items.items.required", contains("sku", "quantity")) + .body("parameters.properties.priority.enum", contains("LOW", "HIGH")) + .body("parameters.required", contains("customer", "items")); + } + + @Test + void nestedArgumentsReachRoute() { + chat("all-tools", + "call createOrder {\"customer\":{\"name\":\"Joe\"},\"items\":[{\"sku\":\"A1\",\"quantity\":2}],\"priority\":\"HIGH\"}") + .body(is("Joe,A1,2,HIGH")); + } + private static ValidatableResponse chat(String service, String message) { return RestAssured.given() .contentType("text/plain")
