gnodet-bot commented on code in PR #30522:
URL: https://github.com/apache/camel/pull/30522#discussion_r4647463521


##########
dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/SpansTab.java:
##########
@@ -645,6 +710,12 @@ public void renderFooter(List<Span> spans) {
         filter.renderHint(spans);
     }
 
+    private boolean selectedSpanIsGenAi() {
+        List<WaterfallNode> nodes = buildWaterfallNodes(selectedTraceId);

Review Comment:
   **Low:** `selectedSpanIsGenAi()` calls `buildWaterfallNodes()` (O(n log n)) 
on every footer render, which is called every frame. `renderWaterfallView()` 
already builds the nodes once per frame, so this PR adds a second rebuild. For 
typical traces (10–50 spans) it's negligible, but for large traces it doubles 
the per-frame cost. Consider caching the last-built nodes list (invalidate on 
`spanRefreshRequested` or `selectedTraceId` change) or passing it in from 
`renderWaterfallView`.



##########
dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/GenAiTranscript.java:
##########
@@ -0,0 +1,375 @@
+/*
+ * 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.tui;
+
+import java.util.ArrayList;
+import java.util.Collection;
+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.component.ai.observability.GenAiOperationName;
+import org.apache.camel.util.json.JsonArray;
+import org.apache.camel.util.json.JsonObject;
+import org.apache.camel.util.json.Jsoner;
+
+/**
+ * The conversation of a GenAI span, from the message attributes the Camel AI 
producers record at observability level
+ * {@code full} ({@code gen_ai.system_instructions}, {@code 
gen_ai.input.messages}, {@code gen_ai.output.messages}, and
+ * the arguments and result of a tool call), in the shape of the OpenTelemetry 
GenAI semantic conventions: messages with
+ * a {@code role} and {@code parts} of type {@code text}, {@code reasoning}, 
{@code tool_call} and
+ * {@code tool_call_response}.
+ * <p/>
+ * An agent run ({@code invoke_agent}) shows the input and output of its last 
model call: that input already holds the
+ * whole conversation of the run, with every tool call and result.
+ */
+final class GenAiTranscript {
+
+    enum Kind {
+        TEXT,
+        REASONING,
+        TOOL_CALL,
+        TOOL_RESULT
+    }
+
+    /**
+     * A part of a message: a text, a tool call with its arguments, or a tool 
result. {@code id} pairs a tool call with
+     * its result.
+     */
+    record Part(Kind kind, String text, String toolName, String id) {
+
+        Part(Kind kind, String text) {
+            this(kind, text, null, null);
+        }
+    }
+
+    /** A message: its role (system, user, assistant, tool), its parts, and 
the finish reason of an output message. */
+    record Turn(String role, List<Part> parts, String finishReason) {
+    }
+
+    /** The mark {@code GenAiMessages} puts at the end of a text cut at {@code 
contentMaxLength}. */
+    static final String TRUNCATED = "...[truncated]";
+
+    private static final List<String> CONTENT_ATTRIBUTES = List.of(
+            GenAiAttributes.INPUT_MESSAGES, GenAiAttributes.OUTPUT_MESSAGES, 
GenAiAttributes.SYSTEM_INSTRUCTIONS,
+            GenAiAttributes.TOOL_CALL_ARGUMENTS, 
GenAiAttributes.TOOL_CALL_RESULT);
+    private static final int MAX_PARENT_HOPS = 50;
+
+    private final List<Turn> turns;
+
+    private GenAiTranscript(List<Turn> turns) {
+        this.turns = List.copyOf(turns);
+    }
+
+    List<Turn> turns() {
+        return turns;
+    }
+
+    boolean isEmpty() {
+        return turns.isEmpty();
+    }
+
+    /**
+     * The conversation as Markdown: a heading per message, the texts as they 
are (a model answers in Markdown), and the
+     * tool arguments and results as code blocks.
+     */
+    String toMarkdown() {
+        StringBuilder sb = new StringBuilder();
+        for (Turn turn : turns) {
+            sb.append("### ").append(heading(turn)).append("\n\n");
+            for (Part part : turn.parts()) {
+                switch (part.kind()) {
+                    case TEXT -> 
sb.append(markTruncated(part.text())).append("\n\n");
+                    case REASONING -> {
+                        for (String line : ("*Thinking:* " + 
markTruncated(part.text())).split("\n")) {
+                            sb.append("> ").append(line).append('\n');
+                        }
+                        sb.append('\n');
+                    }
+                    case TOOL_CALL -> {
+                        sb.append("Calls **").append(part.toolName() != null ? 
part.toolName() : "a tool").append("**\n\n");
+                        codeBlock(sb, part.text());
+                    }
+                    case TOOL_RESULT -> codeBlock(sb, part.text());
+                }
+            }
+        }
+        return sb.toString().strip();
+    }
+
+    private static String heading(Turn turn) {
+        String role = turn.role() != null ? turn.role() : "message";
+        String heading = Character.toUpperCase(role.charAt(0)) + 
role.substring(1);

Review Comment:
   **Low:** `role.charAt(0)` is guarded against `null` (line 117) but not 
against an empty string. If a malformed message carries `"role":""`, this 
throws `StringIndexOutOfBoundsException`. Consistent with the defensive style 
already applied:
   
   ```suggestion
           String role = turn.role() != null && !turn.role().isEmpty() ? 
turn.role() : "message";
           String heading = Character.toUpperCase(role.charAt(0)) + 
role.substring(1);
   ```



-- 
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]

Reply via email to