feat: support ANTHROPIC_API_KEY field and add AiHubMix provider
Add compatibility for providers that use ANTHROPIC_API_KEY instead of ANTHROPIC_AUTH_TOKEN, enabling support for AiHubMix and similar services. Changes: - Backend: Add fallback to ANTHROPIC_API_KEY in credential extraction - migration.rs: Support API_KEY during config migration - services/provider.rs: Extract credentials from either field - Frontend: Smart API key field detection and management - getApiKeyFromConfig: Read from either field (AUTH_TOKEN priority) - setApiKeyInConfig: Write to existing field, preserve user choice - hasApiKeyField: Detect presence of either field - Add AiHubMix provider preset with dual endpoint candidates - Define apiKeyField metadata for provider presets (documentation) Technical Details: - Uses or_else() for zero-cost field fallback in Rust - Runtime field detection ensures correct field name preservation - Maintains backward compatibility with existing configurations - Priority: ANTHROPIC_AUTH_TOKEN > ANTHROPIC_API_KEY
This commit is contained in:
@@ -164,12 +164,14 @@ export const hasCommonConfigSnippet = (
|
||||
}
|
||||
};
|
||||
|
||||
// 读取配置中的 API Key(env.ANTHROPIC_AUTH_TOKEN)
|
||||
// 读取配置中的 API Key(优先 ANTHROPIC_AUTH_TOKEN,其次 ANTHROPIC_API_KEY)
|
||||
export const getApiKeyFromConfig = (jsonString: string): string => {
|
||||
try {
|
||||
const config = JSON.parse(jsonString);
|
||||
const key = config?.env?.ANTHROPIC_AUTH_TOKEN;
|
||||
return typeof key === "string" ? key : "";
|
||||
const token = config?.env?.ANTHROPIC_AUTH_TOKEN;
|
||||
const apiKey = config?.env?.ANTHROPIC_API_KEY;
|
||||
const value = typeof token === "string" ? token : typeof apiKey === "string" ? apiKey : "";
|
||||
return value;
|
||||
} catch (err) {
|
||||
return "";
|
||||
}
|
||||
@@ -224,9 +226,10 @@ export const applyTemplateValues = (
|
||||
export const hasApiKeyField = (jsonString: string): boolean => {
|
||||
try {
|
||||
const config = JSON.parse(jsonString);
|
||||
return Object.prototype.hasOwnProperty.call(
|
||||
config?.env ?? {},
|
||||
"ANTHROPIC_AUTH_TOKEN",
|
||||
const env = config?.env ?? {};
|
||||
return (
|
||||
Object.prototype.hasOwnProperty.call(env, "ANTHROPIC_AUTH_TOKEN") ||
|
||||
Object.prototype.hasOwnProperty.call(env, "ANTHROPIC_API_KEY")
|
||||
);
|
||||
} catch (err) {
|
||||
return false;
|
||||
@@ -246,10 +249,17 @@ export const setApiKeyInConfig = (
|
||||
if (!createIfMissing) return jsonString;
|
||||
config.env = {};
|
||||
}
|
||||
if (!("ANTHROPIC_AUTH_TOKEN" in config.env) && !createIfMissing) {
|
||||
const env = config.env as Record<string, any>;
|
||||
// 优先写入已存在的字段;若两者均不存在且允许创建,则默认创建 AUTH_TOKEN 字段
|
||||
if ("ANTHROPIC_AUTH_TOKEN" in env) {
|
||||
env.ANTHROPIC_AUTH_TOKEN = apiKey;
|
||||
} else if ("ANTHROPIC_API_KEY" in env) {
|
||||
env.ANTHROPIC_API_KEY = apiKey;
|
||||
} else if (createIfMissing) {
|
||||
env.ANTHROPIC_AUTH_TOKEN = apiKey;
|
||||
} else {
|
||||
return jsonString;
|
||||
}
|
||||
config.env.ANTHROPIC_AUTH_TOKEN = apiKey;
|
||||
return JSON.stringify(config, null, 2);
|
||||
} catch (err) {
|
||||
return jsonString;
|
||||
|
||||
Reference in New Issue
Block a user