From 0059a232a6c29dbabb5403ce3730de100bf954fa Mon Sep 17 00:00:00 2001 From: song Date: Mon, 26 Jan 2026 20:51:40 +0800 Subject: [PATCH] =?UTF-8?q?feat(gemini):=20=E4=B8=BA=20Gemini=20=E5=8E=9F?= =?UTF-8?q?=E7=94=9F=E5=B9=B3=E5=8F=B0=E6=B7=BB=E5=8A=A0=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E8=AE=A1=E8=B4=B9=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 对齐 Antigravity 平台的图片计费逻辑: - 添加 extractImageSize() 方法提取图片尺寸 - Forward() 和 ForwardNative() 返回 ImageCount/ImageSize - 支持分组自定义图片价格和倍率 --- .../service/gemini_messages_compat_service.go | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/backend/internal/service/gemini_messages_compat_service.go b/backend/internal/service/gemini_messages_compat_service.go index 396c48293..aea880c25 100644 --- a/backend/internal/service/gemini_messages_compat_service.go +++ b/backend/internal/service/gemini_messages_compat_service.go @@ -931,6 +931,13 @@ func (s *GeminiMessagesCompatService) Forward(ctx context.Context, c *gin.Contex } } + // 图片生成计费 + imageCount := 0 + imageSize := s.extractImageSize(body) + if isImageGenerationModel(originalModel) { + imageCount = 1 + } + return &ForwardResult{ RequestID: requestID, Usage: *usage, @@ -938,6 +945,8 @@ func (s *GeminiMessagesCompatService) Forward(ctx context.Context, c *gin.Contex Stream: req.Stream, Duration: time.Since(startTime), FirstTokenMs: firstTokenMs, + ImageCount: imageCount, + ImageSize: imageSize, }, nil } @@ -1371,6 +1380,13 @@ func (s *GeminiMessagesCompatService) ForwardNative(ctx context.Context, c *gin. usage = &ClaudeUsage{} } + // 图片生成计费 + imageCount := 0 + imageSize := s.extractImageSize(body) + if isImageGenerationModel(originalModel) { + imageCount = 1 + } + return &ForwardResult{ RequestID: requestID, Usage: *usage, @@ -1378,6 +1394,8 @@ func (s *GeminiMessagesCompatService) ForwardNative(ctx context.Context, c *gin. Stream: stream, Duration: time.Since(startTime), FirstTokenMs: firstTokenMs, + ImageCount: imageCount, + ImageSize: imageSize, }, nil } @@ -3031,3 +3049,26 @@ func convertClaudeGenerationConfig(req map[string]any) map[string]any { } return out } + +// extractImageSize 从 Gemini 请求中提取 image_size 参数 +func (s *GeminiMessagesCompatService) extractImageSize(body []byte) string { + var req struct { + GenerationConfig *struct { + ImageConfig *struct { + ImageSize string `json:"imageSize"` + } `json:"imageConfig"` + } `json:"generationConfig"` + } + if err := json.Unmarshal(body, &req); err != nil { + return "2K" + } + + if req.GenerationConfig != nil && req.GenerationConfig.ImageConfig != nil { + size := strings.ToUpper(strings.TrimSpace(req.GenerationConfig.ImageConfig.ImageSize)) + if size == "1K" || size == "2K" || size == "4K" { + return size + } + } + + return "2K" +}