session-start.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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
  11. # SECURITY: Prevents command injection attacks from malicious SKILL.md files
  12. escape_for_json() {
  13. local s="$1"
  14. # Escape backslashes FIRST - order matters!
  15. s="${s//\\/\\\\}"
  16. # Escape double quotes
  17. s="${s//\"/\\\"}"
  18. # Escape newlines, carriage returns, tabs
  19. s="${s//$'\n'/\\n}"
  20. s="${s//$'\r'/\\r}"
  21. s="${s//$'\t'/\\t}"
  22. printf '%s' "$s"
  23. }
  24. using_oac_escaped=$(escape_for_json "$using_oac_content")
  25. # Build skill catalogue from skills directory
  26. skill_catalogue=""
  27. if [ -d "${PLUGIN_ROOT}/skills" ]; then
  28. for skill_dir in "${PLUGIN_ROOT}/skills"/*/; do
  29. skill_name=$(basename "$skill_dir")
  30. skill_file="${skill_dir}SKILL.md"
  31. if [ -f "$skill_file" ]; then
  32. # Extract description from frontmatter
  33. description=$(grep -m1 '^description:' "$skill_file" 2>/dev/null | sed 's/^description: *//;s/^"//;s/"$//' || echo "")
  34. if [ -n "$description" ]; then
  35. skill_catalogue="${skill_catalogue}\n- oac:${skill_name} — ${description}"
  36. else
  37. skill_catalogue="${skill_catalogue}\n- oac:${skill_name}"
  38. fi
  39. fi
  40. done
  41. fi
  42. # Build warning message for first-time users
  43. warning_message=""
  44. if [[ ! -f "${PLUGIN_ROOT}/.context-manifest.json" ]] && [[ ! -f "$(pwd)/.context-manifest.json" ]]; then
  45. warning_message="\n\n<important-reminder>IN YOUR FIRST REPLY AFTER SEEING THIS MESSAGE YOU MUST TELL THE USER: **No context files found.** Invoke the install-context skill now to set up context for this project — it will check your project type and recommend the right profile. Say: \"I notice you don't have context files set up. Let me get that sorted for you.\" Then immediately run the install-context skill.</important-reminder>"
  46. fi
  47. warning_escaped=$(escape_for_json "$warning_message")
  48. skill_catalogue_escaped=$(escape_for_json "$skill_catalogue")
  49. # Build OAC system paths block
  50. PROTOCOL_PATH="${PLUGIN_ROOT}/skills/context-discovery/context-discovery-protocol.md"
  51. plugin_root_escaped=$(escape_for_json "$PLUGIN_ROOT")
  52. protocol_path_escaped=$(escape_for_json "$PROTOCOL_PATH")
  53. OAC_SYSTEM_PATHS="## OAC System Paths\n- Plugin Root: ${plugin_root_escaped}\n- Context Discovery Protocol: ${protocol_path_escaped}"
  54. # Build context string
  55. OAC_CONTEXT="<EXTREMELY_IMPORTANT>\nYou have OAC (OpenAgents Control).\n\n**Below is the full content of your 'oac:using-oac' skill — your introduction to using OAC skills. For all other skills, use the 'Skill' tool:**\n\n${using_oac_escaped}\n\n## Available OAC Skills (invoke with the Skill tool):\n${skill_catalogue_escaped}\n\n${OAC_SYSTEM_PATHS}\n\n${warning_escaped}\n</EXTREMELY_IMPORTANT>"
  56. # Output dual-format JSON for cross-tool compatibility
  57. # - additionalContext: Claude Code (hookSpecificOutput)
  58. # - additional_context: Cursor / OpenCode / other tools
  59. cat <<EOF
  60. {
  61. "additional_context": "${OAC_CONTEXT}",
  62. "hookSpecificOutput": {
  63. "hookEventName": "SessionStart",
  64. "additionalContext": "${OAC_CONTEXT}"
  65. }
  66. }
  67. EOF
  68. exit 0