| user | 5a7d60d | 2025-07-27 21:22:04 +0400 | [diff] [blame] | 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "time" |
| 7 | |
| 8 | "gopkg.in/yaml.v3" |
| 9 | ) |
| 10 | |
| 11 | // Config represents the Staff MVP configuration |
| 12 | type Config struct { |
| iomodo | 90de915 | 2025-07-31 13:20:01 +0400 | [diff] [blame] | 13 | Server ServerConfig `yaml:"server"` |
| iomodo | 50598c6 | 2025-07-27 22:06:32 +0400 | [diff] [blame] | 14 | OpenAI OpenAIConfig `yaml:"openai"` |
| 15 | GitHub GitHubConfig `yaml:"github"` |
| iomodo | 578f504 | 2025-07-28 20:46:02 +0400 | [diff] [blame] | 16 | Gerrit GerritConfig `yaml:"gerrit"` |
| user | 5a7d60d | 2025-07-27 21:22:04 +0400 | [diff] [blame] | 17 | Agents []AgentConfig `yaml:"agents"` |
| 18 | Tasks TasksConfig `yaml:"tasks"` |
| 19 | Git GitConfig `yaml:"git"` |
| 20 | } |
| 21 | |
| iomodo | 90de915 | 2025-07-31 13:20:01 +0400 | [diff] [blame] | 22 | type ServerConfig struct { |
| 23 | ListenAddress string `yaml:"listen_address"` |
| 24 | } |
| 25 | |
| user | 5a7d60d | 2025-07-27 21:22:04 +0400 | [diff] [blame] | 26 | // OpenAIConfig represents OpenAI provider configuration |
| 27 | type 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 |
| 36 | type GitHubConfig struct { |
| iomodo | b23799d | 2025-07-31 14:08:22 +0400 | [diff] [blame] | 37 | 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 |
| user | 5a7d60d | 2025-07-27 21:22:04 +0400 | [diff] [blame] | 41 | } |
| 42 | |
| iomodo | 578f504 | 2025-07-28 20:46:02 +0400 | [diff] [blame] | 43 | // GerritConfig represents Gerrit integration configuration |
| 44 | type 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 | |
| user | 5a7d60d | 2025-07-27 21:22:04 +0400 | [diff] [blame] | 51 | // AgentConfig represents individual agent configuration |
| 52 | type AgentConfig struct { |
| 53 | Name string `yaml:"name"` |
| 54 | Role string `yaml:"role"` |
| 55 | Model string `yaml:"model"` |
| 56 | SystemPromptFile string `yaml:"system_prompt_file"` |
| iomodo | 50598c6 | 2025-07-27 22:06:32 +0400 | [diff] [blame] | 57 | 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 |
| user | 5a7d60d | 2025-07-27 21:22:04 +0400 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | // TasksConfig represents task management configuration |
| 64 | type TasksConfig struct { |
| 65 | StoragePath string `yaml:"storage_path"` |
| 66 | CompletedPath string `yaml:"completed_path"` |
| 67 | } |
| 68 | |
| 69 | // GitConfig represents Git operation configuration |
| 70 | type GitConfig struct { |
| iomodo | a53240a | 2025-07-30 17:33:35 +0400 | [diff] [blame] | 71 | RepoPath string `yaml:"repo_path"` |
| iomodo | e7477eb | 2025-07-30 20:26:30 +0400 | [diff] [blame] | 72 | WorkspacePath string `yaml:"workspace_path"` |
| user | 5a7d60d | 2025-07-27 21:22:04 +0400 | [diff] [blame] | 73 | CommitMessageTemplate string `yaml:"commit_message_template"` |
| 74 | PRTemplate string `yaml:"pr_template"` |
| 75 | } |
| 76 | |
| 77 | // LoadConfig loads configuration from a YAML file |
| 78 | func 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 |
| 103 | func 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 | } |
| iomodo | b23799d | 2025-07-31 14:08:22 +0400 | [diff] [blame] | 122 | if webhookSecret := os.Getenv("GITHUB_WEBHOOK_SECRET"); webhookSecret != "" { |
| 123 | config.GitHub.WebhookSecret = webhookSecret |
| 124 | } |
| iomodo | 578f504 | 2025-07-28 20:46:02 +0400 | [diff] [blame] | 125 | 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 | } |
| user | 5a7d60d | 2025-07-27 21:22:04 +0400 | [diff] [blame] | 137 | |
| iomodo | 90de915 | 2025-07-31 13:20:01 +0400 | [diff] [blame] | 138 | if listenAddr := os.Getenv("LISTEN_ADDRESS"); listenAddr != "" { |
| 139 | config.Server.ListenAddress = listenAddr |
| 140 | } |
| 141 | |
| user | 5a7d60d | 2025-07-27 21:22:04 +0400 | [diff] [blame] | 142 | // 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 |
| 151 | func 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 == "" { |
| iomodo | 50598c6 | 2025-07-27 22:06:32 +0400 | [diff] [blame] | 168 | config.Tasks.StoragePath = "../operations/" |
| user | 5a7d60d | 2025-07-27 21:22:04 +0400 | [diff] [blame] | 169 | } |
| 170 | if config.Tasks.CompletedPath == "" { |
| iomodo | 50598c6 | 2025-07-27 22:06:32 +0400 | [diff] [blame] | 171 | config.Tasks.CompletedPath = "../operations/completed/" |
| user | 5a7d60d | 2025-07-27 21:22:04 +0400 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | // Git defaults |
| user | 5a7d60d | 2025-07-27 21:22:04 +0400 | [diff] [blame] | 175 | 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 |
| 209 | func 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 | |
| iomodo | 578f504 | 2025-07-28 20:46:02 +0400 | [diff] [blame] | 218 | // 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 != "" |
| iomodo | a53240a | 2025-07-30 17:33:35 +0400 | [diff] [blame] | 221 | |
| iomodo | 578f504 | 2025-07-28 20:46:02 +0400 | [diff] [blame] | 222 | if !hasGitHub && !hasGerrit { |
| 223 | return fmt.Errorf("either GitHub or Gerrit configuration is required") |
| user | 5a7d60d | 2025-07-27 21:22:04 +0400 | [diff] [blame] | 224 | } |
| iomodo | a53240a | 2025-07-30 17:33:35 +0400 | [diff] [blame] | 225 | |
| iomodo | 578f504 | 2025-07-28 20:46:02 +0400 | [diff] [blame] | 226 | // 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 | } |
| user | 5a7d60d | 2025-07-27 21:22:04 +0400 | [diff] [blame] | 234 | } |
| iomodo | a53240a | 2025-07-30 17:33:35 +0400 | [diff] [blame] | 235 | |
| iomodo | 578f504 | 2025-07-28 20:46:02 +0400 | [diff] [blame] | 236 | // 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 | } |
| user | 5a7d60d | 2025-07-27 21:22:04 +0400 | [diff] [blame] | 247 | } |
| 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 |
| 270 | func (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 |
| 280 | func (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 |
| iomodo | 50598c6 | 2025-07-27 22:06:32 +0400 | [diff] [blame] | 286 | } |
| iomodo | 578f504 | 2025-07-28 20:46:02 +0400 | [diff] [blame] | 287 | |
| 288 | // HasGitHubConfig returns true if GitHub is properly configured |
| 289 | func (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 |
| 294 | func (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 |
| 300 | func (c *Config) GetPrimaryGitProvider() string { |
| 301 | if c.HasGitHubConfig() { |
| 302 | return "github" |
| 303 | } |
| 304 | if c.HasGerritConfig() { |
| 305 | return "gerrit" |
| 306 | } |
| 307 | return "" |
| 308 | } |