#!/bin/sh
# agent-memory-wake: the live wake's plugin helper (docs/PROTOCOL.md section 11).
#
#   agent-memory-wake watch [HINT]   the plugin's monitor: `agent-memory inbox --watch`
#   agent-memory-wake read           the inbox skill: `agent-memory inbox`
#
# A relay started with --wake monitor marks each batch that may wake this agent's
# session; the watch prints one line for it, and Claude Code turns that line into a turn
# in the live session. `agent-memory` is found on PATH, or named by $AGENT_MEMORY_CLI,
# which may be a command with arguments ("python3 -m agent_memory").
#
# A plugin monitor that exits is not started again, and its end is itself something the
# session is told, so `watch` never exits on its own: with no agent-memory to run, or one
# older than `inbox --watch`, it waits quietly until the session ends.
set -u
cli=${AGENT_MEMORY_CLI:-agent-memory}
case "${1:-}" in
    watch)
        hint=${2:-}
        # $cli unquoted on purpose: it may be a command and its arguments
        if $cli inbox --help >/dev/null 2>&1; then
            if [ -n "$hint" ]; then
                exec $cli inbox --watch --hint "$hint"
            fi
            exec $cli inbox --watch
        fi
        while :; do sleep 3600; done
        ;;
    read)
        exec $cli inbox
        ;;
    *)
        echo "usage: agent-memory-wake watch [HINT] | read" >&2
        exit 2
        ;;
esac
