| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514 |
- package service
- import (
- "bufio"
- "bytes"
- "context"
- "encoding/json"
- "errors"
- "fmt"
- "io"
- "net/http"
- "strings"
- "time"
- "github.com/Wei-Shaw/sub2api/internal/pkg/apicompat"
- "github.com/Wei-Shaw/sub2api/internal/pkg/claude"
- "github.com/Wei-Shaw/sub2api/internal/pkg/logger"
- "github.com/Wei-Shaw/sub2api/internal/util/responseheaders"
- "github.com/gin-gonic/gin"
- "github.com/tidwall/gjson"
- "go.uber.org/zap"
- )
- // ForwardAsChatCompletions accepts an OpenAI Chat Completions API request body,
- // converts it to Anthropic Messages format (chained via Responses format),
- // forwards to the Anthropic upstream, and converts the response back to Chat
- // Completions format. This enables Chat Completions clients to access Anthropic
- // models through Anthropic platform groups.
- func (s *GatewayService) ForwardAsChatCompletions(
- ctx context.Context,
- c *gin.Context,
- account *Account,
- body []byte,
- parsed *ParsedRequest,
- ) (*ForwardResult, error) {
- startTime := time.Now()
- // 1. Parse Chat Completions request
- var ccReq apicompat.ChatCompletionsRequest
- if err := json.Unmarshal(body, &ccReq); err != nil {
- return nil, fmt.Errorf("parse chat completions request: %w", err)
- }
- originalModel := ccReq.Model
- clientStream := ccReq.Stream
- includeUsage := ccReq.StreamOptions != nil && ccReq.StreamOptions.IncludeUsage
- // 2. Convert CC → Responses → Anthropic (chained conversion)
- responsesReq, err := apicompat.ChatCompletionsToResponses(&ccReq)
- if err != nil {
- return nil, fmt.Errorf("convert chat completions to responses: %w", err)
- }
- anthropicReq, err := apicompat.ResponsesToAnthropicRequest(responsesReq)
- if err != nil {
- return nil, fmt.Errorf("convert responses to anthropic: %w", err)
- }
- // 3. Force upstream streaming
- anthropicReq.Stream = true
- reqStream := true
- // 4. Model mapping
- mappedModel := originalModel
- if account.Type == AccountTypeAPIKey || account.Type == AccountTypeServiceAccount {
- mappedModel = account.GetMappedModel(originalModel)
- }
- if mappedModel == originalModel && account.Platform == PlatformAnthropic && account.Type == AccountTypeServiceAccount {
- normalized := normalizeVertexAnthropicModelID(claude.NormalizeModelID(originalModel))
- if normalized != originalModel {
- mappedModel = normalized
- }
- } else if mappedModel == originalModel && account.Platform == PlatformAnthropic && account.Type != AccountTypeAPIKey {
- normalized := claude.NormalizeModelID(originalModel)
- if normalized != originalModel {
- mappedModel = normalized
- }
- }
- anthropicReq.Model = mappedModel
- logger.L().Debug("gateway forward_as_chat_completions: model mapping applied",
- zap.Int64("account_id", account.ID),
- zap.String("original_model", originalModel),
- zap.String("mapped_model", mappedModel),
- zap.Bool("client_stream", clientStream),
- )
- // 5. Marshal Anthropic request body
- anthropicBody, err := json.Marshal(anthropicReq)
- if err != nil {
- return nil, fmt.Errorf("marshal anthropic request: %w", err)
- }
- // 6. Apply Claude Code mimicry for OAuth accounts.
- // Chat Completions 协议进来的请求永远不是 Claude Code 客户端,所以对 OAuth 账号
- // 必须完整执行 /v1/messages 主路径上的伪装链路(system 重写 + normalize + metadata 注入),
- // 否则会被 Anthropic 判为第三方应用并扣 extra usage。
- // 见 applyClaudeCodeOAuthMimicryToBody 的 godoc。
- isClaudeCode := false
- shouldMimicClaudeCode := account.IsOAuth() && !isClaudeCode
- if shouldMimicClaudeCode {
- anthropicBody = s.applyClaudeCodeOAuthMimicryToBody(ctx, c, account, anthropicBody, anthropicReq.System, mappedModel)
- }
- // 7. Enforce cache_control block limit
- anthropicBody = enforceCacheControlLimit(anthropicBody)
- // 8. Get access token
- token, tokenType, err := s.GetAccessToken(ctx, account)
- if err != nil {
- return nil, fmt.Errorf("get access token: %w", err)
- }
- // 9. Get proxy URL
- proxyURL := ""
- if account.ProxyID != nil && account.Proxy != nil {
- proxyURL = account.Proxy.URL()
- }
- // 10. Build upstream request
- upstreamCtx, releaseUpstreamCtx := detachStreamUpstreamContext(ctx, reqStream)
- upstreamReq, _, err := s.buildUpstreamRequest(upstreamCtx, c, account, anthropicBody, token, tokenType, mappedModel, reqStream, shouldMimicClaudeCode)
- releaseUpstreamCtx()
- if err != nil {
- return nil, fmt.Errorf("build upstream request: %w", err)
- }
- // 11. Send request
- resp, err := s.httpUpstream.DoWithTLS(WithAccountTrafficRequest(upstreamReq, account), proxyURL, account.ID, account.Concurrency, s.tlsFPProfileService.ResolveTLSProfile(account))
- if err != nil {
- if resp != nil && resp.Body != nil {
- _ = resp.Body.Close()
- }
- return nil, s.handleUpstreamTransportError(ctx, c, account, err, OpsUpstreamErrorEvent{
- UpstreamURL: safeUpstreamURL(upstreamReq.URL.String()),
- })
- }
- defer func() { _ = resp.Body.Close() }()
- // 12. Handle error response with failover
- if resp.StatusCode >= 400 {
- respBody, _ := s.readUpstreamErrorBody(resp)
- _ = resp.Body.Close()
- resp.Body = io.NopCloser(bytes.NewReader(respBody))
- upstreamMsg := strings.TrimSpace(extractUpstreamErrorMessage(respBody))
- upstreamMsg = sanitizeUpstreamErrorMessage(upstreamMsg)
- if s.shouldFailoverUpstreamError(resp.StatusCode) {
- appendOpsUpstreamError(c, OpsUpstreamErrorEvent{
- ProxyID: opsUpstreamProxyID(account),
- ProxyName: opsUpstreamProxyName(account),
- Platform: account.Platform,
- AccountID: account.ID,
- AccountName: account.Name,
- UpstreamStatusCode: resp.StatusCode,
- UpstreamRequestID: resp.Header.Get("x-request-id"),
- Kind: "failover",
- Message: upstreamMsg,
- })
- shouldDisable := false
- if s.rateLimitService != nil {
- shouldDisable = s.rateLimitService.HandleUpstreamError(ctx, account, resp.StatusCode, resp.Header, respBody, mappedModel)
- }
- return nil, &UpstreamFailoverError{
- StatusCode: resp.StatusCode,
- ResponseBody: respBody,
- RetryableOnSameAccount: !shouldDisable && account.IsPoolMode() && account.IsPoolModeRetryableStatus(resp.StatusCode),
- }
- }
- writeGatewayCCError(c, mapUpstreamStatusCode(resp.StatusCode), "server_error", upstreamMsg)
- return nil, fmt.Errorf("upstream error: %d %s", resp.StatusCode, upstreamMsg)
- }
- // 13. Extract reasoning effort from CC request body
- reasoningEffort := extractCCReasoningEffortFromBody(body, mappedModel, originalModel)
- // 国产模型默认 effort 补充:本路径是客户端 CC 请求 → Anthropic 上游,
- // 如果上游是 passback-required 国产模型 (Kimi-anthropic / GLM-anthropic / MiniMax)
- // 且客户端在 body 里传了 thinking.type=enabled,补中默认 effort。
- reasoningEffort = ApplyThinkingEnabledFallback(reasoningEffort, body, mappedModel)
- // 14. Handle normal response
- // Read Anthropic SSE → convert to Responses events → convert to CC format
- var result *ForwardResult
- var handleErr error
- if clientStream {
- result, handleErr = s.handleCCStreamingFromAnthropic(resp, c, originalModel, mappedModel, reasoningEffort, startTime, includeUsage)
- } else {
- result, handleErr = s.handleCCBufferedFromAnthropic(resp, c, originalModel, mappedModel, reasoningEffort, startTime)
- }
- return result, handleErr
- }
- // extractCCReasoningEffortFromBody reads reasoning effort from a Chat Completions
- // request body. It checks both nested (reasoning.effort) and flat (reasoning_effort)
- // formats used by OpenAI-compatible clients.
- func extractCCReasoningEffortFromBody(body []byte, modelCandidates ...string) *string {
- raw := strings.TrimSpace(gjson.GetBytes(body, "reasoning.effort").String())
- if raw == "" {
- raw = strings.TrimSpace(gjson.GetBytes(body, "reasoning_effort").String())
- }
- if raw == "" {
- return nil
- }
- model := firstNonEmpty(modelCandidates...)
- if model == "" {
- model = strings.TrimSpace(gjson.GetBytes(body, "model").String())
- }
- normalized := normalizeOpenAIReasoningEffortForModel(raw, model)
- if normalized == "" {
- return nil
- }
- return &normalized
- }
- // handleCCBufferedFromAnthropic reads Anthropic SSE events, assembles the full
- // response, then converts Anthropic → Responses → Chat Completions.
- func (s *GatewayService) handleCCBufferedFromAnthropic(
- resp *http.Response,
- c *gin.Context,
- originalModel string,
- mappedModel string,
- reasoningEffort *string,
- startTime time.Time,
- ) (*ForwardResult, error) {
- requestID := resp.Header.Get("x-request-id")
- scanner := bufio.NewScanner(resp.Body)
- maxLineSize := defaultMaxLineSize
- if s.cfg != nil && s.cfg.Gateway.MaxLineSize > 0 {
- maxLineSize = s.cfg.Gateway.MaxLineSize
- }
- scanner.Buffer(make([]byte, 0, 64*1024), maxLineSize)
- var finalResp *apicompat.AnthropicResponse
- var usage ClaudeUsage
- for scanner.Scan() {
- line := scanner.Text()
- // SSE 规范允许 `event:xxx`(冒号后无空格):Kimi 等 Anthropic 兼容上游
- // 返回紧凑格式,严格匹配 "event: " 会丢弃全部事件(#4653 同根因)。
- if _, ok := extractOpenAISSEEventLine(line); !ok {
- continue
- }
- if !scanner.Scan() {
- break
- }
- payload, ok := extractOpenAISSEDataLine(scanner.Text())
- if !ok {
- continue
- }
- var event apicompat.AnthropicStreamEvent
- if err := json.Unmarshal([]byte(payload), &event); err != nil {
- continue
- }
- // message_start carries the initial response structure and cache usage
- if event.Type == "message_start" && event.Message != nil {
- finalResp = event.Message
- mergeAnthropicUsage(&usage, event.Message.Usage)
- }
- // message_delta carries final usage and stop_reason
- if event.Type == "message_delta" {
- if event.Usage != nil {
- mergeAnthropicUsage(&usage, *event.Usage)
- }
- if event.Delta != nil && event.Delta.StopReason != "" && finalResp != nil {
- finalResp.StopReason = apicompat.AnthropicStopReasonPtr(event.Delta.StopReason)
- }
- }
- if event.Type == "content_block_start" && event.ContentBlock != nil && finalResp != nil {
- finalResp.Content = append(finalResp.Content, *event.ContentBlock)
- }
- if event.Type == "content_block_delta" && event.Delta != nil && finalResp != nil && event.Index != nil {
- idx := *event.Index
- if idx < len(finalResp.Content) {
- switch event.Delta.Type {
- case "text_delta":
- finalResp.Content[idx].Text += event.Delta.Text
- case "thinking_delta":
- finalResp.Content[idx].Thinking += event.Delta.Thinking
- case "input_json_delta":
- finalResp.Content[idx].Input = appendRawJSON(finalResp.Content[idx].Input, event.Delta.PartialJSON)
- }
- }
- }
- }
- if err := scanner.Err(); err != nil {
- if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
- logger.L().Warn("forward_as_cc buffered: read error",
- zap.Error(err),
- zap.String("request_id", requestID),
- )
- }
- }
- if finalResp == nil {
- writeGatewayCCError(c, http.StatusBadGateway, "server_error", "Upstream stream ended without a response")
- return nil, fmt.Errorf("upstream stream ended without response")
- }
- // Update usage from accumulated delta
- if usage.InputTokens > 0 || usage.OutputTokens > 0 {
- finalResp.Usage = apicompat.AnthropicUsage{
- InputTokens: usage.InputTokens,
- OutputTokens: usage.OutputTokens,
- CacheCreationInputTokens: usage.CacheCreationInputTokens,
- CacheReadInputTokens: usage.CacheReadInputTokens,
- }
- }
- // Chain: Anthropic → Responses → Chat Completions
- responsesResp := apicompat.AnthropicToResponsesResponse(finalResp)
- ccResp := apicompat.ResponsesToChatCompletions(responsesResp, originalModel)
- if s.responseHeaderFilter != nil {
- responseheaders.WriteFilteredHeaders(c.Writer.Header(), resp.Header, s.responseHeaderFilter)
- }
- // 非流式响应必须是 application/json。上游被强制流式后会返回
- // Content-Type: text/event-stream,经 WriteFilteredHeaders 透传后会污染
- // 响应头;而 c.Data/c.JSON 走 Gin 的 writeContentType(仅当头不存在时才设置),
- // 无法覆盖已存在的 SSE 头。这里显式 Set 强制改回 JSON,避免下游中间层
- // (如 new-api)按 Content-Type 误判为流式。
- c.Writer.Header().Set("Content-Type", "application/json; charset=utf-8")
- // Marshal then bytes-replace so tool name mapping is reversed at byte level
- // (parity with Parrot non-stream flow that marshals → restore → emit).
- if respBytes, err := json.Marshal(ccResp); err == nil {
- respBytes = reverseToolNamesIfPresent(c, respBytes)
- c.Data(http.StatusOK, "application/json; charset=utf-8", respBytes)
- } else {
- c.JSON(http.StatusOK, ccResp)
- }
- return &ForwardResult{
- RequestID: requestID,
- UpstreamHeaders: resp.Header,
- Usage: usage,
- Model: originalModel,
- UpstreamModel: mappedModel,
- ReasoningEffort: reasoningEffort,
- Stream: false,
- Duration: time.Since(startTime),
- }, nil
- }
- // handleCCStreamingFromAnthropic reads Anthropic SSE events, converts each
- // to Responses events, then to Chat Completions chunks, and writes them.
- func (s *GatewayService) handleCCStreamingFromAnthropic(
- resp *http.Response,
- c *gin.Context,
- originalModel string,
- mappedModel string,
- reasoningEffort *string,
- startTime time.Time,
- includeUsage bool,
- ) (*ForwardResult, error) {
- requestID := resp.Header.Get("x-request-id")
- if s.responseHeaderFilter != nil {
- responseheaders.WriteFilteredHeaders(c.Writer.Header(), resp.Header, s.responseHeaderFilter)
- }
- c.Writer.Header().Set("Content-Type", "text/event-stream")
- c.Writer.Header().Set("Cache-Control", "no-cache")
- c.Writer.Header().Set("Connection", "keep-alive")
- c.Writer.Header().Set("X-Accel-Buffering", "no")
- c.Writer.WriteHeader(http.StatusOK)
- // Use Anthropic→Responses state machine, then convert Responses→CC
- anthState := apicompat.NewAnthropicEventToResponsesState()
- anthState.Model = originalModel
- ccState := apicompat.NewResponsesEventToChatState()
- ccState.Model = originalModel
- ccState.IncludeUsage = includeUsage
- var usage ClaudeUsage
- var firstTokenMs *int
- firstChunk := true
- scanner := bufio.NewScanner(resp.Body)
- maxLineSize := defaultMaxLineSize
- if s.cfg != nil && s.cfg.Gateway.MaxLineSize > 0 {
- maxLineSize = s.cfg.Gateway.MaxLineSize
- }
- scanner.Buffer(make([]byte, 0, 64*1024), maxLineSize)
- resultWithUsage := func() *ForwardResult {
- return &ForwardResult{
- RequestID: requestID,
- UpstreamHeaders: resp.Header,
- Usage: usage,
- Model: originalModel,
- UpstreamModel: mappedModel,
- ReasoningEffort: reasoningEffort,
- Stream: true,
- Duration: time.Since(startTime),
- FirstTokenMs: firstTokenMs,
- }
- }
- writeChunk := func(chunk apicompat.ChatCompletionsChunk) bool {
- sse, err := apicompat.ChatChunkToSSE(chunk)
- if err != nil {
- return false
- }
- // Reverse tool name mapping: fake → real, per-chunk bytes.Replace.
- // c 可能持有请求侧注入的 ToolNameRewrite;无则仅做静态前缀还原。
- out := string(reverseToolNamesIfPresent(c, []byte(sse)))
- if _, err := fmt.Fprint(c.Writer, out); err != nil {
- return true // client disconnected
- }
- return false
- }
- processAnthropicEvent := func(event *apicompat.AnthropicStreamEvent) bool {
- if firstChunk {
- firstChunk = false
- ms := int(time.Since(startTime).Milliseconds())
- firstTokenMs = &ms
- }
- // Extract usage from message_delta
- if event.Type == "message_delta" && event.Usage != nil {
- mergeAnthropicUsage(&usage, *event.Usage)
- }
- // Also capture usage from message_start (carries cache fields)
- if event.Type == "message_start" && event.Message != nil {
- mergeAnthropicUsage(&usage, event.Message.Usage)
- }
- // Chain: Anthropic event → Responses events → CC chunks
- responsesEvents := apicompat.AnthropicEventToResponsesEvents(event, anthState)
- for _, resEvt := range responsesEvents {
- ccChunks := apicompat.ResponsesEventToChatChunks(&resEvt, ccState)
- for _, chunk := range ccChunks {
- if disconnected := writeChunk(chunk); disconnected {
- return true
- }
- }
- }
- c.Writer.Flush()
- return false
- }
- for scanner.Scan() {
- line := scanner.Text()
- // 与缓冲路径一致:接受 SSE 紧凑格式(冒号后无空格,#4653 同根因)。
- if _, ok := extractOpenAISSEEventLine(line); !ok {
- continue
- }
- if !scanner.Scan() {
- break
- }
- payload, ok := extractOpenAISSEDataLine(scanner.Text())
- if !ok {
- continue
- }
- var event apicompat.AnthropicStreamEvent
- if err := json.Unmarshal([]byte(payload), &event); err != nil {
- continue
- }
- if processAnthropicEvent(&event) {
- return resultWithUsage(), nil
- }
- }
- if err := scanner.Err(); err != nil {
- if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
- logger.L().Warn("forward_as_cc stream: read error",
- zap.Error(err),
- zap.String("request_id", requestID),
- )
- }
- }
- // Finalize both state machines
- finalResEvents := apicompat.FinalizeAnthropicResponsesStream(anthState)
- for _, resEvt := range finalResEvents {
- ccChunks := apicompat.ResponsesEventToChatChunks(&resEvt, ccState)
- for _, chunk := range ccChunks {
- writeChunk(chunk) //nolint:errcheck
- }
- }
- finalCCChunks := apicompat.FinalizeResponsesChatStream(ccState)
- for _, chunk := range finalCCChunks {
- writeChunk(chunk) //nolint:errcheck
- }
- // Write [DONE] marker
- fmt.Fprint(c.Writer, "data: [DONE]\n\n") //nolint:errcheck
- c.Writer.Flush()
- return resultWithUsage(), nil
- }
- // writeGatewayCCError writes an error in OpenAI Chat Completions format for
- // the Anthropic-upstream CC forwarding path.
- func writeGatewayCCError(c *gin.Context, statusCode int, errType, message string) {
- MarkResponseCommitted(c)
- c.JSON(statusCode, gin.H{
- "error": gin.H{
- "type": errType,
- "message": message,
- },
- })
- }
|