Documentation
¶
Overview ¶
Package file 提供 AI Agent 的文件操作工具
本包实现了安全的文件读写工具,支持:
- 文件读取 (ReadFile)
- 文件写入 (WriteFile)
- 文件列表 (ListFiles)
- 文件删除 (DeleteFile)
安全特性:
- 路径白名单限制
- 文件大小限制
- 扩展名黑名单
使用示例:
tools := file.New(file.DefaultConfig()) readTool := tools.ReadFile() writeTool := tools.WriteFile()
Index ¶
- func ListDir(path string) tool.Tool
- func ReadFile(path string) tool.Tool
- func WriteFile(path, content string) tool.Tool
- type Config
- type DeleteInput
- type DeleteOutput
- type ExistsInput
- type ExistsOutput
- type FileEntry
- type InfoInput
- type InfoOutput
- type ListInput
- type ListOutput
- type ReadInput
- type ReadOutput
- type Tools
- type WriteInput
- type WriteOutput
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct {
// AllowedPaths 允许访问的路径前缀
AllowedPaths []string
// MaxFileSize 最大文件大小(字节)
MaxFileSize int64
// MaxReadSize 最大读取大小(字节)
MaxReadSize int64
// DeniedExtensions 禁止的文件扩展名
DeniedExtensions []string
}
Config 文件工具配置
type DeleteInput ¶
type DeleteInput struct {
Path string `json:"path" desc:"文件或目录路径" required:"true"`
Recursive bool `json:"recursive" desc:"是否递归删除目录"`
}
DeleteInput 删除输入
type DeleteOutput ¶
DeleteOutput 删除输出
type ExistsInput ¶
type ExistsInput struct {
Path string `json:"path" desc:"文件或目录路径" required:"true"`
}
ExistsInput 检查存在输入
type ExistsOutput ¶
ExistsOutput 检查存在输出
type FileEntry ¶
type FileEntry struct {
Name string `json:"name"`
Path string `json:"path"`
Size int64 `json:"size"`
IsDir bool `json:"is_dir"`
ModTime string `json:"mod_time"`
}
FileEntry 文件条目
type InfoInput ¶
type InfoInput struct {
Path string `json:"path" desc:"文件路径" required:"true"`
}
InfoInput 获取文件信息输入
type InfoOutput ¶
type InfoOutput struct {
Name string `json:"name"`
Path string `json:"path"`
Size int64 `json:"size"`
IsDir bool `json:"is_dir"`
Mode string `json:"mode"`
ModTime string `json:"mod_time"`
}
InfoOutput 获取文件信息输出
type ListInput ¶
type ListInput struct {
Path string `json:"path" desc:"目录路径" required:"true"`
Recursive bool `json:"recursive" desc:"是否递归列出"`
Pattern string `json:"pattern" desc:"匹配模式(如 *.txt)"`
}
ListInput 列出目录输入
type ListOutput ¶
type ListOutput struct {
Path string `json:"path"`
Entries []FileEntry `json:"entries"`
Count int `json:"count"`
}
ListOutput 列出目录输出
type ReadInput ¶
type ReadInput struct {
Path string `json:"path" desc:"文件路径" required:"true"`
Encoding string `json:"encoding" desc:"编码格式,默认 utf-8"`
}
ReadInput 读取文件输入
type ReadOutput ¶
ReadOutput 读取文件输出
type WriteInput ¶
type WriteInput struct {
Path string `json:"path" desc:"文件路径" required:"true"`
Content string `json:"content" desc:"文件内容" required:"true"`
Append bool `json:"append" desc:"是否追加模式"`
}
WriteInput 写入文件输入
type WriteOutput ¶
WriteOutput 写入文件输出
Click to show internal directories.
Click to hide internal directories.