Skip to content

Commit 8246e8d

Browse files
committed
refactor(plugins): share channel command normalization
1 parent 5df513c commit 8246e8d

3 files changed

Lines changed: 6 additions & 49 deletions

File tree

src/plugins/manifest-registry-installed.ts

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type { BundledChannelConfigCollector } from "./manifest-registry.js";
1616
import {
1717
DEFAULT_PLUGIN_ENTRY_CANDIDATES,
1818
getPackageManifestMetadata,
19+
normalizeManifestChannelCommandDefaults,
1920
type OpenClawPackageManifest,
2021
type PackageManifest,
2122
type PluginPackageChannel,
@@ -274,28 +275,6 @@ function resolveFallbackPluginSource(record: InstalledPluginIndexRecord): string
274275
return path.join(rootDir, DEFAULT_PLUGIN_ENTRY_CANDIDATES[0]);
275276
}
276277

277-
function normalizePackageChannelCommands(
278-
commands: unknown,
279-
): PluginPackageChannel["commands"] | undefined {
280-
if (!isRecord(commands)) {
281-
return undefined;
282-
}
283-
const nativeCommandsAutoEnabled =
284-
typeof commands.nativeCommandsAutoEnabled === "boolean"
285-
? commands.nativeCommandsAutoEnabled
286-
: undefined;
287-
const nativeSkillsAutoEnabled =
288-
typeof commands.nativeSkillsAutoEnabled === "boolean"
289-
? commands.nativeSkillsAutoEnabled
290-
: undefined;
291-
return nativeCommandsAutoEnabled !== undefined || nativeSkillsAutoEnabled !== undefined
292-
? {
293-
...(nativeCommandsAutoEnabled !== undefined ? { nativeCommandsAutoEnabled } : {}),
294-
...(nativeSkillsAutoEnabled !== undefined ? { nativeSkillsAutoEnabled } : {}),
295-
}
296-
: undefined;
297-
}
298-
299278
function normalizePackageChannelExposure(
300279
exposure: unknown,
301280
): PluginPackageChannel["exposure"] | undefined {
@@ -480,7 +459,7 @@ function normalizePersistedPackageChannel(value: unknown): PluginPackageChannel
480459
if (exposure) {
481460
channel.exposure = exposure;
482461
}
483-
const commands = normalizePackageChannelCommands(value.commands);
462+
const commands = normalizeManifestChannelCommandDefaults(value.commands);
484463
if (commands) {
485464
channel.commands = commands;
486465
}

src/plugins/manifest-registry.ts

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import {
5353
type PluginManifestToolMetadata,
5454
type PluginPackageChannel,
5555
type PluginPackageInstall,
56+
normalizeManifestChannelCommandDefaults,
5657
} from "./manifest.js";
5758
import { checkMinHostVersion } from "./min-host-version.js";
5859
import {
@@ -295,29 +296,6 @@ function normalizePreferredPluginIds(raw: unknown): string[] | undefined {
295296
return normalizeOptionalTrimmedStringList(raw);
296297
}
297298

298-
function normalizePackageChannelCommands(
299-
commands: unknown,
300-
): PluginManifestChannelCommandDefaults | undefined {
301-
if (!commands || typeof commands !== "object" || Array.isArray(commands)) {
302-
return undefined;
303-
}
304-
const record = commands as Record<string, unknown>;
305-
const nativeCommandsAutoEnabled =
306-
typeof record.nativeCommandsAutoEnabled === "boolean"
307-
? record.nativeCommandsAutoEnabled
308-
: undefined;
309-
const nativeSkillsAutoEnabled =
310-
typeof record.nativeSkillsAutoEnabled === "boolean"
311-
? record.nativeSkillsAutoEnabled
312-
: undefined;
313-
return nativeCommandsAutoEnabled !== undefined || nativeSkillsAutoEnabled !== undefined
314-
? {
315-
...(nativeCommandsAutoEnabled !== undefined ? { nativeCommandsAutoEnabled } : {}),
316-
...(nativeSkillsAutoEnabled !== undefined ? { nativeSkillsAutoEnabled } : {}),
317-
}
318-
: undefined;
319-
}
320-
321299
function mergePackageChannelMetaIntoChannelConfigs(params: {
322300
channelConfigs?: Record<string, PluginManifestChannelConfig>;
323301
packageChannel?: OpenClawPackageManifest["channel"];
@@ -342,7 +320,7 @@ function mergePackageChannelMetaIntoChannelConfigs(params: {
342320
const preferOver =
343321
existing.preferOver ?? normalizePreferredPluginIds(params.packageChannel?.preferOver);
344322
const commands =
345-
existing.commands ?? normalizePackageChannelCommands(params.packageChannel?.commands);
323+
existing.commands ?? normalizeManifestChannelCommandDefaults(params.packageChannel?.commands);
346324

347325
const merged: Record<string, PluginManifestChannelConfig> = Object.create(null);
348326
for (const [key, value] of Object.entries(params.channelConfigs)) {
@@ -501,7 +479,7 @@ function buildRecord(params: {
501479
}),
502480
packageChannel: params.candidate.packageManifest?.channel,
503481
});
504-
const packageChannelCommands = normalizePackageChannelCommands(
482+
const packageChannelCommands = normalizeManifestChannelCommandDefaults(
505483
params.candidate.packageManifest?.channel?.commands,
506484
);
507485
return {

src/plugins/manifest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1615,7 +1615,7 @@ function normalizeChannelConfigs(
16151615
return Object.keys(normalized).length > 0 ? normalized : undefined;
16161616
}
16171617

1618-
function normalizeManifestChannelCommandDefaults(
1618+
export function normalizeManifestChannelCommandDefaults(
16191619
value: unknown,
16201620
): PluginManifestChannelCommandDefaults | undefined {
16211621
if (!isRecord(value)) {

0 commit comments

Comments
 (0)