72 lines · 4.5 KB · ExecutableRaw
#!/bin/bash
# Installed by herdr-app (repo: hooks/herdr-activity.sh). Claude Code and Codex hook for UserPromptSubmit and Stop.
# Asks Haiku for a one-line label of what this agent is working on and stores it as the `kit.activity`
# label on its herdr-core terminal; the kit layout's agents-activity mod shows it.
set -uo pipefail
exec 2>/dev/null
term=${HC_TERMINAL_ID:-}
[[ -n "$term" ]] || { cat >/dev/null; exit 0; } # not in a herdr-core terminal
PATH="$PATH:$HOME/.local/bin:/opt/homebrew/bin:/usr/local/bin"
PROMPTS=6 # recent prompts sent to the summariser, newest included
REPLY_CHARS=3000 # how much of the agent's messages is sent (their end)
input=$(cat)
event=$(jq -r '.hook_event_name // empty' <<<"$input")
log=$(jq -r '.transcript_path // empty' <<<"$input")
newest=$(jq -r '.prompt // empty' <<<"$input") # only on UserPromptSubmit: it may not be in the log yet
case "$event" in UserPromptSubmit) type=start ;; Stop) type=end ;; *) exit 0 ;; esac
[[ -f "$log" ]] || exit 0
# The session's prompts and the agent's replies, skipping wrappers, tool results, sidechains and half-written lines.
ctx=$(jq -nR --arg newest "$newest" --argjson n "$PROMPTS" --argjson c "$REPLY_CHARS" '
def wrapper: test("^\\s*(<(command-|local-command-|system-reminder|task-notification|task-id|bash-|environment_context|user_instructions|INSTRUCTIONS)|# AGENTS\\.md|\\[Request interrupted by user)");
def text_of($type): [.[]? | select(.type == $type) | .text | select(wrapper | not)] | join("\n");
[ inputs | fromjson? |
if .type == "user" and (.isMeta | not) and (.isCompactSummary | not) and (.isSidechain | not) then
{user: (.message.content | if type == "string" then (if wrapper then "" else . end)
elif any(.[]?; .type == "tool_result") then ""
else text_of("text") end)}
elif .type == "attachment" and .attachment.type == "queued_command" and (.attachment.commandMode // "prompt") == "prompt" then
{user: (.attachment.prompt | if wrapper then "" else . end)}
elif .type == "assistant" and (.isSidechain | not) then {agent: (.message.content | text_of("text"))}
elif .type == "response_item" and .payload.type == "message" and .payload.role == "user" then # Codex
{user: (.payload.content | text_of("input_text"))}
elif .type == "response_item" and .payload.type == "message" and .payload.role == "assistant" then
{agent: (.payload.content | text_of("output_text"))}
else empty end
| select((.user // .agent) != "") ]
| [.[] | select(.user) | .user] as $logged
| ($logged + (if $newest != "" and ($newest | wrapper | not) and $newest != $logged[-1] then [$newest] else [] end)) as $u
| select($u | length > 0)
| { new: ($u[-1] | .[0:2000]), earlier: $u[:-1][-($n - 1):],
reply: ([.[] | select(.agent) | .agent] | join("\n\n") | .[-$c:]) }' "$log")
[[ -n "$ctx" ]] || exit 0
prompt=$(jq -r --arg type "$type" '
"You write the line a sidebar shows for a coding agent.\n\n"
+ "Earlier prompts from the user, oldest first:\n"
+ ([.earlier[] | "- " + (gsub("\\s+"; " ") | .[0:300])] | join("\n")) + "\n\n"
+ (if $type == "start" then
"The agent'\''s latest messages, oldest first (their end):\n\(.reply)\n\n"
+ "The user has just replied to that, and the agent is starting on it:\n\(.new)\n\n"
+ "Say what the agent is now doing. A short reply answers the agent'\''s last message: "
+ "a number or letter picks exactly that option from it (2 means the option numbered 2), "
+ "and yes, go or ok approves what it proposed. "
+ "Start with an -ing verb (Adding, Fixing, Checking). "
else
"Newest prompt from the user:\n\(.new)\n\n"
+ "The agent'\''s latest messages, oldest first (their end):\n\(.reply)\n\n"
+ "The agent has just finished its work on the newest prompt. Say what it just did. "
+ "Start with a past-tense verb (Added, Fixed, Found). "
end)
+ "Reply with only that line: one plain phrase of 5 to 10 words, no quotes, no final period."' <<<"$ctx")
# Throwaway session: no tools, settings, hooks or MCP; without HC_TERMINAL_ID this hook can't fire inside it.
cd "${TMPDIR:-/tmp}" || exit 0
text=$(env -u HC_TERMINAL_ID -u HC_TERMINAL_SOCKET -u HC_SCOPE \
claude -p --model haiku --no-session-persistence --tools "" --setting-sources "" --strict-mcp-config \
--disable-slash-commands "$prompt" </dev/null \
| grep -m1 '[^[:space:]]' | sed -e 's/^[[:space:]"'\''`*]*//' -e 's/[[:space:]"'\''`*.]*$//')
[[ -n "$text" ]] && hc label "$term" "kit.activity=$text" >/dev/null
exit 0