fix: deliver validated AI insight responses

This commit is contained in:
2026-08-22 15:47:38 +08:00
parent ca55c2fb3d
commit 3fba4261ee
68 changed files with 6870 additions and 493 deletions

View File

@@ -8,27 +8,33 @@ import (
)
type Config struct {
AppEnv string
HTTPAddr string
DatabaseURL string
RedisAddr string
RedisPassword string
ImportPath string
DemoUsername string
DemoPassword string
SessionSecret string
CursorSecret string
FrontendOrigin string
CookieSecure bool
AIGrowthEnabled bool
AvalAIAPIKey string
AvalAIBaseURL string
AvalAIModel string
AIDailyRequestBudget int64
AIDailyTokenBudget int64
AIMaxConcurrent int
AICreatePerMinute int64
AIUncachedScopesPerDay int64
AppEnv string
HTTPAddr string
DatabaseURL string
RedisAddr string
RedisPassword string
ImportPath string
DemoUsername string
DemoPassword string
SessionSecret string
CursorSecret string
FrontendOrigin string
CookieSecure bool
AIGrowthEnabled bool
AICoachEnabled bool
AIPageInsightsEnabled bool
AvalAIAPIKey string
AvalAIBaseURL string
AvalAIModel string
AIDailyRequestBudget int64
AIDailyTokenBudget int64
AIMaxConcurrent int
AICreatePerMinute int64
AIUncachedScopesPerDay int64
AICoachDailyRequestBudget int64
AICoachDailyTokenBudget int64
AIPageInsightsDailyRequestBudget int64
AIPageInsightsDailyTokenBudget int64
}
func Load() (Config, error) {
@@ -58,6 +64,14 @@ func Load() (Config, error) {
if err != nil {
return Config{}, fmt.Errorf("parsing AI growth enabled: %w", err)
}
cfg.AICoachEnabled, err = strconv.ParseBool(value("AI_COACH_ENABLED", strconv.FormatBool(cfg.AIGrowthEnabled)))
if err != nil {
return Config{}, fmt.Errorf("parsing AI coach enabled: %w", err)
}
cfg.AIPageInsightsEnabled, err = strconv.ParseBool(value("AI_PAGE_INSIGHTS_ENABLED", strconv.FormatBool(cfg.AIGrowthEnabled)))
if err != nil {
return Config{}, fmt.Errorf("parsing AI page insights enabled: %w", err)
}
cfg.AIDailyRequestBudget, err = parseInt64("AI_DAILY_REQUEST_BUDGET", 0)
if err != nil {
return Config{}, err
@@ -66,6 +80,22 @@ func Load() (Config, error) {
if err != nil {
return Config{}, err
}
cfg.AICoachDailyRequestBudget, err = parseInt64("AI_COACH_DAILY_REQUEST_BUDGET", cfg.AIDailyRequestBudget)
if err != nil {
return Config{}, err
}
cfg.AICoachDailyTokenBudget, err = parseInt64("AI_COACH_DAILY_TOKEN_BUDGET", cfg.AIDailyTokenBudget)
if err != nil {
return Config{}, err
}
cfg.AIPageInsightsDailyRequestBudget, err = parseInt64("AI_PAGE_INSIGHTS_DAILY_REQUEST_BUDGET", cfg.AIDailyRequestBudget)
if err != nil {
return Config{}, err
}
cfg.AIPageInsightsDailyTokenBudget, err = parseInt64("AI_PAGE_INSIGHTS_DAILY_TOKEN_BUDGET", cfg.AIDailyTokenBudget)
if err != nil {
return Config{}, err
}
maxConcurrent, err := parseInt64("AI_MAX_CONCURRENT", 4)
if err != nil {
return Config{}, err
@@ -98,13 +128,19 @@ func (c Config) ValidateAPI() error {
if c.AIMaxConcurrent < 1 || c.AICreatePerMinute < 1 || c.AIUncachedScopesPerDay < 1 {
return errors.New("config: AI limits must be positive")
}
if c.AppEnv == "production" && c.AIGrowthEnabled {
if c.AppEnv == "production" && (c.AIGrowthEnabled || c.AICoachEnabled || c.AIPageInsightsEnabled) {
if c.AvalAIAPIKey == "" || c.AvalAIModel == "" {
return errors.New("config: enabled production AI requires AVALAI_API_KEY and AVALAI_MODEL")
}
if c.AIDailyRequestBudget < 1 || c.AIDailyTokenBudget < 1 {
if c.AIGrowthEnabled && (c.AIDailyRequestBudget < 1 || c.AIDailyTokenBudget < 1) {
return errors.New("config: enabled production AI requires positive daily budgets")
}
if c.AICoachEnabled && (c.AICoachDailyRequestBudget < 1 || c.AICoachDailyTokenBudget < 1) {
return errors.New("config: enabled production coach requires positive coach budgets")
}
if c.AIPageInsightsEnabled && (c.AIPageInsightsDailyRequestBudget < 1 || c.AIPageInsightsDailyTokenBudget < 1) {
return errors.New("config: enabled production page insights require positive page-insight budgets")
}
}
return nil
}