refactor: share active plugin runtime lookup

This commit is contained in:
Peter Steinberger
2026-04-20 14:01:54 +01:00
parent 99123dc5fd
commit 848348f423
3 changed files with 32 additions and 48 deletions

View 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;
}

View File

@@ -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 }),
);
}

View File

@@ -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)
: [];