feat: 非 custom 提供商新建密钥时默认开启自动获取上游模型

- 前端 KeyFormDialog 根据提供商类型自动设置 auto_fetch_models 默认值
- OAuth 创建密钥时传入 auto_fetch_models=True
- OAuth 完成、refresh token 导入、批量导入后自动触发模型获取
This commit is contained in:
fawney19
2026-02-09 13:50:27 +08:00
parent 0da34f729e
commit d0b9dc7a24
2 changed files with 46 additions and 1 deletions

View File

@@ -417,6 +417,11 @@ const apiKeyFieldName = computed(() => `api-key-field-${formNonce.value}`)
// 可用的能力列表
const availableCapabilities = ref<CapabilityDefinition[]>([])
// 非 custom 提供商默认开启自动获取上游模型
const defaultAutoFetchModels = computed(() =>
!!props.providerType && props.providerType !== 'custom'
)
const form = ref({
name: '',
api_key: '', // 标准 API Key
@@ -519,7 +524,7 @@ function resetForm() {
note: '',
is_active: true,
capabilities: {},
auto_fetch_models: false,
auto_fetch_models: defaultAutoFetchModels.value,
model_include_patterns_text: '',
model_exclude_patterns_text: ''
}

View File

@@ -238,6 +238,7 @@ def _create_oauth_key(
api_formats: list[str],
flush_only: bool = False,
proxy: dict[str, Any] | None = None,
auto_fetch_models: bool = True,
) -> "ProviderAPIKey":
"""创建 OAuth Key 记录并持久化。
@@ -245,6 +246,7 @@ def _create_oauth_key(
flush_only: True 时仅 flush批量导入场景False 时 commit + refresh。
proxy: Key 级别代理配置(如 {"node_id": "xxx", "enabled": True}
创建时设置后,后续 token 刷新、额度刷新等操作立即走代理,避免 IP 污染。
auto_fetch_models: 是否启用自动获取上游模型,非 custom 提供商默认开启。
"""
from src.models.database import ProviderAPIKey as ProviderAPIKeyModel
@@ -256,6 +258,7 @@ def _create_oauth_key(
auth_config=crypto_service.encrypt(json.dumps(auth_config)),
api_formats=api_formats,
is_active=True,
auto_fetch_models=auto_fetch_models,
)
if proxy:
new_key.proxy = proxy
@@ -268,6 +271,24 @@ def _create_oauth_key(
return new_key
async def _trigger_auto_fetch_models(key_ids: list[str]) -> None:
"""为启用了 auto_fetch_models 的新建 Key 触发模型获取。"""
if not key_ids:
return
try:
from src.services.model.fetch_scheduler import get_model_fetch_scheduler
scheduler = get_model_fetch_scheduler()
for key_id in key_ids:
logger.info("[AUTO_FETCH] OAuth Key {} 默认开启自动获取模型,触发模型获取", key_id)
try:
await scheduler._fetch_models_for_key_by_id(key_id)
except Exception as e:
logger.error(f"[AUTO_FETCH] Key {key_id} 触发模型获取失败: {e}")
except Exception as e:
logger.error(f"[AUTO_FETCH] 获取 ModelFetchScheduler 失败: {e}")
async def _fetch_kiro_email(
auth_config: dict[str, Any],
proxy_config: dict[str, Any] | None = None,
@@ -1034,6 +1055,9 @@ async def complete_provider_oauth(
proxy=key_proxy,
)
# 默认开启了 auto_fetch_models触发模型获取
await _trigger_auto_fetch_models([str(new_key.id)])
return ProviderCompleteOAuthResponse(
key_id=str(new_key.id),
provider_type=provider_type,
@@ -1257,6 +1281,9 @@ async def import_refresh_token(
proxy=key_proxy,
)
# 默认开启了 auto_fetch_models触发模型获取
await _trigger_auto_fetch_models([str(new_key.id)])
return ProviderCompleteOAuthResponse(
key_id=str(new_key.id),
provider_type=provider_type,
@@ -1378,6 +1405,9 @@ async def import_refresh_token(
proxy=key_proxy,
)
# 默认开启了 auto_fetch_models触发模型获取
await _trigger_auto_fetch_models([str(new_key.id)])
return ProviderCompleteOAuthResponse(
key_id=str(new_key.id),
provider_type=provider_type,
@@ -1647,6 +1677,11 @@ async def batch_import_oauth(
if success_count > 0:
db.commit()
# 批量导入完成后,触发所有成功 Key 的模型获取
success_key_ids = [r.key_id for r in results if r.status == "success" and r.key_id]
if success_key_ids:
await _trigger_auto_fetch_models(success_key_ids)
logger.info(
"[BATCH_IMPORT] Provider {} ({}): 成功 {}/{}, 失败 {}",
provider_id,
@@ -1784,6 +1819,11 @@ async def _batch_import_kiro_internal(
if success_count > 0:
db.commit()
# 批量导入完成后,触发所有成功 Key 的模型获取
success_key_ids = [r.key_id for r in results if r.status == "success" and r.key_id]
if success_key_ids:
await _trigger_auto_fetch_models(success_key_ids)
logger.info(
"[KIRO_BATCH_IMPORT] Provider {}: 成功 {}/{}, 失败 {}",
provider_id,