write_file:null内容双层拒防静默清空;目录目标中文预检;返回值区分新建/覆盖(原N字节);checkToolArgs缺参判定改为null判缺/空串放行;描述补覆盖与UTF-8边界
This commit is contained in:
@@ -851,8 +851,10 @@ function checkToolArgs(tools, name, raw) {
|
||||
}
|
||||
const def = (tools || []).find(t => t && t.function && t.function.name === name);
|
||||
const required = def && def.function.parameters && def.function.parameters.required;
|
||||
// null/undefined 都算缺;空串放行(如 write_file content:"" 写空文件合法),
|
||||
// 必填串参传空的场景由各工具自己的中文校验兑底(path 空→「路径不能为空」,url 空→「缺少参数 url」…)。
|
||||
const missing = required && required.length
|
||||
? required.filter(k => args[k] === undefined || args[k] === '') : [];
|
||||
? required.filter(k => args[k] == null) : [];
|
||||
if (missing.length) return { args: null, error: 'Error: ToolCall Missing Params: ' + missing.join('、') };
|
||||
return { args, error: null };
|
||||
}
|
||||
|
||||
@@ -166,18 +166,18 @@
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "write_file",
|
||||
"description": "Creates a new file or overwrites an existing file with the given content.\n\nUse `edit_file` for targeted changes to existing files. Parent directories are created automatically as needed.",
|
||||
"description": "Creates a new file or overwrites an existing file with the given content.\n\nParent directories are created automatically, so the path does not need to exist beforehand.\n\nOverwriting replaces the file's entire content irreversibly. If the file already exists, read it first, or use `edit_file` for targeted changes instead.\n\nText is written as UTF-8. To produce binary files, use `terminal` instead.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"required": ["path", "content"],
|
||||
"properties": {
|
||||
"path": {
|
||||
"type": "string",
|
||||
"description": "Path of the file to create or overwrite, relative to the session working directory. Absolute paths are also accepted."
|
||||
"description": "Path of the file to create or overwrite, relative to the session working directory. Absolute paths inside the working directory are also accepted."
|
||||
},
|
||||
"content": {
|
||||
"type": "string",
|
||||
"description": "The entire content to write to the file."
|
||||
"description": "The entire content to write. Required — pass an empty string to create an empty file; omitting it is an error."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -197,10 +197,21 @@ async function toolReadFile(ctx, args) {
|
||||
|
||||
async function toolWriteFile(ctx, args) {
|
||||
const abs = resolveInTask(ctx.taskDir, args.path);
|
||||
// 纵深防御:引擎 checkToolArgs 已把 null 判缺,但直调 executor(测试等)不经过它;
|
||||
// 旧版 `?? ''` 会把 null 洗成空串静默清空已有文件,这里显式拒绝。
|
||||
if (args.content == null) throw new Error('write_file 缺少参数 content(写空文件请显式传空字符串 "")');
|
||||
let st = null;
|
||||
try { st = await fsp.stat(abs); } catch (_) {}
|
||||
if (st && st.isDirectory()) throw new Error('无法写入:路径是目录 ' + args.path);
|
||||
await fsp.mkdir(path.dirname(abs), { recursive: true });
|
||||
await fsp.writeFile(abs, args.content ?? '', 'utf8');
|
||||
await fsp.writeFile(abs, String(args.content), 'utf8');
|
||||
recordFile(ctx, abs);
|
||||
return '已成功写入文件 ' + relOf(ctx.taskDir, abs) + '(' + Buffer.byteLength(args.content || '') + ' 字节)';
|
||||
const nbytes = Buffer.byteLength(String(args.content), 'utf8');
|
||||
const rel = relOf(ctx.taskDir, abs);
|
||||
// 区分新建/覆盖:「原 M 字节」是给模型的止损信号(盲覆盖大文件时能立刻察觉)。
|
||||
return st
|
||||
? '已覆盖文件 ' + rel + '(' + nbytes + ' 字节,原 ' + st.size + ' 字节)'
|
||||
: '已创建文件 ' + rel + '(' + nbytes + ' 字节)';
|
||||
}
|
||||
|
||||
// ---------- edit_file 匹配(对齐 Zed edit_session/streaming_fuzzy_matcher 的终止态语义) ----------
|
||||
|
||||
Reference in New Issue
Block a user