Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
#!/bin/sh
# ai-memory plugin hook launcher. Usage: ai-memory-hook <agent> <event>
#
# Generated by `ai-memory render-plugins`; edit
# crates/ai-memory-cli/templates/plugin/ai-memory-hook.sh instead.
#
# Hands one lifecycle event to the installed ai-memory binary's native
# `ai-memory hook` command, which owns capture policy (`ignore_paths`,
# allowlist mode), marker scope, server profiles and spooling. This script
# has no capture logic of its own, and it never blocks the agent: without a
# binary it drains stdin, prints a JSON reply and exits 0. It sends nothing
# anywhere itself.
agent="${1:-}"
event="${2:-}"
reply_without_binary() {
cat >/dev/null
if [ "$event" = "session-start" ]; then
# The backticks are literal Markdown in the message, not a command.
# shellcheck disable=SC2016
printf '%s\n' '{"systemMessage":"ai-memory: the ai-memory binary was not found, so this session is not captured. Install it and run `ai-memory setup`: https://github.com/akitaonrails/ai-memory#install"}'
else
printf '{}\n'
fi
exit 0
}
# Other harnesses can read the Claude Code plugin through their Claude
# fallbacks. Attribute the event to the harness that really ran it, or stay
# out of the way when ai-memory has no agent for it.
# Muse Code sets CLAUDE_PLUGIN_ROOT for the Claude plugins it loads, so it is
# told apart by the MUSE_PLUGIN_ID it adds for plugin hooks, next to the
# MUSE_SESSION_ID every Muse hook gets.
if [ "$agent" = "claude-code" ]; then
if [ -n "${DROID_PLUGIN_ROOT:-}" ]; then
agent="droid"
elif [ -n "${MUSE_PLUGIN_ID:-}" ] && [ -n "${MUSE_SESSION_ID:-}" ]; then
agent="muse"
elif [ -z "${CLAUDE_PLUGIN_ROOT:-}" ]; then
if [ -n "${PLUGIN_ROOT:-}" ]; then
agent="codex"
else
cat >/dev/null
printf '{}\n'
exit 0
fi
fi
fi
if [ -z "$agent" ] || [ -z "$event" ]; then
cat >/dev/null
printf '{}\n'
exit 0
fi
# An empty URL lets the binary use the server_url of its own config, so the
# plugin and the CLI never disagree about where events go.
url="${CLAUDE_PLUGIN_OPTION_SERVER_URL:-}"
data_dir="${CLAUDE_PLUGIN_OPTION_DATA_DIR:-}"
bin="${CLAUDE_PLUGIN_OPTION_AI_MEMORY_BIN:-}"
# Only an absolute path is honoured. A relative one would resolve against the
# hook's working directory, the checkout, so a repository could ship the
# "binary" every event is handed to.
case "$bin" in
/* | [A-Za-z]:[\\/]*) ;;
*) bin="" ;;
esac
if [ -z "$bin" ]; then
bin=""
if command -v ai-memory >/dev/null 2>&1; then
bin=$(command -v ai-memory)
else
# A GUI-launched app may not have these on PATH.
for candidate in "${HOME:-}/.local/bin/ai-memory" "${HOME:-}/.cargo/bin/ai-memory" \
/opt/homebrew/bin/ai-memory /usr/local/bin/ai-memory /usr/bin/ai-memory; do
if [ -x "$candidate" ]; then
bin="$candidate"
break
fi
done
fi
fi
if [ -z "$bin" ] || [ ! -x "$bin" ]; then
reply_without_binary
fi
# `hook` comes first so the Docker host wrapper can route it to its native
# client; --data-dir is a global flag and may follow it.
set -- hook --event "$event" --agent "$agent"
if [ -n "$data_dir" ]; then
set -- "$@" --data-dir "$data_dir"
fi
AI_MEMORY_HOOK_ORIGIN="$agent-plugin"
export AI_MEMORY_HOOK_ORIGIN
if [ -n "$url" ]; then
exec "$bin" "$@" --server-url "$url"
fi
# No URL option: ai-memory 3.0 and later read server_url from their own
# config when --server-url is omitted. An older binary requires the flag
# (and would post an empty one to a bare /hook), so it rejects the call with
# a usage error, exit 2, before reading stdin. Retry it once against the
# server AI_MEMORY_SERVER_URL names, else the default local server; stdin
# still has the whole payload. Say why.
status=0
"$bin" "$@" || status=$?
if [ "$status" -ne 2 ]; then
exit "$status"
fi
fallback="${AI_MEMORY_SERVER_URL:-http://127.0.0.1:49374}"
printf '%s\n' "ai-memory: this ai-memory binary predates the plugin (it needs 3.0 or later); sending to $fallback. Upgrade ai-memory, or set the plugin's server_url option." >&2
exec "$bin" "$@" --server-url "$fallback"