read_file/edit_file:新增32MB全文读取上限防OOM(超限引导terminal);空文件/end<start显式反馈;CRLF显示剥\r;read_file描述补齐阈值/vision/大小契约
This commit is contained in:
@@ -72,7 +72,7 @@
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "read_file",
|
||||
"description": "Reads the content of a file.\n\n- For large text files, this tool may return an outline with symbol names and line numbers instead of the full content. This outline IS a successful response; use the line numbers to read specific sections with `start_line` and `end_line`.\n- Do NOT retry reading the same file without line numbers if you receive an outline.\n- Supports reading image files (PNG, JPEG, WebP, GIF, BMP, TIFF) and returns them as visual content you can analyze directly.\n- When reading text files, each line in the returned content is prefixed with a right-aligned line number field and a tab. When extracting text for use in `edit_file`, remove this prefix and keep only the content after the tab, preserving original indentation.",
|
||||
"description": "Reads the content of a file.\n\nText files: each returned line is prefixed with a line number right-aligned in a 6-character field, followed by a tab, then the line content.\n\nLarge text files (roughly over 16 KB) are not returned in full: you get an outline listing top-level symbols (or Markdown headings) with line numbers instead. The outline IS a successful response — do NOT retry the same call without a line range; use the outline line numbers with `start_line` / `end_line` to read specific sections. If no outline can be extracted, a first-1KB excerpt is returned instead. Supplying `start_line` or `end_line` disables outline mode.\n\nImage files (PNG, JPEG, WebP, GIF, BMP, TIFF) are returned as visual content you can analyze directly, but only if the current model supports vision; otherwise a text placeholder is returned — that placeholder is not an error. Very large images (roughly over 50 MB) are skipped, and images may be downscaled (long edge ~2000 px, re-encoded as JPEG) before being returned.\n\nBinary files are not returned as text; a short notice is returned instead. Text files over 32 MB cannot be read by this tool — use `terminal` (grep -n / head / sed -n).",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"required": ["path"],
|
||||
@@ -84,14 +84,14 @@
|
||||
"start_line": {
|
||||
"type": "integer",
|
||||
"nullable": true,
|
||||
"description": "Optional line number to start reading from (1-based).",
|
||||
"description": "Optional 1-based line number to start reading from.",
|
||||
"default": null,
|
||||
"minimum": 1
|
||||
},
|
||||
"end_line": {
|
||||
"type": "integer",
|
||||
"nullable": true,
|
||||
"description": "Optional line number to end reading at (1-based, inclusive).",
|
||||
"description": "Optional 1-based line number to end reading at (inclusive). Must be >= start_line. Supplying either bound disables outline mode.",
|
||||
"default": null,
|
||||
"minimum": 1
|
||||
}
|
||||
|
||||
@@ -55,6 +55,8 @@ function truncate(text, max) {
|
||||
|
||||
// 对齐 Zed:整读时文件字节数超过该阈值则返回大纲(outline),而非截断内容。
|
||||
const AUTO_OUTLINE_SIZE = 16384;
|
||||
// 全文读/编辑的硬上限:超过即拒绝整读进内存(OOM 防护,st.size 在 read 前就有),引导走 terminal。
|
||||
const TEXT_FULL_READ_CAP = 32 * 1024 * 1024;
|
||||
const IMAGE_EXTS = new Set(['.png', '.jpg', '.jpeg', '.webp', '.gif', '.bmp', '.tiff', '.tif']);
|
||||
|
||||
// read_file 读图(仅当会话绑定的模型 vision: true):sharp 压缩后落盘到任务目录 .imgcache/,
|
||||
@@ -166,12 +168,21 @@ async function toolReadFile(ctx, args) {
|
||||
return await readImageAsBlocks(ctx, abs, rel, st.size);
|
||||
}
|
||||
if (isProbablyBinary(abs)) return '[二进制文件,跳过文本读取] ' + relOf(ctx.taskDir, abs);
|
||||
if (st.size > TEXT_FULL_READ_CAP) {
|
||||
return '(文件过大:' + Math.round(st.size / 1048576) + ' MB,超过 read_file 全文读取上限 '
|
||||
+ (TEXT_FULL_READ_CAP / 1048576) + ' MB,拒绝整读;请用 terminal 的 grep -n / head / sed -n 定位阅读)';
|
||||
}
|
||||
const text = await fsp.readFile(abs, 'utf8');
|
||||
if (!text) return '(空文件)';
|
||||
let lines = text.split('\n');
|
||||
// CRLF 文件显示层剥去行尾 \r:模型拿到的行更干净;edit_file 精确/模糊匹配与 CRLF 归一不受影响。
|
||||
if (text.includes('\r\n')) lines = lines.map(l => (l.endsWith('\r') ? l.slice(0, -1) : l));
|
||||
const total = lines.length;
|
||||
const startLine = Math.max(1, args.start_line || 1);
|
||||
let endLine = args.end_line || total;
|
||||
if (startLine > total) return '(文件共 ' + total + ' 行,start_line 超出范围)';
|
||||
// 显式拒绝而非 slice 出空串静默返回:空串会让模型误判文件为空/工具坏了而换路重试。
|
||||
if (endLine < startLine) throw new Error('end_line 必须 >= start_line(got start_line=' + startLine + ', end_line=' + args.end_line + ')');
|
||||
// 对齐 Zed:未指定范围且文件超过 16KB 时返回大纲;提取不到大纲则回退回首 1KB。
|
||||
if (!args.start_line && !args.end_line && Buffer.byteLength(text, 'utf8') > AUTO_OUTLINE_SIZE) {
|
||||
const outline = buildOutline(abs, text);
|
||||
@@ -419,6 +430,8 @@ async function toolEditFile(ctx, args) {
|
||||
try { st = await fsp.stat(abs); }
|
||||
catch (_) { throw new Error('文件不存在:' + args.path); }
|
||||
if (st.isDirectory()) throw new Error('无法编辑:路径是目录 ' + args.path);
|
||||
// 同 read_file 的 OOM 防护:edit_file 也要全量读入,超限拒操作。
|
||||
if (st.size > TEXT_FULL_READ_CAP) throw new Error('文件超过 ' + (TEXT_FULL_READ_CAP / 1048576) + 'MB 全文读取上限,edit_file 拒绝操作;请用 terminal 的 sed 等工具编辑');
|
||||
const edits = args.edits || [];
|
||||
let text = await fsp.readFile(abs, 'utf8');
|
||||
const original = text;
|
||||
|
||||
@@ -285,8 +285,8 @@ stateDiagram-v2
|
||||
|
||||
| 工具 | 实现要点 |
|
||||
|---|---|
|
||||
| `read_file` | 走 `resolveInTask` 防路径穿越;图像(PNG/JPEG/WebP/GIF/BMP/TIFF)按 vision 能力走 `sharp` 压缩后落盘 `.imgcache/` 并回传 `image_rel`(长边 ≤ 2000px,JPEG q=80;文件名带源文件 mtime+size 指纹,重读复用不增殖;重编码无收益时不落盘、直指原文件),发送前由 `llm.buildMessages` 现签成公网外链(engine 侧 mint 前 realpath 复核);大文本/二进制给 outline;行号前缀格式 "`<6位右对齐行号>\\t<原文>`"(`edit_file` 用) |
|
||||
| `write_file` / `edit_file` | `edit_file` 用模糊定位(Zed 风格):先精确匹配,失败后用 Levenshtein DP 找最相似行;DP 候选区间不连续(中间跳过无关 buffer 行)即弃用,防静默吞行;空 `new_text` = 整行干净删除(精确/模糊两层统一,对齐 `"".lines()` 零行语义);`old_text`/`new_text` 协议见 `definitions.json` |
|
||||
| `read_file` | 走 `resolveInTask` 防路径穿越;图像(PNG/JPEG/WebP/GIF/BMP/TIFF)按 vision 能力走 `sharp` 压缩后落盘 `.imgcache/` 并回传 `image_rel`(长边 ≤ 2000px,JPEG q=80;文件名带源文件 mtime+size 指纹,重读复用不增殖;重编码无收益时不落盘、直指原文件),发送前由 `llm.buildMessages` 现签成公网外链(engine 侧 mint 前 realpath 复核);大文本/二进制给 outline,超 32MB 拒读并引导 terminal;行号前缀格式 "`<6位右对齐行号>\\t<原文>`"(`edit_file` 用) |
|
||||
| `write_file` / `edit_file` | `edit_file` 用模糊定位(Zed 风格):先精确匹配,失败后用 Levenshtein DP 找最相似行;超 32MB 拒绝整文件读写(OOM 防护);DP 候选区间不连续(中间跳过无关 buffer 行)即弃用,防静默吞行;空 `new_text` = 整行干净删除(精确/模糊两层统一,对齐 `"".lines()` 零行语义);`old_text`/`new_text` 协议见 `definitions.json` |
|
||||
| 已移除的工具 | `list_directory` / `create_directory` / `delete_path` / `move_path` / `copy_path` / `find_path` / `grep`:环境固定,文件操作与检索统一走 `terminal`(ls / mkdir / rm / mv / cp / find / rg),内置工具 17 → 10 |
|
||||
| `terminal` | **唯一一个走 executor 的内置工具**(当 `rexec.isRemote(cfg)`);本地分支用 `spawn` + `detached` + 进程组 kill;远端分支 `rexec.run` SSE 收流,结果套上超时/截断/取消注解后 `truncate(text, cfg.terminal.max_output_chars)` 返回 |
|
||||
| `fetch` | `AbortController` 覆盖全生命周期(响应头之后的 body 读流同样被超时/取消打断,取消用与 terminal 同款的 200ms 轮询);content-type 先行,非文本只回状态/类型/尺寸不读 body;流式读到 `cfg.fetch.max_bytes`(默认 1MB)即断开并标注截断;按响应 charset 解码(full-icu 支持 gbk/gb18030 等,未知编码退回 UTF-8);HTML 用 `htmlToMarkdown` 转;HTML 实体单遍解码(常见命名实体+数字实体通吃,修 `&` 双重解码);网络错误解包 `err.cause` 直述 ENOTFOUND/TLS 等真因;engine 不截工具结果,返回值末尾另有 `truncate` 1MB 字符护栏 |
|
||||
|
||||
Reference in New Issue
Block a user