Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Convert Google Gemini Web into OpenAI, Claude, and Gemini compatible APIs.
| `gemini-4-hydrogen-task`, `gemini-4-carbon-task`, `gemini-4-neon-task` | Task | Tasks performed on your behalf | | |
| `gemini-default` | Default | Each account's official default model | | |
| `gemini-3.1-flash-image` | Nano Banana 2 | Image generation and editing | | |
| `gemini-gif` | GIF | Animated GIF generation and editing | | |
| `gemini-video` | Video | Video generation | | |
| `gemini-music` | Music | Music generation | | |
| `gemini-canvas` | Canvas | Documents and code (hidden by default; toggle on Models page) | | |
Expand All @@ -60,9 +61,9 @@ Convert Google Gemini Web into OpenAI, Claude, and Gemini compatible APIs.
- **Subscription tiers**: a model that only a higher subscription tier offers returns 403 with the required tier and the current accounts' tiers
- **Catalog fields**: `available_account_count` is the number of usable accounts, `default` marks the default model, and `thinking_levels` lists the available thinking levels
- **Tool models**: tool IDs appear only for accounts that have the corresponding official tool and run on that account's official default model
- **Images**: the image ID works with conversational image requests and `/v1/images/*`; `size` and `imageConfig.aspectRatio` support 1:1, 9:16, 3:4, 4:3, 16:9, 2:3, 21:9, 1:8, and 8:1
- **Images**: the image IDs work with conversational image requests, Gemini `generateContent` / `:predict`, and `/v1/images/*` (text-to-image and multi-image editing); `size` and `imageConfig.aspectRatio` support 1:1, 9:16, 3:4, 4:3, 16:9, 2:3, 21:9, 1:8, and 8:1 (`gemini-gif` snaps to 16:9 or 9:16)
- **Video**: for video IDs, `imageConfig.aspectRatio` supports landscape 16:9 and portrait 9:16
- **Chat history**: Task, Deep Research, and video requests use saved chats; Deep Research and video chats remain in the account history, and with `GEMINI_SAVE_HISTORY=false` (the default) a Task chat is deleted from the account together with its task record once generation has ended for over a minute without a follow-up, or when the service stops
- **Chat history**: Task, Deep Research, animated GIF, and video requests use saved chats; Deep Research, animated GIF, and video chats remain in the account history, and with `GEMINI_SAVE_HISTORY=false` (the default) a Task chat is deleted from the account together with its task record once generation has ended for over a minute without a follow-up, or when the service stops
- **Deep Research**: the research plan and progress are returned as reasoning and the report as text, with the report's citations returned as web sources in each protocol's citation fields; a run usually takes over ten minutes, so set the client request timeout longer than that
- **Quotas**: context windows, model access, and quotas depend on the account's plan and region; account usage is shown on the admin Accounts page
- **Token usage**: text input, visible output, and reasoning summaries are counted with a local Gemini tokenizer; the token counting endpoints estimate images, audio, video, and PDFs with the Gemini API counting rules
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
| `gemini-4-hydrogen-task`、`gemini-4-carbon-task`、`gemini-4-neon-task` | Task | 代为执行任务 | | |
| `gemini-default` | Default | 各账号的官方默认模型 | | |
| `gemini-3.1-flash-image` | Nano Banana 2 | 图片生成与编辑 | | |
| `gemini-gif` | GIF | 动图生成与编辑 | | |
| `gemini-video` | Video | 视频生成 | | |
| `gemini-music` | Music | 音乐生成 | | |
| `gemini-canvas` | Canvas | 文档与代码(默认关闭,可在模型页开启) | | |
Expand All @@ -60,9 +61,9 @@
- **订阅层级**:只有更高订阅层级才有的模型返回 403,写明所需层级与当前账号的层级
- **目录字段**:`available_account_count` 是可用账号数,`default` 标出默认模型,`thinking_levels` 列出可选思考强度
- **工具模型**:工具 ID 只在账号具备对应官网工具时出现,由该账号的官网默认模型执行
- **图片**:图片 ID 用于对话生图和 `/v1/images/*`,`size` 与 `imageConfig.aspectRatio` 支持 1:1、9:16、3:4、4:3、16:9、2:3、21:9、1:8、8:1
- **图片**:图片 ID 用于对话生图、Gemini `generateContent` / `:predict` 和 `/v1/images/*`(支持文生图与多图图生图),`size` 与 `imageConfig.aspectRatio` 支持 1:1、9:16、3:4、4:3、16:9、2:3、21:9、1:8、8:1(`gemini-gif` 动图对齐到官网 16:9 或 9:16)
- **视频**:视频 ID 的 `imageConfig.aspectRatio` 支持横向 16:9 与竖向 9:16
- **会话历史**:Task、Deep Research 与视频请求使用普通会话;Deep Research 与视频的对话保存在账号历史中,`GEMINI_SAVE_HISTORY=false`(默认)时,Task 的会话在生成结束超过一分钟且没有续接后或服务停止时,连同任务记录从账号中删除
- **会话历史**:Task、Deep Research、GIF 动图与视频请求使用普通会话;Deep Research、GIF 与视频的对话保存在账号历史中,`GEMINI_SAVE_HISTORY=false`(默认)时,Task 的会话在生成结束超过一分钟且没有续接后或服务停止时,连同任务记录从账号中删除
- **Deep Research**:研究计划与进度作为思考内容返回,报告作为正文返回,报告中的引用以各协议的引用字段给出网页来源;一次通常需要十几分钟,客户端请求超时要设得更长
- **配额**:上下文窗口、模型权限和配额由账号的套餐与地区决定,账号用量在管理端账号页查看
- **token usage**:文本输入、可见输出和思考摘要按 Gemini tokenizer 计算;计数接口中的图片、音频、视频与 PDF 按 Gemini API 计数规则估算
Expand Down
7 changes: 5 additions & 2 deletions docs/protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ payload[24] = ["neon_launch_ultra_policy",[
| 公开 ID | 官网工具 | featureMode | 能力号 |
| --- | --- | ---: | ---: |
| `gemini-3.1-flash-image` | Create image | `14` | `82` |
| `gemini-gif` | Create image(GIF) | `14` | `82`、`247` |
| `gemini-video` | Create video | `11` | `71`、`140` |
| `gemini-music` | Create music | `21` | `139` |
| `gemini-canvas` | Canvas | `2` | `51` |
Expand All @@ -292,7 +293,7 @@ payload[24] = ["neon_launch_ultra_policy",[
- **模型内容**:显示名称与说明取 Gem 行 `[ID,[名称,说明],[指令]]`,协调模型取账号默认模型,生成请求在 `inner[19]` 写 Gem ID
- **读取失败**:重读时 Gem 列表读取失败(如 `CNgdBe` 返回 1096“无法加载对话”)沿用上次读到的 Gem;首次初始化就失败的账号不提供 Gem,其余模型照常可用

Task、Deep Research 只在普通会话中提供,视频成品在生成流结束后从会话读取;这三类请求在 `GEMINI_SAVE_HISTORY=false` 时也使用普通会话。Deep Research 与 Create video 的会话保留在账号历史中;Task 会话的删除见 [Task](#task) 的“不保存历史时的清理”。
Task、Deep Research、GIF 动图只在普通会话中提供,视频与 GIF 成品在生成流结束后从会话读取;这几类请求在 `GEMINI_SAVE_HISTORY=false` 时也使用普通会话。Deep Research、Create image(GIF)与 Create video 的会话保留在账号历史中;Task 会话的删除见 [Task](#task) 的“不保存历史时的清理”。

`payload[19]` 是工具选项组。组槽 `0` 为 featureMode,`3` 为组名,`7` 为选项;选项槽 `0` 为选项 ID,`2` 为默认选中,`3` 为显示名称。Pro 账号的选项组为:

Expand Down Expand Up @@ -1162,7 +1163,9 @@ candidate[12][7] = [[[

同一 payload 的镜像结构也在 `payload[26][0][0][0][9][0][0][3][18]` 重复 generator tag;当前解码器以候选 rich content 为主读取路径。

服务公开的 `gemini-3.1-flash-image` 见「工具条目」,协调模型为账号默认模型。该 ID 支持对话入口、Gemini `generateContent` 与 `/v1/images/generations`、`/v1/images/edits`;Responses 的 `image_generation` 工具对所选模型发送 Create image。OpenAI Chat/Responses 通过 `provider_model` 返回实际 generator tag。
服务公开的 `gemini-3.1-flash-image` 见「工具条目」,协调模型为账号默认模型。该 ID 支持对话入口、Gemini `generateContent` / `:predict` 与 `/v1/images/generations`、`/v1/images/edits`(均支持文生图与最多 10 张参考图);Responses 的 `image_generation` 工具对所选模型发送 Create image。OpenAI Chat/Responses 通过 `provider_model` 返回实际 generator tag。

服务同时公开 `gemini-gif`(Create image 工具并强制选中格式选项 `64`,宽高比自动对齐 `16:9` 或 `9:16`):该请求在持久化会话中以 `application/x-protobuf` 二进制 WebChannel 通道发送和解码,服务端收到后台生成标记后以 `5s` 间隔调用 `hNvQHb` 轮询直至成品完成,并在 OpenAI `/v1/images/*` 与 Gemini `generateContent`(兼容 `responseModalities: ["IMAGE"]`) / `:predict` 中按标准 `image/gif` 返回,`provider_model` 为 `models/omni_1p5_gif`。

图片生成期间还会先出现根级稀疏进度:

Expand Down
58 changes: 52 additions & 6 deletions internal/adapter/gemini_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ func GeminiRouterHandler(pool *balancer.AccountPool, store *requestdb.Store) gin
bindScope(c, pool, model)
switch method {
case "generateContent", "streamGenerateContent", "countTokens":
case "predict":
if imageModel(pool, model) {
geminiImagePredict(c, pool, model)
return
}
writeGeminiError(c, http.StatusNotFound, "NOT_FOUND", geminiUnsupportedMethod(model, method, geminiMethodCapability(method)))
return
case "predictLongRunning":
if videoModel(pool, model) {
geminiVideoOperation(c, pool, store, model)
Expand Down Expand Up @@ -595,19 +602,53 @@ func geminiToolBridge(request GeminiGenerateContentRequest) (ToolBridge, []strin
return bridge, nil, err
}
for field := range config {
if field == "responseMimeType" || field == "responseJsonSchema" || field == "responseSchema" || field == "responseFormat" || field == "thinkingConfig" || field == "imageConfig" || field == "candidateCount" || field == "stopSequences" || field == "maxOutputTokens" {
if field == "responseMimeType" || field == "responseJsonSchema" || field == "responseSchema" || field == "responseFormat" || field == "responseModalities" || field == "thinkingConfig" || field == "imageConfig" || field == "candidateCount" || field == "stopSequences" || field == "maxOutputTokens" {
continue
}
unsupported = append(unsupported, "generationConfig."+field)
}
if raw, ok := config["responseModalities"]; ok && len(raw) > 0 && string(raw) != "null" {
var modalities []string
if err := json.Unmarshal(raw, &modalities); err != nil {
return bridge, nil, fmt.Errorf("generationConfig.responseModalities 必须是字符串数组")
}
var hasImage, hasText bool
for _, modality := range modalities {
switch strings.ToUpper(strings.TrimSpace(modality)) {
case "IMAGE":
hasImage = true
case "TEXT":
hasText = true
}
}
if hasImage && !hasText {
bridge.ImageOnly = true
}
}
var imageConfig map[string]json.RawMessage
_ = json.Unmarshal(config["imageConfig"], &imageConfig)
if len(config["imageConfig"]) > 0 && string(config["imageConfig"]) != "null" {
if err := json.Unmarshal(config["imageConfig"], &imageConfig); err != nil {
return bridge, nil, fmt.Errorf("generationConfig.imageConfig 必须是对象")
}
}
for field := range imageConfig {
if field != "aspectRatio" {
if field != "aspectRatio" && field != "outputMimeType" {
unsupported = append(unsupported, "generationConfig.imageConfig."+field)
}
}
_ = json.Unmarshal(imageConfig["aspectRatio"], &bridge.AspectRatio)
var rawAspectRatio string
_ = json.Unmarshal(imageConfig["aspectRatio"], &rawAspectRatio)
bridge.AspectRatio = normalizeGeminiAspectRatio(rawAspectRatio)
if rawOutMime := imageConfig["outputMimeType"]; len(rawOutMime) > 0 && string(rawOutMime) != "null" {
var outMime string
if err := json.Unmarshal(rawOutMime, &outMime); err != nil {
return bridge, nil, fmt.Errorf("generationConfig.imageConfig.outputMimeType 必须是字符串")
}
outMime = strings.ToLower(strings.TrimSpace(outMime))
if outMime != "" && outMime != "image/gif" && outMime != "image/png" && outMime != "image/jpeg" && outMime != "image/webp" {
unsupported = append(unsupported, "generationConfig.imageConfig.outputMimeType")
}
}
if raw, ok := config["maxOutputTokens"]; ok {
if err := json.Unmarshal(raw, &bridge.MaxTokens); err != nil {
return bridge, nil, fmt.Errorf("generationConfig.maxOutputTokens 必须是整数")
Expand Down Expand Up @@ -645,7 +686,7 @@ func geminiToolBridge(request GeminiGenerateContentRequest) (ToolBridge, []strin
}
if format.Image != nil {
if format.Image.AspectRatio != "" {
bridge.AspectRatio = geminiResponseFormatValue(format.Image.AspectRatio)
bridge.AspectRatio = normalizeGeminiAspectRatio(geminiResponseFormatValue(format.Image.AspectRatio))
}
if format.Image.ImageSize != "" {
unsupported = append(unsupported, "generationConfig.responseFormat.image.imageSize")
Expand All @@ -656,6 +697,7 @@ func geminiToolBridge(request GeminiGenerateContentRequest) (ToolBridge, []strin
}
}
switch {
case strings.HasPrefix(strings.ToLower(strings.TrimSpace(mime)), "image/"):
case mime == "text/x.enum":
var enum struct {
Enum []string `json:"enum"`
Expand Down Expand Up @@ -805,7 +847,11 @@ func buildGeminiResponse(result generationResult, bridge ToolBridge, includeThou
if includeThoughts && primary.Thought != "" {
parts = append(parts, gin.H{"text": primary.Thought, "thought": true})
}
for _, part := range geminiStructuredParts(content, primary) {
displayText := content
if bridge.ImageOnly && hasGeneratedImage(primary) {
displayText = ""
}
for _, part := range geminiStructuredParts(displayText, primary) {
parts = append(parts, geminiOutputPart(part))
}
for index, call := range calls {
Expand Down
Loading