file

package
v0.5.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 12, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package file 提供 AI Agent 的文件操作工具

本包实现了安全的文件读写工具,支持:

  • 文件读取 (ReadFile)
  • 文件写入 (WriteFile)
  • 文件列表 (ListFiles)
  • 文件删除 (DeleteFile)

安全特性:

  • 路径白名单限制
  • 文件大小限制
  • 扩展名黑名单

使用示例:

tools := file.New(file.DefaultConfig())
readTool := tools.ReadFile()
writeTool := tools.WriteFile()

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListDir

func ListDir(path string) tool.Tool

ListDir 列出目录的便捷函数

func ReadFile

func ReadFile(path string) tool.Tool

ReadFile 读取文件的便捷函数

func WriteFile

func WriteFile(path, content string) tool.Tool

WriteFile 写入文件的便捷函数

Types

type Config

type Config struct {
	// AllowedPaths 允许访问的路径前缀
	AllowedPaths []string

	// MaxFileSize 最大文件大小(字节)
	MaxFileSize int64

	// MaxReadSize 最大读取大小(字节)
	MaxReadSize int64

	// DeniedExtensions 禁止的文件扩展名
	DeniedExtensions []string
}

Config 文件工具配置

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig 返回默认配置

type DeleteInput

type DeleteInput struct {
	Path      string `json:"path" desc:"文件或目录路径" required:"true"`
	Recursive bool   `json:"recursive" desc:"是否递归删除目录"`
}

DeleteInput 删除输入

type DeleteOutput

type DeleteOutput struct {
	Path    string `json:"path"`
	Deleted bool   `json:"deleted"`
}

DeleteOutput 删除输出

type ExistsInput

type ExistsInput struct {
	Path string `json:"path" desc:"文件或目录路径" required:"true"`
}

ExistsInput 检查存在输入

type ExistsOutput

type ExistsOutput struct {
	Exists bool `json:"exists"`
	IsDir  bool `json:"is_dir"`
}

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

type ReadOutput struct {
	Content string `json:"content"`
	Size    int64  `json:"size"`
}

ReadOutput 读取文件输出

type Tools

type Tools struct {
	// contains filtered or unexported fields
}

Tools 文件工具集

func New

func New(config *Config) *Tools

New 创建文件工具集

func (*Tools) All

func (t *Tools) All() []tool.Tool

All 返回所有文件工具

func (*Tools) DeleteTool

func (t *Tools) DeleteTool() tool.Tool

DeleteTool 返回删除文件工具

func (*Tools) ExistsTool

func (t *Tools) ExistsTool() tool.Tool

ExistsTool 返回检查文件存在工具

func (*Tools) InfoTool

func (t *Tools) InfoTool() tool.Tool

InfoTool 返回获取文件信息工具

func (*Tools) ListTool

func (t *Tools) ListTool() tool.Tool

ListTool 返回列出目录工具

func (*Tools) ReadTool

func (t *Tools) ReadTool() tool.Tool

ReadTool 返回读取文件工具

func (*Tools) WriteTool

func (t *Tools) WriteTool() tool.Tool

WriteTool 返回写入文件工具

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

type WriteOutput struct {
	Path    string `json:"path"`
	Written int    `json:"written"`
}

WriteOutput 写入文件输出

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL