coder.go

 1package prompt
 2
 3import (
 4	"context"
 5	_ "embed"
 6	"fmt"
 7	"os"
 8	"path/filepath"
 9	"runtime"
10	"time"
11
12	"github.com/charmbracelet/crush/internal/llm/tools"
13)
14
15func CoderPrompt(cwd string, contextFiles ...string) string {
16	var basePrompt string
17
18	basePrompt = string(baseCoderPrompt)
19	envInfo := getEnvironmentInfo(cwd)
20
21	basePrompt = fmt.Sprintf("%s\n\n%s", basePrompt, envInfo)
22
23	contextContent := getContextFromPaths(cwd, contextFiles)
24	if contextContent != "" {
25		return fmt.Sprintf("%s\n\n# Project-Specific Context\n Make sure to follow the instructions in the context below\n%s", basePrompt, contextContent)
26	}
27	return basePrompt
28}
29
30//go:embed coder.md
31var baseCoderPrompt []byte
32
33func getEnvironmentInfo(cwd string) string {
34	isGit := isGitRepo(cwd)
35	platform := runtime.GOOS
36	date := time.Now().Format("1/2/2006")
37	ls := tools.NewLsTool(cwd)
38	r, _ := ls.Run(context.Background(), tools.ToolCall{
39		Input: `{"path":"."}`,
40	})
41	return fmt.Sprintf(`Here is useful information about the environment you are running in:
42<env>
43Working directory: %s
44Is directory a git repo: %s
45Platform: %s
46Today's date: %s
47</env>
48<project>
49%s
50</project>
51		`, cwd, boolToYesNo(isGit), platform, date, r.Content)
52}
53
54func isGitRepo(dir string) bool {
55	_, err := os.Stat(filepath.Join(dir, ".git"))
56	return err == nil
57}
58
59func LSPInformation() string {
60	return `# LSP Information
61Tools that support it will also include useful diagnostics such as linting and typechecking.
62- These diagnostics will be automatically enabled when you run the tool, and will be displayed in the output at the bottom within the <file_diagnostics></file_diagnostics> and <project_diagnostics></project_diagnostics> tags.
63- Take necessary actions to fix the issues.
64- You should ignore diagnostics of files that you did not change or are not related or caused by your changes unless the user explicitly asks you to fix them.
65`
66}
67
68func boolToYesNo(b bool) string {
69	if b {
70		return "Yes"
71	}
72	return "No"
73}