summaryrefslogtreecommitdiff
path: root/internal/showcase/ai_context.go
blob: 1c812f8d28c0127cc98e6b6320058c938a4bdbe9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package showcase

import (
	"fmt"
	"io/fs"
	"os"
	"path/filepath"
	"sort"
	"strings"
)

// buildAIInputContext prepares a textual context for AI tools when no README exists.
// It returns a string to be piped to the AI tool's stdin and a boolean indicating
// whether this was sourced from an actual README (true) or synthesized (false).
func buildAIInputContext(repoPath string) (string, bool) {
	// 1) Try to load a README first
	readmeFiles := []string{
		"README.md", "readme.md", "Readme.md",
		"README.MD", "README.txt", "readme.txt",
		"README", "readme",
	}
	for _, f := range readmeFiles {
		p := filepath.Join(repoPath, f)
		if b, err := os.ReadFile(p); err == nil {
			return string(b), true
		}
	}

	// 2) No README: synthesize compact context
	var sb strings.Builder

	// File tree (depth-limited)
	sb.WriteString("[CONTEXT]\n")
	sb.WriteString("Repository does not contain a README.\n")
	sb.WriteString("The following is a compact file tree and key manifests/snippets.\n\n")

	sb.WriteString("FILE TREE (depth 2):\n")
	tree := listFileTree(repoPath, 2, 200)
	for _, line := range tree {
		sb.WriteString("- ")
		sb.WriteString(line)
		sb.WriteString("\n")
	}
	sb.WriteString("\n")

	// Key manifests we often care about
	manifests := []string{
		"go.mod", "go.sum", "package.json", "Cargo.toml", "Cargo.lock",
		"pyproject.toml", "requirements.txt", "Makefile", "Dockerfile",
		"build.gradle", "pom.xml", "composer.json",
	}
	wroteHeader := false
	for _, m := range manifests {
		p := filepath.Join(repoPath, m)
		if b, err := os.ReadFile(p); err == nil {
			if !wroteHeader {
				sb.WriteString("KEY MANIFESTS:\n")
				wroteHeader = true
			}
			sb.WriteString(fmt.Sprintf("--- %s ---\n", m))
			sb.WriteString(trimTo(string(b), 2000))
			sb.WriteString("\n\n")
		}
	}

	// Source hints: capture first main-ish entry file snippets
	// Priority: Go main, Rust main, Node entry, Python main
	candidates := []string{
		"cmd", // Go convention
		"main.go",
		"cmd/main.go",
		"src/main.rs",
		"index.js",
		"src/index.js",
		"main.py",
		"src/main.py",
	}
	wroteSrc := false
	for _, c := range candidates {
		p := filepath.Join(repoPath, c)
		info, err := os.Stat(p)
		if err != nil {
			continue
		}
		if info.IsDir() {
			// collect a few go files under cmd/*/main.go
			if c == "cmd" {
				_ = filepath.WalkDir(p, func(path string, d fs.DirEntry, err error) error {
					if err != nil {
						return nil
					}
					if d.IsDir() {
						return nil
					}
					base := filepath.Base(path)
					if base == "main.go" {
						if b, e := os.ReadFile(path); e == nil {
							if !wroteSrc {
								sb.WriteString("PRIMARY SOURCE SNIPPETS:\n")
								wroteSrc = true
							}
							rel, _ := filepath.Rel(repoPath, path)
							sb.WriteString(fmt.Sprintf("--- %s ---\n", rel))
							sb.WriteString(trimTo(string(b), 2000))
							sb.WriteString("\n\n")
						}
					}
					return nil
				})
			}
			continue
		}
		if b, e := os.ReadFile(p); e == nil {
			if !wroteSrc {
				sb.WriteString("PRIMARY SOURCE SNIPPETS:\n")
				wroteSrc = true
			}
			rel, _ := filepath.Rel(repoPath, p)
			sb.WriteString(fmt.Sprintf("--- %s ---\n", rel))
			sb.WriteString(trimTo(string(b), 2000))
			sb.WriteString("\n\n")
		}
	}

	// Fallback: include a few top-level .go, .rs, .py, .js files if we still have nothing
	if !wroteSrc {
		topFiles := listTopFiles(repoPath, []string{".go", ".rs", ".py", ".js", ".ts", ".tsx"}, 5)
		for _, f := range topFiles {
			if b, e := os.ReadFile(filepath.Join(repoPath, f)); e == nil {
				if !wroteSrc {
					sb.WriteString("PRIMARY SOURCE SNIPPETS:\n")
					wroteSrc = true
				}
				sb.WriteString(fmt.Sprintf("--- %s ---\n", f))
				sb.WriteString(trimTo(string(b), 2000))
				sb.WriteString("\n\n")
			}
		}
	}

	// Instruction to the model
	sb.WriteString("[TASK]\n")
	sb.WriteString("Summarize this project in 1–2 paragraphs: what it does, why it's useful, and how it's implemented. Mention notable tech choices. Be concise and informative.\n")

	return sb.String(), false
}

// listFileTree returns a sorted list of relative paths up to a given depth and limit.
func listFileTree(root string, maxDepth int, maxEntries int) []string {
	var entries []string
	var count int
	_ = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
		if err != nil {
			return nil
		}
		if path == root {
			return nil
		}
		rel, e := filepath.Rel(root, path)
		if e != nil {
			return nil
		}
		// depth check
		depth := 1 + strings.Count(rel, string(os.PathSeparator))
		if depth > maxDepth {
			return fs.SkipDir
		}
		entries = append(entries, rel)
		count++
		if count >= maxEntries {
			return fs.SkipDir
		}
		return nil
	})
	sort.Strings(entries)
	if len(entries) > maxEntries {
		entries = entries[:maxEntries]
	}
	return entries
}

// listTopFiles lists top-level files with certain extensions up to a limit.
func listTopFiles(root string, exts []string, limit int) []string {
	dir, err := os.ReadDir(root)
	if err != nil {
		return nil
	}
	var out []string
	for _, e := range dir {
		if e.IsDir() {
			continue
		}
		name := e.Name()
		for _, x := range exts {
			if strings.HasSuffix(strings.ToLower(name), strings.ToLower(x)) {
				out = append(out, name)
				break
			}
		}
		if len(out) >= limit {
			break
		}
	}
	sort.Strings(out)
	return out
}

// trimTo soft-limits content length for inclusion in AI context.
func trimTo(s string, max int) string {
	if len(s) <= max {
		return s
	}
	return s[:max] + "\n... [truncated]"
}