Files
ai-terminal-assistant/src/tools/filesystem/create_directory.ts
T
kurihada bc1ece3dad feat: 实现 Tool Search Tool 动态工具发现机制
- 新增 ToolRegistry 工具注册表,支持核心工具和延迟加载工具分离
- 新增 tool_search 元工具,支持关键词搜索发现可用工具
- 新增基于关键词的搜索算法,按相关度评分排序
- 为所有工具添加 metadata(分类、关键词、延迟加载标识)
- 修改 Agent 支持动态工具注入,tool_search 结果自动添加到可用工具
- 核心工具(tool_search, bash)始终加载,其他工具按需发现
2025-12-10 19:51:25 +08:00

92 lines
2.6 KiB
TypeScript

import * as fs from 'fs/promises';
import * as path from 'path';
import type { ToolResult } from '../../types/index.js';
import type { ToolWithMetadata } from '../types.js';
import { loadDescription } from '../load_description.js';
import { getPermissionManager } from '../../permission/index.js';
export const createDirectoryTool: ToolWithMetadata = {
name: 'create_directory',
description: loadDescription('create_directory'),
metadata: {
name: 'create_directory',
category: 'filesystem',
description: '创建目录',
keywords: ['create', 'directory', 'mkdir', 'folder', 'new', '创建', '目录', '文件夹', '新建'],
deferLoading: true,
},
parameters: {
path: {
type: 'string',
description: '要创建的目录路径',
required: true,
},
},
execute: async (params: Record<string, unknown>): Promise<ToolResult> => {
const dirPath = params.path as string;
const cwd = process.cwd();
const absolutePath = path.isAbsolute(dirPath)
? dirPath
: path.join(cwd, dirPath);
// 权限检查
const permissionManager = getPermissionManager();
const permResult = await permissionManager.checkFilePermission({
operation: 'mkdir',
path: absolutePath,
workdir: cwd,
});
if (!permResult.allowed) {
if (permResult.needsConfirmation) {
return {
success: false,
output: '',
error: `需要用户确认: 创建目录 ${absolutePath}\n原因: ${permResult.reason || '需要权限确认'}`,
};
}
return {
success: false,
output: '',
error: `权限被拒绝: ${permResult.reason || '不允许创建此目录'}`,
};
}
try {
// 检查目录是否已存在
try {
const stats = await fs.stat(absolutePath);
if (stats.isDirectory()) {
return {
success: true,
output: `目录已存在: ${absolutePath}`,
};
} else {
return {
success: false,
output: '',
error: `路径已存在且不是目录: ${absolutePath}`,
};
}
} catch {
// 目录不存在,继续创建
}
// 创建目录(递归创建父目录)
await fs.mkdir(absolutePath, { recursive: true });
return {
success: true,
output: `已创建目录: ${absolutePath}`,
};
} catch (error) {
return {
success: false,
output: '',
error: error instanceof Error ? error.message : String(error),
};
}
},
};