Skip to content

Commit 46803fc

Browse files
authored
Update filesystem.py
1 parent 3b142ed commit 46803fc

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

python_agent_harness/tools/filesystem.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,16 @@ class Write(Tool):
440440
}
441441

442442
def run(self, args: dict, ctx: ToolContext) -> str:
443-
path = os.path.abspath(os.path.join(args["path"], args["filename"]))
443+
dir_path = args.get("path") or "."
444+
filename = args.get("filename") or ""
445+
content = args.get("content") or ""
446+
# LLM may put the full file path in "filename" or in "path"
447+
if filename:
448+
path = os.path.abspath(os.path.join(dir_path, filename))
449+
else:
450+
path = os.path.abspath(dir_path)
451+
if not filename:
452+
filename = os.path.basename(path) or os.path.basename(dir_path)
444453
existed = os.path.exists(path)
445454
old_content = ""
446455
if existed:
@@ -450,15 +459,17 @@ def run(self, args: dict, ctx: ToolContext) -> str:
450459
except OSError:
451460
old_content = ""
452461
try:
453-
os.makedirs(os.path.dirname(path), exist_ok=True)
462+
parent = os.path.dirname(path)
463+
if parent:
464+
os.makedirs(parent, exist_ok=True)
454465
with open(path, "w", encoding="utf-8") as f:
455-
f.write(args["content"])
466+
f.write(content)
456467
except OSError as e:
457468
return f"Error: {e}"
458-
diff_text = unified_diff(old_content, args["content"], path)
469+
diff_text = unified_diff(old_content, content, path)
459470
if diff_text:
460471
ctx.record_diff(diff_text)
461-
return f"Created file {args['filename']} in {args['path']}"
472+
return f"Created file {filename} in {dir_path}"
462473

463474

464475
class Edit(Tool):

0 commit comments

Comments
 (0)