refactor: dedupe provider registry normalizers

This commit is contained in:
Peter Steinberger
2026-04-07 09:47:02 +01:00
parent bf040219e4
commit dd3e86d35b
4 changed files with 23 additions and 32 deletions

View File

@@ -1,9 +1,14 @@
import { normalizeOptionalString } from "../shared/string-coerce.js";
export function normalizeCapabilityProviderId(providerId: string | undefined): string | undefined {
return normalizeOptionalString(providerId)?.toLowerCase();
}
export function buildCapabilityProviderMaps<T extends { id: string; aliases?: readonly string[] }>(
providers: readonly T[],
normalizeId: (providerId: string | undefined) => string | undefined = (providerId) =>
normalizeOptionalString(providerId)?.toLowerCase(),
normalizeId: (
providerId: string | undefined,
) => string | undefined = normalizeCapabilityProviderId,
): {
canonical: Map<string, T>;
aliases: Map<string, T>;

View File

@@ -1,13 +1,16 @@
import type { OpenClawConfig } from "../config/config.js";
import { resolvePluginCapabilityProviders } from "../plugins/capability-provider-runtime.js";
import {
buildCapabilityProviderMaps,
normalizeCapabilityProviderId,
} from "../plugins/provider-registry-shared.js";
import type { RealtimeTranscriptionProviderPlugin } from "../plugins/types.js";
import { normalizeOptionalString } from "../shared/string-coerce.js";
import type { RealtimeTranscriptionProviderId } from "./provider-types.js";
export function normalizeRealtimeTranscriptionProviderId(
providerId: string | undefined,
): RealtimeTranscriptionProviderId | undefined {
return normalizeOptionalString(providerId)?.toLowerCase();
return normalizeCapabilityProviderId(providerId);
}
function resolveRealtimeTranscriptionProviderEntries(
@@ -23,28 +26,7 @@ function buildProviderMaps(cfg?: OpenClawConfig): {
canonical: Map<string, RealtimeTranscriptionProviderPlugin>;
aliases: Map<string, RealtimeTranscriptionProviderPlugin>;
} {
const canonical = new Map<string, RealtimeTranscriptionProviderPlugin>();
const aliases = new Map<string, RealtimeTranscriptionProviderPlugin>();
const register = (provider: RealtimeTranscriptionProviderPlugin) => {
const id = normalizeRealtimeTranscriptionProviderId(provider.id);
if (!id) {
return;
}
canonical.set(id, provider);
aliases.set(id, provider);
for (const alias of provider.aliases ?? []) {
const normalizedAlias = normalizeRealtimeTranscriptionProviderId(alias);
if (normalizedAlias) {
aliases.set(normalizedAlias, provider);
}
}
};
for (const provider of resolveRealtimeTranscriptionProviderEntries(cfg)) {
register(provider);
}
return { canonical, aliases };
return buildCapabilityProviderMaps(resolveRealtimeTranscriptionProviderEntries(cfg));
}
export function listRealtimeTranscriptionProviders(

View File

@@ -1,14 +1,16 @@
import type { OpenClawConfig } from "../config/config.js";
import { resolvePluginCapabilityProviders } from "../plugins/capability-provider-runtime.js";
import { buildCapabilityProviderMaps } from "../plugins/provider-registry-shared.js";
import {
buildCapabilityProviderMaps,
normalizeCapabilityProviderId,
} from "../plugins/provider-registry-shared.js";
import type { RealtimeVoiceProviderPlugin } from "../plugins/types.js";
import { normalizeOptionalString } from "../shared/string-coerce.js";
import type { RealtimeVoiceProviderId } from "./provider-types.js";
export function normalizeRealtimeVoiceProviderId(
providerId: string | undefined,
): RealtimeVoiceProviderId | undefined {
return normalizeOptionalString(providerId)?.toLowerCase();
return normalizeCapabilityProviderId(providerId);
}
function resolveRealtimeVoiceProviderEntries(cfg?: OpenClawConfig): RealtimeVoiceProviderPlugin[] {

View File

@@ -1,14 +1,16 @@
import type { OpenClawConfig } from "../config/config.js";
import { resolvePluginCapabilityProviders } from "../plugins/capability-provider-runtime.js";
import { buildCapabilityProviderMaps } from "../plugins/provider-registry-shared.js";
import {
buildCapabilityProviderMaps,
normalizeCapabilityProviderId,
} from "../plugins/provider-registry-shared.js";
import type { SpeechProviderPlugin } from "../plugins/types.js";
import { normalizeOptionalString } from "../shared/string-coerce.js";
import type { SpeechProviderId } from "./provider-types.js";
export function normalizeSpeechProviderId(
providerId: string | undefined,
): SpeechProviderId | undefined {
return normalizeOptionalString(providerId)?.toLowerCase();
return normalizeCapabilityProviderId(providerId);
}
function resolveSpeechProviderPluginEntries(cfg?: OpenClawConfig): SpeechProviderPlugin[] {