refactor: split provider config policy hooks

This commit is contained in:
Peter Steinberger
2026-04-05 17:17:00 +01:00
parent acd78e0c2f
commit 15aed55470
3 changed files with 85 additions and 50 deletions

View 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;
}

View 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,
},
});
}

View File

@@ -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);
}