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

This commit is contained in:
李建琦
2026-08-21 18:30:13 +08:00
commit 6d655c9903
3584 changed files with 1270640 additions and 0 deletions
@@ -0,0 +1,59 @@
package service
import "github.com/gin-gonic/gin"
// 请求级 hint 仅限 HTTP:缺失表示 unknownfalse/true 都表示已完成 canonical 判定。
const openAIImageIntentHintContextKey = "openai_image_intent_hint"
type openAIImageIntentClassifier func(endpoint string, requestedModel string, body []byte) bool
// SetOpenAIImageIntentHint 只写入请求级 canonical 判定,不记录 attempt-local 结果。
func SetOpenAIImageIntentHint(c *gin.Context, imageIntent bool) {
if c == nil || GetOpenAIClientTransport(c) != OpenAIClientTransportHTTP {
return
}
c.Set(openAIImageIntentHintContextKey, imageIntent)
}
func getOpenAIImageIntentHint(c *gin.Context) (imageIntent bool, known bool) {
if c == nil || GetOpenAIClientTransport(c) != OpenAIClientTransportHTTP {
return false, false
}
value, ok := c.Get(openAIImageIntentHintContextKey)
if !ok {
return false, false
}
imageIntent, ok = value.(bool)
return imageIntent, ok
}
func resolveOpenAIImageIntentHint(
c *gin.Context,
requestedModel string,
canonicalBody []byte,
classify openAIImageIntentClassifier,
) bool {
if imageIntent, known := getOpenAIImageIntentHint(c); known {
return imageIntent
}
imageIntent := classify(openAIResponsesEndpoint, requestedModel, canonicalBody)
SetOpenAIImageIntentHint(c, imageIntent)
return imageIntent
}
func resolveOpenAIPassthroughImageIntent(
c *gin.Context,
canonicalRequestedModel string,
canonicalBody []byte,
attemptRequestedModel string,
attemptBody []byte,
attemptInvalidated bool,
classify openAIImageIntentClassifier,
) bool {
imageIntent := resolveOpenAIImageIntentHint(c, canonicalRequestedModel, canonicalBody, classify)
if attemptInvalidated {
// strip/compact 改写只重算当前 attempt,不得把变换后的结果写回请求级 canonical hint。
imageIntent = classify(openAIResponsesEndpoint, attemptRequestedModel, attemptBody)
}
return imageIntent
}