mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 10:20:42 +00:00
refactor: share active plugin runtime lookup
This commit is contained in:
28
src/plugins/active-runtime-registry.ts
Normal file
28
src/plugins/active-runtime-registry.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { createRequire } from "node:module";
|
||||
import type { PluginRegistry } from "./registry-types.js";
|
||||
|
||||
type PluginRuntimeModule = Pick<typeof import("./runtime.js"), "getActivePluginRegistry">;
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
const RUNTIME_MODULE_CANDIDATES = ["./runtime.js", "./runtime.ts"] as const;
|
||||
|
||||
let pluginRuntimeModule: PluginRuntimeModule | undefined;
|
||||
|
||||
function loadPluginRuntime(): PluginRuntimeModule | null {
|
||||
if (pluginRuntimeModule) {
|
||||
return pluginRuntimeModule;
|
||||
}
|
||||
for (const candidate of RUNTIME_MODULE_CANDIDATES) {
|
||||
try {
|
||||
pluginRuntimeModule = require(candidate) as PluginRuntimeModule;
|
||||
return pluginRuntimeModule;
|
||||
} catch {
|
||||
// Try built/runtime source candidates in order.
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function getActiveRuntimePluginRegistry(): PluginRegistry | null {
|
||||
return loadPluginRuntime()?.getActivePluginRegistry() ?? null;
|
||||
}
|
||||
@@ -1,34 +1,12 @@
|
||||
import { createRequire } from "node:module";
|
||||
import { getActiveRuntimePluginRegistry } from "./active-runtime-registry.js";
|
||||
import type { CliBackendPlugin } from "./cli-backend.types.js";
|
||||
|
||||
export type PluginCliBackendEntry = CliBackendPlugin & {
|
||||
pluginId: string;
|
||||
};
|
||||
|
||||
type PluginRuntimeModule = Pick<typeof import("./runtime.js"), "getActivePluginRegistry">;
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
const RUNTIME_MODULE_CANDIDATES = ["./runtime.js", "./runtime.ts"] as const;
|
||||
|
||||
let pluginRuntimeModule: PluginRuntimeModule | undefined;
|
||||
|
||||
function loadPluginRuntime(): PluginRuntimeModule | null {
|
||||
if (pluginRuntimeModule) {
|
||||
return pluginRuntimeModule;
|
||||
}
|
||||
for (const candidate of RUNTIME_MODULE_CANDIDATES) {
|
||||
try {
|
||||
pluginRuntimeModule = require(candidate) as PluginRuntimeModule;
|
||||
return pluginRuntimeModule;
|
||||
} catch {
|
||||
// Try source/runtime candidates in order.
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function resolveRuntimeCliBackends(): PluginCliBackendEntry[] {
|
||||
return (loadPluginRuntime()?.getActivePluginRegistry()?.cliBackends ?? []).map((entry) =>
|
||||
return (getActiveRuntimePluginRegistry()?.cliBackends ?? []).map((entry) =>
|
||||
Object.assign({}, entry.backend, { pluginId: entry.pluginId }),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,31 +1,9 @@
|
||||
import { createRequire } from "node:module";
|
||||
import { mergePluginTextTransforms } from "../agents/plugin-text-transforms.js";
|
||||
import { getActiveRuntimePluginRegistry } from "./active-runtime-registry.js";
|
||||
import type { PluginTextTransforms } from "./types.js";
|
||||
|
||||
type PluginRuntimeModule = Pick<typeof import("./runtime.js"), "getActivePluginRegistry">;
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
const RUNTIME_MODULE_CANDIDATES = ["./runtime.js", "./runtime.ts"] as const;
|
||||
|
||||
let pluginRuntimeModule: PluginRuntimeModule | undefined;
|
||||
|
||||
function loadPluginRuntime(): PluginRuntimeModule | null {
|
||||
if (pluginRuntimeModule) {
|
||||
return pluginRuntimeModule;
|
||||
}
|
||||
for (const candidate of RUNTIME_MODULE_CANDIDATES) {
|
||||
try {
|
||||
pluginRuntimeModule = require(candidate) as PluginRuntimeModule;
|
||||
return pluginRuntimeModule;
|
||||
} catch {
|
||||
// Try source/runtime candidates in order.
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function resolveRuntimeTextTransforms(): PluginTextTransforms | undefined {
|
||||
const registry = loadPluginRuntime()?.getActivePluginRegistry();
|
||||
const registry = getActiveRuntimePluginRegistry();
|
||||
const pluginTextTransforms = Array.isArray(registry?.textTransforms)
|
||||
? registry.textTransforms.map((entry) => entry.transforms)
|
||||
: [];
|
||||
|
||||
Reference in New Issue
Block a user