NewFilesystemMiddleware 创建文件系统中间件
(config *FilesystemMiddlewareConfig)
| 48 | |
| 49 | // NewFilesystemMiddleware 创建文件系统中间件 |
| 50 | func NewFilesystemMiddleware(config *FilesystemMiddlewareConfig) *FilesystemMiddleware { |
| 51 | if config.TokenLimit == 0 { |
| 52 | config.TokenLimit = 5000 // 默认 5k tokens(优化:降低阈值以减少 token 消耗) |
| 53 | } |
| 54 | |
| 55 | // 默认启用路径验证 |
| 56 | enablePathValidation := config.EnablePathValidation |
| 57 | if !enablePathValidation && len(config.AllowedPathPrefixes) == 0 { |
| 58 | // 只有在明确禁用且没有指定前缀时才不验证 |
| 59 | enablePathValidation = false |
| 60 | } |
| 61 | |
| 62 | m := &FilesystemMiddleware{ |
| 63 | BaseMiddleware: NewBaseMiddleware("filesystem", 100), |
| 64 | backend: config.Backend, |
| 65 | tokenLimit: config.TokenLimit, |
| 66 | enableEviction: config.EnableEviction, |
| 67 | allowedPathPrefixes: config.AllowedPathPrefixes, |
| 68 | enablePathValidation: enablePathValidation, |
| 69 | customToolDescriptions: config.CustomToolDescriptions, |
| 70 | systemPromptOverride: config.SystemPromptOverride, |
| 71 | hasSystemPromptOverride: config.HasSystemPromptOverride, |
| 72 | } |
| 73 | |
| 74 | // 创建文件系统工具 |
| 75 | m.fsTools = m.createFilesystemTools() |
| 76 | |
| 77 | fsLog.Info(context.Background(), "path validation configured", map[string]any{"enabled": m.enablePathValidation, "prefixes": m.allowedPathPrefixes}) |
| 78 | return m |
| 79 | } |
| 80 | |
| 81 | // createFilesystemTools 创建文件系统工具 |
| 82 | func (m *FilesystemMiddleware) createFilesystemTools() []tools.Tool { |