mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 07:37:39 +00:00
refactor(infra): dedupe provider api key resolution
This commit is contained in:
@@ -80,61 +80,41 @@ function resolveZaiApiKey(): string | undefined {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function resolveMinimaxApiKey(): string | undefined {
|
function resolveMinimaxApiKey(): string | undefined {
|
||||||
const envDirect =
|
return resolveProviderApiKeyFromConfigAndStore({
|
||||||
normalizeSecretInput(process.env.MINIMAX_CODE_PLAN_KEY) ||
|
providerId: "minimax",
|
||||||
normalizeSecretInput(process.env.MINIMAX_API_KEY);
|
envDirect: [process.env.MINIMAX_CODE_PLAN_KEY, process.env.MINIMAX_API_KEY],
|
||||||
if (envDirect) {
|
|
||||||
return envDirect;
|
|
||||||
}
|
|
||||||
|
|
||||||
const envResolved = resolveEnvApiKey("minimax");
|
|
||||||
if (envResolved?.apiKey) {
|
|
||||||
return envResolved.apiKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
const cfg = loadConfig();
|
|
||||||
const key = getCustomProviderApiKey(cfg, "minimax");
|
|
||||||
if (key) {
|
|
||||||
return key;
|
|
||||||
}
|
|
||||||
|
|
||||||
const store = ensureAuthProfileStore();
|
|
||||||
const apiProfile = listProfilesForProvider(store, "minimax").find((id) => {
|
|
||||||
const cred = store.profiles[id];
|
|
||||||
return cred?.type === "api_key" || cred?.type === "token";
|
|
||||||
});
|
});
|
||||||
if (!apiProfile) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
const cred = store.profiles[apiProfile];
|
|
||||||
if (cred?.type === "api_key" && normalizeSecretInput(cred.key)) {
|
|
||||||
return normalizeSecretInput(cred.key);
|
|
||||||
}
|
|
||||||
if (cred?.type === "token" && normalizeSecretInput(cred.token)) {
|
|
||||||
return normalizeSecretInput(cred.token);
|
|
||||||
}
|
|
||||||
return undefined;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolveXiaomiApiKey(): string | undefined {
|
function resolveXiaomiApiKey(): string | undefined {
|
||||||
const envDirect = normalizeSecretInput(process.env.XIAOMI_API_KEY);
|
return resolveProviderApiKeyFromConfigAndStore({
|
||||||
|
providerId: "xiaomi",
|
||||||
|
envDirect: [process.env.XIAOMI_API_KEY],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function resolveProviderApiKeyFromConfigAndStore(params: {
|
||||||
|
providerId: UsageProviderId;
|
||||||
|
envDirect: Array<string | undefined>;
|
||||||
|
}): string | undefined {
|
||||||
|
const envDirect = params.envDirect.map(normalizeSecretInput).find(Boolean);
|
||||||
if (envDirect) {
|
if (envDirect) {
|
||||||
return envDirect;
|
return envDirect;
|
||||||
}
|
}
|
||||||
|
|
||||||
const envResolved = resolveEnvApiKey("xiaomi");
|
const envResolved = resolveEnvApiKey(params.providerId);
|
||||||
if (envResolved?.apiKey) {
|
if (envResolved?.apiKey) {
|
||||||
return envResolved.apiKey;
|
return envResolved.apiKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
const cfg = loadConfig();
|
const cfg = loadConfig();
|
||||||
const key = getCustomProviderApiKey(cfg, "xiaomi");
|
const key = getCustomProviderApiKey(cfg, params.providerId);
|
||||||
if (key) {
|
if (key) {
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
const store = ensureAuthProfileStore();
|
const store = ensureAuthProfileStore();
|
||||||
const apiProfile = listProfilesForProvider(store, "xiaomi").find((id) => {
|
const apiProfile = listProfilesForProvider(store, params.providerId).find((id) => {
|
||||||
const cred = store.profiles[id];
|
const cred = store.profiles[id];
|
||||||
return cred?.type === "api_key" || cred?.type === "token";
|
return cred?.type === "api_key" || cred?.type === "token";
|
||||||
});
|
});
|
||||||
@@ -142,10 +122,10 @@ function resolveXiaomiApiKey(): string | undefined {
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
const cred = store.profiles[apiProfile];
|
const cred = store.profiles[apiProfile];
|
||||||
if (cred?.type === "api_key" && normalizeSecretInput(cred.key)) {
|
if (cred?.type === "api_key") {
|
||||||
return normalizeSecretInput(cred.key);
|
return normalizeSecretInput(cred.key);
|
||||||
}
|
}
|
||||||
if (cred?.type === "token" && normalizeSecretInput(cred.token)) {
|
if (cred?.type === "token") {
|
||||||
return normalizeSecretInput(cred.token);
|
return normalizeSecretInput(cred.token);
|
||||||
}
|
}
|
||||||
return undefined;
|
return undefined;
|
||||||
|
|||||||
Reference in New Issue
Block a user