davsclaus commented on code in PR #26607:
URL: https://github.com/apache/camel/pull/26607#discussion_r4051488627
##########
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:
Added the limit property (default 50) and the cap in callListExamples, in
6a6fcffece09.
##########
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:
Aligned to count and total like the other two listings, and the limit cap is
in the loop, in 6a6fcffece09.
##########
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:
Null-safe comparator applied in 6a6fcffece09.
--
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]