mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-02 06:50:21 +00:00
refactor: split provider config policy hooks
This commit is contained in:
24
src/agents/models-config.providers.policy.lookup.ts
Normal file
24
src/agents/models-config.providers.policy.lookup.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { MODEL_APIS } from "../config/types.models.js";
|
||||
import type { ProviderConfig } from "./models-config.providers.secrets.js";
|
||||
|
||||
const GENERIC_PROVIDER_APIS = new Set<string>([
|
||||
"openai-completions",
|
||||
"openai-responses",
|
||||
"anthropic-messages",
|
||||
"google-generative-ai",
|
||||
]);
|
||||
|
||||
export function resolveProviderPluginLookupKey(
|
||||
providerKey: string,
|
||||
provider?: ProviderConfig,
|
||||
): string {
|
||||
const api = typeof provider?.api === "string" ? provider.api.trim() : "";
|
||||
if (
|
||||
api &&
|
||||
MODEL_APIS.includes(api as (typeof MODEL_APIS)[number]) &&
|
||||
!GENERIC_PROVIDER_APIS.has(api)
|
||||
) {
|
||||
return api;
|
||||
}
|
||||
return providerKey;
|
||||
}
|
||||
54
src/agents/models-config.providers.policy.runtime.ts
Normal file
54
src/agents/models-config.providers.policy.runtime.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import {
|
||||
applyProviderNativeStreamingUsageCompatWithPlugin,
|
||||
normalizeProviderConfigWithPlugin,
|
||||
resolveProviderConfigApiKeyWithPlugin,
|
||||
} from "../plugins/provider-runtime.js";
|
||||
import { resolveProviderPluginLookupKey } from "./models-config.providers.policy.lookup.js";
|
||||
import type { ProviderConfig } from "./models-config.providers.secrets.js";
|
||||
|
||||
export function applyProviderNativeStreamingUsagePolicy(
|
||||
providerKey: string,
|
||||
provider: ProviderConfig,
|
||||
): ProviderConfig {
|
||||
const runtimeProviderKey = resolveProviderPluginLookupKey(providerKey, provider);
|
||||
return (
|
||||
applyProviderNativeStreamingUsageCompatWithPlugin({
|
||||
provider: runtimeProviderKey,
|
||||
context: {
|
||||
provider: providerKey,
|
||||
providerConfig: provider,
|
||||
},
|
||||
}) ?? provider
|
||||
);
|
||||
}
|
||||
|
||||
export function normalizeProviderConfigPolicy(
|
||||
providerKey: string,
|
||||
provider: ProviderConfig,
|
||||
): ProviderConfig {
|
||||
const runtimeProviderKey = resolveProviderPluginLookupKey(providerKey, provider);
|
||||
return (
|
||||
normalizeProviderConfigWithPlugin({
|
||||
provider: runtimeProviderKey,
|
||||
context: {
|
||||
provider: providerKey,
|
||||
providerConfig: provider,
|
||||
},
|
||||
}) ?? provider
|
||||
);
|
||||
}
|
||||
|
||||
export function resolveProviderConfigApiKeyPolicy(
|
||||
providerKey: string,
|
||||
provider?: ProviderConfig,
|
||||
): ((env: NodeJS.ProcessEnv) => string | undefined) | undefined {
|
||||
const runtimeProviderKey = resolveProviderPluginLookupKey(providerKey, provider).trim();
|
||||
return (env) =>
|
||||
resolveProviderConfigApiKeyWithPlugin({
|
||||
provider: runtimeProviderKey,
|
||||
context: {
|
||||
provider: providerKey,
|
||||
env,
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -1,29 +1,10 @@
|
||||
import { MODEL_APIS } from "../config/types.models.js";
|
||||
import {
|
||||
applyProviderNativeStreamingUsageCompatWithPlugin,
|
||||
normalizeProviderConfigWithPlugin,
|
||||
resolveProviderConfigApiKeyWithPlugin,
|
||||
} from "../plugins/provider-runtime.js";
|
||||
applyProviderNativeStreamingUsagePolicy,
|
||||
normalizeProviderConfigPolicy,
|
||||
resolveProviderConfigApiKeyPolicy,
|
||||
} from "./models-config.providers.policy.runtime.js";
|
||||
import type { ProviderConfig } from "./models-config.providers.secrets.js";
|
||||
|
||||
const GENERIC_PROVIDER_APIS = new Set<string>([
|
||||
"openai-completions",
|
||||
"openai-responses",
|
||||
"anthropic-messages",
|
||||
"google-generative-ai",
|
||||
]);
|
||||
function resolveProviderPluginLookupKey(providerKey: string, provider?: ProviderConfig): string {
|
||||
const api = typeof provider?.api === "string" ? provider.api.trim() : "";
|
||||
if (
|
||||
api &&
|
||||
MODEL_APIS.includes(api as (typeof MODEL_APIS)[number]) &&
|
||||
!GENERIC_PROVIDER_APIS.has(api)
|
||||
) {
|
||||
return api;
|
||||
}
|
||||
return providerKey;
|
||||
}
|
||||
|
||||
export function applyNativeStreamingUsageCompat(
|
||||
providers: Record<string, ProviderConfig>,
|
||||
): Record<string, ProviderConfig> {
|
||||
@@ -31,15 +12,7 @@ export function applyNativeStreamingUsageCompat(
|
||||
const nextProviders: Record<string, ProviderConfig> = {};
|
||||
|
||||
for (const [providerKey, provider] of Object.entries(providers)) {
|
||||
const runtimeProviderKey = resolveProviderPluginLookupKey(providerKey, provider);
|
||||
const nextProvider =
|
||||
applyProviderNativeStreamingUsageCompatWithPlugin({
|
||||
provider: runtimeProviderKey,
|
||||
context: {
|
||||
provider: providerKey,
|
||||
providerConfig: provider,
|
||||
},
|
||||
}) ?? provider;
|
||||
const nextProvider = applyProviderNativeStreamingUsagePolicy(providerKey, provider);
|
||||
nextProviders[providerKey] = nextProvider;
|
||||
changed ||= nextProvider !== provider;
|
||||
}
|
||||
@@ -51,15 +24,7 @@ export function normalizeProviderSpecificConfig(
|
||||
providerKey: string,
|
||||
provider: ProviderConfig,
|
||||
): ProviderConfig {
|
||||
const runtimeProviderKey = resolveProviderPluginLookupKey(providerKey, provider);
|
||||
const normalized =
|
||||
normalizeProviderConfigWithPlugin({
|
||||
provider: runtimeProviderKey,
|
||||
context: {
|
||||
provider: providerKey,
|
||||
providerConfig: provider,
|
||||
},
|
||||
}) ?? undefined;
|
||||
const normalized = normalizeProviderConfigPolicy(providerKey, provider);
|
||||
if (normalized && normalized !== provider) {
|
||||
return normalized;
|
||||
}
|
||||
@@ -70,13 +35,5 @@ export function resolveProviderConfigApiKeyResolver(
|
||||
providerKey: string,
|
||||
provider?: ProviderConfig,
|
||||
): ((env: NodeJS.ProcessEnv) => string | undefined) | undefined {
|
||||
const runtimeProviderKey = resolveProviderPluginLookupKey(providerKey, provider).trim();
|
||||
return (env) =>
|
||||
resolveProviderConfigApiKeyWithPlugin({
|
||||
provider: runtimeProviderKey,
|
||||
context: {
|
||||
provider: providerKey,
|
||||
env,
|
||||
},
|
||||
});
|
||||
return resolveProviderConfigApiKeyPolicy(providerKey, provider);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user