Repository navigation
feat(sdk): allow write_file to overwrite existing files
#4109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 13 commits
a936a6e
77787ef
cf3b9d1
b6304b9
a51e55e
e997f1e
f12b530
cb25c85
c34109f
6260f39
6bddae5
e5c14e4
5280d7b
20c5986
11a7f5f
f70ce7c
a237edd
faed53b
b502e21
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -596,20 +596,19 @@ def write( | |
| file_path: str, | ||
| content: str, | ||
| ) -> WriteResult: | ||
| """Create a new file with content. | ||
| """Write content to a file, creating it or overwriting it if it already exists. | ||
|
|
||
| Returns WriteResult on success or error. | ||
| """ | ||
| store = self._get_store() | ||
| namespace = self._get_namespace() | ||
|
|
||
| # Check if file exists | ||
| existing = store.get(namespace, file_path) | ||
| if existing is not None: | ||
| return WriteResult(error=f"Cannot write to {file_path} because it already exists. Read and then make an edit, or write to a new path.") | ||
|
|
||
| # Create new file | ||
| file_data = create_file_data(content) | ||
| existing_file_data = self._convert_store_item_to_file_data(existing) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Old behavior was inefficient in terms of network requests. Current behavior is consistent with old behavior. We can revisit whether we want to preserve creation timestamp. (The cost of this can be 10-100 ms of latency.) |
||
| file_data = update_file_data(existing_file_data, content) | ||
| else: | ||
| file_data = create_file_data(content) | ||
| store_value = self._convert_file_data_to_store_value(file_data) | ||
| store.put(namespace, file_path, store_value) | ||
| return WriteResult(path=file_path) | ||
|
|
@@ -626,13 +625,12 @@ async def awrite( | |
| store = self._get_store() | ||
| namespace = self._get_namespace() | ||
|
|
||
| # Check if file exists using async method | ||
| existing = await store.aget(namespace, file_path) | ||
| if existing is not None: | ||
| return WriteResult(error=f"Cannot write to {file_path} because it already exists. Read and then make an edit, or write to a new path.") | ||
|
|
||
| # Create new file using async method | ||
| file_data = create_file_data(content) | ||
| existing_file_data = self._convert_store_item_to_file_data(existing) | ||
| file_data = update_file_data(existing_file_data, content) | ||
| else: | ||
| file_data = create_file_data(content) | ||
| store_value = self._convert_file_data_to_store_value(file_data) | ||
| await store.aput(namespace, file_path, store_value) | ||
| return WriteResult(path=file_path) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -346,7 +346,7 @@ def test_write_file_longterm(self): | |
| assert isinstance(content, str), f"Expected str content, got {type(content)}" | ||
| assert "fiery" in content or "Fiery" in content | ||
|
|
||
| def test_write_file_fail_already_exists_in_local(self): | ||
| def test_write_file_overwrites_existing_in_local(self): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure these integration tests ever run fwiw |
||
| checkpointer = MemorySaver() | ||
| store = InMemoryStore() | ||
| agent = create_agent( | ||
|
|
@@ -376,7 +376,8 @@ def test_write_file_fail_already_exists_in_local(self): | |
| messages = response["messages"] | ||
| write_file_message = next(message for message in messages if message.type == "tool" and message.name == "write_file") | ||
| assert write_file_message is not None | ||
| assert "Cannot write" in write_file_message.content | ||
| assert write_file_message.status == "success" | ||
| assert "fiery" in response["files"]["/charmander.txt"]["content"].lower() or "Fiery" in response["files"]["/charmander.txt"]["content"] | ||
|
|
||
| def test_edit_file_longterm(self): | ||
| checkpointer = MemorySaver() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
write has two modes:
This implements (1) which is probably correct