| 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 { |
| 37 | Token string `yaml:"token"` |
| 38 | Owner string `yaml:"owner"` |
| 39 | Repo string `yaml:"repo"` |
| 40 | } |
| 41 | |
| iomodo | 578f504 | 2025-07-28 20:46:02 +0400 | [diff] [blame] | 42 | // GerritConfig represents Gerrit integration configuration |
| 43 | type 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 | |
| user | 5a7d60d | 2025-07-27 21:22:04 +0400 | [diff] [blame] | 50 | // AgentConfig represents individual agent configuration |
| 51 | type AgentConfig struct { |
| 52 | Name string `yaml:"name"` |
| 53 | Role string `yaml:"role"` |
| 54 | Model string `yaml:"model"` |
| 55 | SystemPromptFile string `yaml:"system_prompt_file"` |
| iomodo | 50598c6 | 2025-07-27 22:06:32 +0400 | [diff] [blame] | 56 | 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 |
| user | 5a7d60d | 2025-07-27 21:22:04 +0400 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | // TasksConfig represents task management configuration |
| 63 | type TasksConfig struct { |
| 64 | StoragePath string `yaml:"storage_path"` |
| 65 | CompletedPath string `yaml:"completed_path"` |
| 66 | } |
| 67 | |
| 68 | // GitConfig represents Git operation configuration |
| 69 | type GitConfig struct { |
| iomodo | a53240a | 2025-07-30 17:33:35 +0400 | [diff] [blame] | 70 | RepoPath string `yaml:"repo_path"` |
| iomodo | e7477eb | 2025-07-30 20:26:30 +0400 | [diff] [blame] | 71 | WorkspacePath string `yaml:"workspace_path"` |
| user | 5a7d60d | 2025-07-27 21:22:04 +0400 | [diff] [blame] | 72 | CommitMessageTemplate string `yaml:"commit_message_template"` |
| 73 | PRTemplate string `yaml:"pr_template"` |
| 74 | } |
| 75 | |
| 76 | // LoadConfig loads configuration from a YAML file |
| 77 | func 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 |
| 102 | func 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 | } |
| iomodo | 578f504 | 2025-07-28 20:46:02 +0400 | [diff] [blame] | 121 | 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 | } |
| user | 5a7d60d | 2025-07-27 21:22:04 +0400 | [diff] [blame] | 133 | |
| iomodo | 90de915 | 2025-07-31 13:20:01 +0400 | [diff] [blame^] | 134 | if listenAddr := os.Getenv("LISTEN_ADDRESS"); listenAddr != "" { |
| 135 | config.Server.ListenAddress = listenAddr |
| 136 | } |
| 137 | |
| user | 5a7d60d | 2025-07-27 21:22:04 +0400 | [diff] [blame] | 138 | // 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 |
| 147 | func 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 == "" { |
| iomodo | 50598c6 | 2025-07-27 22:06:32 +0400 | [diff] [blame] | 164 | config.Tasks.StoragePath = "../operations/" |
| user | 5a7d60d | 2025-07-27 21:22:04 +0400 | [diff] [blame] | 165 | } |
| 166 | if config.Tasks.CompletedPath == "" { |
| iomodo | 50598c6 | 2025-07-27 22:06:32 +0400 | [diff] [blame] | 167 | config.Tasks.CompletedPath = "../operations/completed/" |
| user | 5a7d60d | 2025-07-27 21:22:04 +0400 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | // Git defaults |
| user | 5a7d60d | 2025-07-27 21:22:04 +0400 | [diff] [blame] | 171 | 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 |
| 205 | func 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 | |
| iomodo | 578f504 | 2025-07-28 20:46:02 +0400 | [diff] [blame] | 214 | // 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 != "" |
| iomodo | a53240a | 2025-07-30 17:33:35 +0400 | [diff] [blame] | 217 | |
| iomodo | 578f504 | 2025-07-28 20:46:02 +0400 | [diff] [blame] | 218 | if !hasGitHub && !hasGerrit { |
| 219 | return fmt.Errorf("either GitHub or Gerrit configuration is required") |
| user | 5a7d60d | 2025-07-27 21:22:04 +0400 | [diff] [blame] | 220 | } |
| iomodo | a53240a | 2025-07-30 17:33:35 +0400 | [diff] [blame] | 221 | |
| iomodo | 578f504 | 2025-07-28 20:46:02 +0400 | [diff] [blame] | 222 | // 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 | } |
| user | 5a7d60d | 2025-07-27 21:22:04 +0400 | [diff] [blame] | 230 | } |
| iomodo | a53240a | 2025-07-30 17:33:35 +0400 | [diff] [blame] | 231 | |
| iomodo | 578f504 | 2025-07-28 20:46:02 +0400 | [diff] [blame] | 232 | // 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 | } |
| user | 5a7d60d | 2025-07-27 21:22:04 +0400 | [diff] [blame] | 243 | } |
| 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 |
| 266 | func (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 |
| 276 | func (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 |
| iomodo | 50598c6 | 2025-07-27 22:06:32 +0400 | [diff] [blame] | 282 | } |
| iomodo | 578f504 | 2025-07-28 20:46:02 +0400 | [diff] [blame] | 283 | |
| 284 | // HasGitHubConfig returns true if GitHub is properly configured |
| 285 | func (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 |
| 290 | func (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 |
| 296 | func (c *Config) GetPrimaryGitProvider() string { |
| 297 | if c.HasGitHubConfig() { |
| 298 | return "github" |
| 299 | } |
| 300 | if c.HasGerritConfig() { |
| 301 | return "gerrit" |
| 302 | } |
| 303 | return "" |
| 304 | } |