From 93f6890a04e8c98f94a15d2b30fdd22274a5ddef Mon Sep 17 00:00:00 2001 From: kurihada Date: Wed, 17 Dec 2025 01:13:27 +0800 Subject: [PATCH] =?UTF-8?q?feat(core):=20=E6=9B=B4=E6=96=B0=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=93=8D=E4=BD=9C=E5=B7=A5=E5=85=B7=E6=8F=8F=E8=BF=B0?= =?UTF-8?q?=E4=B8=BA=E6=A8=A1=E6=9D=BF=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 更新 read_file 工具描述,支持模板变量 - 更新 edit_file 工具描述,支持模板变量 - 更新 write_file 工具描述 - 添加 read-file-tool 常量 (DEFAULT_READ_LINES, MAX_LINE_LENGTH) - 在模板渲染器中注册新常量 --- packages/core/src/constants/read-file-tool.ts | 9 +++++++++ packages/core/src/template/renderer.ts | 12 +++++++++++- .../src/tools/descriptions/filesystem/edit_file.txt | 10 +++++++++- .../src/tools/descriptions/filesystem/read_file.txt | 13 ++++++++++++- .../tools/descriptions/filesystem/write_file.txt | 9 ++++++++- 5 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 packages/core/src/constants/read-file-tool.ts diff --git a/packages/core/src/constants/read-file-tool.ts b/packages/core/src/constants/read-file-tool.ts new file mode 100644 index 0000000..d27e5d8 --- /dev/null +++ b/packages/core/src/constants/read-file-tool.ts @@ -0,0 +1,9 @@ +/** + * Read File 工具配置常量 + */ + +/** 默认读取行数 */ +export const DEFAULT_READ_LINES = 2000; + +/** 最大行长度(超过会被截断) */ +export const MAX_LINE_LENGTH = 2000; diff --git a/packages/core/src/template/renderer.ts b/packages/core/src/template/renderer.ts index dbf3d3e..6941ec8 100644 --- a/packages/core/src/template/renderer.ts +++ b/packages/core/src/template/renderer.ts @@ -26,6 +26,10 @@ import { MAX_TIMEOUT_MS, MAX_OUTPUT_CHARS, } from '../constants/bash-tool.js'; +import { + DEFAULT_READ_LINES, + MAX_LINE_LENGTH, +} from '../constants/read-file-tool.js'; /** * 获取嵌套属性值 @@ -466,6 +470,12 @@ export function createToolDescriptionContext( EXPLORE_SUBAGENT_TYPE: 'explore', }; + // 添加 read_file 工具相关常量 + const readFileVars: Record = { + DEFAULT_READ_LINES, + MAX_LINE_LENGTH, + }; + // 注册模板函数 const functions: TemplateFunctionRegistry = { CUSTOM_TIMEOUT_MS: () => CUSTOM_TIMEOUT_MS, @@ -475,7 +485,7 @@ export function createToolDescriptionContext( return { ...base, - custom: { ...base.custom, ...toolVars, ...subagentVars }, + custom: { ...base.custom, ...toolVars, ...subagentVars, ...readFileVars }, __functions__: functions, }; } diff --git a/packages/core/src/tools/descriptions/filesystem/edit_file.txt b/packages/core/src/tools/descriptions/filesystem/edit_file.txt index 352f2a9..a6c38b6 100644 --- a/packages/core/src/tools/descriptions/filesystem/edit_file.txt +++ b/packages/core/src/tools/descriptions/filesystem/edit_file.txt @@ -1 +1,9 @@ -通过字符串替换编辑文件的部分内容。比 write_file 更高效,适合修改文件的一小部分。 \ No newline at end of file +Performs exact string replacements in files. + +Usage: +- You must use your `${READ_TOOL_NAME}` tool at least once in the conversation before editing. This tool will error if you attempt an edit without reading the file. +- When editing text from Read tool output, ensure you preserve the exact indentation (tabs/spaces) as it appears AFTER the line number prefix. The line number prefix format is: spaces + line number + tab. Everything after that tab is the actual file content to match. Never include any part of the line number prefix in the old_string or new_string. +- ALWAYS prefer editing existing files in the codebase. NEVER write new files unless explicitly required. +- Only use emojis if the user explicitly requests it. Avoid adding emojis to files unless asked. +- The edit will FAIL if `old_string` is not unique in the file. Either provide a larger string with more surrounding context to make it unique or use `replace_all` to change every instance of `old_string`. +- Use `replace_all` for replacing and renaming strings across the file. This parameter is useful if you want to rename a variable for instance. diff --git a/packages/core/src/tools/descriptions/filesystem/read_file.txt b/packages/core/src/tools/descriptions/filesystem/read_file.txt index cdcebad..2e633be 100644 --- a/packages/core/src/tools/descriptions/filesystem/read_file.txt +++ b/packages/core/src/tools/descriptions/filesystem/read_file.txt @@ -1 +1,12 @@ -读取指定文件的内容 \ No newline at end of file +Reads a file from the local filesystem. You can access any file directly by using this tool. +Assume this tool is able to read all files on the machine. If the User provides a path to a file assume that path is valid. It is okay to read a file that does not exist; an error will be returned. + +Usage: +- The file_path parameter must be an absolute path, not a relative path +- By default, it reads up to ${DEFAULT_READ_LINES} lines starting from the beginning of the file +- You can optionally specify a line offset and limit (especially handy for long files), but it's recommended to read the whole file by not providing these parameters +- Any lines longer than ${MAX_LINE_LENGTH} characters will be truncated +- Results are returned using cat -n format, with line numbers starting at 1 +- This tool can only read files, not directories. To read a directory, use an ls command via the ${BASH_TOOL_NAME} tool. +- You can call multiple tools in a single response. It is always better to speculatively read multiple potentially useful files in parallel. +- If you read a file that exists but has empty contents you will receive a system reminder warning in place of file contents. diff --git a/packages/core/src/tools/descriptions/filesystem/write_file.txt b/packages/core/src/tools/descriptions/filesystem/write_file.txt index 07a94df..37fb1d8 100644 --- a/packages/core/src/tools/descriptions/filesystem/write_file.txt +++ b/packages/core/src/tools/descriptions/filesystem/write_file.txt @@ -1 +1,8 @@ -创建新文件或完全覆盖现有文件。如果只需修改文件的一部分,请使用 edit_file。 \ No newline at end of file +Writes a file to the local filesystem. + +Usage: +- This tool will overwrite the existing file if there is one at the provided path. +- If this is an existing file, you MUST use the ${READ_TOOL_NAME} tool first to read the file's contents. This tool will fail if you did not read the file first. +- ALWAYS prefer editing existing files in the codebase. NEVER write new files unless explicitly required. +- NEVER proactively create documentation files (*.md) or README files. Only create documentation files if explicitly requested by the User. +- Only use emojis if the user explicitly requests it. Avoid writing emojis to files unless asked. \ No newline at end of file