diff options
| author | Taylor Mullen <[email protected]> | 2025-05-16 23:33:12 -0700 |
|---|---|---|
| committer | N. Taylor Mullen <[email protected]> | 2025-05-16 23:34:48 -0700 |
| commit | 5dcdbe64ab6ef2ca2692e75b8600b8726ac72178 (patch) | |
| tree | ecc774a5e10c5d2e6c65d11ba0008edf3c9cb09e /packages/server/src/tools/write-file.ts | |
| parent | 58e02240612e0a0eddc1427a795f5003ee2b3d07 (diff) | |
refactor: Unify file modification confirmation state
- Modifies `EditTool` and `WriteFileTool` to share a single confirmation preference.
- The "Always Proceed" choice for file modifications is now stored in `Config.alwaysSkipModificationConfirmation`.
- This ensures that if a user chooses to always skip confirmation for one file modification tool, this preference is respected by the other.
- `WriteFileTool` constructor now accepts `Config` instead of `targetDir` to facilitate this shared state.
- Tests updated to reflect the new shared confirmation logic.
Fixes https://b.corp.google.com/issues/415897960
Diffstat (limited to 'packages/server/src/tools/write-file.ts')
| -rw-r--r-- | packages/server/src/tools/write-file.ts | 28 |
1 files changed, 17 insertions, 11 deletions
diff --git a/packages/server/src/tools/write-file.ts b/packages/server/src/tools/write-file.ts index 21178b5b..2979ffb8 100644 --- a/packages/server/src/tools/write-file.ts +++ b/packages/server/src/tools/write-file.ts @@ -7,6 +7,7 @@ import fs from 'fs'; import path from 'path'; import * as Diff from 'diff'; +import { Config } from '../config/config.js'; import { BaseTool, ToolResult, @@ -15,9 +16,10 @@ import { ToolConfirmationOutcome, ToolCallConfirmationDetails, } from './tools.js'; -import { SchemaValidator } from '../utils/schemaValidator.js'; // Updated import -import { makeRelative, shortenPath } from '../utils/paths.js'; // Updated import +import { SchemaValidator } from '../utils/schemaValidator.js'; +import { makeRelative, shortenPath } from '../utils/paths.js'; import { isNodeError } from '../utils/errors.js'; + /** * Parameters for the WriteFile tool */ @@ -38,9 +40,8 @@ export interface WriteFileToolParams { */ export class WriteFileTool extends BaseTool<WriteFileToolParams, ToolResult> { static readonly Name: string = 'write_file'; - private shouldAlwaysWrite = false; - constructor(private readonly rootDirectory: string) { + constructor(private readonly config: Config) { super( WriteFileTool.Name, 'WriteFile', @@ -61,12 +62,11 @@ export class WriteFileTool extends BaseTool<WriteFileToolParams, ToolResult> { type: 'object', }, ); - this.rootDirectory = path.resolve(rootDirectory); } private isWithinRoot(pathToCheck: string): boolean { const normalizedPath = path.normalize(pathToCheck); - const normalizedRoot = path.normalize(this.rootDirectory); + const normalizedRoot = path.normalize(this.config.getTargetDir()); const rootWithSep = normalizedRoot.endsWith(path.sep) ? normalizedRoot : normalizedRoot + path.sep; @@ -90,13 +90,16 @@ export class WriteFileTool extends BaseTool<WriteFileToolParams, ToolResult> { return `File path must be absolute: ${params.file_path}`; } if (!this.isWithinRoot(params.file_path)) { - return `File path must be within the root directory (${this.rootDirectory}): ${params.file_path}`; + return `File path must be within the root directory (${this.config.getTargetDir()}): ${params.file_path}`; } return null; } getDescription(params: WriteFileToolParams): string { - const relativePath = makeRelative(params.file_path, this.rootDirectory); + const relativePath = makeRelative( + params.file_path, + this.config.getTargetDir(), + ); return `Writing to ${shortenPath(relativePath)}`; } @@ -106,7 +109,7 @@ export class WriteFileTool extends BaseTool<WriteFileToolParams, ToolResult> { async shouldConfirmExecute( params: WriteFileToolParams, ): Promise<ToolCallConfirmationDetails | false> { - if (this.shouldAlwaysWrite) { + if (this.config.getAlwaysSkipModificationConfirmation()) { return false; } @@ -118,7 +121,10 @@ export class WriteFileTool extends BaseTool<WriteFileToolParams, ToolResult> { return false; } - const relativePath = makeRelative(params.file_path, this.rootDirectory); + const relativePath = makeRelative( + params.file_path, + this.config.getTargetDir(), + ); const fileName = path.basename(params.file_path); let currentContent = ''; @@ -143,7 +149,7 @@ export class WriteFileTool extends BaseTool<WriteFileToolParams, ToolResult> { fileDiff, onConfirm: async (outcome: ToolConfirmationOutcome) => { if (outcome === ToolConfirmationOutcome.ProceedAlways) { - this.shouldAlwaysWrite = true; + this.config.setAlwaysSkipModificationConfirmation(true); } }, }; |
