fix: support custom models not in DEFAULT_MODELS (issue #4221)#6858
Draft
ljluestc wants to merge 1 commit into
Draft
fix: support custom models not in DEFAULT_MODELS (issue #4221)#6858ljluestc wants to merge 1 commit into
ljluestc wants to merge 1 commit into
Conversation
…b#4221) - Widen ModelType to (typeof DEFAULT_MODELS)[number]["name"] | (string & {}) so custom model names are accepted by the type system without unsafe casts - Fix fillTemplateWith to fall back to modelConfig.providerName when the model is not found in DEFAULT_MODELS, so {{ServiceProvider}} in templates reflects the actual provider for user-defined models - Add fallback in isModelNotavailableInServer: if any available entry in the custom model table has providerType=="custom" and matches the model name, the model is allowed even when providerNames doesn't include the model name itself - Add test/custom-model.test.ts covering collectModelTable and isModelNotavailableInServer behaviour for custom models
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix: Support custom models not in DEFAULT_MODELS (issue #4221)
Problem
When a user adds a custom model name (e.g.
chatglm3-6b) through the CustomModel setting and then sends a message, NextChat would fail because:
TypeScript type mismatch –
ModelTypewas narrowly typed as a union ofliteral strings from
DEFAULT_MODELS. Any custom model name was not part ofthat union, leading to unsafe
as ModelTypecasts throughout the code.Incorrect
{{ServiceProvider}}in templates –fillTemplateWithlookedup the model only in
DEFAULT_MODELS. For custom models the lookup returnsundefined, causing the service provider to always fall back to"OpenAI"even when a different provider was actually configured.
Server-side model gate could incorrectly block custom models –
isModelNotavailableInServerchecked a fixed list of provider names. If therequest arrived with only standard provider names (
["OpenAI", "Azure"]) butthe model was added as a provider-less custom model (stored under the key
<name>@<name>), the model could still be denied even though the server'sCUSTOM_MODELenv var explicitly listed it.Root cause (historical)
An older version of the code contained an explicit throw:
That check was removed, but related fragilities remained.
Changes
app/store/config.tsModelTypefrom a strict union of built-in model name literals to:string & {}trick preserves IDE autocomplete for known model names whileaccepting any custom string at the type level — no more unsafe
as ModelType.app/store/chat.tsfillTemplateWith: when the model is not found inDEFAULT_MODELS, thefunction now falls back to
modelConfig.providerNamefor the{{ServiceProvider}}template variable, so templates correctly reflect theconfigured provider for user-defined models.
app/utils/model.tsisModelNotavailableInServer: added a final fallback check — if anyentry in the built model table has
name === modelName,available === true,and
providerType === "custom", the model is allowed. This ensures that acustom model listed in the server's
CUSTOM_MODELenv var is never blockedsimply because the incoming request's provider list doesn't match the
auto-generated
<name>@<name>key.test/custom-model.test.ts(new)collectModelTablewith provider-less custom models, explicit-providercustom models, disabled custom models, and display-name aliases
isModelNotavailableInServerfor the standard pattern (provider = modelname), the new fallback (only
["OpenAI", "Azure"]passed), blockedunknown models, and built-in model pass-through
Testing
How to reproduce / verify the fix
chatglm3-6b(no@provider).server (e.g. Ollama, vLLM).
chatglm3-6bin the model picker and send a message.no "not found in DEFAULT_MODELS" error.
Notes
(
getClientApifalls through to the GPT client for unrecognised providers).Users who run a custom model behind an OpenAI-compatible API need to configure
a custom base URL in Settings → Provider.
vision-model-checker.test.tspre-existing ESM/nanoid parse failure isunrelated to this PR and was present on
mainbefore these changes.Branch:
private/issue-4221-custom-model-fixFixes: #4221