1package prompt
2
3import (
4 "os"
5 "path/filepath"
6 "strings"
7 "sync"
8)
9
10type PromptID string
11
12const (
13 PromptCoder PromptID = "coder"
14 PromptTitle PromptID = "title"
15 PromptTask PromptID = "task"
16 PromptSummarizer PromptID = "summarizer"
17 PromptDefault PromptID = "default"
18)
19
20func GetPrompt(promptID PromptID, provider string, contextPaths ...string) string {
21 basePrompt := ""
22 switch promptID {
23 case PromptCoder:
24 basePrompt = CoderPrompt(provider)
25 case PromptTitle:
26 basePrompt = TitlePrompt()
27 case PromptTask:
28 basePrompt = TaskPrompt()
29 case PromptSummarizer:
30 basePrompt = SummarizerPrompt()
31 default:
32 basePrompt = "You are a helpful assistant"
33 }
34 return basePrompt
35}
36
37func getContextFromPaths(workingDir string, contextPaths []string) string {
38 return processContextPaths(workingDir, contextPaths)
39}
40
41func processContextPaths(workDir string, paths []string) string {
42 var (
43 wg sync.WaitGroup
44 resultCh = make(chan string)
45 )
46
47 // Track processed files to avoid duplicates
48 processedFiles := make(map[string]bool)
49 var processedMutex sync.Mutex
50
51 for _, path := range paths {
52 wg.Add(1)
53 go func(p string) {
54 defer wg.Done()
55
56 if strings.HasSuffix(p, "/") {
57 filepath.WalkDir(filepath.Join(workDir, p), func(path string, d os.DirEntry, err error) error {
58 if err != nil {
59 return err
60 }
61 if !d.IsDir() {
62 // Check if we've already processed this file (case-insensitive)
63 lowerPath := strings.ToLower(path)
64
65 processedMutex.Lock()
66 alreadyProcessed := processedFiles[lowerPath]
67 if !alreadyProcessed {
68 processedFiles[lowerPath] = true
69 }
70 processedMutex.Unlock()
71
72 if !alreadyProcessed {
73 if result := processFile(path); result != "" {
74 resultCh <- result
75 }
76 }
77 }
78 return nil
79 })
80 } else {
81 fullPath := filepath.Join(workDir, p)
82
83 // Check if we've already processed this file (case-insensitive)
84 lowerPath := strings.ToLower(fullPath)
85
86 processedMutex.Lock()
87 alreadyProcessed := processedFiles[lowerPath]
88 if !alreadyProcessed {
89 processedFiles[lowerPath] = true
90 }
91 processedMutex.Unlock()
92
93 if !alreadyProcessed {
94 result := processFile(fullPath)
95 if result != "" {
96 resultCh <- result
97 }
98 }
99 }
100 }(path)
101 }
102
103 go func() {
104 wg.Wait()
105 close(resultCh)
106 }()
107
108 results := make([]string, 0)
109 for result := range resultCh {
110 results = append(results, result)
111 }
112
113 return strings.Join(results, "\n")
114}
115
116func processFile(filePath string) string {
117 content, err := os.ReadFile(filePath)
118 if err != nil {
119 return ""
120 }
121 return "# From:" + filePath + "\n" + string(content)
122}