biz

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2026 License: Apache-2.0 Imports: 39 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RedisKeyConfigModelVersions      = "aigw:config:model_versions"
	RedisKeyConfigAliasPrefix        = "aigw:config:alias:"
	RedisKeyConfigModelAliasesPrefix = "aigw:config:model_aliases:"
)

Variables

This section is empty.

Functions

func ClearGatewayConfigCache

func ClearGatewayConfigCache()

ClearGatewayConfigCache 主动清除所有网关缓存项

func MergeOAuthAccountHeader added in v0.3.0

func MergeOAuthAccountHeader(headers map[string]string, provider *schema.Provider, authType string) map[string]string

MergeOAuthAccountHeader injects Chatgpt-Account-Id for oauth_token endpoints when the provider OAuth credential carries an account_id. Existing header values win.

Types

type BillingPolicy

type BillingPolicy struct {
	InputPrice         float64 `json:"input_price"`
	OutputPrice        float64 `json:"output_price"`
	CachedPrice        float64 `json:"cached_price"`
	CacheCreationPrice float64 `json:"cache_creation_price"`
}

type ConfigRedisSync

type ConfigRedisSync struct {
	RedisClient   *redis.Client
	EndpointDAL   *dal.Endpoint
	ModelDAL      *dal.Model
	ModelAliasDAL *dal.ModelAlias
}

func (*ConfigRedisSync) DeleteAlias

func (s *ConfigRedisSync) DeleteAlias(ctx context.Context, alias string) error

DeleteAlias removes a single alias mapping from Redis and updates the reverse index.

func (*ConfigRedisSync) GetModelCodesByProvider

func (s *ConfigRedisSync) GetModelCodesByProvider(ctx context.Context, providerID string) ([]string, error)

GetModelCodesByProvider queries model codes referencing the given provider ID.

func (*ConfigRedisSync) SyncAlias

func (s *ConfigRedisSync) SyncAlias(ctx context.Context, alias string, modelCode string) error

SyncAlias synchronizes a single alias mapping to Redis: aigw:config:alias:{alias} → modelCode. It also maintains the reverse index: aigw:config:model_aliases:{modelCode} → Set[aliases].

func (*ConfigRedisSync) SyncAliasesByModelId

func (s *ConfigRedisSync) SyncAliasesByModelId(ctx context.Context, modelId string, modelCode string, enabled int) error

SyncAliasesByModelId re-syncs all aliases for a given model ID to Redis. If the model is disabled (enabled=0), it deletes all alias keys instead.

func (*ConfigRedisSync) SyncAllToRedis

func (s *ConfigRedisSync) SyncAllToRedis(ctx context.Context) error

SyncAllToRedis synchronizes all active endpoints, model versions, and tenant bindings to Redis.

func (*ConfigRedisSync) SyncModelByCode

func (s *ConfigRedisSync) SyncModelByCode(ctx context.Context, modelCode string) error

func (*ConfigRedisSync) SyncModelCodeChange

func (s *ConfigRedisSync) SyncModelCodeChange(ctx context.Context, modelID, oldModelCode, newModelCode string) error

SyncModelCodeChange handles updating tenant-related cache keys in Redis when a model's code changes.

func (*ConfigRedisSync) SyncModelDisable

func (s *ConfigRedisSync) SyncModelDisable(ctx context.Context, modelID, modelCode string, tenantCodes ...string) error

SyncModelDisable handles removing model code from associated tenants' allowed model sets and deleting provider whitelist caches.

func (*ConfigRedisSync) SyncModelEnable

func (s *ConfigRedisSync) SyncModelEnable(ctx context.Context, modelID, modelCode string) error

SyncModelEnable handles adding model code back to associated tenants' allowed model sets and rebuilding endpoint whitelist caches.

func (*ConfigRedisSync) SyncProviderID

func (s *ConfigRedisSync) SyncProviderID(ctx context.Context, providerID string) error

SyncProviderID synchronizes all models affected by the provider ID.

type DataPermission

type DataPermission struct {
	Trans             *util.Trans
	DataPermissionDAL *dal.DataPermission
}

Data permission management

func (*DataPermission) Create

Create a new data permission in the data access object.

func (*DataPermission) CreateByOwner

func (a *DataPermission) CreateByOwner(ctx context.Context, dataType, dataId, tenant string) error

CreateByOwner creates a data permission record for the resource owner.

func (*DataPermission) Delete

func (a *DataPermission) Delete(ctx context.Context, id string) error

Delete the specified data permission from the data access object.

func (*DataPermission) DeleteByTypeAndDataId

func (a *DataPermission) DeleteByTypeAndDataId(ctx context.Context, dataType, dataId string) error

DeleteByTypeAndDataId deletes data permissions by type and data ID.

func (*DataPermission) Get

Get the specified data permission from the data access object.

func (*DataPermission) HasReadPermission

func (a *DataPermission) HasReadPermission(ctx context.Context, dataType, dataId string) (bool, error)

HasReadPermission checks if the current user has read permission for a specific data item.

func (*DataPermission) Query

Query data permissions from the data access object based on the provided parameters and options.

func (*DataPermission) Update

func (a *DataPermission) Update(ctx context.Context, id string, formItem *schema.DataPermissionForm) error

Update the specified data permission in the data access object.

type Endpoint

type Endpoint struct {
	Trans             *util.Trans
	EndpointDAL       *dal.Endpoint
	DataPermissionBIZ *DataPermission
	ModelDAL          *dal.Model
	ProviderDAL       *dal.Provider
	ConfigRedisSync   *ConfigRedisSync
	RedisClient       *redis.Client
	AuditLogBIZ       *opsBiz.AuditLog
}

Endpoint business logic layer

func (*Endpoint) Create

func (e *Endpoint) Create(ctx context.Context, formItem *schema.EndpointForm) (*schema.Endpoint, error)

Create a new endpoint.

func (*Endpoint) Delete

func (e *Endpoint) Delete(ctx context.Context, id string) error

Delete the specified endpoint.

func (*Endpoint) Get

func (e *Endpoint) Get(ctx context.Context, id string) (*schema.Endpoint, error)

Get the specified endpoint.

func (*Endpoint) Query

Query endpoints.

func (*Endpoint) QueryEndpointsByModelCode

func (e *Endpoint) QueryEndpointsByModelCode(ctx context.Context, modelCode string) (schema.Endpoints, error)

QueryEndpointsByModelCode queries enabled endpoints by model code (for routing). Joins endpoint -> model -> provider and filters all three to enabled + not deleted.

func (*Endpoint) QueryEndpointsByModelID

func (e *Endpoint) QueryEndpointsByModelID(ctx context.Context, modelID string) (schema.Endpoints, error)

QueryEndpointsByModelID queries endpoints associated with a given Model ID (only enabled endpoints).

func (*Endpoint) QueryEndpointsByProviderID

func (e *Endpoint) QueryEndpointsByProviderID(ctx context.Context, providerID string) (schema.Endpoints, error)

QueryEndpointsByProviderID queries endpoints associated with a given Provider ID (only enabled endpoints).

func (*Endpoint) SelectEndpoint

func (e *Endpoint) SelectEndpoint(ctx context.Context, modelCode string) (*schema.Endpoint, error)

SelectEndpoint selects the best enabled endpoint for a given model code, applying priority-based failover and weighted load balancing within the same priority group.

func (*Endpoint) Test

Test 临时测试草稿端点配置

func (*Endpoint) TestByID

func (e *Endpoint) TestByID(ctx context.Context, id string) (*schema.EndpointTestResult, error)

TestByID 测试已保存端点的连通性

func (*Endpoint) ToggleEnabled

func (e *Endpoint) ToggleEnabled(ctx context.Context, id string, formItem *schema.EndpointEnabledForm) error

ToggleEnabled updates only the enabled status of an endpoint and re-syncs the routing config to Redis.

func (*Endpoint) Update

func (e *Endpoint) Update(ctx context.Context, id string, formItem *schema.EndpointForm) error

Update the specified endpoint.

type EndpointConfig

type EndpointConfig struct {
	ID        string            `json:"id"`
	Code      string            `json:"code"`
	Provider  string            `json:"provider"`
	URL       string            `json:"url"`
	RealModel string            `json:"real_model"`
	APIKey    string            `json:"api_key,omitempty"`
	AuthType  string            `json:"auth_type,omitempty"`
	Protocol  string            `json:"protocol,omitempty"`
	Timeout   string            `json:"timeout,omitempty"`
	Priority  int               `json:"priority"`
	Weight    int               `json:"weight"`
	Headers   map[string]string `json:"headers,omitempty"`
	Metadata  map[string]string `json:"metadata,omitempty"`
}

type GatewayConfig

type GatewayConfig struct {
	Models    map[string]ModelConfig    `json:"models"`
	Providers map[string]ProviderConfig `json:"providers"`
	Fallbacks map[string][]string       `json:"fallbacks"`
	Aliases   map[string]string         `json:"aliases,omitempty"`
}

type GatewaySync

type GatewaySync struct {
	DB            *gorm.DB
	EndpointDAL   *dal.Endpoint
	ModelDAL      *dal.Model
	ModelAliasDAL *dal.ModelAlias
}

func (*GatewaySync) GetGatewayApiKeys

func (s *GatewaySync) GetGatewayApiKeys(ctx context.Context, apiKey string) ([]HTTPApiKeyItem, error)

GetGatewayApiKeys 获取 API 密钥配置(支持单 key 查询)

func (*GatewaySync) GetGatewayConfig

func (s *GatewaySync) GetGatewayConfig(ctx context.Context, modelCode string) (*GatewayConfig, error)

GetGatewayConfig 获取大模型、端点及 Provider 路由配置

func (*GatewaySync) GetGatewayPolicies

func (s *GatewaySync) GetGatewayPolicies(ctx context.Context, modelCode string) ([]HTTPPolicyItem, error)

GetGatewayPolicies 获取治理策略及计费策略

type HTTPApiKeyItem

type HTTPApiKeyItem struct {
	APIKey      string `json:"api_key"`
	UserID      string `json:"user_id"`
	Tenant      string `json:"tenant"`
	WorkspaceID string `json:"workspace_id"`
	UserTenant  string `json:"user_tenant"`
	Status      int    `json:"status"`
	Credits     int64  `json:"credits"`
	ExpiresAt   int64  `json:"expires_at"`
}

type HTTPPolicyItem

type HTTPPolicyItem struct {
	Scope string           `json:"scope"` // "user:userID", "tenant:tenantCode", "model:modelCode", "global"
	Model string           `json:"model"` // model_code or "*"
	Value *HTTPPolicyValue `json:"value"`
}

type HTTPPolicyValue

type HTTPPolicyValue struct {
	LoadBalancePolicy    *policySchema.PolicyLoadbalanceForm    `json:"load_balance_policy,omitempty"`
	InvocationPolicy     *policySchema.PolicyInvocationForm     `json:"invocation_policy,omitempty"`
	LimitPolicies        []*policySchema.PolicyLimitForm        `json:"limit_policies,omitempty"`
	RoutePolicies        []*policySchema.PolicyRouteForm        `json:"route_policies,omitempty"`
	CircuitBreakPolicies []*policySchema.PolicyCircuitBreakForm `json:"circuit_break_policies,omitempty"`
	TaggingPolicies      []*policySchema.PolicyTaggingForm      `json:"tagging_policies,omitempty"`
	Billing              *BillingPolicy                         `json:"billing,omitempty"`
}

type Model

type Model struct {
	Trans                 *util.Trans
	ModelDAL              *dal.Model
	DataPermissionBIZ     *DataPermission
	ConfigRedisSync       *ConfigRedisSync
	PolicyRedisSync       *policyBiz.PolicyRedisSync
	PolicyInvocationBIZ   *policyBiz.PolicyInvocation
	PolicyCircuitBreakBIZ *policyBiz.PolicyCircuitBreak
	RedisClient           *redis.Client
	AuditLogBIZ           *opsBiz.AuditLog
}

Model business logic layer

func (*Model) Create

func (m *Model) Create(ctx context.Context, formItem *schema.ModelForm) (*schema.ModelCreateResult, error)

Create a new model.

func (*Model) Delete

func (m *Model) Delete(ctx context.Context, id string) error

Delete the specified model.

func (*Model) Get

func (m *Model) Get(ctx context.Context, id string) (*schema.Model, error)

Get the specified model.

func (*Model) Query

Query models.

func (*Model) Sync

func (m *Model) Sync(ctx context.Context, id string) error

Sync model's Redis cache data.

func (*Model) ToggleEnabled

func (m *Model) ToggleEnabled(ctx context.Context, id string, formItem *schema.ModelEnabledForm) error

ToggleEnabled updates only the enabled status of a model and re-syncs Redis. It replicates the enabled-change side effects of Update: SyncModelByCode plus SyncModelEnable/SyncModelDisable (which handle tenant binding relationships). model_code is not changed by a toggle, so no SyncModelCodeChange is needed.

func (*Model) Update

func (m *Model) Update(ctx context.Context, id string, formItem *schema.ModelForm) error

Update the specified model.

type ModelAlias

type ModelAlias struct {
	Trans           *util.Trans
	ModelAliasDAL   *dal.ModelAlias
	ModelDAL        *dal.Model
	ConfigRedisSync *ConfigRedisSync
}

ModelAlias business logic layer

func (*ModelAlias) Create

func (m *ModelAlias) Create(ctx context.Context, formItem *schema.ModelAliasForm) (*schema.ModelAlias, error)

Create a new model alias.

func (*ModelAlias) Delete

func (m *ModelAlias) Delete(ctx context.Context, id string) error

Delete the specified model alias.

func (*ModelAlias) Get

func (m *ModelAlias) Get(ctx context.Context, id string) (*schema.ModelAlias, error)

Get the specified model alias.

func (*ModelAlias) Query

Query model aliases.

func (*ModelAlias) Update

func (m *ModelAlias) Update(ctx context.Context, id string, formItem *schema.ModelAliasForm) error

Update the specified model alias.

type ModelCatalog

type ModelCatalog struct {
	Trans               *util.Trans
	ModelCatalogDAL     *dal.ModelCatalog
	ModelCatalogI18nDAL *dal.ModelCatalogI18n
	AuditLogBIZ         *opsBiz.AuditLog
}

ModelCatalog business logic layer

func (*ModelCatalog) Create

Create a new model catalog.

func (*ModelCatalog) Delete

func (m *ModelCatalog) Delete(ctx context.Context, modelID string) error

Delete the specified model catalog.

func (*ModelCatalog) Get

func (m *ModelCatalog) Get(ctx context.Context, modelID string) (*schema.ModelCatalog, error)

Get the specified model catalog.

func (*ModelCatalog) GetBySlug

func (m *ModelCatalog) GetBySlug(ctx context.Context, slug string) (*schema.ModelCatalog, error)

GetBySlug gets a model catalog by slug.

func (*ModelCatalog) Publish

func (m *ModelCatalog) Publish(ctx context.Context, modelID string, formItem *schema.ModelCatalogPublishForm) error

Publish publishes a model catalog (sets visibility and published_at).

func (*ModelCatalog) Query

Query model catalogs.

func (*ModelCatalog) QueryPublic

func (m *ModelCatalog) QueryPublic(ctx context.Context, limit int) (schema.ModelCatalogs, error)

QueryPublic queries public available model catalogs.

func (*ModelCatalog) Update

func (m *ModelCatalog) Update(ctx context.Context, modelID string, formItem *schema.ModelCatalogForm) error

Update the specified model catalog.

type ModelCatalogI18n

type ModelCatalogI18n struct {
	Trans               *util.Trans
	ModelCatalogI18nDAL *dal.ModelCatalogI18n
	ModelCatalogDAL     *dal.ModelCatalog
}

ModelCatalogI18n business logic layer

func (*ModelCatalogI18n) BatchUpsert

func (m *ModelCatalogI18n) BatchUpsert(ctx context.Context, formItem *schema.ModelCatalogI18nBatchForm) error

BatchUpsert batch upserts i18n entries for a model.

func (*ModelCatalogI18n) Create

Create creates a new i18n entry.

func (*ModelCatalogI18n) Delete

func (m *ModelCatalogI18n) Delete(ctx context.Context, modelID, locale string) error

Delete deletes a specific i18n entry.

func (*ModelCatalogI18n) Get

func (m *ModelCatalogI18n) Get(ctx context.Context, modelID, locale string) (*schema.ModelCatalogI18n, error)

Get gets a specific i18n entry.

func (*ModelCatalogI18n) Query

Query model catalog i18n entries.

func (*ModelCatalogI18n) QueryByModelID

func (m *ModelCatalogI18n) QueryByModelID(ctx context.Context, modelID string) (schema.ModelCatalogI18ns, error)

QueryByModelID gets all i18n entries for a model.

func (*ModelCatalogI18n) Update

func (m *ModelCatalogI18n) Update(ctx context.Context, modelID, locale string, formItem *schema.ModelCatalogI18nForm) error

Update updates an i18n entry.

type ModelConfig

type ModelConfig struct {
	RequestTypes []string         `json:"request_types"`
	Endpoints    []EndpointConfig `json:"endpoints"`
}

type ModelPriceVersion

type ModelPriceVersion struct {
	Trans                *util.Trans
	ModelPriceVersionDAL *dal.ModelPriceVersion
	ModelCatalogDAL      *dal.ModelCatalog
	AuditLogBIZ          *opsBiz.AuditLog
}

ModelPriceVersion business logic layer

func (*ModelPriceVersion) Create

Create a new model price version.

func (*ModelPriceVersion) Deactivate

func (m *ModelPriceVersion) Deactivate(ctx context.Context, id string) error

Deactivate deactivates a price version.

func (*ModelPriceVersion) Delete

func (m *ModelPriceVersion) Delete(ctx context.Context, id string) error

Delete the specified model price version.

func (*ModelPriceVersion) Get

Get the specified model price version.

func (*ModelPriceVersion) GetCurrentPrice

func (m *ModelPriceVersion) GetCurrentPrice(ctx context.Context, modelID, currency string) (*schema.ModelPriceVersion, error)

GetCurrentPrice gets the currently effective price for a model.

func (*ModelPriceVersion) Query

Query model price versions.

func (*ModelPriceVersion) QueryByModelID

func (m *ModelPriceVersion) QueryByModelID(ctx context.Context, modelID string) (schema.ModelPriceVersions, error)

QueryByModelID queries all price versions for a model.

func (*ModelPriceVersion) Update

Update the specified model price version.

type OAuthStartResult

type OAuthStartResult struct {
	URL      string `json:"url"`
	UserCode string `json:"user_code,omitempty"`
	State    string `json:"state"`
	// Flow is "device_code" (xAI) or "authorization_code" (Codex).
	Flow string `json:"flow,omitempty"`
}

OAuthStartResult carries the details returned to the frontend so it can open the authorization page and finish the flow (poll for xAI, paste callback for Codex).

type OAuthTokenResult

type OAuthTokenResult struct {
	AccessToken             string `json:"access_token"`
	RefreshToken            string `json:"refresh_token"`
	ExpiresIn               int    `json:"expires_in"`
	TokenEndpoint           string `json:"token_endpoint"`
	BaseURL                 string `json:"base_url"`
	AccountID               string `json:"account_id,omitempty"`
	Email                   string `json:"email,omitempty"`
	SubscriptionActiveUntil string `json:"subscription_active_until,omitempty"`
	Provider                string `json:"provider,omitempty"`
}

type Provider

type Provider struct {
	Trans             *util.Trans
	Cache             cachex.Cacher
	ProviderDAL       *dal.Provider
	EndpointDAL       *dal.Endpoint
	DataPermissionBIZ *DataPermission
	ConfigRedisSync   *ConfigRedisSync
	AuditLogBIZ       *opsBiz.AuditLog
}

Provider business logic layer

func (*Provider) CompleteOAuthFlow added in v0.3.0

func (p *Provider) CompleteOAuthFlow(ctx context.Context, formItem *schema.OAuthCompleteForm) (*OAuthTokenResult, error)

CompleteOAuthFlow finishes authorization-code OAuth (Codex) using the pasted callback URL.

func (*Provider) Create

func (p *Provider) Create(ctx context.Context, formItem *schema.ProviderForm) (*schema.Provider, error)

Create a new provider.

func (*Provider) Delete

func (p *Provider) Delete(ctx context.Context, id string) error

Delete the specified provider.

func (*Provider) FetchModels

func (p *Provider) FetchModels(ctx context.Context, providerID string, formItem *schema.FetchModelsForm) (*schema.FetchModelsResult, error)

FetchModels calls the upstream provider's /v1/models API and returns the model list.

func (*Provider) Get

func (p *Provider) Get(ctx context.Context, id string) (*schema.Provider, error)

Get the specified provider.

func (*Provider) GetQuota added in v0.4.0

func (p *Provider) GetQuota(ctx context.Context, providerID string) (*schema.ProviderQuotaResult, error)

GetQuota fetches live upstream usage/quota for an oauth provider (xAI / Codex).

func (*Provider) PollOAuthStatus

func (p *Provider) PollOAuthStatus(ctx context.Context, state string) (*OAuthTokenResult, error)

func (*Provider) Query

func (*Provider) StartOAuthFlow

func (p *Provider) StartOAuthFlow(ctx context.Context, providerName string) (*OAuthStartResult, error)

StartOAuthFlow starts an upstream provider OAuth binding flow. xAI uses device-code; Codex uses authorization-code + PKCE with manual callback paste.

func (*Provider) Update

func (p *Provider) Update(ctx context.Context, id string, formItem *schema.ProviderForm) error

Update the specified provider.

type ProviderConfig

type ProviderConfig struct {
	Protocol   string `json:"protocol"`
	APIKey     string `json:"api_key"`
	Timeout    string `json:"timeout"`
	MaxRetries int    `json:"max_retries"`
}

type ResolvedEndpoint

type ResolvedEndpoint struct {
	ID                 string            `json:"id,omitempty"`
	Code               string            `json:"code,omitempty"`
	Description        string            `json:"description,omitempty"`
	RealModel          string            `json:"real_model"`
	ProviderName       string            `json:"provider_name"`
	ProviderProtocol   string            `json:"provider_protocol"`
	APIKey             string            `json:"api_key"`
	URL                string            `json:"url"`
	Timeout            int64             `json:"timeout"` // 毫秒
	MaxRetries         int               `json:"max_retries"`
	Priority           int               `json:"priority"`
	Weight             int               `json:"weight"`
	Headers            map[string]string `json:"headers,omitempty"`
	Metadata           map[string]string `json:"metadata,omitempty"`
	RequestTypes       []string          `json:"request_types,omitempty"`
	ContextLength      int64             `json:"context_length,omitempty"`
	MaxOutputTokens    int64             `json:"max_output_tokens,omitempty"`
	InputPrice         *float64          `json:"input_price,omitempty"`
	OutputPrice        *float64          `json:"output_price,omitempty"`
	CachedPrice        *float64          `json:"cached_price,omitempty"`
	CacheCreationPrice *float64          `json:"cache_creation_price,omitempty"`
}

type TokenRefresher

type TokenRefresher struct {
	DB         *gorm.DB
	RedisSync  *ConfigRedisSync
	InstanceID string
}

func NewTokenRefresher

func NewTokenRefresher(db *gorm.DB, redisSync *ConfigRedisSync) *TokenRefresher

func (*TokenRefresher) StartCronLoop

func (r *TokenRefresher) StartCronLoop(ctx context.Context)

StartCronLoop starts the background cron loop to refresh expiring OAuth endpoints

Jump to

Keyboard shortcuts

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