mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-26 23:27:22 +00:00
321 lines
11 KiB
Go
321 lines
11 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/pkg/apicompat"
|
|
"github.com/Wei-Shaw/sub2api/internal/pkg/xai"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
const (
|
|
grokChatResponsesEndpoint = "/v1/responses"
|
|
grokChatRawEndpoint = "/v1/chat/completions"
|
|
)
|
|
|
|
var grokChatResponsesBridgeTopLevelFields = map[string]struct{}{
|
|
"model": {},
|
|
"messages": {},
|
|
"stream": {},
|
|
"stream_options": {},
|
|
"max_tokens": {},
|
|
"max_completion_tokens": {},
|
|
"temperature": {},
|
|
"top_p": {},
|
|
"prompt_cache_key": {},
|
|
"tools": {},
|
|
"tool_choice": {},
|
|
"functions": {},
|
|
"function_call": {},
|
|
}
|
|
|
|
// grokChatResponsesBridgeEligibility deliberately accepts only request shapes
|
|
// whose Chat Completions semantics are preserved by the Responses bridge.
|
|
// Everything else stays on raw Chat Completions rather than being silently
|
|
// dropped or rewritten.
|
|
func grokChatResponsesBridgeEligibility(body []byte) (bool, string) {
|
|
var root map[string]json.RawMessage
|
|
if err := json.Unmarshal(body, &root); err != nil || root == nil {
|
|
return false, "invalid_json"
|
|
}
|
|
|
|
for _, field := range []string{"stop", "reasoning_effort"} {
|
|
if _, exists := root[field]; exists {
|
|
return false, "unsupported_" + field
|
|
}
|
|
}
|
|
for _, field := range []string{"tools", "functions"} {
|
|
if raw, exists := root[field]; exists && !grokChatNullOrEmptyArray(raw) {
|
|
return false, "unsupported_" + field
|
|
}
|
|
}
|
|
if raw, exists := root["tool_choice"]; exists && !grokChatNullOrNone(raw) {
|
|
return false, "unsupported_tool_choice"
|
|
}
|
|
if raw, exists := root["function_call"]; exists && !grokChatNullOrNone(raw) {
|
|
return false, "unsupported_function_call"
|
|
}
|
|
for field := range root {
|
|
if _, supported := grokChatResponsesBridgeTopLevelFields[field]; !supported {
|
|
return false, "unknown_field_" + field
|
|
}
|
|
}
|
|
|
|
var model string
|
|
if raw, ok := root["model"]; !ok || json.Unmarshal(raw, &model) != nil || strings.TrimSpace(model) == "" {
|
|
return false, "invalid_model"
|
|
}
|
|
|
|
if raw, ok := root["stream"]; ok {
|
|
var stream *bool
|
|
if json.Unmarshal(raw, &stream) != nil || stream == nil {
|
|
return false, "invalid_stream"
|
|
}
|
|
}
|
|
if raw, ok := root["stream_options"]; ok {
|
|
var options map[string]json.RawMessage
|
|
if json.Unmarshal(raw, &options) != nil || options == nil {
|
|
return false, "invalid_stream_options"
|
|
}
|
|
for field, value := range options {
|
|
if field != "include_usage" {
|
|
return false, "unknown_stream_option_" + field
|
|
}
|
|
var includeUsage *bool
|
|
if json.Unmarshal(value, &includeUsage) != nil || includeUsage == nil {
|
|
return false, "invalid_stream_include_usage"
|
|
}
|
|
}
|
|
}
|
|
|
|
for _, field := range []string{"max_tokens", "max_completion_tokens"} {
|
|
if raw, ok := root[field]; ok {
|
|
var value *int
|
|
if json.Unmarshal(raw, &value) != nil || value == nil || *value < 128 {
|
|
return false, "unsafe_" + field
|
|
}
|
|
}
|
|
}
|
|
if _, hasMaxTokens := root["max_tokens"]; hasMaxTokens {
|
|
if _, hasMaxCompletionTokens := root["max_completion_tokens"]; hasMaxCompletionTokens {
|
|
return false, "conflicting_max_tokens"
|
|
}
|
|
}
|
|
for _, field := range []string{"temperature", "top_p"} {
|
|
if raw, ok := root[field]; ok {
|
|
var value *float64
|
|
if json.Unmarshal(raw, &value) != nil || value == nil {
|
|
return false, "invalid_" + field
|
|
}
|
|
}
|
|
}
|
|
if raw, ok := root["prompt_cache_key"]; ok {
|
|
var key string
|
|
if json.Unmarshal(raw, &key) != nil {
|
|
return false, "invalid_prompt_cache_key"
|
|
}
|
|
}
|
|
|
|
var messages []map[string]json.RawMessage
|
|
rawMessages, ok := root["messages"]
|
|
if !ok || json.Unmarshal(rawMessages, &messages) != nil || len(messages) == 0 {
|
|
return false, "invalid_messages"
|
|
}
|
|
for _, message := range messages {
|
|
for field := range message {
|
|
if field != "role" && field != "content" {
|
|
return false, "unsafe_message_field_" + field
|
|
}
|
|
}
|
|
var role string
|
|
if raw, exists := message["role"]; !exists || json.Unmarshal(raw, &role) != nil {
|
|
return false, "invalid_message_role"
|
|
}
|
|
switch role {
|
|
case "system", "user", "assistant":
|
|
default:
|
|
return false, "unsupported_message_role_" + role
|
|
}
|
|
var content string
|
|
if raw, exists := message["content"]; !exists || json.Unmarshal(raw, &content) != nil {
|
|
// Structured content includes image_url and other parts whose exact
|
|
// behavior is not guaranteed by this bridge.
|
|
return false, "non_text_message_content"
|
|
}
|
|
if strings.TrimSpace(content) == "" {
|
|
return false, "empty_message_content"
|
|
}
|
|
}
|
|
|
|
return true, ""
|
|
}
|
|
|
|
func grokChatNullOrEmptyArray(raw json.RawMessage) bool {
|
|
if strings.TrimSpace(string(raw)) == "null" {
|
|
return true
|
|
}
|
|
var values []json.RawMessage
|
|
return json.Unmarshal(raw, &values) == nil && len(values) == 0
|
|
}
|
|
|
|
func grokChatNullOrNone(raw json.RawMessage) bool {
|
|
if strings.TrimSpace(string(raw)) == "null" {
|
|
return true
|
|
}
|
|
var value string
|
|
return json.Unmarshal(raw, &value) == nil && strings.EqualFold(strings.TrimSpace(value), "none")
|
|
}
|
|
|
|
func grokChatCacheIntentBody(body []byte) ([]byte, error) {
|
|
var root map[string]json.RawMessage
|
|
if err := json.Unmarshal(body, &root); err != nil {
|
|
return nil, err
|
|
}
|
|
for _, field := range []string{"tools", "tool_choice", "functions", "function_call"} {
|
|
delete(root, field)
|
|
}
|
|
return json.Marshal(root)
|
|
}
|
|
|
|
func grokChatResponsesRuntimeEligible(upstreamModel, cacheIdentity string) bool {
|
|
return strings.TrimSpace(upstreamModel) == "grok-4.5" && strings.TrimSpace(cacheIdentity) != ""
|
|
}
|
|
|
|
// forwardGrokChatCompletionsViaResponses converts a strictly compatible Chat
|
|
// request into xAI Responses format and reuses the established Responses-to-
|
|
// Chat response translators. It intentionally does not run the Codex OAuth
|
|
// transform because Grok CLI is a separate upstream protocol.
|
|
func (s *OpenAIGatewayService) forwardGrokChatCompletionsViaResponses(
|
|
ctx context.Context,
|
|
c *gin.Context,
|
|
account *Account,
|
|
body []byte,
|
|
promptCacheKey string,
|
|
defaultMappedModel string,
|
|
) (*OpenAIForwardResult, error) {
|
|
startTime := time.Now()
|
|
|
|
var chatReq apicompat.ChatCompletionsRequest
|
|
if err := json.Unmarshal(body, &chatReq); err != nil {
|
|
return nil, fmt.Errorf("parse grok chat completions request: %w", err)
|
|
}
|
|
originalModel := chatReq.Model
|
|
clientStream := chatReq.Stream
|
|
billingModel := resolveOpenAIForwardModel(account, originalModel, defaultMappedModel)
|
|
upstreamModel := normalizeOpenAIModelForUpstream(account, billingModel)
|
|
cacheIdentity := resolveGrokCacheIdentity(c, body, promptCacheKey, upstreamModel)
|
|
if !grokChatResponsesRuntimeEligible(upstreamModel, cacheIdentity) {
|
|
return s.forwardAsRawChatCompletions(ctx, c, account, body, defaultMappedModel)
|
|
}
|
|
|
|
responsesReq, err := apicompat.ChatCompletionsToResponses(&chatReq)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("convert grok chat completions to responses: %w", err)
|
|
}
|
|
responsesReq.Model = upstreamModel
|
|
responsesReq.Stream = true
|
|
// These fields are useful to Codex but are not needed by the Grok CLI
|
|
// protocol. Keep the bridge request as close as possible to native Grok.
|
|
responsesReq.Include = nil
|
|
responsesReq.Store = nil
|
|
|
|
responsesBody, err := json.Marshal(responsesReq)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("marshal grok responses bridge request: %w", err)
|
|
}
|
|
responsesBody, err = patchGrokResponsesBody(responsesBody, upstreamModel)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("patch grok responses bridge request: %w", err)
|
|
}
|
|
intentBody, err := grokChatCacheIntentBody(body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("normalize grok responses bridge tool intent: %w", err)
|
|
}
|
|
responsesBody, err = applyGrokResponsesCacheIdentity(responsesBody, intentBody, cacheIdentity, true)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("apply grok responses bridge cache identity: %w", err)
|
|
}
|
|
|
|
updatedBody, policyErr := s.applyOpenAIFastPolicyToBody(ctx, account, upstreamModel, responsesBody)
|
|
if policyErr != nil {
|
|
var blocked *OpenAIFastBlockedError
|
|
if errors.As(policyErr, &blocked) {
|
|
MarkOpsClientBusinessLimited(c, OpsClientBusinessLimitedReasonLocalPolicyDenied)
|
|
writeChatCompletionsError(c, http.StatusForbidden, "permission_error", blocked.Message)
|
|
}
|
|
return nil, policyErr
|
|
}
|
|
responsesBody = updatedBody
|
|
|
|
token, _, err := s.GetAccessToken(ctx, account)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("get grok access token: %w", err)
|
|
}
|
|
upstreamCtx, releaseUpstreamCtx := detachUpstreamContext(ctx)
|
|
upstreamReq, err := buildGrokResponsesRequest(upstreamCtx, c, account, responsesBody, token, cacheIdentity)
|
|
releaseUpstreamCtx()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("build grok responses bridge request: %w", err)
|
|
}
|
|
SetActualOpenAIUpstreamEndpoint(c, grokChatResponsesEndpoint)
|
|
|
|
proxyURL := ""
|
|
if account.ProxyID != nil && account.Proxy != nil {
|
|
proxyURL = account.Proxy.URL()
|
|
}
|
|
resp, err := s.httpUpstream.Do(upstreamReq, proxyURL, account.ID, account.Concurrency)
|
|
if err != nil {
|
|
return nil, s.handleOpenAIUpstreamTransportError(ctx, c, account, err, false)
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
if resp.StatusCode >= http.StatusBadRequest {
|
|
respBody, upstreamMsg := s.readOpenAIUpstreamError(resp)
|
|
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.handleChatCompletionsErrorResponse(resp, c, account, billingModel)
|
|
}
|
|
|
|
s.updateGrokUsageSnapshot(ctx, account, xai.ParseQuotaHeaders(resp.Header, resp.StatusCode))
|
|
|
|
var result *OpenAIForwardResult
|
|
if clientStream {
|
|
result, err = s.handleChatStreamingResponse(resp, c, account, originalModel, billingModel, upstreamModel, startTime, len(body))
|
|
} else {
|
|
result, err = s.handleChatBufferedStreamingResponse(resp, c, account, originalModel, billingModel, upstreamModel, startTime)
|
|
}
|
|
if result != nil {
|
|
result.UpstreamEndpoint = grokChatResponsesEndpoint
|
|
result.ResponseHeaders = resp.Header.Clone()
|
|
if result.RequestID == "" {
|
|
result.RequestID = firstNonEmpty(resp.Header.Get("x-request-id"), resp.Header.Get("xai-request-id"))
|
|
}
|
|
result.ReasoningEffort = extractOpenAIReasoningEffortFromBody(body, upstreamModel, billingModel, originalModel)
|
|
}
|
|
return result, err
|
|
}
|