qwen-code/packages/cli/src/ui/models/availableModels.ts
mingholy.lmh 51214d9032 fix: 修复模型常量更新导致的单测失败
- 将 ModelDialog.test.tsx 中的 MAINLINE_CODER_MODEL 替换为 DEFAULT_QWEN_MODEL
- 修复 modelConfigResolver.test.ts 中 OpenAI auth 类型的默认模型期望
- 统一使用 core 包导出的 MAINLINE_CODER_MODEL 常量

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
2026-02-26 15:45:06 +08:00

129 lines
3.4 KiB
TypeScript

/**
* @license
* Copyright 2025 Qwen
* SPDX-License-Identifier: Apache-2.0
*/
import {
AuthType,
type Config,
type AvailableModel as CoreAvailableModel,
QWEN_OAUTH_MODELS,
} from '@qwen-code/qwen-code-core';
import { t } from '../../i18n/index.js';
export type AvailableModel = {
id: string;
label: string;
description?: string;
isVision?: boolean;
};
const CACHED_QWEN_OAUTH_MODELS: AvailableModel[] = QWEN_OAUTH_MODELS.map(
(model) => ({
id: model.id,
label: model.name ?? model.id,
description: model.description,
isVision: model.capabilities?.vision ?? false,
}),
);
function getQwenOAuthModels(): readonly AvailableModel[] {
return CACHED_QWEN_OAUTH_MODELS;
}
/**
* Get available Qwen models
* coder-model now has vision capabilities by default.
*/
export function getFilteredQwenModels(): AvailableModel[] {
return [...getQwenOAuthModels()];
}
/**
* Currently we use the single model of `OPENAI_MODEL` in the env.
* In the future, after settings.json is updated, we will allow users to configure this themselves.
*/
export function getOpenAIAvailableModelFromEnv(): AvailableModel | null {
const id = process.env['OPENAI_MODEL']?.trim();
return id
? {
id,
label: id,
get description() {
return t('Configured via OPENAI_MODEL environment variable');
},
}
: null;
}
export function getAnthropicAvailableModelFromEnv(): AvailableModel | null {
const id = process.env['ANTHROPIC_MODEL']?.trim();
return id
? {
id,
label: id,
get description() {
return t('Configured via ANTHROPIC_MODEL environment variable');
},
}
: null;
}
/**
* Convert core AvailableModel to CLI AvailableModel format
*/
function convertCoreModelToCliModel(
coreModel: CoreAvailableModel,
): AvailableModel {
return {
id: coreModel.id,
label: coreModel.label,
description: coreModel.description,
isVision: coreModel.isVision ?? coreModel.capabilities?.vision ?? false,
};
}
/**
* Get available models for the given authType.
*
* If a Config object is provided, uses config.getAvailableModelsForAuthType().
* Falls back to environment variables only when no config is provided.
*/
export function getAvailableModelsForAuthType(
authType: AuthType,
config?: Config,
): AvailableModel[] {
// Use config's model registry when available
if (config) {
try {
const models = config.getAvailableModelsForAuthType(authType);
if (models.length > 0) {
return models.map(convertCoreModelToCliModel);
}
} catch {
// If config throws (e.g., not initialized), return empty array
}
// When a Config object is provided, we intentionally do NOT fall back to env-based
// "raw" models. These may reflect the currently effective config but should not be
// presented as selectable options in /model.
return [];
}
// Fall back to environment variables for specific auth types (no config provided)
switch (authType) {
case AuthType.QWEN_OAUTH: {
return [...getQwenOAuthModels()];
}
case AuthType.USE_OPENAI: {
const openAIModel = getOpenAIAvailableModelFromEnv();
return openAIModel ? [openAIModel] : [];
}
case AuthType.USE_ANTHROPIC: {
const anthropicModel = getAnthropicAvailableModelFromEnv();
return anthropicModel ? [anthropicModel] : [];
}
default:
return [];
}
}