add file operation tool definitions

This commit is contained in:
scorpio 2026-03-10 09:23:59 +08:00
parent 4280054944
commit ae4afe9271

View File

@ -0,0 +1,130 @@
package tools
import "github.com/sashabaranov/go-openai"
var FileTools = []openai.Tool{
{
Type: openai.ToolTypeFunction,
Function: &openai.FunctionDefinition{
Name: "glob",
Description: "模式匹配查找文件,支持 * ** ? 等通配符",
Parameters: `{
"type": "object",
"properties": {
"pattern": {
"type": "string",
"description": "glob 模式,如 **/*.go, *.ts"
}
},
"required": ["pattern"]
}`,
},
},
{
Type: openai.ToolTypeFunction,
Function: &openai.FunctionDefinition{
Name: "grep",
Description: "在文件中搜索内容,支持正则表达式",
Parameters: `{
"type": "object",
"properties": {
"pattern": {
"type": "string",
"description": "正则表达式搜索模式"
},
"path": {
"type": "string",
"description": "搜索目录路径"
},
"include": {
"type": "string",
"description": "文件过滤,如 *.go, *.ts"
}
},
"required": ["pattern"]
}`,
},
},
{
Type: openai.ToolTypeFunction,
Function: &openai.FunctionDefinition{
Name: "read_file",
Description: "读取文件内容,支持大文件分段读取",
Parameters: `{
"type": "object",
"properties": {
"filename": {
"type": "string",
"description": "文件名(相对于 workspace"
},
"offset": {
"type": "integer",
"description": "起始行号(从 0 开始)"
},
"limit": {
"type": "integer",
"description": "读取行数,默认 100"
}
},
"required": ["filename"]
}`,
},
},
{
Type: openai.ToolTypeFunction,
Function: &openai.FunctionDefinition{
Name: "edit_file",
Description: "编辑文件内容,使用字符串替换",
Parameters: `{
"type": "object",
"properties": {
"filename": {
"type": "string",
"description": "文件名(相对于 workspace"
},
"old_string": {
"type": "string",
"description": "要替换的原始内容"
},
"new_string": {
"type": "string",
"description": "替换后的新内容"
}
},
"required": ["filename", "old_string", "new_string"]
}`,
},
},
{
Type: openai.ToolTypeFunction,
Function: &openai.FunctionDefinition{
Name: "write_file",
Description: "写入或创建文件",
Parameters: `{
"type": "object",
"properties": {
"filename": {
"type": "string",
"description": "文件名(相对于 workspace"
},
"content": {
"type": "string",
"description": "文件内容"
}
},
"required": ["filename", "content"]
}`,
},
},
{
Type: openai.ToolTypeFunction,
Function: &openai.FunctionDefinition{
Name: "list_workspace",
Description: "列出工作区的所有文件",
Parameters: `{
"type": "object",
"properties": {}
}`,
},
},
}