Documentation
¶
Overview ¶
Package surplus polls the Surplus Intelligence market, model list and balance on a fixed schedule.
Index ¶
- Constants
- type Account
- type Balance
- type Charges
- func (c *Charges) Actual(row bifrost.Log) (float64, bool)
- func (c *Charges) Ended(reqID, engineProvider, model string)
- func (c *Charges) Run(ctx context.Context)
- func (c *Charges) Seen(reqID, engineProvider, model, upstreamID, keyID string, microUSD *int64)
- func (c *Charges) Wants(engineProvider string) bool
- type Config
- type Equivalent
- type Quote
- type SettleFunc
- type Trend
- type Worker
- func (w *Worker) Balance(provider string) (Balance, bool)
- func (w *Worker) IsReady() bool
- func (w *Worker) LastError() string
- func (w *Worker) Market(model string) (Quote, bool)
- func (w *Worker) MarketStatus() (map[string]Quote, time.Time, string)
- func (w *Worker) Markets() (map[string]Quote, time.Time)
- func (w *Worker) Models() []string
- func (w *Worker) OnUpdate(fn func())
- func (w *Worker) Ready() <-chan struct{}
- func (w *Worker) RefreshBalance(ctx context.Context, provider string) bool
- func (w *Worker) Run(ctx context.Context)
- func (w *Worker) Wake()
Constants ¶
const ( ErrMinimumDiscountNotMet = "minimum_discount_not_met" ErrMinimumDiscountSupplyCooldown = "minimum_discount_supply_cooldown" ErrNoHealthySellers = "no_healthy_sellers" ErrNoHealthyTrustedSellers = "no_healthy_trusted_sellers" ErrInsufficientCredit = "insufficient_credit" ErrBudgetExceeded = "budget_exceeded" )
Surplus upstream error codes handled specially by remora's cooling.
Verified 2026-12-03 from https://www.surplusintelligence.ai/llms-full.txt: param appears only on 400 validation errors, never on 404 or 402, so the front door falls back in-request on 404 minimum_discount_not_met (B/core/failureclass.go:194,318-324) and remora cools the member for 60 s.
const ( MinInterval = 5 * time.Minute DefaultInterval = 5 * time.Minute )
Polling cadence bounds.
const DefaultBaseURL = "https://api.surplusintelligence.ai"
DefaultBaseURL is the public Surplus Intelligence host root; /api/markets and /v1 sit beside each other.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Account ¶
type Account struct {
Provider, KeyID, Secret string
}
Account identifies a Surplus provider and its first enabled key.
type Charges ¶
type Charges struct {
// contains filtered or unexported fields
}
Charges bridges proxy response headers and the log feed to supply real Surplus per-request charges. Inline charges are returned synchronously from Actual; missing or streamed charges are looked up via GET /v1/requests/{id} and settled once both the response end and the engine log row are observed.
func NewCharges ¶
func NewCharges( baseURL string, secret func(ctx context.Context, keyID string) (string, error), settle SettleFunc, isSurplus func(provider string) bool, log *slog.Logger, ) *Charges
NewCharges creates a Charges store. A nil log discards.
func (*Charges) Actual ¶
Actual returns the real USD charge for a successful or cancelled Surplus log row. It is idempotent on re-delivery. Error/in-progress rows and unknown/pending charges return (0, false).
func (*Charges) Ended ¶
Ended marks the HTTP response body as finished and queues a lookup job if the log row has already arrived.
func (*Charges) Run ¶
Run drains pending charge lookup jobs through a 4/s rate limiter until ctx is cancelled.
func (*Charges) Seen ¶
Seen records the upstream request id and optional inline micro-USD charge captured from response headers. If the log feed already delivered the row via Actual before headers arrived, Seen merges the metadata and either queues an immediate settlement (when an inline charge is present) or lets Ended queue the lookup.
type Config ¶
type Config struct {
BaseURL string // DefaultBaseURL unless overridden
Interval time.Duration // DefaultInterval when <= 0
Client *http.Client // default client with timeout when nil
OnCredit func(ctx context.Context, provider string) // called when a balance read returns > 0
}
Config configures the Surplus market, model and balance Worker.
type Equivalent ¶
type Equivalent struct {
// contains filtered or unexported fields
}
Equivalent supplies Surplus direct list prices from a Worker's cached /v1/models and /api/markets snapshots.
func NewEquivalent ¶
func NewEquivalent(w *Worker) *Equivalent
NewEquivalent constructs an Equivalent source backed by w.
func (*Equivalent) Equivalent ¶
func (e *Equivalent) Equivalent(_, model string) (prices.Price, bool)
Equivalent returns the direct USD-per-1M-token rates for model when known, else ok=false so the pricer falls back to the daily price catalogue. Missing cache read/write rates default to direct input, matching prices.Catalog.
type Quote ¶
type Quote struct {
Input, Output, CacheRead, CacheWrite *int64
DirectInput, DirectOutput, DirectCacheRead, DirectCacheWrite *int64
HealthySellers int
DiscountPct *float64
Trend *Trend
}
Quote holds the best trusted-seller prices and direct reference prices for one model, in micro-USD per 1M tokens (nil = not offered).
type SettleFunc ¶
SettleFunc records a late actual charge for a previously unpriced log row.
type Worker ¶
type Worker struct {
// contains filtered or unexported fields
}
Worker periodically reads /api/markets, per-provider /v1/payments/balance, and daily /v1/models from Surplus, keeping the last good snapshot on failure.
func NewWorker ¶
func NewWorker(cfg Config, providers func(context.Context) ([]Account, error), log *slog.Logger) *Worker
NewWorker returns a Worker configured with cfg. A nil log discards.
func (*Worker) IsReady ¶ added in v0.11.0
IsReady reports whether at least one /api/markets snapshot has been loaded.
func (*Worker) LastError ¶
LastError returns the error from the most recent /api/markets read, or "".
func (*Worker) MarketStatus ¶
MarketStatus atomically returns the last good snapshot, its fetch time, and the last market error.
func (*Worker) Markets ¶
Markets returns a copy of the last good market snapshot and its fetch time.
func (*Worker) OnUpdate ¶ added in v0.11.0
func (w *Worker) OnUpdate(fn func())
OnUpdate registers fn to run after each successful /api/markets refresh, outside the worker lock.
func (*Worker) Ready ¶
func (w *Worker) Ready() <-chan struct{}
Ready returns a channel that closes once after the first successful /api/markets read.
func (*Worker) RefreshBalance ¶
RefreshBalance immediately reads /v1/payments/balance for provider when it is a configured Surplus provider, updating the cached balance and firing OnCredit if credit > 0.