Skip to content

[Fix] fallback from unavailable conversation models#862

Merged
dingyi222666 merged 3 commits into
v1-devfrom
fix/issue-861-model-fallback
May 15, 2026
Merged

[Fix] fallback from unavailable conversation models#862
dingyi222666 merged 3 commits into
v1-devfrom
fix/issue-861-model-fallback

Conversation

@dingyi222666
Copy link
Copy Markdown
Member

@dingyi222666 dingyi222666 commented May 15, 2026

This pr fixes conversation model resolution so existing conversations with unavailable saved models fall back to a valid configured model instead of failing every request.

Fixes #861

New Features

None

Bug fixes

  • Skip unavailable fixed, saved, and constraint default models when resolving the effective conversation model.
  • Fall back to the configured default model when older conversations reference removed or renamed models.
  • Normalize the resolved conversation model so chat middlewares, rollback, time-limit checks, and conversation listing display the fallback model consistently.
  • Stop resolve_model from reusing the stale saved conversation model after effectiveModel has already been resolved.
  • Use the same fallback model selection when agent triggers create conversations.

Other Changes

  • Keep model availability selection inside ConversationService so fallback behavior is bound to service state.
  • Expose ConversationService.pickModel() for middleware and trigger paths that need the same fallback decision.
  • Add conversation service coverage for unavailable saved/fixed/default models falling back to the configured default model.
  • Add model registry stubs to conversation service test helpers.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 15, 2026

Review Change Stack

Warning

Rate limit exceeded

@dingyi222666 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 32 minutes and 22 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, 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 have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 63b14d4d-3c64-4e25-a754-5696a82b14d7

📥 Commits

Reviewing files that changed from the base of the PR and between 8021fd8 and 2b1bdb3.

📒 Files selected for processing (8)
  • packages/core/src/middlewares/chat/chat_time_limit_check.ts
  • packages/core/src/middlewares/chat/read_chat_message.ts
  • packages/core/src/middlewares/chat/rollback_chat.ts
  • packages/core/src/middlewares/model/resolve_model.ts
  • packages/core/src/middlewares/system/conversation_manage.ts
  • packages/core/src/services/conversation.ts
  • packages/core/tests/conversation-service.spec.ts
  • packages/extension-agent/src/trigger/executor.ts

Walkthrough

该PR通过在模型解析过程中添加平台侧可用性校验,解决已有会话使用不可用旧模型时直接报错的问题。当模型不存在时,系统现在会跳过该模型并自动回退到配置的默认模型,而非中断流程。

Changes

模型可用性校验与智能回退

Layer / File(s) Summary
模型验证辅助函数
packages/core/src/services/conversation.ts
新增 ModelTypeparseRawModelName 导入,实现 hasModel() 函数用于解析和检查模型是否在对应平台的LLM模型列表中存在;实现 pickModel() 函数从候选模型列表中逐一选择第一个平台侧存在的模型,若全部不可用则返回 null
会话解析中的模型选择
packages/core/src/services/conversation.ts
resolveConversationContext 中将 effectiveModel 计算从简单的 ?? 级联改为 pickModel([constraint.fixedModel, constraint.defaultModel]);在 resolveConversation 的主活跃分支和归档恢复分支中应用 pickModel() 选择候选模型,确保仅当模型在平台侧实际存在时才被采用。
中间件侧的回退链简化
packages/core/src/middlewares/model/resolve_model.ts
移除 resolved.conversation?.model 作为中间回退项,使 modelName 的回退链改为 resolved.effectiveModel ?? config.defaultModel ?? 'empty',依赖上游已校验的有效模型值。
测试基础设施与验证用例
packages/core/tests/helpers.ts, packages/core/tests/conversation-service.spec.ts
在测试辅助函数中为 test-platform 注入模型定义,包括 listPlatformModels() 方法和内部 _models 数据;新增测试用例 skips unavailable models before using config default,验证当约束指向不可用模型时,系统正确回退到配置默认模型。

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • ChatLunaLab/chatluna#856: 两个PR均涉及 packages/core/src/middlewares/model/resolve_model.ts 的核心模型解析路径,本PR修改了模型回退和可用性验证逻辑,相关PR则调整了"不可用预设 vs 无效模型名"的验证流程。

Poem

🐰 啊呀,那些老旧的模型名称
在平台消失时不再尖叫,
我们温柔地检查,轻轻回退—
默认值总在那儿,
让对话继续跳跃!✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.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 标题清晰准确地总结了主要改动:修复会话模型在不可用时的回退机制。
Description check ✅ Passed 描述详细说明了修复内容、关联问题和测试覆盖情况,与代码改动完全相关。
Linked Issues check ✅ Passed PR 实现了 issue #861 的主要需求:不可用模型时回退到 defaultModel,并在 resolve_model 中移除了对 conversation.model 的中间回退。
Out of Scope Changes check ✅ Passed 所有改动都围绕模型可用性校验和回退逻辑展开,未发现与 issue #861 修复无关的改动。

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/issue-861-model-fallback

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.

Copy link
Copy Markdown
Contributor

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

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 a robust model resolution mechanism by adding hasModel and pickModel utilities to verify model availability against the platform registry. The ConversationService now uses these utilities to determine the effectiveModel by prioritizing fixed models, conversation-specific models, and defaults. Feedback suggests refactoring the duplicated resolution logic into a private helper method and addressing a minor redundancy where the global default model may be checked multiple times.

Comment thread packages/core/src/services/conversation.ts Outdated
Comment thread packages/core/src/services/conversation.ts Outdated
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

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/core/src/services/conversation.ts`:
- Around line 121-147: hasModel is a single-use helper used only by pickModel,
so inline its logic into pickModel: remove the standalone hasModel function and
update pickModel to iterate over the input models array, for each model perform
the same null/empty/placeholder checks (model == null, model.trim().length < 1,
model === '无' or 'empty'), call parseRawModelName(model) and skip if platform or
name is null, fetch available models via
ctx.chatluna.platform.listPlatformModels(platform, ModelType.llm).value and
return the first model string whose name matches an entry in that list (or
return null if none match); keep references to Context, parseRawModelName, and
ModelType.llm as in the original code.
🪄 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: 1e5932ba-262d-4d04-9cb5-14f07f0c581b

📥 Commits

Reviewing files that changed from the base of the PR and between a88113f and 8021fd8.

📒 Files selected for processing (4)
  • packages/core/src/middlewares/model/resolve_model.ts
  • packages/core/src/services/conversation.ts
  • packages/core/tests/conversation-service.spec.ts
  • packages/core/tests/helpers.ts

Comment thread packages/core/src/services/conversation.ts Outdated
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8021fd82e6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/core/src/middlewares/model/resolve_model.ts Outdated
@dingyi222666 dingyi222666 merged commit bec9533 into v1-dev May 15, 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.

[Bug] 修改 defaultModel 后,已有会话仍使用不可用旧模型,且没有批量迁移或回退机制

1 participant