Skip to content

Commit 5f14d46

Browse files
authored
refactor(plugin-sdk): unify lazy channel config schemas (#120938)
* refactor(plugin-sdk): unify lazy channel config schemas * fix(plugin-sdk): preserve channel schema contract types
1 parent 4c2d8d8 commit 5f14d46

4 files changed

Lines changed: 55 additions & 19 deletions

File tree

‎src/plugin-sdk/channel-entry-contract.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,26 @@ function createBundledChannelEntry(params: {
120120
}
121121

122122
describe("defineBundledChannelEntry", () => {
123+
it("defers and memoizes config schema factories", () => {
124+
const configSchema = {
125+
schema: { type: "object" as const, additionalProperties: false },
126+
};
127+
const createConfigSchema = vi.fn(() => configSchema);
128+
const entry = defineBundledChannelEntry({
129+
id: "lazy-config-schema",
130+
name: "Lazy Config Schema",
131+
description: "lazy config schema test",
132+
importMetaUrl: import.meta.url,
133+
plugin: { specifier: "./unused.js" },
134+
configSchema: createConfigSchema,
135+
});
136+
137+
expect(createConfigSchema).not.toHaveBeenCalled();
138+
expect(entry.configSchema).toBe(configSchema);
139+
expect(entry.configSchema).toBe(configSchema);
140+
expect(createConfigSchema).toHaveBeenCalledTimes(1);
141+
});
142+
123143
it("runs tool registrations without channel sidecar hydration during tool discovery", () => {
124144
const tempRoot = tempDirs.make("openclaw-bundled-entry-tools-");
125145
const runtimeMarker = path.join(tempRoot, "runtime-loaded");

‎src/plugin-sdk/channel-entry-contract.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import type {
2424
BundledChannelLegacyStateMigrationDetector,
2525
BundledEntryModuleLoadOptions,
2626
} from "./channel-entry-contract.types.js";
27+
import { createCachedLazyValueGetter } from "./lazy-value.js";
2728

2829
export type AnyAgentTool = import("../plugins/types.js").AnyAgentTool;
2930
export type OpenClawPluginApi = import("../plugins/types.js").OpenClawPluginApi;
@@ -102,7 +103,7 @@ export type BundledChannelEntryContract<TPlugin = ChannelPlugin> = {
102103
id: string;
103104
name: string;
104105
description: string;
105-
configSchema: ChannelEntryConfigSchema<TPlugin>;
106+
configSchema: ChannelConfigSchema;
106107
features?: BundledChannelEntryFeatures;
107108
register: (api: OpenClawPluginApi) => void;
108109
loadChannelPlugin: (options?: BundledEntryModuleLoadOptions) => TPlugin;
@@ -497,10 +498,7 @@ export function defineBundledChannelEntry<TPlugin = ChannelPlugin>({
497498
registerFull,
498499
registerCapabilities,
499500
}: DefineBundledChannelEntryOptions<TPlugin>): BundledChannelEntryContract<TPlugin> {
500-
const resolvedConfigSchema: ChannelEntryConfigSchema<TPlugin> =
501-
typeof configSchema === "function"
502-
? configSchema()
503-
: ((configSchema ?? emptyChannelConfigSchema()) as ChannelEntryConfigSchema<TPlugin>);
501+
const getConfigSchema = createCachedLazyValueGetter(configSchema ?? emptyChannelConfigSchema);
504502
const loadChannelPlugin = (options?: BundledEntryModuleLoadOptions) =>
505503
loadBundledEntryExportSync<TPlugin>(importMetaUrl, plugin, options);
506504
const loadChannelOutbound = outbound
@@ -542,7 +540,9 @@ export function defineBundledChannelEntry<TPlugin = ChannelPlugin>({
542540
id,
543541
name,
544542
description,
545-
configSchema: resolvedConfigSchema,
543+
get configSchema() {
544+
return getConfigSchema();
545+
},
546546
...(features || accountInspect
547547
? { features: { ...features, ...(accountInspect ? { accountInspect: true } : {}) } }
548548
: {}),

‎src/plugin-sdk/core.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,25 @@ function createApi(registrationMode: PluginRegistrationMode): OpenClawPluginApi
4040
}
4141

4242
describe("defineChannelPluginEntry", () => {
43+
it("defers and memoizes config schema factories", () => {
44+
const configSchema = {
45+
schema: { type: "object" as const, additionalProperties: false },
46+
};
47+
const createConfigSchema = vi.fn(() => configSchema);
48+
const entry = defineChannelPluginEntry({
49+
id: "lazy-config-schema",
50+
name: "Lazy Config Schema",
51+
description: "lazy config schema test",
52+
plugin: createChannelPlugin("lazy-config-schema"),
53+
configSchema: createConfigSchema,
54+
});
55+
56+
expect(createConfigSchema).not.toHaveBeenCalled();
57+
expect(entry.configSchema).toBe(configSchema);
58+
expect(entry.configSchema).toBe(configSchema);
59+
expect(createConfigSchema).toHaveBeenCalledTimes(1);
60+
});
61+
4362
it("runs tool registrations without channel runtime wiring during tool discovery", () => {
4463
const setRuntime = vi.fn<(runtime: PluginRuntime) => void>();
4564
const registerCliMetadata = vi.fn<(api: OpenClawPluginApi) => void>();

‎src/plugin-sdk/core.ts‎

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
normalizeSessionKeyPreservingOpaquePeerIds,
3737
parseThreadSessionSuffix,
3838
} from "../sessions/session-key-utils.js";
39+
import { createCachedLazyValueGetter } from "./lazy-value.js";
3940
export type {
4041
AgentPromptGuidance,
4142
AgentPromptGuidanceEntry,
@@ -509,7 +510,7 @@ type DefinedChannelPluginEntry<TPlugin> = {
509510
id: string;
510511
name: string;
511512
description: string;
512-
configSchema: ChannelEntryConfigSchema<TPlugin>;
513+
configSchema: ChannelConfigSchema;
513514
register: (api: OpenClawPluginApi) => void;
514515
channelPlugin: TPlugin;
515516
setChannelRuntime?: (runtime: PluginRuntime) => void;
@@ -563,9 +564,9 @@ type CreatedChannelPluginBase<TResolvedAccount> = Pick<
563564
/**
564565
* Canonical entry helper for channel plugins.
565566
*
566-
* This wraps `definePluginEntry(...)`, registers the channel capability, and
567-
* optionally exposes extra full-runtime registration such as tools or gateway
568-
* handlers that only make sense outside setup-only registration modes.
567+
* This shares `definePluginEntry(...)`'s lazy schema primitive, registers the
568+
* channel capability, and optionally exposes extra full-runtime registration
569+
* such as tools or gateway handlers outside setup-only registration modes.
569570
*/
570571
export function defineChannelPluginEntry<TPlugin>({
571572
id,
@@ -578,15 +579,14 @@ export function defineChannelPluginEntry<TPlugin>({
578579
registerFull,
579580
registerCapabilities,
580581
}: DefineChannelPluginEntryOptions<TPlugin>): DefinedChannelPluginEntry<TPlugin> {
581-
const resolvedConfigSchema: ChannelEntryConfigSchema<TPlugin> =
582-
typeof configSchema === "function"
583-
? configSchema()
584-
: ((configSchema ?? emptyChannelConfigSchema()) as ChannelEntryConfigSchema<TPlugin>);
585-
const entry = {
582+
const getConfigSchema = createCachedLazyValueGetter(configSchema ?? emptyChannelConfigSchema);
583+
return {
586584
id,
587585
name,
588586
description,
589-
configSchema: resolvedConfigSchema,
587+
get configSchema() {
588+
return getConfigSchema();
589+
},
590590
register(api: OpenClawPluginApi) {
591591
if (api.registrationMode === "cli-metadata") {
592592
registerCliMetadata?.(api);
@@ -611,9 +611,6 @@ export function defineChannelPluginEntry<TPlugin>({
611611
registerFull?.(api);
612612
registerCapabilities?.(api);
613613
},
614-
};
615-
return {
616-
...entry,
617614
channelPlugin: plugin,
618615
...(setRuntime ? { setChannelRuntime: setRuntime } : {}),
619616
};

0 commit comments

Comments
 (0)