Skip to content

Commit 416057e

Browse files
authored
refactor(plugins): simplify registration lifecycle policy (#133364)
1 parent d3e058c commit 416057e

4 files changed

Lines changed: 34 additions & 42 deletions

File tree

‎config/assertion-safety-baseline.txt‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3436,7 +3436,6 @@ src/plugin-state/plugin-store-validation.ts 1
34363436
src/plugin-state/runtime-health-store.ts 1
34373437
src/plugins/agent-tool-result-middleware.ts 1
34383438
src/plugins/api-facades.ts 1
3439-
src/plugins/api-lifecycle.ts 1
34403439
src/plugins/bundle-config-shared.ts 1
34413440
src/plugins/bundle-lsp.ts 1
34423441
src/plugins/bundle-mcp.ts 2
@@ -3500,7 +3499,7 @@ src/plugins/loader-channel-setup.ts 17
35003499
src/plugins/loader-cli-registry.ts 2
35013500
src/plugins/loader-discovery.ts 1
35023501
src/plugins/loader-load-context.ts 4
3503-
src/plugins/loader-module-runtime.ts 12
3502+
src/plugins/loader-module-runtime.ts 11
35043503
src/plugins/loader-records.ts 1
35053504
src/plugins/loader-runtime-candidate.ts 1
35063505
src/plugins/loader-runtime-load.ts 2

‎src/plugins/api-lifecycle.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { expectDefined } from "@openclaw/normalization-core";
44
import { describe, expect, it, vi } from "vitest";
55
import type { OpenClawConfig } from "../config/types.openclaw.js";
66
import { buildPluginApi } from "./api-builder.js";
7+
import { isLateCallablePluginApiMethod } from "./api-lifecycle.js";
78
import { runPluginRegisterSyncInRegistry } from "./loader-module-runtime.js";
89
import { createEmptyPluginRegistry } from "./registry-empty.js";
910
import type { PluginRuntime } from "./runtime/types.js";
@@ -147,4 +148,24 @@ describe("plugin api lifecycle", () => {
147148
expect(result).toBeUndefined();
148149
expect(registerSessionExtension).not.toHaveBeenCalled();
149150
});
151+
152+
it.each<[string, boolean]>([
153+
["clearRunContext", true],
154+
["emitAgentEvent", true],
155+
["enqueueNextTurnInjection", true],
156+
["getRunContext", true],
157+
["sendSessionAttachment", true],
158+
["scheduleSessionTurn", true],
159+
["setRunContext", true],
160+
["unscheduleSessionTurnsByTag", true],
161+
["registerTool", false],
162+
["registerSessionExtension", false],
163+
["unknown", false],
164+
["", false],
165+
["constructor", false],
166+
["toString", false],
167+
["__proto__", false],
168+
])("classifies late-call eligibility for %j as %s", (methodName, expected) => {
169+
expect(isLateCallablePluginApiMethod(methodName)).toBe(expected);
170+
});
150171
});

‎src/plugins/api-lifecycle.ts‎

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,18 @@
11
/** Tracks plugin API lifecycle callbacks registered during runtime activation. */
22
import type { OpenClawPluginApi } from "./types.js";
33

4-
type FunctionPropertyNames<T> = Extract<
5-
{
6-
[K in keyof T]-?: Exclude<T[K], undefined> extends (...args: unknown[]) => unknown ? K : never;
7-
}[keyof T],
8-
string
9-
>;
10-
11-
/** Names of plugin API methods exposed on the OpenClaw plugin API. */
12-
type PluginApiMethodName = FunctionPropertyNames<OpenClawPluginApi>;
13-
14-
/** Lifecycle policy for whether a plugin API method can be called after registration. */
15-
type PluginApiLifecyclePolicy = {
16-
phase: "registration" | "runtime";
17-
lateCallable: boolean;
18-
};
19-
20-
const PLUGIN_API_METHOD_POLICIES: Partial<Record<PluginApiMethodName, PluginApiLifecyclePolicy>> = {
21-
clearRunContext: { phase: "runtime", lateCallable: true },
22-
emitAgentEvent: { phase: "runtime", lateCallable: true },
23-
enqueueNextTurnInjection: { phase: "runtime", lateCallable: true },
24-
getRunContext: { phase: "runtime", lateCallable: true },
25-
sendSessionAttachment: { phase: "runtime", lateCallable: true },
26-
scheduleSessionTurn: { phase: "runtime", lateCallable: true },
27-
setRunContext: { phase: "runtime", lateCallable: true },
28-
unscheduleSessionTurnsByTag: { phase: "runtime", lateCallable: true },
29-
};
30-
31-
/** Returns lifecycle policy for one plugin API method name. */
32-
function getPluginApiMethodLifecyclePolicy(
33-
methodName: string,
34-
): PluginApiLifecyclePolicy | undefined {
35-
return PLUGIN_API_METHOD_POLICIES[methodName as PluginApiMethodName];
36-
}
4+
const LATE_CALLABLE_PLUGIN_API_METHODS: ReadonlySet<string> = new Set<keyof OpenClawPluginApi>([
5+
"clearRunContext",
6+
"emitAgentEvent",
7+
"enqueueNextTurnInjection",
8+
"getRunContext",
9+
"sendSessionAttachment",
10+
"scheduleSessionTurn",
11+
"setRunContext",
12+
"unscheduleSessionTurnsByTag",
13+
]);
3714

3815
/** True when a plugin API method remains callable after registration. */
39-
export function isLateCallablePluginApiMethod(
40-
methodName: string,
41-
): methodName is PluginApiMethodName {
42-
return getPluginApiMethodLifecyclePolicy(methodName)?.lateCallable === true;
16+
export function isLateCallablePluginApiMethod(methodName: string): boolean {
17+
return LATE_CALLABLE_PLUGIN_API_METHODS.has(methodName);
4318
}

‎src/plugins/loader-module-runtime.ts‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,6 @@ export function resolvePluginModuleExport(moduleExport: unknown): {
296296
}
297297
}
298298
const resolved = candidates[0];
299-
if (typeof resolved === "function") {
300-
return { register: resolved as OpenClawPluginDefinition["register"] };
301-
}
302299
if (resolved && typeof resolved === "object") {
303300
const definition = resolved as OpenClawPluginDefinition;
304301
return { definition, register: definition.register };

0 commit comments

Comments
 (0)