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


##########
dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/AuthoringTools.java:
##########
@@ -171,19 +173,43 @@ public JsonObject camel_write_file(
     }
 
     @Tool(annotations = @Tool.Annotations(readOnlyHint = false, 
destructiveHint = false, openWorldHint = false),
-          description = "Changes a file by replacing one snippet: the exact 
text to find (it must occur once) and "
+          description = "Changes a file by replacing a snippet: the exact text 
to find (it must occur once) and "
                         + "what to put there. Validated and reloaded as a 
write is. Use it to change an existing file, "
-                        + "camel_write_file for a new one.")
+                        + "camel_write_file for a new one. A change to several 
places of the file is given at once in "
+                        + "edits, so the file is written and reloaded once, 
not half done.")
     public JsonObject camel_edit_file(
             @ToolArg(description = DIRECTORY_DESC, required = false) String 
directory,
             @ToolArg(description = "File path relative to the directory", 
required = true) String file,
             @ToolArg(description = "The lines to replace as they stand in the 
file; other indentation is fine when "
-                                   + "the lines name one place",
-                     required = true) String find,
-            @ToolArg(description = "The text to put there; empty removes it", 
required = true) String replace,
+                                   + "the lines name one place (or use edits)",
+                     required = false) String find,
+            @ToolArg(description = "The text to put there; empty removes it", 
required = false) String replace,
+            @ToolArg(description = 
org.apache.camel.dsl.jbang.core.commands.ai.AuthoringTools.EDITS_DESC,
+                     required = false) List<FileEdit> edits,
             @ToolArg(description = VERSION_DESC, required = false) String 
camelVersion) {
         return call("camel_edit_file", args("directory", directory, "file", 
file, "find", find, "replace", replace,
-                "camelVersion", camelVersion));
+                "edits", editsJson(edits), "camelVersion", camelVersion));
+    }
+
+    /** One change of camel_edit_file's edits: the text to find as it stands 
in the file, and what to put there. */
+    public record FileEdit(String find, String replace) {
+    }
+
+    /** The edits as the JSON list the shared tool reads, or null when there 
are none. */
+    static String editsJson(List<FileEdit> edits) {
+        if (edits == null || edits.isEmpty()) {
+            return null;
+        }
+        JsonArray list = new JsonArray();
+        for (FileEdit edit : edits) {
+            JsonObject jo = new JsonObject();
+            if (edit != null) {
+                jo.put("find", edit.find());
+                jo.put("replace", edit.replace());
+            }
+            list.add(jo);

Review Comment:
   ⚠️ **Re-raised from previous review:** The null guard on `edit` protects 
only the `jo.put()` calls, but `list.add(jo)` runs unconditionally after it — a 
null element silently becomes `{}` in the serialized JSON and only fails when 
`parseEdits` rejects it downstream with the shape error. The guard reads as 
defensive but doesn't prevent the element from entering the list. Either skip 
the null element by moving `list.add(jo)` inside the `if` block:
   ```suggestion
               if (edit != null) {
                   JsonObject jo = new JsonObject();
                   jo.put("find", edit.find());
                   jo.put("replace", edit.replace());
                   list.add(jo);
               }
   ```



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