mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-11 14:32:36 +00:00
231 lines
7.4 KiB
Go
231 lines
7.4 KiB
Go
package service
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/pkg/xai"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/tidwall/gjson"
|
|
"github.com/tidwall/sjson"
|
|
)
|
|
|
|
func (s *OpenAIGatewayService) forwardGrokResponses(
|
|
ctx context.Context,
|
|
c *gin.Context,
|
|
account *Account,
|
|
body []byte,
|
|
originalModel string,
|
|
reqStream bool,
|
|
startTime time.Time,
|
|
) (*OpenAIForwardResult, error) {
|
|
if account.Type != AccountTypeOAuth {
|
|
return nil, fmt.Errorf("grok account type %s is not supported by subscription forwarding", account.Type)
|
|
}
|
|
|
|
upstreamModel := account.GetMappedModel(originalModel)
|
|
if strings.TrimSpace(upstreamModel) == "" {
|
|
upstreamModel = "grok-4.3"
|
|
}
|
|
patchedBody, err := patchGrokResponsesBody(body, upstreamModel)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
token, _, err := s.GetAccessToken(ctx, account)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
upstreamCtx, releaseUpstreamCtx := detachUpstreamContext(ctx)
|
|
defer releaseUpstreamCtx()
|
|
upstreamReq, err := buildGrokResponsesRequest(upstreamCtx, c, account, patchedBody, token)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
proxyURL := ""
|
|
if account.ProxyID != nil && account.Proxy != nil {
|
|
proxyURL = account.Proxy.URL()
|
|
}
|
|
|
|
upstreamStart := time.Now()
|
|
resp, err := s.httpUpstream.Do(upstreamReq, proxyURL, account.ID, account.Concurrency)
|
|
SetOpsLatencyMs(c, OpsUpstreamLatencyMsKey, time.Since(upstreamStart).Milliseconds())
|
|
if err != nil {
|
|
return nil, s.handleOpenAIUpstreamTransportError(ctx, c, account, err, false)
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
if resp.StatusCode >= 400 {
|
|
respBody := s.readUpstreamErrorBody(resp)
|
|
resp.Body = io.NopCloser(bytes.NewReader(respBody))
|
|
s.updateGrokUsageSnapshot(ctx, account.ID, xai.ParseQuotaHeaders(resp.Header, resp.StatusCode))
|
|
upstreamMsg := sanitizeUpstreamErrorMessage(extractUpstreamErrorMessage(respBody))
|
|
if upstreamMsg == "" {
|
|
upstreamMsg = fmt.Sprintf("xAI upstream returned status %d", resp.StatusCode)
|
|
}
|
|
appendOpsUpstreamError(c, OpsUpstreamErrorEvent{
|
|
Platform: account.Platform,
|
|
AccountID: account.ID,
|
|
AccountName: account.Name,
|
|
UpstreamStatusCode: resp.StatusCode,
|
|
UpstreamRequestID: firstNonEmpty(resp.Header.Get("x-request-id"), resp.Header.Get("xai-request-id")),
|
|
Kind: "failover",
|
|
Message: upstreamMsg,
|
|
})
|
|
s.handleGrokAccountUpstreamError(ctx, account, resp.StatusCode, resp.Header, respBody)
|
|
if s.shouldFailoverUpstreamError(resp.StatusCode) {
|
|
return nil, &UpstreamFailoverError{
|
|
StatusCode: resp.StatusCode,
|
|
ResponseBody: respBody,
|
|
RetryableOnSameAccount: account.IsPoolMode() && account.IsPoolModeRetryableStatus(resp.StatusCode),
|
|
}
|
|
}
|
|
return s.handleErrorResponse(ctx, resp, c, account, patchedBody, upstreamModel)
|
|
}
|
|
|
|
s.updateGrokUsageSnapshot(ctx, account.ID, xai.ParseQuotaHeaders(resp.Header, resp.StatusCode))
|
|
|
|
var usage *OpenAIUsage
|
|
var firstTokenMs *int
|
|
responseID := ""
|
|
if reqStream {
|
|
streamResult, err := s.handleStreamingResponse(ctx, resp, c, account, startTime, originalModel, upstreamModel)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
usage = streamResult.usage
|
|
firstTokenMs = streamResult.firstTokenMs
|
|
responseID = strings.TrimSpace(streamResult.responseID)
|
|
} else {
|
|
nonStreamResult, err := s.handleNonStreamingResponse(ctx, resp, c, account, originalModel, upstreamModel)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
usage = nonStreamResult.usage
|
|
responseID = strings.TrimSpace(nonStreamResult.responseID)
|
|
}
|
|
|
|
if usage == nil {
|
|
usage = &OpenAIUsage{}
|
|
}
|
|
return &OpenAIForwardResult{
|
|
RequestID: firstNonEmpty(resp.Header.Get("x-request-id"), resp.Header.Get("xai-request-id")),
|
|
ResponseID: responseID,
|
|
Usage: *usage,
|
|
Model: originalModel,
|
|
UpstreamModel: upstreamModel,
|
|
ReasoningEffort: ptrStringOrNil(normalizeOpenAIReasoningEffort(gjson.GetBytes(patchedBody, "reasoning.effort").String())),
|
|
Stream: reqStream,
|
|
OpenAIWSMode: false,
|
|
ResponseHeaders: resp.Header.Clone(),
|
|
Duration: time.Since(startTime),
|
|
FirstTokenMs: firstTokenMs,
|
|
}, nil
|
|
}
|
|
|
|
func patchGrokResponsesBody(body []byte, upstreamModel string) ([]byte, error) {
|
|
if !json.Valid(body) {
|
|
return nil, fmt.Errorf("invalid json request body")
|
|
}
|
|
out, err := sjson.SetBytes(body, "model", upstreamModel)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, unsupportedField := range []string{"prompt_cache_retention", "safety_identifier"} {
|
|
if gjson.GetBytes(out, unsupportedField).Exists() {
|
|
out, err = sjson.DeleteBytes(out, unsupportedField)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func buildGrokResponsesRequest(ctx context.Context, c *gin.Context, account *Account, body []byte, token string) (*http.Request, error) {
|
|
targetURL, err := xai.BuildResponsesURL(account.GetGrokBaseURL())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, targetURL, bytes.NewReader(body))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Accept", "application/json, text/event-stream")
|
|
req.Header.Set("User-Agent", "sub2api-grok/1.0")
|
|
if c != nil {
|
|
if v := c.GetHeader("OpenAI-Beta"); strings.TrimSpace(v) != "" {
|
|
req.Header.Set("OpenAI-Beta", v)
|
|
}
|
|
}
|
|
return req, nil
|
|
}
|
|
|
|
func (s *OpenAIGatewayService) updateGrokUsageSnapshot(ctx context.Context, accountID int64, snapshot *xai.QuotaSnapshot) {
|
|
if s == nil || s.accountRepo == nil || accountID <= 0 || snapshot == nil {
|
|
return
|
|
}
|
|
if s.codexSnapshotThrottle != nil && !s.codexSnapshotThrottle.Allow(accountID, time.Now()) {
|
|
return
|
|
}
|
|
_ = s.accountRepo.UpdateExtra(ctx, accountID, map[string]any{
|
|
grokQuotaSnapshotExtraKey: snapshot,
|
|
})
|
|
}
|
|
|
|
func (s *OpenAIGatewayService) handleGrokAccountUpstreamError(ctx context.Context, account *Account, statusCode int, headers http.Header, responseBody []byte) {
|
|
if s == nil || account == nil {
|
|
return
|
|
}
|
|
switch statusCode {
|
|
case http.StatusUnauthorized:
|
|
s.tempUnscheduleGrok(ctx, account, 10*time.Minute, "grok oauth token unauthorized")
|
|
case http.StatusForbidden:
|
|
s.tempUnscheduleGrok(ctx, account, 30*time.Minute, "grok entitlement or subscription tier denied")
|
|
case http.StatusTooManyRequests:
|
|
cooldown := 2 * time.Minute
|
|
if snapshot := xai.ParseQuotaHeaders(headers, statusCode); snapshot != nil && snapshot.RetryAfterSeconds != nil && *snapshot.RetryAfterSeconds > 0 {
|
|
cooldown = time.Duration(*snapshot.RetryAfterSeconds) * time.Second
|
|
}
|
|
s.tempUnscheduleGrok(ctx, account, cooldown, "grok rate limited")
|
|
default:
|
|
if statusCode >= 500 {
|
|
s.tempUnscheduleGrok(ctx, account, 2*time.Minute, "grok upstream temporary error")
|
|
}
|
|
}
|
|
_ = responseBody
|
|
}
|
|
|
|
func (s *OpenAIGatewayService) tempUnscheduleGrok(ctx context.Context, account *Account, cooldown time.Duration, reason string) {
|
|
if s == nil || account == nil {
|
|
return
|
|
}
|
|
until := time.Now().Add(cooldown)
|
|
if account.TempUnschedulableUntil != nil && account.TempUnschedulableUntil.After(until) {
|
|
until = *account.TempUnschedulableUntil
|
|
}
|
|
s.BlockAccountScheduling(account, until, reason)
|
|
if s.accountRepo != nil {
|
|
stateCtx, cancel := openAIAccountStateContext(ctx)
|
|
defer cancel()
|
|
_ = s.accountRepo.SetTempUnschedulable(stateCtx, account.ID, until, reason)
|
|
}
|
|
}
|
|
|
|
func ptrStringOrNil(value string) *string {
|
|
if strings.TrimSpace(value) == "" {
|
|
return nil
|
|
}
|
|
return &value
|
|
}
|