|
| 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 | +} |
0 commit comments