feat: 添加 file_delete 工具支持
This commit is contained in:
@ -20,6 +20,7 @@ import type {
|
|||||||
ToolName,
|
ToolName,
|
||||||
FileReadArgs,
|
FileReadArgs,
|
||||||
FileWriteArgs,
|
FileWriteArgs,
|
||||||
|
FileDeleteArgs,
|
||||||
FileListArgs,
|
FileListArgs,
|
||||||
SyntaxCheckArgs,
|
SyntaxCheckArgs,
|
||||||
SimulationArgs,
|
SimulationArgs,
|
||||||
@ -61,6 +62,9 @@ export async function executeToolCall(
|
|||||||
case 'file_write':
|
case 'file_write':
|
||||||
resultText = await executeFileWrite(args as unknown as FileWriteArgs);
|
resultText = await executeFileWrite(args as unknown as FileWriteArgs);
|
||||||
break;
|
break;
|
||||||
|
case 'file_delete':
|
||||||
|
resultText = await executeFileDelete(args as unknown as FileDeleteArgs);
|
||||||
|
break;
|
||||||
case 'file_list':
|
case 'file_list':
|
||||||
resultText = await executeFileList(args as unknown as FileListArgs);
|
resultText = await executeFileList(args as unknown as FileListArgs);
|
||||||
break;
|
break;
|
||||||
@ -108,6 +112,43 @@ async function executeFileWrite(args: FileWriteArgs): Promise<string> {
|
|||||||
return `文件已写入: ${args.path}`;
|
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 工具
|
* 执行 file_list 工具
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -192,6 +192,7 @@ export interface ToolResultResponse {
|
|||||||
export type ToolName =
|
export type ToolName =
|
||||||
| 'file_read'
|
| 'file_read'
|
||||||
| 'file_write'
|
| 'file_write'
|
||||||
|
| 'file_delete'
|
||||||
| 'file_list'
|
| 'file_list'
|
||||||
| 'syntax_check'
|
| 'syntax_check'
|
||||||
| 'simulation'
|
| 'simulation'
|
||||||
@ -208,6 +209,12 @@ export interface FileWriteArgs {
|
|||||||
content: string;
|
content: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** file_delete 工具参数 */
|
||||||
|
export interface FileDeleteArgs {
|
||||||
|
/** 要删除的文件路径 */
|
||||||
|
path: string;
|
||||||
|
}
|
||||||
|
|
||||||
/** file_list 工具参数 */
|
/** file_list 工具参数 */
|
||||||
export interface FileListArgs {
|
export interface FileListArgs {
|
||||||
path?: string;
|
path?: string;
|
||||||
@ -237,6 +244,7 @@ export interface WaveformSummaryArgs {
|
|||||||
export type ToolArgs =
|
export type ToolArgs =
|
||||||
| FileReadArgs
|
| FileReadArgs
|
||||||
| FileWriteArgs
|
| FileWriteArgs
|
||||||
|
| FileDeleteArgs
|
||||||
| FileListArgs
|
| FileListArgs
|
||||||
| SyntaxCheckArgs
|
| SyntaxCheckArgs
|
||||||
| SimulationArgs
|
| SimulationArgs
|
||||||
|
|||||||
Reference in New Issue
Block a user