mirror of
https://github.com/moltbot/moltbot.git
synced 2026-04-20 21:23:23 +00:00
refactor: trim bundled capability metadata imports
This commit is contained in:
committed by
Peter Steinberger
parent
ac7e1f7c6c
commit
9cf7b92e0d
76
src/plugins/bundled-capability-metadata.test.ts
Normal file
76
src/plugins/bundled-capability-metadata.test.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
BUNDLED_AUTO_ENABLE_PROVIDER_PLUGIN_IDS,
|
||||
BUNDLED_LEGACY_PLUGIN_ID_ALIASES,
|
||||
BUNDLED_PLUGIN_CONTRACT_SNAPSHOTS,
|
||||
} from "./bundled-capability-metadata.js";
|
||||
import { listBundledPluginMetadata } from "./bundled-plugin-metadata.js";
|
||||
|
||||
function uniqueStrings(values: readonly string[] | undefined): string[] {
|
||||
const result: string[] = [];
|
||||
const seen = new Set<string>();
|
||||
for (const value of values ?? []) {
|
||||
const normalized = value.trim();
|
||||
if (!normalized || seen.has(normalized)) {
|
||||
continue;
|
||||
}
|
||||
seen.add(normalized);
|
||||
result.push(normalized);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
describe("bundled capability metadata", () => {
|
||||
it("keeps contract snapshots aligned with bundled plugin manifests", () => {
|
||||
const expected = listBundledPluginMetadata()
|
||||
.map(({ manifest }) => ({
|
||||
pluginId: manifest.id,
|
||||
cliBackendIds: uniqueStrings(manifest.cliBackends),
|
||||
providerIds: uniqueStrings(manifest.providers),
|
||||
speechProviderIds: uniqueStrings(manifest.contracts?.speechProviders),
|
||||
mediaUnderstandingProviderIds: uniqueStrings(
|
||||
manifest.contracts?.mediaUnderstandingProviders,
|
||||
),
|
||||
imageGenerationProviderIds: uniqueStrings(manifest.contracts?.imageGenerationProviders),
|
||||
webSearchProviderIds: uniqueStrings(manifest.contracts?.webSearchProviders),
|
||||
toolNames: uniqueStrings(manifest.contracts?.tools),
|
||||
}))
|
||||
.filter(
|
||||
(entry) =>
|
||||
entry.cliBackendIds.length > 0 ||
|
||||
entry.providerIds.length > 0 ||
|
||||
entry.speechProviderIds.length > 0 ||
|
||||
entry.mediaUnderstandingProviderIds.length > 0 ||
|
||||
entry.imageGenerationProviderIds.length > 0 ||
|
||||
entry.webSearchProviderIds.length > 0 ||
|
||||
entry.toolNames.length > 0,
|
||||
)
|
||||
.toSorted((left, right) => left.pluginId.localeCompare(right.pluginId));
|
||||
|
||||
expect(BUNDLED_PLUGIN_CONTRACT_SNAPSHOTS).toEqual(expected);
|
||||
});
|
||||
|
||||
it("keeps lightweight alias maps aligned with bundled plugin manifests", () => {
|
||||
const manifests = listBundledPluginMetadata().map((entry) => entry.manifest);
|
||||
const expectedLegacyAliases = Object.fromEntries(
|
||||
manifests
|
||||
.flatMap((manifest) =>
|
||||
(manifest.legacyPluginIds ?? []).map((legacyPluginId) => [legacyPluginId, manifest.id]),
|
||||
)
|
||||
.toSorted(([left], [right]) => left.localeCompare(right)),
|
||||
);
|
||||
const expectedAutoEnableProviderPluginIds = Object.fromEntries(
|
||||
manifests
|
||||
.flatMap((manifest) =>
|
||||
(manifest.autoEnableWhenConfiguredProviders ?? []).map((providerId) => [
|
||||
providerId,
|
||||
manifest.id,
|
||||
]),
|
||||
)
|
||||
.toSorted(([left], [right]) => left.localeCompare(right)),
|
||||
);
|
||||
|
||||
expect(BUNDLED_LEGACY_PLUGIN_ID_ALIASES).toEqual(expectedLegacyAliases);
|
||||
expect(BUNDLED_AUTO_ENABLE_PROVIDER_PLUGIN_IDS).toEqual(expectedAutoEnableProviderPluginIds);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,4 @@
|
||||
import { listBundledPluginMetadata } from "./bundled-plugin-metadata.js";
|
||||
import { listBundledPluginManifestSnapshots } from "./bundled-manifest-snapshots.js";
|
||||
|
||||
export type BundledPluginContractSnapshot = {
|
||||
pluginId: string;
|
||||
@@ -27,7 +27,7 @@ function uniqueStrings(values: readonly string[] | undefined): string[] {
|
||||
}
|
||||
|
||||
export const BUNDLED_PLUGIN_CONTRACT_SNAPSHOTS: readonly BundledPluginContractSnapshot[] =
|
||||
listBundledPluginMetadata()
|
||||
listBundledPluginManifestSnapshots()
|
||||
.map(({ manifest }) => ({
|
||||
pluginId: manifest.id,
|
||||
cliBackendIds: uniqueStrings(manifest.cliBackends),
|
||||
@@ -107,7 +107,7 @@ export const BUNDLED_PROVIDER_PLUGIN_ID_ALIASES = Object.fromEntries(
|
||||
) as Readonly<Record<string, string>>;
|
||||
|
||||
export const BUNDLED_LEGACY_PLUGIN_ID_ALIASES = Object.fromEntries(
|
||||
listBundledPluginMetadata()
|
||||
listBundledPluginManifestSnapshots()
|
||||
.flatMap(({ manifest }) =>
|
||||
(manifest.legacyPluginIds ?? []).map(
|
||||
(legacyPluginId) => [legacyPluginId, manifest.id] as const,
|
||||
@@ -117,7 +117,7 @@ export const BUNDLED_LEGACY_PLUGIN_ID_ALIASES = Object.fromEntries(
|
||||
) as Readonly<Record<string, string>>;
|
||||
|
||||
export const BUNDLED_AUTO_ENABLE_PROVIDER_PLUGIN_IDS = Object.fromEntries(
|
||||
listBundledPluginMetadata()
|
||||
listBundledPluginManifestSnapshots()
|
||||
.flatMap(({ manifest }) =>
|
||||
(manifest.autoEnableWhenConfiguredProviders ?? []).map((providerId) => [
|
||||
providerId,
|
||||
|
||||
75
src/plugins/bundled-manifest-snapshots.ts
Normal file
75
src/plugins/bundled-manifest-snapshots.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { resolveBundledPluginsDir } from "./bundled-dir.js";
|
||||
import {
|
||||
loadPluginManifest,
|
||||
resolvePackageExtensionEntries,
|
||||
type PackageManifest,
|
||||
type PluginManifest,
|
||||
} from "./manifest.js";
|
||||
|
||||
export type BundledPluginManifestSnapshot = {
|
||||
dirName: string;
|
||||
manifest: PluginManifest;
|
||||
};
|
||||
|
||||
const bundledPluginManifestSnapshotCache = new Map<
|
||||
string,
|
||||
readonly BundledPluginManifestSnapshot[]
|
||||
>();
|
||||
|
||||
export function clearBundledPluginManifestSnapshotCache(): void {
|
||||
bundledPluginManifestSnapshotCache.clear();
|
||||
}
|
||||
|
||||
function readPackageManifest(pluginDir: string): PackageManifest | undefined {
|
||||
const packagePath = path.join(pluginDir, "package.json");
|
||||
if (!fs.existsSync(packagePath)) {
|
||||
return undefined;
|
||||
}
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(packagePath, "utf-8")) as PackageManifest;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
export function listBundledPluginManifestSnapshots(params?: {
|
||||
bundledDir?: string;
|
||||
env?: NodeJS.ProcessEnv;
|
||||
}): readonly BundledPluginManifestSnapshot[] {
|
||||
const bundledDir = params?.bundledDir ?? resolveBundledPluginsDir(params?.env ?? process.env);
|
||||
if (!bundledDir || !fs.existsSync(bundledDir)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const cacheKey = path.resolve(bundledDir);
|
||||
const cached = bundledPluginManifestSnapshotCache.get(cacheKey);
|
||||
if (cached) {
|
||||
return cached;
|
||||
}
|
||||
|
||||
const entries: BundledPluginManifestSnapshot[] = [];
|
||||
for (const dirName of fs
|
||||
.readdirSync(bundledDir, { withFileTypes: true })
|
||||
.filter((entry) => entry.isDirectory())
|
||||
.map((entry) => entry.name)
|
||||
.toSorted((left, right) => left.localeCompare(right))) {
|
||||
const pluginDir = path.join(bundledDir, dirName);
|
||||
if (resolvePackageExtensionEntries(readPackageManifest(pluginDir)).status !== "ok") {
|
||||
continue;
|
||||
}
|
||||
const manifestResult = loadPluginManifest(pluginDir, false);
|
||||
if (!manifestResult.ok) {
|
||||
continue;
|
||||
}
|
||||
entries.push({
|
||||
dirName,
|
||||
manifest: manifestResult.manifest,
|
||||
});
|
||||
}
|
||||
|
||||
const snapshots = Object.freeze(entries);
|
||||
bundledPluginManifestSnapshotCache.set(cacheKey, snapshots);
|
||||
return snapshots;
|
||||
}
|
||||
Reference in New Issue
Block a user