davsclaus commented on code in PR #27633:
URL: https://github.com/apache/camel/pull/27633#discussion_r4234681273
##########
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:
Keeping this one as it is: a null element is a malformed call, and passing
it on as {} makes the shared tool answer with the edits shape error ("edits
must be a list of {\"find\": ..., \"replace\": ...}"), so the model learns what
to send. Dropping it silently would apply the other edits and hide a call the
model got wrong.
--
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]