Skip to content

Commit a9c7c2e

Browse files
authored
feat(plugins): narrow CLI loading via activation planning (#65120)
* feat(plugins): narrow cli loading via activation planning * fix(plugins): normalize primary CLI command nullability * fix(plugins): enforce activation planner exhaustiveness
1 parent 24051dd commit a9c7c2e

8 files changed

Lines changed: 379 additions & 7 deletions

File tree

‎docs/plugins/architecture.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,9 @@ actual behavior such as hooks, tools, commands, or provider flows.
527527
Optional manifest `activation` and `setup` blocks stay on the control plane.
528528
They are metadata-only descriptors for activation planning and setup discovery;
529529
they do not replace runtime registration, `register(...)`, or `setupEntry`.
530+
The first activation consumer now uses manifest command hints to narrow CLI
531+
plugin loading when a primary command is known, instead of always loading every
532+
CLI-capable plugin up front.
530533

531534
Setup discovery now prefers descriptor-owned ids such as `setup.providers` and
532535
`setup.cliBackends` to narrow candidate plugins before it falls back to

‎docs/plugins/manifest.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ should activate it later.
221221

222222
This block is metadata only. It does not register runtime behavior, and it does
223223
not replace `register(...)`, `setupEntry`, or other runtime/plugin entrypoints.
224+
Current consumers use it as a narrowing hint before broader plugin loading, so
225+
missing activation metadata only costs performance; it should not change
226+
correctness.
224227

225228
```json
226229
{
@@ -242,6 +245,10 @@ not replace `register(...)`, `setupEntry`, or other runtime/plugin entrypoints.
242245
| `onRoutes` | No | `string[]` | Route kinds that should activate this plugin. |
243246
| `onCapabilities` | No | `Array<"provider" \| "channel" \| "tool" \| "hook">` | Broad capability hints used by control-plane activation planning. |
244247

248+
For command-triggered planning specifically, OpenClaw still falls back to
249+
legacy `commandAliases[].cliCommand` or `commandAliases[].name` when a plugin
250+
has not added explicit `activation.onCommands` metadata yet.
251+
245252
## setup reference
246253

247254
Use `setup` when setup and onboarding surfaces need cheap plugin-owned metadata
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
2+
3+
const mocks = vi.hoisted(() => ({
4+
loadPluginManifestRegistry: vi.fn(),
5+
}));
6+
7+
vi.mock("./manifest-registry.js", () => ({
8+
loadPluginManifestRegistry: (...args: unknown[]) => mocks.loadPluginManifestRegistry(...args),
9+
}));
10+
11+
let resolveManifestActivationPluginIds: typeof import("./activation-planner.js").resolveManifestActivationPluginIds;
12+
13+
describe("resolveManifestActivationPluginIds", () => {
14+
beforeAll(async () => {
15+
({ resolveManifestActivationPluginIds } = await import("./activation-planner.js"));
16+
});
17+
18+
beforeEach(() => {
19+
mocks.loadPluginManifestRegistry.mockReset();
20+
mocks.loadPluginManifestRegistry.mockReturnValue({
21+
plugins: [
22+
{
23+
id: "memory-core",
24+
commandAliases: [{ name: "dreaming", kind: "runtime-slash", cliCommand: "memory" }],
25+
providers: [],
26+
channels: [],
27+
cliBackends: [],
28+
skills: [],
29+
hooks: [],
30+
origin: "bundled",
31+
},
32+
{
33+
id: "device-pair",
34+
commandAliases: [{ name: "pair", kind: "runtime-slash" }],
35+
providers: [],
36+
channels: [],
37+
cliBackends: [],
38+
skills: [],
39+
hooks: [],
40+
origin: "bundled",
41+
},
42+
{
43+
id: "openai",
44+
providers: ["openai"],
45+
setup: {
46+
providers: [{ id: "openai-codex" }],
47+
},
48+
channels: [],
49+
cliBackends: [],
50+
skills: [],
51+
hooks: [],
52+
origin: "bundled",
53+
},
54+
{
55+
id: "demo-channel",
56+
channels: ["telegram"],
57+
providers: [],
58+
cliBackends: [],
59+
skills: [],
60+
hooks: ["before-agent-start"],
61+
contracts: {
62+
tools: ["web-search"],
63+
},
64+
activation: {
65+
onRoutes: ["webhook"],
66+
onCommands: ["demo-tools"],
67+
},
68+
origin: "workspace",
69+
},
70+
],
71+
diagnostics: [],
72+
});
73+
});
74+
75+
it("matches command triggers from activation metadata and legacy command aliases", () => {
76+
expect(
77+
resolveManifestActivationPluginIds({
78+
trigger: {
79+
kind: "command",
80+
command: "memory",
81+
},
82+
}),
83+
).toEqual(["memory-core"]);
84+
85+
expect(
86+
resolveManifestActivationPluginIds({
87+
trigger: {
88+
kind: "command",
89+
command: "pair",
90+
},
91+
}),
92+
).toEqual(["device-pair"]);
93+
94+
expect(
95+
resolveManifestActivationPluginIds({
96+
trigger: {
97+
kind: "command",
98+
command: "demo-tools",
99+
},
100+
}),
101+
).toEqual(["demo-channel"]);
102+
});
103+
104+
it("matches provider, channel, and route triggers from manifest-owned metadata", () => {
105+
expect(
106+
resolveManifestActivationPluginIds({
107+
trigger: {
108+
kind: "provider",
109+
provider: "openai",
110+
},
111+
}),
112+
).toEqual(["openai"]);
113+
114+
expect(
115+
resolveManifestActivationPluginIds({
116+
trigger: {
117+
kind: "provider",
118+
provider: "openai-codex",
119+
},
120+
}),
121+
).toEqual(["openai"]);
122+
123+
expect(
124+
resolveManifestActivationPluginIds({
125+
trigger: {
126+
kind: "channel",
127+
channel: "telegram",
128+
},
129+
}),
130+
).toEqual(["demo-channel"]);
131+
132+
expect(
133+
resolveManifestActivationPluginIds({
134+
trigger: {
135+
kind: "route",
136+
route: "webhook",
137+
},
138+
}),
139+
).toEqual(["demo-channel"]);
140+
});
141+
142+
it("matches capability triggers from explicit hints or existing manifest ownership", () => {
143+
expect(
144+
resolveManifestActivationPluginIds({
145+
trigger: {
146+
kind: "capability",
147+
capability: "provider",
148+
},
149+
}),
150+
).toEqual(["openai"]);
151+
152+
expect(
153+
resolveManifestActivationPluginIds({
154+
trigger: {
155+
kind: "capability",
156+
capability: "tool",
157+
},
158+
}),
159+
).toEqual(["demo-channel"]);
160+
161+
expect(
162+
resolveManifestActivationPluginIds({
163+
trigger: {
164+
kind: "capability",
165+
capability: "hook",
166+
},
167+
}),
168+
).toEqual(["demo-channel"]);
169+
});
170+
});

‎src/plugins/activation-planner.ts‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import { normalizeProviderId } from "../agents/provider-id.js";
2+
import type { OpenClawConfig } from "../config/types.js";
3+
import { normalizeOptionalLowercaseString } from "../shared/string-coerce.js";
4+
import { loadPluginManifestRegistry, type PluginManifestRecord } from "./manifest-registry.js";
5+
import type { PluginManifestActivationCapability } from "./manifest.js";
6+
import type { PluginOrigin } from "./plugin-origin.types.js";
7+
8+
export type PluginActivationPlannerTrigger =
9+
| { kind: "command"; command: string }
10+
| { kind: "provider"; provider: string }
11+
| { kind: "channel"; channel: string }
12+
| { kind: "route"; route: string }
13+
| { kind: "capability"; capability: PluginManifestActivationCapability };
14+
15+
export function resolveManifestActivationPluginIds(params: {
16+
trigger: PluginActivationPlannerTrigger;
17+
config?: OpenClawConfig;
18+
workspaceDir?: string;
19+
env?: NodeJS.ProcessEnv;
20+
origin?: PluginOrigin;
21+
onlyPluginIds?: readonly string[];
22+
}): string[] {
23+
const onlyPluginIds =
24+
params.onlyPluginIds && params.onlyPluginIds.length > 0
25+
? new Set(params.onlyPluginIds.map((pluginId) => pluginId.trim()).filter(Boolean))
26+
: null;
27+
28+
return [
29+
...new Set(
30+
loadPluginManifestRegistry({
31+
config: params.config,
32+
workspaceDir: params.workspaceDir,
33+
env: params.env,
34+
})
35+
.plugins.filter(
36+
(plugin) =>
37+
(!params.origin || plugin.origin === params.origin) &&
38+
(!onlyPluginIds || onlyPluginIds.has(plugin.id)) &&
39+
matchesManifestActivationTrigger(plugin, params.trigger),
40+
)
41+
.map((plugin) => plugin.id),
42+
),
43+
].toSorted((left, right) => left.localeCompare(right));
44+
}
45+
46+
function matchesManifestActivationTrigger(
47+
plugin: PluginManifestRecord,
48+
trigger: PluginActivationPlannerTrigger,
49+
): boolean {
50+
switch (trigger.kind) {
51+
case "command":
52+
return listActivationCommandIds(plugin).includes(normalizeCommandId(trigger.command));
53+
case "provider":
54+
return listActivationProviderIds(plugin).includes(normalizeProviderId(trigger.provider));
55+
case "channel":
56+
return listActivationChannelIds(plugin).includes(normalizeCommandId(trigger.channel));
57+
case "route":
58+
return listActivationRouteIds(plugin).includes(normalizeCommandId(trigger.route));
59+
case "capability":
60+
return hasActivationCapability(plugin, trigger.capability);
61+
}
62+
const unreachableTrigger: never = trigger;
63+
return unreachableTrigger;
64+
}
65+
66+
function listActivationCommandIds(plugin: PluginManifestRecord): string[] {
67+
return [
68+
...(plugin.activation?.onCommands ?? []),
69+
...(plugin.commandAliases ?? []).flatMap((alias) => alias.cliCommand ?? alias.name),
70+
]
71+
.map(normalizeCommandId)
72+
.filter(Boolean);
73+
}
74+
75+
function listActivationProviderIds(plugin: PluginManifestRecord): string[] {
76+
return [
77+
...(plugin.activation?.onProviders ?? []),
78+
...plugin.providers,
79+
...(plugin.setup?.providers?.map((provider) => provider.id) ?? []),
80+
]
81+
.map((value) => normalizeProviderId(value))
82+
.filter(Boolean);
83+
}
84+
85+
function listActivationChannelIds(plugin: PluginManifestRecord): string[] {
86+
return [...(plugin.activation?.onChannels ?? []), ...plugin.channels]
87+
.map(normalizeCommandId)
88+
.filter(Boolean);
89+
}
90+
91+
function listActivationRouteIds(plugin: PluginManifestRecord): string[] {
92+
return (plugin.activation?.onRoutes ?? []).map(normalizeCommandId).filter(Boolean);
93+
}
94+
95+
function hasActivationCapability(
96+
plugin: PluginManifestRecord,
97+
capability: PluginManifestActivationCapability,
98+
): boolean {
99+
if (plugin.activation?.onCapabilities?.includes(capability)) {
100+
return true;
101+
}
102+
switch (capability) {
103+
case "provider":
104+
return listActivationProviderIds(plugin).length > 0;
105+
case "channel":
106+
return listActivationChannelIds(plugin).length > 0;
107+
case "tool":
108+
return (plugin.contracts?.tools?.length ?? 0) > 0;
109+
case "hook":
110+
return plugin.hooks.length > 0;
111+
}
112+
const unreachableCapability: never = capability;
113+
return unreachableCapability;
114+
}
115+
116+
function normalizeCommandId(value: string | undefined): string {
117+
return normalizeOptionalLowercaseString(value) ?? "";
118+
}

0 commit comments

Comments
 (0)