// Package access defines managed downstream keys and their upstream routing rules. package access import ( "crypto/sha256" "encoding/hex" "strings" "time" ) const ( StatusActive = "active" StatusDisabled = "disabled" StatusArchived = "archived" RouteAuto = "auto" RouteStrict = "strict" ResetNone = "none" ResetDaily = "daily" ResetWeekly = "weekly" ResetMonthly = "monthly" ) type ManagedKey struct { ID string `json:"id"` Name string `json:"name"` Secret string `json:"secret"` Status string `json:"status"` RouteMode string `json:"route_mode"` UpstreamAccountID string `json:"upstream_account_id,omitempty"` AllModels bool `json:"all_models"` ShowInStats bool `json:"show_in_stats"` Models []string `json:"models"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` } type UpstreamAccount struct { ID string `json:"id"` CPAAuthID string `json:"cpa_auth_id"` CPAAuthIndex string `json:"cpa_auth_index"` Provider string `json:"provider"` DisplayName string `json:"display_name"` Status string `json:"status"` StatusMessage string `json:"status_message,omitempty"` Disabled bool `json:"disabled"` Unavailable bool `json:"unavailable"` Priority int `json:"priority"` LastSeenAt time.Time `json:"last_seen_at"` } type KeyStats struct { KeyID string `json:"key_id"` Total UsageSummary `json:"total"` Today UsageSummary `json:"today"` LastUsedAt *time.Time `json:"last_used_at,omitempty"` } type UsageSummary struct { Requests int64 `json:"requests"` InputTokens int64 `json:"input_tokens"` OutputTokens int64 `json:"output_tokens"` TotalTokens int64 `json:"total_tokens"` CostMicros int64 `json:"cost_micros"` } type BillingState struct { KeyID string `json:"key_id"` QuotaMicros int64 `json:"-"` SpentMicros int64 `json:"-"` LifetimeSpentMicros int64 `json:"-"` BalanceMicros int64 `json:"-"` ResetPeriod string `json:"reset_period"` NextResetAt *time.Time `json:"next_reset_at,omitempty"` MaxConcurrency int `json:"max_concurrency"` ActiveRequests int `json:"active_requests"` CycleSequence int64 `json:"cycle_sequence"` CycleStartedAt time.Time `json:"cycle_started_at"` } type BillingSettings struct { QuotaMicros int64 ResetPeriod string NextResetAt *time.Time MaxConcurrency int } type LedgerEntry struct { ID int64 `json:"id"` KeyID string `json:"key_id"` CycleSequence int64 `json:"cycle_sequence"` Kind string `json:"kind"` AmountMicros int64 `json:"-"` BalanceAfterMicros int64 `json:"-"` RequestID string `json:"request_id,omitempty"` ExecutionID string `json:"execution_id,omitempty"` Model string `json:"model,omitempty"` OccurredAt time.Time `json:"occurred_at"` CreatedAt time.Time `json:"created_at"` } // CallerScope mirrors CLIProxyAPI's stable downstream principal namespace. func CallerScope(principal string) string { principal = strings.TrimSpace(principal) if principal == "" { return "" } sum := sha256.Sum256([]byte("cli-proxy-api:caller-scope:v1\x00" + principal)) return hex.EncodeToString(sum[:]) }