config.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "strconv"
  6. )
  7. // Config holds the agent configuration
  8. type Config struct {
  9. // ARP Server Configuration
  10. ARPURL string
  11. ARPUsername string
  12. ARPPassword string
  13. // OpenAI Configuration
  14. OpenAIKey string
  15. OpenAIModel string
  16. OpenAITemperature float64
  17. OpenAIBaseURL string
  18. OpenAIMaxTokens int
  19. // Agent Identity Configuration
  20. AgentName string
  21. Specialization string
  22. Values string
  23. Goals string
  24. // Queue Configuration
  25. MaxQueueSize int
  26. // Agent Iteration Configuration
  27. MaxIterations int
  28. // MCP Configuration
  29. MCPConfigPath string
  30. // LLM Retry Configuration
  31. LLMMaxRetries int
  32. LLMRetryDelay int // in seconds
  33. // MCP Reconnection Configuration
  34. MCPReconnectDelay int // in seconds
  35. }
  36. // LoadConfig loads configuration from environment variables
  37. func LoadConfig() (*Config, error) {
  38. cfg := &Config{
  39. ARPURL: os.Getenv("ARP_URL"),
  40. ARPUsername: os.Getenv("ARP_USERNAME"),
  41. ARPPassword: os.Getenv("ARP_PASSWORD"),
  42. OpenAIKey: os.Getenv("OPENAI_API_KEY"),
  43. }
  44. // Required fields
  45. if cfg.ARPURL == "" {
  46. return nil, fmt.Errorf("ARP_URL environment variable is required")
  47. }
  48. if cfg.ARPUsername == "" {
  49. return nil, fmt.Errorf("ARP_USERNAME environment variable is required")
  50. }
  51. if cfg.ARPPassword == "" {
  52. return nil, fmt.Errorf("ARP_PASSWORD environment variable is required")
  53. }
  54. if cfg.OpenAIKey == "" {
  55. return nil, fmt.Errorf("OPENAI_API_KEY environment variable is required")
  56. }
  57. // Optional fields with defaults
  58. cfg.OpenAIModel = getEnvWithDefault("OPENAI_MODEL", "gpt-4")
  59. cfg.OpenAITemperature = getEnvFloatWithDefault("OPENAI_TEMPERATURE", 0.0)
  60. cfg.OpenAIBaseURL = os.Getenv("OPENAI_BASE_URL") // Empty means use default OpenAI API
  61. cfg.OpenAIMaxTokens = getEnvIntWithDefault("OPENAI_MAX_TOKENS", 4096)
  62. // Agent Identity Configuration with defaults
  63. cfg.AgentName = getEnvWithDefault("ARP_AGENT_NAME", "AI Assistant")
  64. cfg.Specialization = getEnvWithDefault("ARP_AGENT_SPECIALIZATION", "general assistance")
  65. cfg.Values = getEnvWithDefault("ARP_AGENT_VALUES", "helpfulness, accuracy, and collaboration")
  66. cfg.Goals = getEnvWithDefault("ARP_AGENT_GOALS", "help teammates accomplish their goals and contribute to the team's success")
  67. // Queue Configuration with defaults
  68. cfg.MaxQueueSize = getEnvIntWithDefault("ARP_MAX_QUEUE_SIZE", 100)
  69. // Agent Iteration Configuration with defaults
  70. cfg.MaxIterations = getEnvIntWithDefault("ARP_MAX_ITERATIONS", 10)
  71. // MCP Configuration with default
  72. cfg.MCPConfigPath = getEnvWithDefault("MCP_CONFIG_PATH", "")
  73. // LLM Retry Configuration with defaults
  74. cfg.LLMMaxRetries = getEnvIntWithDefault("ARP_LLM_MAX_RETRIES", 3)
  75. cfg.LLMRetryDelay = getEnvIntWithDefault("ARP_LLM_RETRY_DELAY", 1)
  76. // MCP Reconnection Configuration with defaults
  77. cfg.MCPReconnectDelay = getEnvIntWithDefault("ARP_MCP_RECONNECT_DELAY", 5)
  78. return cfg, nil
  79. }
  80. // getEnvWithDefault returns the environment variable value or a default
  81. func getEnvWithDefault(key, defaultValue string) string {
  82. if value := os.Getenv(key); value != "" {
  83. return value
  84. }
  85. return defaultValue
  86. }
  87. // getEnvFloatWithDefault returns the environment variable as float64 or a default
  88. func getEnvFloatWithDefault(key string, defaultValue float64) float64 {
  89. if value := os.Getenv(key); value != "" {
  90. if f, err := strconv.ParseFloat(value, 64); err == nil {
  91. return f
  92. }
  93. }
  94. return defaultValue
  95. }
  96. // getEnvIntWithDefault returns the environment variable as int or a default
  97. func getEnvIntWithDefault(key string, defaultValue int) int {
  98. if value := os.Getenv(key); value != "" {
  99. if i, err := strconv.Atoi(value); err == nil {
  100. return i
  101. }
  102. }
  103. return defaultValue
  104. }