refactor: dedupe helpers and source seams

This commit is contained in:
Peter Steinberger
2026-03-24 20:39:45 +00:00
parent ebe18c0379
commit bcd61f0a38
83 changed files with 2795 additions and 4495 deletions

View File

@@ -4,13 +4,16 @@ import type { OpenClawConfig } from "../config/config.js";
import { applyMergePatch } from "../config/merge-patch.js";
import { openBoundaryFileSync } from "../infra/boundary-file-read.js";
import { isRecord } from "../utils.js";
import {
inspectBundleServerRuntimeSupport,
loadEnabledBundleConfig,
readBundleJsonObject,
} from "./bundle-config-shared.js";
import {
CLAUDE_BUNDLE_MANIFEST_RELATIVE_PATH,
mergeBundlePathLists,
normalizeBundlePathList,
} from "./bundle-manifest.js";
import { normalizePluginsConfig, resolveEffectiveEnableState } from "./config-state.js";
import { loadPluginManifestRegistry } from "./manifest-registry.js";
import type { PluginBundleFormat } from "./types.js";
export type BundleLspServerConfig = Record<string, unknown>;
@@ -30,33 +33,6 @@ const MANIFEST_PATH_BY_FORMAT: Partial<Record<PluginBundleFormat, string>> = {
claude: CLAUDE_BUNDLE_MANIFEST_RELATIVE_PATH,
};
function readPluginJsonObject(params: {
rootDir: string;
relativePath: string;
}): { ok: true; raw: Record<string, unknown> } | { ok: false; error: string } {
const absolutePath = path.join(params.rootDir, params.relativePath);
const opened = openBoundaryFileSync({
absolutePath,
rootPath: params.rootDir,
boundaryLabel: "plugin root",
rejectHardlinks: true,
});
if (!opened.ok) {
return { ok: true, raw: {} };
}
try {
const raw = JSON.parse(fs.readFileSync(opened.fd, "utf-8")) as unknown;
if (!isRecord(raw)) {
return { ok: false, error: `${params.relativePath} must contain a JSON object` };
}
return { ok: true, raw };
} catch (error) {
return { ok: false, error: `failed to parse ${params.relativePath}: ${String(error)}` };
} finally {
fs.closeSync(opened.fd);
}
}
function extractLspServerMap(raw: unknown): Record<string, BundleLspServerConfig> {
if (!isRecord(raw)) {
return {};
@@ -120,7 +96,7 @@ function loadBundleLspConfig(params: {
return { config: { lspServers: {} }, diagnostics: [] };
}
const manifestLoaded = readPluginJsonObject({
const manifestLoaded = readBundleJsonObject({
rootDir: params.rootDir,
relativePath: manifestRelativePath,
});
@@ -151,23 +127,15 @@ export function inspectBundleLspRuntimeSupport(params: {
rootDir: string;
bundleFormat: PluginBundleFormat;
}): BundleLspRuntimeSupport {
const loaded = loadBundleLspConfig(params);
const supportedServerNames: string[] = [];
const unsupportedServerNames: string[] = [];
let hasStdioServer = false;
for (const [serverName, server] of Object.entries(loaded.config.lspServers)) {
if (typeof server.command === "string" && server.command.trim().length > 0) {
hasStdioServer = true;
supportedServerNames.push(serverName);
continue;
}
unsupportedServerNames.push(serverName);
}
const support = inspectBundleServerRuntimeSupport({
loaded: loadBundleLspConfig(params),
resolveServers: (config) => config.lspServers,
});
return {
hasStdioServer,
supportedServerNames,
unsupportedServerNames,
diagnostics: loaded.diagnostics,
hasStdioServer: support.hasSupportedServer,
supportedServerNames: support.supportedServerNames,
unsupportedServerNames: support.unsupportedServerNames,
diagnostics: support.diagnostics,
};
}
@@ -175,38 +143,11 @@ export function loadEnabledBundleLspConfig(params: {
workspaceDir: string;
cfg?: OpenClawConfig;
}): { config: BundleLspConfig; diagnostics: Array<{ pluginId: string; message: string }> } {
const registry = loadPluginManifestRegistry({
return loadEnabledBundleConfig({
workspaceDir: params.workspaceDir,
config: params.cfg,
cfg: params.cfg,
createEmptyConfig: () => ({ lspServers: {} }),
loadBundleConfig: loadBundleLspConfig,
createDiagnostic: (pluginId, message) => ({ pluginId, message }),
});
const normalizedPlugins = normalizePluginsConfig(params.cfg?.plugins);
const diagnostics: Array<{ pluginId: string; message: string }> = [];
let merged: BundleLspConfig = { lspServers: {} };
for (const record of registry.plugins) {
if (record.format !== "bundle" || !record.bundleFormat) {
continue;
}
const enableState = resolveEffectiveEnableState({
id: record.id,
origin: record.origin,
config: normalizedPlugins,
rootConfig: params.cfg,
});
if (!enableState.enabled) {
continue;
}
const loaded = loadBundleLspConfig({
pluginId: record.id,
rootDir: record.rootDir,
bundleFormat: record.bundleFormat,
});
merged = applyMergePatch(merged, loaded.config) as BundleLspConfig;
for (const message of loaded.diagnostics) {
diagnostics.push({ pluginId: record.id, message });
}
}
return { config: merged, diagnostics };
}