Documentation / Hooks

Agent hooks

Universal, auto-detecting lifecycle hooks — pipe agent hook JSON in, get memories out. No per-agent config to maintain. Eight adapters cover seven named agents plus a generic fallback.

How hooks work

A hook script is generated per agent. When an agent fires a lifecycle event, it pipes a JSON payload to the memorius-hook engine, which auto-detects which agent sent it and runs the configured actions.

Generate the hook scripts and plugins for every supported agent:

memorius-plugin-gen init      # creates universal-manifest.yaml
# edit the manifest to pick agents + actions
memorius-plugin-gen generate  # emits per-agent plugins, hook scripts, skill cards

Then point your agent at the generated hook. The engine reads stdin, detects the agent, and executes the actions for that event — mine, diary, inject_context, and more.

Hook actions

Ten action types the engine can run on an event. The engine may also return block to pause agent execution (allow/block semantics).

ActionDescription
mine_dirRead transcript file(s) and mine via the vault engine.
diaryWrite a diary entry.
conditional_diaryWrite a diary only after N exchanges (interval-based).
commandExecute a shell command (sanitized via shlex, shell=False).
logLog a message.
webhookPOST the event payload to a URL (with SSRF protection).
inject_contextInject relevant memories into context.
consolidateRun memory consolidation.
factcheckFact-check a statement from the event payload.
allow/blockEngine returns block to pause agent execution.

Auto-detection & priority

No --agent flag needed — the engine figures out which agent sent the event from the payload. Force one with --agent if you prefer.

Detection priority (most-specific first):

OpenClaude → Claude Code → Codex → Gemini CLI → OpenClaw → OpenCode → Pi → Generic
AgentMarker / protocolEvents
OpenClaudeOpenClaude marker in payloadstop, precompact, session_start
Claude Codestop_hook_active / precompactstop, precompact, session_start
Codex CLIsession_id + context_dirsession_start, session_stop, stop, precompact
Gemini CLIconversation_id + extensionsstop, session_start
OpenClawopenclaw markerstop, session_stop, precompact, session_start
OpenCodeprovider dict + openCodeVersionstop, session_stop, session_start, precompact
Pievent in the Pi event setsession_start, session_shutdown, pre_compact, tool_call, turn_end
Genericfallback for unrecognized payloadsany

The memorius-hook CLI

memorius-hook run              # process a hook event from stdin
memorius-hook init-config      # generate default ~/.memorius/hooks.yaml
memorius-hook status           # show hook state summary

Auto-detect the agent by piping the payload in:

cat hook-payload.json | memorius-hook mine
cat hook-payload.json | memorius-hook diary

# or force a specific agent:
memorius-hook mine --agent claude-code
memorius-hook diary --agent opencode

Plugin generator

Generates per-agent plugins: plugin.json, hooks.json, hook shell scripts, skill cards, commands, marketplace.json, and a README.

memorius-plugin-gen list              # show supported agents
memorius-plugin-gen init              # create universal-manifest.yaml
memorius-plugin-gen generate          # generate plugins for all agents
memorius-plugin-gen generate --watch  # live regeneration on config change

Conversation normalizers

The memorius-normalize CLI converts Discord, Telegram, WhatsApp, and generic chat exports into a uniform transcript that mine / extract understand.

memorius-normalize detect <files>   # auto-detect the format
memorius-normalize convert <file>   # convert one file to a transcript
memorius-normalize batch <dir>      # convert a whole directory
memorius-normalize pipe             # read stdin, write stdout
FormatDescription
discordDiscord channel export JSON (DiscordChatExporter).
telegramTelegram Desktop chat export JSON.
whatsappWhatsApp plain-text export (international + US/EU variants).
generic-jsonAny JSON with {role, content}, {author, text}, or {from, text}.
generic-messagesList of message objects with role/author fields.
generic-chatDict with a conversations / chats key.
generic-textPlain text with speaker: lines or > transcript markers.

Security hardening

DefenseDetail
Command executionshlex.split() with shell=False — no shell injection; shell metacharacters are stripped from templates.
Webhook SSRFBlocks localhost, private IPs, cloud metadata endpoints, and non-HTTP schemes.
Context injectionPrompt-injection protection sanitizes memory content before LLM injection (strips "ignore previous instructions", XML tags, control characters).

Repository

Memorius is MIT-licensed and fully open source. Star the repo, open an issue, or dig into the source on GitHub.