feat: 添加 file_delete 工具支持

This commit is contained in:
XiaoFeng
2025-12-25 16:17:31 +08:00
parent 6c5d470bad
commit 5ea5ddba6e
2 changed files with 49 additions and 0 deletions

View File

@ -20,6 +20,7 @@ import type {
ToolName,
FileReadArgs,
FileWriteArgs,
FileDeleteArgs,
FileListArgs,
SyntaxCheckArgs,
SimulationArgs,
@ -61,6 +62,9 @@ export async function executeToolCall(
case 'file_write':
resultText = await executeFileWrite(args as unknown as FileWriteArgs);
break;
case 'file_delete':
resultText = await executeFileDelete(args as unknown as FileDeleteArgs);
break;
case 'file_list':
resultText = await executeFileList(args as unknown as FileListArgs);
break;
@ -108,6 +112,43 @@ async function executeFileWrite(args: FileWriteArgs): Promise<string> {
return `文件已写入: ${args.path}`;
}
/**
* 执行 file_delete 工具
* 删除指定路径的文件
*/
async function executeFileDelete(args: FileDeleteArgs): Promise<string> {
const filePath = args.path;
// 获取工作区路径
const workspaceFolders = vscode.workspace.workspaceFolders;
if (!workspaceFolders || workspaceFolders.length === 0) {
throw new Error('请先打开一个工作区');
}
const workspacePath = workspaceFolders[0].uri.fsPath;
// 解析文件路径(支持相对路径和绝对路径)
const absolutePath = path.isAbsolute(filePath)
? filePath
: path.join(workspacePath, filePath);
// 检查文件是否存在
if (!fs.existsSync(absolutePath)) {
throw new Error(`文件不存在: ${filePath}`);
}
// 检查是否为文件(不允许删除目录)
const stat = fs.statSync(absolutePath);
if (stat.isDirectory()) {
throw new Error(`不能删除目录,请指定文件路径: ${filePath}`);
}
// 删除文件
fs.unlinkSync(absolutePath);
return `文件已删除: ${filePath}`;
}
/**
* 执行 file_list 工具
*/

View File

@ -192,6 +192,7 @@ export interface ToolResultResponse {
export type ToolName =
| 'file_read'
| 'file_write'
| 'file_delete'
| 'file_list'
| 'syntax_check'
| 'simulation'
@ -208,6 +209,12 @@ export interface FileWriteArgs {
content: string;
}
/** file_delete 工具参数 */
export interface FileDeleteArgs {
/** 要删除的文件路径 */
path: string;
}
/** file_list 工具参数 */
export interface FileListArgs {
path?: string;
@ -237,6 +244,7 @@ export interface WaveformSummaryArgs {
export type ToolArgs =
| FileReadArgs
| FileWriteArgs
| FileDeleteArgs
| FileListArgs
| SyntaxCheckArgs
| SimulationArgs