|
| 1 | +package action |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +// projectFile defines a well-known project file and its significance. |
| 11 | +type projectFile struct { |
| 12 | + name string |
| 13 | + desc string |
| 14 | +} |
| 15 | + |
| 16 | +var keyProjectFiles = []projectFile{ |
| 17 | + {"go.mod", "Go module"}, |
| 18 | + {"package.json", "Node.js project"}, |
| 19 | + {"Cargo.toml", "Rust project"}, |
| 20 | + {"pyproject.toml", "Python project"}, |
| 21 | + {"requirements.txt", "Python dependencies"}, |
| 22 | + {"Makefile", "Makefile"}, |
| 23 | + {"Dockerfile", "Docker"}, |
| 24 | + {"docker-compose.yml", "Docker Compose"}, |
| 25 | + {"README.md", "README"}, |
| 26 | + {"CLAUDE.md", "AI instructions"}, |
| 27 | + {".gitignore", "Git ignore rules"}, |
| 28 | +} |
| 29 | + |
| 30 | +// BuildProjectContext generates a snapshot of the project structure and key |
| 31 | +// files for inclusion in the system prompt. This gives providers immediate |
| 32 | +// context without needing a tool round to explore. |
| 33 | +func BuildProjectContext(workDir string) string { |
| 34 | + var b strings.Builder |
| 35 | + |
| 36 | + b.WriteString("## Project Context\n\n") |
| 37 | + |
| 38 | + // Detect project type from key files. |
| 39 | + var detected []string |
| 40 | + for _, kf := range keyProjectFiles { |
| 41 | + if _, err := os.Stat(filepath.Join(workDir, kf.name)); err == nil { |
| 42 | + detected = append(detected, fmt.Sprintf("%s (%s)", kf.name, kf.desc)) |
| 43 | + } |
| 44 | + } |
| 45 | + if len(detected) > 0 { |
| 46 | + b.WriteString("**Project type:** ") |
| 47 | + b.WriteString(strings.Join(detected, ", ")) |
| 48 | + b.WriteString("\n\n") |
| 49 | + } |
| 50 | + |
| 51 | + // Top-level directory listing. |
| 52 | + entries, err := os.ReadDir(workDir) |
| 53 | + if err != nil { |
| 54 | + return b.String() |
| 55 | + } |
| 56 | + |
| 57 | + b.WriteString("**Project root (`./`):**\n```\n") |
| 58 | + for _, entry := range entries { |
| 59 | + name := entry.Name() |
| 60 | + // Skip hidden files except key ones. |
| 61 | + if strings.HasPrefix(name, ".") { |
| 62 | + switch name { |
| 63 | + case ".gitignore", ".github", ".env.example": |
| 64 | + // keep |
| 65 | + default: |
| 66 | + continue |
| 67 | + } |
| 68 | + } |
| 69 | + if entry.IsDir() { |
| 70 | + name += "/" |
| 71 | + } |
| 72 | + b.WriteString(name + "\n") |
| 73 | + } |
| 74 | + b.WriteString("```\n") |
| 75 | + |
| 76 | + return b.String() |
| 77 | +} |
| 78 | + |
| 79 | +// ToolUsageHints returns guidance text for providers on how to use the |
| 80 | +// available read-only tools effectively during fan-out. |
| 81 | +func ToolUsageHints() string { |
| 82 | + return `## Available Tools |
| 83 | +
|
| 84 | +You have the following read-only tools for exploring this codebase: |
| 85 | +
|
| 86 | +- **file_read** — Read a file's contents. Pass a directory path to get its listing. Use "." for the project root. |
| 87 | +- **list_directory** — List directory contents. Set recursive=true to see up to 3 levels deep. Use "." for the project root. |
| 88 | +- **grep_search** — Search for text/regex patterns across files. Supports file type filtering with the "include" parameter (e.g., "*.go"). |
| 89 | +
|
| 90 | +**Tips for efficient exploration:** |
| 91 | +1. Start by reading key files like README.md, go.mod, or the main entry point. |
| 92 | +2. Use grep_search to find specific functions, types, or patterns. |
| 93 | +3. Use list_directory with recursive=true on specific subdirectories, not the whole project. |
| 94 | +4. Do NOT attempt to use shell_exec, file_write, or any tools not listed above — they are not available to you.` |
| 95 | +} |
0 commit comments