session-start.sh 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env bash
  2. # SessionStart hook for OAC plugin
  3. set -euo pipefail
  4. # Determine plugin root directory
  5. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
  6. PLUGIN_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
  7. SKILL_FILE="${PLUGIN_ROOT}/skills/using-oac/SKILL.md"
  8. # Read using-oac content
  9. using_oac_content=$(cat "${SKILL_FILE}" 2>&1 || echo "Error reading using-oac skill")
  10. # Escape string for JSON embedding using bash parameter substitution
  11. # Each ${s//old/new} is a single C-level pass - orders of magnitude
  12. # faster than character-by-character loop
  13. escape_for_json() {
  14. local s="$1"
  15. s="${s//\\/\\\\}"
  16. s="${s//\"/\\\"}"
  17. s="${s//$'\n'/\\n}"
  18. s="${s//$'\r'/\\r}"
  19. s="${s//$'\t'/\\t}"
  20. printf '%s' "$s"
  21. }
  22. using_oac_escaped=$(escape_for_json "$using_oac_content")
  23. # Build warning message for first-time users
  24. warning_message=""
  25. if [[ ! -f ".context-manifest.json" ]]; then
  26. warning_message="\n\n<important-reminder>IN YOUR FIRST REPLY AFTER SEEING THIS MESSAGE YOU MUST TELL THE USER:👋 **Welcome to OpenAgents Control!** To get started, run /oac:setup to download context files. Then use /oac:help to learn the 6-stage workflow.</important-reminder>"
  27. fi
  28. warning_escaped=$(escape_for_json "$warning_message")
  29. # Output context injection as JSON
  30. cat <<EOF
  31. {
  32. "hookSpecificOutput": {
  33. "hookEventName": "SessionStart",
  34. "additionalContext": "<EXTREMELY_IMPORTANT>\nYou are using OpenAgents Control (OAC).\n\n**Below is the full content of your 'using-oac' skill - your guide to the 6-stage workflow. For all other skills, use the 'Skill' tool:**\n\n${using_oac_escaped}\n\n${warning_escaped}\n</EXTREMELY_IMPORTANT>"
  35. }
  36. }
  37. EOF
  38. exit 0