pricingoverrides

package
v0.1.66 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const CurrencyUSD = "USD"

Variables

View Source
var ErrNotFound = errors.New("model pricing override not found")

ErrNotFound indicates a requested pricing override was not found.

Functions

func IsValidationError

func IsValidationError(err error) bool

IsValidationError reports whether err is a validation error.

Types

type Catalog

type Catalog = modelselectors.Catalog

Catalog is the minimal configured-provider surface needed for selector validation.

type DuplicateSelectorError

type DuplicateSelectorError struct {
	Normalized string
	Original   string
	Existing   string
}

DuplicateSelectorError reports two stored rows that normalize to one selector.

func (*DuplicateSelectorError) Error

func (e *DuplicateSelectorError) Error() string

type MongoDBStore

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

MongoDBStore stores model pricing overrides in MongoDB.

func NewMongoDBStore

func NewMongoDBStore(database *mongo.Database) (*MongoDBStore, error)

NewMongoDBStore creates collection indexes if needed.

func (*MongoDBStore) Close

func (s *MongoDBStore) Close() error

func (*MongoDBStore) Delete

func (s *MongoDBStore) Delete(ctx context.Context, selector string) error

func (*MongoDBStore) List

func (s *MongoDBStore) List(ctx context.Context) ([]Override, error)

func (*MongoDBStore) Upsert

func (s *MongoDBStore) Upsert(ctx context.Context, override Override) error

type Override

type Override struct {
	Selector     string    `json:"selector" bson:"_id"`
	ProviderName string    `json:"provider_name,omitempty" bson:"provider_name,omitempty"`
	Model        string    `json:"model,omitempty" bson:"model,omitempty"`
	Pricing      Pricing   `json:"pricing" bson:"pricing"`
	CreatedAt    time.Time `json:"created_at" bson:"created_at"`
	UpdatedAt    time.Time `json:"updated_at" bson:"updated_at"`
}

Override stores one persisted pricing override for a model selector.

func (Override) ScopeKind

func (o Override) ScopeKind() ScopeKind

ScopeKind reports the normalized selector scope for one override.

type Pricing

type Pricing struct {
	InputPerMtok           *float64      `json:"input_per_mtok,omitempty" bson:"input_per_mtok,omitempty"`
	OutputPerMtok          *float64      `json:"output_per_mtok,omitempty" bson:"output_per_mtok,omitempty"`
	CachedInputPerMtok     *float64      `json:"cached_input_per_mtok,omitempty" bson:"cached_input_per_mtok,omitempty"`
	CacheWritePerMtok      *float64      `json:"cache_write_per_mtok,omitempty" bson:"cache_write_per_mtok,omitempty"`
	ReasoningOutputPerMtok *float64      `json:"reasoning_output_per_mtok,omitempty" bson:"reasoning_output_per_mtok,omitempty"`
	BatchInputPerMtok      *float64      `json:"batch_input_per_mtok,omitempty" bson:"batch_input_per_mtok,omitempty"`
	BatchOutputPerMtok     *float64      `json:"batch_output_per_mtok,omitempty" bson:"batch_output_per_mtok,omitempty"`
	AudioInputPerMtok      *float64      `json:"audio_input_per_mtok,omitempty" bson:"audio_input_per_mtok,omitempty"`
	AudioOutputPerMtok     *float64      `json:"audio_output_per_mtok,omitempty" bson:"audio_output_per_mtok,omitempty"`
	PerImage               *float64      `json:"per_image,omitempty" bson:"per_image,omitempty"`
	InputPerImage          *float64      `json:"input_per_image,omitempty" bson:"input_per_image,omitempty"`
	PerSecondInput         *float64      `json:"per_second_input,omitempty" bson:"per_second_input,omitempty"`
	PerSecondOutput        *float64      `json:"per_second_output,omitempty" bson:"per_second_output,omitempty"`
	PerCharacterInput      *float64      `json:"per_character_input,omitempty" bson:"per_character_input,omitempty"`
	PerRequest             *float64      `json:"per_request,omitempty" bson:"per_request,omitempty"`
	PerPage                *float64      `json:"per_page,omitempty" bson:"per_page,omitempty"`
	Tiers                  []PricingTier `json:"tiers,omitempty" bson:"tiers,omitempty"`
}

Pricing stores operator-supplied pricing fields. Currency is intentionally omitted from persistence and API payloads; UI-managed pricing is USD-only.

type PricingTier

type PricingTier struct {
	UpToTokens    *float64 `json:"up_to_tokens,omitempty" bson:"up_to_tokens,omitempty"`
	UpToMtok      *float64 `json:"up_to_mtok,omitempty" bson:"up_to_mtok,omitempty"`
	InputPerMtok  *float64 `json:"input_per_mtok,omitempty" bson:"input_per_mtok,omitempty"`
	OutputPerMtok *float64 `json:"output_per_mtok,omitempty" bson:"output_per_mtok,omitempty"`
}

PricingTier stores future tiered pricing without changing the DB schema.

type Result

type Result struct {
	Service *Service
	Store   Store
	// contains filtered or unexported fields
}

Result holds the initialized pricing override service and any owned resources.

func New

func New(ctx context.Context, cfg *config.Config, shared storage.Storage, catalog Catalog, base usage.PricingResolver) (*Result, error)

New creates a pricing override subsystem using an existing storage connection.

func (*Result) Close

func (r *Result) Close() error

Close releases resources held by the pricing override subsystem.

type SQLStore added in v0.1.60

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

SQLStore stores model pricing overrides in a SQL database.

func NewSQLStore added in v0.1.60

func NewSQLStore(ctx context.Context, db sqlx.DB) (*SQLStore, error)

NewSQLStore creates the model_pricing_overrides table and indexes if needed.

func (*SQLStore) Close added in v0.1.60

func (s *SQLStore) Close() error

func (*SQLStore) Delete added in v0.1.60

func (s *SQLStore) Delete(ctx context.Context, selector string) error

func (*SQLStore) List added in v0.1.60

func (s *SQLStore) List(ctx context.Context) ([]Override, error)

func (*SQLStore) Upsert added in v0.1.60

func (s *SQLStore) Upsert(ctx context.Context, override Override) error

type ScopeKind

type ScopeKind = modelselectors.ScopeKind

ScopeKind identifies how broadly an override applies.

type Service

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

Service keeps pricing overrides cached in memory and resolves effective pricing.

func NewService

func NewService(store Store, catalog Catalog, base usage.PricingResolver) (*Service, error)

NewService creates a pricing override service backed by storage.

func (*Service) Delete

func (s *Service) Delete(ctx context.Context, selector string) error

Delete removes one override and refreshes the in-memory snapshot.

func (*Service) Get

func (s *Service) Get(selector string) (*Override, bool)

Get returns one cached override by normalized selector.

func (*Service) GetView

func (s *Service) GetView(selector string) (*View, bool)

GetView returns one cached override with scope metadata by normalized selector.

func (*Service) List

func (s *Service) List() []Override

List returns all cached overrides sorted by selector.

func (*Service) ListViews

func (s *Service) ListViews() []View

ListViews returns all cached overrides with scope metadata.

func (*Service) Refresh

func (s *Service) Refresh(ctx context.Context) error

Refresh reloads overrides from storage and atomically swaps the snapshot.

func (*Service) ResolvePricing

func (s *Service) ResolvePricing(model, providerName string) *core.ModelPricing

ResolvePricing resolves base pricing and applies the most specific DB override.

func (*Service) StartBackgroundRefresh

func (s *Service) StartBackgroundRefresh(interval time.Duration) func()

StartBackgroundRefresh periodically reloads pricing overrides from storage until stopped. Each s.Refresh call is capped by refreshTimeout, and shorter intervals are clamped to refreshTimeout.

func (*Service) Upsert

func (s *Service) Upsert(ctx context.Context, override Override) error

Upsert validates and stores one override, then refreshes the in-memory snapshot.

type Store

type Store interface {
	List(ctx context.Context) ([]Override, error)
	Upsert(ctx context.Context, override Override) error
	Delete(ctx context.Context, selector string) error
	Close() error
}

Store defines persistence operations for pricing overrides.

type ValidationError

type ValidationError = modelselectors.ValidationError

ValidationError indicates invalid override input or invalid override state.

type View

type View struct {
	Override
	ScopeKind ScopeKind `json:"scope_kind"`
}

View is the admin-facing representation of one persisted pricing override.

Jump to

Keyboard shortcuts

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