移除7个冗余文件操作类内置工具(17→10),去掉minimatch依赖,版本升到2.2.0
This commit is contained in:
@@ -1,59 +1,4 @@
|
||||
[
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "copy_path",
|
||||
"description": "Copies a file or directory to a new location, preserving the original. Directories are copied recursively.\n\nUse this tool when you need to duplicate a file or directory without modifying the original. It is more efficient than reading and writing contents manually. Returns confirmation on success.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"required": ["source_path", "destination_path"],
|
||||
"properties": {
|
||||
"source_path": {
|
||||
"type": "string",
|
||||
"description": "Path of the file or directory to copy, relative to the session working directory. Absolute paths are also accepted."
|
||||
},
|
||||
"destination_path": {
|
||||
"type": "string",
|
||||
"description": "Destination path where the copy should be placed, relative to the session working directory. Absolute paths are also accepted."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "create_directory",
|
||||
"description": "Creates a new directory (and any missing parent directories) at the specified path. Returns confirmation on success.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"required": ["path"],
|
||||
"properties": {
|
||||
"path": {
|
||||
"type": "string",
|
||||
"description": "Path of the directory to create, relative to the session working directory. Absolute paths are also accepted."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "delete_path",
|
||||
"description": "Deletes a file or directory (recursively) at the specified path. Returns confirmation on success.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"required": ["path"],
|
||||
"properties": {
|
||||
"path": {
|
||||
"type": "string",
|
||||
"description": "Path of the file or directory to delete, relative to the session working directory. Absolute paths are also accepted."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
@@ -106,78 +51,6 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "find_path",
|
||||
"description": "Finds file paths matching a glob pattern.\n\n- Supports patterns like \"**/*.js\" or \"src/**/*.ts\".\n- Returns matching paths sorted alphabetically.\n- Use `grep` instead when searching for file contents.\n- Results are paginated (50 per page); use `offset` to get more.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"required": ["glob"],
|
||||
"properties": {
|
||||
"glob": {
|
||||
"type": "string",
|
||||
"description": "Glob pattern to match against file paths, relative to the session working directory."
|
||||
},
|
||||
"offset": {
|
||||
"type": "integer",
|
||||
"description": "Optional starting position for paginated results (0-based). Defaults to 0.",
|
||||
"default": 0,
|
||||
"minimum": 0
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "grep",
|
||||
"description": "Searches file contents using a regular expression.\n\n- Prefer this tool when looking for symbols or text inside files, rather than guessing paths.\n- Supports full regex syntax.\n- Use `include_pattern` to limit which files are searched.\n- Results are paginated (20 per page); use `offset` for more.\n- Do not use HTML entities to escape characters.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"required": ["regex"],
|
||||
"properties": {
|
||||
"regex": {
|
||||
"type": "string",
|
||||
"description": "Regular expression pattern to search for. Matches against file contents only."
|
||||
},
|
||||
"include_pattern": {
|
||||
"type": "string",
|
||||
"nullable": true,
|
||||
"description": "Optional glob pattern to filter which files are searched (e.g., \"**/*.py\"). If omitted, all files are searched."
|
||||
},
|
||||
"offset": {
|
||||
"type": "integer",
|
||||
"description": "Optional starting position for paginated results (0-based). Defaults to 0.",
|
||||
"default": 0,
|
||||
"minimum": 0
|
||||
},
|
||||
"case_sensitive": {
|
||||
"type": "boolean",
|
||||
"description": "Whether the search is case-sensitive. Defaults to false (case-insensitive).",
|
||||
"default": false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "list_directory",
|
||||
"description": "Lists files and directories in a given path.\n\nUse this to see the contents of a directory. For searching by name or content, prefer `find_path` or `grep`.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"path": {
|
||||
"type": "string",
|
||||
"description": "Path of the directory to list, relative to the session working directory. Absolute paths are also accepted. If empty or omitted, lists the current working directory (like `ls` with no argument)."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
@@ -195,27 +68,6 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "move_path",
|
||||
"description": "Moves or renames a file or directory, and returns confirmation that the move succeeded. If source and destination are in the same directory but with different names, this acts as a rename.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"required": ["source_path", "destination_path"],
|
||||
"properties": {
|
||||
"source_path": {
|
||||
"type": "string",
|
||||
"description": "Path of the file or directory to move or rename, relative to the session working directory. Absolute paths are also accepted."
|
||||
},
|
||||
"destination_path": {
|
||||
"type": "string",
|
||||
"description": "Destination path after the move/rename, relative to the session working directory. Absolute paths are also accepted."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
@@ -314,7 +166,7 @@
|
||||
"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. Before creating a new file, verify the parent directory exists with `list_directory`.",
|
||||
"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.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"required": ["path", "content"],
|
||||
|
||||
@@ -4,20 +4,12 @@ const fs = require('fs');
|
||||
const path = require('path');
|
||||
const fsp = require('fs').promises;
|
||||
const { spawn } = require('child_process');
|
||||
const { Minimatch } = require('minimatch');
|
||||
const mcp = require('../mcp');
|
||||
const skills = require('./skills');
|
||||
const rexec = require('../remote-exec');
|
||||
const publicFiles = require('../public-files');
|
||||
const scheduler = require('../scheduler'); // 只依赖 log/cron-parser,不成环(engine→executor→scheduler 单向)
|
||||
|
||||
function makeMatcher(pattern, opts) {
|
||||
const mm = new Minimatch(pattern, opts || {});
|
||||
return function (p) { return mm.match(p); };
|
||||
}
|
||||
|
||||
const IGNORED_DIRS = new Set(['.git', 'node_modules', '__pycache__', '.venv', 'venv', 'dist', 'build', '.next', '.cache']);
|
||||
|
||||
// ---------- 路径沙箱 ----------
|
||||
function resolveInTask(taskDir, p) {
|
||||
if (typeof p !== 'string' || !p.trim()) throw new Error('路径不能为空');
|
||||
@@ -38,29 +30,6 @@ function relOf(taskDir, abs) {
|
||||
return path.relative(path.resolve(taskDir), abs) || '.';
|
||||
}
|
||||
|
||||
// ---------- 文件系统遍历 ----------
|
||||
function walk(taskDir, opts) {
|
||||
const out = [];
|
||||
const maxFiles = (opts && opts.maxFiles) || 20000;
|
||||
function rec(dir) {
|
||||
let entries;
|
||||
try { entries = fs.readdirSync(dir, { withFileTypes: true }); } catch (_) { return; }
|
||||
for (const ent of entries) {
|
||||
if (out.length >= maxFiles) return;
|
||||
if (ent.name.startsWith('.') && IGNORED_DIRS.has(ent.name)) continue;
|
||||
if (IGNORED_DIRS.has(ent.name)) continue;
|
||||
const abs = path.join(dir, ent.name);
|
||||
if (ent.isDirectory()) {
|
||||
rec(abs);
|
||||
} else if (ent.isFile()) {
|
||||
out.push(abs);
|
||||
}
|
||||
}
|
||||
}
|
||||
rec(taskDir);
|
||||
return out;
|
||||
}
|
||||
|
||||
function isProbablyBinary(abs) {
|
||||
let fd;
|
||||
try {
|
||||
@@ -185,7 +154,7 @@ async function toolReadFile(ctx, args) {
|
||||
let st;
|
||||
try { st = await fsp.stat(abs); }
|
||||
catch (_) { throw new Error('文件不存在:' + args.path); }
|
||||
if (st.isDirectory()) throw new Error(args.path + ' 是目录不是文件,请用 list_directory 工具查看目录内容。');
|
||||
if (st.isDirectory()) throw new Error(args.path + ' 是目录不是文件,请用 terminal 执行 ls 查看目录内容。');
|
||||
if (IMAGE_EXTS.has(path.extname(abs).toLowerCase())) {
|
||||
const rel = relOf(ctx.taskDir, abs);
|
||||
if (!ctx.vision) {
|
||||
@@ -439,131 +408,6 @@ async function toolEditFile(ctx, args) {
|
||||
return '已成功应用 ' + applied + ' 处编辑到 ' + relOf(ctx.taskDir, abs);
|
||||
}
|
||||
|
||||
// 对齐 Zed list_directory 的输出格式:
|
||||
// # Folders:
|
||||
// <dir>/<name>
|
||||
//
|
||||
// # Files:
|
||||
// <dir>/<name>
|
||||
// 条目为相对任务目录的完整路径(不带尾部斜杠),目录/文件分节、各自排序,空节省略。
|
||||
async function toolListDirectory(ctx, args) {
|
||||
// 对齐 ls 语义:path 缺省或为空字符串时等同于当前目录(任务目录)。
|
||||
const p = typeof args.path === 'string' && args.path.trim() ? args.path : '.';
|
||||
const abs = resolveInTask(ctx.taskDir, p);
|
||||
const entries = await fsp.readdir(abs, { withFileTypes: true });
|
||||
const rel = relOf(ctx.taskDir, abs);
|
||||
const dirs = []; const files = [];
|
||||
for (const e of entries) {
|
||||
const full = rel === '.' ? e.name : rel + '/' + e.name;
|
||||
if (e.isDirectory()) dirs.push(full);
|
||||
else files.push(full);
|
||||
}
|
||||
dirs.sort(); files.sort();
|
||||
const total = dirs.length + files.length;
|
||||
if (total === 0) return 'Directory is empty';
|
||||
const shownDirs = dirs.slice(0, 500);
|
||||
const shownFiles = files.slice(0, 500 - shownDirs.length);
|
||||
const sections = [];
|
||||
if (shownDirs.length) sections.push('# Folders:\n' + shownDirs.join('\n'));
|
||||
if (shownFiles.length) sections.push('# Files:\n' + shownFiles.join('\n'));
|
||||
let out = sections.join('\n\n');
|
||||
if (shownDirs.length + shownFiles.length < total) out += '\n\n...(' + total + ' entries total, truncated)';
|
||||
return out;
|
||||
}
|
||||
|
||||
async function toolCreateDirectory(ctx, args) {
|
||||
const abs = resolveInTask(ctx.taskDir, args.path);
|
||||
await fsp.mkdir(abs, { recursive: true });
|
||||
return '已创建目录 ' + relOf(ctx.taskDir, abs);
|
||||
}
|
||||
|
||||
async function toolDeletePath(ctx, args) {
|
||||
const abs = resolveInTask(ctx.taskDir, args.path);
|
||||
if (path.resolve(abs) === path.resolve(ctx.taskDir)) throw new Error('禁止删除任务目录本身');
|
||||
await fsp.rm(abs, { recursive: true, force: true });
|
||||
return '已删除 ' + relOf(ctx.taskDir, abs);
|
||||
}
|
||||
|
||||
async function toolMovePath(ctx, args) {
|
||||
const src = resolveInTask(ctx.taskDir, args.source_path);
|
||||
const dst = resolveInTask(ctx.taskDir, args.destination_path);
|
||||
await fsp.mkdir(path.dirname(dst), { recursive: true });
|
||||
await fsp.rename(src, dst);
|
||||
recordFile(ctx, dst);
|
||||
return '已移动/重命名 ' + relOf(ctx.taskDir, src) + ' → ' + relOf(ctx.taskDir, dst);
|
||||
}
|
||||
|
||||
async function toolCopyPath(ctx, args) {
|
||||
const src = resolveInTask(ctx.taskDir, args.source_path);
|
||||
const dst = resolveInTask(ctx.taskDir, args.destination_path);
|
||||
await fsp.mkdir(path.dirname(dst), { recursive: true });
|
||||
await fsp.cp(src, dst, { recursive: true });
|
||||
recordFile(ctx, dst);
|
||||
return '已复制 ' + relOf(ctx.taskDir, src) + ' → ' + relOf(ctx.taskDir, dst);
|
||||
}
|
||||
|
||||
async function toolFindPath(ctx, args) {
|
||||
const files = walk(ctx.taskDir).map(f => relOf(ctx.taskDir, f)).sort();
|
||||
// 对齐 Zed:空 glob 视为 *;不含 / 的裸模式按文件名匹配,
|
||||
// 否则 schema 里 "*thing*.txt 匹配 directory1/a/something.txt" 的例子永远命不中。
|
||||
const glob = (args.glob || '').trim() || '*';
|
||||
const bare = !glob.includes('/');
|
||||
const matcher = makeMatcher(glob, { dot: true });
|
||||
const matched = files.filter(bare ? f => matcher(path.basename(f)) : matcher);
|
||||
const offset = args.offset || 0;
|
||||
const page = matched.slice(offset, offset + 50);
|
||||
if (page.length === 0) return '没有匹配 ' + args.glob + ' 的路径';
|
||||
return page.join('\n') + '\n(共 ' + matched.length + ' 个匹配' + (offset + 50 < matched.length ? ',用 offset=' + (offset + 50) + ' 看下一页' : '') + ')';
|
||||
}
|
||||
|
||||
async function toolGrep(ctx, args) {
|
||||
let re;
|
||||
try { re = new RegExp(args.regex, args.case_sensitive ? '' : 'i'); } catch (e) {
|
||||
return '正则解析失败:' + e.message;
|
||||
}
|
||||
const pattern = args.include_pattern ? makeMatcher(args.include_pattern, { dot: false }) : null;
|
||||
const files = walk(ctx.taskDir);
|
||||
// 对齐 Zed:分页单位是“片段”(匹配行 ±2 行上下文,相邻匹配合并),而非单行。
|
||||
const SNIPPET_CAP = 200; // 防止病态全仓匹配撞大内存
|
||||
const snippets = [];
|
||||
let capped = false;
|
||||
outer:
|
||||
for (const abs of files) {
|
||||
const rel = relOf(ctx.taskDir, abs);
|
||||
if (pattern && !pattern(rel)) continue;
|
||||
if (isProbablyBinary(abs)) continue;
|
||||
let text;
|
||||
try { text = await fsp.readFile(abs, 'utf8'); } catch (_) { continue; }
|
||||
if (text.length > 2 * 1024 * 1024) continue;
|
||||
const lines = text.split('\n');
|
||||
let cur = null;
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
re.lastIndex = 0;
|
||||
if (!re.test(lines[i])) continue;
|
||||
if (cur && i <= cur.end) { if (i + 2 > cur.end) cur.end = Math.min(lines.length - 1, i + 2); continue; }
|
||||
if (cur) snippets.push(cur);
|
||||
if (snippets.length >= SNIPPET_CAP) { capped = true; cur = null; break outer; }
|
||||
cur = { file: rel, lines, start: Math.max(0, i - 2), end: Math.min(lines.length - 1, i + 2) };
|
||||
}
|
||||
if (cur) snippets.push(cur);
|
||||
}
|
||||
const offset = args.offset || 0;
|
||||
const page = snippets.slice(offset, offset + 20);
|
||||
if (page.length === 0) return '没有匹配';
|
||||
let outText = '';
|
||||
let lastFile = null;
|
||||
for (const s of page) {
|
||||
if (s.file !== lastFile) { outText += '\n## ' + s.file + ' 中的匹配\n'; lastFile = s.file; }
|
||||
const label = s.start === s.end ? 'L' + (s.start + 1) : 'L' + (s.start + 1) + '-' + (s.end + 1);
|
||||
outText += '\n### ' + label + '\n```\n' + s.lines.slice(s.start, s.end + 1).join('\n') + '\n```\n';
|
||||
}
|
||||
const more = capped || offset + 20 < snippets.length;
|
||||
const header = more
|
||||
? '显示匹配 ' + (offset + 1) + '-' + (offset + page.length) + '(还有更多,用 offset=' + (offset + 20) + ' 看下一页):\n'
|
||||
: '共找到 ' + snippets.length + ' 处匹配:\n';
|
||||
return header + outText;
|
||||
}
|
||||
|
||||
// 对齐 Zed select_terminal_output_lines:head/tail 各自取行;同时给时中间空一行;不去重叠;0 合法。
|
||||
function selectOutput(text, head, tail) {
|
||||
const hasH = typeof head === 'number' && isFinite(head);
|
||||
@@ -755,14 +599,7 @@ const HANDLERS = {
|
||||
read_file: toolReadFile,
|
||||
write_file: toolWriteFile,
|
||||
edit_file: toolEditFile,
|
||||
list_directory: toolListDirectory,
|
||||
mint_public_link: toolMintPublicLink,
|
||||
create_directory: toolCreateDirectory,
|
||||
delete_path: toolDeletePath,
|
||||
move_path: toolMovePath,
|
||||
copy_path: toolCopyPath,
|
||||
find_path: toolFindPath,
|
||||
grep: toolGrep,
|
||||
terminal: toolTerminal,
|
||||
fetch: toolFetch,
|
||||
schedule_task: toolScheduleTask,
|
||||
|
||||
Reference in New Issue
Block a user