aitaste

package
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package aitaste meters the free account's AI taste: a few answers a month from the operator's key (Pro v6).

Two counters stand behind it. ai_taste_usage holds one row per (user, UTC month) on the request pool under row-level security; Reserve takes a unit atomically and Refund gives it back, so the count cannot pass the quota however the calls race. ai_taste_spend holds what the day's free-taste calls may still cost the operator, on the system pool: a hold at the route's estimate is taken before the call and settled to the real cost after it. Both fail CLOSED: a store error refuses the call.

Index

Constants

View Source
const (
	// MonthlyQuota is how many operator-key answers a free account takes in a UTC calendar month.
	MonthlyQuota = 5
	// DefaultDailyCeilingUSD is what free-taste calls may cost the operator in a UTC day, on top of the Console spend limit.
	DefaultDailyCeilingUSD = 5.0
	// EstimateUSD is held per call before it runs and settled to the real cost after. It sits above the dearest measured
	// operator answer (about four cents on the efficiency route).
	EstimateUSD = 0.06
)

Variables

This section is empty.

Functions

func DayStart

func DayStart(now time.Time) time.Time

DayStart is the UTC day, the key of ai_taste_spend.

func MonthStart

func MonthStart(now time.Time) time.Time

MonthStart is the first day of now's UTC calendar month, the key of ai_taste_usage.

func NextDay

func NextDay(now time.Time) time.Time

NextDay is when the daily ceiling renews.

func ResetAt

func ResetAt(now time.Time) time.Time

ResetAt is when the month's taste renews: the first instant of the next UTC month.

Types

type Gate

type Gate struct {
	Store *Store
	Spend *Spend
	User  uuid.UUID
	Quota int
	Now   func() time.Time
	// contains filtered or unexported fields
}

Gate is one request's ai.Taste for a free account: the ceiling's hold first (so a refused call burns no taste), then the user's unit.

func (*Gate) Refund

func (g *Gate) Refund(ctx context.Context)

Refund implements ai.Taste.

func (*Gate) Spent

func (g *Gate) Spent(ctx context.Context, usd float64)

Spent implements ai.Taste: the hold becomes the real cost.

func (*Gate) Take

func (g *Gate) Take(ctx context.Context) error

Take implements ai.Taste. Every failure to ask a store refuses the call.

type Spend

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

Spend is the install-wide daily ceiling on free-taste cost, on the system pool.

func NewSpend

func NewSpend(sys *sql.DB, ceilingUSD float64) *Spend

NewSpend wires the ceiling to the system pool. A nil pool yields a nil Spend, which refuses every hold.

func (*Spend) Adjust

func (sp *Spend) Adjust(ctx context.Context, day time.Time, deltaUSD float64) error

Adjust moves the day's spend by delta (a settlement: real cost minus the hold; a release: minus the hold). Never below zero.

func (*Spend) Hold

func (sp *Spend) Hold(ctx context.Context, now time.Time, est float64) (bool, error)

Hold adds est to the day's spend unless that would pass the ceiling.

func (*Spend) Today

func (sp *Spend) Today(ctx context.Context, now time.Time) (float64, error)

Today is the day's spend so far, in dollars.

type Store

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

Store counts a user's taste on the request pool.

func NewStore

func NewStore(req *sql.DB) *Store

NewStore wires a Store to the request pool. A nil pool yields a nil Store.

func (*Store) Refund

func (s *Store) Refund(ctx context.Context, uid uuid.UUID, now time.Time) error

Refund gives one unit back, never below zero.

func (*Store) Reserve

func (s *Store) Reserve(ctx context.Context, uid uuid.UUID, now time.Time, quota int) (bool, error)

Reserve takes one unit of the month's quota for uid, and reports false (and no error) when the quota is spent. The pool's user must be uid: the row is under row-level security.

func (*Store) Used

func (s *Store) Used(ctx context.Context, uid uuid.UUID, now time.Time) (int, error)

Used is how many units uid has taken this month.

Jump to

Keyboard shortcuts

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