Skip to content

Commit 6efaff9

Browse files
committed
feat: add support for customizable prompt configuration
1 parent 6b1bcfe commit 6efaff9

File tree

3 files changed

+114
-3
lines changed

3 files changed

+114
-3
lines changed

internal/config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ func InitConfig() {
6262
fmt.Println("Error unmarshalling config:", err)
6363
os.Exit(1)
6464
}
65+
66+
// Initialize prompt configuration
67+
InitPromptConfig()
6568
}
6669

6770
func GetProvider() string {

internal/config/prompts.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
8+
"gopkg.in/yaml.v3"
9+
)
10+
11+
type PromptConfig struct {
12+
SystemMessage string `yaml:"system_message"`
13+
CommitMessageTemplate string `yaml:"commit_message_template"`
14+
}
15+
16+
var promptsCfg *PromptConfig
17+
18+
// InitPromptConfig initializes the prompt configuration
19+
func InitPromptConfig() {
20+
if promptsCfg != nil {
21+
return
22+
}
23+
24+
promptsFile := filepath.Join(getConfigDir(), ".lazycommit.prompts.yaml")
25+
26+
// Check if prompts file exists
27+
if _, err := os.Stat(promptsFile); os.IsNotExist(err) {
28+
// Create default prompts file
29+
defaultConfig := getDefaultPromptConfig()
30+
if err := savePromptConfig(promptsFile, defaultConfig); err != nil {
31+
fmt.Printf("Error creating default prompts file: %v\n", err)
32+
fmt.Printf("Using default prompts\n")
33+
} else {
34+
fmt.Printf("Created default prompts config at %s\n", promptsFile)
35+
}
36+
promptsCfg = defaultConfig
37+
return
38+
}
39+
40+
// Load existing prompts file
41+
data, err := os.ReadFile(promptsFile)
42+
if err != nil {
43+
fmt.Printf("Error reading prompts file: %v\n", err)
44+
fmt.Printf("Using default prompts\n")
45+
promptsCfg = getDefaultPromptConfig()
46+
return
47+
}
48+
49+
var config PromptConfig
50+
if err := yaml.Unmarshal(data, &config); err != nil {
51+
fmt.Printf("Error parsing prompts file: %v\n", err)
52+
fmt.Printf("Using default prompts\n")
53+
promptsCfg = getDefaultPromptConfig()
54+
return
55+
}
56+
57+
promptsCfg = &config
58+
}
59+
60+
// getDefaultPromptConfig returns the default prompt configuration
61+
func getDefaultPromptConfig() *PromptConfig {
62+
return &PromptConfig{
63+
SystemMessage: "You are a helpful assistant that generates git commit messages.",
64+
CommitMessageTemplate: "Based on the following git diff, generate 10 conventional commit messages. Each message should be on a new line, without any numbering or bullet points:\n\n%s",
65+
}
66+
}
67+
68+
// savePromptConfig saves the prompt configuration to a file
69+
func savePromptConfig(filename string, config *PromptConfig) error {
70+
data, err := yaml.Marshal(config)
71+
if err != nil {
72+
return fmt.Errorf("error marshalling prompt config: %w", err)
73+
}
74+
75+
if err := os.WriteFile(filename, data, 0o644); err != nil {
76+
return fmt.Errorf("error writing prompt config file: %w", err)
77+
}
78+
79+
return nil
80+
}
81+
82+
// GetPromptConfig returns the current prompt configuration
83+
func GetPromptConfig() *PromptConfig {
84+
if promptsCfg == nil {
85+
InitPromptConfig()
86+
}
87+
return promptsCfg
88+
}
89+
90+
// GetSystemMessageFromConfig returns the system message from configuration
91+
func GetSystemMessageFromConfig() string {
92+
config := GetPromptConfig()
93+
if config.SystemMessage != "" {
94+
return config.SystemMessage
95+
}
96+
// Fallback to hardcoded default
97+
return "You are a helpful assistant that generates git commit messages."
98+
}
99+
100+
// GetCommitMessagePromptFromConfig returns the commit message prompt from configuration
101+
func GetCommitMessagePromptFromConfig(diff string) string {
102+
config := GetPromptConfig()
103+
if config.CommitMessageTemplate != "" {
104+
return fmt.Sprintf(config.CommitMessageTemplate, diff)
105+
}
106+
// Fallback to hardcoded default
107+
return fmt.Sprintf("Based on the following git diff, generate 10 conventional commit messages. Each message should be on a new line, without any numbering or bullet points:\n\n%s", diff)
108+
}

internal/provider/prompts.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package provider
22

3-
import "fmt"
3+
import "github.com/m7medvision/lazycommit/internal/config"
44

55
// GetCommitMessagePrompt returns the standardized prompt for generating commit messages
66
func GetCommitMessagePrompt(diff string) string {
7-
return fmt.Sprintf("Based on the following git diff, generate 10 conventional commit messages. Each message should be on a new line, without any numbering or bullet points:\n\n%s", diff)
7+
return config.GetCommitMessagePromptFromConfig(diff)
88
}
99

1010
// GetSystemMessage returns the standardized system message for commit message generation
1111
func GetSystemMessage() string {
12-
return "You are a helpful assistant that generates git commit messages."
12+
return config.GetSystemMessageFromConfig()
1313
}

0 commit comments

Comments
 (0)