gnodet-bot commented on code in PR #26607:
URL: https://github.com/apache/camel/pull/26607#discussion_r4051400864
##########
dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/TuiToolDefinitions.java:
##########
@@ -500,22 +500,27 @@ private static void addLogTools(List<ToolDef> tools) {
private static void addExampleTools(List<ToolDef> tools) {
tools.add(toToolDef(toolDef(
"tui_list_examples",
- "Returns the list of available bundled Camel examples as
structured JSON. "
- + "Each example has: name, title,
description, level, category, tags, "
- + "bundled, requiresDocker,
infraServices. "
+ "Returns the Camel examples as structured JSON, grouped as the
ladder of the examples "
+ + "(quick-start, run, transform, route,
fail-well, connect, connect-service, "
+ + "contracts, ai, cloud, showcase) in
reading order: the groups with level, "
+ + "title, intro and count, and the
examples with name, title, description, "
+ + "level, order, tags, teaches
(components, EIPs, languages, data formats), "
+ + "bundled, requiresDocker and
infraServices. "
+ + "Call it without arguments for the
whole ladder, with level for one group. "
+ "Use the 'name' field with
tui_run_example to launch one.",
Map.of("filter", propDef("string",
"Case-insensitive substring filter on name, title,
description, level, or tags"),
"level", propDef("string",
- "Filter by difficulty level: beginner,
intermediate, or advanced")))));
+ "Only the examples of one group: quick-start,
run, transform, route, fail-well, "
+ + "connect,
connect-service, contracts, ai, cloud or showcase")))));
Review Comment:
⚠️ **Missing `limit` parameter — `tui_list_examples` is now unbounded while
the other two implementations cap at 50.**
`list_examples` (ToolRegistry) and `camel_catalog_examples` (ExampleTools)
both accept a `limit` parameter (default 50). `tui_list_examples` doesn't — so
an LLM calling it gets the entire catalog dumped in one go, with no way to page
or cap it. Add the `limit` property here, and add the corresponding cap loop in
`TuiToolRegistry.callListExamples`.
```suggestion
Map.of("filter", propDef("string",
"Case-insensitive substring filter on name, title,
description, level, or tags"),
"level", propDef("string",
"Only the examples of one group:
quick-start, run, transform, route, fail-well, "
+ "connect,
connect-service, contracts, ai, cloud or showcase"),
"limit", propDef("integer",
"Maximum number of examples to return
(default: 50)")))));
```
##########
dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/TuiToolRegistry.java:
##########
@@ -1713,32 +1713,45 @@ private String callListExamples(Map<String, Object>
args) {
if (filter != null && !filter.isEmpty()) {
filtered = ExampleHelper.filterExamples(filtered, filter);
}
- if (level != null && !level.isEmpty()) {
- String lowerLevel = level.toLowerCase();
- filtered = filtered.stream()
- .filter(e ->
lowerLevel.equals(e.getStringOrDefault("level", "")))
- .toList();
- }
+ JsonArray groups = new JsonArray();
JsonArray examples = new JsonArray();
- for (JsonObject entry : filtered) {
- JsonObject ex = new JsonObject();
- ex.put("name", entry.getStringOrDefault("name", ""));
- ex.put("title", entry.getStringOrDefault("title", ""));
- ex.put("description", entry.getStringOrDefault("description", ""));
- ex.put("level", entry.getStringOrDefault("level", ""));
- ex.put("category", ExampleHelper.getCategory(entry));
- ex.put("tags", toJsonArray(
- entry.get("tags") instanceof java.util.Collection<?> c
- ? c.stream().map(Object::toString).toList()
- : List.of()));
- ex.put("bundled", ExampleHelper.isBundled(entry));
- ex.put("requiresDocker", ExampleHelper.requiresDocker(entry));
- ex.put("infraServices",
toJsonArray(ExampleHelper.getInfraServices(entry)));
- examples.add(ex);
+ for (Map.Entry<String, List<JsonObject>> group :
ExampleHelper.groupByLevel(filtered).entrySet()) {
+ if (level != null && !level.isEmpty() &&
!group.getKey().equalsIgnoreCase(level)) {
+ continue;
+ }
+ JsonObject g = new JsonObject();
+ g.put("level", group.getKey());
+ g.put("title", ExampleHelper.getGroupTitle(group.getKey()));
+ g.put("intro", ExampleHelper.getGroupIntro(group.getKey()));
+ g.put("count", group.getValue().size());
+ groups.add(g);
+ for (JsonObject entry : group.getValue()) {
+ JsonObject ex = new JsonObject();
+ ex.put("name", entry.getStringOrDefault("name", ""));
+ ex.put("title", entry.getStringOrDefault("title", ""));
+ ex.put("description", entry.getStringOrDefault("description",
""));
+ ex.put("level", entry.getStringOrDefault("level", ""));
+ if (ExampleHelper.getOrder(entry) != Integer.MAX_VALUE) {
+ ex.put("order", ExampleHelper.getOrder(entry));
+ }
+ ex.put("category", ExampleHelper.getCategory(entry));
+ ex.put("tags", toJsonArray(
+ entry.get("tags") instanceof java.util.Collection<?> c
+ ? c.stream().map(Object::toString).toList()
+ : List.of()));
+ JsonObject teaches = new JsonObject();
+ ExampleHelper.getTeaches(entry).forEach((k, v) ->
teaches.put(k, toJsonArray(v)));
+ ex.put("teaches", teaches);
+ ex.put("bundled", ExampleHelper.isBundled(entry));
+ ex.put("requiresDocker", ExampleHelper.requiresDocker(entry));
+ ex.put("infraServices",
toJsonArray(ExampleHelper.getInfraServices(entry)));
+ examples.add(ex);
+ }
}
JsonObject result = new JsonObject();
+ result.put("groups", groups);
result.put("examples", examples);
result.put("totalCount", examples.size());
Review Comment:
⚠️ **Schema inconsistency: `totalCount` vs `count`/`total` across the three
tool implementations.**
`ToolRegistry.list_examples` returns `{count, total, groups, examples}`.
`ExampleTools.camel_catalog_examples` returns a record with `count()`,
`total()`, `groups()`, `examples()`. This `TuiToolRegistry.tui_list_examples`
returns `{groups, examples, totalCount}` — no `count`, no `total`, and the
field is named differently. An agent or test that switches between tools will
break silently. Align the field names:
```suggestion
JsonObject result = new JsonObject();
result.put("groups", groups);
result.put("examples", examples);
result.put("count", examples.size());
result.put("total", groups.stream().mapToInt(g -> ((Number)
g.get("count")).intValue()).sum());
```
Also add a `limit` cap in the examples-assembly loop (parallel to the fix
needed in `TuiToolDefinitions`).
##########
dsl/camel-jbang/camel-jbang-mcp/src/test/java/org/apache/camel/dsl/jbang/core/commands/mcp/ExampleToolsTest.java:
##########
@@ -0,0 +1,70 @@
+/*
+ * 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.dsl.jbang.core.commands.mcp;
+
+import java.util.List;
+
+import org.apache.camel.dsl.jbang.core.common.ExampleHelper;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+class ExampleToolsTest {
+
+ @Test
+ void listsTheWholeLadderInReadingOrderWithoutArguments() {
+ ExampleTools.ExampleListResult all = new
ExampleTools().camel_catalog_examples(null, null, null);
+
+ assertThat(all.count()).isEqualTo(all.total());
+ assertThat(all.groups()).isNotEmpty();
+ List<String> levels =
all.groups().stream().map(ExampleTools.GroupInfo::level).toList();
+ assertThat(levels).isSubsetOf(ExampleHelper.getGroupOrder());
+ assertThat(levels).isSortedAccordingTo((a, b) -> Integer.compare(
+ ExampleHelper.getGroupOrder().indexOf(a),
ExampleHelper.getGroupOrder().indexOf(b)));
+ assertThat(levels.get(0)).isEqualTo("quick-start");
+ ExampleTools.GroupInfo first = all.groups().get(0);
+
assertThat(first.title()).isEqualTo(ExampleHelper.getGroupTitle("quick-start"));
+ assertThat(first.intro()).isNotBlank();
+
+ // the examples follow the groups in order
+ List<String> exampleLevels =
all.examples().stream().map(ExampleTools.ExampleInfo::level).distinct().toList();
+ assertThat(exampleLevels).isEqualTo(levels);
+
assertThat(all.examples().stream().map(ExampleTools.ExampleInfo::name)).contains("quick-start/timer-log");
+ }
+
+ @Test
+ void filtersOneGroupAndReturnsWhatTheExamplesTeach() {
+ ExampleTools.ExampleListResult run = new
ExampleTools().camel_catalog_examples(null, "run", null);
+
+ assertThat(run.groups()).hasSize(1);
+ assertThat(run.groups().get(0).level()).isEqualTo("run");
+ assertThat(run.groups().get(0).count()).isEqualTo(run.total());
+ assertThat(run.examples()).allMatch(e -> "run".equals(e.level()));
+ assertThat(run.examples()).isSortedAccordingTo((a, b) ->
Integer.compare(a.order(), b.order()));
Review Comment:
🐛 **NPE if any example in the `run` group has no `order` metadata.**
`ExampleInfo.order()` is `Integer` (nullable) — `toExampleInfo` sets it to
`null` when `getOrder(entry) == Integer.MAX_VALUE`. `Integer.compare(a.order(),
b.order())` auto-unboxes both — if either is `null`, this throws
`NullPointerException` at test runtime. The `run` group may contain examples
without an `order` field in the catalog.
Use a null-safe comparator:
```suggestion
assertThat(run.examples()).isSortedAccordingTo(
Comparator.comparingInt(e -> e.order() != null ? e.order() :
Integer.MAX_VALUE));
```
Same issue on line 36 for the `isSortedAccordingTo` on group levels (though
`getGroupOrder().indexOf(a)` returns -1 for unknowns, not null, so that one is
safe — just this line needs fixing).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]