Documentation
¶
Index ¶
- Constants
- func CheckEmptyContent(content string) string
- func ExtractPreview(content string, numLines int) string
- func FormatContentWithLineNumbers(content string, startLine int) string
- func FormatFileSize(bytes int64) string
- func FormatGrepResults(matches []GrepMatch, mode string) string
- func GroupGrepMatches(matches []GrepMatch) map[string][]GrepMatch
- func IsTextFile(path string) bool
- func JoinPath(base, rel string) string
- func NormalizePath(path string) string
- func SanitizeToolCallID(toolCallID string) string
- func TruncateIfTooLong(result string, limit int) string
- type BackendFactory
- type BackendProtocol
- type CompositeBackend
- func (b *CompositeBackend) Edit(ctx context.Context, path, oldStr, newStr string, replaceAll bool) (*EditResult, error)
- func (b *CompositeBackend) GlobInfo(ctx context.Context, pattern, path string) ([]FileInfo, error)
- func (b *CompositeBackend) GrepRaw(ctx context.Context, pattern, path, glob string) ([]GrepMatch, error)
- func (b *CompositeBackend) ListInfo(ctx context.Context, path string) ([]FileInfo, error)
- func (b *CompositeBackend) Read(ctx context.Context, path string, offset, limit int) (string, error)
- func (b *CompositeBackend) String() string
- func (b *CompositeBackend) Write(ctx context.Context, path, content string) (*WriteResult, error)
- type EditResult
- type FileData
- type FileInfo
- type FileMeta
- type FilesystemBackend
- func (b *FilesystemBackend) Edit(ctx context.Context, path, oldStr, newStr string, replaceAll bool) (*EditResult, error)
- func (b *FilesystemBackend) GlobInfo(ctx context.Context, pattern, path string) ([]FileInfo, error)
- func (b *FilesystemBackend) GrepRaw(ctx context.Context, pattern, path, glob string) ([]GrepMatch, error)
- func (b *FilesystemBackend) ListInfo(ctx context.Context, path string) ([]FileInfo, error)
- func (b *FilesystemBackend) Read(ctx context.Context, path string, offset, limit int) (string, error)
- func (b *FilesystemBackend) Write(ctx context.Context, path, content string) (*WriteResult, error)
- type GrepMatch
- type RouteConfig
- type StateBackend
- func (b *StateBackend) Edit(ctx context.Context, path, oldStr, newStr string, replaceAll bool) (*EditResult, error)
- func (b *StateBackend) GetFiles() map[string]*FileData
- func (b *StateBackend) GlobInfo(ctx context.Context, pattern, path string) ([]FileInfo, error)
- func (b *StateBackend) GrepRaw(ctx context.Context, pattern, path, glob string) ([]GrepMatch, error)
- func (b *StateBackend) ListInfo(ctx context.Context, path string) ([]FileInfo, error)
- func (b *StateBackend) LoadFiles(files map[string]*FileData)
- func (b *StateBackend) Read(ctx context.Context, path string, offset, limit int) (string, error)
- func (b *StateBackend) Write(ctx context.Context, path, content string) (*WriteResult, error)
- type StoreBackend
- func (b *StoreBackend) Edit(ctx context.Context, path, oldStr, newStr string, replaceAll bool) (*EditResult, error)
- func (b *StoreBackend) GlobInfo(ctx context.Context, pattern, path string) ([]FileInfo, error)
- func (b *StoreBackend) GrepRaw(ctx context.Context, pattern, path, glob string) ([]GrepMatch, error)
- func (b *StoreBackend) ListInfo(ctx context.Context, path string) ([]FileInfo, error)
- func (b *StoreBackend) Read(ctx context.Context, path string, offset, limit int) (string, error)
- func (b *StoreBackend) Write(ctx context.Context, path, content string) (*WriteResult, error)
- type WriteResult
Constants ¶
const ( // EmptyContentWarning 空文件内容警告 EmptyContentWarning = "System reminder: File exists but has empty contents" // MaxLineLength 单行最大长度,超过此长度将分块显示 MaxLineLength = 10000 // LineNumberWidth 行号宽度(用于对齐) LineNumberWidth = 6 // ToolResultTokenLimit 工具结果 token 限制(与驱逐阈值一致) ToolResultTokenLimit = 20000 // TruncationGuidance 截断提示信息 TruncationGuidance = "... [results truncated, try being more specific with your parameters]" )
常量定义
Variables ¶
This section is empty.
Functions ¶
func CheckEmptyContent ¶
CheckEmptyContent 检查内容是否为空,返回警告信息
参数:
- content: 要检查的内容
返回: 如果为空返回警告信息,否则返回空字符串
参考: deepagents/backends/utils.py:84-95
func ExtractPreview ¶
ExtractPreview 提取内容的前几行作为预览
参数:
- content: 完整内容
- numLines: 提取的行数(默认 10)
返回: 带行号的预览内容
func FormatContentWithLineNumbers ¶
FormatContentWithLineNumbers 格式化文件内容,添加行号(cat -n 风格)
超长行会自动分块,使用延续标记(如 5.1, 5.2)
参数:
- content: 文件内容(字符串或行数组)
- startLine: 起始行号(默认 1)
返回: 带行号和延续标记的格式化内容
参考: deepagents/backends/utils.py:38-81
func FormatFileSize ¶
FormatFileSize 格式化文件大小为人类可读格式
参数:
- bytes: 文件大小(字节)
返回: 格式化后的字符串(如 "1.5 KB", "2.3 MB")
func FormatGrepResults ¶
FormatGrepResults 格式化 Grep 匹配结果
支持三种输出模式:
- "files_with_matches": 只返回包含匹配的文件路径列表
- "content": 返回完整的匹配内容(文件:行号:内容)
- "count": 返回每个文件的匹配数量统计
参数:
- matches: Grep 匹配结果列表
- mode: 输出模式
返回: 格式化后的字符串
func GroupGrepMatches ¶
GroupGrepMatches 将匹配结果按文件分组
参数:
- matches: Grep 匹配结果列表
返回: 按文件分组的匹配结果 map[文件路径][]匹配行
func SanitizeToolCallID ¶
SanitizeToolCallID 清理 tool_call_id,防止路径遍历攻击
将危险字符 (., /, \) 替换为下划线
参考: deepagents/backends/utils.py:29-35
func TruncateIfTooLong ¶
TruncateIfTooLong 如果内容过长则截断,并添加提示信息
参数:
- result: 要检查的结果字符串
- limit: token 限制(默认使用 ToolResultTokenLimit)
返回: 可能被截断的结果(包含截断提示)
参考: deepagents/backends/utils.py 的截断逻辑
Types ¶
type BackendFactory ¶
type BackendFactory func(ctx context.Context) (BackendProtocol, error)
BackendFactory Backend 工厂函数类型 支持延迟初始化和依赖注入
type BackendProtocol ¶
type BackendProtocol interface {
// ListInfo 列出目录内容
// path: 目录路径,空字符串表示根目录
// 返回: 文件信息列表
ListInfo(ctx context.Context, path string) ([]FileInfo, error)
// Read 读取文件内容
// path: 文件路径
// offset: 起始行号 (0-based)
// limit: 读取行数限制 (0表示无限制)
// 返回: 文件内容(字符串)
Read(ctx context.Context, path string, offset, limit int) (string, error)
// Write 写入文件
// path: 文件路径
// content: 文件内容
// 返回: WriteResult,包含状态更新信息(用于 StateBackend)
Write(ctx context.Context, path, content string) (*WriteResult, error)
// Edit 编辑文件(字符串替换)
// path: 文件路径
// oldStr: 要替换的旧字符串
// newStr: 新字符串
// replaceAll: true=替换所有匹配, false=仅替换第一个
// 返回: EditResult,包含替换次数
Edit(ctx context.Context, path, oldStr, newStr string, replaceAll bool) (*EditResult, error)
// GrepRaw 正则表达式搜索
// pattern: 正则表达式模式
// path: 搜索路径(文件或目录)
// glob: 文件名匹配模式(如 "*.go")
// 返回: 匹配结果列表
GrepRaw(ctx context.Context, pattern, path, glob string) ([]GrepMatch, error)
// GlobInfo Glob 模式匹配
// pattern: Glob 模式(如 "**/*.go")
// path: 搜索起始路径
// 返回: 匹配文件信息列表
GlobInfo(ctx context.Context, pattern, path string) ([]FileInfo, error)
}
BackendProtocol 统一后端存储协议 该接口定义了文件系统操作的统一抽象,支持多种后端实现: - StateBackend: 内存临时存储 (会话级) - StoreBackend: 持久化存储 (跨会话) - FilesystemBackend: 真实文件系统 - CompositeBackend: 路由组合器
type CompositeBackend ¶
type CompositeBackend struct {
// contains filtered or unexported fields
}
CompositeBackend 组合路由后端 根据路径前缀将请求路由到不同的后端 例如: "/" -> StateBackend, "/memories/" -> StoreBackend, "/workspace/" -> FilesystemBackend
func NewCompositeBackend ¶
func NewCompositeBackend(defaultBackend BackendProtocol, routes []RouteConfig) *CompositeBackend
NewCompositeBackend 创建 CompositeBackend 实例
func (*CompositeBackend) Edit ¶
func (b *CompositeBackend) Edit(ctx context.Context, path, oldStr, newStr string, replaceAll bool) (*EditResult, error)
Edit 实现 BackendProtocol.Edit
func (*CompositeBackend) GrepRaw ¶
func (b *CompositeBackend) GrepRaw(ctx context.Context, pattern, path, glob string) ([]GrepMatch, error)
GrepRaw 实现 BackendProtocol.GrepRaw
func (*CompositeBackend) Read ¶
func (b *CompositeBackend) Read(ctx context.Context, path string, offset, limit int) (string, error)
Read 实现 BackendProtocol.Read
func (*CompositeBackend) Write ¶
func (b *CompositeBackend) Write(ctx context.Context, path, content string) (*WriteResult, error)
Write 实现 BackendProtocol.Write
type EditResult ¶
type EditResult struct {
Error string `json:"error,omitempty"` // 错误信息,空字符串表示成功
Path string `json:"path,omitempty"` // 编辑文件路径,失败时为空
ReplacementsMade int `json:"replacements_made,omitempty"` // 替换次数,失败时为 0
FilesUpdate map[string]any `json:"files_update,omitempty"` // StateBackend 状态更新,外部存储为 nil
}
EditResult 编辑操作结果 设计参考: DeepAgents backends/protocol.py:61-85
type FileData ¶
type FileData struct {
Lines []string `json:"lines"`
CreatedAt time.Time `json:"created_at"`
ModifiedAt time.Time `json:"modified_at"`
}
FileData 文件数据结构
type FileInfo ¶
type FileInfo struct {
Path string `json:"path"`
IsDirectory bool `json:"is_directory"`
Size int64 `json:"size"`
ModifiedTime time.Time `json:"modified_time"`
CreatedTime time.Time `json:"created_time"`
}
FileInfo 文件信息
type FileMeta ¶
type FileMeta struct {
Size int64 `json:"size"`
CreatedAt time.Time `json:"created_at"`
ModifiedAt time.Time `json:"modified_at"`
}
FileMeta 文件元信息
type FilesystemBackend ¶
type FilesystemBackend struct {
// contains filtered or unexported fields
}
FilesystemBackend 真实文件系统后端 直接操作磁盘文件,通过 Sandbox 接口进行安全访问 适用于需要与实际工作空间文件交互的场景
func NewFilesystemBackend ¶
func NewFilesystemBackend(fs sandbox.SandboxFS) *FilesystemBackend
NewFilesystemBackend 创建 FilesystemBackend 实例
func (*FilesystemBackend) Edit ¶
func (b *FilesystemBackend) Edit(ctx context.Context, path, oldStr, newStr string, replaceAll bool) (*EditResult, error)
Edit 实现 BackendProtocol.Edit
func (*FilesystemBackend) GrepRaw ¶
func (b *FilesystemBackend) GrepRaw(ctx context.Context, pattern, path, glob string) ([]GrepMatch, error)
GrepRaw 实现 BackendProtocol.GrepRaw
func (*FilesystemBackend) Read ¶
func (b *FilesystemBackend) Read(ctx context.Context, path string, offset, limit int) (string, error)
Read 实现 BackendProtocol.Read
func (*FilesystemBackend) Write ¶
func (b *FilesystemBackend) Write(ctx context.Context, path, content string) (*WriteResult, error)
Write 实现 BackendProtocol.Write
type GrepMatch ¶
type GrepMatch struct {
Path string `json:"path"`
LineNumber int `json:"line_number"`
Line string `json:"line"`
Match string `json:"match"`
}
GrepMatch Grep 搜索匹配结果
type RouteConfig ¶
type RouteConfig struct {
Prefix string // 路径前缀
Backend BackendProtocol // 对应的后端
}
RouteConfig 路由配置
type StateBackend ¶
type StateBackend struct {
// contains filtered or unexported fields
}
StateBackend 内存临时存储后端 数据存储在内存 map 中,生命周期与 Agent 会话一致 适用于临时文件、中间结果等短期存储需求
func (*StateBackend) Edit ¶
func (b *StateBackend) Edit(ctx context.Context, path, oldStr, newStr string, replaceAll bool) (*EditResult, error)
Edit 实现 BackendProtocol.Edit
func (*StateBackend) GetFiles ¶
func (b *StateBackend) GetFiles() map[string]*FileData
GetFiles 获取所有文件数据 (用于调试)
func (*StateBackend) GrepRaw ¶
func (b *StateBackend) GrepRaw(ctx context.Context, pattern, path, glob string) ([]GrepMatch, error)
GrepRaw 实现 BackendProtocol.GrepRaw
func (*StateBackend) LoadFiles ¶
func (b *StateBackend) LoadFiles(files map[string]*FileData)
LoadFiles 加载文件数据 (用于状态恢复)
func (*StateBackend) Write ¶
func (b *StateBackend) Write(ctx context.Context, path, content string) (*WriteResult, error)
Write 实现 BackendProtocol.Write
type StoreBackend ¶
type StoreBackend struct {
// contains filtered or unexported fields
}
StoreBackend 持久化存储后端 使用 Store 接口进行持久化,数据跨会话保留 适用于需要长期保存的文件(如知识库、记忆等)
func NewStoreBackend ¶
func NewStoreBackend(s store.Store, agentID string) *StoreBackend
NewStoreBackend 创建 StoreBackend 实例
func (*StoreBackend) Edit ¶
func (b *StoreBackend) Edit(ctx context.Context, path, oldStr, newStr string, replaceAll bool) (*EditResult, error)
Edit 实现 BackendProtocol.Edit
func (*StoreBackend) GrepRaw ¶
func (b *StoreBackend) GrepRaw(ctx context.Context, pattern, path, glob string) ([]GrepMatch, error)
GrepRaw 实现 BackendProtocol.GrepRaw
func (*StoreBackend) Write ¶
func (b *StoreBackend) Write(ctx context.Context, path, content string) (*WriteResult, error)
Write 实现 BackendProtocol.Write
type WriteResult ¶
type WriteResult struct {
Error string `json:"error,omitempty"` // 错误信息,空字符串表示成功
Path string `json:"path,omitempty"` // 写入文件路径,失败时为空
BytesWritten int64 `json:"bytes_written,omitempty"` // 写入字节数
FilesUpdate map[string]any `json:"files_update,omitempty"` // StateBackend 状态更新,外部存储为 nil
}
WriteResult 写入操作结果 设计参考: DeepAgents backends/protocol.py:36-58