refactor: 优化 kiro token 刷新逻辑和 build_all_format_configs

- kiro auth: 拆分无缓存 token 和占位符 key 的判断逻辑,避免不必要的解密操作
- kiro auth: 简化 effective_token 获取逻辑
- build_all_format_configs: 只对实际配置了端点的格式构建请求配置,
  不再用某个端点的 base_url 尝试其他未配置的格式
- get_adapter_for_format: 简化为单行表达式
- 修复 f-string 日志为 loguru 风格占位符
- 新增 build_all_format_configs 单元测试
This commit is contained in:
fawney19
2026-02-09 10:11:38 +08:00
parent 1b8e73bb5c
commit 8673ed1459
3 changed files with 106 additions and 59 deletions

View File

@@ -1047,8 +1047,9 @@ async def get_provider_auth(
# Kiro 特殊处理:如果没有缓存的 access_token 或 key.api_key 是占位符,强制刷新
if provider_type == "kiro" and not should_refresh:
decrypted_api_key = crypto_service.decrypt(key.api_key)
if not cached_access_token or decrypted_api_key == "__placeholder__":
if not cached_access_token:
should_refresh = True
elif crypto_service.decrypt(key.api_key) == "__placeholder__":
should_refresh = True
if should_refresh and refresh_token and provider_type:
@@ -1079,16 +1080,11 @@ async def get_provider_auth(
# 获取最终使用的 access_token
# Kiro 优先使用 token_meta 中缓存的 access_token刷新后会更新到 token_meta
effective_access_token: str
if provider_type == "kiro":
# Kiro: 优先使用 token_meta 中的 access_token回退到 key.api_key
cached_token = str(token_meta.get("access_token") or "").strip()
if cached_token:
effective_access_token = cached_token
else:
effective_access_token = crypto_service.decrypt(key.api_key)
refreshed_token = str(token_meta.get("access_token") or "").strip()
effective_token = refreshed_token or crypto_service.decrypt(key.api_key)
else:
effective_access_token = crypto_service.decrypt(key.api_key)
effective_token = crypto_service.decrypt(key.api_key)
decrypted_auth_config: dict[str, Any] | None = None
if isinstance(token_meta, dict) and token_meta:
@@ -1096,7 +1092,7 @@ async def get_provider_auth(
return ProviderAuthInfo(
auth_header="Authorization",
auth_value=f"Bearer {effective_access_token}",
auth_value=f"Bearer {effective_token}",
decrypted_auth_config=decrypted_auth_config,
)
if auth_type == "vertex_ai":