Files
OmniRoute/src/shared/utils/noAuthProviders.ts
Rouzbeh† 805252a924 feat(mcp): implement dynamic runtime tool schema plumbing for blocked providers (#11100) (#11155)
Cherry-picked onto the current tip (authorship preserved), generated-count noise stripped. Two pre-merge adjustments: (1) dropped the unrelated localDb.ts re-export hunk (nothing in this PR uses those symbols); (2) automated security review flagged the blocked-provider list resolving once at server creation — the handler now rebuilds the schema per invocation via the resolver (advertised tools/list schema stays a creation-time snapshot, which is inherent to MCP). Vitest: new runtime-blocked-schema suite 3/3, full MCP __tests__ 118/118; contract suites (mcp-web-search-provider-enum-contract, search-blocked-providers-11100) 6/6; typecheck clean. This closes the residual gap noted when #11120 was closed. Thank you @rqzbeh!
2026-08-22 20:23:04 -03:00

82 lines
3.3 KiB
TypeScript

import { NOAUTH_PROVIDERS, getProviderById } from "@/shared/constants/providers";
type ProviderWithAlias = { alias?: string };
type NoAuthProviderEntry = { id: string; alias?: string };
const noAuthProviderEntries = Object.values(NOAUTH_PROVIDERS) as NoAuthProviderEntry[];
export function normalizeBlockedProviderSet(blockedProviders: unknown): Set<string> {
const entries = blockedProviders instanceof Set ? Array.from(blockedProviders) : blockedProviders;
return new Set(
Array.isArray(entries)
? entries.filter(
(provider): provider is string => typeof provider === "string" && provider.length > 0
)
: []
);
}
export function isProviderBlockedByIdOrAlias(
providerId: string,
blockedProviders: unknown
): boolean {
const blockedProviderSet = normalizeBlockedProviderSet(blockedProviders);
const provider = getProviderById(providerId) as ProviderWithAlias | undefined;
const baseId = providerId.replace(/-search$/, "");
return (
blockedProviderSet.has(providerId) ||
blockedProviderSet.has(baseId) ||
(typeof provider?.alias === "string" && blockedProviderSet.has(provider.alias))
);
}
export function isNoAuthProviderKey(...keys: Array<string | null | undefined>): boolean {
return noAuthProviderEntries.some((provider) =>
keys.some((key) => key === provider.id || key === provider.alias)
);
}
export function isNoAuthProviderBlocked(
blockedProviders: unknown,
...keys: Array<string | null | undefined>
): boolean {
const blockedProviderSet = normalizeBlockedProviderSet(blockedProviders);
return noAuthProviderEntries.some(
(provider) =>
keys.some((key) => key === provider.id || key === provider.alias) &&
(blockedProviderSet.has(provider.id) ||
(typeof provider.alias === "string" && blockedProviderSet.has(provider.alias)))
);
}
/**
* Partition a list of no-auth provider entries into the ones that are visible
* (not blocked) and the ones currently in `blockedProviders`, matched by either
* the provider id or its alias. Blocked entries are RETURNED (in `blocked`),
* never discarded — the dashboard surfaces them with a "Disabled" badge + an
* Enable button instead of silently hiding them (#5166/#5183: a disabled no-auth
* provider used to vanish from the All Providers page with no in-place restore).
* Order within each bucket is preserved.
*/
export function partitionNoAuthEntriesByBlocked<
T extends { providerId: string; provider: { alias?: string } },
>(entries: T[], blockedProviders: unknown): { visible: T[]; blocked: T[] } {
const blockedProviderSet = normalizeBlockedProviderSet(blockedProviders);
const visible: T[] = [];
const blocked: T[] = [];
for (const entry of entries) {
const alias = typeof entry.provider.alias === "string" ? entry.provider.alias : null;
const isBlocked =
blockedProviderSet.has(entry.providerId) || (alias !== null && blockedProviderSet.has(alias));
(isBlocked ? blocked : visible).push(entry);
}
return { visible, blocked };
}
export function isNoAuthRawProviderPrefix(providerId: string, prefix: string): boolean {
const provider = noAuthProviderEntries.find((entry) => entry.id === providerId);
return (
typeof provider?.alias === "string" && provider.alias !== providerId && prefix === providerId
);
}