#!/bin/bash
# Hook: SubagentStart — inject behavioral context into sub-agents
# Triggered when a sub-agent starts
#
# Ensures sub-agents follow the same Claude-style rules.

set -e

# Resolve prompts dir relative to this hook's location
HOOK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROMPTS_DIR="$HOOK_DIR/prompts/core"

# If not found next to hook, check extension directory
if [ ! -d "$PROMPTS_DIR" ]; then
    EXTENSION_DIR="$(cd "$HOOK_DIR/.." && pwd)"
    PROMPTS_DIR="$EXTENSION_DIR/prompts/core"
fi

if [ ! -d "$PROMPTS_DIR" ]; then
    echo '{"type": "no-op"}'
    exit 0
fi

# Always inject core communication rules for sub-agents
CONTEXT=""
for file in \
    "$PROMPTS_DIR/communication-style.md" \
    "$PROMPTS_DIR/read-before-modify.md" \
    "$PROMPTS_DIR/no-unnecessary-additions.md"; do
    if [ -f "$file" ]; then
        CONTEXT="${CONTEXT}$(cat "$file")

"
    fi
done

if [ -z "$CONTEXT" ]; then
    echo '{"type": "no-op"}'
    exit 0
fi

printf '{"type": "additionalContext", "context": %s}\n' "$(printf '%s' "$CONTEXT" | jq -Rs .)"
