File size: 6,891 Bytes
fcd8223 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | // Stores active runtime plugin registry state and activation metadata.
import { normalizeSortedUniqueStringEntries } from "@openclaw/normalization-core/string-normalization";
import { resolvePluginLoadCacheContext } from "./loader-load-context.js";
import type { PluginLoadOptions } from "./loader-types.js";
import type { PluginManifestRecord } from "./manifest-registry.js";
import { matchesPluginRuntimeArtifactSelection } from "./plugin-runtime-artifact-binding.js";
import type { PluginRecord, PluginRegistry } from "./registry-types.js";
import {
getActivePluginRegistry,
getActivePluginRegistryKey,
getActivePluginRegistryWorkspaceDir,
} from "./runtime.js";
import { getPluginRuntimeLoadContextState } from "./runtime/load-context-state.js";
export function getActiveRuntimePluginRegistry(): PluginRegistry | null {
return getActivePluginRegistry();
}
/** Return the exact active registry without triggering a fresh load on cache miss. */
export function resolveCompatibleRuntimePluginRegistry(
options?: PluginLoadOptions,
): PluginRegistry | undefined {
const activeRegistry = getActivePluginRegistry() ?? undefined;
if (!activeRegistry || options === undefined) {
return activeRegistry;
}
const activeCacheKey = getActivePluginRegistryKey();
if (!activeCacheKey) {
return undefined;
}
const requestedKey = resolvePluginLoadCacheContext(options).cacheKey;
if (requestedKey === activeCacheKey) {
return activeRegistry;
}
const identity = getPluginRuntimeLoadContextState(activeRegistry)?.loaderCacheIdentity;
return identity &&
((identity.requestKey === activeCacheKey && identity.resolvedKey === requestedKey) ||
(identity.resolvedKey === activeCacheKey && identity.requestKey === requestedKey))
? activeRegistry
: undefined;
}
function isRuntimePluginRecordLoaded(plugin: PluginRecord): boolean {
return plugin.status === "loaded" && (plugin.format === "bundle" || plugin.imported !== false);
}
/** Lists runtime-loaded plugin ids from an immutable/request-scoped registry handle. */
export function listRuntimePluginIdsFromRegistry(registry: PluginRegistry): string[] {
return normalizeSortedUniqueStringEntries(
registry.plugins.filter(isRuntimePluginRecordLoaded).map((plugin) => plugin.id),
);
}
export function listLoadedRuntimePluginIds(): string[] {
const registry = getActivePluginRegistry();
return registry ? listRuntimePluginIdsFromRegistry(registry) : [];
}
function normalizeRequiredPluginIds(ids?: readonly string[]): string[] | undefined {
if (ids === undefined) {
return undefined;
}
return normalizeSortedUniqueStringEntries(ids);
}
export function registryContainsRuntimePluginIds(
registry: PluginRegistry,
pluginIds: readonly string[] | undefined,
): boolean {
if (pluginIds === undefined) {
return true;
}
if (pluginIds.length === 0 && registry.plugins.length > 0) {
return false;
}
const missing = new Set(pluginIds);
for (const plugin of registry.plugins) {
if (plugin.status === undefined || isRuntimePluginRecordLoaded(plugin)) {
missing.delete(plugin.id);
}
}
if (pluginIds.length > 0 && missing.size === 0) {
return true;
}
// Loader records decide runtime availability. Direct SDK registrations can
// lack a record, but must never revive a disabled, failed, or deferred owner.
if (registry.plugins.some((plugin) => missing.has(plugin.id))) {
return false;
}
for (const [key, value] of Object.entries(registry)) {
if (key === "diagnostics" || key === "channelSetups" || !Array.isArray(value)) {
continue;
}
for (const entry of value) {
if (entry && typeof entry === "object" && "pluginId" in entry) {
const pluginId = entry.pluginId;
if (typeof pluginId === "string" && pluginId.length > 0) {
if (pluginIds.length === 0) {
return false;
}
missing.delete(pluginId);
if (missing.size === 0) {
return true;
}
}
}
}
}
return pluginIds.length === 0;
}
/** Indexes selected owners; omitted artifact preference retains the loaded owner's policy. */
export function createRuntimePluginManifestLookup(
registry: PluginRegistry,
manifestPlugins: readonly PluginManifestRecord[],
preferBuiltPluginArtifacts?: boolean,
): (pluginId: string) => PluginRecord | undefined {
// Loader order chooses the owner; later disabled duplicates are diagnostics only.
const records = new Map(
registry.plugins.filter(isRuntimePluginRecordLoaded).map((plugin) => [plugin.id, plugin]),
);
const manifests = new Map(manifestPlugins.toReversed().map((plugin) => [plugin.id, plugin]));
return (pluginId) => {
const record = records.get(pluginId);
const manifest = manifests.get(pluginId);
return record &&
manifest &&
record.origin === manifest.origin &&
matchesPluginRuntimeArtifactSelection(record, manifest, preferBuiltPluginArtifacts)
? record
: undefined;
};
}
export function getLoadedRuntimePluginRegistry(
params: {
env?: NodeJS.ProcessEnv;
loadOptions?: PluginLoadOptions;
workspaceDir?: string;
requiredPluginIds?: readonly string[];
} = {},
): PluginRegistry | undefined {
const requiredPluginIds = normalizeRequiredPluginIds(
params.requiredPluginIds ?? params.loadOptions?.onlyPluginIds,
);
if (params.loadOptions && requiredPluginIds === undefined) {
// Unscoped requests need the full load identity. Bounded manifest scopes
// can compare their prepared ownership facts below.
return resolveCompatibleRuntimePluginRegistry(params.loadOptions);
}
const activeWorkspaceDir = getActivePluginRegistryWorkspaceDir();
const requestedWorkspaceDir = params.workspaceDir ?? params.loadOptions?.workspaceDir;
if (
(Object.hasOwn(params, "workspaceDir") ||
params.loadOptions ||
requestedWorkspaceDir !== undefined) &&
activeWorkspaceDir !== requestedWorkspaceDir
) {
return undefined;
}
const registry = getActivePluginRegistry();
if (!registry) {
return undefined;
}
if (
!registryContainsRuntimePluginIds(registry, requiredPluginIds) ||
(params.loadOptions?.manifestRegistry &&
requiredPluginIds !== undefined &&
!requiredPluginIds.every(
createRuntimePluginManifestLookup(
registry,
params.loadOptions.manifestRegistry.plugins,
params.loadOptions.preferBuiltPluginArtifacts,
),
))
) {
return undefined;
}
// Raw discovery has not established manifest winners, so ID containment alone
// cannot prove which candidate would load. Prepared manifests keep the fast path.
if (params.loadOptions?.discovery && !params.loadOptions.manifestRegistry) {
return resolveCompatibleRuntimePluginRegistry(params.loadOptions);
}
return registry;
}
|