billing

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 19, 2026 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const PluginName = bconfig.PluginName

Variables

View Source
var (
	BifrostErrVKRequired = &schemas.BifrostError{
		Type:           bifrost.Ptr("bifrost_vk_required"),
		StatusCode:     bifrost.Ptr(401),
		IsBifrostError: true,
		Error: &schemas.ErrorField{
			Message: ("bifrost vk required"),
		},
	}
	BifrostErrVKInvalid = &schemas.BifrostError{
		Type:           bifrost.Ptr("bifrost_vk_invalid"),
		StatusCode:     bifrost.Ptr(403),
		IsBifrostError: true,
		Error: &schemas.ErrorField{
			Message: ("bifrost vk invalid"),
		},
	}
	BifrostErrBillingBudgetExceeded = &schemas.BifrostError{
		Type:           bifrost.Ptr("billing_budget_exceeded"),
		StatusCode:     bifrost.Ptr(403),
		IsBifrostError: true,
		Error: &schemas.ErrorField{
			Message: ("billing budget exceeded"),
		},
	}
)

bifrost errors

Functions

func TriggerMigrations

func TriggerMigrations(ctx context.Context, store configstore.ConfigStore) error

TriggerMigrations runs all migrations including schema init. It is safe to call multiple times (e.g. after plugin upgrade) — migrations are idempotent via gormigrate's migration ID deduplication.

Types

type BillingBudgetChecker

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

BillingBudgetChecker 实现 OR 逻辑的 billing budget 检查和扣费

func NewBillingBudgetChecker

func NewBillingBudgetChecker(budgetStore store.BudgetStore, logger schemas.Logger) *BillingBudgetChecker

NewBillingBudgetChecker 创建新的 BillingBudgetChecker

func (*BillingBudgetChecker) Check

func (c *BillingBudgetChecker) Check(ctx context.Context, entityWiseBudgets store.EntityWiseBudgets, baselines map[string]float64) (Decision, error)

Check 实现 OR 逻辑:任意 budget 有余额则允许

func (*BillingBudgetChecker) Deduct

func (c *BillingBudgetChecker) Deduct(ctx context.Context, entityWiseBudgets store.EntityWiseBudgets, cost float64) (map[string]float64, error)

Deduct 实现混合扣费逻辑(通过 BudgetStore 原子操作)

type BillingCheckRequest

type BillingCheckRequest struct {
	VirtualKey string                `json:"virtual_key"`
	Provider   schemas.ModelProvider `json:"provider"`
	Model      string                `json:"model"`
	UserID     string                `json:"user_id,omitempty"`
}

BillingCheckRequest 包含 billing check 的上下文

type BillingCheckResult

type BillingCheckResult struct {
	Decision     Decision            `json:"decision"`
	Reason       string              `json:"reason,omitempty"`
	BudgetInfos  []*store.BudgetData `json:"budget_infos,omitempty"`
	TotalBalance float64             `json:"total_balance,omitempty"`
}

BillingCheckResult 包含 billing check 的结果

type BillingPlugin

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

BillingPlugin 实现 LLMPlugin 接口,提供 billing budget 的检查和扣费

func NewBillingPlugin

func NewBillingPlugin(
	config *lib.Config,
	logger schemas.Logger,
	modelCatalog *modelcatalog.ModelCatalog,
) (*BillingPlugin, error)

NewBillingPlugin 创建 billing 插件

func (*BillingPlugin) Cleanup

func (p *BillingPlugin) Cleanup() error

func (*BillingPlugin) GetBudgetStore

func (p *BillingPlugin) GetBudgetStore() store.BudgetStore

GetBudgetStore 返回 BudgetStore(供 handler 和 service 使用)

func (*BillingPlugin) GetConfigStore

func (p *BillingPlugin) GetConfigStore() configstore.ConfigStore

GetConfigStore 返回 ConfigStore

func (*BillingPlugin) GetGatewayConfig

func (p *BillingPlugin) GetGatewayConfig() *bpayment.BillingConfig

GetGatewayConfig 返回 payment gateway 配置

func (*BillingPlugin) GetName

func (p *BillingPlugin) GetName() string

func (*BillingPlugin) GetPaymentBudgetStore

func (p *BillingPlugin) GetPaymentBudgetStore() bpayment.BudgetStore

GetPaymentBudgetStore 返回适配了 payment.BudgetStore 接口的 store

func (*BillingPlugin) PostLLMHook

func (*BillingPlugin) RegisterRoutes

func (p *BillingPlugin) RegisterRoutes(r *router.Router, outerMiddlewares ...schemas.BifrostHTTPMiddleware)

type BudgetChecker

type BudgetChecker interface {
	// Check decides whether the request is allowed
	Check(ctx context.Context, entityWiseBudgets store.EntityWiseBudgets, baselines map[string]float64) (Decision, error)

	// Deduct updates usage after a successful request
	// Returns which budget IDs were actually charged and by how much
	Deduct(ctx context.Context, entityWiseBudgets store.EntityWiseBudgets, cost float64) (map[string]float64, error)
}

BudgetChecker defines how a budget type is evaluated and deducted

type BudgetWithInfo

type BudgetWithInfo struct {
	Budget       *store.BudgetData
	IsPackage    bool    // 是否为套餐类型(有 ExpiresAt)
	DurationSecs float64 // 套餐时长(秒),用于排序
	Remaining    float64 // 剩余额度
}

BudgetWithInfo 扁平化的 budget + 计算信息(用于 checker 排序)

type Decision

type Decision string

Decision 表示 billing 评估结果

const (
	DecisionAllow          Decision = "allow"
	DecisionBudgetExceeded Decision = "budget_exceeded"
)

type OffPeakDiscount

type OffPeakDiscount struct {
	Percentage float64 `json:"percentage"`
	StartHour  int     `json:"start_hour,omitempty"`
	EndHour    int     `json:"end_hour,omitempty"`
	Days       []int   `json:"days,omitempty"`
	Timezone   string  `json:"timezone,omitempty"`
}

OffPeakDiscount 表示折扣规则(从 EntityPackage/Package JSON 解析)

type OffPeakDiscountRule

type OffPeakDiscountRule struct {
	StartHour int     `json:"start_hour"` // 0-23
	EndHour   int     `json:"end_hour"`   // 0-23
	Discount  float64 `json:"discount"`   // 0.0-1.0 (e.g., 0.5 means 50% off)
}

OffPeakDiscountRule 表示折扣规则

type UsageTracker

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

UsageTracker 管理 billing budget 的后台 worker 负责:定期 reset 过期 budget、dump 到 DB、刷新 VK/Team/Customer 缓存

func NewUsageTracker

func NewUsageTracker(budgetStore store.BudgetStore, configStore configstore.ConfigStore, logger schemas.Logger, dumpInterval time.Duration) *UsageTracker

NewUsageTracker 创建 UsageTracker

func (*UsageTracker) DumpBudgets

func (t *UsageTracker) DumpBudgets(ctx context.Context) error

DumpBudgets 将 Redis 中的 budget 数据增量写入 DB

func (*UsageTracker) Start

func (t *UsageTracker) Start(ctx context.Context)

Start 启动后台 worker

func (*UsageTracker) Stop

func (t *UsageTracker) Stop()

Stop 停止后台 worker

Directories

Path Synopsis
internal
handlers
Package handlers provides HTTP handlers for the Bifrost gateway.
Package handlers provides HTTP handlers for the Bifrost gateway.
handlers/authn
Package handlers provides HTTP request handlers for the Bifrost HTTP transport.
Package handlers provides HTTP request handlers for the Bifrost HTTP transport.
services/payment
Package payment provides payment gateway abstractions and billing service logic.
Package payment provides payment gateway abstractions and billing service logic.
pkg

Jump to

Keyboard shortcuts

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