gnodet-bot commented on code in PR #27633:
URL: https://github.com/apache/camel/pull/27633#discussion_r4234671911
##########
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:
`editsJson` adds an empty `{}` to the array when `edit` is `null` — the
guard skips the `put` calls but `list.add(jo)` is unconditional. `parseEdits`
will then fail on the round-trip because the empty object has no `"find"` key.
A `null` element in the caller-supplied list is unlikely but worth guarding:
```suggestion
if (edit != null) {
jo.put("find", edit.find());
jo.put("replace", edit.replace());
list.add(jo);
}
```
##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/AuthoringTools.java:
##########
@@ -1027,9 +1140,50 @@ public static JsonObject writeFile(ToolContext ctx, Path
dir, String file, Strin
result.put("message", "An integration running the file in dev mode
reloads it now; otherwise restart the"
+ " integration for the change to take
effect.");
}
+ if (waiting != null) {
+ result.put("message", result.getString("message") + " Note: " +
waiting.substring(0, waiting.indexOf(" ("))
+ + " (see notes).");
Review Comment:
`waiting.substring(0, waiting.indexOf(" ("))` couples silently to the
internal format of `unconsumedDirectNote`. If the format ever changes and `"
("` is absent, this throws `StringIndexOutOfBoundsException` at runtime.
Consider extracting just the summary sentence inside `unconsumedDirectNote`
itself (returning a two-element structure or a dedicated record), or at least
guard with a fallback:
```suggestion
result.put("message", result.getString("message") + " Note: "
+ (waiting.indexOf(" (") >= 0 ?
waiting.substring(0, waiting.indexOf(" (")) : waiting)
+ " (see notes).");
```
--
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]