davsclaus commented on code in PR #30542:
URL: https://github.com/apache/camel/pull/30542#discussion_r4651508131
##########
dsl/camel-jbang/camel-jbang-mcp/src/test/java/org/apache/camel/dsl/jbang/core/commands/mcp/GenAiTraceReportTest.java:
##########
@@ -17,239 +17,308 @@
package org.apache.camel.dsl.jbang.core.commands.mcp;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import org.apache.camel.component.ai.observability.GenAiAttributes;
-import org.apache.camel.component.ai.observability.GenAiMessages;
-import org.apache.camel.dsl.jbang.core.common.genai.SpanEntry;
+import
org.apache.camel.dsl.jbang.core.commands.mcp.GenAiTraceReport.Conversation;
+import org.apache.camel.dsl.jbang.core.commands.mcp.GenAiTraceReport.GenAi;
+import org.apache.camel.dsl.jbang.core.commands.mcp.GenAiTraceReport.Message;
+import org.apache.camel.dsl.jbang.core.commands.mcp.GenAiTraceReport.Run;
+import org.apache.camel.dsl.jbang.core.commands.mcp.GenAiTraceReport.RunStep;
import org.apache.camel.util.json.JsonArray;
import org.apache.camel.util.json.JsonObject;
+import org.apache.camel.util.json.Jsoner;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.tuple;
+/**
+ * CAMEL-27312: the GenAI part of camel_runtime_ai_trace and
camel_runtime_ai_conversation, from answers shaped as the
+ * genai dev console of camel-ai-observability gives them.
+ */
class GenAiTraceReportTest {
- private static final String QUESTION
- = "Order ORD-1003 has a Camel cap in it: is the order paid, and is
the cap in stock?";
-
@Test
void summaryComesFirstAndFitsInWhatAChatHistoryKeeps() {
- String summary = GenAiTraceReport.summary(run("run1", 1000, true),
"*");
+ String summary = GenAiTraceReport.summary(console("full", run("7",
"support")), "*");
- assertThat(summary).isEqualTo(
- "GenAI level=full: 1 agent run, 2 model calls (granite4:3b), 2
tool calls; runs newest first. "
- + "camel_runtime_ai_conversation with a
runId shows the messages of a run.");
- assertThat(summary.length()).isLessThan(400);
+ assertThat(summary).isEqualTo("GenAI level=full: 1 agent run, 2 model
calls (granite4:3b), 1 tool call; "
+ + "runs newest first.
camel_runtime_ai_conversation with a runId shows the "
+ + "messages of a run.");
+ assertThat(summary.length()).isLessThan(300);
}
@Test
void agentRunHasItsQuestionAndItsStepsInOrder() {
- List<SpanEntry> spans = run("run1", 1000, true);
-
- GenAiTraceReport.GenAi genAi = GenAiTraceReport.genAi(spans, null);
+ GenAi genAi = GenAiTraceReport.genAi(console("full", run("7",
"support")), "*");
assertThat(genAi.level()).isEqualTo("full");
- assertThat(genAi.modelCalls()).singleElement().satisfies(m -> {
- assertThat(m.model()).isEqualTo("granite4:3b");
- assertThat(m.calls()).isEqualTo(2);
- assertThat(m.inputTokens()).isEqualTo(694);
- assertThat(m.cachedTokens()).isEqualTo(585);
- assertThat(m.finishReasons()).isEqualTo("tool_calls,stop");
- });
- GenAiTraceReport.Run run = genAi.agentRuns().get(0);
- assertThat(run.runId()).isEqualTo("run1");
- assertThat(run.question()).isEqualTo(QUESTION);
- assertThat(run.exchangeId()).isEqualTo("EX-run1");
- assertThat(run.routeId()).isEqualTo("customer-questions");
- assertThat(run.steps()).extracting(GenAiTraceReport.RunStep::step)
- .containsExactly("model call", "tool call", "tool call",
"model call");
- assertThat(run.steps().get(1)).satisfies(step -> {
- assertThat(step.name()).isEqualTo("order_status");
-
assertThat(step.arguments()).isEqualTo("{\"orderId\":\"ORD-1003\"}");
- assertThat(step.result()).isEqualTo("Order ORD-1003 for customer
C-134 in US is pending with 3 line(s)");
- assertThat(step.inputTokens()).isNull();
+ assertThat(genAi.modelCalls()).singleElement().satisfies(model -> {
+ assertThat(model.model()).isEqualTo("granite4:3b");
+ assertThat(model.calls()).isEqualTo(2);
+ assertThat(model.cachedTokens()).isEqualTo(270);
+ assertThat(model.avgMs()).isEqualTo(130);
+ assertThat(model.failed()).isNull();
});
+ Run run = genAi.agentRuns().get(0);
+ assertThat(run.runId()).isEqualTo("7");
+ assertThat(run.question()).isEqualTo("Is the Camel cap in stock?");
+ assertThat(run.answer()).isEqualTo("No, it is sold out.");
+ assertThat(run.agent()).isEqualTo("support");
+ assertThat(run.start()).isEqualTo("2026-11-21T10:00:00Z");
+ assertThat(run.running()).isNull();
+ assertThat(run.inputTokens()).isEqualTo(700);
+ assertThat(run.cachedTokens()).isEqualTo(270);
+
assertThat(run.steps()).extracting(RunStep::step).containsExactly("model call",
"tool call", "model call");
+ RunStep tool = run.steps().get(1);
+ assertThat(tool.name()).isEqualTo("execute_tool lookupStock");
+ assertThat(tool.arguments()).isEqualTo("{\"sku\":\"CAMEL-CAP\"}");
+
assertThat(tool.result()).hasSize(GenAiTraceReport.MAX_STEP_TEXT).endsWith("...");
+ assertThat(run.steps().get(0).finishReason()).isEqualTo("tool_calls");
+ assertThat(run.steps().get(2).cachedTokens()).isNull();
assertThat(genAi.moreRuns()).isNull();
}
@Test
void newestFiveRunsAreShown() {
- List<SpanEntry> spans = new ArrayList<>();
- for (int i = 1; i <= 7; i++) {
- spans.addAll(run("run" + i, i * 10_000, false));
+ JsonObject[] runs = new JsonObject[7];
+ for (int i = 0; i < runs.length; i++) {
+ runs[i] = run(String.valueOf(100 - i), "support");
}
- GenAiTraceReport.GenAi genAi = GenAiTraceReport.genAi(spans, "*");
+ GenAi genAi = GenAiTraceReport.genAi(console("detailed", runs), "*");
- assertThat(genAi.level()).isEqualTo("detailed");
- assertThat(genAi.agentRuns()).extracting(GenAiTraceReport.Run::runId)
- .containsExactly("run7", "run6", "run5", "run4", "run3");
+
assertThat(genAi.agentRuns()).extracting(Run::runId).containsExactly("100",
"99", "98", "97", "96");
assertThat(genAi.moreRuns()).isEqualTo("+2 more runs");
Review Comment:
Thanks, done in 7603191321e5. The newest-5 test now uses runs shaped like a
level detailed answer (no question, answer or content), and asserts that the
run's question and answer and the tool step's arguments and result are null.
_Claude Code on behalf of Claus Ibsen_
##########
dsl/camel-jbang/camel-jbang-mcp/src/test/java/org/apache/camel/dsl/jbang/core/commands/mcp/GenAiTraceReportTest.java:
##########
@@ -17,239 +17,308 @@
package org.apache.camel.dsl.jbang.core.commands.mcp;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import org.apache.camel.component.ai.observability.GenAiAttributes;
-import org.apache.camel.component.ai.observability.GenAiMessages;
-import org.apache.camel.dsl.jbang.core.common.genai.SpanEntry;
+import
org.apache.camel.dsl.jbang.core.commands.mcp.GenAiTraceReport.Conversation;
+import org.apache.camel.dsl.jbang.core.commands.mcp.GenAiTraceReport.GenAi;
+import org.apache.camel.dsl.jbang.core.commands.mcp.GenAiTraceReport.Message;
+import org.apache.camel.dsl.jbang.core.commands.mcp.GenAiTraceReport.Run;
+import org.apache.camel.dsl.jbang.core.commands.mcp.GenAiTraceReport.RunStep;
import org.apache.camel.util.json.JsonArray;
import org.apache.camel.util.json.JsonObject;
+import org.apache.camel.util.json.Jsoner;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.tuple;
+/**
+ * CAMEL-27312: the GenAI part of camel_runtime_ai_trace and
camel_runtime_ai_conversation, from answers shaped as the
+ * genai dev console of camel-ai-observability gives them.
+ */
class GenAiTraceReportTest {
- private static final String QUESTION
- = "Order ORD-1003 has a Camel cap in it: is the order paid, and is
the cap in stock?";
-
@Test
void summaryComesFirstAndFitsInWhatAChatHistoryKeeps() {
- String summary = GenAiTraceReport.summary(run("run1", 1000, true),
"*");
+ String summary = GenAiTraceReport.summary(console("full", run("7",
"support")), "*");
- assertThat(summary).isEqualTo(
- "GenAI level=full: 1 agent run, 2 model calls (granite4:3b), 2
tool calls; runs newest first. "
- + "camel_runtime_ai_conversation with a
runId shows the messages of a run.");
- assertThat(summary.length()).isLessThan(400);
+ assertThat(summary).isEqualTo("GenAI level=full: 1 agent run, 2 model
calls (granite4:3b), 1 tool call; "
+ + "runs newest first.
camel_runtime_ai_conversation with a runId shows the "
+ + "messages of a run.");
+ assertThat(summary.length()).isLessThan(300);
}
@Test
void agentRunHasItsQuestionAndItsStepsInOrder() {
- List<SpanEntry> spans = run("run1", 1000, true);
-
- GenAiTraceReport.GenAi genAi = GenAiTraceReport.genAi(spans, null);
+ GenAi genAi = GenAiTraceReport.genAi(console("full", run("7",
"support")), "*");
assertThat(genAi.level()).isEqualTo("full");
- assertThat(genAi.modelCalls()).singleElement().satisfies(m -> {
- assertThat(m.model()).isEqualTo("granite4:3b");
- assertThat(m.calls()).isEqualTo(2);
- assertThat(m.inputTokens()).isEqualTo(694);
- assertThat(m.cachedTokens()).isEqualTo(585);
- assertThat(m.finishReasons()).isEqualTo("tool_calls,stop");
- });
- GenAiTraceReport.Run run = genAi.agentRuns().get(0);
- assertThat(run.runId()).isEqualTo("run1");
- assertThat(run.question()).isEqualTo(QUESTION);
- assertThat(run.exchangeId()).isEqualTo("EX-run1");
- assertThat(run.routeId()).isEqualTo("customer-questions");
- assertThat(run.steps()).extracting(GenAiTraceReport.RunStep::step)
- .containsExactly("model call", "tool call", "tool call",
"model call");
- assertThat(run.steps().get(1)).satisfies(step -> {
- assertThat(step.name()).isEqualTo("order_status");
-
assertThat(step.arguments()).isEqualTo("{\"orderId\":\"ORD-1003\"}");
- assertThat(step.result()).isEqualTo("Order ORD-1003 for customer
C-134 in US is pending with 3 line(s)");
- assertThat(step.inputTokens()).isNull();
+ assertThat(genAi.modelCalls()).singleElement().satisfies(model -> {
+ assertThat(model.model()).isEqualTo("granite4:3b");
+ assertThat(model.calls()).isEqualTo(2);
+ assertThat(model.cachedTokens()).isEqualTo(270);
+ assertThat(model.avgMs()).isEqualTo(130);
+ assertThat(model.failed()).isNull();
});
+ Run run = genAi.agentRuns().get(0);
+ assertThat(run.runId()).isEqualTo("7");
+ assertThat(run.question()).isEqualTo("Is the Camel cap in stock?");
+ assertThat(run.answer()).isEqualTo("No, it is sold out.");
+ assertThat(run.agent()).isEqualTo("support");
+ assertThat(run.start()).isEqualTo("2026-11-21T10:00:00Z");
+ assertThat(run.running()).isNull();
+ assertThat(run.inputTokens()).isEqualTo(700);
+ assertThat(run.cachedTokens()).isEqualTo(270);
+
assertThat(run.steps()).extracting(RunStep::step).containsExactly("model call",
"tool call", "model call");
+ RunStep tool = run.steps().get(1);
+ assertThat(tool.name()).isEqualTo("execute_tool lookupStock");
+ assertThat(tool.arguments()).isEqualTo("{\"sku\":\"CAMEL-CAP\"}");
+
assertThat(tool.result()).hasSize(GenAiTraceReport.MAX_STEP_TEXT).endsWith("...");
+ assertThat(run.steps().get(0).finishReason()).isEqualTo("tool_calls");
+ assertThat(run.steps().get(2).cachedTokens()).isNull();
assertThat(genAi.moreRuns()).isNull();
}
@Test
void newestFiveRunsAreShown() {
- List<SpanEntry> spans = new ArrayList<>();
- for (int i = 1; i <= 7; i++) {
- spans.addAll(run("run" + i, i * 10_000, false));
+ JsonObject[] runs = new JsonObject[7];
+ for (int i = 0; i < runs.length; i++) {
+ runs[i] = run(String.valueOf(100 - i), "support");
}
- GenAiTraceReport.GenAi genAi = GenAiTraceReport.genAi(spans, "*");
+ GenAi genAi = GenAiTraceReport.genAi(console("detailed", runs), "*");
- assertThat(genAi.level()).isEqualTo("detailed");
- assertThat(genAi.agentRuns()).extracting(GenAiTraceReport.Run::runId)
- .containsExactly("run7", "run6", "run5", "run4", "run3");
+
assertThat(genAi.agentRuns()).extracting(Run::runId).containsExactly("100",
"99", "98", "97", "96");
assertThat(genAi.moreRuns()).isEqualTo("+2 more runs");
- // level detailed records no messages: no question, no tool arguments
- assertThat(genAi.agentRuns().get(0).question()).isNull();
-
assertThat(genAi.agentRuns().get(0).steps().get(1).arguments()).isNull();
- assertThat(GenAiTraceReport.summary(spans, "*"))
- .startsWith("GenAI level=detailed: 7 agent runs, 14 model
calls (granite4:3b), 14 tool calls")
- .endsWith("The messages need
camel.aiObservability.level=full.");
}
@Test
- void summarySaysWhatToDoAtLevelBasicOrWithoutSpans() {
- SpanEntry chat = span("chat", null, "orders", 0, 100,
- attrs("chat", GenAiAttributes.SYSTEM, "openai",
GenAiAttributes.REQUEST_MODEL, "gpt-4.1"));
+ void summarySaysWhatToDoAtLevelBasicOrWithoutTheConsole() {
+ JsonObject basic = console("basic");
+ basic.put("modelCalls", 3L);
+ assertThat(GenAiTraceReport.summary(basic, "*")).isEqualTo(
+ "GenAI level=basic: 3 model calls (granite4:3b). Agent runs
and tool calls are not traced: set "
+ +
"camel.aiObservability.level=detailed.");
+
+ JsonObject absent = new JsonObject(Map.of("enabled", false));
+ assertThat(GenAiTraceReport.summary(absent,
"*")).isEqualTo(GenAiTraceReport.NO_CONSOLE);
+ assertThat(GenAiTraceReport.summary(null,
"*")).isEqualTo(GenAiTraceReport.NO_CONSOLE);
+ assertThat(GenAiTraceReport.genAi(absent, "*")).isNull();
- assertThat(GenAiTraceReport.summary(List.of(chat), "*"))
- .isEqualTo("GenAI level=basic: 1 model call (gpt-4.1). Agent
runs and tool calls are not traced: "
- + "set camel.aiObservability.level=detailed.");
- assertThat(GenAiTraceReport.summary(List.of(),
"*")).isEqualTo(GenAiTraceReport.NO_SPANS);
- assertThat(GenAiTraceReport.genAi(List.of(), "*")).isNull();
+ JsonObject off = console("detailed");
+ off.put("recording", false);
+ off.put("modelCalls", 0L);
+ off.put("agentRuns", 0L);
+ assertThat(GenAiTraceReport.summary(off,
"*")).isEqualTo(GenAiTraceReport.NOT_RECORDING);
+ assertThat(GenAiTraceReport.genAi(off, "*")).isNull();
}
@Test
void routeFilterKeepsTheRunsOfThatRoute() {
- assertThat(GenAiTraceReport.genAi(run("run1", 1000, true),
"other-route").agentRuns()).isNull();
- assertThat(GenAiTraceReport.genAi(run("run1", 1000, true),
"customer-questions").agentRuns()).hasSize(1);
- }
+ JsonObject console = console("detailed", run("9", "billing"), run("8",
"support"));
- @Test
- void spansAreReadFromTheDump() {
- JsonObject span = new JsonObject();
- span.put("traceId", "t1");
- span.put("spanId", "s1");
- span.put("name", "chat");
- JsonObject attributes = new JsonObject();
- attributes.put(GenAiAttributes.OPERATION_NAME, "chat");
- span.put("attributes", attributes);
- JsonObject dump = new JsonObject();
- dump.put("enabled", true);
- dump.put("spans", new JsonArray(List.of(span)));
-
- assertThat(GenAiTraceReport.spans(dump)).singleElement()
- .satisfies(s ->
assertThat(s.attributes()).containsEntry(GenAiAttributes.OPERATION_NAME,
"chat"));
- dump.put("enabled", false);
- assertThat(GenAiTraceReport.spans(dump)).isEmpty();
- assertThat(GenAiTraceReport.spans(null)).isEmpty();
+ assertThat(GenAiTraceReport.genAi(console,
"support").agentRuns()).extracting(Run::runId).containsExactly("8");
+ assertThat(GenAiTraceReport.summary(console, "support"))
+ .contains("(all routes; the runs below are of route support)");
}
@Test
void conversationListsTheMessagesOfTheRunInOrder() {
- GenAiTraceReport.Conversation conversation =
GenAiTraceReport.conversation(run("run1", 1000, true), "run1");
+ Conversation conversation =
GenAiTraceReport.conversation(answer(run("7", "support")), "7");
assertThat(conversation.level()).isEqualTo("full");
- assertThat(conversation.summary()).startsWith("GenAI level=full: 7
message parts of run run1.");
- assertThat(conversation.messages())
- .extracting(m -> m.role() + ":" + m.type() + (m.tool() != null
? ":" + m.tool() : ""))
- .containsExactly("system:text", "user:text", "assistant:tool
call:order_status",
- "assistant:tool call:stock_level", "tool:tool
result:order_status",
- "tool:tool result:stock_level", "assistant:text");
- assertThat(conversation.messages().get(6).text())
- .isEqualTo("Your order ORD-1003 is still pending and the Camel
cap is currently out of stock.");
- assertThat(conversation.cut()).isNull();
+ assertThat(conversation.agent()).isEqualTo("support");
+ assertThat(conversation.messages()).extracting(Message::role,
Message::type).containsExactly(
+ tuple("system", "text"), tuple("user", "text"),
tuple("assistant", "tool call"),
+ tuple("tool", "tool result"), tuple("assistant", "text"));
+
assertThat(conversation.messages().get(2).tool()).isEqualTo("lookupStock");
+
assertThat(conversation.messages().get(2).text()).isEqualTo("{\"sku\":\"CAMEL-CAP\"}");
+
assertThat(conversation.messages().get(4).finishReason()).isEqualTo("stop");
+ assertThat(conversation.summary()).startsWith("GenAI level=full: 5
message parts of run 7.");
+ }
+
+ @Test
+ void conversationOfARunWithItsInputOnTheRun() {
+ // as camel-openai records it: the input of the run on the run itself
+ JsonObject run = run("7", "support");
+ content(run).put("inputMessages", messages(
+ message("system", text("You answer the customers.")),
message("user", text("Where is my order?"))));
+ content(steps(run).get(0)).remove("inputMessages");
+
+ Conversation conversation = GenAiTraceReport.conversation(answer(run),
"7");
+
+ assertThat(conversation.messages()).extracting(Message::text)
+ .startsWith("You answer the customers.", "Where is my order?");
}
@Test
void conversationIsCapped() {
- String longAnswer = "x".repeat(GenAiTraceReport.MAX_MESSAGE * 2);
- List<SpanEntry> spans = new ArrayList<>();
- SpanEntry agent = span("run1", "route1", "customer-questions", 0, 1000,
- attrs("invoke_agent", GenAiAttributes.REQUEST_MODEL,
"granite4:3b"));
- GenAiMessages input = new GenAiMessages();
+ JsonObject run = run("7", "support");
+ JsonArray output = new JsonArray();
for (int i = 0; i < 10; i++) {
- input.text(GenAiMessages.USER, "question " + i + " " + longAnswer);
+ output.add(message("assistant", text("x".repeat(2000))));
}
- spans.add(agent);
- spans.add(span("chat1", "run1", "customer-questions", 10, 100,
- attrs("chat", GenAiAttributes.REQUEST_MODEL, "granite4:3b",
- GenAiAttributes.INPUT_MESSAGES, input.toJson(8192))));
+ content(steps(run).get(2)).put("outputMessages", output);
- GenAiTraceReport.Conversation conversation =
GenAiTraceReport.conversation(spans, "run1");
+ Conversation conversation = GenAiTraceReport.conversation(answer(run),
"7");
- assertThat(conversation.messages()).allSatisfy(m ->
assertThat(m.text().length())
- .isLessThanOrEqualTo(GenAiTraceReport.MAX_MESSAGE));
- assertThat(conversation.messages().stream().mapToInt(m ->
m.text().length()).sum())
- .isLessThanOrEqualTo(GenAiTraceReport.MAX_CONVERSATION);
- assertThat(conversation.messages().get(0).text()).endsWith("...");
- assertThat(conversation.cut()).isEqualTo("4 more message parts not
shown (6000 characters in all)");
+ int total = conversation.messages().stream().mapToInt(m -> m.text() !=
null ? m.text().length() : 0).sum();
+
assertThat(total).isLessThanOrEqualTo(GenAiTraceReport.MAX_CONVERSATION);
+ assertThat(conversation.messages())
+ .allSatisfy(m ->
assertThat(m.text()).hasSizeLessThanOrEqualTo(GenAiTraceReport.MAX_MESSAGE));
+ assertThat(conversation.cut()).contains("more message parts not
shown");
Review Comment:
Thanks, done in 7603191321e5. Restored the exact assertion: 4 more message
parts not shown (6000 characters in all).
_Claude Code on behalf of Claus Ibsen_
--
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]