Skip to content

Commit

Permalink
Added CreateFileNew for claude
Browse files Browse the repository at this point in the history
  • Loading branch information
Aman Rusia committed Nov 18, 2024
1 parent f54f389 commit faf0035
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
12 changes: 7 additions & 5 deletions src/wcgw/client/anthropic_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from ..types_ import (
BashCommand,
BashInteraction,
CreateFileNew,
FileEditFindReplace,
ReadImage,
Writefile,
Expand Down Expand Up @@ -178,12 +179,14 @@ def loop(
""",
),
ToolParam(
input_schema=Writefile.model_json_schema(),
name="WriteFile",
input_schema=CreateFileNew.model_json_schema(),
name="CreateFileNew",
description="""
- Write content to a file. Provide file path and content. Use this instead of BashCommand for writing files.
- Write content to a new file. Provide file path and content. Use this instead of BashCommand for writing new files.
- This doesn't create any directories, please create directories using `mkdir -p` BashCommand.
- Provide absolute file path only.""",
- Provide absolute file path only.
- For editing existing files, use FileEditFindReplace.
""",
),
ToolParam(
input_schema=ReadImage.model_json_schema(),
Expand All @@ -202,7 +205,6 @@ def loop(
- Find and replace multiple lines in a file.
- Use absolute file path only.
- Replaces complete lines.
- Prefer this over WriteFile if edits are to be made on large files.
""",
),
]
Expand Down
26 changes: 22 additions & 4 deletions src/wcgw/client/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
ParsedChatCompletionMessage,
)
from nltk.metrics.distance import edit_distance
from ..types_ import FileEditFindReplace, ResetShell, Writefile
from ..types_ import CreateFileNew, FileEditFindReplace, ResetShell, Writefile

from ..types_ import BashCommand

Expand Down Expand Up @@ -415,12 +415,17 @@ def read_image_from_shell(file_path: str) -> ImageData:
return ImageData(media_type=image_type, data=image_b64)


def write_file(writefile: Writefile) -> str:
def write_file(writefile: Writefile | CreateFileNew, error_on_exist: bool) -> str:
if not os.path.isabs(writefile.file_path):
return "Failure: file_path should be absolute path"
else:
path_ = writefile.file_path

if error_on_exist and os.path.exists(path_):
file_data = Path(path_).read_text()
if file_data:
return f"Error: can't write to existing file {path_}, use other functions to edit the file"

path = Path(path_)
path.parent.mkdir(parents=True, exist_ok=True)

Expand Down Expand Up @@ -555,6 +560,7 @@ def get_tool_output(
| BashInteraction
| ResetShell
| Writefile
| CreateFileNew
| FileEditFindReplace
| AIAssistant
| DoneFlag
Expand All @@ -571,6 +577,7 @@ def get_tool_output(
| BashInteraction
| ResetShell
| Writefile
| CreateFileNew
| FileEditFindReplace
| AIAssistant
| DoneFlag
Expand All @@ -581,6 +588,7 @@ def get_tool_output(
| BashInteraction
| ResetShell
| Writefile
| CreateFileNew
| FileEditFindReplace
| AIAssistant
| DoneFlag
Expand All @@ -598,7 +606,10 @@ def get_tool_output(
output = execute_bash(enc, arg, max_tokens)
elif isinstance(arg, Writefile):
console.print("Calling write file tool")
output = write_file(arg), 0
output = write_file(arg, False), 0
elif isinstance(arg, CreateFileNew):
console.print("Calling write file tool")
output = write_file(arg, True), 0
elif isinstance(arg, FileEditFindReplace):
console.print("Calling file edit tool")
output = file_edit(arg), 0.0
Expand Down Expand Up @@ -630,7 +641,14 @@ def get_tool_output(


class Mdata(BaseModel):
data: BashCommand | BashInteraction | Writefile | ResetShell | FileEditFindReplace
data: (
BashCommand
| BashInteraction
| Writefile
| CreateFileNew
| ResetShell
| FileEditFindReplace
)


execution_lock = threading.Lock()
Expand Down
5 changes: 5 additions & 0 deletions src/wcgw/types_.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ class Writefile(BaseModel):
file_content: str


class CreateFileNew(BaseModel):
file_path: str
file_content: str


class FileEditFindReplace(BaseModel):
file_path: str
find_lines: str
Expand Down

0 comments on commit faf0035

Please sign in to comment.