blob: 2a397413ae0448a0fb456692a4758c5cedf54a85 [file] [log] [blame]
user5a7d60d2025-07-27 21:22:04 +04001package config
2
3import (
4 "fmt"
5 "os"
6 "time"
7
8 "gopkg.in/yaml.v3"
9)
10
11// Config represents the Staff MVP configuration
12type Config struct {
iomodo90de9152025-07-31 13:20:01 +040013 Server ServerConfig `yaml:"server"`
iomodo50598c62025-07-27 22:06:32 +040014 OpenAI OpenAIConfig `yaml:"openai"`
15 GitHub GitHubConfig `yaml:"github"`
iomodo578f5042025-07-28 20:46:02 +040016 Gerrit GerritConfig `yaml:"gerrit"`
user5a7d60d2025-07-27 21:22:04 +040017 Agents []AgentConfig `yaml:"agents"`
18 Tasks TasksConfig `yaml:"tasks"`
19 Git GitConfig `yaml:"git"`
20}
21
iomodo90de9152025-07-31 13:20:01 +040022type ServerConfig struct {
23 ListenAddress string `yaml:"listen_address"`
24}
25
user5a7d60d2025-07-27 21:22:04 +040026// OpenAIConfig represents OpenAI provider configuration
27type OpenAIConfig struct {
28 APIKey string `yaml:"api_key"`
29 Model string `yaml:"model"`
30 BaseURL string `yaml:"base_url"`
31 Timeout time.Duration `yaml:"timeout"`
32 MaxRetries int `yaml:"max_retries"`
33}
34
35// GitHubConfig represents GitHub integration configuration
36type GitHubConfig struct {
iomodob23799d2025-07-31 14:08:22 +040037 Token string `yaml:"token"`
38 Owner string `yaml:"owner"`
39 Repo string `yaml:"repo"`
40 WebhookSecret string `yaml:"webhook_secret"` // Secret for webhook signature validation
user5a7d60d2025-07-27 21:22:04 +040041}
42
iomodo578f5042025-07-28 20:46:02 +040043// GerritConfig represents Gerrit integration configuration
44type GerritConfig struct {
45 Username string `yaml:"username"`
46 Password string `yaml:"password"` // HTTP password or API token
47 BaseURL string `yaml:"base_url"` // Gerrit server URL (e.g. https://gerrit.example.com)
48 Project string `yaml:"project"` // Gerrit project name
49}
50
user5a7d60d2025-07-27 21:22:04 +040051// AgentConfig represents individual agent configuration
52type AgentConfig struct {
53 Name string `yaml:"name"`
54 Role string `yaml:"role"`
55 Model string `yaml:"model"`
56 SystemPromptFile string `yaml:"system_prompt_file"`
iomodo50598c62025-07-27 22:06:32 +040057 Capabilities []string `yaml:"capabilities"` // For auto-assignment
58 TaskTypes []string `yaml:"task_types"` // Types of tasks this agent handles
59 MaxTokens *int `yaml:"max_tokens"` // Model-specific token limits
60 Temperature *float64 `yaml:"temperature"` // Model creativity setting
user5a7d60d2025-07-27 21:22:04 +040061}
62
63// TasksConfig represents task management configuration
64type TasksConfig struct {
65 StoragePath string `yaml:"storage_path"`
66 CompletedPath string `yaml:"completed_path"`
67}
68
69// GitConfig represents Git operation configuration
70type GitConfig struct {
iomodoa53240a2025-07-30 17:33:35 +040071 RepoPath string `yaml:"repo_path"`
iomodoe7477eb2025-07-30 20:26:30 +040072 WorkspacePath string `yaml:"workspace_path"`
user5a7d60d2025-07-27 21:22:04 +040073 CommitMessageTemplate string `yaml:"commit_message_template"`
74 PRTemplate string `yaml:"pr_template"`
75}
76
77// LoadConfig loads configuration from a YAML file
78func LoadConfig(configPath string) (*Config, error) {
79 // Read the config file
80 data, err := os.ReadFile(configPath)
81 if err != nil {
82 return nil, fmt.Errorf("failed to read config file: %w", err)
83 }
84
85 // Parse YAML
86 var config Config
87 if err := yaml.Unmarshal(data, &config); err != nil {
88 return nil, fmt.Errorf("failed to parse config YAML: %w", err)
89 }
90
91 // Apply defaults
92 config = applyDefaults(config)
93
94 // Validate configuration
95 if err := validateConfig(config); err != nil {
96 return nil, fmt.Errorf("invalid configuration: %w", err)
97 }
98
99 return &config, nil
100}
101
102// LoadConfigWithEnvOverrides loads config with environment variable overrides
103func LoadConfigWithEnvOverrides(configPath string) (*Config, error) {
104 config, err := LoadConfig(configPath)
105 if err != nil {
106 return nil, err
107 }
108
109 // Override with environment variables if present
110 if apiKey := os.Getenv("OPENAI_API_KEY"); apiKey != "" {
111 config.OpenAI.APIKey = apiKey
112 }
113 if githubToken := os.Getenv("GITHUB_TOKEN"); githubToken != "" {
114 config.GitHub.Token = githubToken
115 }
116 if owner := os.Getenv("GITHUB_OWNER"); owner != "" {
117 config.GitHub.Owner = owner
118 }
119 if repo := os.Getenv("GITHUB_REPO"); repo != "" {
120 config.GitHub.Repo = repo
121 }
iomodob23799d2025-07-31 14:08:22 +0400122 if webhookSecret := os.Getenv("GITHUB_WEBHOOK_SECRET"); webhookSecret != "" {
123 config.GitHub.WebhookSecret = webhookSecret
124 }
iomodo578f5042025-07-28 20:46:02 +0400125 if gerritUsername := os.Getenv("GERRIT_USERNAME"); gerritUsername != "" {
126 config.Gerrit.Username = gerritUsername
127 }
128 if gerritPassword := os.Getenv("GERRIT_PASSWORD"); gerritPassword != "" {
129 config.Gerrit.Password = gerritPassword
130 }
131 if gerritBaseURL := os.Getenv("GERRIT_BASE_URL"); gerritBaseURL != "" {
132 config.Gerrit.BaseURL = gerritBaseURL
133 }
134 if gerritProject := os.Getenv("GERRIT_PROJECT"); gerritProject != "" {
135 config.Gerrit.Project = gerritProject
136 }
user5a7d60d2025-07-27 21:22:04 +0400137
iomodo90de9152025-07-31 13:20:01 +0400138 if listenAddr := os.Getenv("LISTEN_ADDRESS"); listenAddr != "" {
139 config.Server.ListenAddress = listenAddr
140 }
141
user5a7d60d2025-07-27 21:22:04 +0400142 // Re-validate after env overrides
143 if err := validateConfig(*config); err != nil {
144 return nil, fmt.Errorf("invalid configuration after env overrides: %w", err)
145 }
146
147 return config, nil
148}
149
150// applyDefaults applies default values to configuration
151func applyDefaults(config Config) Config {
152 // OpenAI defaults
153 if config.OpenAI.Model == "" {
154 config.OpenAI.Model = "gpt-4"
155 }
156 if config.OpenAI.BaseURL == "" {
157 config.OpenAI.BaseURL = "https://api.openai.com/v1"
158 }
159 if config.OpenAI.Timeout == 0 {
160 config.OpenAI.Timeout = 30 * time.Second
161 }
162 if config.OpenAI.MaxRetries == 0 {
163 config.OpenAI.MaxRetries = 3
164 }
165
166 // Tasks defaults
167 if config.Tasks.StoragePath == "" {
iomodo50598c62025-07-27 22:06:32 +0400168 config.Tasks.StoragePath = "../operations/"
user5a7d60d2025-07-27 21:22:04 +0400169 }
170 if config.Tasks.CompletedPath == "" {
iomodo50598c62025-07-27 22:06:32 +0400171 config.Tasks.CompletedPath = "../operations/completed/"
user5a7d60d2025-07-27 21:22:04 +0400172 }
173
174 // Git defaults
user5a7d60d2025-07-27 21:22:04 +0400175 if config.Git.CommitMessageTemplate == "" {
176 config.Git.CommitMessageTemplate = "Task {task_id}: {task_title}\n\n{solution}\n\nGenerated by Staff AI Agent: {agent_name}"
177 }
178 if config.Git.PRTemplate == "" {
179 config.Git.PRTemplate = `## Task: {task_title}
180
181**Task ID:** {task_id}
182**Agent:** {agent_name}
183**Priority:** {priority}
184
185### Description
186{task_description}
187
188### Solution
189{solution}
190
191### Files Changed
192{files_changed}
193
194---
195*Generated by Staff AI Multi-Agent System*`
196 }
197
198 // Agent defaults
199 for i := range config.Agents {
200 if config.Agents[i].Model == "" {
201 config.Agents[i].Model = config.OpenAI.Model
202 }
203 }
204
205 return config
206}
207
208// validateConfig validates the configuration
209func validateConfig(config Config) error {
210 // Validate OpenAI config
211 if config.OpenAI.APIKey == "" {
212 return fmt.Errorf("openai.api_key is required")
213 }
214 if config.OpenAI.Model == "" {
215 return fmt.Errorf("openai.model is required")
216 }
217
iomodo578f5042025-07-28 20:46:02 +0400218 // Validate that at least one Git provider is configured
219 hasGitHub := config.GitHub.Token != "" && config.GitHub.Owner != "" && config.GitHub.Repo != ""
220 hasGerrit := config.Gerrit.Username != "" && config.Gerrit.Password != "" && config.Gerrit.BaseURL != "" && config.Gerrit.Project != ""
iomodoa53240a2025-07-30 17:33:35 +0400221
iomodo578f5042025-07-28 20:46:02 +0400222 if !hasGitHub && !hasGerrit {
223 return fmt.Errorf("either GitHub or Gerrit configuration is required")
user5a7d60d2025-07-27 21:22:04 +0400224 }
iomodoa53240a2025-07-30 17:33:35 +0400225
iomodo578f5042025-07-28 20:46:02 +0400226 // Validate GitHub config if provided
227 if config.GitHub.Token != "" {
228 if config.GitHub.Owner == "" {
229 return fmt.Errorf("github.owner is required when github.token is provided")
230 }
231 if config.GitHub.Repo == "" {
232 return fmt.Errorf("github.repo is required when github.token is provided")
233 }
user5a7d60d2025-07-27 21:22:04 +0400234 }
iomodoa53240a2025-07-30 17:33:35 +0400235
iomodo578f5042025-07-28 20:46:02 +0400236 // Validate Gerrit config if provided
237 if config.Gerrit.Username != "" {
238 if config.Gerrit.Password == "" {
239 return fmt.Errorf("gerrit.password is required when gerrit.username is provided")
240 }
241 if config.Gerrit.BaseURL == "" {
242 return fmt.Errorf("gerrit.base_url is required when gerrit.username is provided")
243 }
244 if config.Gerrit.Project == "" {
245 return fmt.Errorf("gerrit.project is required when gerrit.username is provided")
246 }
user5a7d60d2025-07-27 21:22:04 +0400247 }
248
249 // Validate agents
250 if len(config.Agents) == 0 {
251 return fmt.Errorf("at least one agent must be configured")
252 }
253
254 for i, agent := range config.Agents {
255 if agent.Name == "" {
256 return fmt.Errorf("agent[%d].name is required", i)
257 }
258 if agent.Role == "" {
259 return fmt.Errorf("agent[%d].role is required", i)
260 }
261 if agent.SystemPromptFile == "" {
262 return fmt.Errorf("agent[%d].system_prompt_file is required", i)
263 }
264 }
265
266 return nil
267}
268
269// GetAgentByName returns an agent config by name
270func (c *Config) GetAgentByName(name string) (*AgentConfig, error) {
271 for _, agent := range c.Agents {
272 if agent.Name == name {
273 return &agent, nil
274 }
275 }
276 return nil, fmt.Errorf("agent not found: %s", name)
277}
278
279// ListAgentNames returns a list of all configured agent names
280func (c *Config) ListAgentNames() []string {
281 names := make([]string, len(c.Agents))
282 for i, agent := range c.Agents {
283 names[i] = agent.Name
284 }
285 return names
iomodo50598c62025-07-27 22:06:32 +0400286}
iomodo578f5042025-07-28 20:46:02 +0400287
288// HasGitHubConfig returns true if GitHub is properly configured
289func (c *Config) HasGitHubConfig() bool {
290 return c.GitHub.Token != "" && c.GitHub.Owner != "" && c.GitHub.Repo != ""
291}
292
293// HasGerritConfig returns true if Gerrit is properly configured
294func (c *Config) HasGerritConfig() bool {
295 return c.Gerrit.Username != "" && c.Gerrit.Password != "" && c.Gerrit.BaseURL != "" && c.Gerrit.Project != ""
296}
297
298// GetPrimaryGitProvider returns the primary git provider type ("github" or "gerrit")
299// If both are configured, GitHub takes precedence for backward compatibility
300func (c *Config) GetPrimaryGitProvider() string {
301 if c.HasGitHubConfig() {
302 return "github"
303 }
304 if c.HasGerritConfig() {
305 return "gerrit"
306 }
307 return ""
308}