feat(adapter-gemini): 支持手动模型列表#910
Conversation
总体概述Gemini 适配器新增对额外模型的配置管理与处理流程:通过 变更项额外模型配置与集成
相关 PR
建议审查人员
诗
🎯 3 (中等复杂度) | ⏱️ ~25 分钟 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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 |
There was a problem hiding this comment.
Code Review
This pull request introduces the ability to manually configure additional models and toggle automatic model fetching (pullModels) in the Gemini adapter. It updates the client to parse and expand these additional models, adds schema configuration and localization for the new settings, and includes corresponding unit tests. The review feedback highlights potential runtime TypeError exceptions if additionalModels is undefined (e.g., when a user updates the plugin but has not yet saved the new configuration). Specifically, it is recommended to add nullish coalescing or optional chaining when iterating or searching through additionalModels in client.ts and utils.ts.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| // --- 将原始模型转换并展开为变体列表 --- | ||
| const models: ModelInfo[] = [] | ||
|
|
||
| for (const model of this._config.additionalModels) { |
There was a problem hiding this comment.
| const extra = pluginConfig.additionalModels.find( | ||
| (item) => | ||
| item.model === model && !item.model.toLowerCase().includes('gemini') | ||
| ) |
There was a problem hiding this comment.
同理,在 prepareModelConfig 中,pluginConfig.additionalModels 在配置未更新时可能为 undefined。直接调用 .find() 会导致运行时抛出 TypeError: Cannot read properties of undefined (reading 'find') 异常。
建议使用可选链(optional chaining)进行安全调用。
| const extra = pluginConfig.additionalModels.find( | |
| (item) => | |
| item.model === model && !item.model.toLowerCase().includes('gemini') | |
| ) | |
| const extra = pluginConfig.additionalModels?.find( | |
| (item) => | |
| item.model === model && !item.model.toLowerCase().includes('gemini') | |
| ) |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/adapter-gemini/src/client.ts`:
- Around line 147-171: The additionalModels branch currently pushes baseInfo
into models without name-based deduplication, causing duplicate model names when
manual config includes repeats or base+variant pairs; update the logic handling
this._config.additionalModels so it uses the same expansion + dedupe path as the
remote results—invoke expandModelVariants(models, baseInfo,
this._config.imageModelSearch) as before and, if it returns false, only push
baseInfo when no existing entry in models has the same name (compare
ModelInfo.name), or reuse the same dedupe routine used later in refreshModels();
ensure references: additionalModels, baseInfo, expandModelVariants, models, and
refreshModels().
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 3691df3b-dee7-4813-bf97-9b171e463b40
⛔ Files ignored due to path filters (3)
packages/adapter-gemini/src/locales/en-US.schema.ymlis excluded by!**/*.ymlpackages/adapter-gemini/src/locales/zh-CN.schema.ymlis excluded by!**/*.ymlpackages/adapter-gemini/tests/tsconfig.jsonis excluded by!**/*.json
📒 Files selected for processing (4)
packages/adapter-gemini/src/client.tspackages/adapter-gemini/src/index.tspackages/adapter-gemini/src/utils.tspackages/adapter-gemini/tests/client.spec.ts
| for (const model of this._config.additionalModels) { | ||
| const modelNameLower = model.model.toLowerCase() | ||
| const isEmbedding = model.modelType === 'Embeddings 嵌入模型' | ||
|
|
||
| const baseInfo: ModelInfo = { | ||
| name: model.model, | ||
| maxTokens: model.contextSize, | ||
| type: isEmbedding ? ModelType.embeddings : ModelType.llm, | ||
| capabilities: isEmbedding | ||
| ? [] | ||
| : modelNameLower.includes('gemini') | ||
| ? createGeminiCapabilities(modelNameLower, false) | ||
| : model.modelCapabilities | ||
| } | ||
|
|
||
| if ( | ||
| !expandModelVariants( | ||
| models, | ||
| baseInfo, | ||
| this._config.imageModelSearch | ||
| ) | ||
| ) { | ||
| models.push(baseInfo) | ||
| } | ||
| } |
There was a problem hiding this comment.
补充模型分支缺少按名称去重,可能返回重复模型
Line 147-171 当前把 additionalModels 展开后直接写入 models,但只在 Line 205 对远端展开结果做了去重。
当手动配置里出现重复项,或同时配置基础模型与其变体(如 gemini-2.5-pro + gemini-2.5-pro-thinking)时,refreshModels() 会返回重复 name,与最终列表去重预期不一致。
建议修复(在 additionalModels 分支也走同样的去重路径)
- for (const model of this._config.additionalModels) {
+ for (const model of this._config.additionalModels) {
const modelNameLower = model.model.toLowerCase()
const isEmbedding = model.modelType === 'Embeddings 嵌入模型'
const baseInfo: ModelInfo = {
name: model.model,
maxTokens: model.contextSize,
type: isEmbedding ? ModelType.embeddings : ModelType.llm,
capabilities: isEmbedding
? []
: modelNameLower.includes('gemini')
? createGeminiCapabilities(modelNameLower, false)
: model.modelCapabilities
}
- if (
- !expandModelVariants(
- models,
- baseInfo,
- this._config.imageModelSearch
- )
- ) {
- models.push(baseInfo)
- }
+ const items: ModelInfo[] = []
+ if (
+ !expandModelVariants(
+ items,
+ baseInfo,
+ this._config.imageModelSearch
+ )
+ ) {
+ items.push(baseInfo)
+ }
+
+ for (const item of items) {
+ if (models.findIndex((info) => info.name === item.name) < 0) {
+ models.push(item)
+ }
+ }
}🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@packages/adapter-gemini/src/client.ts` around lines 147 - 171, The
additionalModels branch currently pushes baseInfo into models without name-based
deduplication, causing duplicate model names when manual config includes repeats
or base+variant pairs; update the logic handling this._config.additionalModels
so it uses the same expansion + dedupe path as the remote results—invoke
expandModelVariants(models, baseInfo, this._config.imageModelSearch) as before
and, if it returns false, only push baseInfo when no existing entry in models
has the same name (compare ModelInfo.name), or reuse the same dedupe routine
used later in refreshModels(); ensure references: additionalModels, baseInfo,
expandModelVariants, models, and refreshModels().
Summary
pullModels和additionalModels配置。expandModelVariants()展开后缀,远端模型按展开后的名称去重追加。Test Plan
yarn fast-build adapter-geminiyarn exec cross-env TS_NODE_PROJECT=packages/adapter-gemini/tests/tsconfig.json mocha -r tsconfig-paths/register -r esbuild-register -r yml-register --exit "packages/adapter-gemini/tests/**/*.spec.ts"yarn eslint packages/adapter-gemini/src --cache --ext=tsyarn eslint packages/adapter-gemini/tests/client.spec.ts --no-ignore --cache --ext=tsgit diff --check