| 68 | } |
| 69 | |
| 70 | func (t *DocsGetTool) Execute(ctx context.Context, input map[string]any, tc *tools.ToolContext) (any, error) { |
| 71 | relPath, _ := input["path"].(string) |
| 72 | if relPath == "" { |
| 73 | return nil, errors.New("path is required") |
| 74 | } |
| 75 | |
| 76 | // 防止路径遍历 |
| 77 | if strings.Contains(relPath, "..") { |
| 78 | return nil, errors.New("path traversal is not allowed") |
| 79 | } |
| 80 | |
| 81 | fullPath := filepath.Join(t.baseDir, filepath.FromSlash(relPath)) |
| 82 | abs, err := filepath.Abs(fullPath) |
| 83 | if err != nil { |
| 84 | return nil, fmt.Errorf("resolve path: %w", err) |
| 85 | } |
| 86 | |
| 87 | if !strings.HasPrefix(abs, t.baseDir) { |
| 88 | return nil, errors.New("path outside baseDir is not allowed") |
| 89 | } |
| 90 | |
| 91 | data, err := os.ReadFile(abs) |
| 92 | if err != nil { |
| 93 | return nil, fmt.Errorf("read file: %w", err) |
| 94 | } |
| 95 | |
| 96 | return map[string]any{ |
| 97 | "path": relPath, |
| 98 | "content": string(data), |
| 99 | }, nil |
| 100 | } |
| 101 | |
| 102 | func (t *DocsGetTool) Prompt() string { |
| 103 | return "Use this tool to fetch documentation files (Markdown or text) from the configured docs directory." |