Skip to content

Commit dc8b23d

Browse files
committed
🤖 refactor: extract duplicated strings to constants
- Add NOTE_READ_FILE_RETRY, NOTE_READ_FILE_FIRST_RETRY, NOTE_READ_FILE_AGAIN_RETRY constants - Add TOOL_EDIT_WARNING constant for tool descriptions - Update all usages to reference constants (7 locations) - Eliminates string duplication across tool definitions and error handling
1 parent 6548623 commit dc8b23d

4 files changed

Lines changed: 29 additions & 9 deletions

File tree

src/services/tools/file_edit_insert.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { FileEditInsertToolResult } from "@/types/tools";
33
import type { ToolConfiguration, ToolFactory } from "@/utils/tools/tools";
44
import { TOOL_DEFINITIONS } from "@/utils/tools/toolDefinitions";
55
import { validatePathInCwd, validateNoRedundantPrefix } from "./fileCommon";
6-
import { EDIT_FAILED_NOTE_PREFIX } from "@/types/tools";
6+
import { EDIT_FAILED_NOTE_PREFIX, NOTE_READ_FILE_RETRY } from "@/types/tools";
77
import { executeFileEditOperation } from "./file_edit_operation";
88
import { RuntimeError } from "@/runtime/Runtime";
99
import { fileExists } from "@/utils/runtime/fileExists";
@@ -92,7 +92,7 @@ export const createFileEditInsertTool: ToolFactory = (config: ToolConfiguration)
9292
return {
9393
success: false,
9494
error: `line_offset ${line_offset} is beyond file length (${lines.length} lines)`,
95-
note: `${EDIT_FAILED_NOTE_PREFIX} The file has ${lines.length} lines. Read the file to get current content, then retry.`,
95+
note: `${EDIT_FAILED_NOTE_PREFIX} The file has ${lines.length} lines. ${NOTE_READ_FILE_RETRY}`,
9696
};
9797
}
9898

src/services/tools/file_edit_replace_shared.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
* providing the core logic while keeping the tool definitions simple for AI providers.
66
*/
77

8-
import { EDIT_FAILED_NOTE_PREFIX } from "@/types/tools";
8+
import {
9+
EDIT_FAILED_NOTE_PREFIX,
10+
NOTE_READ_FILE_FIRST_RETRY,
11+
NOTE_READ_FILE_RETRY,
12+
NOTE_READ_FILE_AGAIN_RETRY,
13+
} from "@/types/tools";
914

1015
interface OperationMetadata {
1116
edits_applied: number;
@@ -56,7 +61,7 @@ export function handleStringReplace(
5661
success: false,
5762
error:
5863
"old_string not found in file. The text to replace must exist exactly as written in the file.",
59-
note: `${EDIT_FAILED_NOTE_PREFIX} The old_string does not exist in the file. Read the file first to get the exact current content, then retry.`,
64+
note: `${EDIT_FAILED_NOTE_PREFIX} The old_string does not exist in the file. ${NOTE_READ_FILE_FIRST_RETRY}`,
6065
};
6166
}
6267

@@ -147,7 +152,7 @@ export function handleLineReplace(
147152
return {
148153
success: false,
149154
error: `start_line ${args.start_line} exceeds current file length (${lines.length}).`,
150-
note: `${EDIT_FAILED_NOTE_PREFIX} The file has ${lines.length} lines. Read the file to get current content, then retry.`,
155+
note: `${EDIT_FAILED_NOTE_PREFIX} The file has ${lines.length} lines. ${NOTE_READ_FILE_RETRY}`,
151156
};
152157
}
153158

@@ -158,7 +163,7 @@ export function handleLineReplace(
158163
return {
159164
success: false,
160165
error: `expected_lines validation failed. Current lines [${currentRange.join("\n")}] differ from expected [${args.expected_lines.join("\n")}].`,
161-
note: `${EDIT_FAILED_NOTE_PREFIX} The file content changed since you last read it. Read the file again and retry.`,
166+
note: `${EDIT_FAILED_NOTE_PREFIX} The file content changed since you last read it. ${NOTE_READ_FILE_AGAIN_RETRY}`,
162167
};
163168
}
164169

src/types/tools.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,20 @@ export const WRITE_DENIED_PREFIX = "WRITE DENIED, FILE UNMODIFIED:";
132132
*/
133133
export const EDIT_FAILED_NOTE_PREFIX = "EDIT FAILED - file was NOT modified.";
134134

135+
/**
136+
* Common note fragments for DRY error messages
137+
*/
138+
export const NOTE_READ_FILE_RETRY = "Read the file to get current content, then retry.";
139+
export const NOTE_READ_FILE_FIRST_RETRY =
140+
"Read the file first to get the exact current content, then retry.";
141+
export const NOTE_READ_FILE_AGAIN_RETRY = "Read the file again and retry.";
142+
143+
/**
144+
* Tool description warning for file edit tools
145+
*/
146+
export const TOOL_EDIT_WARNING =
147+
"Always check the tool result before proceeding with other operations.";
148+
135149
export type FileEditToolArgs =
136150
| FileEditReplaceStringToolArgs
137151
| FileEditReplaceLinesToolArgs

src/utils/tools/toolDefinitions.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
BASH_MAX_LINE_BYTES,
1313
BASH_MAX_TOTAL_BYTES,
1414
} from "@/constants/toolLimits";
15+
import { TOOL_EDIT_WARNING } from "@/types/tools";
1516

1617
import { zodToJsonSchema } from "zod-to-json-schema";
1718

@@ -69,7 +70,7 @@ export const TOOL_DEFINITIONS = {
6970
file_edit_replace_string: {
7071
description:
7172
"Apply one or more edits to a file by replacing exact text matches. All edits are applied sequentially. Each old_string must be unique in the file unless replace_count > 1 or replace_count is -1. " +
72-
"IMPORTANT: Edits may fail if old_string is not found or not unique. Always check the tool result before proceeding with other operations.",
73+
`IMPORTANT: Edits may fail if old_string is not found or not unique. ${TOOL_EDIT_WARNING}`,
7374
schema: z.object({
7475
file_path: z.string().describe("The absolute path to the file to edit"),
7576
old_string: z
@@ -90,7 +91,7 @@ export const TOOL_DEFINITIONS = {
9091
file_edit_replace_lines: {
9192
description:
9293
"Replace a range of lines in a file. Use this for line-based edits when you know the exact line numbers to modify. " +
93-
"IMPORTANT: Edits may fail if line numbers are invalid or file content has changed. Always check the tool result before proceeding with other operations.",
94+
`IMPORTANT: Edits may fail if line numbers are invalid or file content has changed. ${TOOL_EDIT_WARNING}`,
9495
schema: z.object({
9596
file_path: z.string().describe("The absolute path to the file to edit"),
9697
start_line: z.number().int().min(1).describe("1-indexed start line (inclusive) to replace"),
@@ -109,7 +110,7 @@ export const TOOL_DEFINITIONS = {
109110
file_edit_insert: {
110111
description:
111112
"Insert content at a specific line position in a file. Line offset is 1-indexed: 0 inserts at the top, 1 inserts after line 1, etc. " +
112-
"IMPORTANT: Edits may fail if line_offset is invalid or file doesn't exist. Always check the tool result before proceeding with other operations.",
113+
`IMPORTANT: Edits may fail if line_offset is invalid or file doesn't exist. ${TOOL_EDIT_WARNING}`,
113114
schema: z.object({
114115
file_path: z.string().describe("The absolute path to the file to edit"),
115116
line_offset: z

0 commit comments

Comments
 (0)