(ctx context.Context, input map[string]any, tc *tools.ToolContext)
| 336 | } |
| 337 | |
| 338 | func (t *GrepTool) Execute(ctx context.Context, input map[string]any, tc *tools.ToolContext) (any, error) { |
| 339 | pattern, _ := input["pattern"].(string) |
| 340 | path := "." |
| 341 | if p, ok := input["path"].(string); ok { |
| 342 | path = p |
| 343 | } |
| 344 | glob := "" |
| 345 | if g, ok := input["glob"].(string); ok { |
| 346 | glob = g |
| 347 | } |
| 348 | |
| 349 | // 路径安全验证 |
| 350 | if t.middleware != nil { |
| 351 | validatedPath, err := t.middleware.validatePath(path) |
| 352 | if err != nil { |
| 353 | return map[string]any{ |
| 354 | "ok": false, |
| 355 | "error": fmt.Sprintf("path validation failed: %v", err), |
| 356 | }, nil |
| 357 | } |
| 358 | path = validatedPath |
| 359 | } |
| 360 | |
| 361 | matches, err := t.backend.GrepRaw(ctx, pattern, path, glob) |
| 362 | if err != nil { |
| 363 | return map[string]any{ |
| 364 | "ok": false, |
| 365 | "error": fmt.Sprintf("failed to grep: %v", err), |
| 366 | }, nil |
| 367 | } |
| 368 | |
| 369 | results := make([]map[string]any, 0, len(matches)) |
| 370 | for _, match := range matches { |
| 371 | results = append(results, map[string]any{ |
| 372 | "path": match.Path, |
| 373 | "line_number": match.LineNumber, |
| 374 | "line": match.Line, |
| 375 | "match": match.Match, |
| 376 | }) |
| 377 | } |
| 378 | |
| 379 | return map[string]any{ |
| 380 | "ok": true, |
| 381 | "pattern": pattern, |
| 382 | "matches": results, |
| 383 | "count": len(results), |
| 384 | }, nil |
| 385 | } |
| 386 | |
| 387 | func (t *GrepTool) Prompt() string { |
| 388 | return `Use this tool to search for patterns across files. |
nothing calls this directly
no test coverage detected