From 8a64d7c9120653c37087afe925bc9322b1936bbd Mon Sep 17 00:00:00 2001 From: Robert Brennan Date: Mon, 25 Mar 2024 08:53:26 -0400 Subject: [PATCH] Fix read and write operations when LLM asks for absolute path (#126) * fix resolution of filepaths * fix imports * Update write.py --- opendevin/lib/actions/read.py | 4 ++-- opendevin/lib/actions/util.py | 10 ++++++++++ opendevin/lib/actions/write.py | 8 ++++---- 3 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 opendevin/lib/actions/util.py diff --git a/opendevin/lib/actions/read.py b/opendevin/lib/actions/read.py index 826a02e495..0913753367 100644 --- a/opendevin/lib/actions/read.py +++ b/opendevin/lib/actions/read.py @@ -1,7 +1,7 @@ -import os +from .util import resolve_path def read(base_path, file_path): - file_path = os.path.join(base_path, file_path) + file_path = resolve_path(base_path, file_path) with open(file_path, 'r') as file: return file.read() diff --git a/opendevin/lib/actions/util.py b/opendevin/lib/actions/util.py new file mode 100644 index 0000000000..c8c10852f6 --- /dev/null +++ b/opendevin/lib/actions/util.py @@ -0,0 +1,10 @@ +import os + +# This is the path where the workspace is mounted in the container +# The LLM sometimes returns paths with this prefix, so we need to remove it +PATH_PREFIX = "/workspace/" + +def resolve_path(base_path, file_path): + if file_path.startswith(PATH_PREFIX): + file_path = file_path[len(PATH_PREFIX):] + return os.path.join(base_path, file_path) diff --git a/opendevin/lib/actions/write.py b/opendevin/lib/actions/write.py index 9480563f98..a8c879872d 100644 --- a/opendevin/lib/actions/write.py +++ b/opendevin/lib/actions/write.py @@ -1,8 +1,8 @@ -import os +from .util import resolve_path -def write(base_path, path, contents): - path = os.path.join(base_path, path) - with open(path, 'w') as file: +def write(base_path, file_path, contents): + file_path = resolve_path(base_path, file_path) + with open(file_path, 'w') as file: file.write(contents) return ""