feat: chdir to playground

pull/18/head
adldotori 1 year ago
parent 8c4139ab3f
commit 94df7bfcde

@ -1,3 +1,4 @@
import os
import re
from typing import Dict, List, TypedDict
@ -23,7 +24,7 @@ app = FastAPI()
app.mount("/static", StaticFiles(directory=StaticUploader.STATIC_DIR), name="static")
uploader = StaticUploader.from_settings(settings)
os.chdir(settings["PLAYGROUND_DIR"])
toolsets: List[BaseToolSet] = [
Terminal(),
@ -91,7 +92,7 @@ async def command(request: Request) -> Response:
except Exception as e:
return {"response": str(e), "files": []}
files = re.findall("(image/\S*png)|(dataframe/\S*csv)", res["output"])
files = re.findall("image/\S*png|dataframe/\S*csv", res["output"])
return {
"response": res["output"],
@ -100,4 +101,4 @@ async def command(request: Request) -> Response:
def serve():
uvicorn.run("api.main:app", host="0.0.0.0", port=settings["PORT"])
uvicorn.run("api.main:app", host="0.0.0.0", port=settings["EVAL_PORT"])

@ -55,7 +55,7 @@ class CodeEditor(BaseToolSet):
"Input should be filename and code to append. "
"Input code must be the code that should be appended, NOT whole code. "
"ex. test.py\nprint('hello world')\n "
"and the output will be last 3 line.",
"and the output will be last 3 lines.",
)
def append(self, inputs: str) -> str:
try:
@ -63,7 +63,6 @@ class CodeEditor(BaseToolSet):
output = (
"Last 3 line was:\n"
+ "\n".join(code.split("\n")[-3:])
+ "\nYou can use CodeEditor.APPEND tool to append the code if this file is not completed."
)
except Exception as e:
output = str(e)
@ -80,7 +79,7 @@ class CodeEditor(BaseToolSet):
"If the code is completed, use the Terminal tool to execute it, if not, append the code through the CodeEditor.APPEND tool. "
"Input should be filename and code. This file must be in playground folder. "
"ex. test.py\nprint('hello world')\n "
"and the output will be last 3 line.",
"and the output will be last 3 lines.",
)
def write(self, inputs: str) -> str:
try:
@ -88,7 +87,6 @@ class CodeEditor(BaseToolSet):
output = (
"Last 3 line was:\n"
+ "\n".join(code.split("\n")[-3:])
+ "\nYou can use CodeEditor.APPEND tool to append the code if this file is not completed."
)
except Exception as e:
output = str(e)
@ -136,7 +134,6 @@ class CodeEditor(BaseToolSet):
"Output will be success or error message.",
)
def delete(self, inputs: str) -> str:
filepath: str = str(Path(settings["PLAYGROUND_DIR"]) / Path(inputs))
try:
with open(filepath, "w") as f:
f.write("")

@ -84,7 +84,7 @@ class PatchCommand:
separator = "|"
def __init__(self, filepath: str, start: Position, end: Position, content: str):
self.filepath: str = str(Path(settings["PLAYGROUND_DIR"]) / Path(filepath))
self.filepath: str = filepath
self.start: Position = start
self.end: Position = end
self.content: str = content
@ -100,12 +100,6 @@ class PatchCommand:
return sum([len(line) for line in lines])
def execute(self) -> Tuple[int, int]:
# make sure the directory exists
if not str(Path(self.filepath).resolve()).startswith(
str(Path(settings["PLAYGROUND_DIR"]).resolve())
):
return "You can't write file outside of current directory."
os.makedirs(os.path.dirname(self.filepath), exist_ok=True)
lines = self.read_lines()
before = sum([len(line) for line in lines])

@ -104,16 +104,11 @@ class ReadCommand:
separator = "|"
def __init__(self, filepath: str, start: int, end: int):
self.filepath: str = str(Path(settings["PLAYGROUND_DIR"]) / Path(filepath))
self.filepath: str = filepath
self.start: int = start
self.end: int = end
def execute(self) -> str:
if not str(Path(self.filepath).resolve()).startswith(
str(Path(settings["PLAYGROUND_DIR"]).resolve())
):
return "You can't write file outside of current directory."
with open(self.filepath, "r") as f:
code = f.readlines()
@ -134,16 +129,11 @@ class SummaryCommand:
separator = "|"
def __init__(self, filepath: str, depth: int, parent_content: Optional[str] = None):
self.filepath: str = str(Path(settings["PLAYGROUND_DIR"]) / Path(filepath))
self.filepath: str = filepath
self.depth: int = depth
self.parent_content: Optional[str] = parent_content
def execute(self) -> str:
if not str(Path(self.filepath).resolve()).startswith(
str(Path(settings["PLAYGROUND_DIR"]).resolve())
):
return "You can't write file outside of current directory."
with open(self.filepath, "r") as f:
code = f.readlines()

@ -14,7 +14,7 @@ class WriteCommand:
separator = "\n"
def __init__(self, filepath: str, content: int):
self.filepath: str = str(Path(settings["PLAYGROUND_DIR"]) / Path(filepath))
self.filepath: str = filepath
self.content: str = content
self.mode: str = "w"
@ -23,13 +23,6 @@ class WriteCommand:
return self
def execute(self) -> str:
# make sure the directory exists
if not str(Path(self.filepath).resolve()).startswith(
str(Path(settings["PLAYGROUND_DIR"]).resolve())
):
return "You can't write file outside of current directory."
os.makedirs(os.path.dirname(self.filepath), exist_ok=True)
with open(self.filepath, self.mode) as f:
f.write(self.content)
return self.content

@ -31,7 +31,6 @@ class Terminal(BaseToolSet):
process = subprocess.Popen(
commands,
shell=True,
cwd=settings["PLAYGROUND_DIR"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)

Loading…
Cancel
Save