This commit is contained in:
AnotherWallace 2026-01-29 19:00:18 +00:00 committed by GitHub
commit 2b40d6c639
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 58 additions and 23 deletions

View File

@ -29,9 +29,9 @@ export function initializeGlobalHookRunner(registry: PluginRegistry): void {
catchErrors: true, catchErrors: true,
}); });
const hookCount = registry.hooks.length; const typedHookCount = registry.typedHooks.length;
if (hookCount > 0) { if (typedHookCount > 0) {
log.info(`hook runner initialized with ${hookCount} registered hooks`); log.info(`hook runner initialized with ${typedHookCount} registered hooks`);
} }
} }

View File

@ -7,26 +7,27 @@ import type {
} from "../gateway/server-methods/types.js"; } from "../gateway/server-methods/types.js";
import { registerInternalHook } from "../hooks/internal-hooks.js"; import { registerInternalHook } from "../hooks/internal-hooks.js";
import { resolveUserPath } from "../utils.js"; import { resolveUserPath } from "../utils.js";
import type { import {
MoltbotPluginApi, TYPED_HOOK_NAMES,
MoltbotPluginChannelRegistration, type MoltbotPluginApi,
MoltbotPluginCliRegistrar, type MoltbotPluginChannelRegistration,
MoltbotPluginCommandDefinition, type MoltbotPluginCliRegistrar,
MoltbotPluginHttpHandler, type MoltbotPluginCommandDefinition,
MoltbotPluginHttpRouteHandler, type MoltbotPluginHttpHandler,
MoltbotPluginHookOptions, type MoltbotPluginHttpRouteHandler,
ProviderPlugin, type MoltbotPluginHookOptions,
MoltbotPluginService, type ProviderPlugin,
MoltbotPluginToolContext, type MoltbotPluginService,
MoltbotPluginToolFactory, type MoltbotPluginToolContext,
PluginConfigUiHint, type MoltbotPluginToolFactory,
PluginDiagnostic, type PluginConfigUiHint,
PluginLogger, type PluginDiagnostic,
PluginOrigin, type PluginLogger,
PluginKind, type PluginOrigin,
PluginHookName, type PluginKind,
PluginHookHandlerMap, type PluginHookName,
PluginHookRegistration as TypedPluginHookRegistration, type PluginHookHandlerMap,
type PluginHookRegistration as TypedPluginHookRegistration,
} from "./types.js"; } from "./types.js";
import { registerPluginCommand } from "./commands.js"; import { registerPluginCommand } from "./commands.js";
import type { PluginRuntime } from "./runtime/types.js"; import type { PluginRuntime } from "./runtime/types.js";
@ -252,6 +253,22 @@ export function createPluginRegistry(registryParams: PluginRegistryParams) {
source: record.source, source: record.source,
}); });
// Bridge to typed hooks: if any event matches a PluginHookName, also register
// in typedHooks so the hook runner can find it. This allows plugins to use
// either api.registerHook() or api.on() for typed hooks like message_received.
for (const event of normalizedEvents) {
if (TYPED_HOOK_NAMES.has(event as PluginHookName)) {
registry.typedHooks.push({
pluginId: record.id,
hookName: event as PluginHookName,
handler: handler as unknown as PluginHookHandlerMap[PluginHookName],
priority: opts?.priority,
source: record.source,
} as TypedPluginHookRegistration);
record.hookCount += 1;
}
}
const hookSystemEnabled = config?.hooks?.internal?.enabled === true; const hookSystemEnabled = config?.hooks?.internal?.enabled === true;
if (!hookSystemEnabled || opts?.register === false) { if (!hookSystemEnabled || opts?.register === false) {
return; return;

View File

@ -81,6 +81,7 @@ export type MoltbotPluginHookOptions = {
name?: string; name?: string;
description?: string; description?: string;
register?: boolean; register?: boolean;
priority?: number;
}; };
export type ProviderAuthKind = "oauth" | "api_key" | "token" | "device_code" | "custom"; export type ProviderAuthKind = "oauth" | "api_key" | "token" | "device_code" | "custom";
@ -302,6 +303,23 @@ export type PluginHookName =
| "gateway_start" | "gateway_start"
| "gateway_stop"; | "gateway_stop";
export const TYPED_HOOK_NAMES: Set<PluginHookName> = new Set<PluginHookName>([
"before_agent_start",
"agent_end",
"before_compaction",
"after_compaction",
"message_received",
"message_sending",
"message_sent",
"before_tool_call",
"after_tool_call",
"tool_result_persist",
"session_start",
"session_end",
"gateway_start",
"gateway_stop",
]);
// Agent context shared across agent hooks // Agent context shared across agent hooks
export type PluginHookAgentContext = { export type PluginHookAgentContext = {
agentId?: string; agentId?: string;