feat: add configurable AgentRouter AI provider

This commit is contained in:
2026-08-22 21:47:41 +08:00
parent 4f91726edb
commit cf2f114dc8
20 changed files with 1204 additions and 79 deletions

View File

@@ -41,21 +41,30 @@ func main() {
defer redisClient.Close()
repository := growthreport.NewRepository(pool)
coordinator := growthreport.NewCoordinator(redisClient, growthreport.CoordinatorConfig{CreatePerMinute: cfg.AICreatePerMinute, UncachedPerDay: cfg.AIUncachedScopesPerDay, DailyRequests: cfg.AIDailyRequestBudget, DailyTokens: cfg.AIDailyTokenBudget, MaxConcurrent: cfg.AIMaxConcurrent})
avalai := growthreport.NewAvalAIClient(growthreport.AvalAIConfig{BaseURL: cfg.AvalAIBaseURL, APIKey: cfg.AvalAIAPIKey, Model: cfg.AvalAIModel, Timeout: 30 * time.Second}, &http.Client{})
processor := growthreport.NewProcessor(repository, growthreport.NewPostgreSQLBuilder(pool), avalai, coordinator, cfg.AIGrowthEnabled && cfg.AvalAIAPIKey != "")
sharedProvider := aiprovider.New(aiprovider.Config{BaseURL: cfg.AvalAIBaseURL, APIKey: cfg.AvalAIAPIKey, Model: cfg.AvalAIModel, Timeout: 30 * time.Second}, &http.Client{})
provider := cfg.SelectedAIProvider()
var growthGenerator growthreport.CopyGenerator
var sharedProvider aiprovider.Generator
if provider.Name == config.AIProviderAgentRouter {
growthGenerator = growthreport.NewAgentRouterClient(growthreport.AgentRouterConfig{BaseURL: provider.BaseURL, APIKey: provider.APIKey, Model: provider.Model, Timeout: 90 * time.Second}, &http.Client{})
sharedProvider = aiprovider.NewAgentRouter(aiprovider.Config{BaseURL: provider.BaseURL, APIKey: provider.APIKey, Model: provider.Model, Timeout: 90 * time.Second}, &http.Client{})
} else {
growthGenerator = growthreport.NewAvalAIClient(growthreport.AvalAIConfig{BaseURL: provider.BaseURL, APIKey: provider.APIKey, Model: provider.Model, Timeout: 30 * time.Second}, &http.Client{})
sharedProvider = aiprovider.New(aiprovider.Config{BaseURL: provider.BaseURL, APIKey: provider.APIKey, Model: provider.Model, Timeout: 30 * time.Second}, &http.Client{})
}
aiAvailable := provider.APIKey != "" && provider.Model != ""
processor := growthreport.NewProcessor(repository, growthreport.NewPostgreSQLBuilder(pool), growthGenerator, coordinator, cfg.AIGrowthEnabled && aiAvailable)
var insightService *insight.Service
if cfg.AIPageInsightsEnabled && cfg.AvalAIAPIKey != "" {
if cfg.AIPageInsightsEnabled && aiAvailable {
insightService = insight.NewService(insight.NewRepository(pool), insight.NewPostgreSQLBuilder(pool), insight.NewAvalAIGenerator(sharedProvider)).WithBudget(cfg.AIPageInsightsDailyRequestBudget, cfg.AIPageInsightsDailyTokenBudget)
} else {
insightService = insight.NewService(insight.NewRepository(pool), insight.NewPostgreSQLBuilder(pool))
}
var coachGenerator coach.Generator
if cfg.AICoachEnabled && cfg.AvalAIAPIKey != "" {
if cfg.AICoachEnabled && aiAvailable {
coachGenerator = coach.NewAvalAIGenerator(sharedProvider)
}
coachService := coach.NewService(coach.NewRepository(pool), coachGenerator).WithBudget(cfg.AICoachDailyRequestBudget, cfg.AICoachDailyTokenBudget)
logger.Info("growth worker ready", "ai_enabled", cfg.AIGrowthEnabled && cfg.AvalAIAPIKey != "")
logger.Info("growth worker ready", "ai_enabled", cfg.AIGrowthEnabled && aiAvailable, "ai_provider", provider.Name, "ai_model", provider.Model)
poll := time.NewTicker(time.Second)
defer poll.Stop()
cleanup := time.NewTicker(24 * time.Hour)