blob: 1a39c6f0328a568ce8e6c64b90c1cce557b24de7 [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 {
37 Token string `yaml:"token"`
38 Owner string `yaml:"owner"`
39 Repo string `yaml:"repo"`
40}
41
iomodo578f5042025-07-28 20:46:02 +040042// GerritConfig represents Gerrit integration configuration
43type GerritConfig struct {
44 Username string `yaml:"username"`
45 Password string `yaml:"password"` // HTTP password or API token
46 BaseURL string `yaml:"base_url"` // Gerrit server URL (e.g. https://gerrit.example.com)
47 Project string `yaml:"project"` // Gerrit project name
48}
49
user5a7d60d2025-07-27 21:22:04 +040050// AgentConfig represents individual agent configuration
51type AgentConfig struct {
52 Name string `yaml:"name"`
53 Role string `yaml:"role"`
54 Model string `yaml:"model"`
55 SystemPromptFile string `yaml:"system_prompt_file"`
iomodo50598c62025-07-27 22:06:32 +040056 Capabilities []string `yaml:"capabilities"` // For auto-assignment
57 TaskTypes []string `yaml:"task_types"` // Types of tasks this agent handles
58 MaxTokens *int `yaml:"max_tokens"` // Model-specific token limits
59 Temperature *float64 `yaml:"temperature"` // Model creativity setting
user5a7d60d2025-07-27 21:22:04 +040060}
61
62// TasksConfig represents task management configuration
63type TasksConfig struct {
64 StoragePath string `yaml:"storage_path"`
65 CompletedPath string `yaml:"completed_path"`
66}
67
68// GitConfig represents Git operation configuration
69type GitConfig struct {
iomodoa53240a2025-07-30 17:33:35 +040070 RepoPath string `yaml:"repo_path"`
iomodoe7477eb2025-07-30 20:26:30 +040071 WorkspacePath string `yaml:"workspace_path"`
user5a7d60d2025-07-27 21:22:04 +040072 CommitMessageTemplate string `yaml:"commit_message_template"`
73 PRTemplate string `yaml:"pr_template"`
74}
75
76// LoadConfig loads configuration from a YAML file
77func LoadConfig(configPath string) (*Config, error) {
78 // Read the config file
79 data, err := os.ReadFile(configPath)
80 if err != nil {
81 return nil, fmt.Errorf("failed to read config file: %w", err)
82 }
83
84 // Parse YAML
85 var config Config
86 if err := yaml.Unmarshal(data, &config); err != nil {
87 return nil, fmt.Errorf("failed to parse config YAML: %w", err)
88 }
89
90 // Apply defaults
91 config = applyDefaults(config)
92
93 // Validate configuration
94 if err := validateConfig(config); err != nil {
95 return nil, fmt.Errorf("invalid configuration: %w", err)
96 }
97
98 return &config, nil
99}
100
101// LoadConfigWithEnvOverrides loads config with environment variable overrides
102func LoadConfigWithEnvOverrides(configPath string) (*Config, error) {
103 config, err := LoadConfig(configPath)
104 if err != nil {
105 return nil, err
106 }
107
108 // Override with environment variables if present
109 if apiKey := os.Getenv("OPENAI_API_KEY"); apiKey != "" {
110 config.OpenAI.APIKey = apiKey
111 }
112 if githubToken := os.Getenv("GITHUB_TOKEN"); githubToken != "" {
113 config.GitHub.Token = githubToken
114 }
115 if owner := os.Getenv("GITHUB_OWNER"); owner != "" {
116 config.GitHub.Owner = owner
117 }
118 if repo := os.Getenv("GITHUB_REPO"); repo != "" {
119 config.GitHub.Repo = repo
120 }
iomodo578f5042025-07-28 20:46:02 +0400121 if gerritUsername := os.Getenv("GERRIT_USERNAME"); gerritUsername != "" {
122 config.Gerrit.Username = gerritUsername
123 }
124 if gerritPassword := os.Getenv("GERRIT_PASSWORD"); gerritPassword != "" {
125 config.Gerrit.Password = gerritPassword
126 }
127 if gerritBaseURL := os.Getenv("GERRIT_BASE_URL"); gerritBaseURL != "" {
128 config.Gerrit.BaseURL = gerritBaseURL
129 }
130 if gerritProject := os.Getenv("GERRIT_PROJECT"); gerritProject != "" {
131 config.Gerrit.Project = gerritProject
132 }
user5a7d60d2025-07-27 21:22:04 +0400133
iomodo90de9152025-07-31 13:20:01 +0400134 if listenAddr := os.Getenv("LISTEN_ADDRESS"); listenAddr != "" {
135 config.Server.ListenAddress = listenAddr
136 }
137
user5a7d60d2025-07-27 21:22:04 +0400138 // Re-validate after env overrides
139 if err := validateConfig(*config); err != nil {
140 return nil, fmt.Errorf("invalid configuration after env overrides: %w", err)
141 }
142
143 return config, nil
144}
145
146// applyDefaults applies default values to configuration
147func applyDefaults(config Config) Config {
148 // OpenAI defaults
149 if config.OpenAI.Model == "" {
150 config.OpenAI.Model = "gpt-4"
151 }
152 if config.OpenAI.BaseURL == "" {
153 config.OpenAI.BaseURL = "https://api.openai.com/v1"
154 }
155 if config.OpenAI.Timeout == 0 {
156 config.OpenAI.Timeout = 30 * time.Second
157 }
158 if config.OpenAI.MaxRetries == 0 {
159 config.OpenAI.MaxRetries = 3
160 }
161
162 // Tasks defaults
163 if config.Tasks.StoragePath == "" {
iomodo50598c62025-07-27 22:06:32 +0400164 config.Tasks.StoragePath = "../operations/"
user5a7d60d2025-07-27 21:22:04 +0400165 }
166 if config.Tasks.CompletedPath == "" {
iomodo50598c62025-07-27 22:06:32 +0400167 config.Tasks.CompletedPath = "../operations/completed/"
user5a7d60d2025-07-27 21:22:04 +0400168 }
169
170 // Git defaults
user5a7d60d2025-07-27 21:22:04 +0400171 if config.Git.CommitMessageTemplate == "" {
172 config.Git.CommitMessageTemplate = "Task {task_id}: {task_title}\n\n{solution}\n\nGenerated by Staff AI Agent: {agent_name}"
173 }
174 if config.Git.PRTemplate == "" {
175 config.Git.PRTemplate = `## Task: {task_title}
176
177**Task ID:** {task_id}
178**Agent:** {agent_name}
179**Priority:** {priority}
180
181### Description
182{task_description}
183
184### Solution
185{solution}
186
187### Files Changed
188{files_changed}
189
190---
191*Generated by Staff AI Multi-Agent System*`
192 }
193
194 // Agent defaults
195 for i := range config.Agents {
196 if config.Agents[i].Model == "" {
197 config.Agents[i].Model = config.OpenAI.Model
198 }
199 }
200
201 return config
202}
203
204// validateConfig validates the configuration
205func validateConfig(config Config) error {
206 // Validate OpenAI config
207 if config.OpenAI.APIKey == "" {
208 return fmt.Errorf("openai.api_key is required")
209 }
210 if config.OpenAI.Model == "" {
211 return fmt.Errorf("openai.model is required")
212 }
213
iomodo578f5042025-07-28 20:46:02 +0400214 // Validate that at least one Git provider is configured
215 hasGitHub := config.GitHub.Token != "" && config.GitHub.Owner != "" && config.GitHub.Repo != ""
216 hasGerrit := config.Gerrit.Username != "" && config.Gerrit.Password != "" && config.Gerrit.BaseURL != "" && config.Gerrit.Project != ""
iomodoa53240a2025-07-30 17:33:35 +0400217
iomodo578f5042025-07-28 20:46:02 +0400218 if !hasGitHub && !hasGerrit {
219 return fmt.Errorf("either GitHub or Gerrit configuration is required")
user5a7d60d2025-07-27 21:22:04 +0400220 }
iomodoa53240a2025-07-30 17:33:35 +0400221
iomodo578f5042025-07-28 20:46:02 +0400222 // Validate GitHub config if provided
223 if config.GitHub.Token != "" {
224 if config.GitHub.Owner == "" {
225 return fmt.Errorf("github.owner is required when github.token is provided")
226 }
227 if config.GitHub.Repo == "" {
228 return fmt.Errorf("github.repo is required when github.token is provided")
229 }
user5a7d60d2025-07-27 21:22:04 +0400230 }
iomodoa53240a2025-07-30 17:33:35 +0400231
iomodo578f5042025-07-28 20:46:02 +0400232 // Validate Gerrit config if provided
233 if config.Gerrit.Username != "" {
234 if config.Gerrit.Password == "" {
235 return fmt.Errorf("gerrit.password is required when gerrit.username is provided")
236 }
237 if config.Gerrit.BaseURL == "" {
238 return fmt.Errorf("gerrit.base_url is required when gerrit.username is provided")
239 }
240 if config.Gerrit.Project == "" {
241 return fmt.Errorf("gerrit.project is required when gerrit.username is provided")
242 }
user5a7d60d2025-07-27 21:22:04 +0400243 }
244
245 // Validate agents
246 if len(config.Agents) == 0 {
247 return fmt.Errorf("at least one agent must be configured")
248 }
249
250 for i, agent := range config.Agents {
251 if agent.Name == "" {
252 return fmt.Errorf("agent[%d].name is required", i)
253 }
254 if agent.Role == "" {
255 return fmt.Errorf("agent[%d].role is required", i)
256 }
257 if agent.SystemPromptFile == "" {
258 return fmt.Errorf("agent[%d].system_prompt_file is required", i)
259 }
260 }
261
262 return nil
263}
264
265// GetAgentByName returns an agent config by name
266func (c *Config) GetAgentByName(name string) (*AgentConfig, error) {
267 for _, agent := range c.Agents {
268 if agent.Name == name {
269 return &agent, nil
270 }
271 }
272 return nil, fmt.Errorf("agent not found: %s", name)
273}
274
275// ListAgentNames returns a list of all configured agent names
276func (c *Config) ListAgentNames() []string {
277 names := make([]string, len(c.Agents))
278 for i, agent := range c.Agents {
279 names[i] = agent.Name
280 }
281 return names
iomodo50598c62025-07-27 22:06:32 +0400282}
iomodo578f5042025-07-28 20:46:02 +0400283
284// HasGitHubConfig returns true if GitHub is properly configured
285func (c *Config) HasGitHubConfig() bool {
286 return c.GitHub.Token != "" && c.GitHub.Owner != "" && c.GitHub.Repo != ""
287}
288
289// HasGerritConfig returns true if Gerrit is properly configured
290func (c *Config) HasGerritConfig() bool {
291 return c.Gerrit.Username != "" && c.Gerrit.Password != "" && c.Gerrit.BaseURL != "" && c.Gerrit.Project != ""
292}
293
294// GetPrimaryGitProvider returns the primary git provider type ("github" or "gerrit")
295// If both are configured, GitHub takes precedence for backward compatibility
296func (c *Config) GetPrimaryGitProvider() string {
297 if c.HasGitHubConfig() {
298 return "github"
299 }
300 if c.HasGerritConfig() {
301 return "gerrit"
302 }
303 return ""
304}