Skip to content

Commit 71e361a

Browse files
committed
refactor(plugins): split installed plugin index modules
1 parent 487f8c5 commit 71e361a

8 files changed

Lines changed: 763 additions & 687 deletions
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import crypto from "node:crypto";
2+
import fs from "node:fs";
3+
import type { PluginDiagnostic } from "./manifest-types.js";
4+
5+
export function hashString(value: string): string {
6+
return crypto.createHash("sha256").update(value).digest("hex");
7+
}
8+
9+
export function hashJson(value: unknown): string {
10+
return hashString(JSON.stringify(value));
11+
}
12+
13+
export function safeHashFile(params: {
14+
filePath: string;
15+
pluginId?: string;
16+
diagnostics: PluginDiagnostic[];
17+
required: boolean;
18+
}): string | undefined {
19+
try {
20+
return crypto.createHash("sha256").update(fs.readFileSync(params.filePath)).digest("hex");
21+
} catch (err) {
22+
if (params.required) {
23+
params.diagnostics.push({
24+
level: "warn",
25+
...(params.pluginId ? { pluginId: params.pluginId } : {}),
26+
source: params.filePath,
27+
message: `installed plugin index could not hash ${params.filePath}: ${
28+
err instanceof Error ? err.message : String(err)
29+
}`,
30+
});
31+
}
32+
return undefined;
33+
}
34+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import type { PluginInstallRecord } from "../config/types.plugins.js";
2+
import type {
3+
InstalledPluginIndex,
4+
InstalledPluginInstallRecordInfo,
5+
} from "./installed-plugin-index-types.js";
6+
7+
function setInstallStringField<Key extends keyof Omit<InstalledPluginInstallRecordInfo, "source">>(
8+
target: InstalledPluginInstallRecordInfo,
9+
key: Key,
10+
value: PluginInstallRecord[Key],
11+
): void {
12+
if (typeof value !== "string") {
13+
return;
14+
}
15+
const normalized = value.trim();
16+
if (normalized) {
17+
target[key] = normalized as InstalledPluginInstallRecordInfo[Key];
18+
}
19+
}
20+
21+
function normalizeInstallRecord(
22+
record: PluginInstallRecord | undefined,
23+
): InstalledPluginInstallRecordInfo | undefined {
24+
if (!record) {
25+
return undefined;
26+
}
27+
const normalized: InstalledPluginInstallRecordInfo = {
28+
source: record.source,
29+
};
30+
setInstallStringField(normalized, "spec", record.spec);
31+
setInstallStringField(normalized, "sourcePath", record.sourcePath);
32+
setInstallStringField(normalized, "installPath", record.installPath);
33+
setInstallStringField(normalized, "version", record.version);
34+
setInstallStringField(normalized, "resolvedName", record.resolvedName);
35+
setInstallStringField(normalized, "resolvedVersion", record.resolvedVersion);
36+
setInstallStringField(normalized, "resolvedSpec", record.resolvedSpec);
37+
setInstallStringField(normalized, "integrity", record.integrity);
38+
setInstallStringField(normalized, "shasum", record.shasum);
39+
setInstallStringField(normalized, "resolvedAt", record.resolvedAt);
40+
setInstallStringField(normalized, "installedAt", record.installedAt);
41+
setInstallStringField(normalized, "clawhubUrl", record.clawhubUrl);
42+
setInstallStringField(normalized, "clawhubPackage", record.clawhubPackage);
43+
setInstallStringField(normalized, "clawhubFamily", record.clawhubFamily);
44+
setInstallStringField(normalized, "clawhubChannel", record.clawhubChannel);
45+
setInstallStringField(normalized, "marketplaceName", record.marketplaceName);
46+
setInstallStringField(normalized, "marketplaceSource", record.marketplaceSource);
47+
setInstallStringField(normalized, "marketplacePlugin", record.marketplacePlugin);
48+
return normalized;
49+
}
50+
51+
function restoreInstallRecord(
52+
record: InstalledPluginInstallRecordInfo | undefined,
53+
): PluginInstallRecord | undefined {
54+
if (!record?.source) {
55+
return undefined;
56+
}
57+
return structuredClone(record) as PluginInstallRecord;
58+
}
59+
60+
export function normalizeInstallRecordMap(
61+
records: Record<string, PluginInstallRecord> | undefined,
62+
): Record<string, InstalledPluginInstallRecordInfo> {
63+
const normalized: Record<string, InstalledPluginInstallRecordInfo> = {};
64+
for (const [pluginId, record] of Object.entries(records ?? {}).toSorted(([left], [right]) =>
65+
left.localeCompare(right),
66+
)) {
67+
const installRecord = normalizeInstallRecord(record);
68+
if (installRecord) {
69+
normalized[pluginId] = installRecord;
70+
}
71+
}
72+
return normalized;
73+
}
74+
75+
function restoreInstallRecordMap(
76+
records: Readonly<Record<string, InstalledPluginInstallRecordInfo>> | undefined,
77+
): Record<string, PluginInstallRecord> {
78+
const restored: Record<string, PluginInstallRecord> = {};
79+
for (const [pluginId, record] of Object.entries(records ?? {}).toSorted(([left], [right]) =>
80+
left.localeCompare(right),
81+
)) {
82+
const installRecord = restoreInstallRecord(record);
83+
if (installRecord) {
84+
restored[pluginId] = installRecord;
85+
}
86+
}
87+
return restored;
88+
}
89+
90+
export function extractPluginInstallRecordsFromInstalledPluginIndex(
91+
index: InstalledPluginIndex | null | undefined,
92+
): Record<string, PluginInstallRecord> {
93+
if (index && Object.prototype.hasOwnProperty.call(index, "installRecords")) {
94+
return restoreInstallRecordMap(index.installRecords);
95+
}
96+
const records: Record<string, PluginInstallRecord> = {};
97+
for (const plugin of index?.plugins ?? []) {
98+
const record = restoreInstallRecord(plugin.installRecord);
99+
if (record) {
100+
records[plugin.pluginId] = record;
101+
}
102+
}
103+
return records;
104+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { hashJson } from "./installed-plugin-index-hash.js";
2+
import type {
3+
InstalledPluginIndex,
4+
InstalledPluginIndexRefreshReason,
5+
} from "./installed-plugin-index-types.js";
6+
7+
export function diffInstalledPluginIndexInvalidationReasons(
8+
previous: InstalledPluginIndex,
9+
current: InstalledPluginIndex,
10+
): readonly InstalledPluginIndexRefreshReason[] {
11+
const reasons = new Set<InstalledPluginIndexRefreshReason>();
12+
if (previous.version !== current.version) {
13+
reasons.add("missing");
14+
}
15+
if (previous.hostContractVersion !== current.hostContractVersion) {
16+
reasons.add("host-contract-changed");
17+
}
18+
if (previous.compatRegistryVersion !== current.compatRegistryVersion) {
19+
reasons.add("compat-registry-changed");
20+
}
21+
if (previous.migrationVersion !== current.migrationVersion) {
22+
reasons.add("migration");
23+
}
24+
if (previous.policyHash !== current.policyHash) {
25+
reasons.add("policy-changed");
26+
}
27+
if (hashJson(previous.installRecords ?? {}) !== hashJson(current.installRecords ?? {})) {
28+
reasons.add("source-changed");
29+
}
30+
31+
const previousByPluginId = new Map(previous.plugins.map((plugin) => [plugin.pluginId, plugin]));
32+
const currentByPluginId = new Map(current.plugins.map((plugin) => [plugin.pluginId, plugin]));
33+
for (const [pluginId, previousPlugin] of previousByPluginId) {
34+
const currentPlugin = currentByPluginId.get(pluginId);
35+
if (!currentPlugin) {
36+
reasons.add("source-changed");
37+
continue;
38+
}
39+
if (
40+
previousPlugin.rootDir !== currentPlugin.rootDir ||
41+
previousPlugin.manifestPath !== currentPlugin.manifestPath ||
42+
previousPlugin.installRecordHash !== currentPlugin.installRecordHash
43+
) {
44+
reasons.add("source-changed");
45+
}
46+
if (previousPlugin.enabled !== currentPlugin.enabled) {
47+
reasons.add("policy-changed");
48+
}
49+
if (previousPlugin.manifestHash !== currentPlugin.manifestHash) {
50+
reasons.add("stale-manifest");
51+
}
52+
if (
53+
previousPlugin.packageVersion !== currentPlugin.packageVersion ||
54+
previousPlugin.packageJson?.path !== currentPlugin.packageJson?.path ||
55+
previousPlugin.packageJson?.hash !== currentPlugin.packageJson?.hash
56+
) {
57+
reasons.add("stale-package");
58+
}
59+
}
60+
for (const pluginId of currentByPluginId.keys()) {
61+
if (!previousByPluginId.has(pluginId)) {
62+
const currentPlugin = currentByPluginId.get(pluginId);
63+
if (currentPlugin?.enabled === false) {
64+
continue;
65+
}
66+
reasons.add("source-changed");
67+
}
68+
}
69+
70+
return Array.from(reasons).toSorted((left, right) => left.localeCompare(right));
71+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import type { OpenClawConfig } from "../config/types.js";
2+
import { listPluginCompatRecords } from "./compat/registry.js";
3+
import { normalizePluginsConfig } from "./config-state.js";
4+
import { hashJson } from "./installed-plugin-index-hash.js";
5+
6+
export function resolveCompatRegistryVersion(): string {
7+
return hashJson(
8+
listPluginCompatRecords().map((record) => ({
9+
code: record.code,
10+
status: record.status,
11+
deprecated: record.deprecated,
12+
warningStarts: record.warningStarts,
13+
removeAfter: record.removeAfter,
14+
replacement: record.replacement,
15+
})),
16+
);
17+
}
18+
19+
export function resolveInstalledPluginIndexPolicyHash(config: OpenClawConfig | undefined): string {
20+
const normalized = normalizePluginsConfig(config?.plugins);
21+
const channelPolicy: Record<string, boolean> = {};
22+
const channels = config?.channels;
23+
if (channels && typeof channels === "object" && !Array.isArray(channels)) {
24+
for (const [channelId, value] of Object.entries(channels)) {
25+
if (value && typeof value === "object" && !Array.isArray(value)) {
26+
const enabled = (value as Record<string, unknown>).enabled;
27+
if (typeof enabled === "boolean") {
28+
channelPolicy[channelId] = enabled;
29+
}
30+
}
31+
}
32+
}
33+
return hashJson({
34+
plugins: {
35+
enabled: normalized.enabled,
36+
allow: normalized.allow,
37+
deny: normalized.deny,
38+
slots: normalized.slots,
39+
entries: Object.fromEntries(
40+
Object.entries(normalized.entries)
41+
.flatMap(([pluginId, entry]) =>
42+
typeof entry.enabled === "boolean" ? [[pluginId, entry.enabled] as const] : [],
43+
)
44+
.toSorted(([left], [right]) => left.localeCompare(right)),
45+
),
46+
},
47+
channels: Object.fromEntries(
48+
Object.entries(channelPolicy).toSorted(([left], [right]) => left.localeCompare(right)),
49+
),
50+
});
51+
}

0 commit comments

Comments
 (0)