| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- package handler
- import (
- "context"
- "errors"
- "net/http"
- "strconv"
- "strings"
- "time"
- pkghttputil "github.com/Wei-Shaw/sub2api/internal/pkg/httputil"
- "github.com/Wei-Shaw/sub2api/internal/pkg/ip"
- "github.com/Wei-Shaw/sub2api/internal/pkg/logger"
- middleware2 "github.com/Wei-Shaw/sub2api/internal/server/middleware"
- "github.com/Wei-Shaw/sub2api/internal/service"
- "github.com/gin-gonic/gin"
- "github.com/tidwall/gjson"
- "go.uber.org/zap"
- )
- // Embeddings handles the OpenAI-compatible Embeddings API.
- // POST /v1/embeddings
- func (h *OpenAIGatewayHandler) Embeddings(c *gin.Context) {
- streamStarted := false
- requestStart := time.Now()
- apiKey, ok := middleware2.GetAPIKeyFromContext(c)
- if !ok {
- h.errorResponse(c, http.StatusUnauthorized, "authentication_error", "Invalid API key")
- return
- }
- subject, ok := middleware2.GetAuthSubjectFromContext(c)
- if !ok {
- h.errorResponse(c, http.StatusInternalServerError, "api_error", "User context not found")
- return
- }
- reqLog := requestLogger(
- c,
- "handler.openai_gateway.embeddings",
- zap.Int64("user_id", subject.UserID),
- zap.Int64("api_key_id", apiKey.ID),
- zap.Any("group_id", apiKey.GroupID),
- )
- if !h.ensureResponsesDependencies(c, reqLog) {
- return
- }
- body, err := pkghttputil.ReadRequestBodyWithPrealloc(c.Request)
- if err != nil {
- if maxErr, ok := extractMaxBytesError(err); ok {
- h.errorResponse(c, http.StatusRequestEntityTooLarge, "invalid_request_error", buildBodyTooLargeMessage(maxErr.Limit))
- return
- }
- h.errorResponse(c, http.StatusBadRequest, "invalid_request_error", "Failed to read request body")
- return
- }
- if len(body) == 0 {
- h.errorResponse(c, http.StatusBadRequest, "invalid_request_error", "Request body is empty")
- return
- }
- if !gjson.ValidBytes(body) {
- logRequestBodyParseFailure(reqLog, body, nil)
- h.errorResponse(c, http.StatusBadRequest, "invalid_request_error", "Failed to parse request body")
- return
- }
- modelResult := gjson.GetBytes(body, "model")
- if !modelResult.Exists() || modelResult.Type != gjson.String || strings.TrimSpace(modelResult.String()) == "" {
- h.errorResponse(c, http.StatusBadRequest, "invalid_request_error", "model is required")
- return
- }
- reqModel := modelResult.String()
- ensureCompositeTargetPlatform(c, apiKey, reqModel)
- if !compositeTargetPlatformAllowed(c, apiKey, reqModel, service.PlatformOpenAI) {
- h.errorResponse(c, http.StatusBadRequest, "invalid_request_error", "Model is not supported by this OpenAI-compatible endpoint for composite groups")
- return
- }
- reqLog = reqLog.With(zap.String("model", reqModel))
- setOpsRequestContext(c, reqModel, false)
- setOpsEndpointContext(c, "", int16(service.RequestTypeSync))
- if decision := h.checkSecurityAudit(c, reqLog, apiKey, subject, "openai_embeddings", reqModel, body); decision != nil && !decision.AllowNextStage {
- h.openAISecurityAuditError(c, decision)
- return
- }
- channelMapping, _ := h.gatewayService.ResolveChannelMappingAndRestrict(c.Request.Context(), apiKey.GroupID, reqModel)
- forwardModel := openAIChannelForwardModel(channelMapping, reqModel)
- subscription, _ := middleware2.GetSubscriptionFromContext(c)
- service.SetOpsLatencyMs(c, service.OpsAuthLatencyMsKey, time.Since(requestStart).Milliseconds())
- userReleaseFunc, acquired := h.acquireResponsesUserSlot(c, subject.UserID, subject.Concurrency, false, &streamStarted, reqLog)
- if !acquired {
- return
- }
- if userReleaseFunc != nil {
- defer userReleaseFunc()
- }
- if err := h.billingCacheService.CheckBillingEligibility(c.Request.Context(), apiKey.User, apiKey, apiKey.Group, subscription, service.QuotaPlatform(c.Request.Context(), apiKey)); err != nil {
- reqLog.Info("openai_embeddings.billing_check_failed", zap.Error(err))
- status, code, message, retryAfter := billingErrorDetails(err)
- if retryAfter > 0 {
- c.Header("Retry-After", strconv.Itoa(retryAfter))
- }
- h.errorResponse(c, status, code, message)
- return
- }
- profitVetoCount := 0
- failedAccountIDs := make(map[int64]struct{})
- var lastFailoverErr *service.UpstreamFailoverError
- switchCount := 0
- maxAccountSwitches := h.maxAccountSwitches
- if maxAccountSwitches <= 0 {
- maxAccountSwitches = 3
- }
- routingStart := time.Now()
- // 分组利润控制:embeddings 文本入口请求级装门并固定 pricingAt。
- embPricingCtx, pricingAt := h.gatewayService.WithOpenAIRequestPricingContext(c.Request.Context(), apiKey.GroupID)
- c.Request = c.Request.WithContext(embPricingCtx)
- for {
- selection, _, err := h.gatewayService.SelectAccountWithSchedulerForCapability(
- c.Request.Context(),
- apiKey.GroupID,
- "",
- "",
- forwardModel,
- failedAccountIDs,
- service.OpenAIUpstreamTransportHTTPSSE,
- service.OpenAIEndpointCapabilityEmbeddings,
- false,
- false,
- true,
- )
- if err != nil {
- if failoverClientGone(c) {
- reqLog.Info("openai_embeddings.account_select_aborted_client_disconnected", zap.Error(err))
- return
- }
- reqLog.Warn("openai_embeddings.account_select_failed",
- zap.Error(err),
- zap.Int("excluded_account_count", len(failedAccountIDs)),
- )
- if len(failedAccountIDs) == 0 {
- cls := classifyNoAccountErrorFromGin(c, h.gatewayService, apiKey, reqModel, reqModel, service.PlatformOpenAI)
- if !cls.ModelNotFound {
- markOpsRoutingCapacityLimitedIfNoAvailable(c, err)
- }
- h.errorResponse(c, cls.Status, cls.ErrType, cls.Message)
- return
- }
- if lastFailoverErr != nil {
- h.handleFailoverExhausted(c, lastFailoverErr, false)
- } else {
- h.errorResponse(c, http.StatusBadGateway, "api_error", "Upstream request failed")
- }
- return
- }
- if selection == nil || selection.Account == nil {
- cls := classifyNoAccountErrorFromGin(c, h.gatewayService, apiKey, reqModel, reqModel, service.PlatformOpenAI)
- if !cls.ModelNotFound {
- markOpsRoutingCapacityLimited(c)
- }
- h.errorResponse(c, cls.Status, cls.ErrType, cls.Message)
- return
- }
- account := selection.Account
- setOpsSelectedAccount(c, account.ID, account.Platform)
- accountReleaseFunc, slotResult := h.acquireResponsesAccountSlot(c, apiKey.GroupID, "", selection, false, &streamStarted, reqLog)
- if slotResult == openAISlotAcquireProfitVetoed {
- // 利润终检否决:排除该账号重新选号;否决次数达上限则按无可用账号终止。
- if !recordOpenAIProfitVeto(failedAccountIDs, account.ID, &profitVetoCount) {
- h.handleOpenAIProfitVetoExhausted(c, streamStarted, reqLog, profitVetoCount)
- return
- }
- continue
- }
- if slotResult != openAISlotAcquireOK {
- return
- }
- service.SetOpsLatencyMs(c, service.OpsRoutingLatencyMsKey, time.Since(routingStart).Milliseconds())
- forwardStart := time.Now()
- forwardBody := body
- if channelMapping.Mapped {
- forwardBody = h.gatewayService.ReplaceModelInBody(body, channelMapping.MappedModel)
- }
- writerSizeBeforeForward := c.Writer.Size()
- result, err := func() (*service.OpenAIForwardResult, error) {
- defer func() {
- if accountReleaseFunc != nil {
- accountReleaseFunc()
- }
- }()
- return h.gatewayService.ForwardEmbeddings(c.Request.Context(), c, account, forwardBody, "")
- }()
- forwardDurationMs := time.Since(forwardStart).Milliseconds()
- upstreamLatencyMs, _ := getContextInt64(c, service.OpsUpstreamLatencyMsKey)
- responseLatencyMs := forwardDurationMs
- if upstreamLatencyMs > 0 && forwardDurationMs > upstreamLatencyMs {
- responseLatencyMs = forwardDurationMs - upstreamLatencyMs
- }
- service.SetOpsLatencyMs(c, service.OpsResponseLatencyMsKey, responseLatencyMs)
- if err != nil {
- var failoverErr *service.UpstreamFailoverError
- if errors.As(err, &failoverErr) {
- if c.Writer.Size() != writerSizeBeforeForward {
- h.handleFailoverExhausted(c, failoverErr, true)
- return
- }
- h.gatewayService.ReportOpenAIAccountScheduleResult(account, openAIAccountScheduleModel(c, account, reqModel, false, result), false, nil, err)
- if failoverClientGone(c) {
- reqLog.Info("openai_embeddings.failover_aborted_client_disconnected",
- zap.Int64("account_id", account.ID),
- zap.Int("upstream_status", failoverErr.StatusCode),
- )
- return
- }
- h.gatewayService.RecordOpenAIAccountSwitch()
- failedAccountIDs[account.ID] = struct{}{}
- lastFailoverErr = failoverErr
- if switchCount >= maxAccountSwitches {
- h.handleFailoverExhausted(c, failoverErr, false)
- return
- }
- switchCount++
- reqLog.Warn("openai_embeddings.upstream_failover_switching",
- zap.Int64("account_id", account.ID),
- zap.Int("upstream_status", failoverErr.StatusCode),
- zap.Int("switch_count", switchCount),
- zap.Int("max_switches", maxAccountSwitches),
- )
- continue
- }
- h.gatewayService.ReportOpenAIAccountScheduleResult(account, openAIAccountScheduleModel(c, account, reqModel, false, result), false, nil, err)
- if c.Writer.Size() == writerSizeBeforeForward {
- h.errorResponse(c, http.StatusBadGateway, "upstream_error", "Upstream request failed")
- }
- reqLog.Warn("openai_embeddings.forward_failed",
- zap.Int64("account_id", account.ID),
- zap.Error(err),
- )
- return
- }
- h.gatewayService.ReportOpenAIAccountScheduleResult(account, openAIAccountScheduleModel(c, account, reqModel, false, result), true, nil)
- userAgent := c.GetHeader("User-Agent")
- clientIP := ip.GetClientIP(c)
- inboundEndpoint := GetInboundEndpoint(c)
- upstreamEndpoint := GetUpstreamEndpoint(c, account.Platform)
- quotaPlatform := service.QuotaPlatform(c.Request.Context(), apiKey)
- sessionID := service.ExtractClientSessionID(c)
- h.submitOpenAIUsageRecordTask(c.Request.Context(), result, func(ctx context.Context) {
- if err := h.gatewayService.RecordUsage(ctx, &service.OpenAIRecordUsageInput{
- Result: result,
- APIKey: apiKey,
- User: apiKey.User,
- Account: account,
- Subscription: subscription,
- InboundEndpoint: inboundEndpoint,
- UpstreamEndpoint: upstreamEndpoint,
- UserAgent: userAgent,
- IPAddress: clientIP,
- APIKeyService: h.apiKeyService,
- QuotaPlatform: quotaPlatform,
- SessionID: sessionID,
- ChannelUsageFields: clientRequestedUsageFields(c, channelMapping, reqModel, result.UpstreamModel),
- PricingAt: pricingAt,
- }); err != nil {
- logger.L().With(
- zap.String("component", "handler.openai_gateway.embeddings"),
- zap.Int64("user_id", subject.UserID),
- zap.Int64("api_key_id", apiKey.ID),
- zap.Any("group_id", apiKey.GroupID),
- zap.String("model", reqModel),
- zap.Int64("account_id", account.ID),
- ).Error("openai_embeddings.record_usage_failed", zap.Error(err))
- }
- })
- reqLog.Debug("openai_embeddings.request_completed",
- zap.Int64("account_id", account.ID),
- zap.Int("switch_count", switchCount),
- )
- return
- }
- }
|