fix: inject pluginConfig into hook handler event context

When plugins register hooks via api.registerHook(), pluginConfig from
openclaw.json was not available in the hook event context. Plugins that
accessed ctx.pluginConfig or event.context.pluginConfig received
undefined, causing silent failures or fallback to defaults.

Changes:
- Add pluginConfig parameter to registerHook() function
- Wrap handler to inject pluginConfig into event.context before invocation
- Pass params.pluginConfig through createApi() call site

Fixes #72880
This commit is contained in:
Ayumi Server
2026-04-27 15:44:49 +00:00
committed by Peter Steinberger
parent ad6e1cd3a0
commit ed0b098d75

View File

@@ -422,6 +422,7 @@ export function createPluginRegistry(registryParams: PluginRegistryParams) {
handler: Parameters<typeof registerInternalHook>[1],
opts: OpenClawPluginHookOptions | undefined,
config: OpenClawPluginApi["config"],
pluginConfig: unknown,
) => {
const eventList = Array.isArray(events) ? events : [events];
const normalizedEvents = eventList.map((event) => event.trim()).filter(Boolean);
@@ -499,8 +500,16 @@ export function createPluginRegistry(registryParams: PluginRegistryParams) {
handler: Parameters<typeof registerInternalHook>[1];
}> = [];
for (const event of normalizedEvents) {
registerInternalHook(event, handler);
nextRegistrations.push({ event, handler });
// Wrap handler to inject pluginConfig into event context
// so plugins can access their configured pluginConfig at invocation time
const wrappedHandler: typeof handler = async (evt) => {
if (evt.context && typeof evt.context === "object") {
(evt.context as Record<string, unknown>).pluginConfig = pluginConfig;
}
return handler(evt);
};
registerInternalHook(event, wrappedHandler);
nextRegistrations.push({ event, handler: wrappedHandler });
}
activePluginHookRegistrations.set(hookName, nextRegistrations);
const rollbackEntries = pluginHookRollback.get(record.id) ?? [];
@@ -1466,7 +1475,7 @@ export function createPluginRegistry(registryParams: PluginRegistryParams) {
? {
registerTool: (tool, opts) => registerTool(record, tool, opts),
registerHook: (events, handler, opts) =>
registerHook(record, events, handler, opts, params.config),
registerHook(record, events, handler, opts, params.config, params.pluginConfig),
registerHttpRoute: (routeParams) => registerHttpRoute(record, routeParams),
registerProvider: (provider) => registerProvider(record, provider),
registerAgentHarness: (harness) => registerAgentHarness(record, harness),