Files
openclaw/src/plugins/runtime-channel-state.ts
Peter Steinberger 2aa4ff0825 refactor: consolidate duplicate helper exports (#106016)
* refactor(normalization): consolidate string helpers

* refactor(normalization): consolidate agent ids

* refactor(paths): consolidate user path resolution

* refactor(gateway): preserve transcript helper facade

* fix(normalization): align helper build aliases

* fix(memory): keep helper import line neutral

* chore(plugin-sdk): align consolidated helper surface

* refactor(normalization): reuse lowercase string helper

* refactor(normalization): reuse record guard

* refactor(normalization): share surrogate boundary helper

* refactor(agents): share token estimate constant

* refactor(memory): distinguish standalone helper contracts

* refactor(normalization): share error cause formatter

* refactor(config): distinguish eligibility predicates

* refactor(plugins): share registry state symbol

* refactor(cli): reuse outbound dependency adapter

* refactor(cron): distinguish positive duration parser

* fix(gateway): keep transcript helper private

* fix(paths): preserve public helper status

* refactor(channels): reuse registry normalizer

* refactor(agents): reuse agent core runtime facade

* refactor(auth): reuse locked profile upsert

* refactor(records): distinguish trap-safe guard

* refactor(migrations): distinguish sync directory helper

* test(plugins): drop stale duration alias expectations

* fix(channels): remove stale registry import

* chore(plugin-sdk): refresh helper API baseline

* fix(memory): keep renamed helpers private

* fix(plugins): keep registry state key private

* fix(plugin-sdk): tighten wildcard surface budget

* chore(plugin-sdk): refresh rebased helper API baseline
2026-07-13 03:22:00 -07:00

84 lines
3.1 KiB
TypeScript

// Stores active plugin channel registry state for the current runtime.
import type { ActivePluginChannelRegistry } from "./channel-registry-state.types.js";
import { PLUGIN_REGISTRY_STATE } from "./runtime-state-key.js";
type GlobalChannelRegistryState = typeof globalThis & {
[PLUGIN_REGISTRY_STATE]?: {
activeVersion?: number;
activeRegistry?: ActivePluginChannelRegistry | null;
channel?: {
registry: ActivePluginChannelRegistry | null;
version?: number;
};
};
};
type GlobalChannelRegistryRuntimeState = GlobalChannelRegistryState[typeof PLUGIN_REGISTRY_STATE];
export type ActivePluginChannelRegistrySnapshot = {
registry: ActivePluginChannelRegistry | null;
version: number;
};
let activePluginChannelRegistrySnapshot:
| {
state: GlobalChannelRegistryRuntimeState;
pinnedRegistry: ActivePluginChannelRegistry | null;
activeRegistry: ActivePluginChannelRegistry | null;
pinnedChannelCount: number;
activeChannelCount: number;
snapshot: ActivePluginChannelRegistrySnapshot;
}
| undefined;
function countChannels(registry: ActivePluginChannelRegistry | null | undefined): number {
return registry?.channels?.length ?? 0;
}
/** Returns a cached channel registry snapshot, preferring pinned channel state when populated. */
export function getActivePluginChannelRegistrySnapshotFromState(): ActivePluginChannelRegistrySnapshot {
const state = (globalThis as GlobalChannelRegistryState)[PLUGIN_REGISTRY_STATE];
const pinnedRegistry = state?.channel?.registry ?? null;
const activeRegistry = state?.activeRegistry ?? null;
const pinnedChannelCount = countChannels(pinnedRegistry);
const activeChannelCount = countChannels(activeRegistry);
const selectedPinnedRegistry =
pinnedChannelCount > 0 || (pinnedRegistry !== null && activeChannelCount === 0);
const version = selectedPinnedRegistry
? (state?.channel?.version ?? 0)
: (state?.activeVersion ?? 0);
const cached = activePluginChannelRegistrySnapshot;
if (
cached &&
cached.state === state &&
cached.pinnedRegistry === pinnedRegistry &&
cached.activeRegistry === activeRegistry &&
cached.pinnedChannelCount === pinnedChannelCount &&
cached.activeChannelCount === activeChannelCount &&
cached.snapshot.version === version
) {
return cached.snapshot;
}
const registry = selectedPinnedRegistry ? pinnedRegistry : activeRegistry;
const snapshot = { registry, version };
activePluginChannelRegistrySnapshot = {
state,
pinnedRegistry,
activeRegistry,
pinnedChannelCount,
activeChannelCount,
snapshot,
};
return snapshot;
}
/** Returns the active plugin channel registry from global runtime state. */
export function getActivePluginChannelRegistryFromState(): ActivePluginChannelRegistry | null {
return getActivePluginChannelRegistrySnapshotFromState().registry;
}
/** Returns the active plugin channel registry version from global runtime state. */
export function getActivePluginChannelRegistryVersionFromState(): number {
return getActivePluginChannelRegistrySnapshotFromState().version;
}