Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public String name() {
public ToolResult execute(Object input, ToolContext context) {
try {
Map<String, Object> args = asMap(input);
Path path = safePath(context, readFilePath(args));
Path path = safePath(context, filePath(args));
List<Long> viewRange = readViewRange(args.get("view_range"));
boolean hasViewRange = viewRange != null && !viewRange.isEmpty();
Long offset = optionalLong(args, "offset");
Expand Down Expand Up @@ -250,7 +250,7 @@ public String name() {
public ToolResult execute(Object input, ToolContext context) {
try {
Map<String, Object> args = asMap(input);
String rawPath = firstNonEmpty(stringValue(args.get("path")), stringValue(args.get("file")));
String rawPath = filePath(args);
Path path = safePath(context, rawPath);
String content = stringValue(args.get("content"));
Files.createDirectories(path.getParent());
Expand All @@ -276,7 +276,7 @@ public String name() {
public ToolResult execute(Object input, ToolContext context) {
try {
Map<String, Object> args = asMap(input);
String rawPath = firstNonEmpty(stringValue(args.get("path")), stringValue(args.get("file")));
String rawPath = filePath(args);
Path path = safePath(context, rawPath);
String oldString = firstNonEmpty(stringValue(args.get("old_string")), stringValue(args.get("old")));
String newString = firstNonEmpty(stringValue(args.get("new_string")), stringValue(args.get("new")));
Expand Down Expand Up @@ -494,7 +494,7 @@ private static long longValue(Object value, String name) {
}
}

private static String readFilePath(Map<String, Object> args) {
private static String filePath(Map<String, Object> args) {
String path = "";
for (String name : new String[] {"file_path", "path", "file"}) {
Object value = args.get(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,69 @@ public void readKeepsLegacyInputsWithoutAmbiguity() throws Exception {
rangeConflict.getContent().get(0).getText());
}

@Test
public void fileToolsAcceptPathAliases() throws Exception {
Path workdir = Files.createTempDirectory("ark-java-file-aliases-");
ToolContext context = new ToolContext(workdir.toString());

for (String key : new String[] {"file_path", "path", "file"}) {
String name = key + ".txt";
Map<String, Object> writeInput = new LinkedHashMap<>();
writeInput.put(key, name);
writeInput.put("content", "old");
assertFalse(new DefaultTools.WriteTool().execute(writeInput, context).isError());

assertFalse(new DefaultTools.ReadTool()
.execute(Collections.singletonMap(key, name), context)
.isError());

Map<String, Object> editInput = new LinkedHashMap<>();
editInput.put(key, name);
editInput.put("old_string", "old");
editInput.put("new_string", "new");
assertFalse(new DefaultTools.EditTool().execute(editInput, context).isError());
assertEquals(
"new",
new String(Files.readAllBytes(workdir.resolve(name)), java.nio.charset.StandardCharsets.UTF_8));
}
}

@Test
public void fileToolsValidatePathAliases() throws Exception {
Path workdir = Files.createTempDirectory("ark-java-file-alias-validation-");
Files.write(workdir.resolve("example.txt"), "old".getBytes(java.nio.charset.StandardCharsets.UTF_8));
ToolContext context = new ToolContext(workdir.toString());
Map<String, Object> common = new LinkedHashMap<>();
common.put("file_path", "example.txt");
common.put("path", "example.txt");
common.put("file", "example.txt");
common.put("content", "old");
common.put("old_string", "old");
common.put("new_string", "new");

for (Tool tool : new Tool[] {
new DefaultTools.ReadTool(), new DefaultTools.WriteTool(), new DefaultTools.EditTool()
}) {
assertFalse(tool.execute(common, context).isError());

Map<String, Object> conflictInput = new LinkedHashMap<>(common);
conflictInput.put("path", "other.txt");
ToolResult conflict = tool.execute(conflictInput, context);
assertTrue(conflict.isError());
assertEquals(
"file_path, path, and file must not conflict",
conflict.getContent().get(0).getText());

Map<String, Object> missingInput = new LinkedHashMap<>();
missingInput.put("content", "new");
missingInput.put("old_string", "old");
missingInput.put("new_string", "new");
ToolResult missing = tool.execute(missingInput, context);
assertTrue(missing.isError());
assertEquals("file_path is required", missing.getContent().get(0).getText());
}
}

@Test
public void bashScrubsSensitiveExplicitEnvironment() throws Exception {
Path workdir = Files.createTempDirectory("ark-java-tools-");
Expand Down
Loading