refactor(models): add scoped model reference helpers

This commit is contained in:
Marcus Castro
2026-05-06 01:50:17 -03:00
parent 7fe8dc7f3b
commit aa24878b98
2 changed files with 68 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
import type { OpenClawConfig } from "../config/types.openclaw.js";
import { findNormalizedProviderValue, normalizeProviderId } from "./provider-id.js";
function dedupeCatalogScopeRefs(values: Array<string | undefined>): string[] {
const refs = new Set<string>();
for (const value of values) {
const trimmed = value?.trim();
if (trimmed) {
refs.add(trimmed);
}
}
return [...refs];
}
function providerFromModelRef(value: string | undefined): string | undefined {
const trimmed = value?.trim();
if (!trimmed) {
return undefined;
}
const slash = trimmed.indexOf("/");
if (slash <= 0) {
return undefined;
}
const provider = normalizeProviderId(trimmed.slice(0, slash));
return provider || undefined;
}
export function resolveModelCatalogScope(params: {
cfg?: OpenClawConfig;
provider: string;
model: string;
}): { providerRefs: string[]; modelRefs: string[] } {
const provider = params.provider.trim();
const model = params.model.trim();
const providerConfig = findNormalizedProviderValue(params.cfg?.models?.providers, provider);
return {
providerRefs: dedupeCatalogScopeRefs([provider, providerConfig?.api]),
modelRefs: dedupeCatalogScopeRefs([provider && model ? `${provider}/${model}` : model, model]),
};
}
export function resolveProviderDiscoveryProviderIdsForCatalogScope(params: {
providerRefs?: readonly string[];
modelRefs?: readonly string[];
}): string[] | undefined {
const providerIds = dedupeCatalogScopeRefs([
...(params.providerRefs ?? []),
...(params.modelRefs ?? []).map(providerFromModelRef),
]);
return providerIds.length > 0 ? providerIds : undefined;
}

View File

@@ -1,3 +1,4 @@
import { normalizeProviderId } from "../agents/provider-id.js";
import { isRecord } from "../utils.js";
export type ConfiguredModelRef = {
@@ -115,3 +116,19 @@ export function collectConfiguredModelRefs(
);
return refs;
}
export function collectConfiguredModelRefValues(
config: unknown,
options?: { includeChannelModelOverrides?: boolean },
): string[] {
return collectConfiguredModelRefs(config, options).map((ref) => ref.value);
}
export function extractProviderFromModelRef(value: string): string | null {
const trimmed = value.trim();
const slash = trimmed.indexOf("/");
if (slash <= 0) {
return null;
}
return normalizeProviderId(trimmed.slice(0, slash));
}