Files
agents_and_robots/tools/file/file.go
T
egutierrez 8d89a762fb refactor: mover tools a subpackages individuales
Cada tool ahora vive en su propio subpackage dentro de tools/ (clock, file,
http, knowledgetools, matrix, memorytools, ssh, weather) en lugar de archivos
planos en el paquete raíz tools/. Esto mejora la organización, permite imports
selectivos y reduce acoplamiento entre tools. El paquete tools/ raíz conserva
los tipos base (Def, Param, Result, ToolFunc, Tool, Registry).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-07 17:16:45 +00:00

71 lines
1.7 KiB
Go

package file
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/enmanuel/agents/internal/config"
"github.com/enmanuel/agents/tools"
)
// NewReadFile creates a read_file tool that reads local files.
// Validates paths against cfg.AllowedPaths when non-empty.
func NewReadFile(cfg config.FileOpsCfg) tools.Tool {
return tools.Tool{
Def: tools.Def{
Name: "read_file",
Description: "Read the contents of a local file.",
Parameters: []tools.Param{
{Name: "path", Type: "string", Description: "Absolute path to the file to read", Required: true},
},
},
Exec: func(ctx context.Context, args map[string]any) tools.Result {
path := tools.GetString(args, "path")
if path == "" {
return tools.Result{Err: fmt.Errorf("read_file: path is required")}
}
absPath, err := filepath.Abs(path)
if err != nil {
return tools.Result{Err: fmt.Errorf("read_file: %w", err)}
}
if err := validatePath(absPath, cfg.AllowedPaths); err != nil {
return tools.Result{Err: err}
}
data, err := os.ReadFile(absPath)
if err != nil {
return tools.Result{Err: fmt.Errorf("read_file: %w", err)}
}
// Limit output to 64 KB
content := string(data)
if len(content) > 64*1024 {
content = content[:64*1024] + "\n... (truncated)"
}
return tools.Result{Output: content}
},
}
}
func validatePath(absPath string, allowedPaths []string) error {
if len(allowedPaths) == 0 {
return nil
}
for _, allowed := range allowedPaths {
a, err := filepath.Abs(allowed)
if err != nil {
continue
}
if strings.HasPrefix(absPath, a) {
return nil
}
}
return fmt.Errorf("path %q not under any allowed path", absPath)
}