|
1 | 1 | /** Tracks plugin API lifecycle callbacks registered during runtime activation. */ |
2 | 2 | import type { OpenClawPluginApi } from "./types.js"; |
3 | 3 |
|
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 | +]); |
37 | 14 |
|
38 | 15 | /** 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); |
43 | 18 | } |
0 commit comments