🧠the-brain

Hook System

Event-driven plugin system powering the-brain's cognitive layers

All plugins communicate through hooks defined in @the-brain-dev/core.

Core Hook Events

17 lifecycle events defined in the HookEvent enum:

HookWhenConsumer
HARVESTER_POLLDaemon polls AI data sourcesHarvesters
HARVESTER_NEW_DATANew interaction foundAll layers
BEFORE_PROMPTBefore prompt reaches LLMGraph Memory
AFTER_RESPONSEAfter LLM respondsSPM Curator
ON_INTERACTIONEvery interactionSPM Curator, Identity Anchor
SELECTION_EVALUATEEvaluate for promotionSPM Curator
SELECTION_PROMOTEPromote to deeper layerSPM Curator → DEEP
DEEP_CONSOLIDATEDeep layer consolidationMLX Trainer, Auto-Wiki
CONSOLIDATION_STARTConsolidation cycle beginsAll layers
CONSOLIDATION_COMPLETEConsolidation cycle endsNotifications
TRAINING_STARTMLX training beginsIdentity Anchor
TRAINING_COMPLETEMLX training finishesNotifications
TRAINING_ERRORMLX training failsError logging, notifications
DAEMON_STARTDaemon startupAll plugins
DAEMON_STOPDaemon shutdownCleanup
IDENTITY_SWITCH_USERUser identity changes (team mode)Identity Anchor
IDENTITY_DRIFT_DETECTEDCoding style drift detectedIdentity Anchor

Registration

import { definePlugin, HookEvent } from "@the-brain-dev/core";

export default definePlugin({
  name: "my-plugin",
  setup(hooks) {
    hooks.hook(HookEvent.AFTER_RESPONSE, async (ctx) => {
      // ctx.interaction, ctx.fragments, ctx.promoteToDeep()
    });
  },
});

Plugin-Defined Custom Hooks

Since 2026-05-09, HookEventName accepts custom string event names in addition to the HookEvent enum. This allows plugins to define their own events without as any casts:

// Plugins can register custom events:
hooks.callHook("identity-anchor:switchUser", userId);
hooks.callHook("wiki:generate", { topic: "memory-layers" });

Known plugin-defined hooks:

HookPluginPurpose
identity-anchor:switchUserIdentity AnchorSwitch active user in team mode
identity-anchor:predictRegressionIdentity AnchorPredict benchmark scores before harness edits
identity-anchor:assessSurpriseIdentity AnchorCompare observed vs predicted benchmark
identity-anchor:getFingerprintsIdentity AnchorDump all benchmark fingerprints
identity-anchor:detectDriftIdentity AnchorDetect systematic model degradation
identity-anchor:fingerprintSummaryIdentity AnchorHuman-readable fingerprint summary
wiki:generateAuto-WikiTrigger wiki generation
spm-curator:getInstanceSPM CuratorAccess SPM curator singleton
training:runMLX TrainerTrigger training programmatically

Execution

  • Handlers execute serially in registration order
  • Errors in one handler don't stop others
  • Powered by hookable — lightweight, async-safe

Layer Routing

const router = new LayerRouter();
router.registerInstant(graphMemory);    // ⚡ Layer 1
router.registerSelection(spmCurator);   // ⚖️ Layer 2
router.registerDeep(mlxTrainer);       // 🌌 Layer 3

await router.runInstant(promptCtx);     // Inject context
await router.runSelection(ctx);         // Evaluate + promote
await router.runDeep(ctx);              // Train + wiki

On this page