Commit 86b503f8 authored by erio's avatar erio
Browse files

refactor: remove Anthropic digest chain from Messages handler

The digest chain fallback is only needed for Gemini endpoints, not
for the Anthropic Messages API path. Remove the handler integration
while keeping the reusable service/repository layer for future use.
parent 50a783ff
......@@ -22,7 +22,6 @@ import (
"github.com/Wei-Shaw/sub2api/internal/service"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
// GatewayHandler handles API gateway requests
......@@ -213,53 +212,6 @@ func (h *GatewayHandler) Messages(c *gin.Context) {
if sessionKey != "" {
sessionBoundAccountID, _ = h.gatewayService.GetCachedSessionAccountID(c.Request.Context(), apiKey.GroupID, sessionKey)
}
// === Anthropic 内容摘要会话 Fallback 逻辑 ===
// 当原有会话标识无效时(sessionBoundAccountID == 0),尝试基于内容摘要链匹配
var anthropicDigestChain string
var anthropicPrefixHash string
var anthropicSessionUUID string
useAnthropicDigestFallback := sessionBoundAccountID == 0 && platform != service.PlatformGemini
if useAnthropicDigestFallback {
anthropicDigestChain = service.BuildAnthropicDigestChain(parsedReq)
if anthropicDigestChain != "" {
userAgent := c.GetHeader("User-Agent")
clientIP := ip.GetClientIP(c)
anthropicPrefixHash = service.GenerateGeminiPrefixHash(
subject.UserID,
apiKey.ID,
clientIP,
userAgent,
platform,
reqModel,
)
foundUUID, foundAccountID, found := h.gatewayService.FindAnthropicSession(
c.Request.Context(),
derefGroupID(apiKey.GroupID),
anthropicPrefixHash,
anthropicDigestChain,
)
if found {
sessionBoundAccountID = foundAccountID
anthropicSessionUUID = foundUUID
log.Printf("[Anthropic] Digest fallback matched: uuid=%s, accountID=%d, chain=%s",
foundUUID[:8], foundAccountID, truncateDigestChain(anthropicDigestChain))
if sessionKey == "" {
sessionKey = service.GenerateAnthropicDigestSessionKey(anthropicPrefixHash, foundUUID)
}
_ = h.gatewayService.BindStickySession(c.Request.Context(), apiKey.GroupID, sessionKey, foundAccountID)
} else {
anthropicSessionUUID = uuid.New().String()
if sessionKey == "" {
sessionKey = service.GenerateAnthropicDigestSessionKey(anthropicPrefixHash, anthropicSessionUUID)
}
}
}
}
// 判断是否真的绑定了粘性会话:有 sessionKey 且已经绑定到某个账号
hasBoundSession := sessionKey != "" && sessionBoundAccountID > 0
......@@ -588,20 +540,6 @@ func (h *GatewayHandler) Messages(c *gin.Context) {
userAgent := c.GetHeader("User-Agent")
clientIP := ip.GetClientIP(c)
// 保存 Anthropic 内容摘要会话(用于 Fallback 匹配)
if useAnthropicDigestFallback && anthropicDigestChain != "" && anthropicPrefixHash != "" {
if err := h.gatewayService.SaveAnthropicSession(
c.Request.Context(),
derefGroupID(apiKey.GroupID),
anthropicPrefixHash,
anthropicDigestChain,
anthropicSessionUUID,
account.ID,
); err != nil {
log.Printf("[Anthropic] Failed to save digest session: %v", err)
}
}
// 异步记录使用量(subscription已在函数开头获取)
go func(result *service.ForwardResult, usedAccount *service.Account, ua, clientIP string, fcb bool) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment