Skip to content

feat(adapter-gemini): 支持手动配置模型列表#911

Merged
dingyi222666 merged 3 commits into
ChatLunaLab:v1-devfrom
Sor85:feat/gemini-additional-models
Jun 10, 2026
Merged

feat(adapter-gemini): 支持手动配置模型列表#911
dingyi222666 merged 3 commits into
ChatLunaLab:v1-devfrom
Sor85:feat/gemini-additional-models

Conversation

@Sor85

@Sor85 Sor85 commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Summary

  • 为 Gemini adapter 增加 pullModelsadditionalModels 配置。
  • 手动配置的模型优先展示,远端模型在完成变体展开后按名称去重追加。
  • 标准 Gemini 模型复用现有 Gemini 变体展开与能力推断逻辑,非 Gemini 模型保留用户填写的模型名和能力配置。

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the ability to configure whether to automatically pull models via pullModels and allows defining additionalModels manually in the Gemini adapter. Feedback on these changes highlights potential runtime errors and backward compatibility issues: pullModels could be undefined in existing configurations (which would unexpectedly disable model pulling), and additionalModels could also be undefined, leading to TypeError crashes during iteration or lookup. Additionally, it is recommended to use English constants (e.g., 'embeddings') instead of hardcoded Chinese strings for modelType to improve internationalization and code robustness.

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.

Comment thread packages/adapter-gemini/src/client.ts
Comment thread packages/adapter-gemini/src/client.ts
Comment thread packages/adapter-gemini/src/client.ts Outdated
Comment thread packages/adapter-gemini/src/utils.ts Outdated
Comment thread packages/adapter-gemini/src/client.ts Outdated
Comment thread packages/adapter-gemini/src/index.ts
@coderabbitai

coderabbitai Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@dingyi222666, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 6 minutes and 51 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 1d09314a-3806-4568-a078-cf2a86c77134

📥 Commits

Reviewing files that changed from the base of the PR and between 64e5555 and 3f84e75.

⛔ Files ignored due to path filters (2)
  • packages/adapter-gemini/src/locales/en-US.schema.yml is excluded by !**/*.yml
  • packages/adapter-gemini/src/locales/zh-CN.schema.yml is excluded by !**/*.yml
📒 Files selected for processing (3)
  • packages/adapter-gemini/src/client.ts
  • packages/adapter-gemini/src/index.ts
  • packages/adapter-gemini/src/utils.ts

Walkthrough

扩展 Gemini 适配器配置(新增 pullModels 与 additionalModels),prepareModelConfig 增加 additionalModels 的早退匹配,并在 GeminiClient.refreshModels 中重构模型映射、变体追加与同名去重逻辑;同时移除若干旧的变体展开/判断导出。

变更内容

Gemini 模型配置与初始化

Layer / File(s) Summary
配置架构扩展
packages/adapter-gemini/src/index.ts
Config 接口新增 pullModels(远程拉取开关)与 additionalModels(包含 model/modelType/modelCapabilities/contextSize),并在 Schema 中加入对应校验与默认值。
prepareModelConfig 的 additionalModels 早退
packages/adapter-gemini/src/utils.ts
prepareModelConfig 中新增对 pluginConfig.additionalModels 的同名匹配校验;若命中且模型名不包含 gemini,直接返回当前配置以跳过后缀解析。
移除旧的变体导出/工具
packages/adapter-gemini/src/utils.ts
删除与图片/思维变体展开相关的一组导出常量与函数(如 IMAGE_VARIANT_MODELS、expandModelVariants、isThinkingModel 等);保留必要的导出如 isGemini3ProFamily
client.ts 导入调整与条件拉取
packages/adapter-gemini/src/client.ts
调整对 ./utils 的导入为仅 createGeminiCapabilitiesshouldFilterOutGeminiModelGeminiClient.refreshModels 改为仅在 pullModels 为真时请求远程模型,启用拉取但远端为空时抛出 MODEL_INIT_ERROR
additionalModels/remote 映射与最终模型组装
packages/adapter-gemini/src/client.ts
additionalModels 与远程 rawModels 分别映射为统一中间 items(区分 embeddings/llm,embeddings 能力为空,llm 根据名称使用 createGeminiCapabilities 或配置能力),在此基础上按 Gemini 规则追加 image/thinking/等级等后缀变体并使用集合去重同名模型,生成最终 models 列表。

🎯 4 (Complex) | ⏱️ ~40 分钟

可能相关的 PR(最多 3 个):

  • ChatLunaLab/chatluna#744:和本 PR 在 refreshModels 的模型组装流程与变体展开管线上有直接代码级关联。
  • ChatLunaLab/chatluna#882:与本次更改在嵌入/非 LLM 模型的能力(ToolCall 等)处理上有交叉影响。
  • ChatLunaLab/chatluna#628:两者都调整了 thinking/non-thinking 与 image 相关的模型变体检测与生成逻辑。

建议审查人:

  • dingyi222666

概述(更新)

该 PR 为 Gemini 适配器扩展模型配置能力,新增可选的额外模型表与条件性远程拉取开关,统一重构模型初始化中的变体展开与去重流程。

变更内容(已更新)

Gemini 模型配置与初始化

层级 / 文件 概述
配置架构扩展
packages/adapter-gemini/src/index.ts
Config 接口新增 pullModels(远程拉取开关,默认启用)与 additionalModels(自定义模型表),Schema 中定义了 modelTypemodelCapabilitiescontextSize 等字段的校验规则与默认值。
模型名称识别工具 / prepareModelConfig 早退
packages/adapter-gemini/src/utils.ts
prepareModelConfig 中添加 additionalModels 同名匹配分支;匹配且模型名不含 gemini 时直接返回配置以跳过后续后缀解析。移除若干旧的变体展开/判断导出。
模型初始化与去重
packages/adapter-gemini/src/client.ts
refreshModels 中将远程拉取改为受 pullModels 控制,新增 additionalModels 与远程模型到统一中间 items 的映射,并在最终生成 models 时追加 Gemini 特定后缀变体并做同名去重。

相关 PR(更新)

建议审查人(更新)

  • dingyi222666

🐰 我在代码丛中跳跃忙,
模型名与变体排成行,
额外配置悄悄加一桩,
远程拉取随开关而往,
去重合并,正是我的歌唱。

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: adding support for manual model list configuration in the Gemini adapter through new config options.
Description check ✅ Passed The description clearly explains the implementation details: adding pullModels and additionalModels config, prioritizing manual models, deduplicating remote models, and handling both Gemini and non-Gemini models appropriately.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@dingyi222666

Copy link
Copy Markdown
Member

我想知道为什么要在 gemini 适配器里用其他非 gemini 模型或者手动加,如果拉不到模型,直接默认内置一些模型更好而不是还需要手动填

@dingyi222666

Copy link
Copy Markdown
Member

如果无法解释这个 pr 我将关闭

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 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`:
- Line 206: 返回 models 时没有对空模型集做校验,当 pullModels 为 false 且 additionalModels
为空会导致返回空数组并“静默成功”;在返回前(在 packages/adapter-gemini/src/client.ts 中处理模型集合的函数,返回
models 的位置)加入校验:如果 models.length === 0 且 pullModels === false(或 additionalModels
为空/未提供),不要返回空数组,而是抛出一个明确的错误(例如抛出或返回带有标识码 MODEL_INIT_ERROR
的异常/错误对象),以便尽早暴露配置问题并阻止下游缓存空模型列表。确保使用现有错误类型/工厂或统一错误格式与项目风格一致。

In `@packages/adapter-gemini/src/utils.ts`:
- Around line 770-772: The current isGeminiModelName function is too permissive
(uses includes('gemini')) and misclassifies non-standard names; update
isGeminiModelName to validate against the standard Gemini request name pattern
(e.g. case-insensitive match for names that start with "gemini" followed by
end-of-string or a delimiter like '-' or ':' — or check against an explicit
allowlist of known Gemini model IDs) so it only returns true for real Gemini
request names; then ensure callers such as prepareModelConfig use the updated
isGeminiModelName to drive Gemini-specific branches and manual capability
priority.
🪄 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: 5c9d0026-5b23-46bf-9519-304e04afbdef

📥 Commits

Reviewing files that changed from the base of the PR and between 97faf21 and 0605e31.

⛔ Files ignored due to path filters (2)
  • packages/adapter-gemini/src/locales/en-US.schema.yml is excluded by !**/*.yml
  • packages/adapter-gemini/src/locales/zh-CN.schema.yml is excluded by !**/*.yml
📒 Files selected for processing (3)
  • packages/adapter-gemini/src/client.ts
  • packages/adapter-gemini/src/index.ts
  • packages/adapter-gemini/src/utils.ts

Comment thread packages/adapter-gemini/src/client.ts
Comment thread packages/adapter-gemini/src/utils.ts
@Sor85

Sor85 commented Jun 9, 2026

Copy link
Copy Markdown
Contributor Author

我想知道为什么要在 gemini 适配器里用其他非 gemini 模型或者手动加,如果拉不到模型,直接默认内置一些模型更好而不是还需要手动填

这里的非 Gemini 模型名不是想让适配器请求其他格式的模型,而是为了支持一些 Gemini 格式上游暴露的自定义模型 ID。

比如我上游提供 chatluna-chat,它实际映射的是 gemini-3.5-flash 模型。下游只请求 chatluna-chat,以后 Gemini 模型更新时只需要改上游映射,下游配置不用变,免去了 Koishi 更换模型麻烦的问题。

手动添加也是为了避免拉上游一大坨模型列表下来

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
packages/adapter-gemini/src/client.ts (1)

182-182: 💤 Low value

去重逻辑区分大小写,可能导致意外重复。

当前使用 model.name 原始大小写进行去重。如果用户在 additionalModels 中配置 Gemini-2.5-Flash,而 API 返回 gemini-2.5-flash,两者会被视为不同模型,导致列表中出现重复。

建议改用小写进行去重:

♻️ 建议修改
 const models: ModelInfo[] = []
-const names = new Set<string>()
+const names = new Set<string>()

 for (const model of items) {
     const name = model.name.toLowerCase()
     const suffixes: string[] = []
     // ...suffix generation...

     for (const suffix of suffixes) {
         const full = model.name + suffix
-        if (!names.has(full)) {
-            names.add(full)
+        if (!names.has(full.toLowerCase())) {
+            names.add(full.toLowerCase())
             models.push({ ...model, name: full })
         }
     }

-    if (names.has(model.name)) continue
+    if (names.has(name)) continue

-    names.add(model.name)
+    names.add(name)
     models.push(model)
 }

Also applies to: 224-224, 230-230, 232-232

🤖 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` at line 182, The deduplication
currently uses the raw model.name (via the names Set) which is case-sensitive
and can produce duplicates like "Gemini-2.5-Flash" vs "gemini-2.5-flash"; update
all checks and inserts into the names Set to use a normalized form (e.g.,
model.name.toLowerCase()) so comparisons are case-insensitive—apply this change
wherever names is read/updated (references around the names variable and uses of
model.name at lines noted).
🤖 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/utils.ts`:
- Around line 547-553: The current check uses
item.model.toLowerCase().includes('gemini') which is too broad and misclassifies
custom models; update the predicate in pluginConfig.additionalModels.some to use
a strict case-insensitive prefix match (e.g.,
item.model.toLowerCase().startsWith('gemini-')) when comparing against model
(and apply the same stricter check in client.ts refreshModels where
name.includes('gemini') is used) so only standard Gemini model names with the
"gemini-" prefix trigger Gemini-specific parsing.

---

Nitpick comments:
In `@packages/adapter-gemini/src/client.ts`:
- Line 182: The deduplication currently uses the raw model.name (via the names
Set) which is case-sensitive and can produce duplicates like "Gemini-2.5-Flash"
vs "gemini-2.5-flash"; update all checks and inserts into the names Set to use a
normalized form (e.g., model.name.toLowerCase()) so comparisons are
case-insensitive—apply this change wherever names is read/updated (references
around the names variable and uses of model.name at lines noted).
🪄 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: e37ff15e-c6af-4817-aff1-5d1553f989ec

📥 Commits

Reviewing files that changed from the base of the PR and between 0605e31 and 64e5555.

📒 Files selected for processing (2)
  • packages/adapter-gemini/src/client.ts
  • packages/adapter-gemini/src/utils.ts

Comment thread packages/adapter-gemini/src/utils.ts
@dingyi222666 dingyi222666 force-pushed the feat/gemini-additional-models branch from f514796 to 3f84e75 Compare June 10, 2026 16:21
@dingyi222666 dingyi222666 merged commit 8691df5 into ChatLunaLab:v1-dev Jun 10, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants