62159
Sy
symb / files
symb / internal / llm / prompt.go
4.6 KB
1// Package llm implements the LLM interaction loop with tool calling support. 2package llm 3 4import ( 5 _ "embed" 6 "fmt" 7 "os" 8 "path/filepath" 9 "strings" 10 11 "github.com/xonecas/symb/internal/treesitter" 12) 13 14// Embedded prompt files — shared base + model-specific overrides. 15// 16//go:embed prompts/base.md 17var basePrompt string 18 19//go:embed prompts/subagent_base.md 20var subagentBasePrompt string 21 22//go:embed prompts/subagent.md 23var subagentPrompt string 24 25//go:embed prompts/subagent_explore.md 26var subagentExplorePrompt string 27 28//go:embed prompts/subagent_editor.md 29var subagentEditorPrompt string 30 31//go:embed prompts/subagent_reviewer.md 32var subagentReviewerPrompt string 33 34//go:embed prompts/subagent_web.md 35var subagentWebPrompt string 36 37//go:embed prompts/anthropic.md 38var anthropicPrompt string 39 40//go:embed prompts/gemini.md 41var geminiPrompt string 42 43//go:embed prompts/qwen.md 44var qwenPrompt string 45 46//go:embed prompts/gpt.md 47var gptPrompt string 48 49// selectModelPrompt returns the model-specific override for the given model. 50func selectModelPrompt(modelID string) string { 51 modelLower := strings.ToLower(modelID) 52 53 if strings.Contains(modelLower, "claude") { 54 return anthropicPrompt 55 } 56 if strings.Contains(modelLower, "gemini") { 57 return geminiPrompt 58 } 59 if strings.Contains(modelLower, "gpt") || strings.Contains(modelLower, "o1") { 60 return gptPrompt 61 } 62 if strings.Contains(modelLower, "qwen") { 63 return qwenPrompt 64 } 65 66 // Default fallback — no model-specific additions 67 return anthropicPrompt 68} 69 70// LoadAgentInstructions searches for AGENTS.md files in the directory hierarchy 71// and returns their concatenated contents. Searches from current working directory 72// up to the root, then checks user's config directory. 73func LoadAgentInstructions() string { 74 var instructions []string 75 76 // Get current working directory 77 cwd, err := os.Getwd() 78 if err != nil { 79 return "" 80 } 81 82 // Search up the directory tree from CWD 83 dir := cwd 84 for { 85 agentsPath := filepath.Join(dir, "AGENTS.md") 86 if content := readFileIfExists(agentsPath); content != "" { 87 header := fmt.Sprintf("Instructions from: %s", agentsPath) 88 instructions = append(instructions, header+"\n"+content) 89 } 90 91 // Check if we've reached the root 92 parent := filepath.Dir(dir) 93 if parent == dir { 94 break 95 } 96 dir = parent 97 } 98 99 // Check user's config directory (~/.config/symb/AGENTS.md) 100 home, err := os.UserHomeDir() 101 if err == nil { 102 configAgents := filepath.Join(home, ".config", "symb", "AGENTS.md") 103 if content := readFileIfExists(configAgents); content != "" { 104 header := fmt.Sprintf("Instructions from: %s", configAgents) 105 instructions = append(instructions, header+"\n"+content) 106 } 107 } 108 109 // Reverse order so project-level takes precedence over user-level 110 // (prepended to prompt, so last in list appears first) 111 for i := 0; i < len(instructions)/2; i++ { 112 j := len(instructions) - 1 - i 113 instructions[i], instructions[j] = instructions[j], instructions[i] 114 } 115 116 return strings.Join(instructions, "\n\n") 117} 118 119// BasePrompt returns the shared base prompt content. 120func BasePrompt() string { 121 return basePrompt 122} 123 124// SubAgentBasePrompt returns the base prompt content for sub-agents. 125func SubAgentBasePrompt() string { 126 return subagentBasePrompt 127} 128 129// SubAgentPrompt returns the base sub-agent prompt content. 130func SubAgentPrompt() string { 131 return subagentPrompt 132} 133 134// SubAgentTypePrompt returns the sub-agent type prompt content. 135func SubAgentTypePrompt(agentType string) string { 136 switch agentType { 137 case "explore": 138 return subagentExplorePrompt 139 case "editor": 140 return subagentEditorPrompt 141 case "reviewer": 142 return subagentReviewerPrompt 143 case "web": 144 return subagentWebPrompt 145 default: 146 return subagentPrompt 147 } 148} 149 150// BuildSystemPrompt constructs the complete system prompt: 151// 1. Base prompt (shared across all models) 152// 2. Model-specific overrides 153// 3. AGENTS.md instructions 154// 4. Tree-sitter project outline 155func BuildSystemPrompt(modelID string, idx *treesitter.Index) string { 156 parts := []string{basePrompt} 157 158 if modelOverride := selectModelPrompt(modelID); modelOverride != "" { 159 parts = append(parts, modelOverride) 160 } 161 162 if agentInstructions := LoadAgentInstructions(); agentInstructions != "" { 163 parts = append(parts, agentInstructions) 164 } 165 166 if idx != nil { 167 if outline := treesitter.FormatOutline(idx.Snapshot()); outline != "" { 168 parts = append(parts, outline) 169 } 170 } 171 172 return strings.Join(parts, "\n\n---\n\n") 173} 174 175// readFileIfExists reads a file if it exists, returns empty string otherwise. 176func readFileIfExists(path string) string { 177 data, err := os.ReadFile(path) 178 if err != nil { 179 return "" 180 } 181 return strings.TrimSpace(string(data)) 182}