refactor(coding): migrate write_file and str_replace to StructuredToolResult#29
Open
Travelguest wants to merge 1 commit intoMagicCube:mainfrom
Open
Conversation
…ol-result
Align the two remaining legacy tools with the StructuredToolResult
architecture introduced in recent commits. Both tools now use
`okToolResult()` / `errorToolResult()` from `./tool-result` and
`ensureAbsolutePath()` from `./tool-utils`.
**write_file changes:**
- Add absolute path validation (INVALID_PATH)
- Add try/catch with structured error (WRITE_FAILED)
- Auto-create parent directories when missing
- Return structured success with `{ path, bytes }`
**str_replace changes:**
- Add absolute path validation (INVALID_PATH)
- Replace ad-hoc `{ ok, error }` returns with structured results
- Add error codes: FILE_NOT_FOUND, INVALID_ARGUMENT, NOT_FOUND, WRITE_FAILED
- Remove unnecessary mkdir logic (str_replace operates on existing files)
- Wrap file.write() in try/catch
**Test updates:**
- Update write-file tests to match structured result shape
- Add test: creates parent directories automatically
- Add test: returns INVALID_PATH for relative paths
- Update str-replace tests to match structured result shape
- Add test: returns INVALID_PATH for relative paths
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description:
Migrates
write_fileandstr_replacetools to the newStructuredToolResultarchitecture, aligning them with the pattern already used bymkdir,move_path, and other tools.·Changes:
write-file.ts: Added path validation via**ensureAbsolutePath()**, auto-creation of parent directories, proper error handling withokToolResult/errorToolResult, and typed error codes (INVALID_PATH,WRITE_FAILED)str-replace.ts:Replaced ad-hoc{ ok, error }returns with structured results, added path validation, removed unnecessarymkdirlogic, added error codes (INVALID_PATH,FILE_NOT_FOUND,INVALID_ARGUMENT,NOT_FOUND,WRITE_FAILED)Tests updated: All existing tests adapted to
toMatchObjectwith structured result shapes; added new tests for relative path rejection and parent directory auto-creation