Skip to content

refactor(coding): migrate write_file and str_replace to StructuredToolResult#29

Open
Travelguest wants to merge 1 commit intoMagicCube:mainfrom
Travelguest:refactor/migrate-tools-to-structured-result
Open

refactor(coding): migrate write_file and str_replace to StructuredToolResult#29
Travelguest wants to merge 1 commit intoMagicCube:mainfrom
Travelguest:refactor/migrate-tools-to-structured-result

Conversation

@Travelguest
Copy link
Copy Markdown
Contributor

Description:
Migrates write_file and str_replace tools to the new StructuredToolResult architecture, aligning them with the pattern already used by mkdir, move_path, and other tools.·

Changes:
write-file.ts: Added path validation via **ensureAbsolutePath()**, auto-creation of parent directories, proper error handling with okToolResult/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 unnecessary mkdir logic, added error codes (INVALID_PATH, FILE_NOT_FOUND, INVALID_ARGUMENT, NOT_FOUND, WRITE_FAILED)

Tests updated: All existing tests adapted to toMatchObject with structured result shapes; added new tests for relative path rejection and parent directory auto-creation

…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant