aiprofile

package
v1.3.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 28, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package aiprofile implements AI_PROFILE enforcement, budget caps, feature flags, per-tier timeouts, and rate-limit coordination. Sprint P88-S05: T-05-06 through T-05-10.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsTierAllowed

func IsTierAllowed(p Profile, t Tier) bool

IsTierAllowed checks whether a tier is permitted by the current profile.

Types

type BudgetConfig

type BudgetConfig struct {
	DailyUSD   float64 // AI_DAILY_BUDGET_USD (0 = disabled / free tier only)
	MonthlyUSD float64 // AI_MONTHLY_BUDGET_USD (0 = disabled / free tier only)
}

BudgetConfig holds the daily and monthly budget caps from the AI config block (CLI-R18: folded into .env.secrets; previously a dedicated .env.ai).

func LoadBudget

func LoadBudget() BudgetConfig

LoadBudget reads budget env vars. Zero means disabled (free tier only, paid tiers blocked). Positive value is a hard cap in USD.

func (BudgetConfig) CheckBudget

func (b BudgetConfig) CheckBudget(spentDaily, spentMonthly float64) error

CheckBudget evaluates whether a request should be allowed given current spend. Returns nil if allowed, or an error with a user-facing message if blocked. spentDaily and spentMonthly are in USD (converted from cost_micros / 1e6).

func (BudgetConfig) IsPaidTierBlocked

func (b BudgetConfig) IsPaidTierBlocked() bool

IsPaidTierBlocked returns true when budget is zero (free tier only mode).

func (BudgetConfig) ShouldAlert

func (b BudgetConfig) ShouldAlert(spentMonthly float64) bool

ShouldAlert returns true when monthly spend has reached the budget threshold.

type FeatureFlags

type FeatureFlags struct {
	AutoProvision    bool // AI_FEATURE_AUTO_PROVISION (A2)
	LocalAutoInstall bool // AI_FEATURE_LOCAL_AUTO_INSTALL (A1)
	RoutingUI        bool // AI_FEATURE_ROUTING_UI (A3 UI)
	HealthDaemon     bool // AI_FEATURE_HEALTH_DAEMON (A5)
}

FeatureFlags holds the AI_FEATURE_* master switches (T-05-08). All default to true (on). Flippable at runtime via admin settings.

func LoadFeatureFlags

func LoadFeatureFlags() FeatureFlags

LoadFeatureFlags reads AI_FEATURE_* env vars. Missing or empty = true (on).

type Profile

type Profile string

Profile represents the AI routing profile.

const (
	ProfileAuto      Profile = "auto"
	ProfileLocalOnly Profile = "local_only"
	ProfilePoolOnly  Profile = "pool_only"
	ProfileOAuthOnly Profile = "oauth_only"
	ProfileCustom    Profile = "custom"
)

func LoadProfile

func LoadProfile() Profile

LoadProfile reads AI_PROFILE from the environment.

func ParseProfile

func ParseProfile(s string) (Profile, error)

ParseProfile parses an AI_PROFILE string into a typed Profile.

type Tier

type Tier string

Tier represents a routing tier.

const (
	TierLocal Tier = "local"
	TierPool  Tier = "pool"
	TierOAuth Tier = "oauth"
	TierPaid  Tier = "paid"
)

func AllowedTiers

func AllowedTiers(p Profile) []Tier

AllowedTiers returns the tiers permitted by the given profile.

func FilterTierChain

func FilterTierChain(p Profile, chain []Tier) []Tier

FilterTierChain filters a tier chain based on the profile. Returns only allowed tiers in the original order. Used by the router at load time (T-05-06).

type TierTimeouts

type TierTimeouts struct {
	Local time.Duration // AI_TIMEOUT_LOCAL_MS
	OAuth time.Duration // AI_TIMEOUT_OAUTH_MS
	Pool  time.Duration // AI_TIMEOUT_POOL_MS
	Paid  time.Duration // AI_TIMEOUT_PAID_MS
}

TierTimeouts holds per-tier timeout overrides from env vars (T-05-09). Zero means "use routing defaults from task config".

func LoadTierTimeouts

func LoadTierTimeouts() TierTimeouts

LoadTierTimeouts reads AI_TIMEOUT_*_MS env vars.

func (TierTimeouts) TimeoutForTier

func (t TierTimeouts) TimeoutForTier(tier Tier) time.Duration

TimeoutForTier returns the timeout override for the given tier, or zero if no override is set (caller should use its own default).

type TokenBucket

type TokenBucket struct {
	// contains filtered or unexported fields
}

TokenBucket implements a per-key rate limiter capping at 15 req/min (T-05-10). Thread-safe. Each key gets its own bucket.

func NewTokenBucket

func NewTokenBucket() *TokenBucket

NewTokenBucket creates a rate limiter with 15 RPM per key.

func (*TokenBucket) Allow

func (tb *TokenBucket) Allow(keyID int) bool

Allow checks whether a request for the given key ID is allowed. Returns true if allowed, false if rate-limited.

func (*TokenBucket) IsAvailable

func (tb *TokenBucket) IsAvailable(keyID int) bool

IsAvailable returns true if the key is not currently in cooldown.

func (*TokenBucket) MarkRateLimited

func (tb *TokenBucket) MarkRateLimited(keyID int, retryAfter time.Duration)

MarkRateLimited marks a key as rate-limited. If retryAfter > 0, uses that exact duration. Otherwise applies exponential backoff: 60s, 120s, 240s, up to 1h.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL