mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-27 01:41:40 +00:00
116 lines
3.3 KiB
TypeScript
116 lines
3.3 KiB
TypeScript
import {
|
|
withBundledPluginAllowlistCompat,
|
|
withBundledPluginEnablementCompat,
|
|
} from "./bundled-compat.js";
|
|
import { resolveBundledWebSearchPluginIds } from "./bundled-web-search.js";
|
|
import {
|
|
hasExplicitPluginConfig,
|
|
normalizePluginsConfig,
|
|
type NormalizedPluginsConfig,
|
|
} from "./config-state.js";
|
|
import type { PluginLoadOptions } from "./loader.js";
|
|
import type { PluginWebSearchProviderEntry } from "./types.js";
|
|
|
|
function resolveBundledWebSearchCompatPluginIds(params: {
|
|
config?: PluginLoadOptions["config"];
|
|
workspaceDir?: string;
|
|
env?: PluginLoadOptions["env"];
|
|
}): string[] {
|
|
return resolveBundledWebSearchPluginIds({
|
|
config: params.config,
|
|
workspaceDir: params.workspaceDir,
|
|
env: params.env,
|
|
});
|
|
}
|
|
|
|
function withBundledWebSearchVitestCompat(params: {
|
|
config: PluginLoadOptions["config"];
|
|
pluginIds: readonly string[];
|
|
env?: PluginLoadOptions["env"];
|
|
}): PluginLoadOptions["config"] {
|
|
const env = params.env ?? process.env;
|
|
const isVitest = Boolean(env.VITEST || process.env.VITEST);
|
|
if (
|
|
!isVitest ||
|
|
hasExplicitPluginConfig(params.config?.plugins) ||
|
|
params.pluginIds.length === 0
|
|
) {
|
|
return params.config;
|
|
}
|
|
|
|
return {
|
|
...params.config,
|
|
plugins: {
|
|
...params.config?.plugins,
|
|
enabled: true,
|
|
allow: [...params.pluginIds],
|
|
slots: {
|
|
...params.config?.plugins?.slots,
|
|
memory: "none",
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
function compareWebSearchProvidersAlphabetically(
|
|
left: Pick<PluginWebSearchProviderEntry, "id" | "pluginId">,
|
|
right: Pick<PluginWebSearchProviderEntry, "id" | "pluginId">,
|
|
): number {
|
|
return left.id.localeCompare(right.id) || left.pluginId.localeCompare(right.pluginId);
|
|
}
|
|
|
|
export function sortWebSearchProviders(
|
|
providers: PluginWebSearchProviderEntry[],
|
|
): PluginWebSearchProviderEntry[] {
|
|
return providers.toSorted(compareWebSearchProvidersAlphabetically);
|
|
}
|
|
|
|
export function sortWebSearchProvidersForAutoDetect(
|
|
providers: PluginWebSearchProviderEntry[],
|
|
): PluginWebSearchProviderEntry[] {
|
|
return providers.toSorted((left, right) => {
|
|
const leftOrder = left.autoDetectOrder ?? Number.MAX_SAFE_INTEGER;
|
|
const rightOrder = right.autoDetectOrder ?? Number.MAX_SAFE_INTEGER;
|
|
if (leftOrder !== rightOrder) {
|
|
return leftOrder - rightOrder;
|
|
}
|
|
return compareWebSearchProvidersAlphabetically(left, right);
|
|
});
|
|
}
|
|
|
|
export function resolveBundledWebSearchResolutionConfig(params: {
|
|
config?: PluginLoadOptions["config"];
|
|
workspaceDir?: string;
|
|
env?: PluginLoadOptions["env"];
|
|
bundledAllowlistCompat?: boolean;
|
|
}): {
|
|
config: PluginLoadOptions["config"];
|
|
normalized: NormalizedPluginsConfig;
|
|
} {
|
|
const bundledCompatPluginIds = resolveBundledWebSearchCompatPluginIds({
|
|
config: params.config,
|
|
workspaceDir: params.workspaceDir,
|
|
env: params.env,
|
|
});
|
|
const allowlistCompat = params.bundledAllowlistCompat
|
|
? withBundledPluginAllowlistCompat({
|
|
config: params.config,
|
|
pluginIds: bundledCompatPluginIds,
|
|
})
|
|
: params.config;
|
|
const enablementCompat = withBundledPluginEnablementCompat({
|
|
config: allowlistCompat,
|
|
pluginIds: bundledCompatPluginIds,
|
|
});
|
|
const config = withBundledWebSearchVitestCompat({
|
|
config: enablementCompat,
|
|
pluginIds: bundledCompatPluginIds,
|
|
env: params.env,
|
|
});
|
|
|
|
return {
|
|
config,
|
|
normalized: normalizePluginsConfig(config?.plugins),
|
|
};
|
|
}
|