Sub2API v1.0 - AI API 网关(二开初始版本,基于上游 Wei-Shaw/sub2api)
Release / update-version (push) Has been cancelled
Release / build-frontend (push) Has been cancelled
Release / release (push) Has been cancelled
Release / sync-version-file (push) Has been cancelled
CI / shell (push) Canceled after 0s
CI / test (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
Security Scan / backend-security (push) Canceled after 0s
Security Scan / frontend-security (push) Canceled after 0s
Release / update-version (push) Has been cancelled
Release / build-frontend (push) Has been cancelled
Release / release (push) Has been cancelled
Release / sync-version-file (push) Has been cancelled
CI / shell (push) Canceled after 0s
CI / test (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
Security Scan / backend-security (push) Canceled after 0s
Security Scan / frontend-security (push) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
const openAICompatMessagesBridgeContextKey = "openai_compat_messages_bridge"
|
||||
|
||||
func isOpenAICompatMessagesBridgeBody(body []byte) bool {
|
||||
if len(body) == 0 {
|
||||
return false
|
||||
}
|
||||
if bytes.Contains(body, []byte(openAICompatClaudeCodeTodoGuardMarker)) {
|
||||
return true
|
||||
}
|
||||
return isOpenAICompatMessagesBridgePromptCacheKey(gjson.GetBytes(body, "prompt_cache_key").String())
|
||||
}
|
||||
|
||||
func isOpenAICompatMessagesBridgeRequestBody(reqBody map[string]any) bool {
|
||||
if reqBody == nil {
|
||||
return false
|
||||
}
|
||||
if input, ok := reqBody["input"].([]any); ok && inputContainsText(input, openAICompatClaudeCodeTodoGuardMarker) {
|
||||
return true
|
||||
}
|
||||
return isOpenAICompatMessagesBridgePromptCacheKey(firstNonEmptyString(reqBody["prompt_cache_key"]))
|
||||
}
|
||||
|
||||
func isOpenAICompatMessagesBridgePromptCacheKey(key string) bool {
|
||||
key = strings.TrimSpace(key)
|
||||
return strings.HasPrefix(key, "anthropic-metadata-") ||
|
||||
strings.HasPrefix(key, "anthropic-cache-") ||
|
||||
strings.HasPrefix(key, "anthropic-digest-")
|
||||
}
|
||||
|
||||
func setOpenAICompatMessagesBridgeContext(c *gin.Context, enabled bool) {
|
||||
if c == nil || !enabled {
|
||||
return
|
||||
}
|
||||
c.Set(openAICompatMessagesBridgeContextKey, true)
|
||||
}
|
||||
|
||||
func isOpenAICompatMessagesBridgeContext(c *gin.Context) bool {
|
||||
if c == nil {
|
||||
return false
|
||||
}
|
||||
value, ok := c.Get(openAICompatMessagesBridgeContextKey)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
enabled, ok := value.(bool)
|
||||
return ok && enabled
|
||||
}
|
||||
Reference in New Issue
Block a user