fix minimax cn api key 401 error (#396)
This commit is contained in:
committed by
GitHub
Unverified
parent
45d7ff61c3
commit
880995af19
@@ -24,6 +24,22 @@ type RuntimeProviderSyncContext = {
|
||||
api: string;
|
||||
};
|
||||
|
||||
function normalizeProviderBaseUrl(config: ProviderConfig, baseUrl?: string): string | undefined {
|
||||
if (!baseUrl) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (config.type === 'minimax-portal' || config.type === 'minimax-portal-cn') {
|
||||
return baseUrl.replace(/\/v1$/, '').replace(/\/anthropic$/, '').replace(/\/$/, '') + '/anthropic';
|
||||
}
|
||||
|
||||
return baseUrl;
|
||||
}
|
||||
|
||||
function shouldUseExplicitDefaultOverride(config: ProviderConfig, runtimeProviderKey: string): boolean {
|
||||
return Boolean(config.baseUrl || config.apiProtocol || runtimeProviderKey !== config.type);
|
||||
}
|
||||
|
||||
export function getOpenClawProviderKey(type: string, providerId: string): string {
|
||||
if (type === 'custom' || type === 'ollama') {
|
||||
const suffix = providerId.replace(/-/g, '').slice(0, 8);
|
||||
@@ -233,7 +249,7 @@ async function syncRuntimeProviderConfig(
|
||||
context: RuntimeProviderSyncContext,
|
||||
): Promise<void> {
|
||||
await syncProviderConfigToOpenClaw(context.runtimeProviderKey, config.model, {
|
||||
baseUrl: config.baseUrl || context.meta?.baseUrl,
|
||||
baseUrl: normalizeProviderBaseUrl(config, config.baseUrl || context.meta?.baseUrl),
|
||||
api: context.api,
|
||||
apiKeyEnv: context.meta?.apiKeyEnv,
|
||||
headers: context.meta?.headers,
|
||||
@@ -311,7 +327,16 @@ export async function syncUpdatedProviderToRuntime(
|
||||
if (defaultProviderId === config.id) {
|
||||
const modelOverride = config.model ? `${ock}/${config.model}` : undefined;
|
||||
if (config.type !== 'custom') {
|
||||
await setOpenClawDefaultModel(ock, modelOverride, fallbackModels);
|
||||
if (shouldUseExplicitDefaultOverride(config, ock)) {
|
||||
await setOpenClawDefaultModelWithOverride(ock, modelOverride, {
|
||||
baseUrl: normalizeProviderBaseUrl(config, config.baseUrl || context.meta?.baseUrl),
|
||||
api: context.api,
|
||||
apiKeyEnv: context.meta?.apiKeyEnv,
|
||||
headers: context.meta?.headers,
|
||||
}, fallbackModels);
|
||||
} else {
|
||||
await setOpenClawDefaultModel(ock, modelOverride, fallbackModels);
|
||||
}
|
||||
} else {
|
||||
await setOpenClawDefaultModelWithOverride(ock, modelOverride, {
|
||||
baseUrl: config.baseUrl,
|
||||
@@ -384,6 +409,13 @@ export async function syncDefaultProviderToRuntime(
|
||||
baseUrl: provider.baseUrl,
|
||||
api: provider.apiProtocol || 'openai-completions',
|
||||
}, fallbackModels);
|
||||
} else if (shouldUseExplicitDefaultOverride(provider, ock)) {
|
||||
await setOpenClawDefaultModelWithOverride(ock, modelOverride, {
|
||||
baseUrl: normalizeProviderBaseUrl(provider, provider.baseUrl || getProviderConfig(provider.type)?.baseUrl),
|
||||
api: provider.apiProtocol || getProviderConfig(provider.type)?.api,
|
||||
apiKeyEnv: getProviderConfig(provider.type)?.apiKeyEnv,
|
||||
headers: getProviderConfig(provider.type)?.headers,
|
||||
}, fallbackModels);
|
||||
} else {
|
||||
await setOpenClawDefaultModel(ock, modelOverride, fallbackModels);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { proxyAwareFetch } from '../../utils/proxy-fetch';
|
||||
import { getProviderConfig } from '../../utils/provider-registry';
|
||||
|
||||
type ValidationProfile =
|
||||
| 'openai-compatible'
|
||||
@@ -59,7 +60,18 @@ function logValidationRequest(
|
||||
);
|
||||
}
|
||||
|
||||
function getValidationProfile(providerType: string): ValidationProfile {
|
||||
function getValidationProfile(
|
||||
providerType: string,
|
||||
options?: { apiProtocol?: string }
|
||||
): ValidationProfile {
|
||||
const providerApi = options?.apiProtocol || getProviderConfig(providerType)?.api;
|
||||
if (providerApi === 'anthropic-messages') {
|
||||
return 'anthropic-header';
|
||||
}
|
||||
if (providerApi === 'openai-completions' || providerApi === 'openai-responses') {
|
||||
return 'openai-compatible';
|
||||
}
|
||||
|
||||
switch (providerType) {
|
||||
case 'anthropic':
|
||||
return 'anthropic-header';
|
||||
@@ -259,15 +271,8 @@ export async function validateApiKeyWithProvider(
|
||||
apiKey: string,
|
||||
options?: { baseUrl?: string; apiProtocol?: string },
|
||||
): Promise<{ valid: boolean; error?: string }> {
|
||||
let profile = getValidationProfile(providerType);
|
||||
|
||||
if (providerType === 'custom' && options?.apiProtocol) {
|
||||
if (options.apiProtocol === 'anthropic-messages') {
|
||||
profile = 'anthropic-header';
|
||||
} else {
|
||||
profile = 'openai-compatible';
|
||||
}
|
||||
}
|
||||
const profile = getValidationProfile(providerType, options);
|
||||
const resolvedBaseUrl = options?.baseUrl || getProviderConfig(providerType)?.baseUrl;
|
||||
|
||||
if (profile === 'none') {
|
||||
return { valid: true };
|
||||
@@ -281,11 +286,11 @@ export async function validateApiKeyWithProvider(
|
||||
try {
|
||||
switch (profile) {
|
||||
case 'openai-compatible':
|
||||
return await validateOpenAiCompatibleKey(providerType, trimmedKey, options?.baseUrl);
|
||||
return await validateOpenAiCompatibleKey(providerType, trimmedKey, resolvedBaseUrl);
|
||||
case 'google-query-key':
|
||||
return await validateGoogleQueryKey(providerType, trimmedKey, options?.baseUrl);
|
||||
return await validateGoogleQueryKey(providerType, trimmedKey, resolvedBaseUrl);
|
||||
case 'anthropic-header':
|
||||
return await validateAnthropicHeaderKey(providerType, trimmedKey, options?.baseUrl);
|
||||
return await validateAnthropicHeaderKey(providerType, trimmedKey, resolvedBaseUrl);
|
||||
case 'openrouter':
|
||||
return await validateOpenRouterKey(providerType, trimmedKey);
|
||||
default:
|
||||
|
||||
Reference in New Issue
Block a user