refactor(plugins): share provider alias matching (#113982)

This commit is contained in:
Peter Steinberger
2026-07-25 20:51:33 -07:00
committed by GitHub
parent c66f0d340b
commit bb12c4b78c
4 changed files with 25 additions and 50 deletions

View File

@@ -1,8 +1,5 @@
// Runtime bridge for invoking provider hooks supplied by plugins.
import {
findNormalizedProviderValue,
normalizeProviderId,
} from "@openclaw/model-catalog-core/provider-id";
import { findNormalizedProviderValue } from "@openclaw/model-catalog-core/provider-id";
import {
normalizeLowercaseStringOrEmpty,
normalizeOptionalString,
@@ -18,6 +15,7 @@ import {
import { resolvePluginControlPlaneFingerprint } from "./plugin-control-plane-context.js";
import type { PluginMetadataRegistryView } from "./plugin-metadata-snapshot.types.js";
import { resolveProviderConfigApiOwnerHint } from "./provider-config-owner.js";
import { matchesProviderPluginRef } from "./provider-registry-shared.js";
import { isPluginProvidersLoadInFlight, resolvePluginProviders } from "./providers.runtime.js";
import type { PluginRegistry } from "./registry-types.js";
import {
@@ -62,19 +60,6 @@ export function clearProviderRuntimePluginCacheForTest(): void {
defaultProviderRuntimePluginCache.clear();
}
function matchesProviderId(provider: ProviderPlugin, providerId: string): boolean {
const normalized = normalizeProviderId(providerId);
if (!normalized) {
return false;
}
if (normalizeProviderId(provider.id) === normalized) {
return true;
}
return [...(provider.aliases ?? []), ...(provider.hookAliases ?? [])].some(
(alias) => normalizeProviderId(alias) === normalized,
);
}
function resolveProviderRuntimePluginCacheKey(
params: ProviderRuntimePluginLookupParams,
registryState = getPluginRegistryState(),
@@ -186,10 +171,10 @@ function findProviderRuntimePluginInRegistry(params: {
if (params.apiOwnerHint) {
return (
matchesProviderLiteralId(plugin, params.provider) ||
matchesProviderId(plugin, params.apiOwnerHint)
matchesProviderPluginRef(plugin, params.apiOwnerHint)
);
}
return matchesProviderId(plugin, params.provider);
return matchesProviderPluginRef(plugin, params.provider);
});
}
@@ -276,10 +261,10 @@ export function resolveProviderRuntimePlugin(
if (apiOwnerHint) {
return (
matchesProviderLiteralId(plugin, params.provider) ||
matchesProviderId(plugin, apiOwnerHint)
matchesProviderPluginRef(plugin, apiOwnerHint)
);
}
return matchesProviderId(plugin, params.provider);
return matchesProviderPluginRef(plugin, params.provider);
}) ?? null
);
};
@@ -335,7 +320,7 @@ export function resolveProviderHookPlugin(params: {
config: params.config,
workspaceDir: params.workspaceDir,
env: params.env,
}).find((candidate) => matchesProviderId(candidate, params.provider));
}).find((candidate) => matchesProviderPluginRef(candidate, params.provider));
}
export function resolveProviderRuntimePluginHandle(

View File

@@ -1,4 +1,5 @@
// Shares provider registry normalization helpers across plugin paths.
import { normalizeProviderId } from "@openclaw/model-catalog-core/provider-id";
import { normalizeOptionalLowercaseString } from "@openclaw/normalization-core/string-coerce";
import { isBlockedObjectKey } from "../infra/prototype-keys.js";
@@ -8,6 +9,20 @@ export function normalizeCapabilityProviderId(providerId: string | undefined): s
return normalized && !isBlockedObjectKey(normalized) ? normalized : undefined;
}
export function matchesProviderPluginRef(
provider: { id: string; aliases?: readonly string[]; hookAliases?: readonly string[] },
providerId: string,
): boolean {
const normalized = normalizeProviderId(providerId);
return Boolean(
normalized &&
(normalizeProviderId(provider.id) === normalized ||
[...(provider.aliases ?? []), ...(provider.hookAliases ?? [])].some(
(alias) => normalizeProviderId(alias) === normalized,
)),
);
}
/** Builds canonical and alias lookup maps for capability providers. */
export function buildCapabilityProviderMaps<T extends { id: string; aliases?: readonly string[] }>(
providers: readonly T[],

View File

@@ -41,6 +41,7 @@ import {
wrapProviderStreamFn,
} from "./provider-hook-runtime.js";
import { resolveBundledProviderPolicySurface } from "./provider-public-artifacts.js";
import { matchesProviderPluginRef } from "./provider-registry-shared.js";
import type { ProviderRuntimeModel } from "./provider-runtime-model.types.js";
import type { ProviderThinkingProfile } from "./provider-thinking.types.js";
import {
@@ -94,19 +95,6 @@ import type {
PluginTextTransforms,
} from "./types.js";
function matchesProviderPluginRef(provider: ProviderPlugin, providerId: string): boolean {
const normalized = normalizeProviderId(providerId);
if (!normalized) {
return false;
}
if (normalizeProviderId(provider.id) === normalized) {
return true;
}
return [...(provider.aliases ?? []), ...(provider.hookAliases ?? [])].some(
(alias) => normalizeProviderId(alias) === normalized,
);
}
function resolveProviderHookRefs(
provider: string,
providerConfig?: ModelProviderConfig,

View File

@@ -1,5 +1,5 @@
// Reads provider thinking policy from the active runtime registry only.
import { normalizeProviderId } from "@openclaw/model-catalog-core/provider-id";
import { matchesProviderPluginRef } from "./provider-registry-shared.js";
import type {
ProviderDefaultThinkingPolicyContext,
ProviderThinkingProfile,
@@ -28,19 +28,6 @@ type ThinkingHookParams<TContext> = {
context: TContext;
};
function matchesProviderId(provider: ActiveThinkingProvider, providerId: string): boolean {
const normalized = normalizeProviderId(providerId);
if (!normalized) {
return false;
}
if (normalizeProviderId(provider.id) === normalized) {
return true;
}
return [...(provider.aliases ?? []), ...(provider.hookAliases ?? [])].some(
(alias) => normalizeProviderId(alias) === normalized,
);
}
function resolveActiveThinkingProvider(providerId: string): ActiveThinkingProvider | undefined {
const state = (
globalThis as typeof globalThis & {
@@ -48,7 +35,7 @@ function resolveActiveThinkingProvider(providerId: string): ActiveThinkingProvid
}
)[PLUGIN_REGISTRY_STATE];
return state?.activeRegistry?.providers?.find((entry) =>
matchesProviderId(entry.provider, providerId),
matchesProviderPluginRef(entry.provider, providerId),
)?.provider;
}