From 57b55883c5e9db8fac3f28b90e14a69380dcef2f Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 18 Apr 2026 21:43:28 +0100 Subject: [PATCH] refactor: share live provider owner matching --- src/agents/live-model-filter.ts | 36 ++-------------------------- src/agents/live-provider-owner.ts | 40 +++++++++++++++++++++++++++++++ src/agents/live-target-matcher.ts | 38 +++-------------------------- 3 files changed, 45 insertions(+), 69 deletions(-) create mode 100644 src/agents/live-provider-owner.ts diff --git a/src/agents/live-model-filter.ts b/src/agents/live-model-filter.ts index 217677353cb..fdca2911984 100644 --- a/src/agents/live-model-filter.ts +++ b/src/agents/live-model-filter.ts @@ -1,7 +1,7 @@ import type { OpenClawConfig } from "../config/types.openclaw.js"; import { resolveProviderModernModelRef } from "../plugins/provider-runtime.js"; -import { resolveOwningPluginIdsForProvider } from "../plugins/providers.js"; import { normalizeLowercaseStringOrEmpty } from "../shared/string-coerce.js"; +import { liveProvidersShareOwningPlugin } from "./live-provider-owner.js"; import { normalizeProviderId } from "./provider-id.js"; export type ModelRef = { @@ -100,36 +100,6 @@ export function isHighSignalLiveModelRef(ref: ModelRef): boolean { return isHighSignalClaudeModelId(id); } -function sharesOwningPlugin(params: { - left: string; - right: string; - config?: OpenClawConfig; - workspaceDir?: string; - env?: NodeJS.ProcessEnv; - ownerCache: Map; -}): boolean { - const resolveOwners = (provider: string): readonly string[] => { - const normalized = normalizeProviderId(provider); - const cached = params.ownerCache.get(normalized); - if (cached) { - return cached; - } - const owners = - resolveOwningPluginIdsForProvider({ - provider: normalized, - config: params.config, - workspaceDir: params.workspaceDir, - env: params.env, - }) ?? []; - params.ownerCache.set(normalized, owners); - return owners; - }; - - const leftOwners = resolveOwners(params.left); - const rightOwners = resolveOwners(params.right); - return leftOwners.some((owner) => rightOwners.includes(owner)); -} - export function shouldExcludeProviderFromDefaultHighSignalLiveSweep(params: { provider?: string | null; useExplicitModels: boolean; @@ -153,9 +123,7 @@ export function shouldExcludeProviderFromDefaultHighSignalLiveSweep(params: { } if ( requestedProvider && - sharesOwningPlugin({ - left: requestedProvider, - right: provider, + liveProvidersShareOwningPlugin(requestedProvider, provider, { config: params.config, workspaceDir: params.workspaceDir, env: params.env, diff --git a/src/agents/live-provider-owner.ts b/src/agents/live-provider-owner.ts new file mode 100644 index 00000000000..ed091fc4b39 --- /dev/null +++ b/src/agents/live-provider-owner.ts @@ -0,0 +1,40 @@ +import type { OpenClawConfig } from "../config/types.openclaw.js"; +import { resolveOwningPluginIdsForProvider } from "../plugins/providers.js"; +import { normalizeProviderId } from "./provider-id.js"; + +export type LiveProviderOwnerContext = { + config?: OpenClawConfig; + workspaceDir?: string; + env?: NodeJS.ProcessEnv; + ownerCache: Map; +}; + +export function resolveCachedOwningPluginIdsForProvider( + provider: string, + context: LiveProviderOwnerContext, +): readonly string[] { + const normalized = normalizeProviderId(provider); + const cached = context.ownerCache.get(normalized); + if (cached) { + return cached; + } + const owners = + resolveOwningPluginIdsForProvider({ + provider: normalized, + config: context.config, + workspaceDir: context.workspaceDir, + env: context.env, + }) ?? []; + context.ownerCache.set(normalized, owners); + return owners; +} + +export function liveProvidersShareOwningPlugin( + left: string, + right: string, + context: LiveProviderOwnerContext, +): boolean { + const leftOwners = resolveCachedOwningPluginIdsForProvider(left, context); + const rightOwners = resolveCachedOwningPluginIdsForProvider(right, context); + return leftOwners.some((owner) => rightOwners.includes(owner)); +} diff --git a/src/agents/live-target-matcher.ts b/src/agents/live-target-matcher.ts index fb1c909ae5b..833906534b5 100644 --- a/src/agents/live-target-matcher.ts +++ b/src/agents/live-target-matcher.ts @@ -1,9 +1,9 @@ import type { OpenClawConfig } from "../config/types.openclaw.js"; -import { resolveOwningPluginIdsForProvider } from "../plugins/providers.js"; import { normalizeLowercaseStringOrEmpty, normalizeOptionalLowercaseString, } from "../shared/string-coerce.js"; +import { liveProvidersShareOwningPlugin } from "./live-provider-owner.js"; import { normalizeProviderId } from "./provider-id.js"; type ModelTarget = { @@ -51,38 +51,6 @@ function parseModelTarget(raw: string): ModelTarget | null { }; } -function hasSharedOwner( - left: string, - right: string, - params: { - config?: OpenClawConfig; - workspaceDir?: string; - env?: NodeJS.ProcessEnv; - ownerCache: Map; - }, -): boolean { - const resolveOwners = (provider: string): readonly string[] => { - const normalized = normalizeProviderId(provider); - const cached = params.ownerCache.get(normalized); - if (cached) { - return cached; - } - const owners = - resolveOwningPluginIdsForProvider({ - provider: normalized, - config: params.config, - workspaceDir: params.workspaceDir, - env: params.env, - }) ?? []; - params.ownerCache.set(normalized, owners); - return owners; - }; - - const leftOwners = resolveOwners(left); - const rightOwners = resolveOwners(right); - return leftOwners.some((owner) => rightOwners.includes(owner)); -} - export function createLiveTargetMatcher(params: { providerFilter: Set | null; modelFilter: Set | null; @@ -108,7 +76,7 @@ export function createLiveTargetMatcher(params: { return true; } if ( - hasSharedOwner(normalizedRequested, normalizedProvider, { + liveProvidersShareOwningPlugin(normalizedRequested, normalizedProvider, { config: params.config, workspaceDir: params.workspaceDir, env: params.env, @@ -144,7 +112,7 @@ export function createLiveTargetMatcher(params: { return true; } if ( - hasSharedOwner(target.provider, normalizedProvider, { + liveProvidersShareOwningPlugin(target.provider, normalizedProvider, { config: params.config, workspaceDir: params.workspaceDir, env: params.env,