feat(platform): 自定义平台 + Gemini OpenAI 兼容#2977
Open
bwliangc wants to merge 1 commit into
Open
Conversation
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
0befef0 to
150e246
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景 / 动机
平台此前是固定枚举(
anthropic/openai/gemini/antigravity),无法接入任意「OpenAI 兼容」的第三方上游(自建网关、聚合服务、各类兼容 endpoint)。本 PR 引入一个通用的
custom自定义平台:管理员只需填Base URL+API Key,即可把请求以 OpenAI 兼容 Chat Completions 协议透传到任意上游。同时把同一套能力作为 Gemini 平台的openai_chat_completions上游模式 复用。关键设计点:无论客户端用哪种入站协议(OpenAI / Anthropic / Gemini 原生),都会转换为 OpenAI Chat Completions 发往上游,因此一个自定义平台分组可被任意客户端访问。
能力总览
POST /v1/chat/completions{base_url}/chat/completionsPOST /v1/messagesPOST /v1beta/models/{model}:generateContent均支持流式 / 非流式、工具调用、用量计费。
改了什么
后端
PlatformCustom = "custom"(domain/constants.go、service/domain_constants.go),加入AllowedQuotaPlatforms;ent/schema/user_platform_quota.go配额平台白名单同步放行(account.platform/group.platform为自由字符串,无需数据库迁移)。service/account.go):IsCustom()、UsesOpenAICompatRawForward()(统一识别自定义平台 + Geminiopenai_chat_completions模式)。{base_url}/chat/completions):gemini_chat_completions_raw.go:OpenAI 入站原样转发;gemini_messages_openai_compat.go:Anthropic Messages ↔ OpenAI(复用apicompat的AnthropicToResponses→ResponsesToChatCompletionsRequest及 CC→Responses→Anthropic 事件链生成 Anthropic SSE);gemini_native_openai_compat.go:GeminigenerateContent↔ OpenAI(请求/响应双向转换,含流式 SSE 与countTokens估算)。gateway_handler_chat_completions.go、gemini_messages_compat_service.go、gemini_v1beta_handler.go在命中 OpenAI 兼容账号时分流到上述路径。upstream_models.go新增自定义平台{base_url}/models同步;统一buildOpenAIModelsURL的版本段识别(兼容/v1、/v1/models、任意版本段、自带/models)。gemini_v1beta_handler.go允许custom分组走 Gemini 原生端点(geminiNativeAllowedPlatform)。billing_service.go):gemini-3.5-flash(业务指定价,输入 $1.5 / 输出 $9 / 缓存读 $0.15 per MTok)、通用 Gemini Flash、非 flash 回退 3.1-Pro;图片模型走图片计费不受影响。account_service.goTestCredentials支持custom。前端
AccountPlatform/GroupPlatform/PlatformQuotaPlatform增加custom;新增GeminiUpstreamMode。PlatformTypeBadge把未知平台兜底显示为 Gemini 的 bug(自定义平台显示用户平台名 /Custom,中性配色);PlatformIcon齿轮图标;platformColors标签。ModelWhitelistSelector对自定义平台启用上游同步;getModelsByPlatform('custom')返回空(依赖上游同步)。custom;i18n(中英)。使用方式(管理员)
{base_url}/models拉取模型。platform = custom的分组并挂入该账号,即可用 OpenAI / Anthropic / Gemini 任一协议访问。兼容性 / 迁移
测试
go build ./...、go test -tags=unit ./...全通过;新增转换器与定价单测(Gemini↔OpenAI、Anthropic↔OpenAI 请求/响应转换、finishReason 映射、Base URL 兼容、Gemini 系列回退定价)。vue-tsc类型检查通过;更新并通过配额相关单测。自查清单
main,单一提交,diff 仅含本功能已知限制