[Feature] Add Gemini 3.0 image generation with configurable sizes#638
Conversation
- Update image generation feature detection to support all image-capable models - Broaden model compatibility check from specific model names to generic 'image' keyword - Update documentation to reflect Gemini 3.0 Pro Image Preview support - Bump package version to 1.3.8 This change makes the image generation feature more flexible and future-proof by detecting any model with 'image' in its name, rather than maintaining a hardcoded list of specific model versions.
…ration This commit adds support for configurable image sizes (2K, 4K) in Gemini 3.0 image generation models. Changes: - adapter-gemini: Add model variants with -2K and -4K suffixes for gemini-3.0-pro-image - adapter-gemini: Extract imageSize from model name in prepareModelConfig - adapter-gemini: Add imageConfig to generationConfig when imageSize is specified - core: Add overrideRequestParams to ModelRequestParams for custom request parameters - core: Export deepAssign utility function for deep object merging - shared-adapter: Apply overrideRequestParams in buildChatCompletionParams The imageSize parameter follows Gemini API's ImageConfig specification, supporting 1K (default), 2K, and 4K image sizes.
|
Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. Walkthrough为 Gemini 适配器添加对图像分辨率后缀(-2K / -4K)的模型变体生成与传递支持;在模型配置中解析并传递 imageSize;新增并导出递归合并工具 Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes
Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary of ChangesHello @dingyi222666, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the Gemini adapter by integrating the latest Gemini 3.0 Pro Image model, providing users with advanced image generation features. It introduces a flexible mechanism to specify image resolutions directly through model names and allows for granular control over API request parameters, improving the adapter's versatility and alignment with the Gemini API's capabilities. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces support for Gemini 3.0 image generation with configurable sizes, which is a great addition. The implementation is well-structured, introducing model name suffixes for size configuration and adding overrideRequestParams for more flexible API calls. The new deepAssign utility is a good reusable component. I've found a potential issue in the deepAssign implementation regarding property iteration and a minor inconsistency in the updated documentation for image generation models. My detailed comments are below.
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
packages/adapter-gemini/src/utils.ts (1)
380-380: 移除不必要的 undefined 初始化TypeScript 中显式初始化为
undefined是冗余的,因为未初始化的变量默认值就是undefined。应用此修改:
- let imageSize: string | undefined = undefined + let imageSize: string | undefined
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (4)
packages/adapter-gemini/package.jsonis excluded by!**/*.jsonpackages/adapter-gemini/src/locales/en-US.schema.ymlis excluded by!**/*.ymlpackages/adapter-gemini/src/locales/zh-CN.schema.ymlis excluded by!**/*.ymlpackages/core/package.jsonis excluded by!**/*.json
📒 Files selected for processing (5)
packages/adapter-gemini/src/client.ts(2 hunks)packages/adapter-gemini/src/utils.ts(6 hunks)packages/core/src/llm-core/platform/api.ts(1 hunks)packages/core/src/utils/object.ts(1 hunks)packages/shared-adapter/src/requester.ts(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (3)
packages/adapter-gemini/src/client.ts (1)
packages/adapter-gemini/src/index.ts (1)
name(102-102)
packages/shared-adapter/src/requester.ts (1)
packages/core/src/utils/object.ts (1)
deepAssign(15-39)
packages/adapter-gemini/src/utils.ts (1)
packages/core/src/utils/object.ts (1)
deepAssign(15-39)
🪛 GitHub Check: CodeFactor
packages/adapter-gemini/src/utils.ts
[warning] 380-380: packages/adapter-gemini/src/utils.ts#L380
It's not necessary to initialize 'imageSize: string | undefined' to undefined. (no-undef-init)
🔇 Additional comments (7)
packages/core/src/llm-core/platform/api.ts (1)
72-77: 很好的扩展!新增的
overrideRequestParams字段为请求参数提供了灵活的覆盖机制,与下游的深度合并逻辑(如packages/shared-adapter/src/requester.ts中的使用)配合良好。packages/adapter-gemini/src/client.ts (1)
79-117: 图像分辨率模型变体实现正确!为包含
gemini-3.0-pro-image的模型自动生成-2K和-4K分辨率变体的逻辑清晰,与utils.ts中的 imageSize 提取逻辑(正则表达式/-(2K|4K)$/)保持一致。原始模型作为 1K 默认分辨率。packages/shared-adapter/src/requester.ts (2)
31-31: 导入正确!引入
deepAssign工具函数用于深度合并请求参数。
96-96: 深度合并实现正确!使用
deepAssign({}, base, params.overrideRequestParams ?? {})创建新对象并合并覆盖参数,避免了对 base 对象的直接修改。合并顺序正确,允许overrideRequestParams深度覆盖基础配置。packages/adapter-gemini/src/utils.ts (3)
33-33: 导入正确!引入
deepAssign工具函数用于深度合并生成配置参数。
411-416: 图像尺寸提取逻辑实现正确!从模型名称后缀提取 imageSize(
-2K或-4K)的正则表达式逻辑清晰,与client.ts中生成的模型变体保持一致。
468-496: 生成配置构建逻辑正确!使用 base 对象统一管理生成配置字段,当
imageSize存在时正确添加imageConfig字段。Line 496 使用deepAssign深度合并overrideRequestParams,模式与requester.ts一致。
Add hasOwnProperty check to deepAssign function to only process own enumerable properties and skip inherited properties from the prototype chain. This prevents potential security issues from prototype pollution and ensures the function behaves correctly when merging objects with custom prototypes.
Remove redundant '= undefined' initialization for imageSize variable as TypeScript variables are undefined by default when declared without initialization.
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
packages/core/src/utils/object.ts (1)
14-41: deepAssign 实现整体正确,可考虑对sources做轻量健壮性增强目前实现已经通过
hasOwnProperty避免了原型链属性被误合并,递归合并普通对象的逻辑也符合预期。不过作为通用工具函数,如果未来某处误传入null/undefined作为 source,for (const key in src)会直接抛异常。可以在遍历前做一次空值过滤,提升健壮性且不改变现有调用行为,例如:
export function deepAssign(target: any, ...sources: any[]): any { - for (const src of sources) { + for (const src of sources) { + if (src == null) continue for (const key in src) { if (!Object.prototype.hasOwnProperty.call(src, key)) { continue } // ... } } return target }packages/adapter-gemini/src/utils.ts (1)
396-432:imageSize解析与overrideRequestParams深度合并逻辑合理,类型上有小幅优化空间
- 从模型后缀
-2K/-4K中提取imageSize,并将模型名规整为无尺寸后缀再下发给后端,这条链路是连贯的;createGenerationConfig中通过imageConfig: modelConfig.imageSize ? { imageSize: ... } : undefined也能按需生成配置。deepAssign({}, base, params.overrideRequestParams ?? {})让overrideRequestParams只覆盖需要改写的字段,同时避免修改base/ 原始 override 对象,本身设计不错。有两点可以酌情优化:
类型收紧
imageSize
目前声明为let imageSize: string | undefined,但实际只可能是'2K' | '4K'。如果希望在调用端获得更强的类型检查,可以改为:
- let imageSize: string | undefined
- let imageSize: '2K' | '4K' | undefined
2. **`imageConfig` 与 `imageGeneration` 的关系**(可选) 当前只要解析到 `imageSize` 就会下发 `imageConfig`,即使 `modelConfig.imageGeneration` 为 `false`。如果你希望“仅当开启 imageGeneration 时才发送 imageConfig”,可以考虑将这两个条件结合起来,例如: ```diff - imageConfig: modelConfig.imageSize + imageConfig: + modelConfig.imageGeneration && modelConfig.imageSize ? { imageSize: modelConfig.imageSize } : undefined,上述两点都不影响现有功能,仅作为类型与配置语义上的可选改进。
Also applies to: 447-449, 484-513
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
packages/adapter-gemini/src/utils.ts(7 hunks)packages/core/src/utils/object.ts(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/adapter-gemini/src/utils.ts (1)
packages/core/src/utils/object.ts (1)
deepAssign(15-43)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: lint
- GitHub Check: build
Changes: - Make _plugin property public in GeminiRequester for better accessibility - Remove unused filterKeys import from utils.ts to reduce dependencies
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (2)
packages/adapter-gemini/src/requester.ts (1)
54-61: 将_plugin改为public的选择看起来合理,但可考虑设为只读把
_plugin声明为public只改变了类型可见性,运行时行为不变,也便于外部访问GeminiRequester所持有的插件实例,这里整体是 OK 的。为了避免后续在外部或本类中误改引用,可以考虑进一步改为只读属性:- public _plugin: ChatLunaPlugin<ClientConfig, Config> + public readonly _plugin: ChatLunaPlugin<ClientConfig, Config>packages/adapter-gemini/src/utils.ts (1)
388-448: 模型名后缀解析 imageSize 的实现基本符合需求,可收紧类型
- 使用
/-(2K|4K)$/从模型名末尾提取尺寸后缀并同步从model中移除,能很好地支持gemini-3.0-pro-image-2K/4K这类本地变体,同时对外实际请求的model仍为基础模型名。- 未匹配到后缀时不设置
imageSize,由“1K 为默认尺寸”的行为接管,也符合本 PR 描述。可以考虑将
imageSize的类型从string | undefined收紧为字面量联合,例如:type ImageSize = '2K' | '4K' let imageSize: ImageSize | undefined这样后续使用时有更强的类型约束,并避免误传入无效字符串。
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
packages/adapter-gemini/src/requester.ts(1 hunks)packages/adapter-gemini/src/utils.ts(7 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
packages/adapter-gemini/src/requester.ts (1)
packages/core/src/services/chat.ts (1)
ChatLunaPlugin(594-800)
packages/adapter-gemini/src/utils.ts (1)
packages/core/src/utils/object.ts (1)
deepAssign(15-43)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: lint
- GitHub Check: build
🔇 Additional comments (2)
packages/adapter-gemini/src/utils.ts (2)
34-34: deepAssign 引入与使用合理
deepAssign仅在createGenerationConfig中使用,通过deepAssign({}, base, params.overrideRequestParams ?? {})深度合并默认配置与覆盖参数且不污染入参引用,实现方式符合预期。
478-512: generationConfig 组装与 overrideRequestParams 合并逻辑清晰
- 在局部
base中集中构造stopSequences、temperature、maxOutputTokens、responseModalities等默认配置,并在modelConfig.imageSize存在时才生成imageConfig,避免对默认 1K 尺寸发送多余字段。- 最终通过
deepAssign({}, base, params.overrideRequestParams ?? {})深度合并,保证调用方在overrideRequestParams中配置的字段(包括嵌套的imageConfig/thinkingConfig)能覆盖默认值,同时不修改原始参数对象,整体设计符合“可覆盖默认请求参数”的目标。
This PR adds support for Gemini 3.0 image generation capabilities with configurable image sizes.
New Features
imageSizeparameter via model name suffix (e.g.,gemini-3.0-pro-image-2K)imageConfigto generation configuration for controlling output image sizeoverrideRequestParamstoModelRequestParamsfor custom request parametersdeepAssignutility for deep object mergingImplementation Details
gemini-3.0-pro-image,gemini-3.0-pro-image-2K,gemini-3.0-pro-image-4K