common

package
v2.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2026 License: Apache-2.0 Imports: 42 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ReqReservedHeaders = []string{
	"authorization", "x-api-key", "api-key", "x-goog-api-key",
	"host", "connection", "keep-alive", "transfer-encoding", "upgrade",
	"content-length", "content-type", "trace-id",
}

请求保留头(不透传)

View Source
var ResReservedHeaders = []string{
	"transfer-encoding", "connection", "keep-alive",
	"content-length", "content-encoding", "set-cookie", "strict-transport-security",
}

响应保留头(不透传)

Functions

func AfterHandler

func AfterHandler(ctx context.Context, mak *MAK, after *mcommon.AfterHandler)

后置处理器

func BeforeHandler

func BeforeHandler(ctx context.Context, before *mcommon.BeforeHandler)

前置处理器

func Billing

func Billing(ctx context.Context, mak *MAK, billingData *common.BillingData, billingItems ...string) (spend common.Spend)

计算花费

func CheckIp

func CheckIp(ctx context.Context, ipWhitelist, ipBlacklist []string) error

func ConvChatCompletionsToResponsesRequest

func ConvChatCompletionsToResponsesRequest(ctx context.Context, body []byte) smodel.OpenAIResponsesReq

func ConvQuota

func ConvQuota(quota int, n ...int) float64

转换额度单位

func ConvResponsesToChatCompletionsRequest

func ConvResponsesToChatCompletionsRequest(request *ghttp.Request, isChatCompletions bool) smodel.ChatCompletionRequest

func DefaultLogUserPrivacy added in v2.4.0

func DefaultLogUserPrivacy(logPrivacy *mcommon.LogPrivacy) *mcommon.UserPrivacy

func DefaultUserPrivacy added in v2.4.0

func DefaultUserPrivacy() *mcommon.UserPrivacy

func GetProviderCode

func GetProviderCode(ctx context.Context, providerId string) string

func GetResolvedSessionKeepConfig added in v2.3.0

func GetResolvedSessionKeepConfig(mak *MAK) *mcommon.ModelAgentSessionKeep

func HandleSessionKeepFailure added in v2.3.0

func HandleSessionKeepFailure(ctx context.Context, mak *MAK)

会话保持失败处理

func HandleSessionKeepSuccess added in v2.3.0

func HandleSessionKeepSuccess(ctx context.Context, mak *MAK)

会话保持成功处理

func IsAborted

func IsAborted(err error) bool

func IsMaxRetry

func IsMaxRetry(isEnableModelAgent bool, agentTotal, keyTotal, retry int) bool

func IsNeedRetry

func IsNeedRetry(err error) (isRetry bool, isDisabled bool)

func MatchTimeRule added in v2.2.0

func MatchTimeRule(ctx context.Context, rules []*common.TimeRule, model ...*model.Model) *common.TimeRule

func New

func New() service.ICommon

func NewAdapter

func NewAdapter(ctx context.Context, mak *MAK, isLong bool) (adapter sdk.Adapter)

func NewAdapterOfficial

func NewAdapterOfficial(ctx context.Context, mak *MAK, isLong bool) (adapter sdk.AdapterOfficial)

func NewAdapterOpenAI

func NewAdapterOpenAI(ctx context.Context, mak *MAK, isLong bool) *openai.OpenAI

func NewConverter

func NewConverter(ctx context.Context, provider string) sdk.Converter

func NewModerationClient

func NewModerationClient(ctx context.Context, m *model.Model, key, baseUrl, path string, passthrough *EffectivePassthrough) *sdk.ModerationClient

func NewRealtimeClient

func NewRealtimeClient(ctx context.Context, model *model.Model, key, baseUrl, path string) *sdk.RealtimeClient

func NormalizeUserPrivacy added in v2.4.0

func NormalizeUserPrivacy(privacy *mcommon.UserPrivacy, logPrivacy *mcommon.LogPrivacy) *mcommon.UserPrivacy

func RecordSpend

func RecordSpend(ctx context.Context, spend common.Spend, mak *MAK) error

记录花费

func TokensFromMessages

func TokensFromMessages(ctx context.Context, model string, messages []smodel.ChatCompletionMessage) int

func TokensFromString

func TokensFromString(ctx context.Context, model, text string) int

func WritePassthroughHeaders added in v2.4.0

func WritePassthroughHeaders(ctx context.Context, pt *EffectivePassthrough, headers http.Header)

WritePassthroughHeaders 将上游响应头透传写入客户端响应

Types

type EffectivePassthrough added in v2.4.0

type EffectivePassthrough struct {
	ReqParams     []string // 请求透传参数
	ResParams     []string // 响应透传参数
	ReqHeaderMode int      // 请求头透传模式[1:全量, 2:指定]
	ReqHeaderList []string // 请求头透传白名单
	ResHeaderMode int      // 响应头透传模式[1:全量, 2:指定]
	ResHeaderList []string // 响应头透传白名单
}

有效透传配置(Model ∩ ModelAgent 交集)

func GetEffectivePassthrough added in v2.4.0

func GetEffectivePassthrough(ctx context.Context, m *model.Model, agent *model.ModelAgent) *EffectivePassthrough

计算有效的透传配置, 无ModelAgent时使用Model配置, 有ModelAgent时取交集

type MAK

type MAK struct {
	Provider           string
	Model              string
	Messages           []smodel.ChatCompletionMessage
	ReqModel           *model.Model
	RealModel          *model.Model
	ModelAgent         *model.ModelAgent
	Key                *model.Key
	FallbackModelAgent *model.ModelAgent
	FallbackModel      *model.Model
	AgentTotal         int
	KeyTotal           int
	RealKey            string
	BaseUrl            string
	Path               string
	User               *model.User
	App                *model.App
	AppKey             *model.AppKey
	Group              *model.Group
	Passthrough        *EffectivePassthrough // 有效透传配置
}

func (*MAK) InitMAK

func (mak *MAK) InitMAK(ctx context.Context, retry ...int) (err error)

Jump to

Keyboard shortcuts

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