2025-12-28 18:41:55 +08:00
|
|
|
|
package antigravity
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2026-01-08 17:27:35 +08:00
|
|
|
|
"crypto/sha256"
|
|
|
|
|
|
"encoding/binary"
|
2025-12-28 18:41:55 +08:00
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"fmt"
|
2025-12-31 21:35:41 +08:00
|
|
|
|
"log"
|
2026-01-08 17:27:35 +08:00
|
|
|
|
"math/rand"
|
|
|
|
|
|
"strconv"
|
2025-12-28 18:41:55 +08:00
|
|
|
|
"strings"
|
2026-01-04 22:33:01 +08:00
|
|
|
|
"sync"
|
2026-01-08 17:27:35 +08:00
|
|
|
|
"time"
|
2025-12-28 18:41:55 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-01-08 17:27:35 +08:00
|
|
|
|
var (
|
|
|
|
|
|
sessionRand = rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
|
|
sessionRandMutex sync.Mutex
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// generateStableSessionID 基于用户消息内容生成稳定的 session ID
|
|
|
|
|
|
func generateStableSessionID(contents []GeminiContent) string {
|
|
|
|
|
|
// 查找第一个 user 消息的文本
|
|
|
|
|
|
for _, content := range contents {
|
|
|
|
|
|
if content.Role == "user" && len(content.Parts) > 0 {
|
|
|
|
|
|
if text := content.Parts[0].Text; text != "" {
|
|
|
|
|
|
h := sha256.Sum256([]byte(text))
|
|
|
|
|
|
n := int64(binary.BigEndian.Uint64(h[:8])) & 0x7FFFFFFFFFFFFFFF
|
|
|
|
|
|
return "-" + strconv.FormatInt(n, 10)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// 回退:生成随机 session ID
|
|
|
|
|
|
sessionRandMutex.Lock()
|
|
|
|
|
|
n := sessionRand.Int63n(9_000_000_000_000_000_000)
|
|
|
|
|
|
sessionRandMutex.Unlock()
|
|
|
|
|
|
return "-" + strconv.FormatInt(n, 10)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-04 22:49:40 +08:00
|
|
|
|
type TransformOptions struct {
|
|
|
|
|
|
EnableIdentityPatch bool
|
|
|
|
|
|
// IdentityPatch 可选:自定义注入到 systemInstruction 开头的身份防护提示词;
|
|
|
|
|
|
// 为空时使用默认模板(包含 [IDENTITY_PATCH] 及 SYSTEM_PROMPT_BEGIN 标记)。
|
|
|
|
|
|
IdentityPatch string
|
2026-01-27 13:09:56 +08:00
|
|
|
|
EnableMCPXML bool
|
2026-01-04 22:49:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func DefaultTransformOptions() TransformOptions {
|
|
|
|
|
|
return TransformOptions{
|
|
|
|
|
|
EnableIdentityPatch: true,
|
2026-01-27 13:09:56 +08:00
|
|
|
|
EnableMCPXML: true,
|
2026-01-04 22:49:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-18 01:09:40 +08:00
|
|
|
|
// webSearchFallbackModel web_search 请求使用的降级模型
|
|
|
|
|
|
const webSearchFallbackModel = "gemini-2.5-flash"
|
|
|
|
|
|
|
2025-12-28 18:41:55 +08:00
|
|
|
|
// TransformClaudeToGemini 将 Claude 请求转换为 v1internal Gemini 格式
|
|
|
|
|
|
func TransformClaudeToGemini(claudeReq *ClaudeRequest, projectID, mappedModel string) ([]byte, error) {
|
2026-01-04 22:49:40 +08:00
|
|
|
|
return TransformClaudeToGeminiWithOptions(claudeReq, projectID, mappedModel, DefaultTransformOptions())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TransformClaudeToGeminiWithOptions 将 Claude 请求转换为 v1internal Gemini 格式(可配置身份补丁等行为)
|
|
|
|
|
|
func TransformClaudeToGeminiWithOptions(claudeReq *ClaudeRequest, projectID, mappedModel string, opts TransformOptions) ([]byte, error) {
|
2025-12-28 18:41:55 +08:00
|
|
|
|
// 用于存储 tool_use id -> name 映射
|
|
|
|
|
|
toolIDToName := make(map[string]string)
|
|
|
|
|
|
|
2026-01-18 01:09:40 +08:00
|
|
|
|
// 检测是否有 web_search 工具
|
|
|
|
|
|
hasWebSearchTool := hasWebSearchTool(claudeReq.Tools)
|
|
|
|
|
|
requestType := "agent"
|
|
|
|
|
|
targetModel := mappedModel
|
|
|
|
|
|
if hasWebSearchTool {
|
|
|
|
|
|
requestType = "web_search"
|
|
|
|
|
|
if targetModel != webSearchFallbackModel {
|
|
|
|
|
|
targetModel = webSearchFallbackModel
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-03 00:08:00 +08:00
|
|
|
|
// 检测是否启用 thinking
|
|
|
|
|
|
isThinkingEnabled := claudeReq.Thinking != nil && claudeReq.Thinking.Type == "enabled"
|
|
|
|
|
|
|
2025-12-28 21:36:21 +08:00
|
|
|
|
// 只有 Gemini 模型支持 dummy thought workaround
|
|
|
|
|
|
// Claude 模型通过 Vertex/Google API 需要有效的 thought signatures
|
2026-01-18 01:09:40 +08:00
|
|
|
|
allowDummyThought := strings.HasPrefix(targetModel, "gemini-")
|
2025-12-28 21:36:21 +08:00
|
|
|
|
|
2025-12-28 18:41:55 +08:00
|
|
|
|
// 1. 构建 contents
|
2026-01-04 22:32:36 +08:00
|
|
|
|
contents, strippedThinking, err := buildContents(claudeReq.Messages, toolIDToName, isThinkingEnabled, allowDummyThought)
|
2025-12-28 18:41:55 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, fmt.Errorf("build contents: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 2. 构建 systemInstruction
|
2026-01-17 01:49:42 +08:00
|
|
|
|
systemInstruction := buildSystemInstruction(claudeReq.System, claudeReq.Model, opts, claudeReq.Tools)
|
2025-12-28 18:41:55 +08:00
|
|
|
|
|
|
|
|
|
|
// 3. 构建 generationConfig
|
2026-01-04 22:32:36 +08:00
|
|
|
|
reqForConfig := claudeReq
|
|
|
|
|
|
if strippedThinking {
|
|
|
|
|
|
// If we had to downgrade thinking blocks to plain text due to missing/invalid signatures,
|
|
|
|
|
|
// disable upstream thinking mode to avoid signature/structure validation errors.
|
|
|
|
|
|
reqCopy := *claudeReq
|
|
|
|
|
|
reqCopy.Thinking = nil
|
|
|
|
|
|
reqForConfig = &reqCopy
|
|
|
|
|
|
}
|
2026-01-18 01:09:40 +08:00
|
|
|
|
if targetModel != "" && targetModel != reqForConfig.Model {
|
|
|
|
|
|
reqCopy := *reqForConfig
|
|
|
|
|
|
reqCopy.Model = targetModel
|
|
|
|
|
|
reqForConfig = &reqCopy
|
|
|
|
|
|
}
|
2026-01-04 22:32:36 +08:00
|
|
|
|
generationConfig := buildGenerationConfig(reqForConfig)
|
2025-12-28 18:41:55 +08:00
|
|
|
|
|
|
|
|
|
|
// 4. 构建 tools
|
|
|
|
|
|
tools := buildTools(claudeReq.Tools)
|
|
|
|
|
|
|
|
|
|
|
|
// 5. 构建内部请求
|
|
|
|
|
|
innerRequest := GeminiRequest{
|
2026-01-08 17:27:35 +08:00
|
|
|
|
Contents: contents,
|
|
|
|
|
|
// 总是设置 toolConfig,与官方客户端一致
|
|
|
|
|
|
ToolConfig: &GeminiToolConfig{
|
|
|
|
|
|
FunctionCallingConfig: &GeminiFunctionCallingConfig{
|
|
|
|
|
|
Mode: "VALIDATED",
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
// 总是生成 sessionId,基于用户消息内容
|
|
|
|
|
|
SessionID: generateStableSessionID(contents),
|
2025-12-28 18:41:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if systemInstruction != nil {
|
|
|
|
|
|
innerRequest.SystemInstruction = systemInstruction
|
|
|
|
|
|
}
|
|
|
|
|
|
if generationConfig != nil {
|
|
|
|
|
|
innerRequest.GenerationConfig = generationConfig
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(tools) > 0 {
|
|
|
|
|
|
innerRequest.Tools = tools
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-08 17:27:35 +08:00
|
|
|
|
// 如果提供了 metadata.user_id,优先使用
|
2025-12-28 18:41:55 +08:00
|
|
|
|
if claudeReq.Metadata != nil && claudeReq.Metadata.UserID != "" {
|
|
|
|
|
|
innerRequest.SessionID = claudeReq.Metadata.UserID
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 6. 包装为 v1internal 请求
|
|
|
|
|
|
v1Req := V1InternalRequest{
|
|
|
|
|
|
Project: projectID,
|
|
|
|
|
|
RequestID: "agent-" + uuid.New().String(),
|
2026-01-08 17:27:35 +08:00
|
|
|
|
UserAgent: "antigravity", // 固定值,与官方客户端一致
|
2026-01-18 01:09:40 +08:00
|
|
|
|
RequestType: requestType,
|
|
|
|
|
|
Model: targetModel,
|
2025-12-28 18:41:55 +08:00
|
|
|
|
Request: innerRequest,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return json.Marshal(v1Req)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-08 17:27:35 +08:00
|
|
|
|
// antigravityIdentity Antigravity identity 提示词
|
|
|
|
|
|
const antigravityIdentity = `<identity>
|
|
|
|
|
|
You are Antigravity, a powerful agentic AI coding assistant designed by the Google Deepmind team working on Advanced Agentic Coding.
|
|
|
|
|
|
You are pair programming with a USER to solve their coding task. The task may require creating a new codebase, modifying or debugging an existing codebase, or simply answering a question.
|
|
|
|
|
|
The USER will send you requests, which you must always prioritize addressing. Along with each USER request, we will attach additional metadata about their current state, such as what files they have open and where their cursor is.
|
|
|
|
|
|
This information may or may not be relevant to the coding task, it is up for you to decide.
|
|
|
|
|
|
</identity>
|
|
|
|
|
|
<communication_style>
|
|
|
|
|
|
- **Proactiveness**. As an agent, you are allowed to be proactive, but only in the course of completing the user's task. For example, if the user asks you to add a new component, you can edit the code, verify build and test statuses, and take any other obvious follow-up actions, such as performing additional research. However, avoid surprising the user. For example, if the user asks HOW to approach something, you should answer their question and instead of jumping into editing a file.</communication_style>`
|
|
|
|
|
|
|
|
|
|
|
|
func defaultIdentityPatch(_ string) string {
|
|
|
|
|
|
return antigravityIdentity
|
2026-01-04 22:49:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-08 20:05:55 +08:00
|
|
|
|
// GetDefaultIdentityPatch 返回默认的 Antigravity 身份提示词
|
|
|
|
|
|
func GetDefaultIdentityPatch() string {
|
|
|
|
|
|
return antigravityIdentity
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-17 01:49:42 +08:00
|
|
|
|
// mcpXMLProtocol MCP XML 工具调用协议(与 Antigravity-Manager 保持一致)
|
|
|
|
|
|
const mcpXMLProtocol = `
|
|
|
|
|
|
==== MCP XML 工具调用协议 (Workaround) ====
|
|
|
|
|
|
当你需要调用名称以 ` + "`mcp__`" + ` 开头的 MCP 工具时:
|
|
|
|
|
|
1) 优先尝试 XML 格式调用:输出 ` + "`<mcp__tool_name>{\"arg\":\"value\"}</mcp__tool_name>`" + `。
|
|
|
|
|
|
2) 必须直接输出 XML 块,无需 markdown 包装,内容为 JSON 格式的入参。
|
|
|
|
|
|
3) 这种方式具有更高的连通性和容错性,适用于大型结果返回场景。
|
|
|
|
|
|
===========================================`
|
|
|
|
|
|
|
|
|
|
|
|
// hasMCPTools 检测是否有 mcp__ 前缀的工具
|
|
|
|
|
|
func hasMCPTools(tools []ClaudeTool) bool {
|
|
|
|
|
|
for _, tool := range tools {
|
|
|
|
|
|
if strings.HasPrefix(tool.Name, "mcp__") {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// filterOpenCodePrompt 过滤 OpenCode 默认提示词,只保留用户自定义指令
|
|
|
|
|
|
func filterOpenCodePrompt(text string) string {
|
|
|
|
|
|
if !strings.Contains(text, "You are an interactive CLI tool") {
|
|
|
|
|
|
return text
|
|
|
|
|
|
}
|
|
|
|
|
|
// 提取 "Instructions from:" 及之后的部分
|
|
|
|
|
|
if idx := strings.Index(text, "Instructions from:"); idx >= 0 {
|
|
|
|
|
|
return text[idx:]
|
|
|
|
|
|
}
|
|
|
|
|
|
// 如果没有自定义指令,返回空
|
|
|
|
|
|
return ""
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// buildSystemInstruction 构建 systemInstruction(与 Antigravity-Manager 保持一致)
|
|
|
|
|
|
func buildSystemInstruction(system json.RawMessage, modelName string, opts TransformOptions, tools []ClaudeTool) *GeminiContent {
|
2026-01-04 22:49:40 +08:00
|
|
|
|
var parts []GeminiPart
|
|
|
|
|
|
|
2026-01-08 17:27:35 +08:00
|
|
|
|
// 先解析用户的 system prompt,检测是否已包含 Antigravity identity
|
|
|
|
|
|
userHasAntigravityIdentity := false
|
|
|
|
|
|
var userSystemParts []GeminiPart
|
2025-12-28 18:41:55 +08:00
|
|
|
|
|
|
|
|
|
|
if len(system) > 0 {
|
|
|
|
|
|
// 尝试解析为字符串
|
|
|
|
|
|
var sysStr string
|
|
|
|
|
|
if err := json.Unmarshal(system, &sysStr); err == nil {
|
|
|
|
|
|
if strings.TrimSpace(sysStr) != "" {
|
2026-01-08 17:27:35 +08:00
|
|
|
|
if strings.Contains(sysStr, "You are Antigravity") {
|
|
|
|
|
|
userHasAntigravityIdentity = true
|
|
|
|
|
|
}
|
2026-01-17 01:49:42 +08:00
|
|
|
|
// 过滤 OpenCode 默认提示词
|
|
|
|
|
|
filtered := filterOpenCodePrompt(sysStr)
|
|
|
|
|
|
if filtered != "" {
|
|
|
|
|
|
userSystemParts = append(userSystemParts, GeminiPart{Text: filtered})
|
|
|
|
|
|
}
|
2025-12-28 18:41:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 尝试解析为数组
|
|
|
|
|
|
var sysBlocks []SystemBlock
|
|
|
|
|
|
if err := json.Unmarshal(system, &sysBlocks); err == nil {
|
|
|
|
|
|
for _, block := range sysBlocks {
|
|
|
|
|
|
if block.Type == "text" && strings.TrimSpace(block.Text) != "" {
|
2026-01-08 17:27:35 +08:00
|
|
|
|
if strings.Contains(block.Text, "You are Antigravity") {
|
|
|
|
|
|
userHasAntigravityIdentity = true
|
|
|
|
|
|
}
|
2026-01-17 01:49:42 +08:00
|
|
|
|
// 过滤 OpenCode 默认提示词
|
|
|
|
|
|
filtered := filterOpenCodePrompt(block.Text)
|
|
|
|
|
|
if filtered != "" {
|
|
|
|
|
|
userSystemParts = append(userSystemParts, GeminiPart{Text: filtered})
|
|
|
|
|
|
}
|
2025-12-28 18:41:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-08 17:27:35 +08:00
|
|
|
|
// 仅在用户未提供 Antigravity identity 时注入
|
|
|
|
|
|
if opts.EnableIdentityPatch && !userHasAntigravityIdentity {
|
|
|
|
|
|
identityPatch := strings.TrimSpace(opts.IdentityPatch)
|
|
|
|
|
|
if identityPatch == "" {
|
|
|
|
|
|
identityPatch = defaultIdentityPatch(modelName)
|
|
|
|
|
|
}
|
|
|
|
|
|
parts = append(parts, GeminiPart{Text: identityPatch})
|
2026-01-04 22:49:40 +08:00
|
|
|
|
}
|
2026-01-08 17:27:35 +08:00
|
|
|
|
|
|
|
|
|
|
// 添加用户的 system prompt
|
|
|
|
|
|
parts = append(parts, userSystemParts...)
|
|
|
|
|
|
|
2026-01-27 13:09:56 +08:00
|
|
|
|
// 检测是否有 MCP 工具,如有且启用了 MCP XML 注入则注入 XML 调用协议
|
|
|
|
|
|
if opts.EnableMCPXML && hasMCPTools(tools) {
|
2026-01-17 01:49:42 +08:00
|
|
|
|
parts = append(parts, GeminiPart{Text: mcpXMLProtocol})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 如果用户没有提供 Antigravity 身份,添加结束标记
|
|
|
|
|
|
if !userHasAntigravityIdentity {
|
|
|
|
|
|
parts = append(parts, GeminiPart{Text: "\n--- [SYSTEM_PROMPT_END] ---"})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-04 22:49:40 +08:00
|
|
|
|
if len(parts) == 0 {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
2025-12-28 18:41:55 +08:00
|
|
|
|
|
|
|
|
|
|
return &GeminiContent{
|
|
|
|
|
|
Role: "user",
|
|
|
|
|
|
Parts: parts,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// buildContents 构建 contents
|
2026-01-04 22:32:36 +08:00
|
|
|
|
func buildContents(messages []ClaudeMessage, toolIDToName map[string]string, isThinkingEnabled, allowDummyThought bool) ([]GeminiContent, bool, error) {
|
2025-12-28 18:41:55 +08:00
|
|
|
|
var contents []GeminiContent
|
2026-01-04 22:32:36 +08:00
|
|
|
|
strippedThinking := false
|
2025-12-28 18:41:55 +08:00
|
|
|
|
|
|
|
|
|
|
for i, msg := range messages {
|
|
|
|
|
|
role := msg.Role
|
|
|
|
|
|
if role == "assistant" {
|
|
|
|
|
|
role = "model"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-04 22:32:36 +08:00
|
|
|
|
parts, strippedThisMsg, err := buildParts(msg.Content, toolIDToName, allowDummyThought)
|
2025-12-28 18:41:55 +08:00
|
|
|
|
if err != nil {
|
2026-01-04 22:32:36 +08:00
|
|
|
|
return nil, false, fmt.Errorf("build parts for message %d: %w", i, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
if strippedThisMsg {
|
|
|
|
|
|
strippedThinking = true
|
2025-12-28 18:41:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-28 21:36:21 +08:00
|
|
|
|
// 只有 Gemini 模型支持 dummy thinking block workaround
|
|
|
|
|
|
// 只对最后一条 assistant 消息添加(Pre-fill 场景)
|
|
|
|
|
|
// 历史 assistant 消息不能添加没有 signature 的 dummy thinking block
|
|
|
|
|
|
if allowDummyThought && role == "model" && isThinkingEnabled && i == len(messages)-1 {
|
2025-12-28 18:41:55 +08:00
|
|
|
|
hasThoughtPart := false
|
|
|
|
|
|
for _, p := range parts {
|
|
|
|
|
|
if p.Thought {
|
|
|
|
|
|
hasThoughtPart = true
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if !hasThoughtPart && len(parts) > 0 {
|
2025-12-28 21:36:21 +08:00
|
|
|
|
// 在开头添加 dummy thinking block
|
2025-12-28 21:29:16 +08:00
|
|
|
|
parts = append([]GeminiPart{{
|
fix: 修复 /v1/messages 间歇性 400 错误 (#112)
* fix(upstream): 修复上游格式兼容性问题
- 跳过Claude模型无signature的thinking block
- 支持custom类型工具(MCP)格式转换
- 添加ClaudeCustomToolSpec结构体支持MCP工具
- 添加Custom字段验证,跳过无效custom工具
- 在convertClaudeToolsToGeminiTools中添加schema清理
- 完整的单元测试覆盖,包含边界情况
修复: Issue 0.1 signature缺失, Issue 0.2 custom工具格式
改进: Codex审查发现的2个重要问题
测试:
- TestBuildParts_ThinkingBlockWithoutSignature: 验证thinking block处理
- TestBuildTools_CustomTypeTools: 验证custom工具转换和边界情况
- TestConvertClaudeToolsToGeminiTools_CustomType: 验证service层转换
* feat(gemini): 添加Gemini限额与TierID支持
实现PR1:Gemini限额与TierID功能
后端修改:
- GeminiTokenInfo结构体添加TierID字段
- fetchProjectID函数返回(projectID, tierID, error)
- 从LoadCodeAssist响应中提取tierID(优先IsDefault,回退到第一个非空tier)
- ExchangeCode、RefreshAccountToken、GetAccessToken函数更新以处理tierID
- BuildAccountCredentials函数保存tier_id到credentials
前端修改:
- AccountStatusIndicator组件添加tier显示
- 支持LEGACY/PRO/ULTRA等tier类型的友好显示
- 使用蓝色badge展示tier信息
技术细节:
- tierID提取逻辑:优先选择IsDefault的tier,否则选择第一个非空tier
- 所有fetchProjectID调用点已更新以处理新的返回签名
- 前端gracefully处理missing/unknown tier_id
* refactor(gemini): 优化TierID实现并添加安全验证
根据并发代码审查(code-reviewer, security-auditor, gemini, codex)的反馈进行改进:
安全改进:
- 添加validateTierID函数验证tier_id格式和长度(最大64字符)
- 限制tier_id字符集为字母数字、下划线、连字符和斜杠
- 在BuildAccountCredentials中验证tier_id后再存储
- 静默跳过无效tier_id,不阻塞账户创建
代码质量改进:
- 提取extractTierIDFromAllowedTiers辅助函数消除重复代码
- 重构fetchProjectID函数,tierID提取逻辑只执行一次
- 改进代码可读性和可维护性
审查工具:
- code-reviewer agent (a09848e)
- security-auditor agent (a9a149c)
- gemini CLI (bcc7c81)
- codex (b5d8919)
修复问题:
- HIGH: 未验证的tier_id输入
- MEDIUM: 代码重复(tierID提取逻辑重复2次)
* fix(format): 修复 gofmt 格式问题
- 修复 claude_types.go 中的字段对齐问题
- 修复 gemini_messages_compat_service.go 中的缩进问题
* fix(upstream): 修复上游格式兼容性问题 (#14)
* fix(upstream): 修复上游格式兼容性问题
- 跳过Claude模型无signature的thinking block
- 支持custom类型工具(MCP)格式转换
- 添加ClaudeCustomToolSpec结构体支持MCP工具
- 添加Custom字段验证,跳过无效custom工具
- 在convertClaudeToolsToGeminiTools中添加schema清理
- 完整的单元测试覆盖,包含边界情况
修复: Issue 0.1 signature缺失, Issue 0.2 custom工具格式
改进: Codex审查发现的2个重要问题
测试:
- TestBuildParts_ThinkingBlockWithoutSignature: 验证thinking block处理
- TestBuildTools_CustomTypeTools: 验证custom工具转换和边界情况
- TestConvertClaudeToolsToGeminiTools_CustomType: 验证service层转换
* fix(format): 修复 gofmt 格式问题
- 修复 claude_types.go 中的字段对齐问题
- 修复 gemini_messages_compat_service.go 中的缩进问题
* fix(format): 修复 claude_types.go 的 gofmt 格式问题
* feat(antigravity): 优化 thinking block 和 schema 处理
- 为 dummy thinking block 添加 ThoughtSignature
- 重构 thinking block 处理逻辑,在每个条件分支内创建 part
- 优化 excludedSchemaKeys,移除 Gemini 实际支持的字段
(minItems, maxItems, minimum, maximum, additionalProperties, format)
- 添加详细注释说明 Gemini API 支持的 schema 字段
* fix(antigravity): 增强 schema 清理的安全性
基于 Codex review 建议:
- 添加 format 字段白名单过滤,只保留 Gemini 支持的 date-time/date/time
- 补充更多不支持的 schema 关键字到黑名单:
* 组合 schema: oneOf, anyOf, allOf, not, if/then/else
* 对象验证: minProperties, maxProperties, patternProperties 等
* 定义引用: $defs, definitions
- 避免不支持的 schema 字段导致 Gemini API 校验失败
* fix(lint): 修复 gemini_messages_compat_service 空分支警告
- 在 cleanToolSchema 的 if 语句中添加 continue
- 移除重复的注释
* fix(antigravity): 移除 minItems/maxItems 以兼容 Claude API
- 将 minItems 和 maxItems 添加到 schema 黑名单
- Claude API (Vertex AI) 不支持这些数组验证字段
- 添加调试日志记录工具 schema 转换过程
- 修复 tools.14.custom.input_schema 验证错误
* fix(antigravity): 修复 additionalProperties schema 对象问题
- 将 additionalProperties 的 schema 对象转换为布尔值 true
- Claude API 只支持 additionalProperties: false,不支持 schema 对象
- 修复 tools.14.custom.input_schema 验证错误
- 参考 Claude 官方文档的 JSON Schema 限制
* fix(antigravity): 修复 Claude 模型 thinking 块兼容性问题
- 完全跳过 Claude 模型的 thinking 块以避免 signature 验证失败
- 只在 Gemini 模型中使用 dummy thought signature
- 修改 additionalProperties 默认值为 false(更安全)
- 添加调试日志以便排查问题
* fix(upstream): 修复跨模型切换时的 dummy signature 问题
基于 Codex review 和用户场景分析的修复:
1. 问题场景
- Gemini (thinking) → Claude (thinking) 切换时
- Gemini 返回的 thinking 块使用 dummy signature
- Claude API 会拒绝 dummy signature,导致 400 错误
2. 修复内容
- request_transformer.go:262: 跳过 dummy signature
- 只保留真实的 Claude signature
- 支持频繁的跨模型切换
3. 其他修复(基于 Codex review)
- gateway_service.go:691: 修复 io.ReadAll 错误处理
- gateway_service.go:687: 条件日志(尊重 LogUpstreamErrorBody 配置)
- gateway_service.go:915: 收紧 400 failover 启发式
- request_transformer.go:188: 移除签名成功日志
4. 新增功能(默认关闭)
- 阶段 1: 上游错误日志(GATEWAY_LOG_UPSTREAM_ERROR_BODY)
- 阶段 2: Antigravity thinking 修复
- 阶段 3: API-key beta 注入(GATEWAY_INJECT_BETA_FOR_APIKEY)
- 阶段 3: 智能 400 failover(GATEWAY_FAILOVER_ON_400)
测试:所有测试通过
* fix(lint): 修复 golangci-lint 问题
- 应用 De Morgan 定律简化条件判断
- 修复 gofmt 格式问题
- 移除未使用的 min 函数
2026-01-01 10:45:57 +08:00
|
|
|
|
Text: "Thinking...",
|
|
|
|
|
|
Thought: true,
|
|
|
|
|
|
ThoughtSignature: dummyThoughtSignature,
|
2025-12-28 21:29:16 +08:00
|
|
|
|
}}, parts...)
|
2025-12-28 18:41:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if len(parts) == 0 {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
contents = append(contents, GeminiContent{
|
|
|
|
|
|
Role: role,
|
|
|
|
|
|
Parts: parts,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-04 22:32:36 +08:00
|
|
|
|
return contents, strippedThinking, nil
|
2025-12-28 18:41:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-28 21:25:04 +08:00
|
|
|
|
// dummyThoughtSignature 用于跳过 Gemini 3 thought_signature 验证
|
|
|
|
|
|
// 参考: https://ai.google.dev/gemini-api/docs/thought-signatures
|
|
|
|
|
|
const dummyThoughtSignature = "skip_thought_signature_validator"
|
|
|
|
|
|
|
2025-12-28 18:41:55 +08:00
|
|
|
|
// buildParts 构建消息的 parts
|
2025-12-28 21:36:21 +08:00
|
|
|
|
// allowDummyThought: 只有 Gemini 模型支持 dummy thought signature
|
2026-01-04 22:32:36 +08:00
|
|
|
|
func buildParts(content json.RawMessage, toolIDToName map[string]string, allowDummyThought bool) ([]GeminiPart, bool, error) {
|
2025-12-28 18:41:55 +08:00
|
|
|
|
var parts []GeminiPart
|
2026-01-04 22:32:36 +08:00
|
|
|
|
strippedThinking := false
|
2025-12-28 18:41:55 +08:00
|
|
|
|
|
|
|
|
|
|
// 尝试解析为字符串
|
|
|
|
|
|
var textContent string
|
|
|
|
|
|
if err := json.Unmarshal(content, &textContent); err == nil {
|
|
|
|
|
|
if textContent != "(no content)" && strings.TrimSpace(textContent) != "" {
|
|
|
|
|
|
parts = append(parts, GeminiPart{Text: strings.TrimSpace(textContent)})
|
|
|
|
|
|
}
|
2026-01-04 22:32:36 +08:00
|
|
|
|
return parts, false, nil
|
2025-12-28 18:41:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 解析为内容块数组
|
|
|
|
|
|
var blocks []ContentBlock
|
|
|
|
|
|
if err := json.Unmarshal(content, &blocks); err != nil {
|
2026-01-04 22:32:36 +08:00
|
|
|
|
return nil, false, fmt.Errorf("parse content blocks: %w", err)
|
2025-12-28 18:41:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for _, block := range blocks {
|
|
|
|
|
|
switch block.Type {
|
|
|
|
|
|
case "text":
|
|
|
|
|
|
if block.Text != "(no content)" && strings.TrimSpace(block.Text) != "" {
|
|
|
|
|
|
parts = append(parts, GeminiPart{Text: block.Text})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
case "thinking":
|
2026-01-03 00:08:00 +08:00
|
|
|
|
part := GeminiPart{
|
|
|
|
|
|
Text: block.Thinking,
|
|
|
|
|
|
Thought: true,
|
2025-12-28 18:41:55 +08:00
|
|
|
|
}
|
2026-02-02 22:13:50 +08:00
|
|
|
|
// signature 处理:
|
|
|
|
|
|
// - Claude 模型(allowDummyThought=false):必须是上游返回的真实 signature(dummy 视为缺失)
|
|
|
|
|
|
// - Gemini 模型(allowDummyThought=true):优先透传真实 signature,缺失时使用 dummy signature
|
|
|
|
|
|
if block.Signature != "" && (allowDummyThought || block.Signature != dummyThoughtSignature) {
|
2026-01-03 00:08:00 +08:00
|
|
|
|
part.ThoughtSignature = block.Signature
|
|
|
|
|
|
} else if !allowDummyThought {
|
2026-01-04 22:32:36 +08:00
|
|
|
|
// Claude 模型需要有效 signature;在缺失时降级为普通文本,并在上层禁用 thinking mode。
|
|
|
|
|
|
if strings.TrimSpace(block.Thinking) != "" {
|
|
|
|
|
|
parts = append(parts, GeminiPart{Text: block.Thinking})
|
|
|
|
|
|
}
|
|
|
|
|
|
strippedThinking = true
|
2025-12-31 21:35:41 +08:00
|
|
|
|
continue
|
2026-01-03 00:08:00 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
// Gemini 模型使用 dummy signature
|
|
|
|
|
|
part.ThoughtSignature = dummyThoughtSignature
|
2025-12-28 18:41:55 +08:00
|
|
|
|
}
|
2026-01-03 00:08:00 +08:00
|
|
|
|
parts = append(parts, part)
|
2025-12-28 18:41:55 +08:00
|
|
|
|
|
|
|
|
|
|
case "image":
|
|
|
|
|
|
if block.Source != nil && block.Source.Type == "base64" {
|
|
|
|
|
|
parts = append(parts, GeminiPart{
|
|
|
|
|
|
InlineData: &GeminiInlineData{
|
|
|
|
|
|
MimeType: block.Source.MediaType,
|
|
|
|
|
|
Data: block.Source.Data,
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
case "tool_use":
|
|
|
|
|
|
// 存储 id -> name 映射
|
|
|
|
|
|
if block.ID != "" && block.Name != "" {
|
|
|
|
|
|
toolIDToName[block.ID] = block.Name
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
part := GeminiPart{
|
|
|
|
|
|
FunctionCall: &GeminiFunctionCall{
|
|
|
|
|
|
Name: block.Name,
|
|
|
|
|
|
Args: block.Input,
|
|
|
|
|
|
ID: block.ID,
|
|
|
|
|
|
},
|
2025-12-28 21:36:21 +08:00
|
|
|
|
}
|
2026-01-17 21:09:59 +08:00
|
|
|
|
// tool_use 的 signature 处理:
|
2026-02-02 22:13:50 +08:00
|
|
|
|
// - Claude 模型(allowDummyThought=false):必须是上游返回的真实 signature(dummy 视为缺失)
|
|
|
|
|
|
// - Gemini 模型(allowDummyThought=true):优先透传真实 signature,缺失时使用 dummy signature
|
|
|
|
|
|
if block.Signature != "" && (allowDummyThought || block.Signature != dummyThoughtSignature) {
|
2026-01-17 21:09:59 +08:00
|
|
|
|
part.ThoughtSignature = block.Signature
|
2026-02-02 22:13:50 +08:00
|
|
|
|
} else if allowDummyThought {
|
|
|
|
|
|
part.ThoughtSignature = dummyThoughtSignature
|
2025-12-28 18:41:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
parts = append(parts, part)
|
|
|
|
|
|
|
|
|
|
|
|
case "tool_result":
|
|
|
|
|
|
// 获取函数名
|
|
|
|
|
|
funcName := block.Name
|
|
|
|
|
|
if funcName == "" {
|
|
|
|
|
|
if name, ok := toolIDToName[block.ToolUseID]; ok {
|
|
|
|
|
|
funcName = name
|
|
|
|
|
|
} else {
|
|
|
|
|
|
funcName = block.ToolUseID
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 解析 content
|
|
|
|
|
|
resultContent := parseToolResultContent(block.Content, block.IsError)
|
|
|
|
|
|
|
|
|
|
|
|
parts = append(parts, GeminiPart{
|
|
|
|
|
|
FunctionResponse: &GeminiFunctionResponse{
|
|
|
|
|
|
Name: funcName,
|
2025-12-29 18:05:05 +08:00
|
|
|
|
Response: map[string]any{
|
2025-12-28 18:41:55 +08:00
|
|
|
|
"result": resultContent,
|
|
|
|
|
|
},
|
|
|
|
|
|
ID: block.ToolUseID,
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-04 22:32:36 +08:00
|
|
|
|
return parts, strippedThinking, nil
|
2025-12-28 18:41:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// parseToolResultContent 解析 tool_result 的 content
|
|
|
|
|
|
func parseToolResultContent(content json.RawMessage, isError bool) string {
|
|
|
|
|
|
if len(content) == 0 {
|
|
|
|
|
|
if isError {
|
|
|
|
|
|
return "Tool execution failed with no output."
|
|
|
|
|
|
}
|
|
|
|
|
|
return "Command executed successfully."
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 尝试解析为字符串
|
|
|
|
|
|
var str string
|
|
|
|
|
|
if err := json.Unmarshal(content, &str); err == nil {
|
|
|
|
|
|
if strings.TrimSpace(str) == "" {
|
|
|
|
|
|
if isError {
|
|
|
|
|
|
return "Tool execution failed with no output."
|
|
|
|
|
|
}
|
|
|
|
|
|
return "Command executed successfully."
|
|
|
|
|
|
}
|
|
|
|
|
|
return str
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 尝试解析为数组
|
2025-12-29 18:05:05 +08:00
|
|
|
|
var arr []map[string]any
|
2025-12-28 18:41:55 +08:00
|
|
|
|
if err := json.Unmarshal(content, &arr); err == nil {
|
|
|
|
|
|
var texts []string
|
|
|
|
|
|
for _, item := range arr {
|
|
|
|
|
|
if text, ok := item["text"].(string); ok {
|
|
|
|
|
|
texts = append(texts, text)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
result := strings.Join(texts, "\n")
|
|
|
|
|
|
if strings.TrimSpace(result) == "" {
|
|
|
|
|
|
if isError {
|
|
|
|
|
|
return "Tool execution failed with no output."
|
|
|
|
|
|
}
|
|
|
|
|
|
return "Command executed successfully."
|
|
|
|
|
|
}
|
|
|
|
|
|
return result
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 返回原始 JSON
|
|
|
|
|
|
return string(content)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// buildGenerationConfig 构建 generationConfig
|
2026-01-23 10:44:21 +08:00
|
|
|
|
const (
|
2026-01-27 13:09:56 +08:00
|
|
|
|
defaultMaxOutputTokens = 64000
|
2026-01-23 10:44:21 +08:00
|
|
|
|
maxOutputTokensUpperBound = 65000
|
|
|
|
|
|
maxOutputTokensClaude = 64000
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func maxOutputTokensLimit(model string) int {
|
|
|
|
|
|
if strings.HasPrefix(model, "claude-") {
|
|
|
|
|
|
return maxOutputTokensClaude
|
|
|
|
|
|
}
|
|
|
|
|
|
return maxOutputTokensUpperBound
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-28 18:41:55 +08:00
|
|
|
|
func buildGenerationConfig(req *ClaudeRequest) *GeminiGenerationConfig {
|
2026-01-23 10:44:21 +08:00
|
|
|
|
maxLimit := maxOutputTokensLimit(req.Model)
|
2025-12-28 18:41:55 +08:00
|
|
|
|
config := &GeminiGenerationConfig{
|
2026-01-23 10:44:21 +08:00
|
|
|
|
MaxOutputTokens: defaultMaxOutputTokens, // 默认最大输出
|
2025-12-28 18:41:55 +08:00
|
|
|
|
StopSequences: DefaultStopSequences,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-16 10:31:55 +08:00
|
|
|
|
// 如果请求中指定了 MaxTokens,使用请求值
|
|
|
|
|
|
if req.MaxTokens > 0 {
|
|
|
|
|
|
config.MaxOutputTokens = req.MaxTokens
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-28 18:41:55 +08:00
|
|
|
|
// Thinking 配置
|
|
|
|
|
|
if req.Thinking != nil && req.Thinking.Type == "enabled" {
|
|
|
|
|
|
config.ThinkingConfig = &GeminiThinkingConfig{
|
|
|
|
|
|
IncludeThoughts: true,
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.Thinking.BudgetTokens > 0 {
|
|
|
|
|
|
budget := req.Thinking.BudgetTokens
|
|
|
|
|
|
// gemini-2.5-flash 上限 24576
|
|
|
|
|
|
if strings.Contains(req.Model, "gemini-2.5-flash") && budget > 24576 {
|
|
|
|
|
|
budget = 24576
|
|
|
|
|
|
}
|
|
|
|
|
|
config.ThinkingConfig.ThinkingBudget = budget
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-23 10:44:21 +08:00
|
|
|
|
if config.MaxOutputTokens > maxLimit {
|
|
|
|
|
|
config.MaxOutputTokens = maxLimit
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-28 18:41:55 +08:00
|
|
|
|
// 其他参数
|
|
|
|
|
|
if req.Temperature != nil {
|
|
|
|
|
|
config.Temperature = req.Temperature
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.TopP != nil {
|
|
|
|
|
|
config.TopP = req.TopP
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.TopK != nil {
|
|
|
|
|
|
config.TopK = req.TopK
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return config
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-18 01:09:40 +08:00
|
|
|
|
func hasWebSearchTool(tools []ClaudeTool) bool {
|
|
|
|
|
|
for _, tool := range tools {
|
|
|
|
|
|
if isWebSearchTool(tool) {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func isWebSearchTool(tool ClaudeTool) bool {
|
|
|
|
|
|
if strings.HasPrefix(tool.Type, "web_search") || tool.Type == "google_search" {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
name := strings.TrimSpace(tool.Name)
|
|
|
|
|
|
switch name {
|
|
|
|
|
|
case "web_search", "google_search", "web_search_20250305":
|
|
|
|
|
|
return true
|
|
|
|
|
|
default:
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-28 18:41:55 +08:00
|
|
|
|
// buildTools 构建 tools
|
|
|
|
|
|
func buildTools(tools []ClaudeTool) []GeminiToolDeclaration {
|
|
|
|
|
|
if len(tools) == 0 {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-18 01:09:40 +08:00
|
|
|
|
hasWebSearch := hasWebSearchTool(tools)
|
2025-12-28 18:41:55 +08:00
|
|
|
|
|
|
|
|
|
|
// 普通工具
|
|
|
|
|
|
var funcDecls []GeminiFunctionDecl
|
2026-01-02 11:44:32 +08:00
|
|
|
|
for _, tool := range tools {
|
2026-01-18 01:09:40 +08:00
|
|
|
|
if isWebSearchTool(tool) {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
2025-12-31 21:44:56 +08:00
|
|
|
|
// 跳过无效工具名称
|
fix: 修复 /v1/messages 间歇性 400 错误 (#112)
* fix(upstream): 修复上游格式兼容性问题
- 跳过Claude模型无signature的thinking block
- 支持custom类型工具(MCP)格式转换
- 添加ClaudeCustomToolSpec结构体支持MCP工具
- 添加Custom字段验证,跳过无效custom工具
- 在convertClaudeToolsToGeminiTools中添加schema清理
- 完整的单元测试覆盖,包含边界情况
修复: Issue 0.1 signature缺失, Issue 0.2 custom工具格式
改进: Codex审查发现的2个重要问题
测试:
- TestBuildParts_ThinkingBlockWithoutSignature: 验证thinking block处理
- TestBuildTools_CustomTypeTools: 验证custom工具转换和边界情况
- TestConvertClaudeToolsToGeminiTools_CustomType: 验证service层转换
* feat(gemini): 添加Gemini限额与TierID支持
实现PR1:Gemini限额与TierID功能
后端修改:
- GeminiTokenInfo结构体添加TierID字段
- fetchProjectID函数返回(projectID, tierID, error)
- 从LoadCodeAssist响应中提取tierID(优先IsDefault,回退到第一个非空tier)
- ExchangeCode、RefreshAccountToken、GetAccessToken函数更新以处理tierID
- BuildAccountCredentials函数保存tier_id到credentials
前端修改:
- AccountStatusIndicator组件添加tier显示
- 支持LEGACY/PRO/ULTRA等tier类型的友好显示
- 使用蓝色badge展示tier信息
技术细节:
- tierID提取逻辑:优先选择IsDefault的tier,否则选择第一个非空tier
- 所有fetchProjectID调用点已更新以处理新的返回签名
- 前端gracefully处理missing/unknown tier_id
* refactor(gemini): 优化TierID实现并添加安全验证
根据并发代码审查(code-reviewer, security-auditor, gemini, codex)的反馈进行改进:
安全改进:
- 添加validateTierID函数验证tier_id格式和长度(最大64字符)
- 限制tier_id字符集为字母数字、下划线、连字符和斜杠
- 在BuildAccountCredentials中验证tier_id后再存储
- 静默跳过无效tier_id,不阻塞账户创建
代码质量改进:
- 提取extractTierIDFromAllowedTiers辅助函数消除重复代码
- 重构fetchProjectID函数,tierID提取逻辑只执行一次
- 改进代码可读性和可维护性
审查工具:
- code-reviewer agent (a09848e)
- security-auditor agent (a9a149c)
- gemini CLI (bcc7c81)
- codex (b5d8919)
修复问题:
- HIGH: 未验证的tier_id输入
- MEDIUM: 代码重复(tierID提取逻辑重复2次)
* fix(format): 修复 gofmt 格式问题
- 修复 claude_types.go 中的字段对齐问题
- 修复 gemini_messages_compat_service.go 中的缩进问题
* fix(upstream): 修复上游格式兼容性问题 (#14)
* fix(upstream): 修复上游格式兼容性问题
- 跳过Claude模型无signature的thinking block
- 支持custom类型工具(MCP)格式转换
- 添加ClaudeCustomToolSpec结构体支持MCP工具
- 添加Custom字段验证,跳过无效custom工具
- 在convertClaudeToolsToGeminiTools中添加schema清理
- 完整的单元测试覆盖,包含边界情况
修复: Issue 0.1 signature缺失, Issue 0.2 custom工具格式
改进: Codex审查发现的2个重要问题
测试:
- TestBuildParts_ThinkingBlockWithoutSignature: 验证thinking block处理
- TestBuildTools_CustomTypeTools: 验证custom工具转换和边界情况
- TestConvertClaudeToolsToGeminiTools_CustomType: 验证service层转换
* fix(format): 修复 gofmt 格式问题
- 修复 claude_types.go 中的字段对齐问题
- 修复 gemini_messages_compat_service.go 中的缩进问题
* fix(format): 修复 claude_types.go 的 gofmt 格式问题
* feat(antigravity): 优化 thinking block 和 schema 处理
- 为 dummy thinking block 添加 ThoughtSignature
- 重构 thinking block 处理逻辑,在每个条件分支内创建 part
- 优化 excludedSchemaKeys,移除 Gemini 实际支持的字段
(minItems, maxItems, minimum, maximum, additionalProperties, format)
- 添加详细注释说明 Gemini API 支持的 schema 字段
* fix(antigravity): 增强 schema 清理的安全性
基于 Codex review 建议:
- 添加 format 字段白名单过滤,只保留 Gemini 支持的 date-time/date/time
- 补充更多不支持的 schema 关键字到黑名单:
* 组合 schema: oneOf, anyOf, allOf, not, if/then/else
* 对象验证: minProperties, maxProperties, patternProperties 等
* 定义引用: $defs, definitions
- 避免不支持的 schema 字段导致 Gemini API 校验失败
* fix(lint): 修复 gemini_messages_compat_service 空分支警告
- 在 cleanToolSchema 的 if 语句中添加 continue
- 移除重复的注释
* fix(antigravity): 移除 minItems/maxItems 以兼容 Claude API
- 将 minItems 和 maxItems 添加到 schema 黑名单
- Claude API (Vertex AI) 不支持这些数组验证字段
- 添加调试日志记录工具 schema 转换过程
- 修复 tools.14.custom.input_schema 验证错误
* fix(antigravity): 修复 additionalProperties schema 对象问题
- 将 additionalProperties 的 schema 对象转换为布尔值 true
- Claude API 只支持 additionalProperties: false,不支持 schema 对象
- 修复 tools.14.custom.input_schema 验证错误
- 参考 Claude 官方文档的 JSON Schema 限制
* fix(antigravity): 修复 Claude 模型 thinking 块兼容性问题
- 完全跳过 Claude 模型的 thinking 块以避免 signature 验证失败
- 只在 Gemini 模型中使用 dummy thought signature
- 修改 additionalProperties 默认值为 false(更安全)
- 添加调试日志以便排查问题
* fix(upstream): 修复跨模型切换时的 dummy signature 问题
基于 Codex review 和用户场景分析的修复:
1. 问题场景
- Gemini (thinking) → Claude (thinking) 切换时
- Gemini 返回的 thinking 块使用 dummy signature
- Claude API 会拒绝 dummy signature,导致 400 错误
2. 修复内容
- request_transformer.go:262: 跳过 dummy signature
- 只保留真实的 Claude signature
- 支持频繁的跨模型切换
3. 其他修复(基于 Codex review)
- gateway_service.go:691: 修复 io.ReadAll 错误处理
- gateway_service.go:687: 条件日志(尊重 LogUpstreamErrorBody 配置)
- gateway_service.go:915: 收紧 400 failover 启发式
- request_transformer.go:188: 移除签名成功日志
4. 新增功能(默认关闭)
- 阶段 1: 上游错误日志(GATEWAY_LOG_UPSTREAM_ERROR_BODY)
- 阶段 2: Antigravity thinking 修复
- 阶段 3: API-key beta 注入(GATEWAY_INJECT_BETA_FOR_APIKEY)
- 阶段 3: 智能 400 failover(GATEWAY_FAILOVER_ON_400)
测试:所有测试通过
* fix(lint): 修复 golangci-lint 问题
- 应用 De Morgan 定律简化条件判断
- 修复 gofmt 格式问题
- 移除未使用的 min 函数
2026-01-01 10:45:57 +08:00
|
|
|
|
if strings.TrimSpace(tool.Name) == "" {
|
2025-12-31 21:44:56 +08:00
|
|
|
|
log.Printf("Warning: skipping tool with empty name")
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-31 20:56:38 +08:00
|
|
|
|
var description string
|
|
|
|
|
|
var inputSchema map[string]any
|
|
|
|
|
|
|
|
|
|
|
|
// 检查是否为 custom 类型工具 (MCP)
|
fix: 修复 /v1/messages 间歇性 400 错误 (#112)
* fix(upstream): 修复上游格式兼容性问题
- 跳过Claude模型无signature的thinking block
- 支持custom类型工具(MCP)格式转换
- 添加ClaudeCustomToolSpec结构体支持MCP工具
- 添加Custom字段验证,跳过无效custom工具
- 在convertClaudeToolsToGeminiTools中添加schema清理
- 完整的单元测试覆盖,包含边界情况
修复: Issue 0.1 signature缺失, Issue 0.2 custom工具格式
改进: Codex审查发现的2个重要问题
测试:
- TestBuildParts_ThinkingBlockWithoutSignature: 验证thinking block处理
- TestBuildTools_CustomTypeTools: 验证custom工具转换和边界情况
- TestConvertClaudeToolsToGeminiTools_CustomType: 验证service层转换
* feat(gemini): 添加Gemini限额与TierID支持
实现PR1:Gemini限额与TierID功能
后端修改:
- GeminiTokenInfo结构体添加TierID字段
- fetchProjectID函数返回(projectID, tierID, error)
- 从LoadCodeAssist响应中提取tierID(优先IsDefault,回退到第一个非空tier)
- ExchangeCode、RefreshAccountToken、GetAccessToken函数更新以处理tierID
- BuildAccountCredentials函数保存tier_id到credentials
前端修改:
- AccountStatusIndicator组件添加tier显示
- 支持LEGACY/PRO/ULTRA等tier类型的友好显示
- 使用蓝色badge展示tier信息
技术细节:
- tierID提取逻辑:优先选择IsDefault的tier,否则选择第一个非空tier
- 所有fetchProjectID调用点已更新以处理新的返回签名
- 前端gracefully处理missing/unknown tier_id
* refactor(gemini): 优化TierID实现并添加安全验证
根据并发代码审查(code-reviewer, security-auditor, gemini, codex)的反馈进行改进:
安全改进:
- 添加validateTierID函数验证tier_id格式和长度(最大64字符)
- 限制tier_id字符集为字母数字、下划线、连字符和斜杠
- 在BuildAccountCredentials中验证tier_id后再存储
- 静默跳过无效tier_id,不阻塞账户创建
代码质量改进:
- 提取extractTierIDFromAllowedTiers辅助函数消除重复代码
- 重构fetchProjectID函数,tierID提取逻辑只执行一次
- 改进代码可读性和可维护性
审查工具:
- code-reviewer agent (a09848e)
- security-auditor agent (a9a149c)
- gemini CLI (bcc7c81)
- codex (b5d8919)
修复问题:
- HIGH: 未验证的tier_id输入
- MEDIUM: 代码重复(tierID提取逻辑重复2次)
* fix(format): 修复 gofmt 格式问题
- 修复 claude_types.go 中的字段对齐问题
- 修复 gemini_messages_compat_service.go 中的缩进问题
* fix(upstream): 修复上游格式兼容性问题 (#14)
* fix(upstream): 修复上游格式兼容性问题
- 跳过Claude模型无signature的thinking block
- 支持custom类型工具(MCP)格式转换
- 添加ClaudeCustomToolSpec结构体支持MCP工具
- 添加Custom字段验证,跳过无效custom工具
- 在convertClaudeToolsToGeminiTools中添加schema清理
- 完整的单元测试覆盖,包含边界情况
修复: Issue 0.1 signature缺失, Issue 0.2 custom工具格式
改进: Codex审查发现的2个重要问题
测试:
- TestBuildParts_ThinkingBlockWithoutSignature: 验证thinking block处理
- TestBuildTools_CustomTypeTools: 验证custom工具转换和边界情况
- TestConvertClaudeToolsToGeminiTools_CustomType: 验证service层转换
* fix(format): 修复 gofmt 格式问题
- 修复 claude_types.go 中的字段对齐问题
- 修复 gemini_messages_compat_service.go 中的缩进问题
* fix(format): 修复 claude_types.go 的 gofmt 格式问题
* feat(antigravity): 优化 thinking block 和 schema 处理
- 为 dummy thinking block 添加 ThoughtSignature
- 重构 thinking block 处理逻辑,在每个条件分支内创建 part
- 优化 excludedSchemaKeys,移除 Gemini 实际支持的字段
(minItems, maxItems, minimum, maximum, additionalProperties, format)
- 添加详细注释说明 Gemini API 支持的 schema 字段
* fix(antigravity): 增强 schema 清理的安全性
基于 Codex review 建议:
- 添加 format 字段白名单过滤,只保留 Gemini 支持的 date-time/date/time
- 补充更多不支持的 schema 关键字到黑名单:
* 组合 schema: oneOf, anyOf, allOf, not, if/then/else
* 对象验证: minProperties, maxProperties, patternProperties 等
* 定义引用: $defs, definitions
- 避免不支持的 schema 字段导致 Gemini API 校验失败
* fix(lint): 修复 gemini_messages_compat_service 空分支警告
- 在 cleanToolSchema 的 if 语句中添加 continue
- 移除重复的注释
* fix(antigravity): 移除 minItems/maxItems 以兼容 Claude API
- 将 minItems 和 maxItems 添加到 schema 黑名单
- Claude API (Vertex AI) 不支持这些数组验证字段
- 添加调试日志记录工具 schema 转换过程
- 修复 tools.14.custom.input_schema 验证错误
* fix(antigravity): 修复 additionalProperties schema 对象问题
- 将 additionalProperties 的 schema 对象转换为布尔值 true
- Claude API 只支持 additionalProperties: false,不支持 schema 对象
- 修复 tools.14.custom.input_schema 验证错误
- 参考 Claude 官方文档的 JSON Schema 限制
* fix(antigravity): 修复 Claude 模型 thinking 块兼容性问题
- 完全跳过 Claude 模型的 thinking 块以避免 signature 验证失败
- 只在 Gemini 模型中使用 dummy thought signature
- 修改 additionalProperties 默认值为 false(更安全)
- 添加调试日志以便排查问题
* fix(upstream): 修复跨模型切换时的 dummy signature 问题
基于 Codex review 和用户场景分析的修复:
1. 问题场景
- Gemini (thinking) → Claude (thinking) 切换时
- Gemini 返回的 thinking 块使用 dummy signature
- Claude API 会拒绝 dummy signature,导致 400 错误
2. 修复内容
- request_transformer.go:262: 跳过 dummy signature
- 只保留真实的 Claude signature
- 支持频繁的跨模型切换
3. 其他修复(基于 Codex review)
- gateway_service.go:691: 修复 io.ReadAll 错误处理
- gateway_service.go:687: 条件日志(尊重 LogUpstreamErrorBody 配置)
- gateway_service.go:915: 收紧 400 failover 启发式
- request_transformer.go:188: 移除签名成功日志
4. 新增功能(默认关闭)
- 阶段 1: 上游错误日志(GATEWAY_LOG_UPSTREAM_ERROR_BODY)
- 阶段 2: Antigravity thinking 修复
- 阶段 3: API-key beta 注入(GATEWAY_INJECT_BETA_FOR_APIKEY)
- 阶段 3: 智能 400 failover(GATEWAY_FAILOVER_ON_400)
测试:所有测试通过
* fix(lint): 修复 golangci-lint 问题
- 应用 De Morgan 定律简化条件判断
- 修复 gofmt 格式问题
- 移除未使用的 min 函数
2026-01-01 10:45:57 +08:00
|
|
|
|
if tool.Type == "custom" {
|
|
|
|
|
|
if tool.Custom == nil || tool.Custom.InputSchema == nil {
|
|
|
|
|
|
log.Printf("[Warning] Skipping invalid custom tool '%s': missing custom spec or input_schema", tool.Name)
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
2025-12-31 20:56:38 +08:00
|
|
|
|
description = tool.Custom.Description
|
|
|
|
|
|
inputSchema = tool.Custom.InputSchema
|
fix: 修复 /v1/messages 间歇性 400 错误 (#112)
* fix(upstream): 修复上游格式兼容性问题
- 跳过Claude模型无signature的thinking block
- 支持custom类型工具(MCP)格式转换
- 添加ClaudeCustomToolSpec结构体支持MCP工具
- 添加Custom字段验证,跳过无效custom工具
- 在convertClaudeToolsToGeminiTools中添加schema清理
- 完整的单元测试覆盖,包含边界情况
修复: Issue 0.1 signature缺失, Issue 0.2 custom工具格式
改进: Codex审查发现的2个重要问题
测试:
- TestBuildParts_ThinkingBlockWithoutSignature: 验证thinking block处理
- TestBuildTools_CustomTypeTools: 验证custom工具转换和边界情况
- TestConvertClaudeToolsToGeminiTools_CustomType: 验证service层转换
* feat(gemini): 添加Gemini限额与TierID支持
实现PR1:Gemini限额与TierID功能
后端修改:
- GeminiTokenInfo结构体添加TierID字段
- fetchProjectID函数返回(projectID, tierID, error)
- 从LoadCodeAssist响应中提取tierID(优先IsDefault,回退到第一个非空tier)
- ExchangeCode、RefreshAccountToken、GetAccessToken函数更新以处理tierID
- BuildAccountCredentials函数保存tier_id到credentials
前端修改:
- AccountStatusIndicator组件添加tier显示
- 支持LEGACY/PRO/ULTRA等tier类型的友好显示
- 使用蓝色badge展示tier信息
技术细节:
- tierID提取逻辑:优先选择IsDefault的tier,否则选择第一个非空tier
- 所有fetchProjectID调用点已更新以处理新的返回签名
- 前端gracefully处理missing/unknown tier_id
* refactor(gemini): 优化TierID实现并添加安全验证
根据并发代码审查(code-reviewer, security-auditor, gemini, codex)的反馈进行改进:
安全改进:
- 添加validateTierID函数验证tier_id格式和长度(最大64字符)
- 限制tier_id字符集为字母数字、下划线、连字符和斜杠
- 在BuildAccountCredentials中验证tier_id后再存储
- 静默跳过无效tier_id,不阻塞账户创建
代码质量改进:
- 提取extractTierIDFromAllowedTiers辅助函数消除重复代码
- 重构fetchProjectID函数,tierID提取逻辑只执行一次
- 改进代码可读性和可维护性
审查工具:
- code-reviewer agent (a09848e)
- security-auditor agent (a9a149c)
- gemini CLI (bcc7c81)
- codex (b5d8919)
修复问题:
- HIGH: 未验证的tier_id输入
- MEDIUM: 代码重复(tierID提取逻辑重复2次)
* fix(format): 修复 gofmt 格式问题
- 修复 claude_types.go 中的字段对齐问题
- 修复 gemini_messages_compat_service.go 中的缩进问题
* fix(upstream): 修复上游格式兼容性问题 (#14)
* fix(upstream): 修复上游格式兼容性问题
- 跳过Claude模型无signature的thinking block
- 支持custom类型工具(MCP)格式转换
- 添加ClaudeCustomToolSpec结构体支持MCP工具
- 添加Custom字段验证,跳过无效custom工具
- 在convertClaudeToolsToGeminiTools中添加schema清理
- 完整的单元测试覆盖,包含边界情况
修复: Issue 0.1 signature缺失, Issue 0.2 custom工具格式
改进: Codex审查发现的2个重要问题
测试:
- TestBuildParts_ThinkingBlockWithoutSignature: 验证thinking block处理
- TestBuildTools_CustomTypeTools: 验证custom工具转换和边界情况
- TestConvertClaudeToolsToGeminiTools_CustomType: 验证service层转换
* fix(format): 修复 gofmt 格式问题
- 修复 claude_types.go 中的字段对齐问题
- 修复 gemini_messages_compat_service.go 中的缩进问题
* fix(format): 修复 claude_types.go 的 gofmt 格式问题
* feat(antigravity): 优化 thinking block 和 schema 处理
- 为 dummy thinking block 添加 ThoughtSignature
- 重构 thinking block 处理逻辑,在每个条件分支内创建 part
- 优化 excludedSchemaKeys,移除 Gemini 实际支持的字段
(minItems, maxItems, minimum, maximum, additionalProperties, format)
- 添加详细注释说明 Gemini API 支持的 schema 字段
* fix(antigravity): 增强 schema 清理的安全性
基于 Codex review 建议:
- 添加 format 字段白名单过滤,只保留 Gemini 支持的 date-time/date/time
- 补充更多不支持的 schema 关键字到黑名单:
* 组合 schema: oneOf, anyOf, allOf, not, if/then/else
* 对象验证: minProperties, maxProperties, patternProperties 等
* 定义引用: $defs, definitions
- 避免不支持的 schema 字段导致 Gemini API 校验失败
* fix(lint): 修复 gemini_messages_compat_service 空分支警告
- 在 cleanToolSchema 的 if 语句中添加 continue
- 移除重复的注释
* fix(antigravity): 移除 minItems/maxItems 以兼容 Claude API
- 将 minItems 和 maxItems 添加到 schema 黑名单
- Claude API (Vertex AI) 不支持这些数组验证字段
- 添加调试日志记录工具 schema 转换过程
- 修复 tools.14.custom.input_schema 验证错误
* fix(antigravity): 修复 additionalProperties schema 对象问题
- 将 additionalProperties 的 schema 对象转换为布尔值 true
- Claude API 只支持 additionalProperties: false,不支持 schema 对象
- 修复 tools.14.custom.input_schema 验证错误
- 参考 Claude 官方文档的 JSON Schema 限制
* fix(antigravity): 修复 Claude 模型 thinking 块兼容性问题
- 完全跳过 Claude 模型的 thinking 块以避免 signature 验证失败
- 只在 Gemini 模型中使用 dummy thought signature
- 修改 additionalProperties 默认值为 false(更安全)
- 添加调试日志以便排查问题
* fix(upstream): 修复跨模型切换时的 dummy signature 问题
基于 Codex review 和用户场景分析的修复:
1. 问题场景
- Gemini (thinking) → Claude (thinking) 切换时
- Gemini 返回的 thinking 块使用 dummy signature
- Claude API 会拒绝 dummy signature,导致 400 错误
2. 修复内容
- request_transformer.go:262: 跳过 dummy signature
- 只保留真实的 Claude signature
- 支持频繁的跨模型切换
3. 其他修复(基于 Codex review)
- gateway_service.go:691: 修复 io.ReadAll 错误处理
- gateway_service.go:687: 条件日志(尊重 LogUpstreamErrorBody 配置)
- gateway_service.go:915: 收紧 400 failover 启发式
- request_transformer.go:188: 移除签名成功日志
4. 新增功能(默认关闭)
- 阶段 1: 上游错误日志(GATEWAY_LOG_UPSTREAM_ERROR_BODY)
- 阶段 2: Antigravity thinking 修复
- 阶段 3: API-key beta 注入(GATEWAY_INJECT_BETA_FOR_APIKEY)
- 阶段 3: 智能 400 failover(GATEWAY_FAILOVER_ON_400)
测试:所有测试通过
* fix(lint): 修复 golangci-lint 问题
- 应用 De Morgan 定律简化条件判断
- 修复 gofmt 格式问题
- 移除未使用的 min 函数
2026-01-01 10:45:57 +08:00
|
|
|
|
|
2025-12-31 20:56:38 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
// 标准格式: 从顶层字段获取
|
|
|
|
|
|
description = tool.Description
|
|
|
|
|
|
inputSchema = tool.InputSchema
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-28 18:41:55 +08:00
|
|
|
|
// 清理 JSON Schema
|
2026-01-20 23:41:53 +08:00
|
|
|
|
// 1. 深度清理 [undefined] 值
|
|
|
|
|
|
DeepCleanUndefined(inputSchema)
|
|
|
|
|
|
// 2. 转换为符合 Gemini v1internal 的 schema
|
|
|
|
|
|
params := CleanJSONSchema(inputSchema)
|
2025-12-31 21:44:56 +08:00
|
|
|
|
// 为 nil schema 提供默认值
|
|
|
|
|
|
if params == nil {
|
|
|
|
|
|
params = map[string]any{
|
2026-01-20 23:41:53 +08:00
|
|
|
|
"type": "object", // lowercase type
|
2025-12-31 21:44:56 +08:00
|
|
|
|
"properties": map[string]any{},
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-28 18:41:55 +08:00
|
|
|
|
funcDecls = append(funcDecls, GeminiFunctionDecl{
|
|
|
|
|
|
Name: tool.Name,
|
2025-12-31 20:56:38 +08:00
|
|
|
|
Description: description,
|
2025-12-28 18:41:55 +08:00
|
|
|
|
Parameters: params,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if len(funcDecls) == 0 {
|
2026-01-18 01:09:40 +08:00
|
|
|
|
if !hasWebSearch {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Web Search 工具映射
|
|
|
|
|
|
return []GeminiToolDeclaration{{
|
|
|
|
|
|
GoogleSearch: &GeminiGoogleSearch{
|
|
|
|
|
|
EnhancedContent: &GeminiEnhancedContent{
|
|
|
|
|
|
ImageSearch: &GeminiImageSearch{
|
|
|
|
|
|
MaxResultCount: 5,
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
}}
|
2025-12-28 18:41:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return []GeminiToolDeclaration{{
|
|
|
|
|
|
FunctionDeclarations: funcDecls,
|
|
|
|
|
|
}}
|
|
|
|
|
|
}
|